• 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 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.helper = await dataShare.createDataShareHelper(this.context, this.uri);
48    Log.showInfo(TAG, `init helper ${this.helper}`);
49    this.registerBrightness();
50    this.getValue();
51  }
52
53  registerBrightness() {
54    this.helper.on("dataChange", this.uri, () => {
55      if (this.sliderChangeMode == 1) {
56        return;
57      }
58      let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault()));
59      Log.showInfo(TAG, `after brightness datachange settings getValue ${parseInt(data)}`);
60      mBrightnessValue.set(parseInt(data));
61    })
62  }
63
64  unRegisterBrightness() {
65    this.helper?.off("dataChange", this.uri, (err) => {
66      Log.showInfo(TAG, `unregister brightness helper`);
67    })
68  }
69
70  getValue() {
71    Log.showInfo(TAG, 'getValue');
72    let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault()));
73    Log.showInfo(TAG, `settings getValue ${parseInt(data)}`);
74    mBrightnessValue.set(parseInt(data));
75  }
76
77  setValue(value: number, sliderChangeMode: number) {
78    this.sliderChangeMode = sliderChangeMode;
79    Log.showInfo(TAG, `setValue ${value}`);
80    Brightness.setValue(value);
81  }
82
83  getMin(){
84    return parseInt(systemParameter.getSync('const.display.brightness.min'))
85  }
86
87  getMax(){
88    return parseInt(systemParameter.getSync('const.display.brightness.max'))
89  }
90
91  getDefault(){
92    return parseInt(systemParameter.getSync('const.display.brightness.default'))
93  }
94}
95
96let mBrightnessManager = createOrGet(brightnessManager, TAG);
97
98export default mBrightnessManager as brightnessManager;