From c9ebd0084161f8561a5cd8267871ada38ece5e24 Mon Sep 17 00:00:00 2001 From: Yuriy Budiyev Date: Tue, 8 Mar 2022 14:16:21 +0300 Subject: [PATCH] Dev --- .../android/codescanner/CodeScanner.java | 38 ++++++++++++------- .../budiyev/android/codescanner/Decoder.java | 2 + .../android/codescanner/ErrorCallback.java | 4 +- .../budiyev/android/codescanner/Utils.java | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/budiyev/android/codescanner/CodeScanner.java b/src/main/java/com/budiyev/android/codescanner/CodeScanner.java index e35c38c..e7dfa69 100644 --- a/src/main/java/com/budiyev/android/codescanner/CodeScanner.java +++ b/src/main/java/com/budiyev/android/codescanner/CodeScanner.java @@ -105,6 +105,7 @@ public final class CodeScanner { private final Runnable mSafeAutoFocusTask; private final Runnable mStopPreviewTask; private final DecoderStateListener mDecoderStateListener; + private final ExceptionHandler mExceptionHandler; private volatile List mFormats = DEFAULT_FORMATS; private volatile ScanMode mScanMode = DEFAULT_SCAN_MODE; private volatile AutoFocusMode mAutoFocusMode = DEFAULT_AUTO_FOCUS_MODE; @@ -149,6 +150,7 @@ public CodeScanner(@NonNull final Context context, @NonNull final CodeScannerVie mSafeAutoFocusTask = new SafeAutoFocusTask(); mStopPreviewTask = new StopPreviewTask(); mDecoderStateListener = new DecoderStateListener(); + mExceptionHandler = new ExceptionHandler(); mScannerView.setCodeScanner(this); mScannerView.setSizeListener(new ScannerSizeListener()); } @@ -273,7 +275,7 @@ public ErrorCallback getErrorCallback() { } /** - * Camera initialization error callback. + * Camera initialization and image decoding error callback. * If not set, an exception will be thrown when error will occur. * * @param errorCallback Callback @@ -542,7 +544,10 @@ private void initialize(final int width, final int height) { if (width > 0 && height > 0) { mInitialization = true; mInitializationRequested = false; - new InitializationThread(width, height).start(); + final InitializationThread initializationThread = + new InitializationThread(width, height); + initializationThread.setUncaughtExceptionHandler(mExceptionHandler); + initializationThread.start(); } else { mInitializationRequested = true; } @@ -807,17 +812,7 @@ public InitializationThread(final int width, final int height) { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - try { - initialize(); - } catch (final Exception e) { - releaseResourcesInternal(); - final ErrorCallback errorCallback = mErrorCallback; - if (errorCallback != null) { - errorCallback.onError(e); - } else { - throw e; - } - } + initialize(); } private void initialize() { @@ -891,7 +886,8 @@ private void initialize() { camera.setDisplayOrientation(orientation); synchronized (mInitializeLock) { final Decoder decoder = - new Decoder(mDecoderStateListener, mFormats, mDecodeCallback); + new Decoder(mDecoderStateListener, mExceptionHandler, mFormats, + mDecodeCallback); mDecoderWrapper = new DecoderWrapper(camera, cameraInfo, decoder, imageSize, previewSize, viewSize, orientation, autoFocusSupported, flashSupported); @@ -903,6 +899,20 @@ private void initialize() { } } + private final class ExceptionHandler implements Thread.UncaughtExceptionHandler { + + @Override + public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable e) { + releaseResourcesInternal(); + final ErrorCallback errorCallback = mErrorCallback; + if (errorCallback != null) { + errorCallback.onError(e); + } else { + throw new CodeScannerException(e); + } + } + } + private final class TouchFocusCallback implements Camera.AutoFocusCallback { @Override public void onAutoFocus(final boolean success, @NonNull final Camera camera) { diff --git a/src/main/java/com/budiyev/android/codescanner/Decoder.java b/src/main/java/com/budiyev/android/codescanner/Decoder.java index 4a5f240..f20078e 100644 --- a/src/main/java/com/budiyev/android/codescanner/Decoder.java +++ b/src/main/java/com/budiyev/android/codescanner/Decoder.java @@ -49,9 +49,11 @@ final class Decoder { private volatile State mState; public Decoder(@NonNull final StateListener stateListener, + @NonNull final Thread.UncaughtExceptionHandler exceptionHandler, @NonNull final List formats, @Nullable final DecodeCallback callback) { mReader = new MultiFormatReader(); mDecoderThread = new DecoderThread(); + mDecoderThread.setUncaughtExceptionHandler(exceptionHandler); mHints = new EnumMap<>(DecodeHintType.class); mHints.put(DecodeHintType.POSSIBLE_FORMATS, formats); mReader.setHints(mHints); diff --git a/src/main/java/com/budiyev/android/codescanner/ErrorCallback.java b/src/main/java/com/budiyev/android/codescanner/ErrorCallback.java index 4e4094b..7554c71 100644 --- a/src/main/java/com/budiyev/android/codescanner/ErrorCallback.java +++ b/src/main/java/com/budiyev/android/codescanner/ErrorCallback.java @@ -46,11 +46,11 @@ public interface ErrorCallback { *
* Note that this method always called on a worker thread * - * @param error Exception that has been thrown + * @param thrown Throwable that has been thrown * @see Handler * @see Looper#getMainLooper() * @see Activity#runOnUiThread(Runnable) */ @WorkerThread - void onError(@NonNull Exception error); + void onError(@NonNull Throwable thrown); } diff --git a/src/main/java/com/budiyev/android/codescanner/Utils.java b/src/main/java/com/budiyev/android/codescanner/Utils.java index 101a18b..5f13ae5 100644 --- a/src/main/java/com/budiyev/android/codescanner/Utils.java +++ b/src/main/java/com/budiyev/android/codescanner/Utils.java @@ -347,7 +347,7 @@ public static Result decodeLuminanceSource(@NonNull final MultiFormatReader read public static final class SuppressErrorCallback implements ErrorCallback { @Override - public void onError(@NonNull final Exception error) { + public void onError(@NonNull final Throwable thrown) { // Do nothing } }