1 /*
2  * Copyright (c) 2023-2023 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 
16 #include "events_subscriber.h"
17 
18 #include "battery_level_strategy.h"
19 #include "battery_strategy.h"
20 #include "common_event_subscribe_info.h"
21 #include "common_event_subscriber.h"
22 #include "common_event_support.h"
23 #include "charging_strategy.h"
24 #include "dp_utils.h"
25 #include "screen_strategy.h"
26 #include "thermal_strategy.h"
27 
28 namespace OHOS {
29 namespace CameraStandard {
30 namespace DeferredProcessing {
31 const std::vector<std::string> EventSubscriber::events_ = {
32     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED,
33     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON,
34     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF,
35     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING,
36     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING,
37     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY,
38     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW,
39     OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED,
40 };
41 
EventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)42 EventSubscriber::EventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo& subscriberInfo)
43     : CommonEventSubscriber(subscriberInfo)
44 {
45     DP_DEBUG_LOG("entered.");
46     Initialize();
47 }
48 
~EventSubscriber()49 EventSubscriber::~EventSubscriber()
50 {
51     DP_DEBUG_LOG("entered.");
52     eventStrategy_.clear();
53 }
54 
Create()55 std::shared_ptr<EventSubscriber> EventSubscriber::Create()
56 {
57     DP_DEBUG_LOG("entered.");
58     OHOS::EventFwk::MatchingSkills matchingSkills;
59     for (auto event : events_) {
60         matchingSkills.AddEvent(event);
61     }
62     EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
63     return CreateShared<EventSubscriber>(subscriberInfo);
64 }
65 
Initialize()66 void EventSubscriber::Initialize()
67 {
68     DP_DEBUG_LOG("entered.");
69     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED]
70         = std::make_shared<ThermalStrategy>();
71     auto screen = std::make_shared<ScreenStrategy>();
72     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON]
73         = screen;
74     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF]
75         = screen;
76     auto charging = std::make_shared<ChargingStrategy>();
77     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING]
78         = charging;
79     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING]
80         = charging;
81     auto batteryState = std::make_shared<BatteryStrategy>();
82     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY]
83         = batteryState;
84     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW]
85         = batteryState;
86     eventStrategy_[OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED]
87         = std::make_shared<BatteryLevelStrategy>();
88 }
89 
Subcribe()90 void EventSubscriber::Subcribe()
91 {
92     DP_CHECK_ERROR_RETURN_LOG(!EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this()),
93         "SubscribeCommonEvent failed");
94     DP_INFO_LOG("SubscribeCommonEvent OK");
95 }
96 
UnSubscribe()97 void EventSubscriber::UnSubscribe()
98 {
99     DP_CHECK_ERROR_RETURN_LOG(!EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this()),
100         "UnSubscribeCommonEvent failed");
101     DP_INFO_LOG("UnSubscribeCommonEvent OK");
102 }
103 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)104 void EventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData& data)
105 {
106     AAFwk::Want want = data.GetWant();
107     auto action = want.GetAction();
108     DP_INFO_LOG("EventSubscriber::OnReceiveEvent: %{public}s.", action.c_str());
109     auto entry = eventStrategy_.find(action);
110     if (entry != eventStrategy_.end()) {
111         auto strategy = entry->second;
112         if (strategy != nullptr) {
113             strategy->handleEvent(data);
114         }
115     }
116     DP_DEBUG_LOG("EventSubscriber::OnReceiveEvent: end");
117 }
118 } // namespace DeferredProcessing
119 } // namespace CameraStandard
120 } // namespace OHOS