• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 bundle from '@ohos.bundle';
17import ResMgr from '@ohos.resourceManager';
18import { AppInfoItem } from '../entity/LocalConfigEntity';
19import SPLogger from '../utils/SPLogger';
20/**
21 * 包管理 获取应用列表、icon、app名称
22 */
23const TAG = 'BundleManager';
24export default class BundleManager {
25  //根据包名获取base64
26  static async getIconByBundleName(
27    mBundleNameArr: Array<String>
28  ): Promise<Map<string, string>> {
29    let mBundleNames = Array.from(new Set(mBundleNameArr));
30    let mMap = new Map();
31    let want = {
32      action: 'action.system.home',
33      entities: ['entity.system.home'],
34    };
35    bundle
36      .queryAbilityByWant(want, 1)
37      .then(async (data) => {
38        SPLogger.INFO(
39          TAG,
40          'getIconByBundleName data length [' + data.length + ']'
41        );
42        for (let j = 0; j < data.length; j++) {
43          let bundleName = data[j].bundleName;
44          for (let i = 0; i < mBundleNames.length; i++) {
45            if (mBundleNames[i] === bundleName) {
46              let bundleContext = globalThis.abilityContext.createBundleContext(
47                mBundleNames[i]
48              );
49              await bundleContext.resourceManager
50                .getMediaBase64(data[j].iconId)
51                .then((value) => {
52                  if (value != null) {
53                    mMap.set(mBundleNames[i], value);
54                  }
55                });
56            }
57          }
58        }
59      })
60      .catch((error) => {
61        SPLogger.ERROR(
62          TAG,
63          'Operation failed. Cause: ' + JSON.stringify(error)
64        );
65        console.error(
66          'BundleManager ... Operation failed. Cause: ' + JSON.stringify(error)
67        );
68      });
69    return mMap;
70  }
71
72  //获取app列表
73  static async getAppList(): Promise<Array<AppInfoItem>> {
74    let appInfoList = new Array<AppInfoItem>();
75    let want = {
76      action: 'action.system.home',
77      entities: ['entity.system.home'],
78    };
79    bundle
80      .queryAbilityByWant(want, 1)
81      .then(async (data) => {
82        SPLogger.INFO(
83          TAG,
84          'xxx getAllApplicationInfo data length [' + data.length + ']'
85        );
86        for (let i = 0; i < data.length; i++) {
87          let bundleName = data[i].bundleName;
88          let bundleContext = globalThis.abilityContext.createBundleContext(
89            data[i].bundleName
90          );
91          try {
92            let appName = await bundleContext.resourceManager.getString(
93              data[i].labelId
94            );
95            let icon = await bundleContext.resourceManager.getMediaBase64(
96              data[i].iconId
97            );
98            bundle.getBundleInfo(bundleName, 1).then((bundleData) => {
99              BundleManager.getAbility(bundleName).then((abilityName) => {
100                console.info(
101                  'index[' + i + '].getAbility for begin data: ',
102                  abilityName
103                );
104                appInfoList.push(
105                  new AppInfoItem(
106                    bundleName,
107                    appName,
108                    bundleData.versionName,
109                    icon,
110                    abilityName
111                  )
112                );
113              });
114            });
115          } catch (err) {
116            SPLogger.ERROR(
117              TAG,
118              'index[' + i + ']  getAllApplicationInfo err' + err
119            );
120          }
121        }
122      })
123      .catch((error) => {
124        SPLogger.ERROR(
125          TAG,
126          'Operation failed. Cause: ' + JSON.stringify(error)
127        );
128        console.error(
129          'BundleManager ... Operation failed. Cause: ' + JSON.stringify(error)
130        );
131      });
132    return appInfoList;
133  }
134  //获取启动ability
135  static async getAbility(bundleName: string): Promise<string> {
136    let abilityName = '';
137    try {
138      await bundle
139        .queryAbilityByWant(
140          {
141            bundleName: bundleName,
142            entities: ['entity.system.home'],
143            action: 'action.system.home',
144          },
145          1,
146          100
147        )
148        .then((abilityInfo) => {
149          if (abilityInfo != null) {
150            abilityName = abilityInfo[0].name;
151          }
152        });
153    } catch (err) {
154      SPLogger.ERROR(TAG, 'index[' + bundleName + '] getAbility err' + err);
155    }
156    SPLogger.INFO(
157      TAG,
158      'index[' + bundleName + '] getAbility abilityName: ' + abilityName
159    );
160    return abilityName;
161  }
162}
163