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 c1906816d18704b19b324e1ca445be002a5b8a2a..8e4ec23e5f2c61bc10633dec3e5f07574bc8cafb 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 fe887d4662a907efc2f0c0fe04e7774fa7c06043..6da21a2bdf5d58a249e577cb3b95bba3093160e8 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);