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.bundleManager'; 17import accountManager, { UserId } from '../accountManager'; 18import baseData from '../baseData'; 19import logger from '../logger' 20import utils from '../utils' 21import { AppPermission, MyApplicationInfo } from '../myApplicationInfo' 22import Want from '@ohos.app.ability.Want'; 23import resourceManager from '@ohos.resourceManager'; 24import common from '@ohos.app.ability.common'; 25 26let defaultIcon = $r('app.media.icon'); 27const TAG = 'AppDetailData'; 28 29const permissionsDetailList: AppPermission[] = [ 30 { 31 permissionName: 'ohos.permission.ENTERPRISE_SET_DATETIME', 32 permissionLabel: $r('app.string.permissionLabel'), 33 permissionDescription: $r('app.string.permissionDescription') 34 }, 35]; 36 37export class AppDetailData { 38 public ret: boolean = true; 39 40 async checkAppItem(elementNameVal: Want) { 41 logger.info(TAG, 'checkAppItem in bundleName:' + elementNameVal.bundleName + ' | abilityName:' + 42 elementNameVal.abilityName); 43 let want: Want = { 44 'bundleName': elementNameVal.bundleName, 45 'abilityName': elementNameVal.abilityName 46 }; 47 let userId: UserId = { localId: 0 }; 48 let retVal = await accountManager.getAccountUserId(userId); 49 if (!retVal) { 50 logger.warn(TAG, 'checkAppItem getAccountUserId fail!'); 51 this.ret = false; 52 return this.ret; 53 } 54 let data: bundle.ExtensionAbilityInfo[] = []; 55 try { 56 await bundle.queryExtensionAbilityInfo(want, bundle.ExtensionAbilityType.ENTERPRISE_ADMIN, 57 bundle.ExtensionAbilityFlag.GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION, userId.localId).then((result) => { 58 data = result; 59 }) 60 } catch (e) { 61 logger.error(TAG, 'checkAppItem queryExtensionAbilityInfo try fail! ' + JSON.stringify(e)); 62 } 63 if (!utils.isValid(data) || data.length <= 0) { 64 logger.warn(TAG, 'checkAppItem queryExtensionAbilityInfo fail! data is null'); 65 this.ret = false; 66 return this.ret; 67 } 68 logger.info(TAG, 'checkAppItem success'); 69 return this.ret; 70 } 71 72 async getBundleInfoItem(bundleName: string, appInfo: MyApplicationInfo) { 73 logger.info(TAG, 'getBundleInfoItem in bundleName:' + bundleName); 74 let userId: UserId = { localId: 0 }; 75 let retVal = await accountManager.getAccountUserId(userId); 76 if (!retVal) { 77 logger.warn(TAG, 'getBundleInfoItem getAccountUserId fail!'); 78 return; 79 } 80 let data = await bundle.getBundleInfo(bundleName, bundle.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION | 81 bundle.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION, userId.localId); 82 await this.getResourceItem(bundleName, data, appInfo); 83 await this.getPermissionList(data, appInfo); 84 logger.info(TAG, 'getBundleInfoItem out'); 85 } 86 87 async getAppInfoLabel(resMgr: resourceManager.ResourceManager, id: number) { 88 let label = await resMgr.getString(id); 89 logger.info(TAG, 'getAppInfoLabel start label:' + label); 90 if (!utils.isValid(label) || label === baseData.EMPTY_STR) { 91 return baseData.EMPTY_STR; 92 } 93 return label; 94 } 95 96 async getAppInfoIcon(resMgr: resourceManager.ResourceManager, id: number) { 97 let iconVal = await resMgr.getMediaContentBase64Sync(id); 98 logger.info(TAG, 'getAppInfoIcon start iconVal:' + iconVal); 99 if (!utils.isValid(iconVal) || iconVal === baseData.EMPTY_STR) { 100 return baseData.EMPTY_STR; 101 } 102 return iconVal; 103 } 104 105 async getResourceItem(bundleName: string, data: bundle.BundleInfo, appInfo: MyApplicationInfo) { 106 logger.info(TAG, 'getResourceItem getResourceManager in'); 107 let bundleContext = getContext().createBundleContext(bundleName); 108 let resMgr = bundleContext.resourceManager; 109 let appInfoTemp = data.appInfo; 110 let label = ''; 111 let iconVal = ''; 112 113 if (appInfoTemp.labelId > 0) { 114 label = await this.getAppInfoLabel(resMgr, appInfoTemp.labelId); 115 logger.info(TAG, 'getResourceItem success appInfo.label:' + label); 116 } else { 117 label = appInfoTemp.label; 118 logger.info(TAG, 'getResourceItem defaults appInfo.label:' + label); 119 } 120 121 if (appInfoTemp.iconId > 0) { 122 iconVal = await this.getAppInfoIcon(resMgr, appInfoTemp.iconId); 123 if (iconVal === baseData.EMPTY_STR) { 124 let icon: resourceManager.Resource = { 125 bundleName: defaultIcon.bundleName, 126 moduleName: defaultIcon.moduleName, 127 id: defaultIcon.id 128 } 129 resMgr.getMediaContentBase64(icon).then((data) => { 130 appInfo.appIcon = data; 131 }) 132 appInfo.appTitle = label; 133 } 134 appInfo.appIcon = iconVal; 135 appInfo.appTitle = label; 136 AppStorage.SetOrCreate('applicationInfo', appInfo); 137 } 138 logger.info(TAG, 'getResourceItem getResourceManager out'); 139 } 140 141 async terminateAbilityPage() { 142 logger.info(TAG, 'terminateAbilityPage in:'); 143 await (getContext(this) as common.UIAbilityContext).terminateSelf(); 144 } 145 146 getPermissionInfoVal(permissionName: string): number { 147 for (let i = 0; i < permissionsDetailList.length; i++) { 148 if (permissionsDetailList[i].permissionName === permissionName) { 149 return i; 150 } 151 } 152 return -1; 153 } 154 155 async getPermissionList(data: bundle.BundleInfo, appInfo: MyApplicationInfo) { 156 let permissions = data.reqPermissionDetails; 157 if (permissions !== null && permissions.length !== 0) { 158 logger.info(TAG, 'getPermissionList permission length:' + permissions.length); 159 for (let i = 0; i < permissions.length; i++) { 160 logger.info(TAG, 'getPermissionList permission is in val:' + permissions[i].name); 161 let j = this.getPermissionInfoVal(permissions[i].name); 162 if (j >= 0) { 163 appInfo.appPermissionList.push({ 164 permissionName: permissionsDetailList[j].permissionName, 165 permissionLabel: permissionsDetailList[j].permissionLabel, 166 permissionDescription: permissionsDetailList[j].permissionDescription, 167 }); 168 } 169 } 170 logger.info(TAG, 'getPermissionList permission appInfo.appPermissionList:' + 171 JSON.stringify(appInfo.appPermissionList)); 172 } 173 logger.info(TAG, 'getPermissionList permission out'); 174 } 175} 176 177let appDetailData = new AppDetailData(); 178 179export default appDetailData as AppDetailData;