diff --git a/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp b/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp index 7a4779205e5fbb762c7f47c1e4a8b0c3b47e60c7..b6436e2238d83acc719849784ca6916f34e97053 100644 --- a/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp +++ b/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp @@ -98,6 +98,7 @@ public: ani_object observer, ani_object etsBundleNameList); static ani_int OnOnApplicationState(ani_env *env, ani_string type, ani_object observer); static void OnOff(ani_env *env, ani_string type, ani_int etsObserverId, ani_object callback); + static void OffApplicationStateCheck(ani_env *env, ani_int etsObserverId); static void OnOnAppForegroundState(ani_env *env, ani_string type, ani_object observer); static void OnOffAppForegroundState(ani_env *env, ani_string type, ani_object observer); static void OnOnAbilityFirstFrameState( @@ -484,6 +485,26 @@ ani_int EtsAppManager::OnOnApplicationState(ani_env *env, ani_string type, ani_o return OnOnApplicationStateInner(env, type, observer, static_cast(undefined)); } +void EtsAppManager::OffApplicationStateCheck(ani_env *env, ani_int etsObserverId) +{ + TAG_LOGD(AAFwkTag::APPMGR, "OffApplicationStateCheck called"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null env"); + return; + } + if (appStateObserver_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null observer"); + AbilityRuntime::EtsErrorUtil::ThrowInvalidParamError(env, "observer is nullptr, please register first."); + return; + } + int32_t observerId = static_cast(etsObserverId); + if (!appStateObserver_->FindObserverByObserverId(observerId)) { + TAG_LOGE(AAFwkTag::APPMGR, "not find observer:%{public}d", static_cast(observerId)); + AbilityRuntime::EtsErrorUtil::ThrowInvalidParamError(env, "not find observerId."); + return; + } +} + void EtsAppManager::OnOff(ani_env *env, ani_string type, ani_int etsObserverId, ani_object callback) { TAG_LOGD(AAFwkTag::APPMGR, "OnOff called"); @@ -500,8 +521,7 @@ void EtsAppManager::OnOff(ani_env *env, ani_string type, ani_int etsObserverId, return; } TAG_LOGD(AAFwkTag::APPMGR, "observerId:%{public}d", etsObserverId); - int64_t observerId = static_cast(etsObserverId); - + int32_t observerId = static_cast(etsObserverId); sptr appMgr = GetAppManagerInstance(); if (appMgr == nullptr) { TAG_LOGE(AAFwkTag::APPMGR, "appManager null ptr"); @@ -1333,6 +1353,8 @@ void EtsAppManagerRegistryInit(ani_env *env) reinterpret_cast(EtsAppManager::OnOnApplicationState)}, ani_native_function {"nativeOff", APPLICATION_STATE_OFF_SIGNATURE, reinterpret_cast(EtsAppManager::OnOff)}, + ani_native_function {"nativeOffApplicationStateCheck", "I:V", + reinterpret_cast(EtsAppManager::OffApplicationStateCheck)}, ani_native_function {"nativeGetAppMemorySize", nullptr, reinterpret_cast(EtsAppManager::GetAppMemorySize)}, ani_native_function {"nativeIsRamConstrainedDevice", nullptr, diff --git a/frameworks/ets/ets/@ohos.app.ability.appManager.ets b/frameworks/ets/ets/@ohos.app.ability.appManager.ets index 836870f7f0f67317c085fb6caf0d777887962abc..5995889d83ffaa28974448b508fda4cadf525a20 100644 --- a/frameworks/ets/ets/@ohos.app.ability.appManager.ets +++ b/frameworks/ets/ets/@ohos.app.ability.appManager.ets @@ -225,7 +225,9 @@ export function on(type: 'applicationState', observer: ApplicationStateObserver, } export native function nativeOff(type:string, observerId: int, callback: AsyncCallbackWrapper) : void; +export native function nativeOffApplicationStateCheck(observerId: int): void; export function off(type: 'applicationState', observerId: int, callback: AsyncCallback): void { + nativeOffApplicationStateCheck(observerId); let myCall = new AsyncCallbackWrapper(callback); taskpool.execute((): void => { appManager.nativeOff(type, observerId, myCall); @@ -233,6 +235,7 @@ export function off(type: 'applicationState', observerId: int, callback: AsyncCa } export function off(type: 'applicationState', observerId: int): Promise { + nativeOffApplicationStateCheck(observerId); let p:Promise = new Promise((resolve: (data:undefined)=>void, reject:(err: BusinessError | null)=>void):void => { let myCall = new AsyncCallbackWrapper((err: BusinessError | null)=>{ if (err == null || err.code == 0) {