1 /* 2 * Copyright (c) 2023-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 16 #include "battery_level_strategy.h" 17 18 #ifdef CAMERA_USE_BATTERY 19 #include "battery_info.h" 20 #endif 21 #include "events_monitor.h" 22 23 namespace OHOS { 24 namespace CameraStandard { 25 namespace DeferredProcessing { 26 namespace { 27 constexpr int32_t DEFAULT_CAPACITY = -1; 28 constexpr int32_t BATTERY_THRESHOLD = 50; 29 const std::string KEY_CAPACITY = "soc"; 30 } 31 BatteryLevelStrategy()32BatteryLevelStrategy::BatteryLevelStrategy() : preLevel_(BATTERY_LEVEL_LOW) 33 { 34 DP_DEBUG_LOG("entered."); 35 } 36 ~BatteryLevelStrategy()37BatteryLevelStrategy::~BatteryLevelStrategy() 38 { 39 DP_DEBUG_LOG("entered."); 40 } 41 handleEvent(const EventFwk::CommonEventData & data)42void BatteryLevelStrategy::handleEvent(const EventFwk::CommonEventData& data) 43 { 44 int32_t capacity = data.GetWant().GetIntParam(KEY_CAPACITY, DEFAULT_CAPACITY); 45 DP_CHECK_RETURN(capacity == DEFAULT_CAPACITY); 46 int32_t batteryLevel = BatteryLevel::BATTERY_LEVEL_LOW; 47 if (capacity >= BATTERY_THRESHOLD) { 48 batteryLevel = BatteryLevel::BATTERY_LEVEL_OKAY; 49 } 50 DP_CHECK_RETURN(batteryLevel == preLevel_); 51 52 preLevel_ = batteryLevel; 53 DP_INFO_LOG("OnBatteryLevelChanged level:%{public}d", preLevel_); 54 EventsMonitor::GetInstance().NotifyBatteryLevel(preLevel_); 55 } 56 } // namespace DeferredProcessing 57 } // namespace CameraStandard 58 } // namespace OHOS