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'; 24import { HiLog } from '../common/HiLog'; 25 26const TAG = 'ATC'; 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: 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 HiLog.error(TAG, `getConfigTips failed, error: ${JSON.stringify(err)}`); 53 reject(err); 54 }); 55 } else { 56 let accountTipsArray: AccountTips[] = JSON.parse(JSON.stringify(AccountTipsConfig.configTipsArray)); 57 resolve(accountTipsArray); 58 } 59 } catch (error) { 60 HiLog.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(async (resolve, reject) => { 73 try { 74 if (!authAccount) { 75 HiLog.error(TAG, `getAccount failed, authAccount is null.`); 76 reject(); 77 return; 78 } 79 let accountDomain = AppStorage.get('accountDomain') as string; 80 let domainAccountInfo: osAccount.GetDomainAccountInfoOptions = { 81 domain: accountDomain, 82 accountName: authAccount.toLocaleLowerCase().trim() 83 }; 84 let result = await osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo); 85 resolve(result); 86 } catch (error) { 87 HiLog.error(TAG, `getAccountInfo error: ${JSON.stringify(error)}`); 88 reject(error); 89 } 90 }); 91 } 92 93 private static loadCcmConfigs(): Promise<Array<AccountTips>> { 94 return new Promise((resolve, reject) => { 95 try { 96 let filePath: string = configPolicy.getOneCfgFileSync(AccountTipsConfig.ccmConfigPath); 97 let isExistFile: boolean = fs.accessSync(filePath); 98 let configStr: string = ''; 99 if (isExistFile) { 100 configStr = fs.readTextSync(filePath); 101 } else { 102 let context: common.UIAbilityContext = getContext() as common.UIAbilityContext; 103 let resourceManager: resourceManager.ResourceManager = context?.resourceManager; 104 if (resourceManager === null) { 105 HiLog.error(TAG, `loadCcmConfigs failed. resourceManager is null.`); 106 reject(); 107 return; 108 } 109 let contentArray: Uint8Array = resourceManager.getRawFileContentSync(AccountTipsConfig.configName); 110 let textDecoder: util.TextDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true }); 111 configStr = textDecoder.decodeWithStream(contentArray, { stream: false }); 112 } 113 114 let accountTipsArray: AccountTips[] = []; 115 if (configStr) { 116 let jsonArray: Object[] = JSON.parse(configStr); 117 for (let jsonObj of jsonArray) { 118 let accountTips: AccountTips = jsonObj as AccountTips; 119 if (accountTips.isTextContent) { 120 AccountTipsConfig.showContentKey = accountTips.key; 121 } 122 accountTipsArray.push(accountTips); 123 } 124 } 125 resolve(accountTipsArray); 126 } catch (error) { 127 HiLog.error(TAG, `loadCcmConfigs error: ${JSON.stringify(error)}`); 128 reject(error); 129 } 130 }); 131 } 132}