handle multiple error listeners

pull/59/merge^2
Tim H 7 years ago
parent 0778f97730
commit 48fb88ae0a
  1. 5
      README.md
  2. 4
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraException.java
  3. 13
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -220,8 +220,9 @@ camera.addCameraListener(new CameraListener() {
/** /**
* Notifies that an error occurred in any of the previously called methods. * 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 * 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 * error handling. As soon as this method was overridden at least once (without calling the
* custom error handling. * super method or re-throwing the exception), the default behavior will be disabled. So pay
* attention to not swallowing any exceptions.
*/ */
@Override @Override
public void onError(CameraException exception) { public void onError(CameraException exception) {

@ -5,7 +5,7 @@ package com.otaliastudios.cameraview;
*/ */
public class CameraException extends RuntimeException { public class CameraException extends RuntimeException {
public CameraException(String message, Throwable cause) { CameraException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
} }

@ -1571,8 +1571,19 @@ public class CameraView extends FrameLayout {
mUiHandler.post(new Runnable() { mUiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
// all error listeners will be called, but at most one of them should actually
// throw the exception
int count = 0;
for (CameraListener listener : mListeners) { for (CameraListener listener : mListeners) {
listener.onError(exception); try {
listener.onError(exception);
} catch (CameraException ce) {
count++;
}
}
if (count == mListeners.size()) {
throw exception;
} }
} }
}); });

Loading…
Cancel
Save