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/charger_listener.h"
16
17 #include "battery_info.h"
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 {
ChargerEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,ChargerListener & listener)26 ChargerEventSubscriber::ChargerEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
27 ChargerListener &listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener) {}
28
OnReceiveEvent(const EventFwk::CommonEventData & data)29 void ChargerEventSubscriber::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_POWER_CONNECTED) {
35 int32_t type = data.GetCode();
36 switch (type) {
37 case static_cast<int32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC):
38 WS_HILOGI("Condition changed: CHARGER_PLUGGED_AC");
39 listener_.OnConditionChanged(WorkCondition::Type::CHARGER,
40 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_AC,
41 0, 0, std::string()));
42 break;
43 case static_cast<int32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB):
44 WS_HILOGI("Condition changed: CHARGER_PLUGGED_USB");
45 listener_.OnConditionChanged(WorkCondition::Type::CHARGER,
46 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_USB,
47 0, 0, std::string()));
48 break;
49 case static_cast<int32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS):
50 WS_HILOGI("Condition changed: CHARGER_WIRELESS");
51 listener_.OnConditionChanged(WorkCondition::Type::CHARGER,
52 std::make_shared<DetectorValue>(WorkCondition::CHARGING_PLUGGED_WIRELESS,
53 0, 0, std::string()));
54 break;
55 default:
56 break;
57 }
58 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
59 int32_t type = data.GetCode();
60 switch (type) {
61 case static_cast<int32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE):
62 case static_cast<int32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_BUTT):
63 WS_HILOGI("Condition changed: CHARGER_PLUGGED_UNPLUGGED");
64 listener_.OnConditionChanged(WorkCondition::Type::CHARGER,
65 std::make_shared<DetectorValue>(WorkCondition::CHARGING_UNPLUGGED, 0, 0, std::string()));
66 break;
67 default:
68 break;
69 }
70 }
71 }
72
CreateChargerEventSubscriber(ChargerListener & listener)73 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateChargerEventSubscriber(ChargerListener &listener)
74 {
75 EventFwk::MatchingSkills skills = EventFwk::MatchingSkills();
76 skills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
77 skills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
78 EventFwk::CommonEventSubscribeInfo info(skills);
79 return std::make_shared<ChargerEventSubscriber>(info, listener);
80 }
81
ChargerListener(std::shared_ptr<WorkQueueManager> workQueueManager)82 ChargerListener::ChargerListener(std::shared_ptr<WorkQueueManager> workQueueManager)
83 {
84 workQueueManager_ = workQueueManager;
85 }
86
~ChargerListener()87 ChargerListener::~ChargerListener()
88 {
89 this->Stop();
90 }
91
Start()92 bool ChargerListener::Start()
93 {
94 WS_HILOGI("Charger listener start");
95 this->commonEventSubscriber = CreateChargerEventSubscriber(*this);
96 return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
97 }
98
Stop()99 bool ChargerListener::Stop()
100 {
101 WS_HILOGI("Charger listener stop");
102 if (this->commonEventSubscriber != nullptr) {
103 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
104 if (result) {
105 this->commonEventSubscriber = nullptr;
106 }
107 return result;
108 }
109 return true;
110 }
111
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)112 void ChargerListener::OnConditionChanged(WorkCondition::Type conditionType,
113 std::shared_ptr<DetectorValue> conditionVal)
114 {
115 if (workQueueManager_ != nullptr) {
116 workQueueManager_->OnConditionChanged(conditionType, conditionVal);
117 } else {
118 WS_HILOGE("workQueueManager_ is nullptr.");
119 }
120 }
121 } // namespace WorkScheduler
122 } // namespace OHOS