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