Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Budiyev committed Mar 8, 2022
1 parent d36d2d7 commit c9ebd00
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
38 changes: 24 additions & 14 deletions src/main/java/com/budiyev/android/codescanner/CodeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarcodeFormat> mFormats = DEFAULT_FORMATS;
private volatile ScanMode mScanMode = DEFAULT_SCAN_MODE;
private volatile AutoFocusMode mAutoFocusMode = DEFAULT_AUTO_FOCUS_MODE;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/budiyev/android/codescanner/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarcodeFormat> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public interface ErrorCallback {
* <br>
* 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);
}
2 changes: 1 addition & 1 deletion src/main/java/com/budiyev/android/codescanner/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit c9ebd00

Please sign in to comment.