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