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     * Standard quantized DPI for extra-high-density screens.
66     */
67    public static final int DENSITY_XHIGH = 320;
68
69    /**
70     * Standard quantized DPI for extra-extra-high-density screens.  Applications
71     * should not generally worry about this density; relying on XHIGH graphics
72     * being scaled up to it should be sufficient for almost all cases.
73     */
74    public static final int DENSITY_XXHIGH = 480;
75
76    /**
77     * The reference density used throughout the system.
78     */
79    public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
80
81    /**
82     * Scaling factor to convert a density in DPI units to the density scale.
83     * @hide
84     */
85    public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
86
87    /**
88     * The device's density.
89     * @hide because eventually this should be able to change while
90     * running, so shouldn't be a constant.
91     * @deprecated There is no longer a static density; you can find the
92     * density for a display in {@link #densityDpi}.
93     */
94    @Deprecated
95    public static int DENSITY_DEVICE = getDeviceDensity();
96
97    /**
98     * The absolute width of the display in pixels.
99     */
100    public int widthPixels;
101    /**
102     * The absolute height of the display in pixels.
103     */
104    public int heightPixels;
105    /**
106     * The logical density of the display.  This is a scaling factor for the
107     * Density Independent Pixel unit, where one DIP is one pixel on an
108     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
109     * providing the baseline of the system's display. Thus on a 160dpi screen
110     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
111     *
112     * <p>This value does not exactly follow the real screen size (as given by
113     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
114     * the overall UI in steps based on gross changes in the display dpi.  For
115     * example, a 240x320 screen will have a density of 1 even if its width is
116     * 1.8", 1.3", etc. However, if the screen resolution is increased to
117     * 320x480 but the screen size remained 1.5"x2" then the density would be
118     * increased (probably to 1.5).
119     *
120     * @see #DENSITY_DEFAULT
121     */
122    public float density;
123    /**
124     * The screen density expressed as dots-per-inch.  May be either
125     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
126     */
127    public int densityDpi;
128    /**
129     * A scaling factor for fonts displayed on the display.  This is the same
130     * as {@link #density}, except that it may be adjusted in smaller
131     * increments at runtime based on a user preference for the font size.
132     */
133    public float scaledDensity;
134    /**
135     * The exact physical pixels per inch of the screen in the X dimension.
136     */
137    public float xdpi;
138    /**
139     * The exact physical pixels per inch of the screen in the Y dimension.
140     */
141    public float ydpi;
142
143    /**
144     * The reported display width prior to any compatibility mode scaling
145     * being applied.
146     * @hide
147     */
148    public int noncompatWidthPixels;
149    /**
150     * The reported display height prior to any compatibility mode scaling
151     * being applied.
152     * @hide
153     */
154    public int noncompatHeightPixels;
155    /**
156     * The reported display density prior to any compatibility mode scaling
157     * being applied.
158     * @hide
159     */
160    public float noncompatDensity;
161    /**
162     * The reported display density prior to any compatibility mode scaling
163     * being applied.
164     * @hide
165     */
166    public int noncompatDensityDpi;
167    /**
168     * The reported scaled density prior to any compatibility mode scaling
169     * being applied.
170     * @hide
171     */
172    public float noncompatScaledDensity;
173    /**
174     * The reported display xdpi prior to any compatibility mode scaling
175     * being applied.
176     * @hide
177     */
178    public float noncompatXdpi;
179    /**
180     * The reported display ydpi prior to any compatibility mode scaling
181     * being applied.
182     * @hide
183     */
184    public float noncompatYdpi;
185
186    public DisplayMetrics() {
187    }
188
189    public void setTo(DisplayMetrics o) {
190        widthPixels = o.widthPixels;
191        heightPixels = o.heightPixels;
192        density = o.density;
193        densityDpi = o.densityDpi;
194        scaledDensity = o.scaledDensity;
195        xdpi = o.xdpi;
196        ydpi = o.ydpi;
197        noncompatWidthPixels = o.noncompatWidthPixels;
198        noncompatHeightPixels = o.noncompatHeightPixels;
199        noncompatDensity = o.noncompatDensity;
200        noncompatDensityDpi = o.noncompatDensityDpi;
201        noncompatScaledDensity = o.noncompatScaledDensity;
202        noncompatXdpi = o.noncompatXdpi;
203        noncompatYdpi = o.noncompatYdpi;
204    }
205
206    public void setToDefaults() {
207        widthPixels = 0;
208        heightPixels = 0;
209        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
210        densityDpi =  DENSITY_DEVICE;
211        scaledDensity = density;
212        xdpi = DENSITY_DEVICE;
213        ydpi = DENSITY_DEVICE;
214        noncompatWidthPixels = widthPixels;
215        noncompatHeightPixels = heightPixels;
216        noncompatDensity = density;
217        noncompatDensityDpi = densityDpi;
218        noncompatScaledDensity = scaledDensity;
219        noncompatXdpi = xdpi;
220        noncompatYdpi = ydpi;
221    }
222
223    @Override
224    public boolean equals(Object o) {
225        return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
226    }
227
228    /**
229     * Returns true if these display metrics equal the other display metrics.
230     *
231     * @param other The display metrics with which to compare.
232     * @return True if the display metrics are equal.
233     */
234    public boolean equals(DisplayMetrics other) {
235        return equalsPhysical(other)
236                && scaledDensity == other.scaledDensity
237                && noncompatScaledDensity == other.noncompatScaledDensity;
238    }
239
240    /**
241     * Returns true if the physical aspects of the two display metrics
242     * are equal.  This ignores the scaled density, which is a logical
243     * attribute based on the current desired font size.
244     *
245     * @param other The display metrics with which to compare.
246     * @return True if the display metrics are equal.
247     * @hide
248     */
249    public boolean equalsPhysical(DisplayMetrics other) {
250        return other != null
251                && widthPixels == other.widthPixels
252                && heightPixels == other.heightPixels
253                && density == other.density
254                && densityDpi == other.densityDpi
255                && xdpi == other.xdpi
256                && ydpi == other.ydpi
257                && noncompatWidthPixels == other.noncompatWidthPixels
258                && noncompatHeightPixels == other.noncompatHeightPixels
259                && noncompatDensity == other.noncompatDensity
260                && noncompatDensityDpi == other.noncompatDensityDpi
261                && noncompatXdpi == other.noncompatXdpi
262                && noncompatYdpi == other.noncompatYdpi;
263    }
264
265    @Override
266    public int hashCode() {
267        return widthPixels * heightPixels * densityDpi;
268    }
269
270    @Override
271    public String toString() {
272        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
273            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
274            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
275    }
276
277    private static int getDeviceDensity() {
278        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
279        // when running in the emulator, allowing for dynamic configurations.
280        // The reason for this is that ro.sf.lcd_density is write-once and is
281        // set by the init process when it parses build.prop before anything else.
282        return SystemProperties.getInt("qemu.sf.lcd_density",
283                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
284    }
285}
286