1 /* 2 * Copyright (C) 2021 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 <condition_variable> 20 #include <mutex> 21 #include <thread> 22 23 #include "global.h" 24 #include "i_input_client.h" 25 #include "i_input_data_channel.h" 26 #include "i_input_method_agent.h" 27 #include "i_input_method_system_ability.h" 28 #include "input_method_property.h" 29 #include "input_method_setting_listener.h" 30 #include "input_method_status.h" 31 #include "input_method_utils.h" 32 #include "ipc_skeleton.h" 33 #include "iremote_object.h" 34 #include "key_event.h" 35 #include "message_handler.h" 36 #include "visibility.h" 37 38 namespace OHOS { 39 namespace MiscServices { 40 class OnTextChangedListener : public virtual RefBase { 41 public: 42 virtual void InsertText(const std::u16string& text) = 0; 43 virtual void DeleteForward(int32_t length) = 0; 44 virtual void DeleteBackward(int32_t length) = 0; 45 virtual void SendKeyEventFromInputMethod(const KeyEvent& event) = 0; 46 virtual void SendKeyboardInfo(const KeyboardInfo& info) = 0; 47 virtual void SetKeyboardStatus(bool status) = 0; 48 virtual void MoveCursor(const Direction direction) = 0; 49 virtual void HandleSetSelection(int32_t start, int32_t end) = 0; 50 virtual void HandleExtendAction(int32_t action) = 0; 51 virtual void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) = 0; 52 }; 53 54 class ImsaDeathRecipient : public IRemoteObject::DeathRecipient { 55 public: 56 explicit ImsaDeathRecipient(); 57 ~ImsaDeathRecipient() = default; 58 59 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 60 }; 61 62 class InputMethodController : public RefBase { 63 public: 64 IMF_API static sptr<InputMethodController> GetInstance(); 65 IMF_API void Attach(sptr<OnTextChangedListener> &listener); 66 IMF_API void Attach(sptr<OnTextChangedListener> &listener, bool isShowKeyboard); 67 IMF_API void Attach(sptr<OnTextChangedListener> &listener, bool isShowKeyboard, InputAttribute &attribute); 68 IMF_API int32_t GetTextBeforeCursor(int32_t number, std::u16string &text); 69 IMF_API int32_t GetTextAfterCursor(int32_t number, std::u16string &text); 70 IMF_API void ShowTextInput(); 71 IMF_API void HideTextInput(); 72 IMF_API void Close(); 73 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 74 IMF_API void OnCursorUpdate(CursorInfo cursorInfo); 75 IMF_API void OnSelectionChange(std::u16string text, int start, int end); 76 IMF_API void OnConfigurationChange(Configuration info); 77 IMF_API void setImeListener(std::shared_ptr<InputMethodSettingListener> imeListener); 78 IMF_API bool dispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent); 79 IMF_API int32_t ListInputMethod(std::vector<Property> &props); 80 IMF_API int32_t ListInputMethod(bool enable, std::vector<Property> &props); 81 IMF_API int32_t ListInputMethodSubtype(const Property &property, std::vector<SubProperty> &subProperties); 82 IMF_API int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProperties); 83 IMF_API int32_t GetEnterKeyType(int32_t &keyType); 84 IMF_API int32_t GetInputPattern(int32_t &inputPattern); 85 IMF_API std::shared_ptr<Property> GetCurrentInputMethod(); 86 IMF_API std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype(); 87 IMF_API void SetCallingWindow(uint32_t windowId); 88 IMF_API int32_t SwitchInputMethod(const std::string &name, const std::string &subName = ""); 89 IMF_API int32_t ShowSoftKeyboard(); 90 IMF_API int32_t HideSoftKeyboard(); 91 IMF_API int32_t StopInputSession(); 92 IMF_API int32_t ShowOptionalInputMethod(); 93 94 // Deprecated innerkits with no permission check, kept for compatibility 95 IMF_API int32_t ShowCurrentInput(); 96 IMF_API int32_t HideCurrentInput(); 97 IMF_API int32_t DisplayOptionalInputMethod(); 98 99 private: 100 InputMethodController(); 101 ~InputMethodController(); 102 103 bool Initialize(); 104 sptr<IInputMethodSystemAbility> GetSystemAbilityProxy(); 105 void PrepareInput(int32_t displayId, sptr<IInputClient> &client, sptr<IInputDataChannel> &channel, 106 InputAttribute &attribute); 107 void StartInput(sptr<IInputClient> &client, bool isShowKeyboard); 108 void StopInput(sptr<IInputClient> &client); 109 void ReleaseInput(sptr<IInputClient> &client); 110 void SetInputMethodAgent(sptr<IRemoteObject> &object); 111 void OnSwitchInput(const Property &property, const SubProperty &subProperty); 112 std::shared_ptr<IInputMethodAgent> GetInputMethodAgent(); 113 void WorkThread(); 114 void QuitWorkThread(); 115 int32_t ListInputMethodCommon(InputMethodStatus status, std::vector<Property> &props); 116 void HandleGetOperation(); 117 bool IsCorrectParam(int32_t number); 118 void DoIncrease(int32_t status); 119 120 sptr<IInputDataChannel> mInputDataChannel; 121 std::shared_ptr<InputMethodSettingListener> imeListener_; 122 sptr<IInputClient> mClient; 123 std::mutex abilityLock_; 124 sptr<IInputMethodSystemAbility> abilityManager_ = nullptr; 125 sptr<ImsaDeathRecipient> deathRecipient_; 126 std::mutex agentLock_; 127 std::shared_ptr<IInputMethodAgent> mAgent = nullptr; 128 std::mutex textListenerLock_; 129 sptr<OnTextChangedListener> textListener; 130 InputAttribute mAttribute; 131 std::u16string mTextString; 132 int mSelectOldBegin = 0; 133 int mSelectOldEnd = 0; 134 int mSelectNewBegin = 0; 135 int mSelectNewEnd = 0; 136 CursorInfo cursorInfo_; 137 138 static std::mutex instanceLock_; 139 static sptr<InputMethodController> instance_; 140 std::thread workThreadHandler; 141 MessageHandler *msgHandler; 142 bool stop_; 143 int32_t enterKeyType_ = 0; 144 int32_t inputPattern_ = 0; 145 146 bool isStopInput {true}; 147 std::mutex textFieldReplyCountLock_; 148 uint32_t textFieldReplyCount_{ 0 }; 149 std::condition_variable textFieldReplyCountCv_; 150 }; 151 } // namespace MiscServices 152 } // namespace OHOS 153 #endif // FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H 154