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