mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 19:57:27 -08:00
Improved history function
git-svn-id: https://zxing.googlecode.com/svn/trunk@1066 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
1c4fe60220
commit
29b88b66e3
BIN
android/res/drawable/unknown_barcode.png
Normal file
BIN
android/res/drawable/unknown_barcode.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 510 B |
|
@ -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 {
|
||||
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());
|
||||
}
|
||||
|
||||
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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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},
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue