diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 9a0ad9535da793da3880db5f7a91db4f407190ab..4a00f6df43ba857b20841aad9c2e0fb1b9cab7a9 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -33,7 +33,9 @@ export default class EntryAbility extends UIAbility { heightBp === 0 ? 'sm' : heightBp === 1 ? 'md' : heightBp === 2 ? 'md' : 'lg'); // Application adaptation for horizontal and vertical screen switching. - this.windowObj?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + this.windowObj?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED).catch((err: BusinessError) => { + hilog.error(0x00, 'testTag', `setPreferredOrientation failed, code = ${err.code}, message = ${err.message}`); + }); }; public onAvoidAreaChange: (avoidArea: window.AvoidAreaOptions) => void = (avoidArea: window.AvoidAreaOptions) => { if (avoidArea.type === window.AvoidAreaType.TYPE_CUTOUT) { @@ -48,6 +50,7 @@ export default class EntryAbility extends UIAbility { onDestroy(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } + // [EndExclude screen_rotation] onWindowStageCreate(windowStage: window.WindowStage): void { // [StartExclude screen_rotation] @@ -70,12 +73,25 @@ export default class EntryAbility extends UIAbility { // Immersive Adaptation this.immersionFuc(windowStage); - let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext(); - AppStorage.setOrCreate('uiContext', uiContext); + try { + let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext(); + AppStorage.setOrCreate('uiContext', uiContext); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'testTag', `getUIContext failed, code = ${err.code}, message = ${err.message}`); + } + // [EndExclude screen_rotation] - this.uiContext = this.windowObj!.getUIContext(); + try { + this.uiContext = this.windowObj!.getUIContext(); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'testTag', `getUIContext failed, code = ${err.code}, message = ${err.message}`); + } // Application adaptation for horizontal and vertical screen switching. - this.windowObj?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + this.windowObj?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED).catch((err: BusinessError) => { + hilog.error(0x00, 'testTag', `setPreferredOrientation failed, code = ${err.code}, message = ${err.message}`); + }); // [StartExclude screen_rotation] // The system interface depends on UIContext and needs to be invoked after the page is loaded. It needs to be written in the loadContent callback function. windowStage.getMainWindow().then((data: window.Window) => { @@ -88,14 +104,26 @@ export default class EntryAbility extends UIAbility { data.on('windowSizeChange', this.onWindowSizeChange); // Monitor changes in the location of the cutout area. - let avoidArea: window.AvoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT); - this.onAvoidAreaChange({ type: window.AvoidAreaType.TYPE_CUTOUT, area: avoidArea }); - data.on('avoidAreaChange', this.onAvoidAreaChange); + try { + let avoidArea: window.AvoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT); + this.onAvoidAreaChange({ type: window.AvoidAreaType.TYPE_CUTOUT, area: avoidArea }); + data.on('avoidAreaChange', this.onAvoidAreaChange); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'testTag', `getWindowAvoidArea failed, code = ${err.code}, message = ${err.message}`); + } + // Window size acquisition and monitoring. - let properties = data.getWindowProperties(); - AppStorage.setOrCreate('windowHeight', properties.windowRect.height); - AppStorage.setOrCreate('windowWidth', properties.windowRect.width); + try { + let properties = data.getWindowProperties(); + AppStorage.setOrCreate('windowHeight', properties.windowRect.height); + AppStorage.setOrCreate('windowWidth', properties.windowRect.width); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'testTag', `getWindowProperties failed, code = ${err.code}, message = ${err.message}`); + } + }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`); @@ -103,6 +131,7 @@ export default class EntryAbility extends UIAbility { // [EndExclude screen_rotation] }); } + // [StartExclude screen_rotation] onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources @@ -123,16 +152,26 @@ export default class EntryAbility extends UIAbility { * Page immersion. */ immersionFuc(windowStage: window.WindowStage): void { - let windowClass: window.Window = windowStage.getMainWindowSync(); - windowClass.setWindowLayoutFullScreen(true); - let navigationBarArea: window.AvoidArea = - windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); - let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); - AppStorage.setOrCreate('naviIndicatorHeight', - windowClass.getUIContext().px2vp(navigationBarArea.bottomRect.height)); - AppStorage.setOrCreate('statusBarHeight', windowClass.getUIContext().px2vp(area.topRect.height)); - AppStorage.setOrCreate('windowClass', windowClass); + try { + let windowClass: window.Window = windowStage.getMainWindowSync(); + windowClass.setWindowLayoutFullScreen(true).catch((err: BusinessError) => { + hilog.error(0x00, 'testTag', `setWindowLayoutFullScreen failed, code = ${err.code}, message = ${err.message}`); + }); + let navigationBarArea: window.AvoidArea = + windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + AppStorage.setOrCreate('naviIndicatorHeight', + windowClass.getUIContext().px2vp(navigationBarArea.bottomRect.height)); + AppStorage.setOrCreate('statusBarHeight', windowClass.getUIContext().px2vp(area.topRect.height)); + AppStorage.setOrCreate('windowClass', windowClass); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'testTag', `getMainWindowSync failed, code = ${err.code}, message = ${err.message}`); + } + } + // [EndExclude screen_rotation] } + // [End screen_rotation] \ No newline at end of file