Reset the layers of DA's on config change.

Adjust foreground animations to finish on top of the control bar.

Bug: 191175096
Test: Manual
Change-Id: I5863e827f94d03dd12dda67f0263e863bbeb87b2
Merged-In: I5863e827f94d03dd12dda67f0263e863bbeb87b2
diff --git a/src/com/android/car/carlauncher/displayarea/CarDisplayAreaController.java b/src/com/android/car/carlauncher/displayarea/CarDisplayAreaController.java
index dc409ec..4e41910 100644
--- a/src/com/android/car/carlauncher/displayarea/CarDisplayAreaController.java
+++ b/src/com/android/car/carlauncher/displayarea/CarDisplayAreaController.java
@@ -24,7 +24,6 @@
 import android.content.res.Resources;
 import android.graphics.Rect;
 import android.util.DisplayMetrics;
-import android.view.SurfaceControl;
 import android.window.DisplayAreaAppearedInfo;
 import android.window.WindowContainerToken;
 import android.window.WindowContainerTransaction;
@@ -189,13 +188,6 @@
         mForegroundApplicationsDisplay = foregroundDisplayAreaInfos.get(0);
         mBackgroundApplicationDisplay = backgroundDisplayAreaInfos.get(0);
         mControlBarDisplay = controlBarDisplayAreaInfos.get(0);
-        SurfaceControl.Transaction tx =
-                new SurfaceControl.Transaction();
-        // TODO(b/188102153): replace to set mForegroundApplicationsDisplay to top.
-        tx.setLayer(mBackgroundApplicationDisplay.getLeash(), BACKGROUND_LAYER_INDEX);
-        tx.setLayer(mForegroundApplicationsDisplay.getLeash(), FOREGROUND_LAYER_INDEX);
-        tx.setLayer(mControlBarDisplay.getLeash(), CONTROL_BAR_LAYER_INDEX);
-        tx.apply();
     }
 
     /** Un-Registers DA organizer. */
@@ -222,7 +214,8 @@
         switch (toState) {
             case CONTROL_BAR:
                 // Foreground DA closes.
-                fromPos = mScreenHeightWithoutNavBar - mDefaultDisplayHeight;
+                fromPos = mScreenHeightWithoutNavBar - mDefaultDisplayHeight
+                        - mControlBarDisplayHeight;
                 toPos = mScreenHeightWithoutNavBar;
                 mBackgroundApplicationDisplayBounds.bottom =
                         mScreenHeightWithoutNavBar - mControlBarDisplayHeight;
@@ -239,7 +232,8 @@
                 // update the bounds to expand the foreground display area before starting
                 // animations.
                 fromPos = mScreenHeightWithoutNavBar;
-                toPos = mScreenHeightWithoutNavBar - mDefaultDisplayHeight;
+                toPos = mScreenHeightWithoutNavBar - mDefaultDisplayHeight
+                        - mControlBarDisplayHeight;
                 mBackgroundApplicationDisplayBounds.bottom = toPos;
                 mOrganizer.scheduleOffset(fromPos, toPos, mBackgroundApplicationDisplayBounds,
                         mBackgroundApplicationDisplay, mForegroundApplicationsDisplay,
@@ -256,8 +250,9 @@
         Rect backgroundBounds = new Rect(0, 0, mTotalScreenWidth, controlBarTop);
         Rect controlBarBounds = new Rect(0, controlBarTop, mTotalScreenWidth,
                 mScreenHeightWithoutNavBar);
-        Rect foregroundBounds = new Rect(0, mScreenHeightWithoutNavBar - mDefaultDisplayHeight,
-                mTotalScreenWidth, mScreenHeightWithoutNavBar);
+        Rect foregroundBounds = new Rect(0,
+                mScreenHeightWithoutNavBar - mDefaultDisplayHeight - mControlBarDisplayHeight,
+                mTotalScreenWidth, mScreenHeightWithoutNavBar - mControlBarDisplayHeight);
 
         // Adjust the bounds based on the nav bar.
         // TODO: account for the case where nav bar is at the top.
diff --git a/src/com/android/car/carlauncher/displayarea/CarDisplayAreaOrganizer.java b/src/com/android/car/carlauncher/displayarea/CarDisplayAreaOrganizer.java
index 9c7191b..59a6572 100644
--- a/src/com/android/car/carlauncher/displayarea/CarDisplayAreaOrganizer.java
+++ b/src/com/android/car/carlauncher/displayarea/CarDisplayAreaOrganizer.java
@@ -18,6 +18,10 @@
 
 import static android.view.Display.DEFAULT_DISPLAY;
 
+import static com.android.car.carlauncher.displayarea.CarDisplayAreaController.BACKGROUND_LAYER_INDEX;
+import static com.android.car.carlauncher.displayarea.CarDisplayAreaController.CONTROL_BAR_LAYER_INDEX;
+import static com.android.car.carlauncher.displayarea.CarDisplayAreaController.FOREGROUND_LAYER_INDEX;
+
 import android.app.ActivityOptions;
 import android.content.Context;
 import android.content.Intent;
@@ -294,6 +298,19 @@
     }
 
     @Override
+    public void onDisplayAreaInfoChanged(DisplayAreaInfo displayAreaInfo) {
+        super.onDisplayAreaInfoChanged(displayAreaInfo);
+
+        SurfaceControl.Transaction tx =
+                new SurfaceControl.Transaction();
+        // TODO(b/188102153): replace to set mForegroundApplicationsDisplay to top.
+        tx.setLayer(mBackgroundApplicationDisplay.getLeash(), BACKGROUND_LAYER_INDEX);
+        tx.setLayer(mForegroundApplicationDisplay.getLeash(), FOREGROUND_LAYER_INDEX);
+        tx.setLayer(mControlBarDisplay.getLeash(), CONTROL_BAR_LAYER_INDEX);
+        tx.apply();
+    }
+
+    @Override
     public List<DisplayAreaAppearedInfo> registerOrganizer(int displayAreaFeature) {
         List<DisplayAreaAppearedInfo> displayAreaInfos =
                 super.registerOrganizer(displayAreaFeature);