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_status_listener.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "matching_skills.h"
21 #include "want.h"
22 #include "work_sched_hilog.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
BatteryStatusEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,BatteryStatusListener & listener)26 BatteryStatusEventSubscriber::BatteryStatusEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
27 BatteryStatusListener &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {}
28
OnReceiveEvent(const EventFwk::CommonEventData & data)29 void BatteryStatusEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
30 {
31 const std::string action = data.GetWant().GetAction();
32 WS_HILOGD("OnReceiveEvent get action: %{public}s", action.c_str());
33
34 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW) {
35 WS_HILOGI("Condition changed: BATTERY_STATUS_LOW");
36 listener_.OnConditionChanged(WorkCondition::Type::BATTERY_STATUS,
37 std::make_shared<DetectorValue>(WorkCondition::BATTERY_STATUS_LOW, 0, 0, std::string()));
38 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY) {
39 WS_HILOGI("Condition changed: BATTERY_STATUS_OKAY");
40 listener_.OnConditionChanged(WorkCondition::Type::BATTERY_STATUS,
41 std::make_shared<DetectorValue>(WorkCondition::BATTERY_STATUS_OKAY, 0, 0, std::string()));
42 } else {
43 WS_HILOGI("OnReceiveEvent action is invalid");
44 }
45 }
46
CreateBatteryEventSubscriber(BatteryStatusListener & listener)47 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateBatteryEventSubscriber(BatteryStatusListener &listener)
48 {
49 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
50 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
51 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
52 EventFwk::CommonEventSubscribeInfo info(skill);
53 return std::make_shared<BatteryStatusEventSubscriber>(info, listener);
54 }
55
BatteryStatusListener(std::shared_ptr<WorkQueueManager> workQueueManager)56 BatteryStatusListener::BatteryStatusListener(std::shared_ptr<WorkQueueManager> workQueueManager)
57 {
58 workQueueManager_ = workQueueManager;
59 }
60
~BatteryStatusListener()61 BatteryStatusListener::~BatteryStatusListener()
62 {
63 this->Stop();
64 }
65
Start()66 bool BatteryStatusListener::Start()
67 {
68 WS_HILOGD("Battery status listener start.");
69 this->commonEventSubscriber = CreateBatteryEventSubscriber(*this);
70 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
71 }
72
Stop()73 bool BatteryStatusListener::Stop()
74 {
75 WS_HILOGD("Battery status listener stop.");
76 if (this->commonEventSubscriber != nullptr) {
77 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
78 if (result) {
79 this->commonEventSubscriber = nullptr;
80 }
81 return result;
82 }
83 return true;
84 }
85
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)86 void BatteryStatusListener::OnConditionChanged(WorkCondition::Type conditionType,
87 std::shared_ptr<DetectorValue> conditionVal)
88 {
89 if (workQueueManager_ != nullptr) {
90 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
91 } else {
92 WS_HILOGE("workQueueManager_ is nullptr.");
93 }
94 }
95 } // namespace WorkScheduler
96 } // namespace OHOS
97 #endif // POWERMGR_BATTERY_MANAGER_ENABLE