Skip to content

Commit 7367804

Browse files
committed
WIP: drop DB before import & handle DB errors
1 parent 7a4c9ce commit 7367804

18 files changed

+594
-208
lines changed

app/src/main/java/protect/card_locker/DBHelper.java

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public class DBHelper extends SQLiteOpenHelper {
2323
public static final int ORIGINAL_DATABASE_VERSION = 1;
2424
public static final int DATABASE_VERSION = 16;
2525

26+
public static class DBException extends Exception {
27+
public DBException(String message) {
28+
super(message);
29+
}
30+
31+
public DBException(String message, Exception rootCause) {
32+
super(message, rootCause);
33+
}
34+
}
35+
2636
public static class LoyaltyCardDbGroups {
2737
public static final String TABLE = "groups";
2838
public static final String ID = "_id";
@@ -323,6 +333,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
323333
}
324334
}
325335

336+
public static void clearDatabase(final SQLiteDatabase db) {
337+
db.execSQL("DELETE FROM " + LoyaltyCardDbGroups.TABLE);
338+
db.execSQL("DELETE FROM " + LoyaltyCardDbIds.TABLE);
339+
db.execSQL("DELETE FROM " + LoyaltyCardDbIdsGroups.TABLE);
340+
}
341+
326342
private static ContentValues generateFTSContentValues(final int id, final String store, final String note) {
327343
// FTS on Android is severely limited and can only search for word starting with a certain string
328344
// So for each word, we grab every single substring
@@ -368,7 +384,7 @@ public static long insertLoyaltyCard(
368384
final SQLiteDatabase database, final String store, final String note, final Date validFrom,
369385
final Date expiry, final BigDecimal balance, final Currency balanceType, final String cardId,
370386
final String barcodeId, final CatimaBarcode barcodeType, final Integer headerColor,
371-
final int starStatus, final Long lastUsed, final int archiveStatus) {
387+
final int starStatus, final Long lastUsed, final int archiveStatus) throws DBException {
372388
database.beginTransaction();
373389

374390
// Card
@@ -388,6 +404,8 @@ public static long insertLoyaltyCard(
388404
contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus);
389405
long id = database.insert(LoyaltyCardDbIds.TABLE, null, contentValues);
390406

407+
if (id == -1) throw new DBException("Failed to insert card");
408+
391409
// FTS
392410
insertFTS(database, (int) id, store, note);
393411

@@ -402,7 +420,7 @@ public static long insertLoyaltyCard(
402420
final Date validFrom, final Date expiry, final BigDecimal balance,
403421
final Currency balanceType, final String cardId, final String barcodeId,
404422
final CatimaBarcode barcodeType, final Integer headerColor, final int starStatus,
405-
final Long lastUsed, final int archiveStatus) {
423+
final Long lastUsed, final int archiveStatus) throws DBException {
406424
database.beginTransaction();
407425

408426
// Card
@@ -421,7 +439,9 @@ public static long insertLoyaltyCard(
421439
contentValues.put(LoyaltyCardDbIds.STAR_STATUS, starStatus);
422440
contentValues.put(LoyaltyCardDbIds.LAST_USED, lastUsed != null ? lastUsed : Utils.getUnixTime());
423441
contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus);
424-
database.insert(LoyaltyCardDbIds.TABLE, null, contentValues);
442+
long rowid = database.insert(LoyaltyCardDbIds.TABLE, null, contentValues);
443+
444+
if (rowid == -1) throw new DBException("Failed to insert card with ID " + id);
425445

426446
// FTS
427447
insertFTS(database, id, store, note);
@@ -432,12 +452,12 @@ public static long insertLoyaltyCard(
432452
return id;
433453
}
434454

435-
public static boolean updateLoyaltyCard(
455+
public static void updateLoyaltyCard(
436456
SQLiteDatabase database, final int id, final String store, final String note,
437457
final Date validFrom, final Date expiry, final BigDecimal balance,
438458
final Currency balanceType, final String cardId, final String barcodeId,
439459
final CatimaBarcode barcodeType, final Integer headerColor, final int starStatus,
440-
final Long lastUsed, final int archiveStatus) {
460+
final Long lastUsed, final int archiveStatus) throws DBException {
441461
database.beginTransaction();
442462

443463
// Card
@@ -465,45 +485,45 @@ public static boolean updateLoyaltyCard(
465485
database.setTransactionSuccessful();
466486
database.endTransaction();
467487

468-
return (rowsUpdated == 1);
488+
if (rowsUpdated != 1) throw new DBException("Failed to update card with ID " + id + ": " + rowsUpdated + " rows updated");
469489
}
470490

471-
public static boolean updateLoyaltyCardArchiveStatus(SQLiteDatabase database, final int id, final int archiveStatus) {
491+
public static void updateLoyaltyCardArchiveStatus(SQLiteDatabase database, final int id, final int archiveStatus) throws DBException {
472492
ContentValues contentValues = new ContentValues();
473493
contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus);
474494
int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues,
475495
whereAttrs(LoyaltyCardDbIds.ID),
476496
withArgs(id));
477-
return (rowsUpdated == 1);
497+
if (rowsUpdated != 1) throw new DBException("Failed to (un)archive card with ID " + id + ": " + rowsUpdated + " rows updated");
478498
}
479499

480-
public static boolean updateLoyaltyCardStarStatus(SQLiteDatabase database, final int id, final int starStatus) {
500+
public static void updateLoyaltyCardStarStatus(SQLiteDatabase database, final int id, final int starStatus) throws DBException {
481501
ContentValues contentValues = new ContentValues();
482502
contentValues.put(LoyaltyCardDbIds.STAR_STATUS, starStatus);
483503
int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues,
484504
whereAttrs(LoyaltyCardDbIds.ID),
485505
withArgs(id));
486-
return (rowsUpdated == 1);
506+
if (rowsUpdated != 1) throw new DBException("Failed to (un)star card with ID " + id + ": " + rowsUpdated + " rows updated");
487507
}
488508

489-
public static boolean updateLoyaltyCardLastUsed(SQLiteDatabase database, final int id) {
509+
public static void updateLoyaltyCardLastUsed(SQLiteDatabase database, final int id) throws DBException {
490510
ContentValues contentValues = new ContentValues();
491511
contentValues.put(LoyaltyCardDbIds.LAST_USED, System.currentTimeMillis() / 1000);
492512
int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues,
493513
whereAttrs(LoyaltyCardDbIds.ID),
494514
withArgs(id));
495-
return (rowsUpdated == 1);
515+
if (rowsUpdated != 1) throw new DBException("Failed to update last used for card with ID " + id + ": " + rowsUpdated + " rows updated");
496516
}
497517

498-
public static boolean updateLoyaltyCardZoomLevel(SQLiteDatabase database, int loyaltyCardId, int zoomLevel) {
518+
public static void updateLoyaltyCardZoomLevel(SQLiteDatabase database, int loyaltyCardId, int zoomLevel) throws DBException {
499519
ContentValues contentValues = new ContentValues();
500520
contentValues.put(LoyaltyCardDbIds.ZOOM_LEVEL, zoomLevel);
501521
Log.d("updateLoyaltyCardZLevel", "Card Id = " + loyaltyCardId + " Zoom level= " + zoomLevel);
502522
int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues,
503523
whereAttrs(LoyaltyCardDbIds.ID),
504524
withArgs(loyaltyCardId));
505525
Log.d("updateLoyaltyCardZLevel", "Rows changed = " + rowsUpdated);
506-
return (rowsUpdated == 1);
526+
if (rowsUpdated != 1) throw new DBException("Failed to update zoom level for card with ID " + loyaltyCardId + ": " + rowsUpdated + " rows updated");
507527
}
508528

509529
public static boolean updateLoyaltyCardBalance(SQLiteDatabase database, final int id, final BigDecimal newBalance) {
@@ -569,7 +589,7 @@ public static void setLoyaltyCardGroups(SQLiteDatabase database, final int id, L
569589
}
570590
}
571591

572-
public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context, final int id) {
592+
public static void deleteLoyaltyCard(SQLiteDatabase database, Context context, final int id) throws DBException {
573593
// Delete card
574594
int rowsDeleted = database.delete(LoyaltyCardDbIds.TABLE,
575595
whereAttrs(LoyaltyCardDbIds.ID),
@@ -594,7 +614,7 @@ public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context
594614
}
595615
}
596616

597-
return (rowsDeleted == 1);
617+
if (rowsDeleted != 1) throw new DBException("Failed to delete card with ID " + id + ": " + rowsDeleted + " rows deleted");
598618
}
599619

600620
public static int getArchivedCardsCount(SQLiteDatabase database) {
@@ -792,19 +812,20 @@ public static List<Integer> getGroupCardIds(SQLiteDatabase database, final Strin
792812
return cardIds;
793813
}
794814

795-
public static long insertGroup(SQLiteDatabase database, final String name) {
796-
if (name.isEmpty()) return -1;
815+
public static void insertGroup(SQLiteDatabase database, final String name) throws DBException {
816+
if (name.isEmpty()) throw new DBException("Failed to insert group with empty name");
797817

798818
ContentValues contentValues = new ContentValues();
799819
contentValues.put(LoyaltyCardDbGroups.ID, name);
800820
contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database));
801-
return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues);
802-
}
821+
long rowid = database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues);
803822

804-
public static boolean updateGroup(SQLiteDatabase database, final String groupName, final String newName) {
805-
if (newName.isEmpty()) return false;
823+
if (rowid == -1) throw new DBException("Failed to insert group with name " + name);
824+
}
806825

807-
boolean success = false;
826+
public static void updateGroup(SQLiteDatabase database, final String groupName, final String newName) throws DBException {
827+
if (groupName.isEmpty()) throw new DBException("Failed to update group: empty old name");
828+
if (newName.isEmpty()) throw new DBException("Failed to update group: empty new name");
808829

809830
ContentValues groupContentValues = new ContentValues();
810831
groupContentValues.put(LoyaltyCardDbGroups.ID, newName);
@@ -826,19 +847,17 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam
826847

827848
if (groupsChanged == 1) {
828849
database.setTransactionSuccessful();
829-
success = true;
850+
return;
830851
}
831852
} catch (SQLiteException ignored) {
832853
} finally {
833854
database.endTransaction();
834855
}
835856

836-
return success;
857+
throw new DBException("Failed to update group");
837858
}
838859

839-
public static boolean deleteGroup(SQLiteDatabase database, final String groupName) {
840-
boolean success = false;
841-
860+
public static void deleteGroup(SQLiteDatabase database, final String groupName) throws DBException {
842861
database.beginTransaction();
843862
try {
844863
// Delete group
@@ -853,16 +872,15 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam
853872

854873
if (groupsDeleted == 1) {
855874
database.setTransactionSuccessful();
856-
success = true;
875+
// Reorder after delete to ensure no bad order IDs
876+
reorderGroups(database, getGroups(database));
877+
return;
857878
}
858879
} finally {
859880
database.endTransaction();
860881
}
861882

862-
// Reorder after delete to ensure no bad order IDs
863-
reorderGroups(database, getGroups(database));
864-
865-
return success;
883+
throw new DBException("Failed to delete group");
866884
}
867885

868886
public static int getGroupCardCount(SQLiteDatabase database, final String groupName) {

app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,21 @@ private void doSave() {
14371437
// This makes the DBHelper set it to the current date
14381438
// So that new and edited card are always on top when sorting by recently used
14391439
if (updateLoyaltyCard) {
1440-
DBHelper.updateLoyaltyCard(mDatabase, loyaltyCardId, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, tempLoyaltyCard.starStatus, null, tempLoyaltyCard.archiveStatus);
1440+
try {
1441+
DBHelper.updateLoyaltyCard(mDatabase, loyaltyCardId, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, tempLoyaltyCard.starStatus, null, tempLoyaltyCard.archiveStatus);
1442+
} catch (DBHelper.DBException e) {
1443+
Log.w(TAG, "Could not update loyalty card " + loyaltyCardId);
1444+
Toast.makeText(this, "Could not update loyalty card", Toast.LENGTH_LONG).show(); // FIXME
1445+
return;
1446+
}
14411447
} else {
1442-
loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, 0, null, 0);
1448+
try {
1449+
loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, 0, null, 0);
1450+
} catch (DBHelper.DBException e) {
1451+
Log.w(TAG, "Could not insert loyalty card");
1452+
Toast.makeText(this, "Could not insert loyalty card", Toast.LENGTH_LONG).show(); // FIXME
1453+
return;
1454+
}
14431455
}
14441456

14451457
try {

app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
263263
Log.d(TAG, "Scaling to " + scale);
264264

265265
loyaltyCard.zoomLevel = progress;
266-
DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel);
266+
267+
try {
268+
DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel);
269+
} catch (DBHelper.DBException e) {
270+
Log.d(TAG, "Could not update zoom level for card " + loyaltyCardId);
271+
}
267272

268273
setScalerGuideline(loyaltyCard.zoomLevel);
269274

@@ -669,7 +674,11 @@ public void onResume() {
669674

670675
setFullscreen(isFullscreen);
671676

672-
DBHelper.updateLoyaltyCardLastUsed(database, loyaltyCard.id);
677+
try {
678+
DBHelper.updateLoyaltyCardLastUsed(database, loyaltyCard.id);
679+
} catch (DBHelper.DBException e) {
680+
Log.w(TAG, "Could not update last used for card " + loyaltyCardId);
681+
}
673682

674683
invalidateOptionsMenu();
675684
}
@@ -770,15 +779,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
770779

771780
return true;
772781
} else if (id == R.id.action_star_unstar) {
773-
DBHelper.updateLoyaltyCardStarStatus(database, loyaltyCardId, loyaltyCard.starStatus == 0 ? 1 : 0);
782+
try {
783+
DBHelper.updateLoyaltyCardStarStatus(database, loyaltyCardId, loyaltyCard.starStatus == 0 ? 1 : 0);
784+
} catch (DBHelper.DBException e) {
785+
Log.w(TAG, "Could not (un)star card " + loyaltyCardId);
786+
Toast.makeText(LoyaltyCardViewActivity.this, "Failed to (un)star card", Toast.LENGTH_LONG).show(); // FIXME
787+
}
774788

775789
// Re-init loyaltyCard with new data from DB
776790
onResume();
777791
invalidateOptionsMenu();
778792

779793
return true;
780794
} else if (id == R.id.action_archive) {
781-
DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 1);
795+
try {
796+
DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 1);
797+
} catch (DBHelper.DBException e) {
798+
Log.w(TAG, "Could not archive card " + loyaltyCardId);
799+
Toast.makeText(LoyaltyCardViewActivity.this, "Failed to archive card", Toast.LENGTH_LONG).show(); // FIXME
800+
}
782801
Toast.makeText(LoyaltyCardViewActivity.this, R.string.archived, Toast.LENGTH_LONG).show();
783802

784803
// Re-init loyaltyCard with new data from DB
@@ -787,7 +806,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
787806

788807
return true;
789808
} else if (id == R.id.action_unarchive) {
790-
DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 0);
809+
try {
810+
DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 0);
811+
} catch (DBHelper.DBException e) {
812+
Log.w(TAG, "Could not unarchive card " + loyaltyCardId);
813+
Toast.makeText(LoyaltyCardViewActivity.this, "Failed to unarchive card", Toast.LENGTH_LONG).show(); // FIXME
814+
}
791815
Toast.makeText(LoyaltyCardViewActivity.this, R.string.unarchived, Toast.LENGTH_LONG).show();
792816

793817
// Re-init loyaltyCard with new data from DB
@@ -802,7 +826,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
802826
builder.setPositiveButton(R.string.confirm, (dialog, which) -> {
803827
Log.e(TAG, "Deleting card: " + loyaltyCardId);
804828

805-
DBHelper.deleteLoyaltyCard(database, LoyaltyCardViewActivity.this, loyaltyCardId);
829+
try {
830+
DBHelper.deleteLoyaltyCard(database, LoyaltyCardViewActivity.this, loyaltyCardId);
831+
} catch (DBHelper.DBException e) {
832+
Log.e(TAG, "Could not delete card " + loyaltyCardId);
833+
Toast.makeText(LoyaltyCardViewActivity.this, "Failed to delete card", Toast.LENGTH_LONG).show(); // FIXME
834+
}
806835

807836
ShortcutHelper.removeShortcut(LoyaltyCardViewActivity.this, loyaltyCardId);
808837

0 commit comments

Comments
 (0)