1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import commonEvent from '@ohos.commonEventManager'; 17import common from '@ohos.app.ability.common'; 18import consts from '../module/Consts'; 19import dataPreferences from '@ohos.data.preferences'; 20import Logger from '../module/Logger'; 21import surveillanceEventsManager from '../module/SurveillanceEventsManager'; 22 23export default class SettingFeature { 24 private innerContext: common.UIAbilityContext = null 25 private pref: dataPreferences.Preferences = null 26 27 constructor(abilityContext: common.UIAbilityContext) { 28 this.innerContext = abilityContext 29 } 30 31 async init() { 32 await dataPreferences.getPreferences(this.innerContext, consts.DATA_BASE_NAME).then((pref=>{ 33 this.pref = pref 34 })).catch(err=>{ 35 Logger.info(`getPreferences err ${JSON.stringify(err)}`) 36 }) 37 } 38 39 changeState(group: string, state: number) { 40 globalThis.settings.set(group, state); 41 let options = { 42 isSticky: true, 43 parameters: surveillanceEventsManager.getSurveillanceEventStates() 44 } 45 commonEvent.publish(consts.COMMON_EVENT_SETTING_UPDATE, options, () => { 46 Logger.info('success to publish setting update event') 47 }) 48 this.pref.put(group, state).then(() => { 49 this.pref.flush() 50 }) 51 } 52 53 checkStateForAlways(group: string): boolean { 54 return globalThis.settings.get(group) == consts.ENABLE_STATE_ALWAYS 55 } 56 57 checkStateForOnce(group: string): boolean { 58 return globalThis.settings.get(group) == consts.ENABLE_STATE_ONCE 59 } 60 61 checkStateForNever(group: string): boolean { 62 return globalThis.settings.get(group) == consts.ENABLE_STATE_NEVER 63 } 64 65 changeStateToAlways(group: string) { 66 this.changeState(group, consts.ENABLE_STATE_ALWAYS) 67 } 68 69 changeStateToOnce(group: string) { 70 this.changeState(group, consts.ENABLE_STATE_ONCE) 71 } 72 73 changeStateToNever(group: string) { 74 this.changeState(group, consts.ENABLE_STATE_NEVER) 75 } 76}