diff --git a/cocos/platform/ohos/CCGLViewImpl-ohos.cpp b/cocos/platform/ohos/CCGLViewImpl-ohos.cpp index aed5adfd1bcc..f871086aeaf2 100644 --- a/cocos/platform/ohos/CCGLViewImpl-ohos.cpp +++ b/cocos/platform/ohos/CCGLViewImpl-ohos.cpp @@ -110,110 +110,29 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) { } Rect GLViewImpl::getSafeAreaRect() const { - Rect safeAreaRect = GLView::getSafeAreaRect(); - float deviceAspectRatio = 0; - if(safeAreaRect.size.height > safeAreaRect.size.width) { - deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width; - } else { - deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height; - } - - float marginX = DEFAULT_MARGIN_OHOS / _scaleX; - float marginY = DEFAULT_MARGIN_OHOS / _scaleY; - bool isScreenRound; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.isRoundScreen")) { - isScreenRound = function->Invoke(); + Rect safeAreaRect1; + int left; + if (auto getSafeAreaLeft = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaLeft")) { + left = getSafeAreaLeft->Invoke(); } - bool hasSoftKeys; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.hasSoftKeys")) { - hasSoftKeys = function->Invoke(); + int top; + if (auto getSafeAreaTop = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaTop")) { + top = getSafeAreaTop->Invoke(); } - bool isCutoutEnabled; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.isCutoutEnable")) { - isCutoutEnabled = function->Invoke(); - } - - if(isScreenRound) { - // edge screen - if(safeAreaRect.size.width < safeAreaRect.size.height) { - safeAreaRect.origin.y += marginY * 2.f; - safeAreaRect.size.height -= (marginY * 2.f); - - safeAreaRect.origin.x += marginX; - safeAreaRect.size.width -= (marginX * 2.f); - } else { - safeAreaRect.origin.y += marginY; - safeAreaRect.size.height -= (marginY * 2.f); - - // landscape: no changes with X-coords - } - } else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_OHOS) { - // almost all devices on the market have round corners - float bottomMarginIfPortrait = 0; - if(hasSoftKeys) { - bottomMarginIfPortrait = marginY * 2.f; - } - - if(safeAreaRect.size.width < safeAreaRect.size.height) { - // portrait: double margin space if device has soft menu - safeAreaRect.origin.y += bottomMarginIfPortrait; - safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY); - } else { - // landscape: ignore double margin at the bottom in any cases - // prepare signle margin for round corners - safeAreaRect.origin.y += marginY; - safeAreaRect.size.height -= (marginY * 2.f); - } - } else { - if(hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height)) { - // portrait: preserve only for soft system menu - safeAreaRect.origin.y += marginY * 2.f; - safeAreaRect.size.height -= (marginY * 2.f); - } + int width; + if (auto getSafeAreaWidth = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaWidth")) { + width = getSafeAreaWidth->Invoke(); } - - if (isCutoutEnabled) { - // screen with enabled cutout area - - int orientation; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getOrientation")) { - orientation = function->Invoke(); - } - - if(static_cast(GLViewImpl::Orientation::PORTRAIT) == orientation) { - int result; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutHeight")) { - result = function->Invoke(); - } - double height = result / _scaleY; - safeAreaRect.origin.y += height; - safeAreaRect.size.height -= height; - } else if(static_cast(GLViewImpl::Orientation::PORTRAIT_INVERTED) == orientation) { - int result; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutHeight")) { - result = function->Invoke(); - } - double height = result / _scaleY; - safeAreaRect.size.height -= height; - } else if(static_cast(GLViewImpl::Orientation::LANDSCAPE) == orientation) { - int result; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutWidth")) { - result = function->Invoke(); - } - double width = result / _scaleX; - safeAreaRect.size.width -= width; - } else if(static_cast(GLViewImpl::Orientation::LANDSCAPE_INVERTED) == orientation) { - int result; - if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutWidth")) { - result = function->Invoke(); - } - double width = result / _scaleX; - safeAreaRect.origin.x += width; - safeAreaRect.size.width -= width; - } + int height; + if (auto getSafeAreaHeight = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaHeight")) { + height = getSafeAreaHeight->Invoke(); } - - return safeAreaRect; + safeAreaRect1.origin.x = left / _scaleX; + safeAreaRect1.origin.y = top / _scaleY; + safeAreaRect1.size.width = width / _scaleX; + safeAreaRect1.size.height = height / _scaleX; + OHOS_LOGD("GLViewImpl getsafeAreaRect1, x:%{public}f, y:%{public}f, width:%{public}f, height:%{public}f", safeAreaRect1.origin.x, safeAreaRect1.origin.y, safeAreaRect1.size.width, safeAreaRect1.size.height); + return safeAreaRect1; } NS_CC_END diff --git a/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp b/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp index bb3740677c43..baedaaa307c1 100644 --- a/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp +++ b/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp @@ -13,9 +13,6 @@ napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_i * If you have more information that can be obtained asynchronously, add it here. */ napi_value Js_Cocos2dxHelper::initAsyncInfo(napi_env env, napi_callback_info info) { - if (auto initScreenInfo = aki::JSBind::GetJSFunction("DeviceUtils.initScreenInfo")) { - initScreenInfo->Invoke(); - } return nullptr; } diff --git a/cocos/platform/ohos/napi/render/plugin_render.cpp b/cocos/platform/ohos/napi/render/plugin_render.cpp index f481008d260c..876b28ebd421 100644 --- a/cocos/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/platform/ohos/napi/render/plugin_render.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -32,7 +33,7 @@ float mousePositionY = -1; bool isMouseLeftActive = false; double scrollDistance = 0; -std::unordered_map ohKeyMap = { +const std::unordered_map ohKeyMap = { {KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE}, {KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE}, {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, @@ -62,7 +63,23 @@ std::unordered_map ohKeyMap = { {KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP}, {KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT}, {KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT}, - {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE} + {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}, + {KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK}, + {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, + {KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT}, + {KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS}, + {KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU}, + {KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE}, + {KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME}, + {KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END}, + {KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP}, + {KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN}, + {KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS}, + {KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS}, + {KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY}, + {KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE}, + {KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER} + }; cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode) @@ -82,8 +99,9 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z) { // A - Z return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A)); - } - return cocos2d::EventKeyboard::KeyCode(ohKeyCode); + } + OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode); + return cocos2d::EventKeyboard::KeyCode::KEY_NONE; } void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) diff --git a/tests/cpp-tests/proj.ohos/build-profile.json5 b/tests/cpp-tests/proj.ohos/build-profile.json5 index f935f419aff4..d7172063f075 100644 --- a/tests/cpp-tests/proj.ohos/build-profile.json5 +++ b/tests/cpp-tests/proj.ohos/build-profile.json5 @@ -4,7 +4,7 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", + "compatibleSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS" } ], diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..de5ea8a9706e --- /dev/null +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets @@ -0,0 +1,117 @@ +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 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); + +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 ' + JSON.stringify(err)); + }); + + try { + DeviceUtils.calculateSafeArea(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(data.area, mainWindow.getWindowProperties().windowRect); + }); + } catch (exception) { + console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { + let stageEventType: window.WindowStageEventType = data; + switch (stageEventType) { + case window.WindowStageEventType.RESUMED: + console.info('[LIFECYCLE-App] onShow_RESUMED') + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + break; + case window.WindowStageEventType.PAUSED: + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + console.info('[LIFECYCLE-App] onHide_PAUSED') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + break; + default: + break; + } + }); + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + } + + onForeground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + // Ability has brought to foreground + console.info('[LIFECYCLE-App] onShow') + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + } + + onBackground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + // Ability has back to background + console.info('[LIFECYCLE-App] onHide') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + } +}; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index 9ddbf56a4a7e..000000000000 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts +++ /dev/null @@ -1,77 +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); - console.info('[LIFECYCLE-App] onCreate') - } - - onDestroy() { - nativeAppLifecycle.onDestroy(); - console.info('[LIFECYCLE-App] onDestroy') - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - return; - } - rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); - rawFileUtils.writablePathInit(this.context.filesDir); - }); - - windowStage.getMainWindow().then((windowIns: window.Window) => { - // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. - let systemBarPromise = windowIns.setWindowSystemBarEnable([]); - // Whether the window layout is displayed in full screen mode - let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); - // Sets whether the screen is always on. - let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); - Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { - console.info('Succeeded in setting the window'); - }).catch((err) => { - console.error('Failed to set the window, cause ' + JSON.stringify(err)); - }); - }) - - windowStage.on("windowStageEvent", (data) => { - let stageEventType: window.WindowStageEventType = data; - switch (stageEventType) { - case window.WindowStageEventType.RESUMED: - nativeAppLifecycle.onShow(); - break; - case window.WindowStageEventType.PAUSED: - nativeAppLifecycle.onHide(); - break; - default: - break; - } - }); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - } - - onForeground() { - // Ability has brought to foreground - console.info('[LIFECYCLE-App] onShow') - nativeAppLifecycle.onShow(); - } - - onBackground() { - // Ability has back to background - console.info('[LIFECYCLE-App] onDestroy') - nativeAppLifecycle.onHide(); - } -}; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index fd928773171d..02edd2a2e900 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -95,8 +95,7 @@ struct Index { }) } catch (error) { console.error(`showDialog args error code is ${error.code}, message is ${error.message}`); - } - ; + }; // If disable system exit needed, remove comment "return true" return true; } diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets new file mode 100644 index 000000000000..d8ffb5cca941 --- /dev/null +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets @@ -0,0 +1,12 @@ +export class WorkerDataEntity { + type?: string; + context?: Object; + viewTag?: number; + text?: string; + eventType?: string; + scrollY?: number; + url?: string; + event?: number; + duration?: number; + currentTime?: number; +} \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts deleted file mode 100644 index 7bd635cdaede..000000000000 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts +++ /dev/null @@ -1,12 +0,0 @@ -export class WorkerDataEntity { - type: string; - context: Object; - viewTag: number; - text: string; - eventType: string; - scrollY: number; - url: string; - event: number; - duration: number; - currentTime: number; -} \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 b/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 index 07e1e13b06f7..953071f73eec 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 +++ b/tests/cpp-tests/proj.ohos/entry/src/main/module.json5 @@ -15,7 +15,7 @@ "abilities": [ { "name": "MainAbility", - "srcEntry": "./ets/MainAbility/MainAbility.ts", + "srcEntry": "./ets/MainAbility/MainAbility.ets", "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ets similarity index 77% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/index.ets index 1e09480968f8..5d70c0bc983e 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ets @@ -1,3 +1,4 @@ export { ContextType, Constants } from './src/main/ets/common/Constants' export { GlobalContext,GlobalContextConstants } from './src/main/ets/common/GlobalContext' export { TextInputDialogEntity } from './src/main/ets/entity/TextInputDialogEntity' +export { DeviceUtils } from './src/main/ets/system/device/DeviceUtils' diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 b/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 index 8c935e9b2a00..d00806d42de9 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/oh-package.json5 @@ -4,7 +4,7 @@ "author": "", "name": "libsyscapabilities", "description": "Please describe the basic information.", - "main": "index.ts", + "main": "index.ets", "version": "1.0.0", "dynamicDependencies": {}, "dependencies": {} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b2664e..00b7c24920b4 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts @@ -22,4 +22,7 @@ export class GlobalContextConstants { static readonly COCOS2DX_ABILITY_CONTEXT = "Cocos2dx.abilityContext"; static readonly COCOS2DX_ABILITY_WANT = "Cocos2dx.abilityWant"; static readonly COCOS2DX_WEB_RESULT= "Cocos2dx.webResult"; -} \ No newline at end of file + static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow"; + static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag"; + static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag"; +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 97% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 4ea4f2e2ea7d..dfb21b2f20cb 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets @@ -17,7 +17,7 @@ export class Dialog { color: '#000000' }, ], - success: function(data) { + success: (data) => { log.debug("handling callback, data:%{public}s", data); } }); diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index ff1b8b93e464..ab52098623a5 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -79,7 +79,7 @@ export class VideoPlayer { removeIndex = index; } }); - GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).get(viewTag).controller.requestFullscreen(false); //4.x已修复 + GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).get(viewTag).controller.requestFullscreen(false); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_ARRAY).splice(removeIndex, 1); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).delete(viewTag); } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets @@ -0,0 +1,23 @@ +interface ResultObjectInterface { + "errCode": number; + "errMsg": string; + "data": string, +} + +export class Result { + public static success(data: string){ + return ({ + "errCode": 0, + "errMsg": "", + "data": data, + }as ResultObjectInterface); + } + + public static error(errCode:number, errMsg:string){ + return ({ + "errCode": errCode, + "errMsg": errMsg, + "data": "", + } as ResultObjectInterface); + } +}; \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts +++ /dev/null @@ -1,16 +0,0 @@ -export class Result { - public static success(data){ - return { - "errCode": 0, - "errMsg": "", - "data": data, - }; - } - - public static error(errCode, errMsg) { - return { - "errCode": errCode, - "errMsg": errMsg, - }; - } -}; \ No newline at end of file diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index f0f8cf0db29f..13ca24f0fc08 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -108,5 +108,10 @@ export class NapiHelper { nativerender.JSBind.bindFunction('DiaLog.showDialog', Dialog.showDialog); nativerender.JSBind.bindFunction('DiaLog.showTextInputDialog', Dialog.showTextInputDialog); nativerender.JSBind.bindFunction('DiaLog.hideTextInputDialog', Dialog.hideTextInputDialog); + + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaLeft", DeviceUtils.getSafeAreaLeft); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaTop", DeviceUtils.getSafeAreaTop); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaWidth", DeviceUtils.getSafeAreaWidth); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaHeight", DeviceUtils.getSafeAreaHeight); } } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 81% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets index 888a840af5a8..0195fff7bb3b 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets @@ -3,9 +3,16 @@ import type common from '@ohos.app.ability.common'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import {Result} from "../../entity/Result" import Logger from '../../utils/Logger' +import { BusinessError } from '@kit.BasicServicesKit'; let log: Logger = new Logger(0x0001, "JumpManager"); +interface WantInfoObjectInterface { + 'action': string; + 'entities': string[]; + 'uri': string; +} + export class JumpManager { static MODULE_NAME: string = 'JumpManager'; @@ -18,14 +25,14 @@ export class JumpManager { static openUrl(url: string) : void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); - let wantInfo = { + let wantInfo: WantInfoObjectInterface = { '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.info('%{public}s', JSON.stringify(Result.success(""))); + }).catch((err:BusinessError) => { log.error('openUrl : err : %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); }); } diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 100% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 69% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584810..f0de00cb18d4 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets @@ -5,6 +5,8 @@ import Logger from '../../utils/Logger'; import window from '@ohos.window'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import { Rect } from '@ohos.application.AccessibilityExtensionAbility'; +import { JSON } 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,39 @@ export class DeviceUtils { return DeviceUtils._cutoutLeft + DeviceUtils._cutoutWidth; } -} \ No newline at end of file + + static calculateSafeArea(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; + DeviceUtils.safeAreaInWorker = safeArea; + } + + static getSafeAreaLeft() : number { + return DeviceUtils.safeAreaInWorker.left; + } + + static getSafeAreaTop() : number { + return DeviceUtils.safeAreaInWorker.top; + } + + static getSafeAreaWidth() : number { + return DeviceUtils.safeAreaInWorker.width; + } + + static getSafeAreaHeight() : number { + return DeviceUtils.safeAreaInWorker.height; + } +} diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets similarity index 82% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts rename to tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets index 56309439f011..f51791f8adc3 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets @@ -1,4 +1,5 @@ import { getContext } from "libnativerender.so"; +import { CPPFunctions } from "libnativerender.so"; import { ContextType } from "../../common/Constants" import sensor from '@ohos.sensor'; import display from '@ohos.display'; @@ -7,7 +8,7 @@ import Logger from '../../utils/Logger' let log: Logger = new Logger(0x0001, "AccelerometerUtils"); -const accUtils = getContext(ContextType.SENSOR_API); +const accUtils: CPPFunctions= getContext(ContextType.SENSOR_API); export default class Accelerometer { @@ -22,7 +23,7 @@ export default class Accelerometer { /* 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) { + sensor.on(sensor.SensorId.ACCELEROMETER, (data) => { let rotation = display.getDefaultDisplaySync().rotation; if (rotation === 0) { // Display device screen rotation 0° @@ -34,10 +35,11 @@ export default class Accelerometer { // 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); + // 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))); + log.error('Accelerometer init fail, err: %{public}s', + JSON.stringify(Result.error(-1, 'unsupported rotation: ' + rotation))); } }, { interval: intervalTime }); } catch (err) { diff --git a/tests/lua-tests/project/proj.ohos/AppScope/resources/base/element/string.json b/tests/lua-tests/project/proj.ohos/AppScope/resources/base/element/string.json index 7a36a2897ac9..2cb1aaa49e28 100644 --- a/tests/lua-tests/project/proj.ohos/AppScope/resources/base/element/string.json +++ b/tests/lua-tests/project/proj.ohos/AppScope/resources/base/element/string.json @@ -2,7 +2,7 @@ "string": [ { "name": "app_name", - "value": "proj.ohos" + "value": "4.0_lua_tests" } ] } diff --git a/tests/lua-tests/project/proj.ohos/build-profile.json5 b/tests/lua-tests/project/proj.ohos/build-profile.json5 index e2fb7a301b1c..3d936f5ce9c5 100644 --- a/tests/lua-tests/project/proj.ohos/build-profile.json5 +++ b/tests/lua-tests/project/proj.ohos/build-profile.json5 @@ -4,7 +4,7 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", + "compatibleSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS" } ], diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts index 039f1682c3da..34e73b50d722 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts @@ -12,7 +12,6 @@ export interface CPPFunctions { writablePathInit: (writePath: string) => void; workerInit: () => void; nativeEngineStart: () => void; - registerFunction: () => void; initAsyncInfo: () => void; mouseWheelCB: (eventType: string, scrollY : number) => void; editBoxOnFocusCB: (viewTag: number) => void; diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets new file mode 100644 index 000000000000..de5ea8a9706e --- /dev/null +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets @@ -0,0 +1,117 @@ +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 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); + +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 ' + JSON.stringify(err)); + }); + + try { + DeviceUtils.calculateSafeArea(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(data.area, mainWindow.getWindowProperties().windowRect); + }); + } catch (exception) { + console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { + let stageEventType: window.WindowStageEventType = data; + switch (stageEventType) { + case window.WindowStageEventType.RESUMED: + console.info('[LIFECYCLE-App] onShow_RESUMED') + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + break; + case window.WindowStageEventType.PAUSED: + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + console.info('[LIFECYCLE-App] onHide_PAUSED') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + break; + default: + break; + } + }); + } + + onWindowStageDestroy() { + // Main window is destroyed, release UI related resources + } + + onForeground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ + // Ability has brought to foreground + console.info('[LIFECYCLE-App] onShow') + nativeAppLifecycle.onShow(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); + } + } + + onBackground() { + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ + // Ability has back to background + console.info('[LIFECYCLE-App] onHide') + nativeAppLifecycle.onHide(); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); + } + } +}; diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts deleted file mode 100644 index 9ddbf56a4a7e..000000000000 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts +++ /dev/null @@ -1,77 +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); - console.info('[LIFECYCLE-App] onCreate') - } - - onDestroy() { - nativeAppLifecycle.onDestroy(); - console.info('[LIFECYCLE-App] onDestroy') - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - return; - } - rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); - rawFileUtils.writablePathInit(this.context.filesDir); - }); - - windowStage.getMainWindow().then((windowIns: window.Window) => { - // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. - let systemBarPromise = windowIns.setWindowSystemBarEnable([]); - // Whether the window layout is displayed in full screen mode - let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); - // Sets whether the screen is always on. - let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); - Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { - console.info('Succeeded in setting the window'); - }).catch((err) => { - console.error('Failed to set the window, cause ' + JSON.stringify(err)); - }); - }) - - windowStage.on("windowStageEvent", (data) => { - let stageEventType: window.WindowStageEventType = data; - switch (stageEventType) { - case window.WindowStageEventType.RESUMED: - nativeAppLifecycle.onShow(); - break; - case window.WindowStageEventType.PAUSED: - nativeAppLifecycle.onHide(); - break; - default: - break; - } - }); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - } - - onForeground() { - // Ability has brought to foreground - console.info('[LIFECYCLE-App] onShow') - nativeAppLifecycle.onShow(); - } - - onBackground() { - // Ability has back to background - console.info('[LIFECYCLE-App] onDestroy') - nativeAppLifecycle.onHide(); - } -}; diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets index fd928773171d..02edd2a2e900 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -95,8 +95,7 @@ struct Index { }) } catch (error) { console.error(`showDialog args error code is ${error.code}, message is ${error.message}`); - } - ; + }; // If disable system exit needed, remove comment "return true" return true; } diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets new file mode 100644 index 000000000000..d8ffb5cca941 --- /dev/null +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ets @@ -0,0 +1,12 @@ +export class WorkerDataEntity { + type?: string; + context?: Object; + viewTag?: number; + text?: string; + eventType?: string; + scrollY?: number; + url?: string; + event?: number; + duration?: number; + currentTime?: number; +} \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts deleted file mode 100644 index 7bd635cdaede..000000000000 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerDataEntity.ts +++ /dev/null @@ -1,12 +0,0 @@ -export class WorkerDataEntity { - type: string; - context: Object; - viewTag: number; - text: string; - eventType: string; - scrollY: number; - url: string; - event: number; - duration: number; - currentTime: number; -} \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to tests/lua-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets diff --git a/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 b/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 index f5bd2f320913..0f737153f77f 100644 --- a/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 +++ b/tests/lua-tests/project/proj.ohos/entry/src/main/module.json5 @@ -15,7 +15,7 @@ "abilities": [ { "name": "MainAbility", - "srcEntry": "./ets/MainAbility/MainAbility.ts", + "srcEntry": "./ets/MainAbility/MainAbility.ets", "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets similarity index 71% rename from tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets index 9f414360d7af..5d70c0bc983e 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/index.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/index.ets @@ -1,3 +1,4 @@ export { ContextType, Constants } from './src/main/ets/common/Constants' export { GlobalContext,GlobalContextConstants } from './src/main/ets/common/GlobalContext' -export { TextInputDialogEntity } from './src/main/ets/entity/TextInputDialogEntity' \ No newline at end of file +export { TextInputDialogEntity } from './src/main/ets/entity/TextInputDialogEntity' +export { DeviceUtils } from './src/main/ets/system/device/DeviceUtils' diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 b/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 index 8c935e9b2a00..d00806d42de9 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 @@ -4,7 +4,7 @@ "author": "", "name": "libsyscapabilities", "description": "Please describe the basic information.", - "main": "index.ts", + "main": "index.ets", "version": "1.0.0", "dynamicDependencies": {}, "dependencies": {} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b2664e..00b7c24920b4 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts @@ -22,4 +22,7 @@ export class GlobalContextConstants { static readonly COCOS2DX_ABILITY_CONTEXT = "Cocos2dx.abilityContext"; static readonly COCOS2DX_ABILITY_WANT = "Cocos2dx.abilityWant"; static readonly COCOS2DX_WEB_RESULT= "Cocos2dx.webResult"; -} \ No newline at end of file + static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow"; + static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag"; + static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag"; +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 97% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index b15f07892047..dfb21b2f20cb 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets @@ -17,7 +17,7 @@ export class Dialog { color: '#000000' }, ], - success: function(data) { + success: (data) => { log.debug("handling callback, data:%{public}s", data); } }); @@ -35,4 +35,4 @@ export class Dialog { tempShowMessage.message = ''; GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_DIALOG_CONTROLLER).close(); } -} +} \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index ff1b8b93e464..ab52098623a5 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -79,7 +79,7 @@ export class VideoPlayer { removeIndex = index; } }); - GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).get(viewTag).controller.requestFullscreen(false); //4.x已修复 + GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).get(viewTag).controller.requestFullscreen(false); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_ARRAY).splice(removeIndex, 1); GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_INDEX_MAP).delete(viewTag); } diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000000..9d05d81c59ef --- /dev/null +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets @@ -0,0 +1,23 @@ +interface ResultObjectInterface { + "errCode": number; + "errMsg": string; + "data": string, +} + +export class Result { + public static success(data: string){ + return ({ + "errCode": 0, + "errMsg": "", + "data": data, + }as ResultObjectInterface); + } + + public static error(errCode:number, errMsg:string){ + return ({ + "errCode": errCode, + "errMsg": errMsg, + "data": "", + } as ResultObjectInterface); + } +}; \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897da3..000000000000 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts +++ /dev/null @@ -1,16 +0,0 @@ -export class Result { - public static success(data){ - return { - "errCode": 0, - "errMsg": "", - "data": data, - }; - } - - public static error(errCode, errMsg) { - return { - "errCode": errCode, - "errMsg": errMsg, - }; - } -}; \ No newline at end of file diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index 2f43fc23ccd7..13ca24f0fc08 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets @@ -9,19 +9,21 @@ import Accelerometer from '../system/sensor/AccelerometerUtils' import nativerender from "libnativerender.so"; export class NapiHelper { - // 需要将JS代码执行在worker线程的在此处绑定方法 + // The JS code needs to be executed in the worker thread and the method needs to be bound here. static registerFunctions() { NapiHelper.registerOthers(); NapiHelper.registerDeviceUtils(); NapiHelper.registerSensor(); } - // 需要将JS代码执行在UI线程的在此处注册方法 + + // The JS code needs to be executed in the UI thread. The method is registered here. static registerUIFunctions() { NapiHelper.registerEditBox(); NapiHelper.registerVideoPlay(); NapiHelper.registerWebView(); NapiHelper.registerOtherUIFunctions(); } + private static registerOthers() { nativerender.JSBind.bindFunction("ApplicationManager.getVersionName", ApplicationManager.getVersionName); } @@ -63,6 +65,7 @@ export class NapiHelper { nativerender.JSBind.bindFunction('CocosEditBox.setNativeInputFlag', CocosEditBox.setNativeInputFlag); nativerender.JSBind.bindFunction('CocosEditBox.hideAllEditBox', CocosEditBox.hideAllEditBox); } + private static registerVideoPlay() { nativerender.JSBind.bindFunction('VideoPlayer.createVideoPlayer', VideoPlayer.createVideoPlayer); nativerender.JSBind.bindFunction('VideoPlayer.removeVideoPlayer', VideoPlayer.removeVideoPlayer); @@ -98,11 +101,17 @@ export class NapiHelper { nativerender.JSBind.bindFunction('WebView.evaluateJS', WebView.evaluateJS); nativerender.JSBind.bindFunction('WebView.setScalesPageToFit', WebView.setScalesPageToFit); } + private static registerOtherUIFunctions() { nativerender.JSBind.bindFunction('JumpManager.openUrl', JumpManager.openUrl); nativerender.JSBind.bindFunction('ApplicationManager.exit', ApplicationManager.exit); nativerender.JSBind.bindFunction('DiaLog.showDialog', Dialog.showDialog); nativerender.JSBind.bindFunction('DiaLog.showTextInputDialog', Dialog.showTextInputDialog); nativerender.JSBind.bindFunction('DiaLog.hideTextInputDialog', Dialog.hideTextInputDialog); + + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaLeft", DeviceUtils.getSafeAreaLeft); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaTop", DeviceUtils.getSafeAreaTop); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaWidth", DeviceUtils.getSafeAreaWidth); + nativerender.JSBind.bindFunction("DeviceUtils.getSafeAreaHeight", DeviceUtils.getSafeAreaHeight); } } diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 81% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets index 888a840af5a8..0195fff7bb3b 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets @@ -3,9 +3,16 @@ import type common from '@ohos.app.ability.common'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import {Result} from "../../entity/Result" import Logger from '../../utils/Logger' +import { BusinessError } from '@kit.BasicServicesKit'; let log: Logger = new Logger(0x0001, "JumpManager"); +interface WantInfoObjectInterface { + 'action': string; + 'entities': string[]; + 'uri': string; +} + export class JumpManager { static MODULE_NAME: string = 'JumpManager'; @@ -18,14 +25,14 @@ export class JumpManager { static openUrl(url: string) : void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); - let wantInfo = { + let wantInfo: WantInfoObjectInterface = { '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.info('%{public}s', JSON.stringify(Result.success(""))); + }).catch((err:BusinessError) => { log.error('openUrl : err : %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); }); } diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 100% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 69% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584810..f0de00cb18d4 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets @@ -5,6 +5,8 @@ import Logger from '../../utils/Logger'; import window from '@ohos.window'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import { Rect } from '@ohos.application.AccessibilityExtensionAbility'; +import { JSON } 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,39 @@ export class DeviceUtils { return DeviceUtils._cutoutLeft + DeviceUtils._cutoutWidth; } -} \ No newline at end of file + + static calculateSafeArea(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; + DeviceUtils.safeAreaInWorker = safeArea; + } + + static getSafeAreaLeft() : number { + return DeviceUtils.safeAreaInWorker.left; + } + + static getSafeAreaTop() : number { + return DeviceUtils.safeAreaInWorker.top; + } + + static getSafeAreaWidth() : number { + return DeviceUtils.safeAreaInWorker.width; + } + + static getSafeAreaHeight() : number { + return DeviceUtils.safeAreaInWorker.height; + } +} diff --git a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets similarity index 82% rename from tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts rename to tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets index 56309439f011..f51791f8adc3 100644 --- a/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts +++ b/tests/lua-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ets @@ -1,4 +1,5 @@ import { getContext } from "libnativerender.so"; +import { CPPFunctions } from "libnativerender.so"; import { ContextType } from "../../common/Constants" import sensor from '@ohos.sensor'; import display from '@ohos.display'; @@ -7,7 +8,7 @@ import Logger from '../../utils/Logger' let log: Logger = new Logger(0x0001, "AccelerometerUtils"); -const accUtils = getContext(ContextType.SENSOR_API); +const accUtils: CPPFunctions= getContext(ContextType.SENSOR_API); export default class Accelerometer { @@ -22,7 +23,7 @@ export default class Accelerometer { /* 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) { + sensor.on(sensor.SensorId.ACCELEROMETER, (data) => { let rotation = display.getDefaultDisplaySync().rotation; if (rotation === 0) { // Display device screen rotation 0° @@ -34,10 +35,11 @@ export default class Accelerometer { // 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); + // 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))); + log.error('Accelerometer init fail, err: %{public}s', + JSON.stringify(Result.error(-1, 'unsupported rotation: ' + rotation))); } }, { interval: intervalTime }); } catch (err) {