1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#include "accessible_ability_channel.h"
17885b47fbSopenharmony_ci#include "accessible_ability_manager_service.h"
18885b47fbSopenharmony_ci#include "accessibility_window_connection.h"
19885b47fbSopenharmony_ci#include "accessibility_window_manager.h"
20885b47fbSopenharmony_ci#include "accessible_ability_connection.h"
21885b47fbSopenharmony_ci#include "hilog_wrapper.h"
22885b47fbSopenharmony_ci#include "transaction/rs_interfaces.h"
23885b47fbSopenharmony_ci
24885b47fbSopenharmony_cinamespace OHOS {
25885b47fbSopenharmony_cinamespace Accessibility {
26885b47fbSopenharmony_cinamespace {
27885b47fbSopenharmony_ci    constexpr uint32_t TIME_OUT_OPERATOR = 5000;
28885b47fbSopenharmony_ci    constexpr int32_t WINDOW_ID_INVALID = -1;
29885b47fbSopenharmony_ci    constexpr int64_t ELEMENT_ID_INVALID = -1;
30885b47fbSopenharmony_ci    MMI::InputManager* inputManager_ = MMI::InputManager::GetInstance();
31885b47fbSopenharmony_ci    std::map<int32_t, std::pair<bool, std::pair<int32_t, int32_t>>> accessibleKeyCodeTable = {
32885b47fbSopenharmony_ci        {ActionType::ACCESSIBILITY_ACTION_HOME,
33885b47fbSopenharmony_ci            {false, {MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_D}}},
34885b47fbSopenharmony_ci        {ActionType::ACCESSIBILITY_ACTION_RECENTTASK,
35885b47fbSopenharmony_ci            {false, {MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_TAB}}},
36885b47fbSopenharmony_ci        {ActionType::ACCESSIBILITY_ACTION_BACK,
37885b47fbSopenharmony_ci            {true, {MMI::KeyEvent::KEYCODE_BACK, MMI::KeyEvent::KEYCODE_BACK}}},
38885b47fbSopenharmony_ci        {ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER,
39885b47fbSopenharmony_ci            {true, {MMI::KeyEvent::KEYCODE_CALL_NOTIFICATION_CENTER, MMI::KeyEvent::KEYCODE_CALL_NOTIFICATION_CENTER}}},
40885b47fbSopenharmony_ci        {ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER,
41885b47fbSopenharmony_ci            {true, {MMI::KeyEvent::KEYCODE_CALL_CONTROL_CENTER, MMI::KeyEvent::KEYCODE_CALL_CONTROL_CENTER}}}};
42885b47fbSopenharmony_ci} // namespace
43885b47fbSopenharmony_ci
44885b47fbSopenharmony_ciAccessibleAbilityChannel::AccessibleAbilityChannel(const int32_t accountId, const std::string &clientName)
45885b47fbSopenharmony_ci    : clientName_(clientName), accountId_(accountId)
46885b47fbSopenharmony_ci{
47885b47fbSopenharmony_ci    eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(
48885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().GetMainRunner());
49885b47fbSopenharmony_ci}
50885b47fbSopenharmony_ci
51885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::SearchElementInfoByAccessibilityId(const ElementBasicInfo elementBasicInfo,
52885b47fbSopenharmony_ci    const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
53885b47fbSopenharmony_ci    const int32_t mode, bool isFilter)
54885b47fbSopenharmony_ci{
55885b47fbSopenharmony_ci    int32_t treeId = elementBasicInfo.treeId;
56885b47fbSopenharmony_ci    int32_t windowId = elementBasicInfo.windowId;
57885b47fbSopenharmony_ci    int64_t elementId = elementBasicInfo.elementId;
58885b47fbSopenharmony_ci    HILOG_DEBUG("elementId:%{public}" PRId64 " winId: %{public}d treeId: %{public}d", elementId, windowId, treeId);
59885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
60885b47fbSopenharmony_ci
61885b47fbSopenharmony_ci    if (eventHandler_ == nullptr || callback == nullptr) {
62885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
63885b47fbSopenharmony_ci            callback != nullptr);
64885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
65885b47fbSopenharmony_ci    }
66885b47fbSopenharmony_ci
67885b47fbSopenharmony_ci    int32_t accountId = accountId_;
68885b47fbSopenharmony_ci    std::string clientName = clientName_;
69885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
70885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
71885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, windowId, elementId, treeId, requestId,
72885b47fbSopenharmony_ci        callback, mode, isFilter]() {
73885b47fbSopenharmony_ci        HILOG_DEBUG("search element accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
74885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
75885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, windowId, FOCUS_TYPE_INVALID, clientName,
76885b47fbSopenharmony_ci            elementOperator, treeId);
77885b47fbSopenharmony_ci        if (ret != RET_OK || !CheckWinFromAwm(windowId)) {
78885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", windowId);
79885b47fbSopenharmony_ci            std::vector<AccessibilityElementInfo> infos = {};
80885b47fbSopenharmony_ci            callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
81885b47fbSopenharmony_ci            syncPromise->set_value(ret);
82885b47fbSopenharmony_ci            return;
83885b47fbSopenharmony_ci        }
84885b47fbSopenharmony_ci
85885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
86885b47fbSopenharmony_ci        if (windowId == SCENE_BOARD_WINDOW_ID && awm.IsInnerWindowRootElement(elementId)) {
87885b47fbSopenharmony_ci            std::vector<AccessibilityElementInfo> infos = {};
88885b47fbSopenharmony_ci            callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
89885b47fbSopenharmony_ci            HILOG_DEBUG("IsInnerWindowRootElement elementId: %{public}" PRId64 "", elementId);
90885b47fbSopenharmony_ci        } else {
91885b47fbSopenharmony_ci            int64_t realElementId = awm.GetSceneBoardElementId(windowId, elementId);
92885b47fbSopenharmony_ci            Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(windowId, treeId,
93885b47fbSopenharmony_ci                requestId, callback);
94885b47fbSopenharmony_ci            elementOperator->SearchElementInfoByAccessibilityId(realElementId, requestId, callback, mode, isFilter);
95885b47fbSopenharmony_ci            HILOG_DEBUG("AccessibleAbilityChannel::SearchElementInfoByAccessibilityId successfully");
96885b47fbSopenharmony_ci        }
97885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
98885b47fbSopenharmony_ci        }, "SearchElementInfoByAccessibilityId");
99885b47fbSopenharmony_ci
100885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
101885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
102885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait SearchElementInfoByAccessibilityId result");
103885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
104885b47fbSopenharmony_ci    }
105885b47fbSopenharmony_ci    return syncFuture.get();
106885b47fbSopenharmony_ci}
107885b47fbSopenharmony_ci
108885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::SearchElementInfosByText(const int32_t accessibilityWindowId,
109885b47fbSopenharmony_ci    const int64_t elementId, const std::string &text, const int32_t requestId,
110885b47fbSopenharmony_ci    const sptr<IAccessibilityElementOperatorCallback> &callback)
111885b47fbSopenharmony_ci{
112885b47fbSopenharmony_ci    HILOG_DEBUG("SearchElementInfosByText :channel SearchElementInfo elementId: %{public}" PRId64 " winId: %{public}d",
113885b47fbSopenharmony_ci        elementId, accessibilityWindowId);
114885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
115885b47fbSopenharmony_ci
116885b47fbSopenharmony_ci    if (eventHandler_ == nullptr) {
117885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
118885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
119885b47fbSopenharmony_ci    }
120885b47fbSopenharmony_ci    if (callback == nullptr) {
121885b47fbSopenharmony_ci        HILOG_ERROR("callback is nullptr.");
122885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
123885b47fbSopenharmony_ci    }
124885b47fbSopenharmony_ci
125885b47fbSopenharmony_ci    int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
126885b47fbSopenharmony_ci    HILOG_DEBUG("SearchElementInfosByText :channel SearchElementInfo treeId: %{public}d", treeId);
127885b47fbSopenharmony_ci    int32_t accountId = accountId_;
128885b47fbSopenharmony_ci    std::string clientName = clientName_;
129885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
130885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
131885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId, text,
132885b47fbSopenharmony_ci        requestId, callback]() {
133885b47fbSopenharmony_ci        HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
134885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
135885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID,
136885b47fbSopenharmony_ci            clientName, elementOperator, treeId);
137885b47fbSopenharmony_ci        if (ret != RET_OK) {
138885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
139885b47fbSopenharmony_ci            std::vector<AccessibilityElementInfo> infos = {};
140885b47fbSopenharmony_ci            callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
141885b47fbSopenharmony_ci            syncPromise->set_value(ret);
142885b47fbSopenharmony_ci            return;
143885b47fbSopenharmony_ci        }
144885b47fbSopenharmony_ci
145885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
146885b47fbSopenharmony_ci        int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
147885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
148885b47fbSopenharmony_ci            requestId, callback);
149885b47fbSopenharmony_ci        elementOperator->SearchElementInfosByText(realElementId, text, requestId, callback);
150885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
151885b47fbSopenharmony_ci        }, "SearchElementInfosByText");
152885b47fbSopenharmony_ci
153885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
154885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
155885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait SearchElementInfosByText result");
156885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
157885b47fbSopenharmony_ci    }
158885b47fbSopenharmony_ci    return syncFuture.get();
159885b47fbSopenharmony_ci}
160885b47fbSopenharmony_ci
161885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::FindFocusedElementInfo(const int32_t accessibilityWindowId,
162885b47fbSopenharmony_ci    const int64_t elementId, const int32_t focusType, const int32_t requestId,
163885b47fbSopenharmony_ci    const sptr<IAccessibilityElementOperatorCallback> &callback)
164885b47fbSopenharmony_ci{
165885b47fbSopenharmony_ci    HILOG_DEBUG("channel FindFocusedElementInfo elementId: %{public}" PRId64 " winId: %{public}d",
166885b47fbSopenharmony_ci        elementId, accessibilityWindowId);
167885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
168885b47fbSopenharmony_ci
169885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
170885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
171885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
172885b47fbSopenharmony_ci    }
173885b47fbSopenharmony_ci    if (callback == nullptr) {
174885b47fbSopenharmony_ci        HILOG_ERROR("callback is nullptr.");
175885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
176885b47fbSopenharmony_ci    }
177885b47fbSopenharmony_ci
178885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
179885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
180885b47fbSopenharmony_ci    int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
181885b47fbSopenharmony_ci    HILOG_DEBUG("FindFocusedElementInfo :channel FindFocusedElementInfo treeId: %{public}d", treeId);
182885b47fbSopenharmony_ci    int32_t accountId = accountId_;
183885b47fbSopenharmony_ci    std::string clientName = clientName_;
184885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId,
185885b47fbSopenharmony_ci        focusType, requestId, callback]() {
186885b47fbSopenharmony_ci        HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
187885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
188885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, accessibilityWindowId, focusType,
189885b47fbSopenharmony_ci            clientName, elementOperator, treeId);
190885b47fbSopenharmony_ci        if (ret != RET_OK) {
191885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
192885b47fbSopenharmony_ci            std::vector<AccessibilityElementInfo> infos = {};
193885b47fbSopenharmony_ci            callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
194885b47fbSopenharmony_ci            syncPromise->set_value(ret);
195885b47fbSopenharmony_ci            return;
196885b47fbSopenharmony_ci        }
197885b47fbSopenharmony_ci
198885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
199885b47fbSopenharmony_ci        int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
200885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
201885b47fbSopenharmony_ci            requestId, callback);
202885b47fbSopenharmony_ci        elementOperator->FindFocusedElementInfo(realElementId, focusType, requestId, callback);
203885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
204885b47fbSopenharmony_ci        }, "FindFocusedElementInfo");
205885b47fbSopenharmony_ci
206885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
207885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
208885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait FindFocusedElementInfo result");
209885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
210885b47fbSopenharmony_ci    }
211885b47fbSopenharmony_ci    return syncFuture.get();
212885b47fbSopenharmony_ci}
213885b47fbSopenharmony_ci
214885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::FocusMoveSearch(const int32_t accessibilityWindowId, const int64_t elementId,
215885b47fbSopenharmony_ci    const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
216885b47fbSopenharmony_ci{
217885b47fbSopenharmony_ci    HILOG_DEBUG("FocusMoveSearch :channel FocusMoveSearch elementId: %{public}" PRId64 " winId: %{public}d",
218885b47fbSopenharmony_ci        elementId, accessibilityWindowId);
219885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
220885b47fbSopenharmony_ci
221885b47fbSopenharmony_ci    if (eventHandler_ == nullptr) {
222885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
223885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
224885b47fbSopenharmony_ci    }
225885b47fbSopenharmony_ci    if (callback == nullptr) {
226885b47fbSopenharmony_ci        HILOG_ERROR("callback is nullptr.");
227885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
228885b47fbSopenharmony_ci    }
229885b47fbSopenharmony_ci
230885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
231885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
232885b47fbSopenharmony_ci    int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
233885b47fbSopenharmony_ci    HILOG_DEBUG("FocusMoveSearch :channel FocusMoveSearch treeId: %{public}d", treeId);
234885b47fbSopenharmony_ci    int32_t accountId = accountId_;
235885b47fbSopenharmony_ci    std::string clientName = clientName_;
236885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId,
237885b47fbSopenharmony_ci        elementId, treeId, direction, requestId, callback]() {
238885b47fbSopenharmony_ci        HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
239885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
240885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID,
241885b47fbSopenharmony_ci            clientName, elementOperator, treeId);
242885b47fbSopenharmony_ci        if (ret != RET_OK) {
243885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
244885b47fbSopenharmony_ci            std::vector<AccessibilityElementInfo> infos = {};
245885b47fbSopenharmony_ci            callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
246885b47fbSopenharmony_ci            syncPromise->set_value(ret);
247885b47fbSopenharmony_ci            return;
248885b47fbSopenharmony_ci        }
249885b47fbSopenharmony_ci
250885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
251885b47fbSopenharmony_ci        int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
252885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
253885b47fbSopenharmony_ci            requestId, callback);
254885b47fbSopenharmony_ci        elementOperator->FocusMoveSearch(realElementId, direction, requestId, callback);
255885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
256885b47fbSopenharmony_ci        }, "FocusMoveSearch");
257885b47fbSopenharmony_ci
258885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
259885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
260885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait FocusMoveSearch result");
261885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
262885b47fbSopenharmony_ci    }
263885b47fbSopenharmony_ci    return syncFuture.get();
264885b47fbSopenharmony_ci}
265885b47fbSopenharmony_ci
266885b47fbSopenharmony_civoid AccessibleAbilityChannel::SetKeyCodeToMmi(std::shared_ptr<MMI::KeyEvent>& keyEvent, const bool isPress,
267885b47fbSopenharmony_ci    const int32_t keyCode)
268885b47fbSopenharmony_ci{
269885b47fbSopenharmony_ci    HILOG_DEBUG();
270885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
271885b47fbSopenharmony_ci    if (keyEvent == nullptr) {
272885b47fbSopenharmony_ci        HILOG_ERROR("KeyEvent is nullptr");
273885b47fbSopenharmony_ci        return;
274885b47fbSopenharmony_ci    }
275885b47fbSopenharmony_ci    MMI::KeyEvent::KeyItem item;
276885b47fbSopenharmony_ci    item.SetPressed(isPress);
277885b47fbSopenharmony_ci    item.SetKeyCode(keyCode);
278885b47fbSopenharmony_ci    keyEvent->AddKeyItem(item);
279885b47fbSopenharmony_ci    keyEvent->SetKeyCode(keyCode);
280885b47fbSopenharmony_ci}
281885b47fbSopenharmony_ci
282885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::TransmitActionToMmi(const int32_t action)
283885b47fbSopenharmony_ci{
284885b47fbSopenharmony_ci    HILOG_DEBUG("The action is %{public}d", action);
285885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
286885b47fbSopenharmony_ci    std::shared_ptr<MMI::KeyEvent> keyEventUp = MMI::KeyEvent::Create();
287885b47fbSopenharmony_ci    std::shared_ptr<MMI::KeyEvent> keyEventDown = MMI::KeyEvent::Create();
288885b47fbSopenharmony_ci    if (keyEventUp == nullptr || keyEventDown == nullptr) {
289885b47fbSopenharmony_ci        HILOG_ERROR("KeyEvent is nullptr");
290885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
291885b47fbSopenharmony_ci    }
292885b47fbSopenharmony_ci
293885b47fbSopenharmony_ci    if (!inputManager_) {
294885b47fbSopenharmony_ci        HILOG_ERROR("inputManager_ is nullptr");
295885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
296885b47fbSopenharmony_ci    }
297885b47fbSopenharmony_ci
298885b47fbSopenharmony_ci    HILOG_INFO("Transmit keycode to MMI");
299885b47fbSopenharmony_ci
300885b47fbSopenharmony_ci    if (accessibleKeyCodeTable.at(action).first) {
301885b47fbSopenharmony_ci        SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.first);
302885b47fbSopenharmony_ci        SetKeyCodeToMmi(keyEventUp, false, accessibleKeyCodeTable.at(action).second.first);
303885b47fbSopenharmony_ci    } else {
304885b47fbSopenharmony_ci        SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.first);
305885b47fbSopenharmony_ci        SetKeyCodeToMmi(keyEventUp, false, accessibleKeyCodeTable.at(action).second.first);
306885b47fbSopenharmony_ci        SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.second);
307885b47fbSopenharmony_ci    }
308885b47fbSopenharmony_ci    keyEventDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
309885b47fbSopenharmony_ci    keyEventUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
310885b47fbSopenharmony_ci    inputManager_->SimulateInputEvent(keyEventDown);
311885b47fbSopenharmony_ci    inputManager_->SimulateInputEvent(keyEventUp);
312885b47fbSopenharmony_ci
313885b47fbSopenharmony_ci    return RET_OK;
314885b47fbSopenharmony_ci}
315885b47fbSopenharmony_ci
316885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::EnableScreenCurtain(bool isEnable)
317885b47fbSopenharmony_ci{
318885b47fbSopenharmony_ci    HILOG_DEBUG();
319885b47fbSopenharmony_ci    auto& aams = Singleton<AccessibleAbilityManagerService>::GetInstance();
320885b47fbSopenharmony_ci    aams.PostDelayUnloadTask();
321885b47fbSopenharmony_ci    return aams.SetCurtainScreenUsingStatus(isEnable);
322885b47fbSopenharmony_ci}
323885b47fbSopenharmony_ci
324885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::ExecuteAction(const int32_t accessibilityWindowId, const int64_t elementId,
325885b47fbSopenharmony_ci    const int32_t action, const std::map<std::string, std::string> &actionArguments, const int32_t requestId,
326885b47fbSopenharmony_ci    const sptr<IAccessibilityElementOperatorCallback> &callback)
327885b47fbSopenharmony_ci{
328885b47fbSopenharmony_ci    HILOG_DEBUG("ExecuteAction elementId:%{public}" PRId64 " winId:%{public}d, action:%{public}d, requestId:%{public}d",
329885b47fbSopenharmony_ci        elementId, accessibilityWindowId, action, requestId);
330885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
331885b47fbSopenharmony_ci    if (eventHandler_ == nullptr || callback == nullptr) {
332885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
333885b47fbSopenharmony_ci            callback != nullptr);
334885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
335885b47fbSopenharmony_ci    }
336885b47fbSopenharmony_ci
337885b47fbSopenharmony_ci    if (accessibleKeyCodeTable.find(action) != accessibleKeyCodeTable.end()) {
338885b47fbSopenharmony_ci        RetError ret = TransmitActionToMmi(action);
339885b47fbSopenharmony_ci        if (ret != RET_OK) {
340885b47fbSopenharmony_ci            HILOG_ERROR("Transmit Action To Mmi failed!");
341885b47fbSopenharmony_ci            callback->SetExecuteActionResult(false, requestId);
342885b47fbSopenharmony_ci            return RET_ERR_FAILED;
343885b47fbSopenharmony_ci        }
344885b47fbSopenharmony_ci        callback->SetExecuteActionResult(true, requestId);
345885b47fbSopenharmony_ci        return RET_OK;
346885b47fbSopenharmony_ci    }
347885b47fbSopenharmony_ci    SetFocusWindowIdAndElementId(accessibilityWindowId, elementId, action);
348885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
349885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
350885b47fbSopenharmony_ci    int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
351885b47fbSopenharmony_ci    int32_t accountId = accountId_;
352885b47fbSopenharmony_ci    std::string clientName = clientName_;
353885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId, action,
354885b47fbSopenharmony_ci        actionArguments, requestId, callback]() {
355885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
356885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID, clientName,
357885b47fbSopenharmony_ci            elementOperator, treeId);
358885b47fbSopenharmony_ci        if (ret != RET_OK) {
359885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
360885b47fbSopenharmony_ci            syncPromise->set_value(ret);
361885b47fbSopenharmony_ci            return;
362885b47fbSopenharmony_ci        }
363885b47fbSopenharmony_ci
364885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
365885b47fbSopenharmony_ci        int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
366885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
367885b47fbSopenharmony_ci            requestId, callback);
368885b47fbSopenharmony_ci        elementOperator->ExecuteAction(realElementId, action, actionArguments, requestId, callback);
369885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
370885b47fbSopenharmony_ci        }, "ExecuteAction");
371885b47fbSopenharmony_ci
372885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
373885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
374885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait ExecuteAction result");
375885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
376885b47fbSopenharmony_ci    }
377885b47fbSopenharmony_ci    return syncFuture.get();
378885b47fbSopenharmony_ci}
379885b47fbSopenharmony_ci
380885b47fbSopenharmony_civoid AccessibleAbilityChannel::SetFocusWindowIdAndElementId(const int32_t accessibilityWindowId,
381885b47fbSopenharmony_ci    const int64_t elementId, const int32_t action)
382885b47fbSopenharmony_ci{
383885b47fbSopenharmony_ci    auto& awm = Singleton<AccessibleAbilityManagerService>::GetInstance();
384885b47fbSopenharmony_ci    if ((action == ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS)) {
385885b47fbSopenharmony_ci        awm.SetFocusWindowId(accessibilityWindowId);
386885b47fbSopenharmony_ci        awm.SetFocusElementId(elementId);
387885b47fbSopenharmony_ci    } else if (action == ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS) {
388885b47fbSopenharmony_ci        awm.SetFocusWindowId(WINDOW_ID_INVALID);
389885b47fbSopenharmony_ci        awm.SetFocusElementId(ELEMENT_ID_INVALID);
390885b47fbSopenharmony_ci    }
391885b47fbSopenharmony_ci}
392885b47fbSopenharmony_ci
393885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
394885b47fbSopenharmony_ci{
395885b47fbSopenharmony_ci    HILOG_DEBUG("windowId:%{public}d", windowId);
396885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
397885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
398885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
399885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
400885b47fbSopenharmony_ci    }
401885b47fbSopenharmony_ci
402885b47fbSopenharmony_ci    int32_t accountId = accountId_;
403885b47fbSopenharmony_ci    std::string clientName = clientName_;
404885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
405885b47fbSopenharmony_ci    std::shared_ptr<AccessibilityWindowInfo> tmpWindowInfo = std::make_shared<AccessibilityWindowInfo>(windowInfo);
406885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
407885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, windowId, tmpWindowInfo, syncPromise]() {
408885b47fbSopenharmony_ci        HILOG_DEBUG("windowId:%{public}d", windowId);
409885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
410885b47fbSopenharmony_ci        if (!clientConnection) {
411885b47fbSopenharmony_ci            HILOG_ERROR("There is no client connection");
412885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CONNECTION);
413885b47fbSopenharmony_ci            return;
414885b47fbSopenharmony_ci        }
415885b47fbSopenharmony_ci        if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
416885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityChannel::GetWindow failed: no capability");
417885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CAPABILITY);
418885b47fbSopenharmony_ci            return;
419885b47fbSopenharmony_ci        }
420885b47fbSopenharmony_ci
421885b47fbSopenharmony_ci        if (Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindow(windowId, *tmpWindowInfo)) {
422885b47fbSopenharmony_ci            syncPromise->set_value(RET_OK);
423885b47fbSopenharmony_ci        } else {
424885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_WINDOW_CONNECTION);
425885b47fbSopenharmony_ci        }
426885b47fbSopenharmony_ci        }, "GetWindow");
427885b47fbSopenharmony_ci
428885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
429885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
430885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait GetWindow result");
431885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
432885b47fbSopenharmony_ci    }
433885b47fbSopenharmony_ci
434885b47fbSopenharmony_ci    windowInfo = *tmpWindowInfo;
435885b47fbSopenharmony_ci    return syncFuture.get();
436885b47fbSopenharmony_ci}
437885b47fbSopenharmony_ci
438885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetWindows(std::vector<AccessibilityWindowInfo> &windows)
439885b47fbSopenharmony_ci{
440885b47fbSopenharmony_ci    HILOG_DEBUG();
441885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
442885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
443885b47fbSopenharmony_ci    uint64_t displayId = Singleton<AccessibilityDisplayManager>::GetInstance().GetDefaultDisplayId();
444885b47fbSopenharmony_ci    HILOG_DEBUG("default display id is %{public}" PRIu64 "", displayId);
445885b47fbSopenharmony_ci    return GetWindows(displayId, windows);
446885b47fbSopenharmony_ci#else
447885b47fbSopenharmony_ci    HILOG_DEBUG("not support display manager");
448885b47fbSopenharmony_ci    return GetWindows(0, windows);
449885b47fbSopenharmony_ci#endif
450885b47fbSopenharmony_ci}
451885b47fbSopenharmony_ci
452885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetWindowsByDisplayId(const uint64_t displayId,
453885b47fbSopenharmony_ci    std::vector<AccessibilityWindowInfo> &windows)
454885b47fbSopenharmony_ci{
455885b47fbSopenharmony_ci    HILOG_DEBUG();
456885b47fbSopenharmony_ci    return GetWindows(displayId, windows);
457885b47fbSopenharmony_ci}
458885b47fbSopenharmony_ci
459885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetWindows(uint64_t displayId, std::vector<AccessibilityWindowInfo> &windows) const
460885b47fbSopenharmony_ci{
461885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
462885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
463885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
464885b47fbSopenharmony_ci    }
465885b47fbSopenharmony_ci
466885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
467885b47fbSopenharmony_ci    auto tmpWindows = std::make_shared<std::vector<AccessibilityWindowInfo>>(windows);
468885b47fbSopenharmony_ci    int32_t accountId = accountId_;
469885b47fbSopenharmony_ci    std::string clientName = clientName_;
470885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
471885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, displayId, tmpWindows, syncPromise]() {
472885b47fbSopenharmony_ci        HILOG_DEBUG();
473885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
474885b47fbSopenharmony_ci        if (!clientConnection) {
475885b47fbSopenharmony_ci            HILOG_ERROR("There is no client connection");
476885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CONNECTION);
477885b47fbSopenharmony_ci            return;
478885b47fbSopenharmony_ci        }
479885b47fbSopenharmony_ci
480885b47fbSopenharmony_ci        if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
481885b47fbSopenharmony_ci            HILOG_ERROR("GetWindows failed: no capability");
482885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CAPABILITY);
483885b47fbSopenharmony_ci            return;
484885b47fbSopenharmony_ci        }
485885b47fbSopenharmony_ci
486885b47fbSopenharmony_ci        std::vector<AccessibilityWindowInfo> windowInfos =
487885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindows();
488885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
489885b47fbSopenharmony_ci        for (auto &window : windowInfos) {
490885b47fbSopenharmony_ci            if (window.GetDisplayId() == displayId) {
491885b47fbSopenharmony_ci                tmpWindows->emplace_back(window);
492885b47fbSopenharmony_ci            }
493885b47fbSopenharmony_ci        }
494885b47fbSopenharmony_ci#else
495885b47fbSopenharmony_ci        for (auto &window : windowInfos) {
496885b47fbSopenharmony_ci            tmpWindows->emplace_back(window);
497885b47fbSopenharmony_ci        }
498885b47fbSopenharmony_ci#endif
499885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
500885b47fbSopenharmony_ci        }, "GetWindows");
501885b47fbSopenharmony_ci
502885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
503885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
504885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait GetWindows result");
505885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
506885b47fbSopenharmony_ci    }
507885b47fbSopenharmony_ci
508885b47fbSopenharmony_ci    windows = *tmpWindows;
509885b47fbSopenharmony_ci    return syncFuture.get();
510885b47fbSopenharmony_ci}
511885b47fbSopenharmony_ci
512885b47fbSopenharmony_civoid AccessibleAbilityChannel::SetOnKeyPressEventResult(const bool handled, const int32_t sequence)
513885b47fbSopenharmony_ci{
514885b47fbSopenharmony_ci    HILOG_DEBUG();
515885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
516885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
517885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
518885b47fbSopenharmony_ci        return;
519885b47fbSopenharmony_ci    }
520885b47fbSopenharmony_ci
521885b47fbSopenharmony_ci    int32_t accountId = accountId_;
522885b47fbSopenharmony_ci    std::string clientName = clientName_;
523885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, handled, sequence]() {
524885b47fbSopenharmony_ci        sptr<KeyEventFilter> keyEventFilter =
525885b47fbSopenharmony_ci            Singleton<AccessibleAbilityManagerService>::GetInstance().GetKeyEventFilter();
526885b47fbSopenharmony_ci        if (!keyEventFilter) {
527885b47fbSopenharmony_ci            return;
528885b47fbSopenharmony_ci        }
529885b47fbSopenharmony_ci
530885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
531885b47fbSopenharmony_ci        if (!clientConnection) {
532885b47fbSopenharmony_ci            HILOG_ERROR("There is no client connection");
533885b47fbSopenharmony_ci            return;
534885b47fbSopenharmony_ci        }
535885b47fbSopenharmony_ci        keyEventFilter->SetServiceOnKeyEventResult(*clientConnection, handled, sequence);
536885b47fbSopenharmony_ci        }, "SetOnKeyPressEventResult");
537885b47fbSopenharmony_ci}
538885b47fbSopenharmony_ci
539885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetCursorPosition(const int32_t accessibilityWindowId, const int64_t elementId,
540885b47fbSopenharmony_ci    const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
541885b47fbSopenharmony_ci{
542885b47fbSopenharmony_ci    HILOG_DEBUG("GetCursorPosition :channel GetCursorPosition elementId: %{public}" PRId64 " winId: %{public}d",
543885b47fbSopenharmony_ci        elementId, accessibilityWindowId);
544885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
545885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
546885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
547885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
548885b47fbSopenharmony_ci    }
549885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
550885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
551885b47fbSopenharmony_ci    int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
552885b47fbSopenharmony_ci    HILOG_DEBUG("GetCursorPosition :channel GetCursorPosition treeId: %{public}d", treeId);
553885b47fbSopenharmony_ci    int32_t accountId = accountId_;
554885b47fbSopenharmony_ci    std::string clientName = clientName_;
555885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId,
556885b47fbSopenharmony_ci        requestId, callback]() {
557885b47fbSopenharmony_ci        HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
558885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> elementOperator = nullptr;
559885b47fbSopenharmony_ci        RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID, clientName,
560885b47fbSopenharmony_ci            elementOperator, treeId);
561885b47fbSopenharmony_ci        if (ret != RET_OK) {
562885b47fbSopenharmony_ci            HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
563885b47fbSopenharmony_ci            syncPromise->set_value(ret);
564885b47fbSopenharmony_ci            return;
565885b47fbSopenharmony_ci        }
566885b47fbSopenharmony_ci
567885b47fbSopenharmony_ci        auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
568885b47fbSopenharmony_ci        int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
569885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
570885b47fbSopenharmony_ci            requestId, callback);
571885b47fbSopenharmony_ci        elementOperator->GetCursorPosition(realElementId, requestId, callback);
572885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
573885b47fbSopenharmony_ci        }, "GetCursorPosition");
574885b47fbSopenharmony_ci
575885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
576885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
577885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait GetCursorPosition result");
578885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
579885b47fbSopenharmony_ci    }
580885b47fbSopenharmony_ci    return syncFuture.get();
581885b47fbSopenharmony_ci}
582885b47fbSopenharmony_ci
583885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::SendSimulateGesture(
584885b47fbSopenharmony_ci    const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
585885b47fbSopenharmony_ci{
586885b47fbSopenharmony_ci    HILOG_INFO();
587885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
588885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
589885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr");
590885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
591885b47fbSopenharmony_ci    }
592885b47fbSopenharmony_ci
593885b47fbSopenharmony_ci    int32_t accountId = accountId_;
594885b47fbSopenharmony_ci    std::string clientName = clientName_;
595885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
596885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
597885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, gesturePath, syncPromise]() {
598885b47fbSopenharmony_ci        HILOG_DEBUG();
599885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
600885b47fbSopenharmony_ci        if (!clientConnection) {
601885b47fbSopenharmony_ci            HILOG_ERROR("There is no client connection");
602885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CONNECTION);
603885b47fbSopenharmony_ci            return;
604885b47fbSopenharmony_ci        }
605885b47fbSopenharmony_ci
606885b47fbSopenharmony_ci        if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_GESTURE)) {
607885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityChannel::SendSimulateGesture failed: no capability");
608885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CAPABILITY);
609885b47fbSopenharmony_ci            return;
610885b47fbSopenharmony_ci        }
611885b47fbSopenharmony_ci
612885b47fbSopenharmony_ci        sptr<TouchEventInjector> touchEventInjector =
613885b47fbSopenharmony_ci            Singleton<AccessibleAbilityManagerService>::GetInstance().GetTouchEventInjector();
614885b47fbSopenharmony_ci        if (!touchEventInjector) {
615885b47fbSopenharmony_ci            HILOG_ERROR("touchEventInjector is null");
616885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_INJECTOR);
617885b47fbSopenharmony_ci            return;
618885b47fbSopenharmony_ci        }
619885b47fbSopenharmony_ci        touchEventInjector->InjectEvents(gesturePath);
620885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
621885b47fbSopenharmony_ci        }, "SendSimulateGesture");
622885b47fbSopenharmony_ci
623885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
624885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
625885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait SendSimulateGesture result");
626885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
627885b47fbSopenharmony_ci    }
628885b47fbSopenharmony_ci    return syncFuture.get();
629885b47fbSopenharmony_ci}
630885b47fbSopenharmony_ci
631885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
632885b47fbSopenharmony_ci{
633885b47fbSopenharmony_ci    HILOG_DEBUG();
634885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
635885b47fbSopenharmony_ci    if (eventHandler_== nullptr) {
636885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr");
637885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
638885b47fbSopenharmony_ci    }
639885b47fbSopenharmony_ci
640885b47fbSopenharmony_ci    int32_t accountId = accountId_;
641885b47fbSopenharmony_ci    std::string clientName = clientName_;
642885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
643885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
644885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, clientName, targetBundleNames, syncPromise]() {
645885b47fbSopenharmony_ci        HILOG_DEBUG();
646885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
647885b47fbSopenharmony_ci        if (!clientConnection) {
648885b47fbSopenharmony_ci            HILOG_ERROR("There is no client connection");
649885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CONNECTION);
650885b47fbSopenharmony_ci            return;
651885b47fbSopenharmony_ci        }
652885b47fbSopenharmony_ci
653885b47fbSopenharmony_ci        clientConnection->SetAbilityInfoTargetBundleName(targetBundleNames);
654885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
655885b47fbSopenharmony_ci        }, "SetTargetBundleName");
656885b47fbSopenharmony_ci
657885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
658885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
659885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait SetTargetBundleName result");
660885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
661885b47fbSopenharmony_ci    }
662885b47fbSopenharmony_ci    return syncFuture.get();
663885b47fbSopenharmony_ci}
664885b47fbSopenharmony_ci
665885b47fbSopenharmony_cisptr<AccessibleAbilityConnection> AccessibleAbilityChannel::GetConnection(
666885b47fbSopenharmony_ci    int32_t accountId, const std::string &clientName)
667885b47fbSopenharmony_ci{
668885b47fbSopenharmony_ci    HILOG_DEBUG();
669885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData =
670885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId);
671885b47fbSopenharmony_ci    if (accountData == nullptr) {
672885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
673885b47fbSopenharmony_ci        return nullptr;
674885b47fbSopenharmony_ci    }
675885b47fbSopenharmony_ci
676885b47fbSopenharmony_ci    HILOG_DEBUG("accountId[%{public}d] clientName[%{public}s]", accountId, clientName.c_str());
677885b47fbSopenharmony_ci    return accountData->GetAccessibleAbilityConnection(clientName);
678885b47fbSopenharmony_ci}
679885b47fbSopenharmony_ci
680885b47fbSopenharmony_ciRetError AccessibleAbilityChannel::GetElementOperator(
681885b47fbSopenharmony_ci    int32_t accountId, int32_t windowId, int32_t focusType, const std::string &clientName,
682885b47fbSopenharmony_ci    sptr<IAccessibilityElementOperator> &elementOperator, const int32_t treeId)
683885b47fbSopenharmony_ci{
684885b47fbSopenharmony_ci    HILOG_DEBUG();
685885b47fbSopenharmony_ci    elementOperator = nullptr;
686885b47fbSopenharmony_ci    sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
687885b47fbSopenharmony_ci    if (!clientConnection) {
688885b47fbSopenharmony_ci        HILOG_ERROR("There is no client connection");
689885b47fbSopenharmony_ci        return RET_ERR_NO_CONNECTION;
690885b47fbSopenharmony_ci    }
691885b47fbSopenharmony_ci    if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
692885b47fbSopenharmony_ci        HILOG_ERROR("the client has no retieve capability");
693885b47fbSopenharmony_ci        return RET_ERR_NO_CAPABILITY;
694885b47fbSopenharmony_ci    }
695885b47fbSopenharmony_ci
696885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData =
697885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId);
698885b47fbSopenharmony_ci    if (!accountData) {
699885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
700885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
701885b47fbSopenharmony_ci    }
702885b47fbSopenharmony_ci    int32_t realId = Singleton<AccessibilityWindowManager>::GetInstance().ConvertToRealWindowId(windowId, focusType);
703885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection =  nullptr;
704885b47fbSopenharmony_ci    connection = accountData->GetAccessibilityWindowConnection(realId);
705885b47fbSopenharmony_ci    if (connection == nullptr) {
706885b47fbSopenharmony_ci        HILOG_ERROR("windowId[%{public}d] has no connection", realId);
707885b47fbSopenharmony_ci        return RET_ERR_NO_WINDOW_CONNECTION;
708885b47fbSopenharmony_ci    }
709885b47fbSopenharmony_ci    if (treeId <= 0) {
710885b47fbSopenharmony_ci        elementOperator = connection->GetProxy();
711885b47fbSopenharmony_ci    } else {
712885b47fbSopenharmony_ci        elementOperator = connection->GetCardProxy(treeId);
713885b47fbSopenharmony_ci    }
714885b47fbSopenharmony_ci    if (!elementOperator) {
715885b47fbSopenharmony_ci        HILOG_ERROR("The proxy of window connection is nullptr");
716885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
717885b47fbSopenharmony_ci    }
718885b47fbSopenharmony_ci    return RET_OK;
719885b47fbSopenharmony_ci}
720885b47fbSopenharmony_ci
721885b47fbSopenharmony_cibool AccessibleAbilityChannel::CheckWinFromAwm(const int32_t windowId)
722885b47fbSopenharmony_ci{
723885b47fbSopenharmony_ci    std::vector<AccessibilityWindowInfo> windowsFromAwm =
724885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindows();
725885b47fbSopenharmony_ci    if (!windowsFromAwm.empty()) {
726885b47fbSopenharmony_ci        for (const auto& window: windowsFromAwm) {
727885b47fbSopenharmony_ci            if (windowId == window.GetWindowId()) {
728885b47fbSopenharmony_ci                return true;
729885b47fbSopenharmony_ci            }
730885b47fbSopenharmony_ci        }
731885b47fbSopenharmony_ci    }
732885b47fbSopenharmony_ci    return false;
733885b47fbSopenharmony_ci}
734885b47fbSopenharmony_ci} // namespace Accessibility
735885b47fbSopenharmony_ci} // namespace OHOS