• 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.createDataShare()
48    Log.showInfo(TAG, `init helper ${this.helper}`);
49  }
50
51  public createDataShare() {
52    if (this.context == undefined || this.context == null) {
53      Log.showInfo(TAG, `constructor, this.context is null`);
54      this.context = AbilityManager.getContext(AbilityManager.getContextName(AbilityManager.ABILITY_NAME_CONTROL_PANEL));
55    }
56    Log.showInfo(TAG, `createDataShare, this.context ${this.context}`);
57    const UPDATE_INTERVAL = 500;
58    const timer = setInterval(() => {
59      dataShare.createDataShareHelper(this.context, this.uri)
60        .then((dataHelper) => {
61          Log.showInfo(TAG, `createDataShareHelper success.`);
62          this.helper = dataHelper;
63          this.registerBrightness();
64          this.getValue();
65          clearInterval(timer);
66        })
67        .catch((err: BusinessError) => {
68          Log.showError(TAG, `createDataShare fail. ${JSON.stringify(err)}`);
69        });
70    }, UPDATE_INTERVAL);
71  }
72
73  registerBrightness() {
74    this.helper.on("dataChange", this.uri, () => {
75      if (this.sliderChangeMode == 1) {
76        return;
77      }
78      if (this.context == undefined || this.context == null) {
79        Log.showInfo(TAG, `registerBrightness: ${context}`);
80        return;
81      }
82      try {
83        let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault()));
84        Log.showDebug(TAG, `after brightness datachange settings getValue ${parseInt(data)}`);
85        mBrightnessValue.set(parseInt(data));
86      } catch (err) {
87        Log.showError(TAG, `registerBrightness: ${context}, ${JSON.stringify(err)}`);
88      }
89
90    })
91  }
92
93  unRegisterBrightness() {
94    this.helper?.off("dataChange", this.uri, (err) => {
95      Log.showInfo(TAG, `unregister brightness helper`);
96    })
97  }
98
99  getValue() {
100    Log.showDebug(TAG, 'getValue');
101    if (this.context == undefined || this.context == null) {
102      Log.showInfo(TAG, `getValue: ${context}`);
103      return;
104    }
105    try {
106      let data = settings.getValueSync(this.context, Constants.KEY_BRIGHTNESS_STATUS, JSON.stringify(this.getDefault()));
107      Log.showInfo(TAG, `settings getValue ${parseInt(data)}`);
108      mBrightnessValue.set(parseInt(data));
109    } catch (err) {
110      Log.showError(TAG, `getValue: ${context}, ${JSON.stringify(err)}`);
111    }
112  }
113
114  setValue(value: number, sliderChangeMode: number) {
115    this.sliderChangeMode = sliderChangeMode;
116    Log.showInfo(TAG, `setValue ${value}`);
117    Brightness.setValue(value);
118  }
119
120  getMin(){
121    return parseInt(systemParameter.getSync('const.display.brightness.min'))
122  }
123
124  getMax(){
125    return parseInt(systemParameter.getSync('const.display.brightness.max'))
126  }
127
128  getDefault(){
129    return parseInt(systemParameter.getSync('const.display.brightness.default'))
130  }
131}
132
133let mBrightnessManager = createOrGet(brightnessManager, TAG);
134
135export default mBrightnessManager as brightnessManager;