• 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 { BusinessError } from '@ohos.base';
21import I18n from '@ohos.i18n';
22import { HiLog } from '../common/HiLog';
23
24const TAG = 'ATC';
25const SYS_LANGUAGE: string = 'zh-Hans';
26
27export interface AccountTips {
28  key: string;
29  description: string;
30  descriptionEn: string;
31  isShow: boolean;
32  value: string;
33  isTextContent?: boolean;
34}
35
36export class AccountTipsConfig {
37  public static configTipsArray: AccountTips[] = [];
38  public static showContentKey: string = '';
39  private static ccmConfigPath: string = 'etc/dlp_manager/account_tips.json';
40  private static configName: string = 'account_tips.json';
41
42  public static getConfigTips(): Promise<Array<AccountTips>> {
43    return new Promise((resolve, reject) => {
44      try {
45        if (AccountTipsConfig.configTipsArray?.length === 0) {
46          AccountTipsConfig.loadCcmConfigs().then((accountTipsArray: Array<AccountTips>) => {
47            AccountTipsConfig.configTipsArray = accountTipsArray;
48            resolve(AccountTipsConfig.configTipsArray);
49          }).catch((err: BusinessError) => {
50            HiLog.error(TAG, `getConfigTips failed, error: ${JSON.stringify(err)}`);
51            reject(err);
52          });
53        } else {
54          let accountTipsArray: AccountTips[] = JSON.parse(JSON.stringify(AccountTipsConfig.configTipsArray));
55          resolve(accountTipsArray);
56        }
57      } catch (error) {
58        HiLog.error(TAG, `getConfigTips error: ${JSON.stringify(error)}`);
59        reject(error);
60      }
61    })
62  }
63
64  public static isSysLanguage(): boolean {
65    let systemLanguage: string = I18n.System.getSystemLanguage();
66    return systemLanguage.indexOf(SYS_LANGUAGE) > -1;
67  }
68
69  private static loadCcmConfigs(): Promise<Array<AccountTips>> {
70    return new Promise((resolve, reject) => {
71      try {
72        let filePath: string = configPolicy.getOneCfgFileSync(AccountTipsConfig.ccmConfigPath);
73        let isExistFile: boolean = fs.accessSync(filePath);
74        let configStr: string = '';
75        if (isExistFile) {
76          configStr = fs.readTextSync(filePath);
77        } else {
78          let context: common.UIAbilityContext = getContext() as common.UIAbilityContext;
79          let resourceManager: resourceManager.ResourceManager = context?.resourceManager;
80          if (resourceManager === null) {
81            HiLog.error(TAG, `loadCcmConfigs failed. resourceManager is null.`);
82            reject();
83            return;
84          }
85          let contentArray: Uint8Array = resourceManager.getRawFileContentSync(AccountTipsConfig.configName);
86          let textDecoder: util.TextDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
87          configStr = textDecoder.decodeToString(contentArray, { stream: false });
88        }
89
90        let accountTipsArray: AccountTips[] = [];
91        if (configStr) {
92          let jsonArray: Object[] = JSON.parse(configStr);
93          for (let jsonObj of jsonArray) {
94            let accountTips: AccountTips = jsonObj as AccountTips;
95            if (accountTips.isTextContent) {
96              AccountTipsConfig.showContentKey = accountTips.key;
97            }
98            accountTipsArray.push(accountTips);
99          }
100        }
101        resolve(accountTipsArray);
102      } catch (error) {
103        HiLog.error(TAG, `loadCcmConfigs error: ${JSON.stringify(error)}`);
104        reject(error);
105      }
106    });
107  }
108}