From 77160163b88c204c07f7b48b48df3e181f07eefb Mon Sep 17 00:00:00 2001 From: Tim H Date: Sun, 12 Nov 2017 19:13:45 +0100 Subject: [PATCH] extend camera errors by exception specific information --- README.md | 9 ++- .../com/otaliastudios/cameraview/Camera1.java | 55 +++++++++++++------ .../CameraConfigurationFailedException.java | 30 +++++++++- .../otaliastudios/cameraview/CameraView.java | 5 +- .../CapturingVideoFailedException.java | 34 +++++++++++- 5 files changed, 109 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 88907a71..ab88a5b6 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,13 @@ camera.addCameraListener(new CameraListener() { } else if (exception instanceof CameraConfigurationFailedException) { // The previously started setting change failed, - // but the camera should be still available. + // but the camera should be still available. + if (exception.getConfiguration() == CONFIGURATION_FACING) { + // change GUI to reflect failed changes + } + else if (exception.getConfiguration() == CONFIGURATION_WHITE_BALANCE) { + // I don't care + } } else if (exception instanceof CapturingFailedException) { // The previously started capturing (of any kind) failed, but the camera @@ -224,6 +230,7 @@ camera.addCameraListener(new CameraListener() { else if (exception instanceof CapturingVideoFailedException) { // The previously started video capturing failed, but the camera // should be still available. + Video failedVideoFile = exception.getVideo(); } } else { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 427c5843..a82e5167 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -24,6 +24,17 @@ import static android.hardware.Camera.CAMERA_ERROR_SERVER_DIED; import static android.hardware.Camera.CAMERA_ERROR_UNKNOWN; import static android.media.MediaRecorder.MEDIA_ERROR_SERVER_DIED; import static android.media.MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_EXPOSURE_CORRECTION; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FACING; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FLASH; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FOCUS; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_HDR; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_LOCATION; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_OTHER; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_UNKNOWN; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_VIDEO_QUALITY; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_WHITE_BALANCE; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_ZOOM; @SuppressWarnings("deprecation") @@ -59,7 +70,7 @@ class Camera1 extends CameraController { // problem may be device-specific to the Samsung Galaxy J5 // TODO why does it fail occasionally and is it possible to prevent such errors? CameraException cameraException = new CameraConfigurationFailedException("Failed to " + - "reset auto focus.", e); + "reset auto focus.", CONFIGURATION_FOCUS, e); mCameraCallbacks.onError(cameraException); } } @@ -205,11 +216,11 @@ class Camera1 extends CameraController { } else if (errorCode == CAMERA_ERROR_UNKNOWN) { cameraException = new CameraConfigurationFailedException( - "Unspecified camera error."); + "Unspecified camera error.", CONFIGURATION_UNKNOWN); } else { cameraException = new CameraConfigurationFailedException( - "Received camera error code: " + errorCode); + "Received camera error code: " + errorCode, CONFIGURATION_OTHER); } // redirect error @@ -320,7 +331,8 @@ class Camera1 extends CameraController { } catch (Exception e) { CameraException cameraException = - new CameraConfigurationFailedException("Failed to set the location.", e); + new CameraConfigurationFailedException("Failed to set the location.", + CONFIGURATION_LOCATION, e); mCameraCallbacks.onError(cameraException); } } @@ -359,7 +371,8 @@ class Camera1 extends CameraController { // -> undo failed configuration change mFacing = oldFacing; CameraException cameraException = - new CameraConfigurationFailedException("Failed to set the camera facing.", e); + new CameraConfigurationFailedException("Failed to set the camera facing.", + CONFIGURATION_FACING, e); mCameraCallbacks.onError(cameraException); } } @@ -380,7 +393,8 @@ class Camera1 extends CameraController { catch (Exception e) { // TODO handle, !mergeWhiteBalance, too? CameraException cameraException = - new CameraConfigurationFailedException("Failed to set the white balance.", e); + new CameraConfigurationFailedException("Failed to set the white balance.", + CONFIGURATION_WHITE_BALANCE, e); mCameraCallbacks.onError(cameraException); } } @@ -409,7 +423,8 @@ class Camera1 extends CameraController { catch (Exception e) { // TODO handle, !mergeHdr, too? CameraException cameraException = - new CameraConfigurationFailedException("Failed to set hdr.", e); + new CameraConfigurationFailedException("Failed to set hdr.", CONFIGURATION_HDR, + e); mCameraCallbacks.onError(cameraException); } } @@ -449,7 +464,8 @@ class Camera1 extends CameraController { catch (Exception e) { // TODO handle, !mergeFlash, too? CameraException cameraException = - new CameraConfigurationFailedException("Failed to set flash.", e); + new CameraConfigurationFailedException("Failed to set flash.", + CONFIGURATION_FLASH, e); mCameraCallbacks.onError(cameraException); } } @@ -523,7 +539,8 @@ class Camera1 extends CameraController { } catch (Exception e) { CameraException cameraException = - new CameraConfigurationFailedException("Failed to set video quality.", e); + new CameraConfigurationFailedException("Failed to set video quality.", + CONFIGURATION_VIDEO_QUALITY, e); mCameraCallbacks.onError(cameraException); } } @@ -734,7 +751,7 @@ class Camera1 extends CameraController { } catch (Exception e) { CameraException cameraException = new CapturingVideoFailedException("Error while starting MediaRecorder. " + - "Swallowing.", e); + "Swallowing.", videoFile, e); mCameraCallbacks.onError(cameraException); mVideoFile = null; mCamera.lock(); @@ -743,7 +760,8 @@ class Camera1 extends CameraController { } } else { CameraException cameraException = - new CapturingVideoFailedException("Can't record video while session type is picture"); + new CapturingVideoFailedException("Can't record video while session type is " + + "picture", videoFile); mCameraCallbacks.onError(cameraException); return false; } @@ -816,7 +834,7 @@ class Camera1 extends CameraController { cameraException = new CapturingVideoFailedException( "Media server died while capturing a video. In this case, the" + "application must release the MediaRecorder object and " + - "instantiate a new one." + extraInfo); + "instantiate a new one." + extraInfo, mVideoFile); } else { cameraException = new CameraUnavailableException( @@ -828,12 +846,13 @@ class Camera1 extends CameraController { else if (what == MEDIA_RECORDER_ERROR_UNKNOWN) { // TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too? cameraException = new CameraConfigurationFailedException( - "Unspecified media recorder error." + extraInfo); + "Unspecified media recorder error." + extraInfo, CONFIGURATION_UNKNOWN); } else { // TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too? cameraException = new CameraConfigurationFailedException( - "Received media recorder error code: " + what + "." + extraInfo); + "Received media recorder error code: " + what + "." + extraInfo, + CONFIGURATION_OTHER); } // redirect error @@ -941,7 +960,8 @@ class Camera1 extends CameraController { } catch (Exception e) { CameraException cameraException = - new CameraConfigurationFailedException("Failed to set zoom.", e); + new CameraConfigurationFailedException("Failed to set zoom.", + CONFIGURATION_ZOOM, e); mCameraCallbacks.onError(cameraException); return false; } @@ -967,7 +987,8 @@ class Camera1 extends CameraController { } catch (Exception e) { CameraException cameraException = - new CameraConfigurationFailedException("Failed to set exposure correction.", e); + new CameraConfigurationFailedException("Failed to set exposure correction.", + CONFIGURATION_EXPOSURE_CORRECTION, e); mCameraCallbacks.onError(cameraException); return false; } @@ -1014,7 +1035,7 @@ class Camera1 extends CameraController { // at least getParameters and setParameters may fail. // TODO why do they fail and is it possible to prevent such errors? CameraException cameraException = new CameraConfigurationFailedException("Failed to " + - "start auto focus.", e); + "start auto focus.", CONFIGURATION_FOCUS, e); mCameraCallbacks.onError(cameraException); return false; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConfigurationFailedException.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConfigurationFailedException.java index ef035ed8..8103ae8d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConfigurationFailedException.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConfigurationFailedException.java @@ -6,11 +6,37 @@ package com.otaliastudios.cameraview; */ public class CameraConfigurationFailedException extends CameraException { - CameraConfigurationFailedException(String message) { + // possible values for this.configuration + public final static int CONFIGURATION_UNKNOWN = -1; + public final static int CONFIGURATION_OTHER = 0; + public final static int CONFIGURATION_FACING = 1; + public final static int CONFIGURATION_FLASH = 2; + public final static int CONFIGURATION_FOCUS = 3; + public final static int CONFIGURATION_ZOOM = 4; + public final static int CONFIGURATION_VIDEO_QUALITY = 5; + public final static int CONFIGURATION_JPEG_QUALITY = 6; + public final static int CONFIGURATION_HDR = 7; + public final static int CONFIGURATION_LOCATION = 8; + public final static int CONFIGURATION_EXPOSURE_CORRECTION = 9; + public final static int CONFIGURATION_WHITE_BALANCE = 10; + + private int configuration; + + CameraConfigurationFailedException(String message, int configuration) { super(message); + this.configuration = configuration; } - CameraConfigurationFailedException(String message, Throwable cause) { + CameraConfigurationFailedException(String message, int configuration, Throwable cause) { super(message, cause); + this.configuration = configuration; + } + + /** + * Get the type of configuration that failed. + * @return + */ + public int getConfiguration() { + return configuration; } } \ No newline at end of file diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index fa1efa54..bb29aecf 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -35,6 +35,7 @@ import static android.view.View.MeasureSpec.EXACTLY; import static android.view.View.MeasureSpec.UNSPECIFIED; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; +import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_JPEG_QUALITY; public class CameraView extends FrameLayout { @@ -1034,7 +1035,7 @@ public class CameraView extends FrameLayout { IllegalArgumentException illegalArgumentException = new IllegalArgumentException("JPEG quality should be > 0 and <= 100"); CameraException cameraException = new CameraConfigurationFailedException("Could not" + - " set JpegQuality", illegalArgumentException); + " set JpegQuality", CONFIGURATION_JPEG_QUALITY, illegalArgumentException); mCameraCallbacks.onError(cameraException); } mJpegQuality = jpegQuality; @@ -1206,7 +1207,7 @@ public class CameraView extends FrameLayout { public void startCapturingVideo(File file, long durationMillis) { if (durationMillis < 500) { CameraException cameraException = new CapturingVideoFailedException("Video duration" + - " can't be < 500 milliseconds"); + " can't be < 500 milliseconds", file); mCameraCallbacks.onError(cameraException); } startCapturingVideo(file); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CapturingVideoFailedException.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CapturingVideoFailedException.java index d9707b8d..5719f1e2 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CapturingVideoFailedException.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CapturingVideoFailedException.java @@ -1,4 +1,7 @@ package com.otaliastudios.cameraview; +import android.support.annotation.NonNull; + +import java.io.File; /** * An object of this class describes an error that occurred during the normal runtime of the camera. @@ -6,11 +9,38 @@ package com.otaliastudios.cameraview; */ public class CapturingVideoFailedException extends CapturingFailedException { - CapturingVideoFailedException(String message) { + @NonNull + private File video; + + /** + * + * @param message + * @param video The video file that was meant to store the captured video. + */ + CapturingVideoFailedException(String message, @NonNull File video) { super(message); + this.video = video; } - CapturingVideoFailedException(String message, Throwable cause) { + /** + * + * @param message + * @param video The video file that was meant to store the captured video. + * @param cause + */ + CapturingVideoFailedException(String message, @NonNull File video, Throwable cause) { super(message, cause); + this.video = video; + } + + /** + * Get the video file that was meant to store the captured video. + * The physical file itself will usually not exist (anymore), but you can use the file object + * information for further processing. + * @return + */ + @NonNull + public File getVideo() { + return video; } } \ No newline at end of file