diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 5890cc5134fc9253ea16bc6658c76c85f919d584..42ecbe0b88982271da965a32f173fec06de85d86 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -76,14 +76,16 @@ namespace MiscServices { void OnConfigurationChange(Configuration info); bool dispatchKeyEvent(std::shared_ptr keyEvent); int32_t DisplayOptionalInputMethod(); - std::vector ListInputMethod(); + std::vector ListInputMethodCommon(InputMethodStatus status); + std::vector ListInputMethod(); + std::vector ListInputMethod(bool enable); int32_t GetEnterKeyType(); int32_t GetInputPattern(); std::shared_ptr GetCurrentInputMethod(); int32_t HideCurrentInput(); int32_t ShowCurrentInput(); void SetCallingWindow(uint32_t windowId); - int32_t SwitchInputMethod(const InputMethodProperty &target); + int32_t SwitchInputMethod(const Property &target); private: InputMethodController(); diff --git a/frameworks/inputmethod_controller/include/input_method_status.h b/frameworks/inputmethod_controller/include/input_method_status.h new file mode 100644 index 0000000000000000000000000000000000000000..6988618ba20cc527d0350c51055bc3190cb72964 --- /dev/null +++ b/frameworks/inputmethod_controller/include/input_method_status.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#ifndef INPUTMETHOD_IMF_INPUT_METHOD_STATUS_H +#define INPUTMETHOD_IMF_INPUT_METHOD_STATUS_H + +namespace OHOS { +namespace MiscServices { +enum InputMethodStatus : uint32_t { + DISABLE = 0, + ENABLE, + ALL +}; +} +} // namespace OHOS + +#endif //INPUTMETHOD_IMF_INPUT_METHOD_STATUS_H 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 d9d04b5e4bbbd0e94f62ff27f501e5892373acc3..5c31cf6baf2908a7da661de4a420f03815d58ab4 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -18,9 +18,9 @@ #include #include +#include #include -#include #include "global.h" #include "i_input_method_system_ability.h" #include "input_attribute.h" @@ -61,10 +61,9 @@ namespace MiscServices { int32_t displayOptionalInputMethod(MessageParcel& data) override; int32_t getDisplayMode(int32_t &retMode) override; int32_t getKeyboardWindowHeight(int32_t &retHeight) override; - int32_t GetCurrentInputMethod(InputMethodProperty ¤tInputMethod) override; + std::shared_ptr GetCurrentInputMethod() override; int32_t getCurrentKeyboardType(KeyboardType *retType) override; - int32_t listInputMethodEnabled(std::vector *properties) override; - int32_t listInputMethod(std::vector *properties) override; + std::vector ListInputMethod(InputMethodStatus stauts) override; int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) override; int32_t SwitchInputMethod(const InputMethodProperty &target); diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 503d2342c4cec0e95d070d1b4d00085722f32171..de8448e90f696871144eb3f37b7853abcd815a51 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -16,11 +16,13 @@ #include "input_method_controller.h" #include "global.h" +#include "input_method_status.h" #include "inputmethod_sysevent.h" #include "inputmethod_trace.h" #include "iservice_registry.h" -#include "system_ability_definition.h" #include "string_ex.h" +#include "system_ability_definition.h" +#include "utils.h" namespace OHOS { namespace MiscServices { @@ -293,15 +295,27 @@ using namespace MessageID; return mImms->displayOptionalInputMethod(data); } - std::vector InputMethodController::ListInputMethod() + std::vector InputMethodController::ListInputMethodCommon(InputMethodStatus status) { - IMSA_HILOGI("InputMethodController::listInputMethod"); - std::vector properties; - if (!mImms) { - return properties; + IMSA_HILOGI("InputMethodController::ListInputMethodCommon"); + if (mImms == nullptr) { + IMSA_HILOGE("mImms is nullptr"); + return {}; } - mImms->listInputMethod(&properties); - return properties; + auto property = mImms->ListInputMethod(status); + return Utils::GetProperty(property); + } + + std::vector InputMethodController::ListInputMethod() + { + IMSA_HILOGI("InputMethodController::listInputMethod"); + return ListInputMethodCommon(ALL); + } + + std::vector InputMethodController::ListInputMethod(bool enable) + { + IMSA_HILOGI("InputMethodController::listInputMethod enable = %{public}s", enable ? "ENABLE" : "DISABLE"); + return ListInputMethodCommon(enable ? ENABLE : DISABLE); } std::shared_ptr InputMethodController::GetCurrentInputMethod() @@ -312,14 +326,13 @@ using namespace MessageID; return nullptr; } - InputMethodProperty property; - int32_t ret = mImms->GetCurrentInputMethod(property); - if (ret != NO_ERROR) { - IMSA_HILOGE("InputMethodController::GetCurrentInputMethod failed: %{public}d", ret); + auto property = mImms->GetCurrentInputMethod(); + if (property == nullptr) { + IMSA_HILOGE("InputMethodController::GetCurrentInputMethod property is nullptr"); return nullptr; } - return { new Property({ Str16ToStr8(property.mPackageName), Str16ToStr8(property.mAbilityName) }), + return { new Property({ Str16ToStr8(property->mPackageName), Str16ToStr8(property->mAbilityName) }), [](auto p) {} }; } @@ -510,13 +523,17 @@ using namespace MessageID; agent->SetCallingWindow(windowId); } - int32_t InputMethodController::SwitchInputMethod(const InputMethodProperty &target) + int32_t InputMethodController::SwitchInputMethod(const Property &target) { IMSA_HILOGI("InputMethodController::SwitchInputMethod"); if (!mImms) { + IMSA_HILOGE("InputMethodController mImms is nullptr"); return false; } - return mImms->SwitchInputMethod(target); + InputMethodProperty property; + property.mPackageName = Str8ToStr16(target.packageName); + property.mAbilityName = Str8ToStr16(target.abilityName); + return mImms->SwitchInputMethod(property); } void InputMethodController::SetInputMethodAgent(sptr &object) 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 d81761a524b6086e8dbccb14adbc32b144d500cd..5d607797fc4db635c619fe97f1700365daa7b193 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -330,28 +330,30 @@ namespace MiscServices { return NO_ERROR; } - int32_t InputMethodSystemAbilityProxy::GetCurrentInputMethod(InputMethodProperty &property) + std::shared_ptr InputMethodSystemAbilityProxy::GetCurrentInputMethod() { MessageParcel data, reply; MessageOption option; if (!data.WriteInterfaceToken(GetDescriptor())) { IMSA_HILOGE("InputMethodSystemAbilityProxy::GetCurrentInputMethod WriteInterfaceToken failed"); - return ERROR_EX_NULL_POINTER; + return nullptr; } auto ret = Remote()->SendRequest(GET_CURRENT_INPUT_METHOD, data, reply, option); if (ret != NO_ERROR) { IMSA_HILOGE("InputMethodSystemAbilityProxy::GetCurrentInputMethod SendRequest failed: %{public}d", ret); - return ret; + return nullptr; } ret = reply.ReadInt32(); if (ret != NO_ERROR) { IMSA_HILOGE("InputMethodSystemAbilityProxy::GetCurrentInputMethod reply error: %{public}d", ret); - return ret; + return nullptr; } - auto currentIme = reply.ReadParcelable(); - property = *currentIme; - delete currentIme; - return NO_ERROR; + auto property = reply.ReadParcelable(); + if (property == nullptr) { + IMSA_HILOGE("InputMethodSystemAbilityProxy::read parcel nullptr"); + return nullptr; + } + return { property, [](auto p) {} }; } int32_t InputMethodSystemAbilityProxy::getCurrentKeyboardType(KeyboardType *retType) @@ -387,71 +389,39 @@ namespace MiscServices { return NO_ERROR; } - int32_t InputMethodSystemAbilityProxy::listInputMethodEnabled(std::vector *properties) + std::vector InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status) { - if (!properties) { - return ERROR_NULL_POINTER; - } - + IMSA_HILOGI("InputMethodSystemAbilityProxy::ListInputMethod"); MessageParcel data, reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { - return ERROR_EX_PARCELABLE; - } - - auto ret = Remote()->SendRequest(LIST_INPUT_METHOD_ENABLED, data, reply, option); - if (ret != NO_ERROR) { - return ERROR_STATUS_FAILED_TRANSACTION; + if (!(data.WriteInterfaceToken(GetDescriptor()) && data.WriteUint32(status))) { + IMSA_HILOGE("Write InterfaceToken or Uint32 failed"); + return {}; } - - ret = reply.ReadInt32(); - if (ret != NO_ERROR) { - return ret; - } - - auto size = reply.ReadInt32(); - while (size > 0) { - InputMethodProperty *imp = reply.ReadParcelable(); - properties->push_back(imp); - size--; - } - - return NO_ERROR; - } - - int32_t InputMethodSystemAbilityProxy::listInputMethod(std::vector *properties) - { - if (!properties) { - return ERROR_NULL_POINTER; - } - - MessageParcel data, reply; - MessageOption option; - - if (!data.WriteInterfaceToken(GetDescriptor())) { - return ERROR_EX_PARCELABLE; - } - auto ret = Remote()->SendRequest(LIST_INPUT_METHOD, data, reply, option); if (ret != NO_ERROR) { - return ERROR_STATUS_FAILED_TRANSACTION; + IMSA_HILOGE("InputMethodSystemAbilityProxy SendRequest failed: %{public}d", ret); + return {}; } ret = reply.ReadInt32(); if (ret != NO_ERROR) { - return ret; + IMSA_HILOGE("InputMethodSystemAbilityProxy reply error: %{public}d", ret); + return {}; } - auto size = reply.ReadInt32(); + auto size = reply.ReadUint32(); + std::vector properties; while (size > 0) { - InputMethodProperty *imp = reply.ReadParcelable(); - properties->push_back(imp); + auto property = reply.ReadParcelable(); + properties.push_back(*property); + delete property; size--; } - return NO_ERROR; + return properties; } int32_t InputMethodSystemAbilityProxy::listKeyboardType(const std::u16string& imeId, @@ -497,10 +467,7 @@ namespace MiscServices { return ERROR_EX_PARCELABLE; } - if (!target.Marshalling(data)) { - IMSA_HILOGE("InputMethodSystemAbilityProxy::switchInputMethod Failed to marshall target to data!"); - return ERROR_IME_PROPERTY_MARSHALL; - } + data.WriteParcelable(&target); auto ret = Remote()->SendRequest(SWITCH_INPUT_METHOD, data, reply, option); if (ret != 0) { return ERROR_STATUS_FAILED_TRANSACTION; diff --git a/interfaces/kits/js/napi/inputmethodclient/async_call.cpp b/interfaces/kits/js/napi/inputmethodclient/async_call.cpp index e073e6fb139d427bc7d80e53fd3e0e8e3b89f644..4d370ed8335b897b803cc14572bb662755883cdf 100644 --- a/interfaces/kits/js/napi/inputmethodclient/async_call.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/async_call.cpp @@ -26,7 +26,7 @@ AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr= 0 && pos < argc) { napi_valuetype valueType = napi_undefined; diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 94f1f214ad43c5a2d11e6af8333ba91f66a0dee2..9bd403e0928e634f1ca7585499538725a491c739 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -12,11 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "js_get_input_method_setting.h" + +#include "input_method_controller.h" +#include "input_method_status.h" +#include "js_input_method.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" -#include "input_method_controller.h" namespace OHOS { namespace MiscServices { @@ -85,61 +89,40 @@ napi_value JsGetInputMethodSetting::GetInputMethodSetting(napi_env env, napi_cal return instance; } -napi_value JsGetInputMethodSetting::GetJSInputMethodProperty(napi_env env, - std::vector &properties) -{ - uint32_t index = 0; - napi_value result = nullptr; - napi_create_array(env, &result); - if (result == nullptr) { - IMSA_HILOGE("create_array failed"); - return result; - } - for (const auto &item : properties) { - if (item == nullptr) { - IMSA_HILOGE("GetResult::item is null"); - continue; - } - napi_value InputMethodSetting = nullptr; - napi_create_object(env, &InputMethodSetting); - - std::string packageName = Str16ToStr8(item->mPackageName); - napi_value jsPackageName = nullptr; - napi_create_string_utf8(env, packageName.c_str(), NAPI_AUTO_LENGTH, &jsPackageName); - napi_set_named_property(env, InputMethodSetting, "packageName", jsPackageName); - - std::string methodId = Str16ToStr8(item->mAbilityName); - napi_value jsMethodId = nullptr; - napi_create_string_utf8(env, methodId.c_str(), NAPI_AUTO_LENGTH, &jsMethodId); - napi_set_named_property(env, InputMethodSetting, "methodId", jsMethodId); - - napi_set_element(env, result, index, InputMethodSetting); - index++; - } - return result; -} - napi_value JsGetInputMethodSetting::ListInputMethod(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); + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, " Parameter number error", napi_invalid_arg); + if (argc == 0) { + ctxt->inputMethodStatus = InputMethodStatus::ALL; + return napi_ok; + } + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_boolean, " Parameter type error", napi_invalid_arg); + bool enable = false; + napi_get_value_bool(env, argv[0], &enable); + ctxt->inputMethodStatus = enable ? InputMethodStatus::ENABLE : InputMethodStatus::DISABLE; return napi_ok; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { - napi_value data = GetJSInputMethodProperty(env, ctxt->properties); - *result = data; + *result = JsInputMethod::GetJSInputMethodProperties(env, ctxt->properties); return napi_ok; }; auto exec = [ctxt](AsyncCall::Context *ctx) { - ctxt->properties = InputMethodController::GetInstance()->ListInputMethod(); + if (ctxt->inputMethodStatus == ALL) { + ctxt->properties = InputMethodController::GetInstance()->ListInputMethod(); + } else { + ctxt->properties = InputMethodController::GetInstance()->ListInputMethod(ctxt->inputMethodStatus == ENABLE); + } if (!ctxt->properties.empty()) { IMSA_HILOGE("exec ---- ListInputMethod success"); ctxt->status = napi_ok; } }; ctxt->SetAction(std::move(input), std::move(output)); - AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 0); + AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt)); return asyncCall.Call(env, exec); } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h index a09e0b26f5f4290f27fcf5dbc01700be815af66c..748d7fd021a9eb551881025ee7c11d4410b4d535 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -15,14 +15,16 @@ #ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H #define INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H -#include "global.h" -#include "input_method_property.h" #include "async_call.h" +#include "global.h" +#include "input_method_controller.h" +#include "input_method_status.h" namespace OHOS { namespace MiscServices { struct ListInputContext : public AsyncCall::Context { - std::vector properties; + InputMethodStatus inputMethodStatus; + std::vector properties; napi_status status = napi_generic_failure; ListInputContext() : Context(nullptr, nullptr) { }; ListInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; @@ -92,7 +94,6 @@ public: static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info); private: static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); - static napi_value GetJSInputMethodProperty(napi_env env, std::vector &properties); static const std::string IMS_CLASS_NAME; static thread_local napi_ref IMSRef_; }; diff --git a/interfaces/kits/js/napi/inputmethodclient/js_input_method.cpp b/interfaces/kits/js/napi/inputmethodclient/js_input_method.cpp index 2e3257e5a2a3dcc9f3faa3f3d73ddc1c100c6df7..b9b91cdbf7453759eae942b02e923942e5ae5f44 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_input_method.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_input_method.cpp @@ -26,6 +26,7 @@ napi_value JsInputMethod::Init(napi_env env, napi_value exports) { napi_property_descriptor descriptor[] = { DECLARE_NAPI_FUNCTION("switchInputMethod", SwitchInputMethod), + DECLARE_NAPI_FUNCTION("getCurrentInputMethod", GetCurrentInputMethod), }; NAPI_CALL( env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor)); @@ -61,6 +62,39 @@ napi_status JsInputMethod::GetInputMethodProperty(napi_env env, napi_value argv, return status; } +napi_value JsInputMethod::GetJsInputMethodProperty(napi_env env, const Property &property) +{ + napi_value prop = nullptr; + napi_create_object(env, &prop); + + napi_value packageName = nullptr; + napi_create_string_utf8(env, property.packageName.c_str(), NAPI_AUTO_LENGTH, &packageName); + napi_set_named_property(env, prop, "packageName", packageName); + + napi_value methodId = nullptr; + napi_create_string_utf8(env, property.abilityName.c_str(), NAPI_AUTO_LENGTH, &methodId); + napi_set_named_property(env, prop, "methodId", methodId); + + return prop; +} + +napi_value JsInputMethod::GetJSInputMethodProperties(napi_env env, const std::vector &properties) +{ + uint32_t index = 0; + napi_value prop = nullptr; + napi_create_array(env, &prop); + if (prop == nullptr) { + IMSA_HILOGE("create array failed"); + return prop; + } + for (const auto &property : properties) { + napi_value pro = GetJsInputMethodProperty(env, property); + napi_set_element(env, prop, index, pro); + index++; + } + return prop; +} + napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); @@ -75,10 +109,10 @@ napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info inf return status; }; auto exec = [ctxt](AsyncCall::Context *ctx) { - InputMethodProperty property; - property.mPackageName = Str8ToStr16(ctxt->packageName); - property.mImeId = Str8ToStr16(ctxt->methodId); - int32_t errCode = InputMethodController::GetInstance()->SwitchInputMethod(property); + int32_t errCode = InputMethodController::GetInstance()->SwitchInputMethod({ + .packageName = ctxt->packageName, + .abilityName = ctxt->methodId + }); if (errCode == ErrorCode::NO_ERROR) { IMSA_HILOGI("exec SwitchInputMethod success"); ctxt->status = napi_ok; @@ -89,5 +123,17 @@ napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info inf AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(ctxt), 1); return asyncCall.Call(env, exec); } + +napi_value JsInputMethod::GetCurrentInputMethod(napi_env env, napi_callback_info info) +{ + std::shared_ptr property = InputMethodController::GetInstance()->GetCurrentInputMethod(); + if (property == nullptr) { + IMSA_HILOGE("get current inputmethod is nullptr"); + napi_value result = nullptr; + napi_get_null(env, &result); + return result; + } + return GetJsInputMethodProperty(env, { property->packageName, property->abilityName }); } -} \ No newline at end of file +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h index f33cf4e0f42f21c30c329b5bfad8e03f037fc13c..9fe04fb86fdcfc83391968ad7ca3e4d468cec735 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h @@ -19,6 +19,7 @@ #include "native_engine/native_value.h" #include "global.h" #include "async_call.h" +#include "input_method_controller.h" namespace OHOS { namespace MiscServices { @@ -50,6 +51,10 @@ public: ~JsInputMethod() = default; static napi_value Init(napi_env env, napi_value exports); static napi_value SwitchInputMethod(napi_env env, napi_callback_info info); + static napi_value GetCurrentInputMethod(napi_env env, napi_callback_info info); + static napi_value GetJsInputMethodProperty(napi_env env, const Property &property); + static napi_value GetJSInputMethodProperties(napi_env env, const std::vector &properties); + private: static std::string GetStringProperty(napi_env env, napi_value result); static napi_status GetInputMethodProperty(napi_env env, napi_value argv, diff --git a/services/include/global.h b/services/include/global.h index 23836623712d43bcdfcd37a494d8748ec743f44d..5be5b28add177c296109d58103391a157f3ebe41 100644 --- a/services/include/global.h +++ b/services/include/global.h @@ -138,6 +138,7 @@ namespace ErrorCode { ERROR_CLIENT_NOT_WINDOW, // the input client is not from a valid window ERROR_IME_PROPERTY_MARSHALL, // failed to marshall the ime property ERROR_GETTING_CURRENT_IME, + ERROR_LIST_IME, // error from ime ERROR_REMOTE_IME_DIED, // remote input method service died abnormally ERROR_RESTART_IME_FAILED, // failed to restart input method service diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index fb973c5e105a20e2f44d8a1178a351a2f27e1dea..40e3e24507fa627a0030032ef2c59a153a87b842 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -16,17 +16,21 @@ #ifndef SERVICES_INCLUDE_I_INPUT_METHOD_SYSTEM_ABILITY_H #define SERVICES_INCLUDE_I_INPUT_METHOD_SYSTEM_ABILITY_H -#include #include -#include "iremote_broker.h" + +#include +#include + +#include "global.h" #include "i_input_client.h" #include "i_input_data_channel.h" +#include "i_input_method_core.h" #include "input_attribute.h" #include "input_method_property.h" +#include "input_method_status.h" +#include "iremote_broker.h" #include "keyboard_type.h" -#include "global.h" #include "message_parcel.h" -#include "i_input_method_core.h" namespace OHOS { namespace MiscServices { @@ -44,7 +48,6 @@ namespace MiscServices { GET_KEYBOARD_WINDOW_HEIGHT, GET_CURRENT_INPUT_METHOD, GET_CURRENT_KEYBOARD_TYPE, - LIST_INPUT_METHOD_ENABLED, LIST_INPUT_METHOD, LIST_KEYBOARD_TYPE, SET_CORE_AND_AGENT, @@ -65,10 +68,9 @@ namespace MiscServices { virtual int32_t displayOptionalInputMethod(MessageParcel& data) = 0; virtual int32_t getDisplayMode(int32_t &retMode) = 0; virtual int32_t getKeyboardWindowHeight(int32_t &retHeight) = 0; - virtual int32_t GetCurrentInputMethod(InputMethodProperty ¤tInputMethod) = 0; + virtual std::shared_ptr GetCurrentInputMethod() = 0; virtual int32_t getCurrentKeyboardType(KeyboardType *retType) = 0; - virtual int32_t listInputMethodEnabled(std::vector *properties) = 0; - virtual int32_t listInputMethod(std::vector *properties) = 0; + virtual std::vector ListInputMethod(InputMethodStatus stauts) = 0; virtual int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) = 0; }; } // namespace MiscServices diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index d2280c7cf156af5c71647a8e40c4cb55f6f1661e..509d26dffeb6b05309228e586f341cde64c8b9af 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -31,6 +31,7 @@ namespace OHOS { namespace MiscServices { class InputDataChannelStub; + using AbilityType = AppExecFwk::ExtensionAbilityType; enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING @@ -52,12 +53,9 @@ namespace MiscServices { int32_t getDisplayMode(int32_t &retMode) override; int32_t getKeyboardWindowHeight(int32_t &retHeight) override; int32_t getCurrentKeyboardType(KeyboardType *retType) override; - int32_t GetCurrentInputMethod(InputMethodProperty &currImeProperty) override; - int32_t listInputMethodEnabled(std::vector *properties) override; - int32_t listInputMethod(std::vector *properties) override; - int32_t listInputMethodByUserId(int32_t userId, std::vector *properties) override; - int32_t listInputMethodByType( - int32_t userId, std::vector *properties, AppExecFwk::ExtensionAbilityType type); + std::shared_ptr GetCurrentInputMethod() override; + std::vector ListInputMethod(InputMethodStatus stauts) override; + std::vector ListInputMethodByUserId(int32_t userId, InputMethodStatus status) override; int32_t listKeyboardType(const std::u16string &imeId, std::vector *types) override; int Dump(int fd, const std::vector &args) override; void DumpAllMethod(int fd); @@ -96,9 +94,13 @@ namespace MiscServices { void OnDisplayOptionalInputMethod(int32_t userId); static sptr GetAbilityManagerService(); OHOS::sptr GetBundleMgr(); + std::vector listInputMethodByType(int32_t userId, AbilityType type); + std::vector ListAllInputMethod(int32_t userId); + std::vector ListEnabledInputMethod(); + std::vector ListDisabledInputMethod(int32_t userId); void StartUserIdListener(); - int32_t OnSwitchInputMethod(int32_t userId, InputMethodProperty *target); - void GetInputMethodParam(std::vector properties, std::string ¶ms); + int32_t OnSwitchInputMethod(int32_t userId, const InputMethodProperty &target); + std::string GetInputMethodParam(const std::vector &properties); ServiceRunningState state_; void InitServiceHandler(); static std::mutex instanceLock_; diff --git a/services/include/input_method_system_ability_stub.h b/services/include/input_method_system_ability_stub.h index 0112d15997dc510d574cef874add9be702341df4..6efd569507a7edcb683851a3eb55813e773ba1f1 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -38,7 +38,8 @@ namespace MiscServices { int32_t ShowCurrentInput(MessageParcel& data) override; int32_t displayOptionalInputMethod(MessageParcel& data) override; void OnGetCurrentInputMethod(MessageParcel &reply); - virtual int32_t listInputMethodByUserId(int32_t userId, std::vector *properties) = 0; + void OnListInputMethod(MessageParcel &data, MessageParcel &reply); + virtual std::vector ListInputMethodByUserId(int32_t userId, InputMethodStatus status) = 0; int32_t SwitchInputMethod(MessageParcel &data); protected: diff --git a/services/include/peruser_setting.h b/services/include/peruser_setting.h index c69298c724792a92cdda8a37eb34687cdf174d86..b8fdc6e0b0a81f39fb51db83edb189187c725fdd 100644 --- a/services/include/peruser_setting.h +++ b/services/include/peruser_setting.h @@ -46,7 +46,6 @@ namespace MiscServices { void OnUserLocked(); void Dump(int32_t fd); - int32_t ListInputMethodEnabled(std::vector *properties); int32_t ListInputMethod(std::vector *properties); int32_t ListKeyboardType(const std::u16string& imeId, std::vector *types); diff --git a/services/include/utils.h b/services/include/utils.h index 2ad61c322ebf9590baac599947f8d6ea790d62b5..d59c94604b56eb13e28dbb7b058a5ed58155255d 100644 --- a/services/include/utils.h +++ b/services/include/utils.h @@ -18,10 +18,14 @@ #ifndef SERVICES_INCLUDE_UTILS_H #define SERVICES_INCLUDE_UTILS_H -#include #include -#include #include +#include +#include + +#include "input_method_controller.h" +#include "input_method_property.h" +#include "string_ex.h" namespace OHOS { namespace MiscServices { @@ -35,6 +39,14 @@ namespace MiscServices { { return std::wstring_convert, char16_t> {}.from_bytes(str); } + static std::vector GetProperty(const std::vector &properties) + { + std::vector props; + for (const auto &property : properties) { + props.push_back({ Str16ToStr8(property.mPackageName), Str16ToStr8(property.mAbilityName) }); + } + return props; + } }; } // namespace MiscServices } // namespace OHOS diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index a07fda6a50649b4e180988329915f657ec2d1bd5..85c15601475ca5df7e5043dd2cea00f6630d6682 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -32,6 +32,7 @@ #include "im_common_event_manager.h" #include "resource_manager.h" #include "os_account_manager.h" +#include "input_method_status.h" namespace OHOS { namespace MiscServices { @@ -142,28 +143,26 @@ namespace MiscServices { return ERR_OK; } - void InputMethodSystemAbility::GetInputMethodParam( - std::vector properties, std::string ¶ms) + std::string InputMethodSystemAbility::GetInputMethodParam(const std::vector &properties) { std::string defaultIme = ParaHandle::GetDefaultIme(userId_); - std::vector::iterator it; - for (it = properties.begin(); it < properties.end(); ++it) { - if (it == properties.begin()) { - params += "{\"imeList\":["; - } else { - params += "},"; - } - InputMethodProperty *property = (InputMethodProperty *)*it; - std::string imeId = Str16ToStr8(property->mPackageName) + "/" + Str16ToStr8(property->mAbilityName); + bool isBegin = true; + std::string params = "{\"imeList\":["; + for (const auto &property : properties) { + params += isBegin ? "" : "},"; + isBegin = false; + + std::string imeId = Str16ToStr8(property.mPackageName) + "/" + Str16ToStr8(property.mAbilityName); params += "{\"ime\": \"" + imeId + "\","; - params += "\"labelId\": \"" + std::to_string(property->labelId) + "\","; - params += "\"descriptionId\": \"" + std::to_string(property->descriptionId) + "\","; + params += "\"labelId\": \"" + std::to_string(property.labelId) + "\","; + params += "\"descriptionId\": \"" + std::to_string(property.descriptionId) + "\","; std::string isDefaultIme = defaultIme == imeId ? "true" : "false"; params += "\"isDefaultIme\": \"" + isDefaultIme + "\","; - params += "\"label\": \"" + Str16ToStr8(property->label) + "\","; - params += "\"description\": \"" + Str16ToStr8(property->description) + "\""; + params += "\"label\": \"" + Str16ToStr8(property.label) + "\","; + params += "\"description\": \"" + Str16ToStr8(property.description) + "\""; } params += "}]}"; + return params; } void InputMethodSystemAbility::DumpAllMethod(int fd) @@ -177,15 +176,13 @@ namespace MiscServices { } dprintf(fd, "\n - DumpAllMethod get Active Id succeed,count=%zu,", ids.size()); for (auto id : ids) { - std::vector properties; - listInputMethodByUserId(id, &properties); + const auto &properties = ListInputMethodByUserId(id, ALL); if (properties.empty()) { IMSA_HILOGI("The IME properties is empty."); dprintf(fd, "\n - The IME properties about the Active Id %d is empty.\n", id); continue; } - std::string params; - GetInputMethodParam(properties, params); + const auto ¶ms = GetInputMethodParam(properties); dprintf(fd, "\n - The Active Id:%d get input method:\n%s\n", id, params.c_str()); } IMSA_HILOGI("InputMethodSystemAbility::DumpAllMethod end."); @@ -427,37 +424,51 @@ namespace MiscServices { return ErrorCode::NO_ERROR; } - /*! Get the enabled input method engine list - \n Run in binder thread - \param[out] properties input method engine list returned to the caller - \return ErrorCode::NO_ERROR no error - \return ErrorCode::ERROR_USER_NOT_UNLOCKED user not unlocked - */ - int32_t InputMethodSystemAbility::listInputMethodEnabled(std::vector *properties) + std::vector InputMethodSystemAbility::ListInputMethod(InputMethodStatus status) { - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = getUserId(uid); - PerUserSetting *setting = GetUserSetting(userId); - if (!setting || setting->GetUserState() != UserState::USER_STATE_UNLOCKED) { - IMSA_HILOGE("%s %d\n", ErrorCode::ToString(ErrorCode::ERROR_USER_NOT_UNLOCKED), userId); - return ErrorCode::ERROR_USER_NOT_UNLOCKED; - } - setting->ListInputMethodEnabled(properties); + return {}; + } - std::vector::iterator it; - for (it = properties->begin(); it != properties->end();) { - if (*it && (*it)->isSystemIme) { - it = properties->erase(it); - } else { - ++it; - } + std::vector InputMethodSystemAbility::ListAllInputMethod(int32_t userId) + { + IMSA_HILOGI("InputMethodSystemAbility::listAllInputMethod"); + std::vector properties; + AbilityType types[] = { AbilityType::SERVICE, AbilityType::INPUTMETHOD }; + for (const auto &type : types) { + auto property = listInputMethodByType(userId, type); + properties.insert(properties.end(), property.begin(), property.end()); + } + return properties; + } + + std::vector InputMethodSystemAbility::ListEnabledInputMethod() + { + IMSA_HILOGI("InputMethodSystemAbility::listEnabledInputMethod"); + auto property = GetCurrentInputMethod(); + if (property == nullptr) { + IMSA_HILOGE("GetCurrentInputMethod property is nullptr"); + return {}; } - return ErrorCode::NO_ERROR; + return { *property }; } - int32_t InputMethodSystemAbility::listInputMethod(std::vector *properties) + std::vector InputMethodSystemAbility::ListDisabledInputMethod(int32_t userId) { - return ErrorCode::NO_ERROR; + IMSA_HILOGI("InputMethodSystemAbility::listDisabledInputMethod"); + auto properties = listInputMethodByType(userId, AbilityType::INPUTMETHOD); + auto filter = GetCurrentInputMethod(); + if (filter == nullptr) { + IMSA_HILOGE("GetCurrentInputMethod property is nullptr"); + return {}; + } + for (auto iter = properties.begin(); iter != properties.end();) { + if (iter->mPackageName == filter->mPackageName && iter->mAbilityName == filter->mAbilityName) { + iter = properties.erase(iter); + continue; + } + ++iter; + } + return properties; } /*! Get all of the input method engine list installed in the system @@ -466,47 +477,53 @@ namespace MiscServices { \return ErrorCode::NO_ERROR no error \return ErrorCode::ERROR_USER_NOT_UNLOCKED user not unlocked */ - int32_t InputMethodSystemAbility::listInputMethodByUserId( - int32_t userId, std::vector *properties) + std::vector InputMethodSystemAbility::ListInputMethodByUserId( + int32_t userId, InputMethodStatus status) { - listInputMethodByType(userId, properties, AppExecFwk::ExtensionAbilityType::SERVICE); - listInputMethodByType(userId, properties, AppExecFwk::ExtensionAbilityType::INPUTMETHOD); - return ErrorCode::NO_ERROR; + IMSA_HILOGI("InputMethodSystemAbility::ListInputMethodByUserId"); + if (status == InputMethodStatus::ALL) { + return ListAllInputMethod(userId); + } else if (status == InputMethodStatus::ENABLE) { + return ListEnabledInputMethod(); + } else if (status == InputMethodStatus::DISABLE) { + return ListDisabledInputMethod(userId); + } + return {}; } - int32_t InputMethodSystemAbility::listInputMethodByType( - int32_t userId, std::vector *properties, AppExecFwk::ExtensionAbilityType type) + std::vector InputMethodSystemAbility::listInputMethodByType(int32_t userId, AbilityType type) { - IMSA_HILOGI("InputMethodSystemAbility::listInputMethodByUserId"); + IMSA_HILOGI("InputMethodSystemAbility::listInputMethodByType userId = %{public}d", userId); std::vector extensionInfos; bool ret = GetBundleMgr()->QueryExtensionAbilityInfos(type, userId, extensionInfos); if (!ret) { - IMSA_HILOGE("InputMethodSystemAbility::listInputMethodByUserId QueryExtensionAbilityInfos error"); - return ErrorCode::ERROR_STATUS_UNKNOWN_ERROR; + IMSA_HILOGE("InputMethodSystemAbility::listInputMethodByType QueryExtensionAbilityInfos error"); + return {}; } + std::vector properties; for (auto extension : extensionInfos) { std::shared_ptr resourceManager( Global::Resource::CreateResourceManager()); - if (!resourceManager) { - IMSA_HILOGE("InputMethodSystemAbility::listInputMethodByUserId resourcemanager is nullptr"); + if (resourceManager == nullptr) { + IMSA_HILOGE("InputMethodSystemAbility::listInputMethodByType resourcemanager is nullptr"); break; } AppExecFwk::ApplicationInfo applicationInfo = extension.applicationInfo; - InputMethodProperty *property = new InputMethodProperty(); - property->mPackageName = Str8ToStr16(extension.bundleName); - property->mAbilityName = Str8ToStr16(extension.name); - property->labelId = applicationInfo.labelId; - property->descriptionId = applicationInfo.descriptionId; resourceManager->AddResource(extension.hapPath.c_str()); std::string labelString; resourceManager->GetStringById(applicationInfo.labelId, labelString); - property->label = Str8ToStr16(labelString); std::string descriptionString; resourceManager->GetStringById(applicationInfo.descriptionId, descriptionString); - property->description = Str8ToStr16(descriptionString); - properties->push_back(property); - } - return ErrorCode::NO_ERROR; + InputMethodProperty property; + property.mPackageName = Str8ToStr16(extension.bundleName); + property.mAbilityName = Str8ToStr16(extension.name); + property.labelId = applicationInfo.labelId; + property.descriptionId = applicationInfo.descriptionId; + property.label = Str8ToStr16(labelString); + property.description = Str8ToStr16(descriptionString); + properties.push_back(property); + } + return properties; } /*! Get the keyboard type list for the given input method engine @@ -528,24 +545,28 @@ namespace MiscServices { return setting->ListKeyboardType(imeId, types); } - int32_t InputMethodSystemAbility::GetCurrentInputMethod(InputMethodProperty &property) + std::shared_ptr InputMethodSystemAbility::GetCurrentInputMethod() { IMSA_HILOGI("InputMethodSystemAbility::GetCurrentInputMethod"); - std::string propertyStr = ParaHandle::GetDefaultIme(MAIN_USER_ID); - if (propertyStr.empty()) { + std::string ime = ParaHandle::GetDefaultIme(MAIN_USER_ID); + if (ime.empty()) { IMSA_HILOGE("InputMethodSystemAbility::GetCurrentInputMethod propertyStr is empty"); - return ErrorCode::ERROR_BAD_PARAMETERS; + return nullptr; } - int pos = propertyStr.find('/'); + int pos = ime.find('/'); if (pos == -1) { IMSA_HILOGE("InputMethodSystemAbility::GetCurrentInputMethod propertyStr can not find '/'"); - return ErrorCode::ERROR_BAD_PARAMETERS; + return nullptr; } - property.mPackageName = Str8ToStr16(propertyStr.substr(0, pos)); - property.mAbilityName = Str8ToStr16(propertyStr.substr(pos + 1, propertyStr.length() - pos - 1)); - return ErrorCode::NO_ERROR; + auto property = std::make_shared(); + if (property == nullptr) { + return nullptr; + } + property->mPackageName = Str8ToStr16(ime.substr(0, pos)); + property->mAbilityName = Str8ToStr16(ime.substr(pos + 1, ime.length() - pos - 1)); + return property; } /*! Get the instance of PerUserSetting for the given user @@ -664,8 +685,9 @@ namespace MiscServices { case MSG_ID_SWITCH_INPUT_METHOD: { MessageParcel *data = msg->msgContent_; int32_t userId = data->ReadInt32(); - InputMethodProperty *target = InputMethodProperty::Unmarshalling(*data); - OnSwitchInputMethod(userId, target); + auto target = data->ReadParcelable(); + OnSwitchInputMethod(userId, *target); + delete target; break; } default: { @@ -996,24 +1018,20 @@ namespace MiscServices { return ErrorCode::NO_ERROR; } - int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, InputMethodProperty *target) + int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const InputMethodProperty &target) { IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod"); - std::vector properties; - listInputMethodByUserId(userId, &properties); - if (!properties.size()) { + const auto &properties = ListInputMethodByUserId(userId, ALL); + if (properties.empty()) { IMSA_HILOGE("InputMethodSystemAbility::OnSwitchInputMethod has no ime"); return ErrorCode::ERROR_BAD_PARAMETERS; } bool isTargetFound = false; - for (auto it = properties.begin(); it < properties.end(); ++it) { - InputMethodProperty *temp = (InputMethodProperty *)*it; - if (temp->mPackageName == target->mPackageName) { - *target = *temp; + for (const auto &property : properties) { + if (property.mPackageName == target.mPackageName) { isTargetFound = true; IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod target is found in installed packages!"); } - delete temp; } if (!isTargetFound) { IMSA_HILOGE("InputMethodSystemAbility::OnSwitchInputMethod target is not an installed package !"); @@ -1021,7 +1039,7 @@ namespace MiscServices { } std::string defaultIme = ParaHandle::GetDefaultIme(userId_); - std::string targetIme = Str16ToStr8(target->mPackageName) + "/" + Str16ToStr8(target->mAbilityName); + std::string targetIme = Str16ToStr8(target.mPackageName) + "/" + Str16ToStr8(target.mAbilityName); IMSA_HILOGI("InputMethodSystemAbility::OnSwitchInputMethod DefaultIme : %{public}s, TargetIme : %{public}s", defaultIme.c_str(), targetIme.c_str()); if (defaultIme != targetIme) { @@ -1049,14 +1067,12 @@ namespace MiscServices { void InputMethodSystemAbility::OnDisplayOptionalInputMethod(int32_t userId) { IMSA_HILOGI("InputMethodSystemAbility::OnDisplayOptionalInputMethod"); - std::vector properties; - listInputMethodByUserId(userId, &properties); - if (!properties.size()) { + const auto &properties = ListInputMethodByUserId(userId, ALL); + if (properties.empty()) { IMSA_HILOGI("InputMethodSystemAbility::OnDisplayOptionalInputMethod has no ime"); return; } - std::string params = ""; - GetInputMethodParam(properties, params); + const auto ¶ms = GetInputMethodParam(properties); IMSA_HILOGI("InputMethodSystemAbility::OnDisplayOptionalInputMethod param : %{public}s", params.c_str()); const int TITLE_HEIGHT = 62; const int SINGLE_IME_HEIGHT = 66; diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index d5ec8b4a29be3d0395bf06cd4d529ef7370f72cb..8246d507469df23ebf6a668afb4600f7f01b2e83 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -14,8 +14,11 @@ */ #include "input_method_system_ability_stub.h" -#include "message_handler.h" + +#include + #include "ipc_skeleton.h" +#include "message_handler.h" namespace OHOS { namespace MiscServices { @@ -106,40 +109,8 @@ namespace MiscServices { } break; } - case LIST_INPUT_METHOD_ENABLED: { - std::vector properties; - int32_t ret = listInputMethodEnabled(&properties); - if (ret != ErrorCode::NO_ERROR) { - reply.WriteInt32(ErrorCode::ERROR_EX_ILLEGAL_STATE); // write exception code - reply.WriteInt32(-1); - } else { - reply.WriteInt32(NO_ERROR); - int32_t size = properties.size(); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - reply.WriteParcelable(properties[i]); - } - properties.clear(); - } - break; - } case LIST_INPUT_METHOD: { - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = getUserId(uid); - std::vector properties; - int32_t ret = listInputMethodByUserId(userId, &properties); - if (ret != ErrorCode::NO_ERROR) { - reply.WriteInt32(ErrorCode::ERROR_EX_ILLEGAL_STATE); // write exception code - reply.WriteInt32(-1); - return ret; - } - reply.WriteInt32(NO_ERROR); - int32_t size = properties.size(); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - reply.WriteParcelable(properties[i]); - } - properties.clear(); + OnListInputMethod(data, reply); break; } case LIST_KEYBOARD_TYPE: { @@ -351,15 +322,11 @@ namespace MiscServices { if (parcel == nullptr) { return ErrorCode::ERROR_EX_NULL_POINTER; } - InputMethodProperty *target = InputMethodProperty::Unmarshalling(data); parcel->WriteInt32(userId); - if (!target->Marshalling(*parcel)) { - IMSA_HILOGE("InputMethodSystemAbilityStub::switchInputMethod Failed to marshall the target! "); - delete target; - delete parcel; - return ErrorCode::ERROR_IME_PROPERTY_MARSHALL; - } - delete target; + auto property = data.ReadParcelable(); + parcel->WriteParcelable(property); + delete property; + auto *msg = new (std::nothrow) Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); if (msg == nullptr) { return ErrorCode::ERROR_EX_NULL_POINTER; @@ -370,15 +337,34 @@ namespace MiscServices { void InputMethodSystemAbilityStub::OnGetCurrentInputMethod(MessageParcel &reply) { - InputMethodProperty property; - int32_t ret = GetCurrentInputMethod(property); - if (ret != NO_ERROR) { - IMSA_HILOGE("InputMethodSystemAbilityStub::OnGetCurrentInputMethod failed: %{public}d", ret); + auto property = GetCurrentInputMethod(); + if (property == nullptr) { + IMSA_HILOGE("InputMethodSystemAbilityStub::OnGetCurrentInputMethod property is nullptr"); reply.WriteInt32(ErrorCode::ERROR_GETTING_CURRENT_IME); return; } reply.WriteInt32(NO_ERROR); - reply.WriteParcelable(&property); + reply.WriteParcelable(property.get()); + } + + void InputMethodSystemAbilityStub::OnListInputMethod(MessageParcel &data, MessageParcel &reply) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::OnListInputMethod"); + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = getUserId(uid); + uint32_t status = data.ReadUint32(); + const auto &properties = ListInputMethodByUserId(userId, InputMethodStatus(status)); + if (properties.empty()) { + IMSA_HILOGE("InputMethodSystemAbilityStub ListInputMethodByUserId failed"); + reply.WriteInt32(ErrorCode::ERROR_LIST_IME); + return; + } + reply.WriteInt32(NO_ERROR); + uint32_t size = properties.size(); + reply.WriteUint32(size); + for (const auto &property : properties) { + reply.WriteParcelable(&property); + } } /*! Get user id from uid diff --git a/services/src/peruser_setting.cpp b/services/src/peruser_setting.cpp index 2fc1d645a13fab2300a42e6cff7f9bab64e69657..79a2c03b0f6afb3b9a5d0f34db5273df2e3f1716 100644 --- a/services/src/peruser_setting.cpp +++ b/services/src/peruser_setting.cpp @@ -394,21 +394,6 @@ namespace MiscServices { return &inputMethodSetting; } - /*! list the details of all the enabled input method engine - \param[out] properties the details will be written to the param properties - \return ErrorCode::NO_ERROR - */ - int32_t PerUserSetting::ListInputMethodEnabled(std::vector *properties) - { - std::u16string enabledInputMethods = inputMethodSetting.GetValue(InputMethodSetting::ENABLED_INPUT_METHODS_TAG); - for (int i = 0; i < (int)inputMethodProperties.size(); i++) { - if (enabledInputMethods.find(inputMethodProperties[i]->mImeId) != std::string::npos) { - properties->push_back(inputMethodProperties[i]); - } - } - return ErrorCode::NO_ERROR; - } - /*! List the details of all input method engine installed in the system \param[out] properties the details will be written to the param properties \return ErrorCode::NO_ERROR diff --git a/unitest/src/input_method_controller_test.cpp b/unitest/src/input_method_controller_test.cpp index b611cf391da6cf190fbfdd9cf624c9a4f735b86d..7871883716b6d174ba3b1b01230615f7734d4702 100644 --- a/unitest/src/input_method_controller_test.cpp +++ b/unitest/src/input_method_controller_test.cpp @@ -278,6 +278,29 @@ namespace MiscServices { EXPECT_TRUE(ret == 0); } + /** + * @tc.name: testIMCListInputMethod + * @tc.desc: IMC ListInputMethod + * @tc.type: FUNC + * @tc.require: issueI5OX20 + */ + HWTEST_F(InputMethodControllerTest, testIMCListInputMethod, TestSize.Level0) + { + IMSA_HILOGI("IMC ListInputMethod Test START"); + sptr imc = InputMethodController::GetInstance(); + EXPECT_NE(imc, nullptr); + + IMSA_HILOGI("Test list all input method"); + std::vector properties = imc->ListInputMethod(); + EXPECT_TRUE(!properties.empty()); + + IMSA_HILOGI("Test list disabled input method"); + properties = imc->ListInputMethod(false); + IMSA_HILOGI("Test list enabled input method"); + properties = imc->ListInputMethod(true); + EXPECT_TRUE(!properties.empty()); + } + /** * @tc.name: testIMSAProxyShowCurrentInput * @tc.desc: IMSAProxy ShowCurrentInput.