From 0231fc65c8fb4cdfc9c81df4515925c4d597fc86 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Tue, 5 Jul 2022 12:16:44 +0800 Subject: [PATCH] add switch napi fuction Signed-off-by: zhaolinglan --- .../include/input_method_controller.h | 1 + .../input_method_system_ability_proxy.h | 1 + .../src/input_method_controller.cpp | 9 +++ .../src/input_method_system_ability_proxy.cpp | 21 +++++++ .../js/declaration/api/@ohos.inputmethod.d.ts | 10 +++ .../src/js_input_method_registry.cpp | 63 +++++++++++++++++++ 6 files changed, 105 insertions(+) diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index ebe06c4..c15acf5 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -72,6 +72,7 @@ namespace MiscServices { int32_t GetInputPattern(); void HideCurrentInput(); void SetCallingWindow(uint32_t windowId); + int32_t SwitchInputMethod(InputMethodProperty *target); private: InputMethodController(); 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 df30d91..e1fd29c 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -56,6 +56,7 @@ namespace MiscServices { int32_t listInputMethodEnabled(std::vector *properties) override; int32_t listInputMethod(std::vector *properties) override; int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) override; + int32_t SwitchInputMethod(InputMethodProperty* target); private: static inline BrokerDelegator delegator_; diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 39d051d..9a8ef44 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -433,5 +433,14 @@ using namespace MessageID; mAgent->SetCallingWindow(windowId); return; } + + int32_t InputMethodController::SwitchInputMethod(InputMethodProperty *target) + { + IMSA_HILOGI("InputMethodController::SwitchInputMethod"); + if (!mImms) { + return false; + } + return mImms->SwitchInputMethod(target); + } } // namespace MiscServices } // namespace OHOS 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 c639b6a..a9a034e 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -437,5 +437,26 @@ namespace MiscServices { } return NO_ERROR; } + + int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(InputMethodProperty* target) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::switchInputMethod"); + MessageParcel data, reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + return false; + } + + if (!target->Marshalling(data)) { + IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod Failed to marshall target to data!"); + delete target; + return false; + } + delete target; + auto ret = Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); + ret = reply.ReadInt32(); + return ret; + } } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts b/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts index 99d5e07..c5a05b5 100644 --- a/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.inputmethod.d.ts @@ -28,6 +28,16 @@ declare namespace inputMethod { function getInputMethodController(): InputMethodController; + /** + * Switch input method + * @since 9 + * @param target Indicates the input method which will replace the current one + * @return - + * @syscap SystemCapability.MiscServices.InputMethodFramework + * @StageModelOnly + */ + function switchInputMethod(target: InputMethodProperty): Promise; + interface InputMethodSetting { listInputMethod(callback: AsyncCallback>): void; listInputMethod(): Promise>; diff --git a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp index a0219b1..ac4a12c 100644 --- a/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp +++ b/interfaces/kits/js/napi/inputmethod/src/js_input_method_registry.cpp @@ -58,6 +58,13 @@ namespace MiscServices { return (me != nullptr) ? me->OnGetInputMethodController(*engine, *info) : nullptr; } + static NativeValue *SwitchInputMethod(NativeEngine *engine, NativeCallbackInfo *info) + { + IMSA_HILOGI("JsInputMethodRegistry::SwitchInputMethod is called"); + JsInputMethodRegistry *me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnSwitchInputMethod(*engine, *info) : nullptr; + } + private: NativeValue* OnGetInputMethodSetting(NativeEngine& engine, NativeCallbackInfo& info) { @@ -80,6 +87,61 @@ namespace MiscServices { return CreateInputMethodController(engine); } + + NativeValue *OnSwitchInputMethod(NativeEngine &engine, NativeCallbackInfo &info) + { + IMSA_HILOGI("JsInputMethodRegistry::OnSwitchInputMethod is called!"); + if (info.argc != 1) { + IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod Params not match"); + return engine.CreateUndefined(); + } + + InputMethodProperty *target = new InputMethodProperty(); + NativeObject *object = ConvertNativeValueTo(info.argv[0]); + if (object == nullptr) { + IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod Failed to get object"); + return engine.CreateUndefined(); + } + + if (!GetInputMethodPropertyFromJs(engine, object, *target)) { + return engine.CreateUndefined(); + } + + bool isSwitchSuccess = false; + if (!InputMethodController::GetInstance()->SwitchInputMethod(target)) { + isSwitchSuccess = true; + } else { + IMSA_HILOGE("JsInputMethodRegistry::OnSwitchInputMethod isSwitchSuccess is false !"); + } + + NativeValue *result = CreateJsValue(engine, isSwitchSuccess); + + return result; + } + + bool GetInputMethodPropertyFromJs(NativeEngine &engine, NativeObject *propertyObject, InputMethodProperty &target) + { + IMSA_HILOGI("JsInputMethodRegistry::GetInputMethodPropertyFromJs is called!"); + NativeValue *packageName = propertyObject->GetProperty("packageName"); + std::string packageNameUtf8; + if (!ConvertFromJsValue(engine, packageName, packageNameUtf8)) { + IMSA_HILOGE("JsInputMethodRegistry::GetInputMethodPropertyFromJs Failed to convert parameter to " + "PackageName"); + return false; + } + target.mPackageName = + std::wstring_convert, char16_t>{}.from_bytes(packageNameUtf8); + + NativeValue *imeId = propertyObject->GetProperty("methodId"); + std::string imeIdUtf8; + if (!ConvertFromJsValue(engine, imeId, imeIdUtf8)) { + IMSA_HILOGE("JsInputMethodRegistry::GetInputMethodPropertyFromJs Failed to convert parameter to ImeId"); + return false; + } + target.mImeId = std::wstring_convert, char16_t>{}.from_bytes(imeIdUtf8); + + return true; + } }; NativeValue* JsInputMethodRegistryInit(NativeEngine* engine, NativeValue* exportObj) @@ -102,6 +164,7 @@ namespace MiscServices { BindNativeFunction(*engine, *object, "getInputMethodSetting", JsInputMethodRegistry::GetInputMethodSetting); BindNativeFunction(*engine, *object, "getInputMethodController", JsInputMethodRegistry::GetInputMethodController); + BindNativeFunction(*engine, *object, "switchInputMethod", JsInputMethodRegistry::SwitchInputMethod); object->SetProperty("MAX_TYPE_NUM", CreateJsValue(*engine, static_cast(MAX_TYPE_NUM)));