Reland "Use non-cached value when calculation button forced visible state"

This reverts commit afe3acbbdec745f24b6ea31161ac4687d25c898d.

Previous landed patch caused nav bar added with a null insets frame
size, and there's a bug the insets size won't get updated at runtime if
it was null when added. Fixed the issue and reland the patch.

Necessary modification was made to make sure the givenContentInsets and
extended by cutout logic can be applied correctly.

Reason for revert: Reland with Fix of the flaky test

Test: SeamlessAppRotationTest
Test: Check setup wizard as stated in b/268210882
Test: DisplayPolicyTests
Test: DisplayPolicyInsetsTests
Bug: 268210882

Change-Id: I3511bd87852a696a2ec09fe71d1cde90b84a5b66
diff --git a/core/java/android/view/InsetsFrameProvider.java b/core/java/android/view/InsetsFrameProvider.java
index 867d2b4..a2e0326c 100644
--- a/core/java/android/view/InsetsFrameProvider.java
+++ b/core/java/android/view/InsetsFrameProvider.java
@@ -293,7 +293,7 @@
     public static void calculateInsetsFrame(Rect displayFrame, Rect containerBounds,
             Rect displayCutoutSafe, Rect inOutFrame, int source, Insets insetsSize,
             @WindowManager.LayoutParams.PrivateFlags int privateFlags,
-            Insets displayCutoutSafeInsetsSize) {
+            Insets displayCutoutSafeInsetsSize, Rect givenContentInsets) {
         boolean extendByCutout = false;
         if (source == InsetsFrameProvider.SOURCE_DISPLAY) {
             inOutFrame.set(displayFrame);
@@ -301,16 +301,20 @@
             inOutFrame.set(containerBounds);
         } else {
             extendByCutout = (privateFlags & PRIVATE_FLAG_LAYOUT_SIZE_EXTENDED_BY_CUTOUT) != 0;
-        }
-        if (insetsSize == null) {
-            return;
+            if (givenContentInsets != null) {
+                inOutFrame.inset(givenContentInsets);
+            }
         }
         if (displayCutoutSafeInsetsSize != null) {
             sTmpRect2.set(inOutFrame);
         }
-        calculateInsetsFrame(inOutFrame, insetsSize);
+        if (insetsSize != null) {
+            calculateInsetsFrame(inOutFrame, insetsSize);
+        }
 
-        if (extendByCutout) {
+        if (extendByCutout && insetsSize != null) {
+            // Only extend if the insets size is not null. Otherwise, the frame has already been
+            // extended by the display cutout during layout process.
             WindowLayout.extendFrameByCutout(displayCutoutSafe, displayFrame, inOutFrame, sTmpRect);
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java b/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
index 8a5bac8..44c718f 100644
--- a/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
+++ b/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java
@@ -159,6 +159,8 @@
 import com.android.wm.shell.back.BackAnimation;
 import com.android.wm.shell.pip.Pip;
 
+import dagger.Lazy;
+
 import java.io.PrintWriter;
 import java.util.Locale;
 import java.util.Map;
@@ -167,8 +169,6 @@
 
 import javax.inject.Inject;
 
-import dagger.Lazy;
-
 /**
  * Contains logic for a navigation bar view.
  */
@@ -245,12 +245,6 @@
 
     private boolean mTransientShown;
     private boolean mTransientShownFromGestureOnSystemBar;
-    /**
-     * This is to indicate whether the navigation bar button is forced visible. This is true
-     * when the setup wizard is on display. When that happens, the window frame should be provided
-     * as insets size directly.
-     */
-    private boolean mIsButtonForceVisible;
     private int mNavBarMode = NAV_BAR_MODE_3BUTTON;
     private LightBarController mLightBarController;
     private final LightBarController mMainLightBarController;
@@ -679,8 +673,7 @@
         mView.setTouchHandler(mTouchHandler);
         setNavBarMode(mNavBarMode);
         mEdgeBackGestureHandler.setStateChangeCallback(mView::updateStates);
-        mEdgeBackGestureHandler.setButtonForceVisibleChangeCallback((forceVisible) -> {
-            mIsButtonForceVisible = forceVisible;
+        mEdgeBackGestureHandler.setButtonForcedVisibleChangeCallback((forceVisible) -> {
             repositionNavigationBar(mCurrentRotation);
         });
         mNavigationBarTransitions.addListener(this::onBarTransition);
@@ -1721,7 +1714,7 @@
                         .setInsetsSizeOverrides(new InsetsFrameProvider.InsetsSizeOverride[] {
                                 new InsetsFrameProvider.InsetsSizeOverride(
                                         TYPE_INPUT_METHOD, null)});
-        if (insetsHeight != -1 && !mIsButtonForceVisible) {
+        if (insetsHeight != -1 && !mEdgeBackGestureHandler.isButtonForcedVisible()) {
             navBarProvider.setInsetsSize(Insets.of(0, 0, 0, insetsHeight));
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/EdgeBackGestureHandler.java b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/EdgeBackGestureHandler.java
index 84d23c6..498d5c0 100644
--- a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/EdgeBackGestureHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/EdgeBackGestureHandler.java
@@ -177,7 +177,7 @@
     private final OverviewProxyService mOverviewProxyService;
     private final SysUiState mSysUiState;
     private Runnable mStateChangeCallback;
-    private Consumer<Boolean> mButtonForceVisibleCallback;
+    private Consumer<Boolean> mButtonForcedVisibleCallback;
 
     private final PluginManager mPluginManager;
     private final ProtoTracer mProtoTracer;
@@ -245,7 +245,7 @@
     private boolean mGestureBlockingActivityRunning;
     private boolean mIsNewBackAffordanceEnabled;
     private boolean mIsTrackpadGestureFeaturesEnabled;
-    private boolean mIsButtonForceVisible;
+    private boolean mIsButtonForcedVisible;
 
     private InputMonitor mInputMonitor;
     private InputChannelCompat.InputEventReceiver mInputEventReceiver;
@@ -412,8 +412,8 @@
         mStateChangeCallback = callback;
     }
 
-    public void setButtonForceVisibleChangeCallback(Consumer<Boolean> callback) {
-        mButtonForceVisibleCallback = callback;
+    public void setButtonForcedVisibleChangeCallback(Consumer<Boolean> callback) {
+        mButtonForcedVisibleCallback = callback;
     }
 
     public int getEdgeWidthLeft() {
@@ -428,13 +428,14 @@
         Resources res = mNavigationModeController.getCurrentUserContext().getResources();
         mEdgeWidthLeft = mGestureNavigationSettingsObserver.getLeftSensitivity(res);
         mEdgeWidthRight = mGestureNavigationSettingsObserver.getRightSensitivity(res);
-        final boolean previousForceVisible = mIsButtonForceVisible;
-        mIsButtonForceVisible =
+        final boolean previousForcedVisible = mIsButtonForcedVisible;
+        mIsButtonForcedVisible =
                 mGestureNavigationSettingsObserver.areNavigationButtonForcedVisible();
-        if (previousForceVisible != mIsButtonForceVisible && mButtonForceVisibleCallback != null) {
-            mButtonForceVisibleCallback.accept(mIsButtonForceVisible);
+        if (previousForcedVisible != mIsButtonForcedVisible
+                && mButtonForcedVisibleCallback != null) {
+            mButtonForcedVisibleCallback.accept(mIsButtonForcedVisible);
         }
-        mIsBackGestureAllowed = !mIsButtonForceVisible;
+        mIsBackGestureAllowed = !mIsButtonForcedVisible;
 
         final DisplayMetrics dm = res.getDisplayMetrics();
         final float defaultGestureHeight = res.getDimension(
@@ -635,6 +636,10 @@
         return mIsEnabled && mIsBackGestureAllowed;
     }
 
+    public boolean isButtonForcedVisible() {
+        return mIsButtonForcedVisible;
+    }
+
     /**
      * Update the PiP bounds, used for exclusion calculation.
      */
diff --git a/services/core/java/com/android/server/wm/DisplayPolicy.java b/services/core/java/com/android/server/wm/DisplayPolicy.java
index 389c908..563240d 100644
--- a/services/core/java/com/android/server/wm/DisplayPolicy.java
+++ b/services/core/java/com/android/server/wm/DisplayPolicy.java
@@ -19,7 +19,6 @@
 import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
 import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
 import static android.view.Display.TYPE_INTERNAL;
-import static android.view.InsetsFrameProvider.SOURCE_FRAME;
 import static android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
 import static android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
 import static android.view.WindowInsetsController.APPEARANCE_LOW_PROFILE_BARS;
@@ -1069,7 +1068,7 @@
                 // runtime as ensured in WMS. Make use of the index in the provider directly
                 // to access the latest provided size at runtime.
                 final TriConsumer<DisplayFrames, WindowContainer, Rect> frameProvider =
-                        getFrameProvider(win, provider, i);
+                        getFrameProvider(win, i);
                 final InsetsFrameProvider.InsetsSizeOverride[] overrides =
                         provider.getInsetsSizeOverrides();
                 final SparseArray<TriConsumer<DisplayFrames, WindowContainer, Rect>>
@@ -1095,19 +1094,15 @@
         }
     }
 
-    @Nullable
     private TriConsumer<DisplayFrames, WindowContainer, Rect> getFrameProvider(WindowState win,
-            InsetsFrameProvider provider, int index) {
-        if (provider.getInsetsSize() == null && provider.getSource() == SOURCE_FRAME) {
-            return null;
-        }
+            int index) {
         return (displayFrames, windowContainer, inOutFrame) -> {
             final LayoutParams lp = win.mAttrs.forRotation(displayFrames.mRotation);
             final InsetsFrameProvider ifp = lp.providedInsets[index];
             InsetsFrameProvider.calculateInsetsFrame(displayFrames.mUnrestricted,
                     windowContainer.getBounds(), displayFrames.mDisplayCutoutSafe, inOutFrame,
                     ifp.getSource(), ifp.getInsetsSize(), lp.privateFlags,
-                    ifp.getMinimalInsetsSizeInDisplayCutoutSafe());
+                    ifp.getMinimalInsetsSizeInDisplayCutoutSafe(), win.mGivenContentInsets);
         };
     }
 
@@ -1120,7 +1115,8 @@
             InsetsFrameProvider.calculateInsetsFrame(displayFrames.mUnrestricted,
                     windowContainer.getBounds(), displayFrames.mDisplayCutoutSafe, inOutFrame,
                     ifp.getSource(), ifp.getInsetsSizeOverrides()[overrideIndex].getInsetsSize(),
-                    lp.privateFlags, null /* displayCutoutSafeInsetsSize */);
+                    lp.privateFlags, null /* displayCutoutSafeInsetsSize */,
+                    null /* givenContentInsets */);
         };
     }