blob: d3f63b43aa5b51fddff4cc8f390423313bd3562e [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.
Jeff Browna506a6e2013-06-04 00:02:38 -0700152 *
153 * @see #getFlags
154 */
155 public static final int FLAG_PRIVATE = 1 << 2;
156
157 /**
Jeff Brown7d00aff2013-08-02 19:03:49 -0700158 * Display flag: Indicates that the display is a presentation display.
159 * <p>
160 * This flag identifies secondary displays that are suitable for
161 * use as presentation displays such as HDMI or Wireless displays. Applications
162 * may automatically project their content to presentation displays to provide
163 * richer second screen experiences.
164 * </p>
165 *
166 * @see #getFlags
167 */
168 public static final int FLAG_PRESENTATION = 1 << 3;
169
170 /**
Jeff Brown92130f62012-10-24 21:28:33 -0700171 * Display type: Unknown display type.
172 * @hide
173 */
174 public static final int TYPE_UNKNOWN = 0;
175
176 /**
177 * Display type: Built-in display.
178 * @hide
179 */
180 public static final int TYPE_BUILT_IN = 1;
181
182 /**
183 * Display type: HDMI display.
184 * @hide
185 */
186 public static final int TYPE_HDMI = 2;
187
188 /**
189 * Display type: WiFi display.
190 * @hide
191 */
192 public static final int TYPE_WIFI = 3;
193
194 /**
195 * Display type: Overlay display.
196 * @hide
197 */
198 public static final int TYPE_OVERLAY = 4;
199
200 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700201 * Display type: Virtual display.
202 * @hide
203 */
204 public static final int TYPE_VIRTUAL = 5;
205
206 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700207 * Internal method to create a display.
208 * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
Jeff Brown98365d72012-08-19 20:30:52 -0700209 * or {@link android.hardware.display.DisplayManager#getDisplay}
210 * to get a display object.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700211 *
212 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700214 public Display(DisplayManagerGlobal global,
215 int displayId, DisplayInfo displayInfo /*not null*/,
Craig Mautner48d0d182013-06-11 07:53:06 -0700216 DisplayAdjustments daj) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700217 mGlobal = global;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700218 mDisplayId = displayId;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700219 mDisplayInfo = displayInfo;
Craig Mautner1abaa532013-06-28 12:03:32 -0700220 mDisplayAdjustments = new DisplayAdjustments(daj);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700221 mIsValid = true;
Jeff Brown92130f62012-10-24 21:28:33 -0700222
223 // Cache properties that cannot change as long as the display is valid.
224 mLayerStack = displayInfo.layerStack;
225 mFlags = displayInfo.flags;
226 mType = displayInfo.type;
227 mAddress = displayInfo.address;
Jeff Browna506a6e2013-06-04 00:02:38 -0700228 mOwnerUid = displayInfo.ownerUid;
229 mOwnerPackageName = displayInfo.ownerPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700233 * Gets the display id.
234 * <p>
235 * Each logical display has a unique id.
236 * The default display has id {@link #DEFAULT_DISPLAY}.
237 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
239 public int getDisplayId() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700240 return mDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
243 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700244 * Returns true if this display is still valid, false if the display has been removed.
245 *
246 * If the display is invalid, then the methods of this class will
247 * continue to report the most recently observed display information.
248 * However, it is unwise (and rather fruitless) to continue using a
249 * {@link Display} object after the display's demise.
250 *
251 * It's possible for a display that was previously invalid to become
252 * valid again if a display with the same id is reconnected.
253 *
254 * @return True if the display is still valid.
255 */
256 public boolean isValid() {
257 synchronized (this) {
258 updateDisplayInfoLocked();
259 return mIsValid;
260 }
261 }
262
263 /**
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700264 * Gets a full copy of the display information.
265 *
266 * @param outDisplayInfo The object to receive the copy of the display information.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700267 * @return True if the display is still valid.
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700268 * @hide
269 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700270 public boolean getDisplayInfo(DisplayInfo outDisplayInfo) {
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700271 synchronized (this) {
272 updateDisplayInfoLocked();
273 outDisplayInfo.copyFrom(mDisplayInfo);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700274 return mIsValid;
Jeff Brown2ab1b7d92012-08-08 01:46:41 -0700275 }
276 }
277
278 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700279 * Gets the display's layer stack.
280 *
281 * Each display has its own independent layer stack upon which surfaces
282 * are placed to be managed by surface flinger.
283 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700284 * @return The display's layer stack number.
Jeff Brown98365d72012-08-19 20:30:52 -0700285 * @hide
286 */
287 public int getLayerStack() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700288 return mLayerStack;
Jeff Brown98365d72012-08-19 20:30:52 -0700289 }
290
291 /**
Jeff Brownc5df37c2012-09-13 11:45:07 -0700292 * Returns a combination of flags that describe the capabilities of the display.
293 *
294 * @return The display flags.
295 *
Jeff Brown77aebfd2012-10-01 21:07:03 -0700296 * @see #FLAG_SUPPORTS_PROTECTED_BUFFERS
Jeff Brownf0681b32012-10-23 17:35:57 -0700297 * @see #FLAG_SECURE
Jeff Browna506a6e2013-06-04 00:02:38 -0700298 * @see #FLAG_PRIVATE
Jeff Brownc5df37c2012-09-13 11:45:07 -0700299 */
300 public int getFlags() {
Jeff Brown92130f62012-10-24 21:28:33 -0700301 return mFlags;
302 }
303
304 /**
305 * Gets the display type.
306 *
307 * @return The display type.
308 *
309 * @see #TYPE_UNKNOWN
310 * @see #TYPE_BUILT_IN
311 * @see #TYPE_HDMI
312 * @see #TYPE_WIFI
313 * @see #TYPE_OVERLAY
Jeff Browna506a6e2013-06-04 00:02:38 -0700314 * @see #TYPE_VIRTUAL
Jeff Brown92130f62012-10-24 21:28:33 -0700315 * @hide
316 */
317 public int getType() {
318 return mType;
319 }
320
321 /**
322 * Gets the display address, or null if none.
323 * Interpretation varies by display type.
324 *
325 * @return The display address.
326 * @hide
327 */
328 public String getAddress() {
329 return mAddress;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700330 }
331
332 /**
Jeff Browna506a6e2013-06-04 00:02:38 -0700333 * Gets the UID of the application that owns this display, or zero if it is
334 * owned by the system.
335 * <p>
336 * If the display is private, then only the owner can use it.
337 * </p>
338 *
339 * @hide
340 */
341 public int getOwnerUid() {
342 return mOwnerUid;
343 }
344
345 /**
346 * Gets the package name of the application that owns this display, or null if it is
347 * owned by the system.
348 * <p>
349 * If the display is private, then only the owner can use it.
350 * </p>
351 *
352 * @hide
353 */
354 public String getOwnerPackageName() {
355 return mOwnerPackageName;
356 }
357
358 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700359 * Gets the compatibility info used by this display instance.
360 *
Craig Mautner48d0d182013-06-11 07:53:06 -0700361 * @return The display adjustments holder, or null if none is required.
Jeff Brown98365d72012-08-19 20:30:52 -0700362 * @hide
363 */
Craig Mautner48d0d182013-06-11 07:53:06 -0700364 public DisplayAdjustments getDisplayAdjustments() {
365 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -0700366 }
367
368 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700369 * Gets the name of the display.
Jeff Brown92130f62012-10-24 21:28:33 -0700370 * <p>
371 * Note that some displays may be renamed by the user.
372 * </p>
373 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700374 * @return The display's name.
375 */
376 public String getName() {
Jeff Brown92130f62012-10-24 21:28:33 -0700377 synchronized (this) {
378 updateDisplayInfoLocked();
379 return mDisplayInfo.name;
380 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700381 }
382
383 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700384 * Gets the size of the display, in pixels.
385 * <p>
386 * Note that this value should <em>not</em> be used for computing layouts,
387 * since a device will typically have screen decoration (such as a status bar)
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800388 * along the edges of the display that reduce the amount of application
Jeff Brownbc68a592011-07-25 12:58:12 -0700389 * space available from the size returned here. Layouts should instead use
390 * the window size.
391 * </p><p>
392 * The size is adjusted based on the current rotation of the display.
393 * </p><p>
394 * The size returned by this method does not necessarily represent the
395 * actual raw size (native resolution) of the display. The returned size may
Jeff Brownfa25bf52012-07-23 19:26:30 -0700396 * be adjusted to exclude certain system decoration elements that are always visible.
Jeff Brownbc68a592011-07-25 12:58:12 -0700397 * It may also be scaled to provide compatibility with older applications that
398 * were originally designed for smaller displays.
399 * </p>
400 *
401 * @param outSize A {@link Point} object to receive the size information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700403 public void getSize(Point outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700404 synchronized (this) {
405 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700406 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700407 outSize.x = mTempMetrics.widthPixels;
408 outSize.y = mTempMetrics.heightPixels;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700409 }
410 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700413 * Gets the size of the display as a rectangle, in pixels.
414 *
415 * @param outSize A {@link Rect} object to receive the size information.
416 * @see #getSize(Point)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700418 public void getRectSize(Rect outSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700419 synchronized (this) {
420 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700421 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700422 outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700423 }
424 }
425
426 /**
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700427 * Return the range of display sizes an application can expect to encounter
428 * under normal operation, as long as there is no physical change in screen
429 * size. This is basically the sizes you will see as the orientation
430 * changes, taking into account whatever screen decoration there is in
431 * each rotation. For example, the status bar is always at the top of the
432 * screen, so it will reduce the height both in landscape and portrait, and
433 * the smallest height returned here will be the smaller of the two.
434 *
435 * This is intended for applications to get an idea of the range of sizes
436 * they will encounter while going through device rotations, to provide a
437 * stable UI through rotation. The sizes here take into account all standard
438 * system decorations that reduce the size actually available to the
439 * application: the status bar, navigation bar, system bar, etc. It does
440 * <em>not</em> take into account more transient elements like an IME
441 * soft keyboard.
442 *
443 * @param outSmallestSize Filled in with the smallest width and height
444 * that the application will encounter, in pixels (not dp units). The x
445 * (width) dimension here directly corresponds to
446 * {@link android.content.res.Configuration#smallestScreenWidthDp
447 * Configuration.smallestScreenWidthDp}, except the value here is in raw
448 * screen pixels rather than dp units. Your application may of course
449 * still get smaller space yet if, for example, a soft keyboard is
450 * being displayed.
451 * @param outLargestSize Filled in with the largest width and height
452 * that the application will encounter, in pixels (not dp units). Your
453 * application may of course still get larger space than this if,
454 * for example, screen decorations like the status bar are being hidden.
455 */
456 public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700457 synchronized (this) {
458 updateDisplayInfoLocked();
459 outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
460 outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
461 outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
462 outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
Dianne Hackborn68c33ca2012-04-19 14:51:25 -0700463 }
464 }
465
466 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700467 * Return the maximum screen size dimension that will happen. This is
468 * mostly for wallpapers.
469 * @hide
470 */
471 public int getMaximumSizeDimension() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700472 synchronized (this) {
473 updateDisplayInfoLocked();
474 return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700475 }
476 }
477
478 /**
479 * @deprecated Use {@link #getSize(Point)} instead.
480 */
481 @Deprecated
482 public int getWidth() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700483 synchronized (this) {
484 updateCachedAppSizeIfNeededLocked();
485 return mCachedAppWidthCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700486 }
487 }
488
489 /**
490 * @deprecated Use {@link #getSize(Point)} instead.
491 */
492 @Deprecated
493 public int getHeight() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700494 synchronized (this) {
495 updateCachedAppSizeIfNeededLocked();
496 return mCachedAppHeightCompat;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700497 }
498 }
499
Jeff Brownbc68a592011-07-25 12:58:12 -0700500 /**
Dianne Hackbornc652de82013-02-15 16:32:56 -0800501 * @hide
502 * Return a rectangle defining the insets of the overscan region of the display.
503 * Each field of the rectangle is the number of pixels the overscan area extends
504 * into the display on that side.
505 */
506 public void getOverscanInsets(Rect outRect) {
507 synchronized (this) {
508 updateDisplayInfoLocked();
509 outRect.set(mDisplayInfo.overscanLeft, mDisplayInfo.overscanTop,
510 mDisplayInfo.overscanRight, mDisplayInfo.overscanBottom);
511 }
512 }
513
514 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800515 * Returns the rotation of the screen from its "natural" orientation.
516 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
517 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
518 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
519 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
520 * example, if a device has a naturally tall screen, and the user has
521 * turned it on its side to go into a landscape orientation, the value
522 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
523 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
524 * the direction it was turned. The angle is the rotation of the drawn
525 * graphics on the screen, which is the opposite direction of the physical
526 * rotation of the device. For example, if the device is rotated 90
527 * degrees counter-clockwise, to compensate rendering will be rotated by
528 * 90 degrees clockwise and thus the returned value here will be
529 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
530 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700531 @Surface.Rotation
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800532 public int getRotation() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700533 synchronized (this) {
534 updateDisplayInfoLocked();
535 return mDisplayInfo.rotation;
536 }
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800537 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700538
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800539 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800540 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 * @return orientation of this display.
542 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700543 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -0700544 @Surface.Rotation
Jeff Brownfa25bf52012-07-23 19:26:30 -0700545 public int getOrientation() {
546 return getRotation();
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
549 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700550 * Gets the pixel format of the display.
551 * @return One of the constants defined in {@link android.graphics.PixelFormat}.
552 *
553 * @deprecated This method is no longer supported.
554 * The result is always {@link PixelFormat#RGBA_8888}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 */
Jeff Brownfa25bf52012-07-23 19:26:30 -0700556 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 public int getPixelFormat() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700558 return PixelFormat.RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700562 * Gets the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 */
564 public float getRefreshRate() {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700565 synchronized (this) {
566 updateDisplayInfoLocked();
567 return mDisplayInfo.refreshRate;
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700572 * Gets display metrics that describe the size and density of this display.
573 * <p>
574 * The size is adjusted based on the current rotation of the display.
575 * </p><p>
576 * The size returned by this method does not necessarily represent the
577 * actual raw size (native resolution) of the display. The returned size may
578 * be adjusted to exclude certain system decor elements that are always visible.
579 * It may also be scaled to provide compatibility with older applications that
580 * were originally designed for smaller displays.
581 * </p>
582 *
583 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 */
585 public void getMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700586 synchronized (this) {
587 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700588 mDisplayInfo.getAppMetrics(outMetrics, mDisplayAdjustments);
Dianne Hackborn68066c22011-04-21 17:26:39 -0700589 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700590 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700591
Jeff Brownfa25bf52012-07-23 19:26:30 -0700592 /**
593 * Gets the real size of the display without subtracting any window decor or
594 * applying any compatibility scale factors.
595 * <p>
596 * The size is adjusted based on the current rotation of the display.
597 * </p><p>
598 * The real size may be smaller than the physical size of the screen when the
599 * window manager is emulating a smaller display (using adb shell am display-size).
600 * </p>
601 *
602 * @param outSize Set to the real size of the display.
603 */
604 public void getRealSize(Point outSize) {
605 synchronized (this) {
606 updateDisplayInfoLocked();
607 outSize.x = mDisplayInfo.logicalWidth;
608 outSize.y = mDisplayInfo.logicalHeight;
609 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700610 }
611
612 /**
Jeff Brownbc68a592011-07-25 12:58:12 -0700613 * Gets display metrics based on the real size of this display.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700614 * <p>
615 * The size is adjusted based on the current rotation of the display.
616 * </p><p>
617 * The real size may be smaller than the physical size of the screen when the
618 * window manager is emulating a smaller display (using adb shell am display-size).
619 * </p>
620 *
621 * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
Dianne Hackborn68066c22011-04-21 17:26:39 -0700622 */
623 public void getRealMetrics(DisplayMetrics outMetrics) {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700624 synchronized (this) {
625 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700626 mDisplayInfo.getLogicalMetrics(outMetrics,
627 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,
628 mDisplayAdjustments.getActivityToken());
Jeff Brownbc68a592011-07-25 12:58:12 -0700629 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700630 }
631
Jeff Browna506a6e2013-06-04 00:02:38 -0700632 /**
633 * Returns true if the specified UID has access to this display.
634 * @hide
635 */
636 public boolean hasAccess(int uid) {
637 return Display.hasAccess(uid, mFlags, mOwnerUid);
638 }
639
640 /** @hide */
641 public static boolean hasAccess(int uid, int flags, int ownerUid) {
642 return (flags & Display.FLAG_PRIVATE) == 0
643 || uid == ownerUid
644 || uid == Process.SYSTEM_UID
645 || uid == 0;
646 }
647
Jeff Brown69b07162013-11-07 00:30:16 -0800648 /**
649 * Returns true if the display is a public presentation display.
650 * @hide
651 */
652 public boolean isPublicPresentation() {
653 return (mFlags & (Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION)) ==
654 Display.FLAG_PRESENTATION;
655 }
656
Jeff Brownfa25bf52012-07-23 19:26:30 -0700657 private void updateDisplayInfoLocked() {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700658 // Note: The display manager caches display info objects on our behalf.
659 DisplayInfo newInfo = mGlobal.getDisplayInfo(mDisplayId);
660 if (newInfo == null) {
661 // Preserve the old mDisplayInfo after the display is removed.
662 if (mIsValid) {
663 mIsValid = false;
664 if (DEBUG) {
665 Log.d(TAG, "Logical display " + mDisplayId + " was removed.");
666 }
667 }
668 } else {
669 // Use the new display info. (It might be the same object if nothing changed.)
670 mDisplayInfo = newInfo;
671 if (!mIsValid) {
672 mIsValid = true;
673 if (DEBUG) {
674 Log.d(TAG, "Logical display " + mDisplayId + " was recreated.");
675 }
676 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700677 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700678 }
679
Jeff Brownfa25bf52012-07-23 19:26:30 -0700680 private void updateCachedAppSizeIfNeededLocked() {
681 long now = SystemClock.uptimeMillis();
682 if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
683 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700684 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700685 mCachedAppWidthCompat = mTempMetrics.widthPixels;
686 mCachedAppHeightCompat = mTempMetrics.heightPixels;
687 mLastCachedAppSizeUpdate = now;
688 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700689 }
Jeff Brownbf5740e2012-08-19 23:20:02 -0700690
691 // For debugging purposes
692 @Override
693 public String toString() {
694 synchronized (this) {
695 updateDisplayInfoLocked();
Craig Mautner48d0d182013-06-11 07:53:06 -0700696 mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);
Jeff Brownbf5740e2012-08-19 23:20:02 -0700697 return "Display id " + mDisplayId + ": " + mDisplayInfo
Jeff Brownbd6e1502012-08-28 03:27:37 -0700698 + ", " + mTempMetrics + ", isValid=" + mIsValid;
Jeff Brownbf5740e2012-08-19 23:20:02 -0700699 }
700 }
Jeff Brown92130f62012-10-24 21:28:33 -0700701
702 /**
703 * @hide
704 */
705 public static String typeToString(int type) {
706 switch (type) {
707 case TYPE_UNKNOWN:
708 return "UNKNOWN";
709 case TYPE_BUILT_IN:
710 return "BUILT_IN";
711 case TYPE_HDMI:
712 return "HDMI";
713 case TYPE_WIFI:
714 return "WIFI";
715 case TYPE_OVERLAY:
716 return "OVERLAY";
Jeff Browna506a6e2013-06-04 00:02:38 -0700717 case TYPE_VIRTUAL:
718 return "VIRTUAL";
Jeff Brown92130f62012-10-24 21:28:33 -0700719 default:
720 return Integer.toString(type);
721 }
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723}
724