Unblock 'am start -W' if activity is brought to front without launching

If -W is used to start an activity successfully, but this activity just
brings another activity to front without actual launching, the waiting
will be stuck because neither activity will report launch complete.

In this case, we have to treat it as if the -W started an activity and
received return code START_TASK_TO_FRONT. It needs to wait for the new
activity to become visible (or report launch complete if it's already
visible).

This reverts earlier commits afb776d5447e19565c9a826a554911decb9ed92a,
since it's causing problems with launch time reporting.

bug: 28333487
bug: 29451567
Change-Id: I9fd79ab5b3ed8f9de5df34ed9c7b0be3a94620b2
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 97bfeaf..033471c 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -111,6 +111,7 @@
 import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
 import static android.app.ActivityManager.RESIZE_MODE_FORCED;
 import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
+import static android.app.ActivityManager.START_TASK_TO_FRONT;
 import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
 import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
 import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
@@ -1002,6 +1003,24 @@
         }
     }
 
+    void reportTaskToFrontNoLaunch(ActivityRecord r) {
+        boolean changed = false;
+        for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
+            WaitResult w = mWaitingActivityLaunched.remove(i);
+            if (w.who == null) {
+                changed = true;
+                // Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
+                // the starting activity ends up moving another activity to front, and it should
+                // wait for this new activity to become visible instead.
+                // Do not modify other fields.
+                w.result = START_TASK_TO_FRONT;
+            }
+        }
+        if (changed) {
+            mService.notifyAll();
+        }
+    }
+
     void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
             long thisTime, long totalTime) {
         boolean changed = false;
@@ -1015,6 +1034,7 @@
                 }
                 w.thisTime = thisTime;
                 w.totalTime = totalTime;
+                // Do not modify w.result.
             }
         }
         if (changed) {