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