1//@ts-nocheck 2/* 3 * Copyright (c) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import { Log } from '../utils/Log'; 18import settings from '@ohos.settings'; 19import dataShare from '@ohos.data.dataShare'; 20import common from '@ohos.app.ability.common'; 21import { Context } from '@ohos.abilityAccessCtrl'; 22import { BusinessError } from '@ohos.base'; 23 24const TAG = 'SettingsDataManager' 25/** 26 * Wrapper class for settings interfaces. 27 */ 28class SettingsDataManager { 29 private readonly uriShare: string = 'datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key='; 30 private dataShareHelper: dataShare.DataShareHelper | null = null; 31 private constructor() { 32 } 33 34 /** 35 * settingsData manager instance 36 * 37 * @return settingsDataManager instance 38 */ 39 static getInstance(): SettingsDataManager { 40 if (globalThis.SettingsDataManagerInstance == null) { 41 globalThis.SettingsDataManagerInstance = new SettingsDataManager(); 42 } 43 return globalThis.SettingsDataManagerInstance; 44 } 45 46 public createDataShareHelper() { 47 Log.showInfo(TAG, 'createDataShareHelper context:' + globalThis.desktopContext); 48 const UPDATE_INTERVAL = 30; 49 const timer = setInterval(() => { 50 dataShare.createDataShareHelper(globalThis.desktopContext, this.uriShare) 51 .then((dataHelper) => { 52 Log.showInfo(TAG, `createDataShareHelper success.`); 53 this.dataShareHelper = dataHelper; 54 globalThis.sGestureNavigationManager.getGestureNavigationStatus(); 55 clearInterval(timer); 56 }) 57 .catch((err: BusinessError) => { 58 Log.showError(TAG, `createDataShareHelper fail. ${JSON.stringify(err)}`); 59 }); 60 }, UPDATE_INTERVAL); 61 } 62 63 /** 64 * Update settingData by settingDataKey. 65 */ 66 setValue(helper: dataShare.DataShareHelper | null, settingDataKey: string, value: string): void { 67 Log.showInfo(TAG, "setValue:" + value) 68 if (typeof globalThis.desktopContext === 'undefined') { 69 settings.setValueSync(globalThis.settingsContext as Context, settingDataKey, value); 70 } else { 71 settings.setValueSync(globalThis.desktopContext as Context, settingDataKey, value); 72 } 73 } 74 75 /** 76 * get settingDataValue by settingDataKey. 77 * 78 * @return settingsDataValue by settingDataKey. 79 */ 80 getValue(helper: dataShare.DataShareHelper | null, settingDataKey: string, defaultValue: string): string { 81 let value: string = '1'; 82 if (typeof globalThis.desktopContext === 'undefined') { 83 value = settings.getValueSync(globalThis.settingsContext as Context, settingDataKey, defaultValue); 84 } else { 85 value = settings.getValueSync(globalThis.desktopContext as Context, settingDataKey, defaultValue); 86 } 87 Log.showInfo(TAG, "getValue:" + value); 88 return value; 89 } 90 91 /** 92 * get settingDataUri by settingDataKey. 93 * 94 * @return settingDataUri by settingDataKey. 95 */ 96 getUri(settingDataKey: string): string { 97 return this.uriShare + settingDataKey; 98 } 99 100 /** 101 * get settingDataHelper by settingDataKey. 102 * 103 * @return settingDataHelper by settingDataUri. 104 */ 105 getHelper(context: common.Context, uri: string): dataShare.DataShareHelper | null { 106 return this.dataShareHelper; 107 } 108} 109 110export const settingsDataManager = SettingsDataManager.getInstance();