diff --git a/BUILD.gn b/BUILD.gn index bf070cec4b00b14a5aa6d21e956c286bd974fd27..05ef0e5e63f06cfe6dd3abed8b1f19584e8894b3 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 06338d533bf86b932e188cda8a1b37dc120f6c44..c0418e772a7e52f6d250d32889eafb6b5a9e9547 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_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/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_listener.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/include" + "header_base": "//base/miscservices/inputmethod/interfaces/kits/js/napi/inputmethodengine/include" } } ], diff --git a/etc/para/inputmethod.para b/etc/para/inputmethod.para index 6a7b185c4a4300cdf388da515a72149225eb5fd3..11bbb3b3a8ae08ca1f61c4bc9dcc066db095ab67 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 214f2424e113dd5efc95a4798cfcacf2250b491a..6fa93ce98f7b9cff76ec2afce857efe516419f27 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 88dce9dc6b1db143f9d91b9dc8ad303ecbfc09a6..63722517111b37a26ad2a784479490d9bcc93da8 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 7b4c1ee5347852e4a133435c544c23c41ed60d7d..7e6ddfbb4d84e686b07888dd451fe905cd5a6cc8 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 355637049db392dad73aa4860bdc38a68003d87a..ba952c115387a83eba9836fd7ca4c65c97b4529e 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 6bb314c8bc09162b40c2fb1c2455f88dbfdf0e00..2fc3af683d026f9ecaa1b393ca08b409e3e7c606 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 2d95e4693a964fc949be0de2fb75f422ab7e732b..4052fc2a987f58e3e6cd7513187d29275ac77854 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 d2db27f5e0c91441c5cfc226e1711a8c5e5b674b..d5708bab62ff4968072f06e02a0c19279f69669c 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 26d8512d452441b291de44bd56150c3c99e34ad7..9f860dbb21c587b89920c3902068be17e7c40c92 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 78cdfed2bc99b9933df0663816288bb022e2d849..8acfdfe37884be7c355d018d6787181208d2f0ec 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 a20901c56da1e613d9f7268133b35a55fd6b2920..4a1536a0ce2629dfbcffeb5d7e2257b86e2605c7 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 7d6207b577022b498e9a3bb64437ae2626f4946d..a72fc078a1f7b8d9a155e7570cf03925038da186 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 6b80035fa83866e759e56edb43013d0410a06d21..d9b9f3068d10792c8f2e9903892d7636d1d209b2 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 05bb100c6e42c22af9788f58fd413141c53cd666..f0a5d479081f4dee293790b9fe6f2279a4a9d1b9 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 4b8bd0113a1a7ccb2090aaa2e474c7ceb3d5b781..f0041243410ee15751cca3373da7ba252dfa8f1c 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 c8c5eac47224dd8887568a4e29a3e64a927dae97..99d5e0719533e74889b50f3e2f8f8c4a1207dfb1 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 a2b85610f71e980b102951aa956142f621644f48..2e15a56696c4ac25ba3b6b9c67cde85bb1c99278 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 eb1a433554d38e96bb0153ab2315abc7e77d8f1f..21a0f7f80a277c76d2f674b220602368d8efb54f 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 0000000000000000000000000000000000000000..9ff62bae0ab09dc31eb0953493e2b219a08a4dda --- /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 0ed3184f12d12ca873ee4ccc77b42a4633ae049e..3d089a700e2c7ceb3093620ef9ac72650e9a643e 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 0000000000000000000000000000000000000000..040fda60ceb263326157d4e9236cceed16b26d87 --- /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 3f67d5369b00406ca8e0398e066829002a888669..fa32354204bcff466bc13b966b304fea1380182d 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 50e65a85b19d6794eb94629bfff8a566dda2c17b..5d9d1dcb2b5afe584f0b570061a9ffea507e8cb7 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 0000000000000000000000000000000000000000..7e0c606d3aee20e62a8010c571708254be1e0072 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_controller.cpp @@ -0,0 +1,54 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = 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 = CreateJsValue(engine, true); + + return result; + } +} +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/js_input_method.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp similarity index 31% rename from interfaces/kits/js/napi/src/js_input_method.cpp rename to interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp index 7b931f9dcfa6455b32ce0aeacb93af86c24737a3..3c5911fdb0694d6fa6e8b9e7309eb115261cff75 100644 --- a/interfaces/kits/js/napi/src/js_input_method.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp @@ -13,122 +13,97 @@ * limitations under the License. */ -#include "js_input_method.h" +#include "js_input_method_registry.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 { + using namespace AbilityRuntime; namespace { constexpr size_t ARGC_ZERO = 0; + constexpr int32_t MAX_TYPE_NUM = 128; } - class JsInputMethod { + class JsInputMethodRegistry { public: - explicit JsInputMethod(NativeEngine* engine) + explicit JsInputMethodRegistry(NativeEngine* engine) { - IMSA_HILOGI("JsInputMethod::Constructor is called"); + IMSA_HILOGI("JsInputMethodRegistry::Constructor is called"); } - ~JsInputMethod() = default; + ~JsInputMethodRegistry() = default; static void Finalizer(NativeEngine* engine, void* data, void* hint) { - IMSA_HILOGI("JsInputMethod::Finalizer is called"); - std::unique_ptr(static_cast(data)); + IMSA_HILOGI("JsInputMethodRegistry::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); + IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodSetting is called"); + JsInputMethodRegistry* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnGetInputMethodSetting(*engine, *info) : nullptr; } - static NativeValue* DisplayOptionalInputMethod(NativeEngine* engine, NativeCallbackInfo* info) + static NativeValue* GetInputMethodController(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(); + IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodController is called"); + JsInputMethodRegistry* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetInputMethodController(*engine, *info) : nullptr; } - 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!"); + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodSetting is called!"); + if (info.argc != ARGC_ZERO) { + IMSA_HILOGI("JsInputMethodRegistry::OnGetInputMethodSetting Params not match"); + return engine.CreateUndefined(); + } - NativeValue* objValue = engine.CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); + return CreateInputMethodSetting(engine); + } - AbilityRuntime::BindNativeFunction(engine, *object, "displayOptionalInputMethod", - JsInputMethod::DisplayOptionalInputMethod); - AbilityRuntime::BindNativeFunction(engine, *object, "listInputMethod", JsInputMethod::ListInputMethod); + 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 objValue; + return CreateInputMethodController(engine); } }; - NativeValue* JsInputMethodInit(NativeEngine* engine, NativeValue* exportObj) + NativeValue* JsInputMethodRegistryInit(NativeEngine* engine, NativeValue* exportObj) { - IMSA_HILOGI("JsInputMethodInit() is called!"); + IMSA_HILOGI("JsInputMethodRegistryInit() is called!"); if (engine == nullptr || exportObj == nullptr) { IMSA_HILOGI("engine or exportObj null"); return nullptr; } - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(exportObj); + NativeObject* object = 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); + std::unique_ptr jsInputMethodRegistry = std::make_unique(engine); + object->SetNativePointer(jsInputMethodRegistry.release(), JsInputMethodRegistry::Finalizer, nullptr); + + BindNativeFunction(*engine, *object, "getInputMethodSetting", JsInputMethodRegistry::GetInputMethodSetting); + BindNativeFunction(*engine, *object, "getInputMethodController", JsInputMethodRegistry::GetInputMethodController); - // create inputMethod - AbilityRuntime::BindNativeFunction(*engine, *object, "getInputMethodSetting", - JsInputMethod::GetInputMethodSetting); + 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 new file mode 100644 index 0000000000000000000000000000000000000000..f89bc61750a570933749833c87fc7e7e2372cabf --- /dev/null +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_setting.cpp @@ -0,0 +1,88 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDisplayOptionalInputMethod(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodSetting::ListInputMethod(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodSetting* me = 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 = ConvertNativeValueTo(arrayValue); + uint32_t index = 0; + for (const auto &info : properties) { + NativeValue* objValue = engine.CreateObject(); + 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", CreateJsValue(engine, Str16ToStr8(info->mPackageName))); + object->SetProperty("methodId", 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 0000000000000000000000000000000000000000..231afaf7f924518980ac065c8b2d66e9e516536c --- /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 { + using namespace AbilityRuntime; + NativeValue* CreateInputMethodSetting(NativeEngine& engine) + { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodSetting is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + BindNativeFunction(engine, *object, "displayOptionalInputMethod", JsInputMethodSetting::DisplayOptionalInputMethod); + BindNativeFunction(engine, *object, "listInputMethod", JsInputMethodSetting::ListInputMethod); + return objValue; + } + + NativeValue* CreateInputMethodController(NativeEngine& engine) + { + IMSA_HILOGI("JsInputMethodUtils::CreateInputMethodController is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + 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 0000000000000000000000000000000000000000..30e925fb7fd1029c97e4db6cdc6a170ceb034b91 --- /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 0000000000000000000000000000000000000000..308cb341f44de25d833069c5b8cc3a6cde1a4ab7 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/include/js_editor_attribute.h @@ -0,0 +1,32 @@ +/* + * 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); + private: + }; + } +} +#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 0000000000000000000000000000000000000000..b13c1dc491aba4568b444618b5c2b1163fdb7c37 --- /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 e55f980cfd30a0f7f0fbb62b23e788622e2617bc..49dc77885c964444ca7bbb2c05044cf5a414a53a 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 0000000000000000000000000000000000000000..337ca6f07b0f0df1787d230c7405e6b50ab7b811 --- /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 0000000000000000000000000000000000000000..9fea1cd3c19a7589331d33bfc9be0f79703f9483 --- /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 0000000000000000000000000000000000000000..d9cd700526b5387a4c8fd47fb27b3eb9e6f21ab3 --- /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 0000000000000000000000000000000000000000..388f0849abd1102567d62a4e406bc6f957222058 --- /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 0000000000000000000000000000000000000000..8e62c92dfd327a056bee772a7f4ba7f4b67e72cf --- /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 0000000000000000000000000000000000000000..e14701a7394a563611f1e16cbc63055dd4a3a554 --- /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 a46c15dc181096c94c753f9ebc6989f22a3eb817..da577f59e303b61801413fe20dcc4c7596783f40 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 0000000000000000000000000000000000000000..b833d925d355fe2f8d3aa5be3de27afd470b2c72 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_editor_attribute.cpp @@ -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. + */ +#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)); + } +} +} \ 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 0000000000000000000000000000000000000000..ca145af535ea37240e8d391802ef07955903543d --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine.cpp @@ -0,0 +1,97 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsInputMethodEngine::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngine* me = 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 (!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 (!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 50% 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 93cdffcd12674355c41a1cf8ea8005a61926488d..429a08a4567c4e662ba01326284ce2e140f7d6fa 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,9 +15,11 @@ #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 { + using namespace AbilityRuntime; void JsInputMethodEngineListener::RegisterListenerWithType(NativeEngine& engine, std::string type, NativeValue* value) { @@ -119,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; } } @@ -132,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; @@ -144,125 +146,19 @@ namespace MiscServices { } else { methodName = "keyboardHide"; } - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + CallJsMethod(methodName, argv, 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* 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 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"; - CallJsMethod(methodName, argv, AbilityRuntime::ArraySize(argv)); + NativeValue *nativeValuekb = CreateKeyboardController(*engine_); + NativeValue *nativeValuetx = CreateTextInputClient(*engine_); + NativeValue* argv[] = {nativeValuekb, nativeValuetx}; + std::string methodName = "inputStart"; + CallJsMethod(methodName, argv, ArraySize(argv)); } void JsInputMethodEngineListener::OnInputStop(std::string imeId) @@ -271,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 new file mode 100644 index 0000000000000000000000000000000000000000..e76fbd789bfa1b5dfdedede5bf0f5b7edefee865 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_registry.cpp @@ -0,0 +1,174 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetInputMethodEngine(*engine, *info) : nullptr; + } + + static NativeValue* GetKeyboardDelegate(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngineRegistry* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetKeyboardDelegate(*engine, *info) : nullptr; + } + + static NativeValue* MoveCursor(NativeEngine* engine, NativeCallbackInfo* info) + { + JsInputMethodEngineRegistry* me = 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 (!ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsInputMethodEngineRegistry::OnMoveCursor Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->MoveCursor(number); + + NativeValue* result = 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 = 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); + + BindNativeFunction(*engine, *object, "getInputMethodEngine", JsInputMethodEngineRegistry::GetInputMethodEngine); + BindNativeFunction(*engine, *object, "createKeyboardDelegate", JsInputMethodEngineRegistry::GetKeyboardDelegate); + + 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", 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("DISPLAY_MODE_PART", CreateJsValue(*engine, static_cast(0))); + object->SetProperty("DISPLAY_MODE_FULL", 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))); + + BindNativeFunction(*engine, *object, "MoveCursor", JsInputMethodEngineRegistry::MoveCursor); + + 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(); + } +} +} \ 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 0000000000000000000000000000000000000000..8a1462bf2948be3d5ea3c47f5bf3d7c635c2c41e --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_input_method_engine_utils.cpp @@ -0,0 +1,126 @@ +/* + * 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 { + using namespace AbilityRuntime; + NativeValue* CreateInputMethodEngine(NativeEngine &engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateInputMethodEngine is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + BindNativeFunction(engine, *object, "on", JsInputMethodEngine::RegisterCallback); + BindNativeFunction(engine, *object, "off", JsInputMethodEngine::UnRegisterCallback); + return objValue; + } + + NativeValue* CreateKeyboardController(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardController is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + BindNativeFunction(engine, *object, "hideKeyboard", JsKeyboardController::HideKeyboardSelf); + + return objValue; + } + + NativeValue* CreateTextInputClient(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateTextInputClient is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + 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; + } + + NativeValue* CreateKeyboardDelegate(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateKeyboardDelegate is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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); + + BindNativeFunction(engine, *object, "on", JsKeyboardDelegate::RegisterCallback); + BindNativeFunction(engine, *object, "off", JsKeyboardDelegate::UnRegisterCallback); + return objValue; + } + + NativeValue* CreateEditorAttribute(NativeEngine & engine) + { + IMSA_HILOGI("JsInputMethodEngineUtils::CreateEditorAttribute is called"); + NativeValue *objValue = engine.CreateObject(); + NativeObject *object = 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", CreateJsValue(engine, InputMethodAbility::GetInstance()->GetEnterKeyType())); + object->SetProperty("inputPattern", 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 0000000000000000000000000000000000000000..afee60723b4982d659ced5c484fd9ef8bff12c29 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_controller.cpp @@ -0,0 +1,52 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = 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 0000000000000000000000000000000000000000..1111ae5656c6d4fed8f500dca7b9024412b45fd5 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate.cpp @@ -0,0 +1,107 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; + } + + NativeValue* JsKeyboardDelegate::UnRegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) + { + JsKeyboardDelegate* me = 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 (!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 (!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 0000000000000000000000000000000000000000..b5118fdc5c6f56229ed2ede58064188813fa542d --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_keyboard_delegate_listener.cpp @@ -0,0 +1,204 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 (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 = 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", 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) + { + std::lock_guard lock(mMutex); + IMSA_HILOGI("JsKeyboardDelegateListener::OnCursorUpdate"); + + auto task = [this, positionX, positionY, 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, 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 = 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, 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 = CreateJsValue(*engine_, text); + + NativeValue* argv[] = {nativeValue}; + std::string methodName = "textChange"; + CallJsMethod(methodName, argv, 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 0000000000000000000000000000000000000000..e13ab70acc5cfd04bd57a5e71bd4c3510bf7f482 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodengine/src/js_text_input_client.cpp @@ -0,0 +1,233 @@ +/* + * 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 { + using namespace AbilityRuntime; + 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 = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnInsertText(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::DeleteForward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDeleteForward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::DeleteBackward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnDeleteBackward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::SendFunctionKey(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnSendFunctionKey(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetForward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetForward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetBackward(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnGetBackward(*engine, *info) : nullptr; + } + + NativeValue* JsTextInputClient::GetEditorAttribute(NativeEngine* engine, NativeCallbackInfo* info) + { + JsTextInputClient* me = 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 (!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 = 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 (!ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnDeleteForward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->DeleteForward(number); + + NativeValue* result = 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 (!ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnDeleteBackward Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->DeleteBackward(number); + + NativeValue* result = 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 (!ConvertFromJsValue(engine, nativeString, number)) { + IMSA_HILOGI("JsTextInputClient::OnSendFunctionKey Failed to convert parameter to string"); + return engine.CreateUndefined(); + } + + InputMethodAbility::GetInstance()->SendFunctionKey(number); + + NativeValue* result = 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 (!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 = 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 (!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 = 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_engine.cpp b/interfaces/kits/js/napi/src/js_input_method_engine.cpp deleted file mode 100644 index 6b4261a795ef46bb943d5f8d229dc4d8e60d298a..0000000000000000000000000000000000000000 --- 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 8274570ae9f1ca5d11c2664f8301a28e9dd531b5..d688d98a9b3962e56816c4256398ec386bcdb4ca 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 96db418c4239c1d09a3216cd0f9d8b9304e74c82..df92a0245fb3ce54d29e980bb1d7c91b370590d7 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 b7e37f9f1e5170df15b054f5fd5df06b6480f9ec..9d050449030fe7e8abdb8bc7444ad66d466b750c 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 b38c4e89994e192e287d3de725ded2679c2225c8..6b2cd4e073b219ec83e8ea9a4dd081b34e8f5887 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 a67aa9cec786e24a9eeef46cb076b244da4fc5e0..53279815b5017876f932ee17fc066b0c18bd2e31 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 1146e25e6053d1bd088b7b34669b842ca63cda69..799bb79dc6c13c7bbb68a75a8606a7c9844d2782 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; }