1//@ts-nocheck 2/* 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16import Log from '../../../../../../../../common/src/main/ets/default/Log'; 17import AbilityManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'; 18import BundleManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/bundleManager'; 19import Bundle from '@ohos.bundle'; 20import { BundleInfo } from 'bundle/bundleInfo'; 21import ResMgr from '@ohos.resourceManager'; 22import {BusinessError} from 'basic'; 23import SwitchUserManager from '../../../../../../../../common/src/main/ets/default/SwitchUserManager'; 24 25const INDEX = 0; 26const IS_INCLUDE_ABILITY_INFO = 0; 27 28const TAG = 'NotificationManagenment-BundleResourceModel'; 29 30export interface BundleItemData { 31 appIcon?: string; 32 appTitle?: string; 33 appValue?: string; 34 appArrow?: string|Resource; 35 appSummary?: string; 36 appBundleName?: string; 37 appIconId?: string; 38 appUri?: string; 39 appUid?: number; 40 systemApp?: boolean; 41} 42 43export default class BundleResourceModel { 44 private readonly mBundleInfoList: BundleItemData[] = []; 45 46 async getAllBundleInfos(): Promise<void> { 47 let userId =(await SwitchUserManager.getInstance().getCurrentUserInfo()).userId; 48 await Bundle.getAllBundleInfo(IS_INCLUDE_ABILITY_INFO, userId).then((data: BundleInfo[]) => { 49 void this.getIconItem(INDEX, data.length, data); 50 }); 51 Log.showInfo(TAG, 'getAllBundleInfos end'); 52 } 53 54 async getIconItem(index: number, count: number, data: BundleInfo[]): Promise<void> { 55 Log.showInfo(TAG, `getIconItem data.length ${data.length}`); 56 let label = ''; 57 let that = this; 58 try { 59 let appInfo = data[index].appInfo; 60 if (appInfo.labelResource.id > 0) { 61 BundleManager.getString(appInfo.labelResource, (value) => { 62 { 63 if (value) { 64 label = value; 65 } 66 } 67 }) 68 } else { 69 label = appInfo.label; 70 } 71 Log.showDebug(TAG, 'getIconItem BundleManager.getString finish label:' + label); 72 if (appInfo.iconResource.id <= 0) { 73 this.nextIconItem(index, count, data, this.mBundleInfoList, that); 74 return; 75 } 76 BundleManager.getMediaBase64(appInfo.iconResource, (value) => { 77 if (value) { 78 Log.showInfo(TAG, `getIconItem BundleManager.getMediaBase64() imageValue:${value}`); 79 let bundleItemData: BundleItemData = { 80 appIcon: value, 81 appTitle: label, 82 appValue: '', 83 appArrow: $r('app.media.ic_settings_arrow'), 84 appSummary: data[index].versionName, 85 appBundleName: data[index].name, 86 appIconId: appInfo.iconId, 87 appUri: 'pages/setEnable', 88 appUid: data[index].uid, 89 systemApp: appInfo.systemApp 90 }; 91 this.mBundleInfoList.push(bundleItemData); 92 } 93 this.nextIconItem(index, count, data, this.mBundleInfoList, that); 94 }); 95 } catch (error) { 96 Log.showError(TAG, `getIconItem catch error: ${JSON.stringify(error)}`); 97 } 98 } 99 100 nextIconItem(index: number, count: number, data: BundleInfo[], bundleInfoList: BundleItemData[], that: BundleResourceModel): void { 101 Log.showInfo(TAG, 'nextIconItem index:' + index + ' | count:' + count); 102 if (count - 1 > index) { 103 index = index + 1; 104 void that.getIconItem(index, count, data); 105 } else { 106 AppStorage.SetOrCreate('appManagementList', bundleInfoList); 107 } 108 } 109 110 async getBundleInfo(bundleName: string, callback: {(data: BundleItemData): void}): Promise<void> { 111 let mBundleInfo: BundleItemData = {}; 112 let label = ''; 113 let userInfo = await SwitchUserManager.getInstance().getCurrentUserInfo(); 114 115 await Bundle.getBundleInfo(bundleName, IS_INCLUDE_ABILITY_INFO, { 116 userId: userInfo.userId 117 }).then((data) => { 118 Log.showInfo(TAG, `getBundleInfo bundleInfo:${JSON.stringify(data)}`); 119 let appInfo = data.appInfo; 120 if (appInfo.labelResource.id > 0) { 121 BundleManager.getString(appInfo.labelResource, (value) => { 122 { 123 if (value) { 124 mBundleInfo.appTitle = value; 125 } 126 } 127 }) 128 } 129 mBundleInfo.appTitle = appInfo.label; 130 mBundleInfo.appValue = ''; 131 mBundleInfo.appArrow = $r('app.media.ic_settings_arrow'); 132 mBundleInfo.appSummary = data.versionName; 133 mBundleInfo.appBundleName = data.name; 134 mBundleInfo.appIconId = appInfo.iconId; 135 mBundleInfo.appUri = ''; 136 mBundleInfo.appUid = data.uid; 137 mBundleInfo.systemApp = appInfo.systemApp; 138 Log.showDebug(TAG, 'getBundleInfo getResourceManager label:' + label); 139 if (appInfo.iconResource.id > 0) { 140 BundleManager.getMediaBase64(appInfo.iconResource, (imageValue) => { 141 if (!!imageValue) { 142 mBundleInfo.appIcon = imageValue; 143 } 144 callback(mBundleInfo); 145 }); 146 } else { 147 callback(mBundleInfo); 148 } 149 }); 150 Log.showInfo(TAG, 'getBundleInfo end'); 151 } 152} 153 154