Small style stuff
[zxing.git] / android / src / com / google / zxing / client / android / CaptureActivity.java
index c2276ee..90b0f3f 100755 (executable)
@@ -34,20 +34,16 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
-import android.content.res.AssetFileDescriptor;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Rect;
-import android.media.AudioManager;
-import android.media.MediaPlayer;
-import android.media.MediaPlayer.OnCompletionListener;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.os.Vibrator;
 import android.preference.PreferenceManager;
 import android.text.ClipboardManager;
 import android.util.Log;
@@ -67,14 +63,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
@@ -87,8 +80,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;
@@ -97,8 +88,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
 
   private static final long INTENT_RESULT_DURATION = 1500L;
   private static final long BULK_MODE_SCAN_DELAY_MS = 1000L;
-  private static final float BEEP_VOLUME = 0.10f;
-  private static final long VIBRATE_DURATION = 200L;
 
   private static final String PACKAGE_NAME = "com.google.zxing.client.android";
   private static final String PRODUCT_SEARCH_URL_PREFIX = "http://www.google";
@@ -107,31 +96,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() + 3);
-    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);
@@ -149,15 +113,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
   }
 
   private CaptureActivityHandler handler;
-
   private ViewfinderView viewfinderView;
   private TextView statusView;
   private View resultView;
-  private MediaPlayer mediaPlayer;
   private Result lastResult;
   private boolean hasSurface;
-  private boolean playBeep;
-  private boolean vibrate;
   private boolean copyToClipboard;
   private Source source;
   private String sourceUrl;
@@ -166,18 +126,10 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
   private String characterSet;
   private String versionName;
   private HistoryManager historyManager;
+  private InactivityTimer inactivityTimer;
+  private BeepManager beepManager;
 
-  /**
-   * When the beep has finished playing, rewind to queue up another one.
-   */
-  private final OnCompletionListener beepListener = new OnCompletionListener() {
-    public void onCompletion(MediaPlayer mediaPlayer) {
-      mediaPlayer.seekTo(0);
-    }
-  };
-
-  private final DialogInterface.OnClickListener aboutListener =
-      new DialogInterface.OnClickListener() {
+  private final DialogInterface.OnClickListener aboutListener = new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialogInterface, int i) {
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.zxing_url)));
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
@@ -210,6 +162,8 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     hasSurface = false;
     historyManager = new HistoryManager(this);
     historyManager.trimHistory();
+    inactivityTimer = new InactivityTimer(this);
+    beepManager = new BeepManager(this);
 
     showHelpOnFirstLaunch();
   }
@@ -238,13 +192,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.
@@ -252,7 +206,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;
@@ -266,61 +220,9 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     }
 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-    playBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
-    if (playBeep) {
-      // See if sound settings overrides this
-      AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
-      if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
-        playBeep = false;
-      }
-    }
-    vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false);
     copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true);
-    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;
+    beepManager.updatePrefs();
   }
 
   @Override
@@ -333,6 +235,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) {
@@ -448,13 +356,14 @@ 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) {
       // This is from history -- no saved barcode
       handleDecodeInternally(rawResult, null);
     } else {
-      playBeepSoundAndVibrate();
+      beepManager.playBeepSoundAndVibrate();
       drawResultPoints(barcode, rawResult);
       switch (source) {
         case NATIVE_APP_INTENT:
@@ -505,8 +414,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) {
@@ -516,6 +430,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);
@@ -524,11 +442,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
 
     ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
     if (barcode == null) {
-      barcodeImageView.setImageResource(R.drawable.launcher_icon_large);
+      barcodeImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
+          R.drawable.launcher_icon));
     } else {
       barcodeImageView.setImageBitmap(barcode);
     }
-    barcodeImageView.setVisibility(View.VISIBLE);
 
     TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
     formatTextView.setText(rawResult.getBarcodeFormat().toString());
@@ -664,42 +582,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     return false;
   }
 
-  /**
-   * Creates the beep MediaPlayer in advance so that the sound can be triggered with the least
-   * latency possible.
-   */
-  private void initBeepSound() {
-    if (playBeep && mediaPlayer == null) {
-      // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
-      // so we now play on the music stream.
-      setVolumeControlStream(AudioManager.STREAM_MUSIC);
-      mediaPlayer = new MediaPlayer();
-      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
-      mediaPlayer.setOnCompletionListener(beepListener);
-
-      AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
-      try {
-        mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
-            file.getLength());
-        file.close();
-        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
-        mediaPlayer.prepare();
-      } catch (IOException e) {
-        mediaPlayer = null;
-      }
-    }
-  }
-
-  private void playBeepSoundAndVibrate() {
-    if (playBeep && mediaPlayer != null) {
-      mediaPlayer.start();
-    }
-    if (vibrate) {
-      Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
-      vibrator.vibrate(VIBRATE_DURATION);
-    }
-  }
-
   private void initCamera(SurfaceHolder surfaceHolder) {
     try {
       CameraManager.get().openDriver(surfaceHolder);
@@ -723,11 +605,8 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle(getString(R.string.app_name));
     builder.setMessage(getString(R.string.msg_camera_framework_bug));
-    builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
-      public void onClick(DialogInterface dialogInterface, int i) {
-        finish();
-      }
-    });
+    builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
+    builder.setOnCancelListener(new FinishListener(this));
     builder.show();
   }