1 /*
2 * Copyright (C) 2022 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 "accessibility_common_event.h"
17 #include <unistd.h>
18 #include "accessible_ability_manager_service.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "hilog_wrapper.h"
22 #include "accessibility_notification_helper.h"
23
24 namespace OHOS {
25 namespace Accessibility {
26 namespace {
27 constexpr int32_t RETRY_SUBSCRIBER = 3;
28 const std::string KEY_USER_ID = "userId";
29 } // namespace
30
AccessibilityCommonEvent()31 AccessibilityCommonEvent::AccessibilityCommonEvent()
32 {
33 HILOG_DEBUG();
34 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED] =
35 &AccessibilityCommonEvent::HandleUserAdded;
36 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED] =
37 &AccessibilityCommonEvent::HandleUserRemoved;
38 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED] =
39 &AccessibilityCommonEvent::HandleUserSwitched;
40 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] =
41 &AccessibilityCommonEvent::HandlePackageAdd;
42 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] =
43 &AccessibilityCommonEvent::HandlePackageRemoved;
44 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] =
45 &AccessibilityCommonEvent::HandlePackageChanged;
46 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY] =
47 &AccessibilityCommonEvent::HandleDataShareReady;
48 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED] =
49 &AccessibilityCommonEvent::HandlePowerOnEvent;
50 handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED] =
51 &AccessibilityCommonEvent::HandleLocalChangedEvent;
52
53 for (auto it = handleEventFunc_.begin(); it != handleEventFunc_.end(); ++it) {
54 HILOG_DEBUG("Add event: %{public}s", it->first.c_str());
55 eventHandles_.emplace(it->first, [this, it](const OHOS::EventFwk::CommonEventData &eventId) {
56 auto requestFunc = it->second;
57 if (requestFunc != nullptr) {
58 (this->*requestFunc)(eventId);
59 }
60 });
61 }
62 }
63
~AccessibilityCommonEvent()64 AccessibilityCommonEvent::~AccessibilityCommonEvent()
65 {
66 eventHandles_.clear();
67 UnSubscriberEvent();
68 }
69
SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> & handler)70 void AccessibilityCommonEvent::SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
71 {
72 HILOG_DEBUG();
73
74 if (subscriber_) {
75 HILOG_DEBUG("Common Event is already subscribered!");
76 return;
77 }
78
79 EventFwk::MatchingSkills matchingSkills;
80 for (auto &event : handleEventFunc_) {
81 HILOG_DEBUG("Add event: %{public}s", event.first.c_str());
82 matchingSkills.AddEvent(event.first);
83 }
84
85 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
86 subscriber_ = std::make_shared<AccessibilityCommonEventSubscriber>(subscribeInfo, *this);
87 eventHandler_ = handler;
88
89 int32_t retry = RETRY_SUBSCRIBER;
90 do {
91 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
92 if (subscribeResult) {
93 HILOG_INFO("SubscriberEvent success.");
94 return;
95 } else {
96 HILOG_DEBUG("SubscriberEvent failed, retry %{public}d", retry);
97 retry--;
98 sleep(1);
99 }
100 } while (retry);
101
102 HILOG_ERROR("SubscriberEvent failed.");
103 }
104
UnSubscriberEvent()105 void AccessibilityCommonEvent::UnSubscriberEvent()
106 {
107 HILOG_INFO();
108 if (subscriber_) {
109 bool unSubscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
110 HILOG_INFO("unSubscribeResult = %{public}d", unSubscribeResult);
111 subscriber_ = nullptr;
112 eventHandler_ = nullptr;
113 }
114 }
115
OnReceiveEvent(const EventFwk::CommonEventData & data)116 void AccessibilityCommonEvent::OnReceiveEvent(const EventFwk::CommonEventData &data)
117 {
118 HILOG_DEBUG();
119 if (!eventHandler_) {
120 HILOG_ERROR("eventHandler_ is nullptr.");
121 return;
122 }
123 eventHandler_->PostTask([this, data]() {
124 HILOG_DEBUG();
125 std::string action = data.GetWant().GetAction();
126 HILOG_INFO("Handle event:[%{public}s] eventHandles size[%{public}zu]", action.c_str(), eventHandles_.size());
127 auto it = eventHandles_.find(action);
128 if (it == eventHandles_.end()) {
129 HILOG_ERROR("Ignore event: %{public}s", action.c_str());
130 return;
131 }
132 it->second(data);
133 }, "TASK_ON_RECEIVE_EVENT");
134 }
135
HandleUserAdded(const EventFwk::CommonEventData & data) const136 void AccessibilityCommonEvent::HandleUserAdded(const EventFwk::CommonEventData &data) const
137 {
138 int32_t accountId = data.GetCode();
139 HILOG_INFO();
140 if (accountId == -1) {
141 HILOG_ERROR("account id is wrong");
142 return;
143 }
144 Singleton<AccessibleAbilityManagerService>::GetInstance().AddedUser(accountId);
145 }
146
HandleUserRemoved(const EventFwk::CommonEventData & data) const147 void AccessibilityCommonEvent::HandleUserRemoved(const EventFwk::CommonEventData &data) const
148 {
149 int32_t accountId = data.GetCode();
150 HILOG_INFO();
151 if (accountId == -1) {
152 HILOG_ERROR("account id is wrong");
153 return;
154 }
155 Singleton<AccessibleAbilityManagerService>::GetInstance().RemovedUser(accountId);
156 }
157
HandleUserSwitched(const EventFwk::CommonEventData & data) const158 void AccessibilityCommonEvent::HandleUserSwitched(const EventFwk::CommonEventData &data) const
159 {
160 int32_t accountId = data.GetCode();
161 HILOG_INFO();
162 if (accountId == -1) {
163 HILOG_ERROR("account id is wrong");
164 return;
165 }
166 Singleton<AccessibleAbilityManagerService>::GetInstance().SwitchedUser(accountId);
167 }
168
HandlePackageRemoved(const EventFwk::CommonEventData & data) const169 void AccessibilityCommonEvent::HandlePackageRemoved(const EventFwk::CommonEventData &data) const
170 {
171 std::string bundleName = data.GetWant().GetBundle();
172 int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
173 int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
174 HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
175 if (userId != accountId) {
176 HILOG_ERROR("not same user.");
177 return;
178 }
179 Singleton<AccessibleAbilityManagerService>::GetInstance().PackageRemoved(bundleName);
180 }
181
HandlePackageAdd(const EventFwk::CommonEventData & data) const182 void AccessibilityCommonEvent::HandlePackageAdd(const EventFwk::CommonEventData &data) const
183 {
184 std::string bundleName = data.GetWant().GetBundle();
185 int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
186 int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
187 HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
188 if (userId != accountId) {
189 HILOG_ERROR("not same user.");
190 return;
191 }
192 Singleton<AccessibleAbilityManagerService>::GetInstance().PackageAdd(bundleName);
193 }
194
HandlePackageChanged(const EventFwk::CommonEventData & data) const195 void AccessibilityCommonEvent::HandlePackageChanged(const EventFwk::CommonEventData &data) const
196 {
197 std::string bundleName = data.GetWant().GetBundle();
198 int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
199 int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
200 HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
201 if (userId != accountId) {
202 HILOG_ERROR("not same user.");
203 return;
204 }
205 Singleton<AccessibleAbilityManagerService>::GetInstance().PackageChanged(bundleName);
206 }
207
HandleDataShareReady(const EventFwk::CommonEventData & data) const208 void AccessibilityCommonEvent::HandleDataShareReady(const EventFwk::CommonEventData &data) const
209 {
210 sptr<AccessibilityAccountData> accountData =
211 Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
212 if (accountData == nullptr) {
213 HILOG_ERROR("get accountData failed");
214 return;
215 }
216
217 auto config = accountData->GetConfig();
218 if (config == nullptr) {
219 HILOG_WARN("config is nullptr");
220 return;
221 }
222
223 if (config->GetInitializeState()) {
224 HILOG_INFO("datashare no need reInit.");
225 return;
226 }
227
228 HILOG_INFO("reInit datashare.");
229 config->Init();
230 }
231
HandlePowerOnEvent(const EventFwk::CommonEventData & data) const232 void AccessibilityCommonEvent::HandlePowerOnEvent(const EventFwk::CommonEventData &data) const
233 {
234 IgnoreRepeatClickNotification::PublishIgnoreRepeatClickReminder();
235 HILOG_ERROR("HandlePowerOnEvent");
236 }
237
HandleLocalChangedEvent(const EventFwk::CommonEventData & data) const238 void AccessibilityCommonEvent::HandleLocalChangedEvent(const EventFwk::CommonEventData &data) const
239 {
240 HILOG_DEBUG("reInit Resource.");
241 Singleton<AccessibleAbilityManagerService>::GetInstance().InitResource(true);
242 }
243 } // namespace Accessibility
244 } // namespace OHOS