From 87e875180f112dcc622081ddcd8c650bae9d8e2d Mon Sep 17 00:00:00 2001 From: Fabian Bender Date: Tue, 20 Feb 2024 09:32:15 +0100 Subject: [PATCH 1/3] catch SQLiteDatabaseLockedException when trying to `openOrCreateDatabase` and throwing IOException after it has been handled --- .../java/io/snabble/sdk/ProductDatabase.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/io/snabble/sdk/ProductDatabase.java b/core/src/main/java/io/snabble/sdk/ProductDatabase.java index 8ef8b8bebb..1268667e62 100644 --- a/core/src/main/java/io/snabble/sdk/ProductDatabase.java +++ b/core/src/main/java/io/snabble/sdk/ProductDatabase.java @@ -6,6 +6,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.database.Cursor; +import android.database.sqlite.SQLiteCantOpenDatabaseException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.CancellationSignal; @@ -332,8 +333,14 @@ synchronized void applyDeltaUpdate(InputStream inputStream) throws IOException { project.logErrorEvent("Could not copy db to temp file: %s", e.getMessage()); throw e; } + final SQLiteDatabase tempDb; - SQLiteDatabase tempDb = SQLiteDatabase.openOrCreateDatabase(tempDbFile, null); + try { + tempDb = SQLiteDatabase.openOrCreateDatabase(tempDbFile, null); + } catch (SQLiteCantOpenDatabaseException e) { + project.logErrorEvent("Could not open or create db: %s", e.getMessage()); + throw new IOException(); + } Scanner scanner = new Scanner(inputStream, "UTF-8"); @@ -531,9 +538,9 @@ private Cursor rawQuery(String sql, String[] args, CancellationSignal cancellati *

* While updating, the database can still be queried for data, after the update completes calls to the database * return the updated data. - * + *

* Note that database updates are usually very cheap and do not transmit data that is already on your device. - * + *

* If the database is not present or schematic changes are done that can not be resolved via a delta update * a full update is needed. */ @@ -549,9 +556,9 @@ public void update() { *

* While updating, the database can still be queried for data, after the update completes calls to the database * return the updated data. - * + *

* Note that database updates are usually very cheap and do not transmit data that is already on your device. - * + *

* If the database is not present or schematic changes are done that can not be resolved via a delta update * a full update is needed. * @@ -570,9 +577,9 @@ public void update(final UpdateCallback callback) { *

* While updating, the database can still be queried for data, after the update completes calls to the database * return the updated data. - * + *

* Note that database updates are usually very cheap and do not transmit data that is already on your device. - * + *

* If the database is not present or schematic changes are done that can not be resolved via a delta update * a full update is needed. * @@ -849,7 +856,7 @@ public Product productAtCursor(Cursor cursor) { String[] split = isPrimaryStr.split(SEPARATOR, -1); if (split.length > 0) { codeIsPrimaryCode = new boolean[split.length]; - for (int i=0; i 0) { codeSpecifiedQuantities = new int[split.length]; - for (int i=0; i * {@link Config#maxProductDatabaseAge} can be used to set the time window the product database * is considered up to date. */ @@ -1432,7 +1439,7 @@ public Cursor searchByCode(String searchString, CancellationSignal cancellationS String query = productSqlString("", sb.toString(), true) + " LIMIT 100"; - return rawQuery(query, new String[]{ searchString + "*", searchString + "*" }, cancellationSignal); + return rawQuery(query, new String[]{searchString + "*", searchString + "*"}, cancellationSignal); } private void notifyOnDatabaseUpdated() { @@ -1468,4 +1475,4 @@ public interface UpdateCallback { void error(); } -} \ No newline at end of file +} From 174e1798ab1b963b77964f2f5cd2c2ab286ab5c8 Mon Sep 17 00:00:00 2001 From: Fabian Bender Date: Tue, 20 Feb 2024 09:54:13 +0100 Subject: [PATCH 2/3] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3c12228cc..ba04d62133 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Changed ### Removed ### Fixed +core: handle `SQLiteDatabaseLockedException` to fix app crash when updating the database ## [0.72.4] ### Added From cb9c1d58a9d5157721782733a61750eb509ce4e3 Mon Sep 17 00:00:00 2001 From: Fabian Bender <91562175+Fabtron@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:25:59 +0100 Subject: [PATCH 3/3] Update core/src/main/java/io/snabble/sdk/ProductDatabase.java Co-authored-by: Christian Maier --- core/src/main/java/io/snabble/sdk/ProductDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/io/snabble/sdk/ProductDatabase.java b/core/src/main/java/io/snabble/sdk/ProductDatabase.java index 1268667e62..e5c5b379e2 100644 --- a/core/src/main/java/io/snabble/sdk/ProductDatabase.java +++ b/core/src/main/java/io/snabble/sdk/ProductDatabase.java @@ -339,7 +339,7 @@ synchronized void applyDeltaUpdate(InputStream inputStream) throws IOException { tempDb = SQLiteDatabase.openOrCreateDatabase(tempDbFile, null); } catch (SQLiteCantOpenDatabaseException e) { project.logErrorEvent("Could not open or create db: %s", e.getMessage()); - throw new IOException(); + throw new IOException("Could not open or create db", e); } Scanner scanner = new Scanner(inputStream, "UTF-8");