From bf821a74595dd4b0fd36a4bfaf8897f7a106ef13 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Mon, 4 Aug 2025 17:02:59 +0800 Subject: [PATCH] fix getForegroundUIAbilities callBack Signed-off-by: zhangzezhong --- .../src/ets_ability_manager.cpp | 38 +++++++++++++++++++ .../ets/@ohos.app.ability.abilityManager.ets | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp b/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp index c1906816d18..8e4ec23e5f2 100644 --- a/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp +++ b/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp @@ -79,6 +79,40 @@ static ani_object GetForegroundUIAbilities(ani_env *env) return aniArray; } +void GetForegroundUIAbilitiesCallBack(ani_env *env, ani_object callbackObj) +{ + TAG_LOGD(AAFwkTag::ABILITYMGR, "call GetForegroundUIAbilitiesCallBack"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null env"); + return; + } + + sptr abilityManager = GetAbilityManagerInstance(); + if (abilityManager == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityManager is null"); + AppExecFwk::AsyncCallback(env, callbackObj, + EtsErrorUtil::CreateError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER), nullptr); + return; + } + std::vector list; + int32_t ret = abilityManager->GetForegroundUIAbilities(list); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "failed: ret=%{public}d", ret); + AbilityRuntime::AbilityErrorCode code = AbilityRuntime::GetJsErrorCodeByNativeError(ret); + AppExecFwk::AsyncCallback(env, callbackObj, EtsErrorUtil::CreateError(env, code), nullptr); + return; + } + TAG_LOGD(AAFwkTag::ABILITYMGR, "GetForegroundUIAbilities succeeds, list.size=%{public}zu", list.size()); + ani_object aniArray = AppExecFwk::CreateAniAbilityStateDataArray(env, list); + if (aniArray == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null aniArray"); + AppExecFwk::AsyncCallback(env, callbackObj, + EtsErrorUtil::CreateError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER), nullptr); + return; + } + AppExecFwk::AsyncCallback(env, callbackObj, EtsErrorUtil::CreateErrorByNativeErr(env, ERR_OK), aniArray); +} + static void GetTopAbility(ani_env *env, ani_object callback) { TAG_LOGD(AAFwkTag::ABILITYMGR, "call GetTopAbility"); @@ -127,6 +161,10 @@ void EtsAbilityManagerRegistryInit(ani_env *env) "nativeGetForegroundUIAbilities", ETS_ABILITY_MANAGER_SIGNATURE_ARRAY, reinterpret_cast(GetForegroundUIAbilities) }, + ani_native_function { + "getForegroundUIAbilitiesCallback", "Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + reinterpret_cast(GetForegroundUIAbilitiesCallBack) + }, ani_native_function {"nativeGetTopAbility", ETS_ABILITY_MANAGER_SIGNATURE_CALLBACK, reinterpret_cast(GetTopAbility)}, }; diff --git a/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets b/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets index fe887d4662a..6da21a2bdf5 100644 --- a/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets +++ b/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets @@ -41,7 +41,7 @@ export default namespace abilityManager { return p; } - export function getForegroundUIAbilities(callback: AsyncCallback, void>): void { + export function getForegroundUIAbilities(callback: AsyncCallback>): void { let myCall = new AsyncCallbackWrapper>(callback); taskpool.execute((): void => { abilityManager.getForegroundUIAbilitiesCallback(myCall); -- Gitee