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 "accessible_ability_channel_proxy.h"
17 #include "accessibility_errorcode.h"
18 #include "parcel.h"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace Accessibility {
AccessibleAbilityChannelProxy(const sptr<IRemoteObject> & object)24 AccessibleAbilityChannelProxy::AccessibleAbilityChannelProxy(
25 const sptr<IRemoteObject> &object): IRemoteProxy<IAccessibleAbilityChannel>(object)
26 {
27 }
28
WriteInterfaceToken(MessageParcel & data)29 bool AccessibleAbilityChannelProxy::WriteInterfaceToken(MessageParcel &data)
30 {
31 HILOG_DEBUG("start.");
32
33 if (!data.WriteInterfaceToken(AccessibleAbilityChannelProxy::GetDescriptor())) {
34 HILOG_ERROR("write interface token failed");
35 return false;
36 }
37 return true;
38 }
39
SendTransactCmd(IAccessibleAbilityChannel::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 bool AccessibleAbilityChannelProxy::SendTransactCmd(IAccessibleAbilityChannel::Message code,
41 MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43 HILOG_DEBUG("start.");
44
45 sptr<IRemoteObject> remote = Remote();
46 if (!remote) {
47 HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
48 return false;
49 }
50 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
51 if (result != NO_ERROR) {
52 HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
53 return false;
54 }
55 return true;
56 }
57
SearchElementInfoByAccessibilityId(const int accessibilityWindowId,const long elementId,const int requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int mode)58 bool AccessibleAbilityChannelProxy::SearchElementInfoByAccessibilityId(const int accessibilityWindowId,
59 const long elementId, const int requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
60 const int mode)
61 {
62 HILOG_DEBUG("start.");
63
64 MessageParcel data;
65 MessageParcel reply;
66 MessageOption option(MessageOption::TF_SYNC);
67
68 if (!WriteInterfaceToken(data)) {
69 return false;
70 }
71 if (!data.WriteInt32(accessibilityWindowId)) {
72 HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
73 return false;
74 }
75 if (!data.WriteInt64(elementId)) {
76 HILOG_ERROR("elementId write error: %{public}ld, ", elementId);
77 return false;
78 }
79 if (!data.WriteInt32(requestId)) {
80 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
81 return false;
82 }
83 if (!data.WriteRemoteObject(callback->AsObject())) {
84 HILOG_ERROR("callback write error");
85 return false;
86 }
87 if (!data.WriteInt32(mode)) {
88 HILOG_ERROR("mode write error: %{public}d, ", mode);
89 return false;
90 }
91
92 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFO_BY_ACCESSIBILITYID,
93 data, reply, option)) {
94 HILOG_ERROR("fail to find elementInfo by elementId");
95 return false;
96 }
97 return reply.ReadBool();
98 }
99
SearchElementInfosByText(const int accessibilityWindowId,const long elementId,const std::string & text,const int requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)100 bool AccessibleAbilityChannelProxy::SearchElementInfosByText(const int accessibilityWindowId,
101 const long elementId, const std::string &text, const int requestId,
102 const sptr<IAccessibilityElementOperatorCallback> &callback)
103 {
104 HILOG_DEBUG("start.");
105
106 MessageParcel data;
107 MessageParcel reply;
108 MessageOption option(MessageOption::TF_SYNC);
109
110 if (!WriteInterfaceToken(data)) {
111 return false;
112 }
113 if (!data.WriteInt32(accessibilityWindowId)) {
114 HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
115 return false;
116 }
117 if (!data.WriteInt32(elementId)) {
118 HILOG_ERROR("elementId write error: %{public}ld, ", elementId);
119 return false;
120 }
121 if (!data.WriteString(text)) {
122 HILOG_ERROR("text write error: %{public}s, ", text.c_str());
123 return false;
124 }
125 if (!data.WriteInt32(requestId)) {
126 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
127 return false;
128 }
129 if (!data.WriteRemoteObject(callback->AsObject())) {
130 HILOG_ERROR("callback write error");
131 return false;
132 }
133
134 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFOS_BY_TEXT,
135 data, reply, option)) {
136 HILOG_ERROR("fail to find elementInfo by text");
137 return false;
138 }
139 return reply.ReadBool();
140 }
141
FindFocusedElementInfo(const int accessibilityWindowId,const long elementId,const int focusType,const int requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)142 bool AccessibleAbilityChannelProxy::FindFocusedElementInfo(const int accessibilityWindowId,
143 const long elementId, const int focusType, const int requestId,
144 const sptr<IAccessibilityElementOperatorCallback> &callback)
145 {
146 HILOG_DEBUG("start.");
147
148 MessageParcel data;
149 MessageParcel reply;
150 MessageOption option(MessageOption::TF_SYNC);
151
152 if (!WriteInterfaceToken(data)) {
153 return false;
154 }
155 if (!data.WriteInt32(accessibilityWindowId)) {
156 HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
157 return false;
158 }
159 if (!data.WriteInt32(elementId)) {
160 HILOG_ERROR("elementId write error: %{public}ld, ", elementId);
161 return false;
162 }
163 if (!data.WriteInt32(focusType)) {
164 HILOG_ERROR("focusType write error: %{public}d, ", focusType);
165 return false;
166 }
167 if (!data.WriteInt32(requestId)) {
168 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
169 return false;
170 }
171 if (!data.WriteRemoteObject(callback->AsObject())) {
172 HILOG_ERROR("callback write error");
173 return false;
174 }
175
176 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::FIND_FOCUSED_ELEMENTINFO, data, reply, option)) {
177 HILOG_ERROR("fail to gain focus");
178 return false;
179 }
180 return reply.ReadBool();
181 }
182
FocusMoveSearch(const int accessibilityWindowId,const long elementId,const int direction,const int requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)183 bool AccessibleAbilityChannelProxy::FocusMoveSearch(const int accessibilityWindowId, const long elementId,
184 const int direction, const int requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
185 {
186 HILOG_DEBUG("start.");
187
188 MessageParcel data;
189 MessageParcel reply;
190 MessageOption option(MessageOption::TF_SYNC);
191
192 if (!WriteInterfaceToken(data)) {
193 return false;
194 }
195 if (!data.WriteInt32(accessibilityWindowId)) {
196 HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
197 return false;
198 }
199 if (!data.WriteInt32(elementId)) {
200 HILOG_ERROR("elementId write error: %{public}ld, ", elementId);
201 return false;
202 }
203 if (!data.WriteInt32(direction)) {
204 HILOG_ERROR("direction write error: %{public}d, ", direction);
205 return false;
206 }
207 if (!data.WriteInt32(requestId)) {
208 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
209 return false;
210 }
211 if (!data.WriteRemoteObject(callback->AsObject())) {
212 HILOG_ERROR("callback write error");
213 return false;
214 }
215
216 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::FOCUS_MOVE_SEARCH, data, reply, option)) {
217 HILOG_ERROR("fail to search focus");
218 return false;
219 }
220 return reply.ReadBool();
221 }
222
ExecuteAction(const int accessibilityWindowId,const long elementId,const int action,std::map<std::string,std::string> & actionArguments,const int requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)223 bool AccessibleAbilityChannelProxy::ExecuteAction(const int accessibilityWindowId, const long elementId,
224 const int action, std::map<std::string, std::string> &actionArguments, const int requestId,
225 const sptr<IAccessibilityElementOperatorCallback> &callback)
226 {
227 HILOG_DEBUG("start.");
228
229 MessageParcel data;
230 MessageParcel reply;
231 MessageOption option(MessageOption::TF_SYNC);
232
233 if (!WriteInterfaceToken(data)) {
234 return false;
235 }
236 if (!data.WriteInt32(accessibilityWindowId)) {
237 HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
238 return false;
239 }
240 if (!data.WriteInt32(elementId)) {
241 HILOG_ERROR("elementId write error: %{public}ld, ", elementId);
242 return false;
243 }
244 if (!data.WriteInt32(action)) {
245 HILOG_ERROR("action write error: %{public}d, ", action);
246 return false;
247 }
248
249 vector<string> actionArgumentsKey {};
250 vector<string> actionArgumentsValue {};
251 for (auto iter = actionArguments.begin(); iter != actionArguments.end(); iter++) {
252 actionArgumentsKey.push_back(iter->first);
253 actionArgumentsValue.push_back(iter->second);
254 }
255 if (!data.WriteStringVector(actionArgumentsKey)) {
256 HILOG_ERROR("actionArgumentsKey write error");
257 return false;
258 }
259 if (!data.WriteStringVector(actionArgumentsValue)) {
260 HILOG_ERROR("actionArgumentsValue write error");
261 return false;
262 }
263
264 if (!data.WriteInt32(requestId)) {
265 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
266 return false;
267 }
268 if (!data.WriteRemoteObject(callback->AsObject())) {
269 HILOG_ERROR("callback write error");
270 return false;
271 }
272
273 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::PERFORM_ACTION,
274 data, reply, option)) {
275 HILOG_ERROR("fail to perform accessibility action");
276 return false;
277 }
278 return reply.ReadBool();
279 }
280
GetWindows()281 vector<AccessibilityWindowInfo> AccessibleAbilityChannelProxy::GetWindows()
282 {
283 HILOG_DEBUG("start.");
284
285 MessageParcel data;
286 MessageParcel reply;
287 MessageOption option(MessageOption::TF_SYNC);
288 vector<AccessibilityWindowInfo> windowsError;
289 vector<AccessibilityWindowInfo> windows;
290
291 if (!WriteInterfaceToken(data)) {
292 return windowsError;
293 }
294 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_WINDOWS, data, reply, option)) {
295 HILOG_ERROR("fail to get windows");
296 return windowsError;
297 }
298
299 int32_t windowsSize = reply.ReadInt32();
300 for (int32_t i = 0; i < windowsSize; i++) {
301 unique_ptr<AccessibilityWindowInfo> window(reply.ReadParcelable<AccessibilityWindowInfo>());
302 if (!window) {
303 HILOG_ERROR("ReadParcelable<AccessibilityWindowInfo> failed");
304 return windowsError;
305 }
306 windows.emplace_back(*window);
307 }
308
309 return windows;
310 }
311
ExecuteCommonAction(const int action)312 bool AccessibleAbilityChannelProxy::ExecuteCommonAction(const int action)
313 {
314 HILOG_DEBUG("start.");
315
316 MessageParcel data;
317 MessageParcel reply;
318 MessageOption option(MessageOption::TF_SYNC);
319
320 if (!WriteInterfaceToken(data)) {
321 return false;
322 }
323 if (!data.WriteInt32(action)) {
324 HILOG_ERROR("action write error: %{public}d, ", action);
325 return false;
326 }
327
328 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::EXECUTE_COMMON_ACTION, data, reply, option)) {
329 HILOG_ERROR("fail to perform common action");
330 return false;
331 }
332 return reply.ReadBool();
333 }
334
SetOnKeyPressEventResult(const bool handled,const int sequence)335 void AccessibleAbilityChannelProxy::SetOnKeyPressEventResult(const bool handled, const int sequence)
336 {
337 HILOG_DEBUG("start.");
338
339 MessageParcel data;
340 MessageParcel reply;
341 MessageOption option(MessageOption::TF_SYNC);
342
343 if (!WriteInterfaceToken(data)) {
344 return;
345 }
346 if (!data.WriteBool(handled)) {
347 HILOG_ERROR("handled write error: %{public}d, ", handled);
348 return;
349 }
350 if (!data.WriteInt32(sequence)) {
351 HILOG_ERROR("sequence write error: %{public}d, ", sequence);
352 return;
353 }
354 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SET_ON_KEY_PRESS_EVENT_RESULT,
355 data, reply, option)) {
356 HILOG_ERROR("fail to set onKeyPressEvent result");
357 }
358 }
359
GetDisplayResizeScale(const int displayId)360 float AccessibleAbilityChannelProxy::GetDisplayResizeScale(const int displayId)
361 {
362 HILOG_DEBUG("start.");
363
364 MessageParcel data;
365 MessageParcel reply;
366 MessageOption option(MessageOption::TF_SYNC);
367
368 if (!WriteInterfaceToken(data)) {
369 return 0;
370 }
371 if (!data.WriteInt32(displayId)) {
372 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
373 return 0;
374 }
375
376 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_DISPALYRESIZE_SCALE, data, reply, option)) {
377 HILOG_ERROR("fail to get displayResize scale");
378 return 0;
379 }
380 return reply.ReadFloat();
381 }
382
GetDisplayResizeCenterX(const int displayId)383 float AccessibleAbilityChannelProxy::GetDisplayResizeCenterX(const int displayId)
384 {
385 HILOG_DEBUG("start.");
386
387 MessageParcel data;
388 MessageParcel reply;
389 MessageOption option(MessageOption::TF_SYNC);
390
391 if (!WriteInterfaceToken(data)) {
392 return 0;
393 }
394 if (!data.WriteInt32(displayId)) {
395 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
396 return 0;
397 }
398
399 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_DISPALYRESIZE_CETER_X, data, reply, option)) {
400 HILOG_ERROR("fail to get displayResize centerX");
401 return 0;
402 }
403 return reply.ReadFloat();
404 }
405
GetDisplayResizeCenterY(const int displayId)406 float AccessibleAbilityChannelProxy::GetDisplayResizeCenterY(const int displayId)
407 {
408 HILOG_DEBUG("start.");
409
410 MessageParcel data;
411 MessageParcel reply;
412 MessageOption option(MessageOption::TF_SYNC);
413
414 if (!WriteInterfaceToken(data)) {
415 return 0;
416 }
417 if (!data.WriteInt32(displayId)) {
418 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
419 return 0;
420 }
421
422 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_DISPLAYRESIZE_CETER_Y, data, reply, option)) {
423 HILOG_ERROR("fail to get displayResize centerY");
424 return 0;
425 }
426 return reply.ReadFloat();
427 }
428
GetDisplayResizeRect(const int displayId)429 Rect AccessibleAbilityChannelProxy::GetDisplayResizeRect(const int displayId)
430 {
431 HILOG_DEBUG("start.");
432
433 MessageParcel data;
434 MessageParcel reply;
435 MessageOption option(MessageOption::TF_SYNC);
436 Rect rect(0, 0, 0, 0);
437
438 if (!WriteInterfaceToken(data)) {
439 return rect;
440 }
441 if (!data.WriteInt32(displayId)) {
442 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
443 return rect;
444 }
445
446 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_DISPLAYRESIZE_RECT, data, reply, option)) {
447 HILOG_ERROR("fail to get displayResize rect");
448 return rect;
449 }
450
451 sptr<Rect> result = reply.ReadStrongParcelable<Rect>();
452 if (!result) {
453 HILOG_ERROR("ReadStrongParcelable<Rect> failed");
454 return rect;
455 }
456 return *result;
457 }
458
ResetDisplayResize(const int displayId,const bool animate)459 bool AccessibleAbilityChannelProxy::ResetDisplayResize(const int displayId, const bool animate)
460 {
461 HILOG_DEBUG("start.");
462
463 MessageParcel data;
464 MessageParcel reply;
465 MessageOption option(MessageOption::TF_SYNC);
466
467 if (!WriteInterfaceToken(data)) {
468 return false;
469 }
470 if (!data.WriteInt32(displayId)) {
471 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
472 return false;
473 }
474 if (!data.WriteBool(animate)) {
475 HILOG_ERROR("animate write error: %{public}d, ", animate);
476 return false;
477 }
478
479 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::RESET_DISPALYRESIZE, data, reply, option)) {
480 HILOG_ERROR("fail to reset displayResize");
481 return false;
482 }
483 return reply.ReadBool();
484 }
485
SetDisplayResizeScaleAndCenter(const int displayId,const float scale,const float centerX,const float centerY,const bool animate)486 bool AccessibleAbilityChannelProxy::SetDisplayResizeScaleAndCenter(const int displayId, const float scale,
487 const float centerX, const float centerY, const bool animate)
488 {
489 HILOG_DEBUG("start.");
490
491 MessageParcel data;
492 MessageParcel reply;
493 MessageOption option(MessageOption::TF_SYNC);
494
495 if (!WriteInterfaceToken(data)) {
496 return false;
497 }
498 if (!data.WriteInt32(displayId)) {
499 HILOG_ERROR("displayId write error: %{public}d, ", displayId);
500 return false;
501 }
502 if (!data.WriteFloat(scale)) {
503 HILOG_ERROR("scale write error: %{public}f, ", scale);
504 return false;
505 }
506 if (!data.WriteFloat(centerX)) {
507 HILOG_ERROR("centerX write error: %{public}f, ", centerX);
508 return false;
509 }
510 if (!data.WriteFloat(centerY)) {
511 HILOG_ERROR("centerY write error: %{public}f, ", centerY);
512 return false;
513 }
514 if (!data.WriteBool(animate)) {
515 HILOG_ERROR("animate write error: %{public}d, ", animate);
516 return false;
517 }
518
519 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SET_DISPLAYRESIZE_SCALE_AND_CENTER,
520 data, reply, option)) {
521 HILOG_ERROR("fail to set displayResize scale and center");
522 return false;
523 }
524 return reply.ReadBool();
525 }
526
SendSimulateGesture(const int requestId,const std::vector<GesturePathDefine> & gestureSteps)527 void AccessibleAbilityChannelProxy::SendSimulateGesture(const int requestId,
528 const std::vector<GesturePathDefine> &gestureSteps)
529 {
530 HILOG_DEBUG("start.");
531
532 MessageParcel data;
533 MessageParcel reply;
534 MessageOption option(MessageOption::TF_ASYNC);
535
536 if (!WriteInterfaceToken(data)) {
537 return;
538 }
539 if (!data.WriteInt32(requestId)) {
540 HILOG_ERROR("requestId write error: %{public}d, ", requestId);
541 return;
542 }
543 if (!data.WriteInt32(gestureSteps.size())) {
544 HILOG_ERROR("gestureSteps.size() write error: %{public}d, ", gestureSteps.size());
545 return;
546 }
547 for (auto &step : gestureSteps) {
548 if (!data.WriteParcelable(&step)) {
549 HILOG_ERROR("gestureSteps write error");
550 return;
551 }
552 }
553 if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEND_SIMULATE_GESTURE, data, reply, option)) {
554 HILOG_ERROR("fail to send simulation gesture");
555 }
556 }
557 } // namespace Accessibility
558 } // namespace OHOS