Replace old app transition type to new transition type (2/N)

AppTransitionController#getTransitionCompatType scans app transition
requests being set in the setup stage, and decides the app transition
animation.

Bug: 166736358
Test: Changes are behind a flag. No logic change yet.
Change-Id: I4320b04da2569d555369e6698f084d9514e36590
diff --git a/core/java/android/view/WindowManager.java b/core/java/android/view/WindowManager.java
index 72f76d1..436acef 100644
--- a/core/java/android/view/WindowManager.java
+++ b/core/java/android/view/WindowManager.java
@@ -410,6 +410,13 @@
     int TRANSIT_FLAG_APP_CRASHED = 0x10;
 
     /**
+     * Transition flag: A window in a new task is being opened behind an existing one in another
+     * activity's task.
+     * @hide
+     */
+    int TRANSIT_FLAG_OPEN_BEHIND = 0x20;
+
+    /**
      * @hide
      */
     @IntDef(flag = true, prefix = { "TRANSIT_FLAG_" }, value = {
@@ -417,7 +424,8 @@
             TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION,
             TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER,
             TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION,
-            TRANSIT_FLAG_APP_CRASHED
+            TRANSIT_FLAG_APP_CRASHED,
+            TRANSIT_FLAG_OPEN_BEHIND
     })
     @Retention(RetentionPolicy.SOURCE)
     @interface TransitionFlags {}
diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json
index eacb629..b68584e 100644
--- a/data/etc/services.core.protolog.json
+++ b/data/etc/services.core.protolog.json
@@ -241,12 +241,6 @@
       "group": "WM_DEBUG_STATES",
       "at": "com\/android\/server\/wm\/Task.java"
     },
-    "-1847087163": {
-      "message": "TRANSIT_TASK_OPEN_BEHIND,  adding %s to mOpeningApps",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_APP_TRANSITIONS",
-      "at": "com\/android\/server\/wm\/ActivityRecord.java"
-    },
     "-1844540996": {
       "message": "  Initial targets: %s",
       "level": "VERBOSE",
@@ -1195,6 +1189,12 @@
       "group": "WM_DEBUG_WINDOW_TRANSITIONS",
       "at": "com\/android\/server\/wm\/Transition.java"
     },
+    "-701167286": {
+      "message": "applyAnimation: transit=%s, enter=%b, wc=%s",
+      "level": "VERBOSE",
+      "group": "WM_DEBUG_APP_TRANSITIONS_ANIM",
+      "at": "com\/android\/server\/wm\/WindowContainer.java"
+    },
     "-694710814": {
       "message": "Pausing rotation during drag",
       "level": "DEBUG",
@@ -3289,6 +3289,12 @@
       "group": "WM_DEBUG_STATES",
       "at": "com\/android\/server\/wm\/RootWindowContainer.java"
     },
+    "1810019902": {
+      "message": "TRANSIT_FLAG_OPEN_BEHIND,  adding %s to mOpeningApps",
+      "level": "DEBUG",
+      "group": "WM_DEBUG_APP_TRANSITIONS",
+      "at": "com\/android\/server\/wm\/ActivityRecord.java"
+    },
     "1822843721": {
       "message": "Aborted starting %s: startingData=%s",
       "level": "VERBOSE",
@@ -3331,6 +3337,12 @@
       "group": "WM_DEBUG_STATES",
       "at": "com\/android\/server\/wm\/Task.java"
     },
+    "1838928243": {
+      "message": "handleAppTransitionReady: appTransition=%s openingApps=[%s] closingApps=[%s] transit=%s transitCompat=%s (%b)",
+      "level": "VERBOSE",
+      "group": "WM_DEBUG_APP_TRANSITIONS",
+      "at": "com\/android\/server\/wm\/AppTransitionController.java"
+    },
     "1853793312": {
       "message": "Notify removed startingWindow %s",
       "level": "VERBOSE",
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index f7e6822..8beec35eb 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -208,7 +208,6 @@
 import com.android.server.statusbar.StatusBarManagerInternal;
 import com.android.server.vr.VrManagerInternal;
 import com.android.server.wm.ActivityTaskManagerInternal;
-import com.android.server.wm.AppTransition;
 import com.android.server.wm.DisplayPolicy;
 import com.android.server.wm.DisplayRotation;
 import com.android.server.wm.WindowManagerInternal;
@@ -1959,14 +1958,14 @@
 
         mWindowManagerInternal.registerAppTransitionListener(new AppTransitionListener() {
             @Override
-            public int onAppTransitionStartingLocked(int transit, long duration,
+            public int onAppTransitionStartingLocked(boolean keyguardGoingAway, long duration,
                     long statusBarAnimationStartTime, long statusBarAnimationDuration) {
-                return handleStartTransitionForKeyguardLw(transit, duration);
+                return handleStartTransitionForKeyguardLw(keyguardGoingAway, duration);
             }
 
             @Override
-            public void onAppTransitionCancelledLocked(int transit) {
-                handleStartTransitionForKeyguardLw(transit, 0 /* duration */);
+            public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {
+                handleStartTransitionForKeyguardLw(keyguardGoingAway, 0 /* duration */);
             }
         });
         mKeyguardDelegate = new KeyguardServiceDelegate(mContext,
@@ -3194,7 +3193,7 @@
         }
     }
 
-    private int handleStartTransitionForKeyguardLw(int transit, long duration) {
+    private int handleStartTransitionForKeyguardLw(boolean keyguardGoingAway, long duration) {
         if (mKeyguardOccludedChanged) {
             if (DEBUG_KEYGUARD) Slog.d(TAG, "transition/occluded changed occluded="
                     + mPendingKeyguardOccluded);
@@ -3203,7 +3202,7 @@
                 return FINISH_LAYOUT_REDO_LAYOUT | FINISH_LAYOUT_REDO_WALLPAPER;
             }
         }
-        if (AppTransition.isKeyguardGoingAwayTransit(transit)) {
+        if (keyguardGoingAway) {
             if (DEBUG_KEYGUARD) Slog.d(TAG, "Starting keyguard exit animation");
             startKeyguardExitAnimation(SystemClock.uptimeMillis(), duration);
         }
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index 22ebf30..bf146d5 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -103,6 +103,7 @@
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
 import static android.view.WindowManager.TRANSIT_CLOSE;
+import static android.view.WindowManager.TRANSIT_FLAG_OPEN_BEHIND;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_OPEN_BEHIND;
@@ -150,7 +151,6 @@
 import static com.android.server.wm.ActivityRecordProto.VISIBLE_REQUESTED;
 import static com.android.server.wm.ActivityRecordProto.VISIBLE_SET_FROM_TRANSFERRED_STARTING_WINDOW;
 import static com.android.server.wm.ActivityRecordProto.WINDOW_TOKEN;
-import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_APP;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CLEANUP;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
@@ -177,6 +177,7 @@
 import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
 import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
 import static com.android.server.wm.ActivityTaskManagerService.getInputDispatchingTimeoutMillisLocked;
+import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
 import static com.android.server.wm.IdentifierProto.HASH_CODE;
 import static com.android.server.wm.IdentifierProto.TITLE;
 import static com.android.server.wm.IdentifierProto.USER_ID;
@@ -4273,14 +4274,15 @@
                 displayContent.mClosingApps.add(this);
                 mEnteringAnimation = false;
             }
-            if (appTransition.getAppTransitionOld() == TRANSIT_OLD_TASK_OPEN_BEHIND) {
+            if (appTransition.getAppTransitionOld() == TRANSIT_OLD_TASK_OPEN_BEHIND
+                    || (appTransition.getTransitFlags() & TRANSIT_FLAG_OPEN_BEHIND) != 0) {
                 // We're launchingBehind, add the launching activity to mOpeningApps.
                 final WindowState win = getDisplayContent().findFocusedWindow();
                 if (win != null) {
                     final ActivityRecord focusedActivity = win.mActivityRecord;
                     if (focusedActivity != null) {
                         ProtoLog.d(WM_DEBUG_APP_TRANSITIONS,
-                                "TRANSIT_TASK_OPEN_BEHIND,  adding %s to mOpeningApps",
+                                "TRANSIT_FLAG_OPEN_BEHIND,  adding %s to mOpeningApps",
                                 focusedActivity);
 
                         // Force animation to be loaded.
diff --git a/services/core/java/com/android/server/wm/AppTransition.java b/services/core/java/com/android/server/wm/AppTransition.java
index e146ada..4d6e9a5 100644
--- a/services/core/java/com/android/server/wm/AppTransition.java
+++ b/services/core/java/com/android/server/wm/AppTransition.java
@@ -24,6 +24,7 @@
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
+import static android.view.WindowManager.TRANSIT_FLAG_OPEN_BEHIND;
 import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
 import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
 import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
@@ -115,7 +116,6 @@
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
-import android.util.ArraySet;
 import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -340,7 +340,7 @@
 
     boolean isUnoccluding() {
         return WindowManagerService.sUseNewAppTransit
-                ? mNextAppTransitionRequests.contains(TRANSIT_OLD_KEYGUARD_UNOCCLUDE)
+                ? mNextAppTransitionRequests.contains(TRANSIT_KEYGUARD_UNOCCLUDE)
                 : mNextAppTransitionOld == TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
     }
 
@@ -460,7 +460,7 @@
      * @return bit-map of WindowManagerPolicy#FINISH_LAYOUT_REDO_* to indicate whether another
      *         layout pass needs to be done
      */
-    int goodToGo(int transit, ActivityRecord topOpeningApp, ArraySet<ActivityRecord> openingApps) {
+    int goodToGo(@TransitionOldType int transit, ActivityRecord topOpeningApp) {
         mNextAppTransitionOld = TRANSIT_OLD_UNSET;
         mNextAppTransitionFlags = 0;
         mNextAppTransitionRequests.clear();
@@ -469,7 +469,8 @@
                 topOpeningApp != null ? topOpeningApp.getAnimatingContainer() : null;
         final AnimationAdapter topOpeningAnim = wc != null ? wc.getAnimation() : null;
 
-        int redoLayout = notifyAppTransitionStartingLocked(transit,
+        int redoLayout = notifyAppTransitionStartingLocked(
+                AppTransition.isKeyguardGoingAwayTransitOld(transit),
                 topOpeningAnim != null ? topOpeningAnim.getDurationHint() : 0,
                 topOpeningAnim != null
                         ? topOpeningAnim.getStatusBarTransitionsStartTime()
@@ -494,7 +495,10 @@
     }
 
     void freeze() {
-        final int transit = mNextAppTransitionOld;
+        final boolean keyguardGoingAway = WindowManagerService.sUseNewAppTransit
+                ? mNextAppTransitionRequests.contains(TRANSIT_KEYGUARD_GOING_AWAY)
+                : isKeyguardGoingAwayTransitOld(mNextAppTransitionOld);
+
         // The RemoteAnimationControl didn't register AppTransitionListener and
         // only initialized the finish and timeout callback when goodToGo().
         // So cancel the remote animation here to prevent the animation can't do
@@ -503,9 +507,10 @@
             mRemoteAnimationController.cancelAnimation("freeze");
         }
         setAppTransitionOld(TRANSIT_OLD_UNSET, 0 /* flags */);
+        mNextAppTransitionRequests.clear();
         clear();
         setReady();
-        notifyAppTransitionCancelledLocked(transit);
+        notifyAppTransitionCancelledLocked(keyguardGoingAway);
     }
 
     private void setAppTransitionState(int state) {
@@ -525,6 +530,7 @@
     private boolean needsBoosting() {
         final boolean recentsAnimRunning = mService.getRecentsAnimationController() != null;
         return mNextAppTransitionOld != TRANSIT_OLD_UNSET
+                || !mNextAppTransitionRequests.isEmpty()
                 || mAppTransitionState == APP_STATE_READY
                 || mAppTransitionState == APP_STATE_RUNNING
                 || recentsAnimRunning;
@@ -550,9 +556,9 @@
         }
     }
 
-    private void notifyAppTransitionCancelledLocked(int transit) {
+    private void notifyAppTransitionCancelledLocked(boolean keyguardGoingAway) {
         for (int i = 0; i < mListeners.size(); i++) {
-            mListeners.get(i).onAppTransitionCancelledLocked(transit);
+            mListeners.get(i).onAppTransitionCancelledLocked(keyguardGoingAway);
         }
     }
 
@@ -562,12 +568,12 @@
         }
     }
 
-    private int notifyAppTransitionStartingLocked(int transit, long duration,
+    private int notifyAppTransitionStartingLocked(boolean keyguardGoingAway, long duration,
             long statusBarAnimationStartTime, long statusBarAnimationDuration) {
         int redoLayout = 0;
         for (int i = 0; i < mListeners.size(); i++) {
-            redoLayout |= mListeners.get(i).onAppTransitionStartingLocked(transit, duration,
-                    statusBarAnimationStartTime, statusBarAnimationDuration);
+            redoLayout |= mListeners.get(i).onAppTransitionStartingLocked(keyguardGoingAway,
+                    duration, statusBarAnimationStartTime, statusBarAnimationDuration);
         }
         return redoLayout;
     }
@@ -1578,7 +1584,8 @@
                 && !mNextAppTransitionOverrideRequested
                 && mNextAppTransitionType != NEXT_TRANSIT_TYPE_CUSTOM_IN_PLACE
                 && mNextAppTransitionType != NEXT_TRANSIT_TYPE_CLIP_REVEAL
-                && mNextAppTransitionOld != TRANSIT_OLD_KEYGUARD_GOING_AWAY;
+                && mNextAppTransitionOld != TRANSIT_OLD_KEYGUARD_GOING_AWAY
+                && !mNextAppTransitionRequests.contains(TRANSIT_KEYGUARD_GOING_AWAY);
     }
 
     RemoteAnimationController getRemoteAnimationController() {
@@ -1616,7 +1623,7 @@
         }
 
         Animation a;
-        if (isKeyguardGoingAwayTransit(transit) && enter) {
+        if (isKeyguardGoingAwayTransitOld(transit) && enter) {
             a = loadKeyguardExitAnimation(transit);
         } else if (transit == TRANSIT_OLD_KEYGUARD_OCCLUDE) {
             a = null;
@@ -1807,6 +1814,7 @@
                 : STACK_CLIP_AFTER_ANIM;
     }
 
+    @TransitionFlags
     public int getTransitFlags() {
         return mNextAppTransitionFlags;
     }
@@ -2194,6 +2202,8 @@
                 "TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION"));
         sFlagToString.add(new Pair<>(TRANSIT_FLAG_APP_CRASHED,
                 "TRANSIT_FLAG_APP_CRASHED"));
+        sFlagToString.add(new Pair<>(TRANSIT_FLAG_OPEN_BEHIND,
+                "TRANSIT_FLAG_OPEN_BEHIND"));
     }
 
     /**
@@ -2305,16 +2315,16 @@
                         + "Callers=%s",
                 appTransitionOldToString(transit), this, alwaysKeepCurrent,
                 mDisplayContent.getDisplayId(), Debug.getCallers(5));
-        final boolean allowSetCrashing = !isKeyguardTransit(mNextAppTransitionOld)
+        final boolean allowSetCrashing = !isKeyguardTransitOld(mNextAppTransitionOld)
                 && transit == TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE;
-        if (forceOverride || isKeyguardTransit(transit) || !isTransitionSet()
+        if (forceOverride || isKeyguardTransitOld(transit) || !isTransitionSet()
                 || mNextAppTransitionOld == TRANSIT_OLD_NONE || allowSetCrashing) {
             setAppTransitionOld(transit, flags);
         }
         // We never want to change from a Keyguard transit to a non-Keyguard transit, as our logic
         // relies on the fact that we always execute a Keyguard transition after preparing one. We
         // also don't want to change away from a crashing transition.
-        else if (!alwaysKeepCurrent && !isKeyguardTransit(mNextAppTransitionOld)
+        else if (!alwaysKeepCurrent && !isKeyguardTransitOld(mNextAppTransitionOld)
                 && mNextAppTransitionOld != TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE) {
             if (transit == TRANSIT_OLD_TASK_OPEN && isTransitionOldEqual(TRANSIT_OLD_TASK_CLOSE)) {
                 // Opening a new task always supersedes a close for the anim.
@@ -2323,7 +2333,7 @@
                     && isTransitionOldEqual(TRANSIT_OLD_ACTIVITY_CLOSE)) {
                 // Opening a new activity always supersedes a close for the anim.
                 setAppTransitionOld(transit, flags);
-            } else if (isTaskTransit(transit) && isActivityTransit(mNextAppTransitionOld)) {
+            } else if (isTaskTransitOld(transit) && isActivityTransitOld(mNextAppTransitionOld)) {
                 // Task animations always supersede activity animations, because if we have both, it
                 // usually means that activity transition were just trampoline activities.
                 setAppTransitionOld(transit, flags);
@@ -2349,39 +2359,39 @@
      * @return true if {@param transit} is representing a transition in which Keyguard is going
      *         away, false otherwise
      */
-    public static boolean isKeyguardGoingAwayTransit(int transit) {
+    public static boolean isKeyguardGoingAwayTransitOld(int transit) {
         return transit == TRANSIT_OLD_KEYGUARD_GOING_AWAY
                 || transit == TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER;
     }
 
-    static boolean isKeyguardTransit(@TransitionOldType int transit) {
-        return isKeyguardGoingAwayTransit(transit) || transit == TRANSIT_OLD_KEYGUARD_OCCLUDE
+    static boolean isKeyguardTransitOld(@TransitionOldType int transit) {
+        return isKeyguardGoingAwayTransitOld(transit) || transit == TRANSIT_OLD_KEYGUARD_OCCLUDE
                 || transit == TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
     }
 
-    static boolean isTaskTransit(@TransitionOldType int transit) {
-        return isTaskOpenTransit(transit)
+    static boolean isTaskTransitOld(@TransitionOldType int transit) {
+        return isTaskOpenTransitOld(transit)
                 || transit == TRANSIT_OLD_TASK_CLOSE
                 || transit == TRANSIT_OLD_TASK_TO_BACK;
     }
 
-    private static  boolean isTaskOpenTransit(@TransitionOldType int transit) {
+    private static  boolean isTaskOpenTransitOld(@TransitionOldType int transit) {
         return transit == TRANSIT_OLD_TASK_OPEN
                 || transit == TRANSIT_OLD_TASK_OPEN_BEHIND
                 || transit == TRANSIT_OLD_TASK_TO_FRONT;
     }
 
-    static boolean isActivityTransit(@TransitionOldType int transit) {
+    static boolean isActivityTransitOld(@TransitionOldType int transit) {
         return transit == TRANSIT_OLD_ACTIVITY_OPEN
                 || transit == TRANSIT_OLD_ACTIVITY_CLOSE
                 || transit == TRANSIT_OLD_ACTIVITY_RELAUNCH;
     }
 
-    static boolean isChangeTransit(@TransitionOldType int transit) {
+    static boolean isChangeTransitOld(@TransitionOldType int transit) {
         return transit == TRANSIT_OLD_TASK_CHANGE_WINDOWING_MODE;
     }
 
-    static boolean isClosingTransit(@TransitionOldType int transit) {
+    static boolean isClosingTransitOld(@TransitionOldType int transit) {
         return transit == TRANSIT_OLD_ACTIVITY_CLOSE
                 || transit == TRANSIT_OLD_TASK_CLOSE
                 || transit == TRANSIT_OLD_WALLPAPER_CLOSE
@@ -2390,6 +2400,51 @@
                 || transit == TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE;
     }
 
+    static boolean isNormalTransit(@TransitionType int transit) {
+        return transit == TRANSIT_OPEN
+                || transit == TRANSIT_CLOSE
+                || transit == TRANSIT_TO_FRONT
+                || transit == TRANSIT_TO_BACK;
+    }
+
+    static boolean isKeyguardTransit(@TransitionType int transit) {
+        return transit == TRANSIT_KEYGUARD_GOING_AWAY
+                || transit == TRANSIT_KEYGUARD_OCCLUDE
+                || transit == TRANSIT_KEYGUARD_UNOCCLUDE;
+    }
+
+    @TransitionType int getKeyguardTransition() {
+        // In case we unocclude Keyguard and occlude it again, meaning that we never actually
+        // unoccclude/occlude Keyguard, but just run a normal transition.
+        final int occludeIndex = mNextAppTransitionRequests.indexOf(TRANSIT_KEYGUARD_UNOCCLUDE);
+        if (occludeIndex != -1
+                && occludeIndex < mNextAppTransitionRequests.indexOf(TRANSIT_KEYGUARD_OCCLUDE)) {
+            return TRANSIT_NONE;
+        }
+
+        for (int i = 0; i < mNextAppTransitionRequests.size(); ++i) {
+            final @TransitionType int transit = mNextAppTransitionRequests.get(i);
+            if (isKeyguardTransit(transit)) {
+                return transit;
+            }
+        }
+        return TRANSIT_NONE;
+    }
+
+    @TransitionType int getFirstAppTransition() {
+        for (int i = 0; i < mNextAppTransitionRequests.size(); ++i) {
+            final @TransitionType int transit = mNextAppTransitionRequests.get(i);
+            if (transit != TRANSIT_NONE && !isKeyguardTransit(transit)) {
+                return transit;
+            }
+        }
+        return TRANSIT_NONE;
+    }
+
+    boolean containsTransitRequest(@TransitionType int transit) {
+        return mNextAppTransitionRequests.contains(transit);
+    }
+
     /**
      * @return whether the transition should show the thumbnail being scaled down.
      */
diff --git a/services/core/java/com/android/server/wm/AppTransitionController.java b/services/core/java/com/android/server/wm/AppTransitionController.java
index 069f962..0c5f89c 100644
--- a/services/core/java/com/android/server/wm/AppTransitionController.java
+++ b/services/core/java/com/android/server/wm/AppTransitionController.java
@@ -17,19 +17,31 @@
 package com.android.server.wm;
 
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
+import static android.view.WindowManager.TRANSIT_CHANGE_WINDOWING_MODE;
+import static android.view.WindowManager.TRANSIT_CLOSE;
+import static android.view.WindowManager.TRANSIT_FLAG_APP_CRASHED;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
+import static android.view.WindowManager.TRANSIT_FLAG_OPEN_BEHIND;
+import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
+import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
+import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
+import static android.view.WindowManager.TRANSIT_NONE;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_OPEN;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_RELAUNCH;
 import static android.view.WindowManager.TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY;
 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER;
+import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_OCCLUDE;
+import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
 import static android.view.WindowManager.TRANSIT_OLD_NONE;
+import static android.view.WindowManager.TRANSIT_OLD_TASK_CHANGE_WINDOWING_MODE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_OPEN;
+import static android.view.WindowManager.TRANSIT_OLD_TASK_OPEN_BEHIND;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_TO_BACK;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_TO_FRONT;
 import static android.view.WindowManager.TRANSIT_OLD_TRANSLUCENT_ACTIVITY_CLOSE;
@@ -38,6 +50,10 @@
 import static android.view.WindowManager.TRANSIT_OLD_WALLPAPER_INTRA_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_WALLPAPER_INTRA_OPEN;
 import static android.view.WindowManager.TRANSIT_OLD_WALLPAPER_OPEN;
+import static android.view.WindowManager.TRANSIT_OPEN;
+import static android.view.WindowManager.TRANSIT_RELAUNCH;
+import static android.view.WindowManager.TRANSIT_TO_BACK;
+import static android.view.WindowManager.TRANSIT_TO_FRONT;
 
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS_ANIM;
@@ -46,7 +62,8 @@
 import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_SNAPSHOT;
 import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_SPLASH_SCREEN;
 import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_WINDOWS_DRAWN;
-import static com.android.server.wm.AppTransition.isKeyguardGoingAwayTransit;
+import static com.android.server.wm.AppTransition.isKeyguardGoingAwayTransitOld;
+import static com.android.server.wm.AppTransition.isNormalTransit;
 import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_APP_TRANSITION;
 import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS;
 import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
@@ -54,6 +71,7 @@
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.os.Trace;
 import android.util.ArrayMap;
 import android.util.ArraySet;
@@ -63,7 +81,9 @@
 import android.view.RemoteAnimationDefinition;
 import android.view.WindowManager;
 import android.view.WindowManager.LayoutParams;
+import android.view.WindowManager.TransitionFlags;
 import android.view.WindowManager.TransitionOldType;
+import android.view.WindowManager.TransitionType;
 import android.view.animation.Animation;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -97,6 +117,30 @@
     }
 
     /**
+     * Returns the currently visible window that is associated with the wallpaper in case we are
+     * transitioning from an activity with a wallpaper to one without.
+     */
+    private @Nullable WindowState getOldWallpaper() {
+        final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
+        final @TransitionType int firstTransit =
+                mDisplayContent.mAppTransition.getFirstAppTransition();
+
+        final ArraySet<WindowContainer> openingWcs = getAnimationTargets(
+                mDisplayContent.mOpeningApps, mDisplayContent.mClosingApps, true /* visible */);
+        final boolean showWallpaper = wallpaperTarget != null
+                && ((wallpaperTarget.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0
+                // Update task open transition to wallpaper transition when wallpaper is visible.
+                // (i.e.launching app info activity from recent tasks)
+                || ((firstTransit == TRANSIT_OPEN || firstTransit == TRANSIT_TO_FRONT)
+                && (!openingWcs.isEmpty() && openingWcs.valueAt(0).asTask() != null)
+                && mWallpaperControllerLocked.isWallpaperVisible()));
+        // If wallpaper is animating or wallpaperTarget doesn't have SHOW_WALLPAPER flag set,
+        // don't consider upgrading to wallpaper transition.
+        return (mWallpaperControllerLocked.isWallpaperTargetAnimating() || !showWallpaper)
+                ? null : wallpaperTarget;
+    }
+
+    /**
      * Handle application transition for given display.
      */
     void handleAppTransitionReady() {
@@ -109,11 +153,11 @@
         Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "AppTransitionReady");
 
         ProtoLog.v(WM_DEBUG_APP_TRANSITIONS, "**** GOOD TO GO");
-        // TODO(new-app-transition): Compute the best transition from
-        //  appTransition.mNextAppTransitionRequests
+        // TODO(new-app-transition): Remove code using appTransition.getAppTransition()
         final AppTransition appTransition = mDisplayContent.mAppTransition;
-        int transit = appTransition.getAppTransitionOld();
-        if (mDisplayContent.mSkipAppTransitionAnimation && !isKeyguardGoingAwayTransit(transit)) {
+        @TransitionOldType int transit = appTransition.getAppTransitionOld();
+        if (mDisplayContent.mSkipAppTransitionAnimation
+                && !isKeyguardGoingAwayTransitOld(transit)) {
             transit = WindowManager.TRANSIT_OLD_UNSET;
         }
         mDisplayContent.mSkipAppTransitionAnimation = false;
@@ -149,7 +193,8 @@
 
         // Determine if closing and opening app token sets are wallpaper targets, in which case
         // special animations are needed.
-        final boolean hasWallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget() != null;
+        final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget();
+        final boolean hasWallpaperTarget =  wallpaperTarget != null;
         final boolean openingAppHasWallpaper = canBeWallpaperTarget(mDisplayContent.mOpeningApps)
                 && hasWallpaperTarget;
         final boolean closingAppHasWallpaper = canBeWallpaperTarget(mDisplayContent.mClosingApps)
@@ -159,6 +204,24 @@
         transit = maybeUpdateTransitToWallpaper(transit, openingAppHasWallpaper,
                 closingAppHasWallpaper);
 
+        final @TransitionOldType int transitCompat = getTransitCompatType(
+                mDisplayContent.mAppTransition,
+                mDisplayContent.mOpeningApps, mDisplayContent.mClosingApps,
+                wallpaperTarget, getOldWallpaper());
+
+        ProtoLog.v(WM_DEBUG_APP_TRANSITIONS,
+                "handleAppTransitionReady: appTransition=%s"
+                + " openingApps=[%s] closingApps=[%s] transit=%s transitCompat=%s (%b)",
+                appTransition.toString(),
+                mDisplayContent.mOpeningApps, mDisplayContent.mClosingApps,
+                AppTransition.appTransitionOldToString(transit),
+                AppTransition.appTransitionOldToString(transitCompat),
+                transit == transitCompat);
+
+        if (WindowManagerService.sUseNewAppTransit) {
+            transit = transitCompat;
+        }
+
         // Find the layout params of the top-most application window in the tokens, which is
         // what will control the animation theme. If all closing windows are obscured, then there is
         // no need to do an animation. This is the case, for example, when this transition is being
@@ -191,8 +254,7 @@
                     topClosingApp, topChangingApp);
 
             final int flags = appTransition.getTransitFlags();
-            layoutRedo = appTransition.goodToGo(transit, topOpeningApp,
-                    mDisplayContent.mOpeningApps);
+            layoutRedo = appTransition.goodToGo(transit, topOpeningApp);
             handleNonAppWindowsInTransition(transit, flags);
             appTransition.postAnimationCallback();
             appTransition.clear();
@@ -222,6 +284,160 @@
                 layoutRedo | FINISH_LAYOUT_REDO_LAYOUT | FINISH_LAYOUT_REDO_CONFIG;
     }
 
+    /**
+     * Get old transit type based on the current transit requests.
+     *
+     * @param appTransition {@link AppTransition} for managing app transition state.
+     * @param openingApps {@link ActivityRecord}s which are becoming visible.
+     * @param closingApps {@link ActivityRecord}s which are becoming invisible.
+     * @param wallpaperTarget If non-null, this is the currently visible window that is associated
+     *                        with the wallpaper.
+     * @param oldWallpaper The currently visible window that is associated with the wallpaper in
+     *                     case we are transitioning from an activity with a wallpaper to one
+     *                     without. Otherwise null.
+     */
+    static @TransitionOldType int getTransitCompatType(AppTransition appTransition,
+            ArraySet<ActivityRecord> openingApps, ArraySet<ActivityRecord> closingApps,
+            @Nullable WindowState wallpaperTarget, @Nullable WindowState oldWallpaper) {
+
+        final boolean openingAppHasWallpaper = canBeWallpaperTarget(openingApps)
+                && wallpaperTarget != null;
+        final boolean closingAppHasWallpaper = canBeWallpaperTarget(closingApps)
+                && wallpaperTarget != null;
+
+        // Keyguard transit has highest priority.
+        switch (appTransition.getKeyguardTransition()) {
+            case TRANSIT_KEYGUARD_GOING_AWAY:
+                return openingAppHasWallpaper ? TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER
+                        : TRANSIT_OLD_KEYGUARD_GOING_AWAY;
+            case TRANSIT_KEYGUARD_OCCLUDE:
+                // When there is a closing app, the keyguard has already been occluded by an
+                // activity, and another activity has started on top of that activity, so normal
+                // app transition animation should be used.
+                return closingApps.isEmpty() ? TRANSIT_OLD_KEYGUARD_OCCLUDE
+                        : TRANSIT_OLD_ACTIVITY_OPEN;
+            case TRANSIT_KEYGUARD_UNOCCLUDE:
+                return TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
+        }
+
+        final @TransitionFlags int flags = appTransition.getTransitFlags();
+        final @TransitionType int firstTransit = appTransition.getFirstAppTransition();
+
+        // Special transitions
+        // TODO(new-app-transitions): Revisit if those can be rewritten by using flags.
+        if (appTransition.containsTransitRequest(TRANSIT_CHANGE_WINDOWING_MODE)) {
+            return TRANSIT_OLD_TASK_CHANGE_WINDOWING_MODE;
+        }
+        if ((flags & TRANSIT_FLAG_APP_CRASHED) != 0) {
+            return TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE;
+        }
+        if (firstTransit == TRANSIT_NONE) {
+            return TRANSIT_OLD_NONE;
+        }
+
+        /*
+         * There are cases where we open/close a new task/activity, but in reality only a
+         * translucent activity on top of existing activities is opening/closing. For that one, we
+         * have a different animation because non of the task/activity animations actually work well
+         * with translucent apps.
+         */
+        if (isNormalTransit(firstTransit)) {
+            boolean allOpeningVisible = true;
+            boolean allTranslucentOpeningApps = !openingApps.isEmpty();
+            for (int i = openingApps.size() - 1; i >= 0; i--) {
+                final ActivityRecord activity = openingApps.valueAt(i);
+                if (!activity.isVisible()) {
+                    allOpeningVisible = false;
+                    if (activity.fillsParent()) {
+                        allTranslucentOpeningApps = false;
+                    }
+                }
+            }
+            boolean allTranslucentClosingApps = !closingApps.isEmpty();
+            for (int i = closingApps.size() - 1; i >= 0; i--) {
+                if (closingApps.valueAt(i).fillsParent()) {
+                    allTranslucentClosingApps = false;
+                    break;
+                }
+            }
+
+            if (allTranslucentClosingApps && allOpeningVisible) {
+                return TRANSIT_OLD_TRANSLUCENT_ACTIVITY_CLOSE;
+            }
+            if (allTranslucentOpeningApps && closingApps.isEmpty()) {
+                return TRANSIT_OLD_TRANSLUCENT_ACTIVITY_OPEN;
+            }
+        }
+
+        final ActivityRecord topOpeningApp = getTopApp(openingApps,
+                false /* ignoreHidden */);
+        final ActivityRecord topClosingApp = getTopApp(closingApps,
+                true /* ignoreHidden */);
+
+        if (closingAppHasWallpaper && openingAppHasWallpaper) {
+            ProtoLog.v(WM_DEBUG_APP_TRANSITIONS, "Wallpaper animation!");
+            switch (firstTransit) {
+                case TRANSIT_OPEN:
+                    return TRANSIT_OLD_WALLPAPER_INTRA_OPEN;
+                case TRANSIT_CLOSE:
+                    return TRANSIT_OLD_WALLPAPER_INTRA_CLOSE;
+            }
+        } else if (oldWallpaper != null && !openingApps.isEmpty()
+                && !openingApps.contains(oldWallpaper.mActivityRecord)
+                && closingApps.contains(oldWallpaper.mActivityRecord)
+                && topClosingApp == oldWallpaper.mActivityRecord) {
+            // We are transitioning from an activity with a wallpaper to one without.
+            return TRANSIT_OLD_WALLPAPER_CLOSE;
+        } else if (wallpaperTarget != null && wallpaperTarget.isVisible()
+                && openingApps.contains(wallpaperTarget.mActivityRecord)
+                && topOpeningApp == wallpaperTarget.mActivityRecord
+                /* && transit != TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE */) {
+            // We are transitioning from an activity without
+            // a wallpaper to now showing the wallpaper
+            return TRANSIT_OLD_WALLPAPER_OPEN;
+        }
+
+        final ArraySet<WindowContainer> openingWcs = getAnimationTargets(
+                openingApps, closingApps, true /* visible */);
+        final ArraySet<WindowContainer> closingWcs = getAnimationTargets(
+                openingApps, closingApps, false /* visible */);
+        final boolean isActivityOpening = !openingWcs.isEmpty()
+                && openingWcs.valueAt(0).asActivityRecord() != null;
+        final boolean isActivityClosing = !closingWcs.isEmpty()
+                && closingWcs.valueAt(0).asActivityRecord() != null;
+        final boolean isTaskOpening = !openingWcs.isEmpty() && !isActivityOpening;
+        final boolean isTaskClosing = !closingWcs.isEmpty() && !isActivityClosing;
+
+        if (appTransition.containsTransitRequest(TRANSIT_TO_FRONT) && isTaskOpening) {
+            return TRANSIT_OLD_TASK_TO_FRONT;
+        }
+        if (appTransition.containsTransitRequest(TRANSIT_TO_BACK) && isTaskClosing) {
+            return TRANSIT_OLD_TASK_TO_BACK;
+        }
+        if (appTransition.containsTransitRequest(TRANSIT_OPEN)) {
+            if (isTaskOpening) {
+                return (appTransition.getTransitFlags() & TRANSIT_FLAG_OPEN_BEHIND) != 0
+                        ? TRANSIT_OLD_TASK_OPEN_BEHIND : TRANSIT_OLD_TASK_OPEN;
+            }
+            if (isActivityOpening) {
+                return TRANSIT_OLD_ACTIVITY_OPEN;
+            }
+        }
+        if (appTransition.containsTransitRequest(TRANSIT_CLOSE)) {
+            if (isTaskClosing) {
+                return TRANSIT_OLD_TASK_CLOSE;
+            }
+            if (isActivityClosing) {
+                return TRANSIT_OLD_ACTIVITY_CLOSE;
+            }
+        }
+        if (appTransition.containsTransitRequest(TRANSIT_RELAUNCH)
+                && !openingWcs.isEmpty() && !openingApps.isEmpty()) {
+            return TRANSIT_OLD_ACTIVITY_RELAUNCH;
+        }
+        return TRANSIT_OLD_NONE;
+    }
+
     private static WindowManager.LayoutParams getAnimLp(ActivityRecord activity) {
         final WindowState mainWindow = activity != null ? activity.findMainWindow() : null;
         return mainWindow != null ? mainWindow.mAttrs : null;
@@ -698,7 +914,7 @@
         // Never convert a change transition since the top activity isn't changing and will likely
         // still be above an opening wallpaper.
         if (transit == TRANSIT_OLD_NONE || transit == TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE
-                || AppTransition.isChangeTransit(transit)) {
+                || AppTransition.isChangeTransitOld(transit)) {
             return transit;
         }
 
@@ -734,7 +950,7 @@
         }
         // We never want to change from a Keyguard transit to a non-Keyguard transit, as our logic
         // relies on the fact that we always execute a Keyguard transition after preparing one.
-        else if (!isKeyguardGoingAwayTransit(transit)) {
+        else if (!isKeyguardGoingAwayTransitOld(transit)) {
             if (closingAppHasWallpaper && openingAppHasWallpaper) {
                 ProtoLog.v(WM_DEBUG_APP_TRANSITIONS, "Wallpaper animation!");
                 switch (transit) {
@@ -788,12 +1004,12 @@
      */
     @VisibleForTesting
     int maybeUpdateTransitToTranslucentAnim(@TransitionOldType int transit) {
-        if (AppTransition.isChangeTransit(transit)) {
+        if (AppTransition.isChangeTransitOld(transit)) {
             // There's no special animation to handle change animations with translucent apps
             return transit;
         }
-        final boolean taskOrActivity = AppTransition.isTaskTransit(transit)
-                || AppTransition.isActivityTransit(transit);
+        final boolean taskOrActivity = AppTransition.isTaskTransitOld(transit)
+                || AppTransition.isActivityTransitOld(transit);
         boolean allOpeningVisible = true;
         boolean allTranslucentOpeningApps = !mDisplayContent.mOpeningApps.isEmpty();
         for (int i = mDisplayContent.mOpeningApps.size() - 1; i >= 0; i--) {
@@ -855,7 +1071,7 @@
         return true;
     }
 
-    private boolean canBeWallpaperTarget(ArraySet<ActivityRecord> apps) {
+    private static boolean canBeWallpaperTarget(ArraySet<ActivityRecord> apps) {
         for (int i = apps.size() - 1; i >= 0; i--) {
             if (apps.valueAt(i).windowsCanBeWallpaperTarget()) {
                 return true;
@@ -873,7 +1089,7 @@
      *                        {@link ActivityRecord#isVisible}.
      * @return The top {@link ActivityRecord}.
      */
-    private ActivityRecord getTopApp(ArraySet<? extends WindowContainer> apps,
+    private static ActivityRecord getTopApp(ArraySet<? extends WindowContainer> apps,
             boolean ignoreInvisible) {
         int topPrefixOrderIndex = Integer.MIN_VALUE;
         ActivityRecord topApp = null;
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index c2f43fe..5be6893 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -73,6 +73,8 @@
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_OPEN;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_OPEN;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_TO_FRONT;
+import static android.view.WindowManager.TRANSIT_OPEN;
+import static android.view.WindowManager.TRANSIT_TO_FRONT;
 import static android.window.DisplayAreaOrganizer.FEATURE_ROOT;
 import static android.window.DisplayAreaOrganizer.FEATURE_WINDOWED_MAGNIFICATION;
 
@@ -4535,6 +4537,10 @@
     void prepareAppTransitionOld(@WindowManager.TransitionOldType int transit,
             boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
             boolean forceOverride) {
+        if (WindowManagerService.sUseNewAppTransit) {
+            return;
+        }
+
         final boolean prepared = mAppTransition.prepareAppTransitionOld(
                 transit, alwaysKeepCurrent, flags, forceOverride);
         if (prepared && okToAnimate()) {
@@ -4620,6 +4626,10 @@
 
     /** Check if pending app transition is for activity / task launch. */
     boolean isNextTransitionForward() {
+        if (WindowManagerService.sUseNewAppTransit) {
+            return mAppTransition.containsTransitRequest(TRANSIT_OPEN)
+                    || mAppTransition.containsTransitRequest(TRANSIT_TO_FRONT);
+        }
         final int transit = mAppTransition.getAppTransitionOld();
         return transit == TRANSIT_OLD_ACTIVITY_OPEN
                 || transit == TRANSIT_OLD_TASK_OPEN
@@ -5586,7 +5596,7 @@
         }
 
         @Override
-        public void onAppTransitionCancelledLocked(int transit) {
+        public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {
             continueUpdateOrientationForDiffOrienLaunchingApp();
         }
 
diff --git a/services/core/java/com/android/server/wm/DisplayPolicy.java b/services/core/java/com/android/server/wm/DisplayPolicy.java
index fc3fc47..969e278 100644
--- a/services/core/java/com/android/server/wm/DisplayPolicy.java
+++ b/services/core/java/com/android/server/wm/DisplayPolicy.java
@@ -612,7 +612,7 @@
             }
 
             @Override
-            public int onAppTransitionStartingLocked(int transit, long duration,
+            public int onAppTransitionStartingLocked(boolean keyguardGoingAway, long duration,
                     long statusBarAnimationStartTime, long statusBarAnimationDuration) {
                 mHandler.post(() -> {
                     StatusBarManagerInternal statusBar = getStatusBarManagerInternal();
@@ -625,7 +625,7 @@
             }
 
             @Override
-            public void onAppTransitionCancelledLocked(int transit) {
+            public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {
                 mHandler.post(mAppTransitionCancelled);
             }
 
diff --git a/services/core/java/com/android/server/wm/KeyguardController.java b/services/core/java/com/android/server/wm/KeyguardController.java
index 7b60092..35c28cb 100644
--- a/services/core/java/com/android/server/wm/KeyguardController.java
+++ b/services/core/java/com/android/server/wm/KeyguardController.java
@@ -35,9 +35,9 @@
 import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
 import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
 
-import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
+import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
 import static com.android.server.wm.KeyguardControllerProto.AOD_SHOWING;
 import static com.android.server.wm.KeyguardControllerProto.KEYGUARD_OCCLUDED_STATES;
 import static com.android.server.wm.KeyguardControllerProto.KEYGUARD_SHOWING;
@@ -397,10 +397,10 @@
         // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
         final DisplayContent dc = mRootWindowContainer.getDefaultDisplay();
         if (mKeyguardShowing && canDismissKeyguard()
-                && dc.mAppTransition.getAppTransitionOld() == TRANSIT_OLD_KEYGUARD_UNOCCLUDE) {
+                && (dc.mAppTransition.getAppTransitionOld() == TRANSIT_OLD_KEYGUARD_UNOCCLUDE
+                || dc.mAppTransition.containsTransitRequest(TRANSIT_KEYGUARD_UNOCCLUDE))) {
             dc.prepareAppTransitionOld(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
                     0 /* flags */, true /* forceOverride */);
-            dc.prepareAppTransition(TRANSIT_KEYGUARD_UNOCCLUDE);
             mWindowManager.executeAppTransition();
         }
     }
diff --git a/services/core/java/com/android/server/wm/RecentsAnimationController.java b/services/core/java/com/android/server/wm/RecentsAnimationController.java
index 5f8b12e..991ba5f 100644
--- a/services/core/java/com/android/server/wm/RecentsAnimationController.java
+++ b/services/core/java/com/android/server/wm/RecentsAnimationController.java
@@ -151,14 +151,14 @@
      */
     final AppTransitionListener mAppTransitionListener = new AppTransitionListener() {
         @Override
-        public int onAppTransitionStartingLocked(int transit, long duration,
+        public int onAppTransitionStartingLocked(boolean keyguardGoingAway, long duration,
                 long statusBarAnimationStartTime, long statusBarAnimationDuration) {
             continueDeferredCancel();
             return 0;
         }
 
         @Override
-        public void onAppTransitionCancelledLocked(int transit) {
+        public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {
             continueDeferredCancel();
         }
 
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index 5e80670..9be0b21 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -42,6 +42,7 @@
 import static android.view.WindowManager.TRANSIT_OLD_CRASHING_ACTIVITY_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_NONE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_TO_BACK;
+import static android.view.WindowManager.TRANSIT_TO_BACK;
 
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_FOCUS_LIGHT;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_KEEP_SCREEN_ON;
@@ -53,11 +54,6 @@
 import static com.android.server.policy.PhoneWindowManager.SYSTEM_DIALOG_REASON_ASSIST;
 import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
 import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
-import static com.android.server.wm.ActivityTaskSupervisor.DEFER_RESUME;
-import static com.android.server.wm.ActivityTaskSupervisor.ON_TOP;
-import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
-import static com.android.server.wm.ActivityTaskSupervisor.dumpHistoryList;
-import static com.android.server.wm.ActivityTaskSupervisor.printThisActivity;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ROOT_TASK;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
@@ -66,6 +62,11 @@
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS;
 import static com.android.server.wm.ActivityTaskManagerService.ANIMATE;
 import static com.android.server.wm.ActivityTaskManagerService.TAG_SWITCH;
+import static com.android.server.wm.ActivityTaskSupervisor.DEFER_RESUME;
+import static com.android.server.wm.ActivityTaskSupervisor.ON_TOP;
+import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
+import static com.android.server.wm.ActivityTaskSupervisor.dumpHistoryList;
+import static com.android.server.wm.ActivityTaskSupervisor.printThisActivity;
 import static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
 import static com.android.server.wm.RootWindowContainerProto.IS_HOME_RECENTS_COMPONENT;
 import static com.android.server.wm.RootWindowContainerProto.KEYGUARD_CONTROLLER;
@@ -2213,8 +2214,10 @@
                 // to the list of apps being closed, and request its transition to be ran.
                 final ActivityRecord oldTopActivity = task.getTopMostActivity();
                 if (oldTopActivity != null && oldTopActivity.isState(STOPPED)
-                        && task.getDisplayContent().mAppTransition.getAppTransitionOld()
-                        == TRANSIT_OLD_TASK_TO_BACK) {
+                        && (task.getDisplayContent().mAppTransition.getAppTransitionOld()
+                        == TRANSIT_OLD_TASK_TO_BACK
+                        || task.getDisplayContent().mAppTransition.containsTransitRequest(
+                                TRANSIT_TO_BACK))) {
                     task.getDisplayContent().mClosingApps.add(oldTopActivity);
                     oldTopActivity.mRequestForceTransition = true;
                 }
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 0e2c2f9..dda1bb8 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -62,6 +62,7 @@
 import static android.view.WindowManager.TRANSIT_CHANGE_WINDOWING_MODE;
 import static android.view.WindowManager.TRANSIT_CLOSE;
 import static android.view.WindowManager.TRANSIT_FLAG_APP_CRASHED;
+import static android.view.WindowManager.TRANSIT_FLAG_OPEN_BEHIND;
 import static android.view.WindowManager.TRANSIT_NONE;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_OPEN;
@@ -85,12 +86,6 @@
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_STATES;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_TASKS;
 import static com.android.server.wm.ActivityRecord.STARTING_WINDOW_SHOWN;
-import static com.android.server.wm.ActivityTaskSupervisor.DEFER_RESUME;
-import static com.android.server.wm.ActivityTaskSupervisor.ON_TOP;
-import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
-import static com.android.server.wm.ActivityTaskSupervisor.REMOVE_FROM_RECENTS;
-import static com.android.server.wm.ActivityTaskSupervisor.dumpHistoryList;
-import static com.android.server.wm.ActivityTaskSupervisor.printThisActivity;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
@@ -113,6 +108,12 @@
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.ActivityTaskManagerService.H.FIRST_ACTIVITY_STACK_MSG;
+import static com.android.server.wm.ActivityTaskSupervisor.DEFER_RESUME;
+import static com.android.server.wm.ActivityTaskSupervisor.ON_TOP;
+import static com.android.server.wm.ActivityTaskSupervisor.PRESERVE_WINDOWS;
+import static com.android.server.wm.ActivityTaskSupervisor.REMOVE_FROM_RECENTS;
+import static com.android.server.wm.ActivityTaskSupervisor.dumpHistoryList;
+import static com.android.server.wm.ActivityTaskSupervisor.printThisActivity;
 import static com.android.server.wm.IdentifierProto.HASH_CODE;
 import static com.android.server.wm.IdentifierProto.TITLE;
 import static com.android.server.wm.IdentifierProto.USER_ID;
@@ -2436,14 +2437,21 @@
 
     @VisibleForTesting
     boolean isInChangeTransition() {
-        return mSurfaceFreezer.hasLeash() || AppTransition.isChangeTransit(mTransit);
+        return mSurfaceFreezer.hasLeash() || AppTransition.isChangeTransitOld(mTransit);
     }
 
     @Override
     public SurfaceControl getFreezeSnapshotTarget() {
-        final int transit = mDisplayContent.mAppTransition.getAppTransitionOld();
-        if (!AppTransition.isChangeTransit(transit)) {
-            return null;
+        if (WindowManagerService.sUseNewAppTransit) {
+            if (!mDisplayContent.mAppTransition.containsTransitRequest(
+                    TRANSIT_CHANGE_WINDOWING_MODE)) {
+                return null;
+            }
+        } else {
+            if (!AppTransition.isChangeTransitOld(
+                    mDisplayContent.mAppTransition.getAppTransitionOld())) {
+                return null;
+            }
         }
         // Skip creating snapshot if this transition is controlled by a remote animator which
         // doesn't need it.
@@ -2451,7 +2459,7 @@
         activityTypes.add(getActivityType());
         final RemoteAnimationAdapter adapter =
                 mDisplayContent.mAppTransitionController.getRemoteAnimationOverride(
-                        this, transit, activityTypes);
+                        this, TRANSIT_OLD_TASK_CHANGE_WINDOWING_MODE, activityTypes);
         if (adapter != null && !adapter.getChangeNeedsSnapshot()) {
             return null;
         }
@@ -6168,7 +6176,8 @@
                             prev.getTask() == next.getTask() ? TRANSIT_OLD_ACTIVITY_OPEN
                                     : next.mLaunchTaskBehind ? TRANSIT_OLD_TASK_OPEN_BEHIND
                                     : TRANSIT_OLD_TASK_OPEN, /* alwaysKeepCurrent */false);
-                    dc.prepareAppTransition(TRANSIT_OPEN);
+                    dc.prepareAppTransition(TRANSIT_OPEN,
+                            next.mLaunchTaskBehind ? TRANSIT_FLAG_OPEN_BEHIND : 0);
                 }
             }
         } else {
@@ -6830,8 +6839,8 @@
         forAllActivities(ActivityRecord::removeLaunchTickRunnable);
     }
 
-    private void updateTransitLocked(@WindowManager.TransitionOldType int transit,
-            @WindowManager.TransitionType int transit2, ActivityOptions options,
+    private void updateTransitLocked(@WindowManager.TransitionOldType int transitOld,
+            @WindowManager.TransitionType int transit, ActivityOptions options,
             boolean forceOverride) {
         if (options != null) {
             ActivityRecord r = topRunningActivity();
@@ -6841,9 +6850,9 @@
                 ActivityOptions.abort(options);
             }
         }
-        mDisplayContent.prepareAppTransitionOld(transit, false,
+        mDisplayContent.prepareAppTransitionOld(transitOld, false,
                 0 /* flags */, forceOverride);
-        mDisplayContent.prepareAppTransition(transit2);
+        mDisplayContent.prepareAppTransition(transit);
     }
 
     final void moveTaskToFront(Task tr, boolean noAnimation, ActivityOptions options,
diff --git a/services/core/java/com/android/server/wm/WallpaperController.java b/services/core/java/com/android/server/wm/WallpaperController.java
index ce13867..7d61c19 100644
--- a/services/core/java/com/android/server/wm/WallpaperController.java
+++ b/services/core/java/com/android/server/wm/WallpaperController.java
@@ -148,7 +148,7 @@
                 ? w.mActivityRecord.getAnimatingContainer() : null;
         final boolean keyguardGoingAwayWithWallpaper = (animatingContainer != null
                 && animatingContainer.isAnimating(TRANSITION | PARENTS)
-                && AppTransition.isKeyguardGoingAwayTransit(animatingContainer.mTransit)
+                && AppTransition.isKeyguardGoingAwayTransitOld(animatingContainer.mTransit)
                 && (animatingContainer.mTransitFlags
                 & TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER) != 0);
 
diff --git a/services/core/java/com/android/server/wm/WindowContainer.java b/services/core/java/com/android/server/wm/WindowContainer.java
index 22dca26..1271420 100644
--- a/services/core/java/com/android/server/wm/WindowContainer.java
+++ b/services/core/java/com/android/server/wm/WindowContainer.java
@@ -2415,6 +2415,9 @@
         try {
             Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "WC#applyAnimation");
             if (okToAnimate()) {
+                ProtoLog.v(WM_DEBUG_APP_TRANSITIONS_ANIM,
+                        "applyAnimation: transit=%s, enter=%b, wc=%s",
+                        AppTransition.appTransitionToString(transit), enter, this);
                 applyAnimationUnchecked(lp, enter, transit, isVoiceInteraction, sources);
             } else {
                 cancelAnimation();
@@ -2449,7 +2452,7 @@
 
         final RemoteAnimationController controller =
                 getDisplayContent().mAppTransition.getRemoteAnimationController();
-        final boolean isChanging = AppTransition.isChangeTransit(transit) && enter
+        final boolean isChanging = AppTransition.isChangeTransitOld(transit) && enter
                 && isChangingAppTransition();
 
         // Delaying animation start isn't compatible with remote animations at all.
@@ -2497,7 +2500,7 @@
 
                 resultAdapters = new Pair<>(adapter, null);
                 mNeedsZBoost = a.getZAdjustment() == Animation.ZORDER_TOP
-                        || AppTransition.isClosingTransit(transit);
+                        || AppTransition.isClosingTransitOld(transit);
                 mTransit = transit;
                 mTransitFlags = getDisplayContent().mAppTransition.getTransitFlags();
             } else {
diff --git a/services/core/java/com/android/server/wm/WindowManagerInternal.java b/services/core/java/com/android/server/wm/WindowManagerInternal.java
index 8729801..d082778 100644
--- a/services/core/java/com/android/server/wm/WindowManagerInternal.java
+++ b/services/core/java/com/android/server/wm/WindowManagerInternal.java
@@ -115,9 +115,9 @@
         /**
          * Called when a pending app transition gets cancelled.
          *
-         * @param transit transition type indicating what kind of transition got cancelled
+         * @param keyguardGoingAway true if keyguard going away transition transition got cancelled.
          */
-        public void onAppTransitionCancelledLocked(int transit) {}
+        public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {}
 
         /**
          * Called when an app transition is timed out.
@@ -127,8 +127,7 @@
         /**
          * Called when an app transition gets started
          *
-         * @param transit transition type indicating what kind of transition gets run, must be one
-         *                of AppTransition.TRANSIT_* values
+         * @param keyguardGoingAway true if keyguard going away transition is started.
          * @param duration the total duration of the transition
          * @param statusBarAnimationStartTime the desired start time for all visual animations in
          *        the status bar caused by this app transition in uptime millis
@@ -140,7 +139,7 @@
          * {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_WALLPAPER},
          * or {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_ANIM}.
          */
-        public int onAppTransitionStartingLocked(int transit, long duration,
+        public int onAppTransitionStartingLocked(boolean keyguardGoingAway, long duration,
                 long statusBarAnimationStartTime, long statusBarAnimationDuration) {
             return 0;
         }
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 5b126b8..c6cc24c 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -1111,7 +1111,7 @@
             = new WindowManagerInternal.AppTransitionListener() {
 
         @Override
-        public void onAppTransitionCancelledLocked(int transit) {
+        public void onAppTransitionCancelledLocked(boolean keyguardGoingAway) {
         }
 
         @Override
diff --git a/services/tests/wmtests/src/com/android/server/wm/RecentsAnimationControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/RecentsAnimationControllerTest.java
index cc92ddd..40f73b1 100644
--- a/services/tests/wmtests/src/com/android/server/wm/RecentsAnimationControllerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/RecentsAnimationControllerTest.java
@@ -252,7 +252,7 @@
         assertNull(mController.mRecentScreenshotAnimator);
 
         // Simulate the app transition finishing
-        mController.mAppTransitionListener.onAppTransitionStartingLocked(0, 0, 0, 0);
+        mController.mAppTransitionListener.onAppTransitionStartingLocked(false, 0, 0, 0);
         verify(mAnimationCallbacks).onAnimationFinished(REORDER_KEEP_IN_PLACE, false);
     }
 
diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowContainerTests.java b/services/tests/wmtests/src/com/android/server/wm/WindowContainerTests.java
index e0c72fb..a284ba8 100644
--- a/services/tests/wmtests/src/com/android/server/wm/WindowContainerTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/WindowContainerTests.java
@@ -943,8 +943,7 @@
 
         // Make sure animation finish callback will be received and reset animating state after
         // animation finish.
-        wc.getDisplayContent().mAppTransition.goodToGo(TRANSIT_OLD_TASK_OPEN, act,
-                mDisplayContent.mOpeningApps);
+        wc.getDisplayContent().mAppTransition.goodToGo(TRANSIT_OLD_TASK_OPEN, act);
         verify(wc).onAnimationFinished(eq(ANIMATION_TYPE_APP_TRANSITION), any());
         assertFalse(wc.isAnimating());
         assertFalse(act.isAnimating(PARENTS));