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_manager_service.h"
17885b47fbSopenharmony_ci
18885b47fbSopenharmony_ci#include <cinttypes>
19885b47fbSopenharmony_ci#include <new>
20885b47fbSopenharmony_ci#include <string>
21885b47fbSopenharmony_ci#include <unistd.h>
22885b47fbSopenharmony_ci#include <functional>
23885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
24885b47fbSopenharmony_ci#include <hitrace_meter.h>
25885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
26885b47fbSopenharmony_ci
27885b47fbSopenharmony_ci#include "ability_info.h"
28885b47fbSopenharmony_ci#include "accessibility_event_info.h"
29885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POWER_MANAGER
30885b47fbSopenharmony_ci#include "accessibility_power_manager.h"
31885b47fbSopenharmony_ci#endif
32885b47fbSopenharmony_ci#include "accessibility_short_key_dialog.h"
33885b47fbSopenharmony_ci#include "accessibility_window_manager.h"
34885b47fbSopenharmony_ci#include "hilog_wrapper.h"
35885b47fbSopenharmony_ci#include "input_manager.h"
36885b47fbSopenharmony_ci#include "iservice_registry.h"
37885b47fbSopenharmony_ci#include "os_account_manager.h"
38885b47fbSopenharmony_ci#include "parameter.h"
39885b47fbSopenharmony_ci#include "system_ability_definition.h"
40885b47fbSopenharmony_ci#include "utils.h"
41885b47fbSopenharmony_ci#include "xcollie_helper.h"
42885b47fbSopenharmony_ci#include <ipc_skeleton.h>
43885b47fbSopenharmony_ci#include "transaction/rs_interfaces.h"
44885b47fbSopenharmony_ci
45885b47fbSopenharmony_ciusing namespace std;
46885b47fbSopenharmony_ci
47885b47fbSopenharmony_cinamespace OHOS {
48885b47fbSopenharmony_cinamespace Accessibility {
49885b47fbSopenharmony_cinamespace {
50885b47fbSopenharmony_ci    const std::string AAMS_SERVICE_NAME = "AccessibleAbilityManagerService";
51885b47fbSopenharmony_ci    const std::string AAMS_ACTION_RUNNER_NAME = "AamsActionRunner";
52885b47fbSopenharmony_ci    const std::string UI_TEST_BUNDLE_NAME = "ohos.uitest";
53885b47fbSopenharmony_ci    const std::string UI_TEST_ABILITY_NAME = "uitestability";
54885b47fbSopenharmony_ci    const std::string SYSTEM_PARAMETER_AAMS_NAME = "accessibility.config.ready";
55885b47fbSopenharmony_ci    const std::string GRAPHIC_ANIMATION_SCALE_NAME = "persist.sys.graphic.animationscale";
56885b47fbSopenharmony_ci    const std::string ARKUI_ANIMATION_SCALE_NAME = "persist.sys.arkui.animationscale";
57885b47fbSopenharmony_ci    const std::string SCREEN_READER_BUNDLE_ABILITY_NAME = "com.huawei.hmos.screenreader/AccessibilityExtAbility";
58885b47fbSopenharmony_ci    const std::string DEVICE_PROVISIONED = "device_provisioned";
59885b47fbSopenharmony_ci    const std::string SCREEN_MAGNIFICATION_KEY = "accessibility_display_magnification_enabled";
60885b47fbSopenharmony_ci    const std::string SCREEN_MAGNIFICATION_TYPE = "accessibility_magnification_capability";
61885b47fbSopenharmony_ci    const std::string DELAY_UNLOAD_TASK = "TASK_UNLOAD_ACCESSIBILITY_SA";
62885b47fbSopenharmony_ci    const std::string USER_SETUP_COMPLETED = "user_setup_complete";
63885b47fbSopenharmony_ci    const std::string ACCESSIBILITY_CLONE_FLAG = "accessibility_config_clone";
64885b47fbSopenharmony_ci    const std::string SHORTCUT_ENABLED = "accessibility_shortcut_enabled";
65885b47fbSopenharmony_ci    constexpr int32_t INVALID_SHORTCUT_STATE = 2;
66885b47fbSopenharmony_ci    constexpr int32_t QUERY_USER_ID_RETRY_COUNT = 600;
67885b47fbSopenharmony_ci    constexpr int32_t QUERY_USER_ID_SLEEP_TIME = 50;
68885b47fbSopenharmony_ci    constexpr uint32_t TIME_OUT_OPERATOR = 5000;
69885b47fbSopenharmony_ci    constexpr int32_t REQUEST_ID_MAX = 0xFFFFFFFF;
70885b47fbSopenharmony_ci    constexpr int32_t REQUEST_ID_MIN = 0x0000FFFF;
71885b47fbSopenharmony_ci    constexpr int32_t DEFAULT_ACCOUNT_ID = 100;
72885b47fbSopenharmony_ci    constexpr int32_t ROOT_UID = 0;
73885b47fbSopenharmony_ci    constexpr int32_t UNLOAD_TASK_INTERNAL = 3 * 60 * 1000; // ms
74885b47fbSopenharmony_ci    constexpr int32_t TREE_ID_INVALID = 0;
75885b47fbSopenharmony_ci    constexpr uint32_t ELEMENT_MOVE_BIT = 40;
76885b47fbSopenharmony_ci    constexpr int32_t SINGLE_TREE_ID = 0;
77885b47fbSopenharmony_ci    constexpr int32_t TREE_ID_MAX = 0x00001FFF;
78885b47fbSopenharmony_ci    constexpr int32_t SHORT_KEY_TIMEOUT_BEFORE_USE = 3000; // ms
79885b47fbSopenharmony_ci    constexpr int32_t SHORT_KEY_TIMEOUT_AFTER_USE = 1000; // ms
80885b47fbSopenharmony_ci    constexpr int32_t WINDOW_ID_INVALID = -1;
81885b47fbSopenharmony_ci    constexpr int64_t ELEMENT_ID_INVALID = -1;
82885b47fbSopenharmony_ci    enum SCREENREADER_STATE : int32_t {
83885b47fbSopenharmony_ci        UNINIT = -1,
84885b47fbSopenharmony_ci        OFF = 0,
85885b47fbSopenharmony_ci        ON = 1,
86885b47fbSopenharmony_ci    };
87885b47fbSopenharmony_ci    const std::string TIMER_REGISTER_STATE_OBSERVER = "accessibility:registerStateObServer";
88885b47fbSopenharmony_ci    const std::string TIMER_REGISTER_CAPTION_OBSERVER = "accessibility:registerCaptionObServer";
89885b47fbSopenharmony_ci    const std::string TIMER_REGISTER_ENABLEABILITY_OBSERVER = "accessibility:registerEnableAbilityObServer";
90885b47fbSopenharmony_ci    const std::string TIMER_GET_ALL_CONFIG = "accessibility:getAllConfig";
91885b47fbSopenharmony_ci    const std::string TIMER_REGISTER_CONFIG_OBSERVER = "accessibility:registerConfigObserver";
92885b47fbSopenharmony_ci    constexpr int32_t XCOLLIE_TIMEOUT = 6; // s
93885b47fbSopenharmony_ci} // namespace
94885b47fbSopenharmony_ci
95885b47fbSopenharmony_ciconst bool REGISTER_RESULT =
96885b47fbSopenharmony_ci    SystemAbility::MakeAndRegisterAbility(&Singleton<AccessibleAbilityManagerService>::GetInstance());
97885b47fbSopenharmony_ci
98885b47fbSopenharmony_ciAccessibleAbilityManagerService::AccessibleAbilityManagerService()
99885b47fbSopenharmony_ci    : SystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID, true)
100885b47fbSopenharmony_ci{
101885b47fbSopenharmony_ci    HILOG_INFO("AccessibleAbilityManagerService is constructed");
102885b47fbSopenharmony_ci    dependentServicesStatus_[ABILITY_MGR_SERVICE_ID] = false;
103885b47fbSopenharmony_ci    dependentServicesStatus_[BUNDLE_MGR_SERVICE_SYS_ABILITY_ID] = false;
104885b47fbSopenharmony_ci    dependentServicesStatus_[COMMON_EVENT_SERVICE_ID] = false;
105885b47fbSopenharmony_ci    dependentServicesStatus_[DISPLAY_MANAGER_SERVICE_SA_ID] = false;
106885b47fbSopenharmony_ci    dependentServicesStatus_[SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN] = false;
107885b47fbSopenharmony_ci    dependentServicesStatus_[WINDOW_MANAGER_SERVICE_ID] = false;
108885b47fbSopenharmony_ci    dependentServicesStatus_[DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID] = false;
109885b47fbSopenharmony_ci
110885b47fbSopenharmony_ci    accessibilitySettings_ = std::make_shared<AccessibilitySettings>();
111885b47fbSopenharmony_ci    accessibilityShortKey_ = std::make_shared<AccessibilityShortKey>();
112885b47fbSopenharmony_ci}
113885b47fbSopenharmony_ci
114885b47fbSopenharmony_ciAccessibleAbilityManagerService::~AccessibleAbilityManagerService()
115885b47fbSopenharmony_ci{
116885b47fbSopenharmony_ci    HILOG_INFO("AccessibleAbilityManagerService::~AccessibleAbilityManagerService");
117885b47fbSopenharmony_ci
118885b47fbSopenharmony_ci    inputInterceptor_ = nullptr;
119885b47fbSopenharmony_ci    touchEventInjector_ = nullptr;
120885b47fbSopenharmony_ci    keyEventFilter_ = nullptr;
121885b47fbSopenharmony_ci    a11yAccountsData_.Clear();
122885b47fbSopenharmony_ci}
123885b47fbSopenharmony_ci
124885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnStart()
125885b47fbSopenharmony_ci{
126885b47fbSopenharmony_ci    HILOG_INFO("AccessibleAbilityManagerService::OnStart start");
127885b47fbSopenharmony_ci    if (!runner_) {
128885b47fbSopenharmony_ci        runner_ = AppExecFwk::EventRunner::Create(AAMS_SERVICE_NAME, AppExecFwk::ThreadMode::FFRT);
129885b47fbSopenharmony_ci        if (!runner_) {
130885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityManagerService::OnStart failed:create AAMS runner failed");
131885b47fbSopenharmony_ci            return;
132885b47fbSopenharmony_ci        }
133885b47fbSopenharmony_ci    }
134885b47fbSopenharmony_ci
135885b47fbSopenharmony_ci    if (!handler_) {
136885b47fbSopenharmony_ci        handler_ = std::make_shared<AAMSEventHandler>(runner_);
137885b47fbSopenharmony_ci        if (!handler_) {
138885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityManagerService::OnStart failed:create AAMS event handler failed");
139885b47fbSopenharmony_ci            return;
140885b47fbSopenharmony_ci        }
141885b47fbSopenharmony_ci    }
142885b47fbSopenharmony_ci
143885b47fbSopenharmony_ci    if (!actionRunner_) {
144885b47fbSopenharmony_ci        actionRunner_ = AppExecFwk::EventRunner::Create(AAMS_ACTION_RUNNER_NAME, AppExecFwk::ThreadMode::FFRT);
145885b47fbSopenharmony_ci        if (!actionRunner_) {
146885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityManagerService::OnStart failed:create AAMS action runner failed");
147885b47fbSopenharmony_ci            return;
148885b47fbSopenharmony_ci        }
149885b47fbSopenharmony_ci    }
150885b47fbSopenharmony_ci
151885b47fbSopenharmony_ci    if (!actionHandler_) {
152885b47fbSopenharmony_ci        actionHandler_ = std::make_shared<AAMSEventHandler>(actionRunner_);
153885b47fbSopenharmony_ci        if (!actionHandler_) {
154885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityManagerService::OnStart failed:create AAMS action handler failed");
155885b47fbSopenharmony_ci            return;
156885b47fbSopenharmony_ci        }
157885b47fbSopenharmony_ci    }
158885b47fbSopenharmony_ci
159885b47fbSopenharmony_ci    SetParameter(SYSTEM_PARAMETER_AAMS_NAME.c_str(), "false");
160885b47fbSopenharmony_ci
161885b47fbSopenharmony_ci    HILOG_DEBUG("AddAbilityListener!");
162885b47fbSopenharmony_ci    AddSystemAbilityListener(ABILITY_MGR_SERVICE_ID);
163885b47fbSopenharmony_ci    AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
164885b47fbSopenharmony_ci    AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
165885b47fbSopenharmony_ci    AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
166885b47fbSopenharmony_ci    AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
167885b47fbSopenharmony_ci    AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
168885b47fbSopenharmony_ci    AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
169885b47fbSopenharmony_ci
170885b47fbSopenharmony_ci    accessibilitySettings_->RegisterSettingsHandler(handler_);
171885b47fbSopenharmony_ci}
172885b47fbSopenharmony_ci
173885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnStop()
174885b47fbSopenharmony_ci{
175885b47fbSopenharmony_ci    HILOG_INFO("stop AccessibleAbilityManagerService");
176885b47fbSopenharmony_ci    if (!handler_) {
177885b47fbSopenharmony_ci        HILOG_ERROR("AccessibleAbilityManagerService::OnStop failed!");
178885b47fbSopenharmony_ci        return;
179885b47fbSopenharmony_ci    }
180885b47fbSopenharmony_ci
181885b47fbSopenharmony_ci    ffrt::promise<void> syncPromise;
182885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
183885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise]() {
184885b47fbSopenharmony_ci        HILOG_DEBUG();
185885b47fbSopenharmony_ci
186885b47fbSopenharmony_ci        Singleton<AccessibilityCommonEvent>::GetInstance().UnSubscriberEvent();
187885b47fbSopenharmony_ci        Singleton<AccessibilityDisplayManager>::GetInstance().UnregisterDisplayListener();
188885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().DeregisterWindowListener();
189885b47fbSopenharmony_ci
190885b47fbSopenharmony_ci        currentAccountId_ = -1;
191885b47fbSopenharmony_ci        a11yAccountsData_.Clear();
192885b47fbSopenharmony_ci        stateObservers_.Clear();
193885b47fbSopenharmony_ci        bundleManager_ = nullptr;
194885b47fbSopenharmony_ci        inputInterceptor_ = nullptr;
195885b47fbSopenharmony_ci        touchEventInjector_ = nullptr;
196885b47fbSopenharmony_ci        keyEventFilter_ = nullptr;
197885b47fbSopenharmony_ci        stateObserversDeathRecipient_ = nullptr;
198885b47fbSopenharmony_ci        bundleManagerDeathRecipient_ = nullptr;
199885b47fbSopenharmony_ci
200885b47fbSopenharmony_ci        syncPromise.set_value();
201885b47fbSopenharmony_ci        }, "TASK_ONSTOP");
202885b47fbSopenharmony_ci    syncFuture.wait();
203885b47fbSopenharmony_ci
204885b47fbSopenharmony_ci    for (auto &iter : dependentServicesStatus_) {
205885b47fbSopenharmony_ci        iter.second = false;
206885b47fbSopenharmony_ci    }
207885b47fbSopenharmony_ci
208885b47fbSopenharmony_ci    isReady_ = false;
209885b47fbSopenharmony_ci    isPublished_ = false;
210885b47fbSopenharmony_ci    SetParameter(SYSTEM_PARAMETER_AAMS_NAME.c_str(), "false");
211885b47fbSopenharmony_ci    HILOG_INFO("AccessibleAbilityManagerService::OnStop OK.");
212885b47fbSopenharmony_ci}
213885b47fbSopenharmony_ci
214885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
215885b47fbSopenharmony_ci{
216885b47fbSopenharmony_ci    HILOG_DEBUG("systemAbilityId:%{public}d added!", systemAbilityId);
217885b47fbSopenharmony_ci    if (!handler_) {
218885b47fbSopenharmony_ci        HILOG_DEBUG("Event handler is nullptr.");
219885b47fbSopenharmony_ci        return;
220885b47fbSopenharmony_ci    }
221885b47fbSopenharmony_ci
222885b47fbSopenharmony_ci    handler_->PostTask([=]() {
223885b47fbSopenharmony_ci        auto iter = dependentServicesStatus_.find(systemAbilityId);
224885b47fbSopenharmony_ci        if (iter == dependentServicesStatus_.end()) {
225885b47fbSopenharmony_ci            HILOG_ERROR("SystemAbilityId is not found!");
226885b47fbSopenharmony_ci            return;
227885b47fbSopenharmony_ci        }
228885b47fbSopenharmony_ci
229885b47fbSopenharmony_ci        dependentServicesStatus_[systemAbilityId] = true;
230885b47fbSopenharmony_ci        if (std::any_of(dependentServicesStatus_.begin(), dependentServicesStatus_.end(),
231885b47fbSopenharmony_ci            [](const std::map<int32_t, bool>::value_type &status) { return !status.second; })) {
232885b47fbSopenharmony_ci            HILOG_DEBUG("Not all the dependence is ready!");
233885b47fbSopenharmony_ci            return;
234885b47fbSopenharmony_ci        }
235885b47fbSopenharmony_ci
236885b47fbSopenharmony_ci        if (Init() == false) {
237885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityManagerService::Init failed!");
238885b47fbSopenharmony_ci            return;
239885b47fbSopenharmony_ci        }
240885b47fbSopenharmony_ci
241885b47fbSopenharmony_ci        if (!isPublished_) {
242885b47fbSopenharmony_ci            if (Publish(this) == false) {
243885b47fbSopenharmony_ci                HILOG_ERROR("AccessibleAbilityManagerService::Publish failed!");
244885b47fbSopenharmony_ci                return;
245885b47fbSopenharmony_ci            }
246885b47fbSopenharmony_ci            isPublished_ = true;
247885b47fbSopenharmony_ci        }
248885b47fbSopenharmony_ci
249885b47fbSopenharmony_ci        InitInnerResource();
250885b47fbSopenharmony_ci
251885b47fbSopenharmony_ci        isReady_ = true;
252885b47fbSopenharmony_ci        SetParameter(SYSTEM_PARAMETER_AAMS_NAME.c_str(), "true");
253885b47fbSopenharmony_ci        HILOG_DEBUG("AAMS is ready!");
254885b47fbSopenharmony_ci        RegisterShortKeyEvent();
255885b47fbSopenharmony_ci        PostDelayUnloadTask();
256885b47fbSopenharmony_ci        RegisterScreenMagnificationState();
257885b47fbSopenharmony_ci        RegisterScreenMagnificationType();
258885b47fbSopenharmony_ci        }, "OnAddSystemAbility");
259885b47fbSopenharmony_ci}
260885b47fbSopenharmony_ci
261885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
262885b47fbSopenharmony_ci{
263885b47fbSopenharmony_ci    HILOG_INFO("systemAbilityId:%{public}d removed!", systemAbilityId);
264885b47fbSopenharmony_ci    if (!handler_) {
265885b47fbSopenharmony_ci        HILOG_DEBUG("Event handler is nullptr.");
266885b47fbSopenharmony_ci        return;
267885b47fbSopenharmony_ci    }
268885b47fbSopenharmony_ci
269885b47fbSopenharmony_ci    handler_->PostTask([=]() {
270885b47fbSopenharmony_ci        HILOG_INFO("Remove system ability start");
271885b47fbSopenharmony_ci        auto iter = dependentServicesStatus_.find(systemAbilityId);
272885b47fbSopenharmony_ci        if (iter == dependentServicesStatus_.end()) {
273885b47fbSopenharmony_ci            HILOG_ERROR("SystemAbilityId is not found!");
274885b47fbSopenharmony_ci            return;
275885b47fbSopenharmony_ci        }
276885b47fbSopenharmony_ci
277885b47fbSopenharmony_ci        dependentServicesStatus_[systemAbilityId] = false;
278885b47fbSopenharmony_ci        if (isReady_) {
279885b47fbSopenharmony_ci            SwitchedUser(-1);
280885b47fbSopenharmony_ci            Singleton<AccessibilityCommonEvent>::GetInstance().UnSubscriberEvent();
281885b47fbSopenharmony_ci            Singleton<AccessibilityDisplayManager>::GetInstance().UnregisterDisplayListener();
282885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().DeregisterWindowListener();
283885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().DeInit();
284885b47fbSopenharmony_ci
285885b47fbSopenharmony_ci            isReady_ = false;
286885b47fbSopenharmony_ci            SetParameter(SYSTEM_PARAMETER_AAMS_NAME.c_str(), "false");
287885b47fbSopenharmony_ci        }
288885b47fbSopenharmony_ci        }, "OnRemoveSystemAbility");
289885b47fbSopenharmony_ci}
290885b47fbSopenharmony_ci
291885b47fbSopenharmony_ciint AccessibleAbilityManagerService::Dump(int fd, const std::vector<std::u16string>& args)
292885b47fbSopenharmony_ci{
293885b47fbSopenharmony_ci    HILOG_DEBUG("dump AccessibilityManagerServiceInfo");
294885b47fbSopenharmony_ci    if (!handler_) {
295885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed!");
296885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
297885b47fbSopenharmony_ci    }
298885b47fbSopenharmony_ci    ffrt::promise<int> syncPromise;
299885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
300885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise, fd, args]() {
301885b47fbSopenharmony_ci        if (!accessibilityDumper_) {
302885b47fbSopenharmony_ci            accessibilityDumper_ = new(std::nothrow) AccessibilityDumper();
303885b47fbSopenharmony_ci            if (!accessibilityDumper_) {
304885b47fbSopenharmony_ci                HILOG_ERROR("accessibilityDumper_ is nullptr");
305885b47fbSopenharmony_ci                syncPromise.set_value(-1);
306885b47fbSopenharmony_ci                return;
307885b47fbSopenharmony_ci            }
308885b47fbSopenharmony_ci        }
309885b47fbSopenharmony_ci        syncPromise.set_value(accessibilityDumper_->Dump(fd, args));
310885b47fbSopenharmony_ci        }, "TASK_DUMP_INFO");
311885b47fbSopenharmony_ci    return syncFuture.get();
312885b47fbSopenharmony_ci}
313885b47fbSopenharmony_ci
314885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::VerifyingToKenId(const int32_t windowId, const int64_t elementId)
315885b47fbSopenharmony_ci{
316885b47fbSopenharmony_ci    uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
317885b47fbSopenharmony_ci    int32_t treeId = (static_cast<uint64_t>(elementId) >> ELEMENT_MOVE_BIT);
318885b47fbSopenharmony_ci    HILOG_DEBUG("VerifyingToKenId: treeId[%{public}d], windowId[%{public}d], elementId[%{public}" PRId64 "]",
319885b47fbSopenharmony_ci        treeId, windowId, elementId);
320885b47fbSopenharmony_ci    if (elementId == ELEMENT_ID_INVALID || windowId == WINDOW_ID_INVALID) {
321885b47fbSopenharmony_ci        HILOG_DEBUG("windowId[%{public}d], elementId[%{public}" PRId64 "]", windowId, elementId);
322885b47fbSopenharmony_ci        return RET_OK;
323885b47fbSopenharmony_ci    }
324885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
325885b47fbSopenharmony_ci    if (accountData == nullptr) {
326885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
327885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
328885b47fbSopenharmony_ci            HILOG_ERROR("Get current account data failed!!");
329885b47fbSopenharmony_ci            return RET_ERR_CONNECTION_EXIST;
330885b47fbSopenharmony_ci    }
331885b47fbSopenharmony_ci    HILOG_DEBUG("treeId %{public}d, windowId %{public}d", treeId, windowId);
332885b47fbSopenharmony_ci    int32_t realId =
333885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().ConvertToRealWindowId(windowId, FOCUS_TYPE_INVALID);
334885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = accountData->GetAccessibilityWindowConnection(realId);
335885b47fbSopenharmony_ci    if (connection == nullptr) {
336885b47fbSopenharmony_ci        HILOG_ERROR("connection is empty.");
337885b47fbSopenharmony_ci        return RET_ERR_REGISTER_EXIST;
338885b47fbSopenharmony_ci    }
339885b47fbSopenharmony_ci    uint32_t expectTokenId = connection->GetTokenIdMap(treeId);
340885b47fbSopenharmony_ci    if (tokenId != expectTokenId) {
341885b47fbSopenharmony_ci        HILOG_DEBUG("tokenId error!");
342885b47fbSopenharmony_ci        return RET_ERR_TOKEN_ID;
343885b47fbSopenharmony_ci    }
344885b47fbSopenharmony_ci
345885b47fbSopenharmony_ci    return RET_OK;
346885b47fbSopenharmony_ci}
347885b47fbSopenharmony_ci
348885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SendEvent(const AccessibilityEventInfo &uiEvent, const int32_t flag)
349885b47fbSopenharmony_ci{
350885b47fbSopenharmony_ci    HILOG_DEBUG("eventType[%{public}d] gestureId[%{public}d] windowId[%{public}d] compnentId: %{public}" PRId64 " "
351885b47fbSopenharmony_ci        "elementId: %{public}" PRId64 " winId: %{public}d treeId: %{public}d",
352885b47fbSopenharmony_ci        uiEvent.GetEventType(), uiEvent.GetGestureType(), uiEvent.GetWindowId(), uiEvent.GetAccessibilityId(),
353885b47fbSopenharmony_ci        uiEvent.GetElementInfo().GetAccessibilityId(),
354885b47fbSopenharmony_ci        uiEvent.GetElementInfo().GetWindowId(), uiEvent.GetElementInfo().GetBelongTreeId());
355885b47fbSopenharmony_ci    if (!handler_) {
356885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed!");
357885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
358885b47fbSopenharmony_ci    }
359885b47fbSopenharmony_ci    if (flag) {
360885b47fbSopenharmony_ci        if (VerifyingToKenId(uiEvent.GetElementInfo().GetWindowId(),
361885b47fbSopenharmony_ci            uiEvent.GetElementInfo().GetAccessibilityId()) == RET_OK) {
362885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId ok");
363885b47fbSopenharmony_ci        } else {
364885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId failed");
365885b47fbSopenharmony_ci            return RET_ERR_CONNECTION_EXIST;
366885b47fbSopenharmony_ci        }
367885b47fbSopenharmony_ci    }
368885b47fbSopenharmony_ci
369885b47fbSopenharmony_ci    UpdateAccessibilityWindowStateByEvent(uiEvent);
370885b47fbSopenharmony_ci    handler_->PostTask([this, uiEvent]() {
371885b47fbSopenharmony_ci        HILOG_DEBUG();
372885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
373885b47fbSopenharmony_ci        if (!accountData) {
374885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr.");
375885b47fbSopenharmony_ci            return;
376885b47fbSopenharmony_ci        }
377885b47fbSopenharmony_ci
378885b47fbSopenharmony_ci        const_cast<AccessibilityEventInfo&>(uiEvent).SetTimeStamp(Utils::GetSystemTime());
379885b47fbSopenharmony_ci        map<string, sptr<AccessibleAbilityConnection>> abilities = accountData->GetConnectedA11yAbilities();
380885b47fbSopenharmony_ci        for (auto &ability : abilities) {
381885b47fbSopenharmony_ci            if (ability.second) {
382885b47fbSopenharmony_ci                ability.second->OnAccessibilityEvent(const_cast<AccessibilityEventInfo&>(uiEvent));
383885b47fbSopenharmony_ci            }
384885b47fbSopenharmony_ci        }
385885b47fbSopenharmony_ci        }, "TASK_SEND_EVENT");
386885b47fbSopenharmony_ci    return RET_OK;
387885b47fbSopenharmony_ci}
388885b47fbSopenharmony_ci
389885b47fbSopenharmony_ciuint32_t AccessibleAbilityManagerService::RegisterStateObserver(
390885b47fbSopenharmony_ci    const sptr<IAccessibleAbilityManagerStateObserver>& stateObserver)
391885b47fbSopenharmony_ci{
392885b47fbSopenharmony_ci    HILOG_DEBUG();
393885b47fbSopenharmony_ci    if (!stateObserver || !handler_) {
394885b47fbSopenharmony_ci        HILOG_ERROR("parameters check failed!");
395885b47fbSopenharmony_ci        return 0;
396885b47fbSopenharmony_ci    }
397885b47fbSopenharmony_ci    XCollieHelper timer(TIMER_REGISTER_STATE_OBSERVER, XCOLLIE_TIMEOUT);
398885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(mutex_);
399885b47fbSopenharmony_ci    if (!stateObserversDeathRecipient_) {
400885b47fbSopenharmony_ci        stateObserversDeathRecipient_ = new(std::nothrow) StateCallbackDeathRecipient();
401885b47fbSopenharmony_ci        if (!stateObserversDeathRecipient_) {
402885b47fbSopenharmony_ci            HILOG_ERROR("stateObserversDeathRecipient_ is null");
403885b47fbSopenharmony_ci            return 0;
404885b47fbSopenharmony_ci        }
405885b47fbSopenharmony_ci    }
406885b47fbSopenharmony_ci
407885b47fbSopenharmony_ci    if (!stateObserver->AsObject()) {
408885b47fbSopenharmony_ci        HILOG_ERROR("object is null");
409885b47fbSopenharmony_ci        return 0;
410885b47fbSopenharmony_ci    }
411885b47fbSopenharmony_ci
412885b47fbSopenharmony_ci    stateObserver->AsObject()->AddDeathRecipient(stateObserversDeathRecipient_);
413885b47fbSopenharmony_ci    stateObservers_.AddStateObserver(stateObserver);
414885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
415885b47fbSopenharmony_ci    if (accountData == nullptr) {
416885b47fbSopenharmony_ci        return 0;
417885b47fbSopenharmony_ci    }
418885b47fbSopenharmony_ci
419885b47fbSopenharmony_ci    return accountData->GetAccessibilityState();
420885b47fbSopenharmony_ci}
421885b47fbSopenharmony_ci
422885b47fbSopenharmony_civoid AccessibleAbilityManagerService::GetRealWindowAndElementId(int32_t& windowId, int64_t& elementId)
423885b47fbSopenharmony_ci{
424885b47fbSopenharmony_ci    HILOG_DEBUG("real windowId %{public}d", windowId);
425885b47fbSopenharmony_ci    if (!handler_) {
426885b47fbSopenharmony_ci        return;
427885b47fbSopenharmony_ci    }
428885b47fbSopenharmony_ci
429885b47fbSopenharmony_ci    ffrt::promise<void> syncPromise;
430885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
431885b47fbSopenharmony_ci    handler_->PostTask([&, this]() {
432885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().GetRealWindowAndElementId(windowId, elementId);
433885b47fbSopenharmony_ci        syncPromise.set_value();
434885b47fbSopenharmony_ci        }, "GET_REAL_WINDOW_AND_ELEMENT_ID");
435885b47fbSopenharmony_ci    return syncFuture.get();
436885b47fbSopenharmony_ci}
437885b47fbSopenharmony_ci
438885b47fbSopenharmony_civoid AccessibleAbilityManagerService::GetSceneBoardInnerWinId(int32_t windowId, int64_t elementId,
439885b47fbSopenharmony_ci    int32_t& innerWid)
440885b47fbSopenharmony_ci{
441885b47fbSopenharmony_ci    HILOG_DEBUG("real windowId %{public}d", windowId);
442885b47fbSopenharmony_ci    if (!handler_) {
443885b47fbSopenharmony_ci        return;
444885b47fbSopenharmony_ci    }
445885b47fbSopenharmony_ci
446885b47fbSopenharmony_ci    ffrt::promise<void> syncPromise;
447885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
448885b47fbSopenharmony_ci    handler_->PostTask([&, this]() {
449885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().GetSceneBoardInnerWinId(windowId, elementId, innerWid);
450885b47fbSopenharmony_ci        syncPromise.set_value();
451885b47fbSopenharmony_ci        }, "GET_SCENE_BOARD_INNER_WINDOW_ID");
452885b47fbSopenharmony_ci    return syncFuture.get();
453885b47fbSopenharmony_ci}
454885b47fbSopenharmony_ci
455885b47fbSopenharmony_cisptr<AccessibilityWindowConnection> AccessibleAbilityManagerService::GetRealIdConnection()
456885b47fbSopenharmony_ci{
457885b47fbSopenharmony_ci    HILOG_DEBUG();
458885b47fbSopenharmony_ci    int32_t windowId = ANY_WINDOW_ID;
459885b47fbSopenharmony_ci    int32_t focusType = FOCUS_TYPE_ACCESSIBILITY;
460885b47fbSopenharmony_ci    int32_t realId = Singleton<AccessibilityWindowManager>::GetInstance().ConvertToRealWindowId(windowId, focusType);
461885b47fbSopenharmony_ci
462885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
463885b47fbSopenharmony_ci    if (!accountData) {
464885b47fbSopenharmony_ci        HILOG_ERROR("GetCurrentAccountData failed");
465885b47fbSopenharmony_ci        return sptr<AccessibilityWindowConnection>();
466885b47fbSopenharmony_ci    }
467885b47fbSopenharmony_ci    return accountData->GetAccessibilityWindowConnection(realId);
468885b47fbSopenharmony_ci}
469885b47fbSopenharmony_ci
470885b47fbSopenharmony_cibool AccessibleAbilityManagerService::FindFocusedElementByConnection(sptr<AccessibilityWindowConnection> connection,
471885b47fbSopenharmony_ci    AccessibilityElementInfo &elementInfo)
472885b47fbSopenharmony_ci{
473885b47fbSopenharmony_ci    HILOG_DEBUG();
474885b47fbSopenharmony_ci    int64_t elementId = -1;
475885b47fbSopenharmony_ci    int32_t focusType = FOCUS_TYPE_ACCESSIBILITY;
476885b47fbSopenharmony_ci    if (!connection || !connection->GetProxy()) {
477885b47fbSopenharmony_ci        HILOG_ERROR("GetAccessibilityWindowConnection failed");
478885b47fbSopenharmony_ci        return false;
479885b47fbSopenharmony_ci    }
480885b47fbSopenharmony_ci
481885b47fbSopenharmony_ci    uint32_t timeOut = 5000;
482885b47fbSopenharmony_ci    sptr<ElementOperatorCallbackImpl> focusCallback = new(std::nothrow) ElementOperatorCallbackImpl();
483885b47fbSopenharmony_ci    if (!focusCallback) {
484885b47fbSopenharmony_ci        HILOG_ERROR("Failed to create focusCallback.");
485885b47fbSopenharmony_ci        return false;
486885b47fbSopenharmony_ci    }
487885b47fbSopenharmony_ci    ffrt::future<void> focusFuture = focusCallback->promise_.get_future();
488885b47fbSopenharmony_ci    connection->GetProxy()->FindFocusedElementInfo(elementId, focusType, GenerateRequestId(), focusCallback);
489885b47fbSopenharmony_ci    ffrt::future_status waitFocus = focusFuture.wait_for(std::chrono::milliseconds(timeOut));
490885b47fbSopenharmony_ci    if (waitFocus != ffrt::future_status::ready) {
491885b47fbSopenharmony_ci        HILOG_ERROR("FindFocusedElementInfo Failed to wait result");
492885b47fbSopenharmony_ci        return false;
493885b47fbSopenharmony_ci    }
494885b47fbSopenharmony_ci    elementInfo = focusCallback->accessibilityInfoResult_;
495885b47fbSopenharmony_ci    return true;
496885b47fbSopenharmony_ci}
497885b47fbSopenharmony_ci
498885b47fbSopenharmony_cibool AccessibleAbilityManagerService::FindFocusedElement(AccessibilityElementInfo &elementInfo)
499885b47fbSopenharmony_ci{
500885b47fbSopenharmony_ci    HILOG_DEBUG();
501885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = GetRealIdConnection();
502885b47fbSopenharmony_ci    FindFocusedElementByConnection(connection, elementInfo);
503885b47fbSopenharmony_ci    if (elementInfo.GetAccessibilityId() >= 0) {
504885b47fbSopenharmony_ci        HILOG_DEBUG("find focused element success.");
505885b47fbSopenharmony_ci        return true;
506885b47fbSopenharmony_ci    }
507885b47fbSopenharmony_ci    int32_t windowId = GetFocusWindowId();
508885b47fbSopenharmony_ci    int64_t elementId = GetFocusElementId();
509885b47fbSopenharmony_ci    sptr<IAccessibilityElementOperator> elementOperator = nullptr;
510885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
511885b47fbSopenharmony_ci    if (accountData == nullptr) {
512885b47fbSopenharmony_ci        HILOG_ERROR("GetCurrentAccountData failed");
513885b47fbSopenharmony_ci        return false;
514885b47fbSopenharmony_ci    }
515885b47fbSopenharmony_ci    connection = accountData->GetAccessibilityWindowConnection(windowId);
516885b47fbSopenharmony_ci    HILOG_DEBUG("windowId[%{public}d], elementId[%{public}" PRId64 "]", windowId, elementId);
517885b47fbSopenharmony_ci    if (connection == nullptr) {
518885b47fbSopenharmony_ci        HILOG_ERROR("connection is nullptr");
519885b47fbSopenharmony_ci        return false;
520885b47fbSopenharmony_ci    }
521885b47fbSopenharmony_ci    sptr<ElementOperatorCallbackImpl> callBack = new(std::nothrow) ElementOperatorCallbackImpl();
522885b47fbSopenharmony_ci    if (callBack == nullptr) {
523885b47fbSopenharmony_ci        HILOG_ERROR("Failed to create callBack.");
524885b47fbSopenharmony_ci        return false;
525885b47fbSopenharmony_ci    }
526885b47fbSopenharmony_ci    ffrt::future<void> promiseFuture = callBack->promise_.get_future();
527885b47fbSopenharmony_ci    GetElementOperatorConnection(connection, elementId, elementOperator);
528885b47fbSopenharmony_ci    if (elementOperator == nullptr) {
529885b47fbSopenharmony_ci        HILOG_ERROR("elementOperator is nullptr");
530885b47fbSopenharmony_ci        return false;
531885b47fbSopenharmony_ci    }
532885b47fbSopenharmony_ci    elementOperator->SearchElementInfoByAccessibilityId(elementId, GenerateRequestId(), callBack, 0);
533885b47fbSopenharmony_ci    ffrt::future_status waitFocus = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
534885b47fbSopenharmony_ci    if (waitFocus != ffrt::future_status::ready) {
535885b47fbSopenharmony_ci        ipcTimeoutNum_++;
536885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait result, number %{public}" PRId64 "", ipcTimeoutNum_);
537885b47fbSopenharmony_ci        return false;
538885b47fbSopenharmony_ci    }
539885b47fbSopenharmony_ci
540885b47fbSopenharmony_ci    if (callBack->elementInfosResult_.size() <= 0) {
541885b47fbSopenharmony_ci        HILOG_ERROR("SearchElementInfoByAccessibilityId return null");
542885b47fbSopenharmony_ci        return false;
543885b47fbSopenharmony_ci    }
544885b47fbSopenharmony_ci    elementInfo = callBack->elementInfosResult_[0];
545885b47fbSopenharmony_ci    return true;
546885b47fbSopenharmony_ci}
547885b47fbSopenharmony_ci
548885b47fbSopenharmony_civoid AccessibleAbilityManagerService::GetElementOperatorConnection(sptr<AccessibilityWindowConnection> &connection,
549885b47fbSopenharmony_ci    const int64_t elementId, sptr<IAccessibilityElementOperator> &elementOperator)
550885b47fbSopenharmony_ci{
551885b47fbSopenharmony_ci    int32_t treeId = 0;
552885b47fbSopenharmony_ci    if (elementId > 0) {
553885b47fbSopenharmony_ci        treeId = GetTreeIdBySplitElementId(elementId);
554885b47fbSopenharmony_ci        elementOperator = connection->GetCardProxy(treeId);
555885b47fbSopenharmony_ci    } else {
556885b47fbSopenharmony_ci        elementOperator = connection->GetProxy();
557885b47fbSopenharmony_ci    }
558885b47fbSopenharmony_ci    HILOG_DEBUG("elementId:%{public}" PRId64 " treeId:%{public}d", elementId, treeId);
559885b47fbSopenharmony_ci}
560885b47fbSopenharmony_ci
561885b47fbSopenharmony_cibool AccessibleAbilityManagerService::ExecuteActionOnAccessibilityFocused(const ActionType &action)
562885b47fbSopenharmony_ci{
563885b47fbSopenharmony_ci    HILOG_DEBUG();
564885b47fbSopenharmony_ci    int32_t windowId = GetFocusWindowId();
565885b47fbSopenharmony_ci    int64_t elementId = GetFocusElementId();
566885b47fbSopenharmony_ci    uint32_t timeOut = 5000;
567885b47fbSopenharmony_ci    int32_t treeId = GetTreeIdBySplitElementId(elementId);
568885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
569885b47fbSopenharmony_ci    if (accountData == nullptr) {
570885b47fbSopenharmony_ci        HILOG_ERROR("GetCurrentAccountData failed");
571885b47fbSopenharmony_ci        return false;
572885b47fbSopenharmony_ci    }
573885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = accountData->GetAccessibilityWindowConnection(windowId);
574885b47fbSopenharmony_ci    HILOG_DEBUG("windowId[%{public}d], elementId[%{public}" PRId64 "], action[%{public}d", windowId, elementId,
575885b47fbSopenharmony_ci        action);
576885b47fbSopenharmony_ci    if (connection == nullptr) {
577885b47fbSopenharmony_ci        HILOG_ERROR("connection is nullptr");
578885b47fbSopenharmony_ci        return false;
579885b47fbSopenharmony_ci    }
580885b47fbSopenharmony_ci    std::map<std::string, std::string> actionArguments {};
581885b47fbSopenharmony_ci    sptr<ElementOperatorCallbackImpl> actionCallback = new(std::nothrow) ElementOperatorCallbackImpl();
582885b47fbSopenharmony_ci    if (actionCallback == nullptr) {
583885b47fbSopenharmony_ci        HILOG_ERROR("Failed to create actionCallback.");
584885b47fbSopenharmony_ci        return false;
585885b47fbSopenharmony_ci    }
586885b47fbSopenharmony_ci    ffrt::future<void> actionFuture = actionCallback->promise_.get_future();
587885b47fbSopenharmony_ci    if (treeId > TREE_ID_INVALID) {
588885b47fbSopenharmony_ci        if (connection->GetCardProxy(treeId) != nullptr) {
589885b47fbSopenharmony_ci            connection->GetCardProxy(treeId)->ExecuteAction(elementId, action,
590885b47fbSopenharmony_ci                actionArguments, GenerateRequestId(), actionCallback);
591885b47fbSopenharmony_ci        } else {
592885b47fbSopenharmony_ci            HILOG_ERROR("get operation is nullptr");
593885b47fbSopenharmony_ci            return false;
594885b47fbSopenharmony_ci        }
595885b47fbSopenharmony_ci    } else {
596885b47fbSopenharmony_ci        if (connection->GetProxy() != nullptr) {
597885b47fbSopenharmony_ci            connection->GetProxy()->ExecuteAction(elementId, action, actionArguments, GenerateRequestId(),
598885b47fbSopenharmony_ci                actionCallback);
599885b47fbSopenharmony_ci        } else {
600885b47fbSopenharmony_ci            HILOG_ERROR("get operation is nullptr");
601885b47fbSopenharmony_ci            return false;
602885b47fbSopenharmony_ci        }
603885b47fbSopenharmony_ci    }
604885b47fbSopenharmony_ci    ffrt::future_status waitAction = actionFuture.wait_for(std::chrono::milliseconds(timeOut));
605885b47fbSopenharmony_ci    if (waitAction != ffrt::future_status::ready) {
606885b47fbSopenharmony_ci        HILOG_ERROR("ExecuteAction Failed to wait result");
607885b47fbSopenharmony_ci        return false;
608885b47fbSopenharmony_ci    }
609885b47fbSopenharmony_ci
610885b47fbSopenharmony_ci    return actionCallback->executeActionResult_;
611885b47fbSopenharmony_ci}
612885b47fbSopenharmony_ci
613885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SetFocusWindowId(const int32_t focusWindowId)
614885b47fbSopenharmony_ci{
615885b47fbSopenharmony_ci    focusWindowId_ = focusWindowId;
616885b47fbSopenharmony_ci}
617885b47fbSopenharmony_ci
618885b47fbSopenharmony_ciint32_t AccessibleAbilityManagerService::GetFocusWindowId()
619885b47fbSopenharmony_ci{
620885b47fbSopenharmony_ci    return focusWindowId_;
621885b47fbSopenharmony_ci}
622885b47fbSopenharmony_ci
623885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SetFocusElementId(const int64_t focusElementId)
624885b47fbSopenharmony_ci{
625885b47fbSopenharmony_ci    focusElementId_ = focusElementId;
626885b47fbSopenharmony_ci}
627885b47fbSopenharmony_ci
628885b47fbSopenharmony_ciint64_t AccessibleAbilityManagerService::GetFocusElementId()
629885b47fbSopenharmony_ci{
630885b47fbSopenharmony_ci    return focusElementId_;
631885b47fbSopenharmony_ci}
632885b47fbSopenharmony_ci
633885b47fbSopenharmony_ciuint32_t AccessibleAbilityManagerService::RegisterCaptionObserver(
634885b47fbSopenharmony_ci    const sptr<IAccessibleAbilityManagerCaptionObserver> &callback)
635885b47fbSopenharmony_ci{
636885b47fbSopenharmony_ci    HILOG_DEBUG();
637885b47fbSopenharmony_ci    if (!callback || !actionHandler_) {
638885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed!");
639885b47fbSopenharmony_ci        return ERR_INVALID_VALUE;
640885b47fbSopenharmony_ci    }
641885b47fbSopenharmony_ci
642885b47fbSopenharmony_ci    XCollieHelper timer(TIMER_REGISTER_CAPTION_OBSERVER, XCOLLIE_TIMEOUT);
643885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<uint32_t>> syncPromise = std::make_shared<ffrt::promise<uint32_t>>();
644885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
645885b47fbSopenharmony_ci    actionHandler_->PostTask([this, syncPromise, callback]() {
646885b47fbSopenharmony_ci        HILOG_DEBUG();
647885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
648885b47fbSopenharmony_ci        if (!accountData) {
649885b47fbSopenharmony_ci            HILOG_ERROR("Account data is null");
650885b47fbSopenharmony_ci            syncPromise->set_value(ERR_INVALID_VALUE);
651885b47fbSopenharmony_ci            return;
652885b47fbSopenharmony_ci        }
653885b47fbSopenharmony_ci        if (!captionPropertyCallbackDeathRecipient_) {
654885b47fbSopenharmony_ci            captionPropertyCallbackDeathRecipient_ = new(std::nothrow) CaptionPropertyCallbackDeathRecipient();
655885b47fbSopenharmony_ci            if (!captionPropertyCallbackDeathRecipient_) {
656885b47fbSopenharmony_ci                HILOG_ERROR("captionPropertyCallbackDeathRecipient_ is null");
657885b47fbSopenharmony_ci                syncPromise->set_value(ERR_INVALID_VALUE);
658885b47fbSopenharmony_ci                return;
659885b47fbSopenharmony_ci            }
660885b47fbSopenharmony_ci        }
661885b47fbSopenharmony_ci        if (!callback->AsObject()) {
662885b47fbSopenharmony_ci            HILOG_ERROR("object is null");
663885b47fbSopenharmony_ci            syncPromise->set_value(0);
664885b47fbSopenharmony_ci            return;
665885b47fbSopenharmony_ci        }
666885b47fbSopenharmony_ci        callback->AsObject()->AddDeathRecipient(captionPropertyCallbackDeathRecipient_);
667885b47fbSopenharmony_ci        accountData->AddCaptionPropertyCallback(callback);
668885b47fbSopenharmony_ci        HILOG_DEBUG("the size of caption property callbacks is %{public}zu",
669885b47fbSopenharmony_ci            accountData->GetCaptionPropertyCallbacks().size());
670885b47fbSopenharmony_ci        syncPromise->set_value(NO_ERROR);
671885b47fbSopenharmony_ci        }, "TASK_REGISTER_CAPTION_OBSERVER");
672885b47fbSopenharmony_ci
673885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
674885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
675885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait RegisterCaptionObserver result");
676885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
677885b47fbSopenharmony_ci    }
678885b47fbSopenharmony_ci    return syncFuture.get();
679885b47fbSopenharmony_ci}
680885b47fbSopenharmony_ci
681885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RegisterEnableAbilityListsObserver(
682885b47fbSopenharmony_ci    const sptr<IAccessibilityEnableAbilityListsObserver> &observer)
683885b47fbSopenharmony_ci{
684885b47fbSopenharmony_ci    HILOG_DEBUG();
685885b47fbSopenharmony_ci    if (!observer || !actionHandler_) {
686885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed!");
687885b47fbSopenharmony_ci        return;
688885b47fbSopenharmony_ci    }
689885b47fbSopenharmony_ci    XCollieHelper timer(TIMER_REGISTER_ENABLEABILITY_OBSERVER, XCOLLIE_TIMEOUT);
690885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<void>> syncPromisePtr = std::make_shared<ffrt::promise<void>>();
691885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromisePtr->get_future();
692885b47fbSopenharmony_ci    actionHandler_->PostTask([this, syncPromisePtr, observer]() {
693885b47fbSopenharmony_ci        HILOG_DEBUG();
694885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
695885b47fbSopenharmony_ci        if (!accountData) {
696885b47fbSopenharmony_ci            HILOG_ERROR("Account data is null");
697885b47fbSopenharmony_ci            syncPromisePtr->set_value();
698885b47fbSopenharmony_ci            return;
699885b47fbSopenharmony_ci        }
700885b47fbSopenharmony_ci        if (!enableAbilityListsObserverDeathRecipient_) {
701885b47fbSopenharmony_ci            enableAbilityListsObserverDeathRecipient_ = new(std::nothrow) EnableAbilityListsObserverDeathRecipient();
702885b47fbSopenharmony_ci            if (!enableAbilityListsObserverDeathRecipient_) {
703885b47fbSopenharmony_ci                HILOG_ERROR("enableAbilityListsObserverDeathRecipient_ is null");
704885b47fbSopenharmony_ci                syncPromisePtr->set_value();
705885b47fbSopenharmony_ci                return;
706885b47fbSopenharmony_ci            }
707885b47fbSopenharmony_ci        }
708885b47fbSopenharmony_ci        if (!observer->AsObject()) {
709885b47fbSopenharmony_ci            HILOG_ERROR("object is null");
710885b47fbSopenharmony_ci            syncPromisePtr->set_value();
711885b47fbSopenharmony_ci            return;
712885b47fbSopenharmony_ci        }
713885b47fbSopenharmony_ci        observer->AsObject()->AddDeathRecipient(enableAbilityListsObserverDeathRecipient_);
714885b47fbSopenharmony_ci        accountData->AddEnableAbilityListsObserver(observer);
715885b47fbSopenharmony_ci        syncPromisePtr->set_value();
716885b47fbSopenharmony_ci        }, "TASK_REGISTER_ENABLE_ABILITY_LISTS_OBSERVER");
717885b47fbSopenharmony_ci
718885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
719885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
720885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait RegisterEnableAbilityListsObserver result");
721885b47fbSopenharmony_ci        return;
722885b47fbSopenharmony_ci    }
723885b47fbSopenharmony_ci    return syncFuture.get();
724885b47fbSopenharmony_ci}
725885b47fbSopenharmony_ci
726885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetAbilityList(const uint32_t abilityTypes, const int32_t stateType,
727885b47fbSopenharmony_ci    std::vector<AccessibilityAbilityInfo> &infos)
728885b47fbSopenharmony_ci{
729885b47fbSopenharmony_ci    HILOG_DEBUG("abilityTypes(%{public}d) stateType(%{public}d)", abilityTypes, stateType);
730885b47fbSopenharmony_ci    if (!handler_ || (stateType > ABILITY_STATE_INSTALLED) || (stateType < ABILITY_STATE_ENABLE)) {
731885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed! stateType:%{public}d", stateType);
732885b47fbSopenharmony_ci        return RET_ERR_INVALID_PARAM;
733885b47fbSopenharmony_ci    }
734885b47fbSopenharmony_ci
735885b47fbSopenharmony_ci    ffrt::promise<RetError> syncPromise;
736885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
737885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise, &infos, abilityTypes, stateType]() {
738885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
739885b47fbSopenharmony_ci        if (!accountData) {
740885b47fbSopenharmony_ci            HILOG_ERROR("Get current account data failed!!");
741885b47fbSopenharmony_ci            syncPromise.set_value(RET_ERR_FAILED);
742885b47fbSopenharmony_ci            return;
743885b47fbSopenharmony_ci        }
744885b47fbSopenharmony_ci
745885b47fbSopenharmony_ci        vector<AccessibilityAbilityInfo> abilities;
746885b47fbSopenharmony_ci        accountData->GetAbilitiesByState(static_cast<AbilityStateType>(stateType), abilities);
747885b47fbSopenharmony_ci        HILOG_DEBUG("abilityes count is %{public}zu", abilities.size());
748885b47fbSopenharmony_ci        for (auto &ability : abilities) {
749885b47fbSopenharmony_ci            if (abilityTypes == AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL ||
750885b47fbSopenharmony_ci                (ability.GetAccessibilityAbilityType() & abilityTypes)) {
751885b47fbSopenharmony_ci                infos.push_back(ability);
752885b47fbSopenharmony_ci            }
753885b47fbSopenharmony_ci        }
754885b47fbSopenharmony_ci        HILOG_DEBUG("infos count is %{public}zu", infos.size());
755885b47fbSopenharmony_ci        syncPromise.set_value(RET_OK);
756885b47fbSopenharmony_ci        }, "TASK_GET_ABILITY_LIST");
757885b47fbSopenharmony_ci    return syncFuture.get();
758885b47fbSopenharmony_ci}
759885b47fbSopenharmony_ci
760885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::RegisterElementOperator(
761885b47fbSopenharmony_ci    const int32_t windowId, const sptr<IAccessibilityElementOperator> &operation, bool isApp)
762885b47fbSopenharmony_ci{
763885b47fbSopenharmony_ci    if (!handler_) {
764885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
765885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
766885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
767885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
768885b47fbSopenharmony_ci    }
769885b47fbSopenharmony_ci    uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
770885b47fbSopenharmony_ci    if (CheckCallingUid() != RET_OK) {
771885b47fbSopenharmony_ci        return RET_ERR_SAMGR;
772885b47fbSopenharmony_ci    }
773885b47fbSopenharmony_ci    handler_->PostTask([=]() {
774885b47fbSopenharmony_ci        HILOG_INFO("Register windowId[%{public}d]", windowId);
775885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
776885b47fbSopenharmony_ci        HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "RegisterElementOperator");
777885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
778885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
779885b47fbSopenharmony_ci        if (!accountData) {
780885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
781885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
782885b47fbSopenharmony_ci            HILOG_ERROR("Get current account data failed!!");
783885b47fbSopenharmony_ci            return;
784885b47fbSopenharmony_ci        }
785885b47fbSopenharmony_ci        sptr<AccessibilityWindowConnection> oldConnection = accountData->GetAccessibilityWindowConnection(windowId);
786885b47fbSopenharmony_ci        if (isApp && oldConnection) {
787885b47fbSopenharmony_ci            HILOG_WARN("no need to register again.");
788885b47fbSopenharmony_ci            return;
789885b47fbSopenharmony_ci        }
790885b47fbSopenharmony_ci        DeleteConnectionAndDeathRecipient(windowId, oldConnection);
791885b47fbSopenharmony_ci        sptr<AccessibilityWindowConnection> connection =
792885b47fbSopenharmony_ci            new(std::nothrow) AccessibilityWindowConnection(windowId, operation, currentAccountId_);
793885b47fbSopenharmony_ci        if (!connection) {
794885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
795885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
796885b47fbSopenharmony_ci            HILOG_ERROR("New  AccessibilityWindowConnection failed!!");
797885b47fbSopenharmony_ci            return;
798885b47fbSopenharmony_ci        }
799885b47fbSopenharmony_ci        connection->SetTokenIdMap(SINGLE_TREE_ID, tokenId);
800885b47fbSopenharmony_ci        accountData->AddAccessibilityWindowConnection(windowId, connection);
801885b47fbSopenharmony_ci
802885b47fbSopenharmony_ci        IsCheckWindowIdEventExist(windowId);
803885b47fbSopenharmony_ci        if (operation && operation->AsObject()) {
804885b47fbSopenharmony_ci            sptr<IRemoteObject::DeathRecipient> deathRecipient =
805885b47fbSopenharmony_ci                new(std::nothrow) InteractionOperationDeathRecipient(windowId, currentAccountId_);
806885b47fbSopenharmony_ci            if (!deathRecipient) {
807885b47fbSopenharmony_ci                Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
808885b47fbSopenharmony_ci                    A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
809885b47fbSopenharmony_ci                HILOG_ERROR("Create interactionOperationDeathRecipient failed");
810885b47fbSopenharmony_ci                return;
811885b47fbSopenharmony_ci            }
812885b47fbSopenharmony_ci
813885b47fbSopenharmony_ci            bool result = operation->AsObject()->AddDeathRecipient(deathRecipient);
814885b47fbSopenharmony_ci            interactionOperationDeathRecipients_[windowId] = deathRecipient;
815885b47fbSopenharmony_ci            HILOG_DEBUG("The result of adding operation's death recipient is %{public}d", result);
816885b47fbSopenharmony_ci        }
817885b47fbSopenharmony_ci        }, "TASK_REGISTER_ELEMENT_OPERATOR");
818885b47fbSopenharmony_ci    return RET_OK;
819885b47fbSopenharmony_ci}
820885b47fbSopenharmony_ci
821885b47fbSopenharmony_civoid AccessibleAbilityManagerService::IsCheckWindowIdEventExist(const int32_t windowId)
822885b47fbSopenharmony_ci{
823885b47fbSopenharmony_ci    if (CheckWindowIdEventExist(windowId)) {
824885b47fbSopenharmony_ci        SendEvent(windowFocusEventMap_[windowId]);
825885b47fbSopenharmony_ci        windowFocusEventMap_.erase(windowId);
826885b47fbSopenharmony_ci    }
827885b47fbSopenharmony_ci}
828885b47fbSopenharmony_ci
829885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::RegisterElementOperatorChildWork(const Registration &parameter,
830885b47fbSopenharmony_ci    const int32_t treeId, const int64_t nodeId, const sptr<IAccessibilityElementOperator> &operation,
831885b47fbSopenharmony_ci    const uint32_t tokenId, bool isApp)
832885b47fbSopenharmony_ci{
833885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
834885b47fbSopenharmony_ci    if (accountData == nullptr) {
835885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
836885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
837885b47fbSopenharmony_ci        HILOG_ERROR("Get current account data failed!!");
838885b47fbSopenharmony_ci        return RET_ERR_REGISTER_EXIST;
839885b47fbSopenharmony_ci    }
840885b47fbSopenharmony_ci
841885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> parentConnection =
842885b47fbSopenharmony_ci        accountData->GetAccessibilityWindowConnection(parameter.parentWindowId);
843885b47fbSopenharmony_ci    if (isApp && parentConnection) {
844885b47fbSopenharmony_ci        sptr<IAccessibilityElementOperator> parentAamsOper =
845885b47fbSopenharmony_ci            parentConnection->GetCardProxy(parameter.parentTreeId);
846885b47fbSopenharmony_ci        if (parentAamsOper != nullptr) {
847885b47fbSopenharmony_ci            parentAamsOper->SetChildTreeIdAndWinId(nodeId, treeId, parameter.windowId);
848885b47fbSopenharmony_ci        } else {
849885b47fbSopenharmony_ci            HILOG_DEBUG("parentAamsOper is nullptr");
850885b47fbSopenharmony_ci        }
851885b47fbSopenharmony_ci    } else {
852885b47fbSopenharmony_ci        return RET_ERR_NO_CONNECTION;
853885b47fbSopenharmony_ci    }
854885b47fbSopenharmony_ci
855885b47fbSopenharmony_ci    operation->SetBelongTreeId(treeId);
856885b47fbSopenharmony_ci    operation->SetParentWindowId(parameter.parentWindowId);
857885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> oldConnection =
858885b47fbSopenharmony_ci        accountData->GetAccessibilityWindowConnection(parameter.windowId);
859885b47fbSopenharmony_ci    if (isApp && oldConnection) {
860885b47fbSopenharmony_ci        if (oldConnection->GetCardProxy(treeId) != nullptr) {
861885b47fbSopenharmony_ci            HILOG_WARN("no need to register again.");
862885b47fbSopenharmony_ci            return RET_ERR_REGISTER_EXIST;
863885b47fbSopenharmony_ci        } else {
864885b47fbSopenharmony_ci            oldConnection->SetCardProxy(treeId, operation);
865885b47fbSopenharmony_ci            SetTokenIdMapAndRootParentId(oldConnection, treeId, nodeId, tokenId);
866885b47fbSopenharmony_ci        }
867885b47fbSopenharmony_ci    }
868885b47fbSopenharmony_ci    return RET_OK;
869885b47fbSopenharmony_ci}
870885b47fbSopenharmony_ci
871885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SetTokenIdMapAndRootParentId(
872885b47fbSopenharmony_ci    const sptr<AccessibilityWindowConnection> connection,
873885b47fbSopenharmony_ci    const int32_t treeId, const int64_t nodeId, const uint32_t tokenId)
874885b47fbSopenharmony_ci{
875885b47fbSopenharmony_ci    connection->SetTokenIdMap(treeId, tokenId);
876885b47fbSopenharmony_ci    connection->SetRootParentId(treeId, nodeId);
877885b47fbSopenharmony_ci}
878885b47fbSopenharmony_ci
879885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::RegisterElementOperator(Registration parameter,
880885b47fbSopenharmony_ci    const sptr<IAccessibilityElementOperator> &operation, bool isApp)
881885b47fbSopenharmony_ci{
882885b47fbSopenharmony_ci    if (CheckCallingUid() != RET_OK) {
883885b47fbSopenharmony_ci        return RET_ERR_SAMGR;
884885b47fbSopenharmony_ci    }
885885b47fbSopenharmony_ci    static std::atomic<int32_t> treeId(1);
886885b47fbSopenharmony_ci    int32_t treeIdSingle = treeId.fetch_add(1, std::memory_order_relaxed);
887885b47fbSopenharmony_ci    if (treeIdSingle > TREE_ID_MAX) {
888885b47fbSopenharmony_ci        HILOG_ERROR("TreeId more than 13.");
889885b47fbSopenharmony_ci        return RET_ERR_TREE_TOO_BIG;
890885b47fbSopenharmony_ci    }
891885b47fbSopenharmony_ci    uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
892885b47fbSopenharmony_ci    int64_t nodeId = parameter.elementId;
893885b47fbSopenharmony_ci    HILOG_INFO("get treeId element and treeid - treeId: %{public}d parameter.elementId[%{public}" PRId64 "]"
894885b47fbSopenharmony_ci        "element[%{public}" PRId64 "]",
895885b47fbSopenharmony_ci        treeIdSingle, parameter.elementId, nodeId);
896885b47fbSopenharmony_ci
897885b47fbSopenharmony_ci    if (!handler_) {
898885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
899885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
900885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
901885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
902885b47fbSopenharmony_ci    }
903885b47fbSopenharmony_ci    handler_->PostTask([=]() {
904885b47fbSopenharmony_ci        HILOG_INFO("Register windowId[%{public}d]", parameter.windowId);
905885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
906885b47fbSopenharmony_ci        HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "RegisterElementOperator");
907885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
908885b47fbSopenharmony_ci        if (RET_OK != RegisterElementOperatorChildWork(parameter, treeIdSingle, nodeId, operation, tokenId, isApp)) {
909885b47fbSopenharmony_ci            return;
910885b47fbSopenharmony_ci        }
911885b47fbSopenharmony_ci        if (CheckWindowIdEventExist(parameter.windowId)) {
912885b47fbSopenharmony_ci            SendEvent(windowFocusEventMap_[parameter.windowId]);
913885b47fbSopenharmony_ci            windowFocusEventMap_.erase(parameter.windowId);
914885b47fbSopenharmony_ci        }
915885b47fbSopenharmony_ci        if (operation && operation->AsObject()) {
916885b47fbSopenharmony_ci            sptr<IRemoteObject::DeathRecipient> deathRecipient =
917885b47fbSopenharmony_ci                new(std::nothrow) InteractionOperationDeathRecipient(parameter.windowId, treeIdSingle,
918885b47fbSopenharmony_ci                    currentAccountId_);
919885b47fbSopenharmony_ci            if (deathRecipient == nullptr) {
920885b47fbSopenharmony_ci                Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
921885b47fbSopenharmony_ci                    A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
922885b47fbSopenharmony_ci                HILOG_ERROR("Create interactionOperationDeathRecipient failed");
923885b47fbSopenharmony_ci                return;
924885b47fbSopenharmony_ci            }
925885b47fbSopenharmony_ci            bool result = operation->AsObject()->AddDeathRecipient(deathRecipient);
926885b47fbSopenharmony_ci            interactionOperationDeathMap_[parameter.windowId][treeIdSingle] = deathRecipient;
927885b47fbSopenharmony_ci            HILOG_DEBUG("The result of adding operation's death recipient is %{public}d", result);
928885b47fbSopenharmony_ci        }
929885b47fbSopenharmony_ci        }, "TASK_REGISTER_ELEMENT_OPERATOR");
930885b47fbSopenharmony_ci    return RET_OK;
931885b47fbSopenharmony_ci}
932885b47fbSopenharmony_ci
933885b47fbSopenharmony_civoid AccessibleAbilityManagerService::DeleteConnectionAndDeathRecipient(
934885b47fbSopenharmony_ci    const int32_t windowId, const sptr<AccessibilityWindowConnection> &connection)
935885b47fbSopenharmony_ci{
936885b47fbSopenharmony_ci    HILOG_DEBUG();
937885b47fbSopenharmony_ci    if (!connection) {
938885b47fbSopenharmony_ci        HILOG_ERROR("connection is nullptr");
939885b47fbSopenharmony_ci        return;
940885b47fbSopenharmony_ci    }
941885b47fbSopenharmony_ci
942885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
943885b47fbSopenharmony_ci    if (!accountData) {
944885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
945885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_TARGET_APPLICATION_FAILED);
946885b47fbSopenharmony_ci        HILOG_ERROR("Get current account data failed!!");
947885b47fbSopenharmony_ci        return;
948885b47fbSopenharmony_ci    }
949885b47fbSopenharmony_ci
950885b47fbSopenharmony_ci    accountData->RemoveAccessibilityWindowConnection(windowId);
951885b47fbSopenharmony_ci    if (!connection->GetProxy()) {
952885b47fbSopenharmony_ci        HILOG_WARN("proxy is null");
953885b47fbSopenharmony_ci        return;
954885b47fbSopenharmony_ci    }
955885b47fbSopenharmony_ci    auto object = connection->GetProxy()->AsObject();
956885b47fbSopenharmony_ci    if (object) {
957885b47fbSopenharmony_ci        auto iter = interactionOperationDeathRecipients_.find(windowId);
958885b47fbSopenharmony_ci        if (iter != interactionOperationDeathRecipients_.end()) {
959885b47fbSopenharmony_ci            sptr<IRemoteObject::DeathRecipient> deathRecipient = iter->second;
960885b47fbSopenharmony_ci            bool result = object->RemoveDeathRecipient(deathRecipient);
961885b47fbSopenharmony_ci            HILOG_DEBUG("The result of deleting connection's death recipient is %{public}d", result);
962885b47fbSopenharmony_ci            interactionOperationDeathRecipients_.erase(iter);
963885b47fbSopenharmony_ci        }
964885b47fbSopenharmony_ci    }
965885b47fbSopenharmony_ci}
966885b47fbSopenharmony_ci
967885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::DeregisterElementOperator(int32_t windowId)
968885b47fbSopenharmony_ci{
969885b47fbSopenharmony_ci    if (!handler_) {
970885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
971885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
972885b47fbSopenharmony_ci    }
973885b47fbSopenharmony_ci
974885b47fbSopenharmony_ci    handler_->PostTask([=]() {
975885b47fbSopenharmony_ci        HILOG_INFO("Deregister windowId[%{public}d]", windowId);
976885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
977885b47fbSopenharmony_ci        if (!accountData) {
978885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr.");
979885b47fbSopenharmony_ci            return;
980885b47fbSopenharmony_ci        }
981885b47fbSopenharmony_ci        sptr<AccessibilityWindowConnection> connection = accountData->GetAccessibilityWindowConnection(windowId);
982885b47fbSopenharmony_ci        if (!connection) {
983885b47fbSopenharmony_ci            HILOG_WARN("The operation of windowId[%{public}d] has not been registered.", windowId);
984885b47fbSopenharmony_ci            return;
985885b47fbSopenharmony_ci        }
986885b47fbSopenharmony_ci        accountData->RemoveAccessibilityWindowConnection(windowId);
987885b47fbSopenharmony_ci        StopCallbackWait(windowId);
988885b47fbSopenharmony_ci
989885b47fbSopenharmony_ci        if (!connection->GetProxy()) {
990885b47fbSopenharmony_ci            HILOG_ERROR("proxy is null");
991885b47fbSopenharmony_ci            return;
992885b47fbSopenharmony_ci        }
993885b47fbSopenharmony_ci
994885b47fbSopenharmony_ci        auto object = connection->GetProxy()->AsObject();
995885b47fbSopenharmony_ci        if (object) {
996885b47fbSopenharmony_ci            auto iter = interactionOperationDeathRecipients_.find(windowId);
997885b47fbSopenharmony_ci            if (iter != interactionOperationDeathRecipients_.end()) {
998885b47fbSopenharmony_ci                sptr<IRemoteObject::DeathRecipient> deathRecipient = iter->second;
999885b47fbSopenharmony_ci                bool result = object->RemoveDeathRecipient(deathRecipient);
1000885b47fbSopenharmony_ci                HILOG_DEBUG("The result of deleting operation's death recipient is %{public}d", result);
1001885b47fbSopenharmony_ci                interactionOperationDeathRecipients_.erase(iter);
1002885b47fbSopenharmony_ci            } else {
1003885b47fbSopenharmony_ci                HILOG_INFO("cannot find remote object. windowId[%{public}d]", windowId);
1004885b47fbSopenharmony_ci            }
1005885b47fbSopenharmony_ci        }
1006885b47fbSopenharmony_ci        }, "TASK_DEREGISTER_ELEMENT_OPERATOR");
1007885b47fbSopenharmony_ci    return RET_OK;
1008885b47fbSopenharmony_ci}
1009885b47fbSopenharmony_ci
1010885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::DeregisterElementOperator(int32_t windowId, const int32_t treeId)
1011885b47fbSopenharmony_ci{
1012885b47fbSopenharmony_ci    if (!handler_) {
1013885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1014885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1015885b47fbSopenharmony_ci    }
1016885b47fbSopenharmony_ci
1017885b47fbSopenharmony_ci    handler_->PostTask([=]() {
1018885b47fbSopenharmony_ci        HILOG_INFO("Deregister windowId[%{public}d], treeId[%{public}d] start", windowId, treeId);
1019885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1020885b47fbSopenharmony_ci        if (!accountData) {
1021885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr.");
1022885b47fbSopenharmony_ci            return;
1023885b47fbSopenharmony_ci        }
1024885b47fbSopenharmony_ci        sptr<AccessibilityWindowConnection> connection = accountData->GetAccessibilityWindowConnection(windowId);
1025885b47fbSopenharmony_ci        if (connection == nullptr) {
1026885b47fbSopenharmony_ci            HILOG_WARN("The operation of windowId[%{public}d] has not been registered.", windowId);
1027885b47fbSopenharmony_ci            return;
1028885b47fbSopenharmony_ci        }
1029885b47fbSopenharmony_ci        StopCallbackWait(windowId, treeId);
1030885b47fbSopenharmony_ci
1031885b47fbSopenharmony_ci        if (!connection->GetCardProxy(treeId)) {
1032885b47fbSopenharmony_ci            HILOG_ERROR("proxy is null");
1033885b47fbSopenharmony_ci            return;
1034885b47fbSopenharmony_ci        }
1035885b47fbSopenharmony_ci
1036885b47fbSopenharmony_ci        auto object = connection->GetCardProxy(treeId)->AsObject();
1037885b47fbSopenharmony_ci        if (object) {
1038885b47fbSopenharmony_ci            RemoveTreeDeathRecipient(windowId, treeId, connection);
1039885b47fbSopenharmony_ci        }
1040885b47fbSopenharmony_ci        }, "TASK_DEREGISTER_ELEMENT_OPERATOR");
1041885b47fbSopenharmony_ci    return RET_OK;
1042885b47fbSopenharmony_ci}
1043885b47fbSopenharmony_ci
1044885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RemoveTreeDeathRecipient(const int32_t windowId, const int32_t treeId,
1045885b47fbSopenharmony_ci    const sptr<AccessibilityWindowConnection> connection)
1046885b47fbSopenharmony_ci{
1047885b47fbSopenharmony_ci    auto object = connection->GetCardProxy(treeId);
1048885b47fbSopenharmony_ci    if (object == nullptr) {
1049885b47fbSopenharmony_ci        HILOG_ERROR("GetCardProxy is null");
1050885b47fbSopenharmony_ci        return;
1051885b47fbSopenharmony_ci    }
1052885b47fbSopenharmony_ci    auto remoteObject = object->AsObject();
1053885b47fbSopenharmony_ci    connection->EraseProxy(treeId);
1054885b47fbSopenharmony_ci    auto iter = interactionOperationDeathMap_.find(windowId);
1055885b47fbSopenharmony_ci    if (iter != interactionOperationDeathMap_.end()) {
1056885b47fbSopenharmony_ci        auto iterTree = iter->second.find(treeId);
1057885b47fbSopenharmony_ci        if (iterTree != iter->second.end()) {
1058885b47fbSopenharmony_ci            sptr<IRemoteObject::DeathRecipient> deathRecipient = iterTree->second;
1059885b47fbSopenharmony_ci            bool result = remoteObject->RemoveDeathRecipient(deathRecipient);
1060885b47fbSopenharmony_ci            HILOG_DEBUG("The result of deleting operation's death recipient is %{public}d", result);
1061885b47fbSopenharmony_ci            iter->second.erase(iterTree);
1062885b47fbSopenharmony_ci        } else {
1063885b47fbSopenharmony_ci            HILOG_ERROR("cannot find remote object. treeId[%{public}d]", treeId);
1064885b47fbSopenharmony_ci        }
1065885b47fbSopenharmony_ci    } else {
1066885b47fbSopenharmony_ci        HILOG_ERROR("cannot find remote object. windowId[%{public}d]", windowId);
1067885b47fbSopenharmony_ci    }
1068885b47fbSopenharmony_ci}
1069885b47fbSopenharmony_ci
1070885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption)
1071885b47fbSopenharmony_ci{
1072885b47fbSopenharmony_ci    return accessibilitySettings_->GetCaptionProperty(caption);
1073885b47fbSopenharmony_ci}
1074885b47fbSopenharmony_ci
1075885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption)
1076885b47fbSopenharmony_ci{
1077885b47fbSopenharmony_ci    return accessibilitySettings_->SetCaptionProperty(caption);
1078885b47fbSopenharmony_ci}
1079885b47fbSopenharmony_ci
1080885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetCaptionState(const bool state)
1081885b47fbSopenharmony_ci{
1082885b47fbSopenharmony_ci    return accessibilitySettings_->SetCaptionState(state);
1083885b47fbSopenharmony_ci}
1084885b47fbSopenharmony_ci
1085885b47fbSopenharmony_cibool AccessibleAbilityManagerService::GetEnabledState()
1086885b47fbSopenharmony_ci{
1087885b47fbSopenharmony_ci    HILOG_DEBUG();
1088885b47fbSopenharmony_ci    if (!handler_) {
1089885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1090885b47fbSopenharmony_ci        return false;
1091885b47fbSopenharmony_ci    }
1092885b47fbSopenharmony_ci
1093885b47fbSopenharmony_ci    ffrt::promise<bool> syncPromise;
1094885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1095885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise]() {
1096885b47fbSopenharmony_ci        HILOG_DEBUG();
1097885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1098885b47fbSopenharmony_ci        if (!accountData) {
1099885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1100885b47fbSopenharmony_ci            syncPromise.set_value(false);
1101885b47fbSopenharmony_ci            return;
1102885b47fbSopenharmony_ci        }
1103885b47fbSopenharmony_ci        bool result = accountData->GetConfig()->GetEnabledState();
1104885b47fbSopenharmony_ci        syncPromise.set_value(result);
1105885b47fbSopenharmony_ci        }, "TASK_GET_ENABLE_STATE");
1106885b47fbSopenharmony_ci    return syncFuture.get();
1107885b47fbSopenharmony_ci}
1108885b47fbSopenharmony_ci
1109885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetCaptionState(bool &state)
1110885b47fbSopenharmony_ci{
1111885b47fbSopenharmony_ci    return accessibilitySettings_->GetCaptionState(state);
1112885b47fbSopenharmony_ci}
1113885b47fbSopenharmony_ci
1114885b47fbSopenharmony_cibool AccessibleAbilityManagerService::GetTouchGuideState()
1115885b47fbSopenharmony_ci{
1116885b47fbSopenharmony_ci    HILOG_DEBUG();
1117885b47fbSopenharmony_ci    if (!handler_) {
1118885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1119885b47fbSopenharmony_ci        return false;
1120885b47fbSopenharmony_ci    }
1121885b47fbSopenharmony_ci
1122885b47fbSopenharmony_ci    ffrt::promise<bool> syncPromise;
1123885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1124885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise]() {
1125885b47fbSopenharmony_ci        HILOG_DEBUG();
1126885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1127885b47fbSopenharmony_ci        if (!accountData) {
1128885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1129885b47fbSopenharmony_ci            syncPromise.set_value(false);
1130885b47fbSopenharmony_ci            return;
1131885b47fbSopenharmony_ci        }
1132885b47fbSopenharmony_ci        bool result = accountData->GetConfig()->GetTouchGuideState();
1133885b47fbSopenharmony_ci        syncPromise.set_value(result);
1134885b47fbSopenharmony_ci        }, "TASK_GET_TOUCH_GUIDE_STATE");
1135885b47fbSopenharmony_ci    return syncFuture.get();
1136885b47fbSopenharmony_ci}
1137885b47fbSopenharmony_ci
1138885b47fbSopenharmony_cibool AccessibleAbilityManagerService::GetGestureState()
1139885b47fbSopenharmony_ci{
1140885b47fbSopenharmony_ci    HILOG_DEBUG();
1141885b47fbSopenharmony_ci    if (!handler_) {
1142885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1143885b47fbSopenharmony_ci        return false;
1144885b47fbSopenharmony_ci    }
1145885b47fbSopenharmony_ci
1146885b47fbSopenharmony_ci    ffrt::promise<bool> syncPromise;
1147885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1148885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise]() {
1149885b47fbSopenharmony_ci        HILOG_DEBUG();
1150885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1151885b47fbSopenharmony_ci        if (!accountData) {
1152885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1153885b47fbSopenharmony_ci            syncPromise.set_value(false);
1154885b47fbSopenharmony_ci            return;
1155885b47fbSopenharmony_ci        }
1156885b47fbSopenharmony_ci        bool result = accountData->GetConfig()->GetGestureState();
1157885b47fbSopenharmony_ci        syncPromise.set_value(result);
1158885b47fbSopenharmony_ci        }, "TASK_GET_GESTURE_STATE");
1159885b47fbSopenharmony_ci    return syncFuture.get();
1160885b47fbSopenharmony_ci}
1161885b47fbSopenharmony_ci
1162885b47fbSopenharmony_cibool AccessibleAbilityManagerService::GetKeyEventObserverState()
1163885b47fbSopenharmony_ci{
1164885b47fbSopenharmony_ci    HILOG_DEBUG();
1165885b47fbSopenharmony_ci    if (!handler_) {
1166885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1167885b47fbSopenharmony_ci        return false;
1168885b47fbSopenharmony_ci    }
1169885b47fbSopenharmony_ci
1170885b47fbSopenharmony_ci    ffrt::promise<bool> syncPromise;
1171885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1172885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise]() {
1173885b47fbSopenharmony_ci        HILOG_DEBUG();
1174885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1175885b47fbSopenharmony_ci        if (!accountData) {
1176885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1177885b47fbSopenharmony_ci            syncPromise.set_value(false);
1178885b47fbSopenharmony_ci            return;
1179885b47fbSopenharmony_ci        }
1180885b47fbSopenharmony_ci        bool result = accountData->GetConfig()->GetKeyEventObserverState();
1181885b47fbSopenharmony_ci        syncPromise.set_value(result);
1182885b47fbSopenharmony_ci        }, "TASK_GET_KEY_EVENT_OBSERVER_STATE");
1183885b47fbSopenharmony_ci    return syncFuture.get();
1184885b47fbSopenharmony_ci}
1185885b47fbSopenharmony_ci
1186885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::EnableAbility(const std::string &name, const uint32_t capabilities)
1187885b47fbSopenharmony_ci{
1188885b47fbSopenharmony_ci    HILOG_DEBUG();
1189885b47fbSopenharmony_ci    if (!handler_) {
1190885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1191885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1192885b47fbSopenharmony_ci    }
1193885b47fbSopenharmony_ci
1194885b47fbSopenharmony_ci    ffrt::promise<RetError> syncPromise;
1195885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1196885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise, &name, &capabilities]() {
1197885b47fbSopenharmony_ci        HILOG_DEBUG();
1198885b47fbSopenharmony_ci        RetError result = InnerEnableAbility(name, capabilities);
1199885b47fbSopenharmony_ci        syncPromise.set_value(result);
1200885b47fbSopenharmony_ci        }, "TASK_ENABLE_ABILITIES");
1201885b47fbSopenharmony_ci    return syncFuture.get();
1202885b47fbSopenharmony_ci}
1203885b47fbSopenharmony_ci
1204885b47fbSopenharmony_cibool AccessibleAbilityManagerService::SetTargetAbility(const int32_t targetAbilityValue)
1205885b47fbSopenharmony_ci{
1206885b47fbSopenharmony_ci    HILOG_DEBUG();
1207885b47fbSopenharmony_ci
1208885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1209885b47fbSopenharmony_ci    if (!accountData) {
1210885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
1211885b47fbSopenharmony_ci        return false;
1212885b47fbSopenharmony_ci    }
1213885b47fbSopenharmony_ci
1214885b47fbSopenharmony_ci    bool state;
1215885b47fbSopenharmony_ci    switch (targetAbilityValue) {
1216885b47fbSopenharmony_ci        case HIGH_CONTRAST_TEXT:
1217885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetHighContrastTextState();
1218885b47fbSopenharmony_ci            return SetHighContrastTextState(!state) == RET_OK;
1219885b47fbSopenharmony_ci        case INVERT_COLOR:
1220885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetInvertColorState();
1221885b47fbSopenharmony_ci            return SetInvertColorState(!state) == RET_OK;
1222885b47fbSopenharmony_ci        case ANIMATION_OFF:
1223885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetAnimationOffState();
1224885b47fbSopenharmony_ci            return SetAnimationOffState(!state) == RET_OK;
1225885b47fbSopenharmony_ci        case SCREEN_MAGNIFICATION:
1226885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetScreenMagnificationState();
1227885b47fbSopenharmony_ci            return SetScreenMagnificationState(!state) == RET_OK;
1228885b47fbSopenharmony_ci        case AUDIO_MONO:
1229885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetAudioMonoState();
1230885b47fbSopenharmony_ci            return SetAudioMonoState(!state) == RET_OK;
1231885b47fbSopenharmony_ci        case MOUSE_KEY:
1232885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetMouseKeyState();
1233885b47fbSopenharmony_ci            return SetMouseKeyState(!state) == RET_OK;
1234885b47fbSopenharmony_ci        case CAPTION_STATE:
1235885b47fbSopenharmony_ci            state = accountData->GetConfig()->GetCaptionState();
1236885b47fbSopenharmony_ci            return SetCaptionState(!state) == RET_OK;
1237885b47fbSopenharmony_ci        default:
1238885b47fbSopenharmony_ci            return false;
1239885b47fbSopenharmony_ci    }
1240885b47fbSopenharmony_ci}
1241885b47fbSopenharmony_ci
1242885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::InnerEnableAbility(const std::string &name, const uint32_t capabilities)
1243885b47fbSopenharmony_ci{
1244885b47fbSopenharmony_ci    HILOG_DEBUG();
1245885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1246885b47fbSopenharmony_ci    if (!accountData) {
1247885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
1248885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1249885b47fbSopenharmony_ci    }
1250885b47fbSopenharmony_ci    auto iter = removedAutoStartAbilities_.begin();
1251885b47fbSopenharmony_ci    for (; iter != removedAutoStartAbilities_.end(); ++iter) {
1252885b47fbSopenharmony_ci        if (*iter == name) {
1253885b47fbSopenharmony_ci            removedAutoStartAbilities_.erase(iter);
1254885b47fbSopenharmony_ci            break;
1255885b47fbSopenharmony_ci        }
1256885b47fbSopenharmony_ci    }
1257885b47fbSopenharmony_ci    return accountData->EnableAbility(name, capabilities);
1258885b47fbSopenharmony_ci}
1259885b47fbSopenharmony_ci
1260885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetEnabledAbilities(std::vector<std::string> &enabledAbilities)
1261885b47fbSopenharmony_ci{
1262885b47fbSopenharmony_ci    HILOG_DEBUG();
1263885b47fbSopenharmony_ci    if (!handler_) {
1264885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1265885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1266885b47fbSopenharmony_ci    }
1267885b47fbSopenharmony_ci
1268885b47fbSopenharmony_ci    ffrt::promise<RetError> syncPromise;
1269885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1270885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise, &enabledAbilities]() {
1271885b47fbSopenharmony_ci        HILOG_DEBUG();
1272885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1273885b47fbSopenharmony_ci        if (!accountData) {
1274885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1275885b47fbSopenharmony_ci            syncPromise.set_value(RET_ERR_NULLPTR);
1276885b47fbSopenharmony_ci            return;
1277885b47fbSopenharmony_ci        }
1278885b47fbSopenharmony_ci        enabledAbilities = accountData->GetEnabledAbilities();
1279885b47fbSopenharmony_ci        syncPromise.set_value(RET_OK);
1280885b47fbSopenharmony_ci        }, "TASK_GET_ENABLE_ABILITIES");
1281885b47fbSopenharmony_ci    return syncFuture.get();
1282885b47fbSopenharmony_ci}
1283885b47fbSopenharmony_ci
1284885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetCurtainScreenUsingStatus(bool isEnable)
1285885b47fbSopenharmony_ci{
1286885b47fbSopenharmony_ci    HILOG_DEBUG();
1287885b47fbSopenharmony_ci    auto rsInterfaces = &(Rosen::RSInterfaces::GetInstance());
1288885b47fbSopenharmony_ci    if (rsInterfaces == nullptr) {
1289885b47fbSopenharmony_ci        HILOG_ERROR("rsInterfaces is nullptr.");
1290885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1291885b47fbSopenharmony_ci    }
1292885b47fbSopenharmony_ci    HILOG_INFO("SetCurtainScreenUsingStatus: status = %{public}d", isEnable);
1293885b47fbSopenharmony_ci    rsInterfaces->SetCurtainScreenUsingStatus(isEnable);
1294885b47fbSopenharmony_ci    return RET_OK;
1295885b47fbSopenharmony_ci}
1296885b47fbSopenharmony_ci
1297885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::DisableAbility(const std::string &name)
1298885b47fbSopenharmony_ci{
1299885b47fbSopenharmony_ci    HILOG_INFO();
1300885b47fbSopenharmony_ci    if (!actionHandler_) {
1301885b47fbSopenharmony_ci        HILOG_ERROR("actionHandler_ is nullptr.");
1302885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1303885b47fbSopenharmony_ci    }
1304885b47fbSopenharmony_ci
1305885b47fbSopenharmony_ci    ffrt::promise<RetError> syncPromise;
1306885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1307885b47fbSopenharmony_ci    actionHandler_->PostTask([this, &syncPromise, &name]() {
1308885b47fbSopenharmony_ci        HILOG_DEBUG();
1309885b47fbSopenharmony_ci        RetError result = InnerDisableAbility(name);
1310885b47fbSopenharmony_ci        syncPromise.set_value(result);
1311885b47fbSopenharmony_ci        }, "TASK_DISABLE_ABILITIES");
1312885b47fbSopenharmony_ci    return syncFuture.get();
1313885b47fbSopenharmony_ci}
1314885b47fbSopenharmony_ci
1315885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::InnerDisableAbility(const std::string &name)
1316885b47fbSopenharmony_ci{
1317885b47fbSopenharmony_ci    HILOG_INFO();
1318885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
1319885b47fbSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "InnerDisableAbility:" + name);
1320885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
1321885b47fbSopenharmony_ci
1322885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1323885b47fbSopenharmony_ci    if (!accountData) {
1324885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
1325885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1326885b47fbSopenharmony_ci    }
1327885b47fbSopenharmony_ci    if (accountData->GetConnectingA11yAbility(name) != nullptr) {
1328885b47fbSopenharmony_ci        HILOG_WARN("refuse to disconnect ability %{public}s when connecting", name.c_str());
1329885b47fbSopenharmony_ci        return RET_OK;
1330885b47fbSopenharmony_ci    }
1331885b47fbSopenharmony_ci    if (name == SCREEN_READER_BUNDLE_ABILITY_NAME) {
1332885b47fbSopenharmony_ci        ExecuteActionOnAccessibilityFocused(ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS);
1333885b47fbSopenharmony_ci        SetCurtainScreenUsingStatus(false);
1334885b47fbSopenharmony_ci    }
1335885b47fbSopenharmony_ci    RetError ret = accountData->RemoveEnabledAbility(name);
1336885b47fbSopenharmony_ci    if (ret != RET_OK) {
1337885b47fbSopenharmony_ci        HILOG_ERROR("RemoveEnabledAbility failed");
1338885b47fbSopenharmony_ci        return ret;
1339885b47fbSopenharmony_ci    }
1340885b47fbSopenharmony_ci    accountData->SetAbilityAutoStartState(name, false);
1341885b47fbSopenharmony_ci    accountData->RemoveConnectingA11yAbility(name);
1342885b47fbSopenharmony_ci    accountData->UpdateAbilities();
1343885b47fbSopenharmony_ci    return RET_OK;
1344885b47fbSopenharmony_ci}
1345885b47fbSopenharmony_ci
1346885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::EnableUITestAbility(const sptr<IRemoteObject> &obj)
1347885b47fbSopenharmony_ci{
1348885b47fbSopenharmony_ci    HILOG_DEBUG();
1349885b47fbSopenharmony_ci    if (!handler_) {
1350885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1351885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1352885b47fbSopenharmony_ci    }
1353885b47fbSopenharmony_ci
1354885b47fbSopenharmony_ci    if (!obj) {
1355885b47fbSopenharmony_ci        HILOG_ERROR("obj is nullptr.");
1356885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1357885b47fbSopenharmony_ci    }
1358885b47fbSopenharmony_ci
1359885b47fbSopenharmony_ci    ffrt::promise<RetError> syncPromise;
1360885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise.get_future();
1361885b47fbSopenharmony_ci    handler_->PostTask([this, &syncPromise, obj]() {
1362885b47fbSopenharmony_ci        HILOG_DEBUG();
1363885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1364885b47fbSopenharmony_ci        if (!accountData) {
1365885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1366885b47fbSopenharmony_ci            syncPromise.set_value(RET_ERR_NULLPTR);
1367885b47fbSopenharmony_ci            return;
1368885b47fbSopenharmony_ci        }
1369885b47fbSopenharmony_ci        std::string uiTestUri = Utils::GetUri(UI_TEST_BUNDLE_NAME, UI_TEST_ABILITY_NAME);
1370885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> connection = accountData->GetAccessibleAbilityConnection(uiTestUri);
1371885b47fbSopenharmony_ci        if (connection) {
1372885b47fbSopenharmony_ci            HILOG_ERROR("connection is existed!!");
1373885b47fbSopenharmony_ci            syncPromise.set_value(RET_ERR_CONNECTION_EXIST);
1374885b47fbSopenharmony_ci            return;
1375885b47fbSopenharmony_ci        }
1376885b47fbSopenharmony_ci
1377885b47fbSopenharmony_ci        std::function<void()> addUITestClientFunc = std::bind(&AccessibilityAccountData::AddUITestClient, accountData,
1378885b47fbSopenharmony_ci            obj, UI_TEST_BUNDLE_NAME, UI_TEST_ABILITY_NAME);
1379885b47fbSopenharmony_ci        handler_->PostTask(addUITestClientFunc, "AddUITestClient");
1380885b47fbSopenharmony_ci        syncPromise.set_value(RET_OK);
1381885b47fbSopenharmony_ci        }, "TASK_ENABLE_UI_TEST_ABILITIES");
1382885b47fbSopenharmony_ci    return syncFuture.get();
1383885b47fbSopenharmony_ci}
1384885b47fbSopenharmony_ci
1385885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::DisableUITestAbility()
1386885b47fbSopenharmony_ci{
1387885b47fbSopenharmony_ci    HILOG_DEBUG();
1388885b47fbSopenharmony_ci    if (!handler_) {
1389885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr.");
1390885b47fbSopenharmony_ci        return RET_ERR_NULLPTR;
1391885b47fbSopenharmony_ci    }
1392885b47fbSopenharmony_ci
1393885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
1394885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
1395885b47fbSopenharmony_ci    handler_->PostTask([this, syncPromise]() {
1396885b47fbSopenharmony_ci        HILOG_DEBUG();
1397885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1398885b47fbSopenharmony_ci        if (!accountData) {
1399885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
1400885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NULLPTR);
1401885b47fbSopenharmony_ci            return;
1402885b47fbSopenharmony_ci        }
1403885b47fbSopenharmony_ci        std::string uiTestUri = Utils::GetUri(UI_TEST_BUNDLE_NAME, UI_TEST_ABILITY_NAME);
1404885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> connection = accountData->GetAccessibleAbilityConnection(uiTestUri);
1405885b47fbSopenharmony_ci        if (!connection) {
1406885b47fbSopenharmony_ci            HILOG_ERROR("connection is not existed!!");
1407885b47fbSopenharmony_ci            syncPromise->set_value(RET_ERR_NO_CONNECTION);
1408885b47fbSopenharmony_ci            return;
1409885b47fbSopenharmony_ci        }
1410885b47fbSopenharmony_ci        std::function<void()> removeUITestClientFunc =
1411885b47fbSopenharmony_ci            std::bind(&AccessibilityAccountData::RemoveUITestClient, accountData, connection, UI_TEST_BUNDLE_NAME);
1412885b47fbSopenharmony_ci        handler_->PostTask(removeUITestClientFunc, "RemoveUITestClient");
1413885b47fbSopenharmony_ci        syncPromise->set_value(RET_OK);
1414885b47fbSopenharmony_ci        }, "TASK_DISABLE_UI_TEST_ABILITIES");
1415885b47fbSopenharmony_ci
1416885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
1417885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
1418885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait DisableUITestAbility result");
1419885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
1420885b47fbSopenharmony_ci    }
1421885b47fbSopenharmony_ci    return syncFuture.get();
1422885b47fbSopenharmony_ci}
1423885b47fbSopenharmony_ci
1424885b47fbSopenharmony_ciint32_t AccessibleAbilityManagerService::GetActiveWindow()
1425885b47fbSopenharmony_ci{
1426885b47fbSopenharmony_ci    HILOG_DEBUG();
1427885b47fbSopenharmony_ci    return Singleton<AccessibilityWindowManager>::GetInstance().GetActiveWindowId();
1428885b47fbSopenharmony_ci}
1429885b47fbSopenharmony_ci
1430885b47fbSopenharmony_cibool AccessibleAbilityManagerService::Init()
1431885b47fbSopenharmony_ci{
1432885b47fbSopenharmony_ci    HILOG_DEBUG();
1433885b47fbSopenharmony_ci    Singleton<AccessibilityCommonEvent>::GetInstance().SubscriberEvent(handler_);
1434885b47fbSopenharmony_ci    Singleton<AccessibilityDisplayManager>::GetInstance().RegisterDisplayListener(handler_);
1435885b47fbSopenharmony_ci    Singleton<AccessibilityWindowManager>::GetInstance().RegisterWindowListener(handler_);
1436885b47fbSopenharmony_ci    bool result = Singleton<AccessibilityWindowManager>::GetInstance().Init();
1437885b47fbSopenharmony_ci    HILOG_DEBUG("wms init result is %{public}d", result);
1438885b47fbSopenharmony_ci
1439885b47fbSopenharmony_ci    int32_t retry = QUERY_USER_ID_RETRY_COUNT;
1440885b47fbSopenharmony_ci    int32_t sleepTime = QUERY_USER_ID_SLEEP_TIME;
1441885b47fbSopenharmony_ci    std::vector<int32_t> accountIds;
1442885b47fbSopenharmony_ci    ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds);
1443885b47fbSopenharmony_ci    while (ret != ERR_OK || accountIds.size() == 0) {
1444885b47fbSopenharmony_ci        HILOG_DEBUG("Query account information failed, left retry count:%{public}d", retry);
1445885b47fbSopenharmony_ci        if (retry == 0) {
1446885b47fbSopenharmony_ci            HILOG_ERROR("Query account information failed!!!");
1447885b47fbSopenharmony_ci            break;
1448885b47fbSopenharmony_ci        }
1449885b47fbSopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
1450885b47fbSopenharmony_ci        ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds);
1451885b47fbSopenharmony_ci        retry--;
1452885b47fbSopenharmony_ci    }
1453885b47fbSopenharmony_ci
1454885b47fbSopenharmony_ci    if (accountIds.size() > 0) {
1455885b47fbSopenharmony_ci        HILOG_DEBUG("Query account information success, account id:%{public}d", accountIds[0]);
1456885b47fbSopenharmony_ci        SwitchedUser(accountIds[0]);
1457885b47fbSopenharmony_ci    }
1458885b47fbSopenharmony_ci
1459885b47fbSopenharmony_ci    return true;
1460885b47fbSopenharmony_ci}
1461885b47fbSopenharmony_ci
1462885b47fbSopenharmony_civoid AccessibleAbilityManagerService::InitInnerResource()
1463885b47fbSopenharmony_ci{
1464885b47fbSopenharmony_ci    UpdateSettingsInAtoHosTask();
1465885b47fbSopenharmony_ci}
1466885b47fbSopenharmony_ci
1467885b47fbSopenharmony_civoid AccessibleAbilityManagerService::InteractionOperationDeathRecipient::OnRemoteDied(
1468885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
1469885b47fbSopenharmony_ci{
1470885b47fbSopenharmony_ci    Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
1471885b47fbSopenharmony_ci        A11yError::ERROR_TARGET_APPLICATION_DISCONNECT_ABNORMALLY);
1472885b47fbSopenharmony_ci    HILOG_INFO();
1473885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData =
1474885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
1475885b47fbSopenharmony_ci    if (accountData == nullptr) {
1476885b47fbSopenharmony_ci        HILOG_ERROR("get accountData failed");
1477885b47fbSopenharmony_ci        return;
1478885b47fbSopenharmony_ci    }
1479885b47fbSopenharmony_ci    int32_t currentAccountId = accountData->GetAccountId();
1480885b47fbSopenharmony_ci    if (currentAccountId != accountId_) {
1481885b47fbSopenharmony_ci        HILOG_ERROR("check accountId failed");
1482885b47fbSopenharmony_ci        return;
1483885b47fbSopenharmony_ci    }
1484885b47fbSopenharmony_ci
1485885b47fbSopenharmony_ci    if (treeId_ > 0) {
1486885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().DeregisterElementOperator(windowId_, treeId_);
1487885b47fbSopenharmony_ci    } else {
1488885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().DeregisterElementOperator(windowId_);
1489885b47fbSopenharmony_ci    }
1490885b47fbSopenharmony_ci}
1491885b47fbSopenharmony_ci
1492885b47fbSopenharmony_cisptr<AccessibilityAccountData> AccessibleAbilityManagerService::GetCurrentAccountData()
1493885b47fbSopenharmony_ci{
1494885b47fbSopenharmony_ci    HILOG_DEBUG();
1495885b47fbSopenharmony_ci    if (currentAccountId_ == -1) {
1496885b47fbSopenharmony_ci        HILOG_ERROR("current account id is wrong");
1497885b47fbSopenharmony_ci        return nullptr;
1498885b47fbSopenharmony_ci    }
1499885b47fbSopenharmony_ci
1500885b47fbSopenharmony_ci    return a11yAccountsData_.GetCurrentAccountData(currentAccountId_);
1501885b47fbSopenharmony_ci}
1502885b47fbSopenharmony_ci
1503885b47fbSopenharmony_cisptr<AccessibilityAccountData> AccessibleAbilityManagerService::GetAccountData(int32_t accountId)
1504885b47fbSopenharmony_ci{
1505885b47fbSopenharmony_ci    HILOG_DEBUG();
1506885b47fbSopenharmony_ci    return a11yAccountsData_.GetAccountData(accountId);
1507885b47fbSopenharmony_ci}
1508885b47fbSopenharmony_ci
1509885b47fbSopenharmony_cistd::vector<int32_t> AccessibleAbilityManagerService::GetAllAccountIds()
1510885b47fbSopenharmony_ci{
1511885b47fbSopenharmony_ci    HILOG_DEBUG();
1512885b47fbSopenharmony_ci    return a11yAccountsData_.GetAllAccountIds();
1513885b47fbSopenharmony_ci}
1514885b47fbSopenharmony_ci
1515885b47fbSopenharmony_cisptr<AppExecFwk::IBundleMgr> AccessibleAbilityManagerService::GetBundleMgrProxy()
1516885b47fbSopenharmony_ci{
1517885b47fbSopenharmony_ci    HILOG_DEBUG();
1518885b47fbSopenharmony_ci    if (bundleManager_) {
1519885b47fbSopenharmony_ci        return bundleManager_;
1520885b47fbSopenharmony_ci    }
1521885b47fbSopenharmony_ci
1522885b47fbSopenharmony_ci    sptr<ISystemAbilityManager> systemAbilityManager =
1523885b47fbSopenharmony_ci        SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1524885b47fbSopenharmony_ci    if (!systemAbilityManager) {
1525885b47fbSopenharmony_ci        HILOG_ERROR("failed:fail to get system ability mgr.");
1526885b47fbSopenharmony_ci        return nullptr;
1527885b47fbSopenharmony_ci    }
1528885b47fbSopenharmony_ci
1529885b47fbSopenharmony_ci    sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1530885b47fbSopenharmony_ci    if (!remoteObject) {
1531885b47fbSopenharmony_ci        HILOG_ERROR("failed:fail to get bundle manager proxy.");
1532885b47fbSopenharmony_ci        return nullptr;
1533885b47fbSopenharmony_ci    }
1534885b47fbSopenharmony_ci
1535885b47fbSopenharmony_ci    bundleManager_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
1536885b47fbSopenharmony_ci    if (!bundleManager_) {
1537885b47fbSopenharmony_ci        HILOG_ERROR("fail to new bundle manager.");
1538885b47fbSopenharmony_ci        return nullptr;
1539885b47fbSopenharmony_ci    }
1540885b47fbSopenharmony_ci
1541885b47fbSopenharmony_ci    if (!bundleManagerDeathRecipient_) {
1542885b47fbSopenharmony_ci        bundleManagerDeathRecipient_ = new(std::nothrow) BundleManagerDeathRecipient();
1543885b47fbSopenharmony_ci        if (!bundleManagerDeathRecipient_) {
1544885b47fbSopenharmony_ci            HILOG_ERROR("bundleManagerDeathRecipient_ is null");
1545885b47fbSopenharmony_ci            return nullptr;
1546885b47fbSopenharmony_ci        }
1547885b47fbSopenharmony_ci    }
1548885b47fbSopenharmony_ci
1549885b47fbSopenharmony_ci    bundleManager_->AsObject()->AddDeathRecipient(bundleManagerDeathRecipient_);
1550885b47fbSopenharmony_ci    return bundleManager_;
1551885b47fbSopenharmony_ci}
1552885b47fbSopenharmony_ci
1553885b47fbSopenharmony_cisptr<AccessibilityWindowConnection> AccessibleAbilityManagerService::GetAccessibilityWindowConnection(
1554885b47fbSopenharmony_ci    int32_t windowId)
1555885b47fbSopenharmony_ci{
1556885b47fbSopenharmony_ci    HILOG_DEBUG("windowId(%{public}d)", windowId);
1557885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1558885b47fbSopenharmony_ci    if (!accountData) {
1559885b47fbSopenharmony_ci        HILOG_ERROR("Get account data failed");
1560885b47fbSopenharmony_ci        return nullptr;
1561885b47fbSopenharmony_ci    }
1562885b47fbSopenharmony_ci    return accountData->GetAccessibilityWindowConnection(windowId);
1563885b47fbSopenharmony_ci}
1564885b47fbSopenharmony_ci
1565885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ClearFocus(int32_t windowId)
1566885b47fbSopenharmony_ci{
1567885b47fbSopenharmony_ci    HILOG_DEBUG();
1568885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = GetAccessibilityWindowConnection(windowId);
1569885b47fbSopenharmony_ci    if (connection && connection->GetProxy()) {
1570885b47fbSopenharmony_ci        connection->GetProxy()->ClearFocus();
1571885b47fbSopenharmony_ci    }
1572885b47fbSopenharmony_ci}
1573885b47fbSopenharmony_ci
1574885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OutsideTouch(int32_t windowId)
1575885b47fbSopenharmony_ci{
1576885b47fbSopenharmony_ci    HILOG_DEBUG();
1577885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = GetAccessibilityWindowConnection(windowId);
1578885b47fbSopenharmony_ci    if (connection && connection->GetProxy()) {
1579885b47fbSopenharmony_ci        connection->GetProxy()->OutsideTouch();
1580885b47fbSopenharmony_ci    }
1581885b47fbSopenharmony_ci}
1582885b47fbSopenharmony_ci
1583885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SetTouchEventInjector(const sptr<TouchEventInjector> &touchEventInjector)
1584885b47fbSopenharmony_ci{
1585885b47fbSopenharmony_ci    HILOG_DEBUG();
1586885b47fbSopenharmony_ci    touchEventInjector_ = touchEventInjector;
1587885b47fbSopenharmony_ci}
1588885b47fbSopenharmony_ci
1589885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SetKeyEventFilter(const sptr<KeyEventFilter> &keyEventFilter)
1590885b47fbSopenharmony_ci{
1591885b47fbSopenharmony_ci    HILOG_DEBUG();
1592885b47fbSopenharmony_ci    keyEventFilter_ = keyEventFilter;
1593885b47fbSopenharmony_ci}
1594885b47fbSopenharmony_ci
1595885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StateCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1596885b47fbSopenharmony_ci{
1597885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveCallback(STATE_CALLBACK, this, remote);
1598885b47fbSopenharmony_ci}
1599885b47fbSopenharmony_ci
1600885b47fbSopenharmony_civoid AccessibleAbilityManagerService::CaptionPropertyCallbackDeathRecipient::OnRemoteDied(
1601885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
1602885b47fbSopenharmony_ci{
1603885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveCallback(CAPTION_PROPERTY_CALLBACK, this, remote);
1604885b47fbSopenharmony_ci}
1605885b47fbSopenharmony_ci
1606885b47fbSopenharmony_civoid AccessibleAbilityManagerService::EnableAbilityListsObserverDeathRecipient::OnRemoteDied(
1607885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
1608885b47fbSopenharmony_ci{
1609885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveCallback(
1610885b47fbSopenharmony_ci        ENABLE_ABILITY_LISTS_CALLBACK, this, remote);
1611885b47fbSopenharmony_ci}
1612885b47fbSopenharmony_ci
1613885b47fbSopenharmony_civoid AccessibleAbilityManagerService::AddedUser(int32_t accountId)
1614885b47fbSopenharmony_ci{
1615885b47fbSopenharmony_ci    HILOG_DEBUG();
1616885b47fbSopenharmony_ci    a11yAccountsData_.AddAccountData(accountId);
1617885b47fbSopenharmony_ci}
1618885b47fbSopenharmony_ci
1619885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RemovedUser(int32_t accountId)
1620885b47fbSopenharmony_ci{
1621885b47fbSopenharmony_ci    HILOG_DEBUG();
1622885b47fbSopenharmony_ci    if (accountId == currentAccountId_) {
1623885b47fbSopenharmony_ci        HILOG_ERROR("Remove user failed, this account is current account.");
1624885b47fbSopenharmony_ci        return;
1625885b47fbSopenharmony_ci    }
1626885b47fbSopenharmony_ci
1627885b47fbSopenharmony_ci    auto accountData = a11yAccountsData_.RemoveAccountData(accountId);
1628885b47fbSopenharmony_ci    if (accountData) {
1629885b47fbSopenharmony_ci        accountData->GetConfig()->ClearData();
1630885b47fbSopenharmony_ci        return;
1631885b47fbSopenharmony_ci    }
1632885b47fbSopenharmony_ci
1633885b47fbSopenharmony_ci    HILOG_ERROR("accountId is not exist");
1634885b47fbSopenharmony_ci}
1635885b47fbSopenharmony_ci
1636885b47fbSopenharmony_civoid AccessibleAbilityManagerService::SwitchedUser(int32_t accountId)
1637885b47fbSopenharmony_ci{
1638885b47fbSopenharmony_ci    HILOG_DEBUG();
1639885b47fbSopenharmony_ci
1640885b47fbSopenharmony_ci    if (accountId == currentAccountId_) {
1641885b47fbSopenharmony_ci        HILOG_WARN("The account is current account id.");
1642885b47fbSopenharmony_ci        return;
1643885b47fbSopenharmony_ci    }
1644885b47fbSopenharmony_ci    OffZoomGesture();
1645885b47fbSopenharmony_ci
1646885b47fbSopenharmony_ci    std::map<std::string, uint32_t> importantEnabledAbilities;
1647885b47fbSopenharmony_ci    SCREENREADER_STATE screenReaderState = SCREENREADER_STATE::UNINIT;
1648885b47fbSopenharmony_ci    if (currentAccountId_ != -1) {
1649885b47fbSopenharmony_ci        HILOG_DEBUG();
1650885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1651885b47fbSopenharmony_ci        if (!accountData) {
1652885b47fbSopenharmony_ci            HILOG_ERROR("Current account data is null");
1653885b47fbSopenharmony_ci            return;
1654885b47fbSopenharmony_ci        }
1655885b47fbSopenharmony_ci        defaultConfigCallbacks_ = accountData->GetConfigCallbacks();
1656885b47fbSopenharmony_ci        screenReaderState = accountData->GetDefaultUserScreenReaderState() ?
1657885b47fbSopenharmony_ci            SCREENREADER_STATE::ON : SCREENREADER_STATE::OFF;
1658885b47fbSopenharmony_ci        accountData->GetImportantEnabledAbilities(importantEnabledAbilities);
1659885b47fbSopenharmony_ci        accountData->OnAccountSwitched();
1660885b47fbSopenharmony_ci        UpdateAccessibilityManagerService();
1661885b47fbSopenharmony_ci    }
1662885b47fbSopenharmony_ci    currentAccountId_ = accountId;
1663885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1664885b47fbSopenharmony_ci    if (!accountData) {
1665885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr.");
1666885b47fbSopenharmony_ci        return;
1667885b47fbSopenharmony_ci    }
1668885b47fbSopenharmony_ci    accountData->Init();
1669885b47fbSopenharmony_ci    accountData->SetConfigCallbacks(defaultConfigCallbacks_);
1670885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POWER_MANAGER
1671885b47fbSopenharmony_ci    float discount = accountData->GetConfig()->GetBrightnessDiscount();
1672885b47fbSopenharmony_ci    if (!Singleton<AccessibilityPowerManager>::GetInstance().DiscountBrightness(discount)) {
1673885b47fbSopenharmony_ci        HILOG_ERROR("Failed to set brightness discount");
1674885b47fbSopenharmony_ci    }
1675885b47fbSopenharmony_ci#endif
1676885b47fbSopenharmony_ci    AccountSA::OsAccountType accountType = accountData->GetAccountType();
1677885b47fbSopenharmony_ci    if (screenReaderState != SCREENREADER_STATE::UNINIT &&
1678885b47fbSopenharmony_ci        (accountType == AccountSA::OsAccountType::PRIVATE || accountType == AccountSA::OsAccountType::ADMIN)) {
1679885b47fbSopenharmony_ci        bool state = (screenReaderState == SCREENREADER_STATE::ON) ? true : false;
1680885b47fbSopenharmony_ci        accountData->SetAbilityAutoStartState(SCREEN_READER_BUNDLE_ABILITY_NAME, state);
1681885b47fbSopenharmony_ci        HILOG_INFO("set screenreader auto-start state = %{public}d", true);
1682885b47fbSopenharmony_ci    }
1683885b47fbSopenharmony_ci
1684885b47fbSopenharmony_ci    if (accountData->GetInstalledAbilitiesFromBMS()) {
1685885b47fbSopenharmony_ci        accountData->UpdateImportantEnabledAbilities(importantEnabledAbilities);
1686885b47fbSopenharmony_ci        accountData->UpdateAbilities();
1687885b47fbSopenharmony_ci        UpdateAccessibilityManagerService();
1688885b47fbSopenharmony_ci    }
1689885b47fbSopenharmony_ci    UpdateAllSetting();
1690885b47fbSopenharmony_ci    UpdateAutoStartAbilities();
1691885b47fbSopenharmony_ci    RegisterShortKeyEvent();
1692885b47fbSopenharmony_ci    RegisterScreenMagnificationState();
1693885b47fbSopenharmony_ci    RegisterScreenMagnificationType();
1694885b47fbSopenharmony_ci}
1695885b47fbSopenharmony_ci
1696885b47fbSopenharmony_civoid AccessibleAbilityManagerService::PackageRemoved(const std::string &bundleName)
1697885b47fbSopenharmony_ci{
1698885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> packageAccount = GetCurrentAccountData();
1699885b47fbSopenharmony_ci    if (!packageAccount) {
1700885b47fbSopenharmony_ci        HILOG_ERROR("packageAccount is nullptr.");
1701885b47fbSopenharmony_ci        return;
1702885b47fbSopenharmony_ci    }
1703885b47fbSopenharmony_ci
1704885b47fbSopenharmony_ci    packageAccount->DelAutoStartPrefKeyInRemovePkg(bundleName);
1705885b47fbSopenharmony_ci    std::vector<std::string> multiTarget = packageAccount->GetConfig()->GetShortkeyMultiTarget();
1706885b47fbSopenharmony_ci    std::string name = packageAccount->GetConfig()->GetShortkeyTarget();
1707885b47fbSopenharmony_ci    auto installedAbilities_ = packageAccount->GetInstalledAbilities();
1708885b47fbSopenharmony_ci    for (auto &installAbility : installedAbilities_) {
1709885b47fbSopenharmony_ci        std::string abilityId = installAbility.GetId();
1710885b47fbSopenharmony_ci        HILOG_DEBUG("abilityId%{public}s", abilityId.c_str());
1711885b47fbSopenharmony_ci        if (bundleName != installAbility.GetPackageName()) {
1712885b47fbSopenharmony_ci            continue;
1713885b47fbSopenharmony_ci        }
1714885b47fbSopenharmony_ci        if (std::find(removedAutoStartAbilities_.begin(), removedAutoStartAbilities_.end(), abilityId)
1715885b47fbSopenharmony_ci            == removedAutoStartAbilities_.end()) {
1716885b47fbSopenharmony_ci            removedAutoStartAbilities_.push_back(abilityId);
1717885b47fbSopenharmony_ci        }
1718885b47fbSopenharmony_ci        // no use later version
1719885b47fbSopenharmony_ci        if (abilityId == name) {
1720885b47fbSopenharmony_ci            std::string targetName = "";
1721885b47fbSopenharmony_ci            packageAccount->GetConfig()->SetShortkeyTarget(targetName);
1722885b47fbSopenharmony_ci            UpdateShortkeyTarget();
1723885b47fbSopenharmony_ci        }
1724885b47fbSopenharmony_ci        // multi
1725885b47fbSopenharmony_ci        for (const auto &target : multiTarget) {
1726885b47fbSopenharmony_ci            if (target == abilityId) {
1727885b47fbSopenharmony_ci                packageAccount->GetConfig()->SetShortkeyMultiTargetInPkgRemove(abilityId);
1728885b47fbSopenharmony_ci                UpdateShortkeyMultiTarget();
1729885b47fbSopenharmony_ci            }
1730885b47fbSopenharmony_ci        }
1731885b47fbSopenharmony_ci    }
1732885b47fbSopenharmony_ci
1733885b47fbSopenharmony_ci    if (packageAccount->RemoveAbility(bundleName)) {
1734885b47fbSopenharmony_ci        HILOG_DEBUG("ability%{public}s removed!", bundleName.c_str());
1735885b47fbSopenharmony_ci        UpdateAccessibilityManagerService();
1736885b47fbSopenharmony_ci    }
1737885b47fbSopenharmony_ci}
1738885b47fbSopenharmony_ci
1739885b47fbSopenharmony_civoid AccessibleAbilityManagerService::PackageAdd(const std::string &bundleName)
1740885b47fbSopenharmony_ci{
1741885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> packageAccount = GetCurrentAccountData();
1742885b47fbSopenharmony_ci    if (!packageAccount) {
1743885b47fbSopenharmony_ci        HILOG_ERROR("packageAccount is nullptr");
1744885b47fbSopenharmony_ci        return;
1745885b47fbSopenharmony_ci    }
1746885b47fbSopenharmony_ci    for (auto &abilityId : removedAutoStartAbilities_) {
1747885b47fbSopenharmony_ci        if (packageAccount->GetAbilityAutoStartState(abilityId)) {
1748885b47fbSopenharmony_ci            packageAccount->SetAbilityAutoStartState(abilityId, false);
1749885b47fbSopenharmony_ci        }
1750885b47fbSopenharmony_ci    }
1751885b47fbSopenharmony_ci    packageAccount->AddAbility(bundleName);
1752885b47fbSopenharmony_ci}
1753885b47fbSopenharmony_ci
1754885b47fbSopenharmony_civoid AccessibleAbilityManagerService::PackageChanged(const std::string &bundleName)
1755885b47fbSopenharmony_ci{
1756885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> packageAccount = GetCurrentAccountData();
1757885b47fbSopenharmony_ci    if (!packageAccount) {
1758885b47fbSopenharmony_ci        HILOG_ERROR("packageAccount is nullptr");
1759885b47fbSopenharmony_ci        return;
1760885b47fbSopenharmony_ci    }
1761885b47fbSopenharmony_ci
1762885b47fbSopenharmony_ci    bool isNeedUpdateShortKeyTarget = false;
1763885b47fbSopenharmony_ci    std::string target = packageAccount->GetConfig()->GetShortkeyTarget();
1764885b47fbSopenharmony_ci    if (target.substr(0, target.find("/")) == bundleName) {
1765885b47fbSopenharmony_ci        isNeedUpdateShortKeyTarget = true;
1766885b47fbSopenharmony_ci    }
1767885b47fbSopenharmony_ci    std::vector<std::string> multiTarget = packageAccount->GetConfig()->GetShortkeyMultiTarget();
1768885b47fbSopenharmony_ci
1769885b47fbSopenharmony_ci    packageAccount->ChangeAbility(bundleName);
1770885b47fbSopenharmony_ci    UpdateAccessibilityManagerService();
1771885b47fbSopenharmony_ci
1772885b47fbSopenharmony_ci    std::vector<std::string> sameBundleTarget;
1773885b47fbSopenharmony_ci    auto installedAbilities_ = packageAccount->GetInstalledAbilities();
1774885b47fbSopenharmony_ci    for (auto &installAbility : installedAbilities_) {
1775885b47fbSopenharmony_ci        std::string abilityId = installAbility.GetId();
1776885b47fbSopenharmony_ci        if (bundleName != installAbility.GetPackageName()) {
1777885b47fbSopenharmony_ci            continue;
1778885b47fbSopenharmony_ci        }
1779885b47fbSopenharmony_ci        if (abilityId == target) {
1780885b47fbSopenharmony_ci            isNeedUpdateShortKeyTarget = false;
1781885b47fbSopenharmony_ci        }
1782885b47fbSopenharmony_ci        sameBundleTarget.push_back(abilityId);
1783885b47fbSopenharmony_ci    }
1784885b47fbSopenharmony_ci
1785885b47fbSopenharmony_ci    if (isNeedUpdateShortKeyTarget) {
1786885b47fbSopenharmony_ci        packageAccount->GetConfig()->SetShortkeyTarget("");
1787885b47fbSopenharmony_ci        UpdateShortkeyTarget();
1788885b47fbSopenharmony_ci    }
1789885b47fbSopenharmony_ci    std::vector<std::string> tmpAbilities = multiTarget;
1790885b47fbSopenharmony_ci    bool isNeedUpdateShortKeyMultiTarget = false;
1791885b47fbSopenharmony_ci    Utils::SelectUsefulFromVecWithSameBundle(tmpAbilities, sameBundleTarget,
1792885b47fbSopenharmony_ci        isNeedUpdateShortKeyMultiTarget, bundleName);
1793885b47fbSopenharmony_ci    if (isNeedUpdateShortKeyMultiTarget) {
1794885b47fbSopenharmony_ci        packageAccount->GetConfig()->SetShortkeyMultiTarget(tmpAbilities);
1795885b47fbSopenharmony_ci        UpdateShortkeyMultiTarget();
1796885b47fbSopenharmony_ci    }
1797885b47fbSopenharmony_ci}
1798885b47fbSopenharmony_ci
1799885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetFindFocusedElementInfoResult(
1800885b47fbSopenharmony_ci    const AccessibilityElementInfo &info, const int32_t requestId)
1801885b47fbSopenharmony_ci{
1802885b47fbSopenharmony_ci    HILOG_DEBUG("Response [requestId:%{public}d]", requestId);
1803885b47fbSopenharmony_ci    if (Singleton<AccessibleAbilityManagerService>::GetInstance().VerifyingToKenId(info.GetWindowId(),
1804885b47fbSopenharmony_ci        info.GetAccessibilityId()) == RET_OK) {
1805885b47fbSopenharmony_ci        HILOG_DEBUG("VerifyingToKenId ok");
1806885b47fbSopenharmony_ci        accessibilityInfoResult_ = info;
1807885b47fbSopenharmony_ci        promise_.set_value();
1808885b47fbSopenharmony_ci    } else {
1809885b47fbSopenharmony_ci        HILOG_DEBUG("VerifyingToKenId failed");
1810885b47fbSopenharmony_ci        promise_.set_value();
1811885b47fbSopenharmony_ci    }
1812885b47fbSopenharmony_ci}
1813885b47fbSopenharmony_ci
1814885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetSearchElementInfoByTextResult(
1815885b47fbSopenharmony_ci    const std::vector<AccessibilityElementInfo> &infos, const int32_t requestId)
1816885b47fbSopenharmony_ci{
1817885b47fbSopenharmony_ci    HILOG_DEBUG("Response [requestId:%{public}d]", requestId);
1818885b47fbSopenharmony_ci    for (auto info : infos) {
1819885b47fbSopenharmony_ci        if (Singleton<AccessibleAbilityManagerService>::GetInstance().VerifyingToKenId(info.GetWindowId(),
1820885b47fbSopenharmony_ci            info.GetAccessibilityId()) == RET_OK) {
1821885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId ok");
1822885b47fbSopenharmony_ci        } else {
1823885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId failed");
1824885b47fbSopenharmony_ci            elementInfosResult_.clear();
1825885b47fbSopenharmony_ci            promise_.set_value();
1826885b47fbSopenharmony_ci            return;
1827885b47fbSopenharmony_ci        }
1828885b47fbSopenharmony_ci        elementInfosResult_ = infos;
1829885b47fbSopenharmony_ci    }
1830885b47fbSopenharmony_ci    promise_.set_value();
1831885b47fbSopenharmony_ci}
1832885b47fbSopenharmony_ci
1833885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetSearchElementInfoByAccessibilityIdResult(
1834885b47fbSopenharmony_ci    const std::vector<AccessibilityElementInfo> &infos, const int32_t requestId)
1835885b47fbSopenharmony_ci{
1836885b47fbSopenharmony_ci    HILOG_DEBUG("Response [requestId:%{public}d]", requestId);
1837885b47fbSopenharmony_ci    for (auto info : infos) {
1838885b47fbSopenharmony_ci        if (Singleton<AccessibleAbilityManagerService>::GetInstance().VerifyingToKenId(info.GetWindowId(),
1839885b47fbSopenharmony_ci            info.GetAccessibilityId()) == RET_OK) {
1840885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId ok");
1841885b47fbSopenharmony_ci        } else {
1842885b47fbSopenharmony_ci            HILOG_DEBUG("VerifyingToKenId failed");
1843885b47fbSopenharmony_ci            elementInfosResult_.clear();
1844885b47fbSopenharmony_ci            promise_.set_value();
1845885b47fbSopenharmony_ci            return;
1846885b47fbSopenharmony_ci        }
1847885b47fbSopenharmony_ci        elementInfosResult_ = infos;
1848885b47fbSopenharmony_ci    }
1849885b47fbSopenharmony_ci    promise_.set_value();
1850885b47fbSopenharmony_ci}
1851885b47fbSopenharmony_ci
1852885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetFocusMoveSearchResult(
1853885b47fbSopenharmony_ci    const AccessibilityElementInfo &info, const int32_t requestId)
1854885b47fbSopenharmony_ci{
1855885b47fbSopenharmony_ci    HILOG_DEBUG("Response [requestId:%{public}d]", requestId);
1856885b47fbSopenharmony_ci    if (Singleton<AccessibleAbilityManagerService>::GetInstance().VerifyingToKenId(info.GetWindowId(),
1857885b47fbSopenharmony_ci        info.GetAccessibilityId()) == RET_OK) {
1858885b47fbSopenharmony_ci        HILOG_DEBUG("VerifyingToKenId ok");
1859885b47fbSopenharmony_ci        accessibilityInfoResult_ = info;
1860885b47fbSopenharmony_ci        promise_.set_value();
1861885b47fbSopenharmony_ci    } else {
1862885b47fbSopenharmony_ci        HILOG_DEBUG("VerifyingToKenId failed");
1863885b47fbSopenharmony_ci        promise_.set_value();
1864885b47fbSopenharmony_ci    }
1865885b47fbSopenharmony_ci}
1866885b47fbSopenharmony_ci
1867885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetExecuteActionResult(const bool succeeded,
1868885b47fbSopenharmony_ci    const int32_t requestId)
1869885b47fbSopenharmony_ci{
1870885b47fbSopenharmony_ci    HILOG_DEBUG("Response [result:%{public}d, requestId:%{public}d]", succeeded, requestId);
1871885b47fbSopenharmony_ci    executeActionResult_ = succeeded;
1872885b47fbSopenharmony_ci    promise_.set_value();
1873885b47fbSopenharmony_ci}
1874885b47fbSopenharmony_ci
1875885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ElementOperatorCallbackImpl::SetCursorPositionResult(const int32_t cursorPosition,
1876885b47fbSopenharmony_ci    const int32_t requestId)
1877885b47fbSopenharmony_ci{
1878885b47fbSopenharmony_ci    HILOG_INFO("ElementOperatorCallbackImpl::SetCursorPositionResult [result:%{public}d]",
1879885b47fbSopenharmony_ci        cursorPosition);
1880885b47fbSopenharmony_ci    HILOG_DEBUG("cursorPosition [result:%{public}d, requestId:%{public}d]", cursorPosition, requestId);
1881885b47fbSopenharmony_ci    callCursorPosition_ = cursorPosition;
1882885b47fbSopenharmony_ci    promise_.set_value();
1883885b47fbSopenharmony_ci}
1884885b47fbSopenharmony_ci
1885885b47fbSopenharmony_cibool AccessibleAbilityManagerService::GetParentElementRecursively(int32_t windowId, int64_t elementId,
1886885b47fbSopenharmony_ci    std::vector<AccessibilityElementInfo>& infos)
1887885b47fbSopenharmony_ci{
1888885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1889885b47fbSopenharmony_ci    if (!accountData) {
1890885b47fbSopenharmony_ci        HILOG_ERROR("GetCurrentAccountData failed");
1891885b47fbSopenharmony_ci        return false;
1892885b47fbSopenharmony_ci    }
1893885b47fbSopenharmony_ci
1894885b47fbSopenharmony_ci    int32_t treeId = 0;
1895885b47fbSopenharmony_ci    sptr<IAccessibilityElementOperator> elementOperator = nullptr;
1896885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = accountData->GetAccessibilityWindowConnection(windowId);
1897885b47fbSopenharmony_ci    if (!connection) {
1898885b47fbSopenharmony_ci        HILOG_ERROR("GetAccessibilityWindowConnection failed");
1899885b47fbSopenharmony_ci        return false;
1900885b47fbSopenharmony_ci    }
1901885b47fbSopenharmony_ci
1902885b47fbSopenharmony_ci    if (elementId > 0) {
1903885b47fbSopenharmony_ci        treeId = GetTreeIdBySplitElementId(elementId);
1904885b47fbSopenharmony_ci        elementOperator = connection->GetCardProxy(treeId);
1905885b47fbSopenharmony_ci    } else {
1906885b47fbSopenharmony_ci        elementOperator = connection->GetProxy();
1907885b47fbSopenharmony_ci    }
1908885b47fbSopenharmony_ci    if (elementOperator == nullptr) {
1909885b47fbSopenharmony_ci        HILOG_DEBUG("elementOperator failed elementId: %{public}" PRId64 " winId: %{public}d treeId: %{public}d",
1910885b47fbSopenharmony_ci            elementId, windowId, treeId);
1911885b47fbSopenharmony_ci        return false;
1912885b47fbSopenharmony_ci    }
1913885b47fbSopenharmony_ci    sptr<ElementOperatorCallbackImpl> callBack = new(std::nothrow) ElementOperatorCallbackImpl();
1914885b47fbSopenharmony_ci    if (callBack == nullptr) {
1915885b47fbSopenharmony_ci        HILOG_ERROR("Failed to create callBack.");
1916885b47fbSopenharmony_ci        return false;
1917885b47fbSopenharmony_ci    }
1918885b47fbSopenharmony_ci
1919885b47fbSopenharmony_ci    ffrt::future<void> promiseFuture = callBack->promise_.get_future();
1920885b47fbSopenharmony_ci    elementOperator->SearchElementInfoByAccessibilityId(elementId, GenerateRequestId(), callBack, 0);
1921885b47fbSopenharmony_ci    ffrt::future_status waitFocus = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
1922885b47fbSopenharmony_ci    if (waitFocus != ffrt::future_status::ready) {
1923885b47fbSopenharmony_ci        ipcTimeoutNum_++;
1924885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait result, number %{public}" PRId64 "", ipcTimeoutNum_);
1925885b47fbSopenharmony_ci        return false;
1926885b47fbSopenharmony_ci    }
1927885b47fbSopenharmony_ci
1928885b47fbSopenharmony_ci    for (auto& info : callBack->elementInfosResult_) {
1929885b47fbSopenharmony_ci        if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
1930885b47fbSopenharmony_ci            HILOG_ERROR("SearchElementInfoByAccessibilityId elementInfo from ace is wrong");
1931885b47fbSopenharmony_ci            return false;
1932885b47fbSopenharmony_ci        }
1933885b47fbSopenharmony_ci    }
1934885b47fbSopenharmony_ci
1935885b47fbSopenharmony_ci    infos = callBack->elementInfosResult_;
1936885b47fbSopenharmony_ci    HILOG_DEBUG("Get parent element success, size %{public}zu", infos.size());
1937885b47fbSopenharmony_ci    return true;
1938885b47fbSopenharmony_ci}
1939885b47fbSopenharmony_ci
1940885b47fbSopenharmony_civoid AccessibleAbilityManagerService::FindInnerWindowId(const AccessibilityEventInfo &event, int32_t& windowId)
1941885b47fbSopenharmony_ci{
1942885b47fbSopenharmony_ci    HILOG_DEBUG();
1943885b47fbSopenharmony_ci    auto mapTable = Singleton<AccessibilityWindowManager>::GetInstance().sceneBoardElementIdMap_.GetAllPairs();
1944885b47fbSopenharmony_ci    int64_t elementId = event.GetAccessibilityId();
1945885b47fbSopenharmony_ci    while (1) {
1946885b47fbSopenharmony_ci        for (auto iter = mapTable.begin(); iter != mapTable.end(); iter++) {
1947885b47fbSopenharmony_ci            if (elementId == iter->second) {
1948885b47fbSopenharmony_ci                windowId = iter->first;
1949885b47fbSopenharmony_ci                HILOG_DEBUG("inner windowId %{public}d", windowId);
1950885b47fbSopenharmony_ci                return;
1951885b47fbSopenharmony_ci            }
1952885b47fbSopenharmony_ci        }
1953885b47fbSopenharmony_ci
1954885b47fbSopenharmony_ci        std::vector<AccessibilityElementInfo> infos = {};
1955885b47fbSopenharmony_ci        if (GetParentElementRecursively(event.GetWindowId(), elementId, infos) == false || infos.size() == 0) {
1956885b47fbSopenharmony_ci            HILOG_ERROR("find parent element failed");
1957885b47fbSopenharmony_ci            return;
1958885b47fbSopenharmony_ci        }
1959885b47fbSopenharmony_ci
1960885b47fbSopenharmony_ci        if (infos[0].GetComponentType() == "root") {
1961885b47fbSopenharmony_ci            HILOG_ERROR("can not find parent element, has reach root node");
1962885b47fbSopenharmony_ci            return;
1963885b47fbSopenharmony_ci        }
1964885b47fbSopenharmony_ci
1965885b47fbSopenharmony_ci        elementId = infos[0].GetParentNodeId();
1966885b47fbSopenharmony_ci    }
1967885b47fbSopenharmony_ci}
1968885b47fbSopenharmony_ci
1969885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAccessibilityWindowStateByEvent(const AccessibilityEventInfo &event)
1970885b47fbSopenharmony_ci{
1971885b47fbSopenharmony_ci    EventType evtType = event.GetEventType();
1972885b47fbSopenharmony_ci    HILOG_DEBUG("windowId is %{public}d", event.GetWindowId());
1973885b47fbSopenharmony_ci    int32_t windowId = event.GetWindowId();
1974885b47fbSopenharmony_ci    if (windowId == 1 && (evtType == TYPE_VIEW_HOVER_ENTER_EVENT || evtType == TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT)) {
1975885b47fbSopenharmony_ci        FindInnerWindowId(event, windowId);
1976885b47fbSopenharmony_ci    }
1977885b47fbSopenharmony_ci
1978885b47fbSopenharmony_ci    switch (evtType) {
1979885b47fbSopenharmony_ci        case TYPE_VIEW_HOVER_ENTER_EVENT:
1980885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().SetActiveWindow(windowId, false);
1981885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().SetAccessibilityFocusedWindow(windowId);
1982885b47fbSopenharmony_ci            break;
1983885b47fbSopenharmony_ci        case TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT:
1984885b47fbSopenharmony_ci            Singleton<AccessibilityWindowManager>::GetInstance().SetAccessibilityFocusedWindow(windowId);
1985885b47fbSopenharmony_ci            break;
1986885b47fbSopenharmony_ci        default:
1987885b47fbSopenharmony_ci            break;
1988885b47fbSopenharmony_ci    }
1989885b47fbSopenharmony_ci}
1990885b47fbSopenharmony_ci
1991885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAccessibilityManagerService()
1992885b47fbSopenharmony_ci{
1993885b47fbSopenharmony_ci    HILOG_DEBUG("start.");
1994885b47fbSopenharmony_ci
1995885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
1996885b47fbSopenharmony_ci    if (!accountData) {
1997885b47fbSopenharmony_ci        HILOG_ERROR("Account data is null");
1998885b47fbSopenharmony_ci        return;
1999885b47fbSopenharmony_ci    }
2000885b47fbSopenharmony_ci
2001885b47fbSopenharmony_ci    accountData->UpdateAccountCapabilities();
2002885b47fbSopenharmony_ci    UpdateInputFilter();
2003885b47fbSopenharmony_ci    UpdateAccessibilityState();
2004885b47fbSopenharmony_ci    UpdateShortKeyRegister();
2005885b47fbSopenharmony_ci}
2006885b47fbSopenharmony_ci
2007885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAccessibilityState()
2008885b47fbSopenharmony_ci{
2009885b47fbSopenharmony_ci    HILOG_DEBUG("start.");
2010885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2011885b47fbSopenharmony_ci    if (!accountData) {
2012885b47fbSopenharmony_ci        HILOG_ERROR("Account data is null");
2013885b47fbSopenharmony_ci        return;
2014885b47fbSopenharmony_ci    }
2015885b47fbSopenharmony_ci    uint32_t state = accountData->GetAccessibilityState();
2016885b47fbSopenharmony_ci    if (!(state & STATE_ACCESSIBILITY_ENABLED)) {
2017885b47fbSopenharmony_ci        Singleton<AccessibilityWindowManager>::GetInstance().ClearAccessibilityFocused();
2018885b47fbSopenharmony_ci    }
2019885b47fbSopenharmony_ci
2020885b47fbSopenharmony_ci    stateObservers_.OnStateObservers(state);
2021885b47fbSopenharmony_ci}
2022885b47fbSopenharmony_ci
2023885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateCaptionProperty()
2024885b47fbSopenharmony_ci{
2025885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateCaptionProperty();
2026885b47fbSopenharmony_ci}
2027885b47fbSopenharmony_ci
2028885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateSettingsInAtoHosTask()
2029885b47fbSopenharmony_ci{
2030885b47fbSopenharmony_ci    HILOG_DEBUG();
2031885b47fbSopenharmony_ci    if (!handler_) {
2032885b47fbSopenharmony_ci        HILOG_ERROR("UpdateSettingsInAtoHosTask: handler is nullptr!");
2033885b47fbSopenharmony_ci        return;
2034885b47fbSopenharmony_ci    }
2035885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2036885b47fbSopenharmony_ci        UpdateSettingsInAtoHos();
2037885b47fbSopenharmony_ci        }, "UPDATE_SETTINGS_IN_ATOHOS_TASK");
2038885b47fbSopenharmony_ci}
2039885b47fbSopenharmony_ci
2040885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAutoStartAbilities()
2041885b47fbSopenharmony_ci{
2042885b47fbSopenharmony_ci    HILOG_DEBUG();
2043885b47fbSopenharmony_ci    if (!handler_) {
2044885b47fbSopenharmony_ci        HILOG_ERROR("UpdateAutoStartAbilities: handler is nullptr.");
2045885b47fbSopenharmony_ci        return;
2046885b47fbSopenharmony_ci    }
2047885b47fbSopenharmony_ci
2048885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2049885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2050885b47fbSopenharmony_ci        if (!accountData) {
2051885b47fbSopenharmony_ci            HILOG_ERROR("Account data is null");
2052885b47fbSopenharmony_ci            return;
2053885b47fbSopenharmony_ci        }
2054885b47fbSopenharmony_ci        for (auto &abilityId : removedAutoStartAbilities_) {
2055885b47fbSopenharmony_ci            if (accountData->GetAbilityAutoStartState(abilityId)) {
2056885b47fbSopenharmony_ci                accountData->SetAbilityAutoStartState(abilityId, false);
2057885b47fbSopenharmony_ci            }
2058885b47fbSopenharmony_ci        }
2059885b47fbSopenharmony_ci        accountData->UpdateAutoStartEnabledAbilities();
2060885b47fbSopenharmony_ci        accountData->UpdateAbilities();
2061885b47fbSopenharmony_ci        }, "UPDATE_AUTO_START_ABILITIES");
2062885b47fbSopenharmony_ci}
2063885b47fbSopenharmony_ci
2064885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateSettingsInAtoHos()
2065885b47fbSopenharmony_ci{
2066885b47fbSopenharmony_ci    HILOG_DEBUG();
2067885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2068885b47fbSopenharmony_ci    if (!accountData) {
2069885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr.");
2070885b47fbSopenharmony_ci        return;
2071885b47fbSopenharmony_ci    }
2072885b47fbSopenharmony_ci
2073885b47fbSopenharmony_ci    if (!accountData->GetConfig()->GetStartFromAtoHosState() || currentAccountId_ != DEFAULT_ACCOUNT_ID) {
2074885b47fbSopenharmony_ci        HILOG_INFO("Not first start from a to hos.");
2075885b47fbSopenharmony_ci        return;
2076885b47fbSopenharmony_ci    }
2077885b47fbSopenharmony_ci    accessibilitySettings_->UpdateSettingsInAtoHos();
2078885b47fbSopenharmony_ci}
2079885b47fbSopenharmony_ci
2080885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateInputFilter()
2081885b47fbSopenharmony_ci{
2082885b47fbSopenharmony_ci    HILOG_DEBUG("start.");
2083885b47fbSopenharmony_ci
2084885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2085885b47fbSopenharmony_ci    if (!accountData) {
2086885b47fbSopenharmony_ci        HILOG_ERROR("Account data is null");
2087885b47fbSopenharmony_ci        return;
2088885b47fbSopenharmony_ci    }
2089885b47fbSopenharmony_ci
2090885b47fbSopenharmony_ci    uint32_t flag = accountData->GetInputFilterFlag();
2091885b47fbSopenharmony_ci    HILOG_DEBUG("InputInterceptor flag is %{public}d", flag);
2092885b47fbSopenharmony_ci
2093885b47fbSopenharmony_ci    inputInterceptor_ = AccessibilityInputInterceptor::GetInstance();
2094885b47fbSopenharmony_ci    if (!inputInterceptor_) {
2095885b47fbSopenharmony_ci        HILOG_ERROR("inputInterceptor_ is null.");
2096885b47fbSopenharmony_ci        return;
2097885b47fbSopenharmony_ci    }
2098885b47fbSopenharmony_ci    inputInterceptor_->SetAvailableFunctions(flag);
2099885b47fbSopenharmony_ci    Utils::RecordStartingA11yEvent(flag);
2100885b47fbSopenharmony_ci}
2101885b47fbSopenharmony_ci
2102885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAllSetting()
2103885b47fbSopenharmony_ci{
2104885b47fbSopenharmony_ci    accessibilitySettings_->UpdateAllSetting();
2105885b47fbSopenharmony_ci}
2106885b47fbSopenharmony_ci
2107885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateShortKeyRegister()
2108885b47fbSopenharmony_ci{
2109885b47fbSopenharmony_ci    HILOG_DEBUG();
2110885b47fbSopenharmony_ci
2111885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2112885b47fbSopenharmony_ci    if (accountData == nullptr) {
2113885b47fbSopenharmony_ci        HILOG_ERROR("Account data is null!");
2114885b47fbSopenharmony_ci        return;
2115885b47fbSopenharmony_ci    }
2116885b47fbSopenharmony_ci
2117885b47fbSopenharmony_ci    bool shortKeyState = accountData->GetConfig()->GetShortKeyState();
2118885b47fbSopenharmony_ci    if (shortKeyState) {
2119885b47fbSopenharmony_ci        accessibilityShortKey_->Unregister();
2120885b47fbSopenharmony_ci        accessibilityShortKey_->Register();
2121885b47fbSopenharmony_ci    } else {
2122885b47fbSopenharmony_ci        accessibilityShortKey_->Unregister();
2123885b47fbSopenharmony_ci    }
2124885b47fbSopenharmony_ci}
2125885b47fbSopenharmony_ci
2126885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetScreenMagnificationState(const bool state)
2127885b47fbSopenharmony_ci{
2128885b47fbSopenharmony_ci    return accessibilitySettings_->SetScreenMagnificationState(state);
2129885b47fbSopenharmony_ci}
2130885b47fbSopenharmony_ci
2131885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetShortKeyState(const bool state)
2132885b47fbSopenharmony_ci{
2133885b47fbSopenharmony_ci    return accessibilitySettings_->SetShortKeyState(state);
2134885b47fbSopenharmony_ci}
2135885b47fbSopenharmony_ci
2136885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetMouseKeyState(const bool state)
2137885b47fbSopenharmony_ci{
2138885b47fbSopenharmony_ci    return accessibilitySettings_->SetMouseKeyState(state);
2139885b47fbSopenharmony_ci}
2140885b47fbSopenharmony_ci
2141885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetMouseAutoClick(const int32_t time)
2142885b47fbSopenharmony_ci{
2143885b47fbSopenharmony_ci    return accessibilitySettings_->SetMouseAutoClick(time);
2144885b47fbSopenharmony_ci}
2145885b47fbSopenharmony_ci
2146885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetShortkeyTarget(const std::string &name)
2147885b47fbSopenharmony_ci{
2148885b47fbSopenharmony_ci    return accessibilitySettings_->SetShortkeyTarget(name);
2149885b47fbSopenharmony_ci}
2150885b47fbSopenharmony_ci
2151885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetShortkeyMultiTarget(const std::vector<std::string> &name)
2152885b47fbSopenharmony_ci{
2153885b47fbSopenharmony_ci    return accessibilitySettings_->SetShortkeyMultiTarget(name);
2154885b47fbSopenharmony_ci}
2155885b47fbSopenharmony_ci
2156885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetHighContrastTextState(const bool state)
2157885b47fbSopenharmony_ci{
2158885b47fbSopenharmony_ci    return accessibilitySettings_->SetHighContrastTextState(state);
2159885b47fbSopenharmony_ci}
2160885b47fbSopenharmony_ci
2161885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetDaltonizationState(const bool state)
2162885b47fbSopenharmony_ci{
2163885b47fbSopenharmony_ci    return accessibilitySettings_->SetDaltonizationState(state);
2164885b47fbSopenharmony_ci}
2165885b47fbSopenharmony_ci
2166885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetInvertColorState(const bool state)
2167885b47fbSopenharmony_ci{
2168885b47fbSopenharmony_ci    return accessibilitySettings_->SetInvertColorState(state);
2169885b47fbSopenharmony_ci}
2170885b47fbSopenharmony_ci
2171885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetAnimationOffState(const bool state)
2172885b47fbSopenharmony_ci{
2173885b47fbSopenharmony_ci    return accessibilitySettings_->SetAnimationOffState(state);
2174885b47fbSopenharmony_ci}
2175885b47fbSopenharmony_ci
2176885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetAudioMonoState(const bool state)
2177885b47fbSopenharmony_ci{
2178885b47fbSopenharmony_ci    return accessibilitySettings_->SetAudioMonoState(state);
2179885b47fbSopenharmony_ci}
2180885b47fbSopenharmony_ci
2181885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetDaltonizationColorFilter(const uint32_t filter)
2182885b47fbSopenharmony_ci{
2183885b47fbSopenharmony_ci    return accessibilitySettings_->SetDaltonizationColorFilter(filter);
2184885b47fbSopenharmony_ci}
2185885b47fbSopenharmony_ci
2186885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetContentTimeout(const uint32_t time)
2187885b47fbSopenharmony_ci{
2188885b47fbSopenharmony_ci    return accessibilitySettings_->SetContentTimeout(time);
2189885b47fbSopenharmony_ci}
2190885b47fbSopenharmony_ci
2191885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetBrightnessDiscount(const float discount)
2192885b47fbSopenharmony_ci{
2193885b47fbSopenharmony_ci    return accessibilitySettings_->SetBrightnessDiscount(discount);
2194885b47fbSopenharmony_ci}
2195885b47fbSopenharmony_ci
2196885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetAudioBalance(const float balance)
2197885b47fbSopenharmony_ci{
2198885b47fbSopenharmony_ci    return accessibilitySettings_->SetAudioBalance(balance);
2199885b47fbSopenharmony_ci}
2200885b47fbSopenharmony_ci
2201885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetClickResponseTime(const uint32_t time)
2202885b47fbSopenharmony_ci{
2203885b47fbSopenharmony_ci    return accessibilitySettings_->SetClickResponseTime(time);
2204885b47fbSopenharmony_ci}
2205885b47fbSopenharmony_ci
2206885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetIgnoreRepeatClickState(const bool state)
2207885b47fbSopenharmony_ci{
2208885b47fbSopenharmony_ci    return accessibilitySettings_->SetIgnoreRepeatClickState(state);
2209885b47fbSopenharmony_ci}
2210885b47fbSopenharmony_ci
2211885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::SetIgnoreRepeatClickTime(const uint32_t time)
2212885b47fbSopenharmony_ci{
2213885b47fbSopenharmony_ci    return accessibilitySettings_->SetIgnoreRepeatClickTime(time);
2214885b47fbSopenharmony_ci}
2215885b47fbSopenharmony_ci
2216885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetScreenMagnificationState(bool &state)
2217885b47fbSopenharmony_ci{
2218885b47fbSopenharmony_ci    return accessibilitySettings_->GetScreenMagnificationState(state);
2219885b47fbSopenharmony_ci}
2220885b47fbSopenharmony_ci
2221885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetShortKeyState(bool &state)
2222885b47fbSopenharmony_ci{
2223885b47fbSopenharmony_ci    return accessibilitySettings_->GetShortKeyState(state);
2224885b47fbSopenharmony_ci}
2225885b47fbSopenharmony_ci
2226885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetMouseKeyState(bool &state)
2227885b47fbSopenharmony_ci{
2228885b47fbSopenharmony_ci    return accessibilitySettings_->GetMouseKeyState(state);
2229885b47fbSopenharmony_ci}
2230885b47fbSopenharmony_ci
2231885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetMouseAutoClick(int32_t &time)
2232885b47fbSopenharmony_ci{
2233885b47fbSopenharmony_ci    return accessibilitySettings_->GetMouseAutoClick(time);
2234885b47fbSopenharmony_ci}
2235885b47fbSopenharmony_ci
2236885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetShortkeyTarget(std::string &name)
2237885b47fbSopenharmony_ci{
2238885b47fbSopenharmony_ci    return accessibilitySettings_->GetShortkeyTarget(name);
2239885b47fbSopenharmony_ci}
2240885b47fbSopenharmony_ci
2241885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetShortkeyMultiTarget(std::vector<std::string> &name)
2242885b47fbSopenharmony_ci{
2243885b47fbSopenharmony_ci    return accessibilitySettings_->GetShortkeyMultiTarget(name);
2244885b47fbSopenharmony_ci}
2245885b47fbSopenharmony_ci
2246885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetHighContrastTextState(bool &state)
2247885b47fbSopenharmony_ci{
2248885b47fbSopenharmony_ci    return accessibilitySettings_->GetHighContrastTextState(state);
2249885b47fbSopenharmony_ci}
2250885b47fbSopenharmony_ci
2251885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetDaltonizationState(bool &state)
2252885b47fbSopenharmony_ci{
2253885b47fbSopenharmony_ci    return accessibilitySettings_->GetDaltonizationState(state);
2254885b47fbSopenharmony_ci}
2255885b47fbSopenharmony_ci
2256885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetInvertColorState(bool &state)
2257885b47fbSopenharmony_ci{
2258885b47fbSopenharmony_ci    return accessibilitySettings_->GetInvertColorState(state);
2259885b47fbSopenharmony_ci}
2260885b47fbSopenharmony_ci
2261885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetAnimationOffState(bool &state)
2262885b47fbSopenharmony_ci{
2263885b47fbSopenharmony_ci    return accessibilitySettings_->GetAnimationOffState(state);
2264885b47fbSopenharmony_ci}
2265885b47fbSopenharmony_ci
2266885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetAudioMonoState(bool &state)
2267885b47fbSopenharmony_ci{
2268885b47fbSopenharmony_ci    return accessibilitySettings_->GetAudioMonoState(state);
2269885b47fbSopenharmony_ci}
2270885b47fbSopenharmony_ci
2271885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetDaltonizationColorFilter(uint32_t &type)
2272885b47fbSopenharmony_ci{
2273885b47fbSopenharmony_ci    return accessibilitySettings_->GetDaltonizationColorFilter(type);
2274885b47fbSopenharmony_ci}
2275885b47fbSopenharmony_ci
2276885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetContentTimeout(uint32_t &timer)
2277885b47fbSopenharmony_ci{
2278885b47fbSopenharmony_ci    return accessibilitySettings_->GetContentTimeout(timer);
2279885b47fbSopenharmony_ci}
2280885b47fbSopenharmony_ci
2281885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetBrightnessDiscount(float &brightness)
2282885b47fbSopenharmony_ci{
2283885b47fbSopenharmony_ci    return accessibilitySettings_->GetBrightnessDiscount(brightness);
2284885b47fbSopenharmony_ci}
2285885b47fbSopenharmony_ci
2286885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetAudioBalance(float &balance)
2287885b47fbSopenharmony_ci{
2288885b47fbSopenharmony_ci    return accessibilitySettings_->GetAudioBalance(balance);
2289885b47fbSopenharmony_ci}
2290885b47fbSopenharmony_ci
2291885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetClickResponseTime(uint32_t &time)
2292885b47fbSopenharmony_ci{
2293885b47fbSopenharmony_ci    return accessibilitySettings_->GetClickResponseTime(time);
2294885b47fbSopenharmony_ci}
2295885b47fbSopenharmony_ci
2296885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetIgnoreRepeatClickState(bool &state)
2297885b47fbSopenharmony_ci{
2298885b47fbSopenharmony_ci    return accessibilitySettings_->GetIgnoreRepeatClickState(state);
2299885b47fbSopenharmony_ci}
2300885b47fbSopenharmony_ci
2301885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetIgnoreRepeatClickTime(uint32_t &time)
2302885b47fbSopenharmony_ci{
2303885b47fbSopenharmony_ci    return accessibilitySettings_->GetIgnoreRepeatClickTime(time);
2304885b47fbSopenharmony_ci}
2305885b47fbSopenharmony_ci
2306885b47fbSopenharmony_civoid AccessibleAbilityManagerService::GetAllConfigs(AccessibilityConfigData &configData)
2307885b47fbSopenharmony_ci{
2308885b47fbSopenharmony_ci    HILOG_DEBUG();
2309885b47fbSopenharmony_ci    XCollieHelper timer(TIMER_GET_ALL_CONFIG, XCOLLIE_TIMEOUT);
2310885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<void>> syncPromise = std::make_shared<ffrt::promise<void>>();
2311885b47fbSopenharmony_ci    std::shared_ptr<AccessibilityConfigData> config = std::make_shared<AccessibilityConfigData>();
2312885b47fbSopenharmony_ci    if (syncPromise == nullptr || config == nullptr) {
2313885b47fbSopenharmony_ci        HILOG_WARN("create syncPromise or config failed");
2314885b47fbSopenharmony_ci        return;
2315885b47fbSopenharmony_ci    }
2316885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromise->get_future();
2317885b47fbSopenharmony_ci    actionHandler_->PostTask([this, syncPromise, config]() {
2318885b47fbSopenharmony_ci        HILOG_DEBUG();
2319885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2320885b47fbSopenharmony_ci        if (!accountData) {
2321885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
2322885b47fbSopenharmony_ci            syncPromise->set_value();
2323885b47fbSopenharmony_ci            return;
2324885b47fbSopenharmony_ci        }
2325885b47fbSopenharmony_ci
2326885b47fbSopenharmony_ci        config->highContrastText_ = accountData->GetConfig()->GetHighContrastTextState();
2327885b47fbSopenharmony_ci        config->daltonizationState_ = accountData->GetConfig()->GetDaltonizationState();
2328885b47fbSopenharmony_ci        config->invertColor_ = accountData->GetConfig()->GetInvertColorState();
2329885b47fbSopenharmony_ci        config->animationOff_ = accountData->GetConfig()->GetAnimationOffState();
2330885b47fbSopenharmony_ci        config->audioMono_ = accountData->GetConfig()->GetAudioMonoState();
2331885b47fbSopenharmony_ci        config->mouseKey_ = accountData->GetConfig()->GetMouseKeyState();
2332885b47fbSopenharmony_ci        config->captionState_ = accountData->GetConfig()->GetCaptionState();
2333885b47fbSopenharmony_ci        config->screenMagnifier_ = accountData->GetConfig()->GetScreenMagnificationState();
2334885b47fbSopenharmony_ci        config->shortkey_ = accountData->GetConfig()->GetShortKeyState();
2335885b47fbSopenharmony_ci        config->mouseAutoClick_ = accountData->GetConfig()->GetMouseAutoClick();
2336885b47fbSopenharmony_ci        config->daltonizationColorFilter_ = accountData->GetConfig()->GetDaltonizationColorFilter();
2337885b47fbSopenharmony_ci        config->contentTimeout_ = accountData->GetConfig()->GetContentTimeout();
2338885b47fbSopenharmony_ci        config->brightnessDiscount_ = accountData->GetConfig()->GetBrightnessDiscount();
2339885b47fbSopenharmony_ci        config->audioBalance_ = accountData->GetConfig()->GetAudioBalance();
2340885b47fbSopenharmony_ci        config->shortkeyTarget_ = accountData->GetConfig()->GetShortkeyTarget();
2341885b47fbSopenharmony_ci        config->shortkeyMultiTarget_ = accountData->GetConfig()->GetShortkeyMultiTarget();
2342885b47fbSopenharmony_ci        config->captionProperty_ = accountData->GetConfig()->GetCaptionProperty();
2343885b47fbSopenharmony_ci        syncPromise->set_value();
2344885b47fbSopenharmony_ci        }, "TASK_GET_ALL_CONFIGS");
2345885b47fbSopenharmony_ci
2346885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
2347885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
2348885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait GetAllConfigs result");
2349885b47fbSopenharmony_ci        return;
2350885b47fbSopenharmony_ci    }
2351885b47fbSopenharmony_ci    configData = *config;
2352885b47fbSopenharmony_ci    return syncFuture.get();
2353885b47fbSopenharmony_ci}
2354885b47fbSopenharmony_ci
2355885b47fbSopenharmony_cibool AccessibleAbilityManagerService::EnableShortKeyTargetAbility(const std::string &name)
2356885b47fbSopenharmony_ci{
2357885b47fbSopenharmony_ci    HILOG_DEBUG();
2358885b47fbSopenharmony_ci    HILOG_INFO("EnableShortKeyTargetAbility name = %{public}s", name.c_str());
2359885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
2360885b47fbSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "EnableShortKeyTargetAbility");
2361885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
2362885b47fbSopenharmony_ci
2363885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2364885b47fbSopenharmony_ci    if (!accountData) {
2365885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2366885b47fbSopenharmony_ci        return false;
2367885b47fbSopenharmony_ci    }
2368885b47fbSopenharmony_ci
2369885b47fbSopenharmony_ci    std::string targetAbility;
2370885b47fbSopenharmony_ci    if (name != "") {
2371885b47fbSopenharmony_ci        targetAbility = name;
2372885b47fbSopenharmony_ci    } else {
2373885b47fbSopenharmony_ci        targetAbility = accountData->GetConfig()->GetShortkeyTarget();
2374885b47fbSopenharmony_ci        HILOG_DEBUG("target ability is [%{public}s]", targetAbility.c_str());
2375885b47fbSopenharmony_ci        if (targetAbility == "") {
2376885b47fbSopenharmony_ci            HILOG_ERROR("target ability is null");
2377885b47fbSopenharmony_ci            return false;
2378885b47fbSopenharmony_ci        }
2379885b47fbSopenharmony_ci    }
2380885b47fbSopenharmony_ci
2381885b47fbSopenharmony_ci    auto it = AccessibilityConfigTable.find(targetAbility);
2382885b47fbSopenharmony_ci    if (it != AccessibilityConfigTable.end()) {
2383885b47fbSopenharmony_ci        return SetTargetAbility(it->second);
2384885b47fbSopenharmony_ci    }
2385885b47fbSopenharmony_ci
2386885b47fbSopenharmony_ci    uint32_t capabilities = CAPABILITY_GESTURE | CAPABILITY_KEY_EVENT_OBSERVER | CAPABILITY_RETRIEVE |
2387885b47fbSopenharmony_ci        CAPABILITY_TOUCH_GUIDE | CAPABILITY_ZOOM;
2388885b47fbSopenharmony_ci    RetError enableState = accountData->EnableAbility(targetAbility, capabilities);
2389885b47fbSopenharmony_ci    if (enableState == RET_ERR_CONNECTION_EXIST) {
2390885b47fbSopenharmony_ci        HILOG_DEBUG();
2391885b47fbSopenharmony_ci        return InnerDisableAbility(targetAbility) == RET_OK;
2392885b47fbSopenharmony_ci    }
2393885b47fbSopenharmony_ci    return enableState == RET_OK;
2394885b47fbSopenharmony_ci}
2395885b47fbSopenharmony_ci
2396885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnShortKeyProcess()
2397885b47fbSopenharmony_ci{
2398885b47fbSopenharmony_ci    HILOG_DEBUG();
2399885b47fbSopenharmony_ci
2400885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2401885b47fbSopenharmony_ci    if (accountData == nullptr) {
2402885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2403885b47fbSopenharmony_ci        return;
2404885b47fbSopenharmony_ci    }
2405885b47fbSopenharmony_ci
2406885b47fbSopenharmony_ci    std::shared_ptr<AccessibilityShortkeyDialog> shortkeyDialog = std::make_shared<AccessibilityShortkeyDialog>();
2407885b47fbSopenharmony_ci
2408885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingProvider> service =
2409885b47fbSopenharmony_ci        AccessibilitySettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
2410885b47fbSopenharmony_ci    if (service == nullptr) {
2411885b47fbSopenharmony_ci        HILOG_ERROR("service is nullptr");
2412885b47fbSopenharmony_ci        return;
2413885b47fbSopenharmony_ci    }
2414885b47fbSopenharmony_ci    bool oobeState = false;
2415885b47fbSopenharmony_ci    bool userSetupState = false;
2416885b47fbSopenharmony_ci    service->GetBoolValue(DEVICE_PROVISIONED, oobeState);
2417885b47fbSopenharmony_ci    if (accountData->GetConfig()->GetDbHandle()) {
2418885b47fbSopenharmony_ci        userSetupState = accountData->GetConfig()->GetDbHandle()->GetBoolValue(USER_SETUP_COMPLETED, false);
2419885b47fbSopenharmony_ci    }
2420885b47fbSopenharmony_ci    if (oobeState && userSetupState) {
2421885b47fbSopenharmony_ci        int32_t shortKeyTimeout = accountData->GetConfig()->GetShortKeyTimeout();
2422885b47fbSopenharmony_ci        if (shortKeyTimeout == SHORT_KEY_TIMEOUT_BEFORE_USE) {
2423885b47fbSopenharmony_ci            HILOG_INFO("first use short cut key");
2424885b47fbSopenharmony_ci            accountData->GetConfig()->SetShortKeyTimeout(SHORT_KEY_TIMEOUT_AFTER_USE);
2425885b47fbSopenharmony_ci            shortkeyDialog->ConnectDialog(ShortKeyDialogType::RECONFIRM);
2426885b47fbSopenharmony_ci            return;
2427885b47fbSopenharmony_ci        }
2428885b47fbSopenharmony_ci    }
2429885b47fbSopenharmony_ci
2430885b47fbSopenharmony_ci    std::vector<std::string> shortkeyMultiTarget = accountData->GetConfig()->GetShortkeyMultiTarget();
2431885b47fbSopenharmony_ci    if (shortkeyMultiTarget.size() == 0) {
2432885b47fbSopenharmony_ci        EnableShortKeyTargetAbility();
2433885b47fbSopenharmony_ci    } else if (shortkeyMultiTarget.size() == 1) {
2434885b47fbSopenharmony_ci        EnableShortKeyTargetAbility(shortkeyMultiTarget[0]);
2435885b47fbSopenharmony_ci    } else {
2436885b47fbSopenharmony_ci        // dialog
2437885b47fbSopenharmony_ci        if (shortkeyDialog->ConnectDialog(ShortKeyDialogType::FUNCTION_SELECT)) {
2438885b47fbSopenharmony_ci            HILOG_DEBUG("ready to build dialog");
2439885b47fbSopenharmony_ci        }
2440885b47fbSopenharmony_ci    }
2441885b47fbSopenharmony_ci}
2442885b47fbSopenharmony_ci
2443885b47fbSopenharmony_cibool AccessibleAbilityManagerService::DisableShortKeyTargetAbility()
2444885b47fbSopenharmony_ci{
2445885b47fbSopenharmony_ci    HILOG_DEBUG();
2446885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2447885b47fbSopenharmony_ci    if (!accountData) {
2448885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2449885b47fbSopenharmony_ci        return false;
2450885b47fbSopenharmony_ci    }
2451885b47fbSopenharmony_ci
2452885b47fbSopenharmony_ci    std::string targetAbility = accountData->GetConfig()->GetShortkeyTarget();
2453885b47fbSopenharmony_ci    HILOG_DEBUG("target ability is [%{public}s]", targetAbility.c_str());
2454885b47fbSopenharmony_ci    if (targetAbility == "") {
2455885b47fbSopenharmony_ci        HILOG_ERROR("target ability is null");
2456885b47fbSopenharmony_ci        return false;
2457885b47fbSopenharmony_ci    }
2458885b47fbSopenharmony_ci    return InnerDisableAbility(targetAbility) == RET_OK;
2459885b47fbSopenharmony_ci}
2460885b47fbSopenharmony_ci
2461885b47fbSopenharmony_ciuint32_t AccessibleAbilityManagerService::RegisterConfigObserver(
2462885b47fbSopenharmony_ci    const sptr<IAccessibleAbilityManagerConfigObserver> &callback)
2463885b47fbSopenharmony_ci{
2464885b47fbSopenharmony_ci    HILOG_DEBUG();
2465885b47fbSopenharmony_ci    if (!callback || !actionHandler_) {
2466885b47fbSopenharmony_ci        HILOG_ERROR("Parameters check failed!");
2467885b47fbSopenharmony_ci        return ERR_INVALID_VALUE;
2468885b47fbSopenharmony_ci    }
2469885b47fbSopenharmony_ci    XCollieHelper timer(TIMER_REGISTER_CONFIG_OBSERVER, XCOLLIE_TIMEOUT);
2470885b47fbSopenharmony_ci    std::shared_ptr<ffrt::promise<uint32_t>> syncPromisePtr = std::make_shared<ffrt::promise<uint32_t>>();
2471885b47fbSopenharmony_ci    ffrt::future syncFuture = syncPromisePtr->get_future();
2472885b47fbSopenharmony_ci    actionHandler_->PostTask([this, syncPromisePtr, callback]() {
2473885b47fbSopenharmony_ci        HILOG_DEBUG();
2474885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2475885b47fbSopenharmony_ci        if (!accountData) {
2476885b47fbSopenharmony_ci            HILOG_ERROR("Account data is null");
2477885b47fbSopenharmony_ci            syncPromisePtr->set_value(ERR_INVALID_VALUE);
2478885b47fbSopenharmony_ci            return;
2479885b47fbSopenharmony_ci        }
2480885b47fbSopenharmony_ci        if (!configCallbackDeathRecipient_) {
2481885b47fbSopenharmony_ci            configCallbackDeathRecipient_ = new(std::nothrow) ConfigCallbackDeathRecipient();
2482885b47fbSopenharmony_ci            if (!configCallbackDeathRecipient_) {
2483885b47fbSopenharmony_ci                HILOG_ERROR("configCallbackDeathRecipient_ is null");
2484885b47fbSopenharmony_ci                syncPromisePtr->set_value(ERR_INVALID_VALUE);
2485885b47fbSopenharmony_ci                return;
2486885b47fbSopenharmony_ci            }
2487885b47fbSopenharmony_ci        }
2488885b47fbSopenharmony_ci        if (!callback->AsObject()) {
2489885b47fbSopenharmony_ci            HILOG_ERROR("object is null");
2490885b47fbSopenharmony_ci            syncPromisePtr->set_value(0);
2491885b47fbSopenharmony_ci            return;
2492885b47fbSopenharmony_ci        }
2493885b47fbSopenharmony_ci        callback->AsObject()->AddDeathRecipient(configCallbackDeathRecipient_);
2494885b47fbSopenharmony_ci        accountData->AddConfigCallback(callback);
2495885b47fbSopenharmony_ci        HILOG_DEBUG("the size of caption property callbacks is %{public}zu",
2496885b47fbSopenharmony_ci            accountData->GetConfigCallbacks().size());
2497885b47fbSopenharmony_ci        syncPromisePtr->set_value(NO_ERROR);
2498885b47fbSopenharmony_ci        }, "TASK_REGISTER_CONFIG_OBSERVER");
2499885b47fbSopenharmony_ci
2500885b47fbSopenharmony_ci    ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
2501885b47fbSopenharmony_ci    if (wait != ffrt::future_status::ready) {
2502885b47fbSopenharmony_ci        HILOG_ERROR("Failed to wait RegisterConfigObserver result");
2503885b47fbSopenharmony_ci        return RET_ERR_TIME_OUT;
2504885b47fbSopenharmony_ci    }
2505885b47fbSopenharmony_ci    return syncFuture.get();
2506885b47fbSopenharmony_ci}
2507885b47fbSopenharmony_ci
2508885b47fbSopenharmony_civoid AccessibleAbilityManagerService::ConfigCallbackDeathRecipient::OnRemoteDied(
2509885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
2510885b47fbSopenharmony_ci{
2511885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveCallback(CONFIG_CALLBACK, this, remote);
2512885b47fbSopenharmony_ci}
2513885b47fbSopenharmony_ci
2514885b47fbSopenharmony_civoid AccessibleAbilityManagerService::BundleManagerDeathRecipient::OnRemoteDied(
2515885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
2516885b47fbSopenharmony_ci{
2517885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().OnBundleManagerDied(remote);
2518885b47fbSopenharmony_ci}
2519885b47fbSopenharmony_ci
2520885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateConfigState()
2521885b47fbSopenharmony_ci{
2522885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateConfigState();
2523885b47fbSopenharmony_ci}
2524885b47fbSopenharmony_ci
2525885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateAudioBalance()
2526885b47fbSopenharmony_ci{
2527885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateAudioBalance();
2528885b47fbSopenharmony_ci}
2529885b47fbSopenharmony_ci
2530885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateBrightnessDiscount()
2531885b47fbSopenharmony_ci{
2532885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateBrightnessDiscount();
2533885b47fbSopenharmony_ci}
2534885b47fbSopenharmony_ci
2535885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateContentTimeout()
2536885b47fbSopenharmony_ci{
2537885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateContentTimeout();
2538885b47fbSopenharmony_ci}
2539885b47fbSopenharmony_ci
2540885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateDaltonizationColorFilter()
2541885b47fbSopenharmony_ci{
2542885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateDaltonizationColorFilter();
2543885b47fbSopenharmony_ci}
2544885b47fbSopenharmony_ci
2545885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateMouseAutoClick()
2546885b47fbSopenharmony_ci{
2547885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateMouseAutoClick();
2548885b47fbSopenharmony_ci}
2549885b47fbSopenharmony_ci
2550885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateShortkeyTarget()
2551885b47fbSopenharmony_ci{
2552885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateShortkeyTarget();
2553885b47fbSopenharmony_ci}
2554885b47fbSopenharmony_ci
2555885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateShortkeyMultiTarget()
2556885b47fbSopenharmony_ci{
2557885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateShortkeyMultiTarget();
2558885b47fbSopenharmony_ci}
2559885b47fbSopenharmony_ci
2560885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateClickResponseTime()
2561885b47fbSopenharmony_ci{
2562885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateClickResponseTime();
2563885b47fbSopenharmony_ci}
2564885b47fbSopenharmony_ci
2565885b47fbSopenharmony_civoid AccessibleAbilityManagerService::UpdateIgnoreRepeatClickTime()
2566885b47fbSopenharmony_ci{
2567885b47fbSopenharmony_ci    return accessibilitySettings_->UpdateIgnoreRepeatClickTime();
2568885b47fbSopenharmony_ci}
2569885b47fbSopenharmony_ci
2570885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RemoveCallback(CallBackID callback,
2571885b47fbSopenharmony_ci    const sptr<DeathRecipient> &recipient, const wptr<IRemoteObject> &remote)
2572885b47fbSopenharmony_ci{
2573885b47fbSopenharmony_ci    HILOG_INFO("remove callback[%{public}d]", callback);
2574885b47fbSopenharmony_ci    if (!handler_) {
2575885b47fbSopenharmony_ci        HILOG_ERROR("handler is nullptr");
2576885b47fbSopenharmony_ci        return;
2577885b47fbSopenharmony_ci    }
2578885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2579885b47fbSopenharmony_ci        if (!remote.GetRefPtr()) {
2580885b47fbSopenharmony_ci            HILOG_ERROR("remote is null");
2581885b47fbSopenharmony_ci            return;
2582885b47fbSopenharmony_ci        }
2583885b47fbSopenharmony_ci        remote->RemoveDeathRecipient(recipient);
2584885b47fbSopenharmony_ci
2585885b47fbSopenharmony_ci        if (callback == CONFIG_CALLBACK) {
2586885b47fbSopenharmony_ci            RemoveSavedConfigCallback(remote);
2587885b47fbSopenharmony_ci        }
2588885b47fbSopenharmony_ci        auto accountData = GetCurrentAccountData();
2589885b47fbSopenharmony_ci        if (!accountData) {
2590885b47fbSopenharmony_ci            HILOG_ERROR("Current account data is null");
2591885b47fbSopenharmony_ci            return;
2592885b47fbSopenharmony_ci        }
2593885b47fbSopenharmony_ci        switch (callback) {
2594885b47fbSopenharmony_ci            case STATE_CALLBACK:
2595885b47fbSopenharmony_ci                stateObservers_.RemoveStateObserver(remote);
2596885b47fbSopenharmony_ci                break;
2597885b47fbSopenharmony_ci            case CAPTION_PROPERTY_CALLBACK:
2598885b47fbSopenharmony_ci                accountData->RemoveCaptionPropertyCallback(remote);
2599885b47fbSopenharmony_ci                break;
2600885b47fbSopenharmony_ci            case ENABLE_ABILITY_LISTS_CALLBACK:
2601885b47fbSopenharmony_ci                accountData->RemoveEnableAbilityListsObserver(remote);
2602885b47fbSopenharmony_ci                break;
2603885b47fbSopenharmony_ci            case CONFIG_CALLBACK:
2604885b47fbSopenharmony_ci                accountData->RemoveConfigCallback(remote);
2605885b47fbSopenharmony_ci                break;
2606885b47fbSopenharmony_ci            default:
2607885b47fbSopenharmony_ci                break;
2608885b47fbSopenharmony_ci        }
2609885b47fbSopenharmony_ci        }, "RemoveCallback");
2610885b47fbSopenharmony_ci}
2611885b47fbSopenharmony_ci
2612885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RemoveSavedConfigCallback(const wptr<IRemoteObject>& callback)
2613885b47fbSopenharmony_ci{
2614885b47fbSopenharmony_ci    HILOG_DEBUG("start.");
2615885b47fbSopenharmony_ci    for (auto itr = defaultConfigCallbacks_.begin(); itr != defaultConfigCallbacks_.end(); itr++) {
2616885b47fbSopenharmony_ci        if ((*itr)->AsObject() == callback) {
2617885b47fbSopenharmony_ci            defaultConfigCallbacks_.erase(itr);
2618885b47fbSopenharmony_ci            break;
2619885b47fbSopenharmony_ci        }
2620885b47fbSopenharmony_ci    }
2621885b47fbSopenharmony_ci}
2622885b47fbSopenharmony_ci
2623885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnBundleManagerDied(const wptr<IRemoteObject> &remote)
2624885b47fbSopenharmony_ci{
2625885b47fbSopenharmony_ci    HILOG_INFO("OnBundleManagerDied ");
2626885b47fbSopenharmony_ci    if (!handler_) {
2627885b47fbSopenharmony_ci        HILOG_ERROR("handler is nullptr");
2628885b47fbSopenharmony_ci        return;
2629885b47fbSopenharmony_ci    }
2630885b47fbSopenharmony_ci
2631885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2632885b47fbSopenharmony_ci        if (!remote.GetRefPtr() || !bundleManager_) {
2633885b47fbSopenharmony_ci            HILOG_ERROR("remote is null");
2634885b47fbSopenharmony_ci            return;
2635885b47fbSopenharmony_ci        }
2636885b47fbSopenharmony_ci
2637885b47fbSopenharmony_ci        bundleManager_->AsObject()->RemoveDeathRecipient(bundleManagerDeathRecipient_);
2638885b47fbSopenharmony_ci        bundleManager_ = nullptr;
2639885b47fbSopenharmony_ci        }, "OnBundleManagerDied");
2640885b47fbSopenharmony_ci}
2641885b47fbSopenharmony_ci
2642885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StateObservers::AddStateObserver(
2643885b47fbSopenharmony_ci    const sptr<IAccessibleAbilityManagerStateObserver>& stateObserver)
2644885b47fbSopenharmony_ci{
2645885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(stateObserversMutex_);
2646885b47fbSopenharmony_ci    auto iter = std::find(observersList_.begin(), observersList_.end(), stateObserver);
2647885b47fbSopenharmony_ci    if (iter == observersList_.end()) {
2648885b47fbSopenharmony_ci        observersList_.push_back(stateObserver);
2649885b47fbSopenharmony_ci        HILOG_DEBUG("register state observer successfully");
2650885b47fbSopenharmony_ci        return;
2651885b47fbSopenharmony_ci    }
2652885b47fbSopenharmony_ci
2653885b47fbSopenharmony_ci    HILOG_INFO("state observer is existed");
2654885b47fbSopenharmony_ci}
2655885b47fbSopenharmony_ci
2656885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StateObservers::OnStateObservers(uint32_t state)
2657885b47fbSopenharmony_ci{
2658885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(stateObserversMutex_);
2659885b47fbSopenharmony_ci    for (auto& stateObserver : observersList_) {
2660885b47fbSopenharmony_ci        if (stateObserver) {
2661885b47fbSopenharmony_ci            stateObserver->OnStateChanged(state);
2662885b47fbSopenharmony_ci        }
2663885b47fbSopenharmony_ci    }
2664885b47fbSopenharmony_ci}
2665885b47fbSopenharmony_ci
2666885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StateObservers::RemoveStateObserver(const wptr<IRemoteObject> &remote)
2667885b47fbSopenharmony_ci{
2668885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(stateObserversMutex_);
2669885b47fbSopenharmony_ci    auto iter = std::find_if(observersList_.begin(), observersList_.end(),
2670885b47fbSopenharmony_ci        [remote](const sptr<IAccessibleAbilityManagerStateObserver>& stateObserver) {
2671885b47fbSopenharmony_ci            return stateObserver->AsObject() == remote;
2672885b47fbSopenharmony_ci        });
2673885b47fbSopenharmony_ci    if (iter != observersList_.end()) {
2674885b47fbSopenharmony_ci        observersList_.erase(iter);
2675885b47fbSopenharmony_ci    }
2676885b47fbSopenharmony_ci}
2677885b47fbSopenharmony_ci
2678885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StateObservers::Clear()
2679885b47fbSopenharmony_ci{
2680885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(stateObserversMutex_);
2681885b47fbSopenharmony_ci    observersList_.clear();
2682885b47fbSopenharmony_ci}
2683885b47fbSopenharmony_ci
2684885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetFocusedWindowId(int32_t &focusedWindowId)
2685885b47fbSopenharmony_ci{
2686885b47fbSopenharmony_ci    HILOG_DEBUG();
2687885b47fbSopenharmony_ci    return Singleton<AccessibilityWindowManager>::GetInstance().GetFocusedWindowId(focusedWindowId);
2688885b47fbSopenharmony_ci}
2689885b47fbSopenharmony_ci
2690885b47fbSopenharmony_civoid AccessibleAbilityManagerService::InsertWindowIdEventPair(int32_t windowId, const AccessibilityEventInfo &event)
2691885b47fbSopenharmony_ci{
2692885b47fbSopenharmony_ci    HILOG_DEBUG("insert event, windowId: %{public}d", windowId);
2693885b47fbSopenharmony_ci    windowFocusEventMap_[windowId] = event;
2694885b47fbSopenharmony_ci}
2695885b47fbSopenharmony_ci
2696885b47fbSopenharmony_cibool AccessibleAbilityManagerService::CheckWindowIdEventExist(int32_t windowId)
2697885b47fbSopenharmony_ci{
2698885b47fbSopenharmony_ci    return windowFocusEventMap_.count(windowId);
2699885b47fbSopenharmony_ci}
2700885b47fbSopenharmony_ci
2701885b47fbSopenharmony_cibool AccessibleAbilityManagerService::CheckWindowRegister(int32_t windowId)
2702885b47fbSopenharmony_ci{
2703885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2704885b47fbSopenharmony_ci    if (!accountData) {
2705885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr.");
2706885b47fbSopenharmony_ci        return false;
2707885b47fbSopenharmony_ci    }
2708885b47fbSopenharmony_ci    return accountData->GetAccessibilityWindowConnection(windowId) != nullptr;
2709885b47fbSopenharmony_ci}
2710885b47fbSopenharmony_ci
2711885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnDeviceProvisioned()
2712885b47fbSopenharmony_ci{
2713885b47fbSopenharmony_ci    HILOG_DEBUG();
2714885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2715885b47fbSopenharmony_ci    if (!accountData) {
2716885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2717885b47fbSopenharmony_ci        return;
2718885b47fbSopenharmony_ci    }
2719885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingProvider> service =
2720885b47fbSopenharmony_ci        AccessibilitySettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
2721885b47fbSopenharmony_ci    if (service == nullptr) {
2722885b47fbSopenharmony_ci        HILOG_ERROR("service is nullptr");
2723885b47fbSopenharmony_ci        return;
2724885b47fbSopenharmony_ci    }
2725885b47fbSopenharmony_ci    service->UnregisterObserver(DEVICE_PROVISIONED);
2726885b47fbSopenharmony_ci    if (accountData->GetConfig()->GetDbHandle()) {
2727885b47fbSopenharmony_ci        accountData->GetConfig()->GetDbHandle()->UnregisterObserver(USER_SETUP_COMPLETED);
2728885b47fbSopenharmony_ci    }
2729885b47fbSopenharmony_ci    if (accountData->GetDefaultUserScreenReaderState()) {
2730885b47fbSopenharmony_ci        HILOG_INFO("Modify shortKeyTimeout and shortKeyOnLockScreenState");
2731885b47fbSopenharmony_ci        accountData->GetConfig()->SetShortKeyTimeout(SHORT_KEY_TIMEOUT_AFTER_USE);
2732885b47fbSopenharmony_ci        accountData->GetConfig()->SetShortKeyOnLockScreenState(true);
2733885b47fbSopenharmony_ci        UpdateConfigState();
2734885b47fbSopenharmony_ci    }
2735885b47fbSopenharmony_ci}
2736885b47fbSopenharmony_ci
2737885b47fbSopenharmony_civoid AccessibleAbilityManagerService::InitializeShortKeyState()
2738885b47fbSopenharmony_ci{
2739885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2740885b47fbSopenharmony_ci    if (!accountData) {
2741885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2742885b47fbSopenharmony_ci        return;
2743885b47fbSopenharmony_ci    }
2744885b47fbSopenharmony_ci
2745885b47fbSopenharmony_ci    bool shortKeyFlag = false;
2746885b47fbSopenharmony_ci    if (accountData->GetAccountId() != DEFAULT_ACCOUNT_ID && accountData->GetConfig()->GetDbHandle() != nullptr) {
2747885b47fbSopenharmony_ci        if (accountData->GetConfig()->GetDbHandle()->GetIntValue(SHORTCUT_ENABLED, INVALID_SHORTCUT_STATE) ==
2748885b47fbSopenharmony_ci            INVALID_SHORTCUT_STATE) {
2749885b47fbSopenharmony_ci            HILOG_INFO("Initialize the shortcut key state of PrivateSpace");
2750885b47fbSopenharmony_ci            shortKeyFlag = true;
2751885b47fbSopenharmony_ci        }
2752885b47fbSopenharmony_ci    } else if (accountData->GetAccountId() == DEFAULT_ACCOUNT_ID) {
2753885b47fbSopenharmony_ci        HILOG_INFO("Initialize the shortcut key state of MainSpace");
2754885b47fbSopenharmony_ci        shortKeyFlag = true;
2755885b47fbSopenharmony_ci    }
2756885b47fbSopenharmony_ci
2757885b47fbSopenharmony_ci    if (shortKeyFlag) {
2758885b47fbSopenharmony_ci        accountData->GetConfig()->SetShortKeyState(true);
2759885b47fbSopenharmony_ci        std::vector<std::string> tmpVec { SCREEN_READER_BUNDLE_ABILITY_NAME };
2760885b47fbSopenharmony_ci        accountData->GetConfig()->SetShortkeyMultiTarget(tmpVec);
2761885b47fbSopenharmony_ci        UpdateConfigState();
2762885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateShortKeyRegister();
2763885b47fbSopenharmony_ci    }
2764885b47fbSopenharmony_ci}
2765885b47fbSopenharmony_ci
2766885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RegisterProvisionCallback()
2767885b47fbSopenharmony_ci{
2768885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2769885b47fbSopenharmony_ci    if (!accountData) {
2770885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2771885b47fbSopenharmony_ci        return;
2772885b47fbSopenharmony_ci    }
2773885b47fbSopenharmony_ci
2774885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingProvider> service =
2775885b47fbSopenharmony_ci        AccessibilitySettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
2776885b47fbSopenharmony_ci    if (service == nullptr) {
2777885b47fbSopenharmony_ci        HILOG_ERROR("service is nullptr");
2778885b47fbSopenharmony_ci        return;
2779885b47fbSopenharmony_ci    }
2780885b47fbSopenharmony_ci    AccessibilitySettingObserver::UpdateFunc func = [ = ](const std::string &state) {
2781885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().OnDeviceProvisioned();
2782885b47fbSopenharmony_ci    };
2783885b47fbSopenharmony_ci    service->RegisterObserver(DEVICE_PROVISIONED, func);
2784885b47fbSopenharmony_ci    if (accountData->GetConfig()->GetDbHandle() != nullptr) {
2785885b47fbSopenharmony_ci        accountData->GetConfig()->GetDbHandle()->RegisterObserver(USER_SETUP_COMPLETED, func);
2786885b47fbSopenharmony_ci    }
2787885b47fbSopenharmony_ci}
2788885b47fbSopenharmony_ci
2789885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RegisterShortKeyEvent()
2790885b47fbSopenharmony_ci{
2791885b47fbSopenharmony_ci    HILOG_DEBUG();
2792885b47fbSopenharmony_ci    if (!handler_) {
2793885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr");
2794885b47fbSopenharmony_ci        return;
2795885b47fbSopenharmony_ci    }
2796885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2797885b47fbSopenharmony_ci        HILOG_DEBUG();
2798885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2799885b47fbSopenharmony_ci        if (!accountData) {
2800885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
2801885b47fbSopenharmony_ci            return;
2802885b47fbSopenharmony_ci        }
2803885b47fbSopenharmony_ci        std::shared_ptr<AccessibilitySettingProvider> service =
2804885b47fbSopenharmony_ci            AccessibilitySettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
2805885b47fbSopenharmony_ci        if (service == nullptr) {
2806885b47fbSopenharmony_ci            HILOG_ERROR("service is nullptr");
2807885b47fbSopenharmony_ci            return;
2808885b47fbSopenharmony_ci        }
2809885b47fbSopenharmony_ci        bool oobeState = false;
2810885b47fbSopenharmony_ci        bool userSetupState = false;
2811885b47fbSopenharmony_ci        service->GetBoolValue(DEVICE_PROVISIONED, oobeState);
2812885b47fbSopenharmony_ci        if (accountData->GetConfig()->GetDbHandle() != nullptr) {
2813885b47fbSopenharmony_ci            userSetupState = accountData->GetConfig()->GetDbHandle()->GetBoolValue(USER_SETUP_COMPLETED, false);
2814885b47fbSopenharmony_ci        }
2815885b47fbSopenharmony_ci        if (accountData->GetAccountId() == DEFAULT_ACCOUNT_ID && (oobeState == false || userSetupState == false)) {
2816885b47fbSopenharmony_ci            InitializeShortKeyState();
2817885b47fbSopenharmony_ci            RegisterProvisionCallback();
2818885b47fbSopenharmony_ci        } else if (accountData->GetAccountId() != DEFAULT_ACCOUNT_ID) {
2819885b47fbSopenharmony_ci            InitializeShortKeyState();
2820885b47fbSopenharmony_ci        }
2821885b47fbSopenharmony_ci        }, "REGISTER_SHORTKEY_OBSERVER");
2822885b47fbSopenharmony_ci}
2823885b47fbSopenharmony_ci
2824885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OffZoomGesture()
2825885b47fbSopenharmony_ci{
2826885b47fbSopenharmony_ci    HILOG_INFO();
2827885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
2828885b47fbSopenharmony_ci    AccessibilityDisplayManager &displayMgr = Singleton<AccessibilityDisplayManager>::GetInstance();
2829885b47fbSopenharmony_ci    uint64_t currentScreen = displayMgr.GetDefaultDisplayId();
2830885b47fbSopenharmony_ci    float normalScale = 1.0f;
2831885b47fbSopenharmony_ci    float defaultAnchor = 0.5f;
2832885b47fbSopenharmony_ci    displayMgr.SetDisplayScale(currentScreen, normalScale, normalScale, defaultAnchor, defaultAnchor);
2833885b47fbSopenharmony_ci    return;
2834885b47fbSopenharmony_ci#else
2835885b47fbSopenharmony_ci    HILOG_INFO("not support zoom");
2836885b47fbSopenharmony_ci    return;
2837885b47fbSopenharmony_ci#endif
2838885b47fbSopenharmony_ci}
2839885b47fbSopenharmony_ci
2840885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnScreenMagnificationStateChanged()
2841885b47fbSopenharmony_ci{
2842885b47fbSopenharmony_ci    HILOG_DEBUG();
2843885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2844885b47fbSopenharmony_ci    if (accountData == nullptr) {
2845885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2846885b47fbSopenharmony_ci        return;
2847885b47fbSopenharmony_ci    }
2848885b47fbSopenharmony_ci
2849885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingsConfig> config = accountData->GetConfig();
2850885b47fbSopenharmony_ci    if (config == nullptr) {
2851885b47fbSopenharmony_ci        HILOG_ERROR("config is nullptr");
2852885b47fbSopenharmony_ci        return;
2853885b47fbSopenharmony_ci    }
2854885b47fbSopenharmony_ci
2855885b47fbSopenharmony_ci    if (config->GetDbHandle() == nullptr) {
2856885b47fbSopenharmony_ci        HILOG_ERROR("datashareHelper is nullptr");
2857885b47fbSopenharmony_ci        return;
2858885b47fbSopenharmony_ci    }
2859885b47fbSopenharmony_ci
2860885b47fbSopenharmony_ci    bool screenMagnificationEnabled = false;
2861885b47fbSopenharmony_ci    screenMagnificationEnabled = config->GetDbHandle()->GetBoolValue(SCREEN_MAGNIFICATION_KEY, false);
2862885b47fbSopenharmony_ci    config->SetScreenMagnificationState(screenMagnificationEnabled);
2863885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateInputFilter();
2864885b47fbSopenharmony_ci    if (!screenMagnificationEnabled) {
2865885b47fbSopenharmony_ci        OffZoomGesture();
2866885b47fbSopenharmony_ci    }
2867885b47fbSopenharmony_ci}
2868885b47fbSopenharmony_ci
2869885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RegisterScreenMagnificationState()
2870885b47fbSopenharmony_ci{
2871885b47fbSopenharmony_ci    HILOG_DEBUG();
2872885b47fbSopenharmony_ci    if (handler_ == nullptr) {
2873885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr");
2874885b47fbSopenharmony_ci        return;
2875885b47fbSopenharmony_ci    }
2876885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2877885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2878885b47fbSopenharmony_ci        if (accountData == nullptr) {
2879885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
2880885b47fbSopenharmony_ci            return;
2881885b47fbSopenharmony_ci        }
2882885b47fbSopenharmony_ci
2883885b47fbSopenharmony_ci        AccessibilitySettingObserver::UpdateFunc func = [ = ](const std::string &state) {
2884885b47fbSopenharmony_ci            Singleton<AccessibleAbilityManagerService>::GetInstance().OnScreenMagnificationStateChanged();
2885885b47fbSopenharmony_ci        };
2886885b47fbSopenharmony_ci        if (accountData->GetConfig()->GetDbHandle()) {
2887885b47fbSopenharmony_ci            accountData->GetConfig()->GetDbHandle()->RegisterObserver(SCREEN_MAGNIFICATION_KEY, func);
2888885b47fbSopenharmony_ci        }
2889885b47fbSopenharmony_ci        }, "REGISTER_SCREEN_ZOOM_OBSERVER");
2890885b47fbSopenharmony_ci}
2891885b47fbSopenharmony_ci
2892885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnScreenMagnificationTypeChanged()
2893885b47fbSopenharmony_ci{
2894885b47fbSopenharmony_ci    HILOG_DEBUG();
2895885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2896885b47fbSopenharmony_ci    if (accountData == nullptr) {
2897885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2898885b47fbSopenharmony_ci        return;
2899885b47fbSopenharmony_ci    }
2900885b47fbSopenharmony_ci
2901885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingsConfig> config = accountData->GetConfig();
2902885b47fbSopenharmony_ci    if (config == nullptr) {
2903885b47fbSopenharmony_ci        HILOG_ERROR("config is nullptr");
2904885b47fbSopenharmony_ci        return;
2905885b47fbSopenharmony_ci    }
2906885b47fbSopenharmony_ci
2907885b47fbSopenharmony_ci    if (config->GetDbHandle() == nullptr) {
2908885b47fbSopenharmony_ci        HILOG_ERROR("datashareHelper is nullptr");
2909885b47fbSopenharmony_ci        return;
2910885b47fbSopenharmony_ci    }
2911885b47fbSopenharmony_ci
2912885b47fbSopenharmony_ci    uint32_t screenMagnificationType = 0;
2913885b47fbSopenharmony_ci    screenMagnificationType =
2914885b47fbSopenharmony_ci        static_cast<uint32_t>(config->GetDbHandle()->GetIntValue(SCREEN_MAGNIFICATION_TYPE, 0));
2915885b47fbSopenharmony_ci    config->SetScreenMagnificationType(screenMagnificationType);
2916885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateInputFilter();
2917885b47fbSopenharmony_ci}
2918885b47fbSopenharmony_ci
2919885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RegisterScreenMagnificationType()
2920885b47fbSopenharmony_ci{
2921885b47fbSopenharmony_ci    HILOG_DEBUG();
2922885b47fbSopenharmony_ci    if (handler_ == nullptr) {
2923885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr");
2924885b47fbSopenharmony_ci        return;
2925885b47fbSopenharmony_ci    }
2926885b47fbSopenharmony_ci    handler_->PostTask([=]() {
2927885b47fbSopenharmony_ci        sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2928885b47fbSopenharmony_ci        if (accountData == nullptr) {
2929885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr");
2930885b47fbSopenharmony_ci            return;
2931885b47fbSopenharmony_ci        }
2932885b47fbSopenharmony_ci
2933885b47fbSopenharmony_ci        AccessibilitySettingObserver::UpdateFunc func = [ = ](const std::string &state) {
2934885b47fbSopenharmony_ci            Singleton<AccessibleAbilityManagerService>::GetInstance().OnScreenMagnificationTypeChanged();
2935885b47fbSopenharmony_ci        };
2936885b47fbSopenharmony_ci        if (accountData->GetConfig()->GetDbHandle()) {
2937885b47fbSopenharmony_ci            accountData->GetConfig()->GetDbHandle()->RegisterObserver(SCREEN_MAGNIFICATION_TYPE, func);
2938885b47fbSopenharmony_ci        }
2939885b47fbSopenharmony_ci        }, "REGISTER_SCREEN_ZOOM_TYPE_OBSERVER");
2940885b47fbSopenharmony_ci}
2941885b47fbSopenharmony_ci
2942885b47fbSopenharmony_civoid AccessibleAbilityManagerService::PostDelayUnloadTask()
2943885b47fbSopenharmony_ci{
2944885b47fbSopenharmony_ci    auto task = [=]() {
2945885b47fbSopenharmony_ci        sptr<ISystemAbilityManager> systemAbilityManager =
2946885b47fbSopenharmony_ci            SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
2947885b47fbSopenharmony_ci        if (systemAbilityManager == nullptr) {
2948885b47fbSopenharmony_ci            HILOG_ERROR("failed to get system ability mgr");
2949885b47fbSopenharmony_ci            return;
2950885b47fbSopenharmony_ci        }
2951885b47fbSopenharmony_ci        if (!IsNeedUnload()) {
2952885b47fbSopenharmony_ci            return;
2953885b47fbSopenharmony_ci        }
2954885b47fbSopenharmony_ci        int32_t ret = systemAbilityManager->UnloadSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID);
2955885b47fbSopenharmony_ci        if (ret != ERR_OK) {
2956885b47fbSopenharmony_ci            HILOG_ERROR("unload system ability failed");
2957885b47fbSopenharmony_ci            return;
2958885b47fbSopenharmony_ci        }
2959885b47fbSopenharmony_ci    };
2960885b47fbSopenharmony_ci    handler_->RemoveTask(DELAY_UNLOAD_TASK);
2961885b47fbSopenharmony_ci    handler_->PostTask(task, DELAY_UNLOAD_TASK, UNLOAD_TASK_INTERNAL);
2962885b47fbSopenharmony_ci}
2963885b47fbSopenharmony_ci
2964885b47fbSopenharmony_cibool AccessibleAbilityManagerService::IsNeedUnload()
2965885b47fbSopenharmony_ci{
2966885b47fbSopenharmony_ci    HILOG_DEBUG();
2967885b47fbSopenharmony_ci#ifndef ACCESSIBILITY_WATCH_FEATURE
2968885b47fbSopenharmony_ci    // always return true to avoid stablity problem
2969885b47fbSopenharmony_ci    return false;
2970885b47fbSopenharmony_ci#else // for watch
2971885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
2972885b47fbSopenharmony_ci    if (!accountData) {
2973885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr");
2974885b47fbSopenharmony_ci        return true;
2975885b47fbSopenharmony_ci    }
2976885b47fbSopenharmony_ci
2977885b47fbSopenharmony_ci    // do not unload when any extension is enabled
2978885b47fbSopenharmony_ci    std::vector<std::string> enableAbilityList = accountData->GetEnabledAbilities();
2979885b47fbSopenharmony_ci    if (enableAbilityList.size() != 0) {
2980885b47fbSopenharmony_ci        return false;
2981885b47fbSopenharmony_ci    }
2982885b47fbSopenharmony_ci    return true;
2983885b47fbSopenharmony_ci#endif // ACCESSIBILITY_WATCH_FEATURE
2984885b47fbSopenharmony_ci}
2985885b47fbSopenharmony_ci
2986885b47fbSopenharmony_ciint32_t AccessibleAbilityManagerService::GetTreeIdBySplitElementId(const int64_t elementId)
2987885b47fbSopenharmony_ci{
2988885b47fbSopenharmony_ci    if (elementId < 0) {
2989885b47fbSopenharmony_ci        HILOG_DEBUG("The elementId is -1");
2990885b47fbSopenharmony_ci        return elementId;
2991885b47fbSopenharmony_ci    }
2992885b47fbSopenharmony_ci    int32_t treeId = (static_cast<uint64_t>(elementId) >> ELEMENT_MOVE_BIT);
2993885b47fbSopenharmony_ci    return treeId;
2994885b47fbSopenharmony_ci}
2995885b47fbSopenharmony_ci
2996885b47fbSopenharmony_civoid AccessibleAbilityManagerService::AddRequestId(int32_t windowId, int32_t treeId, int32_t requestId,
2997885b47fbSopenharmony_ci    sptr<IAccessibilityElementOperatorCallback> callback)
2998885b47fbSopenharmony_ci{
2999885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(mutex_);
3000885b47fbSopenharmony_ci    HILOG_DEBUG("Add windowId: %{public}d treeId: %{public}d requestId: %{public}d", windowId, treeId, requestId);
3001885b47fbSopenharmony_ci    if (!windowRequestIdMap_.count(windowId)) {
3002885b47fbSopenharmony_ci        windowRequestIdMap_[windowId] = {};
3003885b47fbSopenharmony_ci    }
3004885b47fbSopenharmony_ci    if (!windowRequestIdMap_[windowId].count(treeId)) {
3005885b47fbSopenharmony_ci        windowRequestIdMap_[windowId][treeId] = {};
3006885b47fbSopenharmony_ci    }
3007885b47fbSopenharmony_ci    if (!windowRequestIdMap_[windowId][treeId].count(requestId)) {
3008885b47fbSopenharmony_ci        windowRequestIdMap_[windowId][treeId].insert(requestId);
3009885b47fbSopenharmony_ci        requestIdMap_[requestId] = callback;
3010885b47fbSopenharmony_ci    }
3011885b47fbSopenharmony_ci}
3012885b47fbSopenharmony_ci
3013885b47fbSopenharmony_civoid AccessibleAbilityManagerService::RemoveRequestId(int32_t requestId)
3014885b47fbSopenharmony_ci{
3015885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(mutex_);
3016885b47fbSopenharmony_ci    HILOG_DEBUG("RemoveRequestId requestId: %{public}d", requestId);
3017885b47fbSopenharmony_ci    for (auto &window : windowRequestIdMap_) {
3018885b47fbSopenharmony_ci        for (auto &tree : window.second) {
3019885b47fbSopenharmony_ci            auto it = tree.second.find(requestId);
3020885b47fbSopenharmony_ci            if (it != tree.second.end()) {
3021885b47fbSopenharmony_ci                HILOG_DEBUG("tree.second.erase requestId:%{public}d", requestId);
3022885b47fbSopenharmony_ci                tree.second.erase(it);
3023885b47fbSopenharmony_ci            }
3024885b47fbSopenharmony_ci            auto ite = requestIdMap_.find(requestId);
3025885b47fbSopenharmony_ci            if (ite != requestIdMap_.end()) {
3026885b47fbSopenharmony_ci                HILOG_DEBUG("requestIdMap_.erase requestId:%{public}d", requestId);
3027885b47fbSopenharmony_ci                requestIdMap_.erase(ite);
3028885b47fbSopenharmony_ci            }
3029885b47fbSopenharmony_ci        }
3030885b47fbSopenharmony_ci    }
3031885b47fbSopenharmony_ci}
3032885b47fbSopenharmony_ci
3033885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StopCallbackWait(int32_t windowId)
3034885b47fbSopenharmony_ci{
3035885b47fbSopenharmony_ci    HILOG_INFO("StopCallbackWait start windowId: %{public}d", windowId);
3036885b47fbSopenharmony_ci    if (!windowRequestIdMap_.count(windowId)) {
3037885b47fbSopenharmony_ci        HILOG_DEBUG("windowId not exists");
3038885b47fbSopenharmony_ci        return;
3039885b47fbSopenharmony_ci    }
3040885b47fbSopenharmony_ci    for (auto iter = windowRequestIdMap_[windowId].begin(); iter != windowRequestIdMap_[windowId].end(); ++iter) {
3041885b47fbSopenharmony_ci        HILOG_DEBUG("stop callback wait windowId: %{public}d, treeId: %{public}d", windowId, iter->first);
3042885b47fbSopenharmony_ci        StopCallbackWait(windowId, iter->first);
3043885b47fbSopenharmony_ci    }
3044885b47fbSopenharmony_ci}
3045885b47fbSopenharmony_ci
3046885b47fbSopenharmony_civoid AccessibleAbilityManagerService::StopCallbackWait(int32_t windowId, int32_t treeId)
3047885b47fbSopenharmony_ci{
3048885b47fbSopenharmony_ci    std::lock_guard<ffrt::mutex> lock(mutex_);
3049885b47fbSopenharmony_ci    HILOG_INFO("StopCallbackWait start windowId: %{public}d treeId: %{public}d", windowId, treeId);
3050885b47fbSopenharmony_ci    if (!windowRequestIdMap_.count(windowId)) {
3051885b47fbSopenharmony_ci        return;
3052885b47fbSopenharmony_ci    }
3053885b47fbSopenharmony_ci    if (!windowRequestIdMap_[windowId].count(treeId)) {
3054885b47fbSopenharmony_ci        return;
3055885b47fbSopenharmony_ci    }
3056885b47fbSopenharmony_ci    auto requestIds = windowRequestIdMap_[windowId][treeId];
3057885b47fbSopenharmony_ci    for (auto requestId = requestIds.begin(); requestId != requestIds.end();) {
3058885b47fbSopenharmony_ci        HILOG_DEBUG("stop callback wait windowId: %{public}d, requestId: %{public}d", windowId, *requestId);
3059885b47fbSopenharmony_ci        auto iter = requestIdMap_.find(*requestId);
3060885b47fbSopenharmony_ci        if (iter != requestIdMap_.end()) {
3061885b47fbSopenharmony_ci            HILOG_DEBUG("requestIdMap_ set callback and erase requestId:%{public}d", *requestId);
3062885b47fbSopenharmony_ci            sptr<IAccessibilityElementOperatorCallback> callback = requestIdMap_[*requestId];
3063885b47fbSopenharmony_ci            if (callback != nullptr) {
3064885b47fbSopenharmony_ci                callback->SetExecuteActionResult(false, *requestId);
3065885b47fbSopenharmony_ci            }
3066885b47fbSopenharmony_ci            requestIdMap_.erase(iter);
3067885b47fbSopenharmony_ci        }
3068885b47fbSopenharmony_ci        requestId = requestIds.erase(requestId);
3069885b47fbSopenharmony_ci    }
3070885b47fbSopenharmony_ci}
3071885b47fbSopenharmony_ci
3072885b47fbSopenharmony_ciint64_t AccessibleAbilityManagerService::GetRootParentId(int32_t windowId, int32_t treeId)
3073885b47fbSopenharmony_ci{
3074885b47fbSopenharmony_ci    HILOG_INFO("aa search treeParent from aams, windowId: %{public}d, treeId: %{public}d", windowId, treeId);
3075885b47fbSopenharmony_ci    int64_t elementId = 0;
3076885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = GetAccessibilityWindowConnection(windowId);
3077885b47fbSopenharmony_ci    if (!connection) {
3078885b47fbSopenharmony_ci        HILOG_WARN("The operator of windowId[%{public}d] has not been registered.", windowId);
3079885b47fbSopenharmony_ci        return RET_ERR_NO_CONNECTION;
3080885b47fbSopenharmony_ci    }
3081885b47fbSopenharmony_ci    connection->GetRootParentId(treeId, elementId);
3082885b47fbSopenharmony_ci    return elementId;
3083885b47fbSopenharmony_ci}
3084885b47fbSopenharmony_ci
3085885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::GetAllTreeId(int32_t windowId, std::vector<int32_t> &treeIds)
3086885b47fbSopenharmony_ci{
3087885b47fbSopenharmony_ci    HILOG_DEBUG();
3088885b47fbSopenharmony_ci    sptr<AccessibilityWindowConnection> connection = GetAccessibilityWindowConnection(windowId);
3089885b47fbSopenharmony_ci    if (!connection) {
3090885b47fbSopenharmony_ci        HILOG_WARN("The operator of windowId[%{public}d] has not been registered.", windowId);
3091885b47fbSopenharmony_ci        return RET_ERR_NO_CONNECTION;
3092885b47fbSopenharmony_ci    }
3093885b47fbSopenharmony_ci    connection->GetAllTreeId(treeIds);
3094885b47fbSopenharmony_ci    return RET_OK;
3095885b47fbSopenharmony_ci}
3096885b47fbSopenharmony_ci
3097885b47fbSopenharmony_ciint32_t AccessibleAbilityManagerService::GenerateRequestId()
3098885b47fbSopenharmony_ci{
3099885b47fbSopenharmony_ci    int32_t requestId = requestId_.fetch_add(1, std::memory_order_relaxed);
3100885b47fbSopenharmony_ci    if (requestId == REQUEST_ID_MAX) {
3101885b47fbSopenharmony_ci        requestId_ = REQUEST_ID_MIN;
3102885b47fbSopenharmony_ci        requestId = requestId_.fetch_add(1, std::memory_order_relaxed);
3103885b47fbSopenharmony_ci    }
3104885b47fbSopenharmony_ci    return requestId;
3105885b47fbSopenharmony_ci}
3106885b47fbSopenharmony_ci
3107885b47fbSopenharmony_ciRetError AccessibleAbilityManagerService::CheckCallingUid()
3108885b47fbSopenharmony_ci{
3109885b47fbSopenharmony_ci    int32_t accountId = Utils::GetUserIdByCallingUid();
3110885b47fbSopenharmony_ci    if (accountId != currentAccountId_ && accountId != ROOT_UID) {
3111885b47fbSopenharmony_ci        HILOG_WARN("accountId is diff from currentAccountId_.");
3112885b47fbSopenharmony_ci        return RET_ERR_SAMGR;
3113885b47fbSopenharmony_ci    }
3114885b47fbSopenharmony_ci    return RET_OK;
3115885b47fbSopenharmony_ci}
3116885b47fbSopenharmony_ci
3117885b47fbSopenharmony_civoid AccessibleAbilityManagerService::OnDataClone()
3118885b47fbSopenharmony_ci{
3119885b47fbSopenharmony_ci    std::shared_ptr<AccessibilitySettingProvider> service =
3120885b47fbSopenharmony_ci        AccessibilitySettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
3121885b47fbSopenharmony_ci    if (service == nullptr) {
3122885b47fbSopenharmony_ci        HILOG_ERROR("service is nullptr");
3123885b47fbSopenharmony_ci        return;
3124885b47fbSopenharmony_ci    }
3125885b47fbSopenharmony_ci    bool cloneState = false;
3126885b47fbSopenharmony_ci    service->GetBoolValue(ACCESSIBILITY_CLONE_FLAG, cloneState);
3127885b47fbSopenharmony_ci    if (cloneState == false) {
3128885b47fbSopenharmony_ci        return;
3129885b47fbSopenharmony_ci    }
3130885b47fbSopenharmony_ci    sptr<AccessibilityAccountData> accountData = GetCurrentAccountData();
3131885b47fbSopenharmony_ci    if (accountData == nullptr) {
3132885b47fbSopenharmony_ci        HILOG_WARN("accountData is nullptr.");
3133885b47fbSopenharmony_ci        return;
3134885b47fbSopenharmony_ci    }
3135885b47fbSopenharmony_ci    if (accountData->GetConfig() != nullptr) {
3136885b47fbSopenharmony_ci        accountData->GetConfig()->OnDataClone();
3137885b47fbSopenharmony_ci        UpdateAllSetting();
3138885b47fbSopenharmony_ci        UpdateAutoStartAbilities();
3139885b47fbSopenharmony_ci        UpdateInputFilter();
3140885b47fbSopenharmony_ci        HILOG_INFO("accessibility reload config.");
3141885b47fbSopenharmony_ci    } else {
3142885b47fbSopenharmony_ci        HILOG_WARN("config_ is nullptr");
3143885b47fbSopenharmony_ci    }
3144885b47fbSopenharmony_ci}
3145885b47fbSopenharmony_ci} // namespace Accessibility
3146885b47fbSopenharmony_ci} // namespace OHOS