1315b9658Sopenharmony_ci/**
2315b9658Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3315b9658Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4315b9658Sopenharmony_ci * you may not use this file except in compliance with the License.
5315b9658Sopenharmony_ci * You may obtain a copy of the License at
6315b9658Sopenharmony_ci *
7315b9658Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8315b9658Sopenharmony_ci *
9315b9658Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10315b9658Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11315b9658Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12315b9658Sopenharmony_ci * See the License for the specific language governing permissions and
13315b9658Sopenharmony_ci * limitations under the License.
14315b9658Sopenharmony_ci */
15315b9658Sopenharmony_ci
16315b9658Sopenharmony_ciimport common from '@ohos.app.ability.common';
17315b9658Sopenharmony_ciimport dataStorage from '@ohos.data.preferences';
18315b9658Sopenharmony_ciimport deviceInfo from '@ohos.deviceInfo';
19315b9658Sopenharmony_ciimport relationalStore from '@ohos.data.relationalStore';
20315b9658Sopenharmony_ciimport fs from '@ohos.file.fs';
21315b9658Sopenharmony_ciimport i18n from '@ohos.i18n';
22315b9658Sopenharmony_ciimport settings from '@ohos.settings';
23315b9658Sopenharmony_ciimport systemParameter from '@ohos.systemparameter';
24315b9658Sopenharmony_ciimport SettingsDataConfig from './SettingsDataConfig';
25315b9658Sopenharmony_ciimport { Log } from '../Utils/Log';
26315b9658Sopenharmony_ciimport { GlobalContext }  from './GlobalContext';
27315b9658Sopenharmony_ciimport contextConstant from '@ohos.app.ability.contextConstant';
28315b9658Sopenharmony_ciimport { TableType } from '../common/Common';
29315b9658Sopenharmony_ci
30315b9658Sopenharmony_ciconst DEFAULT_JSON_FILE_NAME : string = 'default_settings.json';
31315b9658Sopenharmony_ciconst SETTINGSDATA_PREFERENCE : string = 'SettingsDataPreference';
32315b9658Sopenharmony_ciconst SETTINGSDATA_PREFERENCE_USER : string = 'SettingsDataPreferenceUser';
33315b9658Sopenharmony_ciconst EL2_DB_PATH: string = '/data/storage/el2/database/entry/rdb/settingsdata.db'
34315b9658Sopenharmony_ciconst EMULATOR_TYPE: string = 'emulator'
35315b9658Sopenharmony_ciinterface  TIME_FORMAT_DATA {
36315b9658Sopenharmony_ci  TIME_FORMAT_24: string ;
37315b9658Sopenharmony_ci  TIME_FORMAT_12: string ;
38315b9658Sopenharmony_ci}
39315b9658Sopenharmony_ciconst TIME_FORMAT: TIME_FORMAT_DATA = {
40315b9658Sopenharmony_ci  TIME_FORMAT_24: '24',
41315b9658Sopenharmony_ci  TIME_FORMAT_12: '12',
42315b9658Sopenharmony_ci}
43315b9658Sopenharmony_ciinterface IContent {
44315b9658Sopenharmony_ci  settings: Array<Map<string,string>> ;
45315b9658Sopenharmony_ci  user: Array<Map<string,string>> ;
46315b9658Sopenharmony_ci  userSecure: Array<Map<string,string>> ;
47315b9658Sopenharmony_ci}
48315b9658Sopenharmony_ci
49315b9658Sopenharmony_ciconst INITIAL_KEY: string = '_CreatedTime';
50315b9658Sopenharmony_ciconst VALID_DB_LENGTH: number = 48;
51315b9658Sopenharmony_ciconst SETTINGS_CLONED_STATUS: string = 'settingsClonedStatus';
52315b9658Sopenharmony_ci
53315b9658Sopenharmony_ciclass SettingsDBHelper {
54315b9658Sopenharmony_ci  public static readonly SHARED_TABLE_CREATE_PREFIX: string =
55315b9658Sopenharmony_ci    `CREATE TABLE IF NOT EXISTS ${SettingsDataConfig.TABLE_NAME}`;
56315b9658Sopenharmony_ci  // 需要在在表名后拼接当前的userid
57315b9658Sopenharmony_ci  public static readonly CURRENT_USER_TABLE_CREATE_PREFIX: string =
58315b9658Sopenharmony_ci    `CREATE TABLE IF NOT EXISTS ${SettingsDataConfig.USER_TABLE_NAME}_`;
59315b9658Sopenharmony_ci  public static readonly CURRENT_SECURE_TABLE_CREATE_PREFIX: string =
60315b9658Sopenharmony_ci    `CREATE TABLE IF NOT EXISTS ${SettingsDataConfig.SECURE_TABLE_NAME}_`;
61315b9658Sopenharmony_ci  public static readonly TABLE_CREATE_SUFFIX = ` (${SettingsDataConfig.FIELD_ID} INTEGER PRIMARY KEY AUTOINCREMENT, ` +
62315b9658Sopenharmony_ci    `${SettingsDataConfig.FIELD_KEYWORD} TEXT, `  +
63315b9658Sopenharmony_ci    `${SettingsDataConfig.FIELD_VALUE} TEXT CHECK (LENGTH(VALUE)<=1000))`;
64315b9658Sopenharmony_ci
65315b9658Sopenharmony_ci  private rdbStore?: relationalStore.RdbStore;
66315b9658Sopenharmony_ci  private context: Context;
67315b9658Sopenharmony_ci  private readonly DEFAULT_USER_ID: number = 100;
68315b9658Sopenharmony_ci  private area: contextConstant.AreaMode | undefined = undefined;
69315b9658Sopenharmony_ci  public isFirstStartup: dataStorage.ValueType = true;
70315b9658Sopenharmony_ci  public maxUserNO: dataStorage.ValueType = 100;
71315b9658Sopenharmony_ci  private faultOccured: boolean = false;
72315b9658Sopenharmony_ci
73315b9658Sopenharmony_ci  private constructor() {
74315b9658Sopenharmony_ci    this.rdbStore = undefined;
75315b9658Sopenharmony_ci    this.context = GlobalContext.getContext().getObject('abilityContext') as Context;
76315b9658Sopenharmony_ci    Log.info('context start'+ JSON.stringify(this.context));
77315b9658Sopenharmony_ci  }
78315b9658Sopenharmony_ci
79315b9658Sopenharmony_ci  private async emulatorParamInit(): Promise<void> {
80315b9658Sopenharmony_ci    if (this.getProductModel() !== EMULATOR_TYPE) {
81315b9658Sopenharmony_ci      Log.info('currently not a emulator');
82315b9658Sopenharmony_ci      return;
83315b9658Sopenharmony_ci    }
84315b9658Sopenharmony_ci
85315b9658Sopenharmony_ci    let tableName: string = this.getTableName(TableType.SETTINGS, this.DEFAULT_USER_ID);
86315b9658Sopenharmony_ci    await this.loadTableSettings('device_provisioned', '1', tableName);
87315b9658Sopenharmony_ci    tableName = this.getTableName(TableType.USER_SECURE, this.DEFAULT_USER_ID);
88315b9658Sopenharmony_ci    await this.loadTableSettings('basic_statement_agreed', '1', tableName);
89315b9658Sopenharmony_ci    await this.loadTableSettings('user_setup_complete', '1', tableName);
90315b9658Sopenharmony_ci    await this.loadTableSettings('is_ota_finished', '1', tableName);
91315b9658Sopenharmony_ci  }
92315b9658Sopenharmony_ci
93315b9658Sopenharmony_ci  public getProductModel(): string {
94315b9658Sopenharmony_ci    return deviceInfo.productModel;
95315b9658Sopenharmony_ci  }
96315b9658Sopenharmony_ci
97315b9658Sopenharmony_ci  public getArea() {
98315b9658Sopenharmony_ci    const dbFile = EL2_DB_PATH;
99315b9658Sopenharmony_ci    if (this.area === undefined) {
100315b9658Sopenharmony_ci      try {
101315b9658Sopenharmony_ci        let stat = fs.statSync(dbFile);
102315b9658Sopenharmony_ci        if (stat.size > VALID_DB_LENGTH) {
103315b9658Sopenharmony_ci          this.area = contextConstant.AreaMode.EL2;
104315b9658Sopenharmony_ci        } else {
105315b9658Sopenharmony_ci          this.area = contextConstant.AreaMode.EL1;
106315b9658Sopenharmony_ci        }
107315b9658Sopenharmony_ci      } catch {
108315b9658Sopenharmony_ci        this.area = contextConstant.AreaMode.EL1;
109315b9658Sopenharmony_ci      }
110315b9658Sopenharmony_ci    }
111315b9658Sopenharmony_ci    Log.info(`Area ${this.area}`);
112315b9658Sopenharmony_ci    return this.area;
113315b9658Sopenharmony_ci  }
114315b9658Sopenharmony_ci
115315b9658Sopenharmony_ci  public async initialInsert(tableName: string): Promise<void> {
116315b9658Sopenharmony_ci    try {
117315b9658Sopenharmony_ci      Log.info(`insert ${tableName} with key: ${tableName + INITIAL_KEY} `);
118315b9658Sopenharmony_ci      if (this.rdbStore) {
119315b9658Sopenharmony_ci        let ret = await this.rdbStore.insert(tableName,
120315b9658Sopenharmony_ci          { 'KEYWORD': tableName + INITIAL_KEY, 'VALUE': new Date().toString() });
121315b9658Sopenharmony_ci        if (ret <= 0) {
122315b9658Sopenharmony_ci          Log.error(`insert initial key-value failed for ${tableName}`);
123315b9658Sopenharmony_ci        }
124315b9658Sopenharmony_ci      } else {
125315b9658Sopenharmony_ci        Log.error(`insert initial key-value failed for ${tableName}, no rdbStore`);
126315b9658Sopenharmony_ci        this.faultOccured = true;
127315b9658Sopenharmony_ci      }
128315b9658Sopenharmony_ci    } catch (e) {
129315b9658Sopenharmony_ci      Log.error(`insert initial key-value failed for ${tableName}`);
130315b9658Sopenharmony_ci    }
131315b9658Sopenharmony_ci  }
132315b9658Sopenharmony_ci
133315b9658Sopenharmony_ci  private async firstStartupConfig() : Promise<void> {
134315b9658Sopenharmony_ci    Log.info('firstStartupConfig start');
135315b9658Sopenharmony_ci    let storage = await dataStorage.getPreferences(this.context as Context, SETTINGSDATA_PREFERENCE);
136315b9658Sopenharmony_ci    this.isFirstStartup = await storage.get('isFirstStartup', true);
137315b9658Sopenharmony_ci    storage = await dataStorage.getPreferences(this.context as Context, SETTINGSDATA_PREFERENCE_USER);
138315b9658Sopenharmony_ci    this.maxUserNO = await storage.get('MAXUSERNO', 100);
139315b9658Sopenharmony_ci    Log.info(`firstStartupConfig isFirstStartUp = ${this.isFirstStartup} max user no: ${this.maxUserNO}`);
140315b9658Sopenharmony_ci    // 总是创建以下三张表 if not exists
141315b9658Sopenharmony_ci    // 创建公共数据表
142315b9658Sopenharmony_ci    await this.rdbStore?.executeSql(SettingsDBHelper.SHARED_TABLE_CREATE_PREFIX +
143315b9658Sopenharmony_ci      SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
144315b9658Sopenharmony_ci    // 创建默认用户数据表
145315b9658Sopenharmony_ci    await this.rdbStore?.executeSql(SettingsDBHelper.CURRENT_USER_TABLE_CREATE_PREFIX +
146315b9658Sopenharmony_ci      this.DEFAULT_USER_ID + SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
147315b9658Sopenharmony_ci    // 创建默认用户 secure 数据表
148315b9658Sopenharmony_ci    await this.rdbStore?.executeSql(SettingsDBHelper.CURRENT_SECURE_TABLE_CREATE_PREFIX +
149315b9658Sopenharmony_ci      this.DEFAULT_USER_ID + SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
150315b9658Sopenharmony_ci    if (this.isFirstStartup) {
151315b9658Sopenharmony_ci      Log.info('loadDefaultSettingsData begin');
152315b9658Sopenharmony_ci      this.loadDefaultSettingsData();
153315b9658Sopenharmony_ci      Log.info('loadDefaultSettingsData finish');
154315b9658Sopenharmony_ci      await this.initialInsert(SettingsDataConfig.TABLE_NAME);
155315b9658Sopenharmony_ci      await this.initialInsert(SettingsDataConfig.USER_TABLE_NAME + '_' + this.DEFAULT_USER_ID);
156315b9658Sopenharmony_ci      await this.initialInsert(SettingsDataConfig.SECURE_TABLE_NAME + '_' + this.DEFAULT_USER_ID);
157315b9658Sopenharmony_ci    }
158315b9658Sopenharmony_ci    Log.info('firstStartupConfig end');
159315b9658Sopenharmony_ci    return;
160315b9658Sopenharmony_ci  }
161315b9658Sopenharmony_ci
162315b9658Sopenharmony_ci  public async initRdbStore() {
163315b9658Sopenharmony_ci    Log.info('call initRdbStore start');
164315b9658Sopenharmony_ci    let  rdbStore = await relationalStore.getRdbStore(this.context as Context, {
165315b9658Sopenharmony_ci      name: SettingsDataConfig.DB_NAME,
166315b9658Sopenharmony_ci      securityLevel:1
167315b9658Sopenharmony_ci    });
168315b9658Sopenharmony_ci    if(rdbStore){
169315b9658Sopenharmony_ci      this.rdbStore = rdbStore;
170315b9658Sopenharmony_ci    }
171315b9658Sopenharmony_ci    await this.firstStartupConfig();
172315b9658Sopenharmony_ci    Log.info('call initRdbStore end');
173315b9658Sopenharmony_ci    return this.rdbStore;
174315b9658Sopenharmony_ci  }
175315b9658Sopenharmony_ci
176315b9658Sopenharmony_ci  public static getInstance(): SettingsDBHelper {
177315b9658Sopenharmony_ci    GlobalContext.getContext().getObject('settingsDBHelper') as SettingsDBHelper;
178315b9658Sopenharmony_ci    if(!GlobalContext.getContext().getObject('settingsDBHelper')){
179315b9658Sopenharmony_ci      GlobalContext.getContext().setObject('settingsDBHelper', new SettingsDBHelper());
180315b9658Sopenharmony_ci    }
181315b9658Sopenharmony_ci    return GlobalContext.getContext().getObject('settingsDBHelper') as SettingsDBHelper;
182315b9658Sopenharmony_ci  }
183315b9658Sopenharmony_ci
184315b9658Sopenharmony_ci  public async getRdbStore() {
185315b9658Sopenharmony_ci    Log.info('call getRdbStore start');
186315b9658Sopenharmony_ci    if (!this.rdbStore) {
187315b9658Sopenharmony_ci      return  await (GlobalContext.getContext().getObject('settingsDBHelper') as SettingsDBHelper).initRdbStore();
188315b9658Sopenharmony_ci      // return await globalThis.settingsDBHelper.initRdbStore();
189315b9658Sopenharmony_ci    }
190315b9658Sopenharmony_ci    return this.rdbStore
191315b9658Sopenharmony_ci  }
192315b9658Sopenharmony_ci
193315b9658Sopenharmony_ci  public async loadTableData(content: IContent, tableType: TableType, userId: number): Promise<void> {
194315b9658Sopenharmony_ci    if (!content) {
195315b9658Sopenharmony_ci      Log.error('content is empty');
196315b9658Sopenharmony_ci      return;
197315b9658Sopenharmony_ci    }
198315b9658Sopenharmony_ci    switch (tableType) {
199315b9658Sopenharmony_ci      case TableType.SETTINGS:
200315b9658Sopenharmony_ci        this.loadDefaultTaleData(content.settings, TableType.SETTINGS, userId);
201315b9658Sopenharmony_ci        return;
202315b9658Sopenharmony_ci      case TableType.USER:
203315b9658Sopenharmony_ci        this.loadDefaultTaleData(content.user, TableType.USER, userId);
204315b9658Sopenharmony_ci        return;
205315b9658Sopenharmony_ci      case TableType.SETTINGS:
206315b9658Sopenharmony_ci        this.loadDefaultTaleData(content.userSecure, TableType.USER_SECURE, userId);
207315b9658Sopenharmony_ci        return;
208315b9658Sopenharmony_ci      default:
209315b9658Sopenharmony_ci        Log.error('invalid type');
210315b9658Sopenharmony_ci    }
211315b9658Sopenharmony_ci  }
212315b9658Sopenharmony_ci
213315b9658Sopenharmony_ci  private  getTableName(tableType: TableType, userId: number): string {
214315b9658Sopenharmony_ci    if (tableType === TableType.SETTINGS) {
215315b9658Sopenharmony_ci      return SettingsDataConfig.TABLE_NAME;
216315b9658Sopenharmony_ci    }
217315b9658Sopenharmony_ci    if (tableType === TableType.USER) {
218315b9658Sopenharmony_ci      return `${SettingsDataConfig.USER_TABLE_NAME}_${userId}`;
219315b9658Sopenharmony_ci    }
220315b9658Sopenharmony_ci    return `${SettingsDataConfig.SECURE_TABLE_NAME}_${userId}`;
221315b9658Sopenharmony_ci  }
222315b9658Sopenharmony_ci
223315b9658Sopenharmony_ci  private async loadTableSettings(key: string, value: string, tableName: string): Promise<void> {
224315b9658Sopenharmony_ci    if (!this.rdbStore) {
225315b9658Sopenharmony_ci      Log.error('rdbStore is null!');
226315b9658Sopenharmony_ci      return
227315b9658Sopenharmony_ci    }
228315b9658Sopenharmony_ci    Log.info(`tableName: ${tableName}, key: ${key}, value: ${value}`);
229315b9658Sopenharmony_ci    try {
230315b9658Sopenharmony_ci      let ret = await this.rdbStore.insert(tableName, { 'KEYWORD': key, 'VALUE': value });
231315b9658Sopenharmony_ci      if (ret >= 0) {
232315b9658Sopenharmony_ci        Log.info(`insert into DB success; ${ret}`);
233315b9658Sopenharmony_ci      } else {
234315b9658Sopenharmony_ci        this.faultOccured = true;
235315b9658Sopenharmony_ci        Log.error(`insert into DB faild; ${ret}`);
236315b9658Sopenharmony_ci      }
237315b9658Sopenharmony_ci    } catch (err) {
238315b9658Sopenharmony_ci      Log.warn(`insert key ${key} failed`);
239315b9658Sopenharmony_ci    }
240315b9658Sopenharmony_ci  }
241315b9658Sopenharmony_ci
242315b9658Sopenharmony_ci  public async loadUserSettings(key: string, value: string, userId: number|undefined): Promise<void> {
243315b9658Sopenharmony_ci    if (!this.rdbStore) {
244315b9658Sopenharmony_ci      Log.error('rdbStore is null!');
245315b9658Sopenharmony_ci      return
246315b9658Sopenharmony_ci    }
247315b9658Sopenharmony_ci    Log.info('key=' + key + ' value ' + value + ' userid ' + userId);
248315b9658Sopenharmony_ci    await this.rdbStore.insert(SettingsDataConfig.USER_TABLE_NAME + '_' + userId,
249315b9658Sopenharmony_ci      { 'KEYWORD': key, 'VALUE': value }, (err, ret) => {
250315b9658Sopenharmony_ci      if (err) {
251315b9658Sopenharmony_ci        Log.error('loadGlobalSettings insert error:' + JSON.stringify(err));
252315b9658Sopenharmony_ci      }
253315b9658Sopenharmony_ci      Log.info('loadGlobalSettings insert ret = ' + ret);
254315b9658Sopenharmony_ci    });
255315b9658Sopenharmony_ci  }
256315b9658Sopenharmony_ci
257315b9658Sopenharmony_ci  public async readDefaultFile(): Promise<Object> {
258315b9658Sopenharmony_ci    let rawStr: string = '';
259315b9658Sopenharmony_ci    try {
260315b9658Sopenharmony_ci      let content: number[] = Array.from(await this.context?.resourceManager.getRawFile(DEFAULT_JSON_FILE_NAME));
261315b9658Sopenharmony_ci      rawStr = String.fromCharCode(...Array.from(content));
262315b9658Sopenharmony_ci    } catch (err) {
263315b9658Sopenharmony_ci      Log.error('readDefaultFile readRawFile err' + err);
264315b9658Sopenharmony_ci    }
265315b9658Sopenharmony_ci
266315b9658Sopenharmony_ci    if (rawStr) {
267315b9658Sopenharmony_ci      Log.info('readDefaultFile success');
268315b9658Sopenharmony_ci      return JSON.parse(rawStr);
269315b9658Sopenharmony_ci    }
270315b9658Sopenharmony_ci    return rawStr;
271315b9658Sopenharmony_ci  }
272315b9658Sopenharmony_ci
273315b9658Sopenharmony_ci  private async loadDefaultSettingsData(): Promise<void> {
274315b9658Sopenharmony_ci    if (!this.isFirstStartup) {
275315b9658Sopenharmony_ci      Log.info('loadDefaultSettingsData exists');
276315b9658Sopenharmony_ci      return;
277315b9658Sopenharmony_ci    }
278315b9658Sopenharmony_ci    Log.info('loadDefaultSettingsData start');
279315b9658Sopenharmony_ci    try {
280315b9658Sopenharmony_ci      let content = await this.readDefaultFile() as IContent;
281315b9658Sopenharmony_ci      if (!content) {
282315b9658Sopenharmony_ci        Log.error('readDefaultFile is failed!');
283315b9658Sopenharmony_ci        return;
284315b9658Sopenharmony_ci      }
285315b9658Sopenharmony_ci      // 同时加载三张表,主要用于首次加载场景
286315b9658Sopenharmony_ci      await this.loadTableData(content, TableType.SETTINGS, this.DEFAULT_USER_ID);
287315b9658Sopenharmony_ci      await this.loadTableData(content, TableType.USER, this.DEFAULT_USER_ID);
288315b9658Sopenharmony_ci      await this.loadTableData(content, TableType.USER_SECURE, this.DEFAULT_USER_ID);
289315b9658Sopenharmony_ci    } catch (err) {
290315b9658Sopenharmony_ci      Log.error('loadDefaultSettingsData catch error! err = ' + err);
291315b9658Sopenharmony_ci    }
292315b9658Sopenharmony_ci
293315b9658Sopenharmony_ci    let tableName: string = this.getTableName(TableType.SETTINGS, this.DEFAULT_USER_ID);
294315b9658Sopenharmony_ci    // 初始化设备名称
295315b9658Sopenharmony_ci    let deviceName: string = deviceInfo.marketName;
296315b9658Sopenharmony_ci    if (deviceName.startsWith('"') && deviceName.endsWith('"')) {
297315b9658Sopenharmony_ci      deviceName = JSON.parse(deviceName);
298315b9658Sopenharmony_ci    }
299315b9658Sopenharmony_ci    await this.loadTableSettings(settings.general.DEVICE_NAME, deviceName, tableName);
300315b9658Sopenharmony_ci
301315b9658Sopenharmony_ci    // 初始化亮度值
302315b9658Sopenharmony_ci    let defaultBrightness = systemParameter.getSync('const.display.brightness.default');
303315b9658Sopenharmony_ci    if (defaultBrightness) {
304315b9658Sopenharmony_ci      await this.loadTableSettings(settings.display.SCREEN_BRIGHTNESS_STATUS, defaultBrightness, tableName);
305315b9658Sopenharmony_ci    }
306315b9658Sopenharmony_ci
307315b9658Sopenharmony_ci    // 初始化克隆标识
308315b9658Sopenharmony_ci    await this.loadTableSettings(SETTINGS_CLONED_STATUS, '0', tableName);
309315b9658Sopenharmony_ci
310315b9658Sopenharmony_ci    // 适配模拟器开机不走OOBE
311315b9658Sopenharmony_ci    await this.emulatorParamInit();
312315b9658Sopenharmony_ci
313315b9658Sopenharmony_ci    //make sure no faultoccured, then write isFirstStartup false;
314315b9658Sopenharmony_ci    if (this.faultOccured === false) {
315315b9658Sopenharmony_ci      let storage = await dataStorage.getPreferences(this.context as Context, SETTINGSDATA_PREFERENCE);
316315b9658Sopenharmony_ci      await storage.put('isFirstStartUp', false);
317315b9658Sopenharmony_ci      await storage.flush();
318315b9658Sopenharmony_ci      Log.info('settingsdata initial DB success. ')
319315b9658Sopenharmony_ci    } else {
320315b9658Sopenharmony_ci      Log.warn('settingsdata initial DB failed! Will retry possible during next startup!!!');
321315b9658Sopenharmony_ci    }
322315b9658Sopenharmony_ci    Log.info('loadDefaultSettingsData end');
323315b9658Sopenharmony_ci  }
324315b9658Sopenharmony_ci  private async loadDefaultTaleData(tableData: Array<Map<string, string>>, tableType: TableType,
325315b9658Sopenharmony_ci                                    userID: number): Promise<void> {
326315b9658Sopenharmony_ci    if (tableData?.length <= 0) {
327315b9658Sopenharmony_ci      Log.error(`${tableType} table data is empty`);
328315b9658Sopenharmony_ci      return;
329315b9658Sopenharmony_ci    }
330315b9658Sopenharmony_ci    let tableName: string = this.getTableName(tableType, userID);
331315b9658Sopenharmony_ci    for (let index = 0; index < tableData.length; index++) {
332315b9658Sopenharmony_ci      await this.loadTableSettings(tableData[index]['name'], tableData[index]['value'], tableName);
333315b9658Sopenharmony_ci    }
334315b9658Sopenharmony_ci  }
335315b9658Sopenharmony_ci}
336315b9658Sopenharmony_ci
337315b9658Sopenharmony_ciexport default SettingsDBHelper;
338