Improved history function

git-svn-id: https://zxing.googlecode.com/svn/trunk@1066 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-09-29 22:33:03 +00:00
parent 1c4fe60220
commit 29b88b66e3
5 changed files with 104 additions and 38 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 510 B

View file

@ -16,6 +16,7 @@
package com.google.zxing.client.android; package com.google.zxing.client.android;
import android.graphics.drawable.BitmapDrawable;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.ResultPoint; import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.result.ResultButtonListener; import com.google.zxing.client.android.result.ResultButtonListener;
@ -60,7 +61,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -146,6 +146,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
lastResult = null; lastResult = null;
hasSurface = false; hasSurface = false;
historyManager = new HistoryManager(this); historyManager = new HistoryManager(this);
historyManager.trimHistory();
showHelpOnFirstLaunch(); showHelpOnFirstLaunch();
} }
@ -329,8 +330,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
*/ */
public void handleDecode(Result rawResult, Bitmap barcode) { public void handleDecode(Result rawResult, Bitmap barcode) {
lastResult = rawResult; lastResult = rawResult;
historyManager.addHistoryItem(rawResult.getText()); historyManager.addHistoryItem(rawResult);
if (barcode != null) { if (barcode == null) {
// This is from history -- no saved barcode
handleDecodeInternally(rawResult, null);
} else {
playBeepSoundAndVibrate(); playBeepSoundAndVibrate();
drawResultPoints(barcode, rawResult); drawResultPoints(barcode, rawResult);
switch (source) { switch (source) {
@ -343,8 +347,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
handleDecodeInternally(rawResult, barcode); handleDecodeInternally(rawResult, barcode);
break; break;
} }
} else {
handleDecodeInternally(rawResult, null);
} }
} }
@ -385,24 +387,19 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
viewfinderView.setVisibility(View.GONE); viewfinderView.setVisibility(View.GONE);
resultView.setVisibility(View.VISIBLE); resultView.setVisibility(View.VISIBLE);
ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
if (barcode == null) { if (barcode == null) {
barcodeImageView.setVisibility(View.GONE); barcode = ((BitmapDrawable) getResources().getDrawable(R.drawable.unknown_barcode)).getBitmap();
} else {
barcodeImageView.setVisibility(View.VISIBLE);
barcodeImageView.setMaxWidth(MAX_RESULT_IMAGE_SIZE);
barcodeImageView.setMaxHeight(MAX_RESULT_IMAGE_SIZE);
barcodeImageView.setImageBitmap(barcode);
} }
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); TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
if (rawResult.getBarcodeFormat() == null) { formatTextView.setVisibility(View.VISIBLE);
formatTextView.setVisibility(View.GONE); formatTextView.setText(getString(R.string.msg_default_format) + ": " +
} else { rawResult.getBarcodeFormat().toString());
formatTextView.setVisibility(View.VISIBLE);
formatTextView.setText(getString(R.string.msg_default_format) + ": " +
rawResult.getBarcodeFormat().toString());
}
ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult); ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
TextView typeTextView = (TextView) findViewById(R.id.type_text_view); 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); ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
buttonView.requestFocus(); buttonView.requestFocus();
for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) { 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) { if (x < buttonCount) {
button.setVisibility(View.VISIBLE); button.setVisibility(View.VISIBLE);
button.setText(resultHandler.getButtonText(x)); button.setText(resultHandler.getButtonText(x));

View file

@ -28,8 +28,10 @@ final class DBHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1; private static final int DB_VERSION = 1;
private static final String DB_NAME = "barcode_scanner_history.db"; private static final String DB_NAME = "barcode_scanner_history.db";
static final String TABLE_NAME = "history"; 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 TEXT_COL = "text";
static final String FORMAT_COL = "format";
static final String DISPLAY_COL = "display";
static final String TIMESTAMP_COL = "timestamp"; static final String TIMESTAMP_COL = "timestamp";
DBHelper(Context context) { DBHelper(Context context) {
@ -42,12 +44,16 @@ final class DBHelper extends SQLiteOpenHelper {
"CREATE TABLE " + TABLE_NAME + " (" + "CREATE TABLE " + TABLE_NAME + " (" +
ID_COL + " INTEGER PRIMARY KEY, " + ID_COL + " INTEGER PRIMARY KEY, " +
TEXT_COL + " TEXT, " + TEXT_COL + " TEXT, " +
FORMAT_COL + " TEXT, " +
DISPLAY_COL + " TEXT, " +
TIMESTAMP_COL + " INTEGER" + TIMESTAMP_COL + " INTEGER" +
");"); ");");
} }
@Override @Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.delete(TABLE_NAME, null, null);
onCreate(sqLiteDatabase);
} }
} }

View file

@ -27,45 +27,57 @@ import android.os.Message;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.android.R; import com.google.zxing.client.android.R;
import com.google.zxing.client.android.CaptureActivity; import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.Result; import com.google.zxing.Result;
/** /**
* <p>Manages functionality related to scan history.</p>
*
* @author Sean Owen * @author Sean Owen
*/ */
public final class HistoryManager { 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; private final CaptureActivity activity;
public HistoryManager(CaptureActivity activity) { public HistoryManager(CaptureActivity activity) {
this.activity = activity; this.activity = activity;
} }
List<String> getHistoryItems() { List<Result> getHistoryItems() {
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
List<Result> items = new ArrayList<Result>();
SQLiteDatabase db = helper.getReadableDatabase(); SQLiteDatabase db = helper.getReadableDatabase();
List<String> items = new ArrayList<String>(); Cursor cursor = null;
try { try {
Cursor cursor = db.query(DBHelper.TABLE_NAME, cursor = db.query(DBHelper.TABLE_NAME,
new String[] {DBHelper.TEXT_COL}, TEXT_FORMAT_COL_PROJECTION,
null, null, null, null, null, null, null, null,
DBHelper.TIMESTAMP_COL + " DESC"); DBHelper.TIMESTAMP_COL + " DESC");
while (cursor.moveToNext()) { 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 { } finally {
if (cursor != null) {
cursor.close();
}
db.close(); db.close();
} }
return items; return items;
} }
public AlertDialog buildAlert() { public AlertDialog buildAlert() {
List<String> items = getHistoryItems(); final List<Result> items = getHistoryItems();
final String[] dialogItems = new String[items.size() + 1]; final String[] dialogItems = new String[items.size() + 1];
for (int i = 0; i < items.size(); i++) { 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); dialogItems[dialogItems.length - 1] = activity.getResources().getString(R.string.history_clear_text);
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@ -73,7 +85,7 @@ public final class HistoryManager {
if (i == dialogItems.length - 1) { if (i == dialogItems.length - 1) {
clearHistory(); clearHistory();
} else { } 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 message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, result);
message.sendToTarget(); message.sendToTarget();
} }
@ -85,20 +97,54 @@ public final class HistoryManager {
return builder.create(); return builder.create();
} }
public void addHistoryItem(String text) { public void addHistoryItem(Result result) {
if (getHistoryItems().contains(text)) {
return;
}
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = helper.getWritableDatabase(); SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = null;
try { 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(); 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()); values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values); db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
} finally { } 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(); db.close();
} }
} }

View file

@ -16,6 +16,8 @@
package com.google.zxing; package com.google.zxing;
import java.util.Hashtable;
/** /**
* Enumerates barcode formats known to this package. * 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. // 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. */ /** QR Code 2D barcode format. */
public static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE"); public static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE");
@ -59,10 +63,23 @@ public final class BarcodeFormat {
private BarcodeFormat(String name) { private BarcodeFormat(String name) {
this.name = name; this.name = name;
VALUES.put(name, this);
}
public String getName() {
return name;
} }
public String toString() { public String toString() {
return name; return name;
} }
public static BarcodeFormat valueOf(String name) {
BarcodeFormat format = (BarcodeFormat) VALUES.get(name);
if (format == null) {
throw new IllegalArgumentException();
}
return format;
}
} }