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 #ifdef POWERMGR_BATTERY_MANAGER_ENABLE
16 #include "conditions/battery_level_listener.h"
17
18 #include <string>
19
20 #include "battery_info.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "matching_skills.h"
24 #include "want.h"
25 #include "work_sched_hilog.h"
26 #include "work_sched_utils.h"
27
28 namespace OHOS {
29 namespace WorkScheduler {
BatteryLevelEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,BatteryLevelListener & listener)30 BatteryLevelEventSubscriber::BatteryLevelEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
31 BatteryLevelListener &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {}
32
OnReceiveEvent(const EventFwk::CommonEventData & data)33 void BatteryLevelEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
34 {
35 const std::string action = data.GetWant().GetAction();
36
37 WS_HILOGD("OnReceiveEvent get action: %{public}s", action.c_str());
38 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED) {
39 std::string KEY_CAPACITY = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CAPACITY;
40 int32_t defaultCapacity = -1;
41 auto capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultCapacity);
42 if (capacity == defaultCapacity || capacity == capacity_) {
43 return;
44 }
45 capacity_ = capacity;
46 WS_HILOGI("capacity: %{public}d", capacity);
47 listener_.OnConditionChanged(WorkCondition::Type::BATTERY_LEVEL,
48 std::make_shared<DetectorValue>(capacity, 0, 0, std::string()));
49 } else {
50 WS_HILOGI("action is invalid");
51 }
52 }
53
CreateBatteryEventSubscriber(BatteryLevelListener & listener)54 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateBatteryEventSubscriber(BatteryLevelListener &listener)
55 {
56 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
57 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
58 EventFwk::CommonEventSubscribeInfo info(skill);
59 return std::make_shared<BatteryLevelEventSubscriber>(info, listener);
60 }
61
BatteryLevelListener(std::shared_ptr<WorkQueueManager> workQueueManager,std::shared_ptr<WorkSchedulerService> service)62 BatteryLevelListener::BatteryLevelListener(std::shared_ptr<WorkQueueManager> workQueueManager,
63 std::shared_ptr<WorkSchedulerService> service)
64 {
65 workQueueManager_ = workQueueManager;
66 service_ = service;
67 }
68
~BatteryLevelListener()69 BatteryLevelListener::~BatteryLevelListener()
70 {
71 this->Stop();
72 }
73
Start()74 bool BatteryLevelListener::Start()
75 {
76 WS_HILOGD("Battery level listener start.");
77 this->commonEventSubscriber = CreateBatteryEventSubscriber(*this);
78 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
79 }
80
Stop()81 bool BatteryLevelListener::Stop()
82 {
83 WS_HILOGD("Battery level listener stop.");
84 if (this->commonEventSubscriber != nullptr) {
85 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
86 if (result) {
87 this->commonEventSubscriber = nullptr;
88 }
89 return result;
90 }
91 return true;
92 }
93
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)94 void BatteryLevelListener::OnConditionChanged(WorkCondition::Type conditionType,
95 std::shared_ptr<DetectorValue> conditionVal)
96 {
97 if (workQueueManager_ != nullptr) {
98 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
99 } else {
100 WS_HILOGE("workQueueManager_ is nullptr.");
101 }
102 }
103 } // namespace WorkScheduler
104 } // namespace OHOS
105 #endif // POWERMGR_BATTERY_MANAGER_ENABLE