From 8a168355600061f615f8cb49519a8193bb21fc76 Mon Sep 17 00:00:00 2001 From: Tim H Date: Thu, 5 Oct 2017 13:47:40 +0200 Subject: [PATCH] implement basic error handling --- .../cameraview/CameraController.java | 12 ++++-------- .../cameraview/CameraException.java | 11 +++++++++++ .../cameraview/CameraListener.java | 13 +++++++++++++ .../otaliastudios/cameraview/CameraView.java | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index c9c1038c..32e1bf63 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -98,8 +98,7 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraOpened(mOptions); } catch (Exception e) { - LOG.e("Error while starting the camera engine.", e); - throw new RuntimeException(e); + mCameraCallbacks.onError("Error while starting the camera engine.", e); } } }); @@ -122,8 +121,7 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraClosed(); } catch (Exception e) { - LOG.e("Error while stopping the camera engine.", e); - throw new RuntimeException(e); + mCameraCallbacks.onError("Error while stopping the camera engine.", e); } } }); @@ -140,8 +138,8 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { LOG.i("Stop immediately. Stopped. State is:", ss()); } catch (Exception e) { // Do nothing. - LOG.i("Stop immediately. Exception while stopping.", e); mState = STATE_STOPPED; + mCameraCallbacks.onError("Stop immediately. Exception while stopping.", e); } } @@ -170,9 +168,7 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraOpened(mOptions); } catch (Exception e) { - LOG.e("Error while restarting the camera engine.", e); - throw new RuntimeException(e); - + mCameraCallbacks.onError("Error while restarting the camera engine.", e); } } }); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java new file mode 100644 index 00000000..8e28309b --- /dev/null +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java @@ -0,0 +1,11 @@ +package com.otaliastudios.cameraview; + +/** + * An object of this class describes an error that occurred during the normal runtime of the camera. + */ +public class CameraException extends RuntimeException { + + public CameraException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java index 1da71188..295f6165 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java @@ -130,4 +130,17 @@ public abstract class CameraListener { } + /** + * Notifies that an error occurred in any of the previously called methods. + * + * The default implementation will just throw the original exception again to prevent missing + * error handling. Override this method without calling the super method in order to implement + * custom error handling. + * + * @param exception the caught exception + */ + @UiThread + public void onError(CameraException exception) { + throw exception; + } } \ 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 5c97f25e..2ddfd678 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -1308,6 +1308,7 @@ public class CameraView extends FrameLayout { void dispatchOnFocusEnd(@Nullable Gesture trigger, boolean success, PointF where); void dispatchOnZoomChanged(final float newValue, final PointF[] fingers); void dispatchOnExposureCorrectionChanged(float newValue, float[] bounds, PointF[] fingers); + void onError(String message, Exception cause); } private class Callbacks implements CameraCallbacks { @@ -1555,6 +1556,24 @@ public class CameraView extends FrameLayout { } }); } + + /** + * Log and redirect the given error information to the CameraListeners. + * + * @param message a message describing which camera action failed + * @param cause the original exception that caused the error + */ + @Override + public void onError(String message, Exception cause) { + // log + LOG.e(message, cause); + + // redirect + CameraException cameraException = new CameraException(message, cause); + for (CameraListener listener : mListeners) { + listener.onError(cameraException); + } + } } //endregion