From 79ee3f6310a7a77ca83dc5f62285e7a3d45f80ab Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Tue, 13 Apr 2021 13:16:56 -0700 Subject: [PATCH] Fixing race condition when an apk is removed. Since the check was happening on a different thread, the view might have change by the time the callback comes back on UI thread. Bug: 185100744 Test: Manual Change-Id: I1347819bee71ed9d7ba6aa676f4318ea03316ea2 --- .../com/android/quickstep/RecentsModel.java | 11 ++-- .../android/quickstep/views/RecentsView.java | 64 ++++++++++--------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/quickstep/src/com/android/quickstep/RecentsModel.java b/quickstep/src/com/android/quickstep/RecentsModel.java index ba24e6a6c7..c786167241 100644 --- a/quickstep/src/com/android/quickstep/RecentsModel.java +++ b/quickstep/src/com/android/quickstep/RecentsModel.java @@ -112,20 +112,19 @@ public class RecentsModel extends TaskStackChangeListener { } /** - * Finds and returns the task key associated with the given task id. + * Checks if a task has been removed or not. * - * @param callback The callback to receive the task key if it is found or null. This is always - * called on the UI thread. + * @param callback Receives true if task is removed, false otherwise */ - public void findTaskWithId(int taskId, Consumer callback) { + public void isTaskRemoved(int taskId, Consumer callback) { mTaskList.getTasks(true /* loadKeysOnly */, (tasks) -> { for (Task task : tasks) { if (task.key.id == taskId) { - callback.accept(task.key); + callback.accept(false); return; } } - callback.accept(null); + callback.accept(true); }); } diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java index 524bae160e..87c46bfe49 100644 --- a/quickstep/src/com/android/quickstep/views/RecentsView.java +++ b/quickstep/src/com/android/quickstep/views/RecentsView.java @@ -72,7 +72,6 @@ import android.graphics.RectF; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Build; -import android.os.Handler; import android.os.UserHandle; import android.text.Layout; import android.text.StaticLayout; @@ -96,6 +95,7 @@ import android.widget.FrameLayout; import android.widget.ListView; import androidx.annotation.Nullable; +import androidx.annotation.UiThread; import com.android.launcher3.BaseActivity; import com.android.launcher3.BaseActivity.MultiWindowModeChangedListener; @@ -111,6 +111,7 @@ import com.android.launcher3.anim.PendingAnimation; import com.android.launcher3.anim.SpringProperty; import com.android.launcher3.compat.AccessibilityManagerCompat; import com.android.launcher3.config.FeatureFlags; +import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.statehandlers.DepthController; import com.android.launcher3.statemanager.BaseState; import com.android.launcher3.statemanager.StatefulActivity; @@ -153,6 +154,7 @@ import com.android.systemui.shared.system.PackageManagerWrapper; import com.android.systemui.shared.system.RemoteAnimationTargetCompat; import com.android.systemui.shared.system.SyncRtSurfaceTransactionApplierCompat.SurfaceParams; import com.android.systemui.shared.system.TaskStackChangeListener; +import com.android.systemui.shared.system.TaskStackChangeListeners; import com.android.wm.shell.pip.IPipAnimationListener; import java.util.ArrayList; @@ -394,34 +396,27 @@ public abstract class RecentsView { - TaskView taskView = getTaskView(taskId); - if (taskView == null) { - return; - } - Handler handler = taskView.getHandler(); - if (handler == null) { - return; - } - - // TODO: Add callbacks from AM reflecting adding/removing from the recents list, and - // remove all these checks - Task.TaskKey taskKey = taskView.getTask().key; - if (PackageManagerWrapper.getInstance().getActivityInfo(taskKey.getComponent(), - taskKey.userId) == null) { - // The package was uninstalled - handler.post(() -> - dismissTask(taskView, true /* animate */, false /* removeTask */)); - } else { - mModel.findTaskWithId(taskKey.id, (key) -> { - if (key == null) { - // The task was removed from the recents list - handler.post(() -> dismissTask(taskView, true /* animate */, - false /* removeTask */)); + TaskView taskView = getTaskView(taskId); + if (taskView == null) { + return; + } + Task.TaskKey taskKey = taskView.getTask().key; + UI_HELPER_EXECUTOR.execute(new HandlerRunnable<>( + UI_HELPER_EXECUTOR.getHandler(), + () -> PackageManagerWrapper.getInstance() + .getActivityInfo(taskKey.getComponent(), taskKey.userId) == null, + MAIN_EXECUTOR, + apkRemoved -> { + if (apkRemoved) { + dismissTask(taskId); + } else { + mModel.isTaskRemoved(taskKey.id, taskRemoved -> { + if (taskRemoved) { + dismissTask(taskId); + } + }); } - }); - } - }); + })); } }; @@ -668,7 +663,7 @@ public abstract class RecentsView