From 6976f7bae901d81a354287088ae232ba7236f30e Mon Sep 17 00:00:00 2001 From: Winson Date: Tue, 3 May 2016 14:58:12 -0700 Subject: [PATCH] Disabling movement of entire affiliated task set. - This CL removes the dependency on the last-active-time of the task record, since that is dependent on the current system time (which can be changed by the user). Instead of working around the previous behavior in ActivityManager which moves all affiliated tasks to the top when one task is launched, we change the behavior in the AM directly, and prevent re-sorting the list of recent tasks improperly (aside from the stable sort which puts the freeform tasks first). Bug: 27398177 Change-Id: I9fa9b3497d08082fe00aa724538255de87e746d6 --- .../systemui/recents/RecentsActivity.java | 9 +++++ .../recents/model/RecentsTaskLoadPlan.java | 39 ++++--------------- .../android/systemui/recents/model/Task.java | 5 +++ .../recents/model/TaskKeyLruCache.java | 3 +- .../systemui/recents/model/TaskStack.java | 22 ++++------- .../com/android/server/am/RecentTasks.java | 5 ++- 6 files changed, 35 insertions(+), 48 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java index afb2195d9f996..5e4cac7c8c518 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java @@ -41,6 +41,7 @@ import android.view.WindowManager.LayoutParams; import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.systemui.Interpolators; +import com.android.systemui.Prefs; import com.android.systemui.R; import com.android.systemui.recents.events.EventBus; import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent; @@ -166,6 +167,13 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD if (action.equals(Intent.ACTION_SCREEN_OFF)) { // When the screen turns off, dismiss Recents to Home dismissRecentsToHomeIfVisible(false); + } else if (action.equals(Intent.ACTION_TIME_CHANGED)) { + // For the time being, if the time changes, then invalidate the + // last-stack-active-time, this ensures that we will just show the last N tasks + // the next time that Recents loads, but prevents really old tasks from showing + // up if the task time is set forward. + Prefs.putLong(RecentsActivity.this, Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, + 0); } } }; @@ -311,6 +319,7 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD // Register the broadcast receiver to handle messages when the screen is turned off IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); + filter.addAction(Intent.ACTION_TIME_CHANGED); registerReceiver(mSystemBroadcastReceiver, filter); getWindow().addPrivateFlags(LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION); diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java index 397909580b0dd..251ad71e5d62f 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java +++ b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java @@ -140,39 +140,10 @@ public class RecentsTaskLoadPlan { lastStackActiveTime = 0; } long newLastStackActiveTime = -1; - long prevLastActiveTime = lastStackActiveTime; int taskCount = mRawTasks.size(); for (int i = 0; i < taskCount; i++) { ActivityManager.RecentTaskInfo t = mRawTasks.get(i); - /* - * Affiliated tasks are returned in a specific order from ActivityManager but without a - * lastActiveTime since it hasn't yet been started. However, we later sort the task list - * by lastActiveTime, which rearranges the tasks. For now, we need to workaround this - * by updating the lastActiveTime of this task to the lastActiveTime of the task it is - * affiliated with, in the same order that we encounter it in the original list (just - * its index in the task group for the task it is affiliated with). - * - * If the parent task is not available, then we will use the last active time of the - * previous task as a base point (since the task itself may not have an active time) - * for the entire affiliated group. - */ - if (t.persistentId != t.affiliatedTaskId) { - Task.TaskKey parentTask = affiliatedTasks.get(t.affiliatedTaskId); - long parentTaskLastActiveTime = parentTask != null - ? parentTask.lastActiveTime - : prevLastActiveTime; - if (RecentsDebugFlags.Static.EnableAffiliatedTaskGroups) { - t.lastActiveTime = parentTaskLastActiveTime + - affiliatedTaskCounts.get(t.affiliatedTaskId, 0) + 1; - } else { - if (t.lastActiveTime == 0) { - t.lastActiveTime = parentTaskLastActiveTime - - affiliatedTaskCounts.get(t.affiliatedTaskId, 0) - 1; - } - } - } - // Compose the task key Task.TaskKey taskKey = new Task.TaskKey(t.persistentId, t.stackId, t.baseIntent, t.userId, t.firstActiveTime, t.lastActiveTime); @@ -180,9 +151,14 @@ public class RecentsTaskLoadPlan { // This task is only shown in the stack if it statisfies the historical time or min // number of tasks constraints. Freeform tasks are also always shown. boolean isFreeformTask = SystemServicesProxy.isFreeformStack(t.stackId); - boolean isStackTask = isFreeformTask || (!isHistoricalTask(t) || - (t.lastActiveTime >= lastStackActiveTime && i >= (taskCount - MIN_NUM_TASKS))); + boolean isStackTask = isFreeformTask || !isHistoricalTask(t) || + (t.lastActiveTime >= lastStackActiveTime && i >= (taskCount - MIN_NUM_TASKS)); boolean isLaunchTarget = taskKey.id == runningTaskId; + + // The last stack active time is the baseline for which we show visible tasks. Since + // the system will store all the tasks, we don't want to show the tasks prior to the + // last visible ones, otherwise, as you dismiss them, the previous tasks may satisfy + // the other stack-task constraints. if (isStackTask && newLastStackActiveTime < 0) { newLastStackActiveTime = t.lastActiveTime; } @@ -211,7 +187,6 @@ public class RecentsTaskLoadPlan { allTasks.add(task); affiliatedTaskCounts.put(taskKey.id, affiliatedTaskCounts.get(taskKey.id, 0) + 1); affiliatedTasks.put(taskKey.id, taskKey); - prevLastActiveTime = t.lastActiveTime; } if (newLastStackActiveTime != -1) { Prefs.putLong(mContext, Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java index c6528a157d7ac..86a0315496a18 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java +++ b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java @@ -117,6 +117,11 @@ public class Task { @ViewDebug.ExportedProperty(deepExport=true, prefix="key_") public TaskKey key; + /** + * The temporary sort index in the stack, used when ordering the stack. + */ + public int temporarySortIndexInStack; + /** * The group will be computed separately from the initialization of the task */ diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/TaskKeyLruCache.java b/packages/SystemUI/src/com/android/systemui/recents/model/TaskKeyLruCache.java index c63a494963a94..23739a0838267 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/model/TaskKeyLruCache.java +++ b/packages/SystemUI/src/com/android/systemui/recents/model/TaskKeyLruCache.java @@ -76,7 +76,8 @@ public class TaskKeyLruCache { final V getAndInvalidateIfModified(Task.TaskKey key) { Task.TaskKey lastKey = mKeys.get(key.id); if (lastKey != null) { - if ((lastKey.stackId != key.stackId) || (lastKey.lastActiveTime < key.lastActiveTime)) { + if ((lastKey.stackId != key.stackId) || + (lastKey.lastActiveTime != key.lastActiveTime)) { // The task has updated (been made active since the last time it was put into the // LRU cache) or the stack id for the task has changed, invalidate that cache item remove(key); diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java index f42eea335e6f6..50e28ca2205d9 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java +++ b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java @@ -517,16 +517,8 @@ public class TaskStack { } } - // A comparator that sorts tasks by their last active time - private Comparator LAST_ACTIVE_TIME_COMPARATOR = new Comparator() { - @Override - public int compare(Task o1, Task o2) { - return Long.compare(o1.key.lastActiveTime, o2.key.lastActiveTime); - } - }; - - // A comparator that sorts tasks by their last active time and freeform state - private Comparator FREEFORM_LAST_ACTIVE_TIME_COMPARATOR = new Comparator() { + // A comparator that sorts tasks by their freeform state + private Comparator FREEFORM_COMPARATOR = new Comparator() { @Override public int compare(Task o1, Task o2) { if (o1.isFreeformTask() && !o2.isFreeformTask()) { @@ -534,7 +526,7 @@ public class TaskStack { } else if (o2.isFreeformTask() && !o1.isFreeformTask()) { return -1; } - return Long.compare(o1.key.lastActiveTime, o2.key.lastActiveTime); + return Long.compare(o1.temporarySortIndexInStack, o2.temporarySortIndexInStack); } }; @@ -696,7 +688,10 @@ public class TaskStack { } // Sort all the tasks to ensure they are ordered correctly - Collections.sort(allTasks, FREEFORM_LAST_ACTIVE_TIME_COMPARATOR); + for (int i = allTasks.size() - 1; i >= 0; i--) { + allTasks.get(i).temporarySortIndexInStack = i; + } + Collections.sort(allTasks, FREEFORM_COMPARATOR); mStackTaskList.set(allTasks); mRawTaskList = allTasks; @@ -769,12 +764,11 @@ public class TaskStack { } /** - * Computes a set of all the active and historical tasks ordered by their last active time. + * Computes a set of all the active and historical tasks. */ public ArrayList computeAllTasksList() { ArrayList tasks = new ArrayList<>(); tasks.addAll(mStackTaskList.getTasks()); - Collections.sort(tasks, LAST_ACTIVE_TIME_COMPARATOR); return tasks; } diff --git a/services/core/java/com/android/server/am/RecentTasks.java b/services/core/java/com/android/server/am/RecentTasks.java index 88faee73322f7..cea76f236fc94 100644 --- a/services/core/java/com/android/server/am/RecentTasks.java +++ b/services/core/java/com/android/server/am/RecentTasks.java @@ -65,6 +65,9 @@ class RecentTasks extends ArrayList { private static final int MAX_RECENT_BITMAPS = 3; private static final int DEFAULT_INITIAL_CAPACITY = 5; + // Whether or not to move all affiliated tasks to the front when one of the tasks is launched + private static final boolean MOVE_AFFILIATED_TASKS_TO_FRONT = false; + /** * Save recent tasks information across reboots. */ @@ -513,7 +516,7 @@ class RecentTasks extends ArrayList { if (task.inRecents) { int taskIndex = indexOf(task); if (taskIndex >= 0) { - if (!isAffiliated) { + if (!isAffiliated || MOVE_AFFILIATED_TASKS_TO_FRONT) { // Simple case: this is not an affiliated task, so we just move it to the front. remove(taskIndex); add(0, task);