blob: 71e2251a8020134d9deb64022f106b86ebff2116 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Craig Mautner48d0d182013-06-11 07:53:06 -070019import android.content.res.CompatibilityInfo;
Jeff Brownfa25bf52012-07-23 19:26:30 -070020import android.graphics.PixelFormat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -070021import android.graphics.Point;
22import android.graphics.Rect;
Jeff Brownbd6e1502012-08-28 03:27:37 -070023import android.hardware.display.DisplayManagerGlobal;
Jeff Browna506a6e2013-06-04 00:02:38 -070024import android.os.Process;
Dianne Hackbornac8dea12011-04-20 18:18:51 -070025import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.DisplayMetrics;
Jeff Brownfa25bf52012-07-23 19:26:30 -070027import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Michael Wright3f145a22014-07-22 19:46:03 -070029import java.util.Arrays;
30
Jeff Brownbc68a592011-07-25 12:58:12 -070031/**
Jeff Brownfa25bf52012-07-23 19:26:30 -070032 * Provides information about the size and density of a logical display.
33 * <p>
34 * The display area is described in two different ways.
35 * <ul>
36 * <li>The application display area specifies the part of the display that may contain
37 * an application window, excluding the system decorations. The application display area may
38 * be smaller than the real display area because the system subtracts the space needed
39 * for decor elements such as the status bar. Use the following methods to query the
40 * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li>
41 * <li>The real display area specifies the part of the display that contains content
42 * including the system decorations. Even so, the real display area may be smaller than the
43 * physical size of the display if the window manager is emulating a smaller display
44 * using (adb shell am display-size). Use the following methods to query the
45 * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li>
46 * </ul>
47 * </p><p>
48 * A logical display does not necessarily represent a particular physical display device
49 * such as the built-in screen or an external monitor. The contents of a logical
50 * display may be presented on one or more physical displays according to the devices
51 * that are currently attached and whether mirroring has been enabled.
52 * </p>
Jeff Brownbc68a592011-07-25 12:58:12 -070053 */
Jeff Brownfa25bf52012-07-23 19:26:30 -070054public final class Display {
55 private static final String TAG = "Display";
Jeff Brownbd6e1502012-08-28 03:27:37 -070056 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -070057
Jeff Brownbd6e1502012-08-28 03:27:37 -070058 private final DisplayManagerGlobal mGlobal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070059 private final int mDisplayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070060 private final int mLayerStack;
Jeff Brown92130f62012-10-24 21:28:33 -070061 private final int mFlags;
62 private final int mType;
63 private final String mAddress;
Jeff Browna506a6e2013-06-04 00:02:38 -070064 private final int mOwnerUid;
65 private final String mOwnerPackageName;
Craig Mautner48d0d182013-06-11 07:53:06 -070066 private final DisplayAdjustments mDisplayAdjustments;
Jeff Brownbd6e1502012-08-28 03:27:37 -070067
68 private DisplayInfo mDisplayInfo; // never null
69 private boolean mIsValid;
Jeff Brownfa25bf52012-07-23 19:26:30 -070070
71 // Temporary display metrics structure used for compatibility mode.
72 private final DisplayMetrics mTempMetrics = new DisplayMetrics();
73
74 // We cache the app width and height properties briefly between calls
75 // to getHeight() and getWidth() to ensure that applications perceive
76 // consistent results when the size changes (most of the time).
77 // Applications should now be using getSize() instead.
78 private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20;
79 private long mLastCachedAppSizeUpdate;
80 private int mCachedAppWidthCompat;
81 private int mCachedAppHeightCompat;
Dianne Hackborn5fd21692011-06-07 14:09:47 -070082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
Jeff Brown98365d72012-08-19 20:30:52 -070084 * The default Display id, which is the id of the built-in primary display
85 * assuming there is one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
87 public static final int DEFAULT_DISPLAY = 0;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Jeff Brown77aebfd2012-10-01 21:07:03 -070090 * Display flag: Indicates that the display supports compositing content
91 * that is stored in protected graphics buffers.
Jeff Brownc5df37c2012-09-13 11:45:07 -070092 * <p>
Jeff Brownf0681b32012-10-23 17:35:57 -070093 * If this flag is set then the display device supports compositing protected buffers.
94 * </p><p>
95 * If this flag is not set then the display device may not support compositing
96 * protected buffers; the user may see a blank region on the screen instead of
97 * the protected content.
98 * </p><p>
Jeff Brown77aebfd2012-10-01 21:07:03 -070099 * Secure (DRM) video decoders may allocate protected graphics buffers to request that
100 * a hardware-protected path be provided between the video decoder and the external
101 * display sink. If a hardware-protected path is not available, then content stored
102 * in protected graphics buffers may not be composited.
Jeff Brownc5df37c2012-09-13 11:45:07 -0700103 * </p><p>
Jeff Brownf0681b32012-10-23 17:35:57 -0700104 * An application can use the absence of this flag as a hint that it should not use protected
105 * buffers for this display because the content may not be visible. For example,
106 * if the flag is not set then the application may choose not to show content on this
107 * display, show an informative error message, select an alternate content stream
108 * or adopt a different strategy for decoding content that does not rely on
109 * protected buffers.
Jeff Brownc5df37c2012-09-13 11:45:07 -0700110 * </p>
Jeff Brownf0681b32012-10-23 17:35:57 -0700111 *
112 * @see #getFlags
Jeff Brownc5df37c2012-09-13 11:45:07 -0700113 */
Jeff Brown77aebfd2012-10-01 21:07:03 -0700114 public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 0;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700115
116 /**
Jeff Brownf0681b32012-10-23 17:35:57 -0700117 * Display flag: Indicates that the display has a secure video output and
118 * supports compositing secure surfaces.
119 * <p>
120 * If this flag is set then the display device has a secure video output
121 * and is capable of showing secure surfaces. It may also be capable of
122 * showing {@link #FLAG_SUPPORTS_PROTECTED_BUFFERS protected buffers}.
123 * </p><p>
124 * If this flag is not set then the display device may not have a secure video
125 * output; the user may see a blank region on the screen instead of
126 * the contents of secure surfaces or protected buffers.
127 * </p><p>
128 * Secure surfaces are used to prevent content rendered into those surfaces
129 * by applications from appearing in screenshots or from being viewed
130 * on non-secure displays. Protected buffers are used by secure video decoders
131 * for a similar purpose.
132 * </p><p>
133 * An application creates a window with a secure surface by specifying the
134 * {@link WindowManager.LayoutParams#FLAG_SECURE} window flag.
135 * Likewise, an application creates a {@link SurfaceView} with a secure surface
136 * by calling {@link SurfaceView#setSecure} before attaching the secure view to
137 * its containing window.
138 * </p><p>
139 * An application can use the absence of this flag as a hint that it should not create
140 * secure surfaces or protected buffers on this display because the content may
141 * not be visible. For example, if the flag is not set then the application may
142 * choose not to show content on this display, show an informative error message,
143 * select an alternate content stream or adopt a different strategy for decoding
144 * content that does not rely on secure surfaces or protected buffers.
145 * </p>
146 *
147 * @see #getFlags
148 */
149 public static final int FLAG_SECURE = 1 << 1;
150
151 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700152 * Display flag: Indicates that the display is private. Only the application that
153 * owns the display can create windows on it.
Jeff Browna506a6e2013-06-04 00:02:38 -0700154 *
155 * @see #getFlags
156 */
157 public static final int FLAG_PRIVATE = 1 << 2;
158
159 /**
Jeff Brown7d00aff2013-08-02 19:03:49 -0700160 * Display flag: Indicates that the display is a presentation display.
161 * <p>
162 * This flag identifies secondary displays that are suitable for
163 * use as presentation displays such as HDMI or Wireless displays. Applications
164 * may automatically project their content to presentation displays to provide
165 * richer second screen experiences.
166 * </p>
167 *
168 * @see #getFlags
169 */
170 public static final int FLAG_PRESENTATION = 1 << 3;
171
172 /**
Jeff Brownd46747a2015-04-15 19:02:36 -0700173 * Display flag: Indicates that the contents of the display should not be scaled
174 * to fit the physical screen dimensions. Used for development only to emulate
175 * devices with smaller physicals screens while preserving density.
176 *
177 * @hide
178 */
179 public static final int FLAG_SCALING_DISABLED = 1 << 30;
180
181 /**
Jeff Brown92130f62012-10-24 21:28:33 -0700182 * Display type: Unknown display type.
183 * @hide
184 */
185 public static final int TYPE_UNKNOWN = 0;
186
187 /**
188 * Display type: Built-in display.
189 * @hide
190 */
191 public static final int TYPE_BUILT_IN = 1;
192
193 /**
194 * Display type: HDMI display.
195 * @hide
196 */
197 public static final int TYPE_HDMI = 2;
198
199 /**
200 * Display type: WiFi display.
201 * @hide
202 */
203 public static final int TYPE_WIFI = 3;
204
205 /**
206 * Display type: Overlay display.
207 * @hide
208 */
209 public static final int TYPE_OVERLAY = 4;
210
211 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700212 * Display type: Virtual display.
213 * @hide
214 */
215 public static final int TYPE_VIRTUAL = 5;
216
217 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700218 * Display state: The display state is unknown.
219 *
220 * @see #getState
221 */
222 public static final int STATE_UNKNOWN = 0;
223
224 /**
225 * Display state: The display is off.
226 *
227 * @see #getState
228 */
229 public static final int STATE_OFF = 1;
230
231 /**
232 * Display state: The display is on.
233 *
234 * @see #getState
235 */
236 public static final int STATE_ON = 2;
237
238 /**
Jeff Brown5dc21912014-07-17 18:50:18 -0700239 * Display state: The display is dozing in a low power state; it is still
240 * on but is optimized for showing system-provided content while the
241 * device is non-interactive.
Jeff Brown037c33e2014-04-09 00:31:55 -0700242 *
243 * @see #getState
244 * @see android.os.PowerManager#isInteractive
245 */
Jeff Brown5dc21912014-07-17 18:50:18 -0700246 public static final int STATE_DOZE = 3;
247
248 /**
249 * Display state: The display is dozing in a suspended low power state; it is still
250 * on but is optimized for showing static system-provided content while the device
251 * is non-interactive. This mode may be used to conserve even more power by allowing
252 * the hardware to stop applying frame buffer updates from the graphics subsystem or
253 * to take over the display and manage it autonomously to implement low power always-on
254 * display functionality.
255 *
256 * @see #getState
257 * @see android.os.PowerManager#isInteractive
258 */
259 public static final int STATE_DOZE_SUSPEND = 4;
Jeff Brown037c33e2014-04-09 00:31:55 -0700260
261 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700262 * Internal method to create a display.
263 * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
Jeff Brown98365d72012-08-19 20:30:52 -0700264 * or {@link android.hardware.display.DisplayManager#getDisplay}
265 * to get a display object.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700266 *
267 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700269 public Display(DisplayManagerGlobal global,
270 int displayId, DisplayInfo displayInfo /*not null*/,
Craig Mautner48d0d182013-06-11 07:53:06 -0700271 DisplayAdjustments daj) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700272 mGlobal = global;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700273 mDisplayId = displayId;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700274 mDisplayInfo = displayInfo;
Craig Mautner1abaa532013-06-28 12:03:32 -0700275 mDisplayAdjustments = new DisplayAdjustments(daj);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700276 mIsValid = true;
Jeff Brown92130f62012-10-24 21:28:33 -0700277
278 // Cache properties that cannot change as long as the display is valid.
279 mLayerStack = displayInfo.layerStack;
280 mFlags = displayInfo.flags;
281 mType = displayInfo.type;
282 mAddress = displayInfo.address;
Jeff Browna506a6e2013-06-04 00:02:38 -0700283 mOwnerUid = displayInfo.ownerUid;
284 mOwnerPackageName = displayInfo.ownerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700288 * Gets the display id.
289 * <p>
290 * Each logical display has a unique id.
291 * The default display has id {@link #DEFAULT_DISPLAY}.
292 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 */
294 public int getDisplayId() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700295 return mDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297
298 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700299 * Returns true if this display is still valid, false if the display has been removed.
300 *
301 * If the display is invalid, then the methods of this class will
302 * continue to report the most recently observed display information.
303 * However, it is unwise (and rather fruitless) to continue using a
304 * {@link Display} object after the display's demise.
305 *
306 * It's possible for a display that was previously invalid to become
307 * valid again if a display with the same id is reconnected.
308 *
309 * @return True if the display is still valid.
310 */
311 public boolean isValid() {
312 synchronized (this) {
313 updateDisplayInfoLocked();
314 return mIsValid;
315 }
316 }
317
318 /**
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700319 * Gets a full copy of the display information.
320 *
321 * @param outDisplayInfo The object to receive the copy of the display information.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700322 * @return True if the display is still valid.
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700323 * @hide
324 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700325 public boolean getDisplayInfo(DisplayInfo outDisplayInfo) {
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700326 synchronized (this) {
327 updateDisplayInfoLocked();
328 outDisplayInfo.copyFrom(mDisplayInfo);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700329 return mIsValid;
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700330 }
331 }
332
333 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700334 * Gets the display's layer stack.
335 *
336 * Each display has its own independent layer stack upon which surfaces
337 * are placed to be managed by surface flinger.
338 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700339 * @return The display's layer stack number.
Jeff Brown98365d72012-08-19 20:30:52 -0700340 * @hide
341 */
342 public int getLayerStack() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700343 return mLayerStack;
Jeff Brown98365d72012-08-19 20:30:52 -0700344 }
345
346 /**
Jeff Brownc5df37c2012-09-13 11:45:07 -0700347 * Returns a combination of flags that describe the capabilities of the display.
348 *
349 * @return The display flags.
350 *
Jeff Brown77aebfd2012-10-01 21:07:03 -0700351 * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS
Jeff Brownf0681b32012-10-23 17:35:57 -0700352 * @see #FLAG_SECURE
Jeff Browna506a6e2013-06-04 00:02:38 -0700353 * @see #FLAG_PRIVATE
Jeff Brownc5df37c2012-09-13 11:45:07 -0700354 */
355 public int getFlags() {
Jeff Brown92130f62012-10-24 21:28:33 -0700356 return mFlags;
357 }
358
359 /**
360 * Gets the display type.
361 *
362 * @return The display type.
363 *
364 * @see #TYPE_UNKNOWN
365 * @see #TYPE_BUILT_IN
366 * @see #TYPE_HDMI
367 * @see #TYPE_WIFI
368 * @see #TYPE_OVERLAY
Jeff Browna506a6e2013-06-04 00:02:38 -0700369 * @see #TYPE_VIRTUAL
Jeff Brown92130f62012-10-24 21:28:33 -0700370 * @hide
371 */
372 public int getType() {
373 return mType;
374 }
375
376 /**
377 * Gets the display address, or null if none.
378 * Interpretation varies by display type.
379 *
380 * @return The display address.
381 * @hide
382 */
383 public String getAddress() {
384 return mAddress;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700385 }
386
387 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700388 * Gets the UID of the application that owns this display, or zero if it is
389 * owned by the system.
390 * <p>
391 * If the display is private, then only the owner can use it.
392 * </p>
393 *
394 * @hide
395 */
396 public int getOwnerUid() {
397 return mOwnerUid;
398 }
399
400 /**
401 * Gets the package name of the application that owns this display, or null if it is
402 * owned by the system.
403 * <p>
404 * If the display is private, then only the owner can use it.
405 * </p>
406 *
407 * @hide
408 */
409 public String getOwnerPackageName() {
410 return mOwnerPackageName;
411 }
412
413 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700414 * Gets the compatibility info used by this display instance.
415 *
Craig Mautner48d0d182013-06-11 07:53:06 -0700416 * @return The display adjustments holder, or null if none is required.
Jeff Brown98365d72012-08-19 20:30:52 -0700417 * @hide
418 */
Craig Mautner48d0d182013-06-11 07:53:06 -0700419 public DisplayAdjustments getDisplayAdjustments() {
420 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -0700421 }
422
423 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700424 * Gets the name of the display.
Jeff Brown92130f62012-10-24 21:28:33 -0700425 * <p>
426 * Note that some displays may be renamed by the user.
427 * </p>
428 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700429 * @return The display's name.
430 */
431 public String getName() {
Jeff Brown92130f62012-10-24 21:28:33 -0700432 synchronized (this) {
433 updateDisplayInfoLocked();
434 return mDisplayInfo.name;
435 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700436 }
437
438 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700439 * Gets the size of the display, in pixels.
440 * <p>
441 * Note that this value should <em>not</em> be used for computing layouts,
442 * since a device will typically have screen decoration (such as a status bar)
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800443 * along the edges of the display that reduce the amount of application
Jeff Brownbc68a592011-07-25 12:58:12 -0700444 * space available from the size returned here. Layouts should instead use
445 * the window size.
446 * </p><p>
447 * The size is adjusted based on the current rotation of the display.
448 * </p><p>
449 * The size returned by this method does not necessarily represent the
450 * actual raw size (native resolution) of the display. The returned size may
Jeff Brownfa25bf52012-07-23 19:26:30 -0700451 * be adjusted to exclude certain system decoration elements that are always visible.
Jeff Brownbc68a592011-07-25 12:58:12 -0700452 * It may also be scaled to provide compatibility with older applications that
453 * were originally designed for smaller displays.
454 * </p>
455 *
456 * @param outSize A {@link Point} object to receive the size information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700458 public void getSize(Point outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700459 synchronized (this) {
460 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700461 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700462 outSize.x = mTempMetrics.widthPixels;
463 outSize.y = mTempMetrics.heightPixels;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700464 }
465 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700468 * Gets the size of the display as a rectangle, in pixels.
469 *
470 * @param outSize A {@link Rect} object to receive the size information.
471 * @see #getSize(Point)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700473 public void getRectSize(Rect outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700474 synchronized (this) {
475 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700476 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700477 outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700478 }
479 }
480
481 /**
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700482 * Return the range of display sizes an application can expect to encounter
483 * under normal operation, as long as there is no physical change in screen
484 * size. This is basically the sizes you will see as the orientation
485 * changes, taking into account whatever screen decoration there is in
486 * each rotation. For example, the status bar is always at the top of the
487 * screen, so it will reduce the height both in landscape and portrait, and
488 * the smallest height returned here will be the smaller of the two.
489 *
490 * This is intended for applications to get an idea of the range of sizes
491 * they will encounter while going through device rotations, to provide a
492 * stable UI through rotation. The sizes here take into account all standard
493 * system decorations that reduce the size actually available to the
494 * application: the status bar, navigation bar, system bar, etc. It does
495 * <em>not</em> take into account more transient elements like an IME
496 * soft keyboard.
497 *
498 * @param outSmallestSize Filled in with the smallest width and height
499 * that the application will encounter, in pixels (not dp units). The x
500 * (width) dimension here directly corresponds to
501 * {@link android.content.res.Configuration#smallestScreenWidthDp
502 * Configuration.smallestScreenWidthDp}, except the value here is in raw
503 * screen pixels rather than dp units. Your application may of course
504 * still get smaller space yet if, for example, a soft keyboard is
505 * being displayed.
506 * @param outLargestSize Filled in with the largest width and height
507 * that the application will encounter, in pixels (not dp units). Your
508 * application may of course still get larger space than this if,
509 * for example, screen decorations like the status bar are being hidden.
510 */
511 public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700512 synchronized (this) {
513 updateDisplayInfoLocked();
514 outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
515 outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
516 outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
517 outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700518 }
519 }
520
521 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700522 * Return the maximum screen size dimension that will happen. This is
523 * mostly for wallpapers.
524 * @hide
525 */
526 public int getMaximumSizeDimension() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700527 synchronized (this) {
528 updateDisplayInfoLocked();
529 return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700530 }
531 }
532
533 /**
534 * @deprecated Use {@link #getSize(Point)} instead.
535 */
536 @Deprecated
537 public int getWidth() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700538 synchronized (this) {
539 updateCachedAppSizeIfNeededLocked();
540 return mCachedAppWidthCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700541 }
542 }
543
544 /**
545 * @deprecated Use {@link #getSize(Point)} instead.
546 */
547 @Deprecated
548 public int getHeight() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700549 synchronized (this) {
550 updateCachedAppSizeIfNeededLocked();
551 return mCachedAppHeightCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700552 }
553 }
554
Jeff Brownbc68a592011-07-25 12:58:12 -0700555 /**
Dianne Hackbornc652de82013-02-15 16:32:56 -0800556 * @hide
557 * Return a rectangle defining the insets of the overscan region of the display.
558 * Each field of the rectangle is the number of pixels the overscan area extends
559 * into the display on that side.
560 */
561 public void getOverscanInsets(Rect outRect) {
562 synchronized (this) {
563 updateDisplayInfoLocked();
564 outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop,
565 mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom);
566 }
567 }
568
569 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800570 * Returns the rotation of the screen from its "natural" orientation.
571 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
572 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
573 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
574 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
575 * example, if a device has a naturally tall screen, and the user has
576 * turned it on its side to go into a landscape orientation, the value
577 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
578 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
579 * the direction it was turned. The angle is the rotation of the drawn
580 * graphics on the screen, which is the opposite direction of the physical
581 * rotation of the device. For example, if the device is rotated 90
582 * degrees counter-clockwise, to compensate rendering will be rotated by
583 * 90 degrees clockwise and thus the returned value here will be
584 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
585 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700586 @Surface.Rotation
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800587 public int getRotation() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700588 synchronized (this) {
589 updateDisplayInfoLocked();
590 return mDisplayInfo.rotation;
591 }
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800592 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700593
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800594 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800595 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @return orientation of this display.
597 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700598 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -0700599 @Surface.Rotation
Jeff Brownfa25bf52012-07-23 19:26:30 -0700600 public int getOrientation() {
601 return getRotation();
602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603
604 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700605 * Gets the pixel format of the display.
606 * @return One of the constants defined in {@link android.graphics.PixelFormat}.
607 *
608 * @deprecated This method is no longer supported.
609 * The result is always {@link PixelFormat#RGBA_8888}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700611 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 public int getPixelFormat() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700613 return PixelFormat.RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700617 * Gets the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 */
619 public float getRefreshRate() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700620 synchronized (this) {
621 updateDisplayInfoLocked();
622 return mDisplayInfo.refreshRate;
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /**
Michael Wright3f145a22014-07-22 19:46:03 -0700627 * Get the supported refresh rates of this display in frames per second.
628 */
629 public float[] getSupportedRefreshRates() {
630 synchronized (this) {
631 updateDisplayInfoLocked();
632 final float[] refreshRates = mDisplayInfo.supportedRefreshRates;
633 return Arrays.copyOf(refreshRates, refreshRates.length);
634 }
635 }
636
637 /**
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700638 * Gets the app VSYNC offset, in nanoseconds. This is a positive value indicating
639 * the phase offset of the VSYNC events provided by Choreographer relative to the
640 * display refresh. For example, if Choreographer reports that the refresh occurred
641 * at time N, it actually occurred at (N - appVsyncOffset).
642 * <p>
643 * Apps generally do not need to be aware of this. It's only useful for fine-grained
644 * A/V synchronization.
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700645 */
646 public long getAppVsyncOffsetNanos() {
647 synchronized (this) {
648 updateDisplayInfoLocked();
649 return mDisplayInfo.appVsyncOffsetNanos;
650 }
651 }
652
653 /**
654 * This is how far in advance a buffer must be queued for presentation at
655 * a given time. If you want a buffer to appear on the screen at
656 * time N, you must submit the buffer before (N - presentationDeadline).
657 * <p>
658 * The desired presentation time for GLES rendering may be set with
659 * {@link android.opengl.EGLExt#eglPresentationTimeANDROID}. For video decoding, use
660 * {@link android.media.MediaCodec#releaseOutputBuffer(int, long)}. Times are
661 * expressed in nanoseconds, using the system monotonic clock
662 * ({@link System#nanoTime}).
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700663 */
664 public long getPresentationDeadlineNanos() {
665 synchronized (this) {
666 updateDisplayInfoLocked();
667 return mDisplayInfo.presentationDeadlineNanos;
668 }
669 }
670
671 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700672 * Gets display metrics that describe the size and density of this display.
673 * <p>
674 * The size is adjusted based on the current rotation of the display.
675 * </p><p>
676 * The size returned by this method does not necessarily represent the
677 * actual raw size (native resolution) of the display. The returned size may
678 * be adjusted to exclude certain system decor elements that are always visible.
679 * It may also be scaled to provide compatibility with older applications that
680 * were originally designed for smaller displays.
681 * </p>
682 *
683 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public void getMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700686 synchronized (this) {
687 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700688 mDisplayInfo.getAppMetrics(outMetrics, mDisplayAdjustments);
Dianne Hackborn68066c22011-04-21 17:26:39 -0700689 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700690 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700691
Jeff Brownfa25bf52012-07-23 19:26:30 -0700692 /**
693 * Gets the real size of the display without subtracting any window decor or
694 * applying any compatibility scale factors.
695 * <p>
696 * The size is adjusted based on the current rotation of the display.
697 * </p><p>
698 * The real size may be smaller than the physical size of the screen when the
699 * window manager is emulating a smaller display (using adb shell am display-size).
700 * </p>
701 *
702 * @param outSize Set to the real size of the display.
703 */
704 public void getRealSize(Point outSize) {
705 synchronized (this) {
706 updateDisplayInfoLocked();
707 outSize.x = mDisplayInfo.logicalWidth;
708 outSize.y = mDisplayInfo.logicalHeight;
709 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700710 }
711
712 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700713 * Gets display metrics based on the real size of this display.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700714 * <p>
715 * The size is adjusted based on the current rotation of the display.
716 * </p><p>
717 * The real size may be smaller than the physical size of the screen when the
718 * window manager is emulating a smaller display (using adb shell am display-size).
719 * </p>
720 *
721 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
Dianne Hackborn68066c22011-04-21 17:26:39 -0700722 */
723 public void getRealMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700724 synchronized (this) {
725 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700726 mDisplayInfo.getLogicalMetrics(outMetrics,
727 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,
Wale Ogunwale7c726682015-02-06 17:34:28 -0800728 mDisplayAdjustments.getConfiguration());
Jeff Brownbc68a592011-07-25 12:58:12 -0700729 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700730 }
731
Jeff Browna506a6e2013-06-04 00:02:38 -0700732 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700733 * Gets the state of the display, such as whether it is on or off.
734 *
735 * @return The state of the display: one of {@link #STATE_OFF}, {@link #STATE_ON},
Jeff Brown5dc21912014-07-17 18:50:18 -0700736 * {@link #STATE_DOZE}, {@link #STATE_DOZE_SUSPEND}, or {@link #STATE_UNKNOWN}.
Jeff Brown037c33e2014-04-09 00:31:55 -0700737 */
738 public int getState() {
739 synchronized (this) {
740 updateDisplayInfoLocked();
741 return mIsValid ? mDisplayInfo.state : STATE_UNKNOWN;
742 }
743 }
744
745 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700746 * Returns true if the specified UID has access to this display.
747 * @hide
748 */
749 public boolean hasAccess(int uid) {
750 return Display.hasAccess(uid, mFlags, mOwnerUid);
751 }
752
753 /** @hide */
754 public static boolean hasAccess(int uid, int flags, int ownerUid) {
755 return (flags & Display.FLAG_PRIVATE) == 0
756 || uid == ownerUid
757 || uid == Process.SYSTEM_UID
758 || uid == 0;
759 }
760
Jeff Brown69b07162013-11-07 00:30:16 -0800761 /**
762 * Returns true if the display is a public presentation display.
763 * @hide
764 */
765 public boolean isPublicPresentation() {
766 return (mFlags & (Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION)) ==
767 Display.FLAG_PRESENTATION;
768 }
769
Jeff Brownfa25bf52012-07-23 19:26:30 -0700770 private void updateDisplayInfoLocked() {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700771 // Note: The display manager caches display info objects on our behalf.
772 DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId);
773 if (newInfo == null) {
774 // Preserve the old mDisplayInfo after the display is removed.
775 if (mIsValid) {
776 mIsValid = false;
777 if (DEBUG) {
778 Log.d(TAG, "Logical display " + mDisplayId + " was removed.");
779 }
780 }
781 } else {
782 // Use the new display info. (It might be the same object if nothing changed.)
783 mDisplayInfo = newInfo;
784 if (!mIsValid) {
785 mIsValid = true;
786 if (DEBUG) {
787 Log.d(TAG, "Logical display " + mDisplayId + " was recreated.");
788 }
789 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700790 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700791 }
792
Jeff Brownfa25bf52012-07-23 19:26:30 -0700793 private void updateCachedAppSizeIfNeededLocked() {
794 long now = SystemClock.uptimeMillis();
795 if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
796 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700797 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700798 mCachedAppWidthCompat = mTempMetrics.widthPixels;
799 mCachedAppHeightCompat = mTempMetrics.heightPixels;
800 mLastCachedAppSizeUpdate = now;
801 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700802 }
Jeff Brownbf5740e2012-08-19 23:20:02 -0700803
804 // For debugging purposes
805 @Override
806 public String toString() {
807 synchronized (this) {
808 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700809 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownbf5740e2012-08-19 23:20:02 -0700810 return "Display id " + mDisplayId + ": " + mDisplayInfo
Jeff Brownbd6e1502012-08-28 03:27:37 -0700811 + ", " + mTempMetrics + ", isValid=" + mIsValid;
Jeff Brownbf5740e2012-08-19 23:20:02 -0700812 }
813 }
Jeff Brown92130f62012-10-24 21:28:33 -0700814
815 /**
816 * @hide
817 */
818 public static String typeToString(int type) {
819 switch (type) {
820 case TYPE_UNKNOWN:
821 return "UNKNOWN";
822 case TYPE_BUILT_IN:
823 return "BUILT_IN";
824 case TYPE_HDMI:
825 return "HDMI";
826 case TYPE_WIFI:
827 return "WIFI";
828 case TYPE_OVERLAY:
829 return "OVERLAY";
Jeff Browna506a6e2013-06-04 00:02:38 -0700830 case TYPE_VIRTUAL:
831 return "VIRTUAL";
Jeff Brown92130f62012-10-24 21:28:33 -0700832 default:
833 return Integer.toString(type);
834 }
835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
Jeff Brown037c33e2014-04-09 00:31:55 -0700837 /**
838 * @hide
839 */
840 public static String stateToString(int state) {
841 switch (state) {
842 case STATE_UNKNOWN:
843 return "UNKNOWN";
844 case STATE_OFF:
845 return "OFF";
846 case STATE_ON:
847 return "ON";
Jeff Brown5dc21912014-07-17 18:50:18 -0700848 case STATE_DOZE:
849 return "DOZE";
850 case STATE_DOZE_SUSPEND:
851 return "DOZE_SUSPEND";
Jeff Brown037c33e2014-04-09 00:31:55 -0700852 default:
853 return Integer.toString(state);
854 }
855 }
Jeff Brown970d4132014-07-19 11:33:47 -0700856
857 /**
858 * Returns true if display updates may be suspended while in the specified
859 * display power state.
860 * @hide
861 */
862 public static boolean isSuspendedState(int state) {
863 return state == STATE_OFF || state == STATE_DOZE_SUSPEND;
864 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700865}