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