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("ImCommonEventManager::OnAddSystemAbility subscribeResult = %{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,StartInputHandler inputHandler)113 bool ImCommonEventManager::SubscribeWindowManagerService(FocusHandle handle, StartInputHandler 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
UnsubscribeEvent()141 bool ImCommonEventManager::UnsubscribeEvent()
142 {
143 return true;
144 }
145
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)146 ImCommonEventManager::EventSubscriber::EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
147 : EventFwk::CommonEventSubscriber(subscribeInfo)
148 {
149 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_SWITCHED] = &EventSubscriber::StartUser;
150 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_REMOVED] = &EventSubscriber::RemoveUser;
151 EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] = &EventSubscriber::RemovePackage;
152 }
153
OnReceiveEvent(const EventFwk::CommonEventData & data)154 void ImCommonEventManager::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
155 {
156 auto const &want = data.GetWant();
157 std::string action = want.GetAction();
158 IMSA_HILOGI("ImCommonEventManager::action = %{public}s!", action.c_str());
159 auto iter = EventManagerFunc_.find(action);
160 if (iter == EventManagerFunc_.end()) {
161 return;
162 }
163 auto EventListenerFunc = iter->second;
164 if (EventListenerFunc != nullptr) {
165 (this->*EventListenerFunc)(data);
166 }
167 }
168
StartUser(const CommonEventData & data)169 void ImCommonEventManager::EventSubscriber::StartUser(const CommonEventData &data)
170 {
171 auto newUserId = data.GetCode();
172 IMSA_HILOGI("ImCommonEventManager::StartUser, userId = %{public}d", newUserId);
173 MessageParcel *parcel = new MessageParcel();
174 parcel->WriteInt32(newUserId);
175 Message *msg = new Message(MessageID::MSG_ID_USER_START, parcel);
176 MessageHandler::Instance()->SendMessage(msg);
177 }
178
RemoveUser(const CommonEventData & data)179 void ImCommonEventManager::EventSubscriber::RemoveUser(const CommonEventData &data)
180 {
181 auto userId = data.GetCode();
182 IMSA_HILOGI("ImCommonEventManager::RemoveUser, userId = %{public}d", userId);
183 MessageParcel *parcel = new MessageParcel();
184 parcel->WriteInt32(userId);
185 Message *msg = new Message(MessageID::MSG_ID_USER_REMOVED, parcel);
186 MessageHandler::Instance()->SendMessage(msg);
187 }
188
RemovePackage(const CommonEventData & data)189 void ImCommonEventManager::EventSubscriber::RemovePackage(const CommonEventData &data)
190 {
191 auto const &want = data.GetWant();
192 auto element = want.GetElement();
193 std::string bundleName = element.GetBundleName();
194 int32_t userId = want.GetIntParam("userId", 0);
195 IMSA_HILOGD("ImCommonEventManager::RemovePackage, bundleName = %{public}s, userId = %{public}d",
196 bundleName.c_str(), userId);
197 MessageParcel *parcel = new (std::nothrow) MessageParcel();
198 if (parcel == nullptr) {
199 IMSA_HILOGE("parcel is nullptr");
200 return;
201 }
202 if (!ITypesUtil::Marshal(*parcel, userId, bundleName)) {
203 IMSA_HILOGE("Failed to write message parcel");
204 delete parcel;
205 return;
206 }
207 Message *msg = new Message(MessageID::MSG_ID_PACKAGE_REMOVED, parcel);
208 MessageHandler::Instance()->SendMessage(msg);
209 }
210
SystemAbilityStatusChangeListener(std::function<void ()> func)211 ImCommonEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(std::function<void()> func)
212 : func_(std::move(func))
213 {
214 }
215
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)216 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
217 int32_t systemAbilityId, const std::string &deviceId)
218 {
219 if (systemAbilityId != COMMON_EVENT_SERVICE_ID && systemAbilityId != MULTIMODAL_INPUT_SERVICE_ID
220 && systemAbilityId != WINDOW_MANAGER_SERVICE_ID) {
221 IMSA_HILOGD("invalid systemAbilityId %{public}d", systemAbilityId);
222 return;
223 }
224 if (func_ != nullptr) {
225 func_();
226 }
227 }
228
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)229 void ImCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
230 int32_t systemAbilityId, const std::string &deviceId)
231 {
232 }
233 } // namespace MiscServices
234 } // namespace OHOS
235