From 3add5b6a1be1ae593bbad0568c7a7dd316d6862e Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Fri, 12 Aug 2022 20:33:46 +0800 Subject: [PATCH 1/8] add Show and Hide keyboard Signed-off-by: zhaolinglan --- BUILD.gn | 2 +- bundle.json | 14 ++-- .../include/i_input_method_core.h | 2 +- .../include/input_method_core_proxy.h | 2 +- .../include/input_method_core_stub.h | 2 +- .../src/input_method_ability.cpp | 6 +- .../src/input_method_core_proxy.cpp | 10 +-- .../src/input_method_core_stub.cpp | 6 +- .../include/input_method_controller.h | 5 +- .../input_method_system_ability_proxy.h | 1 + .../src/input_method_controller.cpp | 25 +++++-- .../src/input_method_system_ability_proxy.cpp | 22 +++++- .../js/declaration/@ohos.inputmethod.d.ts | 8 ++ .../kits/js/napi/inputmethodclient/BUILD.gn | 75 +++++++++++++++++++ .../js_get_input_method_controller.cpp | 54 +++++++++++++ .../js_get_input_method_controller.h | 55 ++++++++++++-- .../include/i_input_method_system_ability.h | 2 + .../input_method_system_ability_stub.h | 1 + services/include/message_handler.h | 1 + services/include/peruser_session.h | 3 +- services/src/input_method_system_ability.cpp | 1 + .../src/input_method_system_ability_stub.cpp | 19 +++++ services/src/peruser_session.cpp | 25 +++++-- 23 files changed, 299 insertions(+), 42 deletions(-) create mode 100644 interfaces/kits/js/napi/inputmethodclient/BUILD.gn diff --git a/BUILD.gn b/BUILD.gn index 30af9cd29..c8dbfd359 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -24,7 +24,7 @@ group("imf_packages") { "frameworks/inputmethod_ability:inputmethod_ability", "frameworks/inputmethod_controller:inputmethod_client", "interfaces/kits/js/declaration:inputmethod", - "interfaces/kits/js/napi/inputmethod:inputmethod", + "interfaces/kits/js/napi/inputmethodclient:inputmethod", "interfaces/kits/js/napi/inputmethodengine:inputmethodengine", "profile:inputmethod_inputmethod_sa_profiles", "services:inputmethod_service", diff --git a/bundle.json b/bundle.json index 26d0a96e3..7f61a454e 100644 --- a/bundle.json +++ b/bundle.json @@ -57,7 +57,7 @@ "//base/inputmethod/imf/frameworks/kits/extension:inputmethod_extension", "//base/inputmethod/imf/frameworks/kits/extension:inputmethod_extension_module", "//base/inputmethod/imf/interfaces/kits/js/declaration:inputmethod", - "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethod:inputmethod", + "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodclient:inputmethod", "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodengine:inputmethodengine", "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethod_extension_ability:inputmethodextensionability_napi", "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethod_extension_context:inputmethodextensioncontext_napi", @@ -105,15 +105,15 @@ } }, { - "name": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethod:inputmethod", + "name": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodclient:inputmethod", "header": { "header_files": [ - "js_input_method_registry.h", - "js_input_method_utils.h", - "js_input_method_setting.h", - "js_input_method_controller.h" + "async_call.h", + "js_get_input_method_controller.h", + "js_get_input_method_setting.h", + "js_input_method.h" ], - "header_base": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethod/include" + "header_base": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodclient" } }, { diff --git a/frameworks/inputmethod_ability/include/i_input_method_core.h b/frameworks/inputmethod_ability/include/i_input_method_core.h index 112aaac4f..a19d604f3 100644 --- a/frameworks/inputmethod_ability/include/i_input_method_core.h +++ b/frameworks/inputmethod_ability/include/i_input_method_core.h @@ -53,7 +53,7 @@ namespace MiscServices { const InputAttribute& editorAttribute, bool supportPhysicalKbd) = 0; virtual int32_t stopInput() = 0; - virtual bool showKeyboard(const sptr& inputDataChannel) = 0; + virtual bool showKeyboard(const sptr& inputDataChannel, bool isShowKeyboard) = 0; virtual bool hideKeyboard(int32_t flags) = 0; virtual int32_t setKeyboardType(const KeyboardType& type) = 0; virtual int32_t getKeyboardWindowHeight(int32_t &retHeight) = 0; diff --git a/frameworks/inputmethod_ability/include/input_method_core_proxy.h b/frameworks/inputmethod_ability/include/input_method_core_proxy.h index 9a1770b1d..5b07fd2f9 100644 --- a/frameworks/inputmethod_ability/include/input_method_core_proxy.h +++ b/frameworks/inputmethod_ability/include/input_method_core_proxy.h @@ -40,7 +40,7 @@ namespace MiscServices { const InputAttribute& editorAttribute, bool supportPhysicalKbd) override; int32_t stopInput() override; - bool showKeyboard(const sptr& inputDataChannel) override; + bool showKeyboard(const sptr& inputDataChannel, bool isShowKeyboard) override; bool hideKeyboard(int32_t flags) override; int32_t setKeyboardType(const KeyboardType& type) override; int32_t getKeyboardWindowHeight(int32_t &retHeight) override; diff --git a/frameworks/inputmethod_ability/include/input_method_core_stub.h b/frameworks/inputmethod_ability/include/input_method_core_stub.h index 0e6805649..a97e2b7a5 100644 --- a/frameworks/inputmethod_ability/include/input_method_core_stub.h +++ b/frameworks/inputmethod_ability/include/input_method_core_stub.h @@ -49,7 +49,7 @@ namespace MiscServices { const InputAttribute& editorAttribute, bool supportPhysicalKbd) override; int32_t stopInput() override; - bool showKeyboard(const sptr& inputDataChannel) override; + bool showKeyboard(const sptr& inputDataChannel, bool isWindowShow) override; bool hideKeyboard(int32_t flags)override; int32_t setKeyboardType(const KeyboardType& type) override; int32_t getKeyboardWindowHeight(int32_t &retHeight) override; diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index f8dbb06be..abd826d8c 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -243,7 +243,10 @@ namespace MiscServices { return; } SetInputDataChannel(channelObject); - ShowInputWindow(); + bool isShowKeyboard = data->ReadBool(); + if (isShowKeyboard) { + ShowInputWindow(); + } } void InputMethodAbility::OnHideKeyboard(Message *msg) @@ -328,7 +331,6 @@ namespace MiscServices { IMSA_HILOGI("InputMethodAbility::ShowInputWindow imeListener_ is nullptr"); return; } - imeListener_->OnInputStart(); imeListener_->OnKeyboardStatus(true); std::shared_ptr channel = GetInputDataChannel(); if (channel == nullptr) { diff --git a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp index a977547bb..27881b5ec 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp @@ -193,7 +193,7 @@ namespace MiscServices { return reply.ReadInt32(); } - bool InputMethodCoreProxy::showKeyboard(const sptr &inputDataChannel) + bool InputMethodCoreProxy::showKeyboard(const sptr &inputDataChannel, bool isShowKeyboard) { IMSA_HILOGI("InputMethodCoreProxy::showKeyboard"); auto remote = Remote(); @@ -203,14 +203,12 @@ namespace MiscServices { } MessageParcel data; - if (!(data.WriteInterfaceToken(GetDescriptor()) - && data.WriteRemoteObject(inputDataChannel->AsObject()))) { + if (!(data.WriteInterfaceToken(GetDescriptor()) && data.WriteRemoteObject(inputDataChannel->AsObject()) + && data.WriteBool(isShowKeyboard))) { return false; } MessageParcel reply; - MessageOption option { - MessageOption::TF_SYNC - }; + MessageOption option{ MessageOption::TF_SYNC }; int32_t res = remote->SendRequest(SHOW_KEYBOARD, data, reply, option); if (res != ErrorCode::NO_ERROR) { diff --git a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp index 0882cda47..5d99c27ef 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp @@ -110,7 +110,8 @@ namespace MiscServices { } case SHOW_KEYBOARD: { sptr inputDataChannel = iface_cast(data.ReadRemoteObject()); - showKeyboard(inputDataChannel); + bool isShowKeyboard = data.ReadBool(); + showKeyboard(inputDataChannel, isShowKeyboard); reply.WriteNoException(); break; } @@ -230,7 +231,7 @@ namespace MiscServices { msgHandler_->SendMessage(msg); } - bool InputMethodCoreStub::showKeyboard(const sptr& inputDataChannel) + bool InputMethodCoreStub::showKeyboard(const sptr& inputDataChannel, bool isShowKeyboard) { IMSA_HILOGI("InputMethodCoreStub::showKeyboard"); if (!msgHandler_) { @@ -240,6 +241,7 @@ namespace MiscServices { if (inputDataChannel) { IMSA_HILOGI("InputMethodCoreStub::showKeyboard inputDataChannel is not nullptr"); data->WriteRemoteObject(inputDataChannel->AsObject()); + data->WriteBool(isShowKeyboard); } Message *msg = new Message(MessageID::MSG_ID_SHOW_KEYBOARD, data); diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 36473c9c2..7753cfe19 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -58,7 +58,7 @@ namespace MiscServices { class InputMethodController : public RefBase { public: static sptr GetInstance(); - void Attach(sptr &listener); + void Attach(sptr &listener, bool isShowKeyboard = true); std::u16string GetTextBeforeCursor(int32_t number); std::u16string GetTextAfterCursor(int32_t number); void ShowTextInput(); @@ -74,6 +74,7 @@ namespace MiscServices { int32_t GetEnterKeyType(); int32_t GetInputPattern(); int32_t HideCurrentInput(); + int32_t ShowCurrentInput(); void SetCallingWindow(uint32_t windowId); int32_t SwitchInputMethod(const InputMethodProperty &target); @@ -85,7 +86,7 @@ namespace MiscServices { sptr GetImsaProxy(); void PrepareInput(int32_t displayId, sptr &client, sptr &channel, InputAttribute &attribute); - void StartInput(sptr &client); + void StartInput(sptr &client, bool isShowKeyboard); void StopInput(sptr &client); void ReleaseInput(sptr &client); void SetInputMethodAgent(sptr &object); 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 b89348b1b..6e47473cc 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -50,6 +50,7 @@ namespace MiscServices { void stopInput(MessageParcel& data) override; void SetCoreAndAgent(MessageParcel& data) override; int32_t HideCurrentInput(MessageParcel& data) override; + int32_t ShowCurrentInput(MessageParcel& data) override; int32_t Prepare(int32_t displayId, sptr &client, sptr &channel, InputAttribute &attribute); diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 44fe052d7..8ced55dde 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -199,19 +199,19 @@ using namespace MessageID; } } - void InputMethodController::Attach(sptr &listener) + void InputMethodController::Attach(sptr &listener, bool isShowKeyboard) { textListener = listener; IMSA_HILOGI("InputMethodController::Attach"); InputmethodTrace tracer("InputMethodController Attach trace."); - StartInput(mClient); + StartInput(mClient, isShowKeyboard); PrepareInput(0, mClient, mInputDataChannel, mAttribute); } void InputMethodController::ShowTextInput() { IMSA_HILOGI("InputMethodController::ShowTextInput"); - StartInput(mClient); + StartInput(mClient, true); } void InputMethodController::HideTextInput() @@ -233,6 +233,19 @@ using namespace MessageID; return mImms->HideCurrentInput(data); } + int32_t InputMethodController::ShowCurrentInput() + { + IMSA_HILOGI("InputMethodController::ShowCurrentInput"); + if (!mImms) { + return ErrorCode::ERROR_KBD_SHOW_FAILED; + } + MessageParcel data; + if (!(data.WriteInterfaceToken(mImms->GetDescriptor()))) { + return ErrorCode::ERROR_KBD_SHOW_FAILED; + } + return mImms->ShowCurrentInput(data); + } + void InputMethodController::Close() { ReleaseInput(mClient); @@ -283,15 +296,15 @@ using namespace MessageID; return properties; } - void InputMethodController::StartInput(sptr &client) + void InputMethodController::StartInput(sptr &client, bool isShowKeyboard) { IMSA_HILOGI("InputMethodController::StartInput"); if (!mImms) { return; } MessageParcel data; - if (!(data.WriteInterfaceToken(mImms->GetDescriptor()) - && data.WriteRemoteObject(client->AsObject()))) { + if (!(data.WriteInterfaceToken(mImms->GetDescriptor()) && data.WriteRemoteObject(client->AsObject()) + && data.WriteBool(isShowKeyboard))) { return; } isStopInput = false; 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 954f02817..df515342e 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -81,7 +81,7 @@ namespace MiscServices { } } - void InputMethodSystemAbilityProxy::startInput(MessageParcel& data) + void InputMethodSystemAbilityProxy::startInput(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityProxy::startInput"); MessageParcel reply; @@ -155,6 +155,26 @@ namespace MiscServices { return ErrorCode::NO_ERROR; } + int32_t InputMethodSystemAbilityProxy::ShowCurrentInput(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput"); + MessageParcel reply; + MessageOption option; + + auto ret = Remote()->SendRequest(SHOW_CURRENT_INPUT, data, reply, option); + if (ret != NO_ERROR) { + IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput SendRequest failed"); + return ErrorCode::ERROR_KBD_SHOW_FAILED; + } + + ret = reply.ReadInt32(); + if (ret != NO_ERROR) { + IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput reply failed"); + return ErrorCode::ERROR_KBD_SHOW_FAILED; + } + return ErrorCode::NO_ERROR; + } + int32_t InputMethodSystemAbilityProxy::Prepare(int32_t displayId, sptr &client, sptr &channel, InputAttribute &attribute) { diff --git a/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts b/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts index babe470dc..045c7f966 100644 --- a/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts +++ b/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts @@ -117,6 +117,14 @@ declare namespace inputMethod { stopInput(callback: AsyncCallback): void; stopInput(): Promise; + + showSoftKeyboard(callback: AsyncCallback): void; + + showSoftKeyboard():Promise; + + hideSoftKeyboard(callback: AsyncCallback): void; + + hideSoftKeyboard():Promise; } /** diff --git a/interfaces/kits/js/napi/inputmethodclient/BUILD.gn b/interfaces/kits/js/napi/inputmethodclient/BUILD.gn new file mode 100644 index 000000000..32fa7ebc3 --- /dev/null +++ b/interfaces/kits/js/napi/inputmethodclient/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (C) 2022 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/inputmethod/imf/inputmethod.gni") +import("//build/ohos.gni") + +config("imf_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +config("imf_public_config") { + visibility = [] + include_dirs = [ + "include", + "${inputmethod_path}/frameworks/inputmethod_controller/include", + "${inputmethod_path}/services/dfx/include", + ] +} + +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", + "async_call.cpp", + "input_method_module.cpp", + "js_get_input_method_controller.cpp", + "js_get_input_method_setting.cpp", + "js_input_method.cpp", + ] + + configs = [ ":imf_config" ] + + deps = [ + "${ability_runtime_inner_api_path}/ability_manager:ability_manager", + "${ability_runtime_inner_api_path}/app_manager:app_manager", + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${inputmethod_path}/services/dfx:inputmethod_dfx_static", + "//base/global/resource_management/frameworks/resmgr:global_resmgr", + "//base/inputmethod/imf/frameworks/inputmethod_ability:inputmethod_ability", + "//foundation/arkui/napi/:ace_napi", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", + "//foundation/multimodalinput/input/frameworks/proxy:libmmi-client", + "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:runtime", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + public_configs = [ ":imf_public_config" ] + + relative_install_dir = "module" + subsystem_name = "inputmethod" + part_name = "imf" +} \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 7523d0a14..3b788203e 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -32,6 +32,8 @@ napi_value JsGetInputMethodController::Init(napi_env env, napi_value info) napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("stopInput", StopInput), + DECLARE_NAPI_FUNCTION("hideSoftKeyboard", HideSoftKeyboard), + DECLARE_NAPI_FUNCTION("showSoftKeyboard", ShowSoftKeyboard), }; napi_value cons = nullptr; NAPI_CALL(env, napi_define_class(env, IMC_CLASS_NAME.c_str(), IMC_CLASS_NAME.size(), @@ -81,6 +83,58 @@ napi_value JsGetInputMethodController::GetInputMethodController(napi_env env, na return instance; } +napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " should null or 1 parameters!", napi_invalid_arg); + return napi_ok; + }; + auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { + napi_status status = napi_get_boolean(env, ctxt->isHideSoftKeyboard, result); + IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); + return status; + }; + auto exec = [ctxt](AsyncCall::Context *ctx) { + int32_t errCode = InputMethodController::GetInstance()->HideCurrentInput(); + IMSA_HILOGI("exec HideCurrentInput %{public}d", errCode); + if (errCode == ErrorCode::NO_ERROR) { + IMSA_HILOGI("exec HideCurrentInput success"); + ctxt->status = napi_ok; + ctxt->isHideSoftKeyboard = true; + } + }; + ctxt->SetAction(std::move(input), std::move(output)); + AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 0); + return asyncCall.Call(env, exec); +} + +napi_value JsGetInputMethodController::ShowSoftKeyboard(napi_env env, napi_callback_info info) +{ + auto ctxt = std::make_shared(); + auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " should null or 1 parameters!", napi_invalid_arg); + return napi_ok; + }; + auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { + napi_status status = napi_get_boolean(env, ctxt->isShowSoftKeyboard, result); + IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); + return status; + }; + auto exec = [ctxt](AsyncCall::Context *ctx) { + int32_t errCode = InputMethodController::GetInstance()->ShowCurrentInput(); + IMSA_HILOGI("exec ShowCurrentInput %{public}d", errCode); + if (errCode == ErrorCode::NO_ERROR) { + IMSA_HILOGI("exec ShowCurrentInput success"); + ctxt->status = napi_ok; + ctxt->isShowSoftKeyboard = true; + } + }; + ctxt->SetAction(std::move(input), std::move(output)); + AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 0); + return asyncCall.Call(env, exec); +} + napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h index e9b9009f1..98ab5aace 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -15,17 +15,57 @@ #ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H #define INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H -#include "global.h" #include "async_call.h" +#include "global.h" #include "js_input_method.h" namespace OHOS { namespace MiscServices { +struct HideSoftKeyboardContext : public AsyncCall::Context { + bool isHideSoftKeyboard = false; + napi_status status = napi_generic_failure; + HideSoftKeyboardContext() : Context(nullptr, nullptr){}; + HideSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + + napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override + { + NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + return Context::operator()(env, argc, argv, self); + } + napi_status operator()(napi_env env, napi_value *result) override + { + if (status != napi_ok) { + return status; + } + return Context::operator()(env, result); + } +}; + +struct ShowSoftKeyboardContext : public AsyncCall::Context { + bool isShowSoftKeyboard = false; + napi_status status = napi_generic_failure; + ShowSoftKeyboardContext() : Context(nullptr, nullptr){}; + ShowSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + + napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override + { + NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + return Context::operator()(env, argc, argv, self); + } + napi_status operator()(napi_env env, napi_value *result) override + { + if (status != napi_ok) { + return status; + } + return Context::operator()(env, result); + } +}; + struct StopInputContext : public AsyncCall::Context { bool isStopInput = false; napi_status status = napi_generic_failure; - StopInputContext() : Context(nullptr, nullptr) { }; - StopInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; + StopInputContext() : Context(nullptr, nullptr){}; + StopInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override { @@ -47,12 +87,15 @@ public: ~JsGetInputMethodController() = default; static napi_value Init(napi_env env, napi_value info); static napi_value GetInputMethodController(napi_env env, napi_callback_info info); - static napi_value StopInput(napi_env env, napi_callback_info Info); + static napi_value HideSoftKeyboard(napi_env env, napi_callback_info info); + static napi_value ShowSoftKeyboard(napi_env env, napi_callback_info info); + static napi_value StopInput(napi_env env, napi_callback_info info); + private: static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); static const std::string IMC_CLASS_NAME; static thread_local napi_ref IMCRef_; }; -} -} +} // namespace MiscServices +} // namespace OHOS #endif // INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index 611f33e5b..c94d5fb9d 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -38,6 +38,7 @@ namespace MiscServices { START_INPUT, STOP_INPUT, HIDE_CURRENT_INPUT, + SHOW_CURRENT_INPUT, SET_INPUT_METHOD_CORE, GET_DISPLAY_MODE, GET_KEYBOARD_WINDOW_HEIGHT, @@ -58,6 +59,7 @@ namespace MiscServices { virtual void stopInput(MessageParcel& data) = 0; virtual void SetCoreAndAgent(MessageParcel& data) = 0; virtual int32_t HideCurrentInput(MessageParcel& data) = 0; + virtual int32_t ShowCurrentInput(MessageParcel& data) = 0; virtual int32_t 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 68a407ae0..8e12227f6 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -35,6 +35,7 @@ namespace MiscServices { void stopInput(MessageParcel& data) override; void SetCoreAndAgent(MessageParcel& data) override; int32_t HideCurrentInput(MessageParcel& data) override; + int32_t ShowCurrentInput(MessageParcel& data) override; int32_t displayOptionalInputMethod(MessageParcel& data) override; virtual int32_t listInputMethodByUserId(int32_t userId, std::vector *properties) = 0; int32_t SwitchInputMethod(MessageParcel &data); diff --git a/services/include/message_handler.h b/services/include/message_handler.h index 9ab10d6c9..2cf88b56d 100644 --- a/services/include/message_handler.h +++ b/services/include/message_handler.h @@ -46,6 +46,7 @@ namespace MessageID { MSG_ID_RELEASE_INPUT, // release input MSG_ID_SET_CORE_AND_AGENT, MSG_HIDE_CURRENT_INPUT, + MSG_SHOW_CURRENT_INPUT, MSG_ID_SWITCH_INPUT_METHOD, // switch input method // the request to handle the condition that the remote object died diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index a8e9dd720..76885a86f 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -171,6 +171,7 @@ namespace MiscServices { void OnClientDied(const wptr& who); void OnImsDied(const wptr& who); void OnHideKeyboardSelf(int flags); + void OnShowKeyboardSelf(int flags); void OnAdvanceToNext(); void OnSetDisplayMode(int mode); void OnRestartIms(int index, const std::u16string& imeId); @@ -181,7 +182,7 @@ namespace MiscServices { int RemoveClient(const sptr& inputClient, int retClientNum); int StartInputMethod(int index); int StopInputMethod(int index); - int ShowKeyboard(const sptr& inputClient); + int ShowKeyboard(const sptr& inputClient, bool isShowKeyboard); int HideKeyboard(const sptr& inputClient); void SetDisplayId(int displayId); int GetImeIndex(const sptr& inputClient); diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 9fc901517..5590adba2 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -603,6 +603,7 @@ namespace MiscServices { case MSG_ID_START_INPUT: case MSG_ID_STOP_INPUT: case MSG_HIDE_CURRENT_INPUT: + case MSG_SHOW_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 85992e86d..12079283a 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -170,6 +170,11 @@ namespace MiscServices { reply.WriteInt32(NO_ERROR); break; } + case SHOW_CURRENT_INPUT: { + ShowCurrentInput(data); + reply.WriteInt32(NO_ERROR); + break; + } case SWITCH_INPUT_METHOD: { int32_t ret = SwitchInputMethod(data); reply.WriteInt32(ret); @@ -255,6 +260,7 @@ namespace MiscServices { MessageParcel *parcel = new MessageParcel(); parcel->WriteInt32(userId); parcel->WriteRemoteObject(data.ReadRemoteObject()); + parcel->WriteBool(data.ReadBool()); Message *msg = new Message(MSG_ID_START_INPUT, parcel); MessageHandler::Instance()->SendMessage(msg); @@ -311,6 +317,19 @@ namespace MiscServices { return ErrorCode::NO_ERROR; } + int32_t InputMethodSystemAbilityStub::ShowCurrentInput(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::ShowCurrentInput"); + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = getUserId(uid); + MessageParcel *parcel = new MessageParcel(); + parcel->WriteInt32(userId); + + Message *msg = new Message(MSG_SHOW_CURRENT_INPUT, parcel); + MessageHandler::Instance()->SendMessage(msg); + return ErrorCode::NO_ERROR; + } + int32_t InputMethodSystemAbilityStub::SwitchInputMethod(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityStub::switchInputMethod"); diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 6f5793596..b75c2df3b 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -186,6 +186,10 @@ namespace MiscServices { OnHideKeyboardSelf(0); break; } + case MSG_SHOW_CURRENT_INPUT: { + OnShowKeyboardSelf(0); + break; + } default: { break; } @@ -271,7 +275,7 @@ namespace MiscServices { break; } if (needReshowClient && GetImeIndex(needReshowClient) == i) { - ShowKeyboard(needReshowClient); + ShowKeyboard(needReshowClient, true); needReshowClient = nullptr; } } @@ -478,7 +482,7 @@ namespace MiscServices { \return ErrorCode::ERROR_KBD_SHOW_FAILED failed to show keyboard \return other errors returned by binder driver */ - int PerUserSession::ShowKeyboard(const sptr& inputClient) + int PerUserSession::ShowKeyboard(const sptr& inputClient, bool isShowKeyboard) { IMSA_HILOGI("PerUserSession::ShowKeyboard"); ClientInfo *clientInfo = GetClientInfo(inputClient); @@ -493,7 +497,7 @@ namespace MiscServices { return ErrorCode::ERROR_NULL_POINTER; } - imsCore[0]->showKeyboard(clientInfo->channel); + imsCore[0]->showKeyboard(clientInfo->channel, isShowKeyboard); currentClient = inputClient; return ErrorCode::NO_ERROR; @@ -799,6 +803,16 @@ namespace MiscServices { HideKeyboard(currentClient); } + /*! Show current keyboard + \param flag the flag to show keyboard. + */ + void PerUserSession::OnShowKeyboardSelf(int flags) + { + IMSA_HILOGI("PerUserSession::OnShowKeyboardSelf"); + (void) flags; + ShowKeyboard(currentClient, true); + } + /*! Switch to next keyboard type */ void PerUserSession::OnAdvanceToNext() @@ -875,7 +889,7 @@ namespace MiscServices { int ret = StartInputMethod(index); if (needReshowClient && GetImeIndex(needReshowClient) == index) { if (ret == ErrorCode::NO_ERROR) { - ShowKeyboard(needReshowClient); + ShowKeyboard(needReshowClient, true); } needReshowClient = nullptr; } @@ -1253,7 +1267,8 @@ namespace MiscServices { if (imsCore[0]) { imsCore[0]->SetClientState(true); } - ShowKeyboard(client); + bool isShowKeyboard = data->ReadBool(); + ShowKeyboard(client, isShowKeyboard); } void PerUserSession::SetCoreAndAgent(Message *msg) -- Gitee From 5ff712012b4c313b2a1fdf568a0cceca551d9eb3 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 13 Aug 2022 10:35:54 +0800 Subject: [PATCH 2/8] modify for codecheck Signed-off-by: zhaolinglan --- .../include/input_method_core_stub.h | 2 +- .../src/input_method_core_proxy.cpp | 2 +- .../js_get_input_method_controller.h | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/inputmethod_ability/include/input_method_core_stub.h b/frameworks/inputmethod_ability/include/input_method_core_stub.h index a97e2b7a5..61d64167d 100644 --- a/frameworks/inputmethod_ability/include/input_method_core_stub.h +++ b/frameworks/inputmethod_ability/include/input_method_core_stub.h @@ -49,7 +49,7 @@ namespace MiscServices { const InputAttribute& editorAttribute, bool supportPhysicalKbd) override; int32_t stopInput() override; - bool showKeyboard(const sptr& inputDataChannel, bool isWindowShow) override; + bool showKeyboard(const sptr& inputDataChannel, bool isShowKeyboard) override; bool hideKeyboard(int32_t flags)override; int32_t setKeyboardType(const KeyboardType& type) override; int32_t getKeyboardWindowHeight(int32_t &retHeight) override; diff --git a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp index 27881b5ec..a310a1b6c 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_proxy.cpp @@ -208,7 +208,7 @@ namespace MiscServices { return false; } MessageParcel reply; - MessageOption option{ MessageOption::TF_SYNC }; + MessageOption option { MessageOption::TF_SYNC }; int32_t res = remote->SendRequest(SHOW_KEYBOARD, data, reply, option); if (res != ErrorCode::NO_ERROR) { diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h index 98ab5aace..319eca7ef 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -24,8 +24,8 @@ namespace MiscServices { struct HideSoftKeyboardContext : public AsyncCall::Context { bool isHideSoftKeyboard = false; napi_status status = napi_generic_failure; - HideSoftKeyboardContext() : Context(nullptr, nullptr){}; - HideSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + HideSoftKeyboardContext() : Context(nullptr, nullptr) { }; + HideSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override { @@ -44,8 +44,8 @@ struct HideSoftKeyboardContext : public AsyncCall::Context { struct ShowSoftKeyboardContext : public AsyncCall::Context { bool isShowSoftKeyboard = false; napi_status status = napi_generic_failure; - ShowSoftKeyboardContext() : Context(nullptr, nullptr){}; - ShowSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + ShowSoftKeyboardContext() : Context(nullptr, nullptr) { }; + ShowSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override { @@ -64,8 +64,8 @@ struct ShowSoftKeyboardContext : public AsyncCall::Context { struct StopInputContext : public AsyncCall::Context { bool isStopInput = false; napi_status status = napi_generic_failure; - StopInputContext() : Context(nullptr, nullptr){}; - StopInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + StopInputContext() : Context(nullptr, nullptr) { }; + StopInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override { -- Gitee From 0e979b93135debc9628b0f6d44c1ed8f50637ccd Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Sat, 13 Aug 2022 12:31:03 +0800 Subject: [PATCH 3/8] modify some code Signed-off-by: zhaolinglan --- bundle.json | 27 ------------------- .../include/input_method_ability.h | 2 +- .../src/input_method_ability.cpp | 11 +++++--- .../kits/js/napi/inputmethodclient/BUILD.gn | 2 +- services/src/peruser_session.cpp | 3 --- 5 files changed, 9 insertions(+), 36 deletions(-) diff --git a/bundle.json b/bundle.json index 7f61a454e..3c8dcb6f7 100644 --- a/bundle.json +++ b/bundle.json @@ -103,33 +103,6 @@ ], "header_base": "//base/inputmethod/imf/frameworks/inputmethod_ability/include" } - }, - { - "name": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodclient:inputmethod", - "header": { - "header_files": [ - "async_call.h", - "js_get_input_method_controller.h", - "js_get_input_method_setting.h", - "js_input_method.h" - ], - "header_base": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodclient" - } - }, - { - "name": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodengine:inputmethodengine", - "header": { - "header_files": [ - "js_input_method_engine_registry.h", - "js_input_method_engine_listener.h", - "js_input_method_engine.h", - "js_input_method_engine_utils.h", - "js_keyboard_controller.h", - "js_text_input_client.h", - "js_keyboard_delegate.h" - ], - "header_base": "//base/inputmethod/imf/interfaces/kits/js/napi/inputmethodengine/include" - } } ], "test": [ diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index 5f5c54b5a..639b7941a 100644 --- a/frameworks/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/inputmethod_ability/include/input_method_ability.h @@ -107,7 +107,7 @@ namespace MiscServices { void OnSelectionChange(Message *msg); void InitialInputWindow(); - void ShowInputWindow(); + void ShowInputWindow(bool isShowKeyboard); void DissmissInputWindow(); }; } // namespace MiscServices diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index abd826d8c..61d08c054 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -244,9 +244,7 @@ namespace MiscServices { } SetInputDataChannel(channelObject); bool isShowKeyboard = data->ReadBool(); - if (isShowKeyboard) { - ShowInputWindow(); - } + ShowInputWindow(isShowKeyboard); } void InputMethodAbility::OnHideKeyboard(Message *msg) @@ -324,13 +322,18 @@ namespace MiscServices { kdListener_->OnSelectionChange(oldBegin, oldEnd, newBegin, newEnd); } - void InputMethodAbility::ShowInputWindow() + void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); if (!imeListener_) { IMSA_HILOGI("InputMethodAbility::ShowInputWindow imeListener_ is nullptr"); return; } + imeListener_->OnInputStart(); + if (!isShowKeyboard) { + IMSA_HILOGI("InputMethodAbility::ShowInputWindow will not show keyboard"); + return; + } imeListener_->OnKeyboardStatus(true); std::shared_ptr channel = GetInputDataChannel(); if (channel == nullptr) { diff --git a/interfaces/kits/js/napi/inputmethodclient/BUILD.gn b/interfaces/kits/js/napi/inputmethodclient/BUILD.gn index 32fa7ebc3..716626a91 100644 --- a/interfaces/kits/js/napi/inputmethodclient/BUILD.gn +++ b/interfaces/kits/js/napi/inputmethodclient/BUILD.gn @@ -72,4 +72,4 @@ ohos_shared_library("inputmethod") { relative_install_dir = "module" subsystem_name = "inputmethod" part_name = "imf" -} \ No newline at end of file +} diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index b75c2df3b..668086389 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -803,9 +803,6 @@ namespace MiscServices { HideKeyboard(currentClient); } - /*! Show current keyboard - \param flag the flag to show keyboard. - */ void PerUserSession::OnShowKeyboardSelf(int flags) { IMSA_HILOGI("PerUserSession::OnShowKeyboardSelf"); -- Gitee From f0de637e09911d4a8ae6627974fd6fdf760d2c02 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 15 Aug 2022 10:54:13 +0800 Subject: [PATCH 4/8] modify some code Signed-off-by: zhaolinglan --- .../src/input_method_core_stub.cpp | 11 ++- .../src/input_method_controller.cpp | 1 + .../js/declaration/@ohos.inputmethod.d.ts | 36 +++++++++- .../js_get_input_method_controller.cpp | 69 +++++-------------- .../js_get_input_method_controller.h | 49 ++----------- services/include/peruser_session.h | 2 +- .../src/input_method_system_ability_stub.cpp | 15 ++-- services/src/peruser_session.cpp | 5 +- 8 files changed, 82 insertions(+), 106 deletions(-) diff --git a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp index 5d99c27ef..575cb0a23 100644 --- a/frameworks/inputmethod_ability/src/input_method_core_stub.cpp +++ b/frameworks/inputmethod_ability/src/input_method_core_stub.cpp @@ -237,14 +237,21 @@ namespace MiscServices { if (!msgHandler_) { return false; } - MessageParcel *data = new MessageParcel(); + auto *data = new (std::nothrow) MessageParcel(); + if (data == nullptr) { + return false; + } + IMSA_HILOGI("InputMethodCoreStub::showKeyboard isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); if (inputDataChannel) { IMSA_HILOGI("InputMethodCoreStub::showKeyboard inputDataChannel is not nullptr"); data->WriteRemoteObject(inputDataChannel->AsObject()); data->WriteBool(isShowKeyboard); } - Message *msg = new Message(MessageID::MSG_ID_SHOW_KEYBOARD, data); + Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_SHOW_KEYBOARD, data); + if (msg == nullptr) { + return false; + } msgHandler_->SendMessage(msg); return true; } diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 8ced55dde..227f79533 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -204,6 +204,7 @@ using namespace MessageID; textListener = listener; IMSA_HILOGI("InputMethodController::Attach"); InputmethodTrace tracer("InputMethodController Attach trace."); + IMSA_HILOGI("InputMethodController::Attach isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); StartInput(mClient, isShowKeyboard); PrepareInput(0, mClient, mInputDataChannel, mAttribute); } diff --git a/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts b/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts index 045c7f966..b28b69f36 100644 --- a/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts +++ b/interfaces/kits/js/declaration/@ohos.inputmethod.d.ts @@ -118,12 +118,46 @@ declare namespace inputMethod { stopInput(): Promise; + /** + * Show soft keyboard + * @since 9 + * @param callback + * @return : + * if true, success. + * if false, fail. + * @syscap SystemCapability.MiscServices.InputMethodFramework + */ showSoftKeyboard(callback: AsyncCallback): void; + /** + * Show soft keyboard + * @since 9 + * @return : + * if true, success. + * if false, fail. + * @syscap SystemCapability.MiscServices.InputMethodFramework + */ showSoftKeyboard():Promise; - hideSoftKeyboard(callback: AsyncCallback): void; + /** + * Hide soft keyboard + * @since 9 + * @param callback + * @return : + * if true, success. + * if false, fail. + * @syscap SystemCapability.MiscServices.InputMethodFramework + */ + hideSoftKeyboard(callback: AsyncCallback): void; + /** + * Hide soft keyboard + * @since 9 + * @return : + * if true, success. + * if false, fail. + * @syscap SystemCapability.MiscServices.InputMethodFramework + */ hideSoftKeyboard():Promise; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 3b788203e..fb9306e57 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -83,25 +83,26 @@ napi_value JsGetInputMethodController::GetInputMethodController(napi_env env, na return instance; } -napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callback_info info) +napi_value JsGetInputMethodController::HandleSoftKeyboard( + napi_env env, napi_callback_info info, std::function callback) { - auto ctxt = std::make_shared(); + auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " should null or 1 parameters!", napi_invalid_arg); return napi_ok; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { - napi_status status = napi_get_boolean(env, ctxt->isHideSoftKeyboard, result); + napi_status status = napi_get_boolean(env, ctxt->isHandle, result); IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); return status; }; auto exec = [ctxt](AsyncCall::Context *ctx) { - int32_t errCode = InputMethodController::GetInstance()->HideCurrentInput(); - IMSA_HILOGI("exec HideCurrentInput %{public}d", errCode); + int32_t errCode = callback(); + IMSA_HILOGI("exec %{public}d", errCode); if (errCode == ErrorCode::NO_ERROR) { - IMSA_HILOGI("exec HideCurrentInput success"); + IMSA_HILOGI("exec success"); ctxt->status = napi_ok; - ctxt->isHideSoftKeyboard = true; + ctxt->isHandle = true; } }; ctxt->SetAction(std::move(input), std::move(output)); @@ -111,54 +112,20 @@ napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callb napi_value JsGetInputMethodController::ShowSoftKeyboard(napi_env env, napi_callback_info info) { - auto ctxt = std::make_shared(); - auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { - NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " should null or 1 parameters!", napi_invalid_arg); - return napi_ok; - }; - auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { - napi_status status = napi_get_boolean(env, ctxt->isShowSoftKeyboard, result); - IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); - return status; - }; - auto exec = [ctxt](AsyncCall::Context *ctx) { - int32_t errCode = InputMethodController::GetInstance()->ShowCurrentInput(); - IMSA_HILOGI("exec ShowCurrentInput %{public}d", errCode); - if (errCode == ErrorCode::NO_ERROR) { - IMSA_HILOGI("exec ShowCurrentInput success"); - ctxt->status = napi_ok; - ctxt->isShowSoftKeyboard = true; - } - }; - ctxt->SetAction(std::move(input), std::move(output)); - AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 0); - return asyncCall.Call(env, exec); + return HandleSoftKeyboard( + env, info, []() -> int32_t { return InputMethodController::GetInstance()->ShowCurrentInput(); }) +} + +napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callback_info info) +{ + return HandleSoftKeyboard( + env, info, []() -> int32_t { return InputMethodController::GetInstance()->HideCurrentInput(); }) } napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_info info) { - auto ctxt = std::make_shared(); - auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { - NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " should null or 1 parameters!", napi_invalid_arg); - return napi_ok; - }; - auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { - napi_status status = napi_get_boolean(env, ctxt->isStopInput, result); - IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); - return status; - }; - auto exec = [ctxt](AsyncCall::Context *ctx) { - int32_t errCode = InputMethodController::GetInstance()->HideCurrentInput(); - IMSA_HILOGE("exec HideCurrentInput %{public}d", errCode); - if (errCode == ErrorCode::NO_ERROR) { - IMSA_HILOGE("exec HideCurrentInput success"); - ctxt->status = napi_ok; - ctxt->isStopInput = true; - } - }; - ctxt->SetAction(std::move(input), std::move(output)); - AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 0); - return asyncCall.Call(env, exec); + return HandleSoftKeyboard( + env, info, []() -> int32_t { return InputMethodController::GetInstance()->HideCurrentInput(); }) } } } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h index 319eca7ef..8dbf9cca8 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -21,51 +21,11 @@ namespace OHOS { namespace MiscServices { -struct HideSoftKeyboardContext : public AsyncCall::Context { - bool isHideSoftKeyboard = false; +struct HandleContext : public AsyncCall::Context { + bool isHandle = false; napi_status status = napi_generic_failure; - HideSoftKeyboardContext() : Context(nullptr, nullptr) { }; - HideSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; - - napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override - { - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - return Context::operator()(env, argc, argv, self); - } - napi_status operator()(napi_env env, napi_value *result) override - { - if (status != napi_ok) { - return status; - } - return Context::operator()(env, result); - } -}; - -struct ShowSoftKeyboardContext : public AsyncCall::Context { - bool isShowSoftKeyboard = false; - napi_status status = napi_generic_failure; - ShowSoftKeyboardContext() : Context(nullptr, nullptr) { }; - ShowSoftKeyboardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; - - napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override - { - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - return Context::operator()(env, argc, argv, self); - } - napi_status operator()(napi_env env, napi_value *result) override - { - if (status != napi_ok) { - return status; - } - return Context::operator()(env, result); - } -}; - -struct StopInputContext : public AsyncCall::Context { - bool isStopInput = false; - napi_status status = napi_generic_failure; - StopInputContext() : Context(nullptr, nullptr) { }; - StopInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; + HandleContext() : Context(nullptr, nullptr) { }; + HandleContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override { @@ -87,6 +47,7 @@ public: ~JsGetInputMethodController() = default; static napi_value Init(napi_env env, napi_value info); static napi_value GetInputMethodController(napi_env env, napi_callback_info info); + static napi_value HandleSoftKeyboard(napi_env env, napi_callback_info info, std::function callback); static napi_value HideSoftKeyboard(napi_env env, napi_callback_info info); static napi_value ShowSoftKeyboard(napi_env env, napi_callback_info info); static napi_value StopInput(napi_env env, napi_callback_info info); diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index 76885a86f..03aa40f30 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -171,7 +171,7 @@ namespace MiscServices { void OnClientDied(const wptr& who); void OnImsDied(const wptr& who); void OnHideKeyboardSelf(int flags); - void OnShowKeyboardSelf(int flags); + void OnShowKeyboardSelf(); void OnAdvanceToNext(); void OnSetDisplayMode(int mode); void OnRestartIms(int index, const std::u16string& imeId); diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 12079283a..90b0691eb 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -171,8 +171,8 @@ namespace MiscServices { break; } case SHOW_CURRENT_INPUT: { - ShowCurrentInput(data); - reply.WriteInt32(NO_ERROR); + int32_t ret = ShowCurrentInput(data); + reply.WriteInt32(ret); break; } case SWITCH_INPUT_METHOD: { @@ -322,10 +322,17 @@ namespace MiscServices { IMSA_HILOGI("InputMethodSystemAbilityStub::ShowCurrentInput"); int32_t uid = IPCSkeleton::GetCallingUid(); int32_t userId = getUserId(uid); - MessageParcel *parcel = new MessageParcel(); + + auto *parcel = new (std::nothrow) MessageParcel(); + if (parcel == nullptr) { + return ErrorCode::ERROR_EX_NULL_POINTER; + } parcel->WriteInt32(userId); - Message *msg = new Message(MSG_SHOW_CURRENT_INPUT, parcel); + auto *msg = new (std::nothrow) Message(MSG_SHOW_CURRENT_INPUT, parcel); + if (msg == nullptr) { + return ErrorCode::ERROR_EX_NULL_POINTER; + } MessageHandler::Instance()->SendMessage(msg); return ErrorCode::NO_ERROR; } diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 668086389..c357d8f70 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -187,7 +187,7 @@ namespace MiscServices { break; } case MSG_SHOW_CURRENT_INPUT: { - OnShowKeyboardSelf(0); + OnShowKeyboardSelf(); break; } default: { @@ -803,10 +803,9 @@ namespace MiscServices { HideKeyboard(currentClient); } - void PerUserSession::OnShowKeyboardSelf(int flags) + void PerUserSession::OnShowKeyboardSelf() { IMSA_HILOGI("PerUserSession::OnShowKeyboardSelf"); - (void) flags; ShowKeyboard(currentClient, true); } -- Gitee From 528550d04522a543818b4781b423dbc6d52dfb92 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 15 Aug 2022 15:01:34 +0800 Subject: [PATCH 5/8] modify some code Signed-off-by: zhaolinglan --- .../js_get_input_method_controller.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index fb9306e57..65d023e41 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -96,8 +96,8 @@ napi_value JsGetInputMethodController::HandleSoftKeyboard( IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); return status; }; - auto exec = [ctxt](AsyncCall::Context *ctx) { - int32_t errCode = callback(); + auto exec = [ctxt, callback](AsyncCall::Context *ctx) { + int errCode = callback(); IMSA_HILOGI("exec %{public}d", errCode); if (errCode == ErrorCode::NO_ERROR) { IMSA_HILOGI("exec success"); @@ -113,19 +113,19 @@ napi_value JsGetInputMethodController::HandleSoftKeyboard( napi_value JsGetInputMethodController::ShowSoftKeyboard(napi_env env, napi_callback_info info) { return HandleSoftKeyboard( - env, info, []() -> int32_t { return InputMethodController::GetInstance()->ShowCurrentInput(); }) + env, info, []{ return InputMethodController::GetInstance()->ShowCurrentInput(); }); } napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callback_info info) { return HandleSoftKeyboard( - env, info, []() -> int32_t { return InputMethodController::GetInstance()->HideCurrentInput(); }) + env, info, []{ return InputMethodController::GetInstance()->HideCurrentInput(); }); } napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_info info) { return HandleSoftKeyboard( - env, info, []() -> int32_t { return InputMethodController::GetInstance()->HideCurrentInput(); }) + env, info, []{ return InputMethodController::GetInstance()->HideCurrentInput(); }); } } } -- Gitee From 36bf6e6f5fd096d29139f260f74d09670f808a32 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 15 Aug 2022 15:56:01 +0800 Subject: [PATCH 6/8] modify for codecheck and tdd Signed-off-by: zhaolinglan --- .../js_get_input_method_controller.cpp | 13 +++++-------- unitest/src/input_method_controller_test.cpp | 8 ++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 65d023e41..8c2a8b26c 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -112,20 +112,17 @@ napi_value JsGetInputMethodController::HandleSoftKeyboard( napi_value JsGetInputMethodController::ShowSoftKeyboard(napi_env env, napi_callback_info info) { - return HandleSoftKeyboard( - env, info, []{ return InputMethodController::GetInstance()->ShowCurrentInput(); }); + return HandleSoftKeyboard(env, info, [] { return InputMethodController::GetInstance()->ShowCurrentInput(); }); } napi_value JsGetInputMethodController::HideSoftKeyboard(napi_env env, napi_callback_info info) { - return HandleSoftKeyboard( - env, info, []{ return InputMethodController::GetInstance()->HideCurrentInput(); }); + return HandleSoftKeyboard(env, info, [] { return InputMethodController::GetInstance()->HideCurrentInput(); }); } napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_info info) { - return HandleSoftKeyboard( - env, info, []{ return InputMethodController::GetInstance()->HideCurrentInput(); }); -} -} + return HandleSoftKeyboard(env, info, [] { return InputMethodController::GetInstance()->HideCurrentInput(); }); } +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/unitest/src/input_method_controller_test.cpp b/unitest/src/input_method_controller_test.cpp index 93194f6d0..ddbc1f1e2 100644 --- a/unitest/src/input_method_controller_test.cpp +++ b/unitest/src/input_method_controller_test.cpp @@ -218,6 +218,14 @@ namespace MiscServices { int waitForStatusOk = 2; sleep(waitForStatusOk); + IMSA_HILOGI("IMC Attach isShowKeyboard true START"); + imc->Attach(textListener, true); + sleep(waitForStatusOk); + + IMSA_HILOGI("IMC Attach isShowKeyboard false START"); + imc->Attach(textListener, false); + sleep(waitForStatusOk); + IMSA_HILOGI("IMC ShowTextInput START"); imc->ShowTextInput(); sleep(10); -- Gitee From 703b343a4a5fb4e123215115899a259b9d98886c Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 15 Aug 2022 23:46:58 +0800 Subject: [PATCH 7/8] modify attach Signed-off-by: zhaolinglan --- .../inputmethod_controller/include/input_method_controller.h | 1 + .../inputmethod_controller/src/input_method_controller.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 7753cfe19..3c031a255 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -58,6 +58,7 @@ namespace MiscServices { class InputMethodController : public RefBase { public: static sptr GetInstance(); + void Attach(sptr &listener); void Attach(sptr &listener, bool isShowKeyboard = true); std::u16string GetTextBeforeCursor(int32_t number); std::u16string GetTextAfterCursor(int32_t number); diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 227f79533..aa92405eb 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -199,6 +199,11 @@ using namespace MessageID; } } + void InputMethodController::Attach(sptr &listener) + { + Attach(listener, true); + } + void InputMethodController::Attach(sptr &listener, bool isShowKeyboard) { textListener = listener; -- Gitee From 48604000c7201230373771bd3daed0a3e20207de Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Mon, 15 Aug 2022 23:59:19 +0800 Subject: [PATCH 8/8] modify include file Signed-off-by: zhaolinglan --- .../inputmethod_controller/include/input_method_controller.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 3c031a255..4771b6437 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -59,7 +59,7 @@ namespace MiscServices { public: static sptr GetInstance(); void Attach(sptr &listener); - void Attach(sptr &listener, bool isShowKeyboard = true); + void Attach(sptr &listener, bool isShowKeyboard); std::u16string GetTextBeforeCursor(int32_t number); std::u16string GetTextAfterCursor(int32_t number); void ShowTextInput(); -- Gitee