Fixed pinned stack lockscreen interaction

- Use focused stack to determine of an activity can occlude the
lockscreen instead of the topmost stack. The topmost stack should be the
pinned stack which we don't want to decide if the lockscreen should be
occluded. Should be based on the stack that can contain a resume
activity.
- Make sure the remove AppWindowToken from the display if some one ever
calls removeImmediately() directly on it. This makes sure internal
states are clean-up correctly if the removal wasn't triggered from the
AM side.
- Remove some logs that end up causing confusion for testers thinking
something is broken.

Change-Id: I635f6c9a372882ff33d2e9780a1407f78cddf457
Fixes: 37208509
Test: android.server.cts.KeyguardLockedTests#testShowWhenLockedActivityAndPipActivity
Test: android.server.cts.KeyguardLockedTests#testShowWhenLockedPipActivity
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 7634644..936739b 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -95,6 +95,9 @@
     // change.
     private boolean mReparenting;
 
+    // True if we are current in the process of removing this app token from the display
+    private boolean mRemovingFromDisplay = false;
+
     // The input dispatching timeout for this application token in nanoseconds.
     long mInputDispatchingTimeoutNanos;
 
@@ -465,6 +468,12 @@
     }
 
     @Override
+    void removeImmediately() {
+        onRemovedFromDisplay();
+        super.removeImmediately();
+    }
+
+    @Override
     void removeIfPossible() {
         mIsExiting = false;
         removeAllWindowsIfPossible();
@@ -480,6 +489,11 @@
     }
 
     void onRemovedFromDisplay() {
+        if (mRemovingFromDisplay) {
+            return;
+        }
+        mRemovingFromDisplay = true;
+
         if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
 
         boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
@@ -512,12 +526,14 @@
             mService.mNoAnimationNotifyOnTransitionFinished.add(token);
         }
 
-        final TaskStack stack = getTask().mStack;
+        final TaskStack stack = getStack();
         if (delayed && !isEmpty()) {
             // set the token aside because it has an active animation to be finished
             if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
                     "removeAppToken make exiting: " + this);
-            stack.mExitingAppTokens.add(this);
+            if (stack != null) {
+                stack.mExitingAppTokens.add(this);
+            }
             mIsExiting = true;
         } else {
             // Make sure there is no animation running on this token, so any windows associated
@@ -540,6 +556,8 @@
         if (!delayed) {
             updateReportedVisibilityLocked();
         }
+
+        mRemovingFromDisplay = false;
     }
 
     void clearAnimatingFlags() {
@@ -1558,6 +1576,9 @@
         if (getController() != null) {
             pw.print(prefix); pw.print("controller="); pw.println(getController());
         }
+        if (mRemovingFromDisplay) {
+            pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
+        }
     }
 
     @Override