1//@ts-nocheck
2/*
3 * Copyright (c) 2021-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 settings from '@ohos.settings';
18import Log from './Log';
19import Constants from './Constants';
20import createOrGet from './SingleInstanceHelper';
21import Context from 'application/ServiceExtensionContext';
22import AbilityManager from './abilitymanager/abilityManager';
23
24const TAG = 'SettingsUtil';
25
26export class SettingsUtil {
27  context: Context;
28
29  constructor() {
30    Log.showDebug(TAG, 'constructor');
31    this.context = AbilityManager.getContext(AbilityManager.ABILITY_NAME_CONTROL_PANEL);
32  }
33
34  getValue(name: string, defValue?: string): string {
35    Log.showDebug(TAG, `getValue, name: ${name} defValue: ${defValue}`);
36    let value: string = null;
37    if (this.context == undefined || this.context == null) {
38      Log.showInfo(TAG,`getValue:${this.context}`);
39      return defValue ? defValue : '';
40    }
41    try {
42      value = settings.getValueSync(this.context, name, defValue ? defValue : '');
43    } catch (e) {
44      Log.showError(TAG, `getValue e: ${JSON.stringify(e)}`);
45    }
46    Log.showDebug(TAG, `getValue, value: ${value}`);
47    return value;
48  }
49
50  setValue(name: string, value: string): boolean {
51    Log.showDebug(TAG, `setValue, name: ${name} value: ${value}`);
52    let result = false;
53    if (this.context == undefined || this.context == null) {
54      Log.showInfo(TAG,`setValue:${this.context}`);
55      return false;
56    }
57    try {
58      result = settings.setValueSync(this.context, name, value);
59    } catch (e) {
60      Log.showError(TAG, `setValue e: ${JSON.stringify(e)}`);
61    }
62    Log.showDebug(TAG, `setValue, result: ${result}`);
63    return result;
64  }
65}
66
67let sSettingsUtil = createOrGet(SettingsUtil, TAG);
68
69export default sSettingsUtil;