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 int32_t res = -1;
34 int32_t ret = SendRequest(
35 DISPATCH_KEY_EVENT,
36 [&keyEvent, &consumer](MessageParcel &data) {
37 return keyEvent->WriteToParcel(data) && data.WriteRemoteObject(consumer->AsObject());
38 },
39 [&res](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, res); });
40 return ret == ErrorCode::NO_ERROR ? res : ret;
41 }
42
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)43 void InputMethodAgentProxy::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
44 {
45 auto ret = SendRequest(ON_CURSOR_UPDATE, [positionX, positionY, height](MessageParcel &data) {
46 return ITypesUtil::Marshal(data, positionX, positionY, height);
47 });
48 IMSA_HILOGD("InputMethodAgentProxy::OnCursorUpdate ret: %{public}d.", ret);
49 }
50
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)51 void InputMethodAgentProxy::OnSelectionChange(
52 std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
53 {
54 auto ret = SendRequest(ON_SELECTION_CHANGE, [&text, oldBegin, oldEnd, newBegin, newEnd](MessageParcel &data) {
55 return ITypesUtil::Marshal(data, text, oldBegin, oldEnd, newBegin, newEnd);
56 });
57 IMSA_HILOGD("InputMethodAgentProxy::OnSelectionChange ret: %{public}d.", ret);
58 }
59
SetCallingWindow(uint32_t windowId)60 void InputMethodAgentProxy::SetCallingWindow(uint32_t windowId)
61 {
62 auto ret = SendRequest(SET_CALLING_WINDOW_ID,
63 [windowId](MessageParcel &data) { return ITypesUtil::Marshal(data, windowId); });
64 IMSA_HILOGD("InputMethodAgentProxy::SetCallingWindow ret: %{public}d.", ret);
65 }
66
OnAttributeChange(const InputAttribute & attribute)67 void InputMethodAgentProxy::OnAttributeChange(const InputAttribute &attribute)
68 {
69 auto ret = SendRequest(ON_ATTRIBUTE_CHANGE,
70 [&attribute](MessageParcel &data) { return ITypesUtil::Marshal(data, attribute); });
71 IMSA_HILOGD("InputMethodAgentProxy, ret: %{public}d.", ret);
72 }
73
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)74 int32_t InputMethodAgentProxy::SendPrivateCommand(
75 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
76 {
77 int32_t res = -1;
78 int32_t ret = SendRequest(
79 SEND_PRIVATE_COMMAND,
80 [&privateCommand](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, privateCommand); },
81 [&res](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, res); });
82 return ret == ErrorCode::NO_ERROR ? res : ret;
83 }
84
SendRequest(int code,ParcelHandler input,ParcelHandler output)85 int32_t InputMethodAgentProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
86 {
87 IMSA_HILOGD("InputMethodAgentProxy start, code: %{public}d.", code);
88 MessageParcel data;
89 MessageParcel reply;
90 MessageOption option{ MessageOption::TF_SYNC };
91 if (!data.WriteInterfaceToken(GetDescriptor())) {
92 IMSA_HILOGE("InputMethodAgentProxy::write interface token failed!");
93 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
94 }
95 if (input != nullptr && (!input(data))) {
96 IMSA_HILOGE("InputMethodAgentProxy::write data failed!");
97 return ErrorCode::ERROR_EX_PARCELABLE;
98 }
99 auto remote = Remote();
100 if (remote == nullptr) {
101 IMSA_HILOGE("InputMethodAgentProxy remote is nullptr!");
102 return ErrorCode::ERROR_EX_NULL_POINTER;
103 }
104 auto ret = remote->SendRequest(code, data, reply, option);
105 if (ret != NO_ERROR) {
106 IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d!", code, ret);
107 return ret;
108 }
109 if (output != nullptr && (!output(reply))) {
110 IMSA_HILOGE("InputMethodCoreProxy::reply parcel error!");
111 return ErrorCode::ERROR_EX_PARCELABLE;
112 }
113 return ret;
114 }
115 } // namespace MiscServices
116 } // namespace OHOS
117