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_client_proxy.h"
17885b47fbSopenharmony_ci#include "accessibility_element_info_parcel.h"
18885b47fbSopenharmony_ci#include "accessibility_event_info_parcel.h"
19885b47fbSopenharmony_ci#include "accessibility_ipc_interface_code.h"
20885b47fbSopenharmony_ci#include "hilog_wrapper.h"
21885b47fbSopenharmony_ci
22885b47fbSopenharmony_cinamespace OHOS {
23885b47fbSopenharmony_cinamespace Accessibility {
24885b47fbSopenharmony_ciAccessibleAbilityClientProxy::AccessibleAbilityClientProxy(const sptr<IRemoteObject> &object)
25885b47fbSopenharmony_ci    : IRemoteProxy<IAccessibleAbilityClient>(object)
26885b47fbSopenharmony_ci{
27885b47fbSopenharmony_ci}
28885b47fbSopenharmony_ci
29885b47fbSopenharmony_cibool AccessibleAbilityClientProxy::WriteInterfaceToken(MessageParcel &data)
30885b47fbSopenharmony_ci{
31885b47fbSopenharmony_ci    HILOG_DEBUG();
32885b47fbSopenharmony_ci    if (!data.WriteInterfaceToken(AccessibleAbilityClientProxy::GetDescriptor())) {
33885b47fbSopenharmony_ci        HILOG_ERROR("write interface token failed");
34885b47fbSopenharmony_ci        return false;
35885b47fbSopenharmony_ci    }
36885b47fbSopenharmony_ci    return true;
37885b47fbSopenharmony_ci}
38885b47fbSopenharmony_ci
39885b47fbSopenharmony_cibool AccessibleAbilityClientProxy::SendTransactCmd(AccessibilityInterfaceCode code, MessageParcel &data,
40885b47fbSopenharmony_ci    MessageParcel &reply,  MessageOption &option)
41885b47fbSopenharmony_ci{
42885b47fbSopenharmony_ci    HILOG_DEBUG();
43885b47fbSopenharmony_ci
44885b47fbSopenharmony_ci    sptr<IRemoteObject> remoteClientProxy = Remote();
45885b47fbSopenharmony_ci    if (remoteClientProxy == nullptr) {
46885b47fbSopenharmony_ci        HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
47885b47fbSopenharmony_ci        return false;
48885b47fbSopenharmony_ci    }
49885b47fbSopenharmony_ci    int32_t resultClientProxy =
50885b47fbSopenharmony_ci        remoteClientProxy->SendRequest(static_cast<uint32_t>(code), data, reply, option);
51885b47fbSopenharmony_ci    if (resultClientProxy != NO_ERROR) {
52885b47fbSopenharmony_ci        HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", resultClientProxy, code);
53885b47fbSopenharmony_ci        return false;
54885b47fbSopenharmony_ci    }
55885b47fbSopenharmony_ci    return true;
56885b47fbSopenharmony_ci}
57885b47fbSopenharmony_ci
58885b47fbSopenharmony_civoid AccessibleAbilityClientProxy::Init(const sptr<IAccessibleAbilityChannel> &channel, const int32_t channelId)
59885b47fbSopenharmony_ci{
60885b47fbSopenharmony_ci    MessageParcel data;
61885b47fbSopenharmony_ci    MessageParcel reply;
62885b47fbSopenharmony_ci    MessageOption option(MessageOption::TF_ASYNC);
63885b47fbSopenharmony_ci
64885b47fbSopenharmony_ci    HILOG_DEBUG();
65885b47fbSopenharmony_ci
66885b47fbSopenharmony_ci    if (!WriteInterfaceToken(data)) {
67885b47fbSopenharmony_ci        return;
68885b47fbSopenharmony_ci    }
69885b47fbSopenharmony_ci    if (channel == nullptr) {
70885b47fbSopenharmony_ci        HILOG_ERROR("channel is null.");
71885b47fbSopenharmony_ci        return;
72885b47fbSopenharmony_ci    }
73885b47fbSopenharmony_ci    if (!data.WriteRemoteObject(channel->AsObject())) {
74885b47fbSopenharmony_ci        HILOG_ERROR("fail, channel write parcelable error");
75885b47fbSopenharmony_ci        return;
76885b47fbSopenharmony_ci    }
77885b47fbSopenharmony_ci
78885b47fbSopenharmony_ci    if (!data.WriteInt32(channelId)) {
79885b47fbSopenharmony_ci        HILOG_ERROR("fail, channelId write int32 error");
80885b47fbSopenharmony_ci        return;
81885b47fbSopenharmony_ci    }
82885b47fbSopenharmony_ci
83885b47fbSopenharmony_ci    if (!SendTransactCmd(AccessibilityInterfaceCode::INIT, data, reply, option)) {
84885b47fbSopenharmony_ci        HILOG_ERROR("Init fail");
85885b47fbSopenharmony_ci        return;
86885b47fbSopenharmony_ci    }
87885b47fbSopenharmony_ci}
88885b47fbSopenharmony_ci
89885b47fbSopenharmony_civoid AccessibleAbilityClientProxy::Disconnect(const int32_t channelId)
90885b47fbSopenharmony_ci{
91885b47fbSopenharmony_ci    MessageParcel data;
92885b47fbSopenharmony_ci    MessageParcel reply;
93885b47fbSopenharmony_ci    MessageOption option(MessageOption::TF_SYNC);
94885b47fbSopenharmony_ci
95885b47fbSopenharmony_ci    HILOG_DEBUG();
96885b47fbSopenharmony_ci
97885b47fbSopenharmony_ci    if (!WriteInterfaceToken(data)) {
98885b47fbSopenharmony_ci        return;
99885b47fbSopenharmony_ci    }
100885b47fbSopenharmony_ci
101885b47fbSopenharmony_ci    if (!data.WriteInt32(channelId)) {
102885b47fbSopenharmony_ci        HILOG_ERROR("fail, channelId write int32 error");
103885b47fbSopenharmony_ci        return;
104885b47fbSopenharmony_ci    }
105885b47fbSopenharmony_ci
106885b47fbSopenharmony_ci    if (!SendTransactCmd(AccessibilityInterfaceCode::DISCONNECT, data, reply, option)) {
107885b47fbSopenharmony_ci        HILOG_ERROR("Disconnect fail");
108885b47fbSopenharmony_ci        return;
109885b47fbSopenharmony_ci    }
110885b47fbSopenharmony_ci}
111885b47fbSopenharmony_ci
112885b47fbSopenharmony_civoid AccessibleAbilityClientProxy::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
113885b47fbSopenharmony_ci{
114885b47fbSopenharmony_ci    MessageParcel data;
115885b47fbSopenharmony_ci    MessageParcel reply;
116885b47fbSopenharmony_ci    MessageOption option(MessageOption::TF_ASYNC);
117885b47fbSopenharmony_ci    AccessibilityEventInfoParcel eventInfoParcel(eventInfo);
118885b47fbSopenharmony_ci
119885b47fbSopenharmony_ci    HILOG_DEBUG();
120885b47fbSopenharmony_ci
121885b47fbSopenharmony_ci    if (!WriteInterfaceToken(data)) {
122885b47fbSopenharmony_ci        return;
123885b47fbSopenharmony_ci    }
124885b47fbSopenharmony_ci    if (!data.WriteParcelable(&eventInfoParcel)) {
125885b47fbSopenharmony_ci        HILOG_ERROR("fail, eventInfo write parcelable error");
126885b47fbSopenharmony_ci        return;
127885b47fbSopenharmony_ci    }
128885b47fbSopenharmony_ci    if (!SendTransactCmd(AccessibilityInterfaceCode::ON_ACCESSIBILITY_EVENT, data, reply, option)) {
129885b47fbSopenharmony_ci        HILOG_ERROR("OnAccessibilityEvent fail");
130885b47fbSopenharmony_ci        return;
131885b47fbSopenharmony_ci    }
132885b47fbSopenharmony_ci}
133885b47fbSopenharmony_ci
134885b47fbSopenharmony_civoid AccessibleAbilityClientProxy::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence)
135885b47fbSopenharmony_ci{
136885b47fbSopenharmony_ci    MessageParcel data;
137885b47fbSopenharmony_ci    MessageParcel reply;
138885b47fbSopenharmony_ci    MessageOption option(MessageOption::TF_ASYNC);
139885b47fbSopenharmony_ci
140885b47fbSopenharmony_ci    HILOG_DEBUG();
141885b47fbSopenharmony_ci
142885b47fbSopenharmony_ci    if (!WriteInterfaceToken(data)) {
143885b47fbSopenharmony_ci        return;
144885b47fbSopenharmony_ci    }
145885b47fbSopenharmony_ci    if (!data.WriteInt32(sequence)) {
146885b47fbSopenharmony_ci        HILOG_ERROR("fail, sequence write int32 error");
147885b47fbSopenharmony_ci        return;
148885b47fbSopenharmony_ci    }
149885b47fbSopenharmony_ci
150885b47fbSopenharmony_ci    if (!keyEvent.WriteToParcel(data)) {
151885b47fbSopenharmony_ci        HILOG_ERROR("fail, keyEvent WriteToParcel error");
152885b47fbSopenharmony_ci        return;
153885b47fbSopenharmony_ci    }
154885b47fbSopenharmony_ci
155885b47fbSopenharmony_ci    if (!SendTransactCmd(AccessibilityInterfaceCode::ON_KEY_PRESS_EVENT, data, reply, option)) {
156885b47fbSopenharmony_ci        HILOG_ERROR("OnKeyPressEvent fail");
157885b47fbSopenharmony_ci        return;
158885b47fbSopenharmony_ci    }
159885b47fbSopenharmony_ci}
160885b47fbSopenharmony_ci} // namespace Accessibility
161885b47fbSopenharmony_ci} // namespace OHOS