Notify WM that app is delayed closing due to the possibility of PIP.

WM determines the IME target after it sets up all the app transitions
and handles visiblity. In most cases, this is fine since everything is
set up properly at once. However, in cases where the app can enter PIP,
the transition to close the app is delayed in case the app decides to
enter PIP. In this case AM needs to notify WM of this information so it
can properly calculate the IME target when the app transitions aren't
fully set up.

The specific usecase is the following:
1. App with the ability to enter PIP is open with IME
2. Home is pressed.
3. Launcher is brought to the front, previous app is not yet hidden
since the close is delayed.
4. IME is recalculated and doesn't see any closing app. IME is
targeted to launcher.
5. Prevous app is ready to close.

By this point, the IME was already incorrectly targeting launcher, which
causes the flicker since IME will get set relative to launcher.

Test: Open Chrome with IME. Press home. No flicker.
Change-Id: Ie3553b0a01ad567d8b34c3453ad838b88684e79d
Fixes: 80352830
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 0ba5a56..e45de45 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -32,12 +32,11 @@
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
 
-import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
 import static android.view.WindowManager.TRANSIT_WALLPAPER_OPEN;
 import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
 import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
 import static android.view.WindowManager.TRANSIT_UNSET;
-import static com.android.server.wm.AppTransition.isKeyguardGoingAwayTransit;
+
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
@@ -257,6 +256,13 @@
     private RemoteAnimationDefinition mRemoteAnimationDefinition;
     private AnimatingAppWindowTokenRegistry mAnimatingAppWindowTokenRegistry;
 
+    /**
+     * A flag to determine if this AWT is in the process of closing or entering PIP. This is needed
+     * to help AWT know that the app is in the process of closing but hasn't yet started closing on
+     * the WM side.
+     */
+    private boolean mWillCloseOrEnterPip;
+
     AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
             DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
             boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
@@ -2235,4 +2241,21 @@
     boolean isLetterboxOverlappingWith(Rect rect) {
         return mLetterbox != null && mLetterbox.isOverlappingWith(rect);
     }
+
+    /**
+     * Sets if this AWT is in the process of closing or entering PIP.
+     * {@link #mWillCloseOrEnterPip}}
+     */
+    void setWillCloseOrEnterPip(boolean willCloseOrEnterPip) {
+        mWillCloseOrEnterPip = willCloseOrEnterPip;
+    }
+
+    /**
+     * Returns whether this AWT is considered closing. Conditions are either
+     * 1. Is this app animating and was requested to be hidden
+     * 2. App is delayed closing since it might enter PIP.
+     */
+    boolean isClosingOrEnteringPip() {
+        return (isAnimating() && hiddenRequested) || mWillCloseOrEnterPip;
+    }
 }