1/** 2 * Copyright (c) 2021 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 */ 15import type common from '@ohos.app.ability.common'; 16import BaseModel from '../../../../../../../common/utils/src/main/ets/default/model/BaseModel'; 17import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil'; 18import { GlobalContext } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/GlobalContext'; 19import Bundle from '@ohos.bundle'; 20import appManager from '@ohos.application.appManager'; 21import osAccount from '@ohos.account.osAccount'; 22import bundleManager from '@ohos.bundle.bundleManager'; 23import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData'; 24 25const INDEX = 0; 26const IS_INCLUDE_ABILITY_INFO = 0; 27const URI_PAGE = 'pages/applicationInfo'; 28let icon_arrow = $r('app.media.ic_settings_arrow'); 29let icon_default = "ohos_app_icon"; 30let icon_default_str = ""; 31 32const MODULE_TAG = ConfigData.TAG + 'application-> '; 33 34/** 35 * app management service class 36 */ 37export class AppManagementModel extends BaseModel { 38 private mBundleInfoList: any[] = []; 39 40 constructor() { 41 super(); 42 try { 43 let context = GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext) as common.Context; 44 context.resourceManager.getMediaBase64ByName(icon_default) 45 .then((res) => { 46 icon_default_str = res; 47 LogUtil.info('settings AppManagementModel getResourceManager init defaultIcon res:' + icon_default_str); 48 }) 49 .catch((err) => { 50 icon_default_str = ""; 51 LogUtil.info('settings AppManagementModel getResourceManager init defaultIcon error:' + err); 52 }); 53 } catch (err) { 54 icon_default_str = ""; 55 LogUtil.info('settings AppManagementModel getResourceManager init defaultIcon error:' + err); 56 } 57 58 } 59 60 /** 61 * get application data 62 */ 63 setAppManagementListener() { 64 this.mBundleInfoList = []; 65 bundleManager.getAllBundleInfo(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION) 66 .then((data) => { 67 LogUtil.info('settings AppManagementModel setAppManagementListener getBundleInfos() start '); 68 LogUtil.info('settings AppManagementModel data.length: ' + data.length + ' data: ' + JSON.stringify(data)); 69 this.getResourceItem(INDEX, data.length, data); 70 }); 71 LogUtil.info('settings appManagement init AppManagementModel setAppManagementListener end'); 72 } 73 74 /** 75 * get resource information according to resource id 76 * 77 * @param index - array position 78 * @param count - array length 79 * @param data - data 80 */ 81 async getResourceItem(index, count, data) { 82 LogUtil.info('settings AppManagementModel getIconItem start data.length' + data.length); 83 let imageValue = ''; 84 let label = ''; 85 let that = this; 86 LogUtil.info('settings AppManagementModel data[index].name :' + data[index].name); 87 try { 88 let context = GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext) as common.Context; 89 let appInfo = data[index].appInfo; 90 LogUtil.info('settings AppManagementModel getResourceManager appInfo.labelId:' + JSON.stringify(appInfo.labelResource)); 91 if (appInfo.labelResource.id > 0) { 92 await context.resourceManager.getString(appInfo.labelResource) 93 .then((res) => { 94 label = res; 95 LogUtil.info('settings AppManagementModel getResourceManager getString() res:' + label); 96 }) 97 .catch((err) => { 98 LogUtil.info('settings AppManagementModel getResourceManager getString() error:' + err); 99 }); 100 } else { 101 label = appInfo.label; 102 if (label.length == 0) { 103 label = appInfo.labelResource.bundleName; 104 } 105 LogUtil.info('settings AppManagementModel getResourceManager getString() id=0:' + appInfo.label); 106 } 107 LogUtil.info('settings AppManagementModel getResourceManager getString() value:' + label); 108 LogUtil.info('settings AppManagementModel getResourceManager appInfo.iconResource:' + JSON.stringify(appInfo.iconResource)); 109 if (appInfo.iconResource.id > 0) { 110 await context.resourceManager.getMediaBase64(appInfo.iconResource) 111 .then((res) => { 112 imageValue = res; 113 LogUtil.info('settings AppManagementModel getResourceManager getMediaBase64() res:' + imageValue); 114 }) 115 .catch((err) => { 116 LogUtil.info('settings AppManagementModel getResourceManager getString() error:' + err); 117 imageValue = icon_default_str; 118 }); 119 } else { 120 imageValue = icon_default_str; 121 LogUtil.info('settings AppManagementModel getResourceManager getMediaBase64() id=0:' + icon_default_str); 122 } 123 LogUtil.info('settings AppManagementModel getResourceManager getMediaBase64() value:' + imageValue); 124 this.mBundleInfoList.push({ 125 settingIcon: imageValue, 126 settingTitle: label, 127 settingValue: '', 128 settingArrow: icon_arrow, 129 settingSummary: data[index].versionName, 130 settingBundleName: data[index].name, 131 settingIconId: appInfo.iconId, 132 settingUri: URI_PAGE 133 }); 134 135 if (count - 1 > index) { 136 LogUtil.info('settings AppManagementModel getMediaBase64() id=0:' + index + ' | count:' + count); 137 index = index + 1; 138 that.getResourceItem(index, count, data); 139 } else { 140 LogUtil.info('settings AppManagementModel getMediaBase64() id=0:' + index + ' | count:' + count); 141 LogUtil.info('settings AppManagementModel mBundleInfoList[i]: ' + JSON.stringify(this.mBundleInfoList)); 142 AppStorage.SetOrCreate('appManagementList', this.mBundleInfoList); 143 } 144 } catch (error) { 145 LogUtil.error('settings AppManagementModel catch error:' + error); 146 } 147 LogUtil.info('settings appManagement AppManagementModel getIconItem end'); 148 } 149 150 /** 151 * Remove duplicate item 152 */ 153 public distinct(arr) { 154 for (let i = 0; i < arr.length; i++) { 155 for (let j = i + 1; j < arr.length; j++) { 156 if (arr[i].settingBundleName === arr[j].settingBundleName) { 157 arr.splice(j, 1); 158 j--; 159 } 160 } 161 } 162 return arr; 163 } 164 165 /** 166 * Clear up application data by bundle name 167 * @param bundleName bundle name 168 */ 169 clearUpApplicationData(bundleName: string, callback) { 170 appManager.clearUpApplicationData(bundleName, callback); 171 } 172 173 /** 174 * Clears cache data of a specified application. 175 * @param bundleName bundle name 176 */ 177 cleanBundleCacheFiles(bundleName: string, callback) { 178 bundleManager.cleanBundleCacheFiles(bundleName, callback); 179 } 180 181 /** 182 * Uninstall an application. 183 * @param bundleName bundle name 184 */ 185 async uninstall(bundleName: string, callback) { 186 LogUtil.info(ConfigData.TAG + "start uninstall in model"); 187 const bundlerInstaller = await Bundle.getBundleInstaller(); 188 const accountManager = await osAccount.getAccountManager(); 189 const mUserID = await accountManager.getOsAccountLocalIdFromProcess(); 190 LogUtil.info(ConfigData.TAG + "get bundlerInstaller : " + typeof bundlerInstaller); 191 bundlerInstaller.uninstall(bundleName, 192 { 193 userId: mUserID, 194 installFlag: 0, 195 isKeepData: false 196 }, (err, result) => { 197 AppStorage.SetOrCreate('appManagementList', []); 198 this.setAppManagementListener(); 199 callback(err, result); 200 }); 201 LogUtil.info(ConfigData.TAG + "end uninstall in model"); 202 } 203 204 getBundleInfo(bundleName, callback) { 205 LogUtil.info(MODULE_TAG + 'start get bundle info'); 206 Bundle.getBundleInfo(bundleName, Bundle.BundleFlag.GET_APPLICATION_INFO_WITH_PERMISSION, callback); 207 LogUtil.info(MODULE_TAG + 'end get bundle info'); 208 } 209 210 /** 211 * Kill processes by bundle name 212 * @param bundleName bundle name 213 */ 214 killProcessesByBundleName(bundleName: string, callback) { 215 appManager.killProcessesByBundleName(bundleName, callback); 216 } 217} 218 219let appManagementModel = new AppManagementModel(); 220 221export default appManagementModel as AppManagementModel 222;