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.view; |
| 18 | |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 19 | import android.content.res.CompatibilityInfo; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 20 | import android.graphics.PixelFormat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 21 | import android.graphics.Point; |
| 22 | import android.graphics.Rect; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 23 | import android.hardware.display.DisplayManager; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 24 | import android.os.RemoteException; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 25 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.util.DisplayMetrics; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 27 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 29 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 30 | * Provides information about the size and density of a logical display. |
| 31 | * <p> |
| 32 | * The display area is described in two different ways. |
| 33 | * <ul> |
| 34 | * <li>The application display area specifies the part of the display that may contain |
| 35 | * an application window, excluding the system decorations. The application display area may |
| 36 | * be smaller than the real display area because the system subtracts the space needed |
| 37 | * for decor elements such as the status bar. Use the following methods to query the |
| 38 | * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li> |
| 39 | * <li>The real display area specifies the part of the display that contains content |
| 40 | * including the system decorations. Even so, the real display area may be smaller than the |
| 41 | * physical size of the display if the window manager is emulating a smaller display |
| 42 | * using (adb shell am display-size). Use the following methods to query the |
| 43 | * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li> |
| 44 | * </ul> |
| 45 | * </p><p> |
| 46 | * A logical display does not necessarily represent a particular physical display device |
| 47 | * such as the built-in screen or an external monitor. The contents of a logical |
| 48 | * display may be presented on one or more physical displays according to the devices |
| 49 | * that are currently attached and whether mirroring has been enabled. |
| 50 | * </p> |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 51 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 52 | public final class Display { |
| 53 | private static final String TAG = "Display"; |
| 54 | |
| 55 | private final int mDisplayId; |
| 56 | private final CompatibilityInfoHolder mCompatibilityInfo; |
| 57 | private final DisplayInfo mDisplayInfo = new DisplayInfo(); |
| 58 | |
| 59 | // Temporary display metrics structure used for compatibility mode. |
| 60 | private final DisplayMetrics mTempMetrics = new DisplayMetrics(); |
| 61 | |
| 62 | // We cache the app width and height properties briefly between calls |
| 63 | // to getHeight() and getWidth() to ensure that applications perceive |
| 64 | // consistent results when the size changes (most of the time). |
| 65 | // Applications should now be using getSize() instead. |
| 66 | private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20; |
| 67 | private long mLastCachedAppSizeUpdate; |
| 68 | private int mCachedAppWidthCompat; |
| 69 | private int mCachedAppHeightCompat; |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 72 | * The default Display id. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | */ |
| 74 | public static final int DEFAULT_DISPLAY = 0; |
| 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 77 | * Internal method to create a display. |
| 78 | * Applications should use {@link android.view.WindowManager#getDefaultDisplay()} |
| 79 | * to get a display object for the default display. |
| 80 | * |
| 81 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 83 | public Display(int displayId, CompatibilityInfoHolder compatibilityInfo) { |
| 84 | mDisplayId = displayId; |
| 85 | mCompatibilityInfo = compatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 89 | * Gets the display id. |
| 90 | * <p> |
| 91 | * Each logical display has a unique id. |
| 92 | * The default display has id {@link #DEFAULT_DISPLAY}. |
| 93 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | */ |
| 95 | public int getDisplayId() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 96 | return mDisplayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 100 | * Gets the size of the display, in pixels. |
| 101 | * <p> |
| 102 | * Note that this value should <em>not</em> be used for computing layouts, |
| 103 | * since a device will typically have screen decoration (such as a status bar) |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 104 | * along the edges of the display that reduce the amount of application |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 105 | * space available from the size returned here. Layouts should instead use |
| 106 | * the window size. |
| 107 | * </p><p> |
| 108 | * The size is adjusted based on the current rotation of the display. |
| 109 | * </p><p> |
| 110 | * The size returned by this method does not necessarily represent the |
| 111 | * actual raw size (native resolution) of the display. The returned size may |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 112 | * be adjusted to exclude certain system decoration elements that are always visible. |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 113 | * It may also be scaled to provide compatibility with older applications that |
| 114 | * were originally designed for smaller displays. |
| 115 | * </p> |
| 116 | * |
| 117 | * @param outSize A {@link Point} object to receive the size information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 119 | public void getSize(Point outSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 120 | synchronized (this) { |
| 121 | updateDisplayInfoLocked(); |
| 122 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 123 | outSize.x = mTempMetrics.widthPixels; |
| 124 | outSize.y = mTempMetrics.heightPixels; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 129 | * Gets the size of the display as a rectangle, in pixels. |
| 130 | * |
| 131 | * @param outSize A {@link Rect} object to receive the size information. |
| 132 | * @see #getSize(Point) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 134 | public void getRectSize(Rect outSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 135 | synchronized (this) { |
| 136 | updateDisplayInfoLocked(); |
| 137 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 138 | outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
Dianne Hackborn | 68c33ca | 2012-04-19 14:51:25 -0700 | [diff] [blame] | 143 | * Return the range of display sizes an application can expect to encounter |
| 144 | * under normal operation, as long as there is no physical change in screen |
| 145 | * size. This is basically the sizes you will see as the orientation |
| 146 | * changes, taking into account whatever screen decoration there is in |
| 147 | * each rotation. For example, the status bar is always at the top of the |
| 148 | * screen, so it will reduce the height both in landscape and portrait, and |
| 149 | * the smallest height returned here will be the smaller of the two. |
| 150 | * |
| 151 | * This is intended for applications to get an idea of the range of sizes |
| 152 | * they will encounter while going through device rotations, to provide a |
| 153 | * stable UI through rotation. The sizes here take into account all standard |
| 154 | * system decorations that reduce the size actually available to the |
| 155 | * application: the status bar, navigation bar, system bar, etc. It does |
| 156 | * <em>not</em> take into account more transient elements like an IME |
| 157 | * soft keyboard. |
| 158 | * |
| 159 | * @param outSmallestSize Filled in with the smallest width and height |
| 160 | * that the application will encounter, in pixels (not dp units). The x |
| 161 | * (width) dimension here directly corresponds to |
| 162 | * {@link android.content.res.Configuration#smallestScreenWidthDp |
| 163 | * Configuration.smallestScreenWidthDp}, except the value here is in raw |
| 164 | * screen pixels rather than dp units. Your application may of course |
| 165 | * still get smaller space yet if, for example, a soft keyboard is |
| 166 | * being displayed. |
| 167 | * @param outLargestSize Filled in with the largest width and height |
| 168 | * that the application will encounter, in pixels (not dp units). Your |
| 169 | * application may of course still get larger space than this if, |
| 170 | * for example, screen decorations like the status bar are being hidden. |
| 171 | */ |
| 172 | public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 173 | synchronized (this) { |
| 174 | updateDisplayInfoLocked(); |
| 175 | outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth; |
| 176 | outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight; |
| 177 | outLargestSize.x = mDisplayInfo.largestNominalAppWidth; |
| 178 | outLargestSize.y = mDisplayInfo.largestNominalAppHeight; |
Dianne Hackborn | 68c33ca | 2012-04-19 14:51:25 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 183 | * Return the maximum screen size dimension that will happen. This is |
| 184 | * mostly for wallpapers. |
| 185 | * @hide |
| 186 | */ |
| 187 | public int getMaximumSizeDimension() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 188 | synchronized (this) { |
| 189 | updateDisplayInfoLocked(); |
| 190 | return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @deprecated Use {@link #getSize(Point)} instead. |
| 196 | */ |
| 197 | @Deprecated |
| 198 | public int getWidth() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 199 | synchronized (this) { |
| 200 | updateCachedAppSizeIfNeededLocked(); |
| 201 | return mCachedAppWidthCompat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @deprecated Use {@link #getSize(Point)} instead. |
| 207 | */ |
| 208 | @Deprecated |
| 209 | public int getHeight() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 210 | synchronized (this) { |
| 211 | updateCachedAppSizeIfNeededLocked(); |
| 212 | return mCachedAppHeightCompat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 216 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 217 | * Returns the rotation of the screen from its "natural" orientation. |
| 218 | * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0} |
| 219 | * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90}, |
| 220 | * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or |
| 221 | * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For |
| 222 | * example, if a device has a naturally tall screen, and the user has |
| 223 | * turned it on its side to go into a landscape orientation, the value |
| 224 | * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90} |
| 225 | * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on |
| 226 | * the direction it was turned. The angle is the rotation of the drawn |
| 227 | * graphics on the screen, which is the opposite direction of the physical |
| 228 | * rotation of the device. For example, if the device is rotated 90 |
| 229 | * degrees counter-clockwise, to compensate rendering will be rotated by |
| 230 | * 90 degrees clockwise and thus the returned value here will be |
| 231 | * {@link Surface#ROTATION_90 Surface.ROTATION_90}. |
| 232 | */ |
| 233 | public int getRotation() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 234 | synchronized (this) { |
| 235 | updateDisplayInfoLocked(); |
| 236 | return mDisplayInfo.rotation; |
| 237 | } |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 238 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 239 | |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 240 | /** |
Joe Onorato | 4c904a3 | 2010-02-26 12:35:55 -0800 | [diff] [blame] | 241 | * @deprecated use {@link #getRotation} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | * @return orientation of this display. |
| 243 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 244 | @Deprecated |
| 245 | public int getOrientation() { |
| 246 | return getRotation(); |
| 247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
| 249 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 250 | * Gets the pixel format of the display. |
| 251 | * @return One of the constants defined in {@link android.graphics.PixelFormat}. |
| 252 | * |
| 253 | * @deprecated This method is no longer supported. |
| 254 | * The result is always {@link PixelFormat#RGBA_8888}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 256 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | public int getPixelFormat() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 258 | return PixelFormat.RGBA_8888; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 260 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 262 | * Gets the refresh rate of this display in frames per second. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | */ |
| 264 | public float getRefreshRate() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 265 | synchronized (this) { |
| 266 | updateDisplayInfoLocked(); |
| 267 | return mDisplayInfo.refreshRate; |
| 268 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 272 | * Gets display metrics that describe the size and density of this display. |
| 273 | * <p> |
| 274 | * The size is adjusted based on the current rotation of the display. |
| 275 | * </p><p> |
| 276 | * The size returned by this method does not necessarily represent the |
| 277 | * actual raw size (native resolution) of the display. The returned size may |
| 278 | * be adjusted to exclude certain system decor elements that are always visible. |
| 279 | * It may also be scaled to provide compatibility with older applications that |
| 280 | * were originally designed for smaller displays. |
| 281 | * </p> |
| 282 | * |
| 283 | * @param outMetrics A {@link DisplayMetrics} object to receive the metrics. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | */ |
| 285 | public void getMetrics(DisplayMetrics outMetrics) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 286 | synchronized (this) { |
| 287 | updateDisplayInfoLocked(); |
| 288 | mDisplayInfo.getAppMetrics(outMetrics, mCompatibilityInfo); |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 289 | } |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 290 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 291 | final CompatibilityInfo ci = mCompatibilityInfo.getIfNeeded(); |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 292 | if (ci != null) { |
| 293 | ci.applyToDisplayMetrics(outMetrics); |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 294 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 295 | } |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 296 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 297 | /** |
| 298 | * Gets the real size of the display without subtracting any window decor or |
| 299 | * applying any compatibility scale factors. |
| 300 | * <p> |
| 301 | * The size is adjusted based on the current rotation of the display. |
| 302 | * </p><p> |
| 303 | * The real size may be smaller than the physical size of the screen when the |
| 304 | * window manager is emulating a smaller display (using adb shell am display-size). |
| 305 | * </p> |
| 306 | * |
| 307 | * @param outSize Set to the real size of the display. |
| 308 | */ |
| 309 | public void getRealSize(Point outSize) { |
| 310 | synchronized (this) { |
| 311 | updateDisplayInfoLocked(); |
| 312 | outSize.x = mDisplayInfo.logicalWidth; |
| 313 | outSize.y = mDisplayInfo.logicalHeight; |
| 314 | } |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 318 | * Gets display metrics based on the real size of this display. |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 319 | * <p> |
| 320 | * The size is adjusted based on the current rotation of the display. |
| 321 | * </p><p> |
| 322 | * The real size may be smaller than the physical size of the screen when the |
| 323 | * window manager is emulating a smaller display (using adb shell am display-size). |
| 324 | * </p> |
| 325 | * |
| 326 | * @param outMetrics A {@link DisplayMetrics} object to receive the metrics. |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 327 | */ |
| 328 | public void getRealMetrics(DisplayMetrics outMetrics) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 329 | synchronized (this) { |
| 330 | updateDisplayInfoLocked(); |
| 331 | mDisplayInfo.getLogicalMetrics(outMetrics, null); |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 332 | } |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 335 | private void updateDisplayInfoLocked() { |
| 336 | // TODO: only refresh the display information when needed |
| 337 | if (!DisplayManager.getInstance().getDisplayInfo(mDisplayId, mDisplayInfo)) { |
| 338 | Log.e(TAG, "Could not get information about logical display " + mDisplayId); |
| 339 | } |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame^] | 342 | private void updateCachedAppSizeIfNeededLocked() { |
| 343 | long now = SystemClock.uptimeMillis(); |
| 344 | if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) { |
| 345 | updateDisplayInfoLocked(); |
| 346 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 347 | mCachedAppWidthCompat = mTempMetrics.widthPixels; |
| 348 | mCachedAppHeightCompat = mTempMetrics.heightPixels; |
| 349 | mLastCachedAppSizeUpdate = now; |
| 350 | } |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |