implement basic error handling

pull/59/merge^2
Tim H 7 years ago
parent aeb833a711
commit 8a16835560
  1. 12
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  2. 11
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java
  3. 13
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java
  4. 19
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.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);
}
}
});

@ -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);
}
}

@ -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;
}
}

@ -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

Loading…
Cancel
Save