diff --git a/CocosDenshion/ohos/CMakeLists.txt b/CocosDenshion/ohos/CMakeLists.txt index 387846c03902..403b55195f79 100644 --- a/CocosDenshion/ohos/CMakeLists.txt +++ b/CocosDenshion/ohos/CMakeLists.txt @@ -29,7 +29,7 @@ include_directories(${COCOSDENSHION_HEADER} #set(CMAKE_C_FLAGS -Wno-psabi -Wno-absolute-value -Wno-extra) #set(CMAKE_CXX_FLAGS -Wno-overloaded-virtual -Wno-unused-function -Wno-unused-private-field -Wno-reorder-ctor -Wno-unsequenced -Wno-extra) -target_compile_definitions(${PROJECT_NAME} PUBLIC -DHarmonyOS_Next=1) +target_compile_definitions(${PROJECT_NAME} PUBLIC -DHARMONYOS_NEXT=1) target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi -Wno-absolute-value -Wno-extra -Wno-overloaded-virtual -Wno-unused-function -Wno-unused-private-field -Wno-reorder-ctor -Wno-unsequenced -Wno-extra -Wno-unused-command-line-argument -Wno-ignored-qualifiers) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../cocos2dx diff --git a/cocos2dx/CMakeLists.txt b/cocos2dx/CMakeLists.txt index ff135a251112..b1e60c36e7ea 100644 --- a/cocos2dx/CMakeLists.txt +++ b/cocos2dx/CMakeLists.txt @@ -189,7 +189,7 @@ if(OHOS) -Wno-unsequenced -Wno-extra -Wno-c++11-narrowing - -DHarmonyOS_Next + -DHARMONYOS_NEXT ) add_library(${PROJECT_NAME} STATIC ${COCOS2DX_SRC}) @@ -200,7 +200,7 @@ if(OHOS) target_link_libraries(${PROJECT_NAME} PUBLIC ext_png ext_zlib libtiff ext_jpeg) target_include_directories(${PROJECT_NAME} PUBLIC ${COCOS2DX_HEADER_PUBLIC}) - target_compile_definitions(${PROJECT_NAME} PUBLIC -DUSE_FILE32API -DHarmonyOS_Next) + target_compile_definitions(${PROJECT_NAME} PUBLIC -DUSE_FILE32API -DHARMONYOS_NEXT) target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi) find_library( # Sets the name of the path variable. diff --git a/cocos2dx/platform/ohos/napi/helper/NapiHelper.h b/cocos2dx/platform/ohos/napi/helper/NapiHelper.h index 19a04fbcc04a..fac4f86c0926 100644 --- a/cocos2dx/platform/ohos/napi/helper/NapiHelper.h +++ b/cocos2dx/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/cocos2dx/platform/ohos/napi/plugin_manager.cpp b/cocos2dx/platform/ohos/napi/plugin_manager.cpp index dbf8f7e05429..67a18c1f25f6 100644 --- a/cocos2dx/platform/ohos/napi/plugin_manager.cpp +++ b/cocos2dx/platform/ohos/napi/plugin_manager.cpp @@ -73,7 +73,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) { @@ -181,6 +185,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/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index 279e051e6d61..67968c32167a 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -156,7 +156,7 @@ if(OHOS) add_library(${PROJECT_NAME} STATIC ${EXTENSION_SRC}) target_link_libraries(${PROJECT_NAME} box2d chipmunk cocosdenshion) set(CMAKE_C_FLAGS -fexceptions) - target_compile_options(${PROJECT_NAME} PUBLIC -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DHarmonyOS_Next=1) + target_compile_options(${PROJECT_NAME} PUBLIC -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DHARMONYOS_NEXT=1) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ ${CMAKE_CURRENT_SOURCE_DIR}/CCBReader ${CMAKE_CURRENT_SOURCE_DIR}/GUI/CCControlExtension diff --git a/samples/Cpp/TestCpp/CMakeLists.txt b/samples/Cpp/TestCpp/CMakeLists.txt index 233c358c70b5..e57e8a6447e4 100644 --- a/samples/Cpp/TestCpp/CMakeLists.txt +++ b/samples/Cpp/TestCpp/CMakeLists.txt @@ -186,7 +186,7 @@ if(OHOS) ) list(APPEND TEST_COMMON_SRC proj.ohos/entry/src/main/cpp/main.cpp) - add_definitions("-DHarmonyOS_Next") + add_definitions("-DHARMONYOS_NEXT") add_definitions("-DCOCOS2D_DEBUG=3") include_directories(${COCOS2DX_HEADER} diff --git a/samples/Cpp/TestCpp/proj.ohos/.clang-format b/samples/Cpp/TestCpp/proj.ohos/.clang-format index ec09519c8cec..48439ed68073 100644 --- a/samples/Cpp/TestCpp/proj.ohos/.clang-format +++ b/samples/Cpp/TestCpp/proj.ohos/.clang-format @@ -1,7 +1,7 @@ Language: Cpp # BasedOnStyle: LLVM ColumnLimit: 120 -SortIncludes: false +SortIncludes: CaseSensitive TabWidth: 4 IndentWidth: 4 UseTab: Never diff --git a/samples/Cpp/TestCpp/proj.ohos/build-profile.json5 b/samples/Cpp/TestCpp/proj.ohos/build-profile.json5 index f2db5649c7a0..c9fc2b639b3b 100644 --- a/samples/Cpp/TestCpp/proj.ohos/build-profile.json5 +++ b/samples/Cpp/TestCpp/proj.ohos/build-profile.json5 @@ -4,10 +4,8 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", - //指定HarmonyOS应用/服务目标版本。若没有设置,默认为compatibleSdkVersion + "compatibleSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS" - //指定为HarmonyOS/OpenHarmony } ], "signingConfigs": [ diff --git a/samples/Cpp/TestCpp/proj.ohos/entry/build-profile.json5 b/samples/Cpp/TestCpp/proj.ohos/entry/build-profile.json5 index 6292e1c582fe..9813abce456b 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/build-profile.json5 +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..d30c29f2d907 --- /dev/null +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index a89ce067f484..000000000000 --- a/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets index 138b7431c3b6..6f67afe3b443 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/pages/Index.ets b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/pages/Index.ets index 795ca736863a..37958c983136 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/samples/Cpp/TestCpp/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(); @@ -53,7 +53,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); @@ -102,7 +102,7 @@ struct Index { } build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Stack() { XComponent({ id: 'xcomponentId', type: 'surface', @@ -113,9 +113,9 @@ struct Index { .focusOnTouch(true) .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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets similarity index 68% rename from samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts rename to samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets index da097b26edba..69a85edfebfa 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets @@ -1,18 +1,19 @@ -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); const workerContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WORKER_INIT); const inputNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.INPUT_NAPI); +const mouseNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.MOUSE_NAPI); const webViewNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WEBVIEW_NAPI); const videoPlayNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.VIDEOPLAYER_NAPI); const napiContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.NATIVE_API); @@ -23,49 +24,54 @@ 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 "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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 97% rename from samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets index e2ec70b1873e..0447223b1e6a 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/module.json5 b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/module.json5 index b4d1dc43a938..6089e6621968 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/src/main/module.json5 +++ b/samples/Cpp/TestCpp/proj.ohos/entry/src/main/module.json5 @@ -13,7 +13,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", @@ -31,7 +31,7 @@ } ], // https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/module-configuration-file.md/ - "supportWindowMode": ["fullscreen"], + "supportWindowMode": ["fullscreen"], "maxWindowWidth": 1080, "minWindowWidth": 1080, "maxWindowHeight": 720, diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/index.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/index.ets similarity index 96% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/index.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/index.ets index 6c537383d25b..decdc1332d1f 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/index.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/oh-package.json5 b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/oh-package.json5 index 5634eb1fb1d6..cb9e2462c3dc 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/oh-package.json5 @@ -7,7 +7,7 @@ "ohos": { "org": "" }, - "main": "index.ts", + "main": "index.ets", "type": "module", "version": "1.0.0", "dependencies": {} diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 96% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets index 948899c40b50..e0f60659d802 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts +++ b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets @@ -5,6 +5,7 @@ export enum ContextType { WORKER_INIT, NATIVE_API, INPUT_NAPI, + MOUSE_NAPI, WEBVIEW_NAPI, VIDEOPLAYER_NAPI, SENSOR_API diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index c124dd77e416..00b7c24920b4 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/samples/Cpp/TestCpp/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"; + static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow"; + static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag"; + static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag"; } diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets similarity index 98% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets index 37b698c6dce5..5355a89f86fa 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts +++ b/samples/Cpp/TestCpp/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 95% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 112bd864dda5..968ed4cca754 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/samples/Cpp/TestCpp/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets similarity index 96% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets index f07362f91007..ed2049cb2c93 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts +++ b/samples/Cpp/TestCpp/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets similarity index 95% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index 50b7a7e69d00..f4a11e84d250 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets similarity index 96% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets index a4cb05784d57..da86feb832ee 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets similarity index 95% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index 50aa7a9e4a52..d76744a10ea8 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts +++ b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -6,7 +6,6 @@ import { ApplicationManager } from '../system/application/ApplicationManager' import { CocosEditBox } from '../components/editbox/CocosEditBox' import { WebView } from '../components/webview/WebView' import { VideoPlayer } from '../components/videoplayer/VideoPlayer' -import Accelerometer from '../system/sensor/AccelerometerUtils' import Preferences from '../preferences/Preferences' export class NapiHelper { @@ -17,7 +16,6 @@ export class NapiHelper { NapiHelper.registerEditBox(registerFunc); NapiHelper.registerWebView(registerFunc); NapiHelper.registerVideoPlay(registerFunc); - NapiHelper.registerSensor(registerFunc); NapiHelper.registerPreferences(registerFunc); } @@ -31,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); @@ -44,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) { @@ -100,11 +101,6 @@ export class NapiHelper { registerFunc('VideoPlayer.setKeepAspectRatioEnabled', VideoPlayer.setKeepAspectRatioEnabled); } - private static registerSensor(registerFunc : Function) { - registerFunc('Accelerometer.enable', Accelerometer.enable); - registerFunc('Accelerometer.disable', Accelerometer.disable); - } - private static registerPreferences(registerFunc : Function) { registerFunc('Preferences.get', Preferences.get); registerFunc('Preferences.getAll', Preferences.getAll); @@ -114,5 +110,4 @@ export class NapiHelper { registerFunc('Preferences.flush', Preferences.flush); registerFunc('Preferences.clear', Preferences.clear); } - } diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets similarity index 68% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets index dfc0e633e3f2..4b64aff5551d 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts +++ b/samples/Cpp/TestCpp/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 { - // Get Preferences instance through preferencesName - 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; } - /* - * Retrieve the specified Preferences instance from the cache using preferencesName, and if the Preferences instance has a corresponding persistent file, delete its persistent file at the same time. Use Promise asynchronous callbacks. - * After calling this interface, it is not recommended to use the old Preferences instance for data operations, otherwise data consistency issues may occur. The Preferences instance should be set to null, and the system will uniformly recycle it. - */ static deletePreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -41,12 +35,6 @@ export default class Preferences { } } - /* - * Retrieve the specified Preferences instance from the cache using preferencesName and use Promise asynchronous callback. - * After the application first calls the getPreferences interface to obtain a Preferences instance, the instance will be cached and will not be read from the persistent file again when getPreferences is retrieved later, - * Retrieve the Preferences instance directly from the cache. After calling this interface to remove the instance from the cache, getPreferences will re read the persistent file and generate a new Preferences instance. - * After calling this interface, it is not recommended to use the old Preferences instance for data operations, otherwise data consistency issues may occur. The Preferences instance should be set to null, and the system will uniformly recycle it. - */ static removePreferencesFromCache(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -59,7 +47,6 @@ export default class Preferences { } } - // Retrieve the value corresponding to the key from the cached Preferences instance. If the value is null or of a non default type, return the default data 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 { } } - // Write data to the cached Preferences instance, which can be persisted through flushing static put(key: string, value: data_preferences.ValueType): void { if (preferences === null) { Preferences.getPreferences(); @@ -92,7 +78,6 @@ export default class Preferences { } } - // Retrieve all key value data from the cached Preferences instance. static getAll(): string | undefined { if (preferences === null) { Preferences.getPreferences(); @@ -111,7 +96,6 @@ export default class Preferences { } } - // Check if the cached Preferences instance contains a stored key value pair named the given Key static has(key: string): boolean { if (preferences === null) { Preferences.getPreferences(); @@ -133,7 +117,6 @@ export default class Preferences { } } - // Deleting the stored key value pair named the given Key from the cached Preferences instance can be persisted by flushing the Preferences instance static delete(key: string): void { if (preferences === null) { Preferences.getPreferences(); @@ -149,7 +132,6 @@ export default class Preferences { } } - // Asynchronous storage of data from cached Preferences instances to persistent files of user preferences, using Promise asynchronous callbacks. static flush(): void { if (preferences === null) { Preferences.getPreferences(); @@ -159,7 +141,6 @@ export default class Preferences { }); } - // Clearing all data from cached Preferences instances can be achieved by persistently flushing the Preferences instance and using Promise asynchronous callbacks. static clear(): void { if (preferences === null) { Preferences.getPreferences(); @@ -175,7 +156,6 @@ export default class Preferences { } - // After the subscription data changes and the value of the subscription key changes, a callback is triggered after executing the flush method. static onChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -192,7 +172,6 @@ export default class Preferences { } } - // Unsubscribe from data changes. static offChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -210,9 +189,7 @@ export default class Preferences { } } -// Due to the absence of Object. keys in ArkTS and the inability to use for in... -//If ArkTS issues are reported, please separate this method into a TS file and expose it, and introduce it into the ETS file that needs to be used function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; -} +} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 100% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets similarity index 76% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets index ea373af90811..2c20f240b2ce 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 99% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets index ffe0826a5f7a..ab8016646c7d 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts +++ b/samples/Cpp/TestCpp/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 67% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index f27a3cbdcf19..239fe6990c0d 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/samples/Cpp/TestCpp/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; } + + 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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts deleted file mode 100644 index 0e8c8fa1154b..000000000000 --- a/samples/Cpp/TestCpp/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))) ?? ''); - } - } -} diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts b/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets similarity index 100% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts rename to samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets diff --git a/samples/Lua/TestLua/proj.ohos/build-profile.json5 b/samples/Lua/TestLua/proj.ohos/build-profile.json5 index 816ffa606c27..f37062fe9107 100644 --- a/samples/Lua/TestLua/proj.ohos/build-profile.json5 +++ b/samples/Lua/TestLua/proj.ohos/build-profile.json5 @@ -4,8 +4,8 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", - "runtimeOS": "HarmonyOS", + "compatibleSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS" } ], "signingConfigs": [ diff --git a/samples/Lua/TestLua/proj.ohos/entry/build-profile.json5 b/samples/Lua/TestLua/proj.ohos/entry/build-profile.json5 index ad31dbad1d17..d1788fe04b85 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/build-profile.json5 +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/entry/src/main/cpp/CMakeLists.txt b/samples/Lua/TestLua/proj.ohos/entry/src/main/cpp/CMakeLists.txt index bf147db7a0b8..eb42daf89c6b 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/cpp/CMakeLists.txt +++ b/samples/Lua/TestLua/proj.ohos/entry/src/main/cpp/CMakeLists.txt @@ -14,7 +14,7 @@ add_definitions("-DCOCOS2D_DEBUG=3") set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_definitions(-DHarmonyOS_Next) +add_definitions(-DHARMONYOS_NEXT) find_library( # Sets the name of the path variable. EGL-lib diff --git a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..d30c29f2d907 --- /dev/null +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index a89ce067f484..000000000000 --- a/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets index 138b7431c3b6..6f67afe3b443 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosWebview.ets b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosWebview.ets index d68969bc065f..878f5d2823b6 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosWebview.ets +++ b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/components/CocosWebview.ets @@ -37,4 +37,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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/pages/Index.ets b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/pages/Index.ets index 795ca736863a..37958c983136 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/samples/Lua/TestLua/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(); @@ -53,7 +53,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); @@ -102,7 +102,7 @@ struct Index { } build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Stack() { XComponent({ id: 'xcomponentId', type: 'surface', @@ -113,9 +113,9 @@ struct Index { .focusOnTouch(true) .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/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets similarity index 68% rename from samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts rename to samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets index da097b26edba..69a85edfebfa 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets @@ -1,18 +1,19 @@ -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); const workerContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WORKER_INIT); const inputNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.INPUT_NAPI); +const mouseNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.MOUSE_NAPI); const webViewNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WEBVIEW_NAPI); const videoPlayNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.VIDEOPLAYER_NAPI); const napiContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.NATIVE_API); @@ -23,49 +24,54 @@ 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 "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/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 97% rename from samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to samples/Lua/TestLua/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets index e2ec70b1873e..0447223b1e6a 100644 --- a/samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/entry/src/main/module.json5 b/samples/Lua/TestLua/proj.ohos/entry/src/main/module.json5 index 0dfabec6fc92..6089e6621968 100644 --- a/samples/Lua/TestLua/proj.ohos/entry/src/main/module.json5 +++ b/samples/Lua/TestLua/proj.ohos/entry/src/main/module.json5 @@ -13,7 +13,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", diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/index.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/index.ets similarity index 96% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/index.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/index.ets index 6c537383d25b..decdc1332d1f 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/index.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/oh-package.json5 b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/oh-package.json5 index 5634eb1fb1d6..cb9e2462c3dc 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/oh-package.json5 @@ -7,7 +7,7 @@ "ohos": { "org": "" }, - "main": "index.ts", + "main": "index.ets", "type": "module", "version": "1.0.0", "dependencies": {} diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 96% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets index e6f5127f8acc..e0f60659d802 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts +++ b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets @@ -5,6 +5,7 @@ export enum ContextType { WORKER_INIT, NATIVE_API, INPUT_NAPI, + MOUSE_NAPI, WEBVIEW_NAPI, VIDEOPLAYER_NAPI, SENSOR_API @@ -18,4 +19,4 @@ export class AppPermissionConsts { static readonly REQUEST_CODE_REQUIRED: number = 1000; static readonly REQUEST_CODE_CUSTOM: number = 1001; -} +} \ No newline at end of file diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b2664e..00b7c24920b4 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/samples/Lua/TestLua/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets similarity index 98% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets index 37b698c6dce5..5355a89f86fa 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts +++ b/samples/Lua/TestLua/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 95% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 112bd864dda5..968ed4cca754 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/samples/Lua/TestLua/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/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets similarity index 96% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets index ca4d8e710922..ed2049cb2c93 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts +++ b/samples/Lua/TestLua/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; } @@ -108,4 +109,4 @@ export class CocosEditBox { let editBoxMsgEntity: EditBoxMsgEntity = new EditBoxMsgEntity(CocosEditBox.MODULE_NAME, 'hideAllEditBox'); CocosEditBox.workerPort.postMessage(editBoxMsgEntity); } -} +} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets similarity index 95% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index 50b7a7e69d00..f4a11e84d250 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets similarity index 96% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets index 0d00f332afe0..da86feb832ee 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts +++ b/samples/Lua/TestLua/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; } @@ -105,4 +106,4 @@ export class WebView { webViewMsgEntity.scalesPageToFit = scalesPageToFit; WebView.workerPort.postMessage(webViewMsgEntity); } -} \ No newline at end of file +} diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets similarity index 95% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index 50aa7a9e4a52..d76744a10ea8 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts +++ b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -6,7 +6,6 @@ import { ApplicationManager } from '../system/application/ApplicationManager' import { CocosEditBox } from '../components/editbox/CocosEditBox' import { WebView } from '../components/webview/WebView' import { VideoPlayer } from '../components/videoplayer/VideoPlayer' -import Accelerometer from '../system/sensor/AccelerometerUtils' import Preferences from '../preferences/Preferences' export class NapiHelper { @@ -17,7 +16,6 @@ export class NapiHelper { NapiHelper.registerEditBox(registerFunc); NapiHelper.registerWebView(registerFunc); NapiHelper.registerVideoPlay(registerFunc); - NapiHelper.registerSensor(registerFunc); NapiHelper.registerPreferences(registerFunc); } @@ -31,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); @@ -44,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) { @@ -100,11 +101,6 @@ export class NapiHelper { registerFunc('VideoPlayer.setKeepAspectRatioEnabled', VideoPlayer.setKeepAspectRatioEnabled); } - private static registerSensor(registerFunc : Function) { - registerFunc('Accelerometer.enable', Accelerometer.enable); - registerFunc('Accelerometer.disable', Accelerometer.disable); - } - private static registerPreferences(registerFunc : Function) { registerFunc('Preferences.get', Preferences.get); registerFunc('Preferences.getAll', Preferences.getAll); @@ -114,5 +110,4 @@ export class NapiHelper { registerFunc('Preferences.flush', Preferences.flush); registerFunc('Preferences.clear', Preferences.clear); } - } diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets similarity index 68% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets index dfc0e633e3f2..4b64aff5551d 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts +++ b/samples/Lua/TestLua/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 { - // Get Preferences instance through preferencesName - 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; } - /* - * Retrieve the specified Preferences instance from the cache using preferencesName, and if the Preferences instance has a corresponding persistent file, delete its persistent file at the same time. Use Promise asynchronous callbacks. - * After calling this interface, it is not recommended to use the old Preferences instance for data operations, otherwise data consistency issues may occur. The Preferences instance should be set to null, and the system will uniformly recycle it. - */ static deletePreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -41,12 +35,6 @@ export default class Preferences { } } - /* - * Retrieve the specified Preferences instance from the cache using preferencesName and use Promise asynchronous callback. - * After the application first calls the getPreferences interface to obtain a Preferences instance, the instance will be cached and will not be read from the persistent file again when getPreferences is retrieved later, - * Retrieve the Preferences instance directly from the cache. After calling this interface to remove the instance from the cache, getPreferences will re read the persistent file and generate a new Preferences instance. - * After calling this interface, it is not recommended to use the old Preferences instance for data operations, otherwise data consistency issues may occur. The Preferences instance should be set to null, and the system will uniformly recycle it. - */ static removePreferencesFromCache(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -59,7 +47,6 @@ export default class Preferences { } } - // Retrieve the value corresponding to the key from the cached Preferences instance. If the value is null or of a non default type, return the default data 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 { } } - // Write data to the cached Preferences instance, which can be persisted through flushing static put(key: string, value: data_preferences.ValueType): void { if (preferences === null) { Preferences.getPreferences(); @@ -92,7 +78,6 @@ export default class Preferences { } } - // Retrieve all key value data from the cached Preferences instance. static getAll(): string | undefined { if (preferences === null) { Preferences.getPreferences(); @@ -111,7 +96,6 @@ export default class Preferences { } } - // Check if the cached Preferences instance contains a stored key value pair named the given Key static has(key: string): boolean { if (preferences === null) { Preferences.getPreferences(); @@ -133,7 +117,6 @@ export default class Preferences { } } - // Deleting the stored key value pair named the given Key from the cached Preferences instance can be persisted by flushing the Preferences instance static delete(key: string): void { if (preferences === null) { Preferences.getPreferences(); @@ -149,7 +132,6 @@ export default class Preferences { } } - // Asynchronous storage of data from cached Preferences instances to persistent files of user preferences, using Promise asynchronous callbacks. static flush(): void { if (preferences === null) { Preferences.getPreferences(); @@ -159,7 +141,6 @@ export default class Preferences { }); } - // Clearing all data from cached Preferences instances can be achieved by persistently flushing the Preferences instance and using Promise asynchronous callbacks. static clear(): void { if (preferences === null) { Preferences.getPreferences(); @@ -175,7 +156,6 @@ export default class Preferences { } - // After the subscription data changes and the value of the subscription key changes, a callback is triggered after executing the flush method. static onChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -192,7 +172,6 @@ export default class Preferences { } } - // Unsubscribe from data changes. static offChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -210,9 +189,7 @@ export default class Preferences { } } -// Due to the absence of Object. keys in ArkTS and the inability to use for in... -//If ArkTS issues are reported, please separate this method into a TS file and expose it, and introduce it into the ETS file that needs to be used function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; -} +} \ No newline at end of file diff --git a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 100% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets similarity index 76% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets index ea373af90811..2c20f240b2ce 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 99% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets index ffe0826a5f7a..ab8016646c7d 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 67% rename from samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584810..239fe6990c0d 100644 --- a/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/samples/Lua/TestLua/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/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts deleted file mode 100644 index 0e8c8fa1154b..000000000000 --- a/samples/Lua/TestLua/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))) ?? ''); - } - } -} diff --git a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets similarity index 99% rename from samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts rename to samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets index bbfbb8588bc6..4e3d20ec55e1 100644 --- a/samples/Cpp/TestCpp/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts +++ b/samples/Lua/TestLua/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets @@ -5,4 +5,4 @@ export default class StringUtils { public static getWidth(text: string, fontSize: number, fontWeight: number): number { return measure.measureText({ textContent: text, fontSize: fontSize + 'px', fontWeight: fontWeight }); } -} +} \ No newline at end of file