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_connection.h"
17885b47fbSopenharmony_ci
18885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
19885b47fbSopenharmony_ci#include <hitrace_meter.h>
20885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
21885b47fbSopenharmony_ci
22885b47fbSopenharmony_ci#include "ability_manager_client.h"
23885b47fbSopenharmony_ci#include "accessible_ability_manager_service.h"
24885b47fbSopenharmony_ci#include "hilog_wrapper.h"
25885b47fbSopenharmony_ci#include "utils.h"
26885b47fbSopenharmony_ci
27885b47fbSopenharmony_cinamespace OHOS {
28885b47fbSopenharmony_cinamespace Accessibility {
29885b47fbSopenharmony_ciAccessibleAbilityConnection::AccessibleAbilityConnection(int32_t accountId, int32_t connectionId,
30885b47fbSopenharmony_ci    AccessibilityAbilityInfo &abilityInfo)
31885b47fbSopenharmony_ci    : accountId_(accountId), connectionId_(connectionId), abilityInfo_(abilityInfo)
32885b47fbSopenharmony_ci{
33885b47fbSopenharmony_ci    eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(
34885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().GetMainRunner());
35885b47fbSopenharmony_ci}
36885b47fbSopenharmony_ci
37885b47fbSopenharmony_ciAccessibleAbilityConnection::~AccessibleAbilityConnection()
38885b47fbSopenharmony_ci{
39885b47fbSopenharmony_ci    HILOG_DEBUG();
40885b47fbSopenharmony_ci    if (abilityClient_ && abilityClient_->AsObject()) {
41885b47fbSopenharmony_ci        abilityClient_->AsObject()->RemoveDeathRecipient(deathRecipient_);
42885b47fbSopenharmony_ci        abilityClient_ = nullptr;
43885b47fbSopenharmony_ci    }
44885b47fbSopenharmony_ci}
45885b47fbSopenharmony_ci
46885b47fbSopenharmony_civoid AccessibleAbilityConnection::HandleNoEventHandler(const AppExecFwk::ElementName &element)
47885b47fbSopenharmony_ci{
48885b47fbSopenharmony_ci    HILOG_ERROR("eventHandler_ is nullptr.");
49885b47fbSopenharmony_ci    auto accountData = Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId_);
50885b47fbSopenharmony_ci    if (accountData != nullptr) {
51885b47fbSopenharmony_ci        accountData->RemoveConnectingA11yAbility(Utils::GetUri(element));
52885b47fbSopenharmony_ci    }
53885b47fbSopenharmony_ci}
54885b47fbSopenharmony_ci
55885b47fbSopenharmony_civoid AccessibleAbilityConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
56885b47fbSopenharmony_ci    const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
57885b47fbSopenharmony_ci{
58885b47fbSopenharmony_ci    HILOG_INFO("ResultCode is %{public}d", resultCode);
59885b47fbSopenharmony_ci    if (!eventHandler_) {
60885b47fbSopenharmony_ci        HandleNoEventHandler(element);
61885b47fbSopenharmony_ci        return;
62885b47fbSopenharmony_ci    }
63885b47fbSopenharmony_ci
64885b47fbSopenharmony_ci    int32_t accountId = accountId_;
65885b47fbSopenharmony_ci    eventHandler_->PostTask([accountId, element, remoteObject, resultCode]() {
66885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
67885b47fbSopenharmony_ci        FinishAsyncTrace(HITRACE_TAG_ACCESSIBILITY_MANAGER, "AccessibleAbilityConnect",
68885b47fbSopenharmony_ci            static_cast<int32_t>(TraceTaskId::ACCESSIBLE_ABILITY_CONNECT));
69885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
70885b47fbSopenharmony_ci        std::string bundleName = element.GetBundleName();
71885b47fbSopenharmony_ci        std::string abilityName = element.GetAbilityName();
72885b47fbSopenharmony_ci        auto accountData = Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId);
73885b47fbSopenharmony_ci        if (!accountData) {
74885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
75885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
76885b47fbSopenharmony_ci            HILOG_ERROR("accountData is nullptr.");
77885b47fbSopenharmony_ci            return;
78885b47fbSopenharmony_ci        }
79885b47fbSopenharmony_ci
80885b47fbSopenharmony_ci        if (resultCode != NO_ERROR) {
81885b47fbSopenharmony_ci            accountData->RemoveEnabledAbility(Utils::GetUri(element));
82885b47fbSopenharmony_ci            accountData->RemoveConnectingA11yAbility(Utils::GetUri(element));
83885b47fbSopenharmony_ci            accountData->UpdateAbilities();
84885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
85885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
86885b47fbSopenharmony_ci            return;
87885b47fbSopenharmony_ci        }
88885b47fbSopenharmony_ci
89885b47fbSopenharmony_ci        if (!remoteObject) {
90885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
91885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
92885b47fbSopenharmony_ci            HILOG_ERROR("AccessibleAbilityConnection::OnAbilityConnectDone get remoteObject failed");
93885b47fbSopenharmony_ci            return;
94885b47fbSopenharmony_ci        }
95885b47fbSopenharmony_ci
96885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> connection = accountData->GetConnectingA11yAbility(Utils::GetUri(element));
97885b47fbSopenharmony_ci        if (!connection) {
98885b47fbSopenharmony_ci            HILOG_ERROR("connection is nullptr");
99885b47fbSopenharmony_ci            return;
100885b47fbSopenharmony_ci        }
101885b47fbSopenharmony_ci
102885b47fbSopenharmony_ci        accountData->AddConnectedAbility(connection);
103885b47fbSopenharmony_ci        accountData->RemoveConnectingA11yAbility(Utils::GetUri(element));
104885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateAccessibilityManagerService();
105885b47fbSopenharmony_ci        connection->InitAbilityClient(remoteObject);
106885b47fbSopenharmony_ci        accountData->UpdateEnableAbilityListsState();
107885b47fbSopenharmony_ci        }, "OnAbilityConnectDone");
108885b47fbSopenharmony_ci}
109885b47fbSopenharmony_ci
110885b47fbSopenharmony_civoid AccessibleAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode)
111885b47fbSopenharmony_ci{
112885b47fbSopenharmony_ci    HILOG_INFO("resultCode[%{public}d]", resultCode);
113885b47fbSopenharmony_ci}
114885b47fbSopenharmony_ci
115885b47fbSopenharmony_civoid AccessibleAbilityConnection::OnAccessibilityEvent(AccessibilityEventInfo &eventInfo)
116885b47fbSopenharmony_ci{
117885b47fbSopenharmony_ci    HILOG_DEBUG();
118885b47fbSopenharmony_ci    if (!abilityClient_) {
119885b47fbSopenharmony_ci        HILOG_ERROR("OnAccessibilityEvent​ failed");
120885b47fbSopenharmony_ci        return;
121885b47fbSopenharmony_ci    }
122885b47fbSopenharmony_ci
123885b47fbSopenharmony_ci    std::vector<std::string> filterBundleNames = abilityInfo_.GetFilterBundleNames();
124885b47fbSopenharmony_ci    if (IsWantedEvent(eventInfo.GetEventType()) && (filterBundleNames.empty() || find(filterBundleNames.begin(),
125885b47fbSopenharmony_ci        filterBundleNames.end(), eventInfo.GetBundleName()) != filterBundleNames.end())) {
126885b47fbSopenharmony_ci        abilityClient_->OnAccessibilityEvent(eventInfo);
127885b47fbSopenharmony_ci        HILOG_DEBUG("windowId[%{public}d] evtType[%{public}d] windowChangeType[%{public}d] GestureId[%{public}d]",
128885b47fbSopenharmony_ci            eventInfo.GetWindowId(), eventInfo.GetEventType(), eventInfo.GetWindowChangeTypes(),
129885b47fbSopenharmony_ci            eventInfo.GetGestureType());
130885b47fbSopenharmony_ci    }
131885b47fbSopenharmony_ci}
132885b47fbSopenharmony_ci
133885b47fbSopenharmony_cibool AccessibleAbilityConnection::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence)
134885b47fbSopenharmony_ci{
135885b47fbSopenharmony_ci    if (!abilityClient_) {
136885b47fbSopenharmony_ci        HILOG_ERROR("OnKeyPressEvent failed");
137885b47fbSopenharmony_ci        return false;
138885b47fbSopenharmony_ci    }
139885b47fbSopenharmony_ci
140885b47fbSopenharmony_ci    if (!(abilityInfo_.GetCapabilityValues() & Capability::CAPABILITY_KEY_EVENT_OBSERVER)) {
141885b47fbSopenharmony_ci        HILOG_ERROR("Capability failed");
142885b47fbSopenharmony_ci        return false;
143885b47fbSopenharmony_ci    }
144885b47fbSopenharmony_ci
145885b47fbSopenharmony_ci    abilityClient_->OnKeyPressEvent(keyEvent, sequence);
146885b47fbSopenharmony_ci    return true;
147885b47fbSopenharmony_ci}
148885b47fbSopenharmony_ci
149885b47fbSopenharmony_civoid AccessibleAbilityConnection::SetAbilityInfoTargetBundleName(const std::vector<std::string> &targetBundleNames)
150885b47fbSopenharmony_ci{
151885b47fbSopenharmony_ci    HILOG_DEBUG();
152885b47fbSopenharmony_ci    abilityInfo_.SetFilterBundleNames(targetBundleNames);
153885b47fbSopenharmony_ci}
154885b47fbSopenharmony_ci
155885b47fbSopenharmony_cibool AccessibleAbilityConnection::IsWantedEvent(int32_t eventType)
156885b47fbSopenharmony_ci{
157885b47fbSopenharmony_ci    uint32_t type = static_cast<uint32_t>(eventType);
158885b47fbSopenharmony_ci    if ((type & abilityInfo_.GetEventTypes()) != type) {
159885b47fbSopenharmony_ci        HILOG_DEBUG("EventType(%{public}d) is not wanted", type);
160885b47fbSopenharmony_ci        return false;
161885b47fbSopenharmony_ci    }
162885b47fbSopenharmony_ci    return true;
163885b47fbSopenharmony_ci}
164885b47fbSopenharmony_ci
165885b47fbSopenharmony_ciAAFwk::Want CreateWant(AppExecFwk::ElementName& element)
166885b47fbSopenharmony_ci{
167885b47fbSopenharmony_ci    AAFwk::Want want;
168885b47fbSopenharmony_ci    want.SetElement(element);
169885b47fbSopenharmony_ci
170885b47fbSopenharmony_ci    return want;
171885b47fbSopenharmony_ci}
172885b47fbSopenharmony_ci
173885b47fbSopenharmony_civoid AccessibleAbilityConnection::Disconnect()
174885b47fbSopenharmony_ci{
175885b47fbSopenharmony_ci    HILOG_DEBUG();
176885b47fbSopenharmony_ci
177885b47fbSopenharmony_ci    if (accountId_ == Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId()) {
178885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateAccessibilityManagerService();
179885b47fbSopenharmony_ci    }
180885b47fbSopenharmony_ci
181885b47fbSopenharmony_ci    if (!abilityClient_) {
182885b47fbSopenharmony_ci        HILOG_ERROR("abilityClient is nullptr");
183885b47fbSopenharmony_ci        return;
184885b47fbSopenharmony_ci    }
185885b47fbSopenharmony_ci    abilityClient_->Disconnect(connectionId_);
186885b47fbSopenharmony_ci
187885b47fbSopenharmony_ci    if (deathRecipient_ == nullptr || !abilityClient_->AsObject() ||
188885b47fbSopenharmony_ci        !abilityClient_->AsObject()->RemoveDeathRecipient(deathRecipient_)) {
189885b47fbSopenharmony_ci        HILOG_ERROR("Failed to add death recipient");
190885b47fbSopenharmony_ci    }
191885b47fbSopenharmony_ci
192885b47fbSopenharmony_ci    auto abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
193885b47fbSopenharmony_ci    if (!abilityManagerClient) {
194885b47fbSopenharmony_ci        HILOG_ERROR("abilityManagerClient is nullptr");
195885b47fbSopenharmony_ci        return;
196885b47fbSopenharmony_ci    }
197885b47fbSopenharmony_ci    if (abilityManagerClient->DisconnectAbility(this) != ERR_OK) {
198885b47fbSopenharmony_ci        HILOG_ERROR("Disconnect failed!");
199885b47fbSopenharmony_ci        return;
200885b47fbSopenharmony_ci    }
201885b47fbSopenharmony_ci}
202885b47fbSopenharmony_ci
203885b47fbSopenharmony_cibool AccessibleAbilityConnection::Connect(const AppExecFwk::ElementName &element)
204885b47fbSopenharmony_ci{
205885b47fbSopenharmony_ci    HILOG_DEBUG();
206885b47fbSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_HITRACE
207885b47fbSopenharmony_ci    StartAsyncTrace(HITRACE_TAG_ACCESSIBILITY_MANAGER, "AccessibleAbilityConnect",
208885b47fbSopenharmony_ci        static_cast<int32_t>(TraceTaskId::ACCESSIBLE_ABILITY_CONNECT));
209885b47fbSopenharmony_ci#endif // OHOS_BUILD_ENABLE_HITRACE
210885b47fbSopenharmony_ci    std::string bundleName = element.GetBundleName();
211885b47fbSopenharmony_ci    std::string abilityName = element.GetAbilityName();
212885b47fbSopenharmony_ci
213885b47fbSopenharmony_ci    elementName_ = element;
214885b47fbSopenharmony_ci    HILOG_DEBUG("bundleName[%{public}s], abilityName [%{public}s], accountId [%{public}d]",
215885b47fbSopenharmony_ci        bundleName.c_str(), abilityName.c_str(), accountId_);
216885b47fbSopenharmony_ci
217885b47fbSopenharmony_ci    auto bundleMgr = Singleton<AccessibleAbilityManagerService>::GetInstance().GetBundleMgrProxy();
218885b47fbSopenharmony_ci    if (!bundleMgr) {
219885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
220885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
221885b47fbSopenharmony_ci        HILOG_ERROR("get bundleMgr failed");
222885b47fbSopenharmony_ci        return false;
223885b47fbSopenharmony_ci    }
224885b47fbSopenharmony_ci    int uid = bundleMgr->GetUidByBundleName(bundleName, accountId_);
225885b47fbSopenharmony_ci    HILOG_DEBUG("uid is %{public}d ", uid);
226885b47fbSopenharmony_ci
227885b47fbSopenharmony_ci    auto abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
228885b47fbSopenharmony_ci    if (!abilityManagerClient) {
229885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
230885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
231885b47fbSopenharmony_ci        HILOG_ERROR("abilityManagerClient is nullptr");
232885b47fbSopenharmony_ci        return false;
233885b47fbSopenharmony_ci    }
234885b47fbSopenharmony_ci    AAFwk::Want want = CreateWant(elementName_);
235885b47fbSopenharmony_ci    if (abilityManagerClient->ConnectAbility(want, this, nullptr, uid / UID_MASK) != ERR_OK) {
236885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
237885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
238885b47fbSopenharmony_ci        HILOG_ERROR("ConnectAbility failed!");
239885b47fbSopenharmony_ci        return false;
240885b47fbSopenharmony_ci    }
241885b47fbSopenharmony_ci    return true;
242885b47fbSopenharmony_ci}
243885b47fbSopenharmony_ci
244885b47fbSopenharmony_ciint32_t AccessibleAbilityConnection::GetChannelId()
245885b47fbSopenharmony_ci{
246885b47fbSopenharmony_ci    HILOG_DEBUG("connectionId_ is %{public}d", connectionId_);
247885b47fbSopenharmony_ci    return connectionId_;
248885b47fbSopenharmony_ci}
249885b47fbSopenharmony_ci
250885b47fbSopenharmony_civoid AccessibleAbilityConnection::OnAbilityConnectDoneSync(const AppExecFwk::ElementName &element,
251885b47fbSopenharmony_ci    const sptr<IRemoteObject> &remoteObject)
252885b47fbSopenharmony_ci{
253885b47fbSopenharmony_ci    HILOG_DEBUG();
254885b47fbSopenharmony_ci    auto accountData = Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId_);
255885b47fbSopenharmony_ci    if (!accountData) {
256885b47fbSopenharmony_ci        HILOG_ERROR("accountData is nullptr.");
257885b47fbSopenharmony_ci        return;
258885b47fbSopenharmony_ci    }
259885b47fbSopenharmony_ci    if (!remoteObject) {
260885b47fbSopenharmony_ci        HILOG_ERROR("AccessibleAbilityConnection::OnAbilityConnectDone get remoteObject failed");
261885b47fbSopenharmony_ci        return;
262885b47fbSopenharmony_ci    }
263885b47fbSopenharmony_ci    elementName_ = element;
264885b47fbSopenharmony_ci
265885b47fbSopenharmony_ci    sptr<AccessibleAbilityConnection> pointer = this;
266885b47fbSopenharmony_ci    accountData->AddConnectedAbility(pointer);
267885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateAccessibilityManagerService();
268885b47fbSopenharmony_ci    InitAbilityClient(remoteObject);
269885b47fbSopenharmony_ci}
270885b47fbSopenharmony_ci
271885b47fbSopenharmony_civoid AccessibleAbilityConnection::OnAbilityDisconnectDoneSync(const AppExecFwk::ElementName &element)
272885b47fbSopenharmony_ci{
273885b47fbSopenharmony_ci    HILOG_DEBUG();
274885b47fbSopenharmony_ci
275885b47fbSopenharmony_ci    if (accountId_ == Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId()) {
276885b47fbSopenharmony_ci        Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateAccessibilityManagerService();
277885b47fbSopenharmony_ci    }
278885b47fbSopenharmony_ci
279885b47fbSopenharmony_ci    if (!abilityClient_) {
280885b47fbSopenharmony_ci        HILOG_ERROR("abilityClient_ is nullptr");
281885b47fbSopenharmony_ci        return;
282885b47fbSopenharmony_ci    }
283885b47fbSopenharmony_ci    abilityClient_->Disconnect(connectionId_);
284885b47fbSopenharmony_ci}
285885b47fbSopenharmony_ci
286885b47fbSopenharmony_civoid AccessibleAbilityConnection::InitAbilityClient(const sptr<IRemoteObject> &remoteObject)
287885b47fbSopenharmony_ci{
288885b47fbSopenharmony_ci    std::string bundleName = elementName_.GetBundleName();
289885b47fbSopenharmony_ci    std::string abilityName = elementName_.GetAbilityName();
290885b47fbSopenharmony_ci    abilityClient_ = new(std::nothrow) AccessibleAbilityClientProxy(remoteObject);
291885b47fbSopenharmony_ci    if (!abilityClient_) {
292885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
293885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
294885b47fbSopenharmony_ci        HILOG_ERROR("abilityClient_ is null");
295885b47fbSopenharmony_ci        return;
296885b47fbSopenharmony_ci    }
297885b47fbSopenharmony_ci
298885b47fbSopenharmony_ci    if (!deathRecipient_) {
299885b47fbSopenharmony_ci        deathRecipient_ = new(std::nothrow) AccessibleAbilityConnectionDeathRecipient(
300885b47fbSopenharmony_ci            accountId_, elementName_, eventHandler_);
301885b47fbSopenharmony_ci        if (!deathRecipient_) {
302885b47fbSopenharmony_ci            Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
303885b47fbSopenharmony_ci                A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
304885b47fbSopenharmony_ci            HILOG_ERROR("deathRecipient_ is null");
305885b47fbSopenharmony_ci            return;
306885b47fbSopenharmony_ci        }
307885b47fbSopenharmony_ci    }
308885b47fbSopenharmony_ci
309885b47fbSopenharmony_ci    if (!abilityClient_->AsObject() || !abilityClient_->AsObject()->AddDeathRecipient(deathRecipient_)) {
310885b47fbSopenharmony_ci        HILOG_ERROR("Failed to add death recipient");
311885b47fbSopenharmony_ci    }
312885b47fbSopenharmony_ci
313885b47fbSopenharmony_ci    channel_ = new(std::nothrow) AccessibleAbilityChannel(accountId_, abilityInfo_.GetId());
314885b47fbSopenharmony_ci    if (!channel_) {
315885b47fbSopenharmony_ci        Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
316885b47fbSopenharmony_ci            A11yError::ERROR_CONNECT_A11Y_APPLICATION_FAILED, bundleName, abilityName);
317885b47fbSopenharmony_ci        HILOG_ERROR("channel_ is null");
318885b47fbSopenharmony_ci        return;
319885b47fbSopenharmony_ci    }
320885b47fbSopenharmony_ci    abilityClient_->Init(channel_, connectionId_);
321885b47fbSopenharmony_ci}
322885b47fbSopenharmony_ci
323885b47fbSopenharmony_civoid AccessibleAbilityConnection::AccessibleAbilityConnectionDeathRecipient::OnRemoteDied(
324885b47fbSopenharmony_ci    const wptr<IRemoteObject> &remote)
325885b47fbSopenharmony_ci{
326885b47fbSopenharmony_ci    Utils::RecordUnavailableEvent(A11yUnavailableEvent::CONNECT_EVENT,
327885b47fbSopenharmony_ci        A11yError::ERROR_A11Y_APPLICATION_DISCONNECT_ABNORMALLY,
328885b47fbSopenharmony_ci        recipientElementName_.GetBundleName(), recipientElementName_.GetAbilityName());
329885b47fbSopenharmony_ci    if (!handler_) {
330885b47fbSopenharmony_ci        HILOG_ERROR("handler_ is nullptr");
331885b47fbSopenharmony_ci        return;
332885b47fbSopenharmony_ci    }
333885b47fbSopenharmony_ci    std::shared_ptr<AppExecFwk::ElementName> sharedElementName =
334885b47fbSopenharmony_ci        std::make_shared<AppExecFwk::ElementName>(recipientElementName_);
335885b47fbSopenharmony_ci
336885b47fbSopenharmony_ci    if (sharedElementName == nullptr) {
337885b47fbSopenharmony_ci        HILOG_ERROR("sharedElementName is nullptr");
338885b47fbSopenharmony_ci        return;
339885b47fbSopenharmony_ci    }
340885b47fbSopenharmony_ci    int32_t accountId = accountId_;
341885b47fbSopenharmony_ci    handler_->PostTask([accountId, sharedElementName]() {
342885b47fbSopenharmony_ci        HILOG_DEBUG();
343885b47fbSopenharmony_ci
344885b47fbSopenharmony_ci        auto &aams = Singleton<AccessibleAbilityManagerService>::GetInstance();
345885b47fbSopenharmony_ci        auto accountData = aams.GetAccountData(accountId);
346885b47fbSopenharmony_ci        if (!accountData) {
347885b47fbSopenharmony_ci            HILOG_ERROR("accountData is null.");
348885b47fbSopenharmony_ci            return;
349885b47fbSopenharmony_ci        }
350885b47fbSopenharmony_ci
351885b47fbSopenharmony_ci        std::string uri = Utils::GetUri(*sharedElementName);
352885b47fbSopenharmony_ci        sptr<AccessibleAbilityConnection> connection = accountData->GetAccessibleAbilityConnection(uri);
353885b47fbSopenharmony_ci        if (!connection) {
354885b47fbSopenharmony_ci            HILOG_ERROR("There is no connection for %{public}s.", uri.c_str());
355885b47fbSopenharmony_ci            return;
356885b47fbSopenharmony_ci        }
357885b47fbSopenharmony_ci        accountData->RemoveConnectedAbility(*sharedElementName);
358885b47fbSopenharmony_ci        accountData->RemoveEnabledAbility(uri);
359885b47fbSopenharmony_ci
360885b47fbSopenharmony_ci        std::string uiTestUri = Utils::GetUri("ohos.uitest", "uitestability");
361885b47fbSopenharmony_ci        if (uri == uiTestUri) {
362885b47fbSopenharmony_ci            accountData->RemoveInstalledAbility("ohos.uitest");
363885b47fbSopenharmony_ci        }
364885b47fbSopenharmony_ci
365885b47fbSopenharmony_ci        accountData->UpdateAbilities();
366885b47fbSopenharmony_ci        aams.UpdateAccessibilityManagerService();
367885b47fbSopenharmony_ci        }, "OnRemoteDied");
368885b47fbSopenharmony_ci}
369885b47fbSopenharmony_ci} // namespace Accessibility
370885b47fbSopenharmony_ci} // namespace OHOS