diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 15d18d145bb09..53e8b527e9285 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -11468,7 +11468,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, outLocalInsets.setEmpty(); return in; } - Pair result = listener.onContentApplyWindowInsets(in); + Pair result = listener.onContentApplyWindowInsets(this, in); outLocalInsets.set(result.first.toRect()); return result.second; } else { diff --git a/core/java/android/view/Window.java b/core/java/android/view/Window.java index 4b284dbf9ed46..000fd80692925 100644 --- a/core/java/android/view/Window.java +++ b/core/java/android/view/Window.java @@ -712,13 +712,14 @@ public abstract class Window { * {@link View#setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener)} through the view * hierarchy. * + * @param view The view for which to apply insets. Must not be directly modified. * @param insets The root level insets that are about to be dispatched * @return A pair, with the first element containing the insets to apply as margin to the * root-level content views, and the second element determining what should be * dispatched to the content view. */ @NonNull - Pair onContentApplyWindowInsets( + Pair onContentApplyWindowInsets(@NonNull View view, @NonNull WindowInsets insets); } diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java index 24222d3afb72b..138d0dd395370 100644 --- a/core/java/com/android/internal/policy/PhoneWindow.java +++ b/core/java/com/android/internal/policy/PhoneWindow.java @@ -135,6 +135,29 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { private final static String TAG = "PhoneWindow"; + /** + * @see Window#setDecorFitsSystemWindows + */ + private static final OnContentApplyWindowInsetsListener sDefaultContentInsetsApplier = + (view, insets) -> { + if ((view.getWindowSystemUiVisibility() & SYSTEM_UI_LAYOUT_FLAGS) != 0) { + return new Pair<>(Insets.NONE, insets); + } + + boolean includeIme = (view.getViewRootImpl().mWindowAttributes.softInputMode + & SOFT_INPUT_MASK_ADJUST) + == SOFT_INPUT_ADJUST_RESIZE; + Insets insetsToApply; + if (ViewRootImpl.sNewInsetsMode == 0) { + insetsToApply = insets.getSystemWindowInsets(); + } else { + insetsToApply = insets.getInsets(systemBars() | (includeIme ? ime() : 0)); + } + insets = insets.inset(insetsToApply); + return new Pair<>(insetsToApply, + insets.inset(insetsToApply).consumeSystemWindowInsets()); + }; + /* If true, shadows drawn around the window will be rendered by the system compositor. If * false, shadows will be drawn by the client by setting an elevation on the root view and * the contents will be inset by the shadow radius. */ @@ -320,8 +343,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { /** @see ViewRootImpl#mActivityConfigCallback */ private ActivityConfigCallback mActivityConfigCallback; - private OnContentApplyWindowInsetsListener mPendingOnContentApplyWindowInsetsListener - = createDefaultContentWindowInsetsListener(); + private OnContentApplyWindowInsetsListener mPendingOnContentApplyWindowInsetsListener = + sDefaultContentInsetsApplier; static class WindowManagerHolder { static final IWindowManager sWindowManager = IWindowManager.Stub.asInterface( @@ -2120,27 +2143,6 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { mPendingOnContentApplyWindowInsetsListener = null; } - private OnContentApplyWindowInsetsListener createDefaultContentWindowInsetsListener() { - return insets -> { - if ((getDecorView().getWindowSystemUiVisibility() & SYSTEM_UI_LAYOUT_FLAGS) != 0) { - return new Pair<>(Insets.NONE, insets); - } - - boolean includeIme = - (getViewRootImpl().mWindowAttributes.softInputMode & SOFT_INPUT_MASK_ADJUST) - == SOFT_INPUT_ADJUST_RESIZE; - Insets insetsToApply; - if (ViewRootImpl.sNewInsetsMode == 0) { - insetsToApply = insets.getSystemWindowInsets(); - } else { - insetsToApply = insets.getInsets(systemBars() | (includeIme ? ime() : 0)); - } - insets = insets.inset(insetsToApply); - return new Pair<>(insetsToApply, - insets.inset(insetsToApply).consumeSystemWindowInsets()); - }; - } - static private final String FOCUSED_ID_TAG = "android:focusedViewId"; static private final String VIEWS_TAG = "android:views"; static private final String PANELS_TAG = "android:Panels"; @@ -3907,7 +3909,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { public void setDecorFitsSystemWindows(boolean decorFitsSystemWindows) { ViewRootImpl impl = getViewRootImplOrNull(); OnContentApplyWindowInsetsListener listener = decorFitsSystemWindows - ? createDefaultContentWindowInsetsListener() + ? sDefaultContentInsetsApplier : null; if (impl != null) { impl.setOnContentApplyWindowInsetsListener(listener);