|
| 1 | +import window from '@ohos.window'; |
| 2 | +import UIAbility from '@ohos.app.ability.UIAbility'; |
| 3 | +import nativeRender from "libnativerender.so"; |
| 4 | +import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities" |
| 5 | +import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" |
| 6 | +import { BusinessError } from '@kit.BasicServicesKit'; |
| 7 | +import { WorkerManager } from '../workers/WorkerManager'; |
| 8 | +import Want from '@ohos.app.ability.Want'; |
| 9 | +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; |
| 10 | + |
| 11 | +const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); |
| 12 | +const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS); |
| 13 | +let cocosWorker = WorkerManager.getInstance().getWorker(); |
| 14 | +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); |
| 15 | +GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); |
| 16 | +export default class MainAbility extends UIAbility { |
| 17 | + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { |
| 18 | + nativeAppLifecycle.onCreate(); |
| 19 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context); |
| 20 | + console.info('[LIFECYCLE-App] onCreate') |
| 21 | + } |
| 22 | + |
| 23 | + onDestroy() { |
| 24 | + nativeAppLifecycle.onDestroy(); |
| 25 | + console.info('[LIFECYCLE-App] onDestroy') |
| 26 | + } |
| 27 | + |
| 28 | + onWindowStageCreate(windowStage: window.WindowStage): void { |
| 29 | + // Main window is created, set main page for this ability |
| 30 | + windowStage.loadContent('pages/Index', (err:BusinessError, data) => { |
| 31 | + if (err.code) { |
| 32 | + return; |
| 33 | + } |
| 34 | + rawFileUtils.nativeResourceManagerInit(this.context.resourceManager); |
| 35 | + rawFileUtils.writablePathInit(this.context.filesDir); |
| 36 | + }); |
| 37 | + |
| 38 | + windowStage.getMainWindow().then((windowIns: window.Window) => { |
| 39 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns); |
| 40 | + // Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed. |
| 41 | + let systemBarPromise = windowIns.setWindowSystemBarEnable([]); |
| 42 | + // Whether the window layout is displayed in full screen mode |
| 43 | + let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true); |
| 44 | + // Sets whether the screen is always on. |
| 45 | + let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true); |
| 46 | + Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => { |
| 47 | + console.info('Succeeded in setting the window'); |
| 48 | + }).catch((err: BusinessError) => { |
| 49 | + console.error('Failed to set the window, cause ', err.code, err.message); |
| 50 | + }); |
| 51 | + |
| 52 | + try { |
| 53 | + DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect); |
| 54 | + windowIns.on('avoidAreaChange', (data) => { |
| 55 | + console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' + |
| 56 | + JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area)); |
| 57 | + |
| 58 | + if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW); |
| 63 | + DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect); |
| 64 | + }); |
| 65 | + } catch (exception) { |
| 66 | + console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`); |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => { |
| 71 | + let stageEventType: window.WindowStageEventType = data; |
| 72 | + switch (stageEventType) { |
| 73 | + case window.WindowStageEventType.RESUMED: |
| 74 | + console.info('[LIFECYCLE-App] onShow_RESUMED') |
| 75 | + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ |
| 76 | + nativeAppLifecycle.onShow(); |
| 77 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); |
| 78 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); |
| 79 | + } |
| 80 | + break; |
| 81 | + case window.WindowStageEventType.PAUSED: |
| 82 | + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ |
| 83 | + console.info('[LIFECYCLE-App] onHide_PAUSED') |
| 84 | + nativeAppLifecycle.onHide(); |
| 85 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); |
| 86 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); |
| 87 | + } |
| 88 | + break; |
| 89 | + default: |
| 90 | + break; |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + onWindowStageDestroy() { |
| 96 | + // Main window is destroyed, release UI related resources |
| 97 | + } |
| 98 | + |
| 99 | + onForeground() { |
| 100 | + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){ |
| 101 | + // Ability has brought to foreground |
| 102 | + console.info('[LIFECYCLE-App] onShow') |
| 103 | + nativeAppLifecycle.onShow(); |
| 104 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false); |
| 105 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + onBackground() { |
| 110 | + if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){ |
| 111 | + // Ability has back to background |
| 112 | + console.info('[LIFECYCLE-App] onHide') |
| 113 | + nativeAppLifecycle.onHide(); |
| 114 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false); |
| 115 | + GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true); |
| 116 | + } |
| 117 | + } |
| 118 | +}; |
0 commit comments