1 /*
2 * Copyright (C) 2021 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 "im_common_event_manager.h"
17
18 #include <utility>
19
20 #include "full_ime_info_manager.h"
21 #include "global.h"
22 #include "ime_info_inquirer.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "itypes_util.h"
26 #include "message_handler.h"
27 #include "os_account_adapter.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace MiscServices {
32 using namespace MessageID;
33 sptr<ImCommonEventManager> ImCommonEventManager::instance_;
34 std::mutex ImCommonEventManager::instanceLock_;
35 using namespace OHOS::EventFwk;
36 constexpr const char *COMMON_EVENT_INPUT_PANEL_STATUS_CHANGED = "usual.event.imf.input_panel_status_changed";
37 constexpr const char *COMMON_EVENT_PARAM_USER_ID = "userId";
38 constexpr const char *COMMON_EVENT_PARAM_PANEL_STATE = "panelState";
39 constexpr const char *COMMON_EVENT_PARAM_PANEL_RECT = "panelRect";
ImCommonEventManager()40 ImCommonEventManager::ImCommonEventManager()
41 {
42 }
43
~ImCommonEventManager()44 ImCommonEventManager::~ImCommonEventManager()
45 {
46 }
47
GetInstance()48 sptr<ImCommonEventManager> ImCommonEventManager::GetInstance()
49 {
50 if (instance_ == nullptr) {
51 std::lock_guard<std::mutex> autoLock(instanceLock_);
52 if (instance_ == nullptr) {
53 IMSA_HILOGI("instance_ is nullptr.");
54 instance_ = new ImCommonEventManager();
55 }
56 }
57 return instance_;
58 }
59
SubscribeEvent()60 bool ImCommonEventManager::SubscribeEvent()
61 {
62 EventFwk::MatchingSkills matchingSkills;
63 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
64 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
65 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
66 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
67 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
68 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED);
69 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY);
70 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_STOPPED);
71 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
72 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
73
74 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
75
76 std::shared_ptr<EventSubscriber> subscriber = std::make_shared<EventSubscriber>(subscriberInfo);
77 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78 if (abilityManager == nullptr) {
79 IMSA_HILOGE("SubscribeEvent abilityManager is nullptr!");
80 return false;
81 }
82 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([subscriber]() {
83 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
84 IMSA_HILOGI("SubscribeCommonEvent ret: %{public}d", subscribeResult);
85 });
86 if (listener == nullptr) {
87 IMSA_HILOGE("SubscribeEvent listener is nullptr!");
88 return false;
89 }
90 int32_t ret = abilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, listener);
91 if (ret != ERR_OK) {
92 IMSA_HILOGE("SubscribeEvent SubscribeSystemAbility failed. ret: %{public}d", ret);
93 return false;
94 }
95 return true;
96 }
97
SubscribeKeyboardEvent(KeyHandle handle)98 bool ImCommonEventManager::SubscribeKeyboardEvent(KeyHandle handle)
99 {
100 IMSA_HILOGI("ImCommonEventManager::SubscribeKeyboardEvent start.");
101 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 if (abilityManager == nullptr) {
103 IMSA_HILOGE("SubscribeKeyboardEvent abilityManager is nullptr!");
104 return false;
105 }
106 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handle]() {
107 int32_t ret = KeyboardEvent::GetInstance().AddKeyEventMonitor(handle);
108 IMSA_HILOGI("SubscribeKeyboardEvent add monitor: %{public}s.",
109 ret == ErrorCode::NO_ERROR ? "success" : "failed");
110 });
111 if (listener == nullptr) {
112 IMSA_HILOGE("listener is nullptr!");
113 return false;
114 }
115 int32_t ret = abilityManager->SubscribeSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, listener);
116 if (ret != ERR_OK) {
117 IMSA_HILOGE("failed to SubscribeSystemAbility, ret: %{public}d!", ret);
118 return false;
119 }
120 return true;
121 }
122
SubscribeWindowManagerService(const Handler & handler)123 bool ImCommonEventManager::SubscribeWindowManagerService(const Handler &handler)
124 {
125 IMSA_HILOGI("start.");
126 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127 if (abilityManager == nullptr) {
128 IMSA_HILOGE("abilityManager is nullptr!");
129 return false;
130 }
131 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handler]() {
132 if (handler != nullptr) {
133 handler();
134 }
135 });
136 if (listener == nullptr) {
137 IMSA_HILOGE("failed to create listener!");
138 return false;
139 }
140 int32_t ret = abilityManager->SubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, listener);
141 if (ret != ERR_OK) {
142 IMSA_HILOGE("subscribe system ability failed, ret: %{public}d", ret);
143 return false;
144 }
145 return true;
146 }
147
SubscribeMemMgrService(const Handler & handler)148 bool ImCommonEventManager::SubscribeMemMgrService(const Handler &handler)
149 {
150 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151 if (abilityManager == nullptr) {
152 IMSA_HILOGE("abilityManager is nullptr!");
153 return false;
154 }
155 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handler]() {
156 if (handler != nullptr) {
157 handler();
158 }
159 });
160 if (listener == nullptr) {
161 IMSA_HILOGE("failed to create listener!");
162 return false;
163 }
164 int32_t ret = abilityManager->SubscribeSystemAbility(MEMORY_MANAGER_SA_ID, listener);
165 if (ret != ERR_OK) {
166 IMSA_HILOGE("subscribe system ability failed, ret: %{public}d", ret);
167 return false;
168 }
169 return true;
170 }
171
SubscribeAccountManagerService(Handler handler)172 bool ImCommonEventManager::SubscribeAccountManagerService(Handler handler)
173 {
174 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
175 if (abilityManager == nullptr) {
176 IMSA_HILOGE("abilityManager is nullptr!");
177 return false;
178 }
179 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handler]() {
180 if (handler != nullptr) {
181 handler();
182 }
183 });
184 if (listener == nullptr) {
185 IMSA_HILOGE("failed to create listener!");
186 return false;
187 }
188 int32_t ret = abilityManager->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, listener);
189 if (ret != ERR_OK) {
190 IMSA_HILOGE("subscribe system ability failed, ret: %{public}d", ret);
191 return false;
192 }
193 return true;
194 }
195
UnsubscribeEvent()196 bool ImCommonEventManager::UnsubscribeEvent()
197 {
198 return true;
199 }
200
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)201 ImCommonEventManager::EventSubscriber::EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
202 : EventFwk::CommonEventSubscriber(subscribeInfo)
203 {
204 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_SWITCHED] =
205 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
206 return that->StartUser(data);
207 };
208 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_STOPPED] =
209 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
210 return that->StopUser(data);
211 };
212 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_REMOVED] =
213 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
214 return that->RemoveUser(data);
215 };
216 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] =
217 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
218 return that->RemovePackage(data);
219 };
220 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED] =
221 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
222 return that->OnBundleScanFinished(data);
223 };
224 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY] =
225 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
226 return that->OnDataShareReady(data);
227 };
228 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] =
229 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
230 return that->AddPackage(data);
231 };
232 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] =
233 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
234 return that->ChangePackage(data);
235 };
236 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED] =
237 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
238 return that->HandleBootCompleted(data);
239 };
240 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_UNLOCKED] =
241 [] (EventSubscriber *that, const EventFwk::CommonEventData &data) {
242 return that->OnUserUnlocked(data);
243 };
244 }
245
OnReceiveEvent(const EventFwk::CommonEventData & data)246 void ImCommonEventManager::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
247 {
248 auto const &want = data.GetWant();
249 std::string action = want.GetAction();
250 IMSA_HILOGI("ImCommonEventManager::action: %{public}s!", action.c_str());
251 auto iter = EventManagerFunc_.find(action);
252 if (iter != EventManagerFunc_.end()) {
253 EventManagerFunc_[action] (this, data);
254 }
255 }
256
StopUser(const CommonEventData & data)257 void ImCommonEventManager::EventSubscriber::StopUser(const CommonEventData &data)
258 {
259 HandleUserEvent(MessageID::MSG_ID_USER_STOP, data);
260 }
261
StartUser(const CommonEventData & data)262 void ImCommonEventManager::EventSubscriber::StartUser(const CommonEventData &data)
263 {
264 HandleUserEvent(MessageID::MSG_ID_USER_START, data);
265 }
266
RemoveUser(const CommonEventData & data)267 void ImCommonEventManager::EventSubscriber::RemoveUser(const CommonEventData &data)
268 {
269 HandleUserEvent(MessageID::MSG_ID_USER_REMOVED, data);
270 }
271
HandleUserEvent(int32_t messageId,const EventFwk::CommonEventData & data)272 void ImCommonEventManager::EventSubscriber::HandleUserEvent(int32_t messageId, const EventFwk::CommonEventData &data)
273 {
274 auto userId = data.GetCode();
275 MessageParcel *parcel = new (std::nothrow) MessageParcel();
276 if (parcel == nullptr) {
277 return;
278 }
279 IMSA_HILOGD("userId:%{public}d, messageId:%{public}d", userId, messageId);
280 parcel->WriteInt32(userId);
281 Message *msg = new (std::nothrow) Message(messageId, parcel);
282 if (msg == nullptr) {
283 delete parcel;
284 return;
285 }
286 MessageHandler::Instance()->SendMessage(msg);
287 }
288
OnBundleScanFinished(const EventFwk::CommonEventData & data)289 void ImCommonEventManager::EventSubscriber::OnBundleScanFinished(const EventFwk::CommonEventData &data)
290 {
291 IMSA_HILOGI("ImCommonEventManager start.");
292 auto parcel = new (std::nothrow) MessageParcel();
293 if (parcel == nullptr) {
294 IMSA_HILOGE("failed to create MessageParcel!");
295 return;
296 }
297 auto msg = new (std::nothrow) Message(MessageID::MSG_ID_BUNDLE_SCAN_FINISHED, parcel);
298 if (msg == nullptr) {
299 IMSA_HILOGE("failed to create Message!");
300 delete parcel;
301 return;
302 }
303 MessageHandler::Instance()->SendMessage(msg);
304 }
305
OnDataShareReady(const EventFwk::CommonEventData & data)306 void ImCommonEventManager::EventSubscriber::OnDataShareReady(const EventFwk::CommonEventData &data)
307 {
308 IMSA_HILOGI("ImCommonEventManager start.");
309 auto parcel = new (std::nothrow) MessageParcel();
310 if (parcel == nullptr) {
311 IMSA_HILOGE("failed to create MessageParcel!");
312 return;
313 }
314 auto msg = new (std::nothrow) Message(MessageID::MSG_ID_DATA_SHARE_READY, parcel);
315 if (msg == nullptr) {
316 IMSA_HILOGE("failed to create Message!");
317 delete parcel;
318 return;
319 }
320 MessageHandler::Instance()->SendMessage(msg);
321 }
322
RemovePackage(const CommonEventData & data)323 void ImCommonEventManager::EventSubscriber::RemovePackage(const CommonEventData &data)
324 {
325 HandlePackageEvent(MessageID::MSG_ID_PACKAGE_REMOVED, data);
326 }
327
AddPackage(const EventFwk::CommonEventData & data)328 void ImCommonEventManager::EventSubscriber::AddPackage(const EventFwk::CommonEventData &data)
329 {
330 HandlePackageEvent(MessageID::MSG_ID_PACKAGE_ADDED, data);
331 }
332
ChangePackage(const EventFwk::CommonEventData & data)333 void ImCommonEventManager::EventSubscriber::ChangePackage(const EventFwk::CommonEventData &data)
334 {
335 HandlePackageEvent(MessageID::MSG_ID_PACKAGE_CHANGED, data);
336 }
337
HandlePackageEvent(int32_t messageId,const EventFwk::CommonEventData & data)338 void ImCommonEventManager::EventSubscriber::HandlePackageEvent(int32_t messageId, const EventFwk::CommonEventData &data)
339 {
340 auto const &want = data.GetWant();
341 auto element = want.GetElement();
342 std::string bundleName = element.GetBundleName();
343 int32_t userId = want.GetIntParam("userId", OsAccountAdapter::INVALID_USER_ID);
344 if (userId == OsAccountAdapter::INVALID_USER_ID) {
345 IMSA_HILOGE("invalid user id, messageId:%{public}d", messageId);
346 return;
347 }
348 IMSA_HILOGD(
349 "messageId:%{public}d, bundleName:%{public}s, userId:%{public}d", messageId, bundleName.c_str(), userId);
350 if (messageId == MessageID::MSG_ID_PACKAGE_REMOVED) {
351 if (!FullImeInfoManager::GetInstance().Has(userId, bundleName)) {
352 return;
353 }
354 } else {
355 if (!ImeInfoInquirer::GetInstance().IsInputMethod(userId, bundleName)) {
356 return;
357 }
358 }
359 MessageParcel *parcel = new (std::nothrow) MessageParcel();
360 if (parcel == nullptr) {
361 IMSA_HILOGE("parcel is nullptr!");
362 return;
363 }
364 if (!ITypesUtil::Marshal(*parcel, userId, bundleName)) {
365 IMSA_HILOGE("failed to write message parcel");
366 delete parcel;
367 return;
368 }
369 Message *msg = new (std::nothrow) Message(messageId, parcel);
370 if (msg == nullptr) {
371 IMSA_HILOGE("failed to create Message");
372 delete parcel;
373 return;
374 }
375 MessageHandler::Instance()->SendMessage(msg);
376 }
377
HandleBootCompleted(const EventFwk::CommonEventData & data)378 void ImCommonEventManager::EventSubscriber::HandleBootCompleted(const EventFwk::CommonEventData &data)
379 {
380 Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_BOOT_COMPLETED, nullptr);
381 if (msg == nullptr) {
382 return;
383 }
384 MessageHandler::Instance()->SendMessage(msg);
385 }
386
OnUserUnlocked(const EventFwk::CommonEventData & data)387 void ImCommonEventManager::EventSubscriber::OnUserUnlocked(const EventFwk::CommonEventData &data)
388 {
389 MessageParcel *parcel = new (std::nothrow) MessageParcel();
390 if (parcel == nullptr) {
391 IMSA_HILOGE("parcel is nullptr!");
392 return;
393 }
394 int32_t userId = data.GetCode();
395 if (!ITypesUtil::Marshal(*parcel, userId)) {
396 IMSA_HILOGE("Failed to write message parcel!");
397 delete parcel;
398 return;
399 }
400 Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_USER_UNLOCKED, parcel);
401 if (msg == nullptr) {
402 IMSA_HILOGE("failed to create Message!");
403 delete parcel;
404 return;
405 }
406 MessageHandler::Instance()->SendMessage(msg);
407 }
408
SystemAbilityStatusChangeListener(std::function<void ()> func)409 ImCommonEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(std::function<void()> func)
410 : func_(std::move(func))
411 {
412 }
413
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)414 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
415 int32_t systemAbilityId, const std::string &deviceId)
416 {
417 IMSA_HILOGD("systemAbilityId: %{public}d.", systemAbilityId);
418 if (systemAbilityId != COMMON_EVENT_SERVICE_ID && systemAbilityId != MULTIMODAL_INPUT_SERVICE_ID &&
419 systemAbilityId != WINDOW_MANAGER_SERVICE_ID && systemAbilityId != SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN &&
420 systemAbilityId != MEMORY_MANAGER_SA_ID) {
421 return;
422 }
423 if (func_ != nullptr) {
424 func_();
425 }
426 }
427
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)428 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
429 int32_t systemAbilityId, const std::string &deviceId)
430 {
431 }
432
PublishPanelStatusChangeEvent(int32_t userId,const InputWindowStatus & status,const ImeWindowInfo & info)433 int32_t ImCommonEventManager::PublishPanelStatusChangeEvent(
434 int32_t userId, const InputWindowStatus &status, const ImeWindowInfo &info)
435 {
436 EventFwk::CommonEventPublishInfo publicInfo;
437 publicInfo.SetOrdered(false);
438 AAFwk::Want want;
439 want.SetAction(COMMON_EVENT_INPUT_PANEL_STATUS_CHANGED);
440 bool visible = (status == InputWindowStatus::SHOW);
441 std::vector<int32_t> panelRect = { info.windowInfo.left, info.windowInfo.top,
442 static_cast<int32_t>(info.windowInfo.width), static_cast<int32_t>(info.windowInfo.height) };
443 want.SetParam(COMMON_EVENT_PARAM_USER_ID, userId);
444 want.SetParam(COMMON_EVENT_PARAM_PANEL_STATE, visible);
445 want.SetParam(COMMON_EVENT_PARAM_PANEL_RECT, panelRect);
446 EventFwk::CommonEventData data;
447 data.SetWant(want);
448 return EventFwk::CommonEventManager::NewPublishCommonEvent(data, publicInfo);
449 }
450 } // namespace MiscServices
451 } // namespace OHOS