Fix lockscreen launch animations once and for all

In SysUI, make sure not to dismiss Keyguard multiple times when just
waiting for a deferred dismissal, so WindowManager doesn't get
multiple calls to keyguardGoingAway.

Change heuristics how notifying Keyguard about activity drawn works.
Always notify Keyguard after executing an app transition, and notify
it also when not doing a transition after a startActivity call.

For that to work, update AppWindowToken.startingDisplayed also when
the window is displayed, but force hidden because of Keyguard.

Further, handle the case correctly when a window gets added during
the Keyguard exit animation by overriding the start time for the
animation of that new window. Also don't apply a transition animation
for a window when executing keyguard exit animation, so by removing
a starting window we don't break this animation.

Last but not least, tell Keyguard to start exiting immediately if
animations for exiting are disabled, like when going to phone/camera
on lockscreen. Before, we always had a delay of 1 second because we
waited for the timeout.

Bug: 1599196
Bug: 18272544
Change-Id: I596b2489f814b934abd256e16079d3d3f326e209
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 099151f..120002e 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -656,7 +656,6 @@
 
     void reportActivityVisibleLocked(ActivityRecord r) {
         sendWaitingVisibleReportLocked(r);
-        notifyActivityDrawnForKeyguard();
     }
 
     void sendWaitingVisibleReportLocked(ActivityRecord r) {
@@ -1832,6 +1831,7 @@
                     final ActivityStack lastStack = getLastStack();
                     ActivityRecord curTop = lastStack == null?
                             null : lastStack.topRunningNonDelayedActivityLocked(notTop);
+                    boolean movedToFront = false;
                     if (curTop != null && (curTop.task != intentActivity.task ||
                             curTop.task != lastStack.topTask())) {
                         r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
@@ -1851,6 +1851,7 @@
                                 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
                             }
                             options = null;
+                            movedToFront = true;
                         }
                     }
                     // If the caller has requested that the target task be
@@ -1865,6 +1866,12 @@
                         // sure we have correctly resumed the top activity.
                         if (doResume) {
                             resumeTopActivitiesLocked(targetStack, null, options);
+
+                            // Make sure to notify Keyguard as well if we are not running an app
+                            // transition later.
+                            if (!movedToFront) {
+                                notifyActivityDrawnForKeyguard();
+                            }
                         } else {
                             ActivityOptions.abort(options);
                         }
@@ -1956,6 +1963,11 @@
                         // sure we have correctly resumed the top activity.
                         if (doResume) {
                             targetStack.resumeTopActivityLocked(null, options);
+                            if (!movedToFront) {
+                                // Make sure to notify Keyguard as well if we are not running an app
+                                // transition later.
+                                notifyActivityDrawnForKeyguard();
+                            }
                         } else {
                             ActivityOptions.abort(options);
                         }