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