• 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 */
15import configPolicy from '@ohos.configPolicy';
16import fs from '@ohos.file.fs'
17import common from '@ohos.app.ability.common';
18import resourceManager from '@ohos.resourceManager';
19import util from '@ohos.util';
20import osAccount from '@ohos.account.osAccount';
21import { BusinessError } from '@ohos.base';
22import GlobalContext from './GlobalContext';
23import I18n from '@ohos.i18n';
24
25const TAG: string = "DLPManager_ATC";
26const DOMAIN_CHINA: string = "china";
27const SYS_LANGUAGE: string = "zh-Hans";
28
29export interface AccountTips {
30  key: string;
31  description: string;
32  descriptionEn: string;
33  isShow: boolean;
34  value: string;
35  isTextContent?: boolean;
36}
37
38export class AccountTipsConfig {
39  public static configTipsArray: Array<AccountTips> = new Array<AccountTips>();
40  public static showContentKey: string = "";
41  private static ccmConfigPath: string = "etc/dlp_manager/account_tips.json";
42  private static configName: string = "account_tips.json";
43
44  public static getConfigTips(): Promise<Array<AccountTips>> {
45    return new Promise((resolve, reject) => {
46      try {
47        if (AccountTipsConfig.configTipsArray?.length === 0) {
48          AccountTipsConfig.loadCcmConfigs().then((accountTipsArray: Array<AccountTips>) => {
49            AccountTipsConfig.configTipsArray = accountTipsArray;
50            resolve(AccountTipsConfig.configTipsArray);
51          }).catch((err: BusinessError) => {
52            console.error(`${TAG} getConfigTips failed, error: ${JSON.stringify(err)}`);
53            reject(err);
54          });
55        } else {
56          let accountTipsArray: Array<AccountTips> = JSON.parse(JSON.stringify(AccountTipsConfig.configTipsArray));
57          resolve(accountTipsArray);
58        }
59      } catch (error) {
60        console.error(`${TAG} getConfigTips error: ${JSON.stringify(error)}`);
61        reject(error);
62      }
63    })
64  }
65
66  public static isSysLanguage(): boolean {
67    let systemLanguage: string = I18n.System.getSystemLanguage();
68    return systemLanguage.indexOf(SYS_LANGUAGE) > -1;
69  }
70
71  public static getAccountInfo(authAccount: string): Promise<osAccount.DomainAccountInfo> {
72    return new Promise((resolve, reject) => {
73      try {
74        if (!authAccount) {
75          console.error(`${TAG} getAccount failed, authAccount is null.`);
76          reject();
77          return;
78        }
79        let domainAccountInfo: osAccount.GetDomainAccountInfoOptions = {
80          domain: DOMAIN_CHINA,
81          accountName: authAccount.toLocaleLowerCase().trim()
82        };
83        osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo).then((result: osAccount.DomainAccountInfo) => {
84          resolve(result);
85        }).catch((err: BusinessError) => {
86          console.error(`${TAG} getAccountInfo failed, error: ${JSON.stringify(err)}`);
87          reject(err);
88        });
89      } catch (error) {
90        console.error(`${TAG} getAccountInfo error: ${JSON.stringify(error)}`);
91        reject(error);
92      }
93    });
94  }
95
96  private static loadCcmConfigs(): Promise<Array<AccountTips>> {
97    return new Promise((resolve, reject) => {
98      try {
99        let filePath: string = configPolicy.getOneCfgFileSync(AccountTipsConfig.ccmConfigPath);
100        let isExitFile: boolean = fs.accessSync(filePath);
101        let configStr: string = '';
102        if (isExitFile) {
103          configStr = fs.readTextSync(filePath);
104        } else {
105          let context: common.UIAbilityContext = GlobalContext.load('context') as common.UIAbilityContext;
106          let resourceManager: resourceManager.ResourceManager = context?.resourceManager;
107          if (resourceManager === null) {
108            console.error(`${TAG} loadCcmConfigs failed. resourceManager is null.`);
109            reject();
110            return;
111          }
112          let contentArray: Uint8Array = resourceManager.getRawFileContentSync(AccountTipsConfig.configName);
113          let textDecoder: util.TextDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
114          configStr = textDecoder.decodeWithStream(contentArray, { stream: false });
115        }
116
117        let accountTipsArray: Array<AccountTips> = new Array<AccountTips>();
118        if (configStr) {
119          let jsonArray: Array<Object> = JSON.parse(configStr);
120          for (let jsonObj of jsonArray) {
121            let accountTips: AccountTips = jsonObj as AccountTips;
122            if (accountTips.isTextContent) {
123              AccountTipsConfig.showContentKey = accountTips.key;
124            }
125            accountTipsArray.push(accountTips);
126          }
127        }
128        resolve(accountTipsArray);
129      } catch (error) {
130        console.error(`${TAG} loadCcmConfigs error: ${JSON.stringify(error)}`);
131        reject(error);
132      }
133    });
134  }
135}