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