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