update napi

Signed-off-by: yangliu <yangliu146@huawei.com>
This commit is contained in:
yangliu 2022-07-25 21:38:51 +08:00
parent 551fd98ee6
commit 972a71a2ab
10 changed files with 985 additions and 179 deletions

View File

@ -1,60 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {AsyncCallback} from './basic';
/**
* inputmethod
*
* @since 8
* @devices phone, tablet, tv, wearable
*/
declare namespace inputMethod {
const MAX_TYPE_NUM: number
function getInputMethodSetting(): InputMethodSetting;
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<boolean>;
interface InputMethodSetting {
listInputMethod(callback: AsyncCallback<Array<InputMethodProperty>>): void;
listInputMethod(): Promise<Array<InputMethodProperty>>;
displayOptionalInputMethod(callback: AsyncCallback<void>): void;
displayOptionalInputMethod(): Promise<void>;
}
interface InputMethodController {
stopInput(callback: AsyncCallback<boolean>): void;
stopInput(): Promise<boolean>;
}
interface InputMethodProperty {
readonly packageName: string;
readonly methodId: string;
}
}
export default inputMethod;

View File

@ -1,119 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback } from './basic';
/**
* inputmethod
*
* @since 8
* @devices phone, tablet, tv, wearable
*/
declare namespace inputMethodEngine {
const ENTER_KEY_TYPE_UNSPECIFIED: number;
const ENTER_KEY_TYPE_GO: number;
const ENTER_KEY_TYPE_SEARCH: number;
const ENTER_KEY_TYPE_SEND: number;
const ENTER_KEY_TYPE_NEXT: number;
const ENTER_KEY_TYPE_DONE: number;
const ENTER_KEY_TYPE_PREVIOUS: number;
const PATTERN_NULL: number;
const PATTERN_TEXT: number;
const PATTERN_NUMBER: number;
const PATTERN_PHONE: number;
const PATTERN_DATETIME: number;
const PATTERN_EMAIL: number;
const PATTERN_URI: number;
const PATTERN_PASSWORD: number;
const FLAG_SELECTING: number;
const FLAG_SINGLE_LINE: number;
const DISPLAY_MODE_PART: number;
const DISPLAY_MODE_FULL: number;
const OPTION_ASCII: number;
const OPTION_NONE: number;
const OPTION_AUTO_CAP_CHARACTERS: number;
const OPTION_AUTO_CAP_SENTENCES: number;
const OPTION_AUTO_WORDS: number;
const OPTION_MULTI_LINE: number;
const OPTION_NO_FULLSCREEN: number;
function getInputMethodEngine(): InputMethodEngine;
function createKeyboardDelegate(): KeyboardDelegate;
interface KeyboardController {
hideKeyboard(callbakc: AsyncCallback<void>): void;
hideKeyboard(): Promise<void>;
}
interface InputMethodEngine {
on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void;
off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void;
on(type: 'keyboardShow', callback: () => void): void;
off(type: 'keyboardShow', callback: () => void): void;
on(type: 'keyboardHide', callback: () => void): void;
off(type: 'keyboardHide', callback: () => void): void;
}
interface TextInputClient {
sendKeyFunction(action: number, callback: AsyncCallback<boolean>): void;
sendKeyFunction(action: number): Promise<boolean>;
deleteForward(length: number, callback: AsyncCallback<boolean>): void;
deleteForward(length: number): Promise<boolean>;
deleteBackward(length: number, callback: AsyncCallback<boolean>): void;
deleteBackward(length: number): Promise<boolean>;
InsertText(text: string, callback: AsyncCallback<boolean>): void;
InsertText(text: string): Promise<boolean>;
getForward(length: number, callback: AsyncCallback<string>): void;
getForward(length: number): Promise<string>;
getEditorAttribute(lcallback: AsyncCallback<EditorAttribute>): void;
getEditorAttribute(): Promise<EditorAttribute>;
}
interface KeyboardDelegate {
on(type: 'keyDown', callback: (event: KeyEvent) => boolean): void;
off(type: 'keyDown', callback?: (event: KeyEvent) => boolean): void;
on(type: 'keyUp', callback: (event: KeyEvent) => boolean): void;
off(type: 'keyUp', callback?: (event: KeyEvent) => boolean): void;
on(type: 'cursorContextChange', callback: (x: number, y: number, height: number) => void): void;
off(type: 'cursorContextChange', callback?: (x: number, y: number, height: number) => void): void;
on(type: 'selectionChange', callback: (oldBegine: number, oldEnd: number, newBegine: number, newEnd: number) => void): void;
off(type: 'selectionChange', callback?: (oldBegine: number, oldEnd: number, newBegine: number, newEnd: number) => void): void;
on(type: 'textChange', callback: (text: string) => void): void;
off(type: 'textChange', callback?: (text: string) => void): void;
}
interface EditorAttribute {
readonly inputPattern: number;
readonly enterKeyType: number;
}
}
export default inputMethodEngine;

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_context.h"
#include "global.h"
namespace OHOS {
namespace MiscServices {
ContextBase::~ContextBase()
{
// ZLOGD("no memory leak after callback or promise[resolved/rejected]");
// if (env != nullptr) { //检查env
// if (work != nullptr) {
// napi_delete_async_work(env, work);
// }
// if (callbackRef != nullptr) {
// napi_delete_reference(env, callbackRef);
// }
// napi_delete_reference(env, selfRef);
// env = nullptr;
// }
}
napi_status ContextBase::GetNative(napi_env envi, napi_callback_info info)
{
env = envi;
size_t argc = ARGC_MAX;
napi_value argv[ARGC_MAX] = { nullptr };
napi_status status = napi_invalid_arg;
status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
if (self == nullptr && argc >= ARGC_MAX) {
//LOG
return status;
}
napi_create_reference(env, self, 1, &selfRef);
status = napi_unwrap(env, self, &native);
return status;
}
napi_value ContextBase::GetErrorCodeValue(napi_env env, int errCode)
{
napi_value jsObject = nullptr;
napi_value jsValue = nullptr;
NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
NAPI_CALL(env, napi_create_object(env, &jsObject));
NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
return jsObject;
}
void ContextBase::ParseContext(napi_env envi, napi_callback_info info, NapiCbInfoParser parse)//函数增加返回值
{
IMSA_HILOGE("run in ParseContext");
env = envi;
size_t argc = ARGC_MAX;
napi_value argv[ARGC_MAX] = { nullptr };
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
IMSA_HILOGE("ListInputMethod::napi_get_cb_info status is: %{public}d and argc is %{public}zu: ",status,argc);
if (status != napi_ok || argc >= ARGC_MAX) {
//LOG
return;
}
// CHECK_ARGS_RETURN_VOID(this, self != nullptr, "no JavaScript this argument!");
// napi_create_reference(env, self, 1, &selfRef);
// status = napi_unwrap(env, self, &native);
// CHECK_STATUS_RETURN_VOID(this, "self unwrap failed!");
IMSA_HILOGE("ParseContext argc is %{public}zu", argc);
if (argc > 0) {
// get the last arguments :: <callback>
size_t index = argc - 1;
napi_valuetype type = napi_undefined;
napi_status tyst = napi_typeof(env, argv[index], &type);
IMSA_HILOGE("ListInputMethod::ParseContext tyst is: %{public}d and type is %{public}d: ",tyst,type);
if ((tyst == napi_ok) && (type == napi_function)) {
IMSA_HILOGE("ListInputMethod::ParseContext callabck");
status = napi_create_reference(env, argv[index], 1, &callbackRef);
if (status != napi_ok) {
//LOG
return;
}
// CHECK_STATUS_RETURN_VOID(this, "ref callback failed!");
argc = index;
// ZLOGD("async callback, no promise");
} else {
// ZLOGD("no callback, async pormose");
}
}
if (parse) {
parse(argc, argv);
} else {
//LOG
return;
// CHECK_ARGS_RETURN_VOID(this, argc == 0, "required no arguments!");
}
}
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERFACE_JS_CONTEXT_H
#define INTERFACE_JS_CONTEXT_H
#include "input_method_property.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
namespace OHOS {
namespace MiscServices {
constexpr size_t ARGC_MAX = 6;
using NapiCbInfoParser = std::function<void(size_t argc, napi_value* argv)>;
struct ContextBase {
virtual ~ContextBase();
void ParseContext(napi_env env, napi_callback_info Info, NapiCbInfoParser prase = NapiCbInfoParser());
napi_status GetNative(napi_env env, napi_callback_info info);
napi_value GetErrorCodeValue(napi_env env, int errCode);
napi_env env = nullptr;
napi_value self = nullptr;
napi_ref selfRef = nullptr;
napi_async_work work = nullptr;
napi_deferred deferred = nullptr;
napi_ref callbackRef = nullptr;
// napi_status jsstatus = napi_invalid_arg;
napi_value outData = nullptr;
napi_callback_info info;
void* native = nullptr;
int32_t errCode;
// std::vector<InputMethodProperty*> properties;//后续继承 1.数据错误吗共同放入结构体?
};
}
}
#endif // INTERFACE_JS_CONTEXT_H

View File

@ -0,0 +1,195 @@
/*
* Copyright (c) 2021-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.
*/
#include "input_method_controller.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "event_handler.h"
#include "event_runner.h"
#include "string_ex.h"
#include "js_getInputMethodController.h"
namespace OHOS {
namespace MiscServices {
void JsGetInputMethodController::CBOrPromiseStopInput(napi_env env, const StopInputInfo *stopInput, napi_value err, napi_value data)
{
IMSA_HILOGE("run in CBOrPromiseStopInput");
napi_value args[RESULT_COUNT] = {err, data};
if (stopInput->deferred) {
if (stopInput->status == napi_ok) {
IMSA_HILOGE("CBOrPromiseStopInput::promise");
napi_resolve_deferred(env, stopInput->deferred, args[RESULT_DATA]);
} else {
napi_reject_deferred(env, stopInput->deferred, args[RESULT_ERROR]);
}
} else {
IMSA_HILOGE("CBOrPromiseStopInput::callback");
napi_value callback = nullptr;
napi_get_reference_value(env, stopInput->callbackRef, &callback);
if (stopInput->callbackRef == nullptr) {
IMSA_HILOGE("CBOrPromiseStopInput::callback2222222222xxxxxxxxx");
}
IMSA_HILOGE("CBOrPromiseStopInput::callback2222222222");
napi_value returnVal = nullptr;
if (callback == nullptr) {
IMSA_HILOGE("CBOrPromiseStopInput::callback333333xxxxxxxxxxxxxx");
}
napi_call_function(env, nullptr, callback, RESULT_COUNT, &args[0], &returnVal);
IMSA_HILOGE("CBOrPromiseStopInput::callback3333333333");
if (stopInput->callbackRef != nullptr) {
napi_delete_reference(env, stopInput->callbackRef);
}
}
}
napi_value JsGetInputMethodController::Init(napi_env env, napi_value info)
{
napi_property_descriptor descriptor[] = {
DECLARE_NAPI_FUNCTION("getInputMethodController", GetInputMethodController),
};
NAPI_CALL(
env, napi_define_properties(env, info, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("stopInput", StopInput),
};
napi_value cons = nullptr;
NAPI_CALL(env, napi_define_class(env, IMC_CLASS_NAME.c_str(), IMC_CLASS_NAME.size(),
JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
NAPI_CALL(env, napi_create_reference(env, cons, 1, &IMSRef_));
NAPI_CALL(env, napi_set_named_property(env, info, IMC_CLASS_NAME.c_str(), cons));
return info;
}
napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback_info cbinfo)
{
IMSA_HILOGI("run in JsConstructor");
napi_value thisVar = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
JsGetInputMethodController *IMSobject = new (std::nothrow) JsGetInputMethodController();
if (IMSobject == nullptr) {
IMSA_HILOGI("IMSobject is nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
napi_wrap(env, thisVar, IMSobject, [](napi_env env, void *data, void *hint) {
auto* objInfo = reinterpret_cast<JsGetInputMethodController*>(data);
if (objInfo != nullptr) {
IMSA_HILOGI("objInfo is nullptr");
delete objInfo;
}
//LOG()
}, nullptr, nullptr);
return thisVar;
}
napi_value JsGetInputMethodController::GetInputMethodController(napi_env env, napi_callback_info cbInfo)
{
IMSA_HILOGE("run in GetInputMethodController");
napi_value instance = nullptr;
napi_value cons = nullptr;
if (napi_get_reference_value(env, IMSRef_, &cons) != napi_ok) {
IMSA_HILOGE("GetInputMethodSetting::napi_get_reference_value not ok");
return nullptr;
}
if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
IMSA_HILOGE("GetInputMethodSetting::napi_new_instance not ok");
return nullptr;
}
IMSA_HILOGE("New the js instance complete");
return instance;
}
napi_value JsGetInputMethodController::GetErrorCodeValue(napi_env env, ErrCode errCode)
{
IMSA_HILOGE("run in GetErrorCodeValue");
napi_value jsObject = nullptr;
napi_value jsValue = nullptr;
NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
NAPI_CALL(env, napi_create_object(env, &jsObject));
NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
return jsObject;
}
napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_info Info)
{
IMSA_HILOGE("run in ListInputMethod");
struct StopInputContext : public ContextBase {
bool sStopInput = false;
}
StopInputContext *stopInput = new (std::nothrow) StopInputContext();
if (stopInput == nullptr) {
IMSA_HILOGE("ListInputMethod::stopInput is nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
stopInput->env = env;
stopInput->callbackRef = nullptr;
stopInput->ParseContext(env, Info);
napi_value promise = nullptr;
if (stopInput->callbackRef == nullptr) {
napi_create_promise(env, &stopInput->deferred, &promise);
} else {
napi_get_undefined(env, &promise);
}
napi_value resource = nullptr;
napi_create_string_utf8(env, "StopInput", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env,
nullptr,
resource,
[](napi_env env, void *data) {
IMSA_HILOGI("ListInputMethod::napi_create_async_work in");
StopInputInfo *stopInput = reinterpret_cast<StopInputInfo *>(data);
stopInput->errCode = InputMethodController::GetInstance()->HideCurrentInput();
IMSA_HILOGI("ListInputMethod::************************************");
if (stopInput->errCode == 0) {
IMSA_HILOGI("JsGetInputMethodController::StopInput successful!");
stopInput->sStopInput = true;
}
stopInput->status = (stopInput->errCode == 0) ? napi_ok : napi_generic_failure;
},
[](napi_env env, napi_status status, void *data) {
IMSA_HILOGI("ListInputMethod::napi_create_async_work out");
StopInputInfo *stopInput = reinterpret_cast<StopInputInfo *>(data);
if (stopInput == nullptr) {
IMSA_HILOGI("StopInput::stopInput is nullptr");
return;
}
stopInput->errCode = 0;
napi_value getResult[RESULT_COUNT] = {0};
getResult[PARAMZERO] = GetErrorCodeValue(env, stopInput->errCode);;
napi_get_boolean(env, stopInput->sStopInput, &getResult[PARAMONE]);
CBOrPromiseStopInput(env, stopInput, getResult[PARAMZERO], getResult[PARAMONE]);
napi_delete_async_work(env, stopInput->work);
delete stopInput;
stopInput = nullptr;
},
reinterpret_cast<void *>(stopInput),
&stopInput->work);
napi_queue_async_work(env, stopInput->work);
return promise;
}
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H
#define INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "global.h"
#include "js_context.h"
#include "js_input_method.h"
namespace OHOS {
namespace MiscServices {
const std::string IMC_CLASS_NAME = "InputMethodController";
class JsGetInputMethodController {
public:
JsGetInputMethodController() = default;
~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);
private:
static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
static napi_value GetErrorCodeValue(napi_env env, ErrCode errCode);
static void CBOrPromiseStopInput(napi_env env,
const StopInputInfo *stopInput, napi_value err, napi_value data);
};
}
}
#endif // INTERFACE_KITS_JS_GETINPUT_METHOD_CCONTROLLER_H

View File

@ -0,0 +1,299 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_get_input_method_setting.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 {
napi_value JsGetInputMethodSetting::Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptor[] = {
DECLARE_NAPI_FUNCTION("getInputMethodSetting", GetInputMethodSetting),
};
NAPI_CALL(
env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("listInputMethod", ListInputMethod),
DECLARE_NAPI_FUNCTION("displayOptionalInputMethod", DisplayOptionalInputMethod),
};
napi_value cons = nullptr;
NAPI_CALL(env, napi_define_class(env, IMS_CLASS_NAME.c_str(), IMS_CLASS_NAME.size(),
JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
NAPI_CALL(env, napi_create_reference(env, cons, 1, &IMSRef_));
NAPI_CALL(env, napi_set_named_property(env, exports, IMS_CLASS_NAME.c_str(), cons));
return exports;
}
napi_value JsGetInputMethodSetting::JsConstructor(napi_env env, napi_callback_info cbinfo)
{
IMSA_HILOGE("run in JsConstructor");
napi_value thisVar = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
JsGetInputMethodSetting *IMSobject = new (std::nothrow) JsGetInputMethodSetting();
if (IMSobject == nullptr) {
IMSA_HILOGE("IMSobject is nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
napi_wrap(env, thisVar, IMSobject, [](napi_env env, void *data, void *hint) {
auto* objInfo = reinterpret_cast<JsGetInputMethodSetting*>(data);
if (objInfo != nullptr) {
IMSA_HILOGE("objInfo is nullptr");
delete objInfo;
}
//LOG()
}, nullptr, nullptr);
return thisVar;
}
napi_value JsGetInputMethodSetting::GetInputMethodSetting(napi_env env, napi_callback_info info)
{
IMSA_HILOGE("run in GetInputMethodSetting");
napi_value instance = nullptr;
napi_value cons = nullptr;
if (napi_get_reference_value(env, IMSRef_, &cons) != napi_ok) {
IMSA_HILOGE("GetInputMethodSetting::napi_get_reference_value not ok");
return nullptr;
}
// LOG("Get a reference to the global variable appAccountRef_ complete");
if (napi_new_instance(env, cons, 0, nullptr, &instance) != napi_ok) {
IMSA_HILOGE("GetInputMethodSetting::napi_new_instance not ok");
return nullptr;
}
// LOGI("New the js instance complete");
IMSA_HILOGE("New the js instance complete");
return instance;
}
void JsGetInputMethodSetting::GetResult(napi_env env, std::vector<InputMethodProperty*> &properties, napi_value &result)
{
IMSA_HILOGE("run in GetResult");
uint32_t index = 0;
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, packageName.c_str(), NAPI_AUTO_LENGTH, &jsMethodId);
napi_set_named_property(env, InputMethodSetting, "methodId", jsMethodId);
napi_set_element(env, result, index, InputMethodSetting);
index++;
}
IMSA_HILOGE("GetResult::index is %{public}d", index);
}
void JsGetInputMethodSetting::ProcessCallbackOrPromiseCBArray(napi_env env,ContextBase *asyncContext)
{
IMSA_HILOGE("run in ProcessCallbackOrPromiseCBArray");
napi_value jsCode = asyncContext->GetErrorCodeValue(env, asyncContext->errCode);
napi_value args[RESULT_ALL] = { jsCode, asyncContext->outData };
if (asyncContext->deferred) {
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::promise");
if (asyncContext->errCode == ErrorCode::NO_ERROR) {
napi_resolve_deferred(env, asyncContext->deferred, args[RESULT_DATA]);
} else {
napi_reject_deferred(env, asyncContext->deferred, args[RESULT_ERROR]);
}
} else {
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::callback");
napi_value callback = nullptr;
napi_get_reference_value(env, asyncContext->callbackRef, &callback);
if (asyncContext->callbackRef == nullptr) {
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::callback2222222222xxxxxxxxx");
}
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::callback2222222222");
napi_value returnVal = nullptr;
if (callback == nullptr) {
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::callback333333xxxxxxxxxxxxxx");
}
napi_call_function(env, nullptr, callback, RESULT_ALL, args, &returnVal);//RESULT_CODE
IMSA_HILOGE("ProcessCallbackOrPromiseCBArray::callback3333333333");
if (asyncContext->callbackRef != nullptr) {
napi_delete_reference(env, asyncContext->callbackRef);
}
}
}
void JsGetInputMethodSetting::ProcessCallbackOrPromise(napi_env env, ContextBase *asyncContext)
{
IMSA_HILOGE("run in ProcessCallbackOrPromise");
napi_value jsCode = asyncContext->GetErrorCodeValue(env, asyncContext->errCode);
napi_value args[RESULT_ALL] = { jsCode, asyncContext->outData };
if (asyncContext->deferred) {
IMSA_HILOGE("ProcessCallbackOrPromise::promise");
if (asyncContext->errCode == ErrorCode::NO_ERROR) {
napi_resolve_deferred(env, asyncContext->deferred, args[RESULT_DATA]);
} else {
napi_reject_deferred(env, asyncContext->deferred, args[RESULT_ERROR]);
}
} else {
IMSA_HILOGE("ProcessCallbackOrPromise::callback");
napi_value callback = nullptr;
napi_get_reference_value(env, asyncContext->callbackRef, &callback);
napi_value returnVal = nullptr;
napi_call_function(env, nullptr, callback, RESULT_ALL, args, &returnVal);
if (asyncContext->callbackRef != nullptr) {
napi_delete_reference(env, asyncContext->callbackRef);
}
}
}
napi_value JsGetInputMethodSetting::ListInputMethod(napi_env env, napi_callback_info info)
{
IMSA_HILOGE("run in ListInputMethod");
// auto ctxt = std::make_shared<ContextBase>();
struct ListInputContext : public ContextBase {
std::vector<InputMethodProperty*> properties;
};
ListInputContext *ctxt = new (std::nothrow) ListInputContext();
if (ctxt == nullptr) {
IMSA_HILOGE("ListInputMethod::ctxt is nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
ctxt->env = env;
ctxt->callbackRef = nullptr;
ctxt->ParseContext(env, info);
napi_value promise = nullptr;//封装进函数
if (ctxt->callbackRef == nullptr) {
napi_create_promise(env, &ctxt->deferred, &promise);
} else {
napi_get_undefined(env, &promise);
}
napi_value resource = nullptr;
napi_create_string_utf8(env, "ListInputMethod", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env,
nullptr,
resource,
[ctxt](napi_env env, void *data) {
// ListInputContext *ctxt = reinterpret_cast<ListInputContext*>(data);
IMSA_HILOGE("ListInputMethod::napi_create_async_work in");
ListInputContext *ctxt = reinterpret_cast<ListInputContext*>(data);
if (ctxt == nullptr) {
IMSA_HILOGE("ListInputMethod::ctxt is nullptr");
return;
}
ctxt->properties = InputMethodController::GetInstance()->ListInputMethod();
if (!ctxt->properties.empty()) {
// ctxt->jsstatus = napi_ok;
ctxt->errCode = ErrorCode::NO_ERROR;
IMSA_HILOGE("JsInputMethodSetting::ListInputMethod get properties successful!");
} else {
ctxt->errCode = ErrorCode::ERROR_STATUS_BAD_VALUE;
IMSA_HILOGE("JsInputMethodSetting::ListInputMethod properties is empty");
}
},
[](napi_env env, napi_status status, void *data) {
IMSA_HILOGE("ListInputMethod::napi_create_async_work out");
ListInputContext *ctxt = reinterpret_cast<ListInputContext*>(data);
if (ctxt == nullptr) {
IMSA_HILOGE("ListInputMethod::ctxt is nullptr");
return;
}
napi_create_array(env, &ctxt->outData);
GetResult(env, ctxt->properties, ctxt->outData);
ProcessCallbackOrPromiseCBArray(env, ctxt);
napi_delete_async_work(env, ctxt->work);
delete ctxt;
ctxt = nullptr;
},
reinterpret_cast<void *>(ctxt),
&ctxt->work);
napi_queue_async_work(env, ctxt->work);
return promise;
}
napi_value JsGetInputMethodSetting::DisplayOptionalInputMethod(napi_env env, napi_callback_info info)
{
IMSA_HILOGE("run in DisplayOptionalInputMethod");
ContextBase *ctxt = new (std::nothrow) ContextBase();
if (ctxt == nullptr) {
IMSA_HILOGE("DisplayOptionalInputMethod::ctxt is nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
ctxt->env = env;
ctxt->callbackRef = nullptr;
ctxt->ParseContext(env, info);
napi_value promise = nullptr;
if (ctxt->callbackRef == nullptr) {
napi_create_promise(env, &ctxt->deferred, &promise);
} else {
napi_get_undefined(env, &promise);
}
napi_value resource = nullptr;
napi_create_string_utf8(env, "DisplayOptionalInputMethod", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env,
nullptr,
resource,
[](napi_env env, void *data) {
IMSA_HILOGE("DisplayOptionalInputMethod::napi_create_async_work in");
ContextBase *ctxt = reinterpret_cast<ContextBase*>(data);
if (ctxt == nullptr) {
IMSA_HILOGE("DisplayOptionalInputMethod::ctxt is nullptr");
return;
}
ctxt->errCode = InputMethodController::GetInstance()->DisplayOptionalInputMethod();
},
[](napi_env env, napi_status status, void *data) {
ContextBase *ctxt = reinterpret_cast<ContextBase*>(data);
if (ctxt == nullptr) {
IMSA_HILOGE("DisplayOptionalInputMethod::ctxt is nullptr");
return;
}
napi_get_undefined(env, &ctxt->outData);//PARAMONE = 1
ProcessCallbackOrPromise(env, ctxt);
napi_delete_async_work(env, ctxt->work);
delete ctxt;
ctxt = nullptr;
},
reinterpret_cast<void *>(ctxt),
&ctxt->work);
napi_queue_async_work(env, ctxt->work);
return promise;
}
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H
#define INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H
#include "napi/native_api.h"
#include "global.h"
#include "input_method_property.h"
#include "js_context.h"
namespace OHOS {
namespace MiscServices {
constexpr int RESULT_ERROR = 0;
constexpr int RESULT_DATA = 1;
constexpr int PARAZERO = 1;
constexpr int PARAMONE = 1;
constexpr int RESULT_ALL = 2;
constexpr int RESULT_COUNT = 2;
const std::string IMS_CLASS_NAME = "InputMethodSetting";
static thread_local napi_ref IMSRef_ = nullptr;
class JsGetInputMethodSetting {
public:
JsGetInputMethodSetting() = default;
~JsGetInputMethodSetting() = default;
static napi_value Init(napi_env env, napi_value info);
static napi_value GetInputMethodSetting(napi_env env, napi_callback_info info);
static napi_value ListInputMethod(napi_env env, napi_callback_info info);
static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info);
private:
static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
static void GetResult(napi_env env, std::vector<InputMethodProperty*> &properties, napi_value &result);
static void ProcessCallbackOrPromiseCBArray(napi_env env,ContextBase *ctxt);
static void ProcessCallbackOrPromise(napi_env env, ContextBase *ctxt);
};
}
}
#endif // INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H

View File

@ -0,0 +1,198 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "input_method_controller.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "event_handler.h"
#include "event_runner.h"
#include "string_ex.h"
#include "js_input_method.h"
namespace OHOS {
namespace MiscServices {
napi_value JsInputMethod::Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptor[] = {
DECLARE_NAPI_FUNCTION("switchInputMethod", SwitchInputMethod),
};
NAPI_CALL(
env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
napi_property_descriptor properties[] = {
};
napi_value cons = nullptr;
NAPI_CALL(env, napi_define_class(env, IMS_CLASS_NAME.c_str(), IMS_CLASS_NAME.size(),
JsConstructor, nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons));
NAPI_CALL(env, napi_create_reference(env, cons, 1, &IMSRef_));
NAPI_CALL(env, napi_set_named_property(env, exports, IMS_CLASS_NAME.c_str(), cons));
return exports;
};
napi_value JsInputMethod::JsConstructor(napi_env env, napi_callback_info cbinfo)
{
IMSA_HILOGE("run in JsConstructor");
napi_value thisVar = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr));
JsInputMethod *IMSobject = new (std::nothrow) JsInputMethod();
if (IMSobject == nullptr) {
IMSA_HILOGE("IMSobject == nullptr");
napi_value result = nullptr;
napi_get_null(env, &result);
return result;
}
napi_wrap(env, thisVar, IMSobject, [](napi_env env, void *data, void *hint) {
auto* objInfo = reinterpret_cast<JsInputMethod*>(data);
if (objInfo != nullptr) {
IMSA_HILOGE("objInfo != nullptr");
delete objInfo;
}
}, nullptr, nullptr);
return thisVar;
}
void JsInputMethod::CallbackOrPromiseSwitchInput(
napi_env env, const SwitchInput *switchInput, napi_value err, napi_value data)
{
IMSA_HILOGE("run in CallbackOrPromiseSwitchInput");
napi_value args[RESULT_COUNT] = {err, data};
if (switchInput->deferred) {
if (switchInput->status == napi_ok) {
IMSA_HILOGE("CallbackOrPromiseSwitchInput::promise");
napi_resolve_deferred(env, switchInput->deferred, args[RESULT_DATA]);
} else {
napi_reject_deferred(env, switchInput->deferred, args[RESULT_ERROR]);
}
} else {
IMSA_HILOGE("CallbackOrPromiseSwitchInput::callback");
napi_value callback = nullptr;
napi_get_reference_value(env, switchInput->callbackRef, &callback);
if (switchInput->callbackRef == nullptr) {
IMSA_HILOGE("CallbackOrPromiseSwitchInput::callback2222222222xxxxxxxxx");
}
IMSA_HILOGE("CallbackOrPromiseSwitchInput::callback2222222222");
napi_value returnVal = nullptr;
if (callback == nullptr) {
IMSA_HILOGE("CallbackOrPromiseSwitchInput::callback333333xxxxxxxxxxxxxx");
}
napi_call_function(env, nullptr, callback, RESULT_COUNT, &args[0], &returnVal);
IMSA_HILOGE("CallbackOrPromiseSwitchInput::callback3333333333");
if (switchInput->callbackRef != nullptr) {
napi_delete_reference(env, switchInput->callbackRef);
}
}
}
napi_value JsInputMethod::GetErrorCodeValue(napi_env env, int errCode)
{
// ACCOUNT_LOGD("enter");
napi_value jsObject = nullptr;
napi_value jsValue = nullptr;
NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
NAPI_CALL(env, napi_create_object(env, &jsObject));
NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
return jsObject;
}
napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info info)
{
struct SwitchInputMethodContext : public ContextBase{
bool sSwitchInput = false;
std::string packageName;
std::string methodId;
}
SwitchInputMethodContext *switchInpput = new (std::nothrow) SwitchInputMethodContext();
if (switchInpput == nullptr) {
napi_value promise = nullptr;
napi_get_null(env, &promise);
return promise;
}
auto input = [env, ctxt](size_t argc, napi_value* argv) {
napi_valuetype valueType = napi_undefined;
napi_typeof(env, argv[0], &valueType);
if (valueType == napi_object) {
napi_value result = nullptr;
napi_get_named_property(env, argv[0], "packageName", &result);
ctxt->packageName = GetStringProperty(env, result);
IMSA_HILOGE("packageName:%{public}s", packageName.c_str());
result = nullptr;
napi_get_named_property(env, argv[0], "methodId", &result);
ctxt->methodId = GetStringProperty(env, result);
IMSA_HILOGE("methodId:%{public}s", methodId.c_str());
}
};
switchInpput->env = env;
switchInpput->callbackRef = nullptr;
switchInpput->ParseContext(env, info, input);
napi_value promise = nullptr;
if (switchInpput->callbackRef == nullptr) {
napi_create_promise(env, &switchInpput->deferred, &promise);
} else {
napi_get_undefined(env, &promise);
}
napi_value resource = nullptr;
napi_create_string_utf8(env, "SwitchInputMethod", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env,
nullptr,
resource,
[](napi_env env, void *data) {
IMSA_HILOGE("SwitchInputMethod::napi_create_async_work in");
SwitchInput *switchInpput = reinterpret_cast<SwitchInput *>(data);
InputMethodProperty *property = new (std::nothrow) InputMethodProperty();
if (property == nullptr){
IMSA_HILOGE("SwitchInputMethod:: property == nullptr");
return;
}
property->mPackageName = Str8ToStr16(switchInpput->packageName);
property->mImeId = Str8ToStr16(switchInpput->methodId);
switchInpput->errCode = InputMethodController::GetInstance()->SwitchInputMethod(property);
IMSA_HILOGI("ListInputMethod::************************************");
if (switchInpput->errCode == 0) {
IMSA_HILOGE("JsInputMethod::switchInpput successful!");
switchInpput->sSwitchInput = true;
}
switchInpput->status = (switchInpput->errCode == 0) ? napi_ok : napi_generic_failure;
//delete property;
//property = nullptr;
},
[](napi_env env, napi_status status, void *data) {
IMSA_HILOGE("JsInputMethod::SwitchInputMethod napi_status status");
SwitchInput *switchInpput = reinterpret_cast<SwitchInput *>(data);
switchInpput->errCode = 0;
napi_value getResult[RESULT_ALL] = {0};
IMSA_HILOGE("JsInputMethod::SwitchInputMethod GetErrorCodeValue start ");
getResult[PARAMZERO] = GetErrorCodeValue(env, switchInpput->errCode);
IMSA_HILOGE("JsInputMethod::SwitchInputMethod GetErrorCodeValue end ");
napi_get_boolean(env, switchInpput->sSwitchInput, &getResult[PARAMONE]);
CallbackOrPromiseSwitchInput(env, switchInpput, getResult[PARAMZERO], getResult[PARAMONE]);
napi_delete_async_work(env, switchInpput->work);
delete switchInpput;
switchInpput = nullptr;
},
reinterpret_cast<void *>(switchInpput),
&switchInpput->work);
napi_queue_async_work(env, switchInpput->work);
return promise;
}
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERFACE_KITS_JS_INPUT_METHOD_H
#define INTERFACE_KITS_JS_INPUT_METHOD_H
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "global.h"
#include "js_context.h"
namespace OHOS {
namespace MiscServices {
constexpr int RESULT_ERROR = 0;
constexpr int RESULT_DATA = 1;
constexpr int PARAMONE = 1;
constexpr int PARAMZERO = 0;
constexpr int RESULT_ALL = 2;
constexpr int RESULT_COUNT = 2;
const std::string IMS_CLASS_NAME = "InputMethod";
static thread_local napi_ref IMSRef_ = nullptr;
class JsInputMethod {
public:
JsInputMethod() = default;
~JsInputMethod() = default;
static napi_value Init(napi_env env, napi_value exports);
static napi_value SwitchInputMethod(napi_env env, napi_callback_info info);
private:
static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
static napi_value GetErrorCodeValue(napi_env env, int errCode);
static void CallbackOrPromiseSwitchInput(napi_env env, const SwitchInput *switchInput, napi_value err, napi_value data);
};
}
}
#endif // INTERFACE_KITS_JS_INPUT_METHOD_H