diff --git a/samples/js-tests/project/proj.ohos/build-profile.json5 b/samples/js-tests/project/proj.ohos/build-profile.json5 index fe6220c41..6a7400f16 100644 --- a/samples/js-tests/project/proj.ohos/build-profile.json5 +++ b/samples/js-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" } ], @@ -13,13 +13,13 @@ "name": "default", "type": "HarmonyOS", "material": { - "certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.cer", - "storePassword": "0000001B6A69302D4D111DCB74975A8165D5E23EA61A73FCCE30D42952F837C28C48131D9A1D6724675265", + "certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.cer", "keyAlias": "debugKey", - "keyPassword": "0000001B8470462D8531EC86C7F826FE0041C5136295A7521B1EF44982FE9731397016A1E84BA83212AFDE", - "profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.p7b", + "keyPassword": "0000001A6E67CC5E817B52C8B8F1A155B53FCB113D9846F6430921A35A91F0C1FBA5081E337B95F52A2E", + "profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.p7b", "signAlg": "SHA256withECDSA", - "storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.p12" + "storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.p12", + "storePassword": "0000001A8D23A0A8E834550BA7E84550FF7BF680D7129698185DB486564E8E45F50BEE6036BB86FE43D1" } } ] diff --git a/samples/js-tests/project/proj.ohos/entry/build-profile.json5 b/samples/js-tests/project/proj.ohos/entry/build-profile.json5 index edb7d04f0..81aa308f8 100644 --- a/samples/js-tests/project/proj.ohos/entry/build-profile.json5 +++ b/samples/js-tests/project/proj.ohos/entry/build-profile.json5 @@ -12,7 +12,7 @@ }, "sourceOption": { "workers": [ - './src/main/ets/workers/CocosWorker.ts' + './src/main/ets/workers/CocosWorker.ets' ] } }, diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/main.cpp b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/main.cpp index b04af9d0e..093a2cf76 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/main.cpp +++ b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/main.cpp @@ -10,7 +10,7 @@ extern "C" void Cocos2dxRenderer_nativeInit(int w, int h) { - OHOS_LOGD("Cocos2dxRenderer_nativeInit() - window width:[%d], height:[%d]", w, h); + OHOS_LOGD("Cocos2dxRenderer_nativeInit() - window width:[%{public}d], height:[%{public}d]", w, h); if (!CCDirector::sharedDirector()->getOpenGLView()) { OHOS_LOGD("Cocos2dxRenderer_nativeInit() - branch 1"); @@ -33,4 +33,4 @@ void Cocos2dxRenderer_nativeInit(int w, int h) } } -} \ No newline at end of file +} diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/napi_init.cpp b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/napi_init.cpp index 4448b8198..29e456357 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/napi_init.cpp +++ b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/napi_init.cpp @@ -1,18 +1,3 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - #include "platform/ohos/CCLogOhos.h" #include "plugin_manager.h" diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts index 6a9293652..972797dab 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts +++ b/samples/js-tests/project/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts @@ -24,7 +24,6 @@ export interface CPPFunctions { failLoading: (viewTag: number, url: string) => void; jsCallback: () => void; onVideoCallBack: (viewTag: number, event: number) => void; - onAccelerometerCallBack: (x: number, y: number, z: number, interval: number) => void; } export const getContext: (a: number) => CPPFunctions; diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts b/samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets similarity index 58% rename from samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts rename to samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets index a89ce067f..3767ce339 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/samples/js-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ets @@ -1,20 +1,21 @@ 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 { 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(); export default class MainAbility extends UIAbility { - onCreate(want, launchParam) { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 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') } @@ -23,9 +24,9 @@ export default class MainAbility extends UIAbility { console.info('[LIFECYCLE-App] onDestroy') } - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability - windowStage.loadContent('pages/Index', (err, data) => { + windowStage.loadContent('pages/Index', (err:BusinessError, data) => { if (err.code) { return; } @@ -34,6 +35,7 @@ export default class MainAbility extends UIAbility { }); 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 @@ -42,12 +44,29 @@ export default class MainAbility extends UIAbility { 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)); + }).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) => { + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { let stageEventType: window.WindowStageEventType = data; switch (stageEventType) { case window.WindowStageEventType.RESUMED: diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets b/samples/js-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets index f26ba14cc..5205ec3ba 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets +++ b/samples/js-tests/project/proj.ohos/entry/src/main/ets/components/CocosWebview.ets @@ -4,7 +4,7 @@ import { WorkerManager } from '../workers/WorkerManager'; @Component export struct CocosWebView { - viewInfo: WebViewInfo = new WebViewInfo(0, 0, 0, 0, 0); + @ObjectLink viewInfo: WebViewInfo ; cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker(); build() { @@ -40,4 +40,4 @@ export struct CocosWebView { this.cocosWorker.postMessage({ type: "onErrorReceive", viewTag: this.viewInfo.viewTag, url: this.viewInfo.url }) }) } -} \ No newline at end of file +} diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets similarity index 72% rename from samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts rename to samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets index 94ddb8d38..d8047e80e 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/CocosWorker.ets @@ -1,13 +1,13 @@ -import worker, { ThreadWorkerGlobalScope } from '@ohos.worker'; +import worker, { ThreadWorkerGlobalScope, MessageEvents } from '@ohos.worker'; import nativeRender from "libnativerender.so"; -import { ContextType } from "@ohos/libSysCapabilities" +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" import { VideoPlayer } from "@ohos/libSysCapabilities" +import { ApplicationManager } from "@ohos/libSysCapabilities" import { CocosEditBox } from "@ohos/libSysCapabilities" import { Dialog } from "@ohos/libSysCapabilities" import { WebView } from "@ohos/libSysCapabilities" import { JumpManager } from "@ohos/libSysCapabilities" import { NapiHelper } from "@ohos/libSysCapabilities" -import { ApplicationManager } from "@ohos/libSysCapabilities" import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const appLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); @@ -24,56 +24,60 @@ NapiHelper.registerFunctions(napiContext.registerFunction) const workerPort: ThreadWorkerGlobalScope = worker.workerPort; -workerPort.onmessage = function(e) : void { - let data = e.data; - switch(data.type) { +workerPort.onmessage = (e: MessageEvents) => { + switch(e.data.type) { case "onXCLoad": console.log("[cocos] onXCLoad Callback"); Dialog.init(workerPort); CocosEditBox.init(workerPort); JumpManager.init(workerPort); WebView.init(workerPort); - VideoPlayer.init(workerPort); ApplicationManager.init(workerPort); + VideoPlayer.init(workerPort); napiContext.initAsyncInfo(); break; case "abilityContextInit": - GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, data.data); + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, e.data.data); break; case "editBoxOnFocus": - inputNapi.editBoxOnFocusCB(data.viewTag); + inputNapi.editBoxOnFocusCB(e.data.viewTag); break; case "editBoxOnChange": - inputNapi.editBoxOnChangeCB(data.viewTag, data.value); + inputNapi.editBoxOnChangeCB(e.data.viewTag, e.data.value); break; case "editBoxOnEnter": - inputNapi.editBoxOnEnterCB(data.viewTag, data.text); + inputNapi.editBoxOnEnterCB(e.data.viewTag, e.data.text); break; case "textFieldTTFOnChange": - inputNapi.textFieldTTFOnChangeCB(data.data); + inputNapi.textFieldTTFOnChangeCB(e.data.data); break; case "onMouseWheel": - mouseNapi.mouseWheelCB(data.eventType, data.scrollY); + mouseNapi.mouseWheelCB(e.data.eventType, e.data.scrollY); break; case "onPageBegin": - webViewNapi.shouldStartLoading(data.viewTag, data.url); + webViewNapi.shouldStartLoading(e.data.viewTag, e.data.url); break; case "onPageEnd": - webViewNapi.finishLoading(data.viewTag, data.url); + webViewNapi.finishLoading(e.data.viewTag, e.data.url); break; case "onJsCallBack": webViewNapi.jsCallback(); break; case "onErrorReceive": - webViewNapi.failLoading(data.viewTag, data.url); + webViewNapi.failLoading(e.data.viewTag, e.data.url); break; case "onVideoCallBack": - videoPlayNapi.onVideoCallBack(data.viewTag, data.event); + videoPlayNapi.onVideoCallBack(e.data.viewTag, e.data.event); break; case "exit": appLifecycle.onBackPress(); break; + case "updateSafeArea": + DeviceUtils.updateWorkerSafeArea(e.data.safeArea); + break; default: console.error("cocos worker: message type unknown") } } + + diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts b/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets similarity index 97% rename from samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts rename to samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets index e2ec70b18..0447223b1 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ts +++ b/samples/js-tests/project/proj.ohos/entry/src/main/ets/workers/WorkerManager.ets @@ -5,7 +5,7 @@ export class WorkerManager { private cocosWorker: worker.ThreadWorker; private constructor() { - this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ts", { + this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ets", { type: "classic", name: "CocosWorker" }); diff --git a/samples/js-tests/project/proj.ohos/entry/src/main/module.json5 b/samples/js-tests/project/proj.ohos/entry/src/main/module.json5 index a1992394e..a71568bbe 100644 --- a/samples/js-tests/project/proj.ohos/entry/src/main/module.json5 +++ b/samples/js-tests/project/proj.ohos/entry/src/main/module.json5 @@ -14,7 +14,7 @@ "abilities": [ { "name": "MainAbility", - "srcEntry": "./ets/MainAbility/MainAbility.ts", + "srcEntry": "./ets/MainAbility/MainAbility.ets", "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", @@ -33,7 +33,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/js-tests/project/proj.ohos/libSysCapabilities/index.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/index.ets similarity index 96% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/index.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/index.ets index 6c537383d..decdc1332 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/index.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/index.ets @@ -12,4 +12,4 @@ export { NapiHelper } from './src/main/ets/napi/NapiHelper' export { JumpManager } from './src/main/ets/system/appJump/JumpManager' export { DeviceUtils } from './src/main/ets/system/device/DeviceUtils' -export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager' +export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager' \ No newline at end of file diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 b/samples/js-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 index 5634eb1fb..cb9e2462c 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/oh-package.json5 +++ b/samples/js-tests/project/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/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets similarity index 99% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets index 1a101a31d..e0f60659d 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ets @@ -19,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/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts index 616534b26..15af26a90 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/common/GlobalContext.ts @@ -22,4 +22,5 @@ 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"; +} diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets similarity index 97% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets index dfc5babf3..5355a89f8 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogMsg.ets @@ -40,8 +40,8 @@ function showDialog(dialogTitle: string, dialogMessage: string) { color: '#000000' }, ], - success: function(data) { + success: (data) => { log.debug("handling callback, data:%{public}s", data); } }); -} +} \ No newline at end of file diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets similarity index 95% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets index 08f0bd7c3..968ed4cca 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/dialog/DialogWorker.ets @@ -3,7 +3,7 @@ import { DialogMsgEntity } from '../../entity/WorkerMsgEntity'; export class Dialog { static MODULE_NAME : string = 'Dialog'; - static workerPort; + static workerPort: ThreadWorkerGlobalScope; static init(workerPort: ThreadWorkerGlobalScope) : void { Dialog.workerPort = workerPort; @@ -26,4 +26,4 @@ export class Dialog { let dialogMsgEntity: DialogMsgEntity = new DialogMsgEntity(Dialog.MODULE_NAME, 'hideTextInputDialog'); Dialog.workerPort.postMessage(dialogMsgEntity); } -} +} \ No newline at end of file diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets similarity index 96% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets index ca4d8e710..ed2049cb2 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/editbox/CocosEditBox.ets @@ -1,11 +1,12 @@ +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; export class CocosEditBox { static MODULE_NAME : string = 'EditBox'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { CocosEditBox.workerPort = workerPort; } @@ -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/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets similarity index 95% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets index 50b7a7e69..f4a11e84d 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayer.ets @@ -1,11 +1,12 @@ import { VideoPlayMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class VideoPlayer { static MODULE_NAME: string = 'VideoPlay'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { VideoPlayer.workerPort = workerPort; } diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets similarity index 96% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets index 872ec4ce7..49ffb1dad 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebView.ets @@ -1,11 +1,12 @@ import { ViewRect, WebViewMsgEntity } from '../../entity/WorkerMsgEntity'; +import type { ThreadWorkerGlobalScope } from '@ohos.worker'; export class WebView { static MODULE_NAME: string = 'WebView'; - private static workerPort; + private static workerPort : ThreadWorkerGlobalScope; - static init(workerPort) : void { + static init(workerPort : ThreadWorkerGlobalScope) : void { WebView.workerPort = workerPort; } diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets index 24e1f86d9..b03c4004f 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/components/webview/WebViewMsg.ets @@ -5,7 +5,7 @@ import { WebViewMsgEntity } from '../../entity/WorkerMsgEntity'; import { BusinessError } from '@ohos.base'; let log: Logger = new Logger(0x0001, "WebViewMsg"); - +@Observed export class WebViewInfo { public uniqueId : number = 0; // position @@ -34,20 +34,7 @@ export class WebViewInfo { this.viewTag = viewTag this.uniqueId = viewTag; } -} -function copyWebViewInfo(viewInfo: WebViewInfo) : WebViewInfo { - let newViewInfo: WebViewInfo = new WebViewInfo(viewInfo.x, viewInfo.y, viewInfo.w, viewInfo.h, viewInfo.viewTag); - newViewInfo.url = viewInfo.url; - newViewInfo.isUrl = viewInfo.isUrl; - newViewInfo.zoomAccess = viewInfo.zoomAccess; - newViewInfo.visible = viewInfo.visible; - newViewInfo.controller = viewInfo.controller; - newViewInfo.opacity = viewInfo.opacity; - newViewInfo.backgroundColor = viewInfo.backgroundColor; - newViewInfo.jsInterfaceScheme = viewInfo.jsInterfaceScheme; - newViewInfo.uniqueId = 100000 - viewInfo.uniqueId; // support 50000 webView exist at the same time - return newViewInfo; } function waitUtilControllerAttached() : Promise { @@ -168,9 +155,6 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { tmpWebInfo.y = px2vp(eventData.viewRect.y); tmpWebInfo.w = px2vp(eventData.viewRect.w); tmpWebInfo.h = px2vp(eventData.viewRect.h); - let newViewInfo:WebViewInfo = copyWebViewInfo(tmpWebInfo); - let tempInfoArray :WebViewInfo[]= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY); - tempInfoArray[index] = newViewInfo; break; } case "setVisible": { @@ -181,9 +165,6 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } let tmpWebInfo:WebViewInfo= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.visible = eventData.visible; - let newViewInfo:WebViewInfo = copyWebViewInfo(tmpWebInfo); - let tempInfoArray :WebViewInfo[]= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY); - tempInfoArray[index] = newViewInfo; break; } case "setOpacityWebView": { @@ -194,9 +175,6 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } let tmpWebInfo :WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.opacity = eventData.opacity; - let newViewInfo :WebViewInfo= copyWebViewInfo(tmpWebInfo); - let tempInfoArray :WebViewInfo[]= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY); - tempInfoArray[index] = newViewInfo; break; } case "setBackgroundTransparent": { @@ -207,9 +185,6 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index] .backgroundColor = Color.Transparent; - let newViewInfo :WebViewInfo= copyWebViewInfo(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]); - let tempInfoArray :WebViewInfo[]= GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY); - tempInfoArray[index] = newViewInfo; break; } case "evaluateJS": { @@ -236,9 +211,6 @@ export function handleWebViewMsg(eventData: WebViewMsgEntity) { } let tmpWebInfo:WebViewInfo = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY)[index]; tmpWebInfo.zoomAccess == eventData.scalesPageToFit - let newViewInfo:WebViewInfo = copyWebViewInfo(tmpWebInfo); - let tempInfoArray:WebViewInfo[] = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY); - tempInfoArray[index] = newViewInfo; } break; } diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ets new file mode 100644 index 000000000..9d05d81c5 --- /dev/null +++ b/samples/js-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/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/Result.ts deleted file mode 100644 index dd9e38897..000000000 --- a/samples/js-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/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets similarity index 100% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/entity/TextInputDialogEntity.ets diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets similarity index 95% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ets index d4df1a5f9..c4f34b95b 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/napi/NapiHelper.ts +++ b/samples/js-tests/project/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); } @@ -43,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,10 +102,7 @@ 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); diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets similarity index 72% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets index e95e89b29..4b64aff55 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/preferences/Preferences.ets @@ -5,13 +5,12 @@ import common from '@ohos.app.ability.common'; import { GlobalContext, GlobalContextConstants } from '../common/GlobalContext'; let log: Logger = new Logger(0x0001, "Preferences"); -let preferences: data_preferences.Preferences | null = null; +let preferences: data_preferences.Preferences; const PREFS_NAME: string = "Cocos2dxPreferences"; export default class Preferences { - // 通过 preferencesName 获取Preferences实例 - static getPreferences(): data_preferences.Preferences { + static getPreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { preferences = data_preferences.getPreferencesSync(context, {name: PREFS_NAME}); @@ -21,13 +20,8 @@ export default class Preferences { let message = (err as BusinessError).message; log.error("Failed to get preferences. code =" + code + ", message =" + message); } - return preferences; } - /* - 通过 preferencesName 从缓存中移出指定的Preferences实例,若Preferences实例有对应的持久化文件,则同时删除其持久化文件。使用Promise异步回调。 - 调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 - */ static deletePreferences(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -41,12 +35,6 @@ export default class Preferences { } } - /* - 通过 preferencesName 从缓存中移出指定的Preferences实例,使用Promise异步回调。 - 应用首次调用getPreferences接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次getPreferences时不会再次从持久化文件中读取, - 直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 - 调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 - */ static removePreferencesFromCache(): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); try { @@ -59,7 +47,6 @@ export default class Preferences { } } - // 从缓存的Preferences实例中获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue static get(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType { if (preferences === null) { Preferences.getPreferences(); @@ -76,7 +63,6 @@ export default class Preferences { } } - // 将数据写入缓存的Preferences实例中,可通过flush将Preferences实例持久化 static put(key: string, value: data_preferences.ValueType): void { if (preferences === null) { Preferences.getPreferences(); @@ -92,7 +78,6 @@ export default class Preferences { } } - // 从缓存的Preferences实例中获取所有键值数据。 static getAll(): string | undefined { if (preferences === null) { Preferences.getPreferences(); @@ -111,7 +96,6 @@ export default class Preferences { } } - // 检查缓存的Preferences实例中是否包含名为给定Key的存储键值对 static has(key: string): boolean { if (preferences === null) { Preferences.getPreferences(); @@ -133,7 +117,6 @@ export default class Preferences { } } - // 从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过flush将Preferences实例持久化 static delete(key: string): void { if (preferences === null) { Preferences.getPreferences(); @@ -149,7 +132,6 @@ export default class Preferences { } } - // 将缓存的Preferences实例中的数据异步存储到用户首选项的持久化文件中,使用Promise异步回调。 static flush(): void { if (preferences === null) { Preferences.getPreferences(); @@ -159,7 +141,6 @@ export default class Preferences { }); } - // 清除缓存的Preferences实例中的所有数据,可通过flush将Preferences实例持久化,使用Promise异步回调。 static clear(): void { if (preferences === null) { Preferences.getPreferences(); @@ -175,7 +156,6 @@ export default class Preferences { } - // 订阅数据变更,订阅的Key的值发生变更后,在执行flush方法后,触发callback回调。 static onChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -192,7 +172,6 @@ export default class Preferences { } } - // 取消订阅数据变更。 static offChange(cb: Function): void { if (preferences === null) { Preferences.getPreferences(); @@ -210,8 +189,6 @@ export default class Preferences { } } -// 由于ArkTS中无Object.keys,且无法使用for..in... -// 若报ArkTS问题,请将此方法单独抽离至一个ts文件中并暴露,在需要用到的ets文件中引入使用 function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets similarity index 100% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManager.ets diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets similarity index 76% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets index ea373af90..2c20f240b 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/appJump/JumpManagerMsg.ets @@ -3,6 +3,8 @@ import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContex import {Result} from "../../entity/Result" import type { JumpMsgEntity } from '../../entity/WorkerMsgEntity'; import Logger from '../../utils/Logger' +import { BusinessError } from '@kit.BasicServicesKit'; +import { Want } from '@kit.AbilityKit' let log: Logger = new Logger(0x0001, "JumpManagerMsg"); @@ -18,14 +20,14 @@ export function handleJumpManagerMsg(eventData: JumpMsgEntity) : void { function openUrl(url: string): void { let context: common.UIAbilityContext = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT); - let wantInfo = { + let wantInfo: Want = { 'action': 'ohos.want.action.viewData', 'entities': ['entity.system.browsable'], 'uri': url } context.startAbility(wantInfo).then(() => { - log.info('%{public}s', JSON.stringify(Result.success({}))); - }).catch((err) => { - log.error('openUrl : err : %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); + log.info('%{public}s', JSON.stringify(Result.success(""))); + }).catch((err: BusinessError) => { + log.error('openUrl : err : %{public}s', err.code, err.message); }); } \ No newline at end of file diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets similarity index 99% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets index 9a552f56e..ab8016646 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/application/ApplicationManager.ets @@ -51,4 +51,4 @@ function terminateSelf(): void { let message = (err as BusinessError).message; console.error(`terminateSelf failed, code is ${code}, message is ${message}`); } -} +} \ No newline at end of file diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets similarity index 67% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets index b778f1584..239fe6990 100644 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ts +++ b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/device/DeviceUtils.ets @@ -5,6 +5,8 @@ import Logger from '../../utils/Logger'; import window from '@ohos.window'; import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext'; import { Rect } from '@ohos.application.AccessibilityExtensionAbility'; +import { JSON, worker } from '@kit.ArkTS'; +import {BusinessError} from '@kit.BasicServicesKit' let log = new Logger(0x0001, "DeviceUtils"); @@ -17,6 +19,10 @@ export class DeviceUtils { static _cutoutWidth: number; static _cutoutTop: number; static _cutoutHeight: number; + static _originalWindowWidth: number = 0; + static _originalWindowHeight: number = 0; + static oldSafeArea: window.Rect = {left: 0, top: 0, width: 0, height: 0}; + static safeAreaInWorker: window.Rect = {left: 0, top: 0, width: 0, height: 0}; static getDpi(): number { return display.getDefaultDisplaySync().densityDPI; @@ -46,7 +52,7 @@ export class DeviceUtils { } static setKeepScreenOn(value: boolean) { - let windowClass = null; + let windowClass:window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { //获取窗口实例 if (err.code) { @@ -58,7 +64,7 @@ export class DeviceUtils { let keepScreenOnPromise = windowClass.setWindowKeepScreenOn(value); Promise.all([keepScreenOnPromise]).then(() => { log.info('Succeeded in setKeepScreenOn, value:%{public}s', value); - }).catch((err) => { + }).catch((err:BusinessError) => { log.error('Failed to setKeepScreenOn, cause:%{public}s', JSON.stringify(err)); }); }); @@ -80,11 +86,11 @@ export class DeviceUtils { } static initScreenInfo() : void { - let windowClass = null; + let windowClass: window.Window; try { window.getLastWindow(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT), (err, data) => { if (err.code) { - log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', JSON.stringify(err)); + log.error('Failed to obtain last window when initScreenInfo. Cause:%{public}s', err.code, err.message); return; } @@ -113,8 +119,8 @@ export class DeviceUtils { DeviceUtils._cutoutTop = data.boundingRects[0].top; DeviceUtils._cutoutWidth = data.boundingRects[0].width; DeviceUtils._cutoutHeight = data.boundingRects[0].height; - }).catch((err) => { - log.error('Failed to obtain all the display objects. Code: ' + JSON.stringify(err)); + }).catch((err: BusinessError) => { + log.error('Failed to obtain all the display objects. Code: ', err.code, err.message); }); } @@ -161,4 +167,43 @@ export class DeviceUtils { return DeviceUtils._cutoutLeft + DeviceUtils._cutoutWidth; } -} \ No newline at end of file + + static calculateSafeArea(cocosWorker: worker.ThreadWorker, avoidArea: window.AvoidArea, windowRect: window.Rect) { + let safeArea : window.Rect = {left: 0, top: 0, width: 0, height: 0}; + if(DeviceUtils._originalWindowWidth == windowRect.width && DeviceUtils._originalWindowHeight == windowRect.height) { + safeArea.left = DeviceUtils.oldSafeArea.left + avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = DeviceUtils.oldSafeArea.top + avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : DeviceUtils.oldSafeArea.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : DeviceUtils.oldSafeArea.height) - safeArea.top; + } else { + safeArea.left = avoidArea.leftRect.left + avoidArea.leftRect.width; + safeArea.top = avoidArea.topRect.top + avoidArea.topRect.height; + safeArea.width = (avoidArea.rightRect.left != 0 ? avoidArea.rightRect.left : windowRect.width) - safeArea.left; + safeArea.height = (avoidArea.bottomRect.top != 0 ? avoidArea.bottomRect.top : windowRect.height) - safeArea.top; + DeviceUtils._originalWindowWidth = windowRect.width; + DeviceUtils._originalWindowHeight = windowRect.height; + } + DeviceUtils.oldSafeArea = safeArea; + cocosWorker.postMessage({type: 'updateSafeArea', safeArea: safeArea}); + } + + static updateWorkerSafeArea(safeArea: window.Rect) { + DeviceUtils.safeAreaInWorker = safeArea; + } + + static getSafeAreaLeft() : number { + return DeviceUtils.safeAreaInWorker.left; + } + + static getSafeAreaTop() : number { + return DeviceUtils.safeAreaInWorker.top; + } + + static getSafeAreaWidth() : number { + return DeviceUtils.safeAreaInWorker.width; + } + + static getSafeAreaHeight() : number { + return DeviceUtils.safeAreaInWorker.height; + } +} diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts deleted file mode 100644 index 0e8c8fa11..000000000 --- a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/system/sensor/AccelerometerUtils.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { getContext } from "libnativerender.so"; -import { ContextType } from "../../common/Constants" -import sensor from '@ohos.sensor'; -import display from '@ohos.display'; -import {Result} from "../../entity/Result" -import Logger from '../../utils/Logger' - -let log: Logger = new Logger(0x0001, "AccelerometerUtils"); - -const accUtils = getContext(ContextType.SENSOR_API); - -export default class Accelerometer { - - private static instance = new Accelerometer(); - - static getInstance() : Accelerometer { - return Accelerometer.instance; - } - - static enable(intervalTime: number) : void { - try { - /* HarmonyOS allow multiple subscriptions, but the game only need one - so if the interval changed, cancel subscription and redo with the new interval */ - sensor.off(sensor.SensorId.ACCELEROMETER); - sensor.on(sensor.SensorId.ACCELEROMETER, function (data) { - let rotation = display.getDefaultDisplaySync().rotation; - if (rotation === 0) { - // Display device screen rotation 0° - accUtils.onAccelerometerCallBack(data.x, data.y, data.z, intervalTime); - } else if (rotation === 1) { - // Display device screen rotation 90° - accUtils.onAccelerometerCallBack(data.y, -data.x, data.z, intervalTime); - } else if (rotation === 2) { - // Display device screen rotation 180° - accUtils.onAccelerometerCallBack(-data.x, -data.y, data.z, intervalTime); - } else if (rotation === 3) { - // Display device screen rotation 270° - accUtils.onAccelerometerCallBack(-data.y, data.x, data.z, intervalTime); - } else { - log.error('Accelerometer init fail, err: %{public}s', JSON.stringify(Result.error(-1, 'unsupported rotation: ' + rotation))); - } - }, { interval: intervalTime }); - } catch (err) { - log.error('Accelerometer init fail, err: %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); - } - } - - static disable() : void { - try { - sensor.off(sensor.SensorId.ACCELEROMETER); - } catch (err) { - log.error('Accelerometer off fail, err: %{public}s', JSON.stringify(Result.error(-1, JSON.stringify(err))) ?? ''); - } - } -} diff --git a/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts b/samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets similarity index 100% rename from samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ts rename to samples/js-tests/project/proj.ohos/libSysCapabilities/src/main/ets/utils/StringUtils.ets