diff --git a/cocos/2d/platform/ohos/napi/helper/NapiHelper.h b/cocos/2d/platform/ohos/napi/helper/NapiHelper.h index 19a04fbcc04a..fac4f86c0926 100644 --- a/cocos/2d/platform/ohos/napi/helper/NapiHelper.h +++ b/cocos/2d/platform/ohos/napi/helper/NapiHelper.h @@ -114,7 +114,11 @@ class JSFunction { napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...}; napi_value return_val; status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val); - + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } ReturnType value; if (!NapiValueConverter::ToCppValue(env, return_val, value)) { // Handle error here @@ -136,6 +140,11 @@ class JSFunction { napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...}; napi_value return_val; status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } static void callFunctionWithParams(WorkParam *param) { @@ -167,6 +176,11 @@ class JSFunction { } if (status != napi_ok) { LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } napi_value thenFunc = nullptr; @@ -187,6 +201,11 @@ class JSFunction { status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret); if (status != napi_ok) { LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } } // Callback Function Type diff --git a/cocos/2d/platform/ohos/napi/plugin_manager.cpp b/cocos/2d/platform/ohos/napi/plugin_manager.cpp index 09311195daad..599387b3bd3b 100644 --- a/cocos/2d/platform/ohos/napi/plugin_manager.cpp +++ b/cocos/2d/platform/ohos/napi/plugin_manager.cpp @@ -58,7 +58,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) { int64_t value; NAPI_CALL(env, napi_get_value_int64(env, args[0], &value)); - + napi_handle_scope scope = nullptr; + NAPI_CALL(env, napi_open_handle_scope(env, &scope)); + if(scope == nullptr){ + return nullptr; + } NAPI_CALL(env, napi_create_object(env, &exports)); switch (value) { @@ -165,6 +169,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) { default: OHOS_LOGE("unknown type"); } + NAPI_CALL(env, napi_close_handle_scope(env, scope)); return exports; } diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp index 0ca94445bbde..bcca12bae71e 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -31,7 +32,7 @@ float mousePositionY = -1; bool isMouseLeftActive = false; double scrollDistance = 0; -std::unordered_map ohKeyMap = { +const std::unordered_map ohKeyMap = { {KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE}, {KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE}, {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, @@ -61,7 +62,22 @@ std::unordered_map ohKeyMap = { {KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW}, {KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT}, {KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT}, - {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE} + {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}, + {KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK}, + {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, + {KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT}, + {KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS}, + {KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU}, + {KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE}, + {KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME}, + {KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END}, + {KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP}, + {KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN}, + {KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS}, + {KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS}, + {KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY}, + {KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE}, + {KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER} }; cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode) @@ -82,7 +98,8 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC // A - Z return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A)); } - return cocos2d::EventKeyboard::KeyCode(ohKeyCode); + OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode); + return cocos2d::EventKeyboard::KeyCode::KEY_NONE; } void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) diff --git a/tests/cpp-tests/proj.ohos/build-profile.json5 b/tests/cpp-tests/proj.ohos/build-profile.json5 index ac94ade7f0a0..6d001e06e417 100644 --- a/tests/cpp-tests/proj.ohos/build-profile.json5 +++ b/tests/cpp-tests/proj.ohos/build-profile.json5 @@ -4,8 +4,7 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", - "targetSdkVersion": "5.0.0(12)", + "compatibleSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS" } ], diff --git a/tests/cpp-tests/proj.ohos/entry/build-profile.json5 b/tests/cpp-tests/proj.ohos/entry/build-profile.json5 index 918eed219fdd..2ddf355dafdc 100644 --- a/tests/cpp-tests/proj.ohos/entry/build-profile.json5 +++ b/tests/cpp-tests/proj.ohos/entry/build-profile.json5 @@ -12,7 +12,7 @@ }, "sourceOption": { "workers": [ - './src/main/ets/workers/CocosWorker.ts' + './src/main/ets/workers/CocosWorker.ets' ] }, }, diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..d30c29f2d907 --- /dev/null +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets @@ -0,0 +1,118 @@ +import window from '@ohos.window'; +import UIAbility from '@ohos.app.ability.UIAbility'; +import nativeRender from "libnativerender.so"; +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" +import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" +import { BusinessError } from '@kit.BasicServicesKit'; +import { WorkerManager } from '../workers/WorkerManager'; +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + +const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); +const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS); +let cocosWorker = WorkerManager.getInstance().getWorker(); +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); +export default class MainAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + nativeAppLifecycle.onCreate(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context); + console.info('[LIFECYCLE-App] onCreate') + } + + onDestroy() { + nativeAppLifecycle.onDestroy(); + console.info('[LIFECYCLE-App] onDestroy') + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + windowStage.loadContent('pages/Index', (err:BusinessError, data) => { + if (err.code) { + return; + } + rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); + rawFileUtils.writablePathInit(this.context.filesDir); + }); + + windowStage.getMainWindow().then((windowIns: window.Window) => { + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns); + // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. + let systemBarPromise = windowIns.setWindowSystemBarEnable([]); + // Whether the window layout is displayed in full screen mode + let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); + // Sets whether the screen is always on. + let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); + Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { + console.info('Succeeded in setting the window'); + }).catch((err: BusinessError) => { + console.error('Failed to set the window, cause ', err.code, err.message); + }); + + try { + DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect); + windowIns.on('avoidAreaChange', (data) => { + console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' + + JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area)); + + if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) { + return; + } + + let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW); + DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect); + }); + } catch (exception) { + console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { + let stageEventType: window.WindowStageEventType = data; + switch (stageEventType) { + case window.WindowStageEventType.RESUMED: + console.info('[LIFECYCLE-App] onShow_RESUMED') + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + break; + case window.WindowStageEventType.PAUSED: + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + console.info('[LIFECYCLE-App] onHide_PAUSED') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + break; + default: + break; + } + }); + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + } + + onForeground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + // Ability has brought to foreground + console.info('[LIFECYCLE-App] onShow') + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + } + + onBackground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + // Ability has back to background + console.info('[LIFECYCLE-App] onHide') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + } +}; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index a89ce067f484..000000000000 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts +++ /dev/null @@ -1,80 +0,0 @@ -import window from '@ohos.window'; -import UIAbility from '@ohos.app.ability.UIAbility'; -import web_webview from '@ohos.web.webview'; -import nativeRender from "libnativerender.so"; -import { ContextType } from "@ohos/libSysCapabilities" -import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" - -const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); -const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS); - -export default class MainAbility extends UIAbility { - onCreate(want, launchParam) { - nativeAppLifecycle.onCreate(); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context); - // Initializes the webView kernel of the system. This parameter is optional if it is not used. - web_webview.WebviewController.initializeWebEngine(); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_WANT, this.context); - console.info('[LIFECYCLE-App] onCreate') - } - - onDestroy() { - nativeAppLifecycle.onDestroy(); - console.info('[LIFECYCLE-App] onDestroy') - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - return; - } - rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); - rawFileUtils.writablePathInit(this.context.filesDir); - }); - - windowStage.getMainWindow().then((windowIns: window.Window) => { - // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. - let systemBarPromise = windowIns.setWindowSystemBarEnable([]); - // Whether the window layout is displayed in full screen mode - let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); - // Sets whether the screen is always on. - let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); - Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { - console.info('Succeeded in setting the window'); - }).catch((err) => { - console.error('Failed to set the window, cause ' + JSON.stringify(err)); - }); - }) - - windowStage.on("windowStageEvent", (data) => { - let stageEventType: window.WindowStageEventType = data; - switch (stageEventType) { - case window.WindowStageEventType.RESUMED: - nativeAppLifecycle.onShow(); - break; - case window.WindowStageEventType.PAUSED: - nativeAppLifecycle.onHide(); - break; - default: - break; - } - }); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - } - - onForeground() { - // Ability has brought to foreground - console.info('[LIFECYCLE-App] onShow') - nativeAppLifecycle.onShow(); - } - - onBackground() { - // Ability has back to background - console.info('[LIFECYCLE-App] onDestroy') - nativeAppLifecycle.onHide(); - } -}; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets index 138b7431c3b6..6f67afe3b443 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets @@ -12,7 +12,7 @@ export struct CocosVideoPlayer { Video({ src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile, controller: this.videoPlayerInfo.controller - }).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y }) + }) .width(this.videoPlayerInfo.w) .height(this.videoPlayerInfo.h) .visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None) diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index 7eb4e1c34369..dbb983025e14 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -17,11 +17,11 @@ import promptAction from '@ohos.promptAction'; import process from '@ohos.process'; const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE); +let cocosWorker = WorkerManager.getInstance().getWorker(); + @Entry @Component struct Index { - - cocosWorker = WorkerManager.getInstance().getWorker(); xcomponentController: XComponentController = new XComponentController(); processMgr = new process.ProcessManager(); @@ -56,7 +56,7 @@ struct Index { console.log('[LIFECYCLE-Page] onPageShow'); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_ARRAY, this.editBoxArray); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_INDEX_MAP, this.editBoxIndexMap); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, this.cocosWorker); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, cocosWorker); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY, this.webViewArray); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP, this.webViewIndexMap); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_ARRAY, this.videoPlayerInfoArray); @@ -80,12 +80,10 @@ struct Index { buttons: [ { text: '鍙栨秷', - // Color can be customized color: '#000000' }, { text: '纭', - // Color can be customized color: '#000000' } ], @@ -105,12 +103,13 @@ struct Index { // If disable system exit needed, remove comment "return true" return true; } + onMouseWheel(eventType: string, scrollY: number) { - this.cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); + cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); } build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Stack() { XComponent({ id: 'xcomponentId', type: 'surface', @@ -135,9 +134,9 @@ struct Index { ) .onLoad((context) => { console.log('[cocos] XComponent.onLoad Callback'); - this.cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)}); - this.cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" }); - this.cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage; + cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)}); + cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" }); + cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage; }) .onDestroy(() => { }) diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets similarity index 72% rename from tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts rename to tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets index 138b986dc3da..d8047e80ea9d 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets @@ -1,13 +1,13 @@ -import worker, { ThreadWorkerGlobalScope } from '@ohos.worker'; +import worker, { ThreadWorkerGlobalScope, MessageEvents } from '@ohos.worker'; import nativeRender from "libnativerender.so"; -import { ContextType } from "@ohos/libSysCapabilities" +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" import { VideoPlayer } from "@ohos/libSysCapabilities" +import { ApplicationManager } from "@ohos/libSysCapabilities" import { CocosEditBox } from "@ohos/libSysCapabilities" import { Dialog } from "@ohos/libSysCapabilities" import { WebView } from "@ohos/libSysCapabilities" import { JumpManager } from "@ohos/libSysCapabilities" import { NapiHelper } from "@ohos/libSysCapabilities" -import { ApplicationManager } from "@ohos/libSysCapabilities" import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const appLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); @@ -24,55 +24,57 @@ NapiHelper.registerFunctions(napiContext.registerFunction) const workerPort: ThreadWorkerGlobalScope = worker.workerPort; -workerPort.onmessage = function(e) : void { - let data = e.data; - switch(data.type) { +workerPort.onmessage = (e: MessageEvents) => { + switch(e.data.type) { case "onXCLoad": console.log("[cocos] onXCLoad Callback"); Dialog.init(workerPort); CocosEditBox.init(workerPort); JumpManager.init(workerPort); WebView.init(workerPort); - VideoPlayer.init(workerPort); ApplicationManager.init(workerPort); + VideoPlayer.init(workerPort); napiContext.initAsyncInfo(); break; case "abilityContextInit": - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, data.data); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, e.data.data); break; case "editBoxOnFocus": - inputNapi.editBoxOnFocusCB(data.viewTag); + inputNapi.editBoxOnFocusCB(e.data.viewTag); break; case "editBoxOnChange": - inputNapi.editBoxOnChangeCB(data.viewTag, data.value); + inputNapi.editBoxOnChangeCB(e.data.viewTag, e.data.value); break; case "editBoxOnEnter": - inputNapi.editBoxOnEnterCB(data.viewTag, data.text); + inputNapi.editBoxOnEnterCB(e.data.viewTag, e.data.text); break; case "textFieldTTFOnChange": - inputNapi.textFieldTTFOnChangeCB(data.data); + inputNapi.textFieldTTFOnChangeCB(e.data.data); break; case "onMouseWheel": - mouseNapi.mouseWheelCB(data.eventType, data.scrollY); + mouseNapi.mouseWheelCB(e.data.eventType, e.data.scrollY); break; case "onPageBegin": - webViewNapi.shouldStartLoading(data.viewTag, data.url); + webViewNapi.shouldStartLoading(e.data.viewTag, e.data.url); break; case "onPageEnd": - webViewNapi.finishLoading(data.viewTag, data.url); + webViewNapi.finishLoading(e.data.viewTag, e.data.url); break; case "onJsCallBack": webViewNapi.jsCallback(); break; case "onErrorReceive": - webViewNapi.failLoading(data.viewTag, data.url); + webViewNapi.failLoading(e.data.viewTag, e.data.url); break; case "onVideoCallBack": - videoPlayNapi.onVideoCallBack(data.viewTag, data.event); + videoPlayNapi.onVideoCallBack(e.data.viewTag, e.data.event); break; case "exit": appLifecycle.onBackPress(); break; + case "updateSafeArea": + DeviceUtils.updateWorkerSafeArea(e.data.safeArea); + break; default: console.error("cocos worker: message type unknown") } diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 97% rename from tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets index e2ec70b1873e..0447223b1e6a 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets @@ -5,7 +5,7 @@ export class WorkerManager { private cocosWorker: worker.ThreadWorker; private constructor() { - this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ts", { + this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ets", { type: "classic", name: "CocosWorker" }); diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 b/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 index 3945c43b8fda..dd4b6c13ea72 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 +++ b/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 @@ -15,7 +15,7 @@ "abilities": [ { "name": "MainAbility", - "srcEntry": "./ets/MainAbility/MainAbility.ts", + "srcEntry": "./ets/MainAbility/MainAbility.ets", "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", @@ -34,23 +34,10 @@ } ], // https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/module-configuration-file.md/ - //将窗口模式设置为只支持全屏 "supportWindowMode": ["fullscreen"], -// 游戏主窗口固定窗口化 -// "supportWindowMode": ["floating"], -// 如果有全屏需求,可以同时加上fullscreen和floating,可以做到窗口化按这只宽高显示,全屏按全屏方案显示 -// "supportWindowMode": ["fullscreen","floating"], - //最大宽高比 -// "maxWindowRatio": 3.5, - //最小宽高比 -// "minWindowRatio": 0.5, - //最大宽度为1080 "maxWindowWidth": 1080, - //最小宽度为1080 "minWindowWidth": 1080, - //最大高度为720 "maxWindowHeight": 720, - //最小高度为720 "minWindowHeight": 720 } ], diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/index.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 b/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 index 8c935e9b2a00..d00806d42de9 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 @@ -4,7 +4,7 @@ "author": "", "name": "libsyscapabilities", "description": "Please describe the basic information.", - "main": "index.ts", + "main": "index.ets", "version": "1.0.0", "dynamicDependencies": {}, "dependencies": {} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b2664e..00b7c24920b4 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts @@ -22,4 +22,7 @@ export class GlobalContextConstants { static readonly COCOS2DX_ABILITY_CONTEXT = "Cocos2dx.abilityContext"; static readonly COCOS2DX_ABILITY_WANT = "Cocos2dx.abilityWant"; static readonly COCOS2DX_WEB_RESULT= "Cocos2dx.webResult"; -} \ No newline at end of file + static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow"; + static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag"; + static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag"; +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets similarity index 98% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets index 37b698c6dce5..5355a89f86fa 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets @@ -40,7 +40,7 @@ function showDialog(dialogTitle: string, dialogMessage: string) { color: '#000000' }, ], - success: function(data) { + success: (data) => { log.debug("handling callback, data:%{public}s", data); } }); diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 95% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 112bd864dda5..968ed4cca754 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets @@ -3,7 +3,7 @@ import { DialogMsgEntity } from '../../entity/WorkerMsgEntity'; export class Dialog { static MODULE_NAME : string = 'Dialog'; - static workerPort; + static workerPort: ThreadWorkerGlobalScope; static init(workerPort: ThreadWorkerGlobalScope) : void { Dialog.workerPort = workerPort; diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets similarity index 96% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets index f07362f91007..ed2049cb2c93 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets @@ -1,11 +1,12 @@ +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; export class CocosEditBox { static MODULE_NAME : string = 'EditBox'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { CocosEditBox.workerPort = workerPort; } diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets similarity index 95% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index 50b7a7e69d00..f4a11e84d250 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -1,11 +1,12 @@ import { VideoPlayMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class VideoPlayer { static MODULE_NAME: string = 'VideoPlay'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { VideoPlayer.workerPort = workerPort; } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets similarity index 96% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets index 872ec4ce70fe..49ffb1dad4e5 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets @@ -1,11 +1,12 @@ import { ViewRect, WebViewMsgEntity } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class WebView { static MODULE_NAME: string = 'WebView'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { WebView.workerPort = workerPort; } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets index 8886802946bc..b03c4004f522 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets @@ -7,7 +7,7 @@ import { BusinessError } from '@ohos.base'; let log: Logger = new Logger(0x0001, "WebViewMsg"); @Observed export class WebViewInfo { - public uniqueId: number = 0; + public uniqueId : number = 0; // position public x: number = 0; public y: number = 0; @@ -17,6 +17,7 @@ export class WebViewInfo { // url public url: string = ''; public isUrl: boolean = true; + public viewTag: number = 0 public zoomAccess: boolean = true public visible: boolean = true @@ -36,9 +37,9 @@ export class WebViewInfo { } -function waitUtilControllerAttached(): Promise { +function waitUtilControllerAttached() : Promise { return new Promise((resolve, reject) => { - resolve(1); + resolve(1); }) } @@ -149,7 +150,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } case "setWebViewRect": { index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo :WebViewInfo= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.x = px2vp(eventData.viewRect.x); tmpWebInfo.y = px2vp(eventData.viewRect.y); tmpWebInfo.w = px2vp(eventData.viewRect.w); @@ -162,7 +163,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .visible == eventData.visible) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo:WebViewInfo= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.visible = eventData.visible; break; } @@ -172,7 +173,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .opacity == eventData.opacity) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo :WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.opacity = eventData.opacity; break; } @@ -190,13 +191,13 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index] .controller - .runJavaScript(eventData.js, (error: BusinessError, result: string) => { + .runJavaScript(eventData.js, (error:BusinessError, result:string) => { if (error) { log.warn("webView run JavaScript error:%{public}s", JSON.stringify(error)); return; } if (result) { - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_RESULT, result) + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_RESULT,result) log.debug("webView run JavaScript result is %{public}s:", result); } }) @@ -208,9 +209,9 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .zoomAccess == eventData.scalesPageToFit) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo:WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.zoomAccess == eventData.scalesPageToFit } break; } -} \ No newline at end of file +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets @@ -0,0 +1,23 @@ +interface ResultObjectInterface { + "errCode": number; + "errMsg": string; + "data": string, +} + +export class Result { + public static success(data: string){ + return ({ + "errCode": 0, + "errMsg": "", + "data": data, + }as ResultObjectInterface); + } + + public static error(errCode:number, errMsg:string){ + return ({ + "errCode": errCode, + "errMsg": errMsg, + "data": "", + } as ResultObjectInterface); + } +}; \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts +++ /dev/null @@ -1,16 +0,0 @@ -export class Result { - public static success(data){ - return { - "errCode": 0, - "errMsg": "", - "data": data, - }; - } - - public static error(errCode, errMsg) { - return { - "errCode": errCode, - "errMsg": errMsg, - }; - } -}; \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets similarity index 95% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index 12d43feedade..cdf556c9f7e1 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -29,7 +29,6 @@ export class NapiHelper { registerFunc('JumpManager.openUrl', JumpManager.openUrl); } - private static registerDeviceUtils(registerFunc : Function) { registerFunc('DeviceUtils.getDpi', DeviceUtils.getDpi); registerFunc('DeviceUtils.getSystemLanguage', DeviceUtils.getSystemLanguage); @@ -42,6 +41,10 @@ export class NapiHelper { registerFunc('DeviceUtils.getOrientation', DeviceUtils.getOrientation); registerFunc('DeviceUtils.getCutoutHeight', DeviceUtils.getCutoutHeight); registerFunc('DeviceUtils.getCutoutWidth', DeviceUtils.getCutoutWidth); + registerFunc('DeviceUtils.getSafeAreaLeft', DeviceUtils.getSafeAreaLeft); + registerFunc('DeviceUtils.getSafeAreaTop', DeviceUtils.getSafeAreaTop); + registerFunc('DeviceUtils.getSafeAreaWidth', DeviceUtils.getSafeAreaWidth); + registerFunc('DeviceUtils.getSafeAreaHeight', DeviceUtils.getSafeAreaHeight); } private static registerEditBox(registerFunc : Function) { @@ -108,5 +111,4 @@ export class NapiHelper { registerFunc('Preferences.flush', Preferences.flush); registerFunc('Preferences.clear', Preferences.clear); } - -} \ No newline at end of file +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets similarity index 72% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets index e95e89b29d10..4b64aff5551d 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets @@ -5,13 +5,12 @@ import common from '@ohos.app.ability.common'; import { GlobalContext, GlobalContextConstants } from '../common/GlobalContext'; let log: Logger = new Logger(0x0001, "Preferences"); -let preferences: data_preferences.Preferences | null = null; +let preferences: data_preferences.Preferences; const PREFS_NAME: string = "Cocos2dxPreferences"; export default class Preferences { - // 閫氳繃 preferencesName 鑾峰彇Preferences瀹炰緥 - static getPreferences(): data_preferences.Preferences { + static getPreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { preferences = data_preferences.getPreferencesSync(context, {name: PREFS_NAME}); @@ -21,13 +20,8 @@ export default class Preferences { let message = (err as BusinessError).message; log.error("Failed to get preferences. code =" + code + ", message =" + message); } - return preferences; } - /* - 閫氳繃 preferencesName 浠庣紦瀛樹腑绉诲嚭鎸囧畾鐨凱references瀹炰緥锛岃嫢Preferences瀹炰緥鏈夊搴旂殑鎸佷箙鍖栨枃浠讹紝鍒欏悓鏃跺垹闄ゅ叾鎸佷箙鍖栨枃浠躲備娇鐢≒romise寮傛鍥炶皟銆 - 璋冪敤璇ユ帴鍙e悗锛屼笉寤鸿鍐嶄娇鐢ㄦ棫鐨凱references瀹炰緥杩涜鏁版嵁鎿嶄綔锛屽惁鍒欎細鍑虹幇鏁版嵁涓鑷存ч棶棰橈紝搴斿皢Preferences瀹炰緥缃负null锛岀郴缁熷皢浼氱粺涓鍥炴敹銆 - */ static deletePreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -41,12 +35,6 @@ export default class Preferences { } } - /* - 閫氳繃 preferencesName 浠庣紦瀛樹腑绉诲嚭鎸囧畾鐨凱references瀹炰緥锛屼娇鐢≒romise寮傛鍥炶皟銆 - 搴旂敤棣栨璋冪敤getPreferences鎺ュ彛鑾峰彇鏌愪釜Preferences瀹炰緥鍚庯紝璇ュ疄渚嬩細琚細琚紦瀛樿捣鏉ワ紝鍚庣画鍐嶆getPreferences鏃朵笉浼氬啀娆′粠鎸佷箙鍖栨枃浠朵腑璇诲彇锛 - 鐩存帴浠庣紦瀛樹腑鑾峰彇Preferences瀹炰緥銆傝皟鐢ㄦ鎺ュ彛绉诲嚭缂撳瓨涓殑瀹炰緥涔嬪悗锛屽啀娆etPreferences灏嗕細閲嶆柊璇诲彇鎸佷箙鍖栨枃浠讹紝鐢熸垚鏂扮殑Preferences瀹炰緥銆 - 璋冪敤璇ユ帴鍙e悗锛屼笉寤鸿鍐嶄娇鐢ㄦ棫鐨凱references瀹炰緥杩涜鏁版嵁鎿嶄綔锛屽惁鍒欎細鍑虹幇鏁版嵁涓鑷存ч棶棰橈紝搴斿皢Preferences瀹炰緥缃负null锛岀郴缁熷皢浼氱粺涓鍥炴敹銆 - */ static removePreferencesFromCache(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -59,7 +47,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓幏鍙栭敭瀵瑰簲鐨勫硷紝濡傛灉鍊间负null鎴栬呴潪榛樿鍊肩被鍨嬶紝杩斿洖榛樿鏁版嵁defValue static get(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType { if (preferences === null) { Preferences.getPreferences(); @@ -76,7 +63,6 @@ export default class Preferences { } } - // 灏嗘暟鎹啓鍏ョ紦瀛樼殑Preferences瀹炰緥涓紝鍙氳繃flush灏哖references瀹炰緥鎸佷箙鍖 static put(key: string, value: data_preferences.ValueType): void { if (preferences === null) { Preferences.getPreferences(); @@ -92,7 +78,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓幏鍙栨墍鏈夐敭鍊兼暟鎹 static getAll(): string | undefined { if (preferences === null) { Preferences.getPreferences(); @@ -111,7 +96,6 @@ export default class Preferences { } } - // 妫鏌ョ紦瀛樼殑Preferences瀹炰緥涓槸鍚﹀寘鍚悕涓虹粰瀹欿ey鐨勫瓨鍌ㄩ敭鍊煎 static has(key: string): boolean { if (preferences === null) { Preferences.getPreferences(); @@ -133,7 +117,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓垹闄ゅ悕涓虹粰瀹欿ey鐨勫瓨鍌ㄩ敭鍊煎锛屽彲閫氳繃flush灏哖references瀹炰緥鎸佷箙鍖 static delete(key: string): void { if (preferences === null) { Preferences.getPreferences(); @@ -149,7 +132,6 @@ export default class Preferences { } } - // 灏嗙紦瀛樼殑Preferences瀹炰緥涓殑鏁版嵁寮傛瀛樺偍鍒扮敤鎴烽閫夐」鐨勬寔涔呭寲鏂囦欢涓紝浣跨敤Promise寮傛鍥炶皟銆 static flush(): void { if (preferences === null) { Preferences.getPreferences(); @@ -159,7 +141,6 @@ export default class Preferences { }); } - // 娓呴櫎缂撳瓨鐨凱references瀹炰緥涓殑鎵鏈夋暟鎹紝鍙氳繃flush灏哖references瀹炰緥鎸佷箙鍖栵紝浣跨敤Promise寮傛鍥炶皟銆 static clear(): void { if (preferences === null) { Preferences.getPreferences(); @@ -175,7 +156,6 @@ export default class Preferences { } - // 璁㈤槄鏁版嵁鍙樻洿锛岃闃呯殑Key鐨勫煎彂鐢熷彉鏇村悗锛屽湪鎵цflush鏂规硶鍚庯紝瑙﹀彂callback鍥炶皟銆 static onChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -192,7 +172,6 @@ export default class Preferences { } } - // 鍙栨秷璁㈤槄鏁版嵁鍙樻洿銆 static offChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -210,8 +189,6 @@ export default class Preferences { } } -// 鐢变簬ArkTS涓棤Object.keys锛屼笖鏃犳硶浣跨敤for..in... -// 鑻ユ姤ArkTS闂锛岃灏嗘鏂规硶鍗曠嫭鎶界鑷充竴涓猼s鏂囦欢涓苟鏆撮湶锛屽湪闇瑕佺敤鍒扮殑ets鏂囦欢涓紩鍏ヤ娇鐢 function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets similarity index 76% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets index ea373af90811..2c20f240b2ce 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets @@ -3,6 +3,8 @@ import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContex import {Result} from "../../entity/Result" import type { JumpMsgEntity } from '../../entity/WorkerMsgEntity'; import Logger from '../../utils/Logger' +import { BusinessError } from '@kit.BasicServicesKit'; +import { Want } from '@kit.AbilityKit' let log: Logger = new Logger(0x0001, "JumpManagerMsg"); @@ -18,14 +20,14 @@ export function handleJumpManagerMsg(eventData: JumpMsgEntity) : void { function openUrl(url: string): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); - let wantInfo = { + let wantInfo: Want = { 'action': 'ohos.want.action.viewData', 'entities': ['entity.system.browsable'], 'uri': url } context.startAbility(wantInfo).then(() => { - log.info('%{public}s', JSON.stringify(Result.success({}))); - }).catch((err) => { - log.error('openUrl : err : %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); + log.info('%{public}s', JSON.stringify(Result.success(""))); + }).catch((err: BusinessError) => { + log.error('openUrl : err : %{public}s', err.code, err.message); }); } \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 99% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets index 9a552f56e49f..ab8016646c7d 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets @@ -51,4 +51,4 @@ function terminateSelf(): void { let message = (err as BusinessError).message; console.error(`terminateSelf failed, code is ${code}, message is ${message}`); } -} +} \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 67% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584810..239fe6990c0d 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets @@ -5,6 +5,8 @@ import Logger from '../../utils/Logger'; import window from '@ohos.window'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import { Rect } from '@ohos.application.AccessibilityExtensionAbility'; +import { JSON, worker } from '@kit.ArkTS'; +import {BusinessError} from '@kit.BasicServicesKit' let log = new Logger(0x0001, "DeviceUtils"); @@ -17,6 +19,10 @@ export class DeviceUtils { static _cutoutWidth: number; static _cutoutTop: number; static _cutoutHeight: number; + static _originalWindowWidth: number = 0; + static _originalWindowHeight: number = 0; + static oldSafeArea: window.Rect = {left: 0, top: 0, width: 0, height: 0}; + static safeAreaInWorker: window.Rect = {left: 0, top: 0, width: 0, height: 0}; static getDpi(): number { return display.getDefaultDisplaySync().densityDPI; @@ -46,7 +52,7 @@ export class DeviceUtils { } static setKeepScreenOn(value: boolean) { - let windowClass = null; + let windowClass:window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { //鑾峰彇绐楀彛瀹炰緥 if (err.code) { @@ -58,7 +64,7 @@ export class DeviceUtils { let keepScreenOnPromise = windowClass.setWindowKeepScreenOn(value); Promise.all([keepScreenOnPromise]).then(() => { log.info('Succeeded in setKeepScreenOn, value:%{public}s', value); - }).catch((err) => { + }).catch((err:BusinessError) => { log.error('Failed to setKeepScreenOn, cause:%{public}s', JSON.stringify(err)); }); }); @@ -80,11 +86,11 @@ export class DeviceUtils { } static initScreenInfo() : void { - let windowClass = null; + let windowClass: window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { if (err.code) { - log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', JSON.stringify(err)); + log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', err.code, err.message); return; } @@ -113,8 +119,8 @@ export class DeviceUtils { DeviceUtils._cutoutTop = data.boundingRects[0].top; DeviceUtils._cutoutWidth = data.boundingRects[0].width; DeviceUtils._cutoutHeight = data.boundingRects[0].height; - }).catch((err) => { - log.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err)); + }).catch((err: BusinessError) => { + log.error('Failed to obtain all the display objects. Code: ', err.code, err.message); }); } @@ -161,4 +167,43 @@ export class DeviceUtils { return DeviceUtils._cutoutLeft + DeviceUtils._cutoutWidth; } -} \ No newline at end of file + + static calculateSafeArea(cocosWorker: worker.ThreadWorker, avoidArea: window.AvoidArea, windowRect: window.Rect) { + let safeArea : window.Rect = {left: 0, top: 0, width: 0, height: 0}; + if(DeviceUtils._originalWindowWidth == windowRect.width && DeviceUtils._originalWindowHeight == windowRect.height) { + safeArea.left = DeviceUtils.oldSafeArea.left + avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = DeviceUtils.oldSafeArea.top + avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : DeviceUtils.oldSafeArea.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : DeviceUtils.oldSafeArea.height) - safeArea.top; + } else { + safeArea.left = avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : windowRect.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : windowRect.height) - safeArea.top; + DeviceUtils._originalWindowWidth = windowRect.width; + DeviceUtils._originalWindowHeight = windowRect.height; + } + DeviceUtils.oldSafeArea = safeArea; + cocosWorker.postMessage({type: 'updateSafeArea', safeArea: safeArea}); + } + + static updateWorkerSafeArea(safeArea: window.Rect) { + DeviceUtils.safeAreaInWorker = safeArea; + } + + static getSafeAreaLeft() : number { + return DeviceUtils.safeAreaInWorker.left; + } + + static getSafeAreaTop() : number { + return DeviceUtils.safeAreaInWorker.top; + } + + static getSafeAreaWidth() : number { + return DeviceUtils.safeAreaInWorker.width; + } + + static getSafeAreaHeight() : number { + return DeviceUtils.safeAreaInWorker.height; + } +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets diff --git a/tests/lua-tests/project/proj.ohos/build-profile.json5 b/tests/lua-tests/project/proj.ohos/build-profile.json5 index 119d7cfe9ce5..b4b36417403b 100644 --- a/tests/lua-tests/project/proj.ohos/build-profile.json5 +++ b/tests/lua-tests/project/proj.ohos/build-profile.json5 @@ -4,7 +4,7 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", + "compatibleSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS" } ], diff --git a/tests/lua-tests/project/proj.ohos/entry/build-profile.json5 b/tests/lua-tests/project/proj.ohos/entry/build-profile.json5 index 63c3ceddf3bf..6217cf4b4ab8 100644 --- a/tests/lua-tests/project/proj.ohos/entry/build-profile.json5 +++ b/tests/lua-tests/project/proj.ohos/entry/build-profile.json5 @@ -12,7 +12,7 @@ }, "sourceOption": { "workers": [ - './src/main/ets/workers/CocosWorker.ts' + './src/main/ets/workers/CocosWorker.ets' ] } }, diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..d30c29f2d907 --- /dev/null +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets @@ -0,0 +1,118 @@ +import window from '@ohos.window'; +import UIAbility from '@ohos.app.ability.UIAbility'; +import nativeRender from "libnativerender.so"; +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" +import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" +import { BusinessError } from '@kit.BasicServicesKit'; +import { WorkerManager } from '../workers/WorkerManager'; +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + +const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); +const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS); +let cocosWorker = WorkerManager.getInstance().getWorker(); +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); +export default class MainAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + nativeAppLifecycle.onCreate(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context); + console.info('[LIFECYCLE-App] onCreate') + } + + onDestroy() { + nativeAppLifecycle.onDestroy(); + console.info('[LIFECYCLE-App] onDestroy') + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + windowStage.loadContent('pages/Index', (err:BusinessError, data) => { + if (err.code) { + return; + } + rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); + rawFileUtils.writablePathInit(this.context.filesDir); + }); + + windowStage.getMainWindow().then((windowIns: window.Window) => { + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns); + // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. + let systemBarPromise = windowIns.setWindowSystemBarEnable([]); + // Whether the window layout is displayed in full screen mode + let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); + // Sets whether the screen is always on. + let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); + Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { + console.info('Succeeded in setting the window'); + }).catch((err: BusinessError) => { + console.error('Failed to set the window, cause ', err.code, err.message); + }); + + try { + DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect); + windowIns.on('avoidAreaChange', (data) => { + console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' + + JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area)); + + if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) { + return; + } + + let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW); + DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect); + }); + } catch (exception) { + console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { + let stageEventType: window.WindowStageEventType = data; + switch (stageEventType) { + case window.WindowStageEventType.RESUMED: + console.info('[LIFECYCLE-App] onShow_RESUMED') + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + break; + case window.WindowStageEventType.PAUSED: + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + console.info('[LIFECYCLE-App] onHide_PAUSED') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + break; + default: + break; + } + }); + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + } + + onForeground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + // Ability has brought to foreground + console.info('[LIFECYCLE-App] onShow') + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + } + + onBackground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + // Ability has back to background + console.info('[LIFECYCLE-App] onHide') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + } +}; diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index a4378409d4f4..000000000000 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts +++ /dev/null @@ -1,80 +0,0 @@ -import window from '@ohos.window'; -import UIAbility from '@ohos.app.ability.UIAbility'; -import web_webview from '@ohos.web.webview'; -import nativeRender from "libnativerender.so"; -import { ContextType } from "@ohos/libSysCapabilities" -import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" - -const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); -const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS); - -export default class MainAbility extends UIAbility { - onCreate(want, launchParam) { - nativeAppLifecycle.onCreate(); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context); - // Initializes the webView kernel of the system. This parameter is optional if it is not used. - web_webview.WebviewController.initializeWebEngine(); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_WANT, this.context); - console.info('[LIFECYCLE-App] onCreate') - } - - onDestroy() { - nativeAppLifecycle.onDestroy(); - console.info('[LIFECYCLE-App] onDestroy') - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - return; - } - rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); - rawFileUtils.writablePathInit(this.context.filesDir); - }); - - windowStage.getMainWindow().then((windowIns: window.Window) => { - // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. - let systemBarPromise = windowIns.setWindowSystemBarEnable([]); - // Whether the window layout is displayed in full screen mode - let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); - // Sets whether the screen is always on. - let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); - Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { - console.info('Succeeded in setting the window'); - }).catch((err) => { - console.error('Failed to set the window, cause ' + JSON.stringify(err)); - }); - }) - - windowStage.on("windowStageEvent", (data) => { - let stageEventType: window.WindowStageEventType = data; - switch (stageEventType) { - case window.WindowStageEventType.RESUMED: - nativeAppLifecycle.onShow(); - break; - case window.WindowStageEventType.PAUSED: - nativeAppLifecycle.onHide(); - break; - default: - break; - } - }); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - } - - onForeground() { - // Ability has brought to foreground - console.info('[LIFECYCLE-App] onShow') - nativeAppLifecycle.onShow(); - } - - onBackground() { - // Ability has back to background - console.info('[LIFECYCLE-App] onDestroy') - nativeAppLifecycle.onHide(); - } -}; diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets index 138b7431c3b6..6f67afe3b443 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets @@ -12,7 +12,7 @@ export struct CocosVideoPlayer { Video({ src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile, controller: this.videoPlayerInfo.controller - }).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y }) + }) .width(this.videoPlayerInfo.w) .height(this.videoPlayerInfo.h) .visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None) diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets index d68969bc065f..5205ec3ba79d 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets @@ -4,7 +4,7 @@ import { WorkerManager } from '../workers/WorkerManager'; @Component export struct CocosWebView { - viewInfo: WebViewInfo = new WebViewInfo(0, 0, 0, 0, 0); + @ObjectLink viewInfo: WebViewInfo ; cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker(); build() { @@ -27,6 +27,9 @@ export struct CocosWebView { }) .onPageEnd((event) => { this.cocosWorker.postMessage({ type: "onPageEnd", viewTag: this.viewInfo.viewTag, url: event == null ? '' : event.url }) + if(this.viewInfo.jsInterfaceScheme != "" && event != null && event.url.startsWith(this.viewInfo.jsInterfaceScheme)) { + this.cocosWorker.postMessage({ type: "onJsCallBack", viewTag: this.viewInfo.viewTag, url: event == null ? '' : event.url }) + } // if u want use the javascript on ur page, u can use registerJavaScriptProxy() to register js function here // and confirm that just register once by a local variable }) @@ -37,4 +40,4 @@ export struct CocosWebView { this.cocosWorker.postMessage({ type: "onErrorReceive", viewTag: this.viewInfo.viewTag, url: this.viewInfo.url }) }) } -} \ No newline at end of file +} diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets index 7eb4e1c34369..dbb983025e14 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -17,11 +17,11 @@ import promptAction from '@ohos.promptAction'; import process from '@ohos.process'; const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE); +let cocosWorker = WorkerManager.getInstance().getWorker(); + @Entry @Component struct Index { - - cocosWorker = WorkerManager.getInstance().getWorker(); xcomponentController: XComponentController = new XComponentController(); processMgr = new process.ProcessManager(); @@ -56,7 +56,7 @@ struct Index { console.log('[LIFECYCLE-Page] onPageShow'); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_ARRAY, this.editBoxArray); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_INDEX_MAP, this.editBoxIndexMap); - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, this.cocosWorker); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, cocosWorker); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY, this.webViewArray); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP, this.webViewIndexMap); GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_ARRAY, this.videoPlayerInfoArray); @@ -80,12 +80,10 @@ struct Index { buttons: [ { text: '鍙栨秷', - // Color can be customized color: '#000000' }, { text: '纭', - // Color can be customized color: '#000000' } ], @@ -105,12 +103,13 @@ struct Index { // If disable system exit needed, remove comment "return true" return true; } + onMouseWheel(eventType: string, scrollY: number) { - this.cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); + cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); } build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Stack() { XComponent({ id: 'xcomponentId', type: 'surface', @@ -135,9 +134,9 @@ struct Index { ) .onLoad((context) => { console.log('[cocos] XComponent.onLoad Callback'); - this.cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)}); - this.cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" }); - this.cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage; + cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)}); + cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" }); + cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage; }) .onDestroy(() => { }) diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets similarity index 70% rename from tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts rename to tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets index e81350f059cb..d8047e80ea9d 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets @@ -1,13 +1,13 @@ -import worker, { ThreadWorkerGlobalScope } from '@ohos.worker'; +import worker, { ThreadWorkerGlobalScope, MessageEvents } from '@ohos.worker'; import nativeRender from "libnativerender.so"; -import { ContextType } from "@ohos/libSysCapabilities" +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" import { VideoPlayer } from "@ohos/libSysCapabilities" +import { ApplicationManager } from "@ohos/libSysCapabilities" import { CocosEditBox } from "@ohos/libSysCapabilities" import { Dialog } from "@ohos/libSysCapabilities" import { WebView } from "@ohos/libSysCapabilities" import { JumpManager } from "@ohos/libSysCapabilities" import { NapiHelper } from "@ohos/libSysCapabilities" -import { ApplicationManager } from "@ohos/libSysCapabilities" import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const appLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); @@ -24,53 +24,60 @@ NapiHelper.registerFunctions(napiContext.registerFunction) const workerPort: ThreadWorkerGlobalScope = worker.workerPort; -workerPort.onmessage = function(e) : void { - let data = e.data; - switch(data.type) { +workerPort.onmessage = (e: MessageEvents) => { + switch(e.data.type) { case "onXCLoad": console.log("[cocos] onXCLoad Callback"); Dialog.init(workerPort); CocosEditBox.init(workerPort); JumpManager.init(workerPort); WebView.init(workerPort); - VideoPlayer.init(workerPort); ApplicationManager.init(workerPort); + VideoPlayer.init(workerPort); napiContext.initAsyncInfo(); break; case "abilityContextInit": - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, data.data); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, e.data.data); break; case "editBoxOnFocus": - inputNapi.editBoxOnFocusCB(data.viewTag); + inputNapi.editBoxOnFocusCB(e.data.viewTag); break; case "editBoxOnChange": - inputNapi.editBoxOnChangeCB(data.viewTag, data.value); + inputNapi.editBoxOnChangeCB(e.data.viewTag, e.data.value); break; case "editBoxOnEnter": - inputNapi.editBoxOnEnterCB(data.viewTag, data.text); + inputNapi.editBoxOnEnterCB(e.data.viewTag, e.data.text); break; case "textFieldTTFOnChange": - inputNapi.textFieldTTFOnChangeCB(data.data); + inputNapi.textFieldTTFOnChangeCB(e.data.data); break; case "onMouseWheel": - mouseNapi.mouseWheelCB(data.eventType, data.scrollY); + mouseNapi.mouseWheelCB(e.data.eventType, e.data.scrollY); break; case "onPageBegin": - webViewNapi.shouldStartLoading(data.viewTag, data.url); + webViewNapi.shouldStartLoading(e.data.viewTag, e.data.url); break; case "onPageEnd": - webViewNapi.finishLoading(data.viewTag, data.url); + webViewNapi.finishLoading(e.data.viewTag, e.data.url); + break; + case "onJsCallBack": + webViewNapi.jsCallback(); break; case "onErrorReceive": - webViewNapi.failLoading(data.viewTag, data.url); + webViewNapi.failLoading(e.data.viewTag, e.data.url); break; case "onVideoCallBack": - videoPlayNapi.onVideoCallBack(data.viewTag, data.event); + videoPlayNapi.onVideoCallBack(e.data.viewTag, e.data.event); break; case "exit": appLifecycle.onBackPress(); break; + case "updateSafeArea": + DeviceUtils.updateWorkerSafeArea(e.data.safeArea); + break; default: console.error("cocos worker: message type unknown") } } + + diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 97% rename from tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets index e2ec70b1873e..0447223b1e6a 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets @@ -5,7 +5,7 @@ export class WorkerManager { private cocosWorker: worker.ThreadWorker; private constructor() { - this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ts", { + this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ets", { type: "classic", name: "CocosWorker" }); diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 b/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 index 34dfcd5b76c8..f196713bf800 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 @@ -15,7 +15,7 @@ "abilities": [ { "name": "MainAbility", - "srcEntry": "./ets/MainAbility/MainAbility.ts", + "srcEntry": "./ets/MainAbility/MainAbility.ets", "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", @@ -33,23 +33,10 @@ } ], // https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/module-configuration-file.md/ - //将窗口模式设置为只支持全屏 - "supportWindowMode": ["fullscreen"], - // 游戏主窗口固定窗口化 - // "supportWindowMode": ["floating"], - // 如果有全屏需求,可以同时加上fullscreen和floating,可以做到窗口化按这只宽高显示,全屏按全屏方案显示 - //"supportWindowMode": ["fullscreen", "floating"], - //最大宽高比 - // "maxWindowRatio": 3.5, - //最小宽高比 - // "minWindowRatio": 0.5, - //最大宽度为1080 + "supportWindowMode": ["fullscreen"], "maxWindowWidth": 1080, - //最小宽度为1080 "minWindowWidth": 1080, - //最大高度为720 "maxWindowHeight": 720, - //最小高度为720 "minWindowHeight": 720 } ], diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets similarity index 96% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets index 6c537383d25b..decdc1332d1f 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets @@ -12,4 +12,4 @@ export { NapiHelper } from './src/main/ets/napi/NapiHelper' export { JumpManager } from './src/main/ets/system/appJump/JumpManager' export { DeviceUtils } from './src/main/ets/system/device/DeviceUtils' -export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager' +export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager' \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 b/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 index 8c935e9b2a00..d00806d42de9 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 @@ -4,7 +4,7 @@ "author": "", "name": "libsyscapabilities", "description": "Please describe the basic information.", - "main": "index.ts", + "main": "index.ets", "version": "1.0.0", "dynamicDependencies": {}, "dependencies": {} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b2664e..00b7c24920b4 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts @@ -22,4 +22,7 @@ export class GlobalContextConstants { static readonly COCOS2DX_ABILITY_CONTEXT = "Cocos2dx.abilityContext"; static readonly COCOS2DX_ABILITY_WANT = "Cocos2dx.abilityWant"; static readonly COCOS2DX_WEB_RESULT= "Cocos2dx.webResult"; -} \ No newline at end of file + static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow"; + static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag"; + static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag"; +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets similarity index 98% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets index 37b698c6dce5..5355a89f86fa 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets @@ -40,7 +40,7 @@ function showDialog(dialogTitle: string, dialogMessage: string) { color: '#000000' }, ], - success: function(data) { + success: (data) => { log.debug("handling callback, data:%{public}s", data); } }); diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 95% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 112bd864dda5..968ed4cca754 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets @@ -3,7 +3,7 @@ import { DialogMsgEntity } from '../../entity/WorkerMsgEntity'; export class Dialog { static MODULE_NAME : string = 'Dialog'; - static workerPort; + static workerPort: ThreadWorkerGlobalScope; static init(workerPort: ThreadWorkerGlobalScope) : void { Dialog.workerPort = workerPort; diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets similarity index 96% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets index f07362f91007..ed2049cb2c93 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets @@ -1,11 +1,12 @@ +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; export class CocosEditBox { static MODULE_NAME : string = 'EditBox'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { CocosEditBox.workerPort = workerPort; } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets similarity index 95% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index 50b7a7e69d00..f4a11e84d250 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -1,11 +1,12 @@ import { VideoPlayMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class VideoPlayer { static MODULE_NAME: string = 'VideoPlay'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { VideoPlayer.workerPort = workerPort; } diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets similarity index 90% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets index 0d00f332afe0..49ffb1dad4e5 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets @@ -1,11 +1,12 @@ import { ViewRect, WebViewMsgEntity } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class WebView { static MODULE_NAME: string = 'WebView'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { WebView.workerPort = workerPort; } @@ -19,6 +20,12 @@ export class WebView { WebView.workerPort.postMessage(webViewMsgEntity); } + static setJavascriptInterfaceScheme(viewTag: number, jsInterfaceScheme: string) : void { + let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity(WebView.MODULE_NAME, 'setJavascriptInterfaceScheme', viewTag); + webViewMsgEntity.jsInterfaceScheme = jsInterfaceScheme; + WebView.workerPort.postMessage(webViewMsgEntity); + } + static loadData(viewTag: number, data: string, mimeType: string, encoding: string, baseURL: string) : void { let webViewMsgEntity: WebViewMsgEntity = new WebViewMsgEntity(WebView.MODULE_NAME, 'loadData', viewTag); webViewMsgEntity.data = data; diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets index cdd1775956f5..b03c4004f522 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets @@ -7,7 +7,7 @@ import { BusinessError } from '@ohos.base'; let log: Logger = new Logger(0x0001, "WebViewMsg"); @Observed export class WebViewInfo { - public uniqueId: number = 0; + public uniqueId : number = 0; // position public x: number = 0; public y: number = 0; @@ -17,11 +17,13 @@ export class WebViewInfo { // url public url: string = ''; public isUrl: boolean = true; + public viewTag: number = 0 public zoomAccess: boolean = true public visible: boolean = true public opacity: number = 1 public backgroundColor: number = 0xffffff; + public jsInterfaceScheme: string = ""; public controller: web_webview.WebviewController = new web_webview.WebviewController() constructor(x: number, y: number, w: number, h: number, viewTag: number) { @@ -35,9 +37,9 @@ export class WebViewInfo { } -function waitUtilControllerAttached(): Promise { +function waitUtilControllerAttached() : Promise { return new Promise((resolve, reject) => { - resolve(1); + resolve(1); }) } @@ -70,6 +72,12 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } break; } + case "setJavascriptInterfaceScheme": { + index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); + let webViewInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + webViewInfo.jsInterfaceScheme = eventData.jsInterfaceScheme; + break; + } case "loadData": { index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); let webViewInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; @@ -142,7 +150,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } case "setWebViewRect": { index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo :WebViewInfo= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.x = px2vp(eventData.viewRect.x); tmpWebInfo.y = px2vp(eventData.viewRect.y); tmpWebInfo.w = px2vp(eventData.viewRect.w); @@ -155,7 +163,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .visible == eventData.visible) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo:WebViewInfo= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.visible = eventData.visible; break; } @@ -165,7 +173,7 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .opacity == eventData.opacity) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo :WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.opacity = eventData.opacity; break; } @@ -183,13 +191,13 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { index = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP).get(eventData.viewTag); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index] .controller - .runJavaScript(eventData.js, (error: BusinessError, result: string) => { + .runJavaScript(eventData.js, (error:BusinessError, result:string) => { if (error) { log.warn("webView run JavaScript error:%{public}s", JSON.stringify(error)); return; } if (result) { - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_RESULT, result) + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_RESULT,result) log.debug("webView run JavaScript result is %{public}s:", result); } }) @@ -201,9 +209,9 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { .zoomAccess == eventData.scalesPageToFit) { return; } - let tmpWebInfo: WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; + let tmpWebInfo:WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.zoomAccess == eventData.scalesPageToFit } break; } -} \ No newline at end of file +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets @@ -0,0 +1,23 @@ +interface ResultObjectInterface { + "errCode": number; + "errMsg": string; + "data": string, +} + +export class Result { + public static success(data: string){ + return ({ + "errCode": 0, + "errMsg": "", + "data": data, + }as ResultObjectInterface); + } + + public static error(errCode:number, errMsg:string){ + return ({ + "errCode": errCode, + "errMsg": errMsg, + "data": "", + } as ResultObjectInterface); + } +}; \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts +++ /dev/null @@ -1,16 +0,0 @@ -export class Result { - public static success(data){ - return { - "errCode": 0, - "errMsg": "", - "data": data, - }; - } - - public static error(errCode, errMsg) { - return { - "errCode": errCode, - "errMsg": errMsg, - }; - } -}; \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/WorkerMsgEntity.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/WorkerMsgEntity.ts index 1c9140ed0db9..608158d25be9 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/WorkerMsgEntity.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/WorkerMsgEntity.ts @@ -124,6 +124,7 @@ export class WebViewMsgEntity extends BaseWorkerMsgEntity { js: string scalesPageToFit: boolean + jsInterfaceScheme: string constructor(module: string, func: string, viewTag: number) { super(module, func); diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets similarity index 93% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index 19887eed7d5b..cdf556c9f7e1 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -29,7 +29,6 @@ export class NapiHelper { registerFunc('JumpManager.openUrl', JumpManager.openUrl); } - private static registerDeviceUtils(registerFunc : Function) { registerFunc('DeviceUtils.getDpi', DeviceUtils.getDpi); registerFunc('DeviceUtils.getSystemLanguage', DeviceUtils.getSystemLanguage); @@ -42,6 +41,10 @@ export class NapiHelper { registerFunc('DeviceUtils.getOrientation', DeviceUtils.getOrientation); registerFunc('DeviceUtils.getCutoutHeight', DeviceUtils.getCutoutHeight); registerFunc('DeviceUtils.getCutoutWidth', DeviceUtils.getCutoutWidth); + registerFunc('DeviceUtils.getSafeAreaLeft', DeviceUtils.getSafeAreaLeft); + registerFunc('DeviceUtils.getSafeAreaTop', DeviceUtils.getSafeAreaTop); + registerFunc('DeviceUtils.getSafeAreaWidth', DeviceUtils.getSafeAreaWidth); + registerFunc('DeviceUtils.getSafeAreaHeight', DeviceUtils.getSafeAreaHeight); } private static registerEditBox(registerFunc : Function) { @@ -66,6 +69,7 @@ export class NapiHelper { private static registerWebView(registerFunc : Function) { registerFunc('WebView.createWebView', WebView.createWebView); registerFunc('WebView.removeWebView', WebView.removeWebView); + registerFunc('WebView.setJavascriptInterfaceScheme', WebView.setJavascriptInterfaceScheme); registerFunc('WebView.loadData', WebView.loadData); registerFunc('WebView.loadURL', WebView.loadURL); registerFunc('WebView.loadFile', WebView.loadFile); @@ -107,4 +111,4 @@ export class NapiHelper { registerFunc('Preferences.flush', Preferences.flush); registerFunc('Preferences.clear', Preferences.clear); } -} \ No newline at end of file +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets similarity index 72% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets index e95e89b29d10..4b64aff5551d 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets @@ -5,13 +5,12 @@ import common from '@ohos.app.ability.common'; import { GlobalContext, GlobalContextConstants } from '../common/GlobalContext'; let log: Logger = new Logger(0x0001, "Preferences"); -let preferences: data_preferences.Preferences | null = null; +let preferences: data_preferences.Preferences; const PREFS_NAME: string = "Cocos2dxPreferences"; export default class Preferences { - // 閫氳繃 preferencesName 鑾峰彇Preferences瀹炰緥 - static getPreferences(): data_preferences.Preferences { + static getPreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { preferences = data_preferences.getPreferencesSync(context, {name: PREFS_NAME}); @@ -21,13 +20,8 @@ export default class Preferences { let message = (err as BusinessError).message; log.error("Failed to get preferences. code =" + code + ", message =" + message); } - return preferences; } - /* - 閫氳繃 preferencesName 浠庣紦瀛樹腑绉诲嚭鎸囧畾鐨凱references瀹炰緥锛岃嫢Preferences瀹炰緥鏈夊搴旂殑鎸佷箙鍖栨枃浠讹紝鍒欏悓鏃跺垹闄ゅ叾鎸佷箙鍖栨枃浠躲備娇鐢≒romise寮傛鍥炶皟銆 - 璋冪敤璇ユ帴鍙e悗锛屼笉寤鸿鍐嶄娇鐢ㄦ棫鐨凱references瀹炰緥杩涜鏁版嵁鎿嶄綔锛屽惁鍒欎細鍑虹幇鏁版嵁涓鑷存ч棶棰橈紝搴斿皢Preferences瀹炰緥缃负null锛岀郴缁熷皢浼氱粺涓鍥炴敹銆 - */ static deletePreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -41,12 +35,6 @@ export default class Preferences { } } - /* - 閫氳繃 preferencesName 浠庣紦瀛樹腑绉诲嚭鎸囧畾鐨凱references瀹炰緥锛屼娇鐢≒romise寮傛鍥炶皟銆 - 搴旂敤棣栨璋冪敤getPreferences鎺ュ彛鑾峰彇鏌愪釜Preferences瀹炰緥鍚庯紝璇ュ疄渚嬩細琚細琚紦瀛樿捣鏉ワ紝鍚庣画鍐嶆getPreferences鏃朵笉浼氬啀娆′粠鎸佷箙鍖栨枃浠朵腑璇诲彇锛 - 鐩存帴浠庣紦瀛樹腑鑾峰彇Preferences瀹炰緥銆傝皟鐢ㄦ鎺ュ彛绉诲嚭缂撳瓨涓殑瀹炰緥涔嬪悗锛屽啀娆etPreferences灏嗕細閲嶆柊璇诲彇鎸佷箙鍖栨枃浠讹紝鐢熸垚鏂扮殑Preferences瀹炰緥銆 - 璋冪敤璇ユ帴鍙e悗锛屼笉寤鸿鍐嶄娇鐢ㄦ棫鐨凱references瀹炰緥杩涜鏁版嵁鎿嶄綔锛屽惁鍒欎細鍑虹幇鏁版嵁涓鑷存ч棶棰橈紝搴斿皢Preferences瀹炰緥缃负null锛岀郴缁熷皢浼氱粺涓鍥炴敹銆 - */ static removePreferencesFromCache(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -59,7 +47,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓幏鍙栭敭瀵瑰簲鐨勫硷紝濡傛灉鍊间负null鎴栬呴潪榛樿鍊肩被鍨嬶紝杩斿洖榛樿鏁版嵁defValue static get(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType { if (preferences === null) { Preferences.getPreferences(); @@ -76,7 +63,6 @@ export default class Preferences { } } - // 灏嗘暟鎹啓鍏ョ紦瀛樼殑Preferences瀹炰緥涓紝鍙氳繃flush灏哖references瀹炰緥鎸佷箙鍖 static put(key: string, value: data_preferences.ValueType): void { if (preferences === null) { Preferences.getPreferences(); @@ -92,7 +78,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓幏鍙栨墍鏈夐敭鍊兼暟鎹 static getAll(): string | undefined { if (preferences === null) { Preferences.getPreferences(); @@ -111,7 +96,6 @@ export default class Preferences { } } - // 妫鏌ョ紦瀛樼殑Preferences瀹炰緥涓槸鍚﹀寘鍚悕涓虹粰瀹欿ey鐨勫瓨鍌ㄩ敭鍊煎 static has(key: string): boolean { if (preferences === null) { Preferences.getPreferences(); @@ -133,7 +117,6 @@ export default class Preferences { } } - // 浠庣紦瀛樼殑Preferences瀹炰緥涓垹闄ゅ悕涓虹粰瀹欿ey鐨勫瓨鍌ㄩ敭鍊煎锛屽彲閫氳繃flush灏哖references瀹炰緥鎸佷箙鍖 static delete(key: string): void { if (preferences === null) { Preferences.getPreferences(); @@ -149,7 +132,6 @@ export default class Preferences { } } - // 灏嗙紦瀛樼殑Preferences瀹炰緥涓殑鏁版嵁寮傛瀛樺偍鍒扮敤鎴烽閫夐」鐨勬寔涔呭寲鏂囦欢涓紝浣跨敤Promise寮傛鍥炶皟銆 static flush(): void { if (preferences === null) { Preferences.getPreferences(); @@ -159,7 +141,6 @@ export default class Preferences { }); } - // 娓呴櫎缂撳瓨鐨凱references瀹炰緥涓殑鎵鏈夋暟鎹紝鍙氳繃flush灏哖references瀹炰緥鎸佷箙鍖栵紝浣跨敤Promise寮傛鍥炶皟銆 static clear(): void { if (preferences === null) { Preferences.getPreferences(); @@ -175,7 +156,6 @@ export default class Preferences { } - // 璁㈤槄鏁版嵁鍙樻洿锛岃闃呯殑Key鐨勫煎彂鐢熷彉鏇村悗锛屽湪鎵цflush鏂规硶鍚庯紝瑙﹀彂callback鍥炶皟銆 static onChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -192,7 +172,6 @@ export default class Preferences { } } - // 鍙栨秷璁㈤槄鏁版嵁鍙樻洿銆 static offChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -210,8 +189,6 @@ export default class Preferences { } } -// 鐢变簬ArkTS涓棤Object.keys锛屼笖鏃犳硶浣跨敤for..in... -// 鑻ユ姤ArkTS闂锛岃灏嗘鏂规硶鍗曠嫭鎶界鑷充竴涓猼s鏂囦欢涓苟鏆撮湶锛屽湪闇瑕佺敤鍒扮殑ets鏂囦欢涓紩鍏ヤ娇鐢 function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets similarity index 76% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets index ea373af90811..2c20f240b2ce 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets @@ -3,6 +3,8 @@ import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContex import {Result} from "../../entity/Result" import type { JumpMsgEntity } from '../../entity/WorkerMsgEntity'; import Logger from '../../utils/Logger' +import { BusinessError } from '@kit.BasicServicesKit'; +import { Want } from '@kit.AbilityKit' let log: Logger = new Logger(0x0001, "JumpManagerMsg"); @@ -18,14 +20,14 @@ export function handleJumpManagerMsg(eventData: JumpMsgEntity) : void { function openUrl(url: string): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); - let wantInfo = { + let wantInfo: Want = { 'action': 'ohos.want.action.viewData', 'entities': ['entity.system.browsable'], 'uri': url } context.startAbility(wantInfo).then(() => { - log.info('%{public}s', JSON.stringify(Result.success({}))); - }).catch((err) => { - log.error('openUrl : err : %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); + log.info('%{public}s', JSON.stringify(Result.success(""))); + }).catch((err: BusinessError) => { + log.error('openUrl : err : %{public}s', err.code, err.message); }); } \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 99% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets index ffe0826a5f7a..ab8016646c7d 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets @@ -51,5 +51,4 @@ function terminateSelf(): void { let message = (err as BusinessError).message; console.error(`terminateSelf failed, code is ${code}, message is ${message}`); } -} - +} \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 67% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584810..239fe6990c0d 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets @@ -5,6 +5,8 @@ import Logger from '../../utils/Logger'; import window from '@ohos.window'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import { Rect } from '@ohos.application.AccessibilityExtensionAbility'; +import { JSON, worker } from '@kit.ArkTS'; +import {BusinessError} from '@kit.BasicServicesKit' let log = new Logger(0x0001, "DeviceUtils"); @@ -17,6 +19,10 @@ export class DeviceUtils { static _cutoutWidth: number; static _cutoutTop: number; static _cutoutHeight: number; + static _originalWindowWidth: number = 0; + static _originalWindowHeight: number = 0; + static oldSafeArea: window.Rect = {left: 0, top: 0, width: 0, height: 0}; + static safeAreaInWorker: window.Rect = {left: 0, top: 0, width: 0, height: 0}; static getDpi(): number { return display.getDefaultDisplaySync().densityDPI; @@ -46,7 +52,7 @@ export class DeviceUtils { } static setKeepScreenOn(value: boolean) { - let windowClass = null; + let windowClass:window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { //鑾峰彇绐楀彛瀹炰緥 if (err.code) { @@ -58,7 +64,7 @@ export class DeviceUtils { let keepScreenOnPromise = windowClass.setWindowKeepScreenOn(value); Promise.all([keepScreenOnPromise]).then(() => { log.info('Succeeded in setKeepScreenOn, value:%{public}s', value); - }).catch((err) => { + }).catch((err:BusinessError) => { log.error('Failed to setKeepScreenOn, cause:%{public}s', JSON.stringify(err)); }); }); @@ -80,11 +86,11 @@ export class DeviceUtils { } static initScreenInfo() : void { - let windowClass = null; + let windowClass: window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { if (err.code) { - log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', JSON.stringify(err)); + log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', err.code, err.message); return; } @@ -113,8 +119,8 @@ export class DeviceUtils { DeviceUtils._cutoutTop = data.boundingRects[0].top; DeviceUtils._cutoutWidth = data.boundingRects[0].width; DeviceUtils._cutoutHeight = data.boundingRects[0].height; - }).catch((err) => { - log.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err)); + }).catch((err: BusinessError) => { + log.error('Failed to obtain all the display objects. Code: ', err.code, err.message); }); } @@ -161,4 +167,43 @@ export class DeviceUtils { return DeviceUtils._cutoutLeft + DeviceUtils._cutoutWidth; } -} \ No newline at end of file + + static calculateSafeArea(cocosWorker: worker.ThreadWorker, avoidArea: window.AvoidArea, windowRect: window.Rect) { + let safeArea : window.Rect = {left: 0, top: 0, width: 0, height: 0}; + if(DeviceUtils._originalWindowWidth == windowRect.width && DeviceUtils._originalWindowHeight == windowRect.height) { + safeArea.left = DeviceUtils.oldSafeArea.left + avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = DeviceUtils.oldSafeArea.top + avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : DeviceUtils.oldSafeArea.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : DeviceUtils.oldSafeArea.height) - safeArea.top; + } else { + safeArea.left = avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : windowRect.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : windowRect.height) - safeArea.top; + DeviceUtils._originalWindowWidth = windowRect.width; + DeviceUtils._originalWindowHeight = windowRect.height; + } + DeviceUtils.oldSafeArea = safeArea; + cocosWorker.postMessage({type: 'updateSafeArea', safeArea: safeArea}); + } + + static updateWorkerSafeArea(safeArea: window.Rect) { + DeviceUtils.safeAreaInWorker = safeArea; + } + + static getSafeAreaLeft() : number { + return DeviceUtils.safeAreaInWorker.left; + } + + static getSafeAreaTop() : number { + return DeviceUtils.safeAreaInWorker.top; + } + + static getSafeAreaWidth() : number { + return DeviceUtils.safeAreaInWorker.width; + } + + static getSafeAreaHeight() : number { + return DeviceUtils.safeAreaInWorker.height; + } +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts deleted file mode 100644 index 56309439f011..000000000000 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { getContext } from "libnativerender.so"; -import { ContextType } from "../../common/Constants" -import sensor from '@ohos.sensor'; -import display from '@ohos.display'; -import {Result} from "../../entity/Result" -import Logger from '../../utils/Logger' - -let log: Logger = new Logger(0x0001, "AccelerometerUtils"); - -const accUtils = getContext(ContextType.SENSOR_API); - -export default class Accelerometer { - - private static instance = new Accelerometer(); - - static getInstance() : Accelerometer { - return Accelerometer.instance; - } - - static enable(intervalTime: number) : void { - try { - /* HarmonyOS allow multiple subscriptions, but the game only need one - so if the interval changed, cancel subscription and redo with the new interval */ - sensor.off(sensor.SensorId.ACCELEROMETER); - sensor.on(sensor.SensorId.ACCELEROMETER, function (data) { - let rotation = display.getDefaultDisplaySync().rotation; - if (rotation === 0) { - // Display device screen rotation 0掳 - accUtils.onAccelerometerCallBack(data.x, data.y, data.z, intervalTime); - } else if (rotation === 1) { - // Display device screen rotation 90掳 - accUtils.onAccelerometerCallBack(data.y, -data.x, data.z, intervalTime); - } else if (rotation === 2) { - // Display device screen rotation 180掳 - accUtils.onAccelerometerCallBack(-data.x, -data.y, data.z, intervalTime); - } else if (rotation === 3) { - // Display device screen rotation 270掳 - accUtils.onAccelerometerCallBack(-data.y, data.x, data.z, intervalTime); - } else { - log.error('Accelerometer init fail, err: %{public}s', JSON.stringify(Result.error(-1, 'unsupported rotation: ' + rotation))); - } - }, { interval: intervalTime }); - } catch (err) { - log.error('Accelerometer init fail, err: %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); - } - } - - static disable() : void { - try { - sensor.off(sensor.SensorId.ACCELEROMETER); - } catch (err) { - log.error('Accelerometer off fail, err: %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); - } - } -} \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets