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 == nullptr) {
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,bool isFilter)53 void AccessibilityElementOperatorProxy::SearchElementInfoByAccessibilityId(const int64_t elementId,
54 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
55 bool isFilter)
56 {
57 HILOG_DEBUG();
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option(MessageOption::TF_SYNC);
61
62 if (!WriteInterfaceToken(data)) {
63 HILOG_ERROR("connection write token failed");
64 return;
65 }
66
67 if (!data.WriteInt64(elementId)) {
68 HILOG_ERROR("connection write parcelable element id failed");
69 return;
70 }
71
72 if (!data.WriteInt32(requestId)) {
73 HILOG_ERROR("connection write parcelable request id failed");
74 return;
75 }
76
77 if (callback == nullptr) {
78 HILOG_ERROR("callback is nullptr");
79 return;
80 }
81 if (!data.WriteRemoteObject(callback->AsObject())) {
82 HILOG_ERROR("connection write parcelable callback failed");
83 return;
84 }
85
86 if (!data.WriteInt32(mode)) {
87 HILOG_ERROR("connection write parcelable mode failed");
88 return;
89 }
90
91 if (!data.WriteBool(isFilter)) {
92 HILOG_ERROR("connection write parcelable isFilter failed");
93 return;
94 }
95
96 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_ACCESSIBILITY_ID,
97 data, reply, option)) {
98 HILOG_ERROR("search element info by accessibility id failed");
99 return;
100 }
101 }
102
SearchDefaultFocusedByWindowId(const int32_t windowId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode,bool isFilter)103 void AccessibilityElementOperatorProxy::SearchDefaultFocusedByWindowId(const int32_t windowId,
104 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
105 bool isFilter)
106 {
107 HILOG_DEBUG();
108 MessageParcel data;
109 MessageParcel reply;
110 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
111
112 if (!WriteInterfaceToken(data)) {
113 HILOG_ERROR("connection write token failed");
114 return;
115 }
116
117 if (!data.WriteInt32(windowId)) {
118 HILOG_ERROR("connection write parcelable window id failed");
119 return;
120 }
121
122 if (!data.WriteInt32(requestId)) {
123 HILOG_ERROR("connection write parcelable request id failed");
124 return;
125 }
126
127 if (callback == nullptr) {
128 HILOG_ERROR("callback is nullptr");
129 return;
130 }
131 if (!data.WriteRemoteObject(callback->AsObject())) {
132 HILOG_ERROR("connection write parcelable callback failed");
133 return;
134 }
135
136 if (!data.WriteInt32(mode)) {
137 HILOG_ERROR("connection write parcelable mode failed");
138 return;
139 }
140
141 if (!data.WriteBool(isFilter)) {
142 HILOG_ERROR("connection write parcelable isFilter failed");
143 return;
144 }
145
146 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_WINDOW_ID,
147 data, reply, option)) {
148 HILOG_ERROR("search element info by accessibility id failed");
149 return;
150 }
151 }
152
SearchElementInfosByText(const int64_t elementId,const std::string & text,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)153 void AccessibilityElementOperatorProxy::SearchElementInfosByText(const int64_t elementId,
154 const std::string &text,
155 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
156 {
157 HILOG_DEBUG();
158 MessageParcel data;
159 MessageParcel reply;
160 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
161
162 if (!WriteInterfaceToken(data)) {
163 HILOG_ERROR("connection write token failed");
164 return;
165 }
166
167 if (!data.WriteInt64(elementId)) {
168 HILOG_ERROR("connection write parcelable elementId failed");
169 return;
170 }
171 if (!data.WriteString(text)) {
172 HILOG_ERROR("connection write text failed");
173 return;
174 }
175
176 if (!data.WriteInt32(requestId)) {
177 HILOG_ERROR("connection write request id failed");
178 return;
179 }
180
181 if (callback == nullptr) {
182 HILOG_ERROR("callback is nullptr");
183 return;
184 }
185 if (!data.WriteRemoteObject(callback->AsObject())) {
186 HILOG_ERROR("connection write callback failed");
187 return;
188 }
189
190 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_TEXT, data, reply, option)) {
191 HILOG_ERROR("search element infos by text failed");
192 return;
193 }
194 }
195
FindFocusedElementInfo(const int64_t elementId,const int32_t focusType,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)196 void AccessibilityElementOperatorProxy::FindFocusedElementInfo(const int64_t elementId,
197 const int32_t focusType, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
198 {
199 HILOG_DEBUG();
200 MessageParcel data;
201 MessageParcel reply;
202 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
203
204 if (!WriteInterfaceToken(data)) {
205 HILOG_ERROR("connection write token failed");
206 return;
207 }
208
209 if (!data.WriteInt64(elementId)) {
210 HILOG_ERROR("connection write elementId failed");
211 return;
212 }
213
214 if (!data.WriteInt32(focusType)) {
215 HILOG_ERROR("connection write focusType failed");
216 return;
217 }
218
219 if (!data.WriteInt32(requestId)) {
220 HILOG_ERROR("connection write requestId failed");
221 return;
222 }
223
224 if (callback == nullptr) {
225 HILOG_ERROR("callback is nullptr");
226 return;
227 }
228 if (!data.WriteRemoteObject(callback->AsObject())) {
229 HILOG_ERROR("connection write callback failed");
230 return;
231 }
232
233 if (!SendTransactCmd(AccessibilityInterfaceCode::FIND_FOCUSED_INFO, data, reply, option)) {
234 HILOG_ERROR("find focused element info failed");
235 return;
236 }
237 }
238
FocusMoveSearch(const int64_t elementId,const int32_t direction,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)239 void AccessibilityElementOperatorProxy::FocusMoveSearch(const int64_t elementId,
240 const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
241 {
242 HILOG_DEBUG();
243 MessageParcel data;
244 MessageParcel reply;
245 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
246
247 if (!WriteInterfaceToken(data)) {
248 HILOG_ERROR("fail, connection write Token");
249 return;
250 }
251
252 if (!data.WriteInt64(elementId)) {
253 HILOG_ERROR("fail, connection write elementId error");
254 return;
255 }
256
257 if (!data.WriteInt32(direction)) {
258 HILOG_ERROR("fail, connection write focusType error");
259 return;
260 }
261
262 if (!data.WriteInt32(requestId)) {
263 HILOG_ERROR("fail, connection write requestId error");
264 return;
265 }
266
267 if (callback == nullptr) {
268 HILOG_ERROR("callback is nullptr.");
269 return;
270 }
271 if (!data.WriteRemoteObject(callback->AsObject())) {
272 HILOG_ERROR("fail, connection write callback error");
273 return;
274 }
275
276 if (!SendTransactCmd(AccessibilityInterfaceCode::FOCUS_FIND, data, reply, option)) {
277 HILOG_ERROR("focus move search failed");
278 return;
279 }
280 }
281
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)282 void AccessibilityElementOperatorProxy::ExecuteAction(const int64_t elementId, const int32_t action,
283 const std::map<std::string, std::string> &arguments, const int32_t requestId,
284 const sptr<IAccessibilityElementOperatorCallback> &callback)
285 {
286 HILOG_DEBUG();
287 MessageParcel data;
288 MessageParcel reply;
289 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
290
291 if (!WriteInterfaceToken(data)) {
292 HILOG_ERROR("connection write token failed");
293 return;
294 }
295
296 if (!data.WriteInt64(elementId)) {
297 HILOG_ERROR("connection write element id failed");
298 return;
299 }
300
301 if (!data.WriteInt32(action)) {
302 HILOG_ERROR("connection write focus type failed");
303 return;
304 }
305
306 auto iter = arguments.begin();
307 std::vector<std::string> keys;
308 std::vector<std::string> values;
309 while (iter != arguments.end()) {
310 keys.push_back(iter->first);
311 values.push_back(iter->second);
312 iter++;
313 }
314
315 if (!data.WriteStringVector(keys)) {
316 HILOG_ERROR("connection write argument keys failed");
317 return;
318 }
319
320 if (!data.WriteStringVector(values)) {
321 HILOG_ERROR("connection write argument values failed");
322 return;
323 }
324
325 if (!data.WriteInt32(requestId)) {
326 HILOG_ERROR("connection write request id failed");
327 return;
328 }
329
330 if (callback == nullptr) {
331 HILOG_ERROR("callback is nullptr");
332 return;
333 }
334 if (!data.WriteRemoteObject(callback->AsObject())) {
335 HILOG_ERROR("connection write callback failed");
336 return;
337 }
338
339 if (!SendTransactCmd(AccessibilityInterfaceCode::PERFORM_ACTION_ELEMENT, data, reply, option)) {
340 HILOG_ERROR("execute action failed");
341 return;
342 }
343 }
344
345
GetCursorPosition(const int64_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)346 void AccessibilityElementOperatorProxy::GetCursorPosition(const int64_t elementId, const int32_t requestId,
347 const sptr<IAccessibilityElementOperatorCallback> &callback)
348 {
349 HILOG_DEBUG();
350 MessageParcel data;
351 MessageParcel reply;
352 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
353
354 if (!WriteInterfaceToken(data)) {
355 HILOG_ERROR("connection write token failed");
356 return;
357 }
358
359 if (!data.WriteInt64(elementId)) {
360 HILOG_ERROR("connection write elementId failed");
361 return;
362 }
363
364 if (!data.WriteInt32(requestId)) {
365 HILOG_ERROR("connection write requestId failed");
366 return;
367 }
368
369 if (callback == nullptr) {
370 HILOG_ERROR("callback is nullptr");
371 return;
372 }
373 if (!data.WriteRemoteObject(callback->AsObject())) {
374 HILOG_ERROR("connection write callback failed");
375 return;
376 }
377
378 if (!SendTransactCmd(AccessibilityInterfaceCode::CURSOR_POSITION, data, reply, option)) {
379 HILOG_ERROR("find cursor position info failed");
380 return;
381 }
382 }
383
ClearFocus()384 void AccessibilityElementOperatorProxy::ClearFocus()
385 {
386 HILOG_DEBUG();
387 MessageParcel data;
388 MessageParcel reply;
389 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
390 if (!WriteInterfaceToken(data)) {
391 HILOG_ERROR("connection write token failed");
392 return;
393 }
394
395 if (!SendTransactCmd(AccessibilityInterfaceCode::CLEAR_FOCUS, data, reply, option)) {
396 HILOG_ERROR("clear focus failed");
397 return;
398 }
399 }
400
OutsideTouch()401 void AccessibilityElementOperatorProxy::OutsideTouch()
402 {
403 HILOG_DEBUG();
404 MessageParcel data;
405 MessageParcel reply;
406 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
407 if (!WriteInterfaceToken(data)) {
408 HILOG_ERROR("connection write token failed");
409 return;
410 }
411
412 if (!SendTransactCmd(AccessibilityInterfaceCode::OUTSIDE_TOUCH, data, reply, option)) {
413 HILOG_ERROR("outside touch failed");
414 return;
415 }
416 }
417
SetChildTreeIdAndWinId(const int64_t elementId,const int32_t treeId,const int32_t childWindowId)418 void AccessibilityElementOperatorProxy::SetChildTreeIdAndWinId(const int64_t elementId, const int32_t treeId,
419 const int32_t childWindowId)
420 {
421 HILOG_DEBUG();
422 MessageParcel data;
423 MessageParcel reply;
424 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
425
426 if (!WriteInterfaceToken(data)) {
427 HILOG_ERROR("connection write token failed");
428 return;
429 }
430
431 if (!data.WriteInt64(elementId)) {
432 HILOG_ERROR("connection write elementId failed");
433 return;
434 }
435
436 if (!data.WriteInt32(treeId)) {
437 HILOG_ERROR("connection write treeId failed");
438 return;
439 }
440
441 if (!data.WriteInt32(childWindowId)) {
442 HILOG_ERROR("connection write childWindowId failed");
443 return;
444 }
445
446 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_CHILDTREEID, data, reply, option)) {
447 HILOG_ERROR("clear focus failed");
448 return;
449 }
450 }
451
SetBelongTreeId(const int32_t treeId)452 void AccessibilityElementOperatorProxy::SetBelongTreeId(const int32_t treeId)
453 {
454 HILOG_DEBUG();
455 MessageParcel data;
456 MessageParcel reply;
457 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
458 if (!WriteInterfaceToken(data)) {
459 HILOG_ERROR("connection write token failed");
460 return;
461 }
462
463 if (!data.WriteInt32(treeId)) {
464 HILOG_ERROR("connection write elementId failed");
465 return;
466 }
467
468 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_BELONGTREEID, data, reply, option)) {
469 HILOG_ERROR("clear focus failed");
470 return;
471 }
472 }
473
SetParentWindowId(const int32_t iParentWindowId)474 void AccessibilityElementOperatorProxy::SetParentWindowId(const int32_t iParentWindowId)
475 {
476 HILOG_DEBUG();
477 MessageParcel data;
478 MessageParcel reply;
479 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
480 if (!WriteInterfaceToken(data)) {
481 HILOG_ERROR("connection write token failed");
482 return;
483 }
484
485 if (!data.WriteInt32(iParentWindowId)) {
486 HILOG_ERROR("connection write iParentWindowId failed");
487 return;
488 }
489
490 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_PARENTWINDOWID, data, reply, option)) {
491 HILOG_ERROR("clear focus failed");
492 return;
493 }
494 }
495 } // namespace Accessibility
496 } // namespace OHOS