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