• 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 #include "accessible_ability_channel_client.h"
17 
18 #include <cinttypes>
19 #ifdef OHOS_BUILD_ENABLE_HITRACE
20 #include <hitrace_meter.h>
21 #endif // OHOS_BUILD_ENABLE_HITRACE
22 #include "accessibility_element_operator_callback_impl.h"
23 #include "hilog_wrapper.h"
24 
25 namespace OHOS {
26 namespace Accessibility {
27 namespace {
28     constexpr uint32_t TIME_OUT_OPERATOR = 5000;
29     constexpr int32_t REQUEST_ID_MAX = 0x0000FFFF;
30 } // namespace
31 
GenerateRequestId()32 int32_t AccessibleAbilityChannelClient::GenerateRequestId()
33 {
34     int32_t requestId = requestId_++;
35     requestId = requestId % REQUEST_ID_MAX;
36 
37     return requestId;
38 }
39 
GetRemote()40 sptr<IRemoteObject> AccessibleAbilityChannelClient::GetRemote()
41 {
42     return proxy_->AsObject();
43 }
44 
SetOnKeyPressEventResult(const bool handled,const int32_t sequence)45 void AccessibleAbilityChannelClient::SetOnKeyPressEventResult(const bool handled, const int32_t sequence)
46 {
47     HILOG_INFO("[channelId:%{public}d]", channelId_);
48     if (proxy_) {
49         proxy_->SetOnKeyPressEventResult(handled, sequence);
50     } else {
51         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
52     }
53 }
54 
FindFocusedElementInfo(int32_t accessibilityWindowId,int64_t elementId,int32_t focusType,AccessibilityElementInfo & elementInfo,bool systemApi)55 RetError AccessibleAbilityChannelClient::FindFocusedElementInfo(int32_t accessibilityWindowId,
56     int64_t elementId, int32_t focusType, AccessibilityElementInfo &elementInfo, bool systemApi)
57 {
58     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
59 #ifdef OHOS_BUILD_ENABLE_HITRACE
60     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "FindFocusedElement");
61 #endif // OHOS_BUILD_ENABLE_HITRACE
62     if (proxy_ == nullptr) {
63         HILOG_ERROR("FindFocusedElementInfo Failed to connect to aams [channelId:%{public}d]",
64             channelId_);
65         return RET_ERR_SAMGR;
66     }
67 
68     int32_t requestId = GenerateRequestId();
69     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
70         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
71     if (elementOperator == nullptr) {
72         HILOG_ERROR("FindFocusedElementInfo Failed to create elementOperator.");
73         return RET_ERR_NULLPTR;
74     }
75     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
76 
77     int32_t windowId = accessibilityWindowId;
78     if (accessibilityWindowId == ANY_WINDOW_ID && focusType == FOCUS_TYPE_ACCESSIBILITY &&
79         accessibilityFocusedWindowId_ != INVALID_WINDOW_ID) {
80         windowId = accessibilityFocusedWindowId_;
81         HILOG_INFO("Convert into accessibility focused window id[%{public}d]", windowId);
82     }
83 
84     RetError ret = proxy_->FindFocusedElementInfo(windowId,
85         elementId, focusType, requestId, elementOperator, systemApi);
86     if (ret != RET_OK) {
87         HILOG_ERROR("FindFocusedElementInfo failed. ret[%{public}d]", ret);
88         return ret;
89     }
90     HILOG_DEBUG("channelId:%{public}d, windowId:%{public}d, elementId:%{public}" PRId64 ", focusType:%{public}d",
91         channelId_, windowId, elementId, focusType);
92 
93     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
94     if (wait != ffrt::future_status::ready) {
95         HILOG_ERROR("FindFocusedElementInfo Failed to wait result");
96         return RET_ERR_TIME_OUT;
97     }
98 
99     if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() ==
100         AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
101         HILOG_ERROR("FindFocusedElementInfo The elementInfo from ace is wrong");
102         return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
103     }
104     HILOG_INFO("Get result successfully from ace.");
105 
106     elementInfo = elementOperator->accessibilityInfoResult_;
107     elementInfo.SetMainWindowId(windowId);
108     return RET_OK;
109 }
110 
SendSimulateGesture(const std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)111 RetError AccessibleAbilityChannelClient::SendSimulateGesture(
112     const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath)
113 {
114     HILOG_INFO("[channelId:%{public}d]", channelId_);
115     if (proxy_) {
116         return proxy_->SendSimulateGesture(gesturePath);
117     } else {
118         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
119         return RET_ERR_SAMGR;
120     }
121 }
122 
GetCursorPosition(int32_t accessibilityWindowId,int64_t elementId,int32_t & position)123 RetError AccessibleAbilityChannelClient::GetCursorPosition(
124     int32_t accessibilityWindowId, int64_t elementId, int32_t &position)
125 {
126 #ifdef OHOS_BUILD_ENABLE_HITRACE
127     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetCursorPosition");
128 #endif // OHOS_BUILD_ENABLE_HITRACE
129     if (proxy_ == nullptr) {
130         HILOG_ERROR("GetCursorPosition Failed to connect to aams [channelId:%{public}d]",
131             channelId_);
132         return RET_ERR_SAMGR;
133     }
134 
135     int32_t requestId = GenerateRequestId();
136     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
137         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
138     if (elementOperator == nullptr) {
139         HILOG_ERROR("GetCursorPosition Failed to create elementOperator.");
140         return RET_ERR_NULLPTR;
141     }
142     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
143 
144     RetError ret = proxy_->GetCursorPosition(accessibilityWindowId, elementId, requestId, elementOperator);
145     if (ret != RET_OK) {
146         HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret);
147         return ret;
148     }
149 
150     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
151     if (wait != ffrt::future_status::ready) {
152         HILOG_ERROR("GetCursorPosition Failed to wait result");
153         return RET_ERR_TIME_OUT;
154     }
155 
156     position = elementOperator->CursorPosition_;
157     HILOG_INFO("position%{public}d", position);
158     return RET_OK;
159 }
160 
ExecuteAction(int32_t accessibilityWindowId,int64_t elementId,int32_t action,const std::map<std::string,std::string> & actionArguments)161 RetError AccessibleAbilityChannelClient::ExecuteAction(int32_t accessibilityWindowId,
162     int64_t elementId, int32_t action, const std::map<std::string, std::string> &actionArguments)
163 {
164 #ifdef OHOS_BUILD_ENABLE_HITRACE
165     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "ExecuteAction");
166 #endif // OHOS_BUILD_ENABLE_HITRACE
167     if (proxy_ == nullptr) {
168         HILOG_ERROR("ExecuteAction Failed to connect to aams [channelId:%{public}d]", channelId_);
169         return RET_ERR_SAMGR;
170     }
171     if (action == ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS &&
172         accessibilityFocusedElementId_ != INVALID_WINDOW_ID && accessibilityFocusedWindowId_ != INVALID_WINDOW_ID) {
173         ExecuteAction(accessibilityFocusedWindowId_, accessibilityFocusedElementId_,
174             ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, actionArguments);
175     }
176 
177     int32_t requestId = GenerateRequestId();
178     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
179         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
180     if (elementOperator == nullptr) {
181         HILOG_ERROR("ExecuteAction Failed to create elementOperator.");
182         return RET_ERR_NULLPTR;
183     }
184     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
185 
186     RetError ret = proxy_->ExecuteAction(accessibilityWindowId,
187         elementId, action, actionArguments, requestId, elementOperator);
188     if (ret != RET_OK) {
189         HILOG_ERROR("ExecuteAction failed. action[%{public}d], ret[%{public}d]", action, ret);
190         return ret;
191     }
192 
193     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
194     if (wait != ffrt::future_status::ready) {
195         HILOG_ERROR("execute action: %{public}d failed to wait result", action);
196         return RET_ERR_TIME_OUT;
197     }
198     HILOG_INFO("action:[%{public}d], executeActionResult_[%{public}d], elementId:%{public}" PRId64 "",
199         action, elementOperator->executeActionResult_, elementId);
200 
201     if (elementOperator->executeActionResult_) {
202         switch (action) {
203             case ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS:
204                 accessibilityFocusedWindowId_ = accessibilityWindowId;
205                 accessibilityFocusedElementId_ = elementId;
206                 break;
207             case ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS:
208                 accessibilityFocusedWindowId_ = INVALID_WINDOW_ID;
209                 accessibilityFocusedElementId_ = INVALID_WINDOW_ID;
210                 break;
211             default:
212                 break;
213         }
214     }
215     return elementOperator->executeActionResult_ ? RET_OK : RET_ERR_PERFORM_ACTION_FAILED_BY_ACE;
216 }
217 
EnableScreenCurtain(bool isEnable)218 RetError AccessibleAbilityChannelClient::EnableScreenCurtain(bool isEnable)
219 {
220     HILOG_INFO("[channelId:%{public}d]", channelId_);
221     if (proxy_ == nullptr) {
222         HILOG_ERROR("EnableScreenCurtain Failed to connect to aams [channelId:%{public}d]", channelId_);
223         return RET_ERR_SAMGR;
224     }
225     return  proxy_->EnableScreenCurtain(isEnable) ? RET_OK : RET_ERR_PERFORM_ACTION_FAILED_BY_ACE;
226 }
227 
HoldRunningLock()228 RetError AccessibleAbilityChannelClient::HoldRunningLock()
229 {
230     HILOG_INFO("[channelId:%{public}d]", channelId_);
231     if (proxy_ == nullptr) {
232         HILOG_ERROR("HoldRunningLock Failed to connect to aams [channelId:%{public}d]", channelId_);
233         return RET_ERR_SAMGR;
234     }
235     return proxy_->HoldRunningLock();
236 }
237 
UnholdRunningLock()238 RetError AccessibleAbilityChannelClient::UnholdRunningLock()
239 {
240     HILOG_INFO("[channelId:%{public}d]", channelId_);
241     if (proxy_ == nullptr) {
242         HILOG_ERROR("UnholdRunningLock Failed to connect to aams [channelId:%{public}d]", channelId_);
243         return RET_ERR_SAMGR;
244     }
245     return proxy_->UnholdRunningLock();
246 }
247 
SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,int64_t elementId,int32_t mode,std::vector<AccessibilityElementInfo> & elementInfos,int32_t treeId,bool isFilter,bool systemApi)248 RetError AccessibleAbilityChannelClient::SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,
249     int64_t elementId, int32_t mode, std::vector<AccessibilityElementInfo> &elementInfos, int32_t treeId,
250     bool isFilter, bool systemApi)
251 {
252     int32_t requestId = GenerateRequestId();
253     HILOG_DEBUG("channelId:%{public}d, elementId:%{public}" PRId64 ", windowId:%{public}d, requestId:%{public}d",
254         channelId_, elementId, accessibilityWindowId, requestId);
255 #ifdef OHOS_BUILD_ENABLE_HITRACE
256     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementById");
257 #endif // OHOS_BUILD_ENABLE_HITRACE
258     if (proxy_ == nullptr) {
259         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to connect to aams [channelId:%{public}d]",
260             channelId_);
261         return RET_ERR_SAMGR;
262     }
263 
264     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
265         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
266     if (elementOperator == nullptr) {
267         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to create elementOperator.");
268         return RET_ERR_NULLPTR;
269     }
270     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
271     ElementBasicInfo elementBasicInfo {};
272     elementBasicInfo.windowId = accessibilityWindowId;
273     elementBasicInfo.treeId = treeId;
274     elementBasicInfo.elementId = elementId;
275 
276     RetError ret = proxy_->SearchElementInfoByAccessibilityId(elementBasicInfo, requestId,
277         elementOperator, mode, isFilter, systemApi);
278     if (ret != RET_OK) {
279         HILOG_ERROR("searchElement failed. ret: %{public}d. elementId: %{public}" PRId64 ", requestId :[%{public}d]",
280             ret, elementId, requestId);
281         return ret;
282     }
283 
284     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
285     if (wait != ffrt::future_status::ready) {
286         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to wait result");
287         return RET_ERR_TIME_OUT;
288     }
289 
290     for (auto &info : elementOperator->elementInfosResult_) {
291         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
292             HILOG_ERROR("SearchElementInfosByAccessibilityId The elementInfo from ace is wrong");
293             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
294         }
295     }
296     HILOG_DEBUG("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
297     elementInfos = elementOperator->elementInfosResult_;
298     if (!elementInfos.empty()) {
299         for (auto &element : elementInfos) {
300             element.SetMainWindowId(accessibilityWindowId);
301         }
302     }
303     return RET_OK;
304 }
305 
SearchDefaultFocusedByWindowId(int32_t accessibilityWindowId,int64_t elementId,int32_t mode,std::vector<AccessibilityElementInfo> & elementInfos,int32_t treeId,bool isFilter)306 RetError AccessibleAbilityChannelClient::SearchDefaultFocusedByWindowId(int32_t accessibilityWindowId,
307     int64_t elementId, int32_t mode, std::vector<AccessibilityElementInfo> &elementInfos, int32_t treeId, bool isFilter)
308 {
309     int32_t requestId = GenerateRequestId();
310     HILOG_DEBUG("channelId:%{public}d, windowId:%{public}d, requestId:%{public}d",
311         channelId_, accessibilityWindowId, requestId);
312     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchDefaultFocusedByWindowId");
313     if (proxy_ == nullptr) {
314         HILOG_ERROR("SearchDefaultFocusedByWindowId Failed to connect to aams [channelId:%{public}d]",
315             channelId_);
316         return RET_ERR_SAMGR;
317     }
318 
319     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
320         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
321     if (elementOperator == nullptr) {
322         HILOG_ERROR("SearchDefaultFocusedByWindowId Failed to create elementOperator.");
323         return RET_ERR_NULLPTR;
324     }
325     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
326     ElementBasicInfo elementBasicInfo {};
327     elementBasicInfo.windowId = accessibilityWindowId;
328     elementBasicInfo.elementId = elementId;
329     elementBasicInfo.treeId = treeId;
330 
331     RetError ret = proxy_->SearchDefaultFocusedByWindowId(elementBasicInfo, requestId,
332         elementOperator, mode, isFilter);
333     if (ret != RET_OK) {
334         HILOG_ERROR("searchElement failed. ret: %{public}d, requestId :[%{public}d]",
335             ret, requestId);
336         return ret;
337     }
338 
339     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
340     if (wait != ffrt::future_status::ready) {
341         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to wait result");
342         return RET_ERR_TIME_OUT;
343     }
344 
345     for (auto &info : elementOperator->elementInfosResult_) {
346         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
347             HILOG_ERROR("SearchElementInfosByAccessibilityId The elementInfo from ace is wrong");
348             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
349         }
350     }
351     elementInfos = elementOperator->elementInfosResult_;
352     HILOG_DEBUG("Get result successfully from arkUI elementInfos size[%{public}zu]", elementInfos.size());
353     return RET_OK;
354 }
355 
GetWindow(const int32_t windowId,AccessibilityWindowInfo & windowInfo)356 RetError AccessibleAbilityChannelClient::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
357 {
358     HILOG_DEBUG("[channelId:%{public}d] [windowId:%{public}d]", channelId_, windowId);
359 #ifdef OHOS_BUILD_ENABLE_HITRACE
360     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
361 #endif // OHOS_BUILD_ENABLE_HITRACE
362     if (proxy_ == nullptr) {
363         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
364         return RET_ERR_SAMGR;
365     }
366     return proxy_->GetWindow(windowId, windowInfo);
367 }
368 
GetWindows(std::vector<AccessibilityWindowInfo> & windows,bool systemApi)369 RetError AccessibleAbilityChannelClient::GetWindows(std::vector<AccessibilityWindowInfo> &windows, bool systemApi)
370 {
371     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
372 #ifdef OHOS_BUILD_ENABLE_HITRACE
373     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
374 #endif // OHOS_BUILD_ENABLE_HITRACE
375     if (proxy_) {
376         return proxy_->GetWindows(windows, systemApi);
377     } else {
378         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
379         return RET_ERR_SAMGR;
380     }
381 }
382 
GetWindows(const uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows,bool systemApi) const383 RetError AccessibleAbilityChannelClient::GetWindows(const uint64_t displayId,
384     std::vector<AccessibilityWindowInfo> &windows, bool systemApi) const
385 {
386     HILOG_DEBUG("[channelId:%{public}d] [displayId:%{public}" PRIu64 "]", channelId_, displayId);
387 #ifdef OHOS_BUILD_ENABLE_HITRACE
388     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetWindowsByDisplayId");
389 #endif // OHOS_BUILD_ENABLE_HITRACE
390     if (proxy_) {
391         return proxy_->GetWindowsByDisplayId(displayId, windows, systemApi);
392     } else {
393         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
394         return RET_ERR_SAMGR;
395     }
396 }
397 
SearchElementInfosByText(int32_t accessibilityWindowId,int64_t elementId,const std::string & text,std::vector<AccessibilityElementInfo> & elementInfos,bool systemApi)398 RetError AccessibleAbilityChannelClient::SearchElementInfosByText(int32_t accessibilityWindowId,
399     int64_t elementId, const std::string &text, std::vector<AccessibilityElementInfo> &elementInfos, bool systemApi)
400 {
401     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
402 #ifdef OHOS_BUILD_ENABLE_HITRACE
403     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementByText");
404 #endif // OHOS_BUILD_ENABLE_HITRACE
405     if (proxy_ == nullptr) {
406         HILOG_ERROR("SearchElementInfosByText Failed to connect to aams [channelId:%{public}d]",
407             channelId_);
408         return RET_ERR_SAMGR;
409     }
410 
411     int32_t requestId = GenerateRequestId();
412     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
413         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
414     if (elementOperator == nullptr) {
415         HILOG_ERROR("SearchElementInfosByText Failed to create elementOperator.");
416         return RET_ERR_NULLPTR;
417     }
418     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
419 
420     RetError ret = proxy_->SearchElementInfosByText(accessibilityWindowId,
421         elementId, text, requestId, elementOperator, systemApi);
422     if (ret != RET_OK) {
423         HILOG_ERROR("SearchElementInfosByText failed. ret[%{public}d]", ret);
424         return ret;
425     }
426 
427     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
428     if (wait != ffrt::future_status::ready) {
429         HILOG_ERROR("SearchElementInfosByText Failed to wait result");
430         return RET_ERR_TIME_OUT;
431     }
432 
433     for (auto &info : elementOperator->elementInfosResult_) {
434         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
435             HILOG_ERROR("SearchElementInfosByText The elementInfo from ace is wrong");
436             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
437         }
438     }
439     HILOG_INFO("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
440     elementInfos = elementOperator->elementInfosResult_;
441     return RET_OK;
442 }
443 
FocusMoveSearch(int32_t accessibilityWindowId,int64_t elementId,int32_t direction,AccessibilityElementInfo & elementInfo,bool systemApi)444 RetError AccessibleAbilityChannelClient::FocusMoveSearch(int32_t accessibilityWindowId,
445     int64_t elementId, int32_t direction, AccessibilityElementInfo &elementInfo, bool systemApi)
446 {
447     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
448     if (proxy_ == nullptr) {
449         HILOG_ERROR("FocusMoveSearch Failed to connect to aams [channelId:%{public}d]", channelId_);
450         return RET_ERR_SAMGR;
451     }
452 
453     int32_t requestId = GenerateRequestId();
454     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
455         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
456     if (elementOperator == nullptr) {
457         HILOG_ERROR("FocusMoveSearch Failed to create elementOperator.");
458         return RET_ERR_NULLPTR;
459     }
460     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
461 
462     RetError ret =
463         proxy_->FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, elementOperator, systemApi);
464     if (ret != RET_OK) {
465         HILOG_ERROR("FocusMoveSearch failed. ret[%{public}d]", ret);
466         return ret;
467     }
468 
469     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
470     if (wait != ffrt::future_status::ready) {
471         HILOG_ERROR("FocusMoveSearch Failed to wait result");
472         return RET_ERR_TIME_OUT;
473     }
474 
475     if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() ==
476         AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
477         HILOG_ERROR("FocusMoveSearch The elementInfo from ace is wrong");
478         return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
479     }
480 
481     HILOG_INFO("Get result successfully from ace");
482     elementInfo = elementOperator->accessibilityInfoResult_;
483     return RET_OK;
484 }
485 
SetTargetBundleName(const std::vector<std::string> & targetBundleNames)486 RetError AccessibleAbilityChannelClient::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
487 {
488     HILOG_INFO("[channelId:%{public}d]", channelId_);
489     if (proxy_) {
490         return proxy_->SetTargetBundleName(targetBundleNames);
491     } else {
492         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
493         return RET_ERR_SAMGR;
494     }
495 }
496 
SetIsRegisterDisconnectCallback(bool isRegister)497 RetError AccessibleAbilityChannelClient::SetIsRegisterDisconnectCallback(bool isRegister)
498 {
499     HILOG_INFO("[channelId:%{public}d]", channelId_);
500     if (proxy_ == nullptr) {
501         HILOG_ERROR("SetIsRegisterDisconnectCallback Failed to connect to aams [channelId:%{public}d]", channelId_);
502         return RET_ERR_SAMGR;
503     }
504     return proxy_->SetIsRegisterDisconnectCallback(isRegister);
505 }
506 
NotifyDisconnect()507 RetError AccessibleAbilityChannelClient::NotifyDisconnect()
508 {
509     HILOG_INFO("[channelId:%{public}d]", channelId_);
510     if (proxy_ == nullptr) {
511         HILOG_ERROR("NotifyDisconnect Failed to connect to aams [channelId:%{public}d]", channelId_);
512         return RET_ERR_SAMGR;
513     }
514     return proxy_->NotifyDisconnect();
515 }
516 
ConfigureEvents(const std::vector<uint32_t> needEvents)517 RetError AccessibleAbilityChannelClient::ConfigureEvents(const std::vector<uint32_t> needEvents)
518 {
519     HILOG_INFO("uitest config need events size is %{public}zu", needEvents.size());
520     if (proxy_ == nullptr) {
521         HILOG_ERROR("ConfigureEvents Failed to connect to aams [channelId:%{public}d]", channelId_);
522         return RET_ERR_SAMGR;
523     }
524     return proxy_->ConfigureEvents(needEvents);
525 }
526 
SearchElementInfosBySpecificProperty(int32_t accessibilityWindowId,int64_t elementId,const SpecificPropertyParam & param,std::vector<AccessibilityElementInfo> & infos,std::vector<AccessibilityElementInfo> & treeInfos,int32_t treeId)527 RetError AccessibleAbilityChannelClient::SearchElementInfosBySpecificProperty(int32_t accessibilityWindowId,
528     int64_t elementId, const SpecificPropertyParam& param, std::vector<AccessibilityElementInfo> &infos,
529     std::vector<AccessibilityElementInfo> &treeInfos, int32_t treeId)
530 {
531     int32_t requestId = GenerateRequestId();
532     HILOG_DEBUG("channelId:%{public}d, elementId:%{public}" PRId64 ", windowId:%{public}d, "
533         "requestId:%{public}d, propertyTarget:%{public}s, propertyType:%{public}u",
534         channelId_, elementId, accessibilityWindowId, requestId, param.propertyTarget.c_str(),
535         static_cast<uint32_t>(param.propertyType));
536 
537     if (proxy_ == nullptr) {
538         HILOG_ERROR("SearchElementInfosBySpecificProperty Failed to connect to aams [channelId:%{public}d]",
539             channelId_);
540         return RET_ERR_SAMGR;
541     }
542 
543     ElementBasicInfo elementBasicInfo;
544     elementBasicInfo.windowId = accessibilityWindowId;
545     elementBasicInfo.elementId = elementId;
546     elementBasicInfo.treeId = treeId;
547 
548     sptr<AccessibilityElementOperatorCallbackImpl> callback =
549         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
550     if (callback == nullptr) {
551         HILOG_ERROR("SearchElementInfosBySpecificProperty Failed to create callback");
552         return RET_ERR_NULLPTR;
553     }
554 
555     ffrt::future<void> promiseFuture = callback->promise_.get_future();
556     proxy_->SearchElementInfoBySpecificProperty(elementBasicInfo, param, requestId, callback);
557 
558     ffrt::future_status waitFocus = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
559     if (waitFocus != ffrt::future_status::ready) {
560         HILOG_ERROR("Failed to wait result");
561         return RET_ERR_TIME_OUT;
562     }
563 
564     if (!callback->elementInfosResult_.empty()) {
565         RetError ret = ValidateAndProcessElementInfos(callback->elementInfosResult_, infos, treeInfos,
566             accessibilityWindowId, "elementInfosResult_");
567         if (ret != RET_OK) {
568             return ret;
569         }
570     } else if (!callback->treeInfosResult_.empty()) {
571         RetError ret = ValidateAndProcessElementInfos(callback->treeInfosResult_, treeInfos, infos,
572             accessibilityWindowId, "treeInfosResult_");
573         if (ret != RET_OK) {
574             return ret;
575         }
576     } else {
577         infos.clear();
578         treeInfos.clear();
579         HILOG_DEBUG("Both result sets are empty");
580     }
581     return RET_OK;
582 }
583 
ValidateAndProcessElementInfos(const std::vector<AccessibilityElementInfo> & sourceInfos,std::vector<AccessibilityElementInfo> & targetInfos,std::vector<AccessibilityElementInfo> & clearInfos,int32_t accessibilityWindowId,const std::string & logType)584 RetError AccessibleAbilityChannelClient::ValidateAndProcessElementInfos(
585     const std::vector<AccessibilityElementInfo>& sourceInfos,
586     std::vector<AccessibilityElementInfo>& targetInfos,
587     std::vector<AccessibilityElementInfo>& clearInfos,
588     int32_t accessibilityWindowId,
589     const std::string& logType)
590 {
591     for (const auto &info : sourceInfos) {
592         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
593             HILOG_ERROR("SearchElementInfosBySpecificProperty The %{public}s from ace is wrong", logType.c_str());
594             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
595         }
596     }
597 
598     targetInfos = sourceInfos;
599     clearInfos.clear();
600 
601     for (auto &element : targetInfos) {
602         element.SetMainWindowId(accessibilityWindowId);
603     }
604 
605     HILOG_DEBUG("Found results in %{public}s, size: %{public}zu", logType.c_str(), targetInfos.size());
606     return RET_OK;
607 }
608 } // namespace Accessibility
609 } // namespace OHOS
610