• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import usageStatistics from '@ohos.resourceschedule.usageStatistics'
17import AppTime from '../model/AppTime'
18import Logger from './Logger'
19import { AppInfo, appInit } from '../model/AppInfo'
20import { getAppName, getAppIcon, getTimStr } from './Util'
21
22const TAG: string = 'BundleStateUtil'
23const BEGIN_HOURS: number = 0
24const BEGIN_MINUTES: number = 0
25const BEGIN_SECONDS: number = 0
26const END_HOURS: number = 23
27const END_MINUTES: number = 59
28const END_SECONDS: number = 59
29
30export class BundleStateUtil {
31  private apps: Array<AppInfo> = appInit()
32  private freeApps: Array<AppInfo> = []
33
34  /**
35   * 获取不常用应用列表
36   */
37  async getFreeAppList(): Promise<Array<AppInfo>> {
38    for (let appsKey in this.apps) {
39      let bundleName = this.apps[appsKey].bundleName
40      let isIdleState = await usageStatistics.isIdleState(bundleName)
41      if (isIdleState) {
42        this.freeApps.push({ bundleName: bundleName, name: getAppName(bundleName), icon: getAppIcon(bundleName) })
43      }
44      Logger.info(TAG, `freeApps=${JSON.stringify(this.freeApps)}`)
45    }
46    return this.freeApps
47  }
48
49  /**
50   * 获取所有应用使用时间列表
51   */
52  async getTotalAppList(): Promise<Array<AppTime>> {
53    let dateBegin = new Date()
54    // 设置开始时间为00:00:00
55    dateBegin.setHours(BEGIN_HOURS)
56    dateBegin.setMinutes(BEGIN_MINUTES)
57    dateBegin.setSeconds(BEGIN_SECONDS)
58    Logger.info(TAG, `dateBegin= ${getTimStr(dateBegin)} ${dateBegin.toString()}`)
59    let dateEnd = new Date()
60    // 设置结束时间为23:59:59
61    dateEnd.setHours(END_HOURS)
62    dateEnd.setMinutes(END_MINUTES)
63    dateEnd.setSeconds(END_SECONDS)
64    Logger.info(TAG, `dateEnd= ${getTimStr(dateEnd)} ${dateEnd.toString()}`)
65    let res = await usageStatistics.queryBundleStatsInfos(Date.parse(dateBegin.toString()), Date.parse(dateEnd.toString()))
66    Logger.info(TAG, `queryBundleStateInfos promise success`)
67    let list: Array<AppTime> = []
68    for (let key in res) {
69      if (res.hasOwnProperty(key)) {
70        Logger.info(TAG, `queryBundleStateInfos promise result ${JSON.stringify(res[key])}`)
71        let appTime = new AppTime(res[key].bundleName, res[key].abilityPrevAccessTime, res[key].abilityInFgTotalTime)
72        list.push(appTime)
73      }
74    }
75    return list
76  }
77}
78