• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 <cinttypes>
17 #include "accessibility_system_ability_client_impl.h"
18 #include "hilog_wrapper.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "parameter.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace Accessibility {
26 namespace {
27     constexpr int32_t SA_CONNECT_TIMEOUT = 500; // ms
28 } // namespaces
29 
30 static std::mutex g_Mutex;
31 static std::shared_ptr<AccessibilitySystemAbilityClientImpl> g_Instance = nullptr;
GetInstance()32 std::shared_ptr<AccessibilitySystemAbilityClient> AccessibilitySystemAbilityClient::GetInstance()
33 {
34     HILOG_DEBUG();
35     std::lock_guard<std::mutex> lock(g_Mutex);
36     if (!g_Instance) {
37         g_Instance = std::make_shared<AccessibilitySystemAbilityClientImpl>();
38     } else {
39         HILOG_DEBUG("AccessibilitySystemAbilityClient had construct instance");
40     }
41 
42     return g_Instance;
43 }
44 
AccessibilitySystemAbilityClientImpl()45 AccessibilitySystemAbilityClientImpl::AccessibilitySystemAbilityClientImpl()
46 {
47     HILOG_DEBUG();
48 
49     stateArray_.fill(false);
50     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51     if (samgr == nullptr) {
52         return;
53     }
54     sptr<AccessibilitySaStatusChange> statusChange = new AccessibilitySaStatusChange();
55     int32_t ret = samgr->SubscribeSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID, statusChange);
56     if (ret != 0) {
57         HILOG_ERROR("subscribe accessibility failed, error = %{public}d", ret);
58     }
59     if (ConnectToService()) {
60         Init();
61     }
62 }
63 
~AccessibilitySystemAbilityClientImpl()64 AccessibilitySystemAbilityClientImpl::~AccessibilitySystemAbilityClientImpl()
65 {
66     HILOG_DEBUG();
67     if (stateObserver_ != nullptr) {
68         stateObserver_->OnClientDeleted();
69     }
70 }
71 
ConnectToService()72 bool AccessibilitySystemAbilityClientImpl::ConnectToService()
73 {
74     HILOG_DEBUG();
75 
76     if (serviceProxy_) {
77         HILOG_DEBUG("AAMS Service is connected");
78         return true;
79     }
80 
81     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82     if (!samgr) {
83         HILOG_ERROR("Failed to get ISystemAbilityManager");
84         return false;
85     }
86 
87     sptr<IRemoteObject> object = samgr->GetSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID);
88     if (object == nullptr) {
89         HILOG_ERROR("Get IAccessibleAbilityManagerService object from samgr failed");
90         return false;
91     }
92 
93     if (!deathRecipient_) {
94         deathRecipient_ = new(std::nothrow) DeathRecipient(*this);
95         if (!deathRecipient_) {
96             HILOG_ERROR("Failed to create deathRecipient.");
97             return false;
98         }
99     }
100 
101     if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
102         HILOG_ERROR("Failed to add death recipient");
103     }
104 
105     HILOG_DEBUG("Get remote object ok");
106     serviceProxy_ = iface_cast<IAccessibleAbilityManagerService>(object);
107     if (!serviceProxy_) {
108         HILOG_ERROR("IAccessibleAbilityManagerService iface_cast failed");
109         return false;
110     }
111     return true;
112 }
113 
LoadAccessibilityService()114 bool AccessibilitySystemAbilityClientImpl::LoadAccessibilityService()
115 {
116     std::unique_lock<std::mutex> lock(conVarMutex_);
117     sptr<AccessibilityLoadCallback> loadCallback = new AccessibilityLoadCallback();
118     if (loadCallback == nullptr) {
119         return false;
120     }
121     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122     if (samgr == nullptr) {
123         return false;
124     }
125     int32_t ret = samgr->LoadSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID, loadCallback);
126     if (ret != 0) {
127         return false;
128     }
129     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(SA_CONNECT_TIMEOUT),
130         [this]() { return serviceProxy_ != nullptr; });
131     if (!waitStatus) {
132         return false;
133     }
134     return true;
135 }
136 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)137 void AccessibilitySystemAbilityClientImpl::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
138 {
139     std::lock_guard<std::mutex> lock(conVarMutex_);
140     if (serviceProxy_ != nullptr) {
141         HILOG_INFO("serviceProxy_ isn't nullptr");
142         proxyConVar_.notify_one();
143         return;
144     }
145     if (remoteObject != nullptr) {
146         serviceProxy_ = iface_cast<IAccessibleAbilityManagerService>(remoteObject);
147         if (!deathRecipient_) {
148             deathRecipient_ = new(std::nothrow) DeathRecipient(*this);
149             if (!deathRecipient_) {
150                 HILOG_ERROR("create deathRecipient_ fail.");
151             }
152         }
153         if (deathRecipient_ && remoteObject->IsProxyObject() && remoteObject->AddDeathRecipient(deathRecipient_)) {
154             HILOG_INFO("successed to add death recipient");
155         }
156     } else {
157         HILOG_WARN("remoteObject is nullptr.");
158     }
159     proxyConVar_.notify_one();
160 }
161 
LoadSystemAbilityFail()162 void AccessibilitySystemAbilityClientImpl::LoadSystemAbilityFail()
163 {
164     std::lock_guard<std::mutex> lock(conVarMutex_);
165     HILOG_WARN("LoadSystemAbilityFail.");
166     proxyConVar_.notify_one();
167 }
168 
Init()169 void AccessibilitySystemAbilityClientImpl::Init()
170 {
171     HILOG_DEBUG();
172     stateArray_.fill(false);
173     if (!stateObserver_) {
174         stateObserver_ = new(std::nothrow) AccessibleAbilityManagerStateObserverImpl(*this);
175         if (!stateObserver_) {
176             HILOG_ERROR("Failed to create stateObserver.");
177             return;
178         }
179     }
180     if (serviceProxy_ == nullptr) {
181         return;
182     }
183     uint32_t stateType = serviceProxy_->RegisterStateObserver(stateObserver_);
184     if (stateType & STATE_ACCESSIBILITY_ENABLED) {
185         stateArray_[AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED] = true;
186     }
187     if (stateType & STATE_EXPLORATION_ENABLED) {
188         stateArray_[AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED] = true;
189     }
190     if (stateType & STATE_KEYEVENT_ENABLED) {
191         stateArray_[AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED] = true;
192     }
193     if (stateType & STATE_GESTURE_ENABLED) {
194         stateArray_[AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED] = true;
195     }
196 }
197 
ResetService(const wptr<IRemoteObject> & remote)198 void AccessibilitySystemAbilityClientImpl::ResetService(const wptr<IRemoteObject> &remote)
199 {
200     HILOG_DEBUG();
201     std::lock_guard<std::mutex> lock(mutex_);
202     if (serviceProxy_ != nullptr) {
203         sptr<IRemoteObject> object = serviceProxy_->AsObject();
204         if (object && (remote == object)) {
205             object->RemoveDeathRecipient(deathRecipient_);
206             serviceProxy_ = nullptr;
207             HILOG_INFO("ResetService OK");
208         }
209     }
210 }
211 
RegisterElementOperator(const int32_t windowId,const std::shared_ptr<AccessibilityElementOperator> & operation)212 RetError AccessibilitySystemAbilityClientImpl::RegisterElementOperator(
213     const int32_t windowId, const std::shared_ptr<AccessibilityElementOperator> &operation)
214 {
215     HILOG_INFO("Register windowId[%{public}d] start", windowId);
216     std::lock_guard<std::mutex> lock(mutex_);
217     if (!operation) {
218         HILOG_ERROR("Input operation is null");
219         return RET_ERR_INVALID_PARAM;
220     }
221     if (serviceProxy_ == nullptr) {
222         HILOG_ERROR("Failed to get aams service");
223         return RET_ERR_SAMGR;
224     }
225     if (serviceProxy_ == nullptr) {
226         HILOG_ERROR("Failed to get aams service");
227         return RET_ERR_SAMGR;
228     }
229 
230     auto iter = elementOperators_.find(windowId);
231     if (iter != elementOperators_.end()) {
232         HILOG_ERROR("windowID[%{public}d] is exited", windowId);
233         return RET_OK;
234     }
235 
236     sptr<AccessibilityElementOperatorImpl> aamsInteractionOperator =
237         new(std::nothrow) AccessibilityElementOperatorImpl(windowId, operation, *this);
238     if (aamsInteractionOperator == nullptr) {
239         HILOG_ERROR("Failed to create aamsInteractionOperator.");
240         return RET_ERR_NULLPTR;
241     }
242     elementOperators_[windowId] = aamsInteractionOperator;
243     return serviceProxy_->RegisterElementOperator(windowId, aamsInteractionOperator);
244 }
245 
RegisterElementOperator(Registration parameter,const std::shared_ptr<AccessibilityElementOperator> & operation)246 RetError AccessibilitySystemAbilityClientImpl::RegisterElementOperator(Registration parameter,
247     const std::shared_ptr<AccessibilityElementOperator> &operation)
248 {
249     HILOG_DEBUG("parentWindowId:%{public}d, parentTreeId:%{public}d, windowId:%{public}d,nodeId:%{public}" PRId64 "",
250         parameter.parentWindowId, parameter.parentTreeId, parameter.windowId, parameter.elementId);
251 
252     std::lock_guard<std::mutex> lock(mutex_);
253     if (parameter.windowId < 0 || parameter.elementId < 0 ||
254         parameter.parentTreeId < 0 || parameter.parentWindowId < 0) {
255         return RET_ERR_INVALID_PARAM;
256     }
257 
258     if (!operation) {
259         HILOG_ERROR("Input operation is null");
260         return RET_ERR_INVALID_PARAM;
261     }
262 
263     if (!serviceProxy_) {
264         HILOG_ERROR("Failed to get aams service");
265         return RET_ERR_SAMGR;
266     }
267 
268     sptr<AccessibilityElementOperatorImpl> aamsInteractionOperator =
269         new(std::nothrow) AccessibilityElementOperatorImpl(parameter.windowId, operation, *this);
270     if (aamsInteractionOperator == nullptr) {
271         HILOG_ERROR("Failed to create aamsInteractionOperator.");
272         return RET_ERR_NULLPTR;
273     }
274     elementOperators_[parameter.windowId] = aamsInteractionOperator;
275     return serviceProxy_->RegisterElementOperator(parameter, aamsInteractionOperator);
276 }
277 
ReregisterElementOperator()278 void AccessibilitySystemAbilityClientImpl::ReregisterElementOperator()
279 {
280     HILOG_DEBUG();
281 
282     if (!serviceProxy_) {
283         HILOG_ERROR("serviceProxy_ is null.");
284         return;
285     }
286     for (auto iter = elementOperators_.begin(); iter != elementOperators_.end(); iter++) {
287         serviceProxy_->RegisterElementOperator(iter->first, iter->second);
288     }
289 }
290 
DeregisterElementOperator(const int32_t windowId)291 RetError AccessibilitySystemAbilityClientImpl::DeregisterElementOperator(const int32_t windowId)
292 {
293     HILOG_INFO("Deregister windowId[%{public}d] start", windowId);
294     std::lock_guard<std::mutex> lock(mutex_);
295 
296     if (serviceProxy_ == nullptr) {
297         HILOG_ERROR("Failed to get aams service");
298         return RET_ERR_SAMGR;
299     }
300     if (serviceProxy_ == nullptr) {
301         HILOG_ERROR("Failed to get aams service");
302         return RET_ERR_SAMGR;
303     }
304     auto iter = elementOperators_.find(windowId);
305     if (iter != elementOperators_.end()) {
306         HILOG_DEBUG("windowID[%{public}d] is erase", windowId);
307         elementOperators_.erase(iter);
308     } else {
309         HILOG_WARN("Not find windowID[%{public}d]", windowId);
310         return RET_ERR_NO_REGISTER;
311     }
312     return serviceProxy_->DeregisterElementOperator(windowId);
313 }
314 
DeregisterElementOperator(const int32_t windowId,const int32_t treeId)315 RetError AccessibilitySystemAbilityClientImpl::DeregisterElementOperator(const int32_t windowId, const int32_t treeId)
316 {
317     HILOG_INFO("Deregister windowId[%{public}d] treeId[%{public}d] start", windowId, treeId);
318     std::lock_guard<std::mutex> lock(mutex_);
319 
320     if (serviceProxy_ == nullptr) {
321         HILOG_ERROR("Failed to get aams service");
322         return RET_ERR_SAMGR;
323     }
324 
325     return serviceProxy_->DeregisterElementOperator(windowId, treeId);
326 }
327 
IsEnabled(bool & isEnabled)328 RetError AccessibilitySystemAbilityClientImpl::IsEnabled(bool &isEnabled)
329 {
330     HILOG_DEBUG();
331     std::lock_guard<std::mutex> lock(mutex_);
332     isEnabled = stateArray_[AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED];
333     return RET_OK;
334 }
335 
IsTouchExplorationEnabled(bool & isEnabled)336 RetError AccessibilitySystemAbilityClientImpl::IsTouchExplorationEnabled(bool &isEnabled)
337 {
338     HILOG_DEBUG();
339     std::lock_guard<std::mutex> lock(mutex_);
340     isEnabled = stateArray_[AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED];
341     return RET_OK;
342 }
343 
GetAbilityList(const uint32_t accessibilityAbilityTypes,const AbilityStateType stateType,std::vector<AccessibilityAbilityInfo> & infos)344 RetError AccessibilitySystemAbilityClientImpl::GetAbilityList(const uint32_t accessibilityAbilityTypes,
345     const AbilityStateType stateType, std::vector<AccessibilityAbilityInfo> &infos)
346 {
347     HILOG_DEBUG();
348     std::lock_guard<std::mutex> lock(mutex_);
349     bool check = false;
350     if ((accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN) ||
351         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC) ||
352         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE) ||
353         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL) ||
354         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC)) {
355         check = true;
356     }
357     if (stateType == ABILITY_STATE_INVALID) {
358         check = false;
359     }
360     if (!check) {
361         HILOG_ERROR("Invalid params: accessibilityAbilityTypes[%{public}d] stateType[%{public}d]",
362             accessibilityAbilityTypes, stateType);
363         return RET_ERR_INVALID_PARAM;
364     }
365     if (serviceProxy_ == nullptr) {
366         HILOG_ERROR("Failed to get aams service");
367         return RET_ERR_SAMGR;
368     }
369     if (serviceProxy_ == nullptr) {
370         HILOG_ERROR("Failed to get aams service");
371         return RET_ERR_SAMGR;
372     }
373     return serviceProxy_->GetAbilityList(accessibilityAbilityTypes, stateType, infos);
374 }
375 
CheckEventType(EventType eventType)376 bool AccessibilitySystemAbilityClientImpl::CheckEventType(EventType eventType)
377 {
378     if ((eventType < EventType::TYPE_VIEW_CLICKED_EVENT) ||
379         ((eventType >= EventType::TYPE_MAX_NUM) && (eventType != EventType::TYPES_ALL_MASK))) {
380         HILOG_ERROR("event type is invalid");
381         return false;
382     } else {
383         return true;
384     }
385 }
386 
SendEvent(const EventType eventType,const int64_t componentId)387 RetError AccessibilitySystemAbilityClientImpl::SendEvent(const EventType eventType, const int64_t componentId)
388 {
389     HILOG_DEBUG("componentId[%{public}" PRId64 "], eventType[%{public}d]", componentId, eventType);
390     std::lock_guard<std::mutex> lock(mutex_);
391     if (!CheckEventType(eventType)) {
392         return RET_ERR_INVALID_PARAM;
393     }
394     AccessibilityEventInfo event;
395     event.SetEventType(eventType);
396     event.SetSource(componentId);
397     if (serviceProxy_ == nullptr) {
398         HILOG_ERROR("Failed to get aams service");
399         return RET_ERR_SAMGR;
400     }
401     if (serviceProxy_ == nullptr) {
402         HILOG_ERROR("Failed to get aams service");
403         return RET_ERR_SAMGR;
404     }
405     return serviceProxy_->SendEvent(event);
406 }
407 
SendEvent(const AccessibilityEventInfo & event)408 RetError AccessibilitySystemAbilityClientImpl::SendEvent(const AccessibilityEventInfo &event)
409 {
410     HILOG_DEBUG("EventType[%{public}d]", event.GetEventType());
411     std::lock_guard<std::mutex> lock(mutex_);
412     if (!CheckEventType(event.GetEventType())) {
413         return RET_ERR_INVALID_PARAM;
414     }
415     if (serviceProxy_ == nullptr) {
416         HILOG_ERROR("Failed to get aams service");
417         return RET_ERR_SAMGR;
418     }
419     if (serviceProxy_ == nullptr) {
420         HILOG_ERROR("Failed to get aams service");
421         return RET_ERR_SAMGR;
422     }
423     return serviceProxy_->SendEvent(event);
424 }
425 
SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> & observer,const uint32_t eventType)426 RetError AccessibilitySystemAbilityClientImpl::SubscribeStateObserver(
427     const std::shared_ptr<AccessibilityStateObserver> &observer, const uint32_t eventType)
428 {
429     HILOG_DEBUG();
430     std::lock_guard<std::mutex> lock(mutex_);
431     if (eventType >= AccessibilityStateEventType::EVENT_TYPE_MAX) {
432         HILOG_ERROR("Input eventType is out of scope");
433         return RET_ERR_INVALID_PARAM;
434     }
435     if (!observer) {
436         HILOG_ERROR("Input observer is null");
437         return RET_ERR_INVALID_PARAM;
438     }
439 
440     StateObserverVector &observerVector = stateObserversArray_[eventType];
441     for (auto iter = observerVector.begin(); iter != observerVector.end(); ++iter) {
442         if (*iter == observer) {
443             HILOG_INFO("Observer has subscribed!");
444             return RET_ERR_REGISTER_EXIST;
445         }
446     }
447     observerVector.push_back(observer);
448     return RET_OK;
449 }
450 
UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> & observer,const uint32_t eventType)451 RetError AccessibilitySystemAbilityClientImpl::UnsubscribeStateObserver(
452     const std::shared_ptr<AccessibilityStateObserver> &observer, const uint32_t eventType)
453 {
454     HILOG_DEBUG("eventType is [%{public}d]", eventType);
455     std::lock_guard<std::mutex> lock(mutex_);
456     if (eventType >= AccessibilityStateEventType::EVENT_TYPE_MAX) {
457         HILOG_ERROR("Input eventType is out of scope");
458         return RET_ERR_INVALID_PARAM;
459     }
460     if (!observer) {
461         HILOG_ERROR("Input observer is null");
462         return RET_ERR_INVALID_PARAM;
463     }
464 
465     StateObserverVector &observerVector = stateObserversArray_[eventType];
466     for (auto iter = observerVector.begin(); iter != observerVector.end(); ++iter) {
467         if (*iter == observer) {
468             observerVector.erase(iter);
469             return RET_OK;
470         }
471     }
472     HILOG_ERROR("The observer has not subscribed.");
473     return RET_ERR_NO_REGISTER;
474 }
475 
NotifyStateChanged(uint32_t eventType,bool value)476 void AccessibilitySystemAbilityClientImpl::NotifyStateChanged(uint32_t eventType, bool value)
477 {
478     HILOG_DEBUG("EventType is %{public}d, value is %{public}d", eventType, value);
479     if (eventType >= AccessibilityStateEventType::EVENT_TYPE_MAX) {
480         HILOG_ERROR("EventType is invalid");
481         return;
482     }
483 
484     if (stateArray_[eventType] == value) {
485         HILOG_DEBUG("State value is not changed");
486         return;
487     }
488 
489     stateArray_[eventType] = value;
490     StateObserverVector &observers = stateObserversArray_[eventType];
491     for (auto &observer : observers) {
492         if (observer) {
493             observer->OnStateChanged(value);
494         } else {
495             HILOG_ERROR("end stateObserversArray[%{public}d] is null", eventType);
496         }
497     }
498     HILOG_DEBUG("end");
499 }
500 
GetEnabledAbilities(std::vector<std::string> & enabledAbilities)501 RetError AccessibilitySystemAbilityClientImpl::GetEnabledAbilities(std::vector<std::string> &enabledAbilities)
502 {
503     HILOG_DEBUG();
504     std::lock_guard<std::mutex> lock(mutex_);
505     if (serviceProxy_ == nullptr) {
506         HILOG_ERROR("Failed to get aams service");
507         return RET_ERR_SAMGR;
508     }
509     if (serviceProxy_ == nullptr) {
510         HILOG_ERROR("Failed to get aams service");
511         return RET_ERR_SAMGR;
512     }
513     return serviceProxy_->GetEnabledAbilities(enabledAbilities);
514 }
515 
OnAccessibleAbilityManagerStateChanged(const uint32_t stateType)516 void AccessibilitySystemAbilityClientImpl::OnAccessibleAbilityManagerStateChanged(const uint32_t stateType)
517 {
518     HILOG_DEBUG("stateType[%{public}d}", stateType);
519     SetAccessibilityState(stateType);
520     std::lock_guard<std::mutex> lock(mutex_);
521     if (stateType & STATE_ACCESSIBILITY_ENABLED) {
522         NotifyStateChanged(AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED, true);
523     } else {
524         NotifyStateChanged(AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED, false);
525     }
526 
527     if (stateType & STATE_EXPLORATION_ENABLED) {
528         NotifyStateChanged(AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED, true);
529     } else {
530         NotifyStateChanged(AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED, false);
531     }
532 
533     if (stateType & STATE_KEYEVENT_ENABLED) {
534         NotifyStateChanged(AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED, true);
535     } else {
536         NotifyStateChanged(AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED, false);
537     }
538 
539     if (stateType & STATE_GESTURE_ENABLED) {
540         NotifyStateChanged(AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED, true);
541     } else {
542         NotifyStateChanged(AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED, false);
543     }
544 }
545 
SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> & infos,const int32_t requestId)546 void AccessibilitySystemAbilityClientImpl::SetSearchElementInfoByAccessibilityIdResult(
547     const std::list<AccessibilityElementInfo> &infos, const int32_t requestId)
548 {
549     std::lock_guard<std::mutex> lock(mutex_);
550     HILOG_DEBUG("search element requestId[%{public}d]", requestId);
551     if (serviceProxy_ == nullptr) {
552         HILOG_ERROR("serviceProxy_ is nullptr");
553         return;
554     }
555     std::vector<AccessibilityElementInfo> filterInfos = TranslateListToVector(infos);
556     sptr<IAccessibilityElementOperatorCallback> callback =
557         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
558     if (requestId < 0) {
559         HILOG_ERROR("requestId is invalid");
560         return;
561     }
562     if (callback != nullptr) {
563         if (callback->GetFilter()) {
564             AccessibilityElementOperatorImpl::SetFiltering(filterInfos);
565         }
566         serviceProxy_->RemoveRequestId(requestId);
567         callback->SetSearchElementInfoByAccessibilityIdResult(filterInfos, requestId);
568         AccessibilityElementOperatorImpl::EraseCallback(requestId);
569     } else {
570         HILOG_INFO("callback is nullptr");
571     }
572 }
573 
SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> & infos,const int32_t requestId)574 void AccessibilitySystemAbilityClientImpl::SetSearchElementInfoByTextResult(
575     const std::list<AccessibilityElementInfo> &infos, const int32_t requestId)
576 {
577     std::lock_guard<std::mutex> lock(mutex_);
578     HILOG_DEBUG("requestId[%{public}d]", requestId);
579     if (serviceProxy_ == nullptr) {
580         HILOG_ERROR("serviceProxy_ is nullptr");
581         return;
582     }
583     std::vector<AccessibilityElementInfo> filterInfos = TranslateListToVector(infos);
584     sptr<IAccessibilityElementOperatorCallback> callback =
585         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
586     if (requestId >= 0) {
587         if (callback != nullptr) {
588             serviceProxy_->RemoveRequestId(requestId);
589             callback->SetSearchElementInfoByTextResult(filterInfos, requestId);
590             AccessibilityElementOperatorImpl::EraseCallback(requestId);
591         } else {
592             HILOG_INFO("callback is nullptr");
593         }
594     }
595 }
596 
SetFindFocusedElementInfoResult(const AccessibilityElementInfo & info,const int32_t requestId)597 void AccessibilitySystemAbilityClientImpl::SetFindFocusedElementInfoResult(
598     const AccessibilityElementInfo &info, const int32_t requestId)
599 {
600     std::lock_guard<std::mutex> lock(mutex_);
601     HILOG_DEBUG("requestId[%{public}d]", requestId);
602     if (serviceProxy_ == nullptr) {
603         HILOG_ERROR("serviceProxy_ is nullptr");
604         return;
605     }
606     sptr<IAccessibilityElementOperatorCallback> callback =
607         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
608     if (requestId >= 0) {
609         if (callback != nullptr) {
610             serviceProxy_->RemoveRequestId(requestId);
611             callback->SetFindFocusedElementInfoResult(info, requestId);
612             AccessibilityElementOperatorImpl::EraseCallback(requestId);
613         } else {
614             HILOG_INFO("callback is nullptr");
615         }
616     }
617 }
618 
SetFocusMoveSearchResult(const AccessibilityElementInfo & info,const int32_t requestId)619 void AccessibilitySystemAbilityClientImpl::SetFocusMoveSearchResult(
620     const AccessibilityElementInfo &info, const int32_t requestId)
621 {
622     std::lock_guard<std::mutex> lock(mutex_);
623     HILOG_DEBUG("requestId[%{public}d]", requestId);
624     if (serviceProxy_ == nullptr) {
625         HILOG_ERROR("serviceProxy_ is nullptr");
626         return;
627     }
628     sptr<IAccessibilityElementOperatorCallback> callback =
629         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
630     if (requestId >= 0) {
631         if (callback != nullptr) {
632             serviceProxy_->RemoveRequestId(requestId);
633             callback->SetFocusMoveSearchResult(info, requestId);
634             AccessibilityElementOperatorImpl::EraseCallback(requestId);
635         } else {
636             HILOG_INFO("callback is nullptr");
637         }
638     }
639 }
640 
SetExecuteActionResult(const bool succeeded,const int32_t requestId)641 void AccessibilitySystemAbilityClientImpl::SetExecuteActionResult(
642     const bool succeeded, const int32_t requestId)
643 {
644     std::lock_guard<std::mutex> lock(mutex_);
645     HILOG_DEBUG("requestId[%{public}d]", requestId);
646     if (serviceProxy_ == nullptr) {
647         HILOG_ERROR("serviceProxy_ is nullptr");
648         return;
649     }
650     sptr<IAccessibilityElementOperatorCallback> callback =
651         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
652     if (requestId >= 0) {
653         if (callback != nullptr) {
654             serviceProxy_->RemoveRequestId(requestId);
655             callback->SetExecuteActionResult(succeeded, requestId);
656             AccessibilityElementOperatorImpl::EraseCallback(requestId);
657         } else {
658             HILOG_INFO("callback is nullptr");
659         }
660     }
661 }
662 
SetCursorPositionResult(const int32_t cursorPosition,const int32_t requestId)663 void AccessibilitySystemAbilityClientImpl::SetCursorPositionResult(
664     const int32_t cursorPosition, const int32_t requestId)
665 {
666     std::lock_guard<std::mutex> lock(mutex_);
667     HILOG_DEBUG("requestId[%{public}d]  cursorPosition[%{public}d]", requestId, cursorPosition);
668     if (serviceProxy_ == nullptr) {
669         HILOG_ERROR("serviceProxy_ is nullptr");
670         return;
671     }
672     sptr<IAccessibilityElementOperatorCallback> callback =
673         AccessibilityElementOperatorImpl::GetCallbackByRequestId(requestId);
674     if (requestId >= 0) {
675         if (callback != nullptr) {
676             serviceProxy_->RemoveRequestId(requestId);
677             callback->SetCursorPositionResult(cursorPosition, requestId);
678             AccessibilityElementOperatorImpl::EraseCallback(requestId);
679         } else {
680             HILOG_INFO("callback is nullptr");
681         }
682     }
683 }
684 
SetAccessibilityState(const uint32_t stateType)685 void AccessibilitySystemAbilityClientImpl::SetAccessibilityState(const uint32_t stateType)
686 {
687     HILOG_DEBUG();
688     state_ = stateType;
689 }
690 
GetAccessibilityState()691 uint32_t AccessibilitySystemAbilityClientImpl::GetAccessibilityState()
692 {
693     HILOG_DEBUG();
694     return state_;
695 }
696 
SetFindAccessibilityNodeInfosResult(const std::list<AccessibilityElementInfo> elementInfos,const int32_t requestId,const int32_t requestCode)697 void AccessibilitySystemAbilityClientImpl::SetFindAccessibilityNodeInfosResult(
698     const std::list<AccessibilityElementInfo> elementInfos, const int32_t requestId, const int32_t requestCode)
699 {
700     HILOG_DEBUG();
701     switch (static_cast<SET_AA_CALLBACK_RESULT>(requestCode)) {
702         case FIND_ACCESSIBILITY_NODE_BY_ACCESSIBILITY_ID:
703             SetSearchElementInfoByAccessibilityIdResult(elementInfos, requestId);
704             break;
705         case FIND_ACCESSIBILITY_NODE_BY_TEXT:
706             SetSearchElementInfoByTextResult(elementInfos, requestId);
707             break;
708         default:
709             break;
710     }
711 }
712 
SetFindAccessibilityNodeInfoResult(const AccessibilityElementInfo elementInfo,const int32_t requestId,const int32_t requestCode)713 void AccessibilitySystemAbilityClientImpl::SetFindAccessibilityNodeInfoResult(
714     const AccessibilityElementInfo elementInfo, const int32_t requestId, const int32_t requestCode)
715 {
716     HILOG_DEBUG();
717     switch (static_cast<SET_AA_CALLBACK_RESULT>(requestCode)) {
718         case FIND_ACCESSIBILITY_NODE_BY_ACCESSIBILITY_ID:
719             {
720                 std::list<AccessibilityElementInfo> elementInfos = {};
721                 elementInfos.push_back(elementInfo);
722                 SetSearchElementInfoByAccessibilityIdResult(elementInfos, requestId);
723             }
724             break;
725         case FIND_FOCUS:
726             SetFindFocusedElementInfoResult(elementInfo, requestId);
727             break;
728         case FIND_FOCUS_SEARCH:
729             SetFocusMoveSearchResult(elementInfo, requestId);
730             break;
731         default:
732             break;
733     }
734 }
735 
SetPerformActionResult(const bool succeeded,const int32_t requestId)736 void AccessibilitySystemAbilityClientImpl::SetPerformActionResult(const bool succeeded, const int32_t requestId)
737 {
738     HILOG_DEBUG();
739     SetExecuteActionResult(succeeded, requestId);
740 }
741 
GetFocusedWindowId(int32_t & focusedWindowId)742 RetError AccessibilitySystemAbilityClientImpl::GetFocusedWindowId(int32_t &focusedWindowId)
743 {
744     HILOG_DEBUG();
745     std::lock_guard<std::mutex> lock(mutex_);
746     if (serviceProxy_ == nullptr) {
747         HILOG_ERROR("Failed to get aams service");
748         return RET_ERR_SAMGR;
749     }
750     if (serviceProxy_ == nullptr) {
751         HILOG_ERROR("Failed to get aams service");
752         return RET_ERR_SAMGR;
753     }
754     return serviceProxy_->GetFocusedWindowId(focusedWindowId);
755 }
756 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)757 void AccessibilitySystemAbilityClientImpl::AccessibilityLoadCallback::OnLoadSystemAbilitySuccess(
758     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
759 {
760     HILOG_DEBUG();
761     if (g_Instance) {
762         g_Instance->LoadSystemAbilitySuccess(remoteObject);
763     }
764 }
765 
OnLoadSystemAbilityFail(int32_t systemAbilityId)766 void AccessibilitySystemAbilityClientImpl::AccessibilityLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
767 {
768     HILOG_DEBUG();
769     if (g_Instance) {
770         g_Instance->LoadSystemAbilityFail();
771     }
772 }
773 
OnAddSystemAbility(int32_t saId,const std::string & deviceId)774 void AccessibilitySystemAbilityClientImpl::AccessibilitySaStatusChange::OnAddSystemAbility(int32_t saId,
775     const std::string &deviceId)
776 {
777     HILOG_DEBUG();
778     if (!g_Instance) {
779         return;
780     }
781     std::lock_guard<std::mutex> lock(g_Instance->mutex_);
782     if (g_Instance->serviceProxy_) {
783         return;
784     }
785     if (!g_Instance->ConnectToService()) {
786         return;
787     }
788     g_Instance->Init();
789     g_Instance->ReregisterElementOperator();
790 }
791 
OnRemoveSystemAbility(int32_t saId,const std::string & deviceId)792 void AccessibilitySystemAbilityClientImpl::AccessibilitySaStatusChange::OnRemoveSystemAbility(int32_t saId,
793     const std::string &deviceId)
794 {
795     HILOG_WARN("disconnect to accessibility service");
796 }
797 } // namespace Accessibility
798 } // namespace OHOS
799