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 #ifndef ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
17 #define ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
18
19 #include <memory>
20 #include <unordered_map>
21 #include "accessibility_element_operator_callback.h"
22 #include "accessibility_element_operator_stub.h"
23 #include "accessibility_element_operator.h"
24 #include "nocopyable.h"
25
26 namespace OHOS {
27 namespace Accessibility {
28 /*
29 * The class define the interface for UI to implement.
30 * It is triggered by ABMS when AA to request the accessibility information.
31 */
32 class AccessibilityElementOperatorImpl : public AccessibilityElementOperatorStub {
33 public:
34 /**
35 * @brief construct function
36 * @param windowId The window id.
37 * @param operation The object implemented by ACE.
38 * @param callback The callback which is to transfer the result from ACE.
39 */
40 explicit AccessibilityElementOperatorImpl(int32_t windowId,
41 const std::shared_ptr<AccessibilityElementOperator> &operation,
42 AccessibilityElementOperatorCallback &callback);
43
44 /**
45 * @brief destruct function
46 */
47 ~AccessibilityElementOperatorImpl();
48
49 /**
50 * @brief Make the node information by accessibility ID and set the result by callback.
51 * @param elementId: The unique id of the component ID.
52 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
53 * @param callback To transfer the node info to ASAC and it defined by ASAC.
54 * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also.
55 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also.
56 * PREFETCH_CHILDREN: Need to make the child node info also.
57 * otherwise: Make the node information by elementId only.
58 */
59 virtual void SearchElementInfoByAccessibilityId(const int64_t elementId, const int32_t requestId,
60 const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
61 bool isFilter = false) override;
62
63 /**
64 * @brief Make the child node information by accessibility ID and filtered by text and set the result by callback.
65 * @param elementId: The unique id of the component ID.
66 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
67 * @param callback To transfer the node info to ASAC and it defined by ASAC.
68 * @param text Filter for the child components to matched with the text
69 */
70 virtual void SearchElementInfosByText(const int64_t elementId, const std::string &text,
71 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
72
73 /**
74 * @brief Make the node information of the component focused by the focus type specified.
75 * @param elementId: The unique id of the component ID.
76 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
77 * @param callback To transfer the node info to ASAC and it defined by ASAC.
78 * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
79 * FOCUS_TYPE_INPUT: text input focus
80 */
81 virtual void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
82 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
83
84 /**
85 * @brief Make the node info by current focus move direction.
86 * @param elementId: The unique id of the component ID.
87 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
88 * @param callback To transfer the node info to ASAC and it defined by ASAC.
89 * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
90 */
91 virtual void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
92 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
93
94 /**
95 * @brief To return the result of perform action.
96 * @param elementId: The unique id of the component ID.
97 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
98 * @param callback To transfer the node info to ASAC and it defined by ASAC.
99 * @param action Refer to [AccessibilityElementInfo.ActionType]
100 * @param actionArguments The parameter for action type. such as:
101 * action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
102 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
103 * action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
104 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
105 * action: ACCESSIBILITY_ACTION_NEXT_TEXT,
106 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
107 * action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
108 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
109 * action: ACCESSIBILITY_ACTION_SET_SELECTION,
110 * actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
111 * {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
112 * action: ACCESSIBILITY_ACTION_SET_TEXT,
113 * actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
114 */
115 virtual void ExecuteAction(const int64_t elementId, const int32_t action,
116 const std::map<std::string, std::string> &actionArguments,
117 int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
118
119 /**
120 * @brief To return the result of cursor position.
121 * @param elementId: The unique id of the component ID.
122 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
123 * @param callback To transfer the node info to ASAC and it defined by ASAC.
124 */
125 virtual void GetCursorPosition(const int64_t elementId, const int32_t requestId,
126 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
127
128 /**
129 * @brief The function is called while accessibility System check the id of window is not equal
130 * to the id of active window when sendAccessibility.
131 */
132
133 virtual void ClearFocus() override;
134
135 /**
136 * @brief the low layer is notified by the function called while accessibility system execute
137 * the function of executeAction from AS to check the all low windows cared the outside event.
138 * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
139 */
140 virtual void OutsideTouch() override;
141
142 /**
143 * @brief Get the window id related with operator object
144 * @return The unique id of the window related with operator object.
145 */
146 int32_t GetWindowId();
147
148 /**
149 * @brief Set the elementId, childWindowId, childtreeId to AA
150 * @param elementId The element ID.
151 * @param childTreeId The childTree ID.
152 * @param childWindowId The childwindow ID.
153 */
154 virtual void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t childTreeId,
155 const int32_t childWindowId) override;
156
157 /**
158 * @brief Set the belongTree id to AA
159 * @param treeId The tree ID.
160 */
161 virtual void SetBelongTreeId(const int32_t treeId) override;
162
163 /**
164 * @brief Set the parent window id to AA
165 * @param parentWindowId The parentWindowId ID.
166 */
167 virtual void SetParentWindowId(const int32_t parentWindowId) override;
168
169 /**
170 * @brief Set the element information by accessibility id to AA.
171 * @param infos The element info searched by accessibility id.
172 * @param requestId The request id from AA, it is used to match with request and response.
173 */
174 void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
175 const int32_t requestId);
176
177 /**
178 * @brief Set whether to perform filtering.
179 */
180 static void SetFiltering(std::vector<AccessibilityElementInfo> &filterInfos);
181
182 /**
183 * @brief Set the element information matched with text to AA.
184 * @param infos The element information searched matched with text.
185 * @param requestId The request id from AA, it is used to match with request and response.
186 */
187 void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
188 const int32_t requestId);
189
190 /**
191 * @brief Set the element information matched with focus type to AA.
192 * @param info The element information searched matched with focus type.
193 * @param requestId The request id from AA, it is used to match with request and response.
194 */
195 void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
196
197 /**
198 * @brief Set the element information by focus direction to AA.
199 * @param info The element information searched by focus direction.
200 * @param requestId The request id from AA, it is used to match with request and response.
201 */
202 void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
203
204 /**
205 * @brief Set the result of action executed to AA.
206 * @param succeeded True: The action is executed successfully; otherwise is false.
207 * @param requestId The request id from AA, it is used to match with request and response.
208 */
209 void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
210
211 /**
212 * @brief Set the result of cursor position to AA.
213 * @param cursorPosition The cursorPosition to be returned.
214 * @param requestId The request id from AA, it is used to match with request and response.
215 */
216 void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId);
217
218 static sptr<IAccessibilityElementOperatorCallback> GetCallbackByRequestId(const int32_t requestId);
219 static void EraseCallback(const int32_t requestId);
220
221 private:
222 int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
223
224 static std::mutex requestsMutex_;
225 int32_t windowId_ = 0;
226 AccessibilityElementOperatorCallback &operatorCallback_;
227 std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
228 static std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
229 DISALLOW_COPY_AND_MOVE(AccessibilityElementOperatorImpl);
230 };
231
232 template<class T>
TranslateListToVector(const std::list<T> & originList)233 std::vector<T> TranslateListToVector(const std::list<T> &originList)
234 {
235 std::size_t len = originList.size();
236 std::vector<T> destVector(len);
237 std::copy(originList.begin(), originList.end(), destVector.begin());
238 return destVector;
239 }
240
241 template<class T>
TranslateVectorToList(const std::vector<T> & originVector)242 std::list<T> TranslateVectorToList(const std::vector<T> &originVector)
243 {
244 size_t len = originVector.length();
245 std::list<T> destList(len);
246 std::copy(originVector.begin(), originVector.end(), destList.begin());
247 return destList;
248 }
249 } // namespace Accessibility
250 } // namespace OHOS
251 #endif // ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H