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 Log from '../../../../../../common/src/main/ets/default/Log'; 18import AbilityManager from '../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'; 19import commonEvent from '@ohos.commonEvent'; 20import settings from '@ohos.settings'; 21import systemParameter from '@ohos.systemparameter' 22import dataShare from '@ohos.data.dataShare'; 23import Brightness from '@ohos.brightness'; 24import Context from 'application/ServiceExtensionContext'; 25import Constants from "../../../../../../common/src/main/ets/default/Constants"; 26import createOrGet from '../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 27 28const TAG = 'Control-brightnessManager'; 29var mBrightnessValue = AppStorage.SetAndLink('BrightnessValue', 100); 30 31export class brightnessManager { 32 helper: dataShare.DataShareHelper; 33 uri: string; 34 context: Context; 35 SLIDER_CHANG_MODE_MOVING = 1; 36 private sliderChangeMode: number; 37 38 constructor() { 39 this.uri = Constants.getUriSync(Constants.KEY_BRIGHTNESS_STATUS); 40 Log.showInfo(TAG, 'settings geturi of brightness is ' + Constants.URI_VAR); 41 this.context = AbilityManager.getContext(AbilityManager.getContextName(AbilityManager.ABILITY_NAME_CONTROL_PANEL)); 42 this.init(); 43 } 44 45 async init(): Promise<void> { 46 Log.showInfo(TAG, 'init'); 47 this.createDataShare() 48 Log.showInfo(TAG, `init helper ${this.helper}`); 49 } 50 51 public createDataShare() { 52 const UPDATE_INTERVAL = 500; 53 const timer = setInterval(() => { 54 dataShare.createDataShareHelper(this.context, this.uri) 55 .then((dataHelper) => { 56 Log.showInfo(TAG, `createDataShareHelper success.`); 57 this.helper = dataHelper; 58 this.registerBrightness(); 59 this.getValue(); 60 clearInterval(timer); 61 }) 62 .catch((err: BusinessError) => { 63 Log.showError(TAG, `createDataShare fail. ${JSON.stringify(err)}`); 64 }); 65 }, UPDATE_INTERVAL); 66 } 67 68 registerBrightness() { 69 this.helper.on("dataChange", this.uri, () => { 70 if (this.sliderChangeMode == 1) { 71 return; 72 } 73 if (this.context == undefined || this.context == null) { 74 Log.showInfo(TAG, `registerBrightness: ${context}`); 75 return; 76 } 77 try { 78 let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault())); 79 Log.showDebug(TAG, `after brightness datachange settings getValue ${parseInt(data)}`); 80 mBrightnessValue.set(parseInt(data)); 81 } catch (err) { 82 Log.showError(TAG, `registerBrightness: ${context}, ${JSON.stringify(err)}`); 83 } 84 85 }) 86 } 87 88 unRegisterBrightness() { 89 this.helper?.off("dataChange", this.uri, (err) => { 90 Log.showInfo(TAG, `unregister brightness helper`); 91 }) 92 } 93 94 getValue() { 95 Log.showDebug(TAG, 'getValue'); 96 if (this.context == undefined || this.context == null) { 97 Log.showInfo(TAG, `getValue: ${context}`); 98 return; 99 } 100 try { 101 let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault())); 102 Log.showInfo(TAG, `settings getValue ${parseInt(data)}`); 103 mBrightnessValue.set(parseInt(data)); 104 } catch (err) { 105 Log.showError(TAG, `getValue: ${context}, ${JSON.stringify(err)}`); 106 } 107 } 108 109 setValue(value: number, sliderChangeMode: number) { 110 this.sliderChangeMode = sliderChangeMode; 111 Log.showInfo(TAG, `setValue ${value}`); 112 Brightness.setValue(value); 113 } 114 115 getMin(){ 116 return parseInt(systemParameter.getSync('const.display.brightness.min')) 117 } 118 119 getMax(){ 120 return parseInt(systemParameter.getSync('const.display.brightness.max')) 121 } 122 123 getDefault(){ 124 return parseInt(systemParameter.getSync('const.display.brightness.default')) 125 } 126} 127 128let mBrightnessManager = createOrGet(brightnessManager, TAG); 129 130export default mBrightnessManager as brightnessManager;