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
17package android.util;
18
19import 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 */
29public 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_260 = 260;
70
71    /**
72     * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
73     * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
74     * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
75     */
76    public static final int DENSITY_280 = 280;
77
78    /**
79     * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
80     * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
81     * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
82     */
83    public static final int DENSITY_300 = 300;
84
85    /**
86     * Standard quantized DPI for extra-high-density screens.
87     */
88    public static final int DENSITY_XHIGH = 320;
89
90    /**
91     * Intermediate density for screens that sit somewhere between
92     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
93     * This is not a density that applications should target, instead relying
94     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
95     */
96    public static final int DENSITY_340 = 340;
97
98    /**
99     * Intermediate density for screens that sit somewhere between
100     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
101     * This is not a density that applications should target, instead relying
102     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
103     */
104    public static final int DENSITY_360 = 360;
105
106    /**
107     * Intermediate density for screens that sit somewhere between
108     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
109     * This is not a density that applications should target, instead relying
110     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
111     */
112    public static final int DENSITY_400 = 400;
113
114    /**
115     * Intermediate density for screens that sit somewhere between
116     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
117     * This is not a density that applications should target, instead relying
118     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
119     */
120    public static final int DENSITY_420 = 420;
121
122    /**
123     * Standard quantized DPI for extra-extra-high-density screens.
124     */
125    public static final int DENSITY_XXHIGH = 480;
126
127    /**
128     * Intermediate density for screens that sit somewhere between
129     * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
130     * This is not a density that applications should target, instead relying
131     * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
132     */
133    public static final int DENSITY_560 = 560;
134
135    /**
136     * Standard quantized DPI for extra-extra-extra-high-density screens.  Applications
137     * should not generally worry about this density; relying on XHIGH graphics
138     * being scaled up to it should be sufficient for almost all cases.  A typical
139     * use of this density would be 4K television screens -- 3840x2160, which
140     * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
141     */
142    public static final int DENSITY_XXXHIGH = 640;
143
144    /**
145     * The reference density used throughout the system.
146     */
147    public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
148
149    /**
150     * Scaling factor to convert a density in DPI units to the density scale.
151     * @hide
152     */
153    public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
154
155    /**
156     * The device's current density.
157     * <p>
158     * This value reflects any changes made to the device density. To obtain
159     * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
160     *
161     * @hide This value should not be used.
162     * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
163     *             device density or {@link #densityDpi} to obtain the current
164     *             density for a specific display.
165     */
166    @Deprecated
167    public static int DENSITY_DEVICE = getDeviceDensity();
168
169    /**
170     * The device's stable density.
171     * <p>
172     * This value is constant at run time and may not reflect the current
173     * display density. To obtain the current density for a specific display,
174     * use {@link #densityDpi}.
175     */
176    public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
177
178    /**
179     * The absolute width of the available display size in pixels.
180     */
181    public int widthPixels;
182    /**
183     * The absolute height of the available display size in pixels.
184     */
185    public int heightPixels;
186    /**
187     * The logical density of the display.  This is a scaling factor for the
188     * Density Independent Pixel unit, where one DIP is one pixel on an
189     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
190     * providing the baseline of the system's display. Thus on a 160dpi screen
191     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
192     *
193     * <p>This value does not exactly follow the real screen size (as given by
194     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
195     * the overall UI in steps based on gross changes in the display dpi.  For
196     * example, a 240x320 screen will have a density of 1 even if its width is
197     * 1.8", 1.3", etc. However, if the screen resolution is increased to
198     * 320x480 but the screen size remained 1.5"x2" then the density would be
199     * increased (probably to 1.5).
200     *
201     * @see #DENSITY_DEFAULT
202     */
203    public float density;
204    /**
205     * The screen density expressed as dots-per-inch.  May be either
206     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
207     */
208    public int densityDpi;
209    /**
210     * A scaling factor for fonts displayed on the display.  This is the same
211     * as {@link #density}, except that it may be adjusted in smaller
212     * increments at runtime based on a user preference for the font size.
213     */
214    public float scaledDensity;
215    /**
216     * The exact physical pixels per inch of the screen in the X dimension.
217     */
218    public float xdpi;
219    /**
220     * The exact physical pixels per inch of the screen in the Y dimension.
221     */
222    public float ydpi;
223
224    /**
225     * The reported display width prior to any compatibility mode scaling
226     * being applied.
227     * @hide
228     */
229    public int noncompatWidthPixels;
230    /**
231     * The reported display height prior to any compatibility mode scaling
232     * being applied.
233     * @hide
234     */
235    public int noncompatHeightPixels;
236    /**
237     * The reported display density prior to any compatibility mode scaling
238     * being applied.
239     * @hide
240     */
241    public float noncompatDensity;
242    /**
243     * The reported display density prior to any compatibility mode scaling
244     * being applied.
245     * @hide
246     */
247    public int noncompatDensityDpi;
248    /**
249     * The reported scaled density prior to any compatibility mode scaling
250     * being applied.
251     * @hide
252     */
253    public float noncompatScaledDensity;
254    /**
255     * The reported display xdpi prior to any compatibility mode scaling
256     * being applied.
257     * @hide
258     */
259    public float noncompatXdpi;
260    /**
261     * The reported display ydpi prior to any compatibility mode scaling
262     * being applied.
263     * @hide
264     */
265    public float noncompatYdpi;
266
267    public DisplayMetrics() {
268    }
269
270    public void setTo(DisplayMetrics o) {
271        if (this == o) {
272            return;
273        }
274
275        widthPixels = o.widthPixels;
276        heightPixels = o.heightPixels;
277        density = o.density;
278        densityDpi = o.densityDpi;
279        scaledDensity = o.scaledDensity;
280        xdpi = o.xdpi;
281        ydpi = o.ydpi;
282        noncompatWidthPixels = o.noncompatWidthPixels;
283        noncompatHeightPixels = o.noncompatHeightPixels;
284        noncompatDensity = o.noncompatDensity;
285        noncompatDensityDpi = o.noncompatDensityDpi;
286        noncompatScaledDensity = o.noncompatScaledDensity;
287        noncompatXdpi = o.noncompatXdpi;
288        noncompatYdpi = o.noncompatYdpi;
289    }
290
291    public void setToDefaults() {
292        widthPixels = 0;
293        heightPixels = 0;
294        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
295        densityDpi =  DENSITY_DEVICE;
296        scaledDensity = density;
297        xdpi = DENSITY_DEVICE;
298        ydpi = DENSITY_DEVICE;
299        noncompatWidthPixels = widthPixels;
300        noncompatHeightPixels = heightPixels;
301        noncompatDensity = density;
302        noncompatDensityDpi = densityDpi;
303        noncompatScaledDensity = scaledDensity;
304        noncompatXdpi = xdpi;
305        noncompatYdpi = ydpi;
306    }
307
308    @Override
309    public boolean equals(Object o) {
310        return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
311    }
312
313    /**
314     * Returns true if these display metrics equal the other display metrics.
315     *
316     * @param other The display metrics with which to compare.
317     * @return True if the display metrics are equal.
318     */
319    public boolean equals(DisplayMetrics other) {
320        return equalsPhysical(other)
321                && scaledDensity == other.scaledDensity
322                && noncompatScaledDensity == other.noncompatScaledDensity;
323    }
324
325    /**
326     * Returns true if the physical aspects of the two display metrics
327     * are equal.  This ignores the scaled density, which is a logical
328     * attribute based on the current desired font size.
329     *
330     * @param other The display metrics with which to compare.
331     * @return True if the display metrics are equal.
332     * @hide
333     */
334    public boolean equalsPhysical(DisplayMetrics other) {
335        return other != null
336                && widthPixels == other.widthPixels
337                && heightPixels == other.heightPixels
338                && density == other.density
339                && densityDpi == other.densityDpi
340                && xdpi == other.xdpi
341                && ydpi == other.ydpi
342                && noncompatWidthPixels == other.noncompatWidthPixels
343                && noncompatHeightPixels == other.noncompatHeightPixels
344                && noncompatDensity == other.noncompatDensity
345                && noncompatDensityDpi == other.noncompatDensityDpi
346                && noncompatXdpi == other.noncompatXdpi
347                && noncompatYdpi == other.noncompatYdpi;
348    }
349
350    @Override
351    public int hashCode() {
352        return widthPixels * heightPixels * densityDpi;
353    }
354
355    @Override
356    public String toString() {
357        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
358            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
359            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
360    }
361
362    private static int getDeviceDensity() {
363        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
364        // when running in the emulator, allowing for dynamic configurations.
365        // The reason for this is that ro.sf.lcd_density is write-once and is
366        // set by the init process when it parses build.prop before anything else.
367        return SystemProperties.getInt("qemu.sf.lcd_density",
368                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
369    }
370}
371