mirror of
https://gitee.com/openharmony/inputmethod_imf
synced 2024-11-24 15:21:02 +00:00
commit
17f2862408
44
frameworks/js/napi/common/BUILD.gn
Normal file
44
frameworks/js/napi/common/BUILD.gn
Normal file
@ -0,0 +1,44 @@
|
||||
# Copyright (C) 2023 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("inputmethod_js_common_config") {
|
||||
visibility = [ ":*" ]
|
||||
include_dirs = [ "./" ]
|
||||
ldflags = [ "-Wl,--exclude-libs=ALL" ]
|
||||
cflags = [
|
||||
"-fdata-sections",
|
||||
"-ffunction-sections",
|
||||
"-fvisibility=hidden",
|
||||
]
|
||||
}
|
||||
|
||||
config("inputmethod_js_common_public_config") {
|
||||
visibility = [ "./*" ]
|
||||
include_dirs = [ "./" ]
|
||||
}
|
||||
|
||||
ohos_static_library("inputmethod_js_common") {
|
||||
sources = [
|
||||
"event_checker.cpp",
|
||||
"js_util.cpp",
|
||||
]
|
||||
configs = [ ":inputmethod_js_common_config" ]
|
||||
public_configs = [ ":inputmethod_js_common_public_config" ]
|
||||
deps = []
|
||||
external_deps = [ "napi:ace_napi" ]
|
||||
subsystem_name = "inputmethod"
|
||||
part_name = "imf"
|
||||
}
|
40
frameworks/js/napi/common/event_checker.cpp
Normal file
40
frameworks/js/napi/common/event_checker.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "event_checker.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
const std::unordered_map<EventSubscribeModule, std::unordered_set<std::string>> EventChecker::EVENT_TYPES{
|
||||
{ EventSubscribeModule::INPUT_METHOD_CONTROLLER,
|
||||
{ "insertText", "deleteLeft", "deleteRight", "sendKeyboardStatus", "sendFunctionKey", "moveCursor",
|
||||
"handleExtendAction", "selectByRange", "selectByMovement" } },
|
||||
{ EventSubscribeModule::INPUT_METHOD_SETTING, { "imeChange", "imeShow", "imeHide" } },
|
||||
{ EventSubscribeModule::INPUT_METHOD_ABILITY,
|
||||
{ "inputStart", "inputStop", "keyboardShow", "keyboardHide", "setCallingWindow", "setSubtype" } },
|
||||
{ EventSubscribeModule::KEYBOARD_DELEGATE,
|
||||
{ "keyDown", "keyUp", "cursorContextChange", "selectionChange", "textChange" } },
|
||||
{ EventSubscribeModule::PANEL, { "show", "hide" } }
|
||||
};
|
||||
bool EventChecker::IsValidEventType(EventSubscribeModule module, const std::string &out)
|
||||
{
|
||||
auto it = EVENT_TYPES.find(module);
|
||||
if (it == EVENT_TYPES.end()) {
|
||||
return false;
|
||||
}
|
||||
return it->second.find(out) != it->second.end();
|
||||
}
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
40
frameworks/js/napi/common/event_checker.h
Normal file
40
frameworks/js/napi/common/event_checker.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_PARAM_CHECK_H
|
||||
#define OHOS_PARAM_CHECK_H
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
enum class EventSubscribeModule : uint32_t {
|
||||
INPUT_METHOD_CONTROLLER = 0,
|
||||
INPUT_METHOD_SETTING,
|
||||
INPUT_METHOD_ABILITY,
|
||||
KEYBOARD_DELEGATE,
|
||||
PANEL,
|
||||
};
|
||||
class EventChecker {
|
||||
public:
|
||||
static bool IsValidEventType(EventSubscribeModule module, const std::string &out);
|
||||
|
||||
private:
|
||||
static const std::unordered_map<EventSubscribeModule, std::unordered_set<std::string>> EVENT_TYPES;
|
||||
};
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_PARAM_CHECK_H
|
@ -14,8 +14,15 @@
|
||||
*/
|
||||
|
||||
#include "js_util.h"
|
||||
namespace OHOS::MiscServices {
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
constexpr int64_t JS_NUMBER_MAX_VALUE = (1LL << 53) - 1;
|
||||
napi_valuetype JsUtil::GetType(napi_env env, napi_value in)
|
||||
{
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, in, &valueType);
|
||||
return valueType;
|
||||
}
|
||||
bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
|
||||
{
|
||||
size_t size = 0;
|
||||
@ -25,6 +32,7 @@ bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out)
|
||||
}
|
||||
out.resize(size + 1, 0);
|
||||
status = napi_get_value_string_utf8(env, in, const_cast<char *>(out.data()), size + 1, &size);
|
||||
out.resize(size);
|
||||
return status == napi_ok;
|
||||
}
|
||||
bool JsUtil::GetValue(napi_env env, napi_value in, int32_t &out)
|
||||
@ -77,4 +85,5 @@ napi_value JsUtil::GetValue(napi_env env, bool in)
|
||||
napi_get_boolean(env, in, &out);
|
||||
return out;
|
||||
}
|
||||
} // namespace OHOS::MiscServices
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
@ -20,9 +20,11 @@
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
namespace OHOS::MiscServices {
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
class JsUtil {
|
||||
public:
|
||||
static napi_valuetype GetType(napi_env env, napi_value in);
|
||||
// js to native
|
||||
static bool GetValue(napi_env env, napi_value in, std::string &out);
|
||||
static bool GetValue(napi_env env, napi_value in, int32_t &out);
|
||||
@ -118,5 +120,6 @@ public:
|
||||
napi_handle_scope scope_;
|
||||
};
|
||||
};
|
||||
} // namespace OHOS::MiscServices
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_JS_UTIL_H
|
||||
|
@ -58,7 +58,10 @@ ohos_shared_library("inputmethodengine") {
|
||||
|
||||
configs = [ ":inputmethodengine_native_config" ]
|
||||
|
||||
deps = [ "${inputmethod_path}/frameworks/native/inputmethod_ability:inputmethod_ability" ]
|
||||
deps = [
|
||||
"${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common",
|
||||
"${inputmethod_path}/frameworks/native/inputmethod_ability:inputmethod_ability",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"ability_base:configuration",
|
||||
|
@ -17,12 +17,14 @@
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "event_checker.h"
|
||||
#include "input_method_ability.h"
|
||||
#include "input_method_property.h"
|
||||
#include "input_method_utils.h"
|
||||
#include "js_keyboard_controller_engine.h"
|
||||
#include "js_runtime_utils.h"
|
||||
#include "js_text_input_client_engine.h"
|
||||
#include "js_util.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "napi_base_context.h"
|
||||
@ -303,18 +305,15 @@ napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_inf
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
PARAM_CHECK_RETURN(
|
||||
env, (argc >= ARGC_TWO) && (argc <= ARGC_MAX), "Wrong number of arguments, requires 2", TYPE_NONE, nullptr);
|
||||
|
||||
std::string type = "";
|
||||
napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
NAPI_ASSERT_BASE(env, status == napi_ok, "get type failed!", nullptr);
|
||||
IMSA_HILOGE("event type is: %{public}s", type.c_str());
|
||||
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valueType);
|
||||
PARAM_CHECK_RETURN(env, valueType == napi_function, "'callback'", TYPE_FUNCTION, nullptr);
|
||||
|
||||
std::string type;
|
||||
// 2 means least param num.
|
||||
if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)
|
||||
|| JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
IMSA_HILOGD("Subscribe type:%{public}s.", type.c_str());
|
||||
auto engine = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (engine == nullptr) {
|
||||
return nullptr;
|
||||
@ -442,20 +441,22 @@ napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_i
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
PARAM_CHECK_RETURN(env, argc >= 1, "Wrong number of arguments, requires 1 or 2", TYPE_NONE, nullptr);
|
||||
std::string type = "";
|
||||
JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
IMSA_HILOGD("event type is: %{public}s", type.c_str());
|
||||
std::string type;
|
||||
// 1 means least param num.
|
||||
if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_ABILITY, type)) {
|
||||
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
// If the type of optional parameter is wrong, make it nullptr
|
||||
if (JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
argv[1] = nullptr;
|
||||
}
|
||||
IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
|
||||
auto setting = reinterpret_cast<JsInputMethodEngineSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (setting == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (argc == ARGC_TWO) {
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valueType);
|
||||
PARAM_CHECK_RETURN(env, valueType == napi_function, " 'callback' ", TYPE_FUNCTION, nullptr);
|
||||
}
|
||||
setting->UnRegisterListener(argv[ARGC_ONE], type);
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
|
@ -15,13 +15,14 @@
|
||||
|
||||
#include "js_keyboard_delegate_setting.h"
|
||||
|
||||
#include "event_checker.h"
|
||||
#include "input_method_ability.h"
|
||||
#include "js_keyboard_controller_engine.h"
|
||||
#include "js_text_input_client_engine.h"
|
||||
#include "js_util.h"
|
||||
#include "js_utils.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
constexpr size_t ARGC_ZERO = 0;
|
||||
@ -216,15 +217,15 @@ napi_value JsKeyboardDelegateSetting::Subscribe(napi_env env, napi_callback_info
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
PARAM_CHECK_RETURN(env, argc >= ARGC_TWO, "Wrong number of arguments, requires 2", TYPE_NONE, nullptr);
|
||||
std::string type = "";
|
||||
JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
IMSA_HILOGE("event type is: %{public}s", type.c_str());
|
||||
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valueType);
|
||||
PARAM_CHECK_RETURN(env, valueType == napi_function, "callback", TYPE_FUNCTION, nullptr);
|
||||
|
||||
std::string type;
|
||||
// 2 means least param num.
|
||||
if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_DELEGATE, type)
|
||||
|| JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
IMSA_HILOGD("Subscribe type:%{public}s.", type.c_str());
|
||||
auto engine = reinterpret_cast<JsKeyboardDelegateSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (engine == nullptr) {
|
||||
return nullptr;
|
||||
@ -245,21 +246,22 @@ napi_value JsKeyboardDelegateSetting::UnSubscribe(napi_env env, napi_callback_in
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, nullptr);
|
||||
|
||||
std::string type = "";
|
||||
JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
|
||||
std::string type;
|
||||
// 1 means least param num.
|
||||
if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_DELEGATE, type)) {
|
||||
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
// If the type of optional parameter is wrong, make it nullptr
|
||||
if (JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
argv[1] = nullptr;
|
||||
}
|
||||
IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
|
||||
auto delegate = reinterpret_cast<JsKeyboardDelegateSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (delegate == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (argc == ARGC_TWO) {
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valueType);
|
||||
PARAM_CHECK_RETURN(env, valueType == napi_function, "callback", TYPE_FUNCTION, nullptr);
|
||||
}
|
||||
delegate->UnRegisterListener(argv[ARGC_ONE], type);
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
|
@ -15,7 +15,9 @@
|
||||
|
||||
#include "js_panel.h"
|
||||
|
||||
#include "event_checker.h"
|
||||
#include "input_method_ability.h"
|
||||
#include "js_util.h"
|
||||
#include "js_utils.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "panel_listener_impl.h"
|
||||
@ -251,16 +253,15 @@ napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info)
|
||||
napi_value argv[ARGC_MAX] = { nullptr };
|
||||
napi_value thisVar = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
||||
// 2 means it has two params.
|
||||
NAPI_ASSERT(env, (argc >= 2) && (argc <= ARGC_MAX), "err number of argument!");
|
||||
std::string type = "";
|
||||
// 0 means the first param type<std::string>
|
||||
JsUtils::GetValue(env, argv[0], type);
|
||||
IMSA_HILOGD("on event type is: %{public}s", type.c_str());
|
||||
|
||||
napi_valuetype valuetype = napi_undefined;
|
||||
napi_typeof(env, argv[1], &valuetype);
|
||||
NAPI_ASSERT(env, valuetype == napi_function, "callback is not a function");
|
||||
std::string type;
|
||||
// 2 means least param num.
|
||||
if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type)
|
||||
|| JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
IMSA_HILOGD("Subscribe type:%{public}s", type.c_str());
|
||||
std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
|
||||
auto inputMethodPanel = UnwrapPanel(env, thisVar);
|
||||
// 1 means the second param callback.
|
||||
@ -277,11 +278,19 @@ napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info)
|
||||
napi_value argv[ARGC_MAX] = { nullptr };
|
||||
napi_value thisVar = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments, requires 1 or 2");
|
||||
std::string type = "";
|
||||
// 0 means the first param type<std::string>
|
||||
JsUtils::GetValue(env, argv[0], type);
|
||||
IMSA_HILOGI("event type is: %{public}s", type.c_str());
|
||||
std::string type;
|
||||
// 1 means least param num.
|
||||
if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type)) {
|
||||
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
|
||||
JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "please check the params", TYPE_NONE);
|
||||
return nullptr;
|
||||
}
|
||||
// If the type of optional parameter is wrong, make it nullptr
|
||||
if (JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
argv[1] = nullptr;
|
||||
}
|
||||
IMSA_HILOGD("UnSubscribe type:%{public}s", type.c_str());
|
||||
std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
|
||||
auto inputMethodPanel = UnwrapPanel(env, thisVar);
|
||||
observer->RemoveInfo(type, inputMethodPanel->windowId_);
|
||||
|
@ -48,7 +48,10 @@ ohos_shared_library("inputmethod") {
|
||||
|
||||
configs = [ ":imf_config" ]
|
||||
|
||||
deps = [ "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client" ]
|
||||
deps = [
|
||||
"${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common",
|
||||
"${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"ability_runtime:abilitykit_native",
|
||||
|
@ -16,9 +16,11 @@
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "event_checker.h"
|
||||
#include "input_method_controller.h"
|
||||
#include "input_method_utils.h"
|
||||
#include "js_get_input_method_textchange_listener.h"
|
||||
#include "js_util.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "string_ex.h"
|
||||
@ -314,12 +316,16 @@ napi_value JsGetInputMethodController::Subscribe(napi_env env, napi_callback_inf
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
PARAM_CHECK_RETURN(env, argc > 1, "should 2 parameters!", TYPE_NONE, nullptr);
|
||||
|
||||
std::string type = "";
|
||||
napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
PARAM_CHECK_RETURN(env, status == napi_ok, "callback", TYPE_FUNCTION, nullptr);
|
||||
|
||||
std::string type;
|
||||
// 2 means least param num.
|
||||
if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_CONTROLLER, type)
|
||||
|| JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
|
||||
JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "please check the params", TYPE_NONE);
|
||||
return nullptr;
|
||||
}
|
||||
IMSA_HILOGD("Subscribe type:%{public}s.", type.c_str());
|
||||
if (TEXT_EVENT_TYPE.find(type) != TEXT_EVENT_TYPE.end()) {
|
||||
if (!InputMethodController::GetInstance()->WasAttached()) {
|
||||
JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_DETACHED, "need to be attached first", TYPE_NONE);
|
||||
@ -347,17 +353,14 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
if (argc < 1) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string type;
|
||||
napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
if ((status != napi_ok) || (EVENT_TYPE.find(type) == EVENT_TYPE.end() &&
|
||||
TEXT_EVENT_TYPE.find(type) == TEXT_EVENT_TYPE.end())) {
|
||||
// 1 means least param num.
|
||||
if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_CONTROLLER, type)) {
|
||||
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
|
||||
auto engine = reinterpret_cast<JsGetInputMethodController *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (engine == nullptr) {
|
||||
return nullptr;
|
||||
|
@ -15,10 +15,12 @@
|
||||
|
||||
#include "js_get_input_method_setting.h"
|
||||
|
||||
#include "event_checker.h"
|
||||
#include "input_client_info.h"
|
||||
#include "input_method_controller.h"
|
||||
#include "input_method_status.h"
|
||||
#include "js_input_method.h"
|
||||
#include "js_util.h"
|
||||
#include "js_utils.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
@ -32,8 +34,6 @@ constexpr size_t ARGC_ONE = 1;
|
||||
constexpr size_t ARGC_TWO = 2;
|
||||
thread_local napi_ref JsGetInputMethodSetting::IMSRef_ = nullptr;
|
||||
const std::string JsGetInputMethodSetting::IMS_CLASS_NAME = "InputMethodSetting";
|
||||
const std::map<std::string, EventType> EVENT_TYPE{ { "imeChange", IME_CHANGE }, { "imeShow", IME_SHOW },
|
||||
{ "imeHide", IME_HIDE } };
|
||||
const std::map<InputWindowStatus, std::string> PANEL_STATUS{ { InputWindowStatus::SHOW, "imeShow" },
|
||||
{ InputWindowStatus::HIDE, "imeHide" } };
|
||||
std::mutex JsGetInputMethodSetting::msMutex_;
|
||||
@ -353,20 +353,18 @@ int32_t JsGetInputMethodSetting::RegisterListener(
|
||||
IMSA_HILOGD("RegisterListener %{public}s", type.c_str());
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
if (jsCbMap_.empty()) {
|
||||
auto eventType = EVENT_TYPE.find(type)->second;
|
||||
InputMethodController::GetInstance()->SetSettingListener(inputMethod_);
|
||||
auto ret = InputMethodController::GetInstance()->UpdateListenEventFlag(eventType, true);
|
||||
auto ret = InputMethodController::GetInstance()->UpdateListenEventFlag(type, true);
|
||||
if (ret != ErrorCode::NO_ERROR) {
|
||||
IMSA_HILOGE("UpdateListenEventFlag failed, ret: %{public}d, eventType: %{public}u", ret, eventType);
|
||||
IMSA_HILOGE("UpdateListenEventFlag failed, ret: %{public}d, type: %{public}s", ret, type.c_str());
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (!jsCbMap_.empty() && jsCbMap_.find(type) == jsCbMap_.end()) {
|
||||
IMSA_HILOGI("start type: %{public}s listening.", type.c_str());
|
||||
auto eventType = EVENT_TYPE.find(type)->second;
|
||||
auto ret = InputMethodController::GetInstance()->UpdateListenEventFlag(eventType, true);
|
||||
auto ret = InputMethodController::GetInstance()->UpdateListenEventFlag(type, true);
|
||||
if (ret != ErrorCode::NO_ERROR) {
|
||||
IMSA_HILOGE("UpdateListenEventFlag failed, ret: %{public}d, eventType: %{public}u", ret, eventType);
|
||||
IMSA_HILOGE("UpdateListenEventFlag failed, ret: %{public}d, type: %{public}s", ret, type.c_str());
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@ -392,16 +390,15 @@ napi_value JsGetInputMethodSetting::Subscribe(napi_env env, napi_callback_info i
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
NAPI_ASSERT(env, argc > ARGC_ONE, "Wrong number of arguments, requires least 2");
|
||||
|
||||
std::string type;
|
||||
JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
NAPI_ASSERT(env, EVENT_TYPE.find(type) != EVENT_TYPE.end(), "subscribe type error");
|
||||
|
||||
napi_valuetype valuetype = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valuetype);
|
||||
NAPI_ASSERT(env, valuetype == napi_function, "callback is not a function");
|
||||
|
||||
// 2 means least param num.
|
||||
if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_SETTING, type)
|
||||
|| JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
IMSA_HILOGD("Subscribe type:%{public}s.", type.c_str());
|
||||
auto engine = reinterpret_cast<JsGetInputMethodSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (engine == nullptr) {
|
||||
return nullptr;
|
||||
@ -430,7 +427,7 @@ void JsGetInputMethodSetting::UnRegisterListener(napi_value callback, std::strin
|
||||
if (callback == nullptr) {
|
||||
jsCbMap_.erase(type);
|
||||
IMSA_HILOGI("stop all type: %{public}s listening.", type.c_str());
|
||||
InputMethodController::GetInstance()->UpdateListenEventFlag(EVENT_TYPE.find(type)->second, false);
|
||||
InputMethodController::GetInstance()->UpdateListenEventFlag(type, false);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -444,7 +441,7 @@ void JsGetInputMethodSetting::UnRegisterListener(napi_value callback, std::strin
|
||||
if (jsCbMap_[type].empty()) {
|
||||
IMSA_HILOGI("stop last type: %{public}s listening.", type.c_str());
|
||||
jsCbMap_.erase(type);
|
||||
InputMethodController::GetInstance()->UpdateListenEventFlag(EVENT_TYPE.find(type)->second, false);
|
||||
InputMethodController::GetInstance()->UpdateListenEventFlag(type, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -455,21 +452,23 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
||||
NAPI_ASSERT(env, argc > ARGC_ZERO, "Wrong number of arguments, requires least 1");
|
||||
|
||||
std::string type;
|
||||
JsUtils::GetValue(env, argv[ARGC_ZERO], type);
|
||||
NAPI_ASSERT(env, EVENT_TYPE.find(type) != EVENT_TYPE.end(), "subscribe type error");
|
||||
// 1 means least param num.
|
||||
if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
|
||||
|| !EventChecker::IsValidEventType(EventSubscribeModule::INPUT_METHOD_SETTING, type)) {
|
||||
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
// If the type of optional parameter is wrong, make it nullptr
|
||||
if (JsUtil::GetType(env, argv[1]) != napi_function) {
|
||||
argv[1] = nullptr;
|
||||
}
|
||||
IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
|
||||
|
||||
auto engine = reinterpret_cast<JsGetInputMethodSetting *>(JsUtils::GetNativeSelf(env, info));
|
||||
if (engine == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (argc > ARGC_ONE) {
|
||||
napi_valuetype valuetype = napi_undefined;
|
||||
napi_typeof(env, argv[ARGC_ONE], &valuetype);
|
||||
NAPI_ASSERT(env, valuetype == napi_function, "callback is not a function");
|
||||
}
|
||||
engine->UnRegisterListener(argv[ARGC_ONE], type);
|
||||
napi_value result = nullptr;
|
||||
napi_get_null(env, &result);
|
||||
|
@ -41,6 +41,8 @@ constexpr int32_t LOOP_COUNT = 5;
|
||||
constexpr int32_t WAIT_TIME = 100;
|
||||
constexpr int64_t DELAY_TIME = 100;
|
||||
constexpr int32_t KEYBOARD_SHOW = 2;
|
||||
const std::unordered_map<std::string, EventType> EVENT_TYPE{ { "imeChange", IME_CHANGE }, { "imeShow", IME_SHOW },
|
||||
{ "imeHide", IME_HIDE } };
|
||||
InputMethodController::InputMethodController() : stop_(false)
|
||||
{
|
||||
IMSA_HILOGI("InputMethodController structure");
|
||||
@ -81,8 +83,13 @@ int32_t InputMethodController::RestoreListenEventFlag()
|
||||
return proxy->UpdateListenEventFlag(clientInfo_, IME_NONE);
|
||||
}
|
||||
|
||||
int32_t InputMethodController::UpdateListenEventFlag(EventType eventType, bool isOn)
|
||||
int32_t InputMethodController::UpdateListenEventFlag(const std::string &type, bool isOn)
|
||||
{
|
||||
auto it = EVENT_TYPE.find(type);
|
||||
if (it == EVENT_TYPE.end()) {
|
||||
return ErrorCode::ERROR_BAD_PARAMETERS;
|
||||
}
|
||||
auto eventType = it->second;
|
||||
auto proxy = GetSystemAbilityProxy();
|
||||
if (proxy == nullptr) {
|
||||
IMSA_HILOGE("proxy is nullptr");
|
||||
|
@ -208,7 +208,7 @@ public:
|
||||
* @since 6
|
||||
*/
|
||||
IMF_API void SetSettingListener(std::shared_ptr<InputMethodSettingListener> listener);
|
||||
IMF_API int32_t UpdateListenEventFlag(EventType eventType, bool isOn);
|
||||
IMF_API int32_t UpdateListenEventFlag(const std::string &type, bool isOn);
|
||||
IMF_API void SetControllerListener(std::shared_ptr<ControllerListener> controllerListener);
|
||||
|
||||
/**
|
||||
|
@ -143,7 +143,7 @@ void TestShowSomething(sptr<InputMethodController> imc)
|
||||
|
||||
auto settingListener = std::make_shared<SettingListener>();
|
||||
imc->SetSettingListener(settingListener);
|
||||
imc->UpdateListenEventFlag(IME_CHANGE, true);
|
||||
imc->UpdateListenEventFlag("imeChange", true);
|
||||
|
||||
imc->StopInputSession();
|
||||
imc->Close();
|
||||
|
@ -235,13 +235,13 @@ void InputMethodPanelTest::ImcPanelListeningTestPrepare(
|
||||
break;
|
||||
}
|
||||
case ListeningStatus::ON: {
|
||||
imc_->UpdateListenEventFlag(IME_HIDE, true);
|
||||
imc_->UpdateListenEventFlag(IME_SHOW, true);
|
||||
imc_->UpdateListenEventFlag("imeShow", true);
|
||||
imc_->UpdateListenEventFlag("imeHide", true);
|
||||
break;
|
||||
}
|
||||
case ListeningStatus::OFF: {
|
||||
imc_->UpdateListenEventFlag(IME_HIDE, false);
|
||||
imc_->UpdateListenEventFlag(IME_SHOW, false);
|
||||
imc_->UpdateListenEventFlag("imeShow", false);
|
||||
imc_->UpdateListenEventFlag("imeHide", false);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -90,7 +90,7 @@ void InputMethodSwitchTest::SetUpTestCase(void)
|
||||
GrantNativePermission();
|
||||
imc_ = InputMethodController::GetInstance();
|
||||
imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
|
||||
imc_->UpdateListenEventFlag(IME_CHANGE, true);
|
||||
imc_->UpdateListenEventFlag("imeChange", true);
|
||||
}
|
||||
|
||||
void InputMethodSwitchTest::TearDownTestCase(void)
|
||||
|
@ -86,7 +86,7 @@ void NewImeSwitchTest::SetUpTestCase(void)
|
||||
GrantNativePermission();
|
||||
imc_ = InputMethodController::GetInstance();
|
||||
imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
|
||||
imc_->UpdateListenEventFlag(IME_CHANGE, true);
|
||||
imc_->UpdateListenEventFlag("imeChange", true);
|
||||
}
|
||||
|
||||
void NewImeSwitchTest::TearDownTestCase(void)
|
||||
|
Loading…
Reference in New Issue
Block a user