diff --git a/common/utils/src/main/ets/default/baseUtil/ConfigData.ts b/common/utils/src/main/ets/default/baseUtil/ConfigData.ts index 1e53ed84da99dce33ecbda1395bbb25a5e54b898..c357da495440e8481e8b5ff4f7db6be7206d639f 100644 --- a/common/utils/src/main/ets/default/baseUtil/ConfigData.ts +++ b/common/utils/src/main/ets/default/baseUtil/ConfigData.ts @@ -19,6 +19,7 @@ export class ConfigData { FILE_URI = '/data/accounts/account_0/applications/com.ohos.settings' + '/com.ohos.settings/assets/phone/resources/rawfile/'; PREFERENCES_PATH = '/data/accounts/account_0/appdata/com.ohos.settings/sharedPreference/SettingPreferences'; + PREFERENCES_NAME = 'SettingPreferences'; BRIGHTNESS_SAVE_VALUE_KEY = 'BrightnessSaveValue'; SENT_EVENT_BROADCAST_BRIGHTNESS_VALUE = 'BRIGHTNESS_VALUE'; diff --git a/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts b/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts index c8ceec7338b65cf544498fbf979c938988a22b0a..c76621675442da6728985f501a92f6a34f53ef5f 100644 --- a/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts +++ b/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts @@ -13,38 +13,46 @@ * limitations under the License. */ -import ConfigData from './ConfigData'; +import ConfigData from './ConfigData' import LogUtil from './LogUtil' -//import Storage from '@ohos.data.storage'; +import Storage from '@ohos.data.storage' -//let preference = Storage.getStorageSync(ConfigData.PREFERENCES_PATH); -this.storage = await dataStorage.getPreferences(globalThis.settingsAbilityContext, ConfigData.PREFERENCES_PATH) export class PreferenceUtil { - /** - * Set up storage data - * - * @param key - key - * @param value - value - */ - setStorageValue(key,value): void { - LogUtil.info(`Set preference, key: ${key}, value: ${value}")`); - preference.putSync(key, value); - preference.flushSync(); - } + private preference: Storage.Storage - /** - * Get stored data - * - * @param key - key - * @param defaultValue - defaultValue - */ - getStorageValue(key: string, defaultValue) { - let value = preference.getSync(key, defaultValue); - LogUtil.info(`Get storage value, key: ${key}, value: ${value}`); - return value; - } + constructor() { + if (!this.preference) { + Storage.getStorage(globalThis.settingsAbilityContext.filesDir + '/' + ConfigData.PREFERENCES_NAME).then(data => { + this.preference = data + }) + } + } + /** + * Set up storage data + * + * @param key - key + * @param value - value + */ + setStorageValue(key, value): void { + LogUtil.info(`Set preference, key: ${key}, value: ${value}")`); + this.preference.putSync(key, value); + this.preference.flushSync(); + } + + /** + * Get stored data + * + * @param key - key + * @param defaultValue - defaultValue + */ + getStorageValue(key: string, defaultValue) { + let value = this.preference.getSync(key, defaultValue); + LogUtil.info(`Get storage value, key: ${key}, value: ${value}`); + return value; + } } let preferenceUtil = new PreferenceUtil(); + export default preferenceUtil as PreferenceUtil \ No newline at end of file diff --git a/product/phone/src/main/ets/MainAbility/MainAbility.ts b/product/phone/src/main/ets/MainAbility/MainAbility.ts index db4f5c4ea7a8efefffc3abce9d56de54272dd0d9..20a44a4aca1aa0eb56471ba5061deb37c8040942 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -34,7 +34,6 @@ export default class MainAbility extends Ability { LogUtil.log("[Main] MainAbility onWindowStageCreate") windowStage.setUIContent(this.context, "pages/settingList", null) - globalThis.settingsAbilityContext =this.context } onWindowStageDestroy() {