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 "calling_window_info.h" 22 #include "concurrent_map.h" 23 #include "context.h" 24 #include "i_input_control_channel.h" 25 #include "i_input_data_channel.h" 26 #include "i_input_method_agent.h" 27 #include "i_input_method_core.h" 28 #include "i_input_method_system_ability.h" 29 #include "input_attribute.h" 30 #include "input_control_channel_proxy.h" 31 #include "input_data_channel_proxy.h" 32 #include "input_method_engine_listener.h" 33 #include "input_method_panel.h" 34 #include "iremote_object.h" 35 #include "keyboard_listener.h" 36 #include "keyevent_consumer_proxy.h" 37 #include "msg_handler_callback_interface.h" 38 #include "message.h" 39 #include "message_handler.h" 40 #include "private_command_interface.h" 41 #include "system_cmd_channel_proxy.h" 42 #include "input_method_types.h" 43 44 namespace OHOS { 45 namespace MiscServices { 46 class MessageHandler; 47 class InputMethodAbility : public RefBase, public PrivateCommandInterface { 48 public: 49 InputMethodAbility(); 50 ~InputMethodAbility(); 51 static sptr<InputMethodAbility> GetInstance(); 52 int32_t SetCoreAndAgent(); 53 void SetCoreAndAgentAsync(); 54 int32_t InitConnect(); 55 int32_t UnRegisteredProxyIme(UnRegisteredType type); 56 int32_t InsertText(const std::string text); 57 void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener); 58 std::shared_ptr<InputMethodEngineListener> GetImeListener(); 59 void SetKdListener(std::shared_ptr<KeyboardListener> kdListener); 60 int32_t DeleteForward(int32_t length); 61 int32_t DeleteBackward(int32_t length); 62 int32_t HideKeyboardSelf(); 63 int32_t StartInput(const InputClientInfo &clientInfo, bool isBindFromClient); 64 int32_t StopInput(const sptr<IRemoteObject> &channelObject); 65 int32_t ShowKeyboard(); 66 int32_t HideKeyboard(); 67 int32_t SendExtendAction(int32_t action); 68 int32_t GetTextBeforeCursor(int32_t number, std::u16string &text); 69 int32_t GetTextAfterCursor(int32_t number, std::u16string &text); 70 int32_t SendFunctionKey(int32_t funcKey); 71 int32_t MoveCursor(int32_t keyCode); 72 int32_t SelectByRange(int32_t start, int32_t end); 73 int32_t SelectByMovement(int32_t direction); 74 int32_t DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer); 75 void SetCallingWindow(uint32_t windowId); 76 int32_t GetEnterKeyType(int32_t &keyType); 77 int32_t GetInputPattern(int32_t &inputPattern); 78 int32_t GetTextIndexAtCursor(int32_t &index); 79 int32_t GetTextConfig(TextTotalConfig &textConfig); 80 int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo, 81 std::shared_ptr<InputMethodPanel> &inputMethodPanel); 82 int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 83 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 84 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 85 bool IsCurrentIme(); 86 bool IsEnable(); 87 bool IsSystemApp(); 88 int32_t ExitCurrentInputType(); 89 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown); 90 int32_t GetSecurityMode(int32_t &security); 91 int32_t OnSecurityChange(int32_t security); 92 int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent); 93 void OnClientInactive(const sptr<IRemoteObject> &channel); 94 void NotifyKeyboardHeight(uint32_t panelHeight, PanelFlag panelFlag); 95 int32_t SendPrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 96 int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 97 bool IsDefaultIme(); 98 int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo); 99 int32_t SetPreviewText(const std::string &text, const Range &range); 100 int32_t FinishTextPreview(bool isAsync); 101 int32_t NotifyPanelStatus(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, 102 SysPanelStatus &sysPanelStatus); 103 InputAttribute GetInputAttribute(); 104 int32_t OnStopInputService(bool isTerminateIme); 105 int32_t SendMessage(const ArrayBuffer &arrayBuffer); 106 int32_t RecvMessage(const ArrayBuffer &arrayBuffer); 107 int32_t RegisterMsgHandler(const std::shared_ptr<MsgHandlerCallbackInterface> &msgHandler = nullptr); 108 109 private: 110 std::thread workThreadHandler; 111 MessageHandler *msgHandler_; 112 bool stop_ = false; 113 114 std::mutex controlChannelLock_; 115 std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr; 116 117 std::mutex dataChannelLock_; 118 sptr<IRemoteObject> dataChannelObject_ = nullptr; 119 std::shared_ptr<InputDataChannelProxy> dataChannelProxy_ = nullptr; 120 121 std::mutex systemCmdChannelLock_; 122 sptr<SystemCmdChannelProxy> systemCmdChannelProxy_ = nullptr; 123 124 std::shared_ptr<InputMethodEngineListener> imeListener_; 125 std::shared_ptr<KeyboardListener> kdListener_; 126 127 static std::mutex instanceLock_; 128 static sptr<InputMethodAbility> instance_; 129 std::mutex abilityLock_; 130 sptr<IInputMethodSystemAbility> abilityManager_{ nullptr }; 131 sptr<InputDeathRecipient> deathRecipient_{ nullptr }; 132 sptr<IInputMethodSystemAbility> GetImsaProxy(); 133 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 134 135 sptr<SystemCmdChannelProxy> GetSystemCmdChannelProxy(); 136 void ClearSystemCmdChannel(); 137 138 void SetInputDataChannel(const sptr<IRemoteObject> &object); 139 std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy(); 140 void ClearDataChannel(const sptr<IRemoteObject> &channel); 141 void SetInputControlChannel(sptr<IRemoteObject> &object); 142 void ClearInputControlChannel(); 143 std::shared_ptr<InputControlChannelProxy> GetInputControlChannel(); 144 std::shared_ptr<MsgHandlerCallbackInterface> GetMsgHandlerCallback(); 145 146 void Initialize(); 147 void WorkThread(); 148 void QuitWorkThread(); 149 150 void OnInitInputControlChannel(Message *msg); 151 void OnSetSubtype(Message *msg); 152 int32_t InvokeStartInputCallback(bool isNotifyInputStart); 153 int32_t InvokeStartInputCallback(const TextTotalConfig &textConfig, bool isNotifyInputStart); 154 void OnCursorUpdate(Message *msg); 155 void OnSelectionChange(Message *msg); 156 void OnAttributeChange(Message *msg); 157 158 int32_t HideKeyboard(Trigger trigger); 159 std::shared_ptr<InputMethodPanel> GetSoftKeyboardPanel(); 160 /* param flag: ShowPanel is async, show/hide softkeyboard in alphabet keyboard attached, 161 flag will be changed before finishing show/hide */ 162 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 163 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 164 void SetInputAttribute(const InputAttribute &inputAttribute); 165 void ClearInputAttribute(); 166 void NotifyPanelStatusInfo(const PanelStatusInfo &info); 167 int32_t HideKeyboardImplWithoutLock(int32_t cmdId); 168 int32_t ShowKeyboardImplWithLock(int32_t cmdId); 169 int32_t ShowKeyboardImplWithoutLock(int32_t cmdId); 170 void NotifyPanelStatusInfo(const PanelStatusInfo &info, std::shared_ptr<InputDataChannelProxy> &channelProxy); 171 172 ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_{}; 173 std::atomic_bool isBound_{ false }; 174 175 sptr<IInputMethodCore> coreStub_{ nullptr }; 176 sptr<IInputMethodAgent> agentStub_{ nullptr }; 177 sptr<IInputMethodAgent> systemAgentStub_{ nullptr }; 178 179 bool isPendingShowKeyboard_ = false; 180 double positionY_ = 0; 181 double height_ = 0; 182 183 std::mutex imeCheckMutex_; 184 bool isCurrentIme_ = false; 185 186 std::mutex defaultImeCheckMutex_; 187 bool isDefaultIme_ = false; 188 std::mutex inputAttrLock_; 189 InputAttribute inputAttribute_{}; 190 std::recursive_mutex keyboardCmdLock_; 191 int32_t cmdId_ = 0; 192 std::atomic<bool> isImeTerminating_ = false; 193 std::atomic<int32_t> securityMode_ = -1; 194 std::mutex msgHandlerMutex_; 195 std::shared_ptr<MsgHandlerCallbackInterface> jsMsgHandler_; 196 197 std::mutex systemAppCheckMutex_; 198 bool isSystemApp_ = false; 199 }; 200 } // namespace MiscServices 201 } // namespace OHOS 202 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 203