• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 account_osAccount from '@ohos.account.osAccount';
19import { GlobalContext } from '../common/utils/globalContext';
20
21const TAG = 'PermissionManager_MainAbility:';
22const USER_ID = 100;
23
24export default class MainAbility extends UIAbility {
25  onCreate(want, launchParam): void {
26    console.log(TAG + 'MainAbility onCreate, ability name is ' + want.abilityName + '.');
27
28    globalThis.bundleName = want.parameters.bundleName;
29    GlobalContext.store('bundleName', want.parameters.bundleName);
30    GlobalContext.store('context', this.context);
31  }
32
33  onWindowStageCreate(windowStage): void {
34    // Main window is created, set main page for this ability
35    console.log(TAG + 'MainAbility onWindowStageCreate.');
36    globalThis.windowStage = windowStage;
37
38    if (globalThis.bundleName) {
39      globalThis.currentApp = globalThis.bundleName;
40      this.getSperifiedApplication(globalThis.bundleName);
41    } else {
42      globalThis.currentApp = 'all';
43      this.getAllApplications();
44    }
45  }
46
47  onNewWant(want): void {
48    console.log(TAG + 'MainAbility onNewWant. want: ' + JSON.stringify(want));
49    console.log(TAG + 'MainAbility onNewWant. bundleName: ' + JSON.stringify(want.parameters.bundleName));
50
51    let bundleName = want.parameters.bundleName ? want.parameters.bundleName : 'all';
52    if (globalThis.currentApp === 'all') {
53      if (globalThis.currentApp !== bundleName) {
54        console.log(TAG + 'MainAbility onNewWant. all -> app');
55        globalThis.windowStage.setUIContent(this.context, 'pages/transition', null);
56        globalThis.currentApp = bundleName;
57        GlobalContext.store('bundleName', bundleName);
58        this.getSperifiedApplication(bundleName);
59      }
60    } else {
61      if (bundleName === 'all') {
62        console.log(TAG + 'MainAbility onNewWant. app -> all');
63        globalThis.windowStage.setUIContent(this.context, 'pages/transition', null);
64        globalThis.currentApp = 'all';
65        this.getAllApplications();
66      } else {
67        if (globalThis.currentApp !== bundleName) {
68          console.log(TAG + 'MainAbility onNewWant. app -> app');
69          globalThis.windowStage.setUIContent(this.context, 'pages/transition', null);
70          globalThis.currentApp = bundleName;
71          GlobalContext.store('bundleName', bundleName);
72          this.getSperifiedApplication(bundleName);
73        }
74      }
75    }
76
77  }
78
79  onForeground(): void {
80    // Ability has brought to foreground
81    console.log(TAG + 'MainAbility onForeground.');
82  }
83
84  onBackground(): void {
85    // Ability has back to background
86    console.log(TAG + 'MainAbility onBackground.');
87  }
88
89  onDestroy(): void {
90    console.log(TAG + 'MainAbility onDestroy.');
91  }
92
93  onWindowStageDestroy(): void {
94    // Main window is destroyed, release UI related resources
95    console.log(TAG + 'MainAbility onWindowStageDestroy.');
96  }
97
98  getAllApplications(): void {
99    const flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
100    let accountManager = account_osAccount.getAccountManager();
101    try {
102      accountManager.getActivatedOsAccountLocalIds((err, idArray: number[])=>{
103        console.log(TAG + 'getActivatedOsAccountLocalIds err:' + JSON.stringify(err));
104        console.log(TAG + 'getActivatedOsAccountLocalIds idArray: ' + JSON.stringify(idArray));
105        let userId = idArray[0];
106        bundleManager.getAllBundleInfo(flag, userId || USER_ID).then(async(bundleInfos) => {
107          if (bundleInfos.length <= 0) {
108            console.info(TAG + 'bundle.getAllBundleInfo result.length less than or equal to zero');
109            return;
110          }
111          let initialGroups = [];
112          for (let i = 0; i < bundleInfos.length; i++) {
113            let info = bundleInfos[i];
114            // Filter blank icon icon and text label resources
115            try {
116              await bundleManager.queryAbilityInfo({
117                bundleName: info.name,
118                action: 'action.system.home',
119                entities: ['entity.system.home']
120              }, bundleManager.AbilityFlag.GET_ABILITY_INFO_WITH_APPLICATION);
121            } catch (error) {
122              console.error(TAG + 'queryAbilityByWant catch app: ' + JSON.stringify(info.name) + 'err: ' + JSON.stringify(error));
123              continue;
124            }
125
126            initialGroups.push(info);
127          }
128          let storage: LocalStorage = new LocalStorage({ 'initialGroups': initialGroups });
129          globalThis.windowStage.loadContent('pages/authority-management', storage);
130        }).catch((error) => {
131          console.error(TAG + 'bundle.getAllBundleInfo failed. Cause: ' + JSON.stringify(error));
132        });
133      });
134    } catch (e) {
135      console.error(TAG + 'getActivatedOsAccountLocalIds exception: ' + JSON.stringify(e));
136    }
137  }
138
139  getSperifiedApplication(bundleName): void {
140    const flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
141    bundleManager.getBundleInfo(bundleName, flag).then(bundleInfo => {
142      let reqPermissions: Array<string> = [];
143      bundleInfo.reqPermissionDetails.forEach(item => {
144        reqPermissions.push(item.name);
145      });
146
147      let info = {
148        'bundleName': bundleInfo.name,
149        'api': bundleInfo.targetVersion,
150        'tokenId': bundleInfo.appInfo.accessTokenId,
151        'icon': '',
152        'iconId': bundleInfo.appInfo.iconId,
153        'label': '',
154        'labelId': bundleInfo.appInfo.labelId,
155        'permissions': reqPermissions,
156        'groupId': [],
157        'zhTag': '',
158        'indexTag': '',
159        'language': ''
160      };
161      GlobalContext.store('applicationInfo', info);
162      globalThis.windowStage.setUIContent(this.context, 'pages/application-secondary', null);
163    }).catch(() => {
164      console.log(TAG + 'MainAbility getSperifiedApplication failed.');
165      this.context.terminateSelf();
166    });
167  }
168};
169