• 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)30 bool InputMethodAgentProxy::DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent)
31 {
32     bool isConsumed = false;
33     int32_t ret = SendRequest(
34         DISPATCH_KEY_EVENT, [&keyEvent](MessageParcel &data) { return keyEvent->WriteToParcel(data); },
35         [&isConsumed](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isConsumed); });
36     return ret == ErrorCode::NO_ERROR ? isConsumed : false;
37 }
38 
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)39 void InputMethodAgentProxy::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
40 {
41     auto ret = SendRequest(ON_CURSOR_UPDATE, [positionX, positionY, height](MessageParcel &data) {
42         return ITypesUtil::Marshal(data, positionX, positionY, height);
43     });
44     IMSA_HILOGD("InputMethodAgentProxy::OnCursorUpdate ret = %{public}d", ret);
45 }
46 
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)47 void InputMethodAgentProxy::OnSelectionChange(
48     std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
49 {
50     auto ret = SendRequest(ON_SELECTION_CHANGE, [&text, oldBegin, oldEnd, newBegin, newEnd](MessageParcel &data) {
51         return ITypesUtil::Marshal(data, text, oldBegin, oldEnd, newBegin, newEnd);
52     });
53     IMSA_HILOGD("InputMethodAgentProxy::OnSelectionChange ret = %{public}d", ret);
54 }
55 
SetCallingWindow(uint32_t windowId)56 void InputMethodAgentProxy::SetCallingWindow(uint32_t windowId)
57 {
58     auto ret = SendRequest(
59         SET_CALLING_WINDOW_ID, [windowId](MessageParcel &data) { return ITypesUtil::Marshal(data, windowId); });
60     IMSA_HILOGD("InputMethodAgentProxy::SetCallingWindow ret = %{public}d", ret);
61 }
62 
OnConfigurationChange(const Configuration & config)63 void InputMethodAgentProxy::OnConfigurationChange(const Configuration &config)
64 {
65     auto ret = SendRequest(ON_CONFIGURATION_CHANGE, [&config](MessageParcel &data) {
66         return data.WriteInt32(static_cast<int32_t>(config.GetEnterKeyType())) &&
67                data.WriteInt32(static_cast<int32_t>(config.GetTextInputType()));
68     });
69     IMSA_HILOGD("InputMethodAgentProxy, ret = %{public}d", ret);
70 }
71 
SendRequest(int code,ParcelHandler input,ParcelHandler output)72 int32_t InputMethodAgentProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
73 {
74     IMSA_HILOGD("InputMethodAgentProxy::%{public}s in", __func__);
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option{ MessageOption::TF_SYNC };
78     if (!data.WriteInterfaceToken(GetDescriptor())) {
79         IMSA_HILOGE("InputMethodAgentProxy::write interface token failed");
80         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
81     }
82     if (input != nullptr && (!input(data))) {
83         IMSA_HILOGE("InputMethodAgentProxy::write data failed");
84         return ErrorCode::ERROR_EX_PARCELABLE;
85     }
86     auto remote = Remote();
87     if (remote == nullptr) {
88         IMSA_HILOGE("InputMethodAgentProxy::SendRequest remote is nullptr.");
89         return ERROR_EX_NULL_POINTER;
90     }
91     auto ret = remote->SendRequest(code, data, reply, option);
92     if (ret != NO_ERROR) {
93         IMSA_HILOGE("InputMethodCoreProxy::SendRequest failed, ret %{public}d", ret);
94         return ret;
95     }
96     if (output != nullptr && (!output(reply))) {
97         IMSA_HILOGE("InputMethodCoreProxy::reply parcel error");
98         return ErrorCode::ERROR_EX_PARCELABLE;
99     }
100     return ret;
101 }
102 } // namespace MiscServices
103 } // namespace OHOS
104