Generalize handling of non-fatal SQL exceptions in Android app

This commit is contained in:
Sean Owen 2017-01-06 13:29:36 +00:00
parent f5b5c3a97e
commit b81c9ea012
No known key found for this signature in database
GPG key ID: F6CE9695C9318406

View file

@ -16,7 +16,7 @@
package com.google.zxing.client.android.history; 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.BarcodeFormat;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.client.android.Intents; import com.google.zxing.client.android.Intents;
@ -42,6 +42,7 @@ import java.nio.charset.Charset;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* <p>Manages functionality related to scan history.</p> * <p>Manages functionality related to scan history.</p>
@ -66,6 +67,7 @@ public final class HistoryManager {
private static final String[] ID_COL_PROJECTION = { DBHelper.ID_COL }; 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 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 Activity activity;
private final boolean enableHistory; private final boolean enableHistory;
@ -85,6 +87,9 @@ public final class HistoryManager {
cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null); cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
cursor.moveToFirst(); cursor.moveToFirst();
return cursor.getInt(0) > 0; return cursor.getInt(0) > 0;
} catch (SQLException sqle) {
Log.w(TAG, sqle);
return false;
} finally { } finally {
close(cursor, db); close(cursor, db);
} }
@ -145,6 +150,8 @@ public final class HistoryManager {
DBHelper.TIMESTAMP_COL + " DESC"); DBHelper.TIMESTAMP_COL + " DESC");
cursor.move(number + 1); cursor.move(number + 1);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null); db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
} catch (SQLException sqle) {
Log.w(TAG, sqle);
} finally { } finally {
close(cursor, db); close(cursor, db);
} }
@ -230,6 +237,8 @@ public final class HistoryManager {
try { try {
db = helper.getWritableDatabase(); db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text }); db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
} catch (SQLException sqle) {
Log.w(TAG, sqle);
} finally { } finally {
close(null, db); close(null, db);
} }
@ -251,7 +260,7 @@ public final class HistoryManager {
Log.i(TAG, "Deleting scan history ID " + id); Log.i(TAG, "Deleting scan history ID " + id);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null); 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 // 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. // and don't understand it. First theory is that it's transient so can be safely ignored.
Log.w(TAG, sqle); Log.w(TAG, sqle);
@ -316,6 +325,8 @@ public final class HistoryManager {
try { try {
db = helper.getWritableDatabase(); db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, null, null); db.delete(DBHelper.TABLE_NAME, null, null);
} catch (SQLException sqle) {
Log.w(TAG, sqle);
} finally { } finally {
close(null, db); close(null, db);
} }
@ -349,7 +360,7 @@ public final class HistoryManager {
} }
private static String massageHistoryField(String value) { 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) { private static void close(Cursor cursor, SQLiteDatabase database) {