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_agent_proxy.h"
17
18 #include "global.h"
19 #include "itypes_util.h"
20 #include "message_option.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace ErrorCode;
InputMethodAgentProxy(const sptr<IRemoteObject> & object)25 InputMethodAgentProxy::InputMethodAgentProxy(const sptr<IRemoteObject> &object)
26 : IRemoteProxy<IInputMethodAgent>(object)
27 {
28 }
29
DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<IKeyEventConsumer> & consumer)30 int32_t InputMethodAgentProxy::DispatchKeyEvent(
31 const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<IKeyEventConsumer> &consumer)
32 {
33 if (consumer == nullptr) {
34 IMSA_HILOGE("consumer is nullptr.");
35 return ErrorCode::ERROR_EX_NULL_POINTER;
36 }
37
38 int32_t res = -1;
39 int32_t ret = SendRequest(
40 DISPATCH_KEY_EVENT,
41 [&keyEvent, &consumer](MessageParcel &data) {
42 return keyEvent->WriteToParcel(data) && data.WriteRemoteObject(consumer->AsObject());
43 },
44 [&res](MessageParcel &reply) {
45 return ITypesUtil::Unmarshal(reply, res);
46 });
47 return ret == ErrorCode::NO_ERROR ? res : ret;
48 }
49
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)50 void InputMethodAgentProxy::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
51 {
52 auto ret = SendRequest(ON_CURSOR_UPDATE, [positionX, positionY, height](MessageParcel &data) {
53 return ITypesUtil::Marshal(data, positionX, positionY, height);
54 });
55 IMSA_HILOGD("InputMethodAgentProxy::OnCursorUpdate ret: %{public}d.", ret);
56 }
57
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)58 void InputMethodAgentProxy::OnSelectionChange(
59 std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
60 {
61 auto ret = SendRequest(ON_SELECTION_CHANGE, [&text, oldBegin, oldEnd, newBegin, newEnd](MessageParcel &data) {
62 return ITypesUtil::Marshal(data, text, oldBegin, oldEnd, newBegin, newEnd);
63 });
64 IMSA_HILOGD("InputMethodAgentProxy::OnSelectionChange ret: %{public}d.", ret);
65 }
66
SetCallingWindow(uint32_t windowId)67 void InputMethodAgentProxy::SetCallingWindow(uint32_t windowId)
68 {
69 auto ret = SendRequest(SET_CALLING_WINDOW_ID, [windowId](MessageParcel &data) {
70 return ITypesUtil::Marshal(data, windowId);
71 });
72 IMSA_HILOGD("InputMethodAgentProxy::SetCallingWindow ret: %{public}d.", ret);
73 }
74
OnAttributeChange(const InputAttribute & attribute)75 void InputMethodAgentProxy::OnAttributeChange(const InputAttribute &attribute)
76 {
77 auto ret = SendRequest(ON_ATTRIBUTE_CHANGE, [&attribute](MessageParcel &data) {
78 return ITypesUtil::Marshal(data, attribute);
79 });
80 IMSA_HILOGD("InputMethodAgentProxy, ret: %{public}d.", ret);
81 }
82
SendMessage(const ArrayBuffer & arraybuffer)83 int32_t InputMethodAgentProxy::SendMessage(const ArrayBuffer &arraybuffer)
84 {
85 int32_t ret = 0;
86 SendRequest(SEND_MESSAGE, [&arraybuffer](MessageParcel &data) {
87 return ITypesUtil::Marshal(data, arraybuffer);
88 }, [&ret](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, ret); });
89 return ret;
90 }
91
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)92 int32_t InputMethodAgentProxy::SendPrivateCommand(
93 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
94 {
95 int32_t res = -1;
96 int32_t ret = SendRequest(
97 SEND_PRIVATE_COMMAND,
98 [&privateCommand](MessageParcel &parcel) {
99 return ITypesUtil::Marshal(parcel, privateCommand);
100 },
101 [&res](MessageParcel &reply) {
102 return ITypesUtil::Unmarshal(reply, res);
103 });
104 return ret == ErrorCode::NO_ERROR ? res : ret;
105 }
106
SendRequest(int code,ParcelHandler input,ParcelHandler output)107 int32_t InputMethodAgentProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
108 {
109 IMSA_HILOGD("InputMethodAgentProxy start, code: %{public}d.", code);
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option { MessageOption::TF_SYNC };
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 IMSA_HILOGE("InputMethodAgentProxy::write interface token failed!");
115 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
116 }
117 if (input != nullptr && (!input(data))) {
118 IMSA_HILOGE("InputMethodAgentProxy::write data failed!");
119 return ErrorCode::ERROR_EX_PARCELABLE;
120 }
121 auto remote = Remote();
122 if (remote == nullptr) {
123 IMSA_HILOGE("InputMethodAgentProxy remote is nullptr!");
124 return ErrorCode::ERROR_EX_NULL_POINTER;
125 }
126 auto ret = remote->SendRequest(code, data, reply, option);
127 if (ret != NO_ERROR) {
128 IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d!", code, ret);
129 return ret;
130 }
131 if (output != nullptr && (!output(reply))) {
132 IMSA_HILOGE("InputMethodCoreProxy::reply parcel error!");
133 return ErrorCode::ERROR_EX_PARCELABLE;
134 }
135 return ret;
136 }
137 } // namespace MiscServices
138 } // namespace OHOS