1e75ebbc8Sopenharmony_ci//@ts-nocheck
2e75ebbc8Sopenharmony_ci/*
3e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
6e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
7e75ebbc8Sopenharmony_ci *
8e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
9e75ebbc8Sopenharmony_ci *
10e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
14e75ebbc8Sopenharmony_ci * limitations under the License.
15e75ebbc8Sopenharmony_ci */
16e75ebbc8Sopenharmony_ci
17e75ebbc8Sopenharmony_ciimport settings from '@ohos.settings';
18e75ebbc8Sopenharmony_ciimport Log from './Log';
19e75ebbc8Sopenharmony_ciimport Constants from './Constants';
20e75ebbc8Sopenharmony_ciimport createOrGet from './SingleInstanceHelper';
21e75ebbc8Sopenharmony_ciimport Context from 'application/ServiceExtensionContext';
22e75ebbc8Sopenharmony_ciimport AbilityManager from './abilitymanager/abilityManager';
23e75ebbc8Sopenharmony_ci
24e75ebbc8Sopenharmony_ciconst TAG = 'SettingsUtil';
25e75ebbc8Sopenharmony_ci
26e75ebbc8Sopenharmony_ciexport class SettingsUtil {
27e75ebbc8Sopenharmony_ci  context: Context;
28e75ebbc8Sopenharmony_ci
29e75ebbc8Sopenharmony_ci  constructor() {
30e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, 'constructor');
31e75ebbc8Sopenharmony_ci    this.context = AbilityManager.getContext(AbilityManager.ABILITY_NAME_CONTROL_PANEL);
32e75ebbc8Sopenharmony_ci  }
33e75ebbc8Sopenharmony_ci
34e75ebbc8Sopenharmony_ci  getValue(name: string, defValue?: string): string {
35e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `getValue, name: ${name} defValue: ${defValue}`);
36e75ebbc8Sopenharmony_ci    let value: string = null;
37e75ebbc8Sopenharmony_ci    if (this.context == undefined || this.context == null) {
38e75ebbc8Sopenharmony_ci      Log.showInfo(TAG,`getValue:${this.context}`);
39e75ebbc8Sopenharmony_ci      return defValue ? defValue : '';
40e75ebbc8Sopenharmony_ci    }
41e75ebbc8Sopenharmony_ci    try {
42e75ebbc8Sopenharmony_ci      value = settings.getValueSync(this.context, name, defValue ? defValue : '');
43e75ebbc8Sopenharmony_ci    } catch (e) {
44e75ebbc8Sopenharmony_ci      Log.showError(TAG, `getValue e: ${JSON.stringify(e)}`);
45e75ebbc8Sopenharmony_ci    }
46e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `getValue, value: ${value}`);
47e75ebbc8Sopenharmony_ci    return value;
48e75ebbc8Sopenharmony_ci  }
49e75ebbc8Sopenharmony_ci
50e75ebbc8Sopenharmony_ci  setValue(name: string, value: string): boolean {
51e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `setValue, name: ${name} value: ${value}`);
52e75ebbc8Sopenharmony_ci    let result = false;
53e75ebbc8Sopenharmony_ci    if (this.context == undefined || this.context == null) {
54e75ebbc8Sopenharmony_ci      Log.showInfo(TAG,`setValue:${this.context}`);
55e75ebbc8Sopenharmony_ci      return false;
56e75ebbc8Sopenharmony_ci    }
57e75ebbc8Sopenharmony_ci    try {
58e75ebbc8Sopenharmony_ci      result = settings.setValueSync(this.context, name, value);
59e75ebbc8Sopenharmony_ci    } catch (e) {
60e75ebbc8Sopenharmony_ci      Log.showError(TAG, `setValue e: ${JSON.stringify(e)}`);
61e75ebbc8Sopenharmony_ci    }
62e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `setValue, result: ${result}`);
63e75ebbc8Sopenharmony_ci    return result;
64e75ebbc8Sopenharmony_ci  }
65e75ebbc8Sopenharmony_ci}
66e75ebbc8Sopenharmony_ci
67e75ebbc8Sopenharmony_cilet sSettingsUtil = createOrGet(SettingsUtil, TAG);
68e75ebbc8Sopenharmony_ci
69e75ebbc8Sopenharmony_ciexport default sSettingsUtil;