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_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 density.
135     * @hide because eventually this should be able to change while
136     * running, so shouldn't be a constant.
137     * @deprecated There is no longer a static density; you can find the
138     * density for a display in {@link #densityDpi}.
139     */
140    @Deprecated
141    public static int DENSITY_DEVICE = getDeviceDensity();
142
143    /**
144     * The absolute width of the display in pixels.
145     */
146    public int widthPixels;
147    /**
148     * The absolute height of the display in pixels.
149     */
150    public int heightPixels;
151    /**
152     * The logical density of the display.  This is a scaling factor for the
153     * Density Independent Pixel unit, where one DIP is one pixel on an
154     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
155     * providing the baseline of the system's display. Thus on a 160dpi screen
156     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
157     *
158     * <p>This value does not exactly follow the real screen size (as given by
159     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
160     * the overall UI in steps based on gross changes in the display dpi.  For
161     * example, a 240x320 screen will have a density of 1 even if its width is
162     * 1.8", 1.3", etc. However, if the screen resolution is increased to
163     * 320x480 but the screen size remained 1.5"x2" then the density would be
164     * increased (probably to 1.5).
165     *
166     * @see #DENSITY_DEFAULT
167     */
168    public float density;
169    /**
170     * The screen density expressed as dots-per-inch.  May be either
171     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
172     */
173    public int densityDpi;
174    /**
175     * A scaling factor for fonts displayed on the display.  This is the same
176     * as {@link #density}, except that it may be adjusted in smaller
177     * increments at runtime based on a user preference for the font size.
178     */
179    public float scaledDensity;
180    /**
181     * The exact physical pixels per inch of the screen in the X dimension.
182     */
183    public float xdpi;
184    /**
185     * The exact physical pixels per inch of the screen in the Y dimension.
186     */
187    public float ydpi;
188
189    /**
190     * The reported display width prior to any compatibility mode scaling
191     * being applied.
192     * @hide
193     */
194    public int noncompatWidthPixels;
195    /**
196     * The reported display height prior to any compatibility mode scaling
197     * being applied.
198     * @hide
199     */
200    public int noncompatHeightPixels;
201    /**
202     * The reported display density prior to any compatibility mode scaling
203     * being applied.
204     * @hide
205     */
206    public float noncompatDensity;
207    /**
208     * The reported display density prior to any compatibility mode scaling
209     * being applied.
210     * @hide
211     */
212    public int noncompatDensityDpi;
213    /**
214     * The reported scaled density prior to any compatibility mode scaling
215     * being applied.
216     * @hide
217     */
218    public float noncompatScaledDensity;
219    /**
220     * The reported display xdpi prior to any compatibility mode scaling
221     * being applied.
222     * @hide
223     */
224    public float noncompatXdpi;
225    /**
226     * The reported display ydpi prior to any compatibility mode scaling
227     * being applied.
228     * @hide
229     */
230    public float noncompatYdpi;
231
232    public DisplayMetrics() {
233    }
234
235    public void setTo(DisplayMetrics o) {
236        widthPixels = o.widthPixels;
237        heightPixels = o.heightPixels;
238        density = o.density;
239        densityDpi = o.densityDpi;
240        scaledDensity = o.scaledDensity;
241        xdpi = o.xdpi;
242        ydpi = o.ydpi;
243        noncompatWidthPixels = o.noncompatWidthPixels;
244        noncompatHeightPixels = o.noncompatHeightPixels;
245        noncompatDensity = o.noncompatDensity;
246        noncompatDensityDpi = o.noncompatDensityDpi;
247        noncompatScaledDensity = o.noncompatScaledDensity;
248        noncompatXdpi = o.noncompatXdpi;
249        noncompatYdpi = o.noncompatYdpi;
250    }
251
252    public void setToDefaults() {
253        widthPixels = 0;
254        heightPixels = 0;
255        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
256        densityDpi =  DENSITY_DEVICE;
257        scaledDensity = density;
258        xdpi = DENSITY_DEVICE;
259        ydpi = DENSITY_DEVICE;
260        noncompatWidthPixels = widthPixels;
261        noncompatHeightPixels = heightPixels;
262        noncompatDensity = density;
263        noncompatDensityDpi = densityDpi;
264        noncompatScaledDensity = scaledDensity;
265        noncompatXdpi = xdpi;
266        noncompatYdpi = ydpi;
267    }
268
269    @Override
270    public boolean equals(Object o) {
271        return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
272    }
273
274    /**
275     * Returns true if these display metrics equal the other display metrics.
276     *
277     * @param other The display metrics with which to compare.
278     * @return True if the display metrics are equal.
279     */
280    public boolean equals(DisplayMetrics other) {
281        return equalsPhysical(other)
282                && scaledDensity == other.scaledDensity
283                && noncompatScaledDensity == other.noncompatScaledDensity;
284    }
285
286    /**
287     * Returns true if the physical aspects of the two display metrics
288     * are equal.  This ignores the scaled density, which is a logical
289     * attribute based on the current desired font size.
290     *
291     * @param other The display metrics with which to compare.
292     * @return True if the display metrics are equal.
293     * @hide
294     */
295    public boolean equalsPhysical(DisplayMetrics other) {
296        return other != null
297                && widthPixels == other.widthPixels
298                && heightPixels == other.heightPixels
299                && density == other.density
300                && densityDpi == other.densityDpi
301                && xdpi == other.xdpi
302                && ydpi == other.ydpi
303                && noncompatWidthPixels == other.noncompatWidthPixels
304                && noncompatHeightPixels == other.noncompatHeightPixels
305                && noncompatDensity == other.noncompatDensity
306                && noncompatDensityDpi == other.noncompatDensityDpi
307                && noncompatXdpi == other.noncompatXdpi
308                && noncompatYdpi == other.noncompatYdpi;
309    }
310
311    @Override
312    public int hashCode() {
313        return widthPixels * heightPixels * densityDpi;
314    }
315
316    @Override
317    public String toString() {
318        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
319            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
320            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
321    }
322
323    private static int getDeviceDensity() {
324        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
325        // when running in the emulator, allowing for dynamic configurations.
326        // The reason for this is that ro.sf.lcd_density is write-once and is
327        // set by the init process when it parses build.prop before anything else.
328        return SystemProperties.getInt("qemu.sf.lcd_density",
329                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
330    }
331}
332