1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import BatteryInfo from "@ohos.batteryInfo"; 17import commonEvent from "@ohos.commonEvent"; 18import createOrGet from "../../../../../../common/src/main/ets/default/SingleInstanceHelper"; 19import Constants from "./common/constants"; 20import Log from "../../../../../../common/src/main/ets/default/Log"; 21import {CommonEventData} from "commonEvent/commonEventData"; 22import {CommonEventManager, getCommonEventManager, POLICY} from "../../../../../../common/src/main/ets/default/commonEvent/CommonEventManager"; 23 24const TAG = "BatteryComponent-batteryModel"; 25const DEFAULT_PROGRESS = 100; 26const SUBSCRIBE_INFO = { 27 events: [commonEvent.Support.COMMON_EVENT_BATTERY_CHANGED], 28}; 29 30function getChargingStatus(state: BatteryInfo.BatteryChargeState): boolean { 31 Log.showDebug(TAG, `charging status update: ${state}`); 32 let batteryStatus = false; 33 switch (state) { 34 case BatteryInfo.BatteryChargeState.DISABLE: 35 case BatteryInfo.BatteryChargeState.ENABLE: 36 case BatteryInfo.BatteryChargeState.FULL: 37 batteryStatus = true; 38 break; 39 default: 40 batteryStatus = false; 41 break; 42 } 43 return batteryStatus; 44} 45 46export class BatteryModel { 47 private mBatterySoc: any; 48 private mBatteryCharging: any; 49 private mManager?: CommonEventManager; 50 51 initBatteryModel() { 52 if (this.mManager) { 53 return; 54 } 55 this.mManager = getCommonEventManager( 56 TAG, 57 SUBSCRIBE_INFO, 58 () => this.updateBatteryStatus(), 59 (isSubscribe: boolean) => isSubscribe && this.updateBatteryStatus() 60 ); 61 Log.showDebug(TAG, "initBatteryModel"); 62 this.mBatterySoc = AppStorage.SetAndLink("batterySoc", 0); 63 this.mBatteryCharging = AppStorage.SetAndLink("batteryCharging", false); 64 this.mManager.subscriberCommonEvent(); 65 this.mManager.applyPolicy([POLICY.SCREEN_POLICY]); 66 } 67 68 unInitBatteryModel() { 69 Log.showDebug(TAG, "unInitBatteryModel"); 70 this.mManager?.release(); 71 this.mManager = undefined; 72 } 73 74 /** 75 * Get battery status and remaining power 76 */ 77 private updateBatteryStatus() { 78 Log.showDebug(TAG, "updateBatteryStatus"); 79 let batterySoc = BatteryInfo.batterySOC ?? DEFAULT_PROGRESS; 80 let batteryCharging = BatteryInfo.chargingStatus; 81 if (batterySoc <= 0) { 82 // If the result is a negative number, set it as positive number. 83 batterySoc = Math.abs(batterySoc) * Constants.PERCENT_NUMBER; 84 } 85 86 Log.showDebug(TAG, "batterySoc = " + batterySoc); 87 88 // Set the battery status as charging when there is no battery hardware 89 this.mBatterySoc.set(batterySoc); 90 this.mBatteryCharging.set(getChargingStatus(batteryCharging)); 91 } 92} 93 94let mBatteryModel = createOrGet(BatteryModel, TAG); 95export default mBatteryModel as BatteryModel; 96