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_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 17 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 18 19 #include <thread> 20 21 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" 22 #include "concurrent_map.h" 23 #include "i_input_control_channel.h" 24 #include "i_input_data_channel.h" 25 #include "i_input_method_agent.h" 26 #include "i_input_method_core.h" 27 #include "input_attribute.h" 28 #include "input_channel.h" 29 #include "input_control_channel_proxy.h" 30 #include "input_data_channel_proxy.h" 31 #include "input_method_core_stub.h" 32 #include "input_method_engine_listener.h" 33 #include "input_method_panel.h" 34 #include "input_method_system_ability_proxy.h" 35 #include "iremote_object.h" 36 #include "keyboard_listener.h" 37 #include "message.h" 38 #include "message_handler.h" 39 40 namespace OHOS { 41 namespace MiscServices { 42 struct InputStartNotifier { 43 bool isNotify{ false }; 44 bool isShowKeyboard{ false }; 45 }; 46 class MessageHandler; 47 class InputMethodAbility : public RefBase { 48 public: 49 InputMethodAbility(); 50 ~InputMethodAbility(); 51 static sptr<InputMethodAbility> GetInstance(); 52 int32_t SetCoreAndAgent(); 53 int32_t InsertText(const std::string text); 54 void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener); 55 void SetKdListener(std::shared_ptr<KeyboardListener> kdListener); 56 int32_t DeleteForward(int32_t length); 57 int32_t DeleteBackward(int32_t length); 58 int32_t HideKeyboardSelf(); 59 int32_t ShowKeyboard(const sptr<IRemoteObject> &channelObject, bool isShowKeyboard, bool attachFlag); 60 int32_t HideKeyboard(); 61 void ClearDataChannel(const sptr<IRemoteObject> &channel); 62 int32_t SendExtendAction(int32_t action); 63 int32_t GetTextBeforeCursor(int32_t number, std::u16string &text); 64 int32_t GetTextAfterCursor(int32_t number, std::u16string &text); 65 int32_t SendFunctionKey(int32_t funcKey); 66 int32_t MoveCursor(int32_t keyCode); 67 int32_t SelectByRange(int32_t start, int32_t end); 68 int32_t SelectByMovement(int32_t direction); 69 bool DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent); 70 void SetCallingWindow(uint32_t windowId); 71 int32_t GetEnterKeyType(int32_t &keyType); 72 int32_t GetInputPattern(int32_t &inputPattern); 73 int32_t GetTextIndexAtCursor(int32_t &index); 74 int32_t GetTextConfig(TextTotalConfig &textConfig); 75 void OnImeReady(); 76 int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo, 77 std::shared_ptr<InputMethodPanel> &inputMethodPanel); 78 int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 79 80 private: 81 std::thread workThreadHandler; 82 MessageHandler *msgHandler; 83 bool stop_ = false; 84 int32_t KEYBOARD_HIDE = 1; 85 int32_t KEYBOARD_SHOW = 2; 86 87 std::mutex controlChannelLock_; 88 std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr; 89 90 std::mutex dataChannelLock_; 91 sptr<IRemoteObject> dataChannelObject_ = nullptr; 92 std::shared_ptr<InputDataChannelProxy> dataChannelProxy_ = nullptr; 93 std::shared_ptr<InputMethodEngineListener> imeListener_; 94 std::shared_ptr<KeyboardListener> kdListener_; 95 static std::mutex instanceLock_; 96 97 static sptr<InputMethodAbility> instance_; 98 sptr<InputMethodSystemAbilityProxy> mImms; 99 struct ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 100 std::shared_ptr<InputMethodEngineListener> listener{ nullptr }; 101 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 102 std::string currentIme_; 103 }; 104 sptr<ServiceDeathRecipient> deathRecipientPtr_{ nullptr }; 105 sptr<InputMethodSystemAbilityProxy> GetImsaProxy(); 106 107 void SetInputDataChannel(const sptr<IRemoteObject> &object); 108 std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy(); 109 void SetInputControlChannel(sptr<IRemoteObject> &object); 110 std::shared_ptr<InputControlChannelProxy> GetInputControlChannel(); 111 112 void Initialize(); 113 void WorkThread(); 114 void QuitWorkThread(); 115 116 void OnInitInputControlChannel(Message *msg); 117 void OnSetSubtype(Message *msg); 118 119 void OnCursorUpdate(Message *msg); 120 void OnSelectionChange(Message *msg); 121 void OnConfigurationChange(Message *msg); 122 int32_t ShowInputWindow(bool isShowKeyboard); 123 void OnTextConfigChange(const TextTotalConfig &textConfig); 124 bool isImeReady_{ false }; 125 InputStartNotifier notifier_; 126 ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_{}; 127 std::atomic_bool isBound_{ false }; 128 }; 129 } // namespace MiscServices 130 } // namespace OHOS 131 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 132