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_client_proxy.h" 17 18 #include "global.h" 19 #include "itypes_util.h" 20 #include "message_option.h" 21 #include "message_parcel.h" 22 23 namespace OHOS { 24 namespace MiscServices { 25 using namespace ErrorCode; InputClientProxy(const sptr<IRemoteObject> & object)26 InputClientProxy::InputClientProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IInputClient>(object) 27 { 28 } 29 onInputReady(const sptr<IInputMethodAgent> & agent)30 int32_t InputClientProxy::onInputReady(const sptr<IInputMethodAgent>& agent) 31 { 32 IMSA_HILOGI("InputClientProxy::onInputReady"); 33 MessageParcel data; 34 MessageParcel reply; 35 MessageOption option; 36 if (!data.WriteInterfaceToken(GetDescriptor())) { 37 return ERROR_EX_PARCELABLE; 38 } 39 40 data.WriteRemoteObject(agent->AsObject().GetRefPtr()); 41 42 auto ret = Remote()->SendRequest(ON_INPUT_READY, data, reply, option); 43 if (ret != NO_ERROR) { 44 IMSA_HILOGI("InputClientProxy::onInputReady SendRequest failed"); 45 return ERROR_STATUS_FAILED_TRANSACTION; 46 } 47 48 return NO_ERROR; 49 } 50 51 OnSwitchInput(const Property & property,const SubProperty & subProperty)52 int32_t InputClientProxy::OnSwitchInput(const Property &property, const SubProperty &subProperty) 53 { 54 IMSA_HILOGI("InputClientProxy::OnSwitchInput"); 55 return SendRequest(ON_SWITCH_INPUT, [&property, &subProperty](MessageParcel &data) { 56 return ITypesUtil::Marshal(data, property, subProperty); 57 }); 58 } 59 SendRequest(int code,ParcelHandler input,ParcelHandler output)60 int32_t InputClientProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output) 61 { 62 IMSA_HILOGI("InputClientProxy::%{public}s in", __func__); 63 MessageParcel data; 64 MessageParcel reply; 65 MessageOption option{ MessageOption::TF_SYNC }; 66 if (!data.WriteInterfaceToken(GetDescriptor())) { 67 IMSA_HILOGE("InputClientProxy::write interface token failed"); 68 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT; 69 } 70 if (input != nullptr && (!input(data))) { 71 IMSA_HILOGE("InputClientProxy::write data failed"); 72 return ErrorCode::ERROR_EX_PARCELABLE; 73 } 74 auto ret = Remote()->SendRequest(code, data, reply, option); 75 if (ret != NO_ERROR) { 76 IMSA_HILOGE("InputClientProxy::SendRequest failed, ret %{public}d", ret); 77 return ret; 78 } 79 ret = reply.ReadInt32(); 80 if (ret != NO_ERROR) { 81 IMSA_HILOGE("InputClientProxy::reply error, ret %{public}d", ret); 82 return ret; 83 } 84 if (output != nullptr && (!output(reply))) { 85 IMSA_HILOGE("InputClientProxy::reply parcel error"); 86 return ErrorCode::ERROR_EX_PARCELABLE; 87 } 88 return ret; 89 } 90 } // namespace MiscServices 91 } // namespace OHOS 92