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