diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 0eb48a24611c41c8ea8c1d65e982c824b97ecc9c..d8032b8b0e6d0ea5c285cc026791ed4ca676259f 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -28,9 +28,10 @@ export default class EntryAbility extends UIAbility { // [StartExclude WindowSizeChange] windowData?: window.Window; uiContext?: UIContext; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); - windowUtil?: WindowUtil = WindowUtil.getInstance(); + cameraUtil?: CameraUtil; + windowUtil?: WindowUtil; isFirstCreated: boolean = true; + isFirstTime: boolean = true; // [EndExclude WindowSizeChange] // [Start Bp2] // [Start SelectCamera2] @@ -51,20 +52,12 @@ export default class EntryAbility extends UIAbility { let displayOrientation: display.Orientation = display.getDefaultDisplaySync().orientation; AppStorage.setOrCreate('displayOrientation', displayOrientation); // [EndExclude WindowSizeChange] - let isFront: boolean | undefined = AppStorage.get('isFront'); - let surfaceId: string | undefined = AppStorage.get('surfaceId'); - // [StartExclude WindowSizeChange] - if (widthBp === WidthBreakpoint.WIDTH_SM && heightBp === HeightBreakpoint.HEIGHT_MD) { - // When switching to a wide folding external screen, onForeground() will be triggered. - return; - } - // [EndExclude WindowSizeChange] // [EndExclude SelectCamera2] // [EndExclude Preview2] - if (isFront) { - this.cameraUtil?.cameraShooting(surfaceId!, this.context!, camera.CameraPosition.CAMERA_POSITION_FRONT); + if (!this.isFirstTime) { + this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); } else { - this.cameraUtil?.cameraShooting(surfaceId!, this.context!, camera.CameraPosition.CAMERA_POSITION_BACK); + this.isFirstTime = false; } // [EndExclude SelectCamera4] // [EndExclude Bp2] @@ -101,7 +94,13 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + AppStorage.setOrCreate('cameraUtil', new CameraUtil()); + this.cameraUtil = AppStorage.get('cameraUtil'); + AppStorage.setOrCreate('windowUtil', new WindowUtil()); + this.windowUtil = AppStorage.get('windowUtil'); this.windowUtil!.setWindowStage(windowStage); + // Monitor window size changes and update breakpoints. + windowStage.getMainWindowSync().on('windowSizeChange', this.onWindowSizeChange); // [EndExclude WindowSizeChange] // [EndExclude Bp2] windowStage.loadContent('pages/Index', (err) => { @@ -136,8 +135,6 @@ export default class EntryAbility extends UIAbility { let displayOrientation: display.Orientation = display.getDefaultDisplaySync().orientation; AppStorage.setOrCreate('displayOrientation', displayOrientation); // [EndExclude WindowSizeChange] - // Monitor window size changes and update breakpoints. - data.on('windowSizeChange', this.onWindowSizeChange); // [StartExclude WindowSizeChange] AppStorage.setOrCreate('isBackground', false); // [EndExclude Bp2] diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4ce1ba6c5593a750998e67aeee1e65b2af66cf22..a21fa89d542a0accc25daa4b438c7ba6354216ad 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -34,10 +34,10 @@ struct Index { @StorageLink('isFront') isFront: boolean = false; @StorageLink('isHalfFolded') isHalfFolded: boolean = false; @StorageLink('creaseRegion') creaseRegion: number[] = []; + @StorageLink('windowUtil') windowUtil: WindowUtil = new WindowUtil(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); xComponentController: XComponentController = new XComponentController(); - cameraUtil?: CameraUtil = CameraUtil.getInstance(); - windowUtil?: WindowUtil = WindowUtil.getInstance(); permissions: Array = [ 'ohos.permission.CAMERA', 'ohos.permission.READ_IMAGEVIDEO', @@ -51,20 +51,20 @@ struct Index { if (this.widthBp === WidthBreakpoint.WIDTH_MD && (orientation === display.Orientation.LANDSCAPE || orientation === display.Orientation.LANDSCAPE_INVERTED)) { this.isHalfFolded = true; - this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); - this.windowUtil?.setMainWindowOrientation(window.Orientation.LANDSCAPE); + this.cameraUtil!.setXComponentRect(this.windowUtil.getWindowSize()); + this.windowUtil.setMainWindowOrientation(window.Orientation.LANDSCAPE); } } // Exit the half folded status page. else { if (this.isHalfFolded) { this.isHalfFolded = false; - this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); + this.cameraUtil!.setXComponentRect(this.windowUtil.getWindowSize()); } else { this.isHalfFolded = false; } if (this.widthBp !== WidthBreakpoint.WIDTH_SM) { - this.windowUtil?.setMainWindowOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + this.windowUtil.setMainWindowOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); } } } @@ -76,6 +76,17 @@ struct Index { display.on('foldStatusChange', this.onFoldStatusChange); // [EndExclude SelectCamera1] // Apply to the user for permission to access the camera and gallery. + // abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.permissions).then(() => { + // setTimeout(() => { + // // After obtaining permission, load the camera preview stream and ensure it is consistent with the aspect ratio of the surface. + // // [StartExclude SelectCamera1] + // this.cameraUtil?.setSurfaceProfile(this.xComponentController, this.getUIContext()); + // // [EndExclude SelectCamera1] + // this.cameraUtil?.cameraShooting(this.surfaceId, this.context!, camera.CameraPosition.CAMERA_POSITION_BACK); + // }, 200); + // }) + } + onPageShow(): void { abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.permissions).then(() => { setTimeout(() => { // After obtaining permission, load the camera preview stream and ensure it is consistent with the aspect ratio of the surface. @@ -90,7 +101,7 @@ struct Index { aboutToDisappear(): void { display.off('foldStatusChange'); - this.windowUtil!.offWindowSizeChange(); + this.windowUtil.offWindowSizeChange(); } build() { diff --git a/entry/src/main/ets/utils/CameraUtil.ets b/entry/src/main/ets/utils/CameraUtil.ets index 424bf3341711d973e2d9ff503c03bcd2ead17344..8a2418aa75e6549e852d136141d7d7d02d8ddb7f 100644 --- a/entry/src/main/ets/utils/CameraUtil.ets +++ b/entry/src/main/ets/utils/CameraUtil.ets @@ -33,14 +33,7 @@ export class CameraUtil { surfaceId: string = ''; xComponentController?: XComponentController; uiContext?: UIContext; - windowUtil?: WindowUtil = WindowUtil.getInstance(); - - static getInstance(): CameraUtil | undefined { - if (!AppStorage.get('cameraUtil')) { - AppStorage.setOrCreate('cameraUtil', new CameraUtil()); - } - return AppStorage.get('cameraUtil'); - } + windowUtil?: WindowUtil; async cameraShooting(surfaceId: string, context: Context, cameraPosition: camera.CameraPosition): Promise { this.surfaceId = surfaceId; @@ -91,6 +84,7 @@ export class CameraUtil { // Save photo. this.setPhotoOutputCb(); // Configure the camera preview stream to match the aspect ratio of the surface, otherwise the preview page will be compressed or stretched. + this.windowUtil = AppStorage.get('windowUtil'); this.setXComponentRect(this.windowUtil!.getWindowSize()); // Create the camera session and config. this.photoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO); diff --git a/entry/src/main/ets/utils/WindowUtil.ets b/entry/src/main/ets/utils/WindowUtil.ets index 635df82a44628bb9e51c39fc335b1e5f22a88905..1f47c3a2a1f7f8734770a166477d2b1ad66de66a 100644 --- a/entry/src/main/ets/utils/WindowUtil.ets +++ b/entry/src/main/ets/utils/WindowUtil.ets @@ -21,13 +21,6 @@ export class WindowUtil { windowStage?: window.WindowStage; mainWindow?: window.Window; - static getInstance(): WindowUtil | undefined { - if (!AppStorage.get('windowUtil')) { - AppStorage.setOrCreate('windowUtil', new WindowUtil()); - } - return AppStorage.get('windowUtil'); - } - setWindowStage(windowStage: window.WindowStage): void { this.windowStage = windowStage; this.windowStage.getMainWindow((err, windowClass: window.Window) => { diff --git a/entry/src/main/ets/views/CommonView.ets b/entry/src/main/ets/views/CommonView.ets index 3bbf212f4539f0e9b66444f0a8c66da21fd6573f..d640d0325af58680e7f473b094a07725371c7f69 100644 --- a/entry/src/main/ets/views/CommonView.ets +++ b/entry/src/main/ets/views/CommonView.ets @@ -75,7 +75,7 @@ export struct ShotAreaSm { @StorageLink('heightBp') heightBp: HeightBreakpoint = HeightBreakpoint.HEIGHT_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() { @@ -162,7 +162,7 @@ export struct ShotArea { @StorageLink('widthBp') widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() { @@ -240,7 +240,7 @@ export struct ShotAreaHalfFolded { @StorageLink('widthBp') widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() {