• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H
17 #define ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H
18 
19 #include <array>
20 #include <mutex>
21 #include "accessibility_element_operator_impl.h"
22 #include "accessibility_system_ability_client.h"
23 #include "accessible_ability_manager_state_observer_stub.h"
24 #include "i_accessible_ability_manager_service.h"
25 
26 namespace OHOS {
27 namespace Accessibility {
28 using StateArray = std::array<bool, AccessibilityStateEventType::EVENT_TYPE_MAX>;
29 using StateObserverVector = std::vector<std::shared_ptr<AccessibilityStateObserver>>;
30 using StateObserversArray = std::array<StateObserverVector, AccessibilityStateEventType::EVENT_TYPE_MAX>;
31 class AccessibilitySystemAbilityClientImpl
32     : public AccessibilitySystemAbilityClient, public AccessibilityElementOperatorCallback {
33 public:
34     /**
35      * @brief Construct.
36      */
37     AccessibilitySystemAbilityClientImpl();
38 
39     /**
40      * @brief Deconstruct.
41      */
42     ~AccessibilitySystemAbilityClientImpl();
43 
44     /**
45      * @brief Register the element operator, so the AA can get node info from ACE.
46      * @param windowId Window ID
47      * @param operation The callback object.
48      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
49      */
50     virtual RetError RegisterElementOperator(const int32_t windowId,
51         const std::shared_ptr<AccessibilityElementOperator> &operation) override;
52 
53     /**
54      * @brief Deregister the element operator.
55      * @param windowId Window ID
56      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
57      */
58     virtual RetError DeregisterElementOperator(const int32_t windowId) override;
59 
60     /**
61      * @brief Checks whether accessibility ability is enabled.
62      * @param isEnabled true: enabled; false: disabled
63      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
64      */
65     virtual RetError IsEnabled(bool &isEnabled) override;
66 
67     /**
68      * @brief Checks whether touch exploration ability is enabled.
69      * @param isEnabled true: enabled; false: disabled
70      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
71      */
72     virtual RetError IsTouchExplorationEnabled(bool &isEnabled) override;
73 
74     /**
75      * @brief Queries the list of accessibility abilities.
76      * @param accessibilityAbilityTypes Indicates the accessibility type specified by AccessibilityAbilityTypes.
77      * @param stateType Indicates the accessibility ability status.
78      *                  1 indicates that the ability is enabled;
79      *                  2 indicates that the ability is disabled;
80      *                  3 indicates that the ability has been installed.
81      * @param infos accessibility ability infos by specified types.
82      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
83      */
84     virtual RetError GetAbilityList(const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType,
85         std::vector<AccessibilityAbilityInfo> &infos) override;
86 
87     /**
88      * @brief Sends an accessibility event.
89      * @param eventType  Identifies the accessibility event specified by AccessibilityEventInfo.
90      * @param componentId Indicates the ID of the component to be associated with the event.
91      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
92      */
93     virtual RetError SendEvent(const EventType eventType, const int64_t componentId) override;
94 
95     /**
96      * @brief Sends information about an accessibility event.
97      * @param event Indicates the accessibility event information specified by AccessibilityEventInfo.
98      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
99      */
100     virtual RetError SendEvent(const AccessibilityEventInfo &event) override;
101 
102     /**
103      * @brief Subscribes to the specified type of accessibility status change events.
104      * @param observer Indicates the observer for listening to status events, which is specified
105      *              by AccessibilityStateObserver.
106      * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType.
107      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
108      */
109     virtual RetError SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer,
110         const uint32_t eventType) override;
111 
112     /**
113      * @brief Unsubscribe the specified type of accessibility status change events.
114      * @param observer Indicates the registered accessibility status event observer.
115      * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType.
116      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
117      */
118     virtual RetError UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer,
119         const uint32_t eventType) override;
120 
121     /**
122      * @brief Get enabled abilities.
123      * @param enabledAbilities The infos of enabled abilities.
124      * @return Return RET_OK if successful, otherwise refer to the RetError for the failure.
125      */
126     virtual RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override;
127 
128     /**
129      * @brief Clean the AAMS object data.
130      * @param remote The object access to AAMS.
131      */
132     void ResetService(const wptr<IRemoteObject> &remote);
133 
134     /**
135      * @brief Receive the state notify from AAMS and send it to the observer registered.
136      * @param stateType The state type and value.
137      *                  state type: Refer to AccessibilityStateEventType.
138      *                  value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/
139      *                         STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED
140      */
141     void OnAccessibleAbilityManagerStateChanged(const uint32_t stateType);
142 
143     /**
144      * @brief Set the accessibility state.
145      * @param stateType The state type and value.
146      *                  state type: Refer to AccessibilityStateEventType.
147      *                  value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/
148      *                         STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED
149      */
150     void SetAccessibilityState(const uint32_t stateType);
151 
152     /**
153      * @brief Set the element information by accessibility id to AA.
154      * @param infos The element info searched by accessibility id.
155      * @param requestId The request id from AA, it is used to match with request and response.
156      */
157     virtual void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
158         const int32_t requestId) override;
159 
160     /**
161      * @brief Set the element information matched with text to AA.
162      * @param infos The element information searched matched with text.
163      * @param requestId The request id from AA, it is used to match with request and response.
164      */
165     virtual void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
166         const int32_t requestId) override;
167 
168     /**
169      * @brief Set the element information matched with focus type to AA.
170      * @param info The element information searched matched with focus type.
171      * @param requestId The request id from AA, it is used to match with request and response.
172      */
173     virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info,
174         const int32_t requestId) override;
175 
176     /**
177      * @brief Set the element information by focus direction to AA.
178      * @param info The element information searched by focus direction.
179      * @param requestId The request id from AA, it is used to match with request and response.
180      */
181     virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override;
182 
183     /**
184      * @brief Set the result of action executed to AA.
185      * @param succeeded True: The action is executed successfully; otherwise is false.
186      * @param requestId The request id from AA, it is used to match with request and response.
187      */
188     virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override;
189 
190     virtual uint32_t GetAccessibilityState() override;
191     virtual void SetFindAccessibilityNodeInfoResult(const AccessibilityElementInfo elementInfo,
192         const int32_t requestId, const int32_t requestCode) override;
193     virtual void SetFindAccessibilityNodeInfosResult(const std::list<AccessibilityElementInfo> elementInfos,
194         const int32_t requestId, const int32_t requestCode) override;
195     virtual void SetPerformActionResult(const bool succeeded, const int32_t requestId) override;
196     virtual RetError GetFocusedWindowId(int32_t &focusedWindowId) override;
197 
198 private:
199     class AccessibleAbilityManagerStateObserverImpl : public AccessibleAbilityManagerStateObserverStub {
200     public:
AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl & client)201         explicit AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl &client)
202             : client_(client) {}
203         ~AccessibleAbilityManagerStateObserverImpl() = default;
204 
OnStateChanged(const uint32_t stateType)205         virtual void OnStateChanged(const uint32_t stateType) override
206         {
207             client_.OnAccessibleAbilityManagerStateChanged(stateType);
208         }
209     private:
210         AccessibilitySystemAbilityClientImpl &client_;
211     };
212 
213     class DeathRecipient : public IRemoteObject::DeathRecipient {
214     public:
DeathRecipient(AccessibilitySystemAbilityClientImpl & client)215         explicit DeathRecipient(AccessibilitySystemAbilityClientImpl &client) : client_(client) {}
216         ~DeathRecipient() = default;
217         DISALLOW_COPY_AND_MOVE(DeathRecipient);
218 
OnRemoteDied(const wptr<IRemoteObject> & remote)219         void OnRemoteDied(const wptr<IRemoteObject> &remote)
220         {
221             client_.ResetService(remote);
222         }
223     private:
224         AccessibilitySystemAbilityClientImpl &client_;
225     };
226 
227     /**
228      * @brief Connect to AAMS Service.
229      * @return success : true, failed : false.
230      */
231     bool ConnectToService();
232 
233     void Init();
234 
235     /**
236      * @brief Notify the state is changed.
237      * @param stateType The state type and value.
238      *                  state type: Refer to AccessibilityStateEventType.
239      * @param value The value be changed.
240      */
241     void NotifyStateChanged(uint32_t eventType, bool value);
242 
243     /**
244      * @brief Check the event type is valid or not.
245      * @param eventType The data of event type.
246      * @return True: The data of event type is valid; otherwise is not.
247      */
248     bool CheckEventType(EventType eventType);
249     static void OnParameterChanged(const char *key, const char *value, void *context);
250     void ReregisterElementOperator();
251 
252     uint32_t state_;
253     std::mutex mutex_;
254     StateArray stateArray_;
255     StateObserversArray stateObserversArray_;
256 
257     std::map<int32_t, sptr<AccessibilityElementOperatorImpl>> elementOperators_;
258     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
259     sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr;
260     sptr<AccessibleAbilityManagerStateObserverImpl> stateObserver_ = nullptr;
261 };
262 } // namespace Accessibility
263 } // namespace OHOS
264 #endif // ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H