/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "accessible_ability_channel_client.h" #include #include #include "accessibility_element_operator_callback_impl.h" #include "hilog_wrapper.h" namespace OHOS { namespace Accessibility { namespace { constexpr uint32_t TIME_OUT_OPERATOR = 5000; constexpr int32_t REQUEST_ID_MAX = 0x0000FFFF; } // namespace int32_t AccessibleAbilityChannelClient::GenerateRequestId() { int32_t requestId = requestId_++; requestId = requestId % REQUEST_ID_MAX; return requestId; } sptr AccessibleAbilityChannelClient::GetRemote() { return proxy_->AsObject(); } void AccessibleAbilityChannelClient::SetOnKeyPressEventResult(const bool handled, const int32_t sequence) { HILOG_INFO("[channelId:%{public}d]", channelId_); if (proxy_) { proxy_->SetOnKeyPressEventResult(handled, sequence); } else { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); } } RetError AccessibleAbilityChannelClient::FindFocusedElementInfo(int32_t accessibilityWindowId, int64_t elementId, int32_t focusType, AccessibilityElementInfo &elementInfo) { HILOG_DEBUG("[channelId:%{public}d]", channelId_); HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "FindFocusedElement"); if (proxy_ == nullptr) { HILOG_ERROR("FindFocusedElementInfo Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } int32_t requestId = GenerateRequestId(); sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("FindFocusedElementInfo Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); int32_t windowId = accessibilityWindowId; if (accessibilityWindowId == ANY_WINDOW_ID && focusType == FOCUS_TYPE_ACCESSIBILITY && accessibilityFocusedWindowId_ != INVALID_WINDOW_ID) { windowId = accessibilityFocusedWindowId_; HILOG_INFO("Convert into accessibility focused window id[%{public}d]", windowId); } RetError ret = proxy_->FindFocusedElementInfo(windowId, elementId, focusType, requestId, elementOperator); if (ret != RET_OK) { HILOG_ERROR("FindFocusedElementInfo failed. ret[%{public}d]", ret); return ret; } HILOG_DEBUG("channelId:%{public}d, windowId:%{public}d, elementId:%{public}" PRId64 ", focusType:%{public}d", channelId_, windowId, elementId, focusType); ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("FindFocusedElementInfo Failed to wait result"); return RET_ERR_TIME_OUT; } if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) { HILOG_ERROR("FindFocusedElementInfo The elementInfo from ace is wrong"); return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE; } HILOG_INFO("Get result successfully from ace."); elementInfo = elementOperator->accessibilityInfoResult_; elementInfo.SetMainWindowId(windowId); return RET_OK; } RetError AccessibleAbilityChannelClient::SendSimulateGesture( const std::shared_ptr &gesturePath) { HILOG_INFO("[channelId:%{public}d]", channelId_); if (proxy_) { return proxy_->SendSimulateGesture(gesturePath); } else { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } } RetError AccessibleAbilityChannelClient::GetCursorPosition( int32_t accessibilityWindowId, int64_t elementId, int32_t &position) { HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetCursorPosition"); if (proxy_ == nullptr) { HILOG_ERROR("GetCursorPosition Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } int32_t requestId = GenerateRequestId(); sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("GetCursorPosition Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); RetError ret = proxy_->GetCursorPosition(accessibilityWindowId, elementId, requestId, elementOperator); if (ret != RET_OK) { HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret); return ret; } ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("GetCursorPosition Failed to wait result"); return RET_ERR_TIME_OUT; } position = elementOperator->CursorPosition_; HILOG_INFO("position%{public}d", position); return RET_OK; } RetError AccessibleAbilityChannelClient::ExecuteAction(int32_t accessibilityWindowId, int64_t elementId, int32_t action, const std::map &actionArguments) { HILOG_DEBUG("execute action:%{public}d, elementId:%{public}" PRId64 "", action, elementId); HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "ExecuteAction"); if (proxy_ == nullptr) { HILOG_ERROR("ExecuteAction Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } if (action == ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS && accessibilityFocusedElementId_ != INVALID_WINDOW_ID) { ExecuteAction(accessibilityFocusedWindowId_, accessibilityFocusedElementId_, ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, actionArguments); } int32_t requestId = GenerateRequestId(); sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("ExecuteAction Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); RetError ret = proxy_->ExecuteAction(accessibilityWindowId, elementId, action, actionArguments, requestId, elementOperator); if (ret != RET_OK) { HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret); return ret; } ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("Failed to wait result"); return RET_ERR_TIME_OUT; } HILOG_INFO("action:[%{public}d], executeActionResult_[%{public}d]", action, elementOperator->executeActionResult_); if (elementOperator->executeActionResult_) { switch (action) { case ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS: accessibilityFocusedWindowId_ = accessibilityWindowId; accessibilityFocusedElementId_ = elementId; HILOG_INFO("Set windowId:%{public}d, elementId:%{public}" PRId64 "", accessibilityWindowId, elementId); break; case ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS: accessibilityFocusedWindowId_ = INVALID_WINDOW_ID; accessibilityFocusedElementId_ = INVALID_WINDOW_ID; HILOG_INFO("Clear accessibility focused window id"); break; default: break; } } return elementOperator->executeActionResult_ ? RET_OK : RET_ERR_PERFORM_ACTION_FAILED_BY_ACE; } RetError AccessibleAbilityChannelClient::EnableScreenCurtain(bool isEnable) { HILOG_INFO("[channelId:%{public}d]", channelId_); if (proxy_ == nullptr) { HILOG_ERROR("EnableScreenCurtain Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } return proxy_->EnableScreenCurtain(isEnable); } RetError AccessibleAbilityChannelClient::SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId, int64_t elementId, int32_t mode, std::vector &elementInfos, int32_t treeId, bool isFilter) { int32_t requestId = GenerateRequestId(); HILOG_DEBUG("channelId:%{public}d, elementId:%{public}" PRId64 ", windowId:%{public}d, requestId:%{public}d", channelId_, elementId, accessibilityWindowId, requestId); HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementById"); if (proxy_ == nullptr) { HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); ElementBasicInfo elementBasicInfo {}; elementBasicInfo.windowId = accessibilityWindowId; elementBasicInfo.treeId = treeId; elementBasicInfo.elementId = elementId; RetError ret = proxy_->SearchElementInfoByAccessibilityId(elementBasicInfo, requestId, elementOperator, mode, isFilter); if (ret != RET_OK) { HILOG_ERROR("SearchElementInfosByAccessibilityId windowId :[%{pubic}d] Failed to wait result, Time out", accessibilityWindowId); return ret; } ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to wait result"); return RET_ERR_TIME_OUT; } for (auto &info : elementOperator->elementInfosResult_) { if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) { HILOG_ERROR("SearchElementInfosByAccessibilityId The elementInfo from ace is wrong"); return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE; } } HILOG_DEBUG("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size()); elementInfos = elementOperator->elementInfosResult_; if (!elementInfos.empty()) { for (auto &element : elementInfos) { element.SetMainWindowId(accessibilityWindowId); } } return RET_OK; } RetError AccessibleAbilityChannelClient::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo) { HILOG_DEBUG("[channelId:%{public}d] [windowId:%{public}d]", channelId_, windowId); HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER); if (proxy_ == nullptr) { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } return proxy_->GetWindow(windowId, windowInfo); } RetError AccessibleAbilityChannelClient::GetWindows(std::vector &windows) { HILOG_DEBUG("[channelId:%{public}d]", channelId_); HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER); if (proxy_) { return proxy_->GetWindows(windows); } else { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } } RetError AccessibleAbilityChannelClient::GetWindows(const uint64_t displayId, std::vector &windows) const { HILOG_DEBUG("[channelId:%{public}d] [displayId:%{public}" PRIu64 "]", channelId_, displayId); HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetWindowsByDisplayId"); if (proxy_) { return proxy_->GetWindowsByDisplayId(displayId, windows); } else { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } } RetError AccessibleAbilityChannelClient::SearchElementInfosByText(int32_t accessibilityWindowId, int64_t elementId, const std::string &text, std::vector &elementInfos) { HILOG_DEBUG("[channelId:%{public}d]", channelId_); HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementByText"); if (proxy_ == nullptr) { HILOG_ERROR("SearchElementInfosByText Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } int32_t requestId = GenerateRequestId(); sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("SearchElementInfosByText Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); RetError ret = proxy_->SearchElementInfosByText(accessibilityWindowId, elementId, text, requestId, elementOperator); if (ret != RET_OK) { HILOG_ERROR("SearchElementInfosByText failed. ret[%{public}d]", ret); return ret; } ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("SearchElementInfosByText Failed to wait result"); return RET_ERR_TIME_OUT; } for (auto &info : elementOperator->elementInfosResult_) { if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) { HILOG_ERROR("SearchElementInfosByText The elementInfo from ace is wrong"); return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE; } } HILOG_INFO("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size()); elementInfos = elementOperator->elementInfosResult_; return RET_OK; } RetError AccessibleAbilityChannelClient::FocusMoveSearch(int32_t accessibilityWindowId, int64_t elementId, int32_t direction, AccessibilityElementInfo &elementInfo) { HILOG_DEBUG("[channelId:%{public}d]", channelId_); if (proxy_ == nullptr) { HILOG_ERROR("FocusMoveSearch Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } int32_t requestId = GenerateRequestId(); sptr elementOperator = new(std::nothrow) AccessibilityElementOperatorCallbackImpl(); if (elementOperator == nullptr) { HILOG_ERROR("FocusMoveSearch Failed to create elementOperator."); return RET_ERR_NULLPTR; } ffrt::future promiseFuture = elementOperator->promise_.get_future(); RetError ret = proxy_->FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, elementOperator); if (ret != RET_OK) { HILOG_ERROR("FocusMoveSearch failed. ret[%{public}d]", ret); return ret; } ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR)); if (wait != ffrt::future_status::ready) { HILOG_ERROR("FocusMoveSearch Failed to wait result"); return RET_ERR_TIME_OUT; } if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) { HILOG_ERROR("FocusMoveSearch The elementInfo from ace is wrong"); return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE; } HILOG_INFO("Get result successfully from ace"); elementInfo = elementOperator->accessibilityInfoResult_; return RET_OK; } RetError AccessibleAbilityChannelClient::SetTargetBundleName(const std::vector &targetBundleNames) { HILOG_INFO("[channelId:%{public}d]", channelId_); if (proxy_) { return proxy_->SetTargetBundleName(targetBundleNames); } else { HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_); return RET_ERR_SAMGR; } } } // namespace Accessibility } // namespace OHOS