blob: 71863b7022e850e08d959476d71d382a069f65e4 [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 Brown92130f62012-10-24 21:28:33 -0700173 * Display type: Unknown display type.
174 * @hide
175 */
176 public static final int TYPE_UNKNOWN = 0;
177
178 /**
179 * Display type: Built-in display.
180 * @hide
181 */
182 public static final int TYPE_BUILT_IN = 1;
183
184 /**
185 * Display type: HDMI display.
186 * @hide
187 */
188 public static final int TYPE_HDMI = 2;
189
190 /**
191 * Display type: WiFi display.
192 * @hide
193 */
194 public static final int TYPE_WIFI = 3;
195
196 /**
197 * Display type: Overlay display.
198 * @hide
199 */
200 public static final int TYPE_OVERLAY = 4;
201
202 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700203 * Display type: Virtual display.
204 * @hide
205 */
206 public static final int TYPE_VIRTUAL = 5;
207
208 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700209 * Display state: The display state is unknown.
210 *
211 * @see #getState
212 */
213 public static final int STATE_UNKNOWN = 0;
214
215 /**
216 * Display state: The display is off.
217 *
218 * @see #getState
219 */
220 public static final int STATE_OFF = 1;
221
222 /**
223 * Display state: The display is on.
224 *
225 * @see #getState
226 */
227 public static final int STATE_ON = 2;
228
229 /**
Jeff Brown5dc21912014-07-17 18:50:18 -0700230 * Display state: The display is dozing in a low power state; it is still
231 * on but is optimized for showing system-provided content while the
232 * device is non-interactive.
Jeff Brown037c33e2014-04-09 00:31:55 -0700233 *
234 * @see #getState
235 * @see android.os.PowerManager#isInteractive
236 */
Jeff Brown5dc21912014-07-17 18:50:18 -0700237 public static final int STATE_DOZE = 3;
238
239 /**
240 * Display state: The display is dozing in a suspended low power state; it is still
241 * on but is optimized for showing static system-provided content while the device
242 * is non-interactive. This mode may be used to conserve even more power by allowing
243 * the hardware to stop applying frame buffer updates from the graphics subsystem or
244 * to take over the display and manage it autonomously to implement low power always-on
245 * display functionality.
246 *
247 * @see #getState
248 * @see android.os.PowerManager#isInteractive
249 */
250 public static final int STATE_DOZE_SUSPEND = 4;
Jeff Brown037c33e2014-04-09 00:31:55 -0700251
252 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700253 * Internal method to create a display.
254 * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
Jeff Brown98365d72012-08-19 20:30:52 -0700255 * or {@link android.hardware.display.DisplayManager#getDisplay}
256 * to get a display object.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700257 *
258 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700260 public Display(DisplayManagerGlobal global,
261 int displayId, DisplayInfo displayInfo /*not null*/,
Craig Mautner48d0d182013-06-11 07:53:06 -0700262 DisplayAdjustments daj) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700263 mGlobal = global;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700264 mDisplayId = displayId;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700265 mDisplayInfo = displayInfo;
Craig Mautner1abaa532013-06-28 12:03:32 -0700266 mDisplayAdjustments = new DisplayAdjustments(daj);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700267 mIsValid = true;
Jeff Brown92130f62012-10-24 21:28:33 -0700268
269 // Cache properties that cannot change as long as the display is valid.
270 mLayerStack = displayInfo.layerStack;
271 mFlags = displayInfo.flags;
272 mType = displayInfo.type;
273 mAddress = displayInfo.address;
Jeff Browna506a6e2013-06-04 00:02:38 -0700274 mOwnerUid = displayInfo.ownerUid;
275 mOwnerPackageName = displayInfo.ownerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700279 * Gets the display id.
280 * <p>
281 * Each logical display has a unique id.
282 * The default display has id {@link #DEFAULT_DISPLAY}.
283 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
285 public int getDisplayId() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700286 return mDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288
289 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700290 * Returns true if this display is still valid, false if the display has been removed.
291 *
292 * If the display is invalid, then the methods of this class will
293 * continue to report the most recently observed display information.
294 * However, it is unwise (and rather fruitless) to continue using a
295 * {@link Display} object after the display's demise.
296 *
297 * It's possible for a display that was previously invalid to become
298 * valid again if a display with the same id is reconnected.
299 *
300 * @return True if the display is still valid.
301 */
302 public boolean isValid() {
303 synchronized (this) {
304 updateDisplayInfoLocked();
305 return mIsValid;
306 }
307 }
308
309 /**
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700310 * Gets a full copy of the display information.
311 *
312 * @param outDisplayInfo The object to receive the copy of the display information.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700313 * @return True if the display is still valid.
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700314 * @hide
315 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700316 public boolean getDisplayInfo(DisplayInfo outDisplayInfo) {
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700317 synchronized (this) {
318 updateDisplayInfoLocked();
319 outDisplayInfo.copyFrom(mDisplayInfo);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700320 return mIsValid;
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700321 }
322 }
323
324 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700325 * Gets the display's layer stack.
326 *
327 * Each display has its own independent layer stack upon which surfaces
328 * are placed to be managed by surface flinger.
329 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700330 * @return The display's layer stack number.
Jeff Brown98365d72012-08-19 20:30:52 -0700331 * @hide
332 */
333 public int getLayerStack() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700334 return mLayerStack;
Jeff Brown98365d72012-08-19 20:30:52 -0700335 }
336
337 /**
Jeff Brownc5df37c2012-09-13 11:45:07 -0700338 * Returns a combination of flags that describe the capabilities of the display.
339 *
340 * @return The display flags.
341 *
Jeff Brown77aebfd2012-10-01 21:07:03 -0700342 * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS
Jeff Brownf0681b32012-10-23 17:35:57 -0700343 * @see #FLAG_SECURE
Jeff Browna506a6e2013-06-04 00:02:38 -0700344 * @see #FLAG_PRIVATE
Jeff Brownc5df37c2012-09-13 11:45:07 -0700345 */
346 public int getFlags() {
Jeff Brown92130f62012-10-24 21:28:33 -0700347 return mFlags;
348 }
349
350 /**
351 * Gets the display type.
352 *
353 * @return The display type.
354 *
355 * @see #TYPE_UNKNOWN
356 * @see #TYPE_BUILT_IN
357 * @see #TYPE_HDMI
358 * @see #TYPE_WIFI
359 * @see #TYPE_OVERLAY
Jeff Browna506a6e2013-06-04 00:02:38 -0700360 * @see #TYPE_VIRTUAL
Jeff Brown92130f62012-10-24 21:28:33 -0700361 * @hide
362 */
363 public int getType() {
364 return mType;
365 }
366
367 /**
368 * Gets the display address, or null if none.
369 * Interpretation varies by display type.
370 *
371 * @return The display address.
372 * @hide
373 */
374 public String getAddress() {
375 return mAddress;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700376 }
377
378 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700379 * Gets the UID of the application that owns this display, or zero if it is
380 * owned by the system.
381 * <p>
382 * If the display is private, then only the owner can use it.
383 * </p>
384 *
385 * @hide
386 */
387 public int getOwnerUid() {
388 return mOwnerUid;
389 }
390
391 /**
392 * Gets the package name of the application that owns this display, or null if it is
393 * owned by the system.
394 * <p>
395 * If the display is private, then only the owner can use it.
396 * </p>
397 *
398 * @hide
399 */
400 public String getOwnerPackageName() {
401 return mOwnerPackageName;
402 }
403
404 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700405 * Gets the compatibility info used by this display instance.
406 *
Craig Mautner48d0d182013-06-11 07:53:06 -0700407 * @return The display adjustments holder, or null if none is required.
Jeff Brown98365d72012-08-19 20:30:52 -0700408 * @hide
409 */
Craig Mautner48d0d182013-06-11 07:53:06 -0700410 public DisplayAdjustments getDisplayAdjustments() {
411 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -0700412 }
413
414 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700415 * Gets the name of the display.
Jeff Brown92130f62012-10-24 21:28:33 -0700416 * <p>
417 * Note that some displays may be renamed by the user.
418 * </p>
419 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700420 * @return The display's name.
421 */
422 public String getName() {
Jeff Brown92130f62012-10-24 21:28:33 -0700423 synchronized (this) {
424 updateDisplayInfoLocked();
425 return mDisplayInfo.name;
426 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700427 }
428
429 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700430 * Gets the size of the display, in pixels.
431 * <p>
432 * Note that this value should <em>not</em> be used for computing layouts,
433 * since a device will typically have screen decoration (such as a status bar)
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800434 * along the edges of the display that reduce the amount of application
Jeff Brownbc68a592011-07-25 12:58:12 -0700435 * space available from the size returned here. Layouts should instead use
436 * the window size.
437 * </p><p>
438 * The size is adjusted based on the current rotation of the display.
439 * </p><p>
440 * The size returned by this method does not necessarily represent the
441 * actual raw size (native resolution) of the display. The returned size may
Jeff Brownfa25bf52012-07-23 19:26:30 -0700442 * be adjusted to exclude certain system decoration elements that are always visible.
Jeff Brownbc68a592011-07-25 12:58:12 -0700443 * It may also be scaled to provide compatibility with older applications that
444 * were originally designed for smaller displays.
445 * </p>
446 *
447 * @param outSize A {@link Point} object to receive the size information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700449 public void getSize(Point outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700450 synchronized (this) {
451 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700452 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700453 outSize.x = mTempMetrics.widthPixels;
454 outSize.y = mTempMetrics.heightPixels;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700455 }
456 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700459 * Gets the size of the display as a rectangle, in pixels.
460 *
461 * @param outSize A {@link Rect} object to receive the size information.
462 * @see #getSize(Point)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700464 public void getRectSize(Rect outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700465 synchronized (this) {
466 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700467 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700468 outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700469 }
470 }
471
472 /**
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700473 * Return the range of display sizes an application can expect to encounter
474 * under normal operation, as long as there is no physical change in screen
475 * size. This is basically the sizes you will see as the orientation
476 * changes, taking into account whatever screen decoration there is in
477 * each rotation. For example, the status bar is always at the top of the
478 * screen, so it will reduce the height both in landscape and portrait, and
479 * the smallest height returned here will be the smaller of the two.
480 *
481 * This is intended for applications to get an idea of the range of sizes
482 * they will encounter while going through device rotations, to provide a
483 * stable UI through rotation. The sizes here take into account all standard
484 * system decorations that reduce the size actually available to the
485 * application: the status bar, navigation bar, system bar, etc. It does
486 * <em>not</em> take into account more transient elements like an IME
487 * soft keyboard.
488 *
489 * @param outSmallestSize Filled in with the smallest width and height
490 * that the application will encounter, in pixels (not dp units). The x
491 * (width) dimension here directly corresponds to
492 * {@link android.content.res.Configuration#smallestScreenWidthDp
493 * Configuration.smallestScreenWidthDp}, except the value here is in raw
494 * screen pixels rather than dp units. Your application may of course
495 * still get smaller space yet if, for example, a soft keyboard is
496 * being displayed.
497 * @param outLargestSize Filled in with the largest width and height
498 * that the application will encounter, in pixels (not dp units). Your
499 * application may of course still get larger space than this if,
500 * for example, screen decorations like the status bar are being hidden.
501 */
502 public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700503 synchronized (this) {
504 updateDisplayInfoLocked();
505 outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
506 outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
507 outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
508 outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700509 }
510 }
511
512 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700513 * Return the maximum screen size dimension that will happen. This is
514 * mostly for wallpapers.
515 * @hide
516 */
517 public int getMaximumSizeDimension() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700518 synchronized (this) {
519 updateDisplayInfoLocked();
520 return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700521 }
522 }
523
524 /**
525 * @deprecated Use {@link #getSize(Point)} instead.
526 */
527 @Deprecated
528 public int getWidth() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700529 synchronized (this) {
530 updateCachedAppSizeIfNeededLocked();
531 return mCachedAppWidthCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700532 }
533 }
534
535 /**
536 * @deprecated Use {@link #getSize(Point)} instead.
537 */
538 @Deprecated
539 public int getHeight() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700540 synchronized (this) {
541 updateCachedAppSizeIfNeededLocked();
542 return mCachedAppHeightCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700543 }
544 }
545
Jeff Brownbc68a592011-07-25 12:58:12 -0700546 /**
Dianne Hackbornc652de82013-02-15 16:32:56 -0800547 * @hide
548 * Return a rectangle defining the insets of the overscan region of the display.
549 * Each field of the rectangle is the number of pixels the overscan area extends
550 * into the display on that side.
551 */
552 public void getOverscanInsets(Rect outRect) {
553 synchronized (this) {
554 updateDisplayInfoLocked();
555 outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop,
556 mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom);
557 }
558 }
559
560 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800561 * Returns the rotation of the screen from its "natural" orientation.
562 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
563 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
564 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
565 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
566 * example, if a device has a naturally tall screen, and the user has
567 * turned it on its side to go into a landscape orientation, the value
568 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
569 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
570 * the direction it was turned. The angle is the rotation of the drawn
571 * graphics on the screen, which is the opposite direction of the physical
572 * rotation of the device. For example, if the device is rotated 90
573 * degrees counter-clockwise, to compensate rendering will be rotated by
574 * 90 degrees clockwise and thus the returned value here will be
575 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
576 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700577 @Surface.Rotation
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800578 public int getRotation() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700579 synchronized (this) {
580 updateDisplayInfoLocked();
581 return mDisplayInfo.rotation;
582 }
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800583 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700584
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800585 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800586 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * @return orientation of this display.
588 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700589 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -0700590 @Surface.Rotation
Jeff Brownfa25bf52012-07-23 19:26:30 -0700591 public int getOrientation() {
592 return getRotation();
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
595 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700596 * Gets the pixel format of the display.
597 * @return One of the constants defined in {@link android.graphics.PixelFormat}.
598 *
599 * @deprecated This method is no longer supported.
600 * The result is always {@link PixelFormat#RGBA_8888}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700602 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 public int getPixelFormat() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700604 return PixelFormat.RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700608 * Gets the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 */
610 public float getRefreshRate() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700611 synchronized (this) {
612 updateDisplayInfoLocked();
613 return mDisplayInfo.refreshRate;
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 /**
Michael Wright3f145a22014-07-22 19:46:03 -0700618 * Get the supported refresh rates of this display in frames per second.
619 */
620 public float[] getSupportedRefreshRates() {
621 synchronized (this) {
622 updateDisplayInfoLocked();
623 final float[] refreshRates = mDisplayInfo.supportedRefreshRates;
624 return Arrays.copyOf(refreshRates, refreshRates.length);
625 }
626 }
627
628 /**
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700629 * Gets the app VSYNC offset, in nanoseconds. This is a positive value indicating
630 * the phase offset of the VSYNC events provided by Choreographer relative to the
631 * display refresh. For example, if Choreographer reports that the refresh occurred
632 * at time N, it actually occurred at (N - appVsyncOffset).
633 * <p>
634 * Apps generally do not need to be aware of this. It's only useful for fine-grained
635 * A/V synchronization.
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700636 */
637 public long getAppVsyncOffsetNanos() {
638 synchronized (this) {
639 updateDisplayInfoLocked();
640 return mDisplayInfo.appVsyncOffsetNanos;
641 }
642 }
643
644 /**
645 * This is how far in advance a buffer must be queued for presentation at
646 * a given time. If you want a buffer to appear on the screen at
647 * time N, you must submit the buffer before (N - presentationDeadline).
648 * <p>
649 * The desired presentation time for GLES rendering may be set with
650 * {@link android.opengl.EGLExt#eglPresentationTimeANDROID}. For video decoding, use
651 * {@link android.media.MediaCodec#releaseOutputBuffer(int, long)}. Times are
652 * expressed in nanoseconds, using the system monotonic clock
653 * ({@link System#nanoTime}).
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700654 */
655 public long getPresentationDeadlineNanos() {
656 synchronized (this) {
657 updateDisplayInfoLocked();
658 return mDisplayInfo.presentationDeadlineNanos;
659 }
660 }
661
662 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700663 * Gets display metrics that describe the size and density of this display.
664 * <p>
665 * The size is adjusted based on the current rotation of the display.
666 * </p><p>
667 * The size returned by this method does not necessarily represent the
668 * actual raw size (native resolution) of the display. The returned size may
669 * be adjusted to exclude certain system decor elements that are always visible.
670 * It may also be scaled to provide compatibility with older applications that
671 * were originally designed for smaller displays.
672 * </p>
673 *
674 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public void getMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700677 synchronized (this) {
678 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700679 mDisplayInfo.getAppMetrics(outMetrics, mDisplayAdjustments);
Dianne Hackborn68066c22011-04-21 17:26:39 -0700680 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700681 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700682
Jeff Brownfa25bf52012-07-23 19:26:30 -0700683 /**
684 * Gets the real size of the display without subtracting any window decor or
685 * applying any compatibility scale factors.
686 * <p>
687 * The size is adjusted based on the current rotation of the display.
688 * </p><p>
689 * The real size may be smaller than the physical size of the screen when the
690 * window manager is emulating a smaller display (using adb shell am display-size).
691 * </p>
692 *
693 * @param outSize Set to the real size of the display.
694 */
695 public void getRealSize(Point outSize) {
696 synchronized (this) {
697 updateDisplayInfoLocked();
698 outSize.x = mDisplayInfo.logicalWidth;
699 outSize.y = mDisplayInfo.logicalHeight;
700 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700701 }
702
703 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700704 * Gets display metrics based on the real size of this display.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700705 * <p>
706 * The size is adjusted based on the current rotation of the display.
707 * </p><p>
708 * The real size may be smaller than the physical size of the screen when the
709 * window manager is emulating a smaller display (using adb shell am display-size).
710 * </p>
711 *
712 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
Dianne Hackborn68066c22011-04-21 17:26:39 -0700713 */
714 public void getRealMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700715 synchronized (this) {
716 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700717 mDisplayInfo.getLogicalMetrics(outMetrics,
718 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,
Wale Ogunwale7c726682015-02-06 17:34:28 -0800719 mDisplayAdjustments.getConfiguration());
Jeff Brownbc68a592011-07-25 12:58:12 -0700720 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700721 }
722
Jeff Browna506a6e2013-06-04 00:02:38 -0700723 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700724 * Gets the state of the display, such as whether it is on or off.
725 *
726 * @return The state of the display: one of {@link #STATE_OFF}, {@link #STATE_ON},
Jeff Brown5dc21912014-07-17 18:50:18 -0700727 * {@link #STATE_DOZE}, {@link #STATE_DOZE_SUSPEND}, or {@link #STATE_UNKNOWN}.
Jeff Brown037c33e2014-04-09 00:31:55 -0700728 */
729 public int getState() {
730 synchronized (this) {
731 updateDisplayInfoLocked();
732 return mIsValid ? mDisplayInfo.state : STATE_UNKNOWN;
733 }
734 }
735
736 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700737 * Returns true if the specified UID has access to this display.
738 * @hide
739 */
740 public boolean hasAccess(int uid) {
741 return Display.hasAccess(uid, mFlags, mOwnerUid);
742 }
743
744 /** @hide */
745 public static boolean hasAccess(int uid, int flags, int ownerUid) {
746 return (flags & Display.FLAG_PRIVATE) == 0
747 || uid == ownerUid
748 || uid == Process.SYSTEM_UID
749 || uid == 0;
750 }
751
Jeff Brown69b07162013-11-07 00:30:16 -0800752 /**
753 * Returns true if the display is a public presentation display.
754 * @hide
755 */
756 public boolean isPublicPresentation() {
757 return (mFlags & (Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION)) ==
758 Display.FLAG_PRESENTATION;
759 }
760
Jeff Brownfa25bf52012-07-23 19:26:30 -0700761 private void updateDisplayInfoLocked() {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700762 // Note: The display manager caches display info objects on our behalf.
763 DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId);
764 if (newInfo == null) {
765 // Preserve the old mDisplayInfo after the display is removed.
766 if (mIsValid) {
767 mIsValid = false;
768 if (DEBUG) {
769 Log.d(TAG, "Logical display " + mDisplayId + " was removed.");
770 }
771 }
772 } else {
773 // Use the new display info. (It might be the same object if nothing changed.)
774 mDisplayInfo = newInfo;
775 if (!mIsValid) {
776 mIsValid = true;
777 if (DEBUG) {
778 Log.d(TAG, "Logical display " + mDisplayId + " was recreated.");
779 }
780 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700781 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700782 }
783
Jeff Brownfa25bf52012-07-23 19:26:30 -0700784 private void updateCachedAppSizeIfNeededLocked() {
785 long now = SystemClock.uptimeMillis();
786 if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
787 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700788 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700789 mCachedAppWidthCompat = mTempMetrics.widthPixels;
790 mCachedAppHeightCompat = mTempMetrics.heightPixels;
791 mLastCachedAppSizeUpdate = now;
792 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700793 }
Jeff Brownbf5740e2012-08-19 23:20:02 -0700794
795 // For debugging purposes
796 @Override
797 public String toString() {
798 synchronized (this) {
799 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700800 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownbf5740e2012-08-19 23:20:02 -0700801 return "Display id " + mDisplayId + ": " + mDisplayInfo
Jeff Brownbd6e1502012-08-28 03:27:37 -0700802 + ", " + mTempMetrics + ", isValid=" + mIsValid;
Jeff Brownbf5740e2012-08-19 23:20:02 -0700803 }
804 }
Jeff Brown92130f62012-10-24 21:28:33 -0700805
806 /**
807 * @hide
808 */
809 public static String typeToString(int type) {
810 switch (type) {
811 case TYPE_UNKNOWN:
812 return "UNKNOWN";
813 case TYPE_BUILT_IN:
814 return "BUILT_IN";
815 case TYPE_HDMI:
816 return "HDMI";
817 case TYPE_WIFI:
818 return "WIFI";
819 case TYPE_OVERLAY:
820 return "OVERLAY";
Jeff Browna506a6e2013-06-04 00:02:38 -0700821 case TYPE_VIRTUAL:
822 return "VIRTUAL";
Jeff Brown92130f62012-10-24 21:28:33 -0700823 default:
824 return Integer.toString(type);
825 }
826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
Jeff Brown037c33e2014-04-09 00:31:55 -0700828 /**
829 * @hide
830 */
831 public static String stateToString(int state) {
832 switch (state) {
833 case STATE_UNKNOWN:
834 return "UNKNOWN";
835 case STATE_OFF:
836 return "OFF";
837 case STATE_ON:
838 return "ON";
Jeff Brown5dc21912014-07-17 18:50:18 -0700839 case STATE_DOZE:
840 return "DOZE";
841 case STATE_DOZE_SUSPEND:
842 return "DOZE_SUSPEND";
Jeff Brown037c33e2014-04-09 00:31:55 -0700843 default:
844 return Integer.toString(state);
845 }
846 }
Jeff Brown970d4132014-07-19 11:33:47 -0700847
848 /**
849 * Returns true if display updates may be suspended while in the specified
850 * display power state.
851 * @hide
852 */
853 public static boolean isSuspendedState(int state) {
854 return state == STATE_OFF || state == STATE_DOZE_SUSPEND;
855 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700856}