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 "global.h"
21 #include "ime_info_inquirer.h"
22 #include "input_method_system_ability.h"
23 #include "input_method_system_ability_stub.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "itypes_util.h"
27 #include "message_handler.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;
ImCommonEventManager()36 ImCommonEventManager::ImCommonEventManager()
37 {
38 }
39
~ImCommonEventManager()40 ImCommonEventManager::~ImCommonEventManager()
41 {
42 }
43
GetInstance()44 sptr<ImCommonEventManager> ImCommonEventManager::GetInstance()
45 {
46 if (instance_ == nullptr) {
47 std::lock_guard<std::mutex> autoLock(instanceLock_);
48 if (instance_ == nullptr) {
49 IMSA_HILOGI("ImCommonEventManager::GetInstance instance_ is nullptr");
50 instance_ = new ImCommonEventManager();
51 }
52 }
53 return instance_;
54 }
55
SubscribeEvent(const std::string & event)56 bool ImCommonEventManager::SubscribeEvent(const std::string &event)
57 {
58 EventFwk::MatchingSkills matchingSkills;
59 matchingSkills.AddEvent(event);
60 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
61 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
62
63 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
64
65 std::shared_ptr<EventSubscriber> subscriber = std::make_shared<EventSubscriber>(subscriberInfo);
66 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67 if (abilityManager == nullptr) {
68 IMSA_HILOGE("SubscribeEvent abilityManager is nullptr");
69 return false;
70 }
71 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([subscriber]() {
72 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
73 IMSA_HILOGI("SubscribeCommonEvent ret = %{public}d", subscribeResult);
74 });
75 if (listener == nullptr) {
76 IMSA_HILOGE("SubscribeEvent listener is nullptr");
77 return false;
78 }
79 int32_t ret = abilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, listener);
80 if (ret != ERR_OK) {
81 IMSA_HILOGE("SubscribeEvent SubscribeSystemAbility failed. ret = %{public}d", ret);
82 return false;
83 }
84 statusChangeListener_ = listener;
85 return true;
86 }
87
SubscribeKeyboardEvent(KeyHandle handle)88 bool ImCommonEventManager::SubscribeKeyboardEvent(KeyHandle handle)
89 {
90 IMSA_HILOGI("ImCommonEventManager::SubscribeKeyboardEvent");
91 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
92 if (abilityManager == nullptr) {
93 IMSA_HILOGE("SubscribeKeyboardEvent abilityManager is nullptr");
94 return false;
95 }
96 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handle]() {
97 int32_t ret = KeyboardEvent::GetInstance().AddKeyEventMonitor(handle);
98 IMSA_HILOGI("SubscribeKeyboardEvent add monitor %{public}s", ret == ErrorCode::NO_ERROR ? "success" : "failed");
99 });
100 if (listener == nullptr) {
101 IMSA_HILOGE("SubscribeKeyboardEvent listener is nullptr");
102 return false;
103 }
104 int32_t ret = abilityManager->SubscribeSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, listener);
105 if (ret != ERR_OK) {
106 IMSA_HILOGE("SubscribeKeyboardEvent SubscribeSystemAbility failed. ret = %{public}d", ret);
107 return false;
108 }
109 keyboardEventListener_ = listener;
110 return true;
111 }
112
SubscribeWindowManagerService(FocusHandle handle,Handler inputHandler)113 bool ImCommonEventManager::SubscribeWindowManagerService(FocusHandle handle, Handler inputHandler)
114 {
115 IMSA_HILOGI("run in");
116 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 if (abilityManager == nullptr) {
118 IMSA_HILOGE("abilityManager is nullptr");
119 return false;
120 }
121 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow)
122 SystemAbilityStatusChangeListener([handle, inputHandler]() {
123 if (inputHandler != nullptr) {
124 inputHandler();
125 }
126 FocusMonitorManager::GetInstance().RegisterFocusChangedListener(handle);
127 });
128 if (listener == nullptr) {
129 IMSA_HILOGE("failed to create listener");
130 return false;
131 }
132 int32_t ret = abilityManager->SubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, listener);
133 if (ret != ERR_OK) {
134 IMSA_HILOGE("subscribe system ability failed, ret = %{public}d", ret);
135 return false;
136 }
137 focusChangeEventListener_ = listener;
138 return true;
139 }
140
SubscribeAccountManagerService(Handler handler)141 bool ImCommonEventManager::SubscribeAccountManagerService(Handler handler)
142 {
143 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
144 if (abilityManager == nullptr) {
145 IMSA_HILOGE("abilityManager is nullptr");
146 return false;
147 }
148 sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handler]() {
149 if (handler != nullptr) {
150 handler();
151 }
152 });
153 if (listener == nullptr) {
154 IMSA_HILOGE("failed to create listener");
155 return false;
156 }
157 int32_t ret = abilityManager->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, listener);
158 if (ret != ERR_OK) {
159 IMSA_HILOGE("subscribe system ability failed, ret = %{public}d", ret);
160 return false;
161 }
162 return true;
163 }
164
UnsubscribeEvent()165 bool ImCommonEventManager::UnsubscribeEvent()
166 {
167 return true;
168 }
169
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)170 ImCommonEventManager::EventSubscriber::EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
171 : EventFwk::CommonEventSubscriber(subscribeInfo)
172 {
173 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_SWITCHED] = &EventSubscriber::StartUser;
174 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_REMOVED] = &EventSubscriber::RemoveUser;
175 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] = &EventSubscriber::RemovePackage;
176 }
177
OnReceiveEvent(const EventFwk::CommonEventData & data)178 void ImCommonEventManager::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
179 {
180 auto const &want = data.GetWant();
181 std::string action = want.GetAction();
182 IMSA_HILOGI("ImCommonEventManager::action = %{public}s!", action.c_str());
183 auto iter = EventManagerFunc_.find(action);
184 if (iter == EventManagerFunc_.end()) {
185 return;
186 }
187 auto EventListenerFunc = iter->second;
188 if (EventListenerFunc != nullptr) {
189 (this->*EventListenerFunc)(data);
190 }
191 }
192
StartUser(const CommonEventData & data)193 void ImCommonEventManager::EventSubscriber::StartUser(const CommonEventData &data)
194 {
195 auto newUserId = data.GetCode();
196 IMSA_HILOGI("ImCommonEventManager::StartUser, userId = %{public}d", newUserId);
197 MessageParcel *parcel = new MessageParcel();
198 parcel->WriteInt32(newUserId);
199 Message *msg = new Message(MessageID::MSG_ID_USER_START, parcel);
200 MessageHandler::Instance()->SendMessage(msg);
201 }
202
RemoveUser(const CommonEventData & data)203 void ImCommonEventManager::EventSubscriber::RemoveUser(const CommonEventData &data)
204 {
205 auto userId = data.GetCode();
206 IMSA_HILOGI("ImCommonEventManager::RemoveUser, userId = %{public}d", userId);
207 MessageParcel *parcel = new MessageParcel();
208 parcel->WriteInt32(userId);
209 Message *msg = new Message(MessageID::MSG_ID_USER_REMOVED, parcel);
210 MessageHandler::Instance()->SendMessage(msg);
211 }
212
RemovePackage(const CommonEventData & data)213 void ImCommonEventManager::EventSubscriber::RemovePackage(const CommonEventData &data)
214 {
215 auto const &want = data.GetWant();
216 auto element = want.GetElement();
217 std::string bundleName = element.GetBundleName();
218 int32_t userId = want.GetIntParam("userId", 0);
219 IMSA_HILOGD("ImCommonEventManager::RemovePackage, bundleName = %{public}s, userId = %{public}d",
220 bundleName.c_str(), userId);
221 MessageParcel *parcel = new (std::nothrow) MessageParcel();
222 if (parcel == nullptr) {
223 IMSA_HILOGE("parcel is nullptr");
224 return;
225 }
226 if (!ITypesUtil::Marshal(*parcel, userId, bundleName)) {
227 IMSA_HILOGE("Failed to write message parcel");
228 delete parcel;
229 return;
230 }
231 Message *msg = new Message(MessageID::MSG_ID_PACKAGE_REMOVED, parcel);
232 MessageHandler::Instance()->SendMessage(msg);
233 }
234
SystemAbilityStatusChangeListener(std::function<void ()> func)235 ImCommonEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(std::function<void()> func)
236 : func_(std::move(func))
237 {
238 }
239
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)240 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
241 int32_t systemAbilityId, const std::string &deviceId)
242 {
243 IMSA_HILOGD("systemAbilityId: %{public}d", systemAbilityId);
244 if (systemAbilityId != COMMON_EVENT_SERVICE_ID && systemAbilityId != MULTIMODAL_INPUT_SERVICE_ID
245 && systemAbilityId != WINDOW_MANAGER_SERVICE_ID && systemAbilityId != SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
246 return;
247 }
248 if (func_ != nullptr) {
249 func_();
250 }
251 }
252
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)253 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
254 int32_t systemAbilityId, const std::string &deviceId)
255 {
256 }
257 } // namespace MiscServices
258 } // namespace OHOS
259