• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility';
17import logger from '../common/logger';
18import Want from '@ohos.app.ability.Want';
19import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
20import configPolicy from '@ohos.configPolicy';
21import fs from '@ohos.file.fs';
22import adminManager from '@ohos.enterprise.adminManager';
23import { BusinessError } from '@ohos.base';
24
25const TAG = 'AutoDeployAbility';
26
27export default class AutoDeployAbility extends UIExtensionAbility {
28  private localStorage: LocalStorage = new LocalStorage();
29
30  async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
31    logger.info(TAG, 'onSessionCreate');
32    let admin: Want = {
33      bundleName: '',
34      abilityName: ''
35    };
36    let enterpriseInfo: adminManager.EnterpriseInfo = {
37      name: '',
38      description: ''
39    };
40    let adminType: adminManager.AdminType;
41    try {
42      let realpath = 'etc/edm/edm_provision_config.json';
43      await configPolicy.getOneCfgFile(realpath).then((value: string) => {
44        logger.info(TAG, 'getOneCfgFile value is : ' + value);
45        let configStr = fs.readTextSync(value);
46        logger.info(TAG, 'getOneCfgFile configStr is ' + JSON.stringify(configStr));
47        if (configStr) {
48          let jsonArray = JSON.parse(configStr);
49          admin.abilityName = jsonArray.admin_info[0].admin.abilityName;
50          admin.bundleName = jsonArray.admin_info[0].admin.bundleName;
51          enterpriseInfo.name = jsonArray.enterprise_info.organization_name;
52          enterpriseInfo.description = jsonArray.enterprise_info.description;
53          if (jsonArray.admin_info[0].admin_type == 'SDA') {
54            adminType = adminManager.AdminType.ADMIN_TYPE_SUPER;
55          }
56          this.localStorage.setOrCreate('adminInfo', admin);
57          this.localStorage.setOrCreate('enterpriseInfo', enterpriseInfo);
58          this.localStorage.setOrCreate('adminType', adminType);
59        }
60      }).catch((err: BusinessError) => {
61        logger.error(`getOneCfgFile edm_provision_config.json fail, errCode: ${err.code}, errMessage: ${err.message}`);
62      })
63    } catch (error) {
64      let code = (error as BusinessError).code;
65      let message = (error as BusinessError).message;
66      logger.info(TAG, 'getOneCfgFile error: ' + code + message);
67    }
68    this.localStorage.setOrCreate('session', session);
69    session.loadContent('pages/custProvisioning/custProvisioning', this.localStorage);
70  }
71
72  onDestroy(): void {
73    logger.info(TAG, 'onDestroy');
74  }
75
76  onForeground(): void {
77    // Ability has brought to foreground
78    logger.info(TAG, 'onForeground');
79  }
80
81  onBackground(): void {
82    // Ability has back to background
83    logger.info(TAG, 'onBackground');
84  }
85};
86