1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util;
18 
19 import android.os.SystemProperties;
20 
21 
22 /**
23  * A structure describing general information about a display, such as its
24  * size, density, and font scaling.
25  * <p>To access the DisplayMetrics members, initialize an object like this:</p>
26  * <pre> DisplayMetrics metrics = new DisplayMetrics();
27  * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
28  */
29 public class DisplayMetrics {
30     /**
31      * Standard quantized DPI for low-density screens.
32      */
33     public static final int DENSITY_LOW = 120;
34 
35     /**
36      * Standard quantized DPI for medium-density screens.
37      */
38     public static final int DENSITY_MEDIUM = 160;
39 
40     /**
41      * This is a secondary density, added for some common screen configurations.
42      * It is recommended that applications not generally target this as a first
43      * class density -- that is, don't supply specific graphics for this
44      * density, instead allow the platform to scale from other densities
45      * (typically {@link #DENSITY_HIGH}) as
46      * appropriate.  In most cases (such as using bitmaps in
47      * {@link android.graphics.drawable.Drawable}) the platform
48      * can perform this scaling at load time, so the only cost is some slight
49      * startup runtime overhead.
50      *
51      * <p>This density was original introduced to correspond with a
52      * 720p TV screen: the density for 1080p televisions is
53      * {@link #DENSITY_XHIGH}, and the value here provides the same UI
54      * size for a TV running at 720p.  It has also found use in 7" tablets,
55      * when these devices have 1280x720 displays.
56      */
57     public static final int DENSITY_TV = 213;
58 
59     /**
60      * Standard quantized DPI for high-density screens.
61      */
62     public static final int DENSITY_HIGH = 240;
63 
64     /**
65      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
66      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
67      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
68      */
69     public static final int DENSITY_280 = 280;
70 
71     /**
72      * Standard quantized DPI for extra-high-density screens.
73      */
74     public static final int DENSITY_XHIGH = 320;
75 
76     /**
77      * Intermediate density for screens that sit somewhere between
78      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
79      * This is not a density that applications should target, instead relying
80      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
81      */
82     public static final int DENSITY_360 = 360;
83 
84     /**
85      * Intermediate density for screens that sit somewhere between
86      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
87      * This is not a density that applications should target, instead relying
88      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
89      */
90     public static final int DENSITY_400 = 400;
91 
92     /**
93      * Intermediate density for screens that sit somewhere between
94      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
95      * This is not a density that applications should target, instead relying
96      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
97      */
98     public static final int DENSITY_420 = 420;
99 
100     /**
101      * Standard quantized DPI for extra-extra-high-density screens.
102      */
103     public static final int DENSITY_XXHIGH = 480;
104 
105     /**
106      * Intermediate density for screens that sit somewhere between
107      * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
108      * This is not a density that applications should target, instead relying
109      * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
110      */
111     public static final int DENSITY_560 = 560;
112 
113     /**
114      * Standard quantized DPI for extra-extra-extra-high-density screens.  Applications
115      * should not generally worry about this density; relying on XHIGH graphics
116      * being scaled up to it should be sufficient for almost all cases.  A typical
117      * use of this density would be 4K television screens -- 3840x2160, which
118      * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
119      */
120     public static final int DENSITY_XXXHIGH = 640;
121 
122     /**
123      * The reference density used throughout the system.
124      */
125     public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
126 
127     /**
128      * Scaling factor to convert a density in DPI units to the density scale.
129      * @hide
130      */
131     public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
132 
133     /**
134      * The device's current density.
135      * <p>
136      * This value reflects any changes made to the device density. To obtain
137      * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
138      *
139      * @hide This value should not be used.
140      * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
141      *             device density or {@link #densityDpi} to obtain the current
142      *             density for a specific display.
143      */
144     @Deprecated
145     public static int DENSITY_DEVICE = getDeviceDensity();
146 
147     /**
148      * The device's stable density.
149      * <p>
150      * This value is constant at run time and may not reflect the current
151      * display density. To obtain the current density for a specific display,
152      * use {@link #densityDpi}.
153      */
154     public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
155 
156     /**
157      * The absolute width of the available display size in pixels.
158      */
159     public int widthPixels;
160     /**
161      * The absolute height of the available display size in pixels.
162      */
163     public int heightPixels;
164     /**
165      * The logical density of the display.  This is a scaling factor for the
166      * Density Independent Pixel unit, where one DIP is one pixel on an
167      * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
168      * providing the baseline of the system's display. Thus on a 160dpi screen
169      * this density value will be 1; on a 120 dpi screen it would be .75; etc.
170      *
171      * <p>This value does not exactly follow the real screen size (as given by
172      * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
173      * the overall UI in steps based on gross changes in the display dpi.  For
174      * example, a 240x320 screen will have a density of 1 even if its width is
175      * 1.8", 1.3", etc. However, if the screen resolution is increased to
176      * 320x480 but the screen size remained 1.5"x2" then the density would be
177      * increased (probably to 1.5).
178      *
179      * @see #DENSITY_DEFAULT
180      */
181     public float density;
182     /**
183      * The screen density expressed as dots-per-inch.  May be either
184      * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
185      */
186     public int densityDpi;
187     /**
188      * A scaling factor for fonts displayed on the display.  This is the same
189      * as {@link #density}, except that it may be adjusted in smaller
190      * increments at runtime based on a user preference for the font size.
191      */
192     public float scaledDensity;
193     /**
194      * The exact physical pixels per inch of the screen in the X dimension.
195      */
196     public float xdpi;
197     /**
198      * The exact physical pixels per inch of the screen in the Y dimension.
199      */
200     public float ydpi;
201 
202     /**
203      * The reported display width prior to any compatibility mode scaling
204      * being applied.
205      * @hide
206      */
207     public int noncompatWidthPixels;
208     /**
209      * The reported display height prior to any compatibility mode scaling
210      * being applied.
211      * @hide
212      */
213     public int noncompatHeightPixels;
214     /**
215      * The reported display density prior to any compatibility mode scaling
216      * being applied.
217      * @hide
218      */
219     public float noncompatDensity;
220     /**
221      * The reported display density prior to any compatibility mode scaling
222      * being applied.
223      * @hide
224      */
225     public int noncompatDensityDpi;
226     /**
227      * The reported scaled density prior to any compatibility mode scaling
228      * being applied.
229      * @hide
230      */
231     public float noncompatScaledDensity;
232     /**
233      * The reported display xdpi prior to any compatibility mode scaling
234      * being applied.
235      * @hide
236      */
237     public float noncompatXdpi;
238     /**
239      * The reported display ydpi prior to any compatibility mode scaling
240      * being applied.
241      * @hide
242      */
243     public float noncompatYdpi;
244 
DisplayMetrics()245     public DisplayMetrics() {
246     }
247 
setTo(DisplayMetrics o)248     public void setTo(DisplayMetrics o) {
249         widthPixels = o.widthPixels;
250         heightPixels = o.heightPixels;
251         density = o.density;
252         densityDpi = o.densityDpi;
253         scaledDensity = o.scaledDensity;
254         xdpi = o.xdpi;
255         ydpi = o.ydpi;
256         noncompatWidthPixels = o.noncompatWidthPixels;
257         noncompatHeightPixels = o.noncompatHeightPixels;
258         noncompatDensity = o.noncompatDensity;
259         noncompatDensityDpi = o.noncompatDensityDpi;
260         noncompatScaledDensity = o.noncompatScaledDensity;
261         noncompatXdpi = o.noncompatXdpi;
262         noncompatYdpi = o.noncompatYdpi;
263     }
264 
setToDefaults()265     public void setToDefaults() {
266         widthPixels = 0;
267         heightPixels = 0;
268         density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
269         densityDpi =  DENSITY_DEVICE;
270         scaledDensity = density;
271         xdpi = DENSITY_DEVICE;
272         ydpi = DENSITY_DEVICE;
273         noncompatWidthPixels = widthPixels;
274         noncompatHeightPixels = heightPixels;
275         noncompatDensity = density;
276         noncompatDensityDpi = densityDpi;
277         noncompatScaledDensity = scaledDensity;
278         noncompatXdpi = xdpi;
279         noncompatYdpi = ydpi;
280     }
281 
282     @Override
equals(Object o)283     public boolean equals(Object o) {
284         return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
285     }
286 
287     /**
288      * Returns true if these display metrics equal the other display metrics.
289      *
290      * @param other The display metrics with which to compare.
291      * @return True if the display metrics are equal.
292      */
equals(DisplayMetrics other)293     public boolean equals(DisplayMetrics other) {
294         return equalsPhysical(other)
295                 && scaledDensity == other.scaledDensity
296                 && noncompatScaledDensity == other.noncompatScaledDensity;
297     }
298 
299     /**
300      * Returns true if the physical aspects of the two display metrics
301      * are equal.  This ignores the scaled density, which is a logical
302      * attribute based on the current desired font size.
303      *
304      * @param other The display metrics with which to compare.
305      * @return True if the display metrics are equal.
306      * @hide
307      */
equalsPhysical(DisplayMetrics other)308     public boolean equalsPhysical(DisplayMetrics other) {
309         return other != null
310                 && widthPixels == other.widthPixels
311                 && heightPixels == other.heightPixels
312                 && density == other.density
313                 && densityDpi == other.densityDpi
314                 && xdpi == other.xdpi
315                 && ydpi == other.ydpi
316                 && noncompatWidthPixels == other.noncompatWidthPixels
317                 && noncompatHeightPixels == other.noncompatHeightPixels
318                 && noncompatDensity == other.noncompatDensity
319                 && noncompatDensityDpi == other.noncompatDensityDpi
320                 && noncompatXdpi == other.noncompatXdpi
321                 && noncompatYdpi == other.noncompatYdpi;
322     }
323 
324     @Override
hashCode()325     public int hashCode() {
326         return widthPixels * heightPixels * densityDpi;
327     }
328 
329     @Override
toString()330     public String toString() {
331         return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
332             ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
333             ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
334     }
335 
getDeviceDensity()336     private static int getDeviceDensity() {
337         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
338         // when running in the emulator, allowing for dynamic configurations.
339         // The reason for this is that ro.sf.lcd_density is write-once and is
340         // set by the init process when it parses build.prop before anything else.
341         return SystemProperties.getInt("qemu.sf.lcd_density",
342                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
343     }
344 }
345