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
This commit is contained in:
Pinyao Ting
2020-11-24 11:35:24 -08:00
parent 54834b0909
commit 0646dde219
@@ -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;
}