• 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 ACCESSIBLE_ABILITY_CLIENT_IMPL_H
17 #define ACCESSIBLE_ABILITY_CLIENT_IMPL_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 #include "accessible_ability_channel_client.h"
23 #include "accessible_ability_client.h"
24 #include "accessible_ability_client_stub.h"
25 #include "i_accessible_ability_manager_service.h"
26 
27 namespace OHOS {
28 namespace Accessibility {
29 class AccessibleAbilityClientImpl : public AccessibleAbilityClient, public AccessibleAbilityClientStub {
30 public:
31     /**
32      * @brief The constructor of AccessibleAbilityClientImpl.
33      */
34     AccessibleAbilityClientImpl();
35 
36     /**
37      * @brief The deconstructor of AccessibleAbilityClientImpl.
38      */
39     ~AccessibleAbilityClientImpl();
40 
41     /**
42      * @brief Get the implement of accessibility ability client.
43      */
44     static sptr<AccessibleAbilityClientImpl> GetAbilityClientImplement();
45 
46     /**
47      * @brief Gets remote object.
48      * @return Remote object.
49      */
50     virtual sptr<IRemoteObject> GetRemoteObject() override;
51 
52     /**
53      * @brief Register ability listener.
54      * @param listener The listener to add.
55      * @return Return RET_OK if registers listener successfully, otherwise refer to the RetError for the failure.
56      */
57     virtual RetError RegisterAbilityListener(const std::shared_ptr<AccessibleAbilityListener> &listener) override;
58 
59     /**
60      * @brief Init accessible ability.
61      * @param channel The object of IAccessibleAbilityChannel.
62      * @param channelId The id of channel.
63      */
64     virtual void Init(const sptr<IAccessibleAbilityChannel> &channel, const int32_t channelId) override;
65 
66     /**
67      * @brief Disconnect accessible ability.
68      * @param channelId The id of channel.
69      */
70     virtual void Disconnect(const int32_t channelId) override;
71 
72     /**
73      * @brief Called when an accessibility event occurs.
74      * @param eventInfo The information of accessible event.
75      */
76     virtual void OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo) override;
77 
78     /**
79      * @brief Called when a key event occurs.
80      * @param keyEvent Indicates the key event to send.
81      * @param sequence The sequence of the key event.
82      */
83     virtual void OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence) override;
84 
85     /**
86      * @brief Obtains elementInfo of focus.
87      * @param focusType The type of focus. It contains FOCUS_TYPE_INPUT and FOCUS_TYPE_ACCESSIBILITY.
88      * @param elementInfo The accessibilityElementInfo of focus.
89      * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure.
90      */
91     virtual RetError GetFocus(const int32_t focusType, AccessibilityElementInfo &elementInfo) override;
92 
93     /**
94      * @brief Obtains elementInfo of focus.
95      * @param sourceInfo The source info to get focus.
96      * @param focusType The type of focus. It contains FOCUS_TYPE_INPUT and FOCUS_TYPE_ACCESSIBILITY.
97      * @param elementInfo The accessibilityElementInfo of focus.
98      * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure.
99      */
100     virtual RetError GetFocusByElementInfo(const AccessibilityElementInfo &sourceInfo, const int32_t focusType,
101         AccessibilityElementInfo &elementInfo) override;
102 
103     /**
104      * @brief Sends simulate gestures to the screen.
105      * @param gesturePath The gesture which need to send.
106      * @return Return RET_OK if the gesture sends successfully, otherwise refer to the RetError for the failure.
107      */
108     virtual RetError InjectGesture(const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath) override;
109 
110     /**
111      * @brief Obtains elementInfo of the accessible root node.
112      * @param elementInfo The elementInfo of the accessible root node.
113      * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure.
114      */
115     virtual RetError GetRoot(AccessibilityElementInfo &elementInfo) override;
116 
117     /**
118      * @brief Obtains elementInfo of the accessible root node.
119      * @param windowInfo The source window info to get root.
120      * @param elementInfo The elementInfo of the accessible root node.
121      * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure.
122      */
123     virtual RetError GetRootByWindow(const AccessibilityWindowInfo &windowInfo,
124         AccessibilityElementInfo &elementInfo) override;
125 
126     /**
127      * @brief Get the window information related with the event
128      * @param windowId The window id.
129      * @param windowInfo The window information.
130      * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure.
131      */
132     virtual RetError GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo) override;
133 
134     /**
135      * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users.
136      * @param windows The information of windows.
137      * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure.
138      */
139     virtual RetError GetWindows(std::vector<AccessibilityWindowInfo> &windows) override;
140 
141     /**
142      * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users.
143      * @param displayId the id of display
144      * @param windows The information of windows.
145      * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure.
146      */
147     virtual RetError GetWindows(const uint64_t displayId, std::vector<AccessibilityWindowInfo> &windows) override;
148 
149     /**
150      * @brief Gets the next focused node in the specified direction of the currently focused node.
151      * @param elementInfo The source info to get next info.
152      * @param direction Indicates the direction to obtain the next focused node. Refer to FocusMoveDirection
153      * @param nextElementInfo The info of next element.
154      * @return Return RET_OK if gets next elementInfo successfully, otherwise refer to the RetError for the failure.
155      */
156     virtual RetError GetNext(const AccessibilityElementInfo &elementInfo, const FocusMoveDirection direction,
157         AccessibilityElementInfo &nextElementInfo) override;
158 
159     /**
160      * @brief Get the child node information by childId
161      * @param index The index of the child.
162      * @param parent The parent info to get child.
163      * @param child The element info of child.
164      * @return Return RET_OK if gets child elementInfo successfully, otherwise refer to the RetError for the failure.
165      */
166     virtual RetError GetChildElementInfo(const int32_t index, const AccessibilityElementInfo &parent,
167         AccessibilityElementInfo &child) override;
168 
169     /**
170      * @brief Get the child node information
171      * @param parent The parent info to get child.
172      * @param children The element info of children.
173      * @return Return RET_OK if gets child elementInfo successfully, otherwise refer to the RetError for the failure.
174      */
175     virtual RetError GetChildren(const AccessibilityElementInfo &parent,
176         std::vector<AccessibilityElementInfo> &children) override;
177 
178     /**
179      * @brief Searches for node information based on the specified content.
180      * @param elementInfo The source info.
181      * @param text specified content
182      * @param elementInfos The element infos of specified content.
183      * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure.
184      */
185     virtual RetError GetByContent(const AccessibilityElementInfo &elementInfo, const std::string &text,
186         std::vector<AccessibilityElementInfo> &elementInfos) override;
187 
188     /**
189      * @brief Get the node information related with the event
190      * @param eventInfo The source info to get source.
191      * @param elementInfo The element info of source.
192      * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure.
193      */
194     virtual RetError GetSource(const AccessibilityEventInfo &eventInfo,
195         AccessibilityElementInfo &elementInfo) override;
196 
197     /**
198      * @brief Get Parent node information
199      * @param child The child element info to get parent.
200      * @param parent The parent element info.
201      * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure.
202      */
203     virtual RetError GetParentElementInfo(const AccessibilityElementInfo &child,
204         AccessibilityElementInfo &parent) override;
205 
206     /**
207      * @brief Executes a specified action.
208      * @param elementInfo The source info to execute action.
209      * @param action: the action type
210      * @param actionArguments: The parameter for action type. such as:
211      *      action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
212      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
213      *      action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
214      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
215      *      action: ACCESSIBILITY_ACTION_NEXT_TEXT,
216      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
217      *      action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
218      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
219      *      action: ACCESSIBILITY_ACTION_SET_SELECTION,
220      *                  actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
221      *                                  {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
222      *      action: ACCESSIBILITY_ACTION_SET_TEXT,
223      *                  actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
224      * @return Return RET_OK if performs action succeed, otherwise refer to the RetError for the failure.
225      */
226     virtual RetError ExecuteAction(const AccessibilityElementInfo &elementInfo, const ActionType action,
227         const std::map<std::string, std::string> &actionArguments) override;
228 
229     /**
230      * @brief Set target bundle names.
231      * @param targetBundleNames The target bundle name
232      * @return Return RET_OK if sets target bundle names successfully, otherwise refer to the RetError for the failure.
233      */
234     virtual RetError SetTargetBundleName(const std::vector<std::string> &targetBundleNames) override;
235 
236     /**
237      * @brief Set cache mode.
238      *        The mode is used for functions: GetRoot, GetRootByWindow, GetChildElementInfo,
239      *        GetChildren, GetSource, GetParentElementInfo.
240      * @param cacheMode The cache mode. It includes:
241      *             PREFETCH_PREDECESSORS: cache the parent node info also.
242      *             PREFETCH_SIBLINGS: cache the sister/brothers node info also.
243      *             PREFETCH_CHILDREN: cache the child node info also.
244      *             otherwise: no cache.
245      * @return Return RET_OK if sets cache mode successfully, otherwise refer to the RetError for the failure.
246      */
247     virtual RetError SetCacheMode(const int32_t cacheMode) override;
248 
249     /**
250      * @brief Clean data.
251      * @param remote The object access to AAMS.
252      */
253     void ResetAAClient(const wptr<IRemoteObject> &remote);
254 
255     /**
256      * @brief Notify AA client and clean data when service is died.
257      * @param remote The object access to AAMS.
258      */
259     void NotifyServiceDied(const wptr<IRemoteObject> &remote);
260 
261     /**
262      * @brief Connect to AAMS. For UI test.
263      * @return Return RET_OK if the command of connection is sent successfully,
264      *         otherwise refer to the RetError for the failure.
265      */
266     RetError Connect();
267 
268     /**
269      * @brief disconnect to AAMS. For UI test.
270      * @return Return RET_OK if the command of disconnect is sent successfully,
271      *         otherwise refer to the RetError for the failure.
272      */
273     RetError Disconnect();
274 
275 private:
276     class AccessibleAbilityDeathRecipient final : public IRemoteObject::DeathRecipient {
277     public:
AccessibleAbilityDeathRecipient(AccessibleAbilityClientImpl & client)278         AccessibleAbilityDeathRecipient(AccessibleAbilityClientImpl &client) : client_(client) {}
279         ~AccessibleAbilityDeathRecipient() = default;
280         DISALLOW_COPY_AND_MOVE(AccessibleAbilityDeathRecipient);
281 
282         void OnRemoteDied(const wptr<IRemoteObject> &remote);
283     private:
284         AccessibleAbilityClientImpl &client_;
285     };
286 
287     class AccessibilityServiceDeathRecipient final : public IRemoteObject::DeathRecipient {
288     public:
AccessibilityServiceDeathRecipient(AccessibleAbilityClientImpl & client)289         AccessibilityServiceDeathRecipient(AccessibleAbilityClientImpl &client) : client_(client) {}
290         ~AccessibilityServiceDeathRecipient() = default;
291         DISALLOW_COPY_AND_MOVE(AccessibilityServiceDeathRecipient);
292 
293         void OnRemoteDied(const wptr<IRemoteObject> &remote);
294     private:
295         AccessibleAbilityClientImpl &client_;
296     };
297 
298     bool GetCacheElementInfo(const int32_t windowId,
299         const int32_t elementId, AccessibilityElementInfo &elementInfo) const;
300     void SetCacheElementInfo(const int32_t windowId,
301         const std::vector<OHOS::Accessibility::AccessibilityElementInfo> &elementInfos);
302     RetError SearchElementInfoFromAce(const int32_t windowId, const int32_t elementId,
303         const uint32_t mode, AccessibilityElementInfo &info);
304     bool InitAccessibilityServiceProxy();
305     static void OnParameterChanged(const char *key, const char *value, void *context);
306 
307     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
308     sptr<IRemoteObject::DeathRecipient> accessibilityServiceDeathRecipient_ = nullptr;
309     sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr;
310     std::shared_ptr<AccessibleAbilityListener> listener_ = nullptr;
311     std::shared_ptr<AccessibleAbilityChannelClient> channelClient_ = nullptr;
312     uint32_t cacheMode_ = 0;
313     int32_t cacheWindowId_ = -1;
314     std::map<int32_t, AccessibilityElementInfo> cacheElementInfos_;
315     std::mutex mutex_;
316     std::atomic<bool> isConnected_ = false;
317 };
318 } // namespace Accessibility
319 } // namespace OHOS
320 #endif // ACCESSIBLE_ABILITY_CLIENT_IMPL_H