1 /*
2  * Copyright (C) 2021-2023 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 #ifndef FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H
17 #define FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <ctime>
22 #include <condition_variable>
23 #include <mutex>
24 #include <thread>
25 #include <variant>
26 
27 #include "block_queue.h"
28 #include "controller_listener.h"
29 #include "element_name.h"
30 #include "event_handler.h"
31 #include "global.h"
32 #include "i_input_method_agent.h"
33 #include "i_input_method_system_ability.h"
34 #include "ime_event_listener.h"
35 #include "input_client_info.h"
36 #include "input_method_property.h"
37 #include "input_method_status.h"
38 #include "input_method_utils.h"
39 #include "ipc_skeleton.h"
40 #include "iremote_object.h"
41 #include "key_event.h"
42 #include "message_handler.h"
43 #include "panel_info.h"
44 #include "private_command_interface.h"
45 #include "visibility.h"
46 
47 namespace OHOS {
48 namespace MiscServices {
49 class OnTextChangedListener : public virtual RefBase {
50 public:
~OnTextChangedListener()51     virtual ~OnTextChangedListener() {}
52     virtual void InsertText(const std::u16string &text) = 0;
53     virtual void DeleteForward(int32_t length) = 0;
54     virtual void DeleteBackward(int32_t length) = 0;
55     virtual void SendKeyEventFromInputMethod(const KeyEvent &event) = 0;
56     virtual void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) = 0;
NotifyPanelStatusInfo(const PanelStatusInfo & info)57     virtual void NotifyPanelStatusInfo(const PanelStatusInfo &info)
58     {
59     }
NotifyKeyboardHeight(uint32_t height)60     virtual void NotifyKeyboardHeight(uint32_t height)
61     {
62     }
63     virtual void SendFunctionKey(const FunctionKey &functionKey) = 0;
64     virtual void SetKeyboardStatus(bool status) = 0;
65     virtual void MoveCursor(const Direction direction) = 0;
66     virtual void HandleSetSelection(int32_t start, int32_t end) = 0;
67     virtual void HandleExtendAction(int32_t action) = 0;
68     virtual void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) = 0;
69     virtual std::u16string GetLeftTextOfCursor(int32_t number) = 0;
70     virtual std::u16string GetRightTextOfCursor(int32_t number) = 0;
71     virtual int32_t GetTextIndexAtCursor() = 0;
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)72     virtual int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
73     {
74         return ErrorCode::NO_ERROR;
75     }
76     /**
77      * @brief Set preview text.
78      *
79      * When InputMethod app sends request to set preview text, the function will be called.
80      *
81      * @param text Indicates the text to be previewed.
82      * @param range Indicates the range of text to be replaced.
83      * @return
84      *     If success, please return 0.
85      *     If parameter range check error, please return -1.
86      *     If other failure, no specific requirement.
87      * @since 12
88      */
SetPreviewText(const std::u16string & text,const Range & range)89     virtual int32_t SetPreviewText(const std::u16string &text, const Range &range)
90     {
91         return ErrorCode::NO_ERROR;
92     }
93     /**
94      * @brief Finish text preview.
95      *
96      * When InputMethod app sends request to finish text preview, the function will be called.
97      *
98      * @since 12
99      */
FinishTextPreview()100     virtual void FinishTextPreview()
101     {
102     }
OnDetach()103     virtual void OnDetach()
104     {
105     }
106     /**
107      * @brief Is listener from ts registration.
108      *
109      * If you use C/C++ interface, ignore this.
110      *
111      * @since 12
112      */
IsFromTs()113     virtual bool IsFromTs()
114     {
115         return false;
116     }
117 };
118 using PrivateDataValue = std::variant<std::string, bool, int32_t>;
119 using KeyEventCallback = std::function<void(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed)>;
120 class InputMethodController : public RefBase, public PrivateCommandInterface {
121 public:
122     /**
123      * @brief Get the instance of InputMethodController.
124      *
125      * This function is used to get the instance of InputMethodController.
126      *
127      * @return The instance of InputMethodController.
128      * @since 6
129      */
130     IMF_API static sptr<InputMethodController> GetInstance();
131 
132     /**
133      * @brief Show soft keyboard, set listener and bind IMSA with default states and attribute.
134      *
135      * This function is used to show soft keyboard,  set listener and bind IMSA,
136      * default state is 'true', default attribute is 'InputAttribute::PATTERN_TEXT'.
137      *
138      * @param listener Indicates the listener in order to manipulate text.
139      * @return Returns 0 for success, others for failure.
140      * @since 6
141      */
142     IMF_API int32_t Attach(sptr<OnTextChangedListener> listener);
143 
144     /**
145      * @brief Set listener and bind IMSA with given states and default attribute.
146      *
147      * This function is used to set listener and bind IMSA,
148      * default attribute is 'InputAttribute::PATTERN_TEXT'. Show soft keyboard when state is true.
149      *
150      * @param listener          Indicates the listener in order to manipulate text.
151      * @param isShowKeyboard    Indicates the state, if you want to show soft keyboard, please pass in true.
152      * @return Returns 0 for success, others for failure.
153      * @since 8
154      */
155     IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, bool isShowKeyboard);
156 
157     /**
158      * @brief Set listener and bind IMSA with given states and attribute.
159      *
160      * This function is used to set listener and bind IMSA.
161      * Show soft keyboard when state is true, and customized attribute.
162      *
163      * @param listener          Indicates the listener in order to manipulate text.
164      * @param isShowKeyboard    Indicates the state, if you want to show soft keyboard, please pass in true.
165      * @param attribute         Indicates the attribute, such as input pattern, enter eyType, input option.
166      * @return Returns 0 for success, others for failure.
167      * @since 8
168      */
169     IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, bool isShowKeyboard, const InputAttribute &attribute);
170 
171     /**
172      * @brief Set listener and bind IMSA with given states and textConfig.
173      *
174      * This function is used to set listener and bind IMSA.
175      * Show soft keyboard when state is true, and customized attribute.
176      *
177      * @param listener          Indicates the listener in order to manipulate text.
178      * @param isShowKeyboard    Indicates the state, if you want to show soft keyboard, please pass in true.
179      * @param textConfig        Indicates the textConfig, such as input attribute, cursorInfo, range of text selection,
180      *                          windowId.
181      * @return Returns 0 for success, others for failure.
182      * @since 10
183      */
184     IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, bool isShowKeyboard, const TextConfig &textConfig);
185 
186     /**
187      * @brief Show soft keyboard.
188      *
189      * This function is used to show soft keyboard of current client.
190      *
191      * @return Returns 0 for success, others for failure.
192      * @since 6
193      */
194     IMF_API int32_t ShowTextInput();
195 
196     /**
197      * @brief Hide soft keyboard.
198      *
199      * This function is used to hide soft keyboard of current client, and keep binding.
200      *
201      * @return Returns 0 for success, others for failure.
202      * @since 6
203      */
204     IMF_API int32_t HideTextInput();
205 
206     /**
207      * @brief Hide current input method, clear text listener and unbind IMSA.
208      *
209      * This function is used to stop input, whick will set listener to nullptr,
210      * hide current soft keyboard and unbind IMSA.
211      *
212      * @return Returns 0 for success, others for failure.
213      * @since 6
214      */
215     IMF_API int32_t Close();
216 
217     /**
218      * @brief A callback function when the cursor changes.
219      *
220      * This function is the callback when the cursor changes.
221      *
222      * @param cursorInfo Indicates the information of current cursor changes.
223      * @return Returns 0 for success, others for failure.
224      * @since 6
225      */
226     IMF_API int32_t OnCursorUpdate(CursorInfo cursorInfo);
227 
228     /**
229      * @brief A callback function when the cursor changes.
230      *
231      * This function is the callback when the cursor changes.
232      *
233      * @param text  Indicates the currently selected text.
234      * @param start Indicates the coordinates of the current start.
235      * @param end   Indicates the coordinates of the current end.
236      * @return Returns 0 for success, others for failure.
237      * @since 6
238      */
239     IMF_API int32_t OnSelectionChange(std::u16string text, int start, int end);
240 
241     /**
242      * @brief Changing the configuration of soft keyboard.
243      *
244      * This function is used to change the configuration of soft keyboard.
245      *
246      * @param info Indicates the current configuration.
247      * @return Returns 0 for success, others for failure.
248      * @since 6
249      */
250     IMF_API int32_t OnConfigurationChange(Configuration info);
251     IMF_API void SetControllerListener(std::shared_ptr<ControllerListener> controllerListener);
252 
253     /**
254      * @brief Dispatch keyboard event.
255      *
256      * This function is used to Dispatch events of keyboard.
257      *
258      * @param keyEvent Indicates the events keyboard.
259      * @return Returns true for success otherwise for failure.
260      * @since 6
261      */
262     /**
263      * @brief Dispatch keyboard event.
264      *
265      * This function is used to Dispatch events of keyboard.
266      *
267      * @param keyEvent Indicates the events keyboard.
268      * @param callback Indicates the consumption result of key event.
269      * @return Returns 0 for success, others for failure.
270      * @since 11
271      */
272     IMF_API int32_t DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent, KeyEventCallback callback);
273 
274     /**
275      * @brief List input methods.
276      *
277      * This function is used to list all of input methods.
278      *
279      * @param props Indicates the input methods that will be listed.
280      * @return Returns 0 for success, others for failure.
281      * @since 6
282      */
283     IMF_API int32_t ListInputMethod(std::vector<Property> &props);
284 
285     /**
286      * @brief List input methods.
287      *
288      * This function is used to list enabled or disabled input methods.
289      *
290      * @param props     Indicates the input methods that will be listed.
291      * @param enable    Indicates the state of input method.
292      * @return Returns 0 for success, others for failure.
293      * @since 6
294      */
295     IMF_API int32_t ListInputMethod(bool enable, std::vector<Property> &props);
296 
297     /**
298      * @brief List input method subtypes.
299      *
300      * This function is used to list specified input method subtypes.
301      *
302      * @param property      Indicates the specified input method property.
303      * @param subProperties Indicates the subtypes of specified input method that will be listed.
304      * @return Returns 0 for success, others for failure.
305      * @since 6
306      */
307     IMF_API int32_t ListInputMethodSubtype(const Property &property, std::vector<SubProperty> &subProperties);
308 
309     /**
310      * @brief List current input method subtypes.
311      *
312      * This function is used to list current input method subtypes.
313      *
314      * @param subProperties Indicates the subtypes of current input method that will be listed.
315      * @return Returns 0 for success, others for failure.
316      * @since 6
317      */
318     IMF_API int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProperties);
319 
320     /**
321      * @brief Get enter key type.
322      *
323      * This function is used to get enter key type of current client.
324      *
325      * @param keyType Indicates the enter key type of current client that will be obtained, such as SEND, SEARCH...
326      * @return Returns 0 for success, others for failure.
327      * @since 6
328      */
329     int32_t GetEnterKeyType(int32_t &keyType);
330 
331     /**
332      * @brief Get input pattern.
333      *
334      * This function is used to get text input type of current client.
335      *
336      * @param inputPattern Indicates the text input type of current client that will be obtained, such as TEXT, URL...
337      * @return Returns 0 for success, others for failure.
338      * @since 6
339      */
340     int32_t GetInputPattern(int32_t &inputPattern);
341 
342     /**
343      * @brief Get text config.
344      *
345      * This function is used to get text config of current client.
346      *
347      * @param textConfig Indicates the text config of current client that will be obtained.
348      * @return Returns 0 for success, others for failure.
349      * @since 10
350      */
351     int32_t GetTextConfig(TextTotalConfig &config);
352 
353     /**
354      * @brief Get current input method property.
355      *
356      * This function is used to get current input method property.
357      *
358      * @return The property of current input method.
359      * @since 6
360      */
361     IMF_API std::shared_ptr<Property> GetCurrentInputMethod();
362 
363     /**
364      * @brief Get current input method subtypes.
365      *
366      * This function is used to get current input method's current subtype.
367      *
368      * @return The subtypes of current input method.
369      * @since 6
370      */
371     IMF_API std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype();
372 
373     /**
374      * @brief Get default input method property.
375      *
376      * This function is used to get default input method property.
377      *
378      * @return The property of default input method.
379      * @since 10
380      */
381     IMF_API int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop);
382 
383     /**
384      * @brief get input method config ability.
385      *
386      * This function is used to get input method config ability.
387      *
388      * @return The info of input settings.
389      * @since 10
390      */
391     IMF_API int32_t GetInputMethodConfig(AppExecFwk::ElementName &inputMethodConfig);
392 
393     /**
394      * @brief Set calling window id.
395      *
396      * This function is used to set calling window id to input method.
397      *
398      * @param windowId Indicates the window id.
399      * @return Returns 0 for success, others for failure.
400      * @since 6
401      */
402     IMF_API int32_t SetCallingWindow(uint32_t windowId);
403 
404     /**
405      * @brief Switch input method or subtype.
406      *
407      * This function is used to switch input method or subtype.
408      *
409      * @param name      Indicates the id of target input method.
410      * @param subName   Optional parameter. Indicates the subtype of target input method.
411      * @return Returns 0 for success, others for failure.
412      * @since 11
413      */
414     IMF_API int32_t SwitchInputMethod(SwitchTrigger trigger, const std::string &name, const std::string &subName = "");
415 
416     /**
417      * @brief Show soft keyboard.
418      *
419      * This function is used to show soft keyboard of current client.
420      *
421      * @return Returns 0 for success, others for failure.
422      * @since 6
423      */
424     IMF_API int32_t ShowSoftKeyboard();
425 
426     /**
427      * @brief Hide soft keyboard.
428      *
429      * This function is used to hide soft keyboard of current client, and keep binding.
430      *
431      * @return Returns 0 for success, others for failure.
432      * @since 6
433      */
434     IMF_API int32_t HideSoftKeyboard();
435 
436     /**
437      * @brief Stop current input session.
438      *
439      * This function is used to stop current input session.
440      *
441      * @return Returns 0 for success, others for failure.
442      * @since 6
443      */
444     IMF_API int32_t StopInputSession();
445 
446     /**
447      * @brief Show input method setting extension dialog.
448      *
449      * This function is used to show input method setting extension dialog.
450      *
451      * @return Returns 0 for success, others for failure.
452      * @since 8
453      */
454     IMF_API int32_t ShowOptionalInputMethod();
455 
456     // Deprecated innerkits with no permission check, kept for compatibility
457 
458     /**
459      * @brief Show soft keyboard.
460      *
461      * This function is used to show soft keyboard of current client.
462      *
463      * @return Returns 0 for success, others for failure.
464      * @deprecated since 9
465      * @since 6
466      */
467     IMF_API int32_t ShowCurrentInput();
468 
469     /**
470      * @brief Hide soft keyboard.
471      *
472      * This function is used to hide soft keyboard of current client, and keep binding.
473      *
474      * @return Returns 0 for success, others for failure.
475      * @deprecated since 9
476      * @since 6
477      */
478     IMF_API int32_t HideCurrentInput();
479 
480     /**
481      * @brief Request to show input method.
482      *
483      * This function is used to request to show input method.
484      *
485      * @return Returns 0 for success, others for failure.
486      * @since 11
487      */
488     IMF_API int32_t RequestShowInput();
489 
490     /**
491      * @brief Request to hide input method.
492      *
493      * This function is used to request to hide input method.
494      *
495      * @return Returns 0 for success, others for failure.
496      * @since 11
497      */
498     IMF_API int32_t RequestHideInput();
499 
500     /**
501      * @brief Show input method setting extension dialog.
502      *
503      * This function is used to show input method setting extension dialog.
504      *
505      * @return Returns 0 for success, others for failure.
506      * @deprecated since 9
507      * @since 6
508      */
509     IMF_API int32_t DisplayOptionalInputMethod();
510 
511     /**
512      * @brief Get attach status.
513      *
514      * This function is used to get status of attach.
515      *
516      * @return Returns true for attached otherwise for detached.
517      * @since 10
518      */
519     IMF_API bool WasAttached();
520 
521     /**
522      * @brief Set agent which will be used to communicate with IMA.
523      *
524      * This function is used to Set agent.
525      *
526      * @since 10
527      */
528     void OnInputReady(sptr<IRemoteObject> agentObject);
529 
530     /**
531      * @brief Unbind IMC with Service.
532      *
533      * This function is unbind imc with service.
534      *
535      * @since 10
536      */
537     void OnInputStop(bool isStopInactiveClient = false);
538 
539     /**
540      * @brief Insert text.
541      *
542      * This function is used to insert text into editor.
543      *
544      * @param text Indicates the text which will be inserted.
545      * @return Returns 0 for success, others for failure.
546      * @since 10
547      */
548     int32_t InsertText(const std::u16string &text);
549 
550     /**
551      * @brief Move cursor.
552      *
553      * This function is used to move cursor according to the direction.
554      *
555      * @param direction Indicates the direction according to which the cursor will be moved.
556      * @return Returns 0 for success, others for failure.
557      * @since 10
558      */
559     int32_t MoveCursor(Direction direction);
560 
561     /**
562      * @brief Delete forward.
563      *
564      * This function is used to delete text at the left of cursor.
565      *
566      * @param length Indicates the length of deleted text.
567      * @return Returns 0 for success, others for failure.
568      * @since 10
569      */
570     int32_t DeleteForward(int32_t length);
571 
572     /**
573      * @brief Delete backward.
574      *
575      * This function is used to delete text at the right of cursor.
576      *
577      * @param length Indicates the length of deleted text.
578      * @return Returns 0 for success, others for failure.
579      * @since 10
580      */
581     int32_t DeleteBackward(int32_t length);
582 
583     /**
584      * @brief Get text at the left of cursor.
585      *
586      * This function is used to get text at the left of cursor.
587      *
588      * @param length Indicates the length of text.
589      * @param text Indicates the text which will be get.
590      * @return Returns 0 for success, others for failure.
591      * @since 10
592      */
593     int32_t GetLeft(int32_t length, std::u16string &text);
594 
595     /**
596      * @brief Get text at the right of cursor.
597      *
598      * This function is used to get text at the right of cursor.
599      *
600      * @param length Indicates the length of text.
601      * @param text Indicates the text which will be get.
602      * @return Returns 0 for success, others for failure.
603      * @since 10
604      */
605     int32_t GetRight(int32_t length, std::u16string &text);
606 
607     /**
608      * @brief Select text in editor by range.
609      *
610      * This function is used to select text in editor by range.
611      *
612      * @param start Indicates the beginning of the range.
613      * @param start Indicates the end of the range.
614      * @return Returns 0 for success, others for failure.
615      * @since 10
616      */
617     void SelectByRange(int32_t start, int32_t end);
618 
619     /**
620      * @brief Select text in editor by cursor movement.
621      *
622      * This function is used to select text in editor by cursor movement.
623      *
624      * @param direction Indicates the direction of cursor movement.
625      * @param cursorMoveSkip Indicates the skip of cursor movement.
626      * @return Returns 0 for success, others for failure.
627      * @since 10
628      */
629     void SelectByMovement(int32_t direction, int32_t cursorMoveSkip);
630 
631     /**
632      * @brief Handle extend action code.
633      *
634      * This function is used to handle extend action code.
635      *
636      * @param action Indicates the action code which will be handled.
637      * @return Returns 0 for success, others for failure.
638      * @since 10
639      */
640     int32_t HandleExtendAction(int32_t action);
641 
642     /**
643      * @brief Get the index number of text at cursor.
644      *
645      * This function is used to get the index number of text at cursor.
646      *
647      * @param index Indicates the index number of text at cursor.
648      * @return Returns 0 for success, others for failure.
649      * @since 10
650      */
651     int32_t GetTextIndexAtCursor(int32_t &index);
652 
653     /**
654      * @brief Send keyboard status.
655      *
656      * This function is used to send keyboard status to editor.
657      *
658      * @param status Indicates the status of keyboard.
659      * @since 10
660      */
661     void SendKeyboardStatus(KeyboardStatus status);
662 
663     /**
664      * @brief Send panel status info.
665      *
666      * This function is used to send panel status info to editor.
667      * Only notify the status info of soft keyboard(not contain candidate column) at present
668      *
669      * @param info Indicates the status info of panel.
670      * @since 11
671      */
672     void NotifyPanelStatusInfo(const PanelStatusInfo &info);
673 
674     /**
675      * @brief Send panel height.
676      *
677      * This function is used to send panel height to editor.
678      *
679      * @param info Indicates the panel height.
680      * @since 11
681      */
682     void NotifyKeyboardHeight(uint32_t height);
683 
684     /**
685      * @brief Send function key.
686      *
687      * This function is used to send function key to editor.
688      *
689      * @param functionKey Indicates the function key.
690      * @return Returns 0 for success, others for failure.
691      * @since 10
692      */
693     int32_t SendFunctionKey(int32_t functionKey);
694 
695     /**
696      * @brief Deactivate the input client.
697      *
698      * This function is used to deactivate the input client.
699      *
700      * @since 11
701      */
702     void DeactivateClient();
703 
704     /**
705      * @brief Query whether an input type is supported.
706      *
707      * This function is used to query whether an input type is supported.
708      *
709      * @param type Indicates the input type being queried.
710      * @return Returns true for supported, false for not supported.
711      * @since 11
712      */
713     IMF_API bool IsInputTypeSupported(InputType type);
714 
715     /**
716      * @brief Start the input method which provides the specific input type.
717      *
718      * This function is used to start the input method which provides the specific input type.
719      *
720      * @param type Indicates the input type being specified.
721      * @return Returns 0 for success, others for failure.
722      * @since 11
723      */
724     IMF_API int32_t StartInputType(InputType type);
725 
726     /**
727      * @brief Query whether the specific type panel is shown.
728      *
729      * This function is used to query whether the specific type panel is shown.
730      *
731      * @param panelInfo Indicates the info of the panel.
732      * @param isShown Indicates the state of the specific panel.
733      * @return Returns 0 for success, others for failure.
734      * @since 11
735      */
736     IMF_API int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown);
737     int32_t UpdateListenEventFlag(uint32_t finalEventFlag, uint32_t eventFlag, bool isOn);
738 
739     /**
740      * @brief Send private command to ime.
741      *
742      * This function is used to send private command to ime.
743      *
744      * @param privateCommand Indicates the private command which will be send.
745      * @return Returns 0 for success, others for failure.
746      * @since 12
747      */
748     IMF_API int32_t SendPrivateCommand(
749         const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
750 
751     /**
752      * @brief Receive private command from ime.
753      *
754      * This function is used to receive private command from ime.
755      *
756      * @param privateCommand Indicates the private command which send from ime.
757      * @return Returns 0 for success, others for failure.
758      * @since 12
759      */
760     int32_t ReceivePrivateCommand(
761         const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
762 
763     /**
764      * @brief Set preview text.
765      *
766      * This function is used to set preview text.
767      *
768      * @param text Indicates the text to be previewed.
769      * @param range Indicates the range of text to be replaced.
770      * @return Returns 0 for success, others for failure.
771      * @since 12
772      */
773     int32_t SetPreviewText(const std::string &text, const Range &range);
774 
775     /**
776      * @brief Finish text preview.
777      *
778      * This function is used to finish text preview.
779      *
780      * @since 12
781      */
782     int32_t FinishTextPreview();
783 
784     /**
785      * @brief Query whether an process id current inputmethod.
786      *
787      * This function is used to query whether an process id is inputmethod.
788      *
789      * @param type Indicates current process id.
790      * @return Returns true for current ime process id, false for not current ime process id.
791      * @since 12
792      */
793     IMF_API bool IsCurrentImeByPid(int32_t pid);
794 
795     /**
796      * @brief Reset controller.
797      *
798      * This function is used to reset controller.
799      * Do not call this interface unless you know what you are doing
800      *
801      * @since 12
802      */
803     IMF_API void Reset();
804 
805     /**
806      * @brief Query whether the default input method is setting.
807      *
808      * This function is used to query whether the default input method is setting.
809      * Do not call this interface unless you know what you are doing
810      *
811      * @since 13
812      */
813     IMF_API bool IsDefaultImeSet();
814 
815     /**
816      * @brief Enable the ime called bundleName.
817      *
818      * This function is used to enable the ime called bundleName.
819      * Do not call this interface unless you know what you are doing
820      *
821      * @since 13
822      */
823     IMF_API bool EnableIme(const std::string &bundleName);
824 
825 private:
826     InputMethodController();
827     ~InputMethodController();
828 
829     int32_t Initialize();
830     sptr<IInputMethodSystemAbility> GetSystemAbilityProxy();
831     void RemoveDeathRecipient();
832     int32_t StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent);
833     int32_t ShowInput(sptr<IInputClient> &client);
834     int32_t HideInput(sptr<IInputClient> &client);
835     int32_t ReleaseInput(sptr<IInputClient> &client);
836     int32_t ListInputMethodCommon(InputMethodStatus status, std::vector<Property> &props);
837     void ClearEditorCache(bool isNewEditor, sptr<OnTextChangedListener> lastListener);
838     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
839     void RestoreListenInfoInSaDied();
840     void RestoreAttachInfoInSaDied();
841     int32_t RestoreListenEventFlag();
842     void SaveTextConfig(const TextConfig &textConfig);
843     sptr<OnTextChangedListener> GetTextListener();
844     void SetTextListener(sptr<OnTextChangedListener> listener);
845     bool IsEditable();
846     bool IsBound();
847     void SetAgent(sptr<IRemoteObject> &agentObject);
848     std::shared_ptr<IInputMethodAgent> GetAgent();
849     void PrintLogIfAceTimeout(int64_t start);
850     void PrintKeyEventLog();
851 
852     std::shared_ptr<ControllerListener> controllerListener_;
853     std::mutex abilityLock_;
854     sptr<IInputMethodSystemAbility> abilityManager_ = nullptr;
855     sptr<InputDeathRecipient> deathRecipient_;
856     std::mutex agentLock_;
857     sptr<IRemoteObject> agentObject_ = nullptr;
858     std::shared_ptr<IInputMethodAgent> agent_ = nullptr;
859     std::mutex textListenerLock_;
860     sptr<OnTextChangedListener> textListener_ = nullptr;
861     std::atomic_bool isDiedAttached_{ false };
862 
863     std::mutex cursorInfoMutex_;
864     CursorInfo cursorInfo_;
865 
866     std::atomic_bool isTextNotified_{ false };
867     std::mutex editorContentLock_;
868     std::u16string textString_;
869     int selectOldBegin_ = 0;
870     int selectOldEnd_ = 0;
871     int selectNewBegin_ = 0;
872     int selectNewEnd_ = 0;
873 
874     static std::mutex instanceLock_;
875     static sptr<InputMethodController> instance_;
876     static std::shared_ptr<AppExecFwk::EventHandler> handler_;
877 
878     static std::mutex logLock_;
879     static int keyEventCountInPeriod_;
880     static std::chrono::system_clock::time_point startLogTime_;
881 
882     std::atomic_bool isEditable_{ false };
883     std::atomic_bool isBound_{ false };
884     std::atomic_bool bootCompleted_{ false };
885 
886     std::recursive_mutex clientInfoLock_;
887     InputClientInfo clientInfo_;
888 
889     static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011;
890     std::atomic_bool isDiedRestoreListen_{ false };
891 
892     std::mutex textConfigLock_;
893     TextConfig textConfig_;
894 
895     struct KeyEventInfo {
896         std::chrono::system_clock::time_point timestamp{};
897         std::shared_ptr<MMI::KeyEvent> keyEvent;
898         bool operator==(const KeyEventInfo &info) const
899         {
900             return (timestamp == info.timestamp && keyEvent == info.keyEvent);
901         }
902     };
903     static constexpr int32_t MAX_WAIT_TIME = 5000;
904     BlockQueue<KeyEventInfo> keyEventQueue_{ MAX_WAIT_TIME };
905 };
906 } // namespace MiscServices
907 } // namespace OHOS
908 #endif // FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H
909