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 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 19 | import android.graphics.PixelFormat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 20 | import android.graphics.Point; |
| 21 | import android.graphics.Rect; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 22 | import android.hardware.display.DisplayManagerGlobal; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 23 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.util.DisplayMetrics; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 25 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 27 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 28 | * Provides information about the size and density of a logical display. |
| 29 | * <p> |
| 30 | * The display area is described in two different ways. |
| 31 | * <ul> |
| 32 | * <li>The application display area specifies the part of the display that may contain |
| 33 | * an application window, excluding the system decorations. The application display area may |
| 34 | * be smaller than the real display area because the system subtracts the space needed |
| 35 | * for decor elements such as the status bar. Use the following methods to query the |
| 36 | * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li> |
| 37 | * <li>The real display area specifies the part of the display that contains content |
| 38 | * including the system decorations. Even so, the real display area may be smaller than the |
| 39 | * physical size of the display if the window manager is emulating a smaller display |
| 40 | * using (adb shell am display-size). Use the following methods to query the |
| 41 | * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li> |
| 42 | * </ul> |
| 43 | * </p><p> |
| 44 | * A logical display does not necessarily represent a particular physical display device |
| 45 | * such as the built-in screen or an external monitor. The contents of a logical |
| 46 | * display may be presented on one or more physical displays according to the devices |
| 47 | * that are currently attached and whether mirroring has been enabled. |
| 48 | * </p> |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 49 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 50 | public final class Display { |
| 51 | private static final String TAG = "Display"; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 52 | private static final boolean DEBUG = false; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 53 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 54 | private final DisplayManagerGlobal mGlobal; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 55 | private final int mDisplayId; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 56 | private final int mLayerStack; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 57 | private final int mFlags; |
| 58 | private final int mType; |
| 59 | private final String mAddress; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 60 | private final CompatibilityInfoHolder mCompatibilityInfo; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 61 | |
| 62 | private DisplayInfo mDisplayInfo; // never null |
| 63 | private boolean mIsValid; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 64 | |
| 65 | // Temporary display metrics structure used for compatibility mode. |
| 66 | private final DisplayMetrics mTempMetrics = new DisplayMetrics(); |
| 67 | |
| 68 | // We cache the app width and height properties briefly between calls |
| 69 | // to getHeight() and getWidth() to ensure that applications perceive |
| 70 | // consistent results when the size changes (most of the time). |
| 71 | // Applications should now be using getSize() instead. |
| 72 | private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20; |
| 73 | private long mLastCachedAppSizeUpdate; |
| 74 | private int mCachedAppWidthCompat; |
| 75 | private int mCachedAppHeightCompat; |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 78 | * The default Display id, which is the id of the built-in primary display |
| 79 | * assuming there is one. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | */ |
| 81 | public static final int DEFAULT_DISPLAY = 0; |
| 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 84 | * Display flag: Indicates that the display supports compositing content |
| 85 | * that is stored in protected graphics buffers. |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 86 | * <p> |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 87 | * If this flag is set then the display device supports compositing protected buffers. |
| 88 | * </p><p> |
| 89 | * If this flag is not set then the display device may not support compositing |
| 90 | * protected buffers; the user may see a blank region on the screen instead of |
| 91 | * the protected content. |
| 92 | * </p><p> |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 93 | * Secure (DRM) video decoders may allocate protected graphics buffers to request that |
| 94 | * a hardware-protected path be provided between the video decoder and the external |
| 95 | * display sink. If a hardware-protected path is not available, then content stored |
| 96 | * in protected graphics buffers may not be composited. |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 97 | * </p><p> |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 98 | * An application can use the absence of this flag as a hint that it should not use protected |
| 99 | * buffers for this display because the content may not be visible. For example, |
| 100 | * if the flag is not set then the application may choose not to show content on this |
| 101 | * display, show an informative error message, select an alternate content stream |
| 102 | * or adopt a different strategy for decoding content that does not rely on |
| 103 | * protected buffers. |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 104 | * </p> |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 105 | * |
| 106 | * @see #getFlags |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 107 | */ |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 108 | public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 0; |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 111 | * Display flag: Indicates that the display has a secure video output and |
| 112 | * supports compositing secure surfaces. |
| 113 | * <p> |
| 114 | * If this flag is set then the display device has a secure video output |
| 115 | * and is capable of showing secure surfaces. It may also be capable of |
| 116 | * showing {@link #FLAG_SUPPORTS_PROTECTED_BUFFERS protected buffers}. |
| 117 | * </p><p> |
| 118 | * If this flag is not set then the display device may not have a secure video |
| 119 | * output; the user may see a blank region on the screen instead of |
| 120 | * the contents of secure surfaces or protected buffers. |
| 121 | * </p><p> |
| 122 | * Secure surfaces are used to prevent content rendered into those surfaces |
| 123 | * by applications from appearing in screenshots or from being viewed |
| 124 | * on non-secure displays. Protected buffers are used by secure video decoders |
| 125 | * for a similar purpose. |
| 126 | * </p><p> |
| 127 | * An application creates a window with a secure surface by specifying the |
| 128 | * {@link WindowManager.LayoutParams#FLAG_SECURE} window flag. |
| 129 | * Likewise, an application creates a {@link SurfaceView} with a secure surface |
| 130 | * by calling {@link SurfaceView#setSecure} before attaching the secure view to |
| 131 | * its containing window. |
| 132 | * </p><p> |
| 133 | * An application can use the absence of this flag as a hint that it should not create |
| 134 | * secure surfaces or protected buffers on this display because the content may |
| 135 | * not be visible. For example, if the flag is not set then the application may |
| 136 | * choose not to show content on this display, show an informative error message, |
| 137 | * select an alternate content stream or adopt a different strategy for decoding |
| 138 | * content that does not rely on secure surfaces or protected buffers. |
| 139 | * </p> |
| 140 | * |
| 141 | * @see #getFlags |
| 142 | */ |
| 143 | public static final int FLAG_SECURE = 1 << 1; |
| 144 | |
| 145 | /** |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 146 | * Display type: Unknown display type. |
| 147 | * @hide |
| 148 | */ |
| 149 | public static final int TYPE_UNKNOWN = 0; |
| 150 | |
| 151 | /** |
| 152 | * Display type: Built-in display. |
| 153 | * @hide |
| 154 | */ |
| 155 | public static final int TYPE_BUILT_IN = 1; |
| 156 | |
| 157 | /** |
| 158 | * Display type: HDMI display. |
| 159 | * @hide |
| 160 | */ |
| 161 | public static final int TYPE_HDMI = 2; |
| 162 | |
| 163 | /** |
| 164 | * Display type: WiFi display. |
| 165 | * @hide |
| 166 | */ |
| 167 | public static final int TYPE_WIFI = 3; |
| 168 | |
| 169 | /** |
| 170 | * Display type: Overlay display. |
| 171 | * @hide |
| 172 | */ |
| 173 | public static final int TYPE_OVERLAY = 4; |
| 174 | |
| 175 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 176 | * Internal method to create a display. |
| 177 | * Applications should use {@link android.view.WindowManager#getDefaultDisplay()} |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 178 | * or {@link android.hardware.display.DisplayManager#getDisplay} |
| 179 | * to get a display object. |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 180 | * |
| 181 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | */ |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 183 | public Display(DisplayManagerGlobal global, |
| 184 | int displayId, DisplayInfo displayInfo /*not null*/, |
| 185 | CompatibilityInfoHolder compatibilityInfo) { |
| 186 | mGlobal = global; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 187 | mDisplayId = displayId; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 188 | mDisplayInfo = displayInfo; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 189 | mCompatibilityInfo = compatibilityInfo; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 190 | mIsValid = true; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 191 | |
| 192 | // Cache properties that cannot change as long as the display is valid. |
| 193 | mLayerStack = displayInfo.layerStack; |
| 194 | mFlags = displayInfo.flags; |
| 195 | mType = displayInfo.type; |
| 196 | mAddress = displayInfo.address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 200 | * Gets the display id. |
| 201 | * <p> |
| 202 | * Each logical display has a unique id. |
| 203 | * The default display has id {@link #DEFAULT_DISPLAY}. |
| 204 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | */ |
| 206 | public int getDisplayId() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 207 | return mDisplayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 211 | * Returns true if this display is still valid, false if the display has been removed. |
| 212 | * |
| 213 | * If the display is invalid, then the methods of this class will |
| 214 | * continue to report the most recently observed display information. |
| 215 | * However, it is unwise (and rather fruitless) to continue using a |
| 216 | * {@link Display} object after the display's demise. |
| 217 | * |
| 218 | * It's possible for a display that was previously invalid to become |
| 219 | * valid again if a display with the same id is reconnected. |
| 220 | * |
| 221 | * @return True if the display is still valid. |
| 222 | */ |
| 223 | public boolean isValid() { |
| 224 | synchronized (this) { |
| 225 | updateDisplayInfoLocked(); |
| 226 | return mIsValid; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
Jeff Brown | 2ab1b7d9 | 2012-08-08 01:46:41 -0700 | [diff] [blame] | 231 | * Gets a full copy of the display information. |
| 232 | * |
| 233 | * @param outDisplayInfo The object to receive the copy of the display information. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 234 | * @return True if the display is still valid. |
Jeff Brown | 2ab1b7d9 | 2012-08-08 01:46:41 -0700 | [diff] [blame] | 235 | * @hide |
| 236 | */ |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 237 | public boolean getDisplayInfo(DisplayInfo outDisplayInfo) { |
Jeff Brown | 2ab1b7d9 | 2012-08-08 01:46:41 -0700 | [diff] [blame] | 238 | synchronized (this) { |
| 239 | updateDisplayInfoLocked(); |
| 240 | outDisplayInfo.copyFrom(mDisplayInfo); |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 241 | return mIsValid; |
Jeff Brown | 2ab1b7d9 | 2012-08-08 01:46:41 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 246 | * Gets the display's layer stack. |
| 247 | * |
| 248 | * Each display has its own independent layer stack upon which surfaces |
| 249 | * are placed to be managed by surface flinger. |
| 250 | * |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 251 | * @return The display's layer stack number. |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 252 | * @hide |
| 253 | */ |
| 254 | public int getLayerStack() { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 255 | return mLayerStack; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 259 | * Returns a combination of flags that describe the capabilities of the display. |
| 260 | * |
| 261 | * @return The display flags. |
| 262 | * |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 263 | * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 264 | * @see #FLAG_SECURE |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 265 | */ |
| 266 | public int getFlags() { |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 267 | return mFlags; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Gets the display type. |
| 272 | * |
| 273 | * @return The display type. |
| 274 | * |
| 275 | * @see #TYPE_UNKNOWN |
| 276 | * @see #TYPE_BUILT_IN |
| 277 | * @see #TYPE_HDMI |
| 278 | * @see #TYPE_WIFI |
| 279 | * @see #TYPE_OVERLAY |
| 280 | * @hide |
| 281 | */ |
| 282 | public int getType() { |
| 283 | return mType; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Gets the display address, or null if none. |
| 288 | * Interpretation varies by display type. |
| 289 | * |
| 290 | * @return The display address. |
| 291 | * @hide |
| 292 | */ |
| 293 | public String getAddress() { |
| 294 | return mAddress; |
Jeff Brown | c5df37c | 2012-09-13 11:45:07 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 298 | * Gets the compatibility info used by this display instance. |
| 299 | * |
| 300 | * @return The compatibility info holder, or null if none is required. |
| 301 | * @hide |
| 302 | */ |
| 303 | public CompatibilityInfoHolder getCompatibilityInfo() { |
| 304 | return mCompatibilityInfo; |
| 305 | } |
| 306 | |
| 307 | /** |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 308 | * Gets the name of the display. |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 309 | * <p> |
| 310 | * Note that some displays may be renamed by the user. |
| 311 | * </p> |
| 312 | * |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 313 | * @return The display's name. |
| 314 | */ |
| 315 | public String getName() { |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 316 | synchronized (this) { |
| 317 | updateDisplayInfoLocked(); |
| 318 | return mDisplayInfo.name; |
| 319 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 323 | * Gets the size of the display, in pixels. |
| 324 | * <p> |
| 325 | * Note that this value should <em>not</em> be used for computing layouts, |
| 326 | * 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] | 327 | * along the edges of the display that reduce the amount of application |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 328 | * space available from the size returned here. Layouts should instead use |
| 329 | * the window size. |
| 330 | * </p><p> |
| 331 | * The size is adjusted based on the current rotation of the display. |
| 332 | * </p><p> |
| 333 | * The size returned by this method does not necessarily represent the |
| 334 | * actual raw size (native resolution) of the display. The returned size may |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 335 | * be adjusted to exclude certain system decoration elements that are always visible. |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 336 | * It may also be scaled to provide compatibility with older applications that |
| 337 | * were originally designed for smaller displays. |
| 338 | * </p> |
| 339 | * |
| 340 | * @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] | 341 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 342 | public void getSize(Point outSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 343 | synchronized (this) { |
| 344 | updateDisplayInfoLocked(); |
| 345 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 346 | outSize.x = mTempMetrics.widthPixels; |
| 347 | outSize.y = mTempMetrics.heightPixels; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 352 | * Gets the size of the display as a rectangle, in pixels. |
| 353 | * |
| 354 | * @param outSize A {@link Rect} object to receive the size information. |
| 355 | * @see #getSize(Point) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 357 | public void getRectSize(Rect outSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 358 | synchronized (this) { |
| 359 | updateDisplayInfoLocked(); |
| 360 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 361 | outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
Dianne Hackborn | 68c33ca | 2012-04-19 14:51:25 -0700 | [diff] [blame] | 366 | * Return the range of display sizes an application can expect to encounter |
| 367 | * under normal operation, as long as there is no physical change in screen |
| 368 | * size. This is basically the sizes you will see as the orientation |
| 369 | * changes, taking into account whatever screen decoration there is in |
| 370 | * each rotation. For example, the status bar is always at the top of the |
| 371 | * screen, so it will reduce the height both in landscape and portrait, and |
| 372 | * the smallest height returned here will be the smaller of the two. |
| 373 | * |
| 374 | * This is intended for applications to get an idea of the range of sizes |
| 375 | * they will encounter while going through device rotations, to provide a |
| 376 | * stable UI through rotation. The sizes here take into account all standard |
| 377 | * system decorations that reduce the size actually available to the |
| 378 | * application: the status bar, navigation bar, system bar, etc. It does |
| 379 | * <em>not</em> take into account more transient elements like an IME |
| 380 | * soft keyboard. |
| 381 | * |
| 382 | * @param outSmallestSize Filled in with the smallest width and height |
| 383 | * that the application will encounter, in pixels (not dp units). The x |
| 384 | * (width) dimension here directly corresponds to |
| 385 | * {@link android.content.res.Configuration#smallestScreenWidthDp |
| 386 | * Configuration.smallestScreenWidthDp}, except the value here is in raw |
| 387 | * screen pixels rather than dp units. Your application may of course |
| 388 | * still get smaller space yet if, for example, a soft keyboard is |
| 389 | * being displayed. |
| 390 | * @param outLargestSize Filled in with the largest width and height |
| 391 | * that the application will encounter, in pixels (not dp units). Your |
| 392 | * application may of course still get larger space than this if, |
| 393 | * for example, screen decorations like the status bar are being hidden. |
| 394 | */ |
| 395 | public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 396 | synchronized (this) { |
| 397 | updateDisplayInfoLocked(); |
| 398 | outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth; |
| 399 | outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight; |
| 400 | outLargestSize.x = mDisplayInfo.largestNominalAppWidth; |
| 401 | outLargestSize.y = mDisplayInfo.largestNominalAppHeight; |
Dianne Hackborn | 68c33ca | 2012-04-19 14:51:25 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 406 | * Return the maximum screen size dimension that will happen. This is |
| 407 | * mostly for wallpapers. |
| 408 | * @hide |
| 409 | */ |
| 410 | public int getMaximumSizeDimension() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 411 | synchronized (this) { |
| 412 | updateDisplayInfoLocked(); |
| 413 | return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @deprecated Use {@link #getSize(Point)} instead. |
| 419 | */ |
| 420 | @Deprecated |
| 421 | public int getWidth() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 422 | synchronized (this) { |
| 423 | updateCachedAppSizeIfNeededLocked(); |
| 424 | return mCachedAppWidthCompat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * @deprecated Use {@link #getSize(Point)} instead. |
| 430 | */ |
| 431 | @Deprecated |
| 432 | public int getHeight() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 433 | synchronized (this) { |
| 434 | updateCachedAppSizeIfNeededLocked(); |
| 435 | return mCachedAppHeightCompat; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 439 | /** |
Dianne Hackborn | c652de8 | 2013-02-15 16:32:56 -0800 | [diff] [blame] | 440 | * @hide |
| 441 | * Return a rectangle defining the insets of the overscan region of the display. |
| 442 | * Each field of the rectangle is the number of pixels the overscan area extends |
| 443 | * into the display on that side. |
| 444 | */ |
| 445 | public void getOverscanInsets(Rect outRect) { |
| 446 | synchronized (this) { |
| 447 | updateDisplayInfoLocked(); |
| 448 | outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop, |
| 449 | mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 454 | * Returns the rotation of the screen from its "natural" orientation. |
| 455 | * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0} |
| 456 | * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90}, |
| 457 | * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or |
| 458 | * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For |
| 459 | * example, if a device has a naturally tall screen, and the user has |
| 460 | * turned it on its side to go into a landscape orientation, the value |
| 461 | * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90} |
| 462 | * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on |
| 463 | * the direction it was turned. The angle is the rotation of the drawn |
| 464 | * graphics on the screen, which is the opposite direction of the physical |
| 465 | * rotation of the device. For example, if the device is rotated 90 |
| 466 | * degrees counter-clockwise, to compensate rendering will be rotated by |
| 467 | * 90 degrees clockwise and thus the returned value here will be |
| 468 | * {@link Surface#ROTATION_90 Surface.ROTATION_90}. |
| 469 | */ |
| 470 | public int getRotation() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 471 | synchronized (this) { |
| 472 | updateDisplayInfoLocked(); |
| 473 | return mDisplayInfo.rotation; |
| 474 | } |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 475 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 476 | |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 477 | /** |
Joe Onorato | 4c904a3 | 2010-02-26 12:35:55 -0800 | [diff] [blame] | 478 | * @deprecated use {@link #getRotation} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | * @return orientation of this display. |
| 480 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 481 | @Deprecated |
| 482 | public int getOrientation() { |
| 483 | return getRotation(); |
| 484 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | |
| 486 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 487 | * Gets the pixel format of the display. |
| 488 | * @return One of the constants defined in {@link android.graphics.PixelFormat}. |
| 489 | * |
| 490 | * @deprecated This method is no longer supported. |
| 491 | * The result is always {@link PixelFormat#RGBA_8888}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | */ |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 493 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | public int getPixelFormat() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 495 | return PixelFormat.RGBA_8888; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | /** |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 499 | * 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] | 500 | */ |
| 501 | public float getRefreshRate() { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 502 | synchronized (this) { |
| 503 | updateDisplayInfoLocked(); |
| 504 | return mDisplayInfo.refreshRate; |
| 505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 509 | * Gets display metrics that describe the size and density of this display. |
| 510 | * <p> |
| 511 | * The size is adjusted based on the current rotation of the display. |
| 512 | * </p><p> |
| 513 | * The size returned by this method does not necessarily represent the |
| 514 | * actual raw size (native resolution) of the display. The returned size may |
| 515 | * be adjusted to exclude certain system decor elements that are always visible. |
| 516 | * It may also be scaled to provide compatibility with older applications that |
| 517 | * were originally designed for smaller displays. |
| 518 | * </p> |
| 519 | * |
| 520 | * @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] | 521 | */ |
| 522 | public void getMetrics(DisplayMetrics outMetrics) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 523 | synchronized (this) { |
| 524 | updateDisplayInfoLocked(); |
| 525 | mDisplayInfo.getAppMetrics(outMetrics, mCompatibilityInfo); |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 526 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 527 | } |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 528 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 529 | /** |
| 530 | * Gets the real size of the display without subtracting any window decor or |
| 531 | * applying any compatibility scale factors. |
| 532 | * <p> |
| 533 | * The size is adjusted based on the current rotation of the display. |
| 534 | * </p><p> |
| 535 | * The real size may be smaller than the physical size of the screen when the |
| 536 | * window manager is emulating a smaller display (using adb shell am display-size). |
| 537 | * </p> |
| 538 | * |
| 539 | * @param outSize Set to the real size of the display. |
| 540 | */ |
| 541 | public void getRealSize(Point outSize) { |
| 542 | synchronized (this) { |
| 543 | updateDisplayInfoLocked(); |
| 544 | outSize.x = mDisplayInfo.logicalWidth; |
| 545 | outSize.y = mDisplayInfo.logicalHeight; |
| 546 | } |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 550 | * Gets display metrics based on the real size of this display. |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 551 | * <p> |
| 552 | * The size is adjusted based on the current rotation of the display. |
| 553 | * </p><p> |
| 554 | * The real size may be smaller than the physical size of the screen when the |
| 555 | * window manager is emulating a smaller display (using adb shell am display-size). |
| 556 | * </p> |
| 557 | * |
| 558 | * @param outMetrics A {@link DisplayMetrics} object to receive the metrics. |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 559 | */ |
| 560 | public void getRealMetrics(DisplayMetrics outMetrics) { |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 561 | synchronized (this) { |
| 562 | updateDisplayInfoLocked(); |
| 563 | mDisplayInfo.getLogicalMetrics(outMetrics, null); |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 564 | } |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 567 | private void updateDisplayInfoLocked() { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 568 | // Note: The display manager caches display info objects on our behalf. |
| 569 | DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId); |
| 570 | if (newInfo == null) { |
| 571 | // Preserve the old mDisplayInfo after the display is removed. |
| 572 | if (mIsValid) { |
| 573 | mIsValid = false; |
| 574 | if (DEBUG) { |
| 575 | Log.d(TAG, "Logical display " + mDisplayId + " was removed."); |
| 576 | } |
| 577 | } |
| 578 | } else { |
| 579 | // Use the new display info. (It might be the same object if nothing changed.) |
| 580 | mDisplayInfo = newInfo; |
| 581 | if (!mIsValid) { |
| 582 | mIsValid = true; |
| 583 | if (DEBUG) { |
| 584 | Log.d(TAG, "Logical display " + mDisplayId + " was recreated."); |
| 585 | } |
| 586 | } |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 587 | } |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 590 | private void updateCachedAppSizeIfNeededLocked() { |
| 591 | long now = SystemClock.uptimeMillis(); |
| 592 | if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) { |
| 593 | updateDisplayInfoLocked(); |
| 594 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 595 | mCachedAppWidthCompat = mTempMetrics.widthPixels; |
| 596 | mCachedAppHeightCompat = mTempMetrics.heightPixels; |
| 597 | mLastCachedAppSizeUpdate = now; |
| 598 | } |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 599 | } |
Jeff Brown | bf5740e | 2012-08-19 23:20:02 -0700 | [diff] [blame] | 600 | |
| 601 | // For debugging purposes |
| 602 | @Override |
| 603 | public String toString() { |
| 604 | synchronized (this) { |
| 605 | updateDisplayInfoLocked(); |
| 606 | mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo); |
| 607 | return "Display id " + mDisplayId + ": " + mDisplayInfo |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 608 | + ", " + mTempMetrics + ", isValid=" + mIsValid; |
Jeff Brown | bf5740e | 2012-08-19 23:20:02 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 611 | |
| 612 | /** |
| 613 | * @hide |
| 614 | */ |
| 615 | public static String typeToString(int type) { |
| 616 | switch (type) { |
| 617 | case TYPE_UNKNOWN: |
| 618 | return "UNKNOWN"; |
| 619 | case TYPE_BUILT_IN: |
| 620 | return "BUILT_IN"; |
| 621 | case TYPE_HDMI: |
| 622 | return "HDMI"; |
| 623 | case TYPE_WIFI: |
| 624 | return "WIFI"; |
| 625 | case TYPE_OVERLAY: |
| 626 | return "OVERLAY"; |
| 627 | default: |
| 628 | return Integer.toString(type); |
| 629 | } |
| 630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |
| 632 | |