• 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 "global.h"
19 #include "itypes_util.h"
20 #include "message_option.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace ErrorCode;
25 
InputMethodSystemAbilityProxy(const sptr<IRemoteObject> & object)26 InputMethodSystemAbilityProxy::InputMethodSystemAbilityProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<IInputMethodSystemAbility>(object)
28 {
29 }
30 
PrepareInput(int32_t displayId,sptr<IInputClient> client,sptr<IInputDataChannel> channel,InputAttribute & attribute)31 int32_t InputMethodSystemAbilityProxy::PrepareInput(int32_t displayId, sptr<IInputClient> client,
32     sptr<IInputDataChannel> channel, InputAttribute &attribute)
33 {
34     return SendRequest(PREPARE_INPUT, [displayId, client, channel, &attribute](MessageParcel &data) {
35         return data.WriteInt32(displayId) && data.WriteRemoteObject(client->AsObject()) &&
36                data.WriteRemoteObject(channel->AsObject()) && InputAttribute::Marshalling(attribute, data);
37     });
38 }
39 
StartInput(sptr<IInputClient> client,bool isShowKeyboard)40 int32_t InputMethodSystemAbilityProxy::StartInput(sptr<IInputClient> client, bool isShowKeyboard)
41 {
42     IMSA_HILOGD("%{public}s in", __func__);
43     return SendRequest(START_INPUT, [isShowKeyboard, client](MessageParcel &data) {
44         return data.WriteRemoteObject(client->AsObject()) && data.WriteBool(isShowKeyboard);
45     });
46 }
47 
ShowCurrentInput()48 int32_t InputMethodSystemAbilityProxy::ShowCurrentInput()
49 {
50     IMSA_HILOGD("%{public}s in", __func__);
51     return SendRequest(SHOW_CURRENT_INPUT);
52 }
53 
HideCurrentInput()54 int32_t InputMethodSystemAbilityProxy::HideCurrentInput()
55 {
56     IMSA_HILOGD("%{public}s in", __func__);
57     return SendRequest(HIDE_CURRENT_INPUT);
58 }
59 
StopInputSession()60 int32_t InputMethodSystemAbilityProxy::StopInputSession()
61 {
62     IMSA_HILOGD("%{public}s in", __func__);
63     return SendRequest(STOP_INPUT_SESSION);
64 }
65 
StopInput(sptr<IInputClient> client)66 int32_t InputMethodSystemAbilityProxy::StopInput(sptr<IInputClient> client)
67 {
68     IMSA_HILOGD("%{public}s in", __func__);
69     return SendRequest(STOP_INPUT,
70         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
71 }
72 
ReleaseInput(sptr<IInputClient> client)73 int32_t InputMethodSystemAbilityProxy::ReleaseInput(sptr<IInputClient> client)
74 {
75     return SendRequest(RELEASE_INPUT,
76         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
77 }
78 
DisplayOptionalInputMethod()79 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod()
80 {
81     IMSA_HILOGI("%{public}s in", __func__);
82     return SendRequest(DISPLAY_OPTIONAL_INPUT_METHOD);
83 }
84 
SetCoreAndAgent(sptr<IInputMethodCore> core,sptr<IInputMethodAgent> agent)85 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent)
86 {
87     IMSA_HILOGD("%{public}s in", __func__);
88     return SendRequest(SET_CORE_AND_AGENT, [core, agent](MessageParcel &data) {
89         return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent->AsObject());
90     });
91 }
92 
GetCurrentInputMethod()93 std::shared_ptr<Property> InputMethodSystemAbilityProxy::GetCurrentInputMethod()
94 {
95     IMSA_HILOGD("%{public}s in", __func__);
96     std::shared_ptr<Property> property = nullptr;
97     int32_t ret = SendRequest(GET_CURRENT_INPUT_METHOD, nullptr, [&property](MessageParcel &reply) {
98         property = std::make_shared<Property>();
99         if (property == nullptr) {
100             IMSA_HILOGE("%{public}s make_shared nullptr", __func__);
101             return false;
102         }
103         return ITypesUtil::Unmarshal(reply, *property);
104     });
105     if (ret != ErrorCode::NO_ERROR) {
106         IMSA_HILOGE("%{public}s SendRequest failed, ret %{public}d", __func__, ret);
107         return nullptr;
108     }
109     return property;
110 }
111 
GetCurrentInputMethodSubtype()112 std::shared_ptr<SubProperty> InputMethodSystemAbilityProxy::GetCurrentInputMethodSubtype()
113 {
114     IMSA_HILOGD("%{public}s in", __func__);
115     std::shared_ptr<SubProperty> property = nullptr;
116     int32_t ret = SendRequest(GET_CURRENT_INPUT_METHOD_SUBTYPE, nullptr, [&property](MessageParcel &reply) {
117         property = std::make_shared<SubProperty>();
118         if (property == nullptr) {
119             IMSA_HILOGE("%{public}s make_shared nullptr", __func__);
120             return false;
121         }
122         return ITypesUtil::Unmarshal(reply, *property);
123     });
124     if (ret != ErrorCode::NO_ERROR) {
125         IMSA_HILOGE("%{public}s SendRequest failed, ret %{public}d", __func__, ret);
126         return nullptr;
127     }
128     return property;
129 }
130 
ListInputMethod(InputMethodStatus status,std::vector<Property> & props)131 int32_t InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status, std::vector<Property> &props)
132 {
133     IMSA_HILOGD("%{public}s in", __func__);
134     int32_t ret = SendRequest(
135         LIST_INPUT_METHOD, [status](MessageParcel &data) { return ITypesUtil::Marshal(data, uint32_t(status)); },
136         [&props](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, props); });
137     if (ret != ErrorCode::NO_ERROR) {
138         IMSA_HILOGE("InputMethodSystemAbilityProxy::SendRequest failed, ret %{public}d", ret);
139     }
140     return ret;
141 }
142 
ShowCurrentInputDeprecated()143 int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated()
144 {
145     IMSA_HILOGD("%{public}s in", __func__);
146     return SendRequest(SHOW_CURRENT_INPUT_DEPRECATED);
147 }
148 
HideCurrentInputDeprecated()149 int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated()
150 {
151     IMSA_HILOGD("%{public}s in", __func__);
152     return SendRequest(HIDE_CURRENT_INPUT_DEPRECATED);
153 }
154 
DisplayOptionalInputMethodDeprecated()155 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethodDeprecated()
156 {
157     IMSA_HILOGD("%{public}s in", __func__);
158     return SendRequest(DISPLAY_OPTIONAL_INPUT_DEPRECATED);
159 }
160 
SetCoreAndAgentDeprecated(sptr<IInputMethodCore> core,sptr<IInputMethodAgent> agent)161 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgentDeprecated(sptr<IInputMethodCore> core,
162     sptr<IInputMethodAgent> agent)
163 {
164     IMSA_HILOGD("%{public}s in", __func__);
165     return SendRequest(SET_CORE_AND_AGENT_DEPRECATED, [core, agent](MessageParcel &data) {
166         return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent->AsObject());
167     });
168 }
169 
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)170 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
171     const std::string &name, std::vector<SubProperty> &subProps)
172 {
173     IMSA_HILOGD("InputMethodSystemAbilityProxy::ListInputMethodSubtype");
174     int32_t ret = SendRequest(
175         LIST_INPUT_METHOD_SUBTYPE, [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
176         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
177     if (ret != ErrorCode::NO_ERROR) {
178         IMSA_HILOGE("InputMethodSystemAbilityProxy::SendRequest failed, ret %{public}d", ret);
179     }
180     return ret;
181 }
182 
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)183 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
184 {
185     IMSA_HILOGD("InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype");
186     int32_t ret = SendRequest(LIST_CURRENT_INPUT_METHOD_SUBTYPE, nullptr,
187         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
188     if (ret != ErrorCode::NO_ERROR) {
189         IMSA_HILOGE("InputMethodSystemAbilityProxy::SendRequest failed, ret %{public}d", ret);
190     }
191     return ret;
192 }
193 
SwitchInputMethod(const std::string & name,const std::string & subName)194 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(const std::string &name, const std::string &subName)
195 {
196     IMSA_HILOGD("InputMethodSystemAbilityProxy::SwitchInputMethod");
197     return SendRequest(SWITCH_INPUT_METHOD,
198         [&name, &subName](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName); });
199 }
200 
SendRequest(int code,ParcelHandler input,ParcelHandler output)201 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
202 {
203     IMSA_HILOGD("%{public}s in", __func__);
204     MessageParcel data;
205     MessageParcel reply;
206     MessageOption option{ MessageOption::TF_SYNC };
207     if (!data.WriteInterfaceToken(GetDescriptor())) {
208         IMSA_HILOGE("write interface token failed");
209         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
210     }
211     if (input != nullptr && (!input(data))) {
212         IMSA_HILOGE("write data failed");
213         return ErrorCode::ERROR_EX_PARCELABLE;
214     }
215     auto ret = Remote()->SendRequest(code, data, reply, option);
216     if (ret != NO_ERROR) {
217         IMSA_HILOGE("SendRequest failed, ret %{public}d", ret);
218         return ret;
219     }
220     ret = reply.ReadInt32();
221     if (ret != NO_ERROR) {
222         IMSA_HILOGE("reply error, ret %{public}d", ret);
223         return ret;
224     }
225     if (output != nullptr && (!output(reply))) {
226         IMSA_HILOGE("reply parcel error");
227         return ErrorCode::ERROR_EX_PARCELABLE;
228     }
229     return ret;
230 }
231 } // namespace MiscServices
232 } // namespace OHOS
233