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