Improved history function
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 29 Sep 2009 22:33:03 +0000 (22:33 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 29 Sep 2009 22:33:03 +0000 (22:33 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1066 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/res/drawable/unknown_barcode.png [new file with mode: 0644]
android/src/com/google/zxing/client/android/CaptureActivity.java
android/src/com/google/zxing/client/android/history/DBHelper.java
android/src/com/google/zxing/client/android/history/HistoryManager.java
core/src/com/google/zxing/BarcodeFormat.java

diff --git a/android/res/drawable/unknown_barcode.png b/android/res/drawable/unknown_barcode.png
new file mode 100644 (file)
index 0000000..b05df59
Binary files /dev/null and b/android/res/drawable/unknown_barcode.png differ
index bc56bd8..eafe9ce 100755 (executable)
@@ -16,6 +16,7 @@
 
 package com.google.zxing.client.android;
 
+import android.graphics.drawable.BitmapDrawable;
 import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.client.android.result.ResultButtonListener;
@@ -60,7 +61,6 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
-import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -146,6 +146,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     lastResult = null;
     hasSurface = false;
     historyManager = new HistoryManager(this);
+    historyManager.trimHistory();
 
     showHelpOnFirstLaunch();
   }
@@ -329,8 +330,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
    */
   public void handleDecode(Result rawResult, Bitmap barcode) {
     lastResult = rawResult;
-    historyManager.addHistoryItem(rawResult.getText());
-    if (barcode != null) {
+    historyManager.addHistoryItem(rawResult);
+    if (barcode == null) {
+      // This is from history -- no saved barcode
+      handleDecodeInternally(rawResult, null);
+    } else {
       playBeepSoundAndVibrate();
       drawResultPoints(barcode, rawResult);
       switch (source) {
@@ -343,8 +347,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
           handleDecodeInternally(rawResult, barcode);
           break;
       }
-    } else {
-      handleDecodeInternally(rawResult, null);
     }
   }
 
@@ -385,24 +387,19 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     viewfinderView.setVisibility(View.GONE);
     resultView.setVisibility(View.VISIBLE);
 
-    ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
     if (barcode == null) {
-      barcodeImageView.setVisibility(View.GONE);
-    } else {
-      barcodeImageView.setVisibility(View.VISIBLE);      
-      barcodeImageView.setMaxWidth(MAX_RESULT_IMAGE_SIZE);
-      barcodeImageView.setMaxHeight(MAX_RESULT_IMAGE_SIZE);
-      barcodeImageView.setImageBitmap(barcode);
+      barcode = ((BitmapDrawable) getResources().getDrawable(R.drawable.unknown_barcode)).getBitmap();
     }
+    ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
+    barcodeImageView.setVisibility(View.VISIBLE);
+    barcodeImageView.setMaxWidth(MAX_RESULT_IMAGE_SIZE);
+    barcodeImageView.setMaxHeight(MAX_RESULT_IMAGE_SIZE);
+    barcodeImageView.setImageBitmap(barcode);
 
     TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
-    if (rawResult.getBarcodeFormat() == null) {
-      formatTextView.setVisibility(View.GONE);
-    } else {
-      formatTextView.setVisibility(View.VISIBLE);
-      formatTextView.setText(getString(R.string.msg_default_format) + ": " +
-          rawResult.getBarcodeFormat().toString());
-    }
+    formatTextView.setVisibility(View.VISIBLE);
+    formatTextView.setText(getString(R.string.msg_default_format) + ": " +
+        rawResult.getBarcodeFormat().toString());
 
     ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
     TextView typeTextView = (TextView) findViewById(R.id.type_text_view);
@@ -421,7 +418,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
     buttonView.requestFocus();
     for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) {
-      Button button = (Button) buttonView.getChildAt(x);
+      TextView button = (TextView) buttonView.getChildAt(x);
       if (x < buttonCount) {
         button.setVisibility(View.VISIBLE);
         button.setText(resultHandler.getButtonText(x));
index 4568e85..e40ed7a 100644 (file)
@@ -28,8 +28,10 @@ final class DBHelper extends SQLiteOpenHelper {
   private static final int DB_VERSION = 1;
   private static final String DB_NAME = "barcode_scanner_history.db";
   static final String TABLE_NAME = "history";
-  private static final String ID_COL = "id";
+  static final String ID_COL = "id";
   static final String TEXT_COL = "text";
+  static final String FORMAT_COL = "format";
+  static final String DISPLAY_COL = "display";
   static final String TIMESTAMP_COL = "timestamp";
 
   DBHelper(Context context) {
@@ -42,12 +44,16 @@ final class DBHelper extends SQLiteOpenHelper {
             "CREATE TABLE " + TABLE_NAME + " (" +
             ID_COL + " INTEGER PRIMARY KEY, " +
             TEXT_COL + " TEXT, " +
+            FORMAT_COL + " TEXT, " +
+            DISPLAY_COL + " TEXT, " +
             TIMESTAMP_COL + " INTEGER" +
             ");");
   }
 
   @Override
   public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
+    sqLiteDatabase.delete(TABLE_NAME, null, null);
+    onCreate(sqLiteDatabase);
   }
 
 }
index 5b8c76f..0b06152 100644 (file)
@@ -27,45 +27,57 @@ import android.os.Message;
 import java.util.List;
 import java.util.ArrayList;
 
+import com.google.zxing.BarcodeFormat;
 import com.google.zxing.client.android.R;
 import com.google.zxing.client.android.CaptureActivity;
 import com.google.zxing.Result;
 
 /**
+ * <p>Manages functionality related to scan history.</p>
+ * 
  * @author Sean Owen
  */
 public final class HistoryManager {
 
+  private static final int MAX_ITEMS = 20;
+  private static final String[] TEXT_COL_PROJECTION = { DBHelper.TEXT_COL };
+  private static final String[] TEXT_FORMAT_COL_PROJECTION = { DBHelper.TEXT_COL, DBHelper.FORMAT_COL };
+  private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
+
   private final CaptureActivity activity;
 
   public HistoryManager(CaptureActivity activity) {
     this.activity = activity;
   }
 
-  List<String> getHistoryItems() {
-
+  List<Result> getHistoryItems() {
     SQLiteOpenHelper helper = new DBHelper(activity);
+    List<Result> items = new ArrayList<Result>();
     SQLiteDatabase db = helper.getReadableDatabase();
-    List<String> items = new ArrayList<String>();
+    Cursor cursor = null;
     try {
-      Cursor cursor = db.query(DBHelper.TABLE_NAME,
-                               new String[] {DBHelper.TEXT_COL},
-                               null, null, null, null,
-                               DBHelper.TIMESTAMP_COL + " DESC");
+      cursor = db.query(DBHelper.TABLE_NAME,
+                        TEXT_FORMAT_COL_PROJECTION,
+                        null, null, null, null,
+                        DBHelper.TIMESTAMP_COL + " DESC");
       while (cursor.moveToNext()) {
-        items.add(cursor.getString(0));
+        Result result = new Result(cursor.getString(0), null, null, BarcodeFormat.valueOf(cursor.getString(1)));
+        items.add(result);
       }
     } finally {
+      if (cursor != null) {
+        cursor.close();
+      }
       db.close();
     }
     return items;
   }
 
   public AlertDialog buildAlert() {
-    List<String> items = getHistoryItems();
+    final List<Result> items = getHistoryItems();
     final String[] dialogItems = new String[items.size() + 1];
     for (int i = 0; i < items.size(); i++) {
-      dialogItems[i] = items.get(i);
+      dialogItems[i] = items.get(i).getText();
     }
     dialogItems[dialogItems.length - 1] = activity.getResources().getString(R.string.history_clear_text);
     DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@@ -73,7 +85,7 @@ public final class HistoryManager {
         if (i == dialogItems.length - 1) {
           clearHistory();
         } else {
-          Result result = new Result(dialogItems[i], null, null, null);
+          Result result = items.get(i);
           Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
           message.sendToTarget();
         }
@@ -85,20 +97,54 @@ public final class HistoryManager {
     return builder.create();
   }
 
-  public void addHistoryItem(String text) {
-
-    if (getHistoryItems().contains(text)) {
-      return;
-    }
+  public void addHistoryItem(Result result) {
 
     SQLiteOpenHelper helper = new DBHelper(activity);
     SQLiteDatabase db = helper.getWritableDatabase();
+    Cursor cursor = null;
     try {
+      cursor = db.query(DBHelper.TABLE_NAME,
+                        TEXT_COL_PROJECTION,
+                        DBHelper.TEXT_COL + "=?",
+                        new String[] { result.getText() },
+                        null, null, null, null);
+      if (cursor.moveToNext()) {
+        return;
+      }
       ContentValues values = new ContentValues();
-      values.put(DBHelper.TEXT_COL, text);
+      values.put(DBHelper.TEXT_COL, result.getText());
+      values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
+      values.put(DBHelper.DISPLAY_COL, result.getText()); // TODO use parsed result display value?
       values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
       db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
     } finally {
+      if (cursor != null) {
+        cursor.close();
+      }
+      db.close();
+    }
+  }
+
+  public void trimHistory() {
+    SQLiteOpenHelper helper = new DBHelper(activity);
+    SQLiteDatabase db = helper.getWritableDatabase();
+    Cursor cursor = null;
+    try {
+      cursor = db.query(DBHelper.TABLE_NAME,
+                        ID_COL_PROJECTION,
+                        null, null, null, null,
+                        DBHelper.TIMESTAMP_COL + " DESC");
+      int count = 0;
+      while (count < MAX_ITEMS && cursor.moveToNext()) {
+        count++;
+      }
+      while (cursor.moveToNext()) {
+        db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
+      }
+    } finally {
+      if (cursor != null) {
+        cursor.close();
+      }
       db.close();
     }
   }
index 8733fa7..49af6f3 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.google.zxing;
 
+import java.util.Hashtable;
+
 /**
  * Enumerates barcode formats known to this package.
  *
@@ -25,6 +27,8 @@ public final class BarcodeFormat {
 
   // No, we can't use an enum here. J2ME doesn't support it.
 
+  private static final Hashtable VALUES = new Hashtable();
+
   /** QR Code 2D barcode format. */
   public static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE");
 
@@ -59,10 +63,23 @@ public final class BarcodeFormat {
 
   private BarcodeFormat(String name) {
     this.name = name;
+    VALUES.put(name, this);
+  }
+
+  public String getName() {
+    return name;
   }
 
   public String toString() {
     return name;
   }
 
+  public static BarcodeFormat valueOf(String name) {
+    BarcodeFormat format = (BarcodeFormat) VALUES.get(name);
+    if (format == null) {
+      throw new IllegalArgumentException();
+    }
+    return format;
+  }
+
 }
\ No newline at end of file