• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 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 BatteryInfo from "@ohos.batteryInfo";
18import commonEvent from "@ohos.commonEvent";
19import createOrGet from "../../../../../../common/src/main/ets/default/SingleInstanceHelper";
20import Constants from "./common/constants";
21import Log from "../../../../../../common/src/main/ets/default/Log";
22import { CommonEventData } from "commonEvent/commonEventData";
23import {
24  CommonEventManager,
25  getCommonEventManager,
26  POLICY,
27} from "../../../../../../common/src/main/ets/default/commonEvent/CommonEventManager";
28
29const TAG = "BatteryComponent-batteryModelSc";
30const DEFAULT_PROGRESS = 100;
31const SUBSCRIBE_INFO = {
32  events: [commonEvent.Support.COMMON_EVENT_BATTERY_CHANGED],
33};
34
35function getChargingStatus(state: typeof BatteryInfo.BatteryChargeState): boolean {
36  Log.showDebug(TAG, `charging status update: ${state}`);
37  let batteryStatus = false;
38  switch (state) {
39    case BatteryInfo.BatteryChargeState.DISABLE:
40    case BatteryInfo.BatteryChargeState.ENABLE:
41    case BatteryInfo.BatteryChargeState.FULL:
42      batteryStatus = true;
43      break;
44    default:
45      batteryStatus = false;
46      break;
47  }
48  return batteryStatus;
49}
50
51export class BatteryModel {
52  private mBatterySoc: any;
53  private mBatteryCharging: any;
54  private mManager?: CommonEventManager;
55
56  initBatteryModel() {
57    if (this.mManager) {
58      return;
59    }
60    this.mManager = getCommonEventManager(
61      TAG,
62      SUBSCRIBE_INFO,
63      () => this.updateBatteryStatus(),
64      (isSubscribe: boolean) => isSubscribe && this.updateBatteryStatus()
65    );
66    Log.showDebug(TAG, "initBatteryModel");
67    this.mBatterySoc = AppStorage.SetAndLink("batterySoc", 0);
68    this.mBatteryCharging = AppStorage.SetAndLink("batteryCharging", false);
69    this.mManager.subscriberCommonEvent();
70    this.mManager.applyPolicy([POLICY.SCREEN_POLICY]);
71  }
72
73  unInitBatteryModel() {
74    Log.showDebug(TAG, "unInitBatteryModel");
75    this.mManager?.release();
76    this.mManager = undefined;
77  }
78
79  /**
80   * Get battery status and remaining power
81   */
82  private updateBatteryStatus() {
83    Log.showDebug(TAG, "updateBatteryStatus");
84    let batterySoc = BatteryInfo.batterySOC ?? DEFAULT_PROGRESS;
85    let batteryCharging = BatteryInfo.chargingStatus;
86    if (batterySoc <= 0) {
87      // If the result is a negative number, set it as positive number.
88      batterySoc = Math.abs(batterySoc) * Constants.PERCENT_NUMBER;
89    }
90
91    Log.showInfo(TAG, "batterySoc = " + batterySoc);
92
93    // Set the battery status as charging when there is no battery hardware
94    this.mBatterySoc.set(batterySoc);
95    this.mBatteryCharging.set(getChargingStatus(batteryCharging));
96  }
97}
98
99let mBatteryModel = createOrGet(BatteryModel, TAG);
100export default mBatteryModel as BatteryModel;
101