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<IRemoteObject> & agent)30 int32_t InputClientProxy::OnInputReady(const sptr<IRemoteObject> &agent)
31 {
32 return SendRequest(ON_INPUT_READY, [agent](MessageParcel &data) { return ITypesUtil::Marshal(data, agent); });
33 }
34
OnInputStop()35 int32_t InputClientProxy::OnInputStop()
36 {
37 return SendRequest(ON_INPUT_STOP);
38 }
39
OnSwitchInput(const Property & property,const SubProperty & subProperty)40 int32_t InputClientProxy::OnSwitchInput(const Property &property, const SubProperty &subProperty)
41 {
42 return SendRequest(ON_SWITCH_INPUT,
43 [&property, &subProperty](MessageParcel &data) { return ITypesUtil::Marshal(data, property, subProperty); });
44 }
45
OnPanelStatusChange(const InputWindowStatus & status,const ImeWindowInfo & info)46 int32_t InputClientProxy::OnPanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info)
47 {
48 return SendRequest(ON_PANEL_STATUS_CHANGE, [&status, &info](MessageParcel &data) {
49 return ITypesUtil::Marshal(data, static_cast<uint32_t>(status), info);
50 });
51 }
52
DeactivateClient()53 void InputClientProxy::DeactivateClient()
54 {
55 SendRequest(DEACTIVATE_CLIENT, nullptr, nullptr, MessageOption::TF_ASYNC);
56 }
57
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)58 int32_t InputClientProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
59 {
60 IMSA_HILOGD("InputClientProxy run in, code = %{public}d", code);
61 MessageParcel data;
62 MessageParcel reply;
63 if (!data.WriteInterfaceToken(GetDescriptor())) {
64 IMSA_HILOGE("InputClientProxy::write interface token failed");
65 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
66 }
67 if (input != nullptr && (!input(data))) {
68 IMSA_HILOGE("InputClientProxy::write data failed");
69 return ErrorCode::ERROR_EX_PARCELABLE;
70 }
71 auto remote = Remote();
72 if (remote == nullptr) {
73 IMSA_HILOGE("InputClientProxy remote is nullptr");
74 return ErrorCode::ERROR_EX_NULL_POINTER;
75 }
76 auto ret = remote->SendRequest(code, data, reply, option);
77 if (ret != NO_ERROR) {
78 IMSA_HILOGE("InputClientProxy send request failed, code: %{public}d, ret: %{public}d", code, ret);
79 return ret;
80 }
81 ret = reply.ReadInt32();
82 if (ret != NO_ERROR) {
83 IMSA_HILOGE("InputClientProxy::reply error, ret %{public}d", ret);
84 return ret;
85 }
86 if (output != nullptr && (!output(reply))) {
87 IMSA_HILOGE("InputClientProxy::reply parcel error");
88 return ErrorCode::ERROR_EX_PARCELABLE;
89 }
90 return ret;
91 }
92 } // namespace MiscServices
93 } // namespace OHOS
94