1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "accessible_ability_client_proxy.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "accessibility_event_info_parcel.h"
19 #include "hilog_wrapper.h"
20
21 namespace OHOS {
22 namespace Accessibility {
AccessibleAbilityClientProxy(const sptr<IRemoteObject> & object)23 AccessibleAbilityClientProxy::AccessibleAbilityClientProxy(const sptr<IRemoteObject> &object)
24 : IRemoteProxy<IAccessibleAbilityClient>(object)
25 {
26 }
27
WriteInterfaceToken(MessageParcel & data)28 bool AccessibleAbilityClientProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30 HILOG_DEBUG();
31 if (!data.WriteInterfaceToken(AccessibleAbilityClientProxy::GetDescriptor())) {
32 HILOG_ERROR("write interface token failed");
33 return false;
34 }
35 return true;
36 }
37
SendTransactCmd(IAccessibleAbilityClient::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 bool AccessibleAbilityClientProxy::SendTransactCmd(IAccessibleAbilityClient::Message code, MessageParcel &data,
39 MessageParcel &reply, MessageOption &option)
40 {
41 HILOG_DEBUG();
42
43 sptr<IRemoteObject> remote = Remote();
44 if (!remote) {
45 HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
46 return false;
47 }
48 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
49 if (result != NO_ERROR) {
50 HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
51 return false;
52 }
53 return true;
54 }
55
Init(const sptr<IAccessibleAbilityChannel> & channel,const int32_t channelId)56 void AccessibleAbilityClientProxy::Init(const sptr<IAccessibleAbilityChannel> &channel, const int32_t channelId)
57 {
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option(MessageOption::TF_ASYNC);
61
62 HILOG_DEBUG();
63
64 if (!WriteInterfaceToken(data)) {
65 return;
66 }
67 if (!channel) {
68 HILOG_ERROR("channel is null.");
69 return;
70 }
71 if (!data.WriteRemoteObject(channel->AsObject())) {
72 HILOG_ERROR("fail, channel write parcelable error");
73 return;
74 }
75
76 if (!data.WriteInt32(channelId)) {
77 HILOG_ERROR("fail, channelId write int32 error");
78 return;
79 }
80
81 if (!SendTransactCmd(IAccessibleAbilityClient::Message::INIT, data, reply, option)) {
82 HILOG_ERROR("Init fail");
83 return;
84 }
85 }
86
Disconnect(const int32_t channelId)87 void AccessibleAbilityClientProxy::Disconnect(const int32_t channelId)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option(MessageOption::TF_ASYNC);
92
93 HILOG_DEBUG();
94
95 if (!WriteInterfaceToken(data)) {
96 return;
97 }
98
99 if (!data.WriteInt32(channelId)) {
100 HILOG_ERROR("fail, channelId write int32 error");
101 return;
102 }
103
104 if (!SendTransactCmd(IAccessibleAbilityClient::Message::DISCONNECT, data, reply, option)) {
105 HILOG_ERROR("Disconnect fail");
106 return;
107 }
108 }
109
OnAccessibilityEvent(const AccessibilityEventInfo & eventInfo)110 void AccessibleAbilityClientProxy::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
111 {
112 MessageParcel data;
113 MessageParcel reply;
114 MessageOption option(MessageOption::TF_ASYNC);
115 AccessibilityEventInfoParcel eventInfoParcel(eventInfo);
116
117 HILOG_DEBUG();
118
119 if (!WriteInterfaceToken(data)) {
120 return;
121 }
122 if (!data.WriteParcelable(&eventInfoParcel)) {
123 HILOG_ERROR("fail, eventInfo write parcelable error");
124 return;
125 }
126 if (!SendTransactCmd(IAccessibleAbilityClient::Message::ON_ACCESSIBILITY_EVENT, data, reply, option)) {
127 HILOG_ERROR("OnAccessibilityEvent fail");
128 return;
129 }
130 }
131
OnKeyPressEvent(const MMI::KeyEvent & keyEvent,const int32_t sequence)132 void AccessibleAbilityClientProxy::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence)
133 {
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option(MessageOption::TF_ASYNC);
137
138 HILOG_DEBUG();
139
140 if (!WriteInterfaceToken(data)) {
141 return;
142 }
143 if (!data.WriteInt32(sequence)) {
144 HILOG_ERROR("fail, sequence write int32 error");
145 return;
146 }
147
148 if (!keyEvent.WriteToParcel(data)) {
149 HILOG_ERROR("fail, keyEvent WriteToParcel error");
150 return;
151 }
152
153 if (!SendTransactCmd(IAccessibleAbilityClient::Message::ON_KEY_PRESS_EVENT, data, reply, option)) {
154 HILOG_ERROR("OnKeyPressEvent fail");
155 return;
156 }
157 }
158 } // namespace Accessibility
159 } // namespace OHOS