1 /*
2 * Copyright (C) 2021 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 "input_data_channel_proxy.h"
17
18 #include "global.h"
19 #include "ipc_types.h"
20 #include "itypes_util.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 namespace OHOS {
25 namespace MiscServices {
InputDataChannelProxy(const sptr<IRemoteObject> & object)26 InputDataChannelProxy::InputDataChannelProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<IInputDataChannel>(object)
28 {
29 }
30
InsertText(const std::u16string & text)31 int32_t InputDataChannelProxy::InsertText(const std::u16string &text)
32 {
33 return SendRequest(INSERT_TEXT, [&text](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, text); });
34 }
35
DeleteForward(int32_t length)36 int32_t InputDataChannelProxy::DeleteForward(int32_t length)
37 {
38 return SendRequest(DELETE_FORWARD, [length](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, length); });
39 }
40
DeleteBackward(int32_t length)41 int32_t InputDataChannelProxy::DeleteBackward(int32_t length)
42 {
43 return SendRequest(
44 DELETE_BACKWARD, [length](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, length); });
45 }
46
GetTextBeforeCursor(int32_t number,std::u16string & text)47 int32_t InputDataChannelProxy::GetTextBeforeCursor(int32_t number, std::u16string &text)
48 {
49 return SendRequest(
50 GET_TEXT_BEFORE_CURSOR, [number](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, number); },
51 [&text](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, text); });
52 }
53
GetTextAfterCursor(int32_t number,std::u16string & text)54 int32_t InputDataChannelProxy::GetTextAfterCursor(int32_t number, std::u16string &text)
55 {
56 return SendRequest(
57 GET_TEXT_AFTER_CURSOR, [number](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, number); },
58 [&text](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, text); });
59 }
60
SendKeyboardStatus(KeyboardStatus status)61 void InputDataChannelProxy::SendKeyboardStatus(KeyboardStatus status)
62 {
63 SendRequest(SEND_KEYBOARD_STATUS,
64 [status](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, static_cast<int32_t>(status)); });
65 }
66
NotifyPanelStatusInfo(const PanelStatusInfo & info)67 void InputDataChannelProxy::NotifyPanelStatusInfo(const PanelStatusInfo &info)
68 {
69 SendRequest(NOTIFY_PANEL_STATUS_INFO, [&info](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, info); });
70 }
71
SendFunctionKey(int32_t funcKey)72 int32_t InputDataChannelProxy::SendFunctionKey(int32_t funcKey)
73 {
74 return SendRequest(
75 SEND_FUNCTION_KEY, [funcKey](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, funcKey); });
76 }
77
MoveCursor(int32_t keyCode)78 int32_t InputDataChannelProxy::MoveCursor(int32_t keyCode)
79 {
80 return SendRequest(MOVE_CURSOR, [keyCode](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, keyCode); });
81 }
82
GetEnterKeyType(int32_t & keyType)83 int32_t InputDataChannelProxy::GetEnterKeyType(int32_t &keyType)
84 {
85 return SendRequest(GET_ENTER_KEY_TYPE, nullptr,
86 [&keyType](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, keyType); });
87 }
88
GetInputPattern(int32_t & inputPattern)89 int32_t InputDataChannelProxy::GetInputPattern(int32_t &inputPattern)
90 {
91 return SendRequest(GET_INPUT_PATTERN, nullptr,
92 [&inputPattern](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, inputPattern); });
93 }
94
GetTextIndexAtCursor(int32_t & index)95 int32_t InputDataChannelProxy::GetTextIndexAtCursor(int32_t &index)
96 {
97 return SendRequest(GET_TEXT_INDEX_AT_CURSOR, nullptr,
98 [&index](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, index); });
99 }
100
GetTextConfig(TextTotalConfig & textConfig)101 int32_t InputDataChannelProxy::GetTextConfig(TextTotalConfig &textConfig)
102 {
103 return SendRequest(GET_TEXT_CONFIG, nullptr,
104 [&textConfig](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, textConfig); });
105 }
106
SelectByRange(int32_t start,int32_t end)107 int32_t InputDataChannelProxy::SelectByRange(int32_t start, int32_t end)
108 {
109 return SendRequest(
110 SELECT_BY_RANGE, [start, end](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, start, end); });
111 }
112
SelectByMovement(int32_t direction,int32_t cursorMoveSkip)113 int32_t InputDataChannelProxy::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
114 {
115 return SendRequest(SELECT_BY_MOVEMENT, [direction, cursorMoveSkip](MessageParcel &parcel) {
116 return ITypesUtil::Marshal(parcel, direction, cursorMoveSkip);
117 });
118 }
119
HandleExtendAction(int32_t action)120 int32_t InputDataChannelProxy::HandleExtendAction(int32_t action)
121 {
122 return SendRequest(
123 HANDLE_EXTEND_ACTION, [action](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, action); });
124 }
125
NotifyKeyboardHeight(uint32_t height)126 void InputDataChannelProxy::NotifyKeyboardHeight(uint32_t height)
127 {
128 SendRequest(
129 NOTIFY_KEYBOARD_HEIGHT, [height](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, height); });
130 }
131
GetMessageOption(int32_t code,MessageOption & option)132 void InputDataChannelProxy::GetMessageOption(int32_t code, MessageOption &option)
133 {
134 switch (code) {
135 case SEND_KEYBOARD_STATUS:
136 case NOTIFY_KEYBOARD_HEIGHT:
137 IMSA_HILOGD("Async IPC.");
138 option.SetFlags(MessageOption::TF_ASYNC);
139 break;
140
141 default:
142 option.SetFlags(MessageOption::TF_SYNC);
143 break;
144 }
145 }
146
SendRequest(int code,ParcelHandler input,ParcelHandler output)147 int32_t InputDataChannelProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
148 {
149 IMSA_HILOGD("InputDataChannelProxy run in, code = %{public}d", code);
150 MessageParcel data;
151 MessageParcel reply;
152 MessageOption option;
153 GetMessageOption(code, option);
154
155 if (!data.WriteInterfaceToken(GetDescriptor())) {
156 IMSA_HILOGE("write interface token failed");
157 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
158 }
159 if (input != nullptr && (!input(data))) {
160 IMSA_HILOGE("write data failed");
161 return ErrorCode::ERROR_EX_PARCELABLE;
162 }
163 auto ret = Remote()->SendRequest(code, data, reply, option);
164 if (ret != NO_ERROR) {
165 IMSA_HILOGE("InputDataChannelProxy send request failed, code: %{public}d ret %{public}d", code, ret);
166 return ret;
167 }
168 if (option.GetFlags() == MessageOption::TF_ASYNC) {
169 return ErrorCode::NO_ERROR;
170 }
171 ret = reply.ReadInt32();
172 if (ret != NO_ERROR) {
173 IMSA_HILOGE("reply error, ret %{public}d", ret);
174 return ret;
175 }
176 if (output != nullptr && (!output(reply))) {
177 IMSA_HILOGE("reply parcel error");
178 return ErrorCode::ERROR_EX_PARCELABLE;
179 }
180 return ret;
181 }
182 } // namespace MiscServices
183 } // namespace OHOS
184