1b0b2a9adSopenharmony_ci/*
2b0b2a9adSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3b0b2a9adSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b0b2a9adSopenharmony_ci * you may not use this file except in compliance with the License.
5b0b2a9adSopenharmony_ci * You may obtain a copy of the License at
6b0b2a9adSopenharmony_ci *
7b0b2a9adSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b0b2a9adSopenharmony_ci *
9b0b2a9adSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b0b2a9adSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b0b2a9adSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b0b2a9adSopenharmony_ci * See the License for the specific language governing permissions and
13b0b2a9adSopenharmony_ci * limitations under the License.
14b0b2a9adSopenharmony_ci */
15b0b2a9adSopenharmony_ci
16b0b2a9adSopenharmony_ciimport UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility';
17b0b2a9adSopenharmony_ciimport logger from '../common/logger';
18b0b2a9adSopenharmony_ciimport Want from '@ohos.app.ability.Want';
19b0b2a9adSopenharmony_ciimport UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
20b0b2a9adSopenharmony_ciimport configPolicy from '@ohos.configPolicy';
21b0b2a9adSopenharmony_ciimport fs from '@ohos.file.fs';
22b0b2a9adSopenharmony_ciimport adminManager from '@ohos.enterprise.adminManager';
23b0b2a9adSopenharmony_ciimport { BusinessError } from '@ohos.base';
24b0b2a9adSopenharmony_ci
25b0b2a9adSopenharmony_ciconst TAG = 'AutoDeployAbility';
26b0b2a9adSopenharmony_ci
27b0b2a9adSopenharmony_ciexport default class AutoDeployAbility extends UIExtensionAbility {
28b0b2a9adSopenharmony_ci  private localStorage: LocalStorage = new LocalStorage();
29b0b2a9adSopenharmony_ci
30b0b2a9adSopenharmony_ci  async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
31b0b2a9adSopenharmony_ci    logger.info(TAG, 'onSessionCreate');
32b0b2a9adSopenharmony_ci    let admin: Want = {
33b0b2a9adSopenharmony_ci      bundleName: '',
34b0b2a9adSopenharmony_ci      abilityName: ''
35b0b2a9adSopenharmony_ci    };
36b0b2a9adSopenharmony_ci    let enterpriseInfo: adminManager.EnterpriseInfo = {
37b0b2a9adSopenharmony_ci      name: '',
38b0b2a9adSopenharmony_ci      description: ''
39b0b2a9adSopenharmony_ci    };
40b0b2a9adSopenharmony_ci    let adminType: adminManager.AdminType;
41b0b2a9adSopenharmony_ci    try {
42b0b2a9adSopenharmony_ci      let realpath = 'etc/edm/edm_provision_config.json';
43b0b2a9adSopenharmony_ci      await configPolicy.getOneCfgFile(realpath).then((value: string) => {
44b0b2a9adSopenharmony_ci        logger.info(TAG, 'getOneCfgFile value is : ' + value);
45b0b2a9adSopenharmony_ci        let configStr = fs.readTextSync(value);
46b0b2a9adSopenharmony_ci        logger.info(TAG, 'getOneCfgFile configStr is ' + JSON.stringify(configStr));
47b0b2a9adSopenharmony_ci        if (configStr) {
48b0b2a9adSopenharmony_ci          let jsonArray = JSON.parse(configStr);
49b0b2a9adSopenharmony_ci          admin.abilityName = jsonArray.admin_info[0].admin.abilityName;
50b0b2a9adSopenharmony_ci          admin.bundleName = jsonArray.admin_info[0].admin.bundleName;
51b0b2a9adSopenharmony_ci          enterpriseInfo.name = jsonArray.enterprise_info.organization_name;
52b0b2a9adSopenharmony_ci          enterpriseInfo.description = jsonArray.enterprise_info.description;
53b0b2a9adSopenharmony_ci          if (jsonArray.admin_info[0].admin_type == 'SDA') {
54b0b2a9adSopenharmony_ci            adminType = adminManager.AdminType.ADMIN_TYPE_SUPER;
55b0b2a9adSopenharmony_ci          }
56b0b2a9adSopenharmony_ci          this.localStorage.setOrCreate('adminInfo', admin);
57b0b2a9adSopenharmony_ci          this.localStorage.setOrCreate('enterpriseInfo', enterpriseInfo);
58b0b2a9adSopenharmony_ci          this.localStorage.setOrCreate('adminType', adminType);
59b0b2a9adSopenharmony_ci        }
60b0b2a9adSopenharmony_ci      }).catch((err: BusinessError) => {
61b0b2a9adSopenharmony_ci        logger.error(`getOneCfgFile edm_provision_config.json fail, errCode: ${err.code}, errMessage: ${err.message}`);
62b0b2a9adSopenharmony_ci      })
63b0b2a9adSopenharmony_ci    } catch (error) {
64b0b2a9adSopenharmony_ci      let code = (error as BusinessError).code;
65b0b2a9adSopenharmony_ci      let message = (error as BusinessError).message;
66b0b2a9adSopenharmony_ci      logger.info(TAG, 'getOneCfgFile error: ' + code + message);
67b0b2a9adSopenharmony_ci    }
68b0b2a9adSopenharmony_ci    this.localStorage.setOrCreate('session', session);
69b0b2a9adSopenharmony_ci    session.loadContent('pages/custProvisioning/custProvisioning', this.localStorage);
70b0b2a9adSopenharmony_ci  }
71b0b2a9adSopenharmony_ci
72b0b2a9adSopenharmony_ci  onDestroy(): void {
73b0b2a9adSopenharmony_ci    logger.info(TAG, 'onDestroy');
74b0b2a9adSopenharmony_ci  }
75b0b2a9adSopenharmony_ci
76b0b2a9adSopenharmony_ci  onForeground(): void {
77b0b2a9adSopenharmony_ci    // Ability has brought to foreground
78b0b2a9adSopenharmony_ci    logger.info(TAG, 'onForeground');
79b0b2a9adSopenharmony_ci  }
80b0b2a9adSopenharmony_ci
81b0b2a9adSopenharmony_ci  onBackground(): void {
82b0b2a9adSopenharmony_ci    // Ability has back to background
83b0b2a9adSopenharmony_ci    logger.info(TAG, 'onBackground');
84b0b2a9adSopenharmony_ci  }
85b0b2a9adSopenharmony_ci};
86