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 "input_method_types.h" 35 #include "iremote_object.h" 36 #include "keyboard_listener.h" 37 #include "keyevent_consumer_proxy.h" 38 #include "msg_handler_callback_interface.h" 39 #include "private_command_interface.h" 40 #include "system_cmd_channel_proxy.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 class InputMethodAbility : public RefBase, public PrivateCommandInterface { 45 public: 46 InputMethodAbility(); 47 ~InputMethodAbility(); 48 static sptr<InputMethodAbility> GetInstance(); 49 int32_t SetCoreAndAgent(); 50 int32_t InitConnect(); 51 int32_t UnRegisteredProxyIme(UnRegisteredType type); 52 int32_t InsertText(const std::string text); 53 void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener); 54 std::shared_ptr<InputMethodEngineListener> GetImeListener(); 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 60 int32_t SendExtendAction(int32_t action); 61 int32_t GetTextBeforeCursor(int32_t number, std::u16string &text); 62 int32_t GetTextAfterCursor(int32_t number, std::u16string &text); 63 int32_t SendFunctionKey(int32_t funcKey); 64 int32_t MoveCursor(int32_t keyCode); 65 int32_t SelectByRange(int32_t start, int32_t end); 66 int32_t SelectByMovement(int32_t direction); 67 int32_t DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer); 68 void SetCallingWindow(uint32_t windowId); 69 int32_t GetEnterKeyType(int32_t &keyType); 70 int32_t GetInputPattern(int32_t &inputPattern); 71 int32_t GetTextIndexAtCursor(int32_t &index); 72 int32_t GetTextConfig(TextTotalConfig &textConfig); 73 int32_t AdjustKeyboard(); 74 int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo, 75 std::shared_ptr<InputMethodPanel> &inputMethodPanel); 76 int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 77 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 78 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel); 79 bool IsCurrentIme(); 80 bool IsEnable(); 81 bool IsSystemApp(); 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 int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent); 87 void OnClientInactive(const sptr<IRemoteObject> &channel); 88 void NotifyKeyboardHeight(uint32_t panelHeight, PanelFlag panelFlag); 89 int32_t SendPrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 90 int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 91 bool IsDefaultIme(); 92 int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo); 93 int32_t SetPreviewText(const std::string &text, const Range &range); 94 int32_t FinishTextPreview(bool isAsync); 95 int32_t NotifyPanelStatus(PanelType panelType, SysPanelStatus &sysPanelStatus); 96 InputAttribute GetInputAttribute(); 97 void OnSetInputType(InputType inputType); 98 int32_t SendMessage(const ArrayBuffer &arrayBuffer); 99 int32_t RecvMessage(const ArrayBuffer &arrayBuffer); 100 int32_t RegisterMsgHandler(const std::shared_ptr<MsgHandlerCallbackInterface> &msgHandler = nullptr); 101 int32_t OnCallingDisplayIdChanged(uint64_t displayId); 102 103 public: 104 /* called from TaskManager worker thread */ 105 int32_t StartInput(const InputClientInfo &clientInfo, bool isBindFromClient); 106 int32_t StopInput(sptr<IRemoteObject> channelObj); 107 int32_t ShowKeyboard(); 108 int32_t HideKeyboard(); 109 110 void OnInitInputControlChannel(sptr<IRemoteObject> channelObj); 111 void OnSetSubtype(SubProperty subProperty); 112 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height); 113 void OnSelectionChange(std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEndg); 114 void OnAttributeChange(InputAttribute attribute); 115 116 int32_t OnStopInputService(bool isTerminateIme); 117 uint64_t GetCallingWindowDisplayId(); 118 private: 119 std::mutex controlChannelLock_; 120 std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr; 121 122 std::mutex dataChannelLock_; 123 sptr<IRemoteObject> dataChannelObject_ = nullptr; 124 std::shared_ptr<InputDataChannelProxy> dataChannelProxy_ = nullptr; 125 126 std::mutex systemCmdChannelLock_; 127 sptr<SystemCmdChannelProxy> systemCmdChannelProxy_ = nullptr; 128 129 std::shared_ptr<InputMethodEngineListener> imeListener_; 130 std::shared_ptr<KeyboardListener> kdListener_; 131 132 static std::mutex instanceLock_; 133 static sptr<InputMethodAbility> instance_; 134 std::mutex abilityLock_; 135 sptr<IInputMethodSystemAbility> abilityManager_ { nullptr }; 136 sptr<InputDeathRecipient> deathRecipient_ { nullptr }; 137 sptr<IInputMethodSystemAbility> GetImsaProxy(); 138 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 139 140 sptr<SystemCmdChannelProxy> GetSystemCmdChannelProxy(); 141 void ClearSystemCmdChannel(); 142 143 void SetInputDataChannel(const sptr<IRemoteObject> &object); 144 std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy(); 145 void ClearDataChannel(const sptr<IRemoteObject> &channel); 146 void SetInputControlChannel(sptr<IRemoteObject> &object); 147 void ClearInputControlChannel(); 148 std::shared_ptr<InputControlChannelProxy> GetInputControlChannel(); 149 150 void Initialize(); 151 int32_t InvokeStartInputCallback(bool isNotifyInputStart); 152 int32_t InvokeStartInputCallback(const TextTotalConfig &textConfig, bool isNotifyInputStart); 153 154 int32_t HideKeyboard(Trigger trigger); 155 std::shared_ptr<InputMethodPanel> GetSoftKeyboardPanel(); 156 /* param flag: ShowPanel is async, show/hide softkeyboard in alphabet keyboard attached, 157 flag will be changed before finishing show/hide */ 158 int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 159 int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger); 160 void SetInputAttribute(const InputAttribute &inputAttribute); 161 void ClearInputAttribute(); 162 void NotifyPanelStatusInfo(const PanelStatusInfo &info); 163 int32_t HideKeyboardImplWithoutLock(int32_t cmdId); 164 int32_t ShowKeyboardImplWithLock(int32_t cmdId); 165 int32_t ShowKeyboardImplWithoutLock(int32_t cmdId); 166 void NotifyPanelStatusInfo(const PanelStatusInfo &info, std::shared_ptr<InputDataChannelProxy> &channelProxy); 167 void ClearInputType(); 168 std::shared_ptr<MsgHandlerCallbackInterface> GetMsgHandlerCallback(); 169 int32_t StartInputInner(const InputClientInfo &clientInfo, bool isBindFromClient); 170 int32_t InsertTextInner(const std::string &text); 171 int32_t SetPreviewTextInner(const std::string &text, const Range &range); 172 int32_t DeleteForwardInner(int32_t length); 173 int32_t DeleteBackwardInner(int32_t length); 174 int32_t FinishTextPreviewInner(bool isAsync); 175 int32_t GetTextBeforeCursorInner(int32_t number, std::u16string &text); 176 int32_t GetTextAfterCursorInner(int32_t number, std::u16string &text); 177 int32_t GetTextIndexAtCursorInner(int32_t &index); 178 void SetBindClientInfo(const InputClientInfo &clientInfo); 179 HiSysEventClientInfo GetBindClientInfo(); 180 void ReportImeStartInput(int32_t eventCode, int32_t errCode, bool isShowKeyboard, int64_t consumeTime = -1); 181 void ReportBaseTextOperation(int32_t eventCode, int32_t errCode, int64_t consumeTime); 182 ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_ {}; 183 std::atomic_bool isBound_ { false }; 184 185 sptr<IInputMethodCore> coreStub_ { nullptr }; 186 sptr<IInputMethodAgent> agentStub_ { nullptr }; 187 sptr<IInputMethodAgent> systemAgentStub_ { nullptr }; 188 189 double positionY_ = 0; 190 double height_ = 0; 191 192 std::mutex imeCheckMutex_; 193 bool isCurrentIme_ = false; 194 195 std::mutex defaultImeCheckMutex_; 196 bool isDefaultIme_ = false; 197 std::mutex inputAttrLock_; 198 InputAttribute inputAttribute_ {}; 199 std::recursive_mutex keyboardCmdLock_; 200 int32_t cmdId_ = 0; 201 202 std::mutex inputTypeLock_; 203 InputType inputType_ = InputType::NONE; 204 std::atomic<bool> isImeTerminating = false; 205 std::atomic_bool isShowAfterCreate_{ false }; 206 std::atomic<int32_t> securityMode_ = -1; 207 std::mutex msgHandlerMutex_; 208 std::shared_ptr<MsgHandlerCallbackInterface> msgHandler_; 209 210 std::mutex systemAppCheckMutex_; 211 bool isSystemApp_ = false; 212 213 std::mutex bindClientInfoLock_; 214 HiSysEventClientInfo bindClientInfo_; 215 }; 216 } // namespace MiscServices 217 } // namespace OHOS 218 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H 219