Issue 460, auto timeout of CaptureActivity after inactivity, for testing. Also break...
[zxing.git] / android / src / com / google / zxing / client / android / DecodeThread.java
index f297361..52a5a8d 100755 (executable)
@@ -27,6 +27,7 @@ import android.preference.PreferenceManager;
 
 import java.util.Hashtable;
 import java.util.Vector;
+import java.util.concurrent.CountDownLatch;
 
 /**
  * This thread does all the heavy lifting of decoding the images.
@@ -40,6 +41,7 @@ final class DecodeThread extends Thread {
   private final CaptureActivity activity;
   private final Hashtable<DecodeHintType, Object> hints;
   private Handler handler;
+  private final CountDownLatch handlerInitLatch;
 
   DecodeThread(CaptureActivity activity,
                Vector<BarcodeFormat> decodeFormats,
@@ -47,6 +49,7 @@ final class DecodeThread extends Thread {
                ResultPointCallback resultPointCallback) {
 
     this.activity = activity;
+    handlerInitLatch = new CountDownLatch(1);
 
     hints = new Hashtable<DecodeHintType, Object>(3);
 
@@ -56,11 +59,11 @@ final class DecodeThread extends Thread {
       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
       if (decode1D && decodeQR) {
-        hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ALL_FORMATS);
+        hints.put(DecodeHintType.POSSIBLE_FORMATS, DecodeFormatManager.ALL_FORMATS);
       } else if (decode1D) {
-        hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ONE_D_FORMATS);
+        hints.put(DecodeHintType.POSSIBLE_FORMATS, DecodeFormatManager.ONE_D_FORMATS);
       } else if (decodeQR) {
-        hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.QR_CODE_FORMATS);
+        hints.put(DecodeHintType.POSSIBLE_FORMATS, DecodeFormatManager.QR_CODE_FORMATS);
       }
     } else {
       hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
@@ -74,13 +77,19 @@ final class DecodeThread extends Thread {
   }
 
   Handler getHandler() {
+    try {
+      handlerInitLatch.await();
+    } catch (InterruptedException ie) {
+      // continue?
+    }
     return handler;
   }
 
   @Override
   public void run() {
     Looper.prepare();
-    handler = new DecodeHandler(activity, hints);    
+    handler = new DecodeHandler(activity, hints);
+    handlerInitLatch.countDown();
     Looper.loop();
   }