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(mBundleNameArr: Array<String>): Promise<Map<string, string>> { 27 let mBundleNames = Array.from(new Set(mBundleNameArr)) 28 let mMap = new Map 29 let want = { 30 action: "action.system.home", 31 entities: ["entity.system.home"] 32 }; 33 bundle.queryAbilityByWant(want, 1).then(async data => { 34 await 35 SPLogger.INFO(TAG,'getIconByBundleName data length [' + data.length + ']'); 36 for (let j = 0; j < data.length; j++) { 37 let bundleName = data[j].bundleName 38 for (let i = 0; i < mBundleNames.length; i++) { 39 if (mBundleNames[i] == bundleName) { 40 let bundleContext = globalThis.abilityContext.createBundleContext(mBundleNames[i]) 41 await bundleContext.resourceManager.getMediaBase64(data[j].iconId).then((value)=> { 42 if (value != null) { 43 mMap.set(mBundleNames[i], value) 44 } 45 }); 46 47 } 48 } 49 } 50 }).catch((error) => { 51 SPLogger.ERROR(TAG,'Operation failed. Cause: ' + JSON.stringify(error)) 52 console.error('BundleManager ... Operation failed. Cause: ' + JSON.stringify(error)); 53 }) 54 return mMap 55 } 56 57 //获取app列表 58 static async getAppList(): Promise<Array<AppInfoItem>> { 59 let appInfoList = new Array<AppInfoItem>() 60 let want = { 61 action: "action.system.home", 62 entities: ["entity.system.home"] 63 }; 64 bundle.queryAbilityByWant(want, 1).then(async data => { 65 await 66 SPLogger.INFO(TAG,'xxx getAllApplicationInfo data length [' + data.length + ']') 67 for (let i = 0; i < data.length; i++) { 68 let bundleName = data[i].bundleName 69 let bundleContext = globalThis.abilityContext.createBundleContext(data[i].bundleName) 70 try { 71 let appName = await bundleContext.resourceManager.getString(data[i].labelId) 72 let icon = await bundleContext.resourceManager.getMediaBase64(data[i].iconId) 73 bundle.getBundleInfo(bundleName, 1).then(bundleData => { 74 BundleManager.getAbility(bundleName).then(abilityName => { 75 console.info("index[" + i + "].getAbility for begin data: ", abilityName); 76 appInfoList.push(new AppInfoItem(bundleName, appName, bundleData.versionName, icon, abilityName)) 77 }); 78 }) 79 } catch (err) { 80 SPLogger.ERROR(TAG,"index[" + i + "] getAllApplicationInfo err" + err); 81 } 82 } 83 }).catch((error) => { 84 SPLogger.ERROR(TAG,'Operation failed. Cause: ' + JSON.stringify(error)) 85 console.error('BundleManager ... Operation failed. Cause: ' + JSON.stringify(error)); 86 }) 87 return appInfoList 88 } 89 //获取启动ability 90 static async getAbility(bundleName: string): Promise<string> { 91 let abilityName = ""; 92 try { 93 await bundle.queryAbilityByWant({ 94 bundleName: bundleName, 95 entities: [ 96 "entity.system.home", 97 ], 98 action: 99 "action.system.home", 100 }, 1, 100).then(abilityInfo => { 101 if (abilityInfo != null) { 102 abilityName = abilityInfo[0].name; 103 } 104 }) 105 } catch (err) { 106 SPLogger.ERROR(TAG,"index[" + bundleName + "] getAbility err" + err); 107 } 108 SPLogger.INFO(TAG,"index[" + bundleName + "] getAbility abilityName: " + abilityName); 109 return abilityName; 110 } 111} 112 113 114 115 116 117 118