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_system_ability_proxy.h"
17
18 #include "element_name.h"
19 #include "global.h"
20 #include "inputmethod_service_ipc_interface_code.h"
21 #include "itypes_util.h"
22 #include "message_option.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 using namespace ErrorCode;
27
InputMethodSystemAbilityProxy(const sptr<IRemoteObject> & object)28 InputMethodSystemAbilityProxy::InputMethodSystemAbilityProxy(const sptr<IRemoteObject> &object)
29 : IRemoteProxy<IInputMethodSystemAbility>(object)
30 {
31 }
32
StartInput(InputClientInfo & inputClientInfo,sptr<IRemoteObject> & agent)33 int32_t InputMethodSystemAbilityProxy::StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent)
34 {
35 return SendRequest(
36 static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT),
37 [&inputClientInfo](MessageParcel &data) {
38 return ITypesUtil::Marshal(
39 data, inputClientInfo, inputClientInfo.client->AsObject(), inputClientInfo.channel);
40 },
41 [&agent](MessageParcel &reply) {
42 agent = reply.ReadRemoteObject();
43 return true;
44 });
45 }
46
ConnectSystemCmd(const sptr<IRemoteObject> & channel,sptr<IRemoteObject> & agent)47 int32_t InputMethodSystemAbilityProxy::ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent)
48 {
49 return SendRequest(
50 static_cast<uint32_t>(InputMethodInterfaceCode::CONNECT_SYSTEM_CMD),
51 [channel](MessageParcel &data) { return data.WriteRemoteObject(channel); },
52 [&agent](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, agent); });
53 }
54
ShowCurrentInput()55 int32_t InputMethodSystemAbilityProxy::ShowCurrentInput()
56 {
57 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT));
58 }
59
HideCurrentInput()60 int32_t InputMethodSystemAbilityProxy::HideCurrentInput()
61 {
62 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT));
63 }
64
StopInputSession()65 int32_t InputMethodSystemAbilityProxy::StopInputSession()
66 {
67 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::STOP_INPUT_SESSION));
68 }
69
ShowInput(sptr<IInputClient> client,int32_t requestKeyboardReason)70 int32_t InputMethodSystemAbilityProxy::ShowInput(
71 sptr<IInputClient> client, int32_t requestKeyboardReason)
72 {
73 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_INPUT),
74 [client, requestKeyboardReason](MessageParcel &data) {
75 auto ret = data.WriteRemoteObject(client->AsObject());
76 ITypesUtil::Marshal(data, requestKeyboardReason);
77 return ret;
78 });
79 }
80
HideInput(sptr<IInputClient> client)81 int32_t InputMethodSystemAbilityProxy::HideInput(sptr<IInputClient> client)
82 {
83 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_INPUT),
84 [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
85 }
86
ReleaseInput(sptr<IInputClient> client)87 int32_t InputMethodSystemAbilityProxy::ReleaseInput(sptr<IInputClient> client)
88 {
89 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::RELEASE_INPUT),
90 [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
91 }
92
RequestShowInput()93 int32_t InputMethodSystemAbilityProxy::RequestShowInput()
94 {
95 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_SHOW_INPUT));
96 }
97
RequestHideInput()98 int32_t InputMethodSystemAbilityProxy::RequestHideInput()
99 {
100 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_HIDE_INPUT));
101 }
102
InitConnect()103 int32_t InputMethodSystemAbilityProxy::InitConnect()
104 {
105 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::INIT_CONNECT));
106 }
107
DisplayOptionalInputMethod()108 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod()
109 {
110 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::DISPLAY_OPTIONAL_INPUT_METHOD));
111 }
112
SetCoreAndAgent(const sptr<IInputMethodCore> & core,const sptr<IRemoteObject> & agent)113 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(
114 const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent)
115 {
116 return SendRequest(
117 static_cast<uint32_t>(InputMethodInterfaceCode::SET_CORE_AND_AGENT), [core, agent](MessageParcel &data) {
118 return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent);
119 });
120 }
121
GetDefaultInputMethod(std::shared_ptr<Property> & property,bool isBrief)122 int32_t InputMethodSystemAbilityProxy::GetDefaultInputMethod(std::shared_ptr<Property> &property, bool isBrief)
123 {
124 return SendRequest(
125 static_cast<uint32_t>(InputMethodInterfaceCode::GET_DEFAULT_INPUT_METHOD),
126 [isBrief](
127 MessageParcel &data) { return ITypesUtil::Marshal(data, isBrief); },
128 [&property](MessageParcel &reply) {
129 property = std::make_shared<Property>();
130 return ITypesUtil::Unmarshal(reply, *property);
131 });
132 }
133
GetInputMethodConfig(OHOS::AppExecFwk::ElementName & inputMethodConfig)134 int32_t InputMethodSystemAbilityProxy::GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig)
135 {
136 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_INPUT_METHOD_SETTINGS), nullptr,
137 [&inputMethodConfig](MessageParcel& reply) {
138 return ITypesUtil::Unmarshal(reply, inputMethodConfig);
139 });
140 }
141
GetSecurityMode(int32_t & security)142 int32_t InputMethodSystemAbilityProxy::GetSecurityMode(int32_t &security)
143 {
144 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_SECURITY_MODE), nullptr,
145 [&security](MessageParcel& reply) {
146 return ITypesUtil::Unmarshal(reply, security);
147 });
148 }
149
UnRegisteredProxyIme(UnRegisteredType type,const sptr<IInputMethodCore> & core)150 int32_t InputMethodSystemAbilityProxy::UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core)
151 {
152 return SendRequest(
153 static_cast<uint32_t>(InputMethodInterfaceCode::UNREGISTERED_PROXY_IME), [&type, &core](MessageParcel &data) {
154 return ITypesUtil::Marshal(data, static_cast<int32_t>(type), core->AsObject());
155 });
156 }
157
GetCurrentInputMethod()158 std::shared_ptr<Property> InputMethodSystemAbilityProxy::GetCurrentInputMethod()
159 {
160 std::shared_ptr<Property> property = nullptr;
161 int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD), nullptr,
162 [&property](MessageParcel &reply) {
163 property = std::make_shared<Property>();
164 return ITypesUtil::Unmarshal(reply, *property);
165 });
166 return ret != ErrorCode::NO_ERROR ? nullptr : property;
167 }
168
GetCurrentInputMethodSubtype()169 std::shared_ptr<SubProperty> InputMethodSystemAbilityProxy::GetCurrentInputMethodSubtype()
170 {
171 std::shared_ptr<SubProperty> property = nullptr;
172 int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD_SUBTYPE),
173 nullptr, [&property](MessageParcel &reply) {
174 property = std::make_shared<SubProperty>();
175 return ITypesUtil::Unmarshal(reply, *property);
176 });
177 return ret != ErrorCode::NO_ERROR ? nullptr : property;
178 }
179
IsDefaultImeSet()180 bool InputMethodSystemAbilityProxy::IsDefaultImeSet()
181 {
182 bool isDefaultImeSet = false;
183 IMSA_HILOGI("InputMethodSystemAbilityProxy::IsDefaultImeSet enter.");
184 SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_DEFAULT_IME_SET), nullptr,
185 [&isDefaultImeSet](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isDefaultImeSet); });
186 return isDefaultImeSet;
187 }
188
EnableIme(const std::string & bundleName)189 bool InputMethodSystemAbilityProxy::EnableIme(const std::string &bundleName)
190 {
191 bool enableIme = false;
192 IMSA_HILOGI("InputMethodSystemAbilityProxy::EnableIme enter.");
193 SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::ENABLE_IME),
194 [&bundleName](MessageParcel &data) { return ITypesUtil::Marshal(data, bundleName); },
195 [&enableIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, enableIme); });
196 return enableIme;
197 }
198
SetCallingWindow(uint32_t windowId,sptr<IInputClient> client)199 int32_t InputMethodSystemAbilityProxy::SetCallingWindow(uint32_t windowId, sptr<IInputClient> client)
200 {
201 IMSA_HILOGI("proxy setCallingWindow enter");
202 if (client == nullptr) {
203 IMSA_HILOGE("nullptr client");
204 return ErrorCode::ERROR_EX_NULL_POINTER;
205 }
206 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SET_CALLING_WINDOW),
207 [windowId, client](MessageParcel &data) { return ITypesUtil::Marshal(data, windowId, client->AsObject()); });
208 }
209
GetInputStartInfo(bool & isInputStart,uint32_t & callingWndId,int32_t & requestKeyboardReason)210 int32_t InputMethodSystemAbilityProxy::GetInputStartInfo(bool &isInputStart,
211 uint32_t &callingWndId, int32_t &requestKeyboardReason)
212 {
213 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_INPUT_START_INFO), nullptr,
214 [&isInputStart, &callingWndId, &requestKeyboardReason](MessageParcel &reply) {
215 return ITypesUtil::Unmarshal(reply, isInputStart, callingWndId, requestKeyboardReason);
216 });
217 }
218
ListInputMethod(InputMethodStatus status,std::vector<Property> & props)219 int32_t InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status, std::vector<Property> &props)
220 {
221 return SendRequest(
222 static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD),
223 [status](MessageParcel &data) { return ITypesUtil::Marshal(data, uint32_t(status)); },
224 [&props](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, props); });
225 }
226
ShowCurrentInputDeprecated()227 int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated()
228 {
229 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT_DEPRECATED));
230 }
231
HideCurrentInputDeprecated()232 int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated()
233 {
234 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT_DEPRECATED));
235 }
236
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)237 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
238 const std::string &name, std::vector<SubProperty> &subProps)
239 {
240 return SendRequest(
241 static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD_SUBTYPE),
242 [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
243 [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
244 }
245
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)246 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
247 {
248 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::LIST_CURRENT_INPUT_METHOD_SUBTYPE), nullptr,
249 [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
250 }
251
SwitchInputMethod(const std::string & name,const std::string & subName,SwitchTrigger trigger)252 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(
253 const std::string &name, const std::string &subName, SwitchTrigger trigger)
254 {
255 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SWITCH_INPUT_METHOD),
256 [&name, &subName, trigger](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName, trigger); });
257 }
258
PanelStatusChange(const InputWindowStatus & status,const ImeWindowInfo & info)259 int32_t InputMethodSystemAbilityProxy::PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info)
260 {
261 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE),
262 [status, &info](MessageParcel &data) {
263 return ITypesUtil::Marshal(data, static_cast<uint32_t>(status), info);
264 });
265 }
266
UpdateListenEventFlag(InputClientInfo & clientInfo,uint32_t eventFlag)267 int32_t InputMethodSystemAbilityProxy::UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag)
268 {
269 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::UPDATE_LISTEN_EVENT_FLAG),
270 [&clientInfo, eventFlag](MessageParcel &data) {
271 return ITypesUtil::Marshal(data, clientInfo, clientInfo.client->AsObject(), clientInfo.channel, eventFlag);
272 });
273 }
274
IsCurrentIme()275 bool InputMethodSystemAbilityProxy::IsCurrentIme()
276 {
277 bool isCurrentIme = false;
278 SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME), nullptr,
279 [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
280 return isCurrentIme;
281 }
282
IsCurrentImeByPid(int32_t pid)283 bool InputMethodSystemAbilityProxy::IsCurrentImeByPid(int32_t pid)
284 {
285 bool isCurrentIme = false;
286 SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME_BY_PID),
287 [&pid](MessageParcel &data) { return ITypesUtil::Marshal(data, pid); },
288 [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
289 return isCurrentIme;
290 }
291
IsInputTypeSupported(InputType type)292 bool InputMethodSystemAbilityProxy::IsInputTypeSupported(InputType type)
293 {
294 bool isSupported = false;
295 SendRequest(
296 static_cast<uint32_t>(InputMethodInterfaceCode::IS_INPUT_TYPE_SUPPORTED),
297 [type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); },
298 [&isSupported](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isSupported); });
299 return isSupported;
300 }
301
StartInputType(InputType type)302 int32_t InputMethodSystemAbilityProxy::StartInputType(InputType type)
303 {
304 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT_TYPE),
305 [&type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); });
306 }
307
ExitCurrentInputType()308 int32_t InputMethodSystemAbilityProxy::ExitCurrentInputType()
309 {
310 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::EXIT_CURRENT_INPUT_TYPE));
311 }
312
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)313 int32_t InputMethodSystemAbilityProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
314 {
315 return SendRequest(
316 static_cast<uint32_t>(InputMethodInterfaceCode::IS_PANEL_SHOWN),
317 [&panelInfo](MessageParcel &data) { return ITypesUtil::Marshal(data, panelInfo); },
318 [&isShown](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isShown); });
319 }
320
IsDefaultIme()321 int32_t InputMethodSystemAbilityProxy::IsDefaultIme()
322 {
323 return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_DEFAULT_IME));
324 }
325
IsSystemApp()326 bool InputMethodSystemAbilityProxy::IsSystemApp()
327 {
328 bool isSystemApp = false;
329 SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_SYSTEM_APP), nullptr,
330 [&isSystemApp](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isSystemApp); });
331 return isSystemApp;
332 }
333
GetInputMethodState(EnabledStatus & status)334 int32_t InputMethodSystemAbilityProxy::GetInputMethodState(EnabledStatus &status)
335 {
336 int32_t statusTmp = 0;
337 auto ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_IME_STATE), nullptr,
338 [&statusTmp](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, statusTmp); });
339 if (ret != ErrorCode::NO_ERROR) {
340 return ret;
341 }
342 status = static_cast<EnabledStatus>(statusTmp);
343 return ErrorCode::NO_ERROR;
344 }
345
GetMessageOption(int32_t code,MessageOption & option)346 void InputMethodSystemAbilityProxy::GetMessageOption(int32_t code, MessageOption &option)
347 {
348 switch (code) {
349 case static_cast<int32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE): {
350 IMSA_HILOGD("Async IPC.");
351 option.SetFlags(MessageOption::TF_ASYNC);
352 break;
353 }
354 default:
355 option.SetFlags(MessageOption::TF_SYNC);
356 break;
357 }
358 }
359
SendRequest(int code,ParcelHandler input,ParcelHandler output)360 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
361 {
362 IMSA_HILOGD("IMSAProxy, code = %{public}d.", code);
363 MessageParcel data;
364 MessageParcel reply;
365 MessageOption option;
366 GetMessageOption(code, option);
367
368 if (!data.WriteInterfaceToken(GetDescriptor())) {
369 IMSA_HILOGE("write interface token failed!");
370 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
371 }
372 if (input != nullptr && (!input(data))) {
373 IMSA_HILOGE("write data failed!");
374 return ErrorCode::ERROR_EX_PARCELABLE;
375 }
376 auto remote = Remote();
377 if (remote == nullptr) {
378 IMSA_HILOGE("remote is nullptr!");
379 return ErrorCode::ERROR_EX_NULL_POINTER;
380 }
381 auto ret = remote->SendRequest(code, data, reply, option);
382 if (ret != NO_ERROR) {
383 IMSA_HILOGE("failed to send request, code: %{public}d, ret %{public}d!", code, ret);
384 return ret;
385 }
386 if (option.GetFlags() == MessageOption::TF_ASYNC) {
387 return ErrorCode::NO_ERROR;
388 }
389 ret = reply.ReadInt32();
390 if (ret != NO_ERROR) {
391 IMSA_HILOGE("dispose failed in service, code: %{public}d, ret: %{public}d!", code, ret);
392 return ret;
393 }
394 if (output != nullptr && (!output(reply))) {
395 IMSA_HILOGE("reply parcel error!");
396 return ErrorCode::ERROR_EX_PARCELABLE;
397 }
398 return ErrorCode::NO_ERROR;
399 }
400 } // namespace MiscServices
401 } // namespace OHOS