• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 */
15
16import { common } from '@kit.AbilityKit';
17import data_preferences from '@ohos.data.preferences';
18import { HiLog } from './HiLog';
19
20const TAG: string = 'StorageUtil';
21
22export default class StorageUtil {
23  private static mInstance: StorageUtil | null = null;
24  private appContext: common.Context | null = null;
25
26  private constructor(appContext: common.Context) {
27    this.appContext = appContext;
28  }
29
30  public static getInstance(appContext: common.Context): StorageUtil {
31    if (!StorageUtil.mInstance) {
32      StorageUtil.mInstance = new StorageUtil(appContext);
33    }
34    return StorageUtil.mInstance;
35  }
36
37  async saveData(key: string, value: string, name: string): Promise<boolean> {
38    if (!this.appContext) {
39      HiLog.error(TAG, 'App context is null.');
40      return false;
41    }
42    try {
43      let preference = await data_preferences.getPreferences(this.appContext, name);
44      if (!preference) {
45        HiLog.error(TAG, 'Preference is null.');
46        return false;
47      }
48      await preference.put(key, value);
49      HiLog.debug(TAG, 'Preference put key-value success.');
50      await preference.flush();
51      HiLog.debug(TAG, 'Flush preference success.');
52      await data_preferences.removePreferencesFromCache(this.appContext, name);
53      HiLog.info(TAG, 'Save data success.');
54      return true;
55    } catch (err) {
56      HiLog.error(TAG, `Save data failed: ${JSON.stringify(err)}`);
57    }
58    return false;
59  }
60
61  async getData(key: string, name: string): Promise<data_preferences.ValueType> {
62    if (!this.appContext) {
63      HiLog.error(TAG, 'App context is null.');
64      return '';
65    }
66    try {
67      let preference = await data_preferences.getPreferences(this.appContext, name);
68      if (!preference) {
69        HiLog.error(TAG, 'Preference is null.');
70        return '';
71      }
72      let res = await preference.get(key, '');
73      HiLog.debug(TAG, 'Preference get value success.');
74      await data_preferences.removePreferencesFromCache(this.appContext, name);
75      HiLog.info(TAG, 'Get data success.');
76      return res;
77    } catch (err) {
78      HiLog.error(TAG, `Get data failed: ${JSON.stringify(err)}`);
79    }
80    return '';
81  }
82
83  async deleteData(key: string, name: string): Promise<void> {
84    if (!this.appContext) {
85      HiLog.error(TAG, 'App context is null.');
86      return;
87    }
88    try {
89      let preference = await data_preferences.getPreferences(this.appContext, name);
90      if (!preference) {
91        HiLog.error(TAG, 'Preference is null.');
92        return;
93      }
94      await preference.delete(key);
95      HiLog.debug(TAG, 'Preference delete key success.');
96      await preference.flush();
97      HiLog.debug(TAG, 'Flush preference success.');
98      await data_preferences.removePreferencesFromCache(this.appContext, name);
99    } catch (err) {
100      HiLog.error(TAG, `Delete data failed: ${JSON.stringify(err)}`);
101      return;
102    }
103    HiLog.info(TAG, 'Delete data success.');
104  }
105
106  public async deleteFile(fileName: string): Promise<void> {
107    if (!this.appContext) {
108      HiLog.error(TAG, 'App context is null.');
109      return;
110    }
111    try {
112      data_preferences.deletePreferences(this.appContext, fileName);
113      HiLog.info(TAG, 'Delete preferences success.');
114    } catch (err) {
115      HiLog.error(TAG, `Delete file failed: ${JSON.stringify(err)}`);
116    }
117  }
118}