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;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
-import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
lastResult = null;
hasSurface = false;
historyManager = new HistoryManager(this);
+ historyManager.trimHistory();
showHelpOnFirstLaunch();
}
*/
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) {
handleDecodeInternally(rawResult, barcode);
break;
}
- } else {
- handleDecodeInternally(rawResult, null);
}
}
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);
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));
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) {
"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);
}
}
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() {
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();
}
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();
}
}
package com.google.zxing;
+import java.util.Hashtable;
+
/**
* Enumerates barcode formats known to this package.
*
// 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");
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