1/* 2 * Copyright (c) 2021-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 UIAbility from '@ohos.app.ability.UIAbility'; 17import bundleMonitor from '@ohos.bundle.bundleMonitor'; 18import account_osAccount from '@ohos.account.osAccount'; 19import { GlobalContext } from '../common/utils/globalContext'; 20import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit'; 21 22const TAG = 'PermissionManager_Log:'; 23const USER_ID = 100; 24 25export default class MainAbility extends UIAbility { 26 onCreate(want, launchParam): void { 27 console.log(TAG + 'MainAbility onCreate, ability name is ' + want.abilityName + '.'); 28 29 globalThis.bundleName = want.parameters.bundleName; 30 GlobalContext.store('bundleName', want.parameters.bundleName); 31 GlobalContext.store('context', this.context); 32 } 33 34 onWindowStageCreate(windowStage): void { 35 // Main window is created, set main page for this ability 36 console.log(TAG + 'MainAbility onWindowStageCreate.'); 37 globalThis.windowStage = windowStage; 38 globalThis.refresh = false; 39 if (!this.permissionCheck()) { 40 windowStage.loadContent('pages/transition'); 41 this.context.terminateSelf(); 42 return; 43 } 44 45 if (globalThis.bundleName) { 46 globalThis.currentApp = globalThis.bundleName; 47 this.getSperifiedApplication(globalThis.bundleName); 48 } else { 49 globalThis.currentApp = 'all'; 50 this.getAllApplications(); 51 } 52 53 try { 54 bundleMonitor.on('add', (bundleChangeInfo) => { 55 console.log(`${TAG} bundleMonitor.add: ${JSON.stringify(bundleChangeInfo)}`); 56 if (globalThis.currentApp === 'all') { 57 this.getAllApplications(); 58 globalThis.refresh = true; 59 } 60 }); 61 bundleMonitor.on('remove', (bundleChangeInfo) => { 62 console.log(`${TAG} bundleMonitor.remove: ${JSON.stringify(bundleChangeInfo)}`); 63 if (globalThis.currentApp === 'all') { 64 this.getAllApplications(); 65 globalThis.refresh = true; 66 } 67 }); 68 bundleMonitor.on('update', (bundleChangeInfo) => { 69 console.log(`${TAG} bundleMonitor.update: ${JSON.stringify(bundleChangeInfo)}`); 70 if (globalThis.currentApp === 'all') { 71 this.getAllApplications(); 72 globalThis.refresh = true; 73 } 74 }); 75 } catch (error) { 76 console.error(TAG + 'bundleMonitor failed.'); 77 } 78 79 } 80 81 onNewWant(want): void { 82 console.log(TAG + 'MainAbility onNewWant. want: ' + JSON.stringify(want)); 83 console.log(TAG + 'MainAbility onNewWant. bundleName: ' + JSON.stringify(want.parameters.bundleName)); 84 85 let bundleName = want.parameters.bundleName ? want.parameters.bundleName : 'all'; 86 if (globalThis.currentApp === 'all') { 87 if (globalThis.currentApp !== bundleName) { 88 console.log(TAG + 'MainAbility onNewWant. all -> app'); 89 globalThis.windowStage?.setUIContent(this.context, 'pages/transition', null); 90 globalThis.currentApp = bundleName; 91 GlobalContext.store('bundleName', bundleName); 92 this.getSperifiedApplication(bundleName); 93 } else { 94 if (globalThis.refresh === true) { 95 globalThis.windowStage?.setUIContent(this.context, 'pages/transition', null); 96 this.getAllApplications(); 97 globalThis.refresh = false; 98 } 99 } 100 } else { 101 if (bundleName === 'all') { 102 console.log(TAG + 'MainAbility onNewWant. app -> all'); 103 globalThis.windowStage?.setUIContent(this.context, 'pages/transition', null); 104 globalThis.currentApp = 'all'; 105 this.getAllApplications(); 106 } else { 107 if (globalThis.currentApp !== bundleName) { 108 console.log(TAG + 'MainAbility onNewWant. app -> app'); 109 globalThis.windowStage?.setUIContent(this.context, 'pages/transition', null); 110 globalThis.currentApp = bundleName; 111 GlobalContext.store('bundleName', bundleName); 112 this.getSperifiedApplication(bundleName); 113 } 114 } 115 } 116 117 } 118 119 onWindowStageDestroy(): void { 120 try { 121 bundleMonitor.off('add'); 122 bundleMonitor.off('remove'); 123 bundleMonitor.off('update'); 124 console.log(TAG + 'MainAbility onWindowStageDestroy.'); 125 } catch (err) { 126 console.log(`errData is errCode:${err.code} message:${err.message}`); 127 } 128 } 129 130 onBackground(): void { 131 console.log(TAG + ' onBackground.'); 132 } 133 134 onDestroy(): void { 135 console.log(TAG + ' onDestroy.'); 136 } 137 138 onForeground(): void { 139 console.log(TAG + ' onForeground.'); 140 } 141 142 private permissionCheck(): boolean { 143 try { 144 let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; 145 let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag); 146 let atManager = abilityAccessCtrl.createAtManager(); 147 let status = 148 atManager.verifyAccessTokenSync(bundleInfo.appInfo.accessTokenId, 'ohos.permission.GET_INSTALLED_BUNDLE_LIST'); 149 if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { 150 console.log(TAG + 'permission status is denied.'); 151 return false; 152 } 153 return true; 154 } catch (err) { 155 console.error(TAG + 'verifyAccessTokenSync failed.'); 156 return false; 157 } 158 } 159 160 getAllApplications(): void { 161 const flag = 162 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | 163 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; 164 let accountManager = account_osAccount.getAccountManager(); 165 try { 166 accountManager.getActivatedOsAccountLocalIds((err, idArray: number[])=>{ 167 console.log(TAG + 'getActivatedOsAccountLocalIds err:' + JSON.stringify(err)); 168 console.log(TAG + 'getActivatedOsAccountLocalIds idArray: ' + JSON.stringify(idArray)); 169 let userId = idArray[0]; 170 bundleManager.getAllBundleInfo(flag, userId || USER_ID).then(async(bundleInfos) => { 171 if (bundleInfos.length <= 0) { 172 console.info(TAG + 'bundle.getAllBundleInfo result.length less than or equal to zero'); 173 this.context.terminateSelf(); 174 return; 175 } 176 let initialGroups = []; 177 for (let i = 0; i < bundleInfos.length; i++) { 178 let info = bundleInfos[i]; 179 // Filter blank icon icon and text label resources 180 try { 181 await bundleManager.queryAbilityInfo({ 182 bundleName: info.name, 183 action: 'action.system.home', 184 entities: ['entity.system.home'] 185 }, bundleManager.AbilityFlag.GET_ABILITY_INFO_WITH_APPLICATION); 186 } catch (error) { 187 console.error( 188 TAG + 'queryAbilityByWant catch app: ' + JSON.stringify(info.name) + 'err: ' + JSON.stringify(error) 189 ); 190 continue; 191 } 192 193 initialGroups.push(info); 194 } 195 let storage: LocalStorage = new LocalStorage({ 'initialGroups': initialGroups }); 196 globalThis.windowStage?.loadContent('pages/authority-management', storage); 197 }).catch((error) => { 198 console.error(TAG + 'bundle.getAllBundleInfo failed. Cause: ' + JSON.stringify(error)); 199 this.context.terminateSelf(); 200 }); 201 }); 202 } catch (e) { 203 console.error(TAG + 'getActivatedOsAccountLocalIds exception: ' + JSON.stringify(e)); 204 this.context.terminateSelf(); 205 } 206 } 207 208 getSperifiedApplication(bundleName): void { 209 const flag = 210 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | 211 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; 212 try { 213 bundleManager.getBundleInfo(bundleName, flag).then(bundleInfo => { 214 let reqPermissions: Array<string> = []; 215 bundleInfo.reqPermissionDetails.forEach(item => { 216 reqPermissions.push(item.name); 217 }); 218 219 let info = { 220 'bundleName': bundleInfo.name, 221 'api': bundleInfo.targetVersion, 222 'tokenId': bundleInfo.appInfo.accessTokenId, 223 'icon': '', 224 'iconId': bundleInfo.appInfo.iconId, 225 'iconResource': bundleInfo.appInfo.iconResource, 226 'label': '', 227 'labelId': bundleInfo.appInfo.labelId, 228 'labelResource': bundleInfo.appInfo.labelResource, 229 'permissions': reqPermissions, 230 'groupId': [], 231 'zhTag': '', 232 'indexTag': '', 233 'language': '' 234 }; 235 GlobalContext.store('applicationInfo', info); 236 globalThis.windowStage?.setUIContent(this.context, 'pages/application-secondary', null); 237 }).catch((error) => { 238 console.log(TAG + 'Special branch getBundleInfo failed:' + JSON.stringify(error)); 239 this.context.terminateSelf(); 240 }); 241 } catch (error) { 242 console.error(TAG + 'Special branch failed: ' + JSON.stringify(error)); 243 this.context.terminateSelf(); 244 } 245 } 246}; 247