From 529318a713fc416b39fd67a4e6b6c8475ae16760 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Mon, 2 Mar 2020 19:00:37 +0800 Subject: [PATCH] Update documentation of Resources#getDisplayMetrics Test: build & run fixes: 150213575 Change-Id: I439a8f8473236e29cc6371cda51e46a3b242ec3e --- core/java/android/content/res/Resources.java | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index d6a9f6990abee..c399bc72e438b 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -40,6 +40,7 @@ import android.annotation.StringRes; import android.annotation.StyleRes; import android.annotation.StyleableRes; import android.annotation.XmlRes; +import android.app.Application; import android.compat.annotation.UnsupportedAppUsage; import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo.Config; @@ -58,9 +59,11 @@ import android.util.Log; import android.util.LongSparseArray; import android.util.Pools.SynchronizedPool; import android.util.TypedValue; +import android.view.Display; import android.view.DisplayAdjustments; import android.view.ViewDebug; import android.view.ViewHierarchyEncoder; +import android.view.WindowManager; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; @@ -101,6 +104,12 @@ import java.util.List; * (such as for different languages and screen sizes). This is an important aspect of developing * Android applications that are compatible on different types of devices.

* + *

After {@link Build.VERSION_CODES#R}, {@link Resources} must be obtained by + * {@link android.app.Activity} or {@link android.content.Context} created with + * {@link android.content.Context#createWindowContext(int, Bundle)}. + * {@link Application#getResources()} may report wrong values in multi-window or on secondary + * displays. + * *

For more information about using resources, see the documentation about Application Resources.

*/ @@ -2024,10 +2033,20 @@ public class Resources { } /** - * Return the current display metrics that are in effect for this resource - * object. The returned object should be treated as read-only. - * - * @return The resource's current display metrics. + * Return the current display metrics that are in effect for this resource + * object. The returned object should be treated as read-only. + * + *

Note that the reported value may be different than the window this application is + * interested in.

+ * + *

Best practices are to obtain metrics from {@link WindowManager#getCurrentWindowMetrics()} + * for window bounds, {@link Display#getRealMetrics(DisplayMetrics)} for display bounds and + * obtain density from {@link Configuration#densityDpi}. The value obtained from this API may be + * wrong if the {@link Resources} is from the context which is different than the window is + * attached such as {@link Application#getResources()}. + *

+ * + * @return The resource's current display metrics. */ public DisplayMetrics getDisplayMetrics() { return mResourcesImpl.getDisplayMetrics();