summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2020-04-06 07:01:35 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2020-04-06 07:01:35 +0000
commit001fddc078c16210bff02405f126b9fcb52d42ca (patch)
tree6c68f1990161ad10684fd143dcc1fa7ab5eae511 /core/java/android
parent072d9fe0069af5feb0ce8892a3d691c1878b25a4 (diff)
parent529318a713fc416b39fd67a4e6b6c8475ae16760 (diff)
Merge "Update documentation of Resources#getDisplayMetrics" into rvc-dev
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/content/res/Resources.java27
1 files changed, 23 insertions, 4 deletions
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java
index d6a9f6990abe..c399bc72e438 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.</p>
*
+ * <p>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.
+ *
* <p>For more information about using resources, see the documentation about <a
* href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.</p>
*/
@@ -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.
+ *
+ * <p>Note that the reported value may be different than the window this application is
+ * interested in.</p>
+ *
+ * <p>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()}.
+ * <p/>
+ *
+ * @return The resource's current display metrics.
*/
public DisplayMetrics getDisplayMetrics() {
return mResourcesImpl.getDisplayMetrics();