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 "common_event_handler.h"
16
17 #include "common_event_support.h"
18 #include "itag_host.h"
19 #include "loghelper.h"
20 #include "want.h"
21
22 namespace OHOS {
23 namespace NFC {
24
25 class CommonEventHandler::ScreenChangedReceiver : public EventFwk::CommonEventSubscriber {
26 public:
27 explicit ScreenChangedReceiver(std::weak_ptr<NfcService> nfcService,
28 const EventFwk::CommonEventSubscribeInfo& subscribeInfo);
~ScreenChangedReceiver()29 ~ScreenChangedReceiver()
30 {
31 }
32 void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
33
34 private:
35 std::weak_ptr<NfcService> nfcService_ {};
36 std::weak_ptr<AppExecFwk::EventHandler> eventHandler_ {};
37 };
38
ScreenChangedReceiver(std::weak_ptr<NfcService> nfcService,const EventFwk::CommonEventSubscribeInfo & subscribeInfo)39 CommonEventHandler::ScreenChangedReceiver::ScreenChangedReceiver(std::weak_ptr<NfcService> nfcService,
40 const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
41 : EventFwk::CommonEventSubscriber(subscribeInfo),
42 nfcService_(nfcService),
43 eventHandler_(nfcService.lock()->eventHandler_)
44 {
45 }
46
OnReceiveEvent(const EventFwk::CommonEventData & data)47 void CommonEventHandler::ScreenChangedReceiver::OnReceiveEvent(const EventFwk::CommonEventData& data)
48 {
49 std::string action = data.GetWant().GetAction();
50 if (action.empty()) {
51 ErrorLog("action is empty");
52 return;
53 }
54
55 ScreenState screenState = ScreenState::SCREEN_STATE_UNKNOWN;
56 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) == 0) {
57 screenState = ScreenState::SCREEN_STATE_ON_UNLOCKED;
58 } else if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) == 0) {
59 screenState = ScreenState::SCREEN_STATE_OFF_UNLOCKED;
60 } else {
61 ErrorLog("Screen changed receiver event:unknown");
62 return;
63 }
64 eventHandler_.lock()->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_SCREEN_CHANGED),
65 static_cast<int64_t>(screenState), static_cast<int64_t>(0));
66 }
67
68 class CommonEventHandler::PackageChangedReceiver : public EventFwk::CommonEventSubscriber {
69 public:
70 explicit PackageChangedReceiver(std::weak_ptr<NfcService> nfcService,
71 const EventFwk::CommonEventSubscribeInfo& subscribeInfo);
~PackageChangedReceiver()72 ~PackageChangedReceiver()
73 {
74 }
75 void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
76
77 private:
78 std::weak_ptr<NfcService> nfcService_ {};
79 std::weak_ptr<AppExecFwk::EventHandler> eventHandler_ {};
80 };
81
PackageChangedReceiver(std::weak_ptr<NfcService> nfcService,const EventFwk::CommonEventSubscribeInfo & subscribeInfo)82 CommonEventHandler::PackageChangedReceiver::PackageChangedReceiver(std::weak_ptr<NfcService> nfcService,
83 const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
84 : EventFwk::CommonEventSubscriber(subscribeInfo),
85 nfcService_(nfcService),
86 eventHandler_(nfcService.lock()->eventHandler_)
87 {
88 }
89
OnReceiveEvent(const EventFwk::CommonEventData & data)90 void CommonEventHandler::PackageChangedReceiver::OnReceiveEvent(const EventFwk::CommonEventData& data)
91 {
92 DebugLog("CommonEventHandler::PackageChangedReceiver");
93 std::string action = data.GetWant().GetAction();
94 if (action.empty()) {
95 ErrorLog("action is empty");
96 return;
97 }
98 const std::shared_ptr<EventFwk::CommonEventData> mdata =
99 std::make_shared<EventFwk::CommonEventData> (data);
100 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) == 0 ||
101 action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) == 0 ||
102 action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) == 0) {
103 eventHandler_.lock()->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_PACKAGE_UPDATED),
104 mdata, static_cast<int64_t>(0));
105 }
106 }
107
CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::weak_ptr<NfcService> servcie)108 CommonEventHandler::CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner,
109 std::weak_ptr<NfcService> servcie)
110 : EventHandler(runner), nfcService_(servcie)
111 {
112 }
113
~CommonEventHandler()114 CommonEventHandler::~CommonEventHandler()
115 {
116 EventFwk::CommonEventManager::UnSubscribeCommonEvent(screenSubscriber_);
117 EventFwk::CommonEventManager::UnSubscribeCommonEvent(pkgSubscriber_);
118 }
119
Intialize(std::weak_ptr<TAG::TagDispatcher> tagDispatcher,std::weak_ptr<CeService> ceService)120 void CommonEventHandler::Intialize(std::weak_ptr<TAG::TagDispatcher> tagDispatcher, std::weak_ptr<CeService> ceService)
121 {
122 DebugLog("CommonEventHandler::Intialize");
123 tagDispatcher_ = tagDispatcher;
124 ceService_ = ceService;
125
126 SubscribeScreenChangedEvent();
127 SubscribePackageChangedEvent();
128 }
129
SubscribeScreenChangedEvent()130 void CommonEventHandler::SubscribeScreenChangedEvent()
131 {
132 EventFwk::MatchingSkills matchingSkills;
133 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
134 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
135 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
136 screenSubscriber_ = std::make_shared<ScreenChangedReceiver>(nfcService_, subscribeInfo);
137 if (screenSubscriber_ == nullptr) {
138 ErrorLog("Create screen changed subscriber failed");
139 return;
140 }
141
142 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(screenSubscriber_)) {
143 ErrorLog("Subscribe screen changed event fail");
144 }
145 }
146
SubscribePackageChangedEvent()147 void CommonEventHandler::SubscribePackageChangedEvent()
148 {
149 EventFwk::MatchingSkills matchingSkills;
150 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
151 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
152 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
153 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
154 pkgSubscriber_ = std::make_shared<PackageChangedReceiver>(nfcService_, subscribeInfo);
155 if (pkgSubscriber_ == nullptr) {
156 ErrorLog("Create package changed subscriber failed");
157 return;
158 }
159
160 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(pkgSubscriber_)) {
161 ErrorLog("Subscribe package changed event fail");
162 }
163 }
164
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)165 void CommonEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
166 {
167 if (event == nullptr) {
168 ErrorLog("event is nullptr");
169 return;
170 }
171 NfcCommonEvent eventId = static_cast<NfcCommonEvent>(event->GetInnerEventId());
172 DebugLog("NFC common event handler receive a message of %{public}d", eventId);
173 switch (eventId) {
174 case NfcCommonEvent::MSG_TAG_FOUND:
175 tagDispatcher_.lock()->HandleTagFound(event->GetSharedObject<NCI::ITagHost>());
176 break;
177 case NfcCommonEvent::MSG_TAG_DEBOUNCE:
178 tagDispatcher_.lock()->HandleTagDebounce();
179 break;
180 case NfcCommonEvent::MSG_SCREEN_CHANGED: {
181 nfcService_.lock()->HandleScreenChanged(event->GetParam());
182 break;
183 }
184 case NfcCommonEvent::MSG_PACKAGE_UPDATED: {
185 nfcService_.lock()->HandlePackageUpdated(event->GetSharedObject<EventFwk::CommonEventData>());
186 break;
187 }
188 case NfcCommonEvent::MSG_COMMIT_ROUTING: {
189 nfcService_.lock()->HandleCommitRouting();
190 break;
191 }
192 case NfcCommonEvent::MSG_COMPUTE_ROUTING_PARAMS: {
193 nfcService_.lock()->HandleComputeRoutingParams();
194 break;
195 }
196 case NfcCommonEvent::MSG_FIELD_ACTIVATED: {
197 ceService_.lock()->HandleFieldActivated();
198 break;
199 }
200 case NfcCommonEvent::MSG_FIELD_DEACTIVATED: {
201 ceService_.lock()->HandleFieldDeactivated();
202 break;
203 }
204 case NfcCommonEvent::MSG_NOTIFY_FIELD_ON: {
205 ceService_.lock()->PublishFieldOnOrOffCommonEvent(true);
206 break;
207 }
208 case NfcCommonEvent::MSG_NOTIFY_FIELD_OFF: {
209 ceService_.lock()->PublishFieldOnOrOffCommonEvent(false);
210 break;
211 }
212 case NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT: {
213 ceService_.lock()->PublishFieldOnOrOffCommonEvent(false);
214 break;
215 }
216 default:
217 ErrorLog("Unknown message received: id %{public}d", eventId);
218 break;
219 }
220 }
221 } // namespace NFC
222 } // namespace OHOS
223