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 #include "accessibility_element_operator_proxy.h"
17 #include "hilog_wrapper.h"
18
19 namespace OHOS {
20 namespace Accessibility {
AccessibilityElementOperatorProxy(const sptr<IRemoteObject> & impl)21 AccessibilityElementOperatorProxy::AccessibilityElementOperatorProxy(
22 const sptr<IRemoteObject> &impl) : IRemoteProxy<IAccessibilityElementOperator>(impl)
23 {}
24
WriteInterfaceToken(MessageParcel & data)25 bool AccessibilityElementOperatorProxy::WriteInterfaceToken(MessageParcel &data)
26 {
27 HILOG_DEBUG();
28
29 if (!data.WriteInterfaceToken(AccessibilityElementOperatorProxy::GetDescriptor())) {
30 HILOG_ERROR("write interface token failed");
31 return false;
32 }
33 return true;
34 }
35
SendTransactCmd(AccessibilityInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 bool AccessibilityElementOperatorProxy::SendTransactCmd(AccessibilityInterfaceCode code,
37 MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39 HILOG_DEBUG();
40 sptr<IRemoteObject> remoteObj = Remote();
41 if (!remoteObj) {
42 HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
43 return false;
44 }
45 int32_t result = remoteObj->SendRequest(static_cast<uint32_t>(code), data, reply, option);
46 if (result != NO_ERROR) {
47 HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
48 return false;
49 }
50 return true;
51 }
52
SearchElementInfoByAccessibilityId(const int64_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode)53 void AccessibilityElementOperatorProxy::SearchElementInfoByAccessibilityId(const int64_t elementId,
54 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode)
55 {
56 HILOG_DEBUG();
57 MessageParcel data;
58 MessageParcel reply;
59 MessageOption option(MessageOption::TF_ASYNC);
60
61 if (!WriteInterfaceToken(data)) {
62 HILOG_ERROR("connection write token failed");
63 return;
64 }
65
66 if (!data.WriteInt64(elementId)) {
67 HILOG_ERROR("connection write parcelable element id failed");
68 return;
69 }
70
71 if (!data.WriteInt32(requestId)) {
72 HILOG_ERROR("connection write parcelable request id failed");
73 return;
74 }
75
76 if (!callback) {
77 HILOG_ERROR("callback is nullptr");
78 return;
79 }
80 if (!data.WriteRemoteObject(callback->AsObject())) {
81 HILOG_ERROR("connection write parcelable callback failed");
82 return;
83 }
84
85 if (!data.WriteInt32(mode)) {
86 HILOG_ERROR("connection write parcelable mode failed");
87 return;
88 }
89
90 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_ACCESSIBILITY_ID,
91 data, reply, option)) {
92 HILOG_ERROR("search element info by accessibility id failed");
93 return;
94 }
95 }
96
SearchElementInfosByText(const int64_t elementId,const std::string & text,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)97 void AccessibilityElementOperatorProxy::SearchElementInfosByText(const int64_t elementId,
98 const std::string &text,
99 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
100 {
101 HILOG_DEBUG();
102 MessageParcel data;
103 MessageParcel reply;
104 MessageOption option(MessageOption::TF_ASYNC);
105
106 if (!WriteInterfaceToken(data)) {
107 HILOG_ERROR("connection write token failed");
108 return;
109 }
110
111 if (!data.WriteInt64(elementId)) {
112 HILOG_ERROR("connection write parcelable elementId failed");
113 return;
114 }
115 if (!data.WriteString(text)) {
116 HILOG_ERROR("connection write text failed");
117 return;
118 }
119
120 if (!data.WriteInt32(requestId)) {
121 HILOG_ERROR("connection write request id failed");
122 return;
123 }
124
125 if (!callback) {
126 HILOG_ERROR("callback is nullptr");
127 return;
128 }
129 if (!data.WriteRemoteObject(callback->AsObject())) {
130 HILOG_ERROR("connection write callback failed");
131 return;
132 }
133
134 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_TEXT, data, reply, option)) {
135 HILOG_ERROR("search element infos by text failed");
136 return;
137 }
138 }
139
FindFocusedElementInfo(const int64_t elementId,const int32_t focusType,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)140 void AccessibilityElementOperatorProxy::FindFocusedElementInfo(const int64_t elementId,
141 const int32_t focusType, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
142 {
143 HILOG_DEBUG();
144 MessageParcel data;
145 MessageParcel reply;
146 MessageOption option(MessageOption::TF_ASYNC);
147
148 if (!WriteInterfaceToken(data)) {
149 HILOG_ERROR("connection write token failed");
150 return;
151 }
152
153 if (!data.WriteInt64(elementId)) {
154 HILOG_ERROR("connection write elementId failed");
155 return;
156 }
157
158 if (!data.WriteInt32(focusType)) {
159 HILOG_ERROR("connection write focusType failed");
160 return;
161 }
162
163 if (!data.WriteInt32(requestId)) {
164 HILOG_ERROR("connection write requestId failed");
165 return;
166 }
167
168 if (!callback) {
169 HILOG_ERROR("callback is nullptr");
170 return;
171 }
172 if (!data.WriteRemoteObject(callback->AsObject())) {
173 HILOG_ERROR("connection write callback failed");
174 return;
175 }
176
177 if (!SendTransactCmd(AccessibilityInterfaceCode::FIND_FOCUSED_INFO, data, reply, option)) {
178 HILOG_ERROR("find focused element info failed");
179 return;
180 }
181 }
182
FocusMoveSearch(const int64_t elementId,const int32_t direction,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)183 void AccessibilityElementOperatorProxy::FocusMoveSearch(const int64_t elementId,
184 const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
185 {
186 HILOG_DEBUG();
187 MessageParcel data;
188 MessageParcel reply;
189 MessageOption option(MessageOption::TF_ASYNC);
190
191 if (!WriteInterfaceToken(data)) {
192 HILOG_ERROR("fail, connection write Token");
193 return;
194 }
195
196 if (!data.WriteInt64(elementId)) {
197 HILOG_ERROR("fail, connection write elementId error");
198 return;
199 }
200
201 if (!data.WriteInt32(direction)) {
202 HILOG_ERROR("fail, connection write focusType error");
203 return;
204 }
205
206 if (!data.WriteInt32(requestId)) {
207 HILOG_ERROR("fail, connection write requestId error");
208 return;
209 }
210
211 if (!callback) {
212 HILOG_ERROR("callback is nullptr.");
213 return;
214 }
215 if (!data.WriteRemoteObject(callback->AsObject())) {
216 HILOG_ERROR("fail, connection write callback error");
217 return;
218 }
219
220 if (!SendTransactCmd(AccessibilityInterfaceCode::FOCUS_FIND, data, reply, option)) {
221 HILOG_ERROR("focus move search failed");
222 return;
223 }
224 }
225
ExecuteAction(const int64_t elementId,const int32_t action,const std::map<std::string,std::string> & arguments,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)226 void AccessibilityElementOperatorProxy::ExecuteAction(const int64_t elementId, const int32_t action,
227 const std::map<std::string, std::string> &arguments, const int32_t requestId,
228 const sptr<IAccessibilityElementOperatorCallback> &callback)
229 {
230 HILOG_DEBUG();
231 MessageParcel data;
232 MessageParcel reply;
233 MessageOption option(MessageOption::TF_ASYNC);
234
235 if (!WriteInterfaceToken(data)) {
236 HILOG_ERROR("connection write token failed");
237 return;
238 }
239
240 if (!data.WriteInt64(elementId)) {
241 HILOG_ERROR("connection write element id failed");
242 return;
243 }
244
245 if (!data.WriteInt32(action)) {
246 HILOG_ERROR("connection write focus type failed");
247 return;
248 }
249
250 auto iter = arguments.begin();
251 std::vector<std::string> keys;
252 std::vector<std::string> values;
253 while (iter != arguments.end()) {
254 keys.push_back(iter->first);
255 values.push_back(iter->second);
256 iter++;
257 }
258
259 if (!data.WriteStringVector(keys)) {
260 HILOG_ERROR("connection write argument keys failed");
261 return;
262 }
263
264 if (!data.WriteStringVector(values)) {
265 HILOG_ERROR("connection write argument values failed");
266 return;
267 }
268
269 if (!data.WriteInt32(requestId)) {
270 HILOG_ERROR("connection write request id failed");
271 return;
272 }
273
274 if (!callback) {
275 HILOG_ERROR("callback is nullptr");
276 return;
277 }
278 if (!data.WriteRemoteObject(callback->AsObject())) {
279 HILOG_ERROR("connection write callback failed");
280 return;
281 }
282
283 if (!SendTransactCmd(AccessibilityInterfaceCode::PERFORM_ACTION_ELEMENT, data, reply, option)) {
284 HILOG_ERROR("execute action failed");
285 return;
286 }
287 }
288
ClearFocus()289 void AccessibilityElementOperatorProxy::ClearFocus()
290 {
291 HILOG_DEBUG();
292 MessageParcel data;
293 MessageParcel reply;
294 MessageOption option(MessageOption::TF_ASYNC);
295 if (!WriteInterfaceToken(data)) {
296 HILOG_ERROR("connection write token failed");
297 return;
298 }
299
300 if (!SendTransactCmd(AccessibilityInterfaceCode::CLEAR_FOCUS, data, reply, option)) {
301 HILOG_ERROR("clear focus failed");
302 return;
303 }
304 }
305
OutsideTouch()306 void AccessibilityElementOperatorProxy::OutsideTouch()
307 {
308 HILOG_DEBUG();
309 MessageParcel data;
310 MessageParcel reply;
311 MessageOption option(MessageOption::TF_ASYNC);
312 if (!WriteInterfaceToken(data)) {
313 HILOG_ERROR("connection write token failed");
314 return;
315 }
316
317 if (!SendTransactCmd(AccessibilityInterfaceCode::OUTSIDE_TOUCH, data, reply, option)) {
318 HILOG_ERROR("outside touch failed");
319 return;
320 }
321 }
322 } // namespace Accessibility
323 } // namespace OHOS