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(
63 SET_CALLING_WINDOW_ID, [windowId](MessageParcel &data) { return ITypesUtil::Marshal(data, windowId); });
64 IMSA_HILOGD("InputMethodAgentProxy::SetCallingWindow ret = %{public}d", ret);
65 }
66
OnConfigurationChange(const Configuration & config)67 void InputMethodAgentProxy::OnConfigurationChange(const Configuration &config)
68 {
69 auto ret = SendRequest(ON_CONFIGURATION_CHANGE, [&config](MessageParcel &data) {
70 return data.WriteInt32(static_cast<int32_t>(config.GetEnterKeyType()))
71 && data.WriteInt32(static_cast<int32_t>(config.GetTextInputType()));
72 });
73 IMSA_HILOGD("InputMethodAgentProxy, ret = %{public}d", ret);
74 }
75
SendRequest(int code,ParcelHandler input,ParcelHandler output)76 int32_t InputMethodAgentProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
77 {
78 IMSA_HILOGD("InputMethodAgentProxy run in, code = %{public}d", code);
79 MessageParcel data;
80 MessageParcel reply;
81 MessageOption option{ MessageOption::TF_SYNC };
82 if (!data.WriteInterfaceToken(GetDescriptor())) {
83 IMSA_HILOGE("InputMethodAgentProxy::write interface token failed");
84 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
85 }
86 if (input != nullptr && (!input(data))) {
87 IMSA_HILOGE("InputMethodAgentProxy::write data failed");
88 return ErrorCode::ERROR_EX_PARCELABLE;
89 }
90 auto remote = Remote();
91 if (remote == nullptr) {
92 IMSA_HILOGE("InputMethodAgentProxy::SendRequest remote is nullptr.");
93 return ERROR_EX_NULL_POINTER;
94 }
95 auto ret = remote->SendRequest(code, data, reply, option);
96 if (ret != NO_ERROR) {
97 IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d", code, ret);
98 return ret;
99 }
100 if (output != nullptr && (!output(reply))) {
101 IMSA_HILOGE("InputMethodCoreProxy::reply parcel error");
102 return ErrorCode::ERROR_EX_PARCELABLE;
103 }
104 return ret;
105 }
106 } // namespace MiscServices
107 } // namespace OHOS
108