Skip to content

Commit 26f45f1

Browse files
Fabtroncmaier
andauthored
ProductDatabase: Fix app crash due to SQLiteDatabaseLockedException (#176)
Co-authored-by: Christian Maier <[email protected]>
1 parent 30884d7 commit 26f45f1

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
* ui: The `ProductResolver.Builder` now requires a project as constructor param, since the default value has been removed
88
### Removed
99
### Fixed
10+
* core: Handle `SQLiteDatabaseLockedException` to fix app crash when updating the database
1011
* ui: Avoid npe caused by `isEmpty()` check on a null shopping cart
1112
* ui: Change project reference in `ProductResolver.kt` to get rid of IllegalArgumentException
1213

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.pm.PackageInfo;
77
import android.content.pm.PackageManager;
88
import android.database.Cursor;
9+
import android.database.sqlite.SQLiteCantOpenDatabaseException;
910
import android.database.sqlite.SQLiteDatabase;
1011
import android.database.sqlite.SQLiteException;
1112
import android.os.CancellationSignal;
@@ -332,8 +333,14 @@ synchronized void applyDeltaUpdate(InputStream inputStream) throws IOException {
332333
project.logErrorEvent("Could not copy db to temp file: %s", e.getMessage());
333334
throw e;
334335
}
336+
final SQLiteDatabase tempDb;
335337

336-
SQLiteDatabase tempDb = SQLiteDatabase.openOrCreateDatabase(tempDbFile, null);
338+
try {
339+
tempDb = SQLiteDatabase.openOrCreateDatabase(tempDbFile, null);
340+
} catch (SQLiteCantOpenDatabaseException e) {
341+
project.logErrorEvent("Could not open or create db: %s", e.getMessage());
342+
throw new IOException("Could not open or create db", e);
343+
}
337344

338345
Scanner scanner = new Scanner(inputStream, "UTF-8");
339346

@@ -531,9 +538,9 @@ private Cursor rawQuery(String sql, String[] args, CancellationSignal cancellati
531538
* <p>
532539
* While updating, the database can still be queried for data, after the update completes calls to the database
533540
* return the updated data.
534-
*
541+
* <p>
535542
* Note that database updates are usually very cheap and do not transmit data that is already on your device.
536-
*
543+
* <p>
537544
* If the database is not present or schematic changes are done that can not be resolved via a delta update
538545
* a full update is needed.
539546
*/
@@ -549,9 +556,9 @@ public void update() {
549556
* <p>
550557
* While updating, the database can still be queried for data, after the update completes calls to the database
551558
* return the updated data.
552-
*
559+
* <p>
553560
* Note that database updates are usually very cheap and do not transmit data that is already on your device.
554-
*
561+
* <p>
555562
* If the database is not present or schematic changes are done that can not be resolved via a delta update
556563
* a full update is needed.
557564
*
@@ -570,9 +577,9 @@ public void update(final UpdateCallback callback) {
570577
* <p>
571578
* While updating, the database can still be queried for data, after the update completes calls to the database
572579
* return the updated data.
573-
*
580+
* <p>
574581
* Note that database updates are usually very cheap and do not transmit data that is already on your device.
575-
*
582+
* <p>
576583
* If the database is not present or schematic changes are done that can not be resolved via a delta update
577584
* a full update is needed.
578585
*
@@ -849,7 +856,7 @@ public Product productAtCursor(Cursor cursor) {
849856
String[] split = isPrimaryStr.split(SEPARATOR, -1);
850857
if (split.length > 0) {
851858
codeIsPrimaryCode = new boolean[split.length];
852-
for (int i=0; i<split.length; i++) {
859+
for (int i = 0; i < split.length; i++) {
853860
if (split[i].equals("1")) {
854861
codeIsPrimaryCode[i] = true;
855862
} else {
@@ -864,7 +871,7 @@ public Product productAtCursor(Cursor cursor) {
864871
String[] split = specifiedQuantities.split(SEPARATOR, -1);
865872
if (split.length > 0) {
866873
codeSpecifiedQuantities = new int[split.length];
867-
for (int i=0; i<split.length; i++) {
874+
for (int i = 0; i < split.length; i++) {
868875
try {
869876
int value = Integer.parseInt(split[i]);
870877
codeSpecifiedQuantities[i] = value;
@@ -961,7 +968,7 @@ public Product productAtCursor(Cursor cursor) {
961968

962969
Shop shop = Snabble.getInstance().getCheckedInShop();
963970

964-
if(!queryPrice(builder, sku, shop)) {
971+
if (!queryPrice(builder, sku, shop)) {
965972
queryPrice(builder, sku, null);
966973
}
967974

@@ -1045,7 +1052,7 @@ private String productSqlString(String appendFields, String appendSql, boolean d
10451052
"p.weighing," +
10461053
"(SELECT group_concat(s.code, \"" + SEPARATOR + "\") FROM scannableCodes s WHERE s.sku = p.sku)," +
10471054
"p.subtitle" +
1048-
",p.saleRestriction"+
1055+
",p.saleRestriction" +
10491056
",p.saleStop" +
10501057
",p.notForSale" +
10511058
",(SELECT group_concat(ifnull(s.transmissionCode, \"\"), \"" + SEPARATOR + "\") FROM scannableCodes s WHERE s.sku = p.sku)" +
@@ -1080,7 +1087,7 @@ public boolean isAvailableOffline() {
10801087

10811088
/**
10821089
* Return true if the database was updated recently and can be used to display accurate prices.
1083-
*
1090+
* <p>
10841091
* {@link Config#maxProductDatabaseAge} can be used to set the time window the product database
10851092
* is considered up to date.
10861093
*/
@@ -1432,7 +1439,7 @@ public Cursor searchByCode(String searchString, CancellationSignal cancellationS
14321439

14331440
String query = productSqlString("", sb.toString(), true) + " LIMIT 100";
14341441

1435-
return rawQuery(query, new String[]{ searchString + "*", searchString + "*" }, cancellationSignal);
1442+
return rawQuery(query, new String[]{searchString + "*", searchString + "*"}, cancellationSignal);
14361443
}
14371444

14381445
private void notifyOnDatabaseUpdated() {
@@ -1468,4 +1475,4 @@ public interface UpdateCallback {
14681475

14691476
void error();
14701477
}
1471-
}
1478+
}

0 commit comments

Comments
 (0)