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_method_core_proxy.h"
17
18 #include <string_ex.h>
19
20 #include "input_attribute.h"
21 #include "itypes_util.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24
25 namespace OHOS {
26 namespace MiscServices {
InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> & impl)27 InputMethodCoreProxy::InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl)
28 : IRemoteProxy<IInputMethodCore>(impl)
29 {
30 }
31
32 InputMethodCoreProxy::~InputMethodCoreProxy() = default;
33
InitInputControlChannel(sptr<IInputControlChannel> & inputControlChannel,const std::string & imeId)34 int32_t InputMethodCoreProxy::InitInputControlChannel(
35 sptr<IInputControlChannel> &inputControlChannel, const std::string &imeId)
36 {
37 return SendRequest(INIT_INPUT_CONTROL_CHANNEL, [&inputControlChannel, &imeId](MessageParcel &data) {
38 return ITypesUtil::Marshal(data, inputControlChannel->AsObject(), imeId);
39 });
40 }
41
ShowKeyboard(const sptr<IInputDataChannel> & inputDataChannel,bool isShowKeyboard,bool attachFlag)42 int32_t InputMethodCoreProxy::ShowKeyboard(
43 const sptr<IInputDataChannel> &inputDataChannel, bool isShowKeyboard, bool attachFlag)
44 {
45 IMSA_HILOGD("InputMethodCoreProxy::showKeyboard");
46 return SendRequest(SHOW_KEYBOARD, [&inputDataChannel, isShowKeyboard, attachFlag](MessageParcel &data) {
47 return ITypesUtil::Marshal(data, inputDataChannel->AsObject(), isShowKeyboard, attachFlag);
48 });
49 }
50
StopInputService(std::string imeId)51 void InputMethodCoreProxy::StopInputService(std::string imeId)
52 {
53 SendRequest(STOP_INPUT_SERVICE, [&imeId](MessageParcel &data) {
54 return ITypesUtil::Marshal(data, Str8ToStr16(imeId));
55 });
56 }
57
HideKeyboard()58 int32_t InputMethodCoreProxy::HideKeyboard()
59 {
60 return SendRequest(HIDE_KEYBOARD);
61 }
62
SetSubtype(const SubProperty & property)63 int32_t InputMethodCoreProxy::SetSubtype(const SubProperty &property)
64 {
65 return SendRequest(SET_SUBTYPE, [&property](MessageParcel &data) { return ITypesUtil::Marshal(data, property); });
66 }
67
ClearDataChannel(const sptr<IInputDataChannel> & channel)68 int32_t InputMethodCoreProxy::ClearDataChannel(const sptr<IInputDataChannel> &channel)
69 {
70 return SendRequest(CLEAR_DATA_CHANNEL,
71 [&channel](MessageParcel &data) { return ITypesUtil::Marshal(data, channel->AsObject()); });
72 }
73
SendRequest(int code,ParcelHandler input,ParcelHandler output)74 int32_t InputMethodCoreProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
75 {
76 IMSA_HILOGD("InputMethodCoreProxy, run in, code = %{public}d", code);
77 MessageParcel data;
78 MessageParcel reply;
79 MessageOption option{ MessageOption::TF_SYNC };
80 if (!data.WriteInterfaceToken(GetDescriptor())) {
81 IMSA_HILOGE("InputMethodCoreProxy::write interface token failed");
82 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
83 }
84 if (input != nullptr && (!input(data))) {
85 IMSA_HILOGE("InputMethodCoreProxy::write data failed");
86 return ErrorCode::ERROR_EX_PARCELABLE;
87 }
88 auto ret = Remote()->SendRequest(code, data, reply, option);
89 if (ret != NO_ERROR) {
90 IMSA_HILOGE("InputMethodCoreProxy::SendRequest failed, ret %{public}d", ret);
91 return ret;
92 }
93 ret = reply.ReadInt32();
94 if (ret != NO_ERROR) {
95 IMSA_HILOGE("InputMethodCoreProxy::reply error, ret %{public}d", ret);
96 return ret;
97 }
98 if (output != nullptr && (!output(reply))) {
99 IMSA_HILOGE("InputMethodCoreProxy::reply parcel error");
100 return ErrorCode::ERROR_EX_PARCELABLE;
101 }
102 return ret;
103 }
104 } // namespace MiscServices
105 } // namespace OHOS
106