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