blob: 5f614b150ad85b4b4efd0c7a062660676d2b7e88 [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
Jeff Brownbc68a592011-07-25 12:58:12 -070029/**
Jeff Brownfa25bf52012-07-23 19:26:30 -070030 * Provides information about the size and density of a logical display.
31 * <p>
32 * The display area is described in two different ways.
33 * <ul>
34 * <li>The application display area specifies the part of the display that may contain
35 * an application window, excluding the system decorations. The application display area may
36 * be smaller than the real display area because the system subtracts the space needed
37 * for decor elements such as the status bar. Use the following methods to query the
38 * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li>
39 * <li>The real display area specifies the part of the display that contains content
40 * including the system decorations. Even so, the real display area may be smaller than the
41 * physical size of the display if the window manager is emulating a smaller display
42 * using (adb shell am display-size). Use the following methods to query the
43 * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li>
44 * </ul>
45 * </p><p>
46 * A logical display does not necessarily represent a particular physical display device
47 * such as the built-in screen or an external monitor. The contents of a logical
48 * display may be presented on one or more physical displays according to the devices
49 * that are currently attached and whether mirroring has been enabled.
50 * </p>
Jeff Brownbc68a592011-07-25 12:58:12 -070051 */
Jeff Brownfa25bf52012-07-23 19:26:30 -070052public final class Display {
53 private static final String TAG = "Display";
Jeff Brownbd6e1502012-08-28 03:27:37 -070054 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -070055
Jeff Brownbd6e1502012-08-28 03:27:37 -070056 private final DisplayManagerGlobal mGlobal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070057 private final int mDisplayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070058 private final int mLayerStack;
Jeff Brown92130f62012-10-24 21:28:33 -070059 private final int mFlags;
60 private final int mType;
61 private final String mAddress;
Jeff Browna506a6e2013-06-04 00:02:38 -070062 private final int mOwnerUid;
63 private final String mOwnerPackageName;
Craig Mautner48d0d182013-06-11 07:53:06 -070064 private final DisplayAdjustments mDisplayAdjustments;
Jeff Brownbd6e1502012-08-28 03:27:37 -070065
66 private DisplayInfo mDisplayInfo; // never null
67 private boolean mIsValid;
Jeff Brownfa25bf52012-07-23 19:26:30 -070068
69 // Temporary display metrics structure used for compatibility mode.
70 private final DisplayMetrics mTempMetrics = new DisplayMetrics();
71
72 // We cache the app width and height properties briefly between calls
73 // to getHeight() and getWidth() to ensure that applications perceive
74 // consistent results when the size changes (most of the time).
75 // Applications should now be using getSize() instead.
76 private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20;
77 private long mLastCachedAppSizeUpdate;
78 private int mCachedAppWidthCompat;
79 private int mCachedAppHeightCompat;
Dianne Hackborn5fd21692011-06-07 14:09:47 -070080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 /**
Jeff Brown98365d72012-08-19 20:30:52 -070082 * The default Display id, which is the id of the built-in primary display
83 * assuming there is one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85 public static final int DEFAULT_DISPLAY = 0;
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
Jeff Brown77aebfd2012-10-01 21:07:03 -070088 * Display flag: Indicates that the display supports compositing content
89 * that is stored in protected graphics buffers.
Jeff Brownc5df37c2012-09-13 11:45:07 -070090 * <p>
Jeff Brownf0681b32012-10-23 17:35:57 -070091 * If this flag is set then the display device supports compositing protected buffers.
92 * </p><p>
93 * If this flag is not set then the display device may not support compositing
94 * protected buffers; the user may see a blank region on the screen instead of
95 * the protected content.
96 * </p><p>
Jeff Brown77aebfd2012-10-01 21:07:03 -070097 * Secure (DRM) video decoders may allocate protected graphics buffers to request that
98 * a hardware-protected path be provided between the video decoder and the external
99 * display sink. If a hardware-protected path is not available, then content stored
100 * in protected graphics buffers may not be composited.
Jeff Brownc5df37c2012-09-13 11:45:07 -0700101 * </p><p>
Jeff Brownf0681b32012-10-23 17:35:57 -0700102 * An application can use the absence of this flag as a hint that it should not use protected
103 * buffers for this display because the content may not be visible. For example,
104 * if the flag is not set then the application may choose not to show content on this
105 * display, show an informative error message, select an alternate content stream
106 * or adopt a different strategy for decoding content that does not rely on
107 * protected buffers.
Jeff Brownc5df37c2012-09-13 11:45:07 -0700108 * </p>
Jeff Brownf0681b32012-10-23 17:35:57 -0700109 *
110 * @see #getFlags
Jeff Brownc5df37c2012-09-13 11:45:07 -0700111 */
Jeff Brown77aebfd2012-10-01 21:07:03 -0700112 public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 0;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700113
114 /**
Jeff Brownf0681b32012-10-23 17:35:57 -0700115 * Display flag: Indicates that the display has a secure video output and
116 * supports compositing secure surfaces.
117 * <p>
118 * If this flag is set then the display device has a secure video output
119 * and is capable of showing secure surfaces. It may also be capable of
120 * showing {@link #FLAG_SUPPORTS_PROTECTED_BUFFERS protected buffers}.
121 * </p><p>
122 * If this flag is not set then the display device may not have a secure video
123 * output; the user may see a blank region on the screen instead of
124 * the contents of secure surfaces or protected buffers.
125 * </p><p>
126 * Secure surfaces are used to prevent content rendered into those surfaces
127 * by applications from appearing in screenshots or from being viewed
128 * on non-secure displays. Protected buffers are used by secure video decoders
129 * for a similar purpose.
130 * </p><p>
131 * An application creates a window with a secure surface by specifying the
132 * {@link WindowManager.LayoutParams#FLAG_SECURE} window flag.
133 * Likewise, an application creates a {@link SurfaceView} with a secure surface
134 * by calling {@link SurfaceView#setSecure} before attaching the secure view to
135 * its containing window.
136 * </p><p>
137 * An application can use the absence of this flag as a hint that it should not create
138 * secure surfaces or protected buffers on this display because the content may
139 * not be visible. For example, if the flag is not set then the application may
140 * choose not to show content on this display, show an informative error message,
141 * select an alternate content stream or adopt a different strategy for decoding
142 * content that does not rely on secure surfaces or protected buffers.
143 * </p>
144 *
145 * @see #getFlags
146 */
147 public static final int FLAG_SECURE = 1 << 1;
148
149 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700150 * Display flag: Indicates that the display is private. Only the application that
151 * owns the display can create windows on it.
152 * <p>
153 * This flag is associated with displays that were created using
154 * {@link android.hardware.display.DisplayManager#createPrivateVirtualDisplay}.
155 * </p>
156 *
157 * @see #getFlags
158 */
159 public static final int FLAG_PRIVATE = 1 << 2;
160
161 /**
Jeff Brown92130f62012-10-24 21:28:33 -0700162 * Display type: Unknown display type.
163 * @hide
164 */
165 public static final int TYPE_UNKNOWN = 0;
166
167 /**
168 * Display type: Built-in display.
169 * @hide
170 */
171 public static final int TYPE_BUILT_IN = 1;
172
173 /**
174 * Display type: HDMI display.
175 * @hide
176 */
177 public static final int TYPE_HDMI = 2;
178
179 /**
180 * Display type: WiFi display.
181 * @hide
182 */
183 public static final int TYPE_WIFI = 3;
184
185 /**
186 * Display type: Overlay display.
187 * @hide
188 */
189 public static final int TYPE_OVERLAY = 4;
190
191 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700192 * Display type: Virtual display.
193 * @hide
194 */
195 public static final int TYPE_VIRTUAL = 5;
196
197 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700198 * Internal method to create a display.
199 * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
Jeff Brown98365d72012-08-19 20:30:52 -0700200 * or {@link android.hardware.display.DisplayManager#getDisplay}
201 * to get a display object.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700202 *
203 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700205 public Display(DisplayManagerGlobal global,
206 int displayId, DisplayInfo displayInfo /*not null*/,
Craig Mautner48d0d182013-06-11 07:53:06 -0700207 DisplayAdjustments daj) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700208 mGlobal = global;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700209 mDisplayId = displayId;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700210 mDisplayInfo = displayInfo;
Craig Mautner1abaa532013-06-28 12:03:32 -0700211 mDisplayAdjustments = new DisplayAdjustments(daj);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700212 mIsValid = true;
Jeff Brown92130f62012-10-24 21:28:33 -0700213
214 // Cache properties that cannot change as long as the display is valid.
215 mLayerStack = displayInfo.layerStack;
216 mFlags = displayInfo.flags;
217 mType = displayInfo.type;
218 mAddress = displayInfo.address;
Jeff Browna506a6e2013-06-04 00:02:38 -0700219 mOwnerUid = displayInfo.ownerUid;
220 mOwnerPackageName = displayInfo.ownerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700224 * Gets the display id.
225 * <p>
226 * Each logical display has a unique id.
227 * The default display has id {@link #DEFAULT_DISPLAY}.
228 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
230 public int getDisplayId() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700231 return mDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233
234 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700235 * Returns true if this display is still valid, false if the display has been removed.
236 *
237 * If the display is invalid, then the methods of this class will
238 * continue to report the most recently observed display information.
239 * However, it is unwise (and rather fruitless) to continue using a
240 * {@link Display} object after the display's demise.
241 *
242 * It's possible for a display that was previously invalid to become
243 * valid again if a display with the same id is reconnected.
244 *
245 * @return True if the display is still valid.
246 */
247 public boolean isValid() {
248 synchronized (this) {
249 updateDisplayInfoLocked();
250 return mIsValid;
251 }
252 }
253
254 /**
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700255 * Gets a full copy of the display information.
256 *
257 * @param outDisplayInfo The object to receive the copy of the display information.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700258 * @return True if the display is still valid.
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700259 * @hide
260 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700261 public boolean getDisplayInfo(DisplayInfo outDisplayInfo) {
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700262 synchronized (this) {
263 updateDisplayInfoLocked();
264 outDisplayInfo.copyFrom(mDisplayInfo);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700265 return mIsValid;
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700266 }
267 }
268
269 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700270 * Gets the display's layer stack.
271 *
272 * Each display has its own independent layer stack upon which surfaces
273 * are placed to be managed by surface flinger.
274 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700275 * @return The display's layer stack number.
Jeff Brown98365d72012-08-19 20:30:52 -0700276 * @hide
277 */
278 public int getLayerStack() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700279 return mLayerStack;
Jeff Brown98365d72012-08-19 20:30:52 -0700280 }
281
282 /**
Jeff Brownc5df37c2012-09-13 11:45:07 -0700283 * Returns a combination of flags that describe the capabilities of the display.
284 *
285 * @return The display flags.
286 *
Jeff Brown77aebfd2012-10-01 21:07:03 -0700287 * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS
Jeff Brownf0681b32012-10-23 17:35:57 -0700288 * @see #FLAG_SECURE
Jeff Browna506a6e2013-06-04 00:02:38 -0700289 * @see #FLAG_PRIVATE
Jeff Brownc5df37c2012-09-13 11:45:07 -0700290 */
291 public int getFlags() {
Jeff Brown92130f62012-10-24 21:28:33 -0700292 return mFlags;
293 }
294
295 /**
296 * Gets the display type.
297 *
298 * @return The display type.
299 *
300 * @see #TYPE_UNKNOWN
301 * @see #TYPE_BUILT_IN
302 * @see #TYPE_HDMI
303 * @see #TYPE_WIFI
304 * @see #TYPE_OVERLAY
Jeff Browna506a6e2013-06-04 00:02:38 -0700305 * @see #TYPE_VIRTUAL
Jeff Brown92130f62012-10-24 21:28:33 -0700306 * @hide
307 */
308 public int getType() {
309 return mType;
310 }
311
312 /**
313 * Gets the display address, or null if none.
314 * Interpretation varies by display type.
315 *
316 * @return The display address.
317 * @hide
318 */
319 public String getAddress() {
320 return mAddress;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700321 }
322
323 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700324 * Gets the UID of the application that owns this display, or zero if it is
325 * owned by the system.
326 * <p>
327 * If the display is private, then only the owner can use it.
328 * </p>
329 *
330 * @hide
331 */
332 public int getOwnerUid() {
333 return mOwnerUid;
334 }
335
336 /**
337 * Gets the package name of the application that owns this display, or null if it is
338 * owned by the system.
339 * <p>
340 * If the display is private, then only the owner can use it.
341 * </p>
342 *
343 * @hide
344 */
345 public String getOwnerPackageName() {
346 return mOwnerPackageName;
347 }
348
349 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700350 * Gets the compatibility info used by this display instance.
351 *
Craig Mautner48d0d182013-06-11 07:53:06 -0700352 * @return The display adjustments holder, or null if none is required.
Jeff Brown98365d72012-08-19 20:30:52 -0700353 * @hide
354 */
Craig Mautner48d0d182013-06-11 07:53:06 -0700355 public DisplayAdjustments getDisplayAdjustments() {
356 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -0700357 }
358
359 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700360 * Gets the name of the display.
Jeff Brown92130f62012-10-24 21:28:33 -0700361 * <p>
362 * Note that some displays may be renamed by the user.
363 * </p>
364 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700365 * @return The display's name.
366 */
367 public String getName() {
Jeff Brown92130f62012-10-24 21:28:33 -0700368 synchronized (this) {
369 updateDisplayInfoLocked();
370 return mDisplayInfo.name;
371 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700372 }
373
374 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700375 * Gets the size of the display, in pixels.
376 * <p>
377 * Note that this value should <em>not</em> be used for computing layouts,
378 * since a device will typically have screen decoration (such as a status bar)
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800379 * along the edges of the display that reduce the amount of application
Jeff Brownbc68a592011-07-25 12:58:12 -0700380 * space available from the size returned here. Layouts should instead use
381 * the window size.
382 * </p><p>
383 * The size is adjusted based on the current rotation of the display.
384 * </p><p>
385 * The size returned by this method does not necessarily represent the
386 * actual raw size (native resolution) of the display. The returned size may
Jeff Brownfa25bf52012-07-23 19:26:30 -0700387 * be adjusted to exclude certain system decoration elements that are always visible.
Jeff Brownbc68a592011-07-25 12:58:12 -0700388 * It may also be scaled to provide compatibility with older applications that
389 * were originally designed for smaller displays.
390 * </p>
391 *
392 * @param outSize A {@link Point} object to receive the size information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700394 public void getSize(Point outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700395 synchronized (this) {
396 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700397 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700398 outSize.x = mTempMetrics.widthPixels;
399 outSize.y = mTempMetrics.heightPixels;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700400 }
401 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700404 * Gets the size of the display as a rectangle, in pixels.
405 *
406 * @param outSize A {@link Rect} object to receive the size information.
407 * @see #getSize(Point)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700409 public void getRectSize(Rect outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700410 synchronized (this) {
411 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700412 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700413 outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700414 }
415 }
416
417 /**
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700418 * Return the range of display sizes an application can expect to encounter
419 * under normal operation, as long as there is no physical change in screen
420 * size. This is basically the sizes you will see as the orientation
421 * changes, taking into account whatever screen decoration there is in
422 * each rotation. For example, the status bar is always at the top of the
423 * screen, so it will reduce the height both in landscape and portrait, and
424 * the smallest height returned here will be the smaller of the two.
425 *
426 * This is intended for applications to get an idea of the range of sizes
427 * they will encounter while going through device rotations, to provide a
428 * stable UI through rotation. The sizes here take into account all standard
429 * system decorations that reduce the size actually available to the
430 * application: the status bar, navigation bar, system bar, etc. It does
431 * <em>not</em> take into account more transient elements like an IME
432 * soft keyboard.
433 *
434 * @param outSmallestSize Filled in with the smallest width and height
435 * that the application will encounter, in pixels (not dp units). The x
436 * (width) dimension here directly corresponds to
437 * {@link android.content.res.Configuration#smallestScreenWidthDp
438 * Configuration.smallestScreenWidthDp}, except the value here is in raw
439 * screen pixels rather than dp units. Your application may of course
440 * still get smaller space yet if, for example, a soft keyboard is
441 * being displayed.
442 * @param outLargestSize Filled in with the largest width and height
443 * that the application will encounter, in pixels (not dp units). Your
444 * application may of course still get larger space than this if,
445 * for example, screen decorations like the status bar are being hidden.
446 */
447 public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700448 synchronized (this) {
449 updateDisplayInfoLocked();
450 outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
451 outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
452 outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
453 outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700454 }
455 }
456
457 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700458 * Return the maximum screen size dimension that will happen. This is
459 * mostly for wallpapers.
460 * @hide
461 */
462 public int getMaximumSizeDimension() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700463 synchronized (this) {
464 updateDisplayInfoLocked();
465 return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700466 }
467 }
468
469 /**
470 * @deprecated Use {@link #getSize(Point)} instead.
471 */
472 @Deprecated
473 public int getWidth() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700474 synchronized (this) {
475 updateCachedAppSizeIfNeededLocked();
476 return mCachedAppWidthCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700477 }
478 }
479
480 /**
481 * @deprecated Use {@link #getSize(Point)} instead.
482 */
483 @Deprecated
484 public int getHeight() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700485 synchronized (this) {
486 updateCachedAppSizeIfNeededLocked();
487 return mCachedAppHeightCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700488 }
489 }
490
Jeff Brownbc68a592011-07-25 12:58:12 -0700491 /**
Dianne Hackbornc652de82013-02-15 16:32:56 -0800492 * @hide
493 * Return a rectangle defining the insets of the overscan region of the display.
494 * Each field of the rectangle is the number of pixels the overscan area extends
495 * into the display on that side.
496 */
497 public void getOverscanInsets(Rect outRect) {
498 synchronized (this) {
499 updateDisplayInfoLocked();
500 outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop,
501 mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom);
502 }
503 }
504
505 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800506 * Returns the rotation of the screen from its "natural" orientation.
507 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
508 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
509 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
510 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
511 * example, if a device has a naturally tall screen, and the user has
512 * turned it on its side to go into a landscape orientation, the value
513 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
514 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
515 * the direction it was turned. The angle is the rotation of the drawn
516 * graphics on the screen, which is the opposite direction of the physical
517 * rotation of the device. For example, if the device is rotated 90
518 * degrees counter-clockwise, to compensate rendering will be rotated by
519 * 90 degrees clockwise and thus the returned value here will be
520 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
521 */
522 public int getRotation() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700523 synchronized (this) {
524 updateDisplayInfoLocked();
525 return mDisplayInfo.rotation;
526 }
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800527 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700528
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800529 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800530 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * @return orientation of this display.
532 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700533 @Deprecated
534 public int getOrientation() {
535 return getRotation();
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537
538 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700539 * Gets the pixel format of the display.
540 * @return One of the constants defined in {@link android.graphics.PixelFormat}.
541 *
542 * @deprecated This method is no longer supported.
543 * The result is always {@link PixelFormat#RGBA_8888}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700545 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public int getPixelFormat() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700547 return PixelFormat.RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700551 * Gets the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public float getRefreshRate() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700554 synchronized (this) {
555 updateDisplayInfoLocked();
556 return mDisplayInfo.refreshRate;
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700561 * Gets display metrics that describe the size and density of this display.
562 * <p>
563 * The size is adjusted based on the current rotation of the display.
564 * </p><p>
565 * The size returned by this method does not necessarily represent the
566 * actual raw size (native resolution) of the display. The returned size may
567 * be adjusted to exclude certain system decor elements that are always visible.
568 * It may also be scaled to provide compatibility with older applications that
569 * were originally designed for smaller displays.
570 * </p>
571 *
572 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 */
574 public void getMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700575 synchronized (this) {
576 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700577 mDisplayInfo.getAppMetrics(outMetrics, mDisplayAdjustments);
Dianne Hackborn68066c22011-04-21 17:26:39 -0700578 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700579 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700580
Jeff Brownfa25bf52012-07-23 19:26:30 -0700581 /**
582 * Gets the real size of the display without subtracting any window decor or
583 * applying any compatibility scale factors.
584 * <p>
585 * The size is adjusted based on the current rotation of the display.
586 * </p><p>
587 * The real size may be smaller than the physical size of the screen when the
588 * window manager is emulating a smaller display (using adb shell am display-size).
589 * </p>
590 *
591 * @param outSize Set to the real size of the display.
592 */
593 public void getRealSize(Point outSize) {
594 synchronized (this) {
595 updateDisplayInfoLocked();
596 outSize.x = mDisplayInfo.logicalWidth;
597 outSize.y = mDisplayInfo.logicalHeight;
598 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700599 }
600
601 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700602 * Gets display metrics based on the real size of this display.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700603 * <p>
604 * The size is adjusted based on the current rotation of the display.
605 * </p><p>
606 * The real size may be smaller than the physical size of the screen when the
607 * window manager is emulating a smaller display (using adb shell am display-size).
608 * </p>
609 *
610 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
Dianne Hackborn68066c22011-04-21 17:26:39 -0700611 */
612 public void getRealMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700613 synchronized (this) {
614 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700615 mDisplayInfo.getLogicalMetrics(outMetrics,
616 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,
617 mDisplayAdjustments.getActivityToken());
Jeff Brownbc68a592011-07-25 12:58:12 -0700618 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700619 }
620
Jeff Browna506a6e2013-06-04 00:02:38 -0700621 /**
622 * Returns true if the specified UID has access to this display.
623 * @hide
624 */
625 public boolean hasAccess(int uid) {
626 return Display.hasAccess(uid, mFlags, mOwnerUid);
627 }
628
629 /** @hide */
630 public static boolean hasAccess(int uid, int flags, int ownerUid) {
631 return (flags & Display.FLAG_PRIVATE) == 0
632 || uid == ownerUid
633 || uid == Process.SYSTEM_UID
634 || uid == 0;
635 }
636
Jeff Brownfa25bf52012-07-23 19:26:30 -0700637 private void updateDisplayInfoLocked() {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700638 // Note: The display manager caches display info objects on our behalf.
639 DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId);
640 if (newInfo == null) {
641 // Preserve the old mDisplayInfo after the display is removed.
642 if (mIsValid) {
643 mIsValid = false;
644 if (DEBUG) {
645 Log.d(TAG, "Logical display " + mDisplayId + " was removed.");
646 }
647 }
648 } else {
649 // Use the new display info. (It might be the same object if nothing changed.)
650 mDisplayInfo = newInfo;
651 if (!mIsValid) {
652 mIsValid = true;
653 if (DEBUG) {
654 Log.d(TAG, "Logical display " + mDisplayId + " was recreated.");
655 }
656 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700657 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700658 }
659
Jeff Brownfa25bf52012-07-23 19:26:30 -0700660 private void updateCachedAppSizeIfNeededLocked() {
661 long now = SystemClock.uptimeMillis();
662 if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
663 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700664 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700665 mCachedAppWidthCompat = mTempMetrics.widthPixels;
666 mCachedAppHeightCompat = mTempMetrics.heightPixels;
667 mLastCachedAppSizeUpdate = now;
668 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700669 }
Jeff Brownbf5740e2012-08-19 23:20:02 -0700670
671 // For debugging purposes
672 @Override
673 public String toString() {
674 synchronized (this) {
675 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700676 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownbf5740e2012-08-19 23:20:02 -0700677 return "Display id " + mDisplayId + ": " + mDisplayInfo
Jeff Brownbd6e1502012-08-28 03:27:37 -0700678 + ", " + mTempMetrics + ", isValid=" + mIsValid;
Jeff Brownbf5740e2012-08-19 23:20:02 -0700679 }
680 }
Jeff Brown92130f62012-10-24 21:28:33 -0700681
682 /**
683 * @hide
684 */
685 public static String typeToString(int type) {
686 switch (type) {
687 case TYPE_UNKNOWN:
688 return "UNKNOWN";
689 case TYPE_BUILT_IN:
690 return "BUILT_IN";
691 case TYPE_HDMI:
692 return "HDMI";
693 case TYPE_WIFI:
694 return "WIFI";
695 case TYPE_OVERLAY:
696 return "OVERLAY";
Jeff Browna506a6e2013-06-04 00:02:38 -0700697 case TYPE_VIRTUAL:
698 return "VIRTUAL";
Jeff Brown92130f62012-10-24 21:28:33 -0700699 default:
700 return Integer.toString(type);
701 }
702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703}
704