• 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 
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)161 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
162     const std::string &name, std::vector<SubProperty> &subProps)
163 {
164     IMSA_HILOGD("InputMethodSystemAbilityProxy::ListInputMethodSubtype");
165     int32_t ret = SendRequest(
166         LIST_INPUT_METHOD_SUBTYPE, [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
167         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
168     if (ret != ErrorCode::NO_ERROR) {
169         IMSA_HILOGE("InputMethodSystemAbilityProxy::SendRequest failed, ret %{public}d", ret);
170     }
171     return ret;
172 }
173 
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)174 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
175 {
176     IMSA_HILOGD("InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype");
177     int32_t ret = SendRequest(LIST_CURRENT_INPUT_METHOD_SUBTYPE, nullptr,
178         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
179     if (ret != ErrorCode::NO_ERROR) {
180         IMSA_HILOGE("InputMethodSystemAbilityProxy::SendRequest failed, ret %{public}d", ret);
181     }
182     return ret;
183 }
184 
SwitchInputMethod(const std::string & name,const std::string & subName)185 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(const std::string &name, const std::string &subName)
186 {
187     IMSA_HILOGD("InputMethodSystemAbilityProxy::SwitchInputMethod");
188     return SendRequest(SWITCH_INPUT_METHOD,
189         [&name, &subName](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName); });
190 }
191 
SendRequest(int code,ParcelHandler input,ParcelHandler output)192 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
193 {
194     IMSA_HILOGD("%{public}s in", __func__);
195     MessageParcel data;
196     MessageParcel reply;
197     MessageOption option{ MessageOption::TF_SYNC };
198     if (!data.WriteInterfaceToken(GetDescriptor())) {
199         IMSA_HILOGE("write interface token failed");
200         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
201     }
202     if (input != nullptr && (!input(data))) {
203         IMSA_HILOGE("write data failed");
204         return ErrorCode::ERROR_EX_PARCELABLE;
205     }
206     auto ret = Remote()->SendRequest(code, data, reply, option);
207     if (ret != NO_ERROR) {
208         IMSA_HILOGE("SendRequest failed, ret %{public}d", ret);
209         return ret;
210     }
211     ret = reply.ReadInt32();
212     if (ret != NO_ERROR) {
213         IMSA_HILOGE("reply error, ret %{public}d", ret);
214         return ret;
215     }
216     if (output != nullptr && (!output(reply))) {
217         IMSA_HILOGE("reply parcel error");
218         return ErrorCode::ERROR_EX_PARCELABLE;
219     }
220     return ret;
221 }
222 } // namespace MiscServices
223 } // namespace OHOS
224