Issue 923 user supplied patch to remember supplemental info in history

git-svn-id: https://zxing.googlecode.com/svn/trunk@1873 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-08-02 10:46:10 +00:00
parent c9d8400936
commit 3f26da0f80
6 changed files with 111 additions and 47 deletions

View file

@ -501,8 +501,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
supplementTextView.setOnClickListener(null); supplementTextView.setOnClickListener(null);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean( if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
PreferencesActivity.KEY_SUPPLEMENTAL, true)) { PreferencesActivity.KEY_SUPPLEMENTAL, true)) {
SupplementalInfoRetriever.maybeInvokeRetrieval(supplementTextView, resultHandler.getResult(), SupplementalInfoRetriever.maybeInvokeRetrieval(supplementTextView,
handler, this); resultHandler.getResult(),
handler,
historyManager,
this);
} }
int buttonCount = resultHandler.getButtonCount(); int buttonCount = resultHandler.getButtonCount();

View file

@ -25,7 +25,7 @@ import android.content.Context;
*/ */
final class DBHelper extends SQLiteOpenHelper { final class DBHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 4; private static final int DB_VERSION = 5;
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";
static final String ID_COL = "id"; static final String ID_COL = "id";
@ -33,6 +33,7 @@ final class DBHelper extends SQLiteOpenHelper {
static final String FORMAT_COL = "format"; static final String FORMAT_COL = "format";
static final String DISPLAY_COL = "display"; static final String DISPLAY_COL = "display";
static final String TIMESTAMP_COL = "timestamp"; static final String TIMESTAMP_COL = "timestamp";
static final String DETAILS_COL = "details";
DBHelper(Context context) { DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION); super(context, DB_NAME, null, DB_VERSION);
@ -46,8 +47,8 @@ final class DBHelper extends SQLiteOpenHelper {
TEXT_COL + " TEXT, " + TEXT_COL + " TEXT, " +
FORMAT_COL + " TEXT, " + FORMAT_COL + " TEXT, " +
DISPLAY_COL + " TEXT, " + DISPLAY_COL + " TEXT, " +
TIMESTAMP_COL + " INTEGER" + TIMESTAMP_COL + " INTEGER, " +
");"); DETAILS_COL + " TEXT);");
} }
@Override @Override

View file

@ -54,22 +54,19 @@ import java.util.List;
* @author Sean Owen * @author Sean Owen
*/ */
public final class HistoryManager { public final class HistoryManager {
private static final String TAG = HistoryManager.class.getSimpleName(); private static final String TAG = HistoryManager.class.getSimpleName();
private static final int MAX_ITEMS = 500; private static final int MAX_ITEMS = 500;
private static final String[] GET_ITEM_COL_PROJECTION = { private static final String[] COLUMNS = {
DBHelper.TEXT_COL,
DBHelper.DISPLAY_COL,
DBHelper.FORMAT_COL,
DBHelper.TIMESTAMP_COL,
};
private static final String[] EXPORT_COL_PROJECTION = {
DBHelper.TEXT_COL, DBHelper.TEXT_COL,
DBHelper.DISPLAY_COL, DBHelper.DISPLAY_COL,
DBHelper.FORMAT_COL, DBHelper.FORMAT_COL,
DBHelper.TIMESTAMP_COL, DBHelper.TIMESTAMP_COL,
DBHelper.DETAILS_COL,
}; };
private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL }; private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance(); private static final DateFormat EXPORT_DATE_TIME_FORMAT = DateFormat.getDateTimeInstance();
@ -80,25 +77,42 @@ public final class HistoryManager {
} }
public AlertDialog buildAlert() { public AlertDialog buildAlert() {
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
List<Result> items = new ArrayList<Result>(); List<Result> items = new ArrayList<Result>();
List<String> dialogItems = new ArrayList<String>(); List<String> dialogItems = new ArrayList<String>();
SQLiteDatabase db = null; SQLiteDatabase db = null;
Cursor cursor = null; Cursor cursor = null;
try { try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, GET_ITEM_COL_PROJECTION, null, null, null, null, db = helper.getReadableDatabase();
DBHelper.TIMESTAMP_COL + " DESC"); cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
Result result = new Result(cursor.getString(0), null, null,
BarcodeFormat.valueOf(cursor.getString(2)), cursor.getLong(3)); String text = cursor.getString(0);
String format = cursor.getString(2);
long timestamp = cursor.getLong(3);
Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
items.add(result); items.add(result);
StringBuilder displayResult = new StringBuilder();
String display = cursor.getString(1); String display = cursor.getString(1);
if (display == null || display.length() == 0) { if (display == null || display.length() == 0) {
display = result.getText(); display = result.getText();
} }
dialogItems.add(display); displayResult.append(display);
String details = cursor.getString(4);
if (details != null && details.length() > 0) {
displayResult.append(" : ").append(details);
} }
dialogItems.add(displayResult.toString());
}
} catch (SQLiteException sqle) { } catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle); Log.w(TAG, "Error while opening database", sqle);
} finally { } finally {
@ -133,6 +147,12 @@ public final class HistoryManager {
deletePrevious(result.getText()); deletePrevious(result.getText());
} }
ContentValues values = new ContentValues();
values.put(DBHelper.TEXT_COL, result.getText());
values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db;
try { try {
@ -143,17 +163,37 @@ public final class HistoryManager {
} }
try { try {
// Insert the new entry into the DB. // Insert the new entry into the DB.
ContentValues values = new ContentValues();
values.put(DBHelper.TEXT_COL, result.getText());
values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
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 {
db.close(); db.close();
} }
} }
public void addHistoryItemDetails(String itemID, String itemDetails) {
// As we're going to do an update only we don't need need to worry
// about the preferences; if the item wasn't saved it won't be udpated
ContentValues values = new ContentValues();
values.put(DBHelper.DETAILS_COL, itemDetails);
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db;
try {
db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
}
try {
// Update the details for the ID entry into the DB.
db.update(DBHelper.TABLE_NAME, values, DBHelper.TEXT_COL + "=?", new String[] {itemID});
} finally {
db.close();
}
}
private void deletePrevious(String text) { private void deletePrevious(String text) {
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db;
@ -227,15 +267,15 @@ public final class HistoryManager {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = db.query(DBHelper.TABLE_NAME, cursor = db.query(DBHelper.TABLE_NAME,
EXPORT_COL_PROJECTION, COLUMNS,
null, null, null, null, null, null, null, null,
DBHelper.TIMESTAMP_COL + " DESC"); DBHelper.TIMESTAMP_COL + " DESC");
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
for (int col = 0; col < EXPORT_COL_PROJECTION.length; col++) { for (int col = 0; col < COLUMNS.length; col++) {
historyText.append('"').append(massageHistoryField(cursor.getString(col))).append("\","); historyText.append('"').append(massageHistoryField(cursor.getString(col))).append("\",");
} }
// Add timestamp again, formatted // Add timestamp again, formatted
long timestamp = cursor.getLong(EXPORT_COL_PROJECTION.length - 1); long timestamp = cursor.getLong(3);
historyText.append('"').append(massageHistoryField( historyText.append('"').append(massageHistoryField(
EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp)))).append("\"\r\n"); EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp)))).append("\"\r\n");
} }
@ -276,7 +316,7 @@ public final class HistoryManager {
} }
private static String massageHistoryField(String value) { private static String massageHistoryField(String value) {
return value.replace("\"","\"\""); return value == null ? "" : value.replace("\"","\"\"");
} }
void clearHistory() { void clearHistory() {

View file

@ -20,6 +20,7 @@ import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import com.google.zxing.client.android.history.HistoryManager;
import com.google.zxing.client.android.AndroidHttpClient; import com.google.zxing.client.android.AndroidHttpClient;
import com.google.zxing.client.android.LocaleManager; import com.google.zxing.client.android.LocaleManager;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
@ -48,9 +49,12 @@ final class ProductResultInfoRetriever extends SupplementalInfoRetriever {
private final String productID; private final String productID;
ProductResultInfoRetriever(TextView textView, String productID, Handler handler, ProductResultInfoRetriever(TextView textView,
String productID,
Handler handler,
HistoryManager historyManager,
Context context) { Context context) {
super(textView, handler, context); super(textView, handler, historyManager, context);
this.productID = productID; this.productID = productID;
} }
@ -71,8 +75,8 @@ final class ProductResultInfoRetriever extends SupplementalInfoRetriever {
String content = consume(response.getEntity()); String content = consume(response.getEntity());
Matcher matcher = PRODUCT_NAME_PRICE_PATTERN.matcher(content); Matcher matcher = PRODUCT_NAME_PRICE_PATTERN.matcher(content);
if (matcher.find()) { if (matcher.find()) {
append(matcher.group(1)); String details = matcher.group(1) + ' ' + matcher.group(2);
append(matcher.group(2)); append(productID, details);
} }
setLink(uri); setLink(uri);
} }

View file

@ -21,13 +21,13 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.text.Html; import android.text.Html;
import android.text.Spanned;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.google.zxing.client.result.ISBNParsedResult; import com.google.zxing.client.result.ISBNParsedResult;
import com.google.zxing.client.result.ParsedResult; import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ProductParsedResult; import com.google.zxing.client.result.ProductParsedResult;
import com.google.zxing.client.result.URIParsedResult; import com.google.zxing.client.result.URIParsedResult;
import com.google.zxing.client.android.history.HistoryManager;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -55,17 +55,26 @@ public abstract class SupplementalInfoRetriever implements Callable<Void> {
return executorInstance; return executorInstance;
} }
public static void maybeInvokeRetrieval(TextView textView, ParsedResult result, Handler handler, public static void maybeInvokeRetrieval(TextView textView,
ParsedResult result,
Handler handler,
HistoryManager historyManager,
Context context) { Context context) {
SupplementalInfoRetriever retriever = null; SupplementalInfoRetriever retriever = null;
if (result instanceof URIParsedResult) { if (result instanceof URIParsedResult) {
retriever = new URIResultInfoRetriever(textView, (URIParsedResult) result, handler, context); retriever = new URIResultInfoRetriever(textView, (URIParsedResult) result, handler, historyManager, context);
} else if (result instanceof ProductParsedResult) { } else if (result instanceof ProductParsedResult) {
retriever = new ProductResultInfoRetriever(textView, retriever = new ProductResultInfoRetriever(textView,
((ProductParsedResult) result).getProductID(), handler, context); ((ProductParsedResult) result).getProductID(),
handler,
historyManager,
context);
} else if (result instanceof ISBNParsedResult) { } else if (result instanceof ISBNParsedResult) {
retriever = new ProductResultInfoRetriever(textView, ((ISBNParsedResult) result).getISBN(), retriever = new ProductResultInfoRetriever(textView,
handler, context); ((ISBNParsedResult) result).getISBN(),
handler,
historyManager,
context);
} }
if (retriever != null) { if (retriever != null) {
ExecutorService executor = getExecutorService(); ExecutorService executor = getExecutorService();
@ -78,11 +87,13 @@ public abstract class SupplementalInfoRetriever implements Callable<Void> {
private final WeakReference<TextView> textViewRef; private final WeakReference<TextView> textViewRef;
private final Handler handler; private final Handler handler;
private final Context context; private final Context context;
private final HistoryManager historyManager;
SupplementalInfoRetriever(TextView textView, Handler handler, Context context) { SupplementalInfoRetriever(TextView textView, Handler handler, HistoryManager historyManager, Context context) {
this.textViewRef = new WeakReference<TextView>(textView); this.textViewRef = new WeakReference<TextView>(textView);
this.handler = handler; this.handler = handler;
this.context = context; this.context = context;
this.historyManager = historyManager;
} }
public final Void call() throws IOException, InterruptedException { public final Void call() throws IOException, InterruptedException {
@ -92,17 +103,18 @@ public abstract class SupplementalInfoRetriever implements Callable<Void> {
abstract void retrieveSupplementalInfo() throws IOException, InterruptedException; abstract void retrieveSupplementalInfo() throws IOException, InterruptedException;
final void append(final String newText) throws InterruptedException { final void append(String itemID, final String newText) throws InterruptedException {
final TextView textView = textViewRef.get(); final TextView textView = textViewRef.get();
if (textView == null) { if (textView == null) {
throw new InterruptedException(); throw new InterruptedException();
} }
handler.post(new Runnable() { handler.post(new Runnable() {
public void run() { public void run() {
Spanned html = Html.fromHtml(newText + '\n'); textView.append(Html.fromHtml(newText + '\n'));
textView.append(html);
} }
}); });
// Add the text to the history.
historyManager.addHistoryItemDetails(itemID, newText);
} }
final void setLink(final String uri) { final void setLink(final String uri) {

View file

@ -19,6 +19,7 @@ package com.google.zxing.client.android.result.supplement;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.widget.TextView; import android.widget.TextView;
import com.google.zxing.client.android.history.HistoryManager;
import com.google.zxing.client.android.AndroidHttpClient; import com.google.zxing.client.android.AndroidHttpClient;
import com.google.zxing.client.android.R; import com.google.zxing.client.android.R;
import com.google.zxing.client.result.URIParsedResult; import com.google.zxing.client.result.URIParsedResult;
@ -42,9 +43,12 @@ final class URIResultInfoRetriever extends SupplementalInfoRetriever {
private final URIParsedResult result; private final URIParsedResult result;
private final String redirectString; private final String redirectString;
URIResultInfoRetriever(TextView textView, URIParsedResult result, Handler handler, URIResultInfoRetriever(TextView textView,
URIParsedResult result,
Handler handler,
HistoryManager historyManager,
Context context) { Context context) {
super(textView, handler, context); super(textView, handler, historyManager, context);
redirectString = context.getString(R.string.msg_redirect); redirectString = context.getString(R.string.msg_redirect);
this.result = result; this.result = result;
} }
@ -55,7 +59,7 @@ final class URIResultInfoRetriever extends SupplementalInfoRetriever {
String newURI = unredirect(oldURI); String newURI = unredirect(oldURI);
int count = 0; int count = 0;
while (count < 3 && !oldURI.equals(newURI)) { while (count < 3 && !oldURI.equals(newURI)) {
append(redirectString + ": " + newURI); append(result.getDisplayResult(), redirectString + " : " + newURI);
count++; count++;
oldURI = newURI; oldURI = newURI;
newURI = unredirect(newURI); newURI = unredirect(newURI);