• 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_core_proxy.h"
17 
18 #include <string_ex.h>
19 
20 #include "input_attribute.h"
21 #include "itypes_util.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> & impl)27 InputMethodCoreProxy::InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl)
28     : IRemoteProxy<IInputMethodCore>(impl)
29 {
30 }
31 
32 InputMethodCoreProxy::~InputMethodCoreProxy() = default;
33 
InitInputControlChannel(const sptr<IInputControlChannel> & inputControlChannel)34 int32_t InputMethodCoreProxy::InitInputControlChannel(const sptr<IInputControlChannel> &inputControlChannel)
35 {
36     if (inputControlChannel == nullptr) {
37         IMSA_HILOGE("inputControlChannel is nullptr.");
38         return ErrorCode::ERROR_EX_NULL_POINTER;
39     }
40 
41     return SendRequest(INIT_INPUT_CONTROL_CHANNEL, [&inputControlChannel](MessageParcel &data) {
42         return ITypesUtil::Marshal(data, inputControlChannel->AsObject());
43     });
44 }
45 
StartInput(const InputClientInfo & clientInfo,bool isBindFromClient)46 int32_t InputMethodCoreProxy::StartInput(const InputClientInfo &clientInfo, bool isBindFromClient)
47 {
48     IMSA_HILOGI("CoreProxy start.");
49     return SendRequest(START_INPUT, [&clientInfo, isBindFromClient](MessageParcel &data) {
50         return ITypesUtil::Marshal(data, isBindFromClient, clientInfo, clientInfo.channel);
51     });
52 }
53 
OnSecurityChange(int32_t security)54 int32_t InputMethodCoreProxy::OnSecurityChange(int32_t security)
55 {
56     return SendRequest(SECURITY_CHANGE, [security](MessageParcel &data) {
57         return ITypesUtil::Marshal(data, security);
58     });
59 }
60 
OnSetInputType(InputType inputType)61 int32_t InputMethodCoreProxy::OnSetInputType(InputType inputType)
62 {
63     return SendRequest(
64         ON_SET_INPUT_TYPE,
65         [inputType](MessageParcel &data) {
66             return ITypesUtil::Marshal(data, inputType);
67         },
68         nullptr, MessageOption::TF_ASYNC);
69 }
70 
OnConnectSystemCmd(const sptr<IRemoteObject> & channel,sptr<IRemoteObject> & agent)71 int32_t InputMethodCoreProxy::OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent)
72 {
73     return SendRequest(
74         ON_CONNECT_SYSTEM_CMD,
75         [channel](MessageParcel &data) {
76             return data.WriteRemoteObject(channel);
77         },
78         [&agent](MessageParcel &reply) {
79             return ITypesUtil::Unmarshal(reply, agent);
80         });
81 }
82 
StopInputService(bool isTerminateIme)83 int32_t InputMethodCoreProxy::StopInputService(bool isTerminateIme)
84 {
85     return SendRequest(STOP_INPUT_SERVICE, [isTerminateIme](MessageParcel &data) {
86         return ITypesUtil::Marshal(data, isTerminateIme);
87     });
88 }
89 
ShowKeyboard()90 int32_t InputMethodCoreProxy::ShowKeyboard()
91 {
92     return SendRequest(SHOW_KEYBOARD);
93 }
94 
HideKeyboard()95 int32_t InputMethodCoreProxy::HideKeyboard()
96 {
97     return SendRequest(HIDE_KEYBOARD);
98 }
99 
SetSubtype(const SubProperty & property)100 int32_t InputMethodCoreProxy::SetSubtype(const SubProperty &property)
101 {
102     return SendRequest(SET_SUBTYPE, [&property](MessageParcel &data) {
103         return ITypesUtil::Marshal(data, property);
104     });
105 }
106 
StopInput(const sptr<IRemoteObject> & channel)107 int32_t InputMethodCoreProxy::StopInput(const sptr<IRemoteObject> &channel)
108 {
109     return SendRequest(STOP_INPUT, [&channel](MessageParcel &data) {
110         return ITypesUtil::Marshal(data, channel);
111     });
112 }
113 
IsEnable()114 bool InputMethodCoreProxy::IsEnable()
115 {
116     bool isEnable = false;
117     SendRequest(IS_ENABLE, nullptr, [&isEnable](MessageParcel &reply) {
118         return ITypesUtil::Unmarshal(reply, isEnable);
119     });
120     return isEnable;
121 }
122 
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)123 int32_t InputMethodCoreProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
124 {
125     return SendRequest(
126         IS_PANEL_SHOWN,
127         [&panelInfo](MessageParcel &data) {
128             return ITypesUtil::Marshal(data, panelInfo);
129         },
130         [&isShown](MessageParcel &reply) {
131             return ITypesUtil::Unmarshal(reply, isShown);
132         });
133 }
134 
OnClientInactive(const sptr<IRemoteObject> & channel)135 void InputMethodCoreProxy::OnClientInactive(const sptr<IRemoteObject> &channel)
136 {
137     SendRequest(
138         ON_CLIENT_INACTIVE,
139         [&channel](MessageParcel &data) {
140             return ITypesUtil::Marshal(data, channel);
141         },
142         nullptr, MessageOption::TF_ASYNC);
143 }
144 
OnCallingDisplayIdChanged(uint64_t dispalyId)145 void InputMethodCoreProxy::OnCallingDisplayIdChanged(uint64_t dispalyId)
146 {
147     SendRequest(
148         ON_CALLING_DISPLAY_CHANGE,
149         [&dispalyId](MessageParcel &data) {
150             return ITypesUtil::Marshal(data, dispalyId);
151         });
152 }
153 
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)154 int32_t InputMethodCoreProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
155 {
156     IMSA_HILOGD("InputMethodCoreProxy, run in, code = %{public}d.", code);
157     MessageParcel data;
158     MessageParcel reply;
159     if (!data.WriteInterfaceToken(GetDescriptor())) {
160         IMSA_HILOGE("InputMethodCoreProxy::write interface token failed!");
161         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
162     }
163     if (input != nullptr && (!input(data))) {
164         IMSA_HILOGE("InputMethodCoreProxy::write data failed!");
165         return ErrorCode::ERROR_EX_PARCELABLE;
166     }
167     auto remote = Remote();
168     if (remote == nullptr) {
169         IMSA_HILOGE("InputMethodCoreProxy::remote is nullptr!");
170         return ErrorCode::ERROR_IPC_REMOTE_NULLPTR;
171     }
172     auto ret = remote->SendRequest(code, data, reply, option);
173     if (ret != NO_ERROR) {
174         IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d!", code, ret);
175         return ret;
176     }
177     ret = reply.ReadInt32();
178     if (ret != NO_ERROR) {
179         IMSA_HILOGE("InputMethodCoreProxy::reply error, ret: %{public}d!", ret);
180         return ret;
181     }
182     if (output != nullptr && (!output(reply))) {
183         IMSA_HILOGE("InputMethodCoreProxy::reply parcel error!");
184         return ErrorCode::ERROR_EX_PARCELABLE;
185     }
186     return ret;
187 }
188 } // namespace MiscServices
189 } // namespace OHOS