diff --git a/en/application-dev/device-usage-statistics/device-usage-statistics-dev-guide.md b/en/application-dev/device-usage-statistics/device-usage-statistics-dev-guide.md index 06edad460749ab7ef5cb00bb820489a8368c01f8..c4e4fdf3850f4bd3b1b8f6902133835764414cb3 100644 --- a/en/application-dev/device-usage-statistics/device-usage-statistics-dev-guide.md +++ b/en/application-dev/device-usage-statistics/device-usage-statistics-dev-guide.md @@ -3,6 +3,7 @@ ## When to Use With device usage statistics APIs, you can have a better understanding of the application, notification, and system usage. For example, in application usage statistics, you can query the application usage, event log, and bundle group. + The application records (usage history statistics and event records) cached by components are updated to the database for persistent storage within 30 minutes after an event is reported. ## Available APIs @@ -19,9 +20,20 @@ import stats from '@ohos.bundleState'; | function queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void | Queries the application usage duration statistics based on the specified start time and end time.| | function queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void | Queries events of this application based on the specified start time and end time.| | function queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void | Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually).| -| function queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void | Queries the priority group of the current invoker application.| +| function queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void | Queries the priority group of this application. This API uses an asynchronous callback to return the result.| +| function queryAppUsagePriorityGroup(): Promise<number>; | Queries the priority group of this application. This API uses a promise to return the result.| | function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. | | function getRecentlyUsedModules(maxNum: number, callback: AsyncCallback<BundleActiveModuleInfo>): void | Obtains the number of FA usage records specified by **maxNum**.| +| function queryAppNotificationNumber(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveEventState>>): void | Queries the number of notifications from all applications based on the specified start time and end time.| +| function queryBundleActiveEventStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveEventState>>): void | Queries statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time.| +| function queryAppUsagePriorityGroup(bundleName? : string, callback: AsyncCallback<number>): void | Queries the priority group of the application specified by **bundleName**. If **bundleName** is not specified, the priority group of the current application is queried. This API uses an asynchronous callback to return the result.| +| function queryAppUsagePriorityGroup(bundleName? : string): Promise<number>; | Queries the priority group of the application specified by **bundleName**. If **bundleName** is not specified, the priority group of the current application is queried. This API uses a promise to return the result.| +| function setBundleGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback>boolean>): void | Sets the group for the application specified by **bundleName**. This API uses an asynchronous callback to return the result.| +| function setBundleGroup(bundleName : string, newGroup : GroupType): Promise>boolean>; | Sets the group for the application specified by **bundleName**. This API uses a promise to return the result.| +| function registerGroupCallBack(callback: Callback>BundleActiveGroupCallbackInfo>, callback: AsyncCallback>boolean>): void | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses an asynchronous callback to return the result.| +| function registerGroupCallBack(callback: Callback>BundleActiveGroupCallbackInfo>): Promise>boolean>; | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses a promise to return the result.| +| function unRegisterGroupCallBack(callback: AsyncCallback>boolean>): void | Deregisters the callback for application group changes. This API uses an asynchronous callback to return the result.| +| function unRegisterGroupCallBack(): Promise>boolean>; | Deregisters the callback for application group changes. This API uses a promise to return the result.| ## How to Develop @@ -44,7 +56,7 @@ import stats from '@ohos.bundleState'; ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.queryBundleActiveStates(0, 20000000000000).then( res => { console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.'); for (let i = 0; i < res.length; i++) { @@ -55,7 +67,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.queryBundleActiveStates(0, 20000000000000, (err, res) => { if (err) { console.log('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code); @@ -74,7 +86,7 @@ import stats from '@ohos.bundleState'; ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.queryBundleStateInfos(0, 20000000000000).then( res => { console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.'); let i = 1; @@ -87,7 +99,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.queryBundleStateInfos(0, 20000000000000, (err, res) => { if (err) { console.log('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code); @@ -108,7 +120,7 @@ import stats from '@ohos.bundleState'; ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.queryCurrentBundleActiveStates(0, 20000000000000).then( res => { console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise success.'); for (let i = 0; i < res.length; i++) { @@ -119,7 +131,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.queryCurrentBundleActiveStates(0, 20000000000000, (err, res) => { if (err) { console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code); @@ -138,7 +150,7 @@ import stats from '@ohos.bundleState'; ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.queryBundleStateInfoByInterval(0, 0, 20000000000000).then( res => { console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.'); for (let i = 0; i < res.length; i++) { @@ -149,7 +161,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.queryBundleStateInfoByInterval(0, 0, 20000000000000, (err, res) => { if (err) { console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code); @@ -163,19 +175,19 @@ import stats from '@ohos.bundleState'; }); ``` -6. Query the priority group of the current invoker application. This requires no permission to be configured in the **config.json** file. +6. Query the priority group of the current application. This requires no permission to be configured in the **config.json** file. ```js import stats from '@ohos.bundleState' - - // Use a promise to return the result. + + // Promise mode stats.queryAppUsagePriorityGroup().then( res => { console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res)); }).catch( err => { console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code); }); - - // Use an asynchronous callback to return the result. + + // Callback mode stats.queryAppUsagePriorityGroup((err, res) => { if (err) { console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code); @@ -184,20 +196,20 @@ import stats from '@ohos.bundleState'; } }); ``` - -7. Check whether the application specified by **bundleName** is in the idle state. This requires no permission to be configured in the **config.json** file. + +7. Check whether the application specified by **bundleName** is in the idle state. This requires no permission to be configured in the **config.json** file. A third-party application can only check the idle status of itself. ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.isIdleState("com.ohos.camera").then( res => { console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res)); }).catch( err => { console.log('BUNDLE_ACTIVE isIdleState promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.isIdleState("com.ohos.camera", (err, res) => { if (err) { console.log('BUNDLE_ACTIVE isIdleState callback failed, because: ' + err.code); @@ -212,7 +224,7 @@ import stats from '@ohos.bundleState'; ```js import stats from '@ohos.bundleState' - // Use a promise to return the result. + // Promise mode stats.getRecentlyUsedModules(1000).then( res => { console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise succeeded'); for (let i = 0; i < res.length; i++) { @@ -223,7 +235,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise failed, because: ' + err.code); }); - // Use a promise to return the result when maxNum is not specified. + // Promise mode when maxNum is not specified stats.getRecentlyUsedModules().then( res => { console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise succeeded'); for (let i = 0; i < res.length; i++) { @@ -234,7 +246,7 @@ import stats from '@ohos.bundleState'; console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise failed, because: ' + err.code); }); - // Use an asynchronous callback to return the result. + // Asynchronous callback mode stats.getRecentlyUsedModules(1000,(err, res) => { if(err) { console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback failed, because: ' + err.code); @@ -247,7 +259,7 @@ import stats from '@ohos.bundleState'; } }); - // Use an asynchronous callback to return the result when maxNum is not specified. + // Asynchronous callback mode when maxNum is not specified stats.getRecentlyUsedModules((err, res) => { if(err) { console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback failed, because: ' + err.code); @@ -260,3 +272,168 @@ import stats from '@ohos.bundleState'; } }); ``` + +9. Query the number of notifications from all applications based on the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured in the **config.json** file. + + ```js + import stats from '@ohos.bundleState' + + // Promise mode + stats.queryAppNotificationNumber(0, 20000000000000).then( res => { + console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise success.'); + console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise result ' + JSON.stringify(res)); + }).catch( err => { + console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise failed, because: ' + err.code); + }); + + // Asynchronous callback mode + stats.queryAppNotificationNumber(0, 20000000000000, (err, res) => { + if (err) { + console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback failed, because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback success.'); + console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback result ' + JSON.stringify(res)); + } + }); + ``` + +10. Query statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured in the **config.json** file. + + ```js + import stats from '@ohos.bundleState' + + // Promise mode + stats.queryBundleActiveEventStates(0, 20000000000000).then( res => { + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise success.'); + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise result ' + JSON.stringify(res)); + }).catch( err => { + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise failed, because: ' + err.code); + }); + + // Asynchronous callback mode + stats.queryBundleActiveEventStates(0, 20000000000000, (err, res) => { + if (err) { + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback failed, because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback success.'); + console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback result ' + JSON.stringify(res)); + } + }); + ``` + +11. Query the priority group of the current application. This requires no permission to be configured in the **config.json** file. Query the priority group of a specified application. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured in the **config.json** file. + + ```js + import stats from '@ohos.bundleState' + + // Promise mode without parameters + stats.queryAppUsagePriorityGroup().then( res => { + console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res)); + }).catch( err => { + console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code); + }); + + // Asynchronous callback mode without parameters + stats.queryAppUsagePriorityGroup((err, res) => { + if (err) { + console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback succeeded. result: ' + JSON.stringify(res)); + } + }); + + // Promise mode with parameters + stats.queryAppUsagePriorityGroup(this.bundleName).then( res => { + console.log('BUNDLE_ACTIVE QueryPackageGroup promise succeeded. result: ' + JSON.stringify(res)); + }).catch( err => { + console.log('BUNDLE_ACTIVE QueryPackageGroup promise failed. because: ' + err.code); + }); + + // Asynchronous callback mode with parameters + stats.queryAppUsagePriorityGroup(this.bundleName, (err, res) => { + if(err) { + console.log('BUNDLE_ACTIVE QueryPackageGroup callback failed. because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE QueryPackageGroup callback succeeded. result: ' + JSON.stringify(res)); + } + }); + ``` + +11. Set the group for the application specified by **bundleName**. + + ```javascript + import stats from '@ohos.bundleState' + + // Promise mode + stats.setBundleGroup(this.bundleName, this.newGroup).then( () => { + console.log('BUNDLE_ACTIVE SetBundleGroup promise succeeded.'); + }).catch( err => { + console.log('BUNDLE_ACTIVE SetBundleGroup promise failed. because: ' + err.code); + }); + // Asynchronous callback mode + stats.setBundleGroup(this.bundleName, this.newGroup, (err) => { + if(err) { + console.log('BUNDLE_ACTIVE SetBundleGroup callback failed. because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE SetBundleGroup callback succeeded.'); + } + }); + ``` + +12. Register a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. + + ```javascript + import stats from '@ohos.bundleState' + + // Promise mode + let onBundleGroupChanged = (err,res) =>{ + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.'); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result oldGroup is : ' + res.oldGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result newGroup is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result changeReason is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result userId is : ' + res.userId); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result bundleName is : ' + res.bundleName); + }; + stats.registerGroupCallBack(onBundleGroupChanged).then( () => { + console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise succeeded.'); + }).catch( err => { + console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise failed. because: ' + err.code); + }); + // Asynchronous callback mode + let onBundleGroupChanged = (err,res) =>{ + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.'); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's oldGroup is : ' + res.oldGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's newGroup is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's changeReason is : ' + res.newGroup); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's userId is : ' + res.userId); + console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's bundleName is : ' + res.bundleName); + }; + stats.registerGroupCallBack(onBundleGroupChanged, (err)=>{ + if(err) { + console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback failed, because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback success.'); + } + }); + ``` + +13. Deregister the callback for application group changes. + + ```javascript + import stats from '@ohos.bundleState' + + //promise + stats.unRegisterGroupCallBack().then( () => { + console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise succeeded.'); + }).catch( err => { + console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise failed. because: ' + err.code); + }); + //callback + stats.unRegisterGroupCallBack((err)=>{ + if(err) { + console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback failed, because: ' + err.code); + } else { + console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback success.'); + } + }); + ``` diff --git a/en/application-dev/device-usage-statistics/device-usage-statistics-overview.md b/en/application-dev/device-usage-statistics/device-usage-statistics-overview.md index 696a32c18ca974da852fa8600d126ca692d81f7d..e9d669f7973dce4360c617a6872fa3c86adf481f 100644 --- a/en/application-dev/device-usage-statistics/device-usage-statistics-overview.md +++ b/en/application-dev/device-usage-statistics/device-usage-statistics-overview.md @@ -12,22 +12,29 @@ Currently you can have access to statistics on the application usage, and notifi 3. Upon start of a new day - **The application usage statistics can include the following**: -1. Events of all applications based on the specified start time and end time +1. Events of all applications based on the specified start time and end time +2. Application usage duration statistics based on the specified start time and end time +3. Events of the current application based on the specified start time and end time +4. Application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually) +5. Priority group of the current invoker application +6. Whether a specific application is in the idle state +7. Number of FA usage records specified by **maxNum**, sorted by time (most recent first). If **maxNum** is not specified, the default value **1000** will be used. +8. Number of notifications from applications based on the specified start time and end time +9. Statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time +9. Priority group of the invoker application or a specified application -2. Application usage duration statistics based on the specified start time and end time +- **The setters can be used to:** -3. Events of the current application based on the specified start time and end time + Set the group for the application specified by **bundleName**. -4. Application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually) +- **The registration APIs can be used to:** -5. Priority group of the current invoker application + Register a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. -6. Whether a specific application is in the idle state +- **The deregistration APIs can be used to:** -7. The number of FA usage records specified by **maxNum**, sorted by time (most recent first) - - If **maxNum** is not specified, the default value **1000** will be used. + Deregister the callback for application group changes. ### Required Permissions -- The **queryBundleActiveStates**, **queryBundleStateInfos**, and **queryBundleStateInfoByInterval** APIs used for device usage statistics are system APIs. Before calling these APIs, you need to apply for the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. -- This permission is not required for calling **queryCurrentBundleActiveStates**, **queryAppUsagePriorityGroup**, and **isIdleState**, which are third-party APIs. +- Before calling the following system APIs, you need to apply for the **ohos.permission.BUNDLE_ACTIVE_INFO** permission: **queryBundleActiveStates**, **queryBundleStateInfos**, **queryBundleStateInfoByInterval**, **queryBundleActiveEventStates**, **queryAppNotificationNumber**, **queryAppUsagePriorityGroup(bundleName?)**, **setBundleGroup**, **registerGroupCallBack**, and **unRegisterGroupCallBack**. +- This permission is not required for calling third-party APIs: **queryCurrentBundleActiveStates**, **queryAppUsagePriorityGroup()**, and **isIdleState**.