Don't need to block multiple thread access. Refactor and update a bit for an upcoming...
[zxing.git] / android / src / com / google / zxing / client / android / CaptureActivity.java
index 687be89..da94c95 100755 (executable)
@@ -68,14 +68,11 @@ import android.widget.Toast;
 
 import java.io.IOException;
 import java.text.DateFormat;
-import java.util.Arrays;
 import java.util.Date;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
-import java.util.regex.Pattern;
 
 /**
  * The barcode reader activity itself. This is loosely based on the CameraPreview
@@ -88,8 +85,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
 
   private static final String TAG = CaptureActivity.class.getSimpleName();
 
-  private static final Pattern COMMA_PATTERN = Pattern.compile(",");
-
   private static final int SHARE_ID = Menu.FIRST;
   private static final int HISTORY_ID = Menu.FIRST + 1;
   private static final int SETTINGS_ID = Menu.FIRST + 2;
@@ -108,31 +103,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
   private static final String RETURN_CODE_PLACEHOLDER = "{CODE}";
   private static final String RETURN_URL_PARAM = "ret";
 
-  static final Vector<BarcodeFormat> PRODUCT_FORMATS;
-  static final Vector<BarcodeFormat> ONE_D_FORMATS;
-  static final Vector<BarcodeFormat> QR_CODE_FORMATS;
-  static final Vector<BarcodeFormat> ALL_FORMATS;
-
-  static {
-    PRODUCT_FORMATS = new Vector<BarcodeFormat>(5);
-    PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
-    PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
-    PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
-    PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
-    PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
-    ONE_D_FORMATS = new Vector<BarcodeFormat>(PRODUCT_FORMATS.size() + 4);
-    ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
-    ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
-    ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
-    ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
-    ONE_D_FORMATS.add(BarcodeFormat.ITF);
-    QR_CODE_FORMATS = new Vector<BarcodeFormat>(1);
-    QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
-    ALL_FORMATS = new Vector<BarcodeFormat>(ONE_D_FORMATS.size() + QR_CODE_FORMATS.size());
-    ALL_FORMATS.addAll(ONE_D_FORMATS);
-    ALL_FORMATS.addAll(QR_CODE_FORMATS);
-  }
-
   private static final Set<ResultMetadataType> DISPLAYABLE_METADATA_TYPES;
   static {
     DISPLAYABLE_METADATA_TYPES = new HashSet<ResultMetadataType>(5);
@@ -167,6 +137,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
   private String characterSet;
   private String versionName;
   private HistoryManager historyManager;
+  private InactivityTimer inactivityTimer;
 
   /**
    * When the beep has finished playing, rewind to queue up another one.
@@ -211,6 +182,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     hasSurface = false;
     historyManager = new HistoryManager(this);
     historyManager.trimHistory();
+    inactivityTimer = new InactivityTimer(this);
 
     showHelpOnFirstLaunch();
   }
@@ -239,13 +211,13 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
       if (action.equals(Intents.Scan.ACTION)) {
         // Scan the formats the intent requested, and return the result to the calling activity.
         source = Source.NATIVE_APP_INTENT;
-        decodeFormats = parseDecodeFormats(intent);
+        decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
       } else if (dataString != null && dataString.contains(PRODUCT_SEARCH_URL_PREFIX) &&
           dataString.contains(PRODUCT_SEARCH_URL_SUFFIX)) {
         // Scan only products and send the result to mobile Product Search.
         source = Source.PRODUCT_SEARCH_LINK;
         sourceUrl = dataString;
-        decodeFormats = PRODUCT_FORMATS;
+        decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;
       } else if (dataString != null && dataString.startsWith(ZXING_URL)) {
         // Scan formats requested in query string (all formats if none specified).
         // If a return URL is specified, send the results there. Otherwise, handle it ourselves.
@@ -253,7 +225,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
         sourceUrl = dataString;
         Uri inputUri = Uri.parse(sourceUrl);
         returnUrlTemplate = inputUri.getQueryParameter(RETURN_URL_PARAM);
-        decodeFormats = parseDecodeFormats(inputUri);
+        decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri);
       } else {
         // Scan all formats and handle the results ourselves (launched from Home).
         source = Source.NONE;
@@ -280,50 +252,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     initBeepSound();
   }
 
-  private static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) {
-    List<String> scanFormats = null;
-    String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS);
-    if (scanFormatsString != null) {
-      scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString));
-    }
-    return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE));
-  }
-
-  private static Vector<BarcodeFormat> parseDecodeFormats(Uri inputUri) {
-    List<String> formats = inputUri.getQueryParameters(Intents.Scan.SCAN_FORMATS);
-    if (formats != null && formats.size() == 1 && formats.get(0) != null){
-      formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0)));
-    }
-    return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE));
-  }
-
-  private static Vector<BarcodeFormat> parseDecodeFormats(List<String> scanFormats,
-                                                          String decodeMode) {
-    if (scanFormats != null) {
-      Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
-      try {
-        for (String format : scanFormats) {
-          formats.add(BarcodeFormat.valueOf(format));
-        }
-        return formats;
-      } catch (IllegalArgumentException iae) {
-        // ignore it then
-      }
-    }
-    if (decodeMode != null) {
-      if (Intents.Scan.PRODUCT_MODE.equals(decodeMode)) {
-        return PRODUCT_FORMATS;
-      }
-      if (Intents.Scan.QR_CODE_MODE.equals(decodeMode)) {
-        return QR_CODE_FORMATS;
-      }
-      if (Intents.Scan.ONE_D_MODE.equals(decodeMode)) {
-        return ONE_D_FORMATS;
-      }
-    }
-    return null;
-  }
-
   @Override
   protected void onPause() {
     super.onPause();
@@ -334,6 +262,12 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     CameraManager.get().closeDriver();
   }
 
+  @Override
+  protected void onDestroy() {
+    inactivityTimer.shutdown();
+    super.onDestroy();
+  }
+
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
@@ -449,6 +383,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
    * @param barcode   A greyscale bitmap of the camera data which was decoded.
    */
   public void handleDecode(Result rawResult, Bitmap barcode) {
+    inactivityTimer.onActivity();
     lastResult = rawResult;
     historyManager.addHistoryItem(rawResult);
     if (barcode == null) {
@@ -506,8 +441,13 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
       paint.setColor(getResources().getColor(R.color.result_points));
       if (points.length == 2) {
         paint.setStrokeWidth(4.0f);
-        canvas.drawLine(points[0].getX(), points[0].getY(), points[1].getX(),
-            points[1].getY(), paint);
+        drawLine(canvas, paint, points[0], points[1]);
+      } else if (points.length == 4 &&
+                 (rawResult.getBarcodeFormat().equals(BarcodeFormat.UPC_A)) ||
+                 (rawResult.getBarcodeFormat().equals(BarcodeFormat.EAN_13))) {
+        // Hacky special case -- draw two lines, for the barcode and metadata
+        drawLine(canvas, paint, points[0], points[1]);
+        drawLine(canvas, paint, points[2], points[3]);
       } else {
         paint.setStrokeWidth(10.0f);
         for (ResultPoint point : points) {
@@ -517,6 +457,10 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     }
   }
 
+  private static void drawLine(Canvas canvas, Paint paint, ResultPoint a, ResultPoint b) {
+    canvas.drawLine(a.getX(), a.getY(), b.getX(), b.getY(), paint);
+  }
+
   // Put up our own UI for how to handle the decoded contents.
   private void handleDecodeInternally(Result rawResult, Bitmap barcode) {
     statusView.setVisibility(View.GONE);