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