• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    try {
38      value = settings.getValueSync(this.context, name, defValue ? defValue : '');
39    } catch (e) {
40      Log.showError(TAG, `getValue e: ${JSON.stringify(e)}`);
41    }
42    Log.showDebug(TAG, `getValue, value: ${value}`);
43    return value;
44  }
45
46  setValue(name: string, value: string): boolean {
47    Log.showDebug(TAG, `setValue, name: ${name} value: ${value}`);
48    let result = false;
49    try {
50      result = settings.setValueSync(this.context, name, value);
51    } catch (e) {
52      Log.showError(TAG, `setValue e: ${JSON.stringify(e)}`);
53    }
54    Log.showDebug(TAG, `setValue, result: ${result}`);
55    return result;
56  }
57}
58
59let sSettingsUtil = createOrGet(SettingsUtil, TAG);
60
61export default sSettingsUtil;