Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Budiyev committed Jan 9, 2018
1 parent 99c25b7 commit 83b19a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/budiyev/android/codescanner/CodeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class CodeScanner {
private final Camera.PreviewCallback mPreviewCallback;
private final Camera.AutoFocusCallback mSafeAutoFocusCallback;
private final Runnable mSafeAutoFocusTask;
private final Runnable mStopPreviewTask;
private final DecoderStateListener mDecoderStateListener;
private volatile List<BarcodeFormat> mFormats = DEFAULT_FORMATS;
private volatile ScanMode mScanMode = DEFAULT_SCAN_MODE;
Expand Down Expand Up @@ -115,7 +114,6 @@ public CodeScanner(@NonNull Context context, @NonNull CodeScannerView view) {
mPreviewCallback = new PreviewCallback();
mSafeAutoFocusCallback = new SafeAutoFocusCallback();
mSafeAutoFocusTask = new SafeAutoFocusTask();
mStopPreviewTask = new StopPreviewTask();
mDecoderStateListener = new DecoderStateListener();
mScannerView.setCodeScanner(this);
}
Expand Down Expand Up @@ -601,14 +599,15 @@ public void surfaceDestroyed(SurfaceHolder holder) {

private final class DecoderStateListener implements Decoder.StateListener {
@Override
public boolean onStateChanged(@NonNull Decoder.State state) {
public boolean onStateChanged(@NonNull Decoder decoder, @NonNull Decoder.State state) {
if (state == Decoder.State.DECODED) {
ScanMode scanMode = mScanMode;
if (scanMode == ScanMode.PREVIEW) {
return false;
} else if (scanMode == ScanMode.SINGLE) {
mStoppingPreview = true;
mMainThreadHandler.post(mStopPreviewTask);
decoder.setForceSkip(true);
mMainThreadHandler.post(new StopPreviewTask(decoder));
}
}
return true;
Expand Down Expand Up @@ -733,9 +732,16 @@ public void run() {
}

private final class StopPreviewTask implements Runnable {
private final Decoder mDecoder;

private StopPreviewTask(@NonNull Decoder decoder) {
mDecoder = decoder;
}

@Override
public void run() {
stopPreview();
mDecoder.setForceSkip(false);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/budiyev/android/codescanner/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class Decoder {
private final Map<DecodeHintType, Object> mHints;
private volatile DecodeCallback mCallback;
private volatile boolean mProcessingResult;
private volatile boolean mForceSkip;

public Decoder(@NonNull StateListener stateListener, @NonNull List<BarcodeFormat> formats,
@Nullable DecodeCallback callback) {
Expand Down Expand Up @@ -82,7 +83,11 @@ public void shutdown() {
}

public boolean shouldSkipTask() {
return mProcessingResult || mDecodeQueue.remainingCapacity() == 0;
return mForceSkip || mProcessingResult || mDecodeQueue.remainingCapacity() == 0;
}

public void setForceSkip(boolean forceSkip) {
mForceSkip = forceSkip;
}

private final class DecoderThread extends Thread {
Expand All @@ -100,18 +105,18 @@ public DecoderThread() {
public void run() {
for (; ; ) {
try {
mStateListener.onStateChanged(Decoder.State.IDLE);
mStateListener.onStateChanged(Decoder.this, Decoder.State.IDLE);
Result result = null;
try {
DecodeTask task = mDecodeQueue.take();
mStateListener.onStateChanged(Decoder.State.DECODING);
mStateListener.onStateChanged(Decoder.this, Decoder.State.DECODING);
result = task.decode(mReader);
} catch (ReaderException ignored) {
} finally {
if (result != null) {
mProcessingResult = true;
mDecodeQueue.clear();
if (mStateListener.onStateChanged(Decoder.State.DECODED)) {
if (mStateListener.onStateChanged(Decoder.this, Decoder.State.DECODED)) {
DecodeCallback callback = mCallback;
if (callback != null) {
callback.onDecoded(result);
Expand All @@ -128,7 +133,7 @@ public void run() {
}

public interface StateListener {
boolean onStateChanged(@NonNull State state);
boolean onStateChanged(@NonNull Decoder decoder, @NonNull State state);
}

public enum State {
Expand Down

0 comments on commit 83b19a1

Please sign in to comment.