1 /* 2 * Copyright (c) 2023 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 #include "battery_status.h" 16 #include "utils_log.h" 17 18 namespace OHOS::FileManagement::CloudSync { 19 constexpr int32_t PAUSE_CAPACITY_LIMIT = 15; 20 constexpr int32_t STOP_CAPACITY_LIMIT = 10; 21 constexpr int32_t DEFAULT_BATTERY_CAPCITY = 100; 22 SetChargingStatus(bool status)23void BatteryStatus::SetChargingStatus(bool status) 24 { 25 isCharging_ = status; 26 } 27 GetInitChargingStatus()28void BatteryStatus::GetInitChargingStatus() 29 { 30 #ifdef SUPPORT_POWER 31 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance(); 32 auto chargingStatus = batterySrvClient.GetChargingStatus(); 33 isCharging_ = (chargingStatus == PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE || 34 chargingStatus == PowerMgr::BatteryChargeState::CHARGE_STATE_FULL); 35 #endif 36 } 37 GetCapacity()38int32_t BatteryStatus::GetCapacity() 39 { 40 int32_t capacity = DEFAULT_BATTERY_CAPCITY; 41 #ifdef SUPPORT_POWER 42 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance(); 43 capacity = batterySrvClient.GetCapacity(); 44 #endif 45 return capacity; 46 } 47 IsAllowUpload(bool forceFlag)48bool BatteryStatus::IsAllowUpload(bool forceFlag) 49 { 50 if (isCharging_) { 51 return true; 52 } 53 54 auto capacity = GetCapacity(); 55 if (capacity < STOP_CAPACITY_LIMIT) { 56 LOGE("power saving, stop upload"); 57 level_ = BatteryStatus::LEVEL_TOO_LOW; 58 return false; 59 } else if (capacity < PAUSE_CAPACITY_LIMIT) { 60 if (forceFlag) { 61 return true; 62 } else { 63 LOGE("power saving, pause upload"); 64 level_ = BatteryStatus::LEVEL_LOW; 65 return false; 66 } 67 } else { 68 level_ = BatteryStatus::LEVEL_NORMAL; 69 return true; 70 } 71 } 72 IsBatteryCapcityOkay()73bool BatteryStatus::IsBatteryCapcityOkay() 74 { 75 if (isCharging_) { 76 return true; 77 } 78 79 auto capacity = GetCapacity(); 80 if (capacity < STOP_CAPACITY_LIMIT) { 81 LOGE("battery capacity too low"); 82 level_ = BatteryStatus::LEVEL_TOO_LOW; 83 return false; 84 } 85 level_ = BatteryStatus::LEVEL_NORMAL; 86 return true; 87 } 88 GetCapacityLevel()89BatteryStatus::CapacityLevel BatteryStatus::GetCapacityLevel() 90 { 91 return level_; 92 } 93 IsCharging()94bool BatteryStatus::IsCharging() 95 { 96 return isCharging_; 97 } 98 } // namespace OHOS::FileManagement::CloudSync