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