• 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 #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) override;
61 
62     /**
63      * @brief Make the child node information by accessibility ID and filtered by text and set the result by callback.
64      * @param elementId: The unique id of the component ID.
65      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
66      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
67      * @param text  Filter for the child components to matched with the text
68      */
69     virtual void SearchElementInfosByText(const int64_t elementId, const std::string &text,
70         const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
71 
72     /**
73      * @brief Make the node information of the component focused by the focus type specified.
74      * @param elementId: The unique id of the component ID.
75      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
76      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
77      * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
78      *                  FOCUS_TYPE_INPUT: text input focus
79      */
80     virtual void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
81         const sptr<IAccessibilityElementOperatorCallback> &callback) override;
82 
83     /**
84      * @brief Make the node info by current focus move direction.
85      * @param elementId: The unique id of the component ID.
86      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
87      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
88      * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
89      */
90     virtual void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
91         const sptr<IAccessibilityElementOperatorCallback> &callback) override;
92 
93     /**
94      * @brief To return the result of perform action.
95      * @param elementId: The unique id of the component ID.
96      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
97      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
98      * @param action Refer to [AccessibilityElementInfo.ActionType]
99      * @param actionArguments The parameter for action type. such as:
100      *      action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
101      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
102      *      action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
103      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
104      *      action: ACCESSIBILITY_ACTION_NEXT_TEXT,
105      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
106      *      action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
107      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
108      *      action: ACCESSIBILITY_ACTION_SET_SELECTION,
109      *                  actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
110      *                                  {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
111      *      action: ACCESSIBILITY_ACTION_SET_TEXT,
112      *                  actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
113      */
114     virtual void ExecuteAction(const int64_t elementId, const int32_t action,
115         const std::map<std::string, std::string> &actionArguments,
116         int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
117 
118     /**
119      * @brief The function is called while accessibility System check the id of window is not equal
120      * to the id of active window when sendAccessibility.
121      */
122 
123     virtual void ClearFocus() override;
124 
125     /**
126      * @brief the low layer is notified by the function called while accessibility system execute
127      * the function of executeAction from AS to check the all low windows cared the outside event.
128      * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
129      */
130     virtual void OutsideTouch() override;
131 
132     /**
133      * @brief Get the window id related with operator object
134      * @return The unique id of the window related with operator object.
135      */
136     int32_t GetWindowId();
137 
138     /**
139      * @brief Set the element information by accessibility id to AA.
140      * @param infos The element info searched by accessibility id.
141      * @param requestId The request id from AA, it is used to match with request and response.
142      */
143     void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
144         const int32_t requestId);
145 
146     /**
147      * @brief Set the element information matched with text to AA.
148      * @param infos The element information searched matched with text.
149      * @param requestId The request id from AA, it is used to match with request and response.
150      */
151     void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
152         const int32_t requestId);
153 
154     /**
155      * @brief Set the element information matched with focus type to AA.
156      * @param info The element information searched matched with focus type.
157      * @param requestId The request id from AA, it is used to match with request and response.
158      */
159     void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
160 
161     /**
162      * @brief Set the element information by focus direction to AA.
163      * @param info The element information searched by focus direction.
164      * @param requestId The request id from AA, it is used to match with request and response.
165      */
166     void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
167 
168     /**
169      * @brief Set the result of action executed to AA.
170      * @param succeeded True: The action is executed successfully; otherwise is false.
171      * @param requestId The request id from AA, it is used to match with request and response.
172      */
173     void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
174 
175 private:
176     int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
177 
178     std::mutex mutex_;
179     int32_t windowId_ = 0;
180     AccessibilityElementOperatorCallback &operatorCallback_;
181     std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
182     std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
183     DISALLOW_COPY_AND_MOVE(AccessibilityElementOperatorImpl);
184 };
185 
186 template<class T>
TranslateListToVector(const std::list<T> & originList)187 std::vector<T> TranslateListToVector(const std::list<T> &originList)
188 {
189     std::size_t len = originList.size();
190     std::vector<T> destVector(len);
191     std::copy(originList.begin(), originList.end(), destVector.begin());
192     return destVector;
193 }
194 
195 template<class T>
TranslateVectorToList(const std::vector<T> & originVector)196 std::list<T> TranslateVectorToList(const std::vector<T> &originVector)
197 {
198     size_t len = originVector.length();
199     std::list<T> destList(len);
200     std::copy(originVector.begin(), originVector.end(), destList.begin());
201     return destList;
202 }
203 } // namespace Accessibility
204 } // namespace OHOS
205 #endif // ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H