From a81ef1bcd6b7996a144d2548f3c64b98dd4f4180 Mon Sep 17 00:00:00 2001 From: zhouyongfei Date: Tue, 22 Feb 2022 16:14:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=88=E5=85=A5js=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouyongfei --- BUILD.gn | 4 +- bundle.json | 25 +- etc/para/inputmethod.para | 2 +- frameworks/inputmethod_ability/BUILD.gn | 13 +- .../include/input_method_ability.h | 10 +- .../src/input_method_ability.cpp | 98 ++++- .../src/input_method_core_proxy.cpp | 1 + .../src/input_method_core_stub.cpp | 2 +- .../include/i_input_data_channel.h | 10 +- .../include/input_data_channel_proxy.h | 7 +- .../include/input_data_channel_stub.h | 7 +- .../include/input_method_controller.h | 9 +- .../input_method_system_ability_proxy.h | 1 + .../src/input_data_channel_proxy.cpp | 40 +- .../src/input_data_channel_stub.cpp | 44 ++- .../src/input_method_controller.cpp | 39 +- .../src/input_method_system_ability_proxy.cpp | 19 + .../js/declaration/api/@ohos.inputmethod.d.ts | 8 + .../api/@ohos.inputmethodengine.d.ts | 106 ++++-- .../kits/js/napi/{ => inputmethod}/BUILD.gn | 59 +-- .../include/js_input_method_controller.h | 36 ++ .../include/js_input_method_registry.h} | 10 +- .../include/js_input_method_setting.h | 38 ++ .../include/js_input_method_utils.h} | 14 +- .../src/input_method_module.cpp | 4 +- .../src/js_input_method_controller.cpp | 53 +++ .../src/js_input_method_registry.cpp | 112 ++++++ .../src/js_input_method_setting.cpp | 87 +++++ .../inputmethod/src/js_input_method_utils.cpp | 61 +++ .../kits/js/napi/inputmethodengine/BUILD.gn | 74 ++++ .../include/js_editor_attribute.h | 36 ++ .../include/js_input_method_engine.h | 41 ++ .../include/js_input_method_engine_listener.h | 11 +- .../include/js_input_method_engine_registry.h | 33 ++ .../include/js_input_method_engine_utils.h | 37 ++ .../include/js_keyboard_controller.h | 34 ++ .../include/js_keyboard_delegate.h | 45 +++ .../include/js_keyboard_delegate_listener.h | 55 +++ .../include/js_text_input_client.h | 46 +++ .../src/input_method_engine_module.cpp | 4 +- .../src/js_editor_attribute.cpp | 75 ++++ .../src/js_input_method_engine.cpp | 96 +++++ .../src/js_input_method_engine_listener.cpp | 132 ++----- .../src/js_input_method_engine_registry.cpp | 177 +++++++++ .../src/js_input_method_engine_utils.cpp | 125 ++++++ .../src/js_keyboard_controller.cpp | 51 +++ .../src/js_keyboard_delegate.cpp | 106 ++++++ .../src/js_keyboard_delegate_listener.cpp | 205 ++++++++++ .../src/js_text_input_client.cpp | 232 ++++++++++++ .../kits/js/napi/src/js_input_method.cpp | 136 ------- .../js/napi/src/js_input_method_engine.cpp | 357 ------------------ .../include/i_input_method_system_ability.h | 2 + .../input_method_system_ability_stub.h | 1 + services/include/message_handler.h | 1 + services/src/input_method_system_ability.cpp | 1 + .../src/input_method_system_ability_stub.cpp | 17 + services/src/peruser_session.cpp | 3 + 57 files changed, 2309 insertions(+), 743 deletions(-) rename interfaces/kits/js/napi/{ => inputmethod}/BUILD.gn (57%) create mode 100644 interfaces/kits/js/napi/inputmethod/include/js_input_method_controller.h rename interfaces/kits/js/napi/{include/js_input_method.h => inputmethod/include/js_input_method_registry.h} (69%) create mode 100644 interfaces/kits/js/napi/inputmethod/include/js_input_method_setting.h rename interfaces/kits/js/napi/{include/js_input_method_engine.h => inputmethod/include/js_input_method_utils.h} (66%) rename interfaces/kits/js/napi/{ => inputmethod}/src/input_method_module.cpp (87%) create mode 100644 interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp create mode 100644 interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp create mode 100644 interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp create mode 100644 interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/BUILD.gn create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h rename interfaces/kits/js/napi/{ => inputmethodengine}/include/js_input_method_engine_listener.h (79%) create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_registry.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_utils.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_controller.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate_listener.h create mode 100644 interfaces/kits/js/napi/inputmethodengine/include/js_text_input_client.h rename interfaces/kits/js/napi/{ => inputmethodengine}/src/input_method_engine_module.cpp (86%) create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp rename interfaces/kits/js/napi/{ => inputmethodengine}/src/js_input_method_engine_listener.cpp (54%) create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp create mode 100644 interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp delete mode 100644 interfaces/kits/js/napi/src/js_input_method.cpp delete mode 100644 interfaces/kits/js/napi/src/js_input_method_engine.cpp diff --git a/BUILD.gn b/BUILD.gn index bf070ce..05ef0e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -24,8 +24,8 @@ group("inputmethod_native_packages") { "frameworks/inputmethod_ability:inputmethod_ability", "frameworks/inputmethod_controller:inputmethod_client", "interfaces/kits/js/declaration:inputmethod", - "interfaces/kits/js/napi:inputmethod", - "interfaces/kits/js/napi:inputmethodengine", + "interfaces/kits/js/napi/inputmethod:inputmethod", + "interfaces/kits/js/napi/inputmethodengine:inputmethodengine", "profile:miscservices_inputmethod_sa_profiles", "services:inputmethod_service", ] diff --git a/bundle.json b/bundle.json index 06338d5..c0418e7 100644 --- a/bundle.json +++ b/bundle.json @@ -102,13 +102,30 @@ } }, { - "name": "//base/miscservices/inputmethod/interfaces/kits/js/napi:inputmethodengine", + "name": "//base/miscservices/inputmethod/interfaces/kits/js/napi/inputmethod:inputmethod", "header": { "header_files": [ - "js_input_method_engine.h", - "js_input_method_engine_listener.h" + "js_input_method_registry.h", + "js_input_method_utils.h", + "js_input_method_setting.h", + "js_input_method_controller.h" ], - "header_base": "//base/miscservices/inputmethod/interfaces/kits/js/napi/include" + "header_base": "//base/miscservices/inputmethod/interfaces/kits/js/napi/inputmethod/include" + } + }, + { + "name": "//base/miscservices/inputmethod/interfaces/kits/js/napi/inputmethodengine:inputmethodengine", + "header": { + "header_files": [ + "js_input_method_engine_registry.h", + "js_input_method_engine_listener.h", + "js_input_method_engine.h", + "js_input_method_engine_utils.h", + "js_keyboard_controller.h", + "js_text_input_client.h", + "js_keyboard_delegate.h" + ], + "header_base": "//base/miscservices/inputmethod/interfaces/kits/js/napi/inputmethodengine/include" } } ], diff --git a/etc/para/inputmethod.para b/etc/para/inputmethod.para index 6a7b185..11bbb3b 100644 --- a/etc/para/inputmethod.para +++ b/etc/para/inputmethod.para @@ -11,4 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -persist.sys.default_ime=com.example.kikakeyboard/com.example.kikakeyboard.ServiceExtAbility \ No newline at end of file +persist.sys.default_ime=com.example.kikakeyboard/ServiceExtAbility \ No newline at end of file diff --git a/frameworks/inputmethod_ability/BUILD.gn b/frameworks/inputmethod_ability/BUILD.gn index 214f242..6fa93ce 100644 --- a/frameworks/inputmethod_ability/BUILD.gn +++ b/frameworks/inputmethod_ability/BUILD.gn @@ -18,7 +18,7 @@ config("inputmethod_ability_native_config") { include_dirs = [ "include", "${inputmethod_path}/frameworks/inputmethod_controller/include", - "${inputmethod_path}/interfaces/kits/js/napi/include", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/include", "${inputmethod_path}/services/include", ] } @@ -33,8 +33,15 @@ config("inputmethod_ability_native_public_config") { ohos_shared_library("inputmethod_ability") { sources = [ "${inputmethod_path}/frameworks/inputmethod_controller/src/input_data_channel_proxy.cpp", - "${inputmethod_path}/interfaces/kits/js/napi/src/js_input_method_engine.cpp", - "${inputmethod_path}/interfaces/kits/js/napi/src/js_input_method_engine_listener.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp", + "${inputmethod_path}/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp", "${inputmethod_path}/services/src/input_attribute.cpp", "${inputmethod_path}/services/src/input_channel.cpp", "${inputmethod_path}/services/src/input_control_channel_proxy.cpp", diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index 88dce9d..6372251 100644 --- a/frameworks/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/inputmethod_ability/include/input_method_ability.h @@ -18,6 +18,7 @@ #include #include "js_input_method_engine_listener.h" +#include "js_keyboard_delegate_listener.h" #include "iremote_object.h" #include "i_input_control_channel.h" #include "i_input_method_core.h" @@ -44,14 +45,18 @@ namespace MiscServices { sptr OnConnect(); bool InsertText(const std::string text); void setImeListener(sptr &imeListener); + void setKdListener(sptr &kdListener); void DeleteForward(int32_t length); void DeleteBackward(int32_t length); void HideKeyboardSelf(); - std::u16string GetTextBeforeCursor(); - std::u16string GetTextAfterCursor(); + std::u16string GetTextBeforeCursor(int32_t number); + std::u16string GetTextAfterCursor(int32_t number); void SendFunctionKey(int32_t funcKey); void MoveCursor(int32_t keyCode); bool DispatchKeyEvent(int32_t keyCode, int32_t keyStatus); + int32_t GetEnterKeyType(); + int32_t GetInputPattern(); + void StopInput(); private: std::thread workThreadHandler; @@ -73,6 +78,7 @@ namespace MiscServices { // communicating with IMC sptr inputDataChannel; sptr imeListener_; + sptr kdListener_; static std::mutex instanceLock_; static sptr instance_; sptr mImms; diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index 7b4c1ee..7e6ddfb 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -125,6 +125,14 @@ namespace MiscServices { } } + void InputMethodAbility::setKdListener(sptr &kdListener) + { + IMSA_HILOGI("InputMethodAbility::setKdListener"); + if (kdListener_ == nullptr) { + kdListener_ = kdListener; + } + } + void InputMethodAbility::WorkThread() { while (!stop_) { @@ -170,7 +178,9 @@ namespace MiscServices { case MSG_ID_STOP_INPUT_SERVICE:{ MessageParcel *data = msg->msgContent_; std::string imeId = Str16ToStr8(data->ReadString16()); - imeListener_->OnInputStop(imeId); + if (imeListener_ != nullptr) { + imeListener_->OnInputStop(imeId); + } break; } default: { @@ -267,22 +277,54 @@ namespace MiscServices { IMSA_HILOGI("InputMethodAbility::DispatchKeyEvent abort. no client"); return false; } - return imeListener_->OnKeyEvent(keyCode, keyStatus); + if (kdListener_ == nullptr) { + IMSA_HILOGI("InputMethodAbility::DispatchKeyEvent kdListener_ is nullptr"); + return false; + } + return kdListener_->OnKeyEvent(keyCode, keyStatus); } void InputMethodAbility::OnCursorUpdate(Message *msg) { IMSA_HILOGI("InputMethodAbility::OnCursorUpdate"); + MessageParcel *data = msg->msgContent_; + int32_t positionX = data->ReadInt32(); + int32_t positionY = data->ReadInt32(); + int32_t height = data->ReadInt32(); + if (kdListener_ == nullptr) { + IMSA_HILOGI("InputMethodAbility::OnCursorUpdate kdListener_ is nullptr"); + return; + } + kdListener_->OnCursorUpdate(positionX, positionY, height); } void InputMethodAbility::OnSelectionChange(Message *msg) { IMSA_HILOGI("InputMethodAbility::OnSelectionChange"); + MessageParcel *data = msg->msgContent_; + std::string text = Str16ToStr8(data->ReadString16()); + int32_t oldBegin = data->ReadInt32(); + int32_t oldEnd = data->ReadInt32(); + int32_t newBegin = data->ReadInt32(); + int32_t newEnd = data->ReadInt32(); + + if (kdListener_ == nullptr) { + IMSA_HILOGI("InputMethodAbility::OnSelectionChange kdListener_ is nullptr"); + return; + } + kdListener_->OnTextChange(text); + + kdListener_->OnSelectionChange(oldBegin, oldEnd, newBegin, newEnd); } void InputMethodAbility::ShowInputWindow() { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); + if (imeListener_ == nullptr) { + IMSA_HILOGI("InputMethodAbility::ShowInputWindow imeListener_ is nullptr"); + return; + } + imeListener_->OnInputStart(); imeListener_->OnKeyboardStatus(true); if (inputDataChannel != nullptr) { inputDataChannel->SendKeyboardStatus(KEYBOARD_SHOW); @@ -292,6 +334,10 @@ namespace MiscServices { void InputMethodAbility::DissmissInputWindow() { IMSA_HILOGI("InputMethodAbility::DissmissInputWindow"); + if (imeListener_ == nullptr) { + IMSA_HILOGI("InputMethodAbility::DissmissInputWindow imeListener_ is nullptr"); + return; + } imeListener_->OnKeyboardStatus(false); if (inputDataChannel != nullptr) { inputDataChannel->SendKeyboardStatus(KEYBOARD_HIDE); @@ -345,16 +391,26 @@ namespace MiscServices { inputControlChannel->hideKeyboardSelf(1); } - std::u16string InputMethodAbility::GetTextBeforeCursor() + std::u16string InputMethodAbility::GetTextBeforeCursor(int32_t number) { IMSA_HILOGI("InputMethodAbility::GetTextBeforeCursor"); - return inputDataChannel->GetTextBeforeCursor(); + + if (inputDataChannel == nullptr) { + IMSA_HILOGI("InputMethodAbility::GetTextBeforeCursor inputDataChanel is nullptr"); + return u""; + } + return inputDataChannel->GetTextBeforeCursor(number); } - std::u16string InputMethodAbility::GetTextAfterCursor() + std::u16string InputMethodAbility::GetTextAfterCursor(int32_t number) { IMSA_HILOGI("InputMethodAbility::GetTextAfterCursor"); - return inputDataChannel->GetTextAfterCursor(); + + if (inputDataChannel == nullptr) { + IMSA_HILOGI("InputMethodAbility::GetTextAfterCursor inputDataChanel is nullptr"); + return u""; + } + return inputDataChannel->GetTextAfterCursor(number); } void InputMethodAbility::MoveCursor(int32_t keyCode) @@ -369,5 +425,35 @@ namespace MiscServices { inputDataChannel->MoveCursor(keyCode); return; } + + int32_t InputMethodAbility::GetEnterKeyType() { + IMSA_HILOGI("InputMethodAbility::GetEnterKeyType"); + + if (inputDataChannel == nullptr) { + IMSA_HILOGI("InputMethodAbility::GetEnterKeyType inputDataChanel is nullptr"); + return 0; + } + return inputDataChannel->GetEnterKeyType(); + } + + int32_t InputMethodAbility::GetInputPattern() { + IMSA_HILOGI("InputMethodAbility::GetInputPattern"); + + if (inputDataChannel == nullptr) { + IMSA_HILOGI("InputMethodAbility::GetInputPattern inputDataChanel is nullptr"); + return 0; + } + return inputDataChannel->GetInputPattern(); + } + + void InputMethodAbility::StopInput() { + IMSA_HILOGI("InputMethodAbility::StopInput"); + + if (inputDataChannel == nullptr) { + IMSA_HILOGI("InputMethodAbility::StopInput inputDataChanel is nullptr"); + return; + } + inputDataChannel->StopInput(); + } } } \ No newline at end of file diff --git a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp index 3556370..ba952c1 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp @@ -71,6 +71,7 @@ namespace MiscServices { if (inputControlChannel == nullptr) { IMSA_HILOGI("InputMethodCoreProxy::InitInputControlChannel inputControlChannel is nullptr"); + return ErrorCode::ERROR_EX_NULL_POINTER; } MessageParcel data, reply; data.WriteInterfaceToken(GetDescriptor()); diff --git a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp index 6bb314c..2fc3af6 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp @@ -234,7 +234,7 @@ namespace MiscServices { } MessageParcel *data = new MessageParcel(); if (inputDataChannel != nullptr) { - IMSA_HILOGI("InputMethodCoreStub::startInput inputDataChannel is not nullptr"); + IMSA_HILOGI("InputMethodCoreStub::showKeyboard inputDataChannel is not nullptr"); data->WriteRemoteObject(inputDataChannel->AsObject()); } diff --git a/frameworks/inputmethod_controller/include/i_input_data_channel.h b/frameworks/inputmethod_controller/include/i_input_data_channel.h index 2d95e46..4052fc2 100644 --- a/frameworks/inputmethod_controller/include/i_input_data_channel.h +++ b/frameworks/inputmethod_controller/include/i_input_data_channel.h @@ -35,6 +35,9 @@ namespace MiscServices { CLOSE, GET_TEXT_BEFORE_CURSOR, GET_TEXT_AFTER_CURSOR, + GET_ENTER_KEY_TYPE, + GET_INPUT_PATTERN, + STOP_INPUT, SEND_KEYBOARD_STATUS, SEND_FUNCTION_KEY, MOVE_CURSOR, @@ -46,11 +49,14 @@ namespace MiscServices { virtual bool DeleteForward(int32_t length) = 0; virtual bool DeleteBackward(int32_t length) = 0; virtual void Close() = 0; - virtual std::u16string GetTextBeforeCursor() = 0; - virtual std::u16string GetTextAfterCursor() = 0; + virtual std::u16string GetTextBeforeCursor(int32_t number) = 0; + virtual std::u16string GetTextAfterCursor(int32_t number) = 0; virtual void SendKeyboardStatus(int32_t status) = 0; virtual void SendFunctionKey(int32_t funcKey) = 0; virtual void MoveCursor(int32_t keyCode) = 0; + virtual int32_t GetEnterKeyType() = 0; + virtual int32_t GetInputPattern() = 0; + virtual void StopInput() = 0; }; } } diff --git a/frameworks/inputmethod_controller/include/input_data_channel_proxy.h b/frameworks/inputmethod_controller/include/input_data_channel_proxy.h index d2db27f..d5708ba 100644 --- a/frameworks/inputmethod_controller/include/input_data_channel_proxy.h +++ b/frameworks/inputmethod_controller/include/input_data_channel_proxy.h @@ -32,11 +32,14 @@ namespace MiscServices { bool DeleteForward(int32_t length) override; bool DeleteBackward(int32_t length) override; void Close() override; - std::u16string GetTextBeforeCursor() override; - std::u16string GetTextAfterCursor() override; + std::u16string GetTextBeforeCursor(int32_t number) override; + std::u16string GetTextAfterCursor(int32_t number) override; void SendKeyboardStatus(int32_t status) override; void SendFunctionKey(int32_t funcKey) override; void MoveCursor(int32_t keyCode) override; + int32_t GetEnterKeyType() override; + int32_t GetInputPattern() override; + void StopInput() override; private: static inline BrokerDelegator delegator_; diff --git a/frameworks/inputmethod_controller/include/input_data_channel_stub.h b/frameworks/inputmethod_controller/include/input_data_channel_stub.h index 26d8512..9f860db 100644 --- a/frameworks/inputmethod_controller/include/input_data_channel_stub.h +++ b/frameworks/inputmethod_controller/include/input_data_channel_stub.h @@ -40,11 +40,14 @@ namespace MiscServices { bool DeleteForward(int32_t length) override; bool DeleteBackward(int32_t length) override; void Close() override; - std::u16string GetTextBeforeCursor() override; - std::u16string GetTextAfterCursor() override; + std::u16string GetTextBeforeCursor(int32_t number) override; + std::u16string GetTextAfterCursor(int32_t number) override; void SendKeyboardStatus(int32_t status) override; void SendFunctionKey(int32_t funcKey) override; void MoveCursor(int32_t keyCode) override; + int32_t GetEnterKeyType() override; + int32_t GetInputPattern() override; + void StopInput() override; private: MessageHandler *msgHandler; }; diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 78cdfed..8acfdfe 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -56,8 +56,8 @@ namespace MiscServices { public: static sptr GetInstance(); void Attach(sptr &listener); - std::u16string GetTextBeforeCursor(); - std::u16string GetTextAfterCursor(); + std::u16string GetTextBeforeCursor(int32_t number); + std::u16string GetTextAfterCursor(int32_t number); void ShowTextInput(); void HideTextInput(); void Close(); @@ -68,6 +68,9 @@ namespace MiscServices { bool dispatchKeyEvent(std::shared_ptr keyEvent); void DisplayOptionalInputMethod(); std::vector ListInputMethod(); + int32_t GetEnterKeyType(); + int32_t GetInputPattern(); + void HideCurrentInput(); private: InputMethodController(); ~InputMethodController(); @@ -100,6 +103,8 @@ namespace MiscServices { std::thread workThreadHandler; MessageHandler *msgHandler; bool stop_; + int32_t enterKeyType_ = 0; + int32_t inputPattern_ = 0; }; } } diff --git a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h index a20901c..4a1536a 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -41,6 +41,7 @@ namespace MiscServices { virtual void startInput(MessageParcel& data) override; virtual void stopInput(MessageParcel& data) override; virtual void SetCoreAndAgent(MessageParcel& data) override; + virtual void HideCurrentInput(MessageParcel& data) override; int32_t Prepare(int32_t displayId, sptr &client, sptr &channel, InputAttribute &attribute); diff --git a/frameworks/inputmethod_controller/src/input_data_channel_proxy.cpp b/frameworks/inputmethod_controller/src/input_data_channel_proxy.cpp index 7d6207b..a72fc07 100644 --- a/frameworks/inputmethod_controller/src/input_data_channel_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_data_channel_proxy.cpp @@ -84,24 +84,26 @@ namespace MiscServices { } } - std::u16string InputDataChannelProxy::GetTextBeforeCursor() + std::u16string InputDataChannelProxy::GetTextBeforeCursor(int32_t number) { IMSA_HILOGI("InputDataChannelProxy::GetTextBeforeCursor"); MessageParcel data, reply; MessageOption option; data.WriteInterfaceToken(GetDescriptor()); + data.WriteInt32(number); Remote()->SendRequest(GET_TEXT_BEFORE_CURSOR, data, reply, option); auto result = reply.ReadString16(); return result; } - std::u16string InputDataChannelProxy::GetTextAfterCursor() + std::u16string InputDataChannelProxy::GetTextAfterCursor(int32_t number) { IMSA_HILOGI("InputDataChannelProxy::GetTextAfterCursor"); MessageParcel data, reply; MessageOption option; data.WriteInterfaceToken(GetDescriptor()); + data.WriteInt32(number); Remote()->SendRequest(GET_TEXT_AFTER_CURSOR, data, reply, option); auto result = reply.ReadString16(); @@ -141,5 +143,39 @@ namespace MiscServices { Remote()->SendRequest(MOVE_CURSOR, data, reply, option); } + + int32_t InputDataChannelProxy::GetEnterKeyType() + { + IMSA_HILOGI("InputDataChannelProxy::GetEnterKeyType"); + MessageParcel data, reply; + MessageOption option; + data.WriteInterfaceToken(GetDescriptor()); + + Remote()->SendRequest(GET_ENTER_KEY_TYPE, data, reply, option); + auto result = reply.ReadInt32(); + return result; + } + + int32_t InputDataChannelProxy::GetInputPattern() + { + IMSA_HILOGI("InputDataChannelProxy::GetInputPattern"); + MessageParcel data, reply; + MessageOption option; + data.WriteInterfaceToken(GetDescriptor()); + + Remote()->SendRequest(GET_INPUT_PATTERN, data, reply, option); + auto result = reply.ReadInt32(); + return result; + } + + void InputDataChannelProxy::StopInput() + { + IMSA_HILOGI("InputDataChannelProxy::StopInput"); + MessageParcel data, reply; + MessageOption option; + data.WriteInterfaceToken(GetDescriptor()); + + Remote()->SendRequest(STOP_INPUT, data, reply, option); + } } } diff --git a/frameworks/inputmethod_controller/src/input_data_channel_stub.cpp b/frameworks/inputmethod_controller/src/input_data_channel_stub.cpp index 6b80035..d9b9f30 100644 --- a/frameworks/inputmethod_controller/src/input_data_channel_stub.cpp +++ b/frameworks/inputmethod_controller/src/input_data_channel_stub.cpp @@ -59,11 +59,13 @@ namespace MiscServices { break; } case GET_TEXT_BEFORE_CURSOR: { - reply.WriteString16(GetTextBeforeCursor()); + auto number = data.ReadInt32(); + reply.WriteString16(GetTextBeforeCursor(number)); break; } case GET_TEXT_AFTER_CURSOR: { - reply.WriteString16(GetTextAfterCursor()); + auto number = data.ReadInt32(); + reply.WriteString16(GetTextAfterCursor(number)); break; } case SEND_KEYBOARD_STATUS: { @@ -80,6 +82,18 @@ namespace MiscServices { auto keyCode = data.ReadInt32(); MoveCursor(keyCode); } + case GET_ENTER_KEY_TYPE: { + reply.WriteInt32(GetEnterKeyType()); + break; + } + case GET_INPUT_PATTERN: { + reply.WriteInt32(GetInputPattern()); + break; + } + case STOP_INPUT: { + StopInput(); + break; + } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -131,16 +145,34 @@ namespace MiscServices { { } - std::u16string InputDataChannelStub::GetTextBeforeCursor() + std::u16string InputDataChannelStub::GetTextBeforeCursor(int32_t number) { IMSA_HILOGI("InputDataChannelStub::GetTextBeforeCursor"); - return InputMethodController::GetInstance()->GetTextBeforeCursor(); + return InputMethodController::GetInstance()->GetTextBeforeCursor(number); } - std::u16string InputDataChannelStub::GetTextAfterCursor() + std::u16string InputDataChannelStub::GetTextAfterCursor(int32_t number) { IMSA_HILOGI("InputDataChannelStub::GetTextAfterCursor"); - return InputMethodController::GetInstance()->GetTextAfterCursor(); + return InputMethodController::GetInstance()->GetTextAfterCursor(number); + } + + int32_t InputDataChannelStub::GetEnterKeyType() + { + IMSA_HILOGI("InputDataChannelStub::GetEnterKeyType"); + return InputMethodController::GetInstance()->GetEnterKeyType(); + } + + int32_t InputDataChannelStub::GetInputPattern() + { + IMSA_HILOGI("InputDataChannelStub::GetInputPattern"); + return InputMethodController::GetInstance()->GetInputPattern(); + } + + void InputDataChannelStub::StopInput() + { + IMSA_HILOGI("InputDataChannelStub::StopInput"); + InputMethodController::GetInstance()->HideTextInput(); } void InputDataChannelStub::SendKeyboardStatus(int32_t status) diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 05bb100..f0a5d47 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -208,6 +208,19 @@ using namespace MessageID; StopInput(mClient); } + void InputMethodController::HideCurrentInput() + { + IMSA_HILOGI("InputMethodController::HideCurrentInput"); + if (mImms == nullptr) { + return; + } + MessageParcel data; + if (!(data.WriteInterfaceToken(mImms->GetDescriptor()))) { + return; + } + mImms->HideCurrentInput(data); + } + void InputMethodController::Close() { ReleaseInput(mClient); @@ -348,24 +361,26 @@ using namespace MessageID; void InputMethodController::OnConfigurationChange(Configuration info) { IMSA_HILOGI("InputMethodController::OnConfigurationChange"); + enterKeyType_ = static_cast(info.GetEnterKeyType()); + inputPattern_ = static_cast(info.GetTextInputType()); } - std::u16string InputMethodController::GetTextBeforeCursor() + std::u16string InputMethodController::GetTextBeforeCursor(int32_t number) { IMSA_HILOGI("InputMethodController::GetTextBeforeCursor"); if (!mTextString.empty()) { - return mTextString.substr(0, mSelectNewBegin); + int32_t startPos = (mSelectNewBegin >= number ? (mSelectNewBegin - number + 1) : 0); + return mTextString.substr(startPos, mSelectNewBegin); } return u""; } - std::u16string InputMethodController::GetTextAfterCursor() + std::u16string InputMethodController::GetTextAfterCursor(int32_t number) { IMSA_HILOGI("InputMethodController::GetTextBeforeCursor"); if (!mTextString.empty()) { - if (mTextString.size() > mSelectNewEnd) { - return mTextString.substr(mSelectNewEnd); - } + int32_t endPos = (mSelectNewEnd+numberDispatchKeyEvent(data); } + + int32_t InputMethodController::GetEnterKeyType() + { + IMSA_HILOGI("InputMethodController::GetEnterKeyType"); + return enterKeyType_; + } + + int32_t InputMethodController::GetInputPattern() + { + IMSA_HILOGI("InputMethodController::GetInputPattern"); + return inputPattern_; + } } } \ No newline at end of file diff --git a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp index 4b8bd01..f004124 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -131,6 +131,25 @@ namespace MiscServices { Remote()->SendRequest(SET_CORE_AND_AGENT, data, reply, option); } + void InputMethodSystemAbilityProxy::HideCurrentInput(MessageParcel& data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput"); + MessageParcel reply; + MessageOption option; + + auto ret = Remote()->SendRequest(HIDE_CURRENT_INPUT, data, reply, option); + if (ret != NO_ERROR) { + IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput SendRequest failed"); + return; + } + + ret = reply.ReadInt32(); + if (ret != NO_ERROR) { + IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput reply failed"); + return; + } + } + int32_t InputMethodSystemAbilityProxy::Prepare(int32_t displayId, sptr &client, sptr &channel, InputAttribute &attribute) { diff --git a/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts b/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts index c8c5eac..99d5e07 100644 --- a/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts @@ -22,9 +22,12 @@ import {AsyncCallback} from './basic'; * @devices phone, tablet, tv, wearable */ declare namespace inputMethod { + const MAX_TYPE_NUM: number function getInputMethodSetting(): InputMethodSetting; + function getInputMethodController(): InputMethodController; + interface InputMethodSetting { listInputMethod(callback: AsyncCallback>): void; listInputMethod(): Promise>; @@ -33,6 +36,11 @@ declare namespace inputMethod { displayOptionalInputMethod(): Promise; } + interface InputMethodController { + stopInput(callback: AsyncCallback): void; + stopInput(): Promise; + } + interface InputMethodProperty { readonly packageName: string; readonly methodId: string; diff --git a/interfaces/kits/js/declaration/api/@ohos.inputmethodengine.d.ts b/interfaces/kits/js/declaration/api/@ohos.inputmethodengine.d.ts index a2b8561..2e15a56 100644 --- a/interfaces/kits/js/declaration/api/@ohos.inputmethodengine.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.inputmethodengine.d.ts @@ -18,38 +18,102 @@ import { AsyncCallback } from './basic'; /** * inputmethod * - * @since 7 + * @since 8 * @devices phone, tablet, tv, wearable */ declare namespace inputMethodEngine { - function on(type: 'keyboardShow', callback: () => void): void; - function off(type: 'keyboardShow', callback: () => void): void; + const ENTER_KEY_TYPE_UNSPECIFIED: number; + const ENTER_KEY_TYPE_GO: number; + const ENTER_KEY_TYPE_SEARCH: number; + const ENTER_KEY_TYPE_SEND: number; + const ENTER_KEY_TYPE_NEXT: number; + const ENTER_KEY_TYPE_DONE: number; + const ENTER_KEY_TYPE_PREVIOUS: number; - function on(type: 'keyboardHide', callback: () => void): void; - function off(type: 'keyboardHide', callback: () => void): void; + const PATTERN_NULL: number; + const PATTERN_TEXT: number; + const PATTERN_NUMBER: number; + const PATTERN_PHONE: number; + const PATTERN_DATETIME: number; + const PATTERN_EMAIL: number; + const PATTERN_URI: number; + const PATTERN_PASSWORD: number; - function InsertText(text: string, callback: AsyncCallback): void; - function InsertText(text: string): Promise; - - function DeleteForward(length: number, callback: () => void): void; - function DeleteForward(length: number): Promise; + const FLAG_SELECTING: number; + const FLAG_SINGLE_LINE: number; - function DeleteBackward(length: number, callback: () => void): void; - function DeleteBackward(length: number): Promise; + const DISPLAY_MODE_PART: number; + const DISPLAY_MODE_FULL: number; - function HideKeyboardSelf(callback: () => void): void; - function HideKeyboardSelf(): Promise; + const OPTION_ASCII: number; + const OPTION_NONE: number; + const OPTION_AUTO_CAP_CHARACTERS: number; + const OPTION_AUTO_CAP_SENTENCES: number; + const OPTION_AUTO_WORDS: number; + const OPTION_MULTI_LINE: number; + const OPTION_NO_FULLSCREEN: number; - function GetTextBeforeCursor(callback: () => string): void; - function GetTextBeforeCursor(): Promise; + function getInputMethodEngine(): InputMethodEngine; - function GetTextAfterCursor(callback: () => string): void; - function GetTextAfterCursor(): Promise; + function createKeyboardDelegate(): KeyboardDelegate; - function SendFunctionKey(callback: () => void): void; - function SendFunctionKey(): Promise; + interface KeyboardController { + hideKeyboard(callbakc: AsyncCallback): void; + hideKeyboard(): Promise; + } - const FUNCTION_KEY_CONFIRM: number; + interface InputMethodEngine { + on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void; + off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void; + + on(type: 'keyboardShow', callback: () => void): void; + off(type: 'keyboardShow', callback: () => void): void; + + on(type: 'keyboardHide', callback: () => void): void; + off(type: 'keyboardHide', callback: () => void): void; + } + + interface TextInputClient { + sendKeyFunction(action: number, callback: AsyncCallback): void; + sendKeyFunction(action: number): Promise; + + deleteForward(length: number, callback: AsyncCallback): void; + deleteForward(length: number): Promise; + + deleteBackward(length: number, callback: AsyncCallback): void; + deleteBackward(length: number): Promise; + + InsertText(text: string, callback: AsyncCallback): void; + InsertText(text: string): Promise; + + getForward(length: number, callback: AsyncCallback): void; + getForward(length: number): Promise; + + getEditorAttribute(lcallback: AsyncCallback): void; + getEditorAttribute(): Promise; + } + + interface KeyboardDelegate { + on(type: 'keyDown', callback: (event: KeyEvent) => boolean): void; + off(type: 'keyDown', callback?: (event: KeyEvent) => boolean): void; + + on(type: 'keyUp', callback: (event: KeyEvent) => boolean): void; + off(type: 'keyUp', callback?: (event: KeyEvent) => boolean): void; + + on(type: 'cursorContextChange', callback: (x: number, y: number, height: number) => void): void; + off(type: 'cursorContextChange', callback?: (x: number, y: number, height: number) => void): void; + + on(type: 'selectionChange', callback: (oldBegine: number, oldEnd: number, newBegine: number, newEnd: number) => void): void; + off(type: 'selectionChange', callback?: (oldBegine: number, oldEnd: number, newBegine: number, newEnd: number) => void): void; + + on(type: 'textChange', callback: (text: string) => void): void; + off(type: 'textChange', callback?: (text: string) => void): void; + } + + interface EditorAttribute { + readonly inputPattern: number; + readonly enterKeyType: number; + } } export default inputMethodEngine; diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/inputmethod/BUILD.gn similarity index 57% rename from interfaces/kits/js/napi/BUILD.gn rename to interfaces/kits/js/napi/inputmethod/BUILD.gn index eb1a433..21a0f7f 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/inputmethod/BUILD.gn @@ -13,16 +13,6 @@ import("//base/miscservices/inputmethod/inputmethod.gni") import("//build/ohos.gni") -config("inputmethodengine_native_config") { - visibility = [ ":*" ] - include_dirs = [ "include" ] -} - -config("inputmethodengine_native_public_config") { - visibility = [] - include_dirs = [ "include" ] -} - config("inputmethod_native_config") { visibility = [ ":*" ] include_dirs = [ "include" ] @@ -33,57 +23,16 @@ config("inputmethod_native_public_config") { include_dirs = [ "include" ] } -ohos_shared_library("inputmethodengine") { - sources = [ - "src/input_method_engine_module.cpp", - "src/js_input_method_engine.cpp", - "src/js_input_method_engine_listener.cpp", - ] - - configs = [ ":inputmethodengine_native_config" ] - - deps = [ - "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr", - "//base/miscservices/inputmethod/frameworks/inputmethod_ability:inputmethod_ability", - "//base/miscservices/inputmethod/services:inputmethod_service", - "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", - "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", - "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", - "//foundation/aafwk/standard/interfaces/innerkits/base:base", - "//foundation/aafwk/standard/interfaces/innerkits/intent:intent", - "//foundation/aafwk/standard/interfaces/innerkits/want:want", - "//foundation/ace/napi/:ace_napi", - "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", - "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", - "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", - "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", - "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - "//foundation/multimodalinput/input/frameworks/proxy:libmmi-client", - "//utils/native/base:utils", - ] - - external_deps = [ - "ability_runtime:runtime", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - ] - - public_configs = [ ":inputmethodengine_native_public_config" ] - - relative_install_dir = "module" - subsystem_name = "miscservices" - part_name = "inputmethod_native" -} - ohos_shared_library("inputmethod") { sources = [ "${inputmethod_path}/frameworks/inputmethod_controller/src/input_client_stub.cpp", "${inputmethod_path}/frameworks/inputmethod_controller/src/input_data_channel_stub.cpp", "${inputmethod_path}/frameworks/inputmethod_controller/src/input_method_controller.cpp", "src/input_method_module.cpp", - "src/js_input_method.cpp", + "src/js_input_method_controller.cpp", + "src/js_input_method_registry.cpp", + "src/js_input_method_setting.cpp", + "src/js_input_method_utils.cpp", ] configs = [ ":inputmethod_native_config" ] diff --git a/interfaces/kits/js/napi/inputmethod/include/js_input_method_controller.h b/interfaces/kits/js/napi/inputmethod/include/js_input_method_controller.h new file mode 100644 index 0000000..9ff62ba --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/include/js_input_method_controller.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_CONTROLLER_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_CONTROLLER_H +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "global.h" +#include "js_runtime_utils.h" +namespace OHOS { + namespace MiscServices { + class JsInputMethodController { + public: + JsInputMethodController() = default; + ~JsInputMethodController() = default; + + static void Finalizer(NativeEngine *engine, void *data, void *hint); + static NativeValue* StopInput(NativeEngine* engine, NativeCallbackInfo* info); + private: + NativeValue* OnStopInput(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_SETTING_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/include/js_input_method.h b/interfaces/kits/js/napi/inputmethod/include/js_input_method_registry.h similarity index 69% rename from interfaces/kits/js/napi/include/js_input_method.h rename to interfaces/kits/js/napi/inputmethod/include/js_input_method_registry.h index 0ed3184..3d089a7 100644 --- a/interfaces/kits/js/napi/include/js_input_method.h +++ b/interfaces/kits/js/napi/inputmethod/include/js_input_method_registry.h @@ -13,18 +13,20 @@ * limitations under the License. */ -#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H -#define OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_REGISTRY_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_REGISTRY_H #include "native_engine/native_engine.h" #include "native_engine/native_value.h" #include "global.h" #include "js_runtime_utils.h" +#include "js_input_method_utils.h" +#include "js_input_method_setting.h" namespace OHOS { namespace MiscServices { - NativeValue* JsInputMethodInit(NativeEngine* engine, NativeValue* exportObj); + NativeValue* JsInputMethodRegistryInit(NativeEngine* engine, NativeValue* exportObj); } } -#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H \ No newline at end of file +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_REGISTRY_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethod/include/js_input_method_setting.h b/interfaces/kits/js/napi/inputmethod/include/js_input_method_setting.h new file mode 100644 index 0000000..040fda6 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/include/js_input_method_setting.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_SETTING_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_SETTING_H +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "global.h" +#include "js_runtime_utils.h" +namespace OHOS { + namespace MiscServices { + class JsInputMethodSetting { + public: + JsInputMethodSetting() = default; + ~JsInputMethodSetting() = default; + + static void Finalizer(NativeEngine *engine, void *data, void *hint); + static NativeValue* DisplayOptionalInputMethod(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* ListInputMethod(NativeEngine* engine, NativeCallbackInfo* info); + private: + NativeValue* OnDisplayOptionalInputMethod(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnListInputMethod(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_SETTING_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/include/js_input_method_engine.h b/interfaces/kits/js/napi/inputmethod/include/js_input_method_utils.h similarity index 66% rename from interfaces/kits/js/napi/include/js_input_method_engine.h rename to interfaces/kits/js/napi/inputmethod/include/js_input_method_utils.h index 3f67d53..fa32354 100644 --- a/interfaces/kits/js/napi/include/js_input_method_engine.h +++ b/interfaces/kits/js/napi/inputmethod/include/js_input_method_utils.h @@ -13,18 +13,18 @@ * limitations under the License. */ -#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H -#define OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H - +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_UTILS_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_UTILS_H #include "native_engine/native_engine.h" #include "native_engine/native_value.h" #include "global.h" #include "js_runtime_utils.h" - +#include "js_input_method_setting.h" namespace OHOS { namespace MiscServices { - NativeValue* JsInputMethodEngineInit(NativeEngine* engine, NativeValue* exportObj); +// class JsInputMethodSetting; + NativeValue *CreateInputMethodSetting(NativeEngine& engine); + NativeValue *CreateInputMethodController(NativeEngine& engine); } } - -#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H \ No newline at end of file +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_SETTING_UTILS_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/input_method_module.cpp b/interfaces/kits/js/napi/inputmethod/src/input_method_module.cpp similarity index 87% rename from interfaces/kits/js/napi/src/input_method_module.cpp rename to interfaces/kits/js/napi/inputmethod/src/input_method_module.cpp index 50e65a8..5d9d1dc 100644 --- a/interfaces/kits/js/napi/src/input_method_module.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/input_method_module.cpp @@ -14,7 +14,7 @@ */ #include "native_engine/native_engine.h" -#include "js_input_method.h" +#include "js_input_method_registry.h" extern "C" __attribute__((constructor)) void NAPI_inputMethod_AutoRegister() { @@ -22,7 +22,7 @@ extern "C" __attribute__((constructor)) void NAPI_inputMethod_AutoRegister() NativeModule newModuleInfo = { .name = "inputMethod", .fileName = "inputmethod.so/inputmethod.js", - .registerCallback = OHOS::MiscServices::JsInputMethodInit, + .registerCallback = OHOS::MiscServices::JsInputMethodRegistryInit, }; moduleManager->Register(&newModuleInfo); diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp new file mode 100644 index 0000000..74aea75 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_input_method_controller.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_ZERO = 0; + + void JsInputMethodController::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsInputMethodController::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + NativeValue* JsInputMethodController::StopInput(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodController* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnStopInput(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodController::OnStopInput(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodController::OnStopInput is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodController::OnStopInput has params, return!"); + return engine.CreateUndefined(); + } + + InputMethodController::GetInstance()->HideCurrentInput(); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + + return result; + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp new file mode 100644 index 0000000..9cbe6a4 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_input_method_registry.h" +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "string_ex.h" +#include "event_handler.h" +#include "event_runner.h" +#include "input_method_controller.h" + +namespace OHOS { +namespace MiscServices { + namespace { + constexpr size_t ARGC_ZERO = 0; + constexpr int32_t MAX_TYPE_NUM = 128; + } + class JsInputMethodRegistry { + public: + explicit JsInputMethodRegistry(NativeEngine* engine) + { + IMSA_HILOGI("JsInputMethodRegistry::Constructor is called"); + } + + ~JsInputMethodRegistry() = default; + + static void Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsInputMethodRegistry::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + static NativeValue* GetInputMethodSetting(NativeEngine* engine, NativeCallbackInfo* info) + { + IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodSetting is called"); + JsInputMethodRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetInputMethodSetting(*engine, *info) : nullptr; + } + + static NativeValue* GetInputMethodController(NativeEngine* engine, NativeCallbackInfo* info) + { + IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodController is called"); + JsInputMethodRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetInputMethodController(*engine, *info) : nullptr; + } + + private: + NativeValue* OnGetInputMethodSetting(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodSetting is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodSetting Params not match"); + return engine.CreateUndefined(); + } + + return CreateInputMethodSetting(engine); + } + + NativeValue* OnGetInputMethodController(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodController is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodController Params not match"); + return engine.CreateUndefined(); + } + + return CreateInputMethodController(engine); + } + }; + + NativeValue* JsInputMethodRegistryInit(NativeEngine* engine, NativeValue* exportObj) + { + IMSA_HILOGI("JsInputMethodRegistryInit() is called!"); + + if (engine == nullptr || exportObj == nullptr) { + IMSA_HILOGI("engine or exportObj null"); + return nullptr; + } + + NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); + if (object == nullptr) { + IMSA_HILOGI("object null"); + return nullptr; + } + + std::unique_ptr jsInputMethodRegistry = std::make_unique(engine); + object->SetNativePointer(jsInputMethodRegistry.release(), JsInputMethodRegistry::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodSetting", + JsInputMethodRegistry::GetInputMethodSetting); + AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodController", + JsInputMethodRegistry::GetInputMethodController); + + object->SetProperty("MAX_TYPE_NUM", AbilityRuntime::CreateJsValue(*engine, static_cast(MAX_TYPE_NUM))); + + return engine->CreateUndefined(); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp new file mode 100644 index 0000000..5b50644 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_input_method_setting.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_ZERO = 0; + + void JsInputMethodSetting::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsInputMethodSetting::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + NativeValue* JsInputMethodSetting::DisplayOptionalInputMethod(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodSetting* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDisplayOptionalInputMethod(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodSetting::ListInputMethod(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodSetting* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnListInputMethod(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodSetting::OnDisplayOptionalInputMethod(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodSetting::OnDisplayOptionalInputMethod is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodSetting::OnDisplayOptionalInputMethod has params, return!"); + return engine.CreateUndefined(); + } + + InputMethodController::GetInstance()->DisplayOptionalInputMethod(); + + return engine.CreateUndefined(); + } + + NativeValue* JsInputMethodSetting::OnListInputMethod(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodSetting::ListInputMethod is called"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodSetting::ListInputMethod has params, return!"); + return engine.CreateUndefined(); + } + + std::vector properties = InputMethodController::GetInstance()->ListInputMethod(); + if (properties.size() == 0) { + IMSA_HILOGI("JsInputMethodSetting::ListInputMethod has no ime"); + return engine.CreateUndefined(); + } + NativeValue* arrayValue = engine.CreateArray(properties.size()); + NativeArray* array = AbilityRuntime::ConvertNativeValueTo(arrayValue); + uint32_t index = 0; + for (const auto &info : properties) { + NativeValue* objValue = engine.CreateObject(); + NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); + + IMSA_HILOGI("JsInputMethodSetting::ListInputMethod %{public}s/%{public}s", Str16ToStr8(info->mPackageName).c_str(), Str16ToStr8(info->mAbilityName).c_str()); + + object->SetProperty("packageName", AbilityRuntime::CreateJsValue(engine, Str16ToStr8(info->mPackageName))); + object->SetProperty("methodId", AbilityRuntime::CreateJsValue(engine, Str16ToStr8(info->mAbilityName))); + array->SetElement(index++, objValue); + } + + return arrayValue; + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp new file mode 100644 index 0000000..4a8be83 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_input_method_utils.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "js_input_method_setting.h" +#include "js_input_method_controller.h" +namespace OHOS { + namespace MiscServices { + NativeValue* CreateInputMethodSetting(NativeEngine& engine) + { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodSetting is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodSetting Failed to get object"); + return nullptr; + } + + std::unique_ptr jsInputMethodSetting = std::make_unique(); + object->SetNativePointer(jsInputMethodSetting.release(), JsInputMethodSetting::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "displayOptionalInputMethod", + JsInputMethodSetting::DisplayOptionalInputMethod); + AbilityRuntime::BindNativeFunction(engine, *object, "listInputMethod", JsInputMethodSetting::ListInputMethod); + return objValue; + } + + NativeValue* CreateInputMethodController(NativeEngine& engine) + { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodController is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodController Failed to get object"); + return nullptr; + } + + std::unique_ptr jsInputMethodController = std::make_unique(); + object->SetNativePointer(jsInputMethodController.release(), JsInputMethodController::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "stopInput", JsInputMethodController::StopInput); + return objValue; + } + } +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/BUILD.gn b/interfaces/kits/js/napi/inputmethodengine/BUILD.gn new file mode 100644 index 0000000..30e925f --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//base/miscservices/inputmethod/inputmethod.gni") +import("//build/ohos.gni") + +config("inputmethodengine_native_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +config("inputmethodengine_native_public_config") { + visibility = [] + include_dirs = [ "include" ] +} + +ohos_shared_library("inputmethodengine") { + sources = [ + "//base/miscservices/inputmethod/services/src/global.cpp", + "src/input_method_engine_module.cpp", + "src/js_input_method_engine.cpp", + "src/js_input_method_engine_listener.cpp", + "src/js_input_method_engine_registry.cpp", + "src/js_input_method_engine_utils.cpp", + "src/js_keyboard_controller.cpp", + "src/js_keyboard_delegate.cpp", + "src/js_text_input_client.cpp", + ] + + configs = [ ":inputmethodengine_native_config" ] + + deps = [ + "//base/global/resmgr_standard/frameworks/resmgr:global_resmgr", + "//base/miscservices/inputmethod/frameworks/inputmethod_ability:inputmethod_ability", + "//base/miscservices/inputmethod/services:inputmethod_service", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", + "//foundation/aafwk/standard/interfaces/innerkits/intent:intent", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/ace/napi/:ace_napi", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimodalinput/input/frameworks/proxy:libmmi-client", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_runtime:runtime", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + public_configs = [ ":inputmethodengine_native_public_config" ] + + relative_install_dir = "module" + subsystem_name = "miscservices" + part_name = "inputmethod_native" +} diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h b/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h new file mode 100644 index 0000000..1c00366 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_EDITOR_ATTRIBUTE_H +#define OHOS_MISCSERVICES_JS_EDITOR_ATTRIBUTE_H +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_runtime_utils.h" +namespace OHOS { + namespace MiscServices { + class JsEditorAttribute { + public: + JsEditorAttribute() = default; + ~JsEditorAttribute() = default; + static void Finalizer(NativeEngine* engine, void* data, void* hint); +// static NativeValue* GetEnterKeyType(NativeEngine* engine, NativeCallbackInfo* info); +// static NativeValue* GetInputPattern(NativeEngine* engine, NativeCallbackInfo* info); + private: +// NativeValue* OnGetEnterKeyType(NativeEngine& engine, NativeCallbackInfo& info); +// NativeValue* OnGetInputPattern(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_EDITOR_ATTRIBUTE_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h new file mode 100644 index 0000000..b13c1dc --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H + +#include "js_runtime_utils.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_input_method_engine_listener.h" + +namespace OHOS { + namespace MiscServices { + class JsInputMethodEngine { + public: + JsInputMethodEngine(NativeEngine* engine); + ~JsInputMethodEngine() = default; + static void Finalizer(NativeEngine* engine, void* data, void* hint); + static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); + private: + sptr imeListener_; + std::mutex mtx_; + NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/include/js_input_method_engine_listener.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h similarity index 79% rename from interfaces/kits/js/napi/include/js_input_method_engine_listener.h rename to interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h index e55f980..49dc778 100644 --- a/interfaces/kits/js/napi/include/js_input_method_engine_listener.h +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_listener.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef FM_IMMS_PROJECT_INPUTMETHODENGINELISTENER_H -#define FM_IMMS_PROJECT_INPUTMETHODENGINELISTENER_H +#ifndef FM_IMMS_PROJECT_JS_INPUT_METHOD_ENGINE_LISTENER_H +#define FM_IMMS_PROJECT_JS_INPUT_METHOD_ENGINE_LISTENER_H #include #include @@ -32,10 +32,7 @@ namespace MiscServices { void RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value); void UnregisterListenerWithType(std::string type, NativeValue* value); void OnKeyboardStatus(bool isShow); - bool OnKeyEvent(int32_t keyCode, int32_t keyStatus); - void OnCursorUpdate(int32_t positionX, int32_t positionY, int height); - void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd); - void OnTextChange(std::string text); + void OnInputStart(); void OnInputStop(std::string imeId); private: void AddCallback(std::string type, NativeValue* jsListenerObject); @@ -49,4 +46,4 @@ namespace MiscServices { }; } } -#endif // FM_IMMS_PROJECT_INPUTMETHODENGINELISTENER_H +#endif // FM_IMMS_PROJECT_JS_INPUT_METHOD_ENGINE_LISTENER_H diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_registry.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_registry.h new file mode 100644 index 0000000..337ca6f --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_registry.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_REGISTRY_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_REGISTRY_H + +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_runtime_utils.h" +#include "js_input_method_engine.h" +#include "js_keyboard_controller.h" +#include "js_text_input_client.h" +#include "js_keyboard_delegate.h" + +namespace OHOS { + namespace MiscServices { + NativeValue* JsInputMethodEngineRegistryInit(NativeEngine* engine, NativeValue* exportObj); + } +} + +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_REGISTRY_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_utils.h b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_utils.h new file mode 100644 index 0000000..9fea1cd --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_input_method_engine_utils.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_UTILS_H +#define OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_UTILS_H + +#include "js_runtime_utils.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_input_method_engine.h" +#include "js_keyboard_controller.h" +#include "js_text_input_client.h" +#include "js_keyboard_delegate.h" +#include "js_keyboard_delegate.h" + +namespace OHOS { + namespace MiscServices { + NativeValue *CreateInputMethodEngine(NativeEngine &engine); + NativeValue *CreateKeyboardController(NativeEngine& engine); + NativeValue *CreateTextInputClient(NativeEngine& engine); + NativeValue *CreateKeyboardDelegate(NativeEngine& engine); + NativeValue *CreateEditorAttribute(NativeEngine& engine); + } +} +#endif // OHOS_MISCSERVICES_JS_INPUT_METHOD_ENGINE_UTILS_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_controller.h b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_controller.h new file mode 100644 index 0000000..d9cd700 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_controller.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_KEYBOARD_CONTROLLER_H +#define OHOS_MISCSERVICES_JS_KEYBOARD_CONTROLLER_H +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_runtime_utils.h" +namespace OHOS { + namespace MiscServices { + class JsKeyboardController { + public: + JsKeyboardController() = default; + ~JsKeyboardController() = default; + static void Finalizer(NativeEngine* engine, void* data, void* hint); + static NativeValue* HideKeyboardSelf(NativeEngine* engine, NativeCallbackInfo* info); + private: + NativeValue* OnHideKeyboardSelf(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_KEYBOARD_CONTROLLER_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate.h b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate.h new file mode 100644 index 0000000..388f084 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_KEYBOARD_DELEGATE_H +#define OHOS_MISCSERVICES_JS_KEYBOARD_DELEGATE_H + +#include "js_runtime_utils.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_keyboard_delegate_listener.h" +#include "event_handler.h" +#include "event_runner.h" + +namespace OHOS { + namespace MiscServices { + class JsKeyboardDelegate { + public: + JsKeyboardDelegate(NativeEngine* engine); + ~JsKeyboardDelegate() = default; + static void Finalizer(NativeEngine* engine, void* data, void* hint); + static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); + private: + sptr kdListener_; + std::mutex mtx_; + NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); + std::shared_ptr GetMainHandler(); + std::shared_ptr mainHandler_ = nullptr; + }; + } +} +#endif // OHOS_MISCSERVICES_JS_KEYBOARD_DELEGATE_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate_listener.h b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate_listener.h new file mode 100644 index 0000000..8e62c92 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_keyboard_delegate_listener.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FM_IMMS_PROJECT_JS_KEYBOARD_DELEGATE_LISTENER_H +#define FM_IMMS_PROJECT_JS_KEYBOARD_DELEGATE_LISTENER_H + +#include +#include +#include +#include +#include +#include +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "event_handler.h" +#include "event_runner.h" +namespace OHOS { +namespace MiscServices { + class JsKeyboardDelegateListener : virtual public RefBase { + public: + explicit JsKeyboardDelegateListener(NativeEngine* engine) : engine_(engine) {} + JsKeyboardDelegateListener(NativeEngine* engine, std::shared_ptr &handler) + : engine_(engine), mainHandler_(handler) {} + virtual ~JsKeyboardDelegateListener() = default; + void RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value); + void UnregisterListenerWithType(std::string type, NativeValue* value); + bool OnKeyEvent(int32_t keyCode, int32_t keyStatus); + void OnCursorUpdate(int32_t positionX, int32_t positionY, int height); + void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd); + void OnTextChange(std::string text); + private: + void AddCallback(std::string type, NativeValue* jsListenerObject); + void CallJsMethod(std::string methodName, NativeValue* const* argv = nullptr, size_t argc = 0); + bool CallJsMethodReturnBool(std::string methodName, NativeValue* const* argv = nullptr, size_t argc = 0); + void RemoveCallback(NativeValue* jsListenerObject); + bool IfCallbackRegistered(std::string type, NativeValue* jsListenerObject); + NativeEngine* engine_ = nullptr; + std::mutex mMutex; + std::map>> jsCbMap_; + std::shared_ptr mainHandler_ = nullptr; + }; +} +} +#endif // FM_IMMS_PROJECT_JS_KEYBOARD_DELEGATE_LISTENER_H diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_text_input_client.h b/interfaces/kits/js/napi/inputmethodengine/include/js_text_input_client.h new file mode 100644 index 0000000..e14701a --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_text_input_client.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MISCSERVICES_JS_TEXT_INPUT_CLIENT_H +#define OHOS_MISCSERVICES_JS_TEXT_INPUT_CLIENT_H +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "js_runtime_utils.h" +namespace OHOS { + namespace MiscServices { + class JsTextInputClient { + public: + JsTextInputClient() = default; + ~JsTextInputClient() = default; + static void Finalizer(NativeEngine* engine, void* data, void* hint); + static NativeValue* InsertText(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* DeleteForward(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* DeleteBackward(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* SendFunctionKey(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* GetForward(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* GetBackward(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* GetEditorAttribute(NativeEngine* engine, NativeCallbackInfo* info); + private: + NativeValue* OnInsertText(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnDeleteForward(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnDeleteBackward(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnSendFunctionKey(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnGetForward(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnGetBackward(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnGetEditorAttribute(NativeEngine& engine, NativeCallbackInfo& info); + }; + } +} +#endif // OHOS_MISCSERVICES_JS_TEXT_INPUT_CLIENT_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/input_method_engine_module.cpp b/interfaces/kits/js/napi/inputmethodengine/src/input_method_engine_module.cpp similarity index 86% rename from interfaces/kits/js/napi/src/input_method_engine_module.cpp rename to interfaces/kits/js/napi/inputmethodengine/src/input_method_engine_module.cpp index a46c15d..da577f5 100644 --- a/interfaces/kits/js/napi/src/input_method_engine_module.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/input_method_engine_module.cpp @@ -14,7 +14,7 @@ */ #include "native_engine/native_engine.h" -#include "js_input_method_engine.h" +#include "js_input_method_engine_registry.h" extern "C" __attribute__((constructor)) void NAPI_inputMethodEngine_AutoRegister() { @@ -22,7 +22,7 @@ extern "C" __attribute__((constructor)) void NAPI_inputMethodEngine_AutoRegister NativeModule newModuleInfo = { .name = "inputMethodEngine", .fileName = "inputmethodengine.so/inputmethod_engine.js", - .registerCallback = OHOS::MiscServices::JsInputMethodEngineInit, + .registerCallback = OHOS::MiscServices::JsInputMethodEngineRegistryInit, }; moduleManager->Register(&newModuleInfo); diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp new file mode 100644 index 0000000..b1536c1 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_editor_attribute.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "global.h" +#include "input_method_ability.h" +namespace OHOS { +namespace MiscServices { +// constexpr size_t ARGC_ZERO = 0; + + void JsEditorAttribute::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsEditorAttribute::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + +// NativeValue* JsEditorAttribute::GetEnterKeyType(NativeEngine* engine, NativeCallbackInfo* info) +// { +// JsEditorAttribute* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); +// return (me != nullptr) ? me->OnGetEnterKeyType(*engine, *info) : nullptr; +// } +// +// NativeValue* JsEditorAttribute::GetInputPattern(NativeEngine* engine, NativeCallbackInfo* info) +// { +// JsEditorAttribute* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); +// return (me != nullptr) ? me->OnGetInputPattern(*engine, *info) : nullptr; +// } + +// NativeValue* JsEditorAttribute::OnGetEnterKeyType(NativeEngine& engine, NativeCallbackInfo& info) +// { +// IMSA_HILOGI("JsEditorAttribute::OnGetEnterKeyType is called!"); +// if (info.argc != ARGC_ZERO) { +// IMSA_HILOGI("JsEditorAttribute::OnGetEnterKeyType has params!"); +// return engine.CreateUndefined(); +// } +// +// int32_t ret = InputMethodAbility::GetInstance()->GetEnterKeyType(); +// +// NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); +// +// return result; +// } +// +// NativeValue* JsEditorAttribute::OnGetInputPattern(NativeEngine& engine, NativeCallbackInfo& info) +// { +// IMSA_HILOGI("JsEditorAttribute::OnGetInputPattern is called!"); +// if (info.argc != ARGC_ZERO) { +// IMSA_HILOGI("JsEditorAttribute::OnGetInputPattern has params!"); +// return engine.CreateUndefined(); +// } +// +// int32_t ret = InputMethodAbility::GetInstance()->GetInputPattern(); +// +// NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); +// +// return result; +// } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp new file mode 100644 index 0000000..6d3fb5d --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_input_method_engine.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "js_input_method_engine_listener.h" +#include "input_method_ability.h" +#include "global.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_TWO = 2; + JsInputMethodEngine::JsInputMethodEngine(NativeEngine* engine) + { + IMSA_HILOGI("JsInputMethodEngine::Constructor is called"); + imeListener_ = new JsInputMethodEngineListener(engine); + InputMethodAbility::GetInstance()->setImeListener(imeListener_); + } + + void JsInputMethodEngine::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsInputMethodEngine::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + NativeValue* JsInputMethodEngine::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodEngine::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodEngine::OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback is called!"); + if (info.argc != ARGC_TWO) { + IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback Params not match"); + return engine.CreateUndefined(); + } + + std::string cbType; + if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback Failed to convert parameter to callbackType"); + return engine.CreateUndefined(); + } + + NativeValue* value = info.argv[1]; + imeListener_->RegisterListenerWithType(engine, cbType, value); + return engine.CreateUndefined(); + } + + NativeValue* JsInputMethodEngine::OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback is called!"); + if (info.argc == 0) { + IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback Params not match"); + return engine.CreateUndefined(); + } + + std::string cbType; + if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback Failed to convert parameter to callbackType"); + return engine.CreateUndefined(); + } + + std::lock_guard lock(mtx_); + NativeValue* value = info.argv[1]; + if (!value->IsCallable()) { + IMSA_HILOGI("JsInputMethodEngine::OnUnregisterWindowManagerCallback info->argv[1] is not callable"); + return engine.CreateUndefined(); + } + imeListener_->UnregisterListenerWithType(cbType, value); + return engine.CreateUndefined(); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/js_input_method_engine_listener.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp similarity index 54% rename from interfaces/kits/js/napi/src/js_input_method_engine_listener.cpp rename to interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp index 93cdffc..3eaadd9 100644 --- a/interfaces/kits/js/napi/src/js_input_method_engine_listener.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp @@ -15,6 +15,7 @@ #include "js_input_method_engine_listener.h" #include "global.h" #include "js_runtime_utils.h" +#include "js_input_method_engine_utils.h" namespace OHOS { namespace MiscServices { @@ -147,121 +148,30 @@ namespace MiscServices { CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); } - bool JsInputMethodEngineListener::OnKeyEvent(int32_t keyCode, int32_t keyStatus) + void JsInputMethodEngineListener::OnInputStart() { std::lock_guard lock(mMutex); - IMSA_HILOGI("JsInputMethodEngineListener::OnKeyEvent"); + IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart"); +// NativeValue *nativeValuekb = engine_->CreateObject(); +// NativeObject *objectkb = AbilityRuntime::ConvertNativeValueTo(nativeValuekb); +// if (objectkb == nullptr) { +// IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart Failed to get object"); +// return; +// } - NativeValue* nativeValue = engine_->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); - if (object == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return false; - } - NativeValue* argv[] = {nativeValue}; - std::string methodName; - if (keyStatus == 2) { - methodName = "keyDown"; - } else { - methodName = "keyUp"; - } - object->SetProperty("keyCode", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyCode))); - object->SetProperty("keyAction", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyStatus))); - return CallJsMethodReturnBool(methodName, argv, AbilityRuntime::ArraySize(argv)); - } + NativeValue *nativeValuekb = CreateKeyboardController(*engine_); +// objectkb->SetProperty("kbController", CreateKeyboardController(*engine_)); - void JsInputMethodEngineListener::OnCursorUpdate(int32_t positionX, int32_t positionY, int height) - { - std::lock_guard lock(mMutex); - IMSA_HILOGI("JsInputMethodEngineListener::OnCursorUpdate"); - - NativeValue* nativeXValue = engine_->CreateObject(); - NativeObject* objectX = AbilityRuntime::ConvertNativeValueTo(nativeXValue); - if (objectX == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectX->SetProperty("x", AbilityRuntime::CreateJsValue(*engine_, static_cast(positionX))); - - NativeValue* nativeYValue = engine_->CreateObject(); - NativeObject* objectY = AbilityRuntime::ConvertNativeValueTo(nativeYValue); - if (objectY == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectY->SetProperty("y", AbilityRuntime::CreateJsValue(*engine_, static_cast(positionY))); - - NativeValue* nativeHValue = engine_->CreateObject(); - NativeObject* objectH = AbilityRuntime::ConvertNativeValueTo(nativeHValue); - if (objectH == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectH->SetProperty("height", AbilityRuntime::CreateJsValue(*engine_, static_cast(height))); - - NativeValue* argv[] = {nativeXValue, nativeYValue, nativeHValue}; - std::string methodName = "cursorContextChange"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); - } - - void JsInputMethodEngineListener::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, - int32_t newBegin, int32_t newEnd) - { - std::lock_guard lock(mMutex); - IMSA_HILOGI("JsInputMethodEngineListener::OnSelectionChange"); - - NativeValue* nativeOBValue = engine_->CreateObject(); - NativeObject* objectOB = AbilityRuntime::ConvertNativeValueTo(nativeOBValue); - if (objectOB == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectOB->SetProperty("oldBegin", AbilityRuntime::CreateJsValue(*engine_, static_cast(oldBegin))); - - NativeValue* nativeOEValue = engine_->CreateObject(); - NativeObject* objectOE = AbilityRuntime::ConvertNativeValueTo(nativeOEValue); - if (objectOE == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectOE->SetProperty("oldEnd", AbilityRuntime::CreateJsValue(*engine_, static_cast(oldEnd))); - - NativeValue* nativeNBHValue = engine_->CreateObject(); - NativeObject* objectNB = AbilityRuntime::ConvertNativeValueTo(nativeNBHValue); - if (objectNB == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectNB->SetProperty("newBegin", AbilityRuntime::CreateJsValue(*engine_, static_cast(newBegin))); - - NativeValue* nativeNEValue = engine_->CreateObject(); - NativeObject* objectNE = AbilityRuntime::ConvertNativeValueTo(nativeNEValue); - if (objectNE == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - objectNE->SetProperty("newEnd", AbilityRuntime::CreateJsValue(*engine_, static_cast(newEnd))); - - NativeValue* argv[] = {nativeOEValue, nativeOEValue, nativeNBHValue, nativeNEValue}; - std::string methodName = "selectionChange"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); - } - - void JsInputMethodEngineListener::OnTextChange(std::string text) - { - std::lock_guard lock(mMutex); - IMSA_HILOGI("JsInputMethodEngineListener::OnTextChange"); - - NativeValue* nativeValue = engine_->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); - if (object == nullptr) { - IMSA_HILOGI("Failed to convert rect to jsObject"); - return; - } - object->SetProperty("text", AbilityRuntime::CreateJsValue(*engine_, text)); - - NativeValue* argv[] = {nativeValue}; - std::string methodName = "textChange"; +// NativeValue *nativeValuetx = engine_->CreateObject(); +// NativeObject *objecttx = AbilityRuntime::ConvertNativeValueTo(nativeValuetx); +// if (objecttx == nullptr) { +// IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart Failed to get object"); +// return; +// } +// objecttx->SetProperty("textInputClient", CreateTextInputClient(*engine_)); + NativeValue *nativeValuetx = CreateTextInputClient(*engine_); + NativeValue* argv[] = {nativeValuekb, nativeValuetx}; + std::string methodName = "inputStart"; CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp new file mode 100644 index 0000000..09ab344 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_input_method_engine_registry.h" +#include +#include "js_input_method_engine_listener.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "string_ex.h" +#include "input_method_utils.h" +#include "input_method_ability.h" +#include "js_input_method_engine_utils.h" + +namespace OHOS { +namespace MiscServices { + namespace { + constexpr size_t ARGC_ZERO = 0; + constexpr size_t ARGC_ONE = 1; + } + class JsInputMethodEngineRegistry { + public: + explicit JsInputMethodEngineRegistry(NativeEngine* engine) + { + IMSA_HILOGI("JsInputMethodEngineRegistry::Constructor is called"); + } + + ~JsInputMethodEngineRegistry() = default; + + static void Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsInputMethodEngineRegistry::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + static NativeValue* GetInputMethodEngine(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetInputMethodEngine(*engine, *info) : nullptr; + } + + static NativeValue* GetKeyboardDelegate(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetKeyboardDelegate(*engine, *info) : nullptr; + } + + static NativeValue* MoveCursor(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnMoveCursor(*engine, *info) : nullptr; + } + private: + NativeValue* OnGetInputMethodEngine(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnGetInputMethodEngine is called!"); + if (info.argc > ARGC_ONE) { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnGetInputMethodEngine Params not match"); + return engine.CreateUndefined(); + } + + return CreateInputMethodEngine(engine); + } + + NativeValue* OnGetKeyboardDelegate(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnGetKeyboardDelegate is called!"); + if (info.argc > ARGC_ONE) { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnGetKeyboardDelegate Params not match"); + return engine.CreateUndefined(); + } + + return CreateKeyboardDelegate(engine); + } + + NativeValue* OnMoveCursor(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnMoveCursor is called!"); + if (info.argc < ARGC_ONE) { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnMoveCursor has no params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnMoveCursor Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->MoveCursor(number); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + + return result; + } + }; + + NativeValue* JsInputMethodEngineRegistryInit(NativeEngine* engine, NativeValue* exportObj) + { + IMSA_HILOGI("JsInputMethodEngineRegistryInit() is called!"); + + if (engine == nullptr || exportObj == nullptr) { + IMSA_HILOGI("engine or exportObj null"); + return nullptr; + } + + NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); + if (object == nullptr) { + IMSA_HILOGI("object null"); + return nullptr; + } + + std::unique_ptr jsInputMethodEngineRegistry = std::make_unique(engine); + object->SetNativePointer(jsInputMethodEngineRegistry.release(), JsInputMethodEngineRegistry::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodEngine", JsInputMethodEngineRegistry::GetInputMethodEngine); + AbilityRuntime::BindNativeFunction(*engine, *object, "createKeyboardDelegate", JsInputMethodEngineRegistry::GetKeyboardDelegate); + + object->SetProperty("ENTER_KEY_TYPE_UNSPECIFIED", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::UNSPECIFIED))); + object->SetProperty("ENTER_KEY_TYPE_GO", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::GO))); + object->SetProperty("ENTER_KEY_TYPE_SEARCH", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::SEARCH))); + object->SetProperty("ENTER_KEY_TYPE_SEND", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::SEND))); + object->SetProperty("ENTER_KEY_TYPE_NEXT", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::NEXT))); + object->SetProperty("ENTER_KEY_TYPE_DONE", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::DONE))); + object->SetProperty("ENTER_KEY_TYPE_PREVIOUS", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::PREVIOUS))); + + object->SetProperty("PATTERN_NULL", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::NONE))); + object->SetProperty("PATTERN_TEXT", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::TEXT))); + object->SetProperty("PATTERN_NUMBER", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::NUMBER))); + object->SetProperty("PATTERN_PHONE", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::PHONE))); + object->SetProperty("PATTERN_DATETIME", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::DATETIME))); + object->SetProperty("PATTERN_EMAIL", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::EMAIL_ADDRESS))); + object->SetProperty("PATTERN_URI", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::URL))); + object->SetProperty("PATTERN_PASSWORD", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::VISIBLE_PASSWORD))); + + + object->SetProperty("FLAG_SELECTING", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); + object->SetProperty("FLAG_SINGLE_LINE", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + + object->SetProperty("DISPLAY_MODE_PART", AbilityRuntime::CreateJsValue(*engine, static_cast(0))); + object->SetProperty("DISPLAY_MODE_FULL", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + + object->SetProperty("OPTION_ASCII", AbilityRuntime::CreateJsValue(*engine, static_cast(20))); + object->SetProperty("OPTION_NONE", AbilityRuntime::CreateJsValue(*engine, static_cast(0))); + object->SetProperty("OPTION_AUTO_CAP_CHARACTERS", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); + object->SetProperty("OPTION_AUTO_CAP_SENTENCES", AbilityRuntime::CreateJsValue(*engine, static_cast(8))); + object->SetProperty("OPTION_AUTO_WORDS", AbilityRuntime::CreateJsValue(*engine, static_cast(4))); + object->SetProperty("OPTION_MULTI_LINE", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + object->SetProperty("OPTION_NO_FULLSCREEN", AbilityRuntime::CreateJsValue(*engine, static_cast(10))); + + + + AbilityRuntime::BindNativeFunction(*engine, *object, "MoveCursor", JsInputMethodEngineRegistry::MoveCursor); + + object->SetProperty("FUNCTION_KEY_CONFIRM", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + object->SetProperty("CURSOR_UP", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + object->SetProperty("CURSOR_DOWN", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); + object->SetProperty("CURSOR_LEFT", AbilityRuntime::CreateJsValue(*engine, static_cast(3))); + object->SetProperty("CURSOR_RIGHT", AbilityRuntime::CreateJsValue(*engine, static_cast(4))); + return engine->CreateUndefined(); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp new file mode 100644 index 0000000..ab3e342 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_input_method_engine_utils.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "js_input_method_engine_listener.h" +#include "input_method_ability.h" +#include "global.h" +#include "js_input_method_engine.h" +#include "js_editor_attribute.h" +namespace OHOS { + namespace MiscServices { + NativeValue* CreateInputMethodEngine(NativeEngine &engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateInputMethodEngine is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateInputMethodEngine Failed to get object"); + return nullptr; + } + + std::unique_ptr jsInputMethodEngine = std::make_unique(&engine); + object->SetNativePointer(jsInputMethodEngine.release(), JsInputMethodEngine::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "on", JsInputMethodEngine::RegisterCallback); + AbilityRuntime::BindNativeFunction(engine, *object, "off", JsInputMethodEngine::UnRegisterCallback); + return objValue; + } + + NativeValue* CreateKeyboardController(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardController is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("CreateKeyboardController Failed to get object"); + return nullptr; + } + + std::unique_ptr jsKeyboardController = std::make_unique(); + object->SetNativePointer(jsKeyboardController.release(), JsKeyboardController::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "hideKeyboard", JsKeyboardController::HideKeyboardSelf); + + return objValue; + } + + NativeValue* CreateTextInputClient(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateTextInputClient is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("CreateTextInputClient Failed to get object"); + return nullptr; + } + + std::unique_ptr jsTextInputClient = std::make_unique(); + object->SetNativePointer(jsTextInputClient.release(), JsTextInputClient::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "insertText", JsTextInputClient::InsertText); + AbilityRuntime::BindNativeFunction(engine, *object, "deleteForward", JsTextInputClient::DeleteForward); + AbilityRuntime::BindNativeFunction(engine, *object, "deleteBackward", JsTextInputClient::DeleteBackward); + AbilityRuntime::BindNativeFunction(engine, *object, "sendKeyFunction", JsTextInputClient::SendFunctionKey); + AbilityRuntime::BindNativeFunction(engine, *object, "getForward", JsTextInputClient::GetForward); + AbilityRuntime::BindNativeFunction(engine, *object, "getBackward", JsTextInputClient::GetBackward); + AbilityRuntime::BindNativeFunction(engine, *object, "getEditorAttribute", JsTextInputClient::GetEditorAttribute); + return objValue; + } + + NativeValue* CreateKeyboardDelegate(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardDelegate is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("CreateKeyboardDelegate Failed to get object"); + return nullptr; + } + + std::unique_ptr jsKeyboardDelegate = std::make_unique(&engine); + object->SetNativePointer(jsKeyboardDelegate.release(), JsKeyboardDelegate::Finalizer, nullptr); + + AbilityRuntime::BindNativeFunction(engine, *object, "on", JsKeyboardDelegate::RegisterCallback); + AbilityRuntime::BindNativeFunction(engine, *object, "off", JsKeyboardDelegate::UnRegisterCallback); + return objValue; + } + + NativeValue* CreateEditorAttribute(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateEditorAttribute is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + if (object == nullptr) { + IMSA_HILOGI("CreateEditorAttribute Failed to get object"); + return nullptr; + } + + std::unique_ptr jsEditorAttribute = std::make_unique(); + object->SetNativePointer(jsEditorAttribute.release(), JsEditorAttribute::Finalizer, nullptr); + + object->SetProperty("enterKeyType", AbilityRuntime::CreateJsValue(engine, InputMethodAbility::GetInstance()->GetEnterKeyType())); + object->SetProperty("inputPattern", AbilityRuntime::CreateJsValue(engine, InputMethodAbility::GetInstance()->GetInputPattern())); + + return objValue; + } + } +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp new file mode 100644 index 0000000..2dfc9b4 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_keyboard_controller.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "input_method_ability.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_ZERO = 0; + void JsKeyboardController::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsKeyboardController::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + NativeValue* JsKeyboardController::HideKeyboardSelf(NativeEngine* engine, NativeCallbackInfo* info) + { + JsKeyboardController* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnHideKeyboardSelf(*engine, *info) : nullptr; + } + + NativeValue* JsKeyboardController::OnHideKeyboardSelf(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsKeyboardController::OnHideKeyboardSelf is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsKeyboardController::OnHideKeyboardSelf has params, return!"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->HideKeyboardSelf(); + + return engine.CreateUndefined(); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp new file mode 100644 index 0000000..71d64a2 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_keyboard_delegate.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "js_keyboard_delegate_listener.h" +#include "input_method_ability.h" +#include "global.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_TWO = 2; + JsKeyboardDelegate::JsKeyboardDelegate(NativeEngine* engine) + { + IMSA_HILOGI("JsKeyboardDelegate::Constructor is called"); + auto mainHandler = GetMainHandler(); + kdListener_ = new JsKeyboardDelegateListener(engine, mainHandler); + InputMethodAbility::GetInstance()->setKdListener(kdListener_); + } + + void JsKeyboardDelegate::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsKeyboardDelegate::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + std::shared_ptr JsKeyboardDelegate::GetMainHandler() + { + if (!mainHandler_) { + mainHandler_ = + std::make_shared(AppExecFwk::EventRunner::GetMainEventRunner()); + } + return mainHandler_; + } + + NativeValue* JsKeyboardDelegate::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsKeyboardDelegate* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsKeyboardDelegate::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsKeyboardDelegate* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsKeyboardDelegate::OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback is called!"); + if (info.argc != ARGC_TWO) { + IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback Params not match"); + return engine.CreateUndefined(); + } + + std::string cbType; + if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback Failed to convert parameter to callbackType"); + return engine.CreateUndefined(); + } + + NativeValue* value = info.argv[1]; + kdListener_->RegisterListenerWithType(engine, cbType, value); + return engine.CreateUndefined(); + } + + NativeValue* JsKeyboardDelegate::OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback is called!"); + if (info.argc == 0) { + IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback Params not match"); + return engine.CreateUndefined(); + } + + std::string cbType; + if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback Failed to convert parameter to callbackType"); + return engine.CreateUndefined(); + } + + std::lock_guard lock(mtx_); + NativeValue* value = info.argv[1]; + if (!value->IsCallable()) { + IMSA_HILOGI("JsKeyboardDelegate::OnUnregisterWindowManagerCallback info->argv[1] is not callable"); + return engine.CreateUndefined(); + } + kdListener_->UnregisterListenerWithType(cbType, value); + return engine.CreateUndefined(); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp new file mode 100644 index 0000000..d1b57c2 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_keyboard_delegate_listener.h" +#include "global.h" +#include "js_runtime_utils.h" +#include "js_input_method_engine_utils.h" + +namespace OHOS { +namespace MiscServices { + void JsKeyboardDelegateListener::RegisterListenerWithType(NativeEngine& engine, + std::string type, NativeValue* value) + { + // should do type check + if (IfCallbackRegistered(type, value)) { + IMSA_HILOGI("JsKeyboardDelegateListener::RegisterListenerWithType callback already registered!"); + return; + } + std::unique_ptr callbackRef; + callbackRef.reset(engine.CreateReference(value, 1)); + + AddCallback(type, value); + } + + void JsKeyboardDelegateListener::AddCallback(std::string type, NativeValue* jsListenerObject) + { + IMSA_HILOGI("JsKeyboardDelegateListener::AddCallback is called"); + std::lock_guard lock(mMutex); + std::unique_ptr callbackRef; + callbackRef.reset(engine_->CreateReference(jsListenerObject, 1)); + jsCbMap_[type].push_back(std::move(callbackRef)); + IMSA_HILOGI("JsKeyboardDelegateListener::AddCallback success"); + IMSA_HILOGI("jsCbMap_ size: %{public}d, and type[%{public}s] size: %{public}d!", + static_cast(jsCbMap_.size()), type.c_str(), static_cast(jsCbMap_[type].size())); + return; + } + + void JsKeyboardDelegateListener::UnregisterListenerWithType(std::string type, NativeValue* value) + { + IMSA_HILOGI("JsKeyboardDelegateListener::UnregisterListenerWithType"); + // should do type check + if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { + IMSA_HILOGI("methodName %{public}s not registerted!", type.c_str()); + return; + } + for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) { + if (value->StrictEquals((*it)->Get())) { + jsCbMap_[type].erase(it); + break; + } + } + // one type with multi jscallback, erase type when there is no callback in one type + if (jsCbMap_[type].empty()) { + jsCbMap_.erase(type); + } + return; + } + + bool JsKeyboardDelegateListener::IfCallbackRegistered(std::string type, NativeValue* jsListenerObject) + { + IMSA_HILOGI("JsKeyboardDelegateListener::IfCallbackRegistered"); + if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { + IMSA_HILOGI("methodName %{public}s not registertd!", type.c_str()); + return false; + } + + for (auto iter = jsCbMap_[type].begin(); iter != jsCbMap_[type].end(); iter++) { + if (jsListenerObject->StrictEquals((*iter)->Get())) { + IMSA_HILOGI("JsKeyboardDelegateListener::IfCallbackRegistered callback already registered!"); + return true; + } + } + return false; + } + + void JsKeyboardDelegateListener::CallJsMethod(std::string methodName, NativeValue* const* argv, size_t argc) + { + IMSA_HILOGI("JsKeyboardDelegateListener::CallJsMethod"); + if (engine_ == nullptr) { + IMSA_HILOGI("engine_ nullptr"); + return; + } + + if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { + IMSA_HILOGI("methodName %{public}s not registertd!", methodName.c_str()); + return; + } + + for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { + engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); + } + } + + bool JsKeyboardDelegateListener::CallJsMethodReturnBool(std::string methodName, + NativeValue* const* argv, size_t argc) + { + IMSA_HILOGI("JsKeyboardDelegateListener::CallJsMethodReturnBool"); + if (engine_ == nullptr) { + IMSA_HILOGI("engine_ nullptr"); + return false; + } + + if (jsCbMap_.empty() || jsCbMap_.find(methodName) == jsCbMap_.end()) { + IMSA_HILOGI("methodName %{public}s not registertd!", methodName.c_str()); + return false; + } + + bool result = false; + for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { + NativeValue* nativeValue = engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); + bool ret = false; + if (AbilityRuntime::ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { + result = true; + } + } + return result; + } + + bool JsKeyboardDelegateListener::OnKeyEvent(int32_t keyCode, int32_t keyStatus) + { + std::lock_guard lock(mMutex); + IMSA_HILOGI("JsKeyboardDelegateListener::OnKeyEvent"); + + NativeValue* nativeValue = engine_->CreateObject(); + NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); + if (object == nullptr) { + IMSA_HILOGI("Failed to convert rect to jsObject"); + return false; + } + NativeValue* argv[] = {nativeValue}; + std::string methodName; + if (keyStatus == 2) { + methodName = "keyDown"; + } else { + methodName = "keyUp"; + } + object->SetProperty("keyCode", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyCode))); + object->SetProperty("keyAction", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyStatus))); + return CallJsMethodReturnBool(methodName, argv, AbilityRuntime::ArraySize(argv)); + } + + void JsKeyboardDelegateListener::OnCursorUpdate(int32_t positionX, int32_t positionY, int height) + { + std::lock_guard lock(mMutex); + IMSA_HILOGI("JsKeyboardDelegateListener::OnCursorUpdate"); + + auto task = [this, positionX, positionY, height] () { + NativeValue* nativeXValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(positionX)); + NativeValue* nativeYValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(positionY)); + NativeValue* nativeHValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(height)); + + NativeValue* argv[] = {nativeXValue, nativeYValue, nativeHValue}; + std::string methodName = "cursorContextChange"; + CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + }; + mainHandler_->PostTask(task); + } + + void JsKeyboardDelegateListener::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, + int32_t newBegin, int32_t newEnd) + { + std::lock_guard lock(mMutex); + IMSA_HILOGI("JsKeyboardDelegateListener::OnSelectionChange"); + + auto task = [this, oldBegin, oldEnd, newBegin, newEnd] () { + NativeValue* nativeOBValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(oldBegin)); + NativeValue* nativeOEValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(oldEnd)); + NativeValue* nativeNBHValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(newBegin)); + NativeValue* nativeNEValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(newEnd)); + + NativeValue* argv[] = {nativeOBValue, nativeOEValue, nativeNBHValue, nativeNEValue}; + std::string methodName = "selectionChange"; + CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + }; + + mainHandler_->PostTask(task); + } + + void JsKeyboardDelegateListener::OnTextChange(std::string text) + { + std::lock_guard lock(mMutex); + IMSA_HILOGI("JsKeyboardDelegateListener::OnTextChange"); + auto task = [this, text] () { + NativeValue* nativeValue = AbilityRuntime::CreateJsValue(*engine_, text); + + NativeValue* argv[] = {nativeValue}; + std::string methodName = "textChange"; + CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + }; + + mainHandler_->PostTask(task); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp new file mode 100644 index 0000000..9a65e4e --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp @@ -0,0 +1,232 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "js_text_input_client.h" +#include "input_method_controller.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "event_handler.h" +#include "event_runner.h" +#include "string_ex.h" +#include "input_method_ability.h" +#include "js_input_method_engine_utils.h" +namespace OHOS { +namespace MiscServices { + constexpr size_t ARGC_ZERO = 0; + constexpr size_t ARGC_ONE = 1; + + void JsTextInputClient::Finalizer(NativeEngine* engine, void* data, void* hint) + { + IMSA_HILOGI("JsTextInputClient::Finalizer is called"); + std::unique_ptr(static_cast(data)); + } + + NativeValue* JsTextInputClient::InsertText(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnInsertText(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::DeleteForward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDeleteForward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::DeleteBackward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDeleteBackward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::SendFunctionKey(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnSendFunctionKey(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetForward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetForward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetBackward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetBackward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetEditorAttribute(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetEditorAttribute(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::OnInsertText(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnInsertText is called!"); + if (info.argc < ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnInsertText has no params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + std::string textString; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, textString)) { + IMSA_HILOGI("JsTextInputClient::OnInsertText Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + bool ret = InputMethodAbility::GetInstance()->InsertText(textString); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + + return result; + } + + NativeValue* JsTextInputClient::OnDeleteForward(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnDeleteForward is called!"); + if (info.argc < ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnDeleteForward has no params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnDeleteForward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->DeleteForward(number); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + + return result; + } + + NativeValue* JsTextInputClient::OnDeleteBackward(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnDeleteBackward is called!"); + if (info.argc < ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnDeleteBackward has no params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnDeleteBackward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->DeleteBackward(number); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + + return result; + } + + NativeValue* JsTextInputClient::OnSendFunctionKey(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnSendFunctionKey is called!"); + if (info.argc < ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnSendFunctionKey has no params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnSendFunctionKey Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->SendFunctionKey(number); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + + return result; + } + + NativeValue* JsTextInputClient::OnGetForward(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnGetForward is called!"); + if (info.argc != ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnGetForward has not one params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnGetForward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextBeforeCursor(number)); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + + return result; + } + + NativeValue* JsTextInputClient::OnGetBackward(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnGetBackward is called!"); + if (info.argc != ARGC_ONE) { + IMSA_HILOGI("JsTextInputClient::OnGetBackward has not one params!"); + return engine.CreateUndefined(); + } + + NativeValue* nativeString = nullptr; + nativeString = info.argv[ARGC_ZERO]; + + int32_t number; + if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnGetBackward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextAfterCursor(number)); + + NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + + return result; + } + + NativeValue* JsTextInputClient::OnGetEditorAttribute(NativeEngine& engine, NativeCallbackInfo& info) + { + IMSA_HILOGI("JsTextInputClient::OnGetEditorAttribute is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsTextInputClient::OnGetEditorAttribute has params!"); + return engine.CreateUndefined(); + } + + return CreateEditorAttribute(engine); + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/js_input_method.cpp b/interfaces/kits/js/napi/src/js_input_method.cpp deleted file mode 100644 index 7b931f9..0000000 --- a/interfaces/kits/js/napi/src/js_input_method.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "js_input_method.h" -#include -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "string_ex.h" -#include "input_method_utils.h" -#include "event_handler.h" -#include "event_runner.h" -#include "input_method_controller.h" - -namespace OHOS { -namespace MiscServices { - namespace { - constexpr size_t ARGC_ZERO = 0; - } - class JsInputMethod { - public: - explicit JsInputMethod(NativeEngine* engine) - { - IMSA_HILOGI("JsInputMethod::Constructor is called"); - } - - ~JsInputMethod() = default; - - static void Finalizer(NativeEngine* engine, void* data, void* hint) - { - IMSA_HILOGI("JsInputMethod::Finalizer is called"); - std::unique_ptr(static_cast(data)); - } - - static NativeValue* GetInputMethodSetting(NativeEngine* engine, NativeCallbackInfo* info) - { - IMSA_HILOGI("JsInputMethod::GetInputMethodSetting is called"); - JsInputMethod* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnGetInputMethodSetting(*engine, *info) : nullptr; - } - - static NativeValue* DisplayOptionalInputMethod(NativeEngine* engine, NativeCallbackInfo* info) - { - IMSA_HILOGI("JsInputMethod::DisplayOptionalInputMethod is called"); - if (info->argc != ARGC_ZERO) { - IMSA_HILOGI("JsInputMethod::OnDisplayOptionalInputMethod has params, return!"); - return engine->CreateUndefined(); - } - - InputMethodController::GetInstance()->DisplayOptionalInputMethod(); - - return engine->CreateUndefined(); - } - - static NativeValue* ListInputMethod(NativeEngine* engine, NativeCallbackInfo* info) - { - IMSA_HILOGI("JsInputMethod::ListInputMethod is called"); - if (info->argc != ARGC_ZERO) { - IMSA_HILOGI("JsInputMethod::ListInputMethod has params, return!"); - return engine->CreateUndefined(); - } - - std::vector properties = InputMethodController::GetInstance()->ListInputMethod(); - if (properties.size() == 0) { - IMSA_HILOGI("JsInputMethod::ListInputMethod has no ime"); - return engine->CreateUndefined(); - } - NativeValue* arrayValue = engine->CreateArray(properties.size()); - NativeArray* array = AbilityRuntime::ConvertNativeValueTo(arrayValue); - uint32_t index = 0; - for (const auto &info : properties) { - NativeValue* objValue = engine->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); - - IMSA_HILOGI("JsInputMethod::ListInputMethod %{public}s/%{public}s", Str16ToStr8(info->mPackageName).c_str(), Str16ToStr8(info->mAbilityName).c_str()); - - object->SetProperty("packageName", AbilityRuntime::CreateJsValue(*engine, Str16ToStr8(info->mPackageName))); - object->SetProperty("methodId", AbilityRuntime::CreateJsValue(*engine, Str16ToStr8(info->mAbilityName))); - array->SetElement(index++, objValue); - } - - return arrayValue; - } - private: - NativeValue* OnGetInputMethodSetting(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethod::OnGetInputMethodSetting is called!"); - - NativeValue* objValue = engine.CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); - - AbilityRuntime::BindNativeFunction(engine, *object, "displayOptionalInputMethod", - JsInputMethod::DisplayOptionalInputMethod); - AbilityRuntime::BindNativeFunction(engine, *object, "listInputMethod", JsInputMethod::ListInputMethod); - - return objValue; - } - }; - - NativeValue* JsInputMethodInit(NativeEngine* engine, NativeValue* exportObj) - { - IMSA_HILOGI("JsInputMethodInit() is called!"); - - if (engine == nullptr || exportObj == nullptr) { - IMSA_HILOGI("engine or exportObj null"); - return nullptr; - } - - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); - if (object == nullptr) { - IMSA_HILOGI("object null"); - return nullptr; - } - - std::unique_ptr jsInputMethod = std::make_unique(engine); - object->SetNativePointer(jsInputMethod.release(), JsInputMethod::Finalizer, nullptr); - - // create inputMethod - AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodSetting", - JsInputMethod::GetInputMethodSetting); - - return engine->CreateUndefined(); - } -} -} \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/js_input_method_engine.cpp b/interfaces/kits/js/napi/src/js_input_method_engine.cpp deleted file mode 100644 index 6b4261a..0000000 --- a/interfaces/kits/js/napi/src/js_input_method_engine.cpp +++ /dev/null @@ -1,357 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "js_input_method_engine.h" -#include -#include "js_input_method_engine_listener.h" -#include "input_method_ability.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "string_ex.h" -#include "input_method_utils.h" - -namespace OHOS { -namespace MiscServices { - namespace { - constexpr size_t ARGC_ZERO = 0; - constexpr size_t ARGC_ONE = 1; - constexpr size_t ARGC_TWO = 2; - } - class JsInputMethodEngine { - public: - sptr imeListener_; - explicit JsInputMethodEngine(NativeEngine* engine) - { - IMSA_HILOGI("JsInputMethodEngine::Constructor is called"); - imeListener_ = new JsInputMethodEngineListener(engine); - InputMethodAbility::GetInstance()->setImeListener(imeListener_); - } - - ~JsInputMethodEngine() = default; - - static void Finalizer(NativeEngine* engine, void* data, void* hint) - { - IMSA_HILOGI("JsInputMethodEngine::Finalizer is called"); - std::unique_ptr(static_cast(data)); - } - - static NativeValue* InsertText(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnInsertText(*engine, *info) : nullptr; - } - - static NativeValue* DeleteForward(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnDeleteForward(*engine, *info) : nullptr; - } - - static NativeValue* DeleteBackward(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnDeleteBackward(*engine, *info) : nullptr; - } - - static NativeValue* MoveCursor(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnMoveCursor(*engine, *info) : nullptr; - } - - static NativeValue* HideKeyboardSelf(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnHideKeyboardSelf(*engine, *info) : nullptr; - } - - static NativeValue* GetTextBeforeCursor(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnGetTextBeforeCursor(*engine, *info) : nullptr; - } - - static NativeValue* GetTextAfterCursor(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnGetTextAfterCursor(*engine, *info) : nullptr; - } - - static NativeValue* SendFunctionKey(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnSendFunctionKey(*engine, *info) : nullptr; - } - - static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; - } - - static NativeValue* UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) - { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; - } - - private: - std::mutex mtx_; - - NativeValue* OnInsertText(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnInsertText is called!"); - if (info.argc < ARGC_ONE) { - IMSA_HILOGI("JsInputMethodEngine::OnInsertText has no params!"); - return engine.CreateUndefined(); - } - - NativeValue* nativeString = nullptr; - nativeString = info.argv[ARGC_ZERO]; - - std::string textString; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, textString)) { - IMSA_HILOGI("JsInputMethodEngine::OnInsertText Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - - bool ret = InputMethodAbility::GetInstance()->InsertText(textString); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); - - return result; - } - - NativeValue* OnDeleteForward(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteForward is called!"); - if (info.argc < ARGC_ONE) { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteForward has no params!"); - return engine.CreateUndefined(); - } - - NativeValue* nativeString = nullptr; - nativeString = info.argv[ARGC_ZERO]; - - int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteForward Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - - InputMethodAbility::GetInstance()->DeleteForward(number); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); - - return result; - } - - NativeValue* OnDeleteBackward(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteBackward is called!"); - if (info.argc < ARGC_ONE) { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteBackward has no params!"); - return engine.CreateUndefined(); - } - - NativeValue* nativeString = nullptr; - nativeString = info.argv[ARGC_ZERO]; - - int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { - IMSA_HILOGI("JsInputMethodEngine::OnDeleteBackward Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - - InputMethodAbility::GetInstance()->DeleteBackward(number); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); - - return result; - } - - NativeValue* OnMoveCursor(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnMoveCursor is called!"); - if (info.argc < ARGC_ONE) { - IMSA_HILOGI("JsInputMethodEngine::OnMoveCursor has no params!"); - return engine.CreateUndefined(); - } - - NativeValue* nativeString = nullptr; - nativeString = info.argv[ARGC_ZERO]; - - int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { - IMSA_HILOGI("JsInputMethodEngine::OnMoveCursor Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - - InputMethodAbility::GetInstance()->MoveCursor(number); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); - - return result; - } - - NativeValue* OnHideKeyboardSelf(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnHideKeyboardSelf is called!"); - if (info.argc != ARGC_ZERO) { - IMSA_HILOGI("JsInputMethodEngine::OnHideKeyboardSelf has params, return!"); - return engine.CreateUndefined(); - } - - InputMethodAbility::GetInstance()->HideKeyboardSelf(); - - return engine.CreateUndefined(); - } - - NativeValue* OnGetTextBeforeCursor(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnGetTextBeforeCursor is called!"); - if (info.argc != ARGC_ZERO) { - IMSA_HILOGI("JsInputMethodEngine::OnGetTextBeforeCursor has params, return!"); - return engine.CreateUndefined(); - } - - std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextBeforeCursor()); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); - - return result; - } - - NativeValue* OnGetTextAfterCursor(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnGetTextAfterCursor is called!"); - if (info.argc != ARGC_ZERO) { - IMSA_HILOGI("JsInputMethodEngine::OnGetTextAfterCursor has params, return!"); - return engine.CreateUndefined(); - } - - std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextAfterCursor()); - - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); - - return result; - } - - NativeValue* OnSendFunctionKey(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnSendFunctionKey is called!"); - if (info.argc < ARGC_ONE) { - IMSA_HILOGI("JsInputMethodEngine::OnSendFunctionKey has no params!"); - return engine.CreateUndefined(); - } - - NativeValue* nativeString = nullptr; - nativeString = info.argv[ARGC_ZERO]; - - int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { - IMSA_HILOGI("JsInputMethodEngine::OnSendFunctionKey Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - - InputMethodAbility::GetInstance()->SendFunctionKey(number); - - return engine.CreateUndefined(); - } - - NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback is called!"); - if (info.argc != ARGC_TWO) { - IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback Params not match"); - return engine.CreateUndefined(); - } - - std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { - IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback Failed to convert parameter to callbackType"); - return engine.CreateUndefined(); - } - - NativeValue* value = info.argv[1]; - imeListener_->RegisterListenerWithType(engine, cbType, value); - return engine.CreateUndefined(); - } - - NativeValue* OnUnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) - { - IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback is called!"); - if (info.argc == 0) { - IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback Params not match"); - return engine.CreateUndefined(); - } - - std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { - IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback Failed to convert parameter to callbackType"); - return engine.CreateUndefined(); - } - - std::lock_guard lock(mtx_); - NativeValue* value = info.argv[1]; - if (!value->IsCallable()) { - IMSA_HILOGI("JsInputMethodEngine::OnUnregisterWindowManagerCallback info->argv[1] is not callable"); - return engine.CreateUndefined(); - } - imeListener_->UnregisterListenerWithType(cbType, value); - return engine.CreateUndefined(); - } - }; - -NativeValue* JsInputMethodEngineInit(NativeEngine* engine, NativeValue* exportObj) -{ - IMSA_HILOGI("JsInputMethodEngineInit() is called!"); - - if (engine == nullptr || exportObj == nullptr) { - IMSA_HILOGI("engine or exportObj null"); - return nullptr; - } - - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); - if (object == nullptr) { - IMSA_HILOGI("object null"); - return nullptr; - } - - std::unique_ptr jsIMEngine = std::make_unique(engine); - object->SetNativePointer(jsIMEngine.release(), JsInputMethodEngine::Finalizer, nullptr); - - AbilityRuntime::BindNativeFunction(*engine, *object, "InsertText", JsInputMethodEngine::InsertText); - AbilityRuntime::BindNativeFunction(*engine, *object, "DeleteForward", JsInputMethodEngine::DeleteForward); - AbilityRuntime::BindNativeFunction(*engine, *object, "DeleteBackward", JsInputMethodEngine::DeleteBackward); - AbilityRuntime::BindNativeFunction(*engine, *object, "MoveCursor", JsInputMethodEngine::MoveCursor); - AbilityRuntime::BindNativeFunction(*engine, *object, "HideKeyboardSelf", JsInputMethodEngine::HideKeyboardSelf); - AbilityRuntime::BindNativeFunction(*engine, *object, "GetTextBeforeCursor", - JsInputMethodEngine::GetTextBeforeCursor); - AbilityRuntime::BindNativeFunction(*engine, *object, "GetTextAfterCursor", JsInputMethodEngine::GetTextAfterCursor); - AbilityRuntime::BindNativeFunction(*engine, *object, "SendFunctionKey", JsInputMethodEngine::SendFunctionKey); - - AbilityRuntime::BindNativeFunction(*engine, *object, "on", JsInputMethodEngine::RegisterCallback); - AbilityRuntime::BindNativeFunction(*engine, *object, "off", JsInputMethodEngine::UnRegisterCallback); - - object->SetProperty("FUNCTION_KEY_CONFIRM", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); - object->SetProperty("CURSOR_UP", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); - object->SetProperty("CURSOR_DOWN", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); - object->SetProperty("CURSOR_LEFT", AbilityRuntime::CreateJsValue(*engine, static_cast(3))); - object->SetProperty("CURSOR_RIGHT", AbilityRuntime::CreateJsValue(*engine, static_cast(4))); - return engine->CreateUndefined(); -} -} -} \ No newline at end of file diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index 8274570..d688d98 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -37,6 +37,7 @@ namespace MiscServices { RELEASE_INPUT, START_INPUT, STOP_INPUT, + HIDE_CURRENT_INPUT, SET_INPUT_METHOD_CORE, GET_DISPLAY_MODE, GET_KEYBOARD_WINDOW_HEIGHT, @@ -55,6 +56,7 @@ namespace MiscServices { virtual void startInput(MessageParcel& data) = 0; virtual void stopInput(MessageParcel& data) = 0; virtual void SetCoreAndAgent(MessageParcel& data) = 0; + virtual void HideCurrentInput(MessageParcel& data) = 0; virtual void displayOptionalInputMethod(MessageParcel& data) = 0; virtual int32_t getDisplayMode(int32_t retMode) = 0; diff --git a/services/include/input_method_system_ability_stub.h b/services/include/input_method_system_ability_stub.h index 96db418..df92a02 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -34,6 +34,7 @@ namespace MiscServices { virtual void startInput(MessageParcel& data) override; virtual void stopInput(MessageParcel& data) override; virtual void SetCoreAndAgent(MessageParcel& data) override; + virtual void HideCurrentInput(MessageParcel& data) override; virtual void displayOptionalInputMethod(MessageParcel& data) override; virtual int32_t listInputMethodByUserId(int32_t userId, std::vector *properties) = 0; protected: diff --git a/services/include/message_handler.h b/services/include/message_handler.h index b7e37f9..9d05044 100644 --- a/services/include/message_handler.h +++ b/services/include/message_handler.h @@ -45,6 +45,7 @@ namespace MessageID { MSG_ID_STOP_INPUT, // stop input MSG_ID_RELEASE_INPUT, // release input MSG_ID_SET_CORE_AND_AGENT, + MSG_HIDE_CURRENT_INPUT, // the request to handle the condition that the remote object died MSG_ID_CLIENT_DIED, // input client died diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index b38c4e8..6b2cd4e 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -491,6 +491,7 @@ namespace MiscServices { case MSG_ID_RELEASE_INPUT: case MSG_ID_START_INPUT: case MSG_ID_STOP_INPUT: + case MSG_HIDE_CURRENT_INPUT: case MSG_ID_SET_CORE_AND_AGENT: case MSG_ID_HIDE_KEYBOARD_SELF: case MSG_ID_SET_DISPLAY_MODE: diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index a67aa9c..5327981 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -165,6 +165,11 @@ namespace MiscServices { reply.WriteInt32(NO_ERROR); break; } + case HIDE_CURRENT_INPUT: { + HideCurrentInput(data); + reply.WriteInt32(NO_ERROR); + break; + } default: { return BRemoteObject::OnRemoteRequest(code, data, reply, option); } @@ -287,6 +292,18 @@ namespace MiscServices { MessageHandler::Instance()->SendMessage(msg); } + void InputMethodSystemAbilityStub::HideCurrentInput(MessageParcel& data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::HideCurrentInput"); + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = getUserId(uid); + MessageParcel *parcel = new MessageParcel(); + parcel->WriteInt32(userId); + + Message *msg = new Message(MSG_HIDE_CURRENT_INPUT, parcel); + MessageHandler::Instance()->SendMessage(msg); + } + /*! Get user id from uid \param uid the uid from which the remote call is \return return user id of the remote caller diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 1146e25..799bb79 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -182,6 +182,9 @@ namespace MiscServices { OnRestartIms(index, imeId); break; } + case MSG_HIDE_CURRENT_INPUT: { + OnHideKeyboardSelf(0); + } default: { break; } From dc69d70367b1785c527831742082b39a8596c872 Mon Sep 17 00:00:00 2001 From: zhouyongfei Date: Tue, 22 Feb 2022 17:41:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouyongfei --- .../src/js_input_method_controller.cpp | 5 +- .../src/js_input_method_registry.cpp | 15 ++-- .../src/js_input_method_setting.cpp | 13 +-- .../inputmethod/src/js_input_method_utils.cpp | 12 +-- .../include/js_editor_attribute.h | 4 - .../src/js_editor_attribute.cpp | 42 ---------- .../src/js_input_method_engine.cpp | 9 +- .../src/js_input_method_engine_listener.cpp | 30 ++----- .../src/js_input_method_engine_registry.cpp | 83 +++++++++---------- .../src/js_input_method_engine_utils.cpp | 39 ++++----- .../src/js_keyboard_controller.cpp | 3 +- .../src/js_keyboard_delegate.cpp | 9 +- .../src/js_keyboard_delegate_listener.cpp | 39 +++++---- .../src/js_text_input_client.cpp | 39 ++++----- 14 files changed, 142 insertions(+), 200 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp index 74aea75..7e0c606 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp @@ -21,6 +21,7 @@ #include "string_ex.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_ZERO = 0; void JsInputMethodController::Finalizer(NativeEngine* engine, void* data, void* hint) @@ -31,7 +32,7 @@ namespace MiscServices { NativeValue* JsInputMethodController::StopInput(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodController* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodController* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnStopInput(*engine, *info) : nullptr; } @@ -45,7 +46,7 @@ namespace MiscServices { InputMethodController::GetInstance()->HideCurrentInput(); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + NativeValue* result = CreateJsValue(engine, true); return result; } diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp index 9cbe6a4..3c5911f 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp @@ -24,6 +24,7 @@ namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; namespace { constexpr size_t ARGC_ZERO = 0; constexpr int32_t MAX_TYPE_NUM = 128; @@ -46,14 +47,14 @@ namespace MiscServices { static NativeValue* GetInputMethodSetting(NativeEngine* engine, NativeCallbackInfo* info) { IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodSetting is called"); - JsInputMethodRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetInputMethodSetting(*engine, *info) : nullptr; } static NativeValue* GetInputMethodController(NativeEngine* engine, NativeCallbackInfo* info) { IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodController is called"); - JsInputMethodRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetInputMethodController(*engine, *info) : nullptr; } @@ -90,7 +91,7 @@ namespace MiscServices { return nullptr; } - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); + NativeObject* object = ConvertNativeValueTo(exportObj); if (object == nullptr) { IMSA_HILOGI("object null"); return nullptr; @@ -99,12 +100,10 @@ namespace MiscServices { std::unique_ptr jsInputMethodRegistry = std::make_unique(engine); object->SetNativePointer(jsInputMethodRegistry.release(), JsInputMethodRegistry::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodSetting", - JsInputMethodRegistry::GetInputMethodSetting); - AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodController", - JsInputMethodRegistry::GetInputMethodController); + BindNativeFunction(*engine, *object, "getInputMethodSetting", JsInputMethodRegistry::GetInputMethodSetting); + BindNativeFunction(*engine, *object, "getInputMethodController", JsInputMethodRegistry::GetInputMethodController); - object->SetProperty("MAX_TYPE_NUM", AbilityRuntime::CreateJsValue(*engine, static_cast(MAX_TYPE_NUM))); + object->SetProperty("MAX_TYPE_NUM", CreateJsValue(*engine, static_cast(MAX_TYPE_NUM))); return engine->CreateUndefined(); } diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp index 5b50644..f89bc61 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp @@ -21,6 +21,7 @@ #include "string_ex.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_ZERO = 0; void JsInputMethodSetting::Finalizer(NativeEngine* engine, void* data, void* hint) @@ -31,13 +32,13 @@ namespace MiscServices { NativeValue* JsInputMethodSetting::DisplayOptionalInputMethod(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodSetting* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodSetting* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnDisplayOptionalInputMethod(*engine, *info) : nullptr; } NativeValue* JsInputMethodSetting::ListInputMethod(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodSetting* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodSetting* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnListInputMethod(*engine, *info) : nullptr; } @@ -68,16 +69,16 @@ namespace MiscServices { return engine.CreateUndefined(); } NativeValue* arrayValue = engine.CreateArray(properties.size()); - NativeArray* array = AbilityRuntime::ConvertNativeValueTo(arrayValue); + NativeArray* array = ConvertNativeValueTo(arrayValue); uint32_t index = 0; for (const auto &info : properties) { NativeValue* objValue = engine.CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject* object = ConvertNativeValueTo(objValue); IMSA_HILOGI("JsInputMethodSetting::ListInputMethod %{public}s/%{public}s", Str16ToStr8(info->mPackageName).c_str(), Str16ToStr8(info->mAbilityName).c_str()); - object->SetProperty("packageName", AbilityRuntime::CreateJsValue(engine, Str16ToStr8(info->mPackageName))); - object->SetProperty("methodId", AbilityRuntime::CreateJsValue(engine, Str16ToStr8(info->mAbilityName))); + object->SetProperty("packageName", CreateJsValue(engine, Str16ToStr8(info->mPackageName))); + object->SetProperty("methodId", CreateJsValue(engine, Str16ToStr8(info->mAbilityName))); array->SetElement(index++, objValue); } diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp index 4a8be83..231afaf 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_utils.cpp @@ -22,11 +22,12 @@ #include "js_input_method_controller.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; NativeValue* CreateInputMethodSetting(NativeEngine& engine) { IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodSetting is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodSetting Failed to get object"); return nullptr; @@ -35,9 +36,8 @@ namespace OHOS { std::unique_ptr jsInputMethodSetting = std::make_unique(); object->SetNativePointer(jsInputMethodSetting.release(), JsInputMethodSetting::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "displayOptionalInputMethod", - JsInputMethodSetting::DisplayOptionalInputMethod); - AbilityRuntime::BindNativeFunction(engine, *object, "listInputMethod", JsInputMethodSetting::ListInputMethod); + BindNativeFunction(engine, *object, "displayOptionalInputMethod", JsInputMethodSetting::DisplayOptionalInputMethod); + BindNativeFunction(engine, *object, "listInputMethod", JsInputMethodSetting::ListInputMethod); return objValue; } @@ -45,7 +45,7 @@ namespace OHOS { { IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodController is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodController Failed to get object"); return nullptr; @@ -54,7 +54,7 @@ namespace OHOS { std::unique_ptr jsInputMethodController = std::make_unique(); object->SetNativePointer(jsInputMethodController.release(), JsInputMethodController::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "stopInput", JsInputMethodController::StopInput); + BindNativeFunction(engine, *object, "stopInput", JsInputMethodController::StopInput); return objValue; } } diff --git a/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h b/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h index 1c00366..308cb34 100644 --- a/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h @@ -25,11 +25,7 @@ namespace OHOS { JsEditorAttribute() = default; ~JsEditorAttribute() = default; static void Finalizer(NativeEngine* engine, void* data, void* hint); -// static NativeValue* GetEnterKeyType(NativeEngine* engine, NativeCallbackInfo* info); -// static NativeValue* GetInputPattern(NativeEngine* engine, NativeCallbackInfo* info); private: -// NativeValue* OnGetEnterKeyType(NativeEngine& engine, NativeCallbackInfo& info); -// NativeValue* OnGetInputPattern(NativeEngine& engine, NativeCallbackInfo& info); }; } } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp index b1536c1..b833d92 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp @@ -29,47 +29,5 @@ namespace MiscServices { IMSA_HILOGI("JsEditorAttribute::Finalizer is called"); std::unique_ptr(static_cast(data)); } - -// NativeValue* JsEditorAttribute::GetEnterKeyType(NativeEngine* engine, NativeCallbackInfo* info) -// { -// JsEditorAttribute* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); -// return (me != nullptr) ? me->OnGetEnterKeyType(*engine, *info) : nullptr; -// } -// -// NativeValue* JsEditorAttribute::GetInputPattern(NativeEngine* engine, NativeCallbackInfo* info) -// { -// JsEditorAttribute* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); -// return (me != nullptr) ? me->OnGetInputPattern(*engine, *info) : nullptr; -// } - -// NativeValue* JsEditorAttribute::OnGetEnterKeyType(NativeEngine& engine, NativeCallbackInfo& info) -// { -// IMSA_HILOGI("JsEditorAttribute::OnGetEnterKeyType is called!"); -// if (info.argc != ARGC_ZERO) { -// IMSA_HILOGI("JsEditorAttribute::OnGetEnterKeyType has params!"); -// return engine.CreateUndefined(); -// } -// -// int32_t ret = InputMethodAbility::GetInstance()->GetEnterKeyType(); -// -// NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); -// -// return result; -// } -// -// NativeValue* JsEditorAttribute::OnGetInputPattern(NativeEngine& engine, NativeCallbackInfo& info) -// { -// IMSA_HILOGI("JsEditorAttribute::OnGetInputPattern is called!"); -// if (info.argc != ARGC_ZERO) { -// IMSA_HILOGI("JsEditorAttribute::OnGetInputPattern has params!"); -// return engine.CreateUndefined(); -// } -// -// int32_t ret = InputMethodAbility::GetInstance()->GetInputPattern(); -// -// NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); -// -// return result; -// } } } \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp index 6d3fb5d..ca145af 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp @@ -24,6 +24,7 @@ #include "global.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_TWO = 2; JsInputMethodEngine::JsInputMethodEngine(NativeEngine* engine) { @@ -40,13 +41,13 @@ namespace MiscServices { NativeValue* JsInputMethodEngine::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodEngine* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; } NativeValue* JsInputMethodEngine::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodEngine* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodEngine* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; } @@ -59,7 +60,7 @@ namespace MiscServices { } std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { IMSA_HILOGI("JsInputMethodEngine::OnRegisterCallback Failed to convert parameter to callbackType"); return engine.CreateUndefined(); } @@ -78,7 +79,7 @@ namespace MiscServices { } std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { IMSA_HILOGI("JsInputMethodEngine::OnUnRegisterCallback Failed to convert parameter to callbackType"); return engine.CreateUndefined(); } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp index 3eaadd9..429a08a 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_listener.cpp @@ -19,6 +19,7 @@ namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; void JsInputMethodEngineListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { @@ -120,7 +121,7 @@ namespace MiscServices { for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { NativeValue* nativeValue = engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); bool ret = false; - if (AbilityRuntime::ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { + if (ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { result = true; } } @@ -133,7 +134,7 @@ namespace MiscServices { IMSA_HILOGI("JsInputMethodEngineListener::OnKeyboardStatus"); NativeValue* nativeValue = engine_->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); + NativeObject* object = ConvertNativeValueTo(nativeValue); if (object == nullptr) { IMSA_HILOGI("Failed to convert rect to jsObject"); return; @@ -145,34 +146,19 @@ namespace MiscServices { } else { methodName = "keyboardHide"; } - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); } void JsInputMethodEngineListener::OnInputStart() { std::lock_guard lock(mMutex); IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart"); -// NativeValue *nativeValuekb = engine_->CreateObject(); -// NativeObject *objectkb = AbilityRuntime::ConvertNativeValueTo(nativeValuekb); -// if (objectkb == nullptr) { -// IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart Failed to get object"); -// return; -// } NativeValue *nativeValuekb = CreateKeyboardController(*engine_); -// objectkb->SetProperty("kbController", CreateKeyboardController(*engine_)); - -// NativeValue *nativeValuetx = engine_->CreateObject(); -// NativeObject *objecttx = AbilityRuntime::ConvertNativeValueTo(nativeValuetx); -// if (objecttx == nullptr) { -// IMSA_HILOGI("JsInputMethodEngineListener::OnInputStart Failed to get object"); -// return; -// } -// objecttx->SetProperty("textInputClient", CreateTextInputClient(*engine_)); NativeValue *nativeValuetx = CreateTextInputClient(*engine_); NativeValue* argv[] = {nativeValuekb, nativeValuetx}; std::string methodName = "inputStart"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); } void JsInputMethodEngineListener::OnInputStop(std::string imeId) @@ -181,16 +167,16 @@ namespace MiscServices { IMSA_HILOGI("JsInputMethodEngineListener::OnInputStop"); NativeValue* nativeValue = engine_->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); + NativeObject* object = ConvertNativeValueTo(nativeValue); if (object == nullptr) { IMSA_HILOGI("Failed to convert rect to jsObject"); return; } - object->SetProperty("imeId", AbilityRuntime::CreateJsValue(*engine_, imeId)); + object->SetProperty("imeId", CreateJsValue(*engine_, imeId)); NativeValue* argv[] = {nativeValue}; std::string methodName = "inputStop"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); } } } \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp index 09ab344..e76fbd7 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp @@ -25,6 +25,7 @@ namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; namespace { constexpr size_t ARGC_ZERO = 0; constexpr size_t ARGC_ONE = 1; @@ -46,19 +47,19 @@ namespace MiscServices { static NativeValue* GetInputMethodEngine(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodEngineRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetInputMethodEngine(*engine, *info) : nullptr; } static NativeValue* GetKeyboardDelegate(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodEngineRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetKeyboardDelegate(*engine, *info) : nullptr; } static NativeValue* MoveCursor(NativeEngine* engine, NativeCallbackInfo* info) { - JsInputMethodEngineRegistry* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsInputMethodEngineRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnMoveCursor(*engine, *info) : nullptr; } private: @@ -96,14 +97,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsInputMethodEngineRegistry::OnMoveCursor Failed to convert parameter to string"); return engine.CreateUndefined(); } InputMethodAbility::GetInstance()->MoveCursor(number); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + NativeValue* result = CreateJsValue(engine, true); return result; } @@ -118,7 +119,7 @@ namespace MiscServices { return nullptr; } - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); + NativeObject* object = ConvertNativeValueTo(exportObj); if (object == nullptr) { IMSA_HILOGI("object null"); return nullptr; @@ -127,50 +128,46 @@ namespace MiscServices { std::unique_ptr jsInputMethodEngineRegistry = std::make_unique(engine); object->SetNativePointer(jsInputMethodEngineRegistry.release(), JsInputMethodEngineRegistry::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodEngine", JsInputMethodEngineRegistry::GetInputMethodEngine); - AbilityRuntime::BindNativeFunction(*engine, *object, "createKeyboardDelegate", JsInputMethodEngineRegistry::GetKeyboardDelegate); + BindNativeFunction(*engine, *object, "getInputMethodEngine", JsInputMethodEngineRegistry::GetInputMethodEngine); + BindNativeFunction(*engine, *object, "createKeyboardDelegate", JsInputMethodEngineRegistry::GetKeyboardDelegate); - object->SetProperty("ENTER_KEY_TYPE_UNSPECIFIED", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::UNSPECIFIED))); - object->SetProperty("ENTER_KEY_TYPE_GO", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::GO))); - object->SetProperty("ENTER_KEY_TYPE_SEARCH", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::SEARCH))); - object->SetProperty("ENTER_KEY_TYPE_SEND", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::SEND))); - object->SetProperty("ENTER_KEY_TYPE_NEXT", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::NEXT))); - object->SetProperty("ENTER_KEY_TYPE_DONE", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::DONE))); - object->SetProperty("ENTER_KEY_TYPE_PREVIOUS", AbilityRuntime::CreateJsValue(*engine, static_cast(EnterKeyType::PREVIOUS))); + object->SetProperty("ENTER_KEY_TYPE_UNSPECIFIED", CreateJsValue(*engine, static_cast(EnterKeyType::UNSPECIFIED))); + object->SetProperty("ENTER_KEY_TYPE_GO", CreateJsValue(*engine, static_cast(EnterKeyType::GO))); + object->SetProperty("ENTER_KEY_TYPE_SEARCH", CreateJsValue(*engine, static_cast(EnterKeyType::SEARCH))); + object->SetProperty("ENTER_KEY_TYPE_SEND", CreateJsValue(*engine, static_cast(EnterKeyType::SEND))); + object->SetProperty("ENTER_KEY_TYPE_NEXT", CreateJsValue(*engine, static_cast(EnterKeyType::NEXT))); + object->SetProperty("ENTER_KEY_TYPE_DONE", CreateJsValue(*engine, static_cast(EnterKeyType::DONE))); + object->SetProperty("ENTER_KEY_TYPE_PREVIOUS", CreateJsValue(*engine, static_cast(EnterKeyType::PREVIOUS))); - object->SetProperty("PATTERN_NULL", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::NONE))); - object->SetProperty("PATTERN_TEXT", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::TEXT))); - object->SetProperty("PATTERN_NUMBER", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::NUMBER))); - object->SetProperty("PATTERN_PHONE", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::PHONE))); - object->SetProperty("PATTERN_DATETIME", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::DATETIME))); - object->SetProperty("PATTERN_EMAIL", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::EMAIL_ADDRESS))); - object->SetProperty("PATTERN_URI", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::URL))); - object->SetProperty("PATTERN_PASSWORD", AbilityRuntime::CreateJsValue(*engine, static_cast(TextInputType::VISIBLE_PASSWORD))); + object->SetProperty("PATTERN_NULL", CreateJsValue(*engine, static_cast(TextInputType::NONE))); + object->SetProperty("PATTERN_TEXT", CreateJsValue(*engine, static_cast(TextInputType::TEXT))); + object->SetProperty("PATTERN_NUMBER", CreateJsValue(*engine, static_cast(TextInputType::NUMBER))); + object->SetProperty("PATTERN_PHONE", CreateJsValue(*engine, static_cast(TextInputType::PHONE))); + object->SetProperty("PATTERN_DATETIME", CreateJsValue(*engine, static_cast(TextInputType::DATETIME))); + object->SetProperty("PATTERN_EMAIL", CreateJsValue(*engine, static_cast(TextInputType::EMAIL_ADDRESS))); + object->SetProperty("PATTERN_URI", CreateJsValue(*engine, static_cast(TextInputType::URL))); + object->SetProperty("PATTERN_PASSWORD", CreateJsValue(*engine, static_cast(TextInputType::VISIBLE_PASSWORD))); + object->SetProperty("FLAG_SELECTING", CreateJsValue(*engine, static_cast(2))); + object->SetProperty("FLAG_SINGLE_LINE", CreateJsValue(*engine, static_cast(1))); - object->SetProperty("FLAG_SELECTING", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); - object->SetProperty("FLAG_SINGLE_LINE", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + object->SetProperty("DISPLAY_MODE_PART", CreateJsValue(*engine, static_cast(0))); + object->SetProperty("DISPLAY_MODE_FULL", CreateJsValue(*engine, static_cast(1))); - object->SetProperty("DISPLAY_MODE_PART", AbilityRuntime::CreateJsValue(*engine, static_cast(0))); - object->SetProperty("DISPLAY_MODE_FULL", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); + object->SetProperty("OPTION_ASCII", CreateJsValue(*engine, static_cast(20))); + object->SetProperty("OPTION_NONE", CreateJsValue(*engine, static_cast(0))); + object->SetProperty("OPTION_AUTO_CAP_CHARACTERS", CreateJsValue(*engine, static_cast(2))); + object->SetProperty("OPTION_AUTO_CAP_SENTENCES", CreateJsValue(*engine, static_cast(8))); + object->SetProperty("OPTION_AUTO_WORDS", CreateJsValue(*engine, static_cast(4))); + object->SetProperty("OPTION_MULTI_LINE", CreateJsValue(*engine, static_cast(1))); + object->SetProperty("OPTION_NO_FULLSCREEN", CreateJsValue(*engine, static_cast(10))); - object->SetProperty("OPTION_ASCII", AbilityRuntime::CreateJsValue(*engine, static_cast(20))); - object->SetProperty("OPTION_NONE", AbilityRuntime::CreateJsValue(*engine, static_cast(0))); - object->SetProperty("OPTION_AUTO_CAP_CHARACTERS", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); - object->SetProperty("OPTION_AUTO_CAP_SENTENCES", AbilityRuntime::CreateJsValue(*engine, static_cast(8))); - object->SetProperty("OPTION_AUTO_WORDS", AbilityRuntime::CreateJsValue(*engine, static_cast(4))); - object->SetProperty("OPTION_MULTI_LINE", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); - object->SetProperty("OPTION_NO_FULLSCREEN", AbilityRuntime::CreateJsValue(*engine, static_cast(10))); + BindNativeFunction(*engine, *object, "MoveCursor", JsInputMethodEngineRegistry::MoveCursor); - - - AbilityRuntime::BindNativeFunction(*engine, *object, "MoveCursor", JsInputMethodEngineRegistry::MoveCursor); - - object->SetProperty("FUNCTION_KEY_CONFIRM", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); - object->SetProperty("CURSOR_UP", AbilityRuntime::CreateJsValue(*engine, static_cast(1))); - object->SetProperty("CURSOR_DOWN", AbilityRuntime::CreateJsValue(*engine, static_cast(2))); - object->SetProperty("CURSOR_LEFT", AbilityRuntime::CreateJsValue(*engine, static_cast(3))); - object->SetProperty("CURSOR_RIGHT", AbilityRuntime::CreateJsValue(*engine, static_cast(4))); + object->SetProperty("CURSOR_UP", CreateJsValue(*engine, static_cast(1))); + object->SetProperty("CURSOR_DOWN", CreateJsValue(*engine, static_cast(2))); + object->SetProperty("CURSOR_LEFT", CreateJsValue(*engine, static_cast(3))); + object->SetProperty("CURSOR_RIGHT", CreateJsValue(*engine, static_cast(4))); return engine->CreateUndefined(); } } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp index ab3e342..8a1462b 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp @@ -26,11 +26,12 @@ #include "js_editor_attribute.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; NativeValue* CreateInputMethodEngine(NativeEngine &engine) { IMSA_HILOGI("JsInputMethodEngineUtils::CreateInputMethodEngine is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("JsInputMethodEngineUtils::CreateInputMethodEngine Failed to get object"); return nullptr; @@ -39,8 +40,8 @@ namespace OHOS { std::unique_ptr jsInputMethodEngine = std::make_unique(&engine); object->SetNativePointer(jsInputMethodEngine.release(), JsInputMethodEngine::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "on", JsInputMethodEngine::RegisterCallback); - AbilityRuntime::BindNativeFunction(engine, *object, "off", JsInputMethodEngine::UnRegisterCallback); + BindNativeFunction(engine, *object, "on", JsInputMethodEngine::RegisterCallback); + BindNativeFunction(engine, *object, "off", JsInputMethodEngine::UnRegisterCallback); return objValue; } @@ -48,7 +49,7 @@ namespace OHOS { { IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardController is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("CreateKeyboardController Failed to get object"); return nullptr; @@ -57,7 +58,7 @@ namespace OHOS { std::unique_ptr jsKeyboardController = std::make_unique(); object->SetNativePointer(jsKeyboardController.release(), JsKeyboardController::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "hideKeyboard", JsKeyboardController::HideKeyboardSelf); + BindNativeFunction(engine, *object, "hideKeyboard", JsKeyboardController::HideKeyboardSelf); return objValue; } @@ -66,7 +67,7 @@ namespace OHOS { { IMSA_HILOGI("JsInputMethodEngineUtils::CreateTextInputClient is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("CreateTextInputClient Failed to get object"); return nullptr; @@ -75,13 +76,13 @@ namespace OHOS { std::unique_ptr jsTextInputClient = std::make_unique(); object->SetNativePointer(jsTextInputClient.release(), JsTextInputClient::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "insertText", JsTextInputClient::InsertText); - AbilityRuntime::BindNativeFunction(engine, *object, "deleteForward", JsTextInputClient::DeleteForward); - AbilityRuntime::BindNativeFunction(engine, *object, "deleteBackward", JsTextInputClient::DeleteBackward); - AbilityRuntime::BindNativeFunction(engine, *object, "sendKeyFunction", JsTextInputClient::SendFunctionKey); - AbilityRuntime::BindNativeFunction(engine, *object, "getForward", JsTextInputClient::GetForward); - AbilityRuntime::BindNativeFunction(engine, *object, "getBackward", JsTextInputClient::GetBackward); - AbilityRuntime::BindNativeFunction(engine, *object, "getEditorAttribute", JsTextInputClient::GetEditorAttribute); + BindNativeFunction(engine, *object, "insertText", JsTextInputClient::InsertText); + BindNativeFunction(engine, *object, "deleteForward", JsTextInputClient::DeleteForward); + BindNativeFunction(engine, *object, "deleteBackward", JsTextInputClient::DeleteBackward); + BindNativeFunction(engine, *object, "sendKeyFunction", JsTextInputClient::SendFunctionKey); + BindNativeFunction(engine, *object, "getForward", JsTextInputClient::GetForward); + BindNativeFunction(engine, *object, "getBackward", JsTextInputClient::GetBackward); + BindNativeFunction(engine, *object, "getEditorAttribute", JsTextInputClient::GetEditorAttribute); return objValue; } @@ -89,7 +90,7 @@ namespace OHOS { { IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardDelegate is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("CreateKeyboardDelegate Failed to get object"); return nullptr; @@ -98,8 +99,8 @@ namespace OHOS { std::unique_ptr jsKeyboardDelegate = std::make_unique(&engine); object->SetNativePointer(jsKeyboardDelegate.release(), JsKeyboardDelegate::Finalizer, nullptr); - AbilityRuntime::BindNativeFunction(engine, *object, "on", JsKeyboardDelegate::RegisterCallback); - AbilityRuntime::BindNativeFunction(engine, *object, "off", JsKeyboardDelegate::UnRegisterCallback); + BindNativeFunction(engine, *object, "on", JsKeyboardDelegate::RegisterCallback); + BindNativeFunction(engine, *object, "off", JsKeyboardDelegate::UnRegisterCallback); return objValue; } @@ -107,7 +108,7 @@ namespace OHOS { { IMSA_HILOGI("JsInputMethodEngineUtils::CreateEditorAttribute is called"); NativeValue *objValue = engine.CreateObject(); - NativeObject *object = AbilityRuntime::ConvertNativeValueTo(objValue); + NativeObject *object = ConvertNativeValueTo(objValue); if (object == nullptr) { IMSA_HILOGI("CreateEditorAttribute Failed to get object"); return nullptr; @@ -116,8 +117,8 @@ namespace OHOS { std::unique_ptr jsEditorAttribute = std::make_unique(); object->SetNativePointer(jsEditorAttribute.release(), JsEditorAttribute::Finalizer, nullptr); - object->SetProperty("enterKeyType", AbilityRuntime::CreateJsValue(engine, InputMethodAbility::GetInstance()->GetEnterKeyType())); - object->SetProperty("inputPattern", AbilityRuntime::CreateJsValue(engine, InputMethodAbility::GetInstance()->GetInputPattern())); + object->SetProperty("enterKeyType", CreateJsValue(engine, InputMethodAbility::GetInstance()->GetEnterKeyType())); + object->SetProperty("inputPattern", CreateJsValue(engine, InputMethodAbility::GetInstance()->GetInputPattern())); return objValue; } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp index 2dfc9b4..afee607 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp @@ -22,6 +22,7 @@ #include "input_method_ability.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_ZERO = 0; void JsKeyboardController::Finalizer(NativeEngine* engine, void* data, void* hint) { @@ -31,7 +32,7 @@ namespace MiscServices { NativeValue* JsKeyboardController::HideKeyboardSelf(NativeEngine* engine, NativeCallbackInfo* info) { - JsKeyboardController* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsKeyboardController* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnHideKeyboardSelf(*engine, *info) : nullptr; } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp index 71d64a2..1111ae5 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp @@ -24,6 +24,7 @@ #include "global.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_TWO = 2; JsKeyboardDelegate::JsKeyboardDelegate(NativeEngine* engine) { @@ -50,13 +51,13 @@ namespace MiscServices { NativeValue* JsKeyboardDelegate::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) { - JsKeyboardDelegate* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsKeyboardDelegate* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; } NativeValue* JsKeyboardDelegate::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) { - JsKeyboardDelegate* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsKeyboardDelegate* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnUnRegisterCallback(*engine, *info) : nullptr; } @@ -69,7 +70,7 @@ namespace MiscServices { } std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { IMSA_HILOGI("JsKeyboardDelegate::OnRegisterCallback Failed to convert parameter to callbackType"); return engine.CreateUndefined(); } @@ -88,7 +89,7 @@ namespace MiscServices { } std::string cbType; - if (!AbilityRuntime::ConvertFromJsValue(engine, info.argv[0], cbType)) { + if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { IMSA_HILOGI("JsKeyboardDelegate::OnUnRegisterCallback Failed to convert parameter to callbackType"); return engine.CreateUndefined(); } diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp index d1b57c2..b5118fd 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp @@ -19,8 +19,8 @@ namespace OHOS { namespace MiscServices { - void JsKeyboardDelegateListener::RegisterListenerWithType(NativeEngine& engine, - std::string type, NativeValue* value) + using namespace AbilityRuntime; + void JsKeyboardDelegateListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { // should do type check if (IfCallbackRegistered(type, value)) { @@ -120,7 +120,7 @@ namespace MiscServices { for (auto iter = jsCbMap_[methodName].begin(); iter != jsCbMap_[methodName].end(); iter++) { NativeValue* nativeValue = engine_->CallFunction(engine_->CreateUndefined(), (*iter)->Get(), argv, argc); bool ret = false; - if (AbilityRuntime::ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { + if (ConvertFromJsValue(*engine_, nativeValue, ret) && ret) { result = true; } } @@ -133,7 +133,7 @@ namespace MiscServices { IMSA_HILOGI("JsKeyboardDelegateListener::OnKeyEvent"); NativeValue* nativeValue = engine_->CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(nativeValue); + NativeObject* object = ConvertNativeValueTo(nativeValue); if (object == nullptr) { IMSA_HILOGI("Failed to convert rect to jsObject"); return false; @@ -145,9 +145,9 @@ namespace MiscServices { } else { methodName = "keyUp"; } - object->SetProperty("keyCode", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyCode))); - object->SetProperty("keyAction", AbilityRuntime::CreateJsValue(*engine_, static_cast(keyStatus))); - return CallJsMethodReturnBool(methodName, argv, AbilityRuntime::ArraySize(argv)); + object->SetProperty("keyCode", CreateJsValue(*engine_, static_cast(keyCode))); + object->SetProperty("keyAction", CreateJsValue(*engine_, static_cast(keyStatus))); + return CallJsMethodReturnBool(methodName, argv, ArraySize(argv)); } void JsKeyboardDelegateListener::OnCursorUpdate(int32_t positionX, int32_t positionY, int height) @@ -156,32 +156,31 @@ namespace MiscServices { IMSA_HILOGI("JsKeyboardDelegateListener::OnCursorUpdate"); auto task = [this, positionX, positionY, height] () { - NativeValue* nativeXValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(positionX)); - NativeValue* nativeYValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(positionY)); - NativeValue* nativeHValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(height)); + NativeValue* nativeXValue = CreateJsValue(*engine_, static_cast(positionX)); + NativeValue* nativeYValue = CreateJsValue(*engine_, static_cast(positionY)); + NativeValue* nativeHValue = CreateJsValue(*engine_, static_cast(height)); NativeValue* argv[] = {nativeXValue, nativeYValue, nativeHValue}; std::string methodName = "cursorContextChange"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); }; mainHandler_->PostTask(task); } - void JsKeyboardDelegateListener::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, - int32_t newBegin, int32_t newEnd) + void JsKeyboardDelegateListener::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { std::lock_guard lock(mMutex); IMSA_HILOGI("JsKeyboardDelegateListener::OnSelectionChange"); auto task = [this, oldBegin, oldEnd, newBegin, newEnd] () { - NativeValue* nativeOBValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(oldBegin)); - NativeValue* nativeOEValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(oldEnd)); - NativeValue* nativeNBHValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(newBegin)); - NativeValue* nativeNEValue = AbilityRuntime::CreateJsValue(*engine_, static_cast(newEnd)); + NativeValue* nativeOBValue = CreateJsValue(*engine_, static_cast(oldBegin)); + NativeValue* nativeOEValue = CreateJsValue(*engine_, static_cast(oldEnd)); + NativeValue* nativeNBHValue = CreateJsValue(*engine_, static_cast(newBegin)); + NativeValue* nativeNEValue = CreateJsValue(*engine_, static_cast(newEnd)); NativeValue* argv[] = {nativeOBValue, nativeOEValue, nativeNBHValue, nativeNEValue}; std::string methodName = "selectionChange"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); }; mainHandler_->PostTask(task); @@ -192,11 +191,11 @@ namespace MiscServices { std::lock_guard lock(mMutex); IMSA_HILOGI("JsKeyboardDelegateListener::OnTextChange"); auto task = [this, text] () { - NativeValue* nativeValue = AbilityRuntime::CreateJsValue(*engine_, text); + NativeValue* nativeValue = CreateJsValue(*engine_, text); NativeValue* argv[] = {nativeValue}; std::string methodName = "textChange"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, ArraySize(argv)); }; mainHandler_->PostTask(task); diff --git a/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp b/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp index 9a65e4e..e13ab70 100644 --- a/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp @@ -23,6 +23,7 @@ #include "js_input_method_engine_utils.h" namespace OHOS { namespace MiscServices { + using namespace AbilityRuntime; constexpr size_t ARGC_ZERO = 0; constexpr size_t ARGC_ONE = 1; @@ -34,43 +35,43 @@ namespace MiscServices { NativeValue* JsTextInputClient::InsertText(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnInsertText(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::DeleteForward(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnDeleteForward(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::DeleteBackward(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnDeleteBackward(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::SendFunctionKey(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnSendFunctionKey(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::GetForward(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetForward(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::GetBackward(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetBackward(*engine, *info) : nullptr; } NativeValue* JsTextInputClient::GetEditorAttribute(NativeEngine* engine, NativeCallbackInfo* info) { - JsTextInputClient* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetEditorAttribute(*engine, *info) : nullptr; } @@ -86,14 +87,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; std::string textString; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, textString)) { + if (!ConvertFromJsValue(engine, nativeString, textString)) { IMSA_HILOGI("JsTextInputClient::OnInsertText Failed to convert parameter to string"); return engine.CreateUndefined(); } bool ret = InputMethodAbility::GetInstance()->InsertText(textString); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + NativeValue* result = CreateJsValue(engine, ret); return result; } @@ -110,14 +111,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsTextInputClient::OnDeleteForward Failed to convert parameter to string"); return engine.CreateUndefined(); } InputMethodAbility::GetInstance()->DeleteForward(number); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + NativeValue* result = CreateJsValue(engine, true); return result; } @@ -134,14 +135,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsTextInputClient::OnDeleteBackward Failed to convert parameter to string"); return engine.CreateUndefined(); } InputMethodAbility::GetInstance()->DeleteBackward(number); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + NativeValue* result = CreateJsValue(engine, true); return result; } @@ -158,14 +159,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsTextInputClient::OnSendFunctionKey Failed to convert parameter to string"); return engine.CreateUndefined(); } InputMethodAbility::GetInstance()->SendFunctionKey(number); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, true); + NativeValue* result = CreateJsValue(engine, true); return result; } @@ -182,14 +183,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsTextInputClient::OnGetForward Failed to convert parameter to string"); return engine.CreateUndefined(); } std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextBeforeCursor(number)); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + NativeValue* result = CreateJsValue(engine, ret); return result; } @@ -206,14 +207,14 @@ namespace MiscServices { nativeString = info.argv[ARGC_ZERO]; int32_t number; - if (!AbilityRuntime::ConvertFromJsValue(engine, nativeString, number)) { + if (!ConvertFromJsValue(engine, nativeString, number)) { IMSA_HILOGI("JsTextInputClient::OnGetBackward Failed to convert parameter to string"); return engine.CreateUndefined(); } std::string ret = Str16ToStr8(InputMethodAbility::GetInstance()->GetTextAfterCursor(number)); - NativeValue* result = AbilityRuntime::CreateJsValue(engine, ret); + NativeValue* result = CreateJsValue(engine, ret); return result; }