Fixed some issues with resuming already resumed activity

- Don’t adjust focus stack when a stack is left empty because we moved
the last task in it to the stack that will be the new top (focused).
Focus can be temporarily adjusted to another stack which we don’t want.
- Protect against trying to resume an already resumed activity in
ASS.resumeFocusedStackTopActivityLocked. This will just cause a crash
on the client side.
- In ASS.moveTasksToFullscreenStackLocked defer resume activity call
until we are done moving all the tasks to the new stack.
- In ASS.moveTaskToStackUncheckedLocked check top running activity
instead of just the top activity when we are trying to determine if
the task as focus and resumed. It is possible the resumed activity
in the task isn’t the top most because the top most is still
initializing or going away.

Bug: 28219871
Change-Id: I8c1113503f6f83d39983375a4f92155ddc04f20f
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 20ef0e8..4bf28b2 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -167,6 +167,7 @@
 import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
 import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
 import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
+import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
 import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
 import static com.android.server.am.ActivityStack.STACK_VISIBLE;
 import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
@@ -1785,7 +1786,10 @@
         if (targetStack != null && isFocusedStack(targetStack)) {
             return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
         }
-        mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
+        final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
+        if (r == null || r.state != RESUMED) {
+            mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
+        }
         return false;
     }
 
@@ -2134,8 +2138,11 @@
                 for (int i = 0; i < size; i++) {
                     moveTaskToStackLocked(tasks.get(i).taskId,
                             FULLSCREEN_WORKSPACE_STACK_ID, onTop, onTop /*forceFocus*/,
-                            "moveTasksToFullscreenStack", ANIMATE);
+                            "moveTasksToFullscreenStack", ANIMATE, DEFER_RESUME);
                 }
+
+                ensureActivitiesVisibleLocked(null, 0, PRESERVE_WINDOWS);
+                resumeFocusedStackTopActivityLocked();
             } else {
                 for (int i = size - 1; i >= 0; i--) {
                     positionTaskInStackLocked(tasks.get(i).taskId,
@@ -2338,7 +2345,7 @@
             }
             // Remove current stack association, so we can re-associate the task with the
             // right stack below.
-            task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
+            task.stack.removeTask(task, "restoreRecentTaskLocked", REMOVE_TASK_MODE_MOVING);
         }
 
         final ActivityStack stack =
@@ -2381,7 +2388,7 @@
                     + "support multi-window task=" + task + " to stackId=" + stackId);
         }
 
-        final ActivityRecord r = task.getTopActivity();
+        final ActivityRecord r = task.topRunningActivityLocked();
         final ActivityStack prevStack = task.stack;
         final boolean wasFocused = isFocusedStack(prevStack) && (topRunningActivityLocked() == r);
         final boolean wasResumed = prevStack.mResumedActivity == r;