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