Permalink
Browse files

Generalize handling of non-fatal SQL exceptions in Android app

1 parent f5b5c3a commit b81c9ea01268da0dab04aad2190c64f51bd52abe @srowen srowen committed Jan 6, 2017
Showing with 14 additions and 3 deletions.
  1. +14 −3 android/src/com/google/zxing/client/android/history/HistoryManager.java
@@ -16,7 +16,7 @@
package com.google.zxing.client.android.history;
-import android.database.sqlite.SQLiteException;
+import android.database.SQLException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.client.android.Intents;
@@ -42,6 +42,7 @@
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
+import java.util.regex.Pattern;
/**
* <p>Manages functionality related to scan history.</p>
@@ -66,6 +67,7 @@
private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL };
private static final String[] ID_DETAIL_COL_PROJECTION = { DBHelper.ID_COL, DBHelper.DETAILS_COL };
+ private static final Pattern DOUBLE_QUOTE = Pattern.compile("\"", Pattern.LITERAL);
private final Activity activity;
private final boolean enableHistory;
@@ -85,6 +87,9 @@ public boolean hasHistoryItems() {
cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
cursor.moveToFirst();
return cursor.getInt(0) > 0;
+ } catch (SQLException sqle) {
+ Log.w(TAG, sqle);
+ return false;
} finally {
close(cursor, db);
}
@@ -145,6 +150,8 @@ public void deleteHistoryItem(int number) {
DBHelper.TIMESTAMP_COL + " DESC");
cursor.move(number + 1);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
+ } catch (SQLException sqle) {
+ Log.w(TAG, sqle);
} finally {
close(cursor, db);
}
@@ -230,6 +237,8 @@ private void deletePrevious(String text) {
try {
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
+ } catch (SQLException sqle) {
+ Log.w(TAG, sqle);
} finally {
close(null, db);
}
@@ -251,7 +260,7 @@ public void trimHistory() {
Log.i(TAG, "Deleting scan history ID " + id);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
}
- } catch (SQLiteException sqle) {
+ } catch (SQLException sqle) {
// We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
// and don't understand it. First theory is that it's transient so can be safely ignored.
Log.w(TAG, sqle);
@@ -316,6 +325,8 @@ void clearHistory() {
try {
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, null, null);
+ } catch (SQLException sqle) {
+ Log.w(TAG, sqle);
} finally {
close(null, db);
}
@@ -349,7 +360,7 @@ static Uri saveHistory(String history) {
}
private static String massageHistoryField(String value) {
- return value == null ? "" : value.replace("\"","\"\"");
+ return value == null ? "" : DOUBLE_QUOTE.matcher(value).replaceAll("\"\"");
}
private static void close(Cursor cursor, SQLiteDatabase database) {

0 comments on commit b81c9ea

Please sign in to comment.