From 0646dde2198aa6e0cc58aa3518dd8917023dc7cf Mon Sep 17 00:00:00 2001 From: Pinyao Ting Date: Tue, 24 Nov 2020 11:35:24 -0800 Subject: [PATCH] Don't crash Launcher when attempting to endTransaction on an already-closed DB. Since the DB is already closed, if we can't end the transaction, it should not put the code in a bad state. Bug: 173162852 Test: manually tested backup and restore flow and existing tests Change-Id: I2692d98f0a8efc8aeb6bfd826fe738e4436c6ee4 --- .../android/launcher3/LauncherProvider.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java index 6af248cd78..23464d1c04 100644 --- a/src/com/android/launcher3/LauncherProvider.java +++ b/src/com/android/launcher3/LauncherProvider.java @@ -1105,11 +1105,20 @@ public class LauncherProvider extends ContentProvider { * @return the max _id in the provided table. */ @Thunk static int getMaxId(SQLiteDatabase db, String query, Object... args) { - int max = (int) DatabaseUtils.longForQuery(db, - String.format(Locale.ENGLISH, query, args), - null); - if (max < 0) { - throw new RuntimeException("Error: could not query max id"); + int max = 0; + try (SQLiteStatement prog = db.compileStatement( + String.format(Locale.ENGLISH, query, args))) { + max = (int) DatabaseUtils.longForQuery(prog, null); + if (max < 0) { + throw new RuntimeException("Error: could not query max id"); + } + } catch (IllegalArgumentException exception) { + String message = exception.getMessage(); + if (message.contains("re-open") && message.contains("already-closed")) { + // Don't crash trying to end a transaction an an already closed DB. See b/173162852. + } else { + throw exception; + } } return max; }