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