• 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) { return ITypesUtil::Marshal(data, inputClientInfo); },
38         [&agent](MessageParcel &reply) {
39             agent = reply.ReadRemoteObject();
40             return true;
41         });
42 }
43 
ShowCurrentInput()44 int32_t InputMethodSystemAbilityProxy::ShowCurrentInput()
45 {
46     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT));
47 }
48 
HideCurrentInput()49 int32_t InputMethodSystemAbilityProxy::HideCurrentInput()
50 {
51     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT));
52 }
53 
StopInputSession()54 int32_t InputMethodSystemAbilityProxy::StopInputSession()
55 {
56     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::STOP_INPUT_SESSION));
57 }
58 
ShowInput(sptr<IInputClient> client)59 int32_t InputMethodSystemAbilityProxy::ShowInput(sptr<IInputClient> client)
60 {
61     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_INPUT),
62         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
63 }
64 
HideInput(sptr<IInputClient> client)65 int32_t InputMethodSystemAbilityProxy::HideInput(sptr<IInputClient> client)
66 {
67     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_INPUT),
68         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
69 }
70 
ReleaseInput(sptr<IInputClient> client)71 int32_t InputMethodSystemAbilityProxy::ReleaseInput(sptr<IInputClient> client)
72 {
73     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::RELEASE_INPUT),
74         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
75 }
76 
RequestShowInput()77 int32_t InputMethodSystemAbilityProxy::RequestShowInput()
78 {
79     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_SHOW_INPUT));
80 }
81 
RequestHideInput()82 int32_t InputMethodSystemAbilityProxy::RequestHideInput()
83 {
84     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_HIDE_INPUT));
85 }
86 
DisplayOptionalInputMethod()87 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod()
88 {
89     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::DISPLAY_OPTIONAL_INPUT_METHOD));
90 }
91 
SetCoreAndAgent(const sptr<IInputMethodCore> & core,const sptr<IInputMethodAgent> & agent)92 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(
93     const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent)
94 {
95     return SendRequest(
96         static_cast<uint32_t>(InputMethodInterfaceCode::SET_CORE_AND_AGENT), [core, agent](MessageParcel &data) {
97             return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent->AsObject());
98         });
99 }
100 
GetDefaultInputMethod(std::shared_ptr<Property> & property)101 int32_t InputMethodSystemAbilityProxy::GetDefaultInputMethod(std::shared_ptr<Property> &property)
102 {
103     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_DEFAULT_INPUT_METHOD), nullptr,
104         [&property](MessageParcel& reply) {
105             property = std::make_shared<Property>();
106             return ITypesUtil::Unmarshal(reply, *property);
107         });
108 }
109 
GetInputMethodConfig(OHOS::AppExecFwk::ElementName & inputMethodConfig)110 int32_t InputMethodSystemAbilityProxy::GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig)
111 {
112     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_INPUT_METHOD_SETTINGS), nullptr,
113         [&inputMethodConfig](MessageParcel& reply) {
114             return ITypesUtil::Unmarshal(reply, inputMethodConfig);
115         });
116 }
117 
GetSecurityMode(int32_t & security)118 int32_t InputMethodSystemAbilityProxy::GetSecurityMode(int32_t &security)
119 {
120     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_SECURITY_MODE), nullptr,
121         [&security](MessageParcel& reply) {
122             return ITypesUtil::Unmarshal(reply, security);
123         });
124 }
125 
UnRegisteredProxyIme(UnRegisteredType type,const sptr<IInputMethodCore> & core)126 int32_t InputMethodSystemAbilityProxy::UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core)
127 {
128     return SendRequest(
129         static_cast<uint32_t>(InputMethodInterfaceCode::UNREGISTERED_PROXY_IME), [&type, &core](MessageParcel &data) {
130             return ITypesUtil::Marshal(data, static_cast<int32_t>(type), core->AsObject());
131         });
132 }
133 
GetCurrentInputMethod()134 std::shared_ptr<Property> InputMethodSystemAbilityProxy::GetCurrentInputMethod()
135 {
136     std::shared_ptr<Property> property = nullptr;
137     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD), nullptr,
138         [&property](MessageParcel &reply) {
139             property = std::make_shared<Property>();
140             return ITypesUtil::Unmarshal(reply, *property);
141         });
142     return ret != ErrorCode::NO_ERROR ? nullptr : property;
143 }
144 
GetCurrentInputMethodSubtype()145 std::shared_ptr<SubProperty> InputMethodSystemAbilityProxy::GetCurrentInputMethodSubtype()
146 {
147     std::shared_ptr<SubProperty> property = nullptr;
148     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD_SUBTYPE),
149         nullptr, [&property](MessageParcel &reply) {
150             property = std::make_shared<SubProperty>();
151             return ITypesUtil::Unmarshal(reply, *property);
152         });
153     return ret != ErrorCode::NO_ERROR ? nullptr : property;
154 }
155 
ListInputMethod(InputMethodStatus status,std::vector<Property> & props)156 int32_t InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status, std::vector<Property> &props)
157 {
158     return SendRequest(
159         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD),
160         [status](MessageParcel &data) { return ITypesUtil::Marshal(data, uint32_t(status)); },
161         [&props](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, props); });
162 }
163 
ShowCurrentInputDeprecated()164 int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated()
165 {
166     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT_DEPRECATED));
167 }
168 
HideCurrentInputDeprecated()169 int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated()
170 {
171     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT_DEPRECATED));
172 }
173 
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)174 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
175     const std::string &name, std::vector<SubProperty> &subProps)
176 {
177     return SendRequest(
178         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD_SUBTYPE),
179         [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
180         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
181 }
182 
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)183 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
184 {
185     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::LIST_CURRENT_INPUT_METHOD_SUBTYPE), nullptr,
186         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
187 }
188 
SwitchInputMethod(const std::string & name,const std::string & subName,SwitchTrigger trigger)189 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(
190     const std::string &name, const std::string &subName, SwitchTrigger trigger)
191 {
192     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SWITCH_INPUT_METHOD),
193         [&name, &subName, trigger](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName, trigger); });
194 }
195 
PanelStatusChange(const InputWindowStatus & status,const InputWindowInfo & windowInfo)196 int32_t InputMethodSystemAbilityProxy::PanelStatusChange(
197     const InputWindowStatus &status, const InputWindowInfo &windowInfo)
198 {
199     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE),
200         [status, windowInfo](
201             MessageParcel &data) { return ITypesUtil::Marshal(data, static_cast<uint32_t>(status), windowInfo); });
202 }
203 
UpdateListenEventFlag(InputClientInfo & clientInfo,EventType eventType)204 int32_t InputMethodSystemAbilityProxy::UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType)
205 {
206     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::UPDATE_LISTEN_EVENT_FLAG),
207         [&clientInfo, eventType](MessageParcel &data) { return ITypesUtil::Marshal(data, clientInfo, eventType); });
208 }
209 
IsCurrentIme()210 bool InputMethodSystemAbilityProxy::IsCurrentIme()
211 {
212     bool isCurrentIme = false;
213     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME), nullptr,
214         [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
215     return isCurrentIme;
216 }
217 
IsInputTypeSupported(InputType type)218 bool InputMethodSystemAbilityProxy::IsInputTypeSupported(InputType type)
219 {
220     bool isSupported = false;
221     SendRequest(
222         static_cast<uint32_t>(InputMethodInterfaceCode::IS_INPUT_TYPE_SUPPORTED),
223         [type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); },
224         [&isSupported](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isSupported); });
225     return isSupported;
226 }
227 
StartInputType(InputType type)228 int32_t InputMethodSystemAbilityProxy::StartInputType(InputType type)
229 {
230     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT_TYPE),
231         [&type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); });
232 }
233 
ExitCurrentInputType()234 int32_t InputMethodSystemAbilityProxy::ExitCurrentInputType()
235 {
236     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::EXIT_CURRENT_INPUT_TYPE));
237 }
238 
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)239 int32_t InputMethodSystemAbilityProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
240 {
241     return SendRequest(
242         static_cast<uint32_t>(InputMethodInterfaceCode::IS_PANEL_SHOWN),
243         [&panelInfo](MessageParcel &data) { return ITypesUtil::Marshal(data, panelInfo); },
244         [&isShown](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isShown); });
245 }
246 
GetMessageOption(int32_t code,MessageOption & option)247 void InputMethodSystemAbilityProxy::GetMessageOption(int32_t code, MessageOption &option)
248 {
249     switch (code) {
250         case static_cast<int32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE): {
251             IMSA_HILOGD("Async IPC.");
252             option.SetFlags(MessageOption::TF_ASYNC);
253             break;
254         }
255         default:
256             option.SetFlags(MessageOption::TF_SYNC);
257             break;
258     }
259 }
260 
SendRequest(int code,ParcelHandler input,ParcelHandler output)261 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
262 {
263     IMSA_HILOGI("IMSAProxy, code = %{public}d", code);
264     MessageParcel data;
265     MessageParcel reply;
266     MessageOption option;
267     GetMessageOption(code, option);
268 
269     if (!data.WriteInterfaceToken(GetDescriptor())) {
270         IMSA_HILOGE("write interface token failed");
271         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
272     }
273     if (input != nullptr && (!input(data))) {
274         IMSA_HILOGE("write data failed");
275         return ErrorCode::ERROR_EX_PARCELABLE;
276     }
277     auto ret = Remote()->SendRequest(code, data, reply, option);
278     if (ret != NO_ERROR) {
279         IMSA_HILOGE("transport exceptions, code: %{public}d, ret %{public}d", code, ret);
280         return ret;
281     }
282     if (option.GetFlags() == MessageOption::TF_ASYNC) {
283         return ErrorCode::NO_ERROR;
284     }
285     ret = reply.ReadInt32();
286     if (ret != NO_ERROR) {
287         IMSA_HILOGE("dispose failed in service, code: %{public}d, ret: %{public}d", code, ret);
288         return ret;
289     }
290     if (output != nullptr && (!output(reply))) {
291         IMSA_HILOGE("reply parcel error");
292         return ErrorCode::ERROR_EX_PARCELABLE;
293     }
294     return ErrorCode::NO_ERROR;
295 }
296 } // namespace MiscServices
297 } // namespace OHOS
298