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