Use window metrics to estimate visible display frame
When the view is not attached to a view tree, we used the display
getRealSize to get an estimated result. This will be affected by the
Configuration hidden fields. It will not always have the stable result
with changes in the configuration.
Use the window metrics instead to estimate the result when the View has
access to the WindowManager to make the result better matches the
description of the public API.
Test: CTS ViewTest
Bug: 321143205
Change-Id: I823acadb0a50272b74288cfcefea2843c07a26c5
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index cd6d79c..986ed0e 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -16716,10 +16716,15 @@
mAttachInfo.mViewRootImpl.getWindowVisibleDisplayFrame(outRect);
return;
}
- // The view is not attached to a display so we don't have a context.
- // Make a best guess about the display size.
- Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
- d.getRectSize(outRect);
+ // TODO (b/327559224): Refine the behavior to better reflect the window environment with API
+ // doc updates.
+ final WindowManager windowManager = mContext.getSystemService(WindowManager.class);
+ final WindowMetrics metrics = windowManager.getMaximumWindowMetrics();
+ final Insets insets = metrics.getWindowInsets().getInsets(
+ WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());
+ outRect.set(metrics.getBounds());
+ outRect.inset(insets);
+ outRect.offsetTo(0, 0);
}
/**