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 "sync_rule/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 #endif 35 } 36 GetCapacity()37int32_t BatteryStatus::GetCapacity() 38 { 39 int32_t capacity = DEFAULT_BATTERY_CAPCITY; 40 #ifdef SUPPORT_POWER 41 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance(); 42 capacity = batterySrvClient.GetCapacity(); 43 #endif 44 return capacity; 45 } 46 IsAllowUpload(bool forceFlag)47bool BatteryStatus::IsAllowUpload(bool forceFlag) 48 { 49 if (isCharging_) { 50 return true; 51 } 52 53 auto capacity = GetCapacity(); 54 if (capacity < STOP_CAPACITY_LIMIT) { 55 LOGE("power saving, stop upload"); 56 level_ = BatteryStatus::LEVEL_TOO_LOW; 57 return false; 58 } else if (capacity < PAUSE_CAPACITY_LIMIT) { 59 if (forceFlag) { 60 return true; 61 } else { 62 LOGE("power saving, pause upload"); 63 level_ = BatteryStatus::LEVEL_LOW; 64 return false; 65 } 66 } else { 67 level_ = BatteryStatus::LEVEL_NORMAL; 68 return true; 69 } 70 } 71 IsBatteryCapcityOkay()72bool BatteryStatus::IsBatteryCapcityOkay() 73 { 74 if (isCharging_) { 75 return true; 76 } 77 78 auto capacity = GetCapacity(); 79 if (capacity < STOP_CAPACITY_LIMIT) { 80 LOGE("battery capacity too low"); 81 level_ = BatteryStatus::LEVEL_TOO_LOW; 82 return false; 83 } 84 level_ = BatteryStatus::LEVEL_NORMAL; 85 return true; 86 } 87 GetCapacityLevel()88BatteryStatus::CapacityLevel BatteryStatus::GetCapacityLevel() 89 { 90 return level_; 91 } 92 IsCharging()93bool BatteryStatus::IsCharging() 94 { 95 return isCharging_; 96 } 97 } // namespace OHOS::FileManagement::CloudSync