Minor refinements to prep for history change

git-svn-id: https://zxing.googlecode.com/svn/trunk@2079 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-12-11 16:24:03 +00:00
parent a12326f2d4
commit 08acd5b8f7
2 changed files with 49 additions and 96 deletions

View file

@ -356,9 +356,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
builder.setNegativeButton(R.string.button_cancel, null); builder.setNegativeButton(R.string.button_cancel, null);
builder.show(); builder.show();
break; break;
} default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
return true;
}
@Override @Override
public void surfaceCreated(SurfaceHolder holder) { public void surfaceCreated(SurfaceHolder holder) {

View file

@ -31,7 +31,6 @@ import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
@ -114,15 +113,8 @@ public final class HistoryManager {
dialogItems.add(displayResult.toString()); dialogItems.add(displayResult.toString());
} }
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
} finally { } finally {
if (cursor != null) { close(cursor, db);
cursor.close();
}
if (db != null) {
db.close();
}
} }
Resources res = activity.getResources(); Resources res = activity.getResources();
@ -155,57 +147,38 @@ public final class HistoryManager {
values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis()); values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db = null;
try { try {
db = helper.getWritableDatabase(); db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
}
try {
// Insert the new entry into the DB. // Insert the new entry into the DB.
db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values); db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
} finally { } finally {
db.close(); close(null, db);
} }
} }
public void addHistoryItemDetails(String itemID, String itemDetails) { public void addHistoryItemDetails(String itemID, String itemDetails) {
// As we're going to do an update only we don't need need to worry // 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 // about the preferences; if the item wasn't saved it won't be udpated
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db = null;
Cursor cursor = null;
try { try {
db = helper.getWritableDatabase(); db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
}
try {
Cursor cursor = null;
String oldID = null;
String oldDetails = null;
try {
cursor = db.query(DBHelper.TABLE_NAME, cursor = db.query(DBHelper.TABLE_NAME,
ID_DETAIL_COL_PROJECTION, ID_DETAIL_COL_PROJECTION,
DBHelper.TEXT_COL + "=?", DBHelper.TEXT_COL + "=?",
new String[] { itemID }, new String[] { itemID },
null, null,
null, null,
DBHelper.TIMESTAMP_COL + " DESC"); DBHelper.TIMESTAMP_COL + " DESC",
"1");
String oldID = null;
String oldDetails = null;
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
oldID = cursor.getString(0); oldID = cursor.getString(0);
oldDetails = cursor.getString(1); oldDetails = cursor.getString(1);
} }
} finally {
if (cursor != null) {
cursor.close();
}
}
String newDetails = oldDetails == null ? itemDetails : oldDetails + " : " + itemDetails; String newDetails = oldDetails == null ? itemDetails : oldDetails + " : " + itemDetails;
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@ -214,53 +187,37 @@ public final class HistoryManager {
db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID }); db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
} finally { } finally {
db.close(); close(cursor, db);
} }
} }
private void deletePrevious(String text) { private void deletePrevious(String text) {
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db = null;
try { try {
db = helper.getWritableDatabase(); db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
}
try {
db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text }); db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
} finally { } finally {
db.close(); close(null, db);
} }
} }
public void trimHistory() { public void trimHistory() {
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
}
Cursor cursor = null; Cursor cursor = null;
try { try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, cursor = db.query(DBHelper.TABLE_NAME,
ID_COL_PROJECTION, ID_COL_PROJECTION,
null, null, null, null, null, null, null, null,
DBHelper.TIMESTAMP_COL + " DESC"); DBHelper.TIMESTAMP_COL + " DESC");
int count = 0; cursor.move(MAX_ITEMS);
while (count < MAX_ITEMS && cursor.moveToNext()) {
count++;
}
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null); db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
} }
} finally { } finally {
if (cursor != null) { close(cursor, db);
cursor.close();
}
db.close();
} }
} }
@ -281,16 +238,10 @@ public final class HistoryManager {
CharSequence buildHistory() { CharSequence buildHistory() {
StringBuilder historyText = new StringBuilder(1000); StringBuilder historyText = new StringBuilder(1000);
SQLiteOpenHelper helper = new DBHelper(activity); SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db; SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return "";
}
Cursor cursor = null; Cursor cursor = null;
try { try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, cursor = db.query(DBHelper.TABLE_NAME,
COLUMNS, COLUMNS,
null, null, null, null, null, null, null, null,
@ -312,14 +263,21 @@ public final class HistoryManager {
historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n"); historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
} }
} finally {
if (cursor != null) {
cursor.close();
}
db.close();
}
return historyText; return historyText;
} finally {
close(cursor, db);
}
}
void clearHistory() {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, null, null);
} finally {
close(null, db);
}
} }
static Uri saveHistory(String history) { static Uri saveHistory(String history) {
@ -353,19 +311,12 @@ public final class HistoryManager {
return value == null ? "" : value.replace("\"","\"\""); return value == null ? "" : value.replace("\"","\"\"");
} }
void clearHistory() { private static void close(Cursor cursor, SQLiteDatabase database) {
SQLiteOpenHelper helper = new DBHelper(activity); if (cursor != null) {
SQLiteDatabase db; cursor.close();
try {
db = helper.getWritableDatabase();
} catch (SQLiteException sqle) {
Log.w(TAG, "Error while opening database", sqle);
return;
} }
try { if (database != null) {
db.delete(DBHelper.TABLE_NAME, null, null); database.close();
} finally {
db.close();
} }
} }