From 0778f97730389e6ec224bc2a8aba9a6604e341ce Mon Sep 17 00:00:00 2001 From: Tim H Date: Thu, 5 Oct 2017 18:57:23 +0200 Subject: [PATCH] apply remaining suggestions --- README.md | 13 ++++++++++++- .../otaliastudios/cameraview/CameraController.java | 12 +++++++++--- .../com/otaliastudios/cameraview/CameraView.java | 12 +++++------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1c3fa1ac..18373dc2 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,17 @@ camera.addCameraListener(new CameraListener() { */ @Override public void onExposureCorrectionChanged(float newValue, float[] bounds, PointF[] fingers) {} + + /** + * 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. + */ + @Override + public void onError(CameraException exception) { + throw exception; + } }); ``` @@ -525,7 +536,7 @@ These are still things that need to be done, off the top of my head: - [ ] add a `setPreferredAspectRatio` API to choose the capture size. Preview size will adapt, and then, if let free, the CameraView will adapt as well - [ ] animate grid lines similar to stock camera app - [ ] add onRequestPermissionResults for easy permission callback -- [ ] better error handling, maybe with a onError(e) method in the public listener, or have each public method return a boolean +- [ ] better error handling, maybe extending the current onError(e) method to handle more use cases, or have each public method return a boolean - [ ] decent code coverage ## Device-specific issues diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 3ddcf32f..78cc64f1 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -98,7 +98,9 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraOpened(mOptions); } catch (Exception e) { - mCameraCallbacks.onError("Error while starting the camera engine.", e); + CameraException cameraException = + new CameraException("Error while starting the camera engine.", e); + mCameraCallbacks.onError(cameraException); } } }); @@ -121,7 +123,9 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraClosed(); } catch (Exception e) { - mCameraCallbacks.onError("Error while stopping the camera engine.", e); + CameraException cameraException = + new CameraException("Error while stopping the camera engine.", e); + mCameraCallbacks.onError(cameraException); } } }); @@ -168,7 +172,9 @@ abstract class CameraController implements CameraPreview.SurfaceCallback { mCameraCallbacks.dispatchOnCameraOpened(mOptions); } catch (Exception e) { - mCameraCallbacks.onError("Error while restarting the camera engine.", e); + CameraException cameraException = + new CameraException("Error while restarting the camera engine.", e); + mCameraCallbacks.onError(cameraException); } } }); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 7e56dc54..d2f441f5 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -1308,7 +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); + void onError(CameraException exception); } private class Callbacks implements CameraCallbacks { @@ -1560,21 +1560,19 @@ 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 + * @param exception the error cause */ @Override - public void onError(String message, Exception cause) { + public void onError(final CameraException exception) { // log - LOG.e(message, cause); + LOG.e(exception.getMessage(), exception.getCause()); // redirect - final CameraException cameraException = new CameraException(message, cause); mUiHandler.post(new Runnable() { @Override public void run() { for (CameraListener listener : mListeners) { - listener.onError(cameraException); + listener.onError(exception); } } });