Merge "Restore force relayout logic for apps targeting < R" into rvc-dev
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index facf861..1226202 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -981,6 +981,13 @@
      */
     protected static boolean sBrokenWindowBackground;
 
+    /**
+     * Prior to R, we were always forcing a layout of the entire hierarchy when insets changed from
+     * the server. This is inefficient and not all apps use it. Instead, we want to rely on apps
+     * calling {@link #requestLayout} when they need to relayout based on an insets change.
+     */
+    static boolean sForceLayoutWhenInsetsChanged;
+
     /** @hide */
     @IntDef({NOT_FOCUSABLE, FOCUSABLE, FOCUSABLE_AUTO})
     @Retention(RetentionPolicy.SOURCE)
@@ -5375,6 +5382,9 @@
 
             GradientDrawable.sWrapNegativeAngleMeasurements =
                     targetSdkVersion >= Build.VERSION_CODES.Q;
+
+            sForceLayoutWhenInsetsChanged = targetSdkVersion < Build.VERSION_CODES.R;
+
             sCompatibilityDone = true;
         }
     }
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index b74c8f6..42f11c1 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -1584,6 +1584,11 @@
         mApplyInsetsRequested = true;
         requestLayout();
 
+        // See comment for View.sForceLayoutWhenInsetsChanged
+        if (View.sForceLayoutWhenInsetsChanged && mView != null) {
+            forceLayout(mView);
+        }
+
         // If this changes during traversal, no need to schedule another one as it will dispatch it
         // during the current traversal.
         if (!mIsInTraversal) {