Switch to showing top-most thumbnail of recent apps.

The way it should have been, and with the new recents enter animation
the way it must be.

Added a new method to retrieve this thumbnail, since it would be less
efficient to use the existing API (which always returns the "base"
thumbnail).  Probably at some point that existing API should be tweaked
to always return the top thumbnail instead, but that is for a later time.

Also removed code that would clear the thumbnail associated with an
activity when it is resumed.  I don't think there should ever be a
reason to clear a thumbnail -- it's much better to have *something*
for the task, even if it is a little out of date.

Change-Id: I83e6ca6403eb2df5e4de3009dfe8c210e8cf8d5b
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index 29ee0bc..df50d89 100755
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -1203,8 +1203,7 @@
         if (mMainStack) {
             mService.reportResumedActivityLocked(next);
         }
-        
-        next.clearThumbnail();
+
         if (mMainStack) {
             mService.setFocusedActivityLocked(next);
         }
@@ -4328,18 +4327,33 @@
         finishTaskMoveLocked(task);
         return true;
     }
-    
+
     public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
         TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
         ActivityRecord resumed = mResumedActivity;
         if (resumed != null && resumed.thumbHolder == tr) {
             info.mainThumbnail = resumed.stack.screenshotActivities(resumed);
-        } else {
-            info.mainThumbnail = tr.lastThumbnail;
         }
         return info;
     }
 
+    public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
+        ActivityRecord resumed = mResumedActivity;
+        if (resumed != null && resumed.task == tr) {
+            // This task is the current resumed task, we just need to take
+            // a screenshot of it and return that.
+            return resumed.stack.screenshotActivities(resumed);
+        }
+        // Return the information about the task, to figure out the top
+        // thumbnail to return.
+        TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
+        if (info.numSubThumbbails <= 0) {
+            return info.mainThumbnail;
+        } else {
+            return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
+        }
+    }
+
     public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
             boolean taskRequired) {
         TaskAccessInfo info = getTaskAccessInfoLocked(taskId, false);
@@ -4370,7 +4384,6 @@
     }
 
     public TaskAccessInfo getTaskAccessInfoLocked(int taskId, boolean inclThumbs) {
-        ActivityRecord resumed = mResumedActivity;
         final TaskAccessInfo thumbs = new TaskAccessInfo();
         // How many different sub-thumbnails?
         final int NA = mHistory.size();
@@ -4380,6 +4393,10 @@
             ActivityRecord ar = mHistory.get(j);
             if (!ar.finishing && ar.task.taskId == taskId) {
                 holder = ar.thumbHolder;
+                if (holder != null) {
+                    thumbs.mainThumbnail = holder.lastThumbnail;
+                }
+                j++;
                 break;
             }
             j++;
@@ -4394,7 +4411,6 @@
 
         ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
         thumbs.subtasks = subtasks;
-        ActivityRecord lastActivity = null;
         while (j < NA) {
             ActivityRecord ar = mHistory.get(j);
             j++;
@@ -4404,30 +4420,28 @@
             if (ar.task.taskId != taskId) {
                 break;
             }
-            lastActivity = ar;
             if (ar.thumbHolder != holder && holder != null) {
                 thumbs.numSubThumbbails++;
                 holder = ar.thumbHolder;
                 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
-                sub.thumbnail = holder.lastThumbnail;
+                sub.holder = holder;
                 sub.activity = ar;
                 sub.index = j-1;
                 subtasks.add(sub);
             }
         }
-        if (lastActivity != null && subtasks.size() > 0) {
-            if (resumed == lastActivity) {
-                TaskAccessInfo.SubTask sub = subtasks.get(subtasks.size()-1);
-                sub.thumbnail = lastActivity.stack.screenshotActivities(lastActivity);
-            }
-        }
         if (thumbs.numSubThumbbails > 0) {
             thumbs.retriever = new IThumbnailRetriever.Stub() {
                 public Bitmap getThumbnail(int index) {
                     if (index < 0 || index >= thumbs.subtasks.size()) {
                         return null;
                     }
-                    return thumbs.subtasks.get(index).thumbnail;
+                    TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
+                    ActivityRecord resumed = mResumedActivity;
+                    if (resumed != null && resumed.thumbHolder == sub.holder) {
+                        return resumed.stack.screenshotActivities(resumed);
+                    }
+                    return sub.holder.lastThumbnail;
                 }
             };
         }