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 "concurrent_map.h" 22 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.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_control_channel_proxy.h" 29 #include "input_data_channel_proxy.h" 30 #include "input_method_agent_stub.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 "keyevent_consumer_proxy.h" 38 #include "message.h" 39 #include "message_handler.h" 40 #include "unRegistered_type.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 class MessageHandler; 45 class InputMethodAbility : public RefBase { 46 public: 47 InputMethodAbility(); 48 ~InputMethodAbility(); 49 static sptr<InputMethodAbility> GetInstance(); 50 int32_t SetCoreAndAgent(); 51 int32_t UnRegisteredProxyIme(UnRegisteredType type); 52 int32_t InsertText(const std::string text); 53 void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener); 54 void SetKdListener(std::shared_ptr<KeyboardListener> kdListener); 55 int32_t DeleteForward(int32_t length); 56 int32_t DeleteBackward(int32_t length); 57 int32_t HideKeyboardSelf(); 58 int32_t StartInput(const InputClientInfo &clientInfo, bool isBindFromClient); 59 int32_t StopInput(const sptr<IRemoteObject> &channelObject); 60 int32_t ShowKeyboard(); 61 int32_t HideKeyboard(); 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 int32_t DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer); 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 int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo, 76 std::shared_ptr<InputMethodPanel> &inputMethodPanel); 77 int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 78 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 79 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 80 bool IsCurrentIme(); 81 bool IsEnable(); 82 int32_t ExitCurrentInputType(); 83 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown); 84 int32_t GetSecurityMode(int32_t &security); 85 int32_t OnSecurityChange(int32_t security); 86 void OnClientInactive(const sptr<IRemoteObject> &channel); 87 int32_t OnTextConfigChange(const InputClientInfo &clientInfo); 88 void NotifyKeyboardHeight(const std::shared_ptr<InputMethodPanel> inputMethodPanel); 89 90 private: 91 std::thread workThreadHandler; 92 MessageHandler *msgHandler_; 93 bool stop_ = false; 94 95 std::mutex controlChannelLock_; 96 std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr; 97 98 std::mutex dataChannelLock_; 99 sptr<IRemoteObject> dataChannelObject_ = nullptr; 100 std::shared_ptr<InputDataChannelProxy> dataChannelProxy_ = nullptr; 101 std::shared_ptr<InputMethodEngineListener> imeListener_; 102 std::shared_ptr<KeyboardListener> kdListener_; 103 104 static std::mutex instanceLock_; 105 static sptr<InputMethodAbility> instance_; 106 std::mutex abilityLock_; 107 sptr<IInputMethodSystemAbility> abilityManager_{ nullptr }; 108 sptr<InputDeathRecipient> deathRecipient_{ nullptr }; 109 sptr<IInputMethodSystemAbility> GetImsaProxy(); 110 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 111 112 void SetInputDataChannel(const sptr<IRemoteObject> &object); 113 std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy(); 114 void ClearDataChannel(const sptr<IRemoteObject> &channel); 115 void SetInputControlChannel(sptr<IRemoteObject> &object); 116 void ClearInputControlChannel(); 117 std::shared_ptr<InputControlChannelProxy> GetInputControlChannel(); 118 119 void Initialize(); 120 void WorkThread(); 121 void QuitWorkThread(); 122 123 void OnInitInputControlChannel(Message *msg); 124 void OnSetSubtype(Message *msg); 125 void NotifyAllTextConfig(); 126 void InvokeTextChangeCallback(const TextTotalConfig &textConfig); 127 void OnCursorUpdate(Message *msg); 128 void OnSelectionChange(Message *msg); 129 void OnConfigurationChange(Message *msg); 130 void OnStopInputService(Message *msg); 131 132 int32_t HideKeyboard(Trigger trigger); 133 std::shared_ptr<InputMethodPanel> GetSoftKeyboardPanel(); 134 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 135 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 136 void NotifyPanelStatusInfo(const PanelStatusInfo &info); 137 138 ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_{}; 139 std::atomic_bool isBound_{ false }; 140 sptr<InputMethodCoreStub> coreStub_{ nullptr }; 141 sptr<InputMethodAgentStub> agentStub_{ nullptr }; 142 std::mutex imeCheckMutex_; 143 bool isCurrentIme_ = false; 144 145 double positionY_ = 0; 146 double height_ = 0; 147 148 bool isPendingShowKeyboard_ = false; 149 }; 150 } // namespace MiscServices 151 } // namespace OHOS 152 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 153