diff --git a/frameworks/src/bundle_active_group_observer.cpp b/frameworks/src/bundle_active_group_observer.cpp index fc899656404e7c2b13c9daec4819a244ce33f544..1f5e9677cf5c6df50c83cc22f9472854ff80bd13 100644 --- a/frameworks/src/bundle_active_group_observer.cpp +++ b/frameworks/src/bundle_active_group_observer.cpp @@ -142,12 +142,12 @@ void BundleActiveGroupObserver::OnBundleGroupChanged(const BundleActiveGroupCall return; } - uv_work_t *work = new (std::nothrow) uv_work_t; + uv_work_t* work = new (std::nothrow) uv_work_t; if (!work) { BUNDLE_ACTIVE_LOGE("work is null"); return; } - CallbackReceiveDataWorker *callbackReceiveDataWorker = new (std::nothrow) CallbackReceiveDataWorker(); + CallbackReceiveDataWorker* callbackReceiveDataWorker = new (std::nothrow) CallbackReceiveDataWorker(); if (!callbackReceiveDataWorker) { BUNDLE_ACTIVE_LOGE("callbackReceiveDataWorker is null"); delete work; @@ -179,7 +179,6 @@ void BundleActiveGroupObserver::OnBundleGroupChanged(const BundleActiveGroupCall delete work; work = nullptr; } - BUNDLE_ACTIVE_LOGI("OnBundleGroupChanged end"); } napi_value GetBundleGroupChangeCallback( @@ -249,7 +248,7 @@ napi_value RegisterGroupCallBack(napi_env env, napi_callback_info info) params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_NULLPTR; return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } - if (memset_s(asyncCallbackInfo, sizeof(AsyncRegisterCallbackInfo), 0, sizeof(AsyncRegisterCallbackInfo)) + if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) != EOK) { params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_INIT_FAILED; delete asyncCallbackInfo; @@ -268,7 +267,7 @@ napi_value RegisterGroupCallBack(napi_env env, napi_callback_info info) [](napi_env env, void *data) { AsyncRegisterCallbackInfo *asyncCallbackInfo = (AsyncRegisterCallbackInfo *)data; if (asyncCallbackInfo) { - asyncCallbackInfo->state = + asyncCallbackInfo->errorCode = BundleActiveClient::GetInstance().RegisterGroupCallBack(asyncCallbackInfo->observer); } else { BUNDLE_ACTIVE_LOGE("RegisterGroupCallBack, asyncCallbackInfo == nullptr"); @@ -278,6 +277,7 @@ napi_value RegisterGroupCallBack(napi_env env, napi_callback_info info) AsyncRegisterCallbackInfo *asyncCallbackInfo = (AsyncRegisterCallbackInfo *)data; if (asyncCallbackInfo) { napi_value result = nullptr; + asyncCallbackInfo->state = (asyncCallbackInfo->errorCode == ERR_OK) ? true : false; napi_get_boolean(env, asyncCallbackInfo->state, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } @@ -317,16 +317,17 @@ napi_value ParseUnRegisterGroupCallBackParameters(const napi_env &env, const nap napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) { BUNDLE_ACTIVE_LOGI("enter UnRegisterGroupCallBack"); - if (!registerObserver) { - return BundleStateCommon::NapiGetNull(env); - } UnRegisterCallbackInfo params; ParseUnRegisterGroupCallBackParameters(env, info, params); if (params.errorCode != ERR_OK) { return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } - + if (!registerObserver) { + BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack observer is not"); + params.errorCode = ERR_REGISTER_OBSERVER_IS_NULL; + return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); + } napi_value promise = nullptr; AsyncUnRegisterCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncUnRegisterCallbackInfo(env); @@ -334,7 +335,7 @@ napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_NULLPTR; return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } - if (memset_s(asyncCallbackInfo, sizeof(AsyncUnRegisterCallbackInfo), 0, sizeof(AsyncUnRegisterCallbackInfo)) + if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) != EOK) { params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_INIT_FAILED; delete asyncCallbackInfo; @@ -353,7 +354,7 @@ napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) [](napi_env env, void *data) { AsyncUnRegisterCallbackInfo *asyncCallbackInfo = (AsyncUnRegisterCallbackInfo *)data; if (asyncCallbackInfo != nullptr) { - asyncCallbackInfo->state = + asyncCallbackInfo->errorCode = BundleActiveClient::GetInstance().UnregisterGroupCallBack(asyncCallbackInfo->observer); } else { BUNDLE_ACTIVE_LOGE("UnRegisterGroupCallBack, asyncCallbackInfo == nullptr"); @@ -363,6 +364,7 @@ napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) AsyncUnRegisterCallbackInfo *asyncCallbackInfo = (AsyncUnRegisterCallbackInfo *)data; if (asyncCallbackInfo != nullptr) { napi_value result = nullptr; + asyncCallbackInfo->state = (asyncCallbackInfo->errorCode == ERR_OK) ? true : false; napi_get_boolean(env, asyncCallbackInfo->state, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } @@ -370,6 +372,7 @@ napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork)); NAPI_CALL(env, napi_queue_async_work(env, callbackPtr->asyncWork)); + registerObserver = nullptr; if (callbackPtr->isCallback) { callbackPtr.release(); return BundleStateCommon::NapiGetNull(env); diff --git a/frameworks/src/bundle_state_common.cpp b/frameworks/src/bundle_state_common.cpp index c5efe405da6eaea29dd83ab3a6f63718efb1a362..dcefc9718700a24ca6d61b1a74c57e7330744866 100644 --- a/frameworks/src/bundle_state_common.cpp +++ b/frameworks/src/bundle_state_common.cpp @@ -67,7 +67,13 @@ void BundleStateCommon::SetCallbackInfo( napi_value resultout = nullptr; napi_get_reference_value(env, callbackIn, &callback); napi_value results[ARGS_TWO] = {nullptr}; - results[PARAM_FIRST] = GetErrorValue(env, errorCode); + if (errorCode == -1) { + results[PARAM_FIRST] = GetErrorValue(env, ERR_SERVICE_FAILED); + } else if (errorCode == 1) { + results[PARAM_FIRST] = GetErrorValue(env, ERR_REPEAT_OPERATION); + } else { + results[PARAM_FIRST] = GetErrorValue(env, errorCode); + } results[PARAM_SECOND] = result; NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM_FIRST], &resultout)); @@ -300,10 +306,19 @@ void BundleStateCommon::GetModuleRecordForResult(napi_env env, void BundleStateCommon::SetPromiseInfo(const napi_env &env, const napi_deferred &deferred, const napi_value &result, const int32_t &errorCode) { - if (errorCode == ERR_OK) { - napi_resolve_deferred(env, deferred, result); - } else { - napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); + switch (errorCode) { + case ERR_OK: + napi_resolve_deferred(env, deferred, result); + break; + case -1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_SERVICE_FAILED)); + break; + case 1: + napi_reject_deferred(env, deferred, GetErrorValue(env, ERR_REPEAT_OPERATION)); + break; + default: + napi_reject_deferred(env, deferred, GetErrorValue(env, errorCode)); + break; } } diff --git a/frameworks/src/bundle_state_init.cpp b/frameworks/src/bundle_state_init.cpp index 91a53b1ad89e43ee8265be11f4c5d6790332fd91..99dddd2fc59cfb91799c180d69570b240e069d73 100644 --- a/frameworks/src/bundle_state_init.cpp +++ b/frameworks/src/bundle_state_init.cpp @@ -24,7 +24,7 @@ EXTERN_C_START static const uint8_t ARG_FIRST = 1; -napi_ref intervalTypeConstructor_ = nullptr; +napi_ref typeConstructor_ = nullptr; /* * Module export function @@ -52,6 +52,7 @@ static napi_value BundleStateInit(napi_env env, napi_value exports) NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); InitIntervalType(env, exports); + InitGroupType(env, exports); return exports; } @@ -84,14 +85,54 @@ napi_value InitIntervalType(napi_env env, napi_value exports) }; napi_value result = nullptr; - napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumIntervalTypeConstructor, + napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumTypeConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); - napi_create_reference(env, result, refCount, &intervalTypeConstructor_); + napi_create_reference(env, result, refCount, &typeConstructor_); napi_set_named_property(env, exports, "IntervalType", result); return exports; } -napi_value EnumIntervalTypeConstructor(napi_env env, napi_callback_info info) +napi_value InitGroupType(napi_env env, napi_value exports) +{ + napi_value active_group_alive; + napi_value active_group_daily; + napi_value active_group_fixed; + napi_value active_group_rare; + napi_value active_group_limit; + napi_value active_group_never; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_ALIVE), + &active_group_alive); + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_DAILY), + &active_group_daily); + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_FIXED), + &active_group_fixed); + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_RARE), + &active_group_rare); + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_LIMIT), + &active_group_limit); + napi_create_uint32(env, static_cast(BundleStateCondition::GroupType::ACTIVE_GROUP_NEVER), + &active_group_never); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_ALIVE", active_group_alive), + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_DAILY", active_group_daily), + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_FIXED", active_group_fixed), + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_RARE", active_group_rare), + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_LIMIT", active_group_limit), + DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_NEVER", active_group_never), + }; + + napi_value result = nullptr; + napi_define_class(env, "GroupType", NAPI_AUTO_LENGTH, EnumTypeConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &typeConstructor_); + napi_set_named_property(env, exports, "GroupType", result); + return exports; +} + +napi_value EnumTypeConstructor(napi_env env, napi_callback_info info) { size_t argc = 0; napi_value args[ARG_FIRST] = {0}; diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index 7f449cbe1062b72e3914c7d60fd2854b0846179e..7fba2dd8f3d33a7b3c2c6283b7c51aaa3ca8ce3a 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -333,8 +333,12 @@ napi_value QueryAppUsagePriorityGroup(napi_env env, napi_callback_info info) [](napi_env env, napi_status status, void *data) { AsyncCallbackInfoPriorityGroup *asyncCallbackInfo = (AsyncCallbackInfoPriorityGroup *)data; if (asyncCallbackInfo) { + if (asyncCallbackInfo->priorityGroup == -1) { + asyncCallbackInfo->priorityGroup = ERR_SERVICE_FAILED; + } napi_value result = nullptr; napi_create_int32(env, asyncCallbackInfo->priorityGroup, &result); + BUNDLE_ACTIVE_LOGD("QueryPackageGroup, group is %{public}d", asyncCallbackInfo->priorityGroup); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } }, @@ -754,10 +758,11 @@ napi_value QueryBundleStateInfos(napi_env env, napi_callback_info info) (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork)); NAPI_CALL(env, napi_queue_async_work(env, callbackPtr->asyncWork)); - callbackPtr.release(); if (callbackPtr->isCallback) { + callbackPtr.release(); return BundleStateCommon::NapiGetNull(env); } else { + callbackPtr.release(); return promise; } } @@ -832,7 +837,7 @@ napi_value SetBundleGroup(napi_env env, napi_callback_info info) params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_NULLPTR; return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); } - if (memset_s(asyncCallbackInfo, sizeof(AsyncCallbackInfoSetBundleGroup), 0, sizeof(AsyncCallbackInfoSetBundleGroup)) + if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) != EOK) { params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_INIT_FAILED; delete asyncCallbackInfo; @@ -853,7 +858,7 @@ napi_value SetBundleGroup(napi_env env, napi_callback_info info) [](napi_env env, void *data) { AsyncCallbackInfoSetBundleGroup *asyncCallbackInfo = (AsyncCallbackInfoSetBundleGroup *)data; if (asyncCallbackInfo) { - asyncCallbackInfo->state = BundleActiveClient::GetInstance().SetBundleGroup( + asyncCallbackInfo->errorCode = BundleActiveClient::GetInstance().SetBundleGroup( asyncCallbackInfo->bundleName, asyncCallbackInfo->newGroup, asyncCallbackInfo->errorCode); } else { BUNDLE_ACTIVE_LOGE("SetBundleGroup, asyncCallbackInfo == nullptr"); @@ -863,6 +868,7 @@ napi_value SetBundleGroup(napi_env env, napi_callback_info info) AsyncCallbackInfoSetBundleGroup *asyncCallbackInfo = (AsyncCallbackInfoSetBundleGroup *)data; if (asyncCallbackInfo) { napi_value result = nullptr; + asyncCallbackInfo->state = (asyncCallbackInfo->errorCode == ERR_OK) ? true : false; napi_get_boolean(env, asyncCallbackInfo->state, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index 38bc5a72a3684bb0a7529ccfac24f3a3ff54ed4e..c7668f2e174f83f45ab54887bea7bd11b56ce94b 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -60,7 +60,7 @@ public: * parameters: bundleName, newGroup, userId * return : void */ - bool SetBundleGroup(std::string bundleName, const int32_t newGroup, int32_t errCode, int32_t userId = -1); + int32_t SetBundleGroup(std::string bundleName, const int32_t newGroup, int32_t errCode, int32_t userId = -1); /* * function: QueryCurrentPackageStats, query bundle usage statistics in specific time span for calling bundle. * parameters: intervalType, beginTime, endTime diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index cbe56c54a30f69cc69e94abaf8106d28f875ae73..a0b90b397062d4ae317c99858be4e47d09ecba20 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -57,7 +57,7 @@ public: * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId */ - bool SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) override; + int32_t SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) override; /* * function: QueryCurrentPackageStats, query bundle usage statistics in specific time span for calling bundle. * parameters: intervalType, beginTime, endTime @@ -111,13 +111,13 @@ public: * parameters: observer * return: result of RegisterGroupCallBack, true or false. */ - bool RegisterGroupCallBack(const sptr &observer) override; + int32_t RegisterGroupCallBack(const sptr &observer) override; /* * function: UnregisterGroupCallBack, remove the observer from groupObservers. * parameters: observer * return: result of UnregisterGroupCallBack, true or false. */ - bool UnregisterGroupCallBack(const sptr &observer) override; + int32_t UnregisterGroupCallBack(const sptr &observer) override; /* * function: ~BundleActiveProxy, default destructor. */ diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index e826996ec709984cb27f8cc3a52e725c6bdf968f..efaf269d0a9303142da7516057266a219d40ede8 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -85,11 +85,11 @@ std::vector BundleActiveClient::QueryEvents(const int64_t beg return bundleActiveProxy_->QueryEvents(beginTime, endTime, errCode, userId); } -bool BundleActiveClient::SetBundleGroup(std::string bundleName, const int32_t newGroup, +int32_t BundleActiveClient::SetBundleGroup(std::string bundleName, const int32_t newGroup, int32_t errCode, int32_t userId) { if (!GetBundleActiveProxy()) { - return false; + return -1; } return bundleActiveProxy_->SetBundleGroup(bundleName, newGroup, errCode, userId); } @@ -117,7 +117,6 @@ int32_t BundleActiveClient::QueryPackageGroup(std::string& bundleName, const int return -1; } int32_t result = bundleActiveProxy_->QueryPackageGroup(bundleName, userId); - BUNDLE_ACTIVE_LOGI("QueryPackageGroup group is %{public}d ", result); return result; } diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index ec934e6bb547ffb13dc400e58dfe4a006726bfc7..a48b4fa41f867546447b700561f8c96aa8fcd351 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -116,14 +116,15 @@ std::vector BundleActiveProxy::QueryEvents(const int64_t begi return result; } -bool BundleActiveProxy::SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) +int32_t BundleActiveProxy::SetBundleGroup(const std::string& bundleName, int32_t newGroup, + int32_t errCode, int32_t userId) { BUNDLE_ACTIVE_LOGI("SetBundleGroup enter bundleActiveProxy"); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(GetDescriptor())) { - return false; + return -1; } data.WriteString(bundleName); data.WriteInt32(newGroup); @@ -210,7 +211,6 @@ int32_t BundleActiveProxy::QueryPackageGroup(std::string& bundleName, const int3 data.WriteInt32(userId); Remote() -> SendRequest(QUERY_BUNDLE_GROUP, data, reply, option); int32_t result = reply.ReadInt32(); - BUNDLE_ACTIVE_LOGI("the result of QueryPackgeGroup is %{public}d", result); return result; } @@ -250,9 +250,9 @@ int32_t BundleActiveProxy::QueryFormStatistics(int32_t maxNum, std::vector &observer) +int32_t BundleActiveProxy::RegisterGroupCallBack(const sptr &observer) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack enter proxy---------------------"); + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack enter proxy"); if (!observer) { BUNDLE_ACTIVE_LOGE("RegisterGroupCallBack observer null"); return false; @@ -268,21 +268,20 @@ bool BundleActiveProxy::RegisterGroupCallBack(const sptrSendRequest(REGISTER_GROUP_CALLBACK, data, reply, option); if (ret!= ERR_OK) { BUNDLE_ACTIVE_LOGE("RegisterGroupCallBack SendRequest failed, error code: %{public}d", ret); } - return true; + return reply.ReadInt32(); } -bool BundleActiveProxy::UnregisterGroupCallBack(const sptr &observer) +int32_t BundleActiveProxy::UnregisterGroupCallBack(const sptr &observer) { if (!observer) { BUNDLE_ACTIVE_LOGE("observer null"); return false; } - BUNDLE_ACTIVE_LOGI("unRegisterApplicationStateObserver start"); + BUNDLE_ACTIVE_LOGI("UnregisterGroupCallBack start"); MessageParcel data; MessageParcel reply; MessageOption option; @@ -294,7 +293,7 @@ bool BundleActiveProxy::UnregisterGroupCallBack(const sptrSendRequest(UNREGISTER_GROUP_CALLBACK, data, reply, option); - return true; + return reply.ReadInt32(); } int32_t BundleActiveProxy::QueryEventStats(int64_t beginTime, int64_t endTime, diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_condition.h b/interfaces/kits/bundlestats/napi/include/bundle_state_condition.h index eb4dc7aee7ccb83f126ca1375943e8776af288ef..d051d6a16e7955dacea35afa18f063fc58d3e32e 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_condition.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_condition.h @@ -30,6 +30,15 @@ public: BY_MONTHLY, BY_ANNUALLY, }; + + enum GroupType { + ACTIVE_GROUP_ALIVE = 10, + ACTIVE_GROUP_DAILY = 20, + ACTIVE_GROUP_FIXED = 30, + ACTIVE_GROUP_RARE = 40, + ACTIVE_GROUP_LIMIT = 50, + ACTIVE_GROUP_NEVER = 60, + }; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_init.h b/interfaces/kits/bundlestats/napi/include/bundle_state_init.h index 7ac5260b0e8a45035b36464000ebeabc005e3012..8e65667bcfce6f98b9cc62e4a618ef0c9968bb8d 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_init.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_init.h @@ -28,7 +28,8 @@ extern "C" { __attribute__((constructor)) void RegisterModule(void); static napi_value BundleStateInit(napi_env env, napi_value exports); static napi_value InitIntervalType(napi_env env, napi_value exports); -static napi_value EnumIntervalTypeConstructor(napi_env env, napi_callback_info info); +static napi_value InitGroupType(napi_env env, napi_value exports); +static napi_value EnumTypeConstructor(napi_env env, napi_callback_info info); #ifdef __cplusplus } diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h index 3c48aecd34b4e7799b966fbbbb61b768553d48f0..fc4c9f6122c40cd39b20b5e937229ad6b87fe263 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h @@ -53,6 +53,9 @@ enum : int32_t { ERR_MODULE_STATS_MAXNUM_INVALID, ERR_EVENT_TYPE, ERR_EVENT_TYPE_NUMBER, + ERR_SERVICE_FAILED, + ERR_REPEAT_OPERATION, + ERR_REGISTER_OBSERVER_IS_NULL, }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 50e4662bf828194f0ab26956124fc9b341832fb7..4f61f0582506f96da6dedd91c163aa31faf3dd1f 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -165,7 +165,7 @@ public: // when user switched, restore old userdata. void OnUserSwitched(const int32_t userId); // force set app group. - bool SetBundleGroup(const std::string& bundleName, const int32_t newGroup, const int32_t userId); + int32_t SetBundleGroup(const std::string& bundleName, const int32_t newGroup, const int32_t userId); // get all user in device. void GetAllActiveUser(std::vector& activatedOsAccountIds); // when service stop, call it to unregister commen event and shutdown call back. @@ -177,14 +177,14 @@ public: * parameters: observer * return: result of RegisterGroupCallBack, true or false. */ - bool RegisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, + int32_t RegisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer); /* * function: UnregisterGroupCallBack, remove the observer from groupObservers. * parameters: observer * return: result of UnregisterGroupCallBack, true or false. */ - bool UnregisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, + int32_t UnregisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer); int32_t currentUsedUser_; void OnBundleGroupChanged(const BundleActiveGroupCallbackInfo& callbackInfo); diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 1b87a98b2ae749095226ab6568d4880206d47498..954a28bac8ff99909618a85869607f94f85feced 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -75,7 +75,7 @@ public: * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId */ - bool SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) override; + int32_t SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) override; /* * function: QueryCurrentPackageStats, query bundle usage statistics in specific time span for calling bundle. * parameters: intervalType, beginTime, endTime @@ -128,13 +128,13 @@ public: * parameters: observer * return: result of RegisterGroupCallBack, true or false. */ - bool RegisterGroupCallBack(const sptr &observer) override; + int32_t RegisterGroupCallBack(const sptr &observer) override; /* * function: UnregisterGroupCallBack, remove the observer from groupObservers. * parameters: observer * return: result of UnregisterGroupCallBack, true or false. */ - bool UnregisterGroupCallBack(const sptr &observer) override; + int32_t UnregisterGroupCallBack(const sptr &observer) override; protected: /** diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index a9de2d5dbe4b34f2260a675f9f2b92bf7a6b17e7..eff21f7fec82534d0480ce2f20a1df0ecf7eba59 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -98,7 +98,8 @@ public: * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId */ - virtual bool SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) = 0; + virtual int32_t SetBundleGroup(const std::string& bundleName, int32_t newGroup, + int32_t errCode, int32_t userId) = 0; /* * function: QueryFormStatistics, query all from usage statistics in specific time span for calling user. * parameters: maxNum, results, userId, default userId is -1 for JS API, @@ -108,8 +109,8 @@ public: virtual int32_t QueryFormStatistics(int32_t maxNum, std::vector& results, int32_t userId) = 0; - virtual bool RegisterGroupCallBack(const sptr &observer) = 0; - virtual bool UnregisterGroupCallBack(const sptr &observer) = 0; + virtual int32_t RegisterGroupCallBack(const sptr &observer) = 0; + virtual int32_t UnregisterGroupCallBack(const sptr &observer) = 0; /* * function: QueryEventStats, query all from event stats in specific time span for calling user. diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 6eadd998bc92b3233e476a1020ac8b404461f85e..df29fad79c24ef6410c6197e502011af3035d4ef 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -649,7 +649,7 @@ int32_t BundleActiveCore::QueryAppNotificationNumber(int64_t beginTime, int64_t return errCode; } -bool BundleActiveCore::SetBundleGroup(const std::string& bundleName, const int32_t newGroup, const int32_t userId) +int32_t BundleActiveCore::SetBundleGroup(const std::string& bundleName, const int32_t newGroup, const int32_t userId) { int32_t newReason = GROUP_CONTROL_REASON_FORCED; sptr timer = MiscServices::TimeServiceClient::GetInstance(); @@ -717,38 +717,36 @@ void BundleActiveCore::OnBundleGroupChanged(const BundleActiveGroupCallbackInfo& } } -bool BundleActiveCore::RegisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, +int32_t BundleActiveCore::RegisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack enter BundleActiveCore, return"); std::lock_guard lock(callbackMutex_); if (!observer) { - BUNDLE_ACTIVE_LOGI("observer is null, return"); - return false; + return -1; } auto item = groupChangeObservers_.find(tokenId); if (item != groupChangeObservers_.end()) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack observer exist, return"); - return false; + return 1; } groupChangeObservers_.emplace(tokenId, observer); AddObserverDeathRecipient(observer); BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack number is %{public}d", static_cast(groupChangeObservers_.size())); - return true; + return 0; } -bool BundleActiveCore::UnregisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, +int32_t BundleActiveCore::UnregisterGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { std::lock_guard lock(callbackMutex_); auto item = groupChangeObservers_.find(tokenId); if (item == groupChangeObservers_.end()) { BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack observer is not exist, return"); - return false; + return 1; } - groupChangeObservers_.erase(tokenId); RemoveObserverDeathRecipient(item->second); - return true; + groupChangeObservers_.erase(tokenId); + return 0; } void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 6113387904d90a9587bf6ec7a8fb107f0675cc56..8fec918d58dbf55019cef1d4e7cba83c32b9ec1b 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -327,10 +327,10 @@ std::vector BundleActiveService::QueryEvents(const int64_t be return result; } -bool BundleActiveService::SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, +int32_t BundleActiveService::SetBundleGroup(const std::string& bundleName, int32_t newGroup, int32_t errCode, int32_t userId) { - bool result = false; + bool result = -1; // get uid int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); @@ -435,25 +435,23 @@ int32_t BundleActiveService::QueryPackageGroup(std::string& bundleName, int32_t std::string g_bundleName = ""; sptrBundleMgr_->GetBundleNameForUid(callingUid, g_bundleName); bundleName = g_bundleName; - } - if (!bundleName.empty()) { + result = bundleActiveCore_->QueryPackageGroup(bundleName, userId); + } else { AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); if (CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId, errCode) || AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) == AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE) { - BUNDLE_ACTIVE_LOGD("QueryPackageGroup bundleName is %{public}s, userId is %{public}d", - bundleName.c_str(), userId); - result = bundleActiveCore_->QueryPackageGroup(bundleName, userId); + result = bundleActiveCore_->QueryPackageGroup(bundleName, userId); } } } return result; } -bool BundleActiveService::RegisterGroupCallBack(const sptr &observer) +int32_t BundleActiveService::RegisterGroupCallBack(const sptr &observer) { BUNDLE_ACTIVE_LOGD("RegisterGroupCallBack enter bundleService"); - int result = false; + int result = -1; if (!bundleActiveCore_) { return result; } @@ -467,9 +465,9 @@ bool BundleActiveService::RegisterGroupCallBack(const sptr &observer) +int32_t BundleActiveService::UnregisterGroupCallBack(const sptr &observer) { - int32_t result = false; + int32_t result = -1; if (!bundleActiveCore_) { return result; } @@ -477,11 +475,11 @@ bool BundleActiveService::UnregisterGroupCallBack(const sptrUnregisterGroupCallBack(tokenId, observer); } - return result; } @@ -531,10 +529,10 @@ bool BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_t u int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); if (bundleHasPermission != 0) { errCode = bundleHasPermission; - BUNDLE_ACTIVE_LOGE("%{public}s hasn't permission", bundleName.c_str()); + BUNDLE_ACTIVE_LOGE("UnRegisterGroupCallBack %{public}s hasn't permission", bundleName.c_str()); return false; } else { - BUNDLE_ACTIVE_LOGI(" %{public}s has permission %{public}d", + BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack %{public}s has permission %{public}d", bundleName.c_str(), bundleHasPermission); return true; } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index 0fe896e587781671213609aeb882bbb97070ae3a..6cfa429189b9ddd7f6be169c5c849a588f2284f2 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -63,7 +63,7 @@ public: void OnUserRemoved(const int32_t userId); void OnBundleUninstalled(const int32_t userId, const std::string bundleName); void OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp); - bool SetBundleGroup(const std::string& bundleName, const int32_t userId, int32_t newGroup, uint32_t reason, + int32_t SetBundleGroup(const std::string& bundleName, const int32_t userId, int32_t newGroup, uint32_t reason, const int64_t bootBasedTimeStamp); void RestoreToDatabase(const int32_t userId); void RestoreDurationToDatabase(); diff --git a/services/packagegroup/include/bundle_active_user_history.h b/services/packagegroup/include/bundle_active_user_history.h index c7e3e5dcef1b1a6983df685021012f70f94dbe00..b6617019d68261668410cb7a938b58a2c6ae9c93 100644 --- a/services/packagegroup/include/bundle_active_user_history.h +++ b/services/packagegroup/include/bundle_active_user_history.h @@ -51,7 +51,7 @@ public: void ReportUsage(std::shared_ptr oneBundleUsageHistory, const std::string& bundleName, const int32_t newGroup, const uint32_t groupReason, const int64_t bootBasedTimeStamp, const int64_t timeUntilNextCheck, const int32_t userId); - bool SetBundleGroup(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp, + int32_t SetBundleGroup(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp, int32_t newGroup, uint32_t groupReason); int32_t GetLevelIndex(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp, const std::vector screenTimeLeve, const std::vector bootFromTimeLevel); diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 2f66c2b2047454e7555ec055a768b4957ff250cf..62ec880abb279edf63b12dd9bf26f2e451915543 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -314,16 +314,16 @@ void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleN } } -bool BundleActiveGroupController::SetBundleGroup(const std::string& bundleName, const int32_t userId, +int32_t BundleActiveGroupController::SetBundleGroup(const std::string& bundleName, const int32_t userId, int32_t newGroup, uint32_t reason, const int64_t bootBasedTimeStamp) { std::lock_guard lock(mutex_); if (!IsBundleInstalled(bundleName, userId)) { - return false; + return -1; } auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName, userId, bootBasedTimeStamp, true); if (!oneBundleHistory) { - return false; + return -1; } return bundleUserHistory_->SetBundleGroup(bundleName, userId, bootBasedTimeStamp, newGroup, reason); } @@ -350,7 +350,10 @@ int32_t BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, int32_t BundleActiveGroupController::QueryPackageGroup(const std::string& bundleName, const int32_t userId) { - BUNDLE_ACTIVE_LOGI("QueryPackageGroup called"); + if (bundleName.empty()) { + BUNDLE_ACTIVE_LOGE("bundleName can not get by userId"); + return -1; + } sptr timer = MiscServices::TimeServiceClient::GetInstance(); if (!IsBundleInstalled(bundleName, userId)) { BUNDLE_ACTIVE_LOGI("QueryPackageGroup is not bundleInstalled"); @@ -362,7 +365,7 @@ int32_t BundleActiveGroupController::QueryPackageGroup(const std::string& bundle if (!oneBundleHistory) { return -1; } - BUNDLE_ACTIVE_LOGI("QueryPackageGroup group is %{public}d ", oneBundleHistory->currentGroup_); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup group is %{public}d", oneBundleHistory->currentGroup_); return oneBundleHistory->currentGroup_; } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index c166ecaef375093286ae67cc4e30eee0286e34d4..bf6bb1713b6a1919ba744cc7c698b122bb4cb9fa 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -194,16 +194,19 @@ void BundleActiveUserHistory::ReportUsage(shared_ptr oneBundleUsageHistory->reasonInGroup_ = GROUP_CONTROL_REASON_USAGE | groupReason; oneBundleUsageHistory->isChanged_ = true; BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack will ReportUsage"); - BundleActiveGroupCallbackInfo callbackInfo( - userId, oldGroup, newGroup, oneBundleUsageHistory->reasonInGroup_, bundleName); - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack BundleActiveGroupCallbackInfo build success"); - if (!bundleActiveCore_.expired()) { - BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack will callback!"); - bundleActiveCore_.lock()->OnBundleGroupChanged(callbackInfo); + bool isGroupChanged = (oldGroup == newGroup) ? true : false; + if (!isGroupChanged) { + BundleActiveGroupCallbackInfo callbackInfo( + userId, oldGroup, newGroup, oneBundleUsageHistory->reasonInGroup_, bundleName); + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack BundleActiveGroupCallbackInfo build success"); + if (!bundleActiveCore_.expired()) { + BUNDLE_ACTIVE_LOGI("RegisterGroupCallBack will callback!"); + bundleActiveCore_.lock()->OnBundleGroupChanged(callbackInfo); + } } } -bool BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int32_t userId, +int32_t BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp, int32_t newGroup, uint32_t groupReason) { std::lock_guard lock(setGroupMutex_); @@ -211,28 +214,31 @@ bool BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int bundleName.c_str(), newGroup, groupReason, userId); shared_ptr>> userBundleHistory = GetUserHistory(userId, false); if (!userBundleHistory) { - return false; + return -1; } shared_ptr oneBundleHistory = GetUsageHistoryInUserHistory(userBundleHistory, bundleName, bootBasedTimeStamp, false); if (!oneBundleHistory) { - return false; + return -1; } if (oneBundleHistory->currentGroup_ == newGroup && oneBundleHistory->reasonInGroup_ == groupReason) { BUNDLE_ACTIVE_LOGI("%{public}s group and reason is same as before, not update", bundleName.c_str()); - return false; + return 1; } int32_t oldGroup = oneBundleHistory->currentGroup_; oneBundleHistory->currentGroup_ = newGroup; oneBundleHistory->reasonInGroup_ = groupReason; oneBundleHistory->isChanged_ = true; BUNDLE_ACTIVE_LOGI("SetBundleGroup set success"); - BundleActiveGroupCallbackInfo callbackInfo( - userId, oldGroup, newGroup, oneBundleHistory->reasonInGroup_, bundleName); - if (!bundleActiveCore_.expired()) { - bundleActiveCore_.lock()->OnBundleGroupChanged(callbackInfo); + bool isGroupChanged = (oldGroup == newGroup) ? true : false; + if (!isGroupChanged) { + BundleActiveGroupCallbackInfo callbackInfo( + userId, oldGroup, newGroup, oneBundleHistory->reasonInGroup_, bundleName); + if (!bundleActiveCore_.expired()) { + bundleActiveCore_.lock()->OnBundleGroupChanged(callbackInfo); + } } - return true; + return 0; } void BundleActiveUserHistory::UpdateBootBasedAndScreenTime(const bool& isScreenOn, const int64_t bootBasedTimeStamp,