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