• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SendMessage(const ArrayBuffer & arraybuffer)74 int32_t InputMethodAgentProxy::SendMessage(const ArrayBuffer &arraybuffer)
75 {
76     int32_t ret = 0;
77     SendRequest(SEND_MESSAGE, [&arraybuffer](MessageParcel &data) {
78         return ITypesUtil::Marshal(data, arraybuffer);
79     }, [&ret](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, ret); });
80     return ret;
81 }
82 
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)83 int32_t InputMethodAgentProxy::SendPrivateCommand(
84     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
85 {
86     int32_t res = -1;
87     int32_t ret = SendRequest(
88         SEND_PRIVATE_COMMAND,
89         [&privateCommand](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, privateCommand); },
90         [&res](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, res); });
91     return ret == ErrorCode::NO_ERROR ? res : ret;
92 }
93 
SendRequest(int code,ParcelHandler input,ParcelHandler output)94 int32_t InputMethodAgentProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
95 {
96     IMSA_HILOGD("InputMethodAgentProxy start, code: %{public}d.", code);
97     MessageParcel data;
98     MessageParcel reply;
99     MessageOption option{ MessageOption::TF_SYNC };
100     if (!data.WriteInterfaceToken(GetDescriptor())) {
101         IMSA_HILOGE("InputMethodAgentProxy::write interface token failed!");
102         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
103     }
104     if (input != nullptr && (!input(data))) {
105         IMSA_HILOGE("InputMethodAgentProxy::write data failed!");
106         return ErrorCode::ERROR_EX_PARCELABLE;
107     }
108     auto remote = Remote();
109     if (remote == nullptr) {
110         IMSA_HILOGE("InputMethodAgentProxy remote is nullptr!");
111         return ErrorCode::ERROR_EX_NULL_POINTER;
112     }
113     auto ret = remote->SendRequest(code, data, reply, option);
114     if (ret != NO_ERROR) {
115         IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d!", code, ret);
116         return ret;
117     }
118     if (output != nullptr && (!output(reply))) {
119         IMSA_HILOGE("InputMethodCoreProxy::reply parcel error!");
120         return ErrorCode::ERROR_EX_PARCELABLE;
121     }
122     return ret;
123 }
124 } // namespace MiscServices
125 } // namespace OHOS
126