• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "accessibility_system_ability_client.h"
17 #include "ability_context.h"
18 #include "accessibility_element_operator.h"
19 #include "accessibility_element_operator_stub.h"
20 #include "accessibility_window_info.h"
21 #include "accessible_ability_manager_service_proxy.h"
22 #include "accessible_ability_manager_service_state_stub.h"
23 #include "accessible_ability_manager_service_caption_property_stub.h"
24 #include "hilog_wrapper.h"
25 #include "if_system_ability_manager.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 
29 using namespace OHOS::AppExecFwk;
30 using namespace std;
31 
32 namespace OHOS {
33 namespace Accessibility {
34 shared_ptr<AccessibilitySystemAbilityClient> AccessibilitySystemAbilityClient::instance_ = nullptr;
35 
36 struct AccessibilitySystemAbilityClient::Impl {
37     class DeathRecipient : public IRemoteObject::DeathRecipient {
38     public:
39         DeathRecipient() = default;
40         ~DeathRecipient() = default;
41         DISALLOW_COPY_AND_MOVE(DeathRecipient);
42 
OnRemoteDied(const wptr<IRemoteObject> & remote)43         void OnRemoteDied(const wptr<IRemoteObject>& remote)
44         {
45             AccessibilitySystemAbilityClient::GetInstance()->ResetService(remote);
46         }
47     };
48     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
49     sptr<AccessibleAbilityManagerServiceStateStub> stateCallback_ = nullptr;
50     sptr<AccessibleAbilityManagerServiceClientProxy> serviceProxy_ = nullptr;
51     sptr<AccessibleAbilityManagerServiceCaptionPropertyStub> captionPropertyCallback_ = nullptr;
GetServiceOHOS::Accessibility::AccessibilitySystemAbilityClient::Impl52     sptr<IAccessibleAbilityManagerServiceClient> GetService()
53     {
54         if (serviceProxy_ != nullptr) {
55             return serviceProxy_;
56         }
57 
58         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59         if (!samgr) {
60             HILOG_ERROR("Failed to get ISystemAbilityManager");
61             return nullptr;
62         }
63 
64         sptr<IRemoteObject> object = samgr->GetSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID);
65         if (!object) {
66             HILOG_ERROR("Get IAccessibleAbilityManagerServiceClient object from samgr failed");
67             return nullptr;
68         }
69 
70         if (!deathRecipient_) {
71             deathRecipient_ = new DeathRecipient();
72         }
73 
74         if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
75             HILOG_ERROR("Failed to add death recipient");
76         }
77 
78         HILOG_DEBUG("get remote object ok");
79         serviceProxy_ = iface_cast<AccessibleAbilityManagerServiceClientProxy>(object);
80         if (!serviceProxy_) {
81             HILOG_ERROR("IAccessibleAbilityManagerServiceClient iface_cast failed");
82         }
83         return serviceProxy_;
84     }
85 };
86 
AccessibilitySystemAbilityClient(const Context & context,int accountId)87 AccessibilitySystemAbilityClient::AccessibilitySystemAbilityClient(const Context& context, int accountId)
88     : pimpl(std::make_unique<Impl>())
89 {
90     interactionOperator_ = nullptr;
91     accountId_ = accountId;
92     pimpl->stateCallback_ = new AccessibleAbilityManagerServiceStateStub();
93     auto proxyService = pimpl->GetService();
94     if (!proxyService) {
95         HILOG_ERROR("Failed to get aams service");
96         return;
97     }
98     uint32_t stateType = proxyService->RegisterStateCallback(pimpl->stateCallback_, accountId_);
99     if (stateType & AccessibilitySystemAbilityClient::STATE_ACCESSIBILITY_ENABLED) {
100         isEnabled_ = true;
101     } else {
102         isEnabled_ = false;
103     }
104 
105     if (stateType & AccessibilitySystemAbilityClient::STATE_EXPLORATION_ENABLED) {
106         isTouchExplorationEnabled_ = true;
107     } else {
108         isTouchExplorationEnabled_ = false;
109     }
110 
111     if (stateType & AccessibilitySystemAbilityClient::STATE_CAPTION_ENABLED) {
112         isCaptionEnabled_ = true;
113     } else {
114         isCaptionEnabled_ = false;
115     }
116 
117     if (stateType & AccessibilitySystemAbilityClient::STATE_KEYEVENT_ENABLED) {
118         isFilteringKeyEventsEnabled_ = true;
119     } else {
120         isFilteringKeyEventsEnabled_ = false;
121     }
122 
123     if (stateType & AccessibilitySystemAbilityClient::STATE_GESTURE_ENABLED) {
124         isGesturesSimulationEnabled_ = true;
125     } else {
126         isGesturesSimulationEnabled_ = false;
127     }
128 
129     pimpl->captionPropertyCallback_ = new AccessibleAbilityManagerServiceCaptionPropertyStub();
130     proxyService->RegisterCaptionPropertyCallback(pimpl->captionPropertyCallback_, accountId_);
131 }
132 
ResetService(const wptr<IRemoteObject> & remote)133 void AccessibilitySystemAbilityClient::ResetService(const wptr<IRemoteObject>& remote)
134 {
135     HILOG_DEBUG("start");
136     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
137     if (pimpl->serviceProxy_ != nullptr) {
138         sptr<IRemoteObject> object = pimpl->serviceProxy_->GetObject();
139         if ((object != nullptr) && (remote == object)) {
140             object->RemoveDeathRecipient(pimpl->deathRecipient_);
141             pimpl->serviceProxy_ = nullptr;
142             HILOG_DEBUG("Reset OK");
143         }
144     }
145 }
146 
RegisterElementOperator(const int windowId,const shared_ptr<AccessibilityElementOperator> & operation,int user)147 int AccessibilitySystemAbilityClient::RegisterElementOperator(
148     const int windowId, const shared_ptr<AccessibilityElementOperator>& operation, int user)
149 {
150     HILOG_INFO();
151     if (!operation) {
152         HILOG_ERROR("Input operation is null");
153         return -1;
154     }
155     int result = 0;
156     connectionWindowId_ = windowId;
157     for (auto iter = interactionOperators_.begin(); iter != interactionOperators_.end(); iter++) {
158         if (iter->first == windowId) {
159             HILOG_ERROR("windowID[%{public}d] is exited", windowId);
160             return result;
161         }
162     }
163     interactionOperators_.insert(pair<int, shared_ptr<AccessibilityElementOperator>>(windowId, operation));
164     if (operation != nullptr) {
165         interactionOperator_ = operation;
166         sptr<AccessibilityElementOperatorStub> aamsInteractionOperator = new AccessibilityElementOperatorStub();
167         aamsInteractionOperator->SetWindowId(windowId);
168         auto proxyService = pimpl->GetService();
169         if (!proxyService) {
170             HILOG_ERROR("Failed to get aams service");
171             return -1;
172         }
173         proxyService->RegisterElementOperator(windowId, aamsInteractionOperator, user);
174     } else {
175         HILOG_DEBUG("AccessibilitySystemAbilityClient had register AccessibilityElementOperator.");
176     }
177 
178     return result;
179 }
180 
DeregisterElementOperator(const int windowId)181 void AccessibilitySystemAbilityClient::DeregisterElementOperator(const int windowId)
182 {
183     HILOG_DEBUG("start");
184     auto proxyService = pimpl->GetService();
185     if (!proxyService) {
186         HILOG_ERROR("Failed to get aams service");
187         return;
188     }
189     proxyService->DeregisterElementOperator(windowId);
190     for (auto iter = interactionOperators_.begin(); iter != interactionOperators_.end(); iter++) {
191         if (iter->first == windowId) {
192             HILOG_DEBUG("windowID[%{public}d] is erase", windowId);
193             interactionOperator_ = nullptr;
194             connectionWindowId_ = -1;
195             interactionOperators_.erase(iter);
196             return;
197         }
198     }
199     HILOG_DEBUG("Not find windowID[%{public}d]", windowId);
200 }
201 
GetOperatorObject(int windowId)202 shared_ptr<AccessibilityElementOperator> AccessibilitySystemAbilityClient::GetOperatorObject(int windowId)
203 {
204     HILOG_DEBUG("windowId[%{public}d]", windowId);
205     for (auto it = interactionOperators_.begin(); it != interactionOperators_.end(); it++) {
206         if (it->second != nullptr && it->first == windowId) {
207             HILOG_DEBUG("find interaction object windowId[%{public}d]", windowId);
208             return it->second;
209         }
210     }
211     HILOG_DEBUG("Failed to get interaction");
212     return nullptr;
213 }
214 
IsEnabled()215 bool AccessibilitySystemAbilityClient::IsEnabled()
216 {
217     HILOG_DEBUG("isEnabled_[%{public}d]", isEnabled_);
218     return isEnabled_;
219 }
220 
IsTouchExplorationEnabled()221 bool AccessibilitySystemAbilityClient::IsTouchExplorationEnabled()
222 {
223     HILOG_DEBUG("isEnabled_[%{public}d]", isTouchExplorationEnabled_);
224     return isTouchExplorationEnabled_;
225 }
226 
IsCaptionEnabled()227 bool AccessibilitySystemAbilityClient::IsCaptionEnabled()
228 {
229     HILOG_DEBUG("isEnabled_[%{public}d]", isCaptionEnabled_);
230     return isCaptionEnabled_;
231 }
232 
GetAbilityList(const uint32_t accessibilityAbilityTypes,const AbilityStateType stateType)233 std::vector<AccessibilityAbilityInfo> AccessibilitySystemAbilityClient::GetAbilityList(
234     const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType)
235 {
236     HILOG_DEBUG("start");
237     bool check = false;
238     if ((accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN) ||
239         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC) ||
240         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE) ||
241         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL) ||
242         (accessibilityAbilityTypes & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC)) {
243         check = true;
244     }
245     if (stateType == ABILITY_STATE_INVALID) {
246         check = false;
247     }
248     auto proxyService = pimpl->GetService();
249     if (!proxyService || !check) {
250         HILOG_ERROR("Failed to get aams service");
251         std::vector<AccessibilityAbilityInfo> infos;
252         return infos;
253     }
254     return (proxyService->GetAbilityList(accessibilityAbilityTypes, stateType));
255 }
256 
GetInstance(const Context & abilityContext)257 shared_ptr<AccessibilitySystemAbilityClient> AccessibilitySystemAbilityClient::GetInstance(
258     const Context& abilityContext)
259 {
260     HILOG_DEBUG("start");
261     if (!instance_) {
262         int accountId = 100;    // temp deal
263         instance_ = std::make_shared<AccessibilitySystemAbilityClient>(abilityContext, accountId);
264     } else {
265         HILOG_DEBUG("IAccessibleAbilityManagerServiceClient had construct instance");
266     }
267 
268     return instance_;
269 }
270 
GetInstance()271 shared_ptr<AccessibilitySystemAbilityClient> AccessibilitySystemAbilityClient::GetInstance()
272 {
273     HILOG_DEBUG("start");
274     AbilityContext abilityContext = {};
275     if (!instance_) {
276         int accountId = 100;    // temp deal
277         instance_ = std::make_shared<AccessibilitySystemAbilityClient>(abilityContext, accountId);
278     } else {
279         HILOG_DEBUG("IAccessibleAbilityManagerServiceClient had construct instance");
280     }
281 
282     return instance_;
283 }
284 
GetCaptionProperty() const285 CaptionProperty AccessibilitySystemAbilityClient::GetCaptionProperty() const
286 {
287     HILOG_DEBUG("start");
288     CaptionProperty cp;
289     auto proxyService = pimpl->GetService();
290     if (!proxyService) {
291         HILOG_ERROR("Failed to get aams service");
292         return cp;
293     }
294     return proxyService->GetCaptionProperty();
295 }
296 
SetCaptionProperty(const CaptionProperty & caption)297 bool AccessibilitySystemAbilityClient::SetCaptionProperty(const CaptionProperty& caption)
298 {
299     HILOG_DEBUG("start");
300     if (captionProperty_.GetFontScale() != caption.GetFontScale() ||
301         captionProperty_.GetFontColor() != caption.GetFontColor() ||
302         strcmp(captionProperty_.GetFontFamily().c_str(), caption.GetFontFamily().c_str()) ||
303         strcmp(captionProperty_.GetFontEdgeType().c_str(), caption.GetFontEdgeType().c_str()) ||
304         captionProperty_.GetBackgroundColor() != caption.GetBackgroundColor() ||
305         captionProperty_.GetWindowColor() != caption.GetWindowColor()) {
306         captionProperty_ = caption;
307         NotifyCaptionChanged();
308     }
309     return true;
310 }
311 
SetCaptionPropertyTojson(const CaptionProperty & caption)312 bool AccessibilitySystemAbilityClient::SetCaptionPropertyTojson(const CaptionProperty& caption)
313 {
314     HILOG_DEBUG("start");
315     if (captionProperty_.GetFontScale() != caption.GetFontScale() ||
316         captionProperty_.GetFontColor() != caption.GetFontColor() ||
317         strcmp(captionProperty_.GetFontFamily().c_str(), caption.GetFontFamily().c_str()) ||
318         strcmp(captionProperty_.GetFontEdgeType().c_str(), caption.GetFontEdgeType().c_str()) ||
319         captionProperty_.GetBackgroundColor() != caption.GetBackgroundColor() ||
320         captionProperty_.GetWindowColor() != caption.GetWindowColor()) {
321         auto proxyService = pimpl->GetService();
322         if (!proxyService) {
323             HILOG_ERROR("Failed to get aams service");
324             return false;
325         }
326         proxyService->SetCaptionProperty(caption);
327     }
328     return true;
329 }
330 
SetCaptionState(const bool state)331 bool AccessibilitySystemAbilityClient::SetCaptionState(const bool state)
332 {
333     HILOG_DEBUG("start");
334     if (isCaptionEnabled_ != state) {
335         isCaptionEnabled_ = state;
336         NotifyCaptionStateChanged();
337     }
338     return true;
339 }
340 
SetCaptionStateTojson(const bool state)341 bool AccessibilitySystemAbilityClient::SetCaptionStateTojson(const bool state)
342 {
343     HILOG_DEBUG("start");
344     bool ret = false;
345     if (isCaptionEnabled_ != state) {
346         auto proxyService = pimpl->GetService();
347         if (!proxyService) {
348             HILOG_ERROR("Failed to get aams service");
349             return false;
350         }
351         ret = proxyService->SetCaptionState(state);
352     }
353 
354     return ret;
355 }
356 
SetEnabled(const bool state)357 bool AccessibilitySystemAbilityClient::SetEnabled(const bool state)
358 {
359     HILOG_DEBUG("set state is %{public}d", state);
360     if (isEnabled_ != state) {
361         isEnabled_ = state;
362         NotifyAccessibilityStateChanged();
363     }
364     return true;
365 }
366 
IsAccessibilityCaptionEnabled() const367 bool AccessibilitySystemAbilityClient::IsAccessibilityCaptionEnabled() const
368 {
369     HILOG_DEBUG("start");
370     return true;
371 }
372 
CheckEventType(EventType eventType)373 bool AccessibilitySystemAbilityClient::CheckEventType(EventType eventType)
374 {
375     if ((eventType < EventType::TYPE_VIEW_CLICKED_EVENT) ||
376         ((eventType >= EventType::TYPE_MAX_NUM) && (eventType != EventType::TYPES_ALL_MASK))) {
377         HILOG_ERROR("event type is invalid");
378         return false;
379     } else {
380         return true;
381     }
382 }
383 
CheckActionType(ActionType actionType)384 bool AccessibilitySystemAbilityClient::CheckActionType(ActionType actionType)
385 {
386     if ((actionType < ActionType::ACCESSIBILITY_ACTION_FOCUS) ||
387         ((actionType > ActionType::ACCESSIBILITY_ACTION_DELETED) &&
388          (actionType != ActionType::ACCESSIBILITY_ACTION_TYPE_MASK))) {
389         HILOG_ERROR("action type is invalid");
390         return false;
391     } else {
392         return true;
393     }
394 }
395 
SendEvent(const EventType eventType,const int componentId)396 bool AccessibilitySystemAbilityClient::SendEvent(const EventType eventType, const int componentId)
397 {
398     HILOG_DEBUG("componentId[%{public}d], eventType[%{public}d]", componentId, eventType);
399     if (!CheckEventType(eventType)) {
400         return false;
401     }
402     AccessibilityEventInfo event;
403     event.SetEventType(eventType);
404     event.SetSource(componentId);
405     auto proxyService = pimpl->GetService();
406     if (!proxyService) {
407         HILOG_ERROR("Failed to get aams service");
408         return false;
409     }
410     proxyService->SendEvent(event, accountId_);
411     return true;
412 }
413 
SendEvent(const AccessibilityEventInfo & event)414 bool AccessibilitySystemAbilityClient::SendEvent(const AccessibilityEventInfo& event)
415 {
416     HILOG_DEBUG("EventType[%{public}d]", event.GetEventType());
417     if (!CheckEventType(event.GetEventType())) {
418         return false;
419     }
420     auto proxyService = pimpl->GetService();
421     if (!proxyService) {
422         HILOG_ERROR("Failed to get aams service");
423         return false;
424     }
425     proxyService->SendEvent(event, accountId_);
426     return true;
427 }
428 
SubscribeStateObserver(const shared_ptr<AccessibilityStateObserver> & observer,const int eventType)429 bool AccessibilitySystemAbilityClient::SubscribeStateObserver(
430     const shared_ptr<AccessibilityStateObserver>& observer, const int eventType)
431 {
432     HILOG_DEBUG("start");
433     if (eventType != AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED &&
434         eventType != AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED &&
435         eventType != AccessibilityStateEventType::EVENT_CAPTION_STATE_CHANGED &&
436         eventType != AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED &&
437         eventType != AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED) {
438         HILOG_ERROR("Input eventType is out of scope");
439         return false;
440     }
441     if (!observer) {
442         HILOG_ERROR("Input observer is null");
443         return false;
444     }
445     AccessibilityStateEventType et = AccessibilityStateEventType(*(const_cast<int*>(&eventType)));
446     shared_ptr<AccessibilityStateObserver> ob = const_cast<shared_ptr<AccessibilityStateObserver>&>(observer);
447     switch (et) {
448         case AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED:
449             observersAccessibilityState_.push_back(ob);
450             break;
451         case AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED:
452             observersTouchState_.push_back(ob);
453             break;
454         case AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED:
455             observersKeyEventState_.push_back(ob);
456             break;
457         case AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED:
458             observersGestureState_.push_back(ob);
459             break;
460         default:
461             HILOG_ERROR("the EventType observed is not supported");
462             return false;
463     }
464     HILOG_DEBUG("end");
465     return true;
466 }
467 
UnsubscribeStateObserver(const shared_ptr<AccessibilityStateObserver> & observer,const int eventType)468 bool AccessibilitySystemAbilityClient::UnsubscribeStateObserver(
469     const shared_ptr<AccessibilityStateObserver>& observer, const int eventType)
470 {
471     HILOG_DEBUG("start");
472     if (eventType != AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED &&
473         eventType != AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED &&
474         eventType != AccessibilityStateEventType::EVENT_CAPTION_STATE_CHANGED &&
475         eventType != AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED &&
476         eventType != AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED) {
477         HILOG_ERROR("Input eventType is out of scope");
478         return false;
479     }
480     if (!observer) {
481         HILOG_ERROR("Input observer is null");
482         return false;
483     }
484     std::vector<std::shared_ptr<AccessibilityStateObserver>> observerTable;
485     AccessibilityStateEventType et = AccessibilityStateEventType(*(const_cast<int*>(&eventType)));
486     switch (et) {
487         case AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED:
488             observerTable = observersAccessibilityState_;
489             break;
490         case AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED:
491             observerTable = observersTouchState_;
492             break;
493         case AccessibilityStateEventType::EVENT_KEVEVENT_STATE_CHANGED:
494             observerTable = observersKeyEventState_;
495             break;
496         case AccessibilityStateEventType::EVENT_GESTURE_STATE_CHANGED:
497             observerTable = observersGestureState_;
498             break;
499         default:
500             HILOG_ERROR("the EventType observed is not supported");
501             return false;
502     }
503     for (auto it = observerTable.begin(); it != observerTable.end(); it++) {
504         if (*it == observer) {
505             observerTable.erase(it);
506             return true;
507         }
508     }
509     HILOG_DEBUG("Not find eventType[%{public}d]", eventType);
510     return false;
511 }
512 
UnsubscribeStateObserver(const shared_ptr<AccessibilityStateObserver> & observer)513 bool AccessibilitySystemAbilityClient::UnsubscribeStateObserver(const shared_ptr<AccessibilityStateObserver>& observer)
514 {
515     HILOG_DEBUG("start");
516     if (!observer) {
517         HILOG_ERROR("Input observer is null");
518         return false;
519     }
520     bool result = false;
521     for (auto it = observersAccessibilityState_.begin(); it != observersAccessibilityState_.end(); it++) {
522         if (*it == observer) {
523             observersAccessibilityState_.erase(it);
524             result = true;
525             break;
526         }
527     }
528 
529     for (auto it = observersTouchState_.begin(); it != observersTouchState_.end(); it++) {
530         if (*it == observer) {
531             observersTouchState_.erase(it);
532             result = true;
533             break;
534         }
535     }
536 
537     for (auto it = observersKeyEventState_.begin(); it != observersKeyEventState_.end(); it++) {
538         if (*it == observer) {
539             observersKeyEventState_.erase(it);
540             result = true;
541             break;
542         }
543     }
544 
545     for (auto it = observersGestureState_.begin(); it != observersGestureState_.end(); it++) {
546         if (*it == observer) {
547             observersGestureState_.erase(it);
548             result = true;
549             break;
550         }
551     }
552 
553     return result;
554 }
555 
UpdateEnabled(const bool enabled)556 void AccessibilitySystemAbilityClient::UpdateEnabled(const bool enabled)
557 {
558     HILOG_DEBUG("start");
559     if (isEnabled_ != enabled) {
560         isEnabled_ = enabled;
561         NotifyAccessibilityStateChanged();
562     }
563     HILOG_DEBUG("end");
564 }
565 
UpdateTouchExplorationEnabled(const bool enabled)566 void AccessibilitySystemAbilityClient::UpdateTouchExplorationEnabled(const bool enabled)
567 {
568     HILOG_DEBUG("start");
569     if (isTouchExplorationEnabled_ != enabled) {
570         isTouchExplorationEnabled_ = enabled;
571         NotifyTouchExplorationStateChanged();
572     }
573     HILOG_DEBUG("end");
574 }
575 
SetCaptionEnabled(const bool enabled)576 void AccessibilitySystemAbilityClient::SetCaptionEnabled(const bool enabled)
577 {
578     HILOG_DEBUG("start");
579     if (isCaptionEnabled_ != enabled) {
580         isCaptionEnabled_ = enabled;
581         NotifyCaptionStateChanged();
582     }
583     HILOG_DEBUG("end");
584 }
585 
NotifyAccessibilityStateChanged()586 void AccessibilitySystemAbilityClient::NotifyAccessibilityStateChanged()
587 {
588     HILOG_DEBUG("isEnabled_ is %{public}d", isEnabled_);
589     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
590     if (!observersAccessibilityState_.size()) {
591         HILOG_DEBUG("observersAccessibilityState_ is null");
592         return;
593     }
594     for (auto it = observersAccessibilityState_.begin(); it != observersAccessibilityState_.end(); it++) {
595         if (*it != nullptr && it->get() != nullptr) {
596             it->get()->OnStateChanged(isEnabled_);
597         } else {
598             HILOG_ERROR("end observersAccessibilityState_ is null");
599         }
600     }
601     HILOG_DEBUG("end");
602 }
603 
NotifyTouchExplorationStateChanged()604 void AccessibilitySystemAbilityClient::NotifyTouchExplorationStateChanged()
605 {
606     HILOG_DEBUG("start");
607     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
608     if (!observersTouchState_.size()) {
609         HILOG_DEBUG("observersTouchState_ is null");
610         return;
611     }
612     for (auto it = observersTouchState_.begin(); it != observersTouchState_.end(); it++) {
613         if (*it != nullptr && it->get() != nullptr) {
614             it->get()->OnStateChanged(isTouchExplorationEnabled_);
615         } else {
616             HILOG_ERROR("end observersTouchState_ is null");
617         }
618     }
619     HILOG_DEBUG("end");
620 }
621 
NotifyCaptionStateChanged()622 void AccessibilitySystemAbilityClient::NotifyCaptionStateChanged()
623 {
624     HILOG_DEBUG("start");
625     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
626     if (!observersCaptionEnable_.size()) {
627         HILOG_DEBUG("observersCaptionEnable_ is null");
628         return;
629     }
630     for (auto it = observersCaptionEnable_.begin(); it != observersCaptionEnable_.end(); it++) {
631         if (*it != nullptr && it->get() != nullptr) {
632             it->get()->OnCaptionStateChanged(isCaptionEnabled_);
633         } else {
634             HILOG_ERROR("end observersCaptionEnable_ is null");
635         }
636     }
637     HILOG_DEBUG("end");
638 }
639 
AddCaptionListener(const std::shared_ptr<CaptionObserver> & ob,const int type)640 bool AccessibilitySystemAbilityClient::AddCaptionListener(const std::shared_ptr<CaptionObserver>& ob, const int type)
641 {
642     HILOG_DEBUG("start");
643     bool result = true;
644     if (type == CaptionObserverType::CAPTION_ENABLE) {
645         if (!observersCaptionEnable_.size()) {
646             observersCaptionEnable_.push_back(ob);
647         }
648     } else if (type == CaptionObserverType::CAPTION_PROPERTY) {
649         if (!observersCaptionProperty_.size()) {
650             observersCaptionProperty_.push_back(ob);
651         }
652     } else {
653         result = false;
654         HILOG_ERROR("Type Error ");
655     }
656 
657     return result;
658 }
659 
DeleteCaptionListener(const std::shared_ptr<CaptionObserver> & ob,const int type)660 bool AccessibilitySystemAbilityClient::DeleteCaptionListener(
661     const std::shared_ptr<CaptionObserver>& ob, const int type)
662 {
663     HILOG_DEBUG("start");
664     bool result = false;
665     if (type == CaptionObserverType::CAPTION_ENABLE) {
666         for (auto it = observersCaptionEnable_.begin(); it != observersCaptionEnable_.end(); ++it) {
667             if (*it == ob) {
668                 observersCaptionEnable_.erase(it);
669                 result = true;
670                 break;
671             }
672         }
673     } else if (type == CaptionObserverType::CAPTION_PROPERTY) {
674         for (auto it = observersCaptionProperty_.begin(); it != observersCaptionProperty_.end(); ++it) {
675             if (*it == ob) {
676                 observersCaptionProperty_.erase(it);
677                 result = true;
678                 break;
679             }
680         }
681     } else {
682         HILOG_ERROR("Type Error ");
683     }
684 
685     return result;
686 }
687 
UpdatecaptionProperty(const CaptionProperty & property)688 void AccessibilitySystemAbilityClient::UpdatecaptionProperty(const CaptionProperty& property)
689 {
690     HILOG_DEBUG("start");
691     captionProperty_ = property;
692     NotifyCaptionChanged();
693     HILOG_DEBUG("end");
694 }
695 
NotifyCaptionChanged()696 void AccessibilitySystemAbilityClient::NotifyCaptionChanged()
697 {
698     HILOG_DEBUG("start");
699     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
700     if (!observersCaptionProperty_.size()) {
701         HILOG_DEBUG("observersCaptionProperty_ is null");
702         return;
703     }
704     for (auto it = observersCaptionProperty_.begin(); it != observersCaptionProperty_.end(); ++it) {
705         if (*it != nullptr && it->get() != nullptr) {
706             it->get()->OnCaptionPropertyChanged(captionProperty_);
707         } else {
708             HILOG_ERROR("end observersCaptionProperty_ is null");
709         }
710     }
711     HILOG_DEBUG("end");
712 }
713 
GetEnabledState()714 bool AccessibilitySystemAbilityClient::GetEnabledState()
715 {
716     HILOG_DEBUG("start");
717     auto proxyService = pimpl->GetService();
718     if (!proxyService) {
719         HILOG_ERROR("Failed to get aams service");
720         return false;
721     }
722     return proxyService->GetEnabledState();
723 }
724 
GetCaptionState()725 bool AccessibilitySystemAbilityClient::GetCaptionState()
726 {
727     HILOG_DEBUG("start");
728     auto proxyService = pimpl->GetService();
729     if (!proxyService) {
730         HILOG_ERROR("Failed to get aams service");
731         return false;
732     }
733 
734     isCaptionEnabled_ = proxyService->GetCaptionState();
735     return isCaptionEnabled_;
736 }
737 
GetTouchGuideState()738 bool AccessibilitySystemAbilityClient::GetTouchGuideState()
739 {
740     HILOG_DEBUG("start");
741     auto proxyService = pimpl->GetService();
742     if (!proxyService) {
743         HILOG_ERROR("Failed to get aams service");
744         return false;
745     }
746     return proxyService->GetTouchGuideState();
747 }
748 
GetGestureState()749 bool AccessibilitySystemAbilityClient::GetGestureState()
750 {
751     HILOG_DEBUG("start");
752     auto proxyService = pimpl->GetService();
753     if (!proxyService) {
754         HILOG_ERROR("Failed to get aams service");
755         return false;
756     }
757     return proxyService->GetGestureState();
758 }
759 
GetKeyEventObserverState()760 bool AccessibilitySystemAbilityClient::GetKeyEventObserverState()
761 {
762     HILOG_DEBUG("start");
763     auto proxyService = pimpl->GetService();
764     if (!proxyService) {
765         HILOG_ERROR("Failed to get aams service");
766         return false;
767     }
768     return proxyService->GetKeyEventObserverState();
769 }
770 
SetTouchGuideState(const bool state)771 bool AccessibilitySystemAbilityClient::SetTouchGuideState(const bool state)
772 {
773     HILOG_DEBUG("start");
774     if (isTouchExplorationEnabled_ != state) {
775         isTouchExplorationEnabled_ = state;
776         NotifyTouchExplorationStateChanged();
777     }
778     return true;
779 }
780 
SetGestureState(const bool state)781 bool AccessibilitySystemAbilityClient::SetGestureState(const bool state)
782 {
783     HILOG_DEBUG("start");
784     if (isGesturesSimulationEnabled_ != state) {
785         isGesturesSimulationEnabled_ = state;
786         NotifyGestureStateChanged();
787     }
788     return true;
789 }
790 
SetKeyEventObserverState(const bool state)791 bool AccessibilitySystemAbilityClient::SetKeyEventObserverState(const bool state)
792 {
793     HILOG_DEBUG("start");
794     if (isFilteringKeyEventsEnabled_ != state) {
795         isFilteringKeyEventsEnabled_ = state;
796         NotifyKeyEventStateChanged();
797     }
798     return true;
799 }
800 
SetEnabledObj(std::map<std::string,AppExecFwk::ElementName> it)801 bool AccessibilitySystemAbilityClient::SetEnabledObj(std::map<std::string, AppExecFwk::ElementName> it)
802 {
803     HILOG_DEBUG("start");
804     auto proxyService = pimpl->GetService();
805     if (!proxyService) {
806         HILOG_ERROR("Failed to get aams service");
807         return false;
808     }
809     proxyService->SetEnabledObj(it);
810     return true;
811 }
812 
GetEnabledAbilities()813 std::map<std::string, AppExecFwk::ElementName> AccessibilitySystemAbilityClient::GetEnabledAbilities()
814 {
815     HILOG_DEBUG("start");
816 
817     auto proxyService = pimpl->GetService();
818     if (!proxyService) {
819         HILOG_ERROR("Failed to get aams service");
820         std::map<std::string, AppExecFwk::ElementName> it;
821         return it;
822     }
823     return proxyService->GetEnabledAbilities();
824 }
825 
GetInstalledAbilities()826 std::vector<AccessibilityAbilityInfo> AccessibilitySystemAbilityClient::GetInstalledAbilities()
827 {
828     HILOG_DEBUG("start");
829     auto proxyService = pimpl->GetService();
830     if (!proxyService) {
831         HILOG_ERROR("Failed to get aams service");
832         std::vector<AccessibilityAbilityInfo> it;
833         return it;
834     }
835     return proxyService->GetInstalledAbilities();
836 }
837 
NotifyGestureStateChanged()838 void AccessibilitySystemAbilityClient::NotifyGestureStateChanged()
839 {
840     HILOG_DEBUG("start");
841     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
842     if (!observersGestureState_.size()) {
843         HILOG_DEBUG("observersGestureState_ is null");
844         return;
845     }
846     for (auto it = observersGestureState_.begin(); it != observersGestureState_.end(); it++) {
847         if (*it != nullptr && it->get() != nullptr) {
848             it->get()->OnStateChanged(isGesturesSimulationEnabled_);
849         } else {
850             HILOG_ERROR("end observersGestureState_ is null");
851         }
852     }
853     HILOG_DEBUG("end");
854 }
855 
NotifyKeyEventStateChanged()856 void AccessibilitySystemAbilityClient::NotifyKeyEventStateChanged()
857 {
858     HILOG_DEBUG("start");
859     std::lock_guard<std::recursive_mutex> lock(asacProxyLock_);
860     if (!observersKeyEventState_.size()) {
861         HILOG_DEBUG("observersKeyEventState_ is null");
862         return;
863     }
864     for (auto it = observersKeyEventState_.begin(); it != observersKeyEventState_.end(); it++) {
865         if (*it != nullptr && it->get() != nullptr) {
866             it->get()->OnStateChanged(isFilteringKeyEventsEnabled_);
867         } else {
868             HILOG_ERROR("end observersKeyEventState_ is null");
869         }
870     }
871     HILOG_DEBUG("end");
872 }
873 
DisableAbilities(std::map<std::string,AppExecFwk::ElementName> it)874 bool AccessibilitySystemAbilityClient::DisableAbilities(std::map<std::string, AppExecFwk::ElementName> it)
875 {
876     HILOG_DEBUG("start");
877     auto proxyService = pimpl->GetService();
878     if (!proxyService) {
879         HILOG_ERROR("Failed to get aams service");
880         return false;
881     }
882     proxyService->DisableAbilities(it);
883 
884     return true;
885 }
886 
GetActiveWindow()887 int AccessibilitySystemAbilityClient::GetActiveWindow()
888 {
889     HILOG_DEBUG("start");
890     auto proxyService = pimpl->GetService();
891     if (!proxyService) {
892         HILOG_ERROR("Failed to get aams service");
893         return INVALID_WINDOW_ID;
894     }
895 
896     return proxyService->GetActiveWindow();
897 }
898 } // namespace Accessibility
899 } // namespace OHOS