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