• 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_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)70 int32_t InputMethodSystemAbilityProxy::ShowInput(sptr<IInputClient> client)
71 {
72     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_INPUT),
73         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
74 }
75 
HideInput(sptr<IInputClient> client)76 int32_t InputMethodSystemAbilityProxy::HideInput(sptr<IInputClient> client)
77 {
78     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_INPUT),
79         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
80 }
81 
ReleaseInput(sptr<IInputClient> client)82 int32_t InputMethodSystemAbilityProxy::ReleaseInput(sptr<IInputClient> client)
83 {
84     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::RELEASE_INPUT),
85         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
86 }
87 
RequestShowInput()88 int32_t InputMethodSystemAbilityProxy::RequestShowInput()
89 {
90     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_SHOW_INPUT));
91 }
92 
RequestHideInput()93 int32_t InputMethodSystemAbilityProxy::RequestHideInput()
94 {
95     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_HIDE_INPUT));
96 }
97 
InitConnect()98 int32_t InputMethodSystemAbilityProxy::InitConnect()
99 {
100     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::INIT_CONNECT));
101 }
102 
DisplayOptionalInputMethod()103 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod()
104 {
105     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::DISPLAY_OPTIONAL_INPUT_METHOD));
106 }
107 
SetCoreAndAgent(const sptr<IInputMethodCore> & core,const sptr<IRemoteObject> & agent)108 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(
109     const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent)
110 {
111     return SendRequest(
112         static_cast<uint32_t>(InputMethodInterfaceCode::SET_CORE_AND_AGENT), [core, agent](MessageParcel &data) {
113             return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent);
114         });
115 }
116 
GetDefaultInputMethod(std::shared_ptr<Property> & property,bool isBrief)117 int32_t InputMethodSystemAbilityProxy::GetDefaultInputMethod(std::shared_ptr<Property> &property, bool isBrief)
118 {
119     return SendRequest(
120         static_cast<uint32_t>(InputMethodInterfaceCode::GET_DEFAULT_INPUT_METHOD),
121         [isBrief](
122             MessageParcel &data) { return ITypesUtil::Marshal(data, isBrief); },
123         [&property](MessageParcel &reply) {
124             property = std::make_shared<Property>();
125             return ITypesUtil::Unmarshal(reply, *property);
126         });
127 }
128 
GetInputMethodConfig(OHOS::AppExecFwk::ElementName & inputMethodConfig)129 int32_t InputMethodSystemAbilityProxy::GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig)
130 {
131     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_INPUT_METHOD_SETTINGS), nullptr,
132         [&inputMethodConfig](MessageParcel& reply) {
133             return ITypesUtil::Unmarshal(reply, inputMethodConfig);
134         });
135 }
136 
GetSecurityMode(int32_t & security)137 int32_t InputMethodSystemAbilityProxy::GetSecurityMode(int32_t &security)
138 {
139     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_SECURITY_MODE), nullptr,
140         [&security](MessageParcel& reply) {
141             return ITypesUtil::Unmarshal(reply, security);
142         });
143 }
144 
UnRegisteredProxyIme(UnRegisteredType type,const sptr<IInputMethodCore> & core)145 int32_t InputMethodSystemAbilityProxy::UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core)
146 {
147     return SendRequest(
148         static_cast<uint32_t>(InputMethodInterfaceCode::UNREGISTERED_PROXY_IME), [&type, &core](MessageParcel &data) {
149             return ITypesUtil::Marshal(data, static_cast<int32_t>(type), core->AsObject());
150         });
151 }
152 
GetCurrentInputMethod()153 std::shared_ptr<Property> InputMethodSystemAbilityProxy::GetCurrentInputMethod()
154 {
155     std::shared_ptr<Property> property = nullptr;
156     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD), nullptr,
157         [&property](MessageParcel &reply) {
158             property = std::make_shared<Property>();
159             return ITypesUtil::Unmarshal(reply, *property);
160         });
161     return ret != ErrorCode::NO_ERROR ? nullptr : property;
162 }
163 
GetCurrentInputMethodSubtype()164 std::shared_ptr<SubProperty> InputMethodSystemAbilityProxy::GetCurrentInputMethodSubtype()
165 {
166     std::shared_ptr<SubProperty> property = nullptr;
167     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD_SUBTYPE),
168         nullptr, [&property](MessageParcel &reply) {
169             property = std::make_shared<SubProperty>();
170             return ITypesUtil::Unmarshal(reply, *property);
171         });
172     return ret != ErrorCode::NO_ERROR ? nullptr : property;
173 }
174 
ListInputMethod(InputMethodStatus status,std::vector<Property> & props)175 int32_t InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status, std::vector<Property> &props)
176 {
177     return SendRequest(
178         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD),
179         [status](MessageParcel &data) { return ITypesUtil::Marshal(data, uint32_t(status)); },
180         [&props](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, props); });
181 }
182 
ShowCurrentInputDeprecated()183 int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated()
184 {
185     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT_DEPRECATED));
186 }
187 
HideCurrentInputDeprecated()188 int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated()
189 {
190     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT_DEPRECATED));
191 }
192 
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)193 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
194     const std::string &name, std::vector<SubProperty> &subProps)
195 {
196     return SendRequest(
197         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD_SUBTYPE),
198         [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
199         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
200 }
201 
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)202 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
203 {
204     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::LIST_CURRENT_INPUT_METHOD_SUBTYPE), nullptr,
205         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
206 }
207 
SwitchInputMethod(const std::string & name,const std::string & subName,SwitchTrigger trigger)208 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(
209     const std::string &name, const std::string &subName, SwitchTrigger trigger)
210 {
211     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SWITCH_INPUT_METHOD),
212         [&name, &subName, trigger](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName, trigger); });
213 }
214 
PanelStatusChange(const InputWindowStatus & status,const ImeWindowInfo & info)215 int32_t InputMethodSystemAbilityProxy::PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info)
216 {
217     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE),
218         [status, &info](MessageParcel &data) {
219             return ITypesUtil::Marshal(data, static_cast<uint32_t>(status), info);
220         });
221 }
222 
UpdateListenEventFlag(InputClientInfo & clientInfo,uint32_t eventFlag)223 int32_t InputMethodSystemAbilityProxy::UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag)
224 {
225     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::UPDATE_LISTEN_EVENT_FLAG),
226         [&clientInfo, eventFlag](MessageParcel &data) {
227             return ITypesUtil::Marshal(data, clientInfo, clientInfo.client->AsObject(), clientInfo.channel, eventFlag);
228         });
229 }
230 
IsCurrentIme()231 bool InputMethodSystemAbilityProxy::IsCurrentIme()
232 {
233     bool isCurrentIme = false;
234     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME), nullptr,
235         [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
236     return isCurrentIme;
237 }
238 
IsCurrentImeByPid(int32_t pid)239 bool InputMethodSystemAbilityProxy::IsCurrentImeByPid(int32_t pid)
240 {
241     bool isCurrentIme = false;
242     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME_BY_PID),
243         [&pid](MessageParcel &data) { return ITypesUtil::Marshal(data, pid); },
244         [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
245     return isCurrentIme;
246 }
247 
IsInputTypeSupported(InputType type)248 bool InputMethodSystemAbilityProxy::IsInputTypeSupported(InputType type)
249 {
250     bool isSupported = false;
251     SendRequest(
252         static_cast<uint32_t>(InputMethodInterfaceCode::IS_INPUT_TYPE_SUPPORTED),
253         [type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); },
254         [&isSupported](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isSupported); });
255     return isSupported;
256 }
257 
StartInputType(InputType type)258 int32_t InputMethodSystemAbilityProxy::StartInputType(InputType type)
259 {
260     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT_TYPE),
261         [&type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); });
262 }
263 
ExitCurrentInputType()264 int32_t InputMethodSystemAbilityProxy::ExitCurrentInputType()
265 {
266     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::EXIT_CURRENT_INPUT_TYPE));
267 }
268 
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)269 int32_t InputMethodSystemAbilityProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
270 {
271     return SendRequest(
272         static_cast<uint32_t>(InputMethodInterfaceCode::IS_PANEL_SHOWN),
273         [&panelInfo](MessageParcel &data) { return ITypesUtil::Marshal(data, panelInfo); },
274         [&isShown](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isShown); });
275 }
276 
IsDefaultIme()277 int32_t InputMethodSystemAbilityProxy::IsDefaultIme()
278 {
279     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_DEFAULT_IME));
280 }
281 
GetMessageOption(int32_t code,MessageOption & option)282 void InputMethodSystemAbilityProxy::GetMessageOption(int32_t code, MessageOption &option)
283 {
284     switch (code) {
285         case static_cast<int32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE): {
286             IMSA_HILOGD("Async IPC.");
287             option.SetFlags(MessageOption::TF_ASYNC);
288             break;
289         }
290         default:
291             option.SetFlags(MessageOption::TF_SYNC);
292             break;
293     }
294 }
295 
SendRequest(int code,ParcelHandler input,ParcelHandler output)296 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
297 {
298     IMSA_HILOGD("IMSAProxy, code = %{public}d.", code);
299     MessageParcel data;
300     MessageParcel reply;
301     MessageOption option;
302     GetMessageOption(code, option);
303 
304     if (!data.WriteInterfaceToken(GetDescriptor())) {
305         IMSA_HILOGE("write interface token failed!");
306         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
307     }
308     if (input != nullptr && (!input(data))) {
309         IMSA_HILOGE("write data failed!");
310         return ErrorCode::ERROR_EX_PARCELABLE;
311     }
312     auto remote = Remote();
313     if (remote == nullptr) {
314         IMSA_HILOGE("remote is nullptr!");
315         return ErrorCode::ERROR_EX_NULL_POINTER;
316     }
317     auto ret = remote->SendRequest(code, data, reply, option);
318     if (ret != NO_ERROR) {
319         IMSA_HILOGE("failed to send request, code: %{public}d, ret %{public}d!", code, ret);
320         return ret;
321     }
322     if (option.GetFlags() == MessageOption::TF_ASYNC) {
323         return ErrorCode::NO_ERROR;
324     }
325     ret = reply.ReadInt32();
326     if (ret != NO_ERROR) {
327         IMSA_HILOGE("dispose failed in service, code: %{public}d, ret: %{public}d!", code, ret);
328         return ret;
329     }
330     if (output != nullptr && (!output(reply))) {
331         IMSA_HILOGE("reply parcel error!");
332         return ErrorCode::ERROR_EX_PARCELABLE;
333     }
334     return ErrorCode::NO_ERROR;
335 }
336 } // namespace MiscServices
337 } // namespace OHOS