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