• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2023-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 systemTime from '@ohos.systemTime';
17import usageStatistics from '@ohos.resourceschedule.usageStatistics';
18import {
19  NumberConstants,
20  Log
21} from '@ohos/common';
22
23const TAG = 'AppFormWeightManager';
24
25const ENTER_RECOMMEND_INTERVAL_DAYS = 1;
26
27/**
28 * FA卡片属性的APP权重管理接口
29 */
30export interface IAppFormWeightManager {
31  refreshUsageWeight(): Promise<void>;
32  getSortedAppUsageWeight(): ((string | number)[]) [];
33}
34
35/**
36 * Card APP Weight Manager Class
37 */
38export default class AppFormWeightManager implements IAppFormWeightManager {
39  private mAppUsageWeight: Map<string, number> = new Map();
40  private mRefreshTime: number = 0;
41
42  private constructor() {
43  }
44
45  /**
46   * getInstance
47   *
48   * @return Instance 单例
49   */
50  static getInstance(): AppFormWeightManager {
51    if (globalThis.AppFormWeightManager == null) {
52      globalThis.AppFormWeightManager = new AppFormWeightManager();
53    }
54    return globalThis.AppFormWeightManager;
55  }
56
57  /**
58   * 刷新权重
59   */
60  async refreshUsageWeight(): Promise<void> {
61    Log.showInfo(TAG, 'refreshUsageWeight start');
62    if (await this.isNeedRefresh()) {
63      this.mAppUsageWeight = new Map();
64      try {
65        let moduleInfoArr = await usageStatistics.queryModuleUsageRecords();
66        Log.showInfo(TAG, `queryModuleUsageRecords length ${moduleInfoArr.length}`);
67        for (let i: number = 0; i < moduleInfoArr.length; i++) {
68          this.mAppUsageWeight.set(moduleInfoArr[i].bundleName, moduleInfoArr[i].launchedCount);
69        }
70      } catch (error) {
71        Log.showError(TAG, `BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ${error.code}, message is:
72          ${error.message}`);
73      }
74    }
75  }
76
77  /**
78   * 是否更新数据:时间超过一天,刷新一次
79   */
80  private async isNeedRefresh(): Promise<boolean> {
81    let current: number = await systemTime.getCurrentTime();
82    let isNeedRefresh: boolean = false;
83    let isIntervalDaysExceedsThreshold: boolean = (current - this.mRefreshTime) >
84      NumberConstants.CONSTANT_DAY_TIME_MILLIS * ENTER_RECOMMEND_INTERVAL_DAYS;
85    if (isIntervalDaysExceedsThreshold) {
86      this.mRefreshTime = current;
87      isNeedRefresh = true;
88    }
89    return isNeedRefresh;
90  }
91
92  /**
93   * 获取app的使用权重.
94   *
95   * @return app的使用权重.
96   */
97  getSortedAppUsageWeight(): ((string | number)[]) [] {
98    if (this.mAppUsageWeight.size === 0) {
99      return new Array();
100    }
101    let mAppUsageWeightArr = Array.from(this.mAppUsageWeight);
102    mAppUsageWeightArr.sort((item1, item2) => {
103      return item2[1] - item1[1];
104    });
105    return mAppUsageWeightArr;
106  }
107}
108