The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 20 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
| 22 | |
| 23 | /** |
| 24 | * A structure describing general information about a display, such as its |
| 25 | * size, density, and font scaling. |
Joshua Baxter | 6b5e85d | 2019-07-18 20:45:53 +0000 | [diff] [blame] | 26 | * <p>To access the DisplayMetrics members, retrieve display metrics like this:</p> |
| 27 | * <pre>context.getResources().getDisplayMetrics();</pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class DisplayMetrics { |
| 30 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 31 | * Standard quantized DPI for low-density screens. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | */ |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 33 | public static final int DENSITY_LOW = 120; |
| 34 | |
| 35 | /** |
Harry Slatyer | e4eb7b4 | 2019-04-09 08:58:02 +1000 | [diff] [blame] | 36 | * Intermediate density for screens that sit between {@link #DENSITY_LOW} (120dpi) and |
| 37 | * {@link #DENSITY_MEDIUM} (160dpi). This is not a density that applications should target, |
| 38 | * instead relying on the system to scale their {@link #DENSITY_MEDIUM} assets for them. |
| 39 | */ |
| 40 | public static final int DENSITY_140 = 140; |
| 41 | |
| 42 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 43 | * Standard quantized DPI for medium-density screens. |
| 44 | */ |
| 45 | public static final int DENSITY_MEDIUM = 160; |
| 46 | |
| 47 | /** |
Harry Slatyer | e4eb7b4 | 2019-04-09 08:58:02 +1000 | [diff] [blame] | 48 | * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and |
| 49 | * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target, |
| 50 | * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them. |
| 51 | */ |
| 52 | public static final int DENSITY_180 = 180; |
| 53 | |
| 54 | /** |
| 55 | * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and |
| 56 | * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target, |
| 57 | * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them. |
| 58 | */ |
| 59 | public static final int DENSITY_200 = 200; |
| 60 | |
| 61 | /** |
Dianne Hackborn | 493861d | 2012-06-18 19:29:45 -0700 | [diff] [blame] | 62 | * This is a secondary density, added for some common screen configurations. |
| 63 | * It is recommended that applications not generally target this as a first |
| 64 | * class density -- that is, don't supply specific graphics for this |
| 65 | * density, instead allow the platform to scale from other densities |
| 66 | * (typically {@link #DENSITY_HIGH}) as |
| 67 | * appropriate. In most cases (such as using bitmaps in |
| 68 | * {@link android.graphics.drawable.Drawable}) the platform |
| 69 | * can perform this scaling at load time, so the only cost is some slight |
| 70 | * startup runtime overhead. |
| 71 | * |
| 72 | * <p>This density was original introduced to correspond with a |
| 73 | * 720p TV screen: the density for 1080p televisions is |
| 74 | * {@link #DENSITY_XHIGH}, and the value here provides the same UI |
| 75 | * size for a TV running at 720p. It has also found use in 7" tablets, |
| 76 | * when these devices have 1280x720 displays. |
Dianne Hackborn | b96cbbd | 2011-05-27 13:40:26 -0700 | [diff] [blame] | 77 | */ |
| 78 | public static final int DENSITY_TV = 213; |
| 79 | |
| 80 | /** |
Harry Slatyer | e4eb7b4 | 2019-04-09 08:58:02 +1000 | [diff] [blame] | 81 | * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and |
| 82 | * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target, |
| 83 | * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them. |
| 84 | */ |
| 85 | public static final int DENSITY_220 = 220; |
| 86 | |
| 87 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 88 | * Standard quantized DPI for high-density screens. |
| 89 | */ |
| 90 | public static final int DENSITY_HIGH = 240; |
| 91 | |
| 92 | /** |
Adam Powell | f6d4045 | 2015-02-11 13:34:43 -0800 | [diff] [blame] | 93 | * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and |
| 94 | * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target, |
| 95 | * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them. |
| 96 | */ |
Erik Wolsheimer | 178bba4 | 2016-06-13 12:51:36 -0700 | [diff] [blame] | 97 | public static final int DENSITY_260 = 260; |
| 98 | |
| 99 | /** |
| 100 | * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and |
| 101 | * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target, |
| 102 | * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them. |
| 103 | */ |
Adam Powell | f6d4045 | 2015-02-11 13:34:43 -0800 | [diff] [blame] | 104 | public static final int DENSITY_280 = 280; |
| 105 | |
| 106 | /** |
Erik Wolsheimer | 178bba4 | 2016-06-13 12:51:36 -0700 | [diff] [blame] | 107 | * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and |
| 108 | * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target, |
| 109 | * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them. |
| 110 | */ |
| 111 | public static final int DENSITY_300 = 300; |
| 112 | |
| 113 | /** |
Dianne Hackborn | a0b46c9 | 2010-10-21 15:32:06 -0700 | [diff] [blame] | 114 | * Standard quantized DPI for extra-high-density screens. |
| 115 | */ |
| 116 | public static final int DENSITY_XHIGH = 320; |
| 117 | |
| 118 | /** |
Dianne Hackborn | 09acb7c | 2013-10-17 17:28:09 -0700 | [diff] [blame] | 119 | * Intermediate density for screens that sit somewhere between |
Dianne Hackborn | a3fb40d | 2014-08-12 15:06:50 -0700 | [diff] [blame] | 120 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
Dianne Hackborn | 09acb7c | 2013-10-17 17:28:09 -0700 | [diff] [blame] | 121 | * This is not a density that applications should target, instead relying |
| 122 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 123 | */ |
Erik Wolsheimer | 178bba4 | 2016-06-13 12:51:36 -0700 | [diff] [blame] | 124 | public static final int DENSITY_340 = 340; |
| 125 | |
| 126 | /** |
| 127 | * Intermediate density for screens that sit somewhere between |
| 128 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
| 129 | * This is not a density that applications should target, instead relying |
| 130 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 131 | */ |
Adam Powell | f6d9cd19 | 2015-06-03 15:50:48 -0700 | [diff] [blame] | 132 | public static final int DENSITY_360 = 360; |
| 133 | |
| 134 | /** |
| 135 | * Intermediate density for screens that sit somewhere between |
| 136 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
| 137 | * This is not a density that applications should target, instead relying |
| 138 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 139 | */ |
Dianne Hackborn | 09acb7c | 2013-10-17 17:28:09 -0700 | [diff] [blame] | 140 | public static final int DENSITY_400 = 400; |
| 141 | |
| 142 | /** |
Dianne Hackborn | 1e9c51a | 2015-08-11 17:18:53 -0700 | [diff] [blame] | 143 | * Intermediate density for screens that sit somewhere between |
| 144 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
| 145 | * This is not a density that applications should target, instead relying |
| 146 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 147 | */ |
| 148 | public static final int DENSITY_420 = 420; |
| 149 | |
| 150 | /** |
weijuncheng | 5185d71 | 2018-01-26 13:05:40 +0800 | [diff] [blame] | 151 | * Intermediate density for screens that sit somewhere between |
| 152 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
| 153 | * This is not a density that applications should target, instead relying |
| 154 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 155 | */ |
| 156 | public static final int DENSITY_440 = 440; |
| 157 | |
| 158 | /** |
bj1211jeon | b38f168 | 2019-06-11 14:52:28 +0900 | [diff] [blame] | 159 | * Intermediate density for screens that sit somewhere between |
| 160 | * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi). |
| 161 | * This is not a density that applications should target, instead relying |
| 162 | * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. |
| 163 | */ |
| 164 | public static final int DENSITY_450 = 450; |
| 165 | |
| 166 | /** |
Jeff Davidson | 61a5a3f | 2014-07-16 12:56:26 -0700 | [diff] [blame] | 167 | * Standard quantized DPI for extra-extra-high-density screens. |
Dianne Hackborn | d96e3df | 2012-01-25 15:12:23 -0800 | [diff] [blame] | 168 | */ |
| 169 | public static final int DENSITY_XXHIGH = 480; |
| 170 | |
| 171 | /** |
Dianne Hackborn | a3fb40d | 2014-08-12 15:06:50 -0700 | [diff] [blame] | 172 | * Intermediate density for screens that sit somewhere between |
Philip Nowell | d561913 | 2014-09-25 15:40:56 -0700 | [diff] [blame] | 173 | * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi). |
Dianne Hackborn | a3fb40d | 2014-08-12 15:06:50 -0700 | [diff] [blame] | 174 | * This is not a density that applications should target, instead relying |
| 175 | * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them. |
| 176 | */ |
| 177 | public static final int DENSITY_560 = 560; |
| 178 | |
| 179 | /** |
st.ji | d41b9ac | 2018-07-18 18:15:24 +0900 | [diff] [blame] | 180 | * Intermediate density for screens that sit somewhere between |
| 181 | * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi). |
| 182 | * This is not a density that applications should target, instead relying |
| 183 | * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them. |
| 184 | */ |
| 185 | public static final int DENSITY_600 = 600; |
| 186 | |
| 187 | /** |
Dianne Hackborn | 56a2301 | 2013-02-12 15:41:49 -0800 | [diff] [blame] | 188 | * Standard quantized DPI for extra-extra-extra-high-density screens. Applications |
| 189 | * should not generally worry about this density; relying on XHIGH graphics |
| 190 | * being scaled up to it should be sufficient for almost all cases. A typical |
| 191 | * use of this density would be 4K television screens -- 3840x2160, which |
| 192 | * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH. |
| 193 | */ |
| 194 | public static final int DENSITY_XXXHIGH = 640; |
| 195 | |
| 196 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 197 | * The reference density used throughout the system. |
| 198 | */ |
| 199 | public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 201 | /** |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 202 | * Scaling factor to convert a density in DPI units to the density scale. |
| 203 | * @hide |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 204 | */ |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 205 | public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT; |
| 206 | |
| 207 | /** |
Alan Viverette | d7bbc8a | 2016-01-29 15:40:38 -0500 | [diff] [blame] | 208 | * The device's current density. |
| 209 | * <p> |
| 210 | * This value reflects any changes made to the device density. To obtain |
| 211 | * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}. |
| 212 | * |
| 213 | * @hide This value should not be used. |
| 214 | * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable |
| 215 | * device density or {@link #densityDpi} to obtain the current |
| 216 | * density for a specific display. |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 217 | */ |
| 218 | @Deprecated |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 219 | @UnsupportedAppUsage |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 220 | public static int DENSITY_DEVICE = getDeviceDensity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
| 222 | /** |
Alan Viverette | d7bbc8a | 2016-01-29 15:40:38 -0500 | [diff] [blame] | 223 | * The device's stable density. |
| 224 | * <p> |
| 225 | * This value is constant at run time and may not reflect the current |
| 226 | * display density. To obtain the current density for a specific display, |
| 227 | * use {@link #densityDpi}. |
| 228 | */ |
| 229 | public static final int DENSITY_DEVICE_STABLE = getDeviceDensity(); |
| 230 | |
| 231 | /** |
Andrii Kulian | 2beb828 | 2016-03-10 22:08:14 -0800 | [diff] [blame] | 232 | * The absolute width of the available display size in pixels. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | */ |
| 234 | public int widthPixels; |
| 235 | /** |
Andrii Kulian | 2beb828 | 2016-03-10 22:08:14 -0800 | [diff] [blame] | 236 | * The absolute height of the available display size in pixels. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | */ |
| 238 | public int heightPixels; |
| 239 | /** |
| 240 | * The logical density of the display. This is a scaling factor for the |
| 241 | * Density Independent Pixel unit, where one DIP is one pixel on an |
| 242 | * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), |
| 243 | * providing the baseline of the system's display. Thus on a 160dpi screen |
Dirk Dougherty | 6b13bc0 | 2009-10-30 19:05:53 -0700 | [diff] [blame] | 244 | * this density value will be 1; on a 120 dpi screen it would be .75; etc. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | * |
| 246 | * <p>This value does not exactly follow the real screen size (as given by |
Joshua Baxter | 9c69b40 | 2019-07-18 21:16:19 +0000 | [diff] [blame] | 247 | * {@link #xdpi} and {@link #ydpi}), but rather is used to scale the size of |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * the overall UI in steps based on gross changes in the display dpi. For |
| 249 | * example, a 240x320 screen will have a density of 1 even if its width is |
| 250 | * 1.8", 1.3", etc. However, if the screen resolution is increased to |
| 251 | * 320x480 but the screen size remained 1.5"x2" then the density would be |
| 252 | * increased (probably to 1.5). |
| 253 | * |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 254 | * @see #DENSITY_DEFAULT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
| 256 | public float density; |
| 257 | /** |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 258 | * The screen density expressed as dots-per-inch. May be either |
| 259 | * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. |
| 260 | */ |
| 261 | public int densityDpi; |
| 262 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * A scaling factor for fonts displayed on the display. This is the same |
| 264 | * as {@link #density}, except that it may be adjusted in smaller |
| 265 | * increments at runtime based on a user preference for the font size. |
| 266 | */ |
| 267 | public float scaledDensity; |
| 268 | /** |
| 269 | * The exact physical pixels per inch of the screen in the X dimension. |
| 270 | */ |
| 271 | public float xdpi; |
| 272 | /** |
| 273 | * The exact physical pixels per inch of the screen in the Y dimension. |
| 274 | */ |
| 275 | public float ydpi; |
| 276 | |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 277 | /** |
| 278 | * The reported display width prior to any compatibility mode scaling |
| 279 | * being applied. |
| 280 | * @hide |
| 281 | */ |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 282 | @UnsupportedAppUsage |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 283 | public int noncompatWidthPixels; |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 284 | /** |
| 285 | * The reported display height prior to any compatibility mode scaling |
| 286 | * being applied. |
| 287 | * @hide |
| 288 | */ |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 289 | @UnsupportedAppUsage |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 290 | public int noncompatHeightPixels; |
| 291 | /** |
| 292 | * The reported display density prior to any compatibility mode scaling |
| 293 | * being applied. |
| 294 | * @hide |
| 295 | */ |
| 296 | public float noncompatDensity; |
| 297 | /** |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 298 | * The reported display density prior to any compatibility mode scaling |
| 299 | * being applied. |
| 300 | * @hide |
| 301 | */ |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 302 | @UnsupportedAppUsage |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 303 | public int noncompatDensityDpi; |
| 304 | /** |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 305 | * The reported scaled density prior to any compatibility mode scaling |
| 306 | * being applied. |
| 307 | * @hide |
| 308 | */ |
| 309 | public float noncompatScaledDensity; |
| 310 | /** |
| 311 | * The reported display xdpi prior to any compatibility mode scaling |
| 312 | * being applied. |
| 313 | * @hide |
| 314 | */ |
| 315 | public float noncompatXdpi; |
| 316 | /** |
| 317 | * The reported display ydpi prior to any compatibility mode scaling |
| 318 | * being applied. |
| 319 | * @hide |
| 320 | */ |
| 321 | public float noncompatYdpi; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | public DisplayMetrics() { |
| 324 | } |
| 325 | |
| 326 | public void setTo(DisplayMetrics o) { |
Adam Lesinski | 4309721 | 2016-10-14 11:10:29 -0700 | [diff] [blame] | 327 | if (this == o) { |
| 328 | return; |
| 329 | } |
| 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | widthPixels = o.widthPixels; |
| 332 | heightPixels = o.heightPixels; |
| 333 | density = o.density; |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 334 | densityDpi = o.densityDpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | scaledDensity = o.scaledDensity; |
| 336 | xdpi = o.xdpi; |
| 337 | ydpi = o.ydpi; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 338 | noncompatWidthPixels = o.noncompatWidthPixels; |
| 339 | noncompatHeightPixels = o.noncompatHeightPixels; |
| 340 | noncompatDensity = o.noncompatDensity; |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 341 | noncompatDensityDpi = o.noncompatDensityDpi; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 342 | noncompatScaledDensity = o.noncompatScaledDensity; |
| 343 | noncompatXdpi = o.noncompatXdpi; |
| 344 | noncompatYdpi = o.noncompatYdpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | public void setToDefaults() { |
| 348 | widthPixels = 0; |
| 349 | heightPixels = 0; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 350 | density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; |
| 351 | densityDpi = DENSITY_DEVICE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | scaledDensity = density; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 353 | xdpi = DENSITY_DEVICE; |
| 354 | ydpi = DENSITY_DEVICE; |
| 355 | noncompatWidthPixels = widthPixels; |
| 356 | noncompatHeightPixels = heightPixels; |
| 357 | noncompatDensity = density; |
| 358 | noncompatDensityDpi = densityDpi; |
| 359 | noncompatScaledDensity = scaledDensity; |
| 360 | noncompatXdpi = xdpi; |
| 361 | noncompatYdpi = ydpi; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public boolean equals(Object o) { |
| 366 | return o instanceof DisplayMetrics && equals((DisplayMetrics)o); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Returns true if these display metrics equal the other display metrics. |
| 371 | * |
| 372 | * @param other The display metrics with which to compare. |
| 373 | * @return True if the display metrics are equal. |
| 374 | */ |
| 375 | public boolean equals(DisplayMetrics other) { |
Dianne Hackborn | 7ac8bbd | 2012-11-29 11:59:58 -0800 | [diff] [blame] | 376 | return equalsPhysical(other) |
| 377 | && scaledDensity == other.scaledDensity |
| 378 | && noncompatScaledDensity == other.noncompatScaledDensity; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Returns true if the physical aspects of the two display metrics |
| 383 | * are equal. This ignores the scaled density, which is a logical |
| 384 | * attribute based on the current desired font size. |
| 385 | * |
| 386 | * @param other The display metrics with which to compare. |
| 387 | * @return True if the display metrics are equal. |
| 388 | * @hide |
| 389 | */ |
| 390 | public boolean equalsPhysical(DisplayMetrics other) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 391 | return other != null |
| 392 | && widthPixels == other.widthPixels |
| 393 | && heightPixels == other.heightPixels |
| 394 | && density == other.density |
| 395 | && densityDpi == other.densityDpi |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 396 | && xdpi == other.xdpi |
| 397 | && ydpi == other.ydpi |
| 398 | && noncompatWidthPixels == other.noncompatWidthPixels |
| 399 | && noncompatHeightPixels == other.noncompatHeightPixels |
| 400 | && noncompatDensity == other.noncompatDensity |
| 401 | && noncompatDensityDpi == other.noncompatDensityDpi |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 402 | && noncompatXdpi == other.noncompatXdpi |
| 403 | && noncompatYdpi == other.noncompatYdpi; |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public int hashCode() { |
| 408 | return widthPixels * heightPixels * densityDpi; |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 411 | @Override |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 412 | public String toString() { |
| 413 | return "DisplayMetrics{density=" + density + ", width=" + widthPixels + |
| 414 | ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + |
| 415 | ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; |
| 416 | } |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 417 | |
| 418 | private static int getDeviceDensity() { |
| 419 | // qemu.sf.lcd_density can be used to override ro.sf.lcd_density |
| 420 | // when running in the emulator, allowing for dynamic configurations. |
| 421 | // The reason for this is that ro.sf.lcd_density is write-once and is |
| 422 | // set by the init process when it parses build.prop before anything else. |
| 423 | return SystemProperties.getInt("qemu.sf.lcd_density", |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 424 | SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | } |