diff --git a/README.md b/README.md index 59fc520a7067b662261ff2bdd45675b07e6d02da..27262a6a75137c83aa85ca14dd89395460db56c6 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ Taking app usage interface as an example, the main exposed interfaces are as fol

Queries application usage duration statistics by time interval(callback).

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number): Promise<Array<BundleStateInfo>>

Queries application usage duration statistics by time interval(Promise).

+

queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void

+

Query the priority group of the application.

+

queryAppUsagePriorityGroup(): Promise<number>

+

Query the priority group of the application.

isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void

Judges whether the application of the specified bundle name is currently idle(callback).

isIdleState(bundleName: string): Promise<boolean>

@@ -80,17 +84,17 @@ Taking app usage interface as an example, the main exposed interfaces are as fol

Query the group of the current application or the application of specified bundlename.

queryAppUsagePriorityGroup(bundleName? : string): Promise<number>

Query the group of the current application or the application of specified bundlename.

-

setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback<boolean>): void

+

setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback<void>): void

Set the group of the specified application as the group provided by the parameter(callback).

-

setBundleGroup(bundleName: string, newGroup: GroupType): Promise<boolean>

+

setBundleGroup(bundleName: string, newGroup: GroupType): Promise<void>

Set the group of the specified application as the group provided by the parameter(Promise).

-

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>, callback: AsyncCallback<boolean>): void

+

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>, callback: AsyncCallback<void>): void

Register callback for application group change(callback).

-

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>): Promise<boolean>

+

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>): Promise<void>

Register callback for application group change(Promise).

-

unRegisterGroupCallBack(callback: AsyncCallback<boolean>): void

+

unRegisterGroupCallBack(callback: AsyncCallback<void>): void

Unregister the registered application group callback(callback).

-

unRegisterGroupCallBack(): Promise<boolean>

+

unRegisterGroupCallBack(): Promise<void>

Unregister the registered application group callback(Promise).

@@ -98,6 +102,7 @@ Taking app usage interface as an example, the main exposed interfaces are as fol + ### Usage Guidelines There are many interfaces for device usage statistics. Take app usage interface as an example to introduce the interface logic. @@ -111,13 +116,14 @@ There are many interfaces for device usage statistics. Take app usage interface >2. Query the usage duration of the application according to the start and end time; >3. Query the event collection of the current application according to the start and end time; >4. Query the usage duration of the application according to the type of interval (day, week, month, year) and the start and end time; ->5. Judge whether the specified application is currently idle; ->6. Query the statistical information of system events (sleep, wake-up, unlock and screen lock) according to the start and end time; ->7. Query the application notification times according to the start and end time; ->8. Query FA usage records. The maximum returned quantity does not exceed the value set by maxnum. FA usage records are sorted from near to far. The maximum maxnum is 1000. If the maxnum parameter is not filled in, the default maxnum is 1000; ->9. Query the group of the current application or the application of specified bundlename; ->10. Set the group of the specified application as the group provided by the parameter; ->11. Register callback for application group change; +>5. Query the priority group of the application; +>6. Judge whether the specified application is currently idle; +>7. Query the statistical information of system events (sleep, wake-up, unlock and screen lock) according to the start and end time; +>8. Query the application notification times according to the start and end time; +>9. Query FA usage records. The maximum returned quantity does not exceed the value set by maxnum. FA usage records are sorted from near to far. The maximum maxnum is 1000. If the maxnum parameter is not filled in, the default maxnum is 1000; +>10. Query the group of the current application or the application of specified bundlename; +>11. Set the group of the specified application as the group provided by the parameter; +>12. Register callback for application group change; >13. Unregister the registered application group callback; ## Repositories Involved diff --git a/README_zh.md b/README_zh.md index dd85e8697a6984dea36a8f37665b41f575ca4dbe..9fabc001ff5343b00ce71ff5b4a494c671daf42f 100644 --- a/README_zh.md +++ b/README_zh.md @@ -58,6 +58,10 @@

通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息(callback形式)。

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number): Promise<Array<BundleStateInfo>>

通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息(Promise形式)。

+

queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void

+

查询当前应用的优先级分组(callback形式)。

+

queryAppUsagePriorityGroup(): Promise<number>

+

查询当前应用的优先级分组(Promise形式)。

isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void

判断指定Bundle Name的应用当前是否是空闲状态(callback形式)。

isIdleState(bundleName: string): Promise<boolean>

@@ -78,17 +82,17 @@

使用可选参数查询当前调用者应用的使用优先级群组(callback形式)。

queryAppUsagePriorityGroup(bundleName? : string): Promise<number>

使用可选参数查询当前调用者应用的使用优先级群组(Promise形式)。

-

setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback<boolean>): void

+

setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback<void>): void

将指定应用的分组设置为参数提供的分组(callback形式)。

-

setBundleGroup(bundleName: string, newGroup: GroupType): Promise<boolean>

+

setBundleGroup(bundleName: string, newGroup: GroupType): Promise<void>

将指定应用的分组设置为参数提供的分组(Promise形式)。

-

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>, callback: AsyncCallback<boolean>): void

+

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>, callback: AsyncCallback<void>): void

给应用分组变化注册回调(callback形式)。

-

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>): Promise<boolean>

+

registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>): Promise<void>

给应用分组变化注册回调(Promise形式)。

-

unRegisterGroupCallBack(callback: AsyncCallback<boolean>): void

+

unRegisterGroupCallBack(callback: AsyncCallback<void>): void

将已注册过的应用分组回调解除注册(callback形式)。

-

unRegisterGroupCallBack(): Promise<boolean>

+

unRegisterGroupCallBack(): Promise<void>

将已注册过的应用分组回调解除注册(Promise形式)。

@@ -96,6 +100,7 @@ + ### 使用说明 设备使用信息统计接口众多,以应用使用详情(app usage)接口为例,介绍接口逻辑。 @@ -109,13 +114,14 @@ >2. 根据起止时间查询应用的使用时长; >3. 根据起止时间查询当前应用的事件集合; >4. 根据interval(日、周、月、年)类型和起止时间查询应用的使用时长; ->5. 判断指定应用当前是否是空闲状态; ->6. 根据起止时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息; ->7. 根据起止时间查询应用通知次数; ->8. 查询FA使用记录。返回数量最大不超过maxNum设置的值,FA使用记录由近及远排序,maxNum最大为1000,若不填写maxNum参数,则maxNum默认为1000; ->9. 查询当前应用或指定bundlename对应的应用的分组; ->10. 将指定应用的分组设置为参数提供的分组; ->11. 给应用分组变化注册回调; +>5. 查询当前应用的优先级分组; +>6. 判断指定应用当前是否是空闲状态; +>7. 根据起止时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息; +>8. 根据起止时间查询应用通知次数; +>9. 查询FA使用记录。返回数量最大不超过maxNum设置的值,FA使用记录由近及远排序,maxNum最大为1000,若不填写maxNum参数,则maxNum默认为1000; +>10. 查询当前应用或指定bundlename对应的应用的分组; +>11. 将指定应用的分组设置为参数提供的分组; +>12. 给应用分组变化注册回调; >13. 将已注册过的应用分组回调解除注册; ## 相关仓 diff --git a/frameworks/src/bundle_active_group_observer.cpp b/frameworks/src/bundle_active_group_observer.cpp index e30f620a7cb50596463b82378cc85832c6969463..1a15740efca198ca593a13ad09ae6ede90d1bbb6 100644 --- a/frameworks/src/bundle_active_group_observer.cpp +++ b/frameworks/src/bundle_active_group_observer.cpp @@ -48,6 +48,27 @@ void BundleActiveGroupObserver::SetCallbackInfo(const napi_env &env, const napi_ bundleGroupCallbackInfo_.ref = ref; } +template +napi_value AsyncInit(napi_env env, PARAMT ¶ms, ASYNCT* &asyncCallbackInfo) +{ + if (params.errorCode != ERR_OK) { + return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); + } + asyncCallbackInfo = new (std::nothrow) ASYNCT(env); + if (!asyncCallbackInfo) { + params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_NULLPTR; + return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); + } + if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) + != EOK) { + params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_INIT_FAILED; + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); + } + return BundleStateCommon::NapiGetNull(env); +} + napi_value SetBundleGroupChangedData(const CallbackReceiveDataWorker *commonEventDataWorkerData, napi_value &result) { BUNDLE_ACTIVE_LOGI("enter"); @@ -269,8 +290,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); + napi_get_null(env, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } }, @@ -310,32 +330,17 @@ napi_value UnRegisterGroupCallBack(napi_env env, napi_callback_info info) { 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 exist"); 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); - if (!asyncCallbackInfo) { - params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_NULLPTR; - return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); - } - if (memset_s(asyncCallbackInfo, sizeof(*asyncCallbackInfo), 0, sizeof(*asyncCallbackInfo)) - != EOK) { - params.errorCode = ERR_USAGE_STATS_ASYNC_CALLBACK_INIT_FAILED; - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - return BundleStateCommon::JSParaError(env, params.callback, params.errorCode); - } + AsyncUnRegisterCallbackInfo *asyncCallbackInfo = nullptr; + AsyncInit(env, params, asyncCallbackInfo); std::unique_ptr callbackPtr {asyncCallbackInfo}; callbackPtr->observer = registerObserver; BundleStateCommon::SettingAsyncWorkData(env, params.callback, *asyncCallbackInfo, promise); - BUNDLE_ACTIVE_LOGI("UnRegisterGroupCallBack will Async"); napi_value resourceName = nullptr; NAPI_CALL(env, napi_create_string_latin1(env, "UnRegisterGroupCallBack", NAPI_AUTO_LENGTH, &resourceName)); NAPI_CALL(env, napi_create_async_work(env, @@ -354,8 +359,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); + napi_get_null(env, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } }, diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index d0f35edbcf8e0fb6ac488c8990ed68af35c99334..b604db1f4a8cf4f25ead7bcb92d9c34966c42789 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -865,8 +865,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); + napi_get_null(env, &result); BundleStateCommon::GetCallbackPromiseResult(env, *asyncCallbackInfo, result); } }, diff --git a/interfaces/kits/bundlestats/js/@ohos.bundleState.d.ts b/interfaces/kits/bundlestats/js/@ohos.bundleState.d.ts index 05181789aab5090b0122af7922d79018fda1dc9c..2def2921ff3a1310fcf7debf92c7ae355184dd4f 100644 --- a/interfaces/kits/bundlestats/js/@ohos.bundleState.d.ts +++ b/interfaces/kits/bundlestats/js/@ohos.bundleState.d.ts @@ -177,6 +177,7 @@ declare namespace bundleState { /** * @since 9 * @syscap SystemCapability.ResourceSchedule.UsageStatistics.App + * @systemapi Hide this for inner system use. */ interface BundleActiveEventState { /** @@ -456,8 +457,8 @@ declare namespace bundleState { * @param newGroup,the group of the application whose name is bundleName. * @return Returns the result of setBundleGroup, true of false. */ - function setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback): void; - function setBundleGroup(bundleName: string, newGroup: GroupType): Promise; + function setBundleGroup(bundleName: string, newGroup: GroupType, callback: AsyncCallback): void; + function setBundleGroup(bundleName: string, newGroup: GroupType): Promise; /** * register callback to service. @@ -469,8 +470,8 @@ declare namespace bundleState { * @param Callback, callback when application group change,return the BundleActiveGroupCallbackInfo. * @return Returns BundleActiveGroupCallbackInfo when the group of bundle changed. the result of AsyncCallback is true or false. */ - function registerGroupCallBack(callback: Callback, callback: AsyncCallback): void; - function registerGroupCallBack(callback: Callback): Promise; + function registerGroupCallBack(callback: Callback, callback: AsyncCallback): void; + function registerGroupCallBack(callback: Callback): Promise; /** * unRegister callback from service. @@ -481,8 +482,8 @@ declare namespace bundleState { * @systemapi Hide this for inner system use. * @return Returns the result of unRegisterGroupCallBack, true of false. */ - function unRegisterGroupCallBack(callback: AsyncCallback): void; - function unRegisterGroupCallBack(): Promise; + function unRegisterGroupCallBack(callback: AsyncCallback): void; + function unRegisterGroupCallBack(): Promise; /* * Queries system event states data within a specified period identified by the start and end time.