From ddc74155c9896b8783c1ba58499b909cce27a577 Mon Sep 17 00:00:00 2001 From: riddle_hsu Date: Tue, 7 Apr 2015 11:30:09 +0800 Subject: [PATCH] [ActivityManager] Improve task order of getRunningTasks. Symptom: During switching task in same stack, the first result of getRunningTasks will be the behind stack's top task. e.g. App Task X is starting task Y, the first entry may be home. Root Cause: TaskRecord's lastActiveTime is updated when pausing or resuming. When X task launch a new task Y, Y is on the top of task history, before X complete pause, Y's lastActiveTime will be 0 because it is a new task. Then when comparing the front task with other stack, other stack will be regarded as the newer one. Solution: If the stack is focused stack, give the top task with the last time. Change-Id: I0adc07608e03d333e0120a0dbc52a0fbbbb12f34 --- .../com/android/server/am/ActivityStack.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java index 82c71e3aec37a..daa38916e8fe1 100644 --- a/services/core/java/com/android/server/am/ActivityStack.java +++ b/services/core/java/com/android/server/am/ActivityStack.java @@ -3900,16 +3900,18 @@ final class ActivityStack { } void getTasksLocked(List list, int callingUid, boolean allowed) { + boolean focusedStack = mStackSupervisor.getFocusedStack() == this; + boolean topTask = true; for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { final TaskRecord task = mTaskHistory.get(taskNdx); + if (task.getTopActivity() == null) { + continue; + } ActivityRecord r = null; ActivityRecord top = null; int numActivities = 0; int numRunning = 0; final ArrayList activities = task.mActivities; - if (activities.isEmpty()) { - continue; - } if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) { continue; } @@ -3938,14 +3940,18 @@ final class ActivityStack { ci.baseActivity = r.intent.getComponent(); ci.topActivity = top.intent.getComponent(); ci.lastActiveTime = task.lastActiveTime; + if (focusedStack && topTask) { + // Give the latest time to ensure foreground task can be sorted + // at the first, because lastActiveTime of creating task is 0. + ci.lastActiveTime = System.currentTimeMillis(); + topTask = false; + } if (top.task != null) { ci.description = top.task.lastDescription; } ci.numActivities = numActivities; ci.numRunning = numRunning; - //System.out.println( - // "#" + maxNum + ": " + " descr=" + ci.description); list.add(ci); } }