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 34 InitInputControlChannel(sptr<IInputControlChannel> & inputControlChannel,const std::string & imeId)35 int32_t InputMethodCoreProxy::InitInputControlChannel( 36 sptr<IInputControlChannel> &inputControlChannel, const std::string &imeId) 37 { 38 IMSA_HILOGD("InputMethodCoreProxy::InitInputControlChannel"); 39 auto remote = Remote(); 40 if (!remote) { 41 IMSA_HILOGI("remote is nullptr"); 42 return ErrorCode::ERROR_NULL_POINTER; 43 } 44 if (!inputControlChannel) { 45 IMSA_HILOGI("InputMethodCoreProxy::InitInputControlChannel inputControlChannel is nullptr"); 46 return ErrorCode::ERROR_EX_NULL_POINTER; 47 } 48 MessageParcel data; 49 MessageParcel reply; 50 data.WriteInterfaceToken(GetDescriptor()); 51 sptr<IRemoteObject> channelObject = inputControlChannel->AsObject(); 52 if (!channelObject) { 53 IMSA_HILOGI("InputMethodCoreProxy::InitInputControlChannel channelObject is nullptr"); 54 return ErrorCode::ERROR_EX_NULL_POINTER; 55 } 56 data.WriteRemoteObject(channelObject); 57 data.WriteString(imeId); 58 MessageOption option { 59 MessageOption::TF_SYNC 60 }; 61 int32_t status = remote->SendRequest(INIT_INPUT_CONTROL_CHANNEL, data, reply, option); 62 if (status != ErrorCode::NO_ERROR) { 63 IMSA_HILOGI("InputMethodCoreProxy::InitInputControlChannel status = %{public}d", status); 64 return status; 65 } 66 int32_t code = reply.ReadException(); 67 return code; 68 } 69 showKeyboard(const sptr<IInputDataChannel> & inputDataChannel,bool isShowKeyboard,const SubProperty & subProperty)70 int32_t InputMethodCoreProxy::showKeyboard( 71 const sptr<IInputDataChannel> &inputDataChannel, bool isShowKeyboard, const SubProperty &subProperty) 72 { 73 IMSA_HILOGD("InputMethodCoreProxy::showKeyboard"); 74 return SendRequest(SHOW_KEYBOARD, [&inputDataChannel, &isShowKeyboard, &subProperty](MessageParcel &data) { 75 return ITypesUtil::Marshal(data, inputDataChannel->AsObject(), isShowKeyboard, subProperty); 76 }); 77 } 78 StopInputService(std::string imeId)79 void InputMethodCoreProxy::StopInputService(std::string imeId) 80 { 81 IMSA_HILOGD("InputMethodCoreProxy::StopInputService"); 82 auto remote = Remote(); 83 if (!remote) { 84 IMSA_HILOGI("InputMethodCoreProxy::StopInputService remote is nullptr"); 85 return; 86 } 87 MessageParcel data; 88 if (!(data.WriteInterfaceToken(GetDescriptor()) 89 && data.WriteString16(Str8ToStr16(imeId)))) { 90 return; 91 } 92 MessageParcel reply; 93 MessageOption option { 94 MessageOption::TF_SYNC 95 }; 96 97 int32_t res = remote->SendRequest(STOP_INPUT_SERVICE, data, reply, option); 98 if (res != ErrorCode::NO_ERROR) { 99 return; 100 } 101 } 102 hideKeyboard(int32_t flags)103 bool InputMethodCoreProxy::hideKeyboard(int32_t flags) 104 { 105 IMSA_HILOGD("InputMethodCoreProxy::hideKeyboard"); 106 auto remote = Remote(); 107 if (!remote) { 108 return false; 109 } 110 111 MessageParcel data; 112 if (!(data.WriteInterfaceToken(GetDescriptor()) && data.WriteInt32(flags))) { 113 return false; 114 } 115 MessageParcel reply; 116 MessageOption option { 117 MessageOption::TF_SYNC 118 }; 119 120 int32_t res = remote->SendRequest(HIDE_KEYBOARD, data, reply, option); 121 if (res != ErrorCode::NO_ERROR) { 122 return false; 123 } 124 return true; 125 } 126 127 SetSubtype(const SubProperty & property)128 int32_t InputMethodCoreProxy::SetSubtype(const SubProperty &property) 129 { 130 IMSA_HILOGD("InputMethodCoreProxy::SetSubtype"); 131 return SendRequest( 132 SET_SUBTYPE, [&property](MessageParcel &data) { return ITypesUtil::Marshal(data, property); }); 133 } 134 SendRequest(int code,ParcelHandler input,ParcelHandler output)135 int32_t InputMethodCoreProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output) 136 { 137 IMSA_HILOGD("InputMethodCoreProxy::%{public}s in", __func__); 138 MessageParcel data; 139 MessageParcel reply; 140 MessageOption option{ MessageOption::TF_SYNC }; 141 if (!data.WriteInterfaceToken(GetDescriptor())) { 142 IMSA_HILOGE("InputMethodCoreProxy::write interface token failed"); 143 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT; 144 } 145 if (input != nullptr && (!input(data))) { 146 IMSA_HILOGE("InputMethodCoreProxy::write data failed"); 147 return ErrorCode::ERROR_EX_PARCELABLE; 148 } 149 auto ret = Remote()->SendRequest(code, data, reply, option); 150 if (ret != NO_ERROR) { 151 IMSA_HILOGE("InputMethodCoreProxy::SendRequest failed, ret %{public}d", ret); 152 return ret; 153 } 154 ret = reply.ReadInt32(); 155 if (ret != NO_ERROR) { 156 IMSA_HILOGE("InputMethodCoreProxy::reply error, ret %{public}d", ret); 157 return ret; 158 } 159 if (output != nullptr && (!output(reply))) { 160 IMSA_HILOGE("InputMethodCoreProxy::reply parcel error"); 161 return ErrorCode::ERROR_EX_PARCELABLE; 162 } 163 return ret; 164 } 165 } // namespace MiscServices 166 } // namespace OHOS 167