From 142ca65d7846adcabd4b8b2a5a3ecbf365508d01 Mon Sep 17 00:00:00 2001 From: Samuel Fufa Date: Thu, 4 Jun 2020 00:49:04 -0700 Subject: [PATCH] Perform DB calls in MODEL thread Bug: 158127067 Change-Id: Ic7820f1ff877b37da550d77e3e166734508b2622 --- .../HotseatPredictionController.java | 2 +- .../hybridhotseat/HotseatRestoreHelper.java | 90 ++++++++----------- 2 files changed, 39 insertions(+), 53 deletions(-) diff --git a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java index 725f516335..bd4d7139e9 100644 --- a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java +++ b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatPredictionController.java @@ -326,7 +326,7 @@ public class HotseatPredictionController implements DragController.DragListener, } private void setPredictedApps(List appTargets) { mComponentKeyMappers.clear(); - if (appTargets.isEmpty() && mRestoreHelper.shouldRestoreToBackup()) { + if (appTargets.isEmpty()) { mRestoreHelper.restoreBackup(); } StringBuilder predictionLog = new StringBuilder("predictedApps: [\n"); diff --git a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatRestoreHelper.java b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatRestoreHelper.java index c95ff7a2d1..8c1db4e2e7 100644 --- a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatRestoreHelper.java +++ b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatRestoreHelper.java @@ -17,6 +17,7 @@ package com.android.launcher3.hybridhotseat; import static com.android.launcher3.LauncherSettings.Favorites.HYBRID_HOTSEAT_BACKUP_TABLE; import static com.android.launcher3.provider.LauncherDbUtils.tableExists; +import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.Launcher; @@ -29,72 +30,57 @@ import com.android.launcher3.provider.LauncherDbUtils; */ public class HotseatRestoreHelper { private final Launcher mLauncher; - private boolean mBackupExists; + private boolean mBackupRestored = false; HotseatRestoreHelper(Launcher context) { mLauncher = context; - setupBackupTable(); } /** * Creates a snapshot backup of Favorite table for future restoration use. */ - public synchronized void createBackup() { - try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) - LauncherSettings.Settings.call( - mLauncher.getContentResolver(), - LauncherSettings.Settings.METHOD_NEW_TRANSACTION) - .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { - InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv; - GridBackupTable backupTable = new GridBackupTable(mLauncher, - transaction.getDb(), idp.numHotseatIcons, idp.numColumns, - idp.numRows); - backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE); - transaction.commit(); - LauncherSettings.Settings.call(mLauncher.getContentResolver(), - LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE); - mBackupExists = true; - } + public void createBackup() { + MODEL_EXECUTOR.execute(() -> { + try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) + LauncherSettings.Settings.call( + mLauncher.getContentResolver(), + LauncherSettings.Settings.METHOD_NEW_TRANSACTION) + .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { + InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv; + GridBackupTable backupTable = new GridBackupTable(mLauncher, + transaction.getDb(), idp.numHotseatIcons, idp.numColumns, + idp.numRows); + backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE); + transaction.commit(); + LauncherSettings.Settings.call(mLauncher.getContentResolver(), + LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE); + } + }); } /** * Finds and restores a previously saved snapshow of Favorites table */ public void restoreBackup() { - try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) - LauncherSettings.Settings.call( - mLauncher.getContentResolver(), - LauncherSettings.Settings.METHOD_NEW_TRANSACTION) - .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { - if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) { - mBackupExists = false; - return; + if (mBackupRestored) return; + MODEL_EXECUTOR.execute(() -> { + try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) + LauncherSettings.Settings.call( + mLauncher.getContentResolver(), + LauncherSettings.Settings.METHOD_NEW_TRANSACTION) + .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { + if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) { + return; + } + InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv; + GridBackupTable backupTable = new GridBackupTable(mLauncher, + transaction.getDb(), idp.numHotseatIcons, idp.numColumns, + idp.numRows); + backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true); + transaction.commit(); + mBackupRestored = true; + mLauncher.getModel().forceReload(); } - InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv; - GridBackupTable backupTable = new GridBackupTable(mLauncher, - transaction.getDb(), idp.numHotseatIcons, idp.numColumns, - idp.numRows); - backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true); - transaction.commit(); - mBackupExists = false; - mLauncher.getModel().forceReload(); - } - } - - /** - * Returns if prediction controller should attempt restoring a backup - */ - public synchronized boolean shouldRestoreToBackup() { - return mBackupExists; - } - - private synchronized void setupBackupTable() { - try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) - LauncherSettings.Settings.call( - mLauncher.getContentResolver(), - LauncherSettings.Settings.METHOD_NEW_TRANSACTION) - .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { - mBackupExists = tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE); - } + }); } }