• 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 bundleManager from '@ohos.bundle.bundleManager';
17import { AppItemInfo } from '../bean/AppItemInfo';
18import { CheckEmptyUtils } from '../utils/CheckEmptyUtils';
19import { CommonConstants } from '../constants/CommonConstants';
20import { ResourceManager } from './ResourceManager';
21import commonBundleManager from './CommonBundleManager';
22import { Log } from '../utils/Log';
23
24const TAG = 'AtomicServiceAbilityManager';
25
26/**
27 * 原服务管理
28 */
29class AtomicServiceAbilityManager {
30  private readonly mAtomicServiceAppMap = new Map<string, AppItemInfo>();
31  private static mInstance: AtomicServiceAbilityManager;
32
33  /**
34   * 获取桌面应用信息管理对象
35   *
36   * @return 桌面应用信息管理对象单一实例
37   */
38  static getInstance(): AtomicServiceAbilityManager {
39    if (AtomicServiceAbilityManager.mInstance == null) {
40      AtomicServiceAbilityManager.mInstance = new AtomicServiceAbilityManager();
41      globalThis.AtomicServiceAbilityManagerInstance = AtomicServiceAbilityManager.mInstance;
42    }
43    Log.showInfo(TAG, 'getInstance!');
44    return AtomicServiceAbilityManager.mInstance;
45  }
46
47  private constructor() {
48  }
49
50  /**
51   * 获取userId.
52   */
53  getUserId(): number {
54    return commonBundleManager.getUserId();
55  }
56
57  /**
58   * 从包管理获取所有的原服务应用信息
59   *
60   * @returns 所有的原服务应用信息
61   */
62  async getAtomicServiceAbilityList(): Promise<AppItemInfo[]> {
63    let abilityList: Array<bundleManager.AbilityInfo> = await commonBundleManager.getAllAbilityList(bundleManager.BundleType.ATOMIC_SERVICE);
64    let appItemInfoList: AppItemInfo[] = [];
65    if (CheckEmptyUtils.isEmptyArr(abilityList)) {
66      return appItemInfoList;
67    }
68    for (let i = 0; i < abilityList.length; i++) {
69      let appItem: AppItemInfo = await this.convertAtomicServiceToAppItemInfo(abilityList[i]);
70      if (!CheckEmptyUtils.isEmpty(appItem)) {
71        appItemInfoList.push(appItem);
72      }
73    }
74    return appItemInfoList;
75  }
76
77  /**
78   * 从包管理获取应用信息
79   *
80   * @param bundleName 包名
81   * @returns 应用信息
82   */
83  async getAtomicServiceAbilityInfoAsync(bundleName: string): Promise<AppItemInfo[]> {
84    if (CheckEmptyUtils.checkStrIsEmpty(bundleName)) {
85      Log.showError(TAG, 'getAtomicServiceAbilityInfoAsync reqParam bundleName is empty');
86      return [];
87    }
88    let bundleInfo: bundleManager.BundleInfo = await commonBundleManager.getBundleInfoByBundleName(bundleName, bundleManager.BundleType.ATOMIC_SERVICE);
89    if (CheckEmptyUtils.isEmpty(bundleInfo)) {
90      Log.showInfo(TAG, `getAtomicServiceAbilityInfoAsync by bundleName:${bundleName} no result from MGR`);
91      return [];
92    }
93    let appItemInfoList :AppItemInfo[] = [];
94    if (CheckEmptyUtils.isEmptyArr(bundleInfo.hapModulesInfo)) {
95      return appItemInfoList;
96    }
97    for (let i = 0; i < bundleInfo.hapModulesInfo.length; i++) {
98      if (CheckEmptyUtils.isEmptyArr(bundleInfo.hapModulesInfo[i].abilitiesInfo)) {
99        continue;
100      }
101      for (let j = 0; j < bundleInfo.hapModulesInfo[i].abilitiesInfo.length; j++) {
102        let appItem: AppItemInfo = await this.convertAtomicServiceToAppItemInfo(bundleInfo.hapModulesInfo[i].abilitiesInfo[j], bundleInfo.appInfo);
103        if (!CheckEmptyUtils.isEmpty(appItem)) {
104          appItemInfoList.push(appItem);
105        }
106      }
107    }
108    return appItemInfoList;
109  }
110
111  /**
112   * 从缓存中获取或包管理获取原服务ability信息
113   *
114   * @param bundleName 包名
115   * @returns 一个ability信息
116   */
117  async getAnAtomicServiceAbilityInfoFromCache(bundleName: string): Promise<AppItemInfo | undefined> {
118    let appItemInfo: AppItemInfo | undefined = undefined;
119    if (CheckEmptyUtils.checkStrIsEmpty(bundleName)) {
120      Log.showError(TAG, 'getAnAtomicServiceAbilityInfoFromCache reqParam bundleName is empty');
121      return appItemInfo;
122    }
123    // get from cache
124    if (this.mAtomicServiceAppMap != null && this.mAtomicServiceAppMap.has(bundleName)) {
125      appItemInfo = this.mAtomicServiceAppMap.get(bundleName);
126    }
127    if (!CheckEmptyUtils.isEmpty(appItemInfo)) {
128      Log.showInfo(TAG, `getAnAtomicServiceAbilityInfoFromCache cache result: ${JSON.stringify(appItemInfo)}`);
129      return appItemInfo;
130    }
131    // get from mgr
132    let abilityList: AppItemInfo[] = await this.getAtomicServiceAbilityInfoAsync(bundleName);
133    if (CheckEmptyUtils.isEmptyArr(abilityList)) {
134      Log.showInfo(TAG, `${bundleName} has no atomic ability`);
135      return undefined;
136    }
137    Log.showInfo(TAG, `getAnAtomicServiceAbilityInfoFromCache from MGR: ${JSON.stringify(abilityList[0])}`);
138    return abilityList[0];
139  }
140
141  private async convertAtomicServiceToAppItemInfo(info: bundleManager.AbilityInfo,
142                                                  applicationInfo?: bundleManager.ApplicationInfo): Promise<AppItemInfo | undefined> {
143    if (CheckEmptyUtils.isEmpty(info)) {
144      Log.showError(TAG, 'convertAtomicServiceToAppItemInfo reqParam is empty');
145      return undefined;
146    }
147    let appInfo: bundleManager.ApplicationInfo = info.applicationInfo;
148    if (CheckEmptyUtils.isEmpty(appInfo)) {
149      appInfo = applicationInfo;
150    }
151    if (CheckEmptyUtils.isEmpty(appInfo)) {
152      Log.showError(TAG, 'convertAtomicServiceToAppItemInfo applicationInfo is empty');
153      return undefined;
154    }
155    const appItemInfo = new AppItemInfo();
156    appItemInfo.appName = await ResourceManager.getInstance().getAppNameSync(
157      appInfo.labelId, info.bundleName, info.moduleName, appInfo.label
158    );
159    appItemInfo.isSystemApp = appInfo.systemApp;
160    appItemInfo.isUninstallAble = appInfo.removable;
161    appItemInfo.appIconId = appInfo.iconId;
162    appItemInfo.appLabelId = appInfo.labelId;
163    appItemInfo.bundleName = info.bundleName;
164    appItemInfo.abilityName = info.name;
165    appItemInfo.moduleName = info.moduleName;
166    appItemInfo.keyName = info.bundleName + info.name + info.moduleName;
167    appItemInfo.bundleType = bundleManager.BundleType.ATOMIC_SERVICE;
168    await ResourceManager.getInstance().updateIconCache(appItemInfo.appIconId, appItemInfo.bundleName, appItemInfo.moduleName);
169    this.mAtomicServiceAppMap.set(appItemInfo.bundleName, appItemInfo);
170    Log.showInfo(TAG, `convertAtomicServiceToAppItemInfo appItemInfo: ${JSON.stringify(appItemInfo)}`);
171    return appItemInfo;
172  }
173
174  cleanAppMapCache(): void {
175    this.mAtomicServiceAppMap.clear();
176  }
177}
178
179const atomicServiceAbilityManager = AtomicServiceAbilityManager.getInstance();
180export default atomicServiceAbilityManager;
181