diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 78b0b233231704f66eae43108a8e17fb75284028..34527867a3d856151f7c7f5ddef70f6ceaa19951 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -28,8 +28,12 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.getMainWindowSync().setWindowLayoutFullScreen(true); - AppStorage.setOrCreate('window', windowStage.getMainWindowSync()) + try { + windowStage.getMainWindowSync().setWindowLayoutFullScreen(true); + AppStorage.setOrCreate('window', windowStage.getMainWindowSync()); + } catch (error) { + hilog.error(0x0000, 'testTag', 'Failed to use windowStage. Cause: %{public}s', JSON.stringify(error) ?? ''); + } windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index b51a44d641c0f7325375c4fbd8feb875713f85d5..e6bd4c6d3af73934beebd7b7d8e0aff7f8586c60 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -66,9 +66,10 @@ class CountDownTimerModifier implements ContentModifier } } +const ONE_THOUSAND = 1000; @Builder function buildTextTimer(config: TextTimerConfiguration) { - Text(Math.ceil(config.count / 1000 - config.elapsedTime / 100).toString()) + Text(Math.ceil(config.count / ONE_THOUSAND - config.elapsedTime / 100).toString()) .width(CameraConstants.FULL_PERCENT) .height(CameraConstants.FULL_PERCENT) .fontSize($r('app.float.count_down_font_size')) @@ -226,7 +227,11 @@ struct XComponentPage { aboutToDisappear(): void { clearInterval(this.countDownInterval); - this.windowClass?.off('windowRectChange'); + try { + this.windowClass?.off('windowRectChange'); + } catch (error) { + console.error(`The aboutToDisappear call failed. error: ${JSON.stringify(error)}`); + } sensor.off(sensor.SensorId.GRAVITY); } @@ -686,11 +691,15 @@ struct XComponentPage { predicates: predicates }; let photoHelper = photoAccessHelper.getPhotoAccessHelper(this.context); - let fetchResult: photoAccessHelper.FetchResult = - await photoHelper.getAssets(fetchOptions); - if (fetchResult !== undefined) { - let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - this.photoUri = await photoAsset.getThumbnail(); + try { + let fetchResult: photoAccessHelper.FetchResult = + await photoHelper.getAssets(fetchOptions); + if (fetchResult !== undefined) { + let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + this.photoUri = await photoAsset.getThumbnail(); + } + } catch (error) { + console.error(`The getThumbnail call failed. error: ${JSON.stringify(error)}`); } } diff --git a/entry/src/main/ets/pages/MovingPhotoPage.ets b/entry/src/main/ets/pages/MovingPhotoPage.ets index 994cbbcf9626bd90a73bffd1ce18df6b1304af82..39e92e5ec43e8c1cfcfcf3bb4b7c0f857903be75 100644 --- a/entry/src/main/ets/pages/MovingPhotoPage.ets +++ b/entry/src/main/ets/pages/MovingPhotoPage.ets @@ -5,8 +5,8 @@ import { MovingPhotoView, MovingPhotoViewController, photoAccessHelper } from '@ struct MovingPhotoPage { // [Start moving_photo_view_controller] @State src: photoAccessHelper.MovingPhoto | undefined = undefined; - controller: MovingPhotoViewController = new MovingPhotoViewController(); + // [End moving_photo_view_controller] build() { diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index 40f1fb16b247833dce66bd91e97ed00fcbbae6cf..90fab2505d18de4929624b9eb86f12ab820526c3 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -139,24 +139,39 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s } export function enableLivePic(isMovingPhoto: boolean): void { - // [Start is_support1] - let isSupported: boolean = photoOutPut.isMovingPhotoSupported(); - // [End is_support1] - // [Start photo_out_put] - if (isSupported) { - photoOutPut.enableMovingPhoto(isMovingPhoto); + try { + // [Start is_support1] + let isSupported: boolean = photoOutPut.isMovingPhotoSupported(); + // [End is_support1] + // [Start photo_out_put] + if (isSupported) { + photoOutPut.enableMovingPhoto(isMovingPhoto); + } + // [End photo_out_put] + } catch (error) { + console.error(`The enableLivePic call failed. error: ${JSON.stringify(error)}`); } - // [End photo_out_put] } export function setPhotoZoom(zoom: number): void { - // [Start setZoom_Ratio] - photoSession.setZoomRatio(zoom); - // [End setZoom_Ratio] + try { + // [Start setZoom_Ratio] + photoSession.setZoomRatio(zoom); + // [End setZoom_Ratio] + } catch (error) { + console.error(`The enableLivePic call failed. error: ${JSON.stringify(error)}`); + } } export function getPhotoZoom(): number { - return photoSession.getZoomRatio(); + let zoomRatio = 1; + try { + zoomRatio = photoSession.getZoomRatio(); + } catch (error) { + console.error(`The getPhotoZoom call failed. error: ${JSON.stringify(error)}`); + } finally { + return zoomRatio; + } } export function setPhotoSmoothZoom(zoom: number): void { @@ -165,14 +180,18 @@ export function setPhotoSmoothZoom(zoom: number): void { export async function capture(isFront: boolean) { const curRotation = await getGravity(); - // [Start PhotoCapture_Setting] - let settings: camera.PhotoCaptureSetting = { - quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, - rotation: curRotation ? getPhotoRotation(photoOutPut, curRotation) : camera.ImageRotation.ROTATION_0, - mirror: isFront - }; - photoOutPut.capture(settings); - // [End PhotoCapture_Setting] + try { + // [Start PhotoCapture_Setting] + let settings: camera.PhotoCaptureSetting = { + quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, + rotation: curRotation ? getPhotoRotation(photoOutPut, curRotation) : camera.ImageRotation.ROTATION_0, + mirror: isFront + }; + photoOutPut.capture(settings); + // [End PhotoCapture_Setting] + } catch (error) { + console.error(`The capture call failed. error: ${error.code}`); + } } export async function burstCapture(isFront: boolean, captureCount: number) { @@ -191,28 +210,35 @@ export async function burstCapture(isFront: boolean, captureCount: number) { } export async function setPhotoFlashMode(flashMode: number): Promise { - photoSession.setFlashMode(flashMode); + try { + photoSession.setFlashMode(flashMode); + } catch (error) { + console.error(`The setPhotoFlashMode call failed. error: ${error.code}`); + } } export async function releaseCamera(): Promise { - - if (photoSession) { - photoSession.stop(); - } - if (cameraInput) { - cameraInput.close(); - } - if (previewOutput) { - previewOutput.release(); - } - if (photoSession) { - photoSession.release(); - } - if (photoOutPut) { - emitter.off('burstCaptureOff'); - photoOutPut.off('photoAssetAvailable'); - photoOutPut.off('captureReady'); - photoOutPut.release(); + try { + if (photoSession) { + photoSession.stop(); + } + if (cameraInput) { + cameraInput.close(); + } + if (previewOutput) { + previewOutput.release(); + } + if (photoSession) { + photoSession.release(); + } + if (photoOutPut) { + emitter.off('burstCaptureOff'); + photoOutPut.off('photoAssetAvailable'); + photoOutPut.off('captureReady'); + photoOutPut.release(); + } + } catch (error) { + console.error(`The releaseCamera call failed. error: ${error.code}`); } } diff --git a/entry/src/main/ets/utils/PreviewUtil.ets b/entry/src/main/ets/utils/PreviewUtil.ets index ea362dce414f9cb2d4b385f256b3aaef944d8bcc..b5423c380aebb1ee3d3061f3539a2cddef88c1a2 100644 --- a/entry/src/main/ets/utils/PreviewUtil.ets +++ b/entry/src/main/ets/utils/PreviewUtil.ets @@ -93,8 +93,14 @@ export function getTargetPreviewProfile(targetRatio: number, previewProfilesArra */ export function getCurrentWindowSize() { const windowClass = AppStorage.get('window'); - const windowSize = windowClass?.getWindowProperties().windowRect; - return { width: windowSize?.width ?? 0, height: windowSize?.height ?? 0 } as Size; + let size: Size = { width: 0, height: 0 }; + try { + const windowSize = windowClass?.getWindowProperties().windowRect; + size = { width: windowSize?.width ?? 0, height: windowSize?.height ?? 0 } + } catch (error) { + console.error(`The getCurrentWindowSize call failed. error: ${JSON.stringify(error)}`); + } + return size; } /** diff --git a/entry/src/main/ets/utils/VideoRecorder.ets b/entry/src/main/ets/utils/VideoRecorder.ets index f4906e7a93f2cf571bd1fcf85cf0f5bf1fa06f23..62ed7b9590410823bb92fc2262c9d82ba0069c3b 100644 --- a/entry/src/main/ets/utils/VideoRecorder.ets +++ b/entry/src/main/ets/utils/VideoRecorder.ets @@ -190,29 +190,44 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n } export async function stopRecordPreview(): Promise { - if (videoSession) { - videoSession.stop(); - } - if (cameraInput) { - cameraInput.close(); - } - if (previewOutput) { - previewOutput.release(); - } - if (videoSession) { - videoSession.release(); - } - if (videoOutput) { - videoOutput.release(); + try { + if (videoSession) { + videoSession.stop(); + } + if (cameraInput) { + cameraInput.close(); + } + if (previewOutput) { + previewOutput.release(); + } + if (videoSession) { + videoSession.release(); + } + if (videoOutput) { + videoOutput.release(); + } + } catch (error) { + console.error(`The stopRecordPreview call failed. error: ${JSON.stringify(error)}`); } } export function setVideoZoom(zoom: number): void { - videoSession.setZoomRatio(zoom); + try { + videoSession.setZoomRatio(zoom); + } catch (error) { + console.error(`The setVideoZoom call failed. error: ${JSON.stringify(error)}`); + } } export function getVideoZoom(): number { - return videoSession.getZoomRatio(); + let zoomRatio = 1; + try { + zoomRatio = videoSession.getZoomRatio(); + } catch (error) { + console.error(`The getVideoZoom call failed. error: ${JSON.stringify(error)}`); + } finally { + return zoomRatio; + } } export function setVideoSmoothZoom(zoom: number): void { @@ -222,23 +237,31 @@ export function setVideoSmoothZoom(zoom: number): void { export async function startRecord(): Promise { // Update the rotation angle before starting recording const deviceDegree = await getGravity(); - await avRecorder.updateRotation(getVideoRotation(videoOutput, deviceDegree)); - await videoOutput.start(); - // [Start recorder1] - await avRecorder.start(); - // [End recorder1] + try { + await avRecorder.updateRotation(getVideoRotation(videoOutput, deviceDegree)); + await videoOutput.start(); + // [Start recorder1] + await avRecorder.start(); + // [End recorder1] + } catch (error) { + console.error(`The startRecord call failed. error: ${JSON.stringify(error)}`); + } } export async function setVideoFlashMode(flashMode: number): Promise { - videoSession.setFlashMode(flashMode); + try { + videoSession.setFlashMode(flashMode); + } catch (error) { + console.error(`The setVideoFlashMode call failed. error: ${JSON.stringify(error)}`); + } } export async function stopRecord(): Promise { - // [Start recorder2] - await avRecorder.stop(); - // [End recorder2] - await avRecorder.release(); try { + // [Start recorder2] + await avRecorder.stop(); + // [End recorder2] + await avRecorder.release(); fileIo.closeSync(file); } catch (error) { console.error(`The stopRecord call failed. error: ${JSON.stringify(error)}`);