• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2025 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_data_channel_service_impl.h"
17 
18 #include "global.h"
19 #include "input_method_controller.h"
20 #include "ipc_object_stub.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "itypes_util.h"
24 #include "message.h"
25 #include "input_method_tools.h"
26 namespace OHOS {
27 namespace MiscServices {
InputDataChannelServiceImpl()28 InputDataChannelServiceImpl::InputDataChannelServiceImpl() {}
29 
~InputDataChannelServiceImpl()30 InputDataChannelServiceImpl::~InputDataChannelServiceImpl() {}
31 // LCOV_EXCL_START
InsertText(const std::string & text,uint64_t msgId,const sptr<IRemoteObject> & agent)32 ErrCode InputDataChannelServiceImpl::InsertText(
33     const std::string &text, uint64_t msgId, const sptr<IRemoteObject> &agent)
34 {
35     IMSA_HILOGD("start.");
36     auto instance = InputMethodController::GetInstance();
37     if (instance == nullptr) {
38         IMSA_HILOGE("failed to get InputMethodController instance!");
39         return ErrorCode::ERROR_EX_NULL_POINTER;
40     }
41     int32_t ret = instance->InsertText(Str8ToStr16(text));
42     ResponseData data = std::monostate{};
43     instance->ResponseDataChannel(agent, msgId, ret, data);
44     IMSA_HILOGD("end.");
45     return ret;
46 }
47 
DeleteForward(int32_t length,uint64_t msgId,const sptr<IRemoteObject> & agent)48 ErrCode InputDataChannelServiceImpl::DeleteForward(int32_t length, uint64_t msgId, const sptr<IRemoteObject> &agent)
49 {
50     auto instance = InputMethodController::GetInstance();
51     if (instance == nullptr) {
52         IMSA_HILOGE("failed to get InputMethodController instance!");
53         return ErrorCode::ERROR_EX_NULL_POINTER;
54     }
55     int32_t ret = instance->DeleteForward(length);
56     ResponseData data = std::monostate{};
57     instance->ResponseDataChannel(agent, msgId, ret, data);
58     return ret;
59 }
60 
DeleteBackward(int32_t length,uint64_t msgId,const sptr<IRemoteObject> & agent)61 ErrCode InputDataChannelServiceImpl::DeleteBackward(int32_t length, uint64_t msgId, const sptr<IRemoteObject> &agent)
62 {
63     auto instance = InputMethodController::GetInstance();
64     if (instance == nullptr) {
65         IMSA_HILOGE("failed to get InputMethodController instance!");
66         return ErrorCode::ERROR_EX_NULL_POINTER;
67     }
68     int32_t ret = instance->DeleteBackward(length);
69     ResponseData data = std::monostate{};
70     instance->ResponseDataChannel(agent, msgId, ret, data);
71     return ret;
72 }
73 
GetTextBeforeCursor(int32_t number,uint64_t msgId,const sptr<IRemoteObject> & agent)74 ErrCode InputDataChannelServiceImpl::GetTextBeforeCursor(
75     int32_t number, uint64_t msgId, const sptr<IRemoteObject> &agent)
76 {
77     auto instance = InputMethodController::GetInstance();
78     if (instance == nullptr) {
79         IMSA_HILOGE("failed to get InputMethodController instance!");
80         return ErrorCode::ERROR_EX_NULL_POINTER;
81     }
82     std::u16string text;
83     int32_t ret = instance->GetLeft(number, text);
84     ResponseData data = Str16ToStr8(text);
85     instance->ResponseDataChannel(agent, msgId, ret, data);
86     return ret;
87 }
88 
GetTextAfterCursor(int32_t number,uint64_t msgId,const sptr<IRemoteObject> & agent)89 ErrCode InputDataChannelServiceImpl::GetTextAfterCursor(
90     int32_t number, uint64_t msgId, const sptr<IRemoteObject> &agent)
91 {
92     auto instance = InputMethodController::GetInstance();
93     if (instance == nullptr) {
94         IMSA_HILOGE("failed to get InputMethodController instance!");
95         return ErrorCode::ERROR_EX_NULL_POINTER;
96     }
97     std::u16string text;
98     auto ret = instance->GetRight(number, text);
99     ResponseData data = Str16ToStr8(text);
100     instance->ResponseDataChannel(agent, msgId, ret, data);
101     return ret;
102 }
103 
GetTextIndexAtCursor(uint64_t msgId,const sptr<IRemoteObject> & agent)104 ErrCode InputDataChannelServiceImpl::GetTextIndexAtCursor(uint64_t msgId, const sptr<IRemoteObject> &agent)
105 {
106     int32_t index = 0;
107     auto instance = InputMethodController::GetInstance();
108     if (instance == nullptr) {
109         IMSA_HILOGE("failed to get InputMethodController instance!");
110         return ErrorCode::ERROR_EX_NULL_POINTER;
111     }
112     auto ret = instance->GetTextIndexAtCursor(index);
113     ResponseData data = index;
114     instance->ResponseDataChannel(agent, msgId, ret, data);
115     return ret;
116 }
117 // LCOV_EXCL_STOP
GetEnterKeyType(int32_t & keyType)118 ErrCode InputDataChannelServiceImpl::GetEnterKeyType(int32_t &keyType)
119 {
120     auto instance = InputMethodController::GetInstance();
121     if (instance == nullptr) {
122         IMSA_HILOGE("failed to get InputMethodController instance!");
123         return ErrorCode::ERROR_EX_NULL_POINTER;
124     }
125     return instance->GetEnterKeyType(keyType);
126 }
127 
GetInputPattern(int32_t & inputPattern)128 ErrCode InputDataChannelServiceImpl::GetInputPattern(int32_t &inputPattern)
129 {
130     auto instance = InputMethodController::GetInstance();
131     if (instance == nullptr) {
132         IMSA_HILOGE("failed to get InputMethodController instance!");
133         return ErrorCode::ERROR_EX_NULL_POINTER;
134     }
135     return instance->GetInputPattern(inputPattern);
136 }
137 
GetTextConfig(TextTotalConfigInner & textConfigInner)138 ErrCode InputDataChannelServiceImpl::GetTextConfig(TextTotalConfigInner &textConfigInner)
139 {
140     TextTotalConfig textConfig = InputMethodTools::GetInstance().InnerToTextTotalConfig(textConfigInner);
141 
142     auto instance = InputMethodController::GetInstance();
143     if (instance == nullptr) {
144         IMSA_HILOGE("failed to get InputMethodController instance!");
145         return ErrorCode::ERROR_EX_NULL_POINTER;
146     }
147     auto ret = instance->GetTextConfig(textConfig);
148     textConfigInner = InputMethodTools::GetInstance().TextTotalConfigToInner(textConfig);
149     return ret;
150 }
151 
SendKeyboardStatus(int32_t status)152 ErrCode InputDataChannelServiceImpl::SendKeyboardStatus(int32_t status)
153 {
154     auto instance = InputMethodController::GetInstance();
155     if (instance != nullptr) {
156         instance->SendKeyboardStatus(static_cast<KeyboardStatus>(status));
157     } else {
158         IMSA_HILOGE("failed to get InputMethodController instance!");
159     }
160     return ERR_OK;
161 }
162 // LCOV_EXCL_START
SendFunctionKey(int32_t funcKey,uint64_t msgId,const sptr<IRemoteObject> & agent)163 ErrCode InputDataChannelServiceImpl::SendFunctionKey(int32_t funcKey, uint64_t msgId, const sptr<IRemoteObject> &agent)
164 {
165     auto instance = InputMethodController::GetInstance();
166     if (instance == nullptr) {
167         IMSA_HILOGE("failed to get InputMethodController instance!");
168         return ErrorCode::ERROR_EX_NULL_POINTER;
169     }
170     auto ret = instance->SendFunctionKey(funcKey);
171     ResponseData data = std::monostate{};
172     instance->ResponseDataChannel(agent, msgId, ret, data);
173     return ret;
174 }
175 
MoveCursor(int32_t keyCode,uint64_t msgId,const sptr<IRemoteObject> & agent)176 ErrCode InputDataChannelServiceImpl::MoveCursor(int32_t keyCode, uint64_t msgId, const sptr<IRemoteObject> &agent)
177 {
178     auto instance = InputMethodController::GetInstance();
179     if (instance == nullptr) {
180         IMSA_HILOGE("failed to get InputMethodController instance!");
181         return ErrorCode::ERROR_EX_NULL_POINTER;
182     }
183     auto ret = instance->MoveCursor(static_cast<Direction>(keyCode));
184     ResponseData data = std::monostate{};
185     instance->ResponseDataChannel(agent, msgId, ret, data);
186     return ret;
187 }
188 
SelectByRange(int32_t start,int32_t end,uint64_t msgId,const sptr<IRemoteObject> & agent)189 ErrCode InputDataChannelServiceImpl::SelectByRange(
190     int32_t start, int32_t end, uint64_t msgId, const sptr<IRemoteObject> &agent)
191 {
192     auto instance = InputMethodController::GetInstance();
193     if (instance == nullptr) {
194         IMSA_HILOGE("failed to get InputMethodController instance!");
195         return ErrorCode::ERROR_EX_NULL_POINTER;
196     }
197 
198     instance->SelectByRange(start, end);
199     ResponseData data = std::monostate{};
200     instance->ResponseDataChannel(agent, msgId, ERR_OK, data);
201     return ERR_OK;
202 }
203 
SelectByMovement(int32_t direction,int32_t cursorMoveSkip,uint64_t msgId,const sptr<IRemoteObject> & agent)204 ErrCode InputDataChannelServiceImpl::SelectByMovement(
205     int32_t direction, int32_t cursorMoveSkip, uint64_t msgId, const sptr<IRemoteObject> &agent)
206 {
207     auto instance = InputMethodController::GetInstance();
208     if (instance == nullptr) {
209         IMSA_HILOGW("failed to get InputMethodController instance!");
210         return ErrorCode::ERROR_EX_NULL_POINTER;
211     }
212     instance->SelectByMovement(direction, cursorMoveSkip);
213     ResponseData data = std::monostate{};
214     instance->ResponseDataChannel(agent, msgId, ERR_OK, data);
215     return ERR_OK;
216 }
217 
HandleExtendAction(int32_t action,uint64_t msgId,const sptr<IRemoteObject> & agent)218 ErrCode InputDataChannelServiceImpl::HandleExtendAction(
219     int32_t action, uint64_t msgId, const sptr<IRemoteObject> &agent)
220 {
221     auto instance = InputMethodController::GetInstance();
222     if (instance == nullptr) {
223         IMSA_HILOGE("failed to get InputMethodController instance!");
224         return ErrorCode::ERROR_EX_NULL_POINTER;
225     }
226     auto ret = instance->HandleExtendAction(action);
227     ResponseData data = std::monostate{};
228     instance->ResponseDataChannel(agent, msgId, ret, data);
229     return ret;
230 }
231 // LCOV_EXCL_STOP
NotifyPanelStatusInfo(const PanelStatusInfoInner & info)232 ErrCode InputDataChannelServiceImpl::NotifyPanelStatusInfo(const PanelStatusInfoInner &info)
233 {
234     PanelStatusInfo panelStatusInfo = InputMethodTools::GetInstance().InnerToPanelStatusInfo(info);
235     auto instance = InputMethodController::GetInstance();
236     if (instance != nullptr) {
237         instance->NotifyPanelStatusInfo(panelStatusInfo);
238     } else {
239         IMSA_HILOGW("failed to get InputMethodController instance!");
240     }
241     return ERR_OK;
242 }
243 
NotifyKeyboardHeight(uint32_t height)244 ErrCode InputDataChannelServiceImpl::NotifyKeyboardHeight(uint32_t height)
245 {
246     auto instance = InputMethodController::GetInstance();
247     if (instance != nullptr) {
248         instance->NotifyKeyboardHeight(height);
249     } else {
250         IMSA_HILOGW("failed to get InputMethodController instance!");
251     }
252     return ERR_OK;
253 }
254 
SendPrivateCommand(const Value & value)255 ErrCode InputDataChannelServiceImpl::SendPrivateCommand(const Value &value)
256 {
257     std::unordered_map<std::string, PrivateDataValue> privateCommand;
258     privateCommand = value.valueMap;
259     auto instance = InputMethodController::GetInstance();
260     if (instance == nullptr) {
261         IMSA_HILOGE("failed to get InputMethodController instance!");
262         return ErrorCode::ERROR_EX_NULL_POINTER;
263     }
264     return instance->ReceivePrivateCommand(privateCommand);
265 }
266 // LCOV_EXCL_START
SetPreviewText(const std::string & text,const RangeInner & rangeInner,uint64_t msgId,const sptr<IRemoteObject> & agent)267 ErrCode InputDataChannelServiceImpl::SetPreviewText(
268     const std::string &text, const RangeInner &rangeInner, uint64_t msgId, const sptr<IRemoteObject> &agent)
269 {
270     Range range = InputMethodTools::GetInstance().InnerToRange(rangeInner);
271     auto instance = InputMethodController::GetInstance();
272     if (instance == nullptr) {
273         IMSA_HILOGE("failed to get InputMethodController instance!");
274         return ErrorCode::ERROR_EX_NULL_POINTER;
275     }
276     auto ret = instance->SetPreviewText(text, range);
277     ResponseData data = std::monostate{};
278     instance->ResponseDataChannel(agent, msgId, ret, data);
279     return ret;
280 }
281 
FinishTextPreview(uint64_t msgId,const sptr<IRemoteObject> & agent)282 ErrCode InputDataChannelServiceImpl::FinishTextPreview(uint64_t msgId, const sptr<IRemoteObject> &agent)
283 {
284     auto instance = InputMethodController::GetInstance();
285     if (instance == nullptr) {
286         IMSA_HILOGE("failed to get InputMethodController instance!");
287         return ErrorCode::ERROR_EX_NULL_POINTER;
288     }
289     auto ret = instance->FinishTextPreview();
290     ResponseData data = std::monostate{};
291     instance->ResponseDataChannel(agent, msgId, ret, data);
292     return ret;
293 }
294 // LCOV_EXCL_STOP
SendMessage(const ArrayBuffer & arraybuffer)295 ErrCode InputDataChannelServiceImpl::SendMessage(const ArrayBuffer &arraybuffer)
296 {
297     auto instance = InputMethodController::GetInstance();
298     if (instance == nullptr) {
299         IMSA_HILOGE("failed to get InputMethodController instance!");
300         return ErrorCode::ERROR_EX_NULL_POINTER;
301     }
302     return instance->RecvMessage(arraybuffer);
303 }
304 
HandleKeyEventResult(uint64_t cbId,bool consumeResult)305 ErrCode InputDataChannelServiceImpl::HandleKeyEventResult(uint64_t cbId, bool consumeResult)
306 {
307     auto instance = InputMethodController::GetInstance();
308     if (instance == nullptr) {
309         IMSA_HILOGE("failed to get InputMethodController instance!");
310         return ErrorCode::ERROR_EX_NULL_POINTER;
311     }
312     instance->HandleKeyEventResult(cbId, consumeResult);
313     return ERR_OK;
314 }
315 } // namespace MiscServices
316 } // namespace OHOS