16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ciimport bundleManager from '@ohos.bundle.bundleManager'; 176e80583aSopenharmony_ciimport { CheckEmptyUtils } from '../utils/CheckEmptyUtils'; 186e80583aSopenharmony_ciimport osAccount from '@ohos.account.osAccount'; 196e80583aSopenharmony_ciimport type Want from '@ohos.app.ability.Want'; 206e80583aSopenharmony_ciimport { Log } from '../utils/Log'; 216e80583aSopenharmony_ci 226e80583aSopenharmony_ciconst TAG = 'CommonBundleManager'; 236e80583aSopenharmony_ciconst DEFAULT_USER_ID = 100; 246e80583aSopenharmony_ci 256e80583aSopenharmony_ci/** 266e80583aSopenharmony_ci * 通用包管理(可以查询桌面包、原子化服务包) 276e80583aSopenharmony_ci */ 286e80583aSopenharmony_ciclass CommonBundleManager { 296e80583aSopenharmony_ci private static mInstance: CommonBundleManager; 306e80583aSopenharmony_ci private mUserId: number = DEFAULT_USER_ID; 316e80583aSopenharmony_ci 326e80583aSopenharmony_ci /** 336e80583aSopenharmony_ci * 获取通用包管理对象 346e80583aSopenharmony_ci * 356e80583aSopenharmony_ci * @return 通用包管理对象单一实例 366e80583aSopenharmony_ci */ 376e80583aSopenharmony_ci static getInstance(): CommonBundleManager { 386e80583aSopenharmony_ci if (CommonBundleManager.mInstance == null) { 396e80583aSopenharmony_ci CommonBundleManager.mInstance = new CommonBundleManager(); 406e80583aSopenharmony_ci globalThis.CommonBundleManagerInstance = CommonBundleManager.mInstance; 416e80583aSopenharmony_ci } 426e80583aSopenharmony_ci return CommonBundleManager.mInstance; 436e80583aSopenharmony_ci } 446e80583aSopenharmony_ci 456e80583aSopenharmony_ci private constructor() { 466e80583aSopenharmony_ci const osAccountManager = osAccount.getAccountManager(); 476e80583aSopenharmony_ci osAccountManager.getOsAccountLocalId((err, localId) => { 486e80583aSopenharmony_ci Log.showDebug(TAG, `getOsAccountLocalIdFromProcess localId ${localId}`); 496e80583aSopenharmony_ci this.mUserId = localId; 506e80583aSopenharmony_ci }); 516e80583aSopenharmony_ci } 526e80583aSopenharmony_ci 536e80583aSopenharmony_ci /** 546e80583aSopenharmony_ci * 获取userId. 556e80583aSopenharmony_ci * 566e80583aSopenharmony_ci * @returns 576e80583aSopenharmony_ci */ 586e80583aSopenharmony_ci getUserId(): number { 596e80583aSopenharmony_ci return this.mUserId; 606e80583aSopenharmony_ci } 616e80583aSopenharmony_ci 626e80583aSopenharmony_ci /** 636e80583aSopenharmony_ci * 获取所有ability信息 646e80583aSopenharmony_ci * 656e80583aSopenharmony_ci * @param bundleType 包类型:bundleManager.BundleType.APP:桌面app, bundleManager.BundleType.ATOMIC_SERVICE:原子化服务 666e80583aSopenharmony_ci * @returns 所有ability信息 676e80583aSopenharmony_ci */ 686e80583aSopenharmony_ci async getAllAbilityList(bundleType?: bundleManager.BundleType): Promise<bundleManager.AbilityInfo[]> { 696e80583aSopenharmony_ci let abilityList: Array<bundleManager.AbilityInfo> = []; 706e80583aSopenharmony_ci let want: Want = { 716e80583aSopenharmony_ci action : 'action.system.home', 726e80583aSopenharmony_ci entities : ['entity.system.home'] 736e80583aSopenharmony_ci }; 746e80583aSopenharmony_ci try { 756e80583aSopenharmony_ci await bundleManager.queryAbilityInfo(want, bundleManager.AbilityFlag.GET_ABILITY_INFO_WITH_APPLICATION, this.mUserId) 766e80583aSopenharmony_ci .then((res: Array<bundleManager.AbilityInfo>) => { 776e80583aSopenharmony_ci Log.showInfo(TAG, `getAllAbilityList res length: ${res.length}`); 786e80583aSopenharmony_ci abilityList = res; 796e80583aSopenharmony_ci }) 806e80583aSopenharmony_ci .catch((err) => { 816e80583aSopenharmony_ci Log.showError(TAG, `getAllAbilityList error: ${JSON.stringify(err)}`); 826e80583aSopenharmony_ci }); 836e80583aSopenharmony_ci } catch (err) { 846e80583aSopenharmony_ci Log.showError(TAG, `getAllAbilityList bundleManager.queryAbilityInfo error: ${JSON.stringify(err)}`); 856e80583aSopenharmony_ci } 866e80583aSopenharmony_ci if (CheckEmptyUtils.isEmptyArr(abilityList)) { 876e80583aSopenharmony_ci Log.showInfo(TAG, 'getAllAbilityList Empty'); 886e80583aSopenharmony_ci return []; 896e80583aSopenharmony_ci } 906e80583aSopenharmony_ci if (CheckEmptyUtils.isEmpty(bundleType)) { 916e80583aSopenharmony_ci return abilityList; 926e80583aSopenharmony_ci } 936e80583aSopenharmony_ci return abilityList.filter(ability => ability.applicationInfo.bundleType === bundleType); 946e80583aSopenharmony_ci } 956e80583aSopenharmony_ci 966e80583aSopenharmony_ci /** 976e80583aSopenharmony_ci * 根据bundleName获取包信息 986e80583aSopenharmony_ci * 996e80583aSopenharmony_ci * @param bundleName 包名 1006e80583aSopenharmony_ci * @param bundleType 包类型:bundleManager.BundleType.APP:桌面app, bundleManager.BundleType.ATOMIC_SERVICE:原子化服务 1016e80583aSopenharmony_ci * @returns 包信息 1026e80583aSopenharmony_ci */ 1036e80583aSopenharmony_ci async getBundleInfoByBundleName(bundleName: string, bundleType?: bundleManager.BundleType): Promise<bundleManager.BundleInfo | undefined> { 1046e80583aSopenharmony_ci if (CheckEmptyUtils.checkStrIsEmpty(bundleName)) { 1056e80583aSopenharmony_ci Log.showError(TAG, 'getBundleInfoByBundleName reqParam bundleName is empty'); 1066e80583aSopenharmony_ci return undefined; 1076e80583aSopenharmony_ci } 1086e80583aSopenharmony_ci let bundleInfo: bundleManager.BundleInfo = undefined; 1096e80583aSopenharmony_ci try { 1106e80583aSopenharmony_ci await bundleManager.getBundleInfo(bundleName, 1116e80583aSopenharmony_ci bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | 1126e80583aSopenharmony_ci bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE | 1136e80583aSopenharmony_ci bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY, 1146e80583aSopenharmony_ci this.mUserId) 1156e80583aSopenharmony_ci .then((res: bundleManager.BundleInfo) => { 1166e80583aSopenharmony_ci Log.showInfo(TAG, `getBundleInfoByBundleName res:${JSON.stringify(res.hapModulesInfo.length)}`); 1176e80583aSopenharmony_ci bundleInfo = res; 1186e80583aSopenharmony_ci }) 1196e80583aSopenharmony_ci .catch((err) => { 1206e80583aSopenharmony_ci Log.showError(TAG, `getBundleInfoByBundleName error: ${JSON.stringify(err)}, bundleName:${bundleName}`); 1216e80583aSopenharmony_ci }); 1226e80583aSopenharmony_ci } catch (err) { 1236e80583aSopenharmony_ci Log.showError(TAG, `getBundleInfoByBundleName bundleManager.getBundleInfo error: ${JSON.stringify(err)}, bundleName:${bundleName}`); 1246e80583aSopenharmony_ci } 1256e80583aSopenharmony_ci if (CheckEmptyUtils.isEmpty(bundleInfo)) { 1266e80583aSopenharmony_ci return undefined; 1276e80583aSopenharmony_ci } 1286e80583aSopenharmony_ci if (CheckEmptyUtils.isEmpty(bundleType)) { 1296e80583aSopenharmony_ci return bundleInfo; 1306e80583aSopenharmony_ci } 1316e80583aSopenharmony_ci return bundleInfo.appInfo.bundleType === bundleType ? bundleInfo : undefined; 1326e80583aSopenharmony_ci } 1336e80583aSopenharmony_ci 1346e80583aSopenharmony_ci /** 1356e80583aSopenharmony_ci * 根据abilityName获取ability信息 1366e80583aSopenharmony_ci * 1376e80583aSopenharmony_ci * @param bundleName 包名 1386e80583aSopenharmony_ci * @param abilityName ability名 1396e80583aSopenharmony_ci * @param bundleType 包类型:bundleManager.BundleType.APP:桌面app, bundleManager.BundleType.ATOMIC_SERVICE:原子化服务 1406e80583aSopenharmony_ci * @returns ability信息 1416e80583aSopenharmony_ci */ 1426e80583aSopenharmony_ci async getAbilityInfoByAbilityName(bundleName: string, abilityName: string, 1436e80583aSopenharmony_ci bundleType?: bundleManager.BundleType): Promise<bundleManager.AbilityInfo | undefined> { 1446e80583aSopenharmony_ci if (CheckEmptyUtils.checkStrIsEmpty(bundleName) || CheckEmptyUtils.checkStrIsEmpty(abilityName)) { 1456e80583aSopenharmony_ci Log.showError(TAG, 'getAbilityInfoByAbilityName reqParam bundleName or abilityName is empty'); 1466e80583aSopenharmony_ci return undefined; 1476e80583aSopenharmony_ci } 1486e80583aSopenharmony_ci // get from system 1496e80583aSopenharmony_ci let abilityList = new Array<bundleManager.AbilityInfo>(); 1506e80583aSopenharmony_ci let want: Want = { 1516e80583aSopenharmony_ci bundleName: bundleName, 1526e80583aSopenharmony_ci abilityName: abilityName 1536e80583aSopenharmony_ci }; 1546e80583aSopenharmony_ci try { 1556e80583aSopenharmony_ci await bundleManager.queryAbilityInfo(want, bundleManager.AbilityFlag.GET_ABILITY_INFO_WITH_APPLICATION, this.mUserId) 1566e80583aSopenharmony_ci .then((res: Array<bundleManager.AbilityInfo>)=>{ 1576e80583aSopenharmony_ci if (res !== undefined) { 1586e80583aSopenharmony_ci Log.showInfo(TAG, `getAbilityInfoByAbilityName res length: ${res.length}`); 1596e80583aSopenharmony_ci abilityList = res; 1606e80583aSopenharmony_ci } 1616e80583aSopenharmony_ci }) 1626e80583aSopenharmony_ci .catch((err)=>{ 1636e80583aSopenharmony_ci Log.showError(TAG, `getAbilityInfoByAbilityName error: ${JSON.stringify(err)}`); 1646e80583aSopenharmony_ci }); 1656e80583aSopenharmony_ci } catch (err) { 1666e80583aSopenharmony_ci Log.showError(TAG, `getAbilityInfoByAbilityName bundleManager.queryAbilityInfo error: ${JSON.stringify(err)}`); 1676e80583aSopenharmony_ci } 1686e80583aSopenharmony_ci if (CheckEmptyUtils.isEmptyArr(abilityList)) { 1696e80583aSopenharmony_ci return undefined; 1706e80583aSopenharmony_ci } 1716e80583aSopenharmony_ci if (CheckEmptyUtils.isEmpty(bundleType)) { 1726e80583aSopenharmony_ci return abilityList[0]; 1736e80583aSopenharmony_ci } 1746e80583aSopenharmony_ci return abilityList[0].applicationInfo.bundleType === bundleType ? abilityList[0] : undefined; 1756e80583aSopenharmony_ci } 1766e80583aSopenharmony_ci} 1776e80583aSopenharmony_ci 1786e80583aSopenharmony_ciconst commonBundleManager = CommonBundleManager.getInstance(); 1796e80583aSopenharmony_ciexport default commonBundleManager;