blob: 01ef58e987d71178edf963dfac6c7c0bacecc3bb [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 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.hardware.display;
18
Kenny Guy29aa30e2017-11-30 13:43:46 +000019import android.Manifest;
Michael Wrightc39d47a2014-07-08 18:07:36 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Kenny Guy29aa30e2017-11-30 13:43:46 +000022import android.annotation.RequiresPermission;
Michael Wrighteedcbf12017-08-16 23:14:54 +010023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Kenny Guy0ff44892018-01-10 14:06:42 +000025import android.annotation.TestApi;
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010026import android.annotation.UnsupportedAppUsage;
Adrian Roose99bc052017-11-20 17:55:31 +010027import android.app.KeyguardManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070028import android.content.Context;
Michael Wrighteedcbf12017-08-16 23:14:54 +010029import android.graphics.Point;
Michael Wrightc39d47a2014-07-08 18:07:36 -070030import android.media.projection.MediaProjection;
Jeff Brown98365d72012-08-19 20:30:52 -070031import android.os.Handler;
Dan Gittik122df862018-03-28 16:59:22 +010032import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070033import android.util.SparseArray;
Jeff Brown98365d72012-08-19 20:30:52 -070034import android.view.Display;
Jeff Browna506a6e2013-06-04 00:02:38 -070035import android.view.Surface;
Jeff Brown98365d72012-08-19 20:30:52 -070036
Jeff Brown92130f62012-10-24 21:28:33 -070037import java.util.ArrayList;
Kenny Guy22bd0442017-10-26 00:15:54 +010038import java.util.List;
Jeff Brown92130f62012-10-24 21:28:33 -070039
Jeff Brownfa25bf52012-07-23 19:26:30 -070040/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070041 * Manages the properties of attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070042 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060043@SystemService(Context.DISPLAY_SERVICE)
Jeff Brownfa25bf52012-07-23 19:26:30 -070044public final class DisplayManager {
45 private static final String TAG = "DisplayManager";
Jeff Brown98365d72012-08-19 20:30:52 -070046 private static final boolean DEBUG = false;
47
Jeff Brownbd6e1502012-08-28 03:27:37 -070048 private final Context mContext;
49 private final DisplayManagerGlobal mGlobal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070050
Jeff Brownbd6e1502012-08-28 03:27:37 -070051 private final Object mLock = new Object();
52 private final SparseArray<Display> mDisplays = new SparseArray<Display>();
Jeff Brownfa25bf52012-07-23 19:26:30 -070053
Jeff Brown92130f62012-10-24 21:28:33 -070054 private final ArrayList<Display> mTempDisplays = new ArrayList<Display>();
55
Jeff Browne08ae382012-09-07 20:36:36 -070056 /**
57 * Broadcast receiver that indicates when the Wifi display status changes.
58 * <p>
59 * The status is provided as a {@link WifiDisplayStatus} object in the
60 * {@link #EXTRA_WIFI_DISPLAY_STATUS} extra.
61 * </p><p>
Jeff Brownbc335452012-09-26 18:34:47 -070062 * This broadcast is only sent to registered receivers and can only be sent by the system.
Jeff Browne08ae382012-09-07 20:36:36 -070063 * </p>
64 * @hide
65 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010066 @UnsupportedAppUsage
Jeff Browne08ae382012-09-07 20:36:36 -070067 public static final String ACTION_WIFI_DISPLAY_STATUS_CHANGED =
68 "android.hardware.display.action.WIFI_DISPLAY_STATUS_CHANGED";
69
70 /**
71 * Contains a {@link WifiDisplayStatus} object.
72 * @hide
73 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010074 @UnsupportedAppUsage
Jeff Browne08ae382012-09-07 20:36:36 -070075 public static final String EXTRA_WIFI_DISPLAY_STATUS =
76 "android.hardware.display.extra.WIFI_DISPLAY_STATUS";
77
Jeff Brown92130f62012-10-24 21:28:33 -070078 /**
79 * Display category: Presentation displays.
80 * <p>
81 * This category can be used to identify secondary displays that are suitable for
Jeff Brown7d00aff2013-08-02 19:03:49 -070082 * use as presentation displays such as HDMI or Wireless displays. Applications
83 * may automatically project their content to presentation displays to provide
84 * richer second screen experiences.
Jeff Brown92130f62012-10-24 21:28:33 -070085 * </p>
86 *
Jeff Brown511cd352013-08-23 17:43:37 -070087 * @see android.app.Presentation
Jeff Brown7d00aff2013-08-02 19:03:49 -070088 * @see Display#FLAG_PRESENTATION
Jeff Brown92130f62012-10-24 21:28:33 -070089 * @see #getDisplays(String)
90 */
91 public static final String DISPLAY_CATEGORY_PRESENTATION =
92 "android.hardware.display.category.PRESENTATION";
93
Jeff Brown7d00aff2013-08-02 19:03:49 -070094 /**
95 * Virtual display flag: Create a public display.
96 *
97 * <h3>Public virtual displays</h3>
98 * <p>
99 * When this flag is set, the virtual display is public.
100 * </p><p>
101 * A public virtual display behaves just like most any other display that is connected
102 * to the system such as an HDMI or Wireless display. Applications can open
103 * windows on the display and the system may mirror the contents of other displays
104 * onto it.
105 * </p><p>
Michael Wright6720be42014-07-29 19:14:16 -0700106 * Creating a public virtual display that isn't restricted to own-content only implicitly
107 * creates an auto-mirroring display. See {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR} for
108 * restrictions on who is allowed to create an auto-mirroring display.
Jeff Brown7d00aff2013-08-02 19:03:49 -0700109 * </p>
110 *
111 * <h3>Private virtual displays</h3>
112 * <p>
113 * When this flag is not set, the virtual display is private as defined by the
114 * {@link Display#FLAG_PRIVATE} display flag.
115 * </p>
Michael Wright6720be42014-07-29 19:14:16 -0700116 *
117 * <p>
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800118 * A private virtual display belongs to the application that created it. Only the a owner of a
119 * private virtual display and the apps that are already on that display are allowed to place
120 * windows upon it. The private virtual display also does not participate in display mirroring:
121 * it will neither receive mirrored content from another display nor allow its own content to be
122 * mirrored elsewhere. More precisely, the only processes that are allowed to enumerate or
123 * interact with the private display are those that have the same UID as the application that
124 * originally created the private virtual display or as the activities that are already on that
125 * display.
Michael Wright6720be42014-07-29 19:14:16 -0700126 * </p>
Jeff Brown7d00aff2013-08-02 19:03:49 -0700127 *
128 * @see #createVirtualDisplay
Jeff Brownd14c8c92014-01-07 18:13:09 -0800129 * @see #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
Michael Wright6720be42014-07-29 19:14:16 -0700130 * @see #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
Jeff Brown7d00aff2013-08-02 19:03:49 -0700131 */
132 public static final int VIRTUAL_DISPLAY_FLAG_PUBLIC = 1 << 0;
133
134 /**
135 * Virtual display flag: Create a presentation display.
136 *
137 * <h3>Presentation virtual displays</h3>
138 * <p>
139 * When this flag is set, the virtual display is registered as a presentation
140 * display in the {@link #DISPLAY_CATEGORY_PRESENTATION presentation display category}.
141 * Applications may automatically project their content to presentation displays
142 * to provide richer second screen experiences.
143 * </p>
144 *
145 * <h3>Non-presentation virtual displays</h3>
146 * <p>
147 * When this flag is not set, the virtual display is not registered as a presentation
148 * display. Applications can still project their content on the display but they
149 * will typically not do so automatically. This option is appropriate for
150 * more special-purpose displays.
151 * </p>
152 *
Jeff Brown511cd352013-08-23 17:43:37 -0700153 * @see android.app.Presentation
Jeff Brown7d00aff2013-08-02 19:03:49 -0700154 * @see #createVirtualDisplay
155 * @see #DISPLAY_CATEGORY_PRESENTATION
156 * @see Display#FLAG_PRESENTATION
157 */
158 public static final int VIRTUAL_DISPLAY_FLAG_PRESENTATION = 1 << 1;
159
160 /**
161 * Virtual display flag: Create a secure display.
162 *
163 * <h3>Secure virtual displays</h3>
164 * <p>
165 * When this flag is set, the virtual display is considered secure as defined
166 * by the {@link Display#FLAG_SECURE} display flag. The caller promises to take
167 * reasonable measures, such as over-the-air encryption, to prevent the contents
168 * of the display from being intercepted or recorded on a persistent medium.
169 * </p><p>
Michael Wrightca022f62018-08-28 16:58:59 +0100170 * Creating a secure virtual display requires the CAPTURE_SECURE_VIDEO_OUTPUT permission.
Jeff Brown7d00aff2013-08-02 19:03:49 -0700171 * This permission is reserved for use by system components and is not available to
172 * third-party applications.
173 * </p>
174 *
175 * <h3>Non-secure virtual displays</h3>
176 * <p>
177 * When this flag is not set, the virtual display is considered unsecure.
178 * The content of secure windows will be blanked if shown on this display.
179 * </p>
180 *
Jeff Brown511cd352013-08-23 17:43:37 -0700181 * @see Display#FLAG_SECURE
Jeff Brown7d00aff2013-08-02 19:03:49 -0700182 * @see #createVirtualDisplay
183 */
184 public static final int VIRTUAL_DISPLAY_FLAG_SECURE = 1 << 2;
185
Jeff Brownd14c8c92014-01-07 18:13:09 -0800186 /**
187 * Virtual display flag: Only show this display's own content; do not mirror
188 * the content of another display.
189 *
190 * <p>
191 * This flag is used in conjunction with {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}.
192 * Ordinarily public virtual displays will automatically mirror the content of the
193 * default display if they have no windows of their own. When this flag is
194 * specified, the virtual display will only ever show its own content and
195 * will be blanked instead if it has no windows.
196 * </p>
197 *
Michael Wright6720be42014-07-29 19:14:16 -0700198 * <p>
199 * This flag is mutually exclusive with {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR}. If both
200 * flags are specified then the own-content only behavior will be applied.
201 * </p>
202 *
203 * <p>
204 * This behavior of this flag is implied whenever neither {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}
205 * nor {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR} have been set. This flag is only required to
206 * override the default behavior when creating a public display.
207 * </p>
208 *
Jeff Brownd14c8c92014-01-07 18:13:09 -0800209 * @see #createVirtualDisplay
210 */
211 public static final int VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY = 1 << 3;
212
Michael Wrightc39d47a2014-07-08 18:07:36 -0700213
214 /**
Michael Wright6720be42014-07-29 19:14:16 -0700215 * Virtual display flag: Allows content to be mirrored on private displays when no content is
216 * being shown.
Michael Wrightc39d47a2014-07-08 18:07:36 -0700217 *
Michael Wright9469cdd2014-07-29 13:03:56 -0700218 * <p>
Michael Wright6720be42014-07-29 19:14:16 -0700219 * This flag is mutually exclusive with {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY}.
220 * If both flags are specified then the own-content only behavior will be applied.
Michael Wright9469cdd2014-07-29 13:03:56 -0700221 * </p>
Michael Wrightc39d47a2014-07-08 18:07:36 -0700222 *
Michael Wright9469cdd2014-07-29 13:03:56 -0700223 * <p>
Michael Wright6720be42014-07-29 19:14:16 -0700224 * The behavior of this flag is implied whenever {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC} is set
225 * and {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY} has not been set. This flag is only
226 * required to override the default behavior when creating a private display.
227 * </p>
228 *
229 * <p>
Michael Wrightca022f62018-08-28 16:58:59 +0100230 * Creating an auto-mirroing virtual display requires the CAPTURE_VIDEO_OUTPUT
231 * or CAPTURE_SECURE_VIDEO_OUTPUT permission.
Michael Wright6720be42014-07-29 19:14:16 -0700232 * These permissions are reserved for use by system components and are not available to
233 * third-party applications.
234 *
235 * Alternatively, an appropriate {@link MediaProjection} may be used to create an
236 * auto-mirroring virtual display.
Michael Wright9469cdd2014-07-29 13:03:56 -0700237 * </p>
Michael Wrightc39d47a2014-07-08 18:07:36 -0700238 *
239 * @see #createVirtualDisplay
240 */
Michael Wright6720be42014-07-29 19:14:16 -0700241 public static final int VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR = 1 << 4;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700242
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800243 /**
244 * Virtual display flag: Allows content to be displayed on private virtual displays when
245 * keyguard is shown but is insecure.
246 *
247 * <p>
Andrii Kulian732a90a2017-08-17 17:21:20 -0700248 * This might be used in a case when the content of a virtual display is captured and sent to an
249 * external hardware display that is not visible to the system directly. This flag will allow
250 * the continued display of content while other displays will be covered by a keyguard which
251 * doesn't require providing credentials to unlock. This means that there is either no password
252 * or other authentication method set, or the device is in a trusted state -
253 * {@link android.service.trust.TrustAgentService} has available and active trust agent.
254 * </p><p>
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800255 * This flag can only be applied to private displays as defined by the
256 * {@link Display#FLAG_PRIVATE} display flag. It is mutually exclusive with
257 * {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}. If both flags are specified then this flag's behavior
258 * will not be applied.
259 * </p>
260 *
261 * @see #createVirtualDisplay
Adrian Roose99bc052017-11-20 17:55:31 +0100262 * @see KeyguardManager#isDeviceSecure()
263 * @see KeyguardManager#isDeviceLocked()
Andrii Kulian22512e82017-04-13 11:34:43 -0700264 * @hide
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800265 */
Andrii Kulian732a90a2017-08-17 17:21:20 -0700266 // TODO: Update name and documentation and un-hide the flag. Don't change the value before that.
Andrii Kulian7211d2e2017-01-27 15:58:05 -0800267 public static final int VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD = 1 << 5;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800268
Santos Cordonb0608632017-04-05 10:31:15 -0700269 /**
270 * Virtual display flag: Specifies that the virtual display can be associated with a
271 * touchpad device that matches its uniqueId.
272 *
273 * @see #createVirtualDisplay
274 * @hide
275 */
276 public static final int VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH = 1 << 6;
277
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -0400278 /**
279 * Virtual display flag: Indicates that the orientation of this display device is coupled to
280 * the rotation of its associated logical display.
281 *
282 * @see #createVirtualDisplay
283 * @hide
284 */
285 public static final int VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT = 1 << 7;
286
rongliu1e90fc32017-10-04 17:30:30 -0700287 /**
288 * Virtual display flag: Indicates that the contents will be destroyed once
289 * the display is removed.
290 *
rongliu3e9f9722017-10-06 11:01:02 -0700291 * Public virtual displays without this flag will move their content to main display
292 * stack once they're removed. Private vistual displays will always destroy their
293 * content on removal even without this flag.
294 *
rongliu1e90fc32017-10-04 17:30:30 -0700295 * @see #createVirtualDisplay
296 * @hide
297 */
298 public static final int VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL = 1 << 8;
299
Louis Changbd48dca2018-08-29 17:44:34 +0800300 /**
301 * Virtual display flag: Indicates that the display should support system decorations. Virtual
302 * displays without this flag shouldn't show home, IME or any other system decorations.
303 *
304 * @see #createVirtualDisplay
305 * @hide
306 */
307 public static final int VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS = 1 << 9;
308
Jeff Brownbd6e1502012-08-28 03:27:37 -0700309 /** @hide */
310 public DisplayManager(Context context) {
311 mContext = context;
312 mGlobal = DisplayManagerGlobal.getInstance();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700313 }
314
315 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700316 * Gets information about a logical display.
Jeff Brown848c2dc2012-08-19 20:18:08 -0700317 *
Jeff Brownbd6e1502012-08-28 03:27:37 -0700318 * The display metrics may be adjusted to provide compatibility
319 * for legacy applications.
Jeff Brown848c2dc2012-08-19 20:18:08 -0700320 *
Jeff Brownbd6e1502012-08-28 03:27:37 -0700321 * @param displayId The logical display id.
322 * @return The display object, or null if there is no valid display with the given id.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700323 */
Jeff Brownbd6e1502012-08-28 03:27:37 -0700324 public Display getDisplay(int displayId) {
325 synchronized (mLock) {
326 return getOrCreateDisplayLocked(displayId, false /*assumeValid*/);
327 }
328 }
329
330 /**
331 * Gets all currently valid logical displays.
332 *
333 * @return An array containing all displays.
334 */
335 public Display[] getDisplays() {
Jeff Brown92130f62012-10-24 21:28:33 -0700336 return getDisplays(null);
337 }
338
339 /**
340 * Gets all currently valid logical displays of the specified category.
341 * <p>
342 * When there are multiple displays in a category the returned displays are sorted
343 * of preference. For example, if the requested category is
344 * {@link #DISPLAY_CATEGORY_PRESENTATION} and there are multiple presentation displays
345 * then the displays are sorted so that the first display in the returned array
346 * is the most preferred presentation display. The application may simply
347 * use the first display or allow the user to choose.
348 * </p>
349 *
350 * @param category The requested display category or null to return all displays.
351 * @return An array containing all displays sorted by order of preference.
352 *
353 * @see #DISPLAY_CATEGORY_PRESENTATION
354 */
355 public Display[] getDisplays(String category) {
356 final int[] displayIds = mGlobal.getDisplayIds();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700357 synchronized (mLock) {
Jeff Brown92130f62012-10-24 21:28:33 -0700358 try {
359 if (category == null) {
Jeff Brown7d00aff2013-08-02 19:03:49 -0700360 addAllDisplaysLocked(mTempDisplays, displayIds);
Jeff Brown92130f62012-10-24 21:28:33 -0700361 } else if (category.equals(DISPLAY_CATEGORY_PRESENTATION)) {
Jeff Brown7d00aff2013-08-02 19:03:49 -0700362 addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_WIFI);
363 addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_HDMI);
364 addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_OVERLAY);
365 addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_VIRTUAL);
Jeff Brown848c2dc2012-08-19 20:18:08 -0700366 }
Jeff Brown92130f62012-10-24 21:28:33 -0700367 return mTempDisplays.toArray(new Display[mTempDisplays.size()]);
368 } finally {
369 mTempDisplays.clear();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700370 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700371 }
Jeff Brown92130f62012-10-24 21:28:33 -0700372 }
373
Jeff Brown7d00aff2013-08-02 19:03:49 -0700374 private void addAllDisplaysLocked(ArrayList<Display> displays, int[] displayIds) {
375 for (int i = 0; i < displayIds.length; i++) {
376 Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/);
377 if (display != null) {
378 displays.add(display);
379 }
380 }
381 }
382
383 private void addPresentationDisplaysLocked(
Jeff Brown92130f62012-10-24 21:28:33 -0700384 ArrayList<Display> displays, int[] displayIds, int matchType) {
385 for (int i = 0; i < displayIds.length; i++) {
386 Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/);
387 if (display != null
Jeff Brown7d00aff2013-08-02 19:03:49 -0700388 && (display.getFlags() & Display.FLAG_PRESENTATION) != 0
389 && display.getType() == matchType) {
Jeff Brown92130f62012-10-24 21:28:33 -0700390 displays.add(display);
391 }
392 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700393 }
394
Jeff Brownbd6e1502012-08-28 03:27:37 -0700395 private Display getOrCreateDisplayLocked(int displayId, boolean assumeValid) {
396 Display display = mDisplays.get(displayId);
397 if (display == null) {
Bryce Leec4061522017-05-09 10:40:23 -0700398 // TODO: We cannot currently provide any override configurations for metrics on displays
399 // other than the display the context is associated with.
Yohei Yukawa5281b6b2018-10-15 07:38:25 +0800400 final Context context = mContext.getDisplayId() == displayId
Bryce Leec4061522017-05-09 10:40:23 -0700401 ? mContext : mContext.getApplicationContext();
402
403 display = mGlobal.getCompatibleDisplay(displayId, context.getResources());
Jeff Brownbd6e1502012-08-28 03:27:37 -0700404 if (display != null) {
405 mDisplays.put(displayId, display);
406 }
407 } else if (!assumeValid && !display.isValid()) {
408 display = null;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700409 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700410 return display;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700411 }
Jeff Brown98365d72012-08-19 20:30:52 -0700412
Jeff Brown98365d72012-08-19 20:30:52 -0700413 /**
414 * Registers an display listener to receive notifications about when
415 * displays are added, removed or changed.
416 *
417 * @param listener The listener to register.
418 * @param handler The handler on which the listener should be invoked, or null
419 * if the listener should be invoked on the calling thread's looper.
420 *
421 * @see #unregisterDisplayListener
422 */
423 public void registerDisplayListener(DisplayListener listener, Handler handler) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700424 mGlobal.registerDisplayListener(listener, handler);
Jeff Brown98365d72012-08-19 20:30:52 -0700425 }
426
427 /**
Michael Wrightc39d47a2014-07-08 18:07:36 -0700428 * Unregisters a display listener.
Jeff Brown98365d72012-08-19 20:30:52 -0700429 *
430 * @param listener The listener to unregister.
431 *
432 * @see #registerDisplayListener
433 */
434 public void unregisterDisplayListener(DisplayListener listener) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700435 mGlobal.unregisterDisplayListener(listener);
Jeff Brown98365d72012-08-19 20:30:52 -0700436 }
437
438 /**
Jeff Brownce468a32013-11-21 16:42:03 -0800439 * Starts scanning for available Wifi displays.
Jeff Browne08ae382012-09-07 20:36:36 -0700440 * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
Jeff Brownaf574182013-11-14 18:16:08 -0800441 * <p>
Jeff Brownce468a32013-11-21 16:42:03 -0800442 * Calls to this method nest and must be matched by an equal number of calls to
443 * {@link #stopWifiDisplayScan()}.
444 * </p><p>
445 * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
446 * </p>
447 *
448 * @hide
449 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100450 @UnsupportedAppUsage
Jeff Brownce468a32013-11-21 16:42:03 -0800451 public void startWifiDisplayScan() {
452 mGlobal.startWifiDisplayScan();
453 }
454
455 /**
456 * Stops scanning for available Wifi displays.
457 * <p>
Jeff Brownaf574182013-11-14 18:16:08 -0800458 * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
459 * </p>
460 *
Jeff Browne08ae382012-09-07 20:36:36 -0700461 * @hide
462 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100463 @UnsupportedAppUsage
Jeff Brownce468a32013-11-21 16:42:03 -0800464 public void stopWifiDisplayScan() {
465 mGlobal.stopWifiDisplayScan();
Jeff Browne08ae382012-09-07 20:36:36 -0700466 }
467
468 /**
469 * Connects to a Wifi display.
470 * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
Jeff Brown89d55462012-09-19 11:33:42 -0700471 * <p>
472 * Automatically remembers the display after a successful connection, if not
473 * already remembered.
Jeff Brownbc335452012-09-26 18:34:47 -0700474 * </p><p>
Jeff Brownaf574182013-11-14 18:16:08 -0800475 * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
Jeff Brown89d55462012-09-19 11:33:42 -0700476 * </p>
Jeff Browne08ae382012-09-07 20:36:36 -0700477 *
478 * @param deviceAddress The MAC address of the device to which we should connect.
479 * @hide
480 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100481 @UnsupportedAppUsage
Jeff Browne08ae382012-09-07 20:36:36 -0700482 public void connectWifiDisplay(String deviceAddress) {
483 mGlobal.connectWifiDisplay(deviceAddress);
484 }
485
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700486 /** @hide */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100487 @UnsupportedAppUsage
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700488 public void pauseWifiDisplay() {
489 mGlobal.pauseWifiDisplay();
490 }
491
492 /** @hide */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100493 @UnsupportedAppUsage
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700494 public void resumeWifiDisplay() {
495 mGlobal.resumeWifiDisplay();
496 }
497
Jeff Browne08ae382012-09-07 20:36:36 -0700498 /**
499 * Disconnects from the current Wifi display.
500 * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
501 * @hide
502 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100503 @UnsupportedAppUsage
Jeff Browne08ae382012-09-07 20:36:36 -0700504 public void disconnectWifiDisplay() {
505 mGlobal.disconnectWifiDisplay();
506 }
507
508 /**
Jeff Brown89d55462012-09-19 11:33:42 -0700509 * Renames a Wifi display.
510 * <p>
511 * The display must already be remembered for this call to succeed. In other words,
512 * we must already have successfully connected to the display at least once and then
513 * not forgotten it.
Jeff Brownbc335452012-09-26 18:34:47 -0700514 * </p><p>
515 * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
Jeff Brown89d55462012-09-19 11:33:42 -0700516 * </p>
517 *
518 * @param deviceAddress The MAC address of the device to rename.
519 * @param alias The alias name by which to remember the device, or null
520 * or empty if no alias should be used.
521 * @hide
522 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100523 @UnsupportedAppUsage
Jeff Brown89d55462012-09-19 11:33:42 -0700524 public void renameWifiDisplay(String deviceAddress, String alias) {
525 mGlobal.renameWifiDisplay(deviceAddress, alias);
526 }
527
528 /**
529 * Forgets a previously remembered Wifi display.
530 * <p>
531 * Automatically disconnects from the display if currently connected to it.
Jeff Brownbc335452012-09-26 18:34:47 -0700532 * </p><p>
533 * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
Jeff Brown89d55462012-09-19 11:33:42 -0700534 * </p>
535 *
536 * @param deviceAddress The MAC address of the device to forget.
537 * @hide
538 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100539 @UnsupportedAppUsage
Jeff Brown89d55462012-09-19 11:33:42 -0700540 public void forgetWifiDisplay(String deviceAddress) {
541 mGlobal.forgetWifiDisplay(deviceAddress);
542 }
543
544 /**
Jeff Browne08ae382012-09-07 20:36:36 -0700545 * Gets the current Wifi display status.
546 * Watch for changes in the status by registering a broadcast receiver for
547 * {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED}.
548 *
549 * @return The current Wifi display status.
550 * @hide
551 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100552 @UnsupportedAppUsage
Jeff Browne08ae382012-09-07 20:36:36 -0700553 public WifiDisplayStatus getWifiDisplayStatus() {
554 return mGlobal.getWifiDisplayStatus();
555 }
556
557 /**
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000558 * Set the level of color saturation to apply to the display.
559 * @param level The amount of saturation to apply, between 0 and 1 inclusive.
560 * 0 produces a grayscale image, 1 is normal.
561 *
562 * @hide
563 */
564 @SystemApi
565 @RequiresPermission(Manifest.permission.CONTROL_DISPLAY_SATURATION)
566 public void setSaturationLevel(float level) {
567 mGlobal.setSaturationLevel(level);
568 }
569
570 /**
Jeff Brown7d00aff2013-08-02 19:03:49 -0700571 * Creates a virtual display.
Michael Wrightc39d47a2014-07-08 18:07:36 -0700572 *
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700573 * @see #createVirtualDisplay(String, int, int, int, Surface, int,
574 * VirtualDisplay.Callback, Handler)
Michael Wrightc39d47a2014-07-08 18:07:36 -0700575 */
576 public VirtualDisplay createVirtualDisplay(@NonNull String name,
577 int width, int height, int densityDpi, @Nullable Surface surface, int flags) {
578 return createVirtualDisplay(name, width, height, densityDpi, surface, flags, null, null);
579 }
580
581 /**
582 * Creates a virtual display.
Jeff Browna506a6e2013-06-04 00:02:38 -0700583 * <p>
584 * The content of a virtual display is rendered to a {@link Surface} provided
Jeff Brown7d00aff2013-08-02 19:03:49 -0700585 * by the application.
Jeff Browna506a6e2013-06-04 00:02:38 -0700586 * </p><p>
Jeff Brown7d00aff2013-08-02 19:03:49 -0700587 * The virtual display should be {@link VirtualDisplay#release released}
588 * when no longer needed. Because a virtual display renders to a surface
Jeff Browna506a6e2013-06-04 00:02:38 -0700589 * provided by the application, it will be released automatically when the
590 * process terminates and all remaining windows on it will be forcibly removed.
Jeff Brown7d00aff2013-08-02 19:03:49 -0700591 * </p><p>
592 * The behavior of the virtual display depends on the flags that are provided
593 * to this method. By default, virtual displays are created to be private,
594 * non-presentation and unsecure. Permissions may be required to use certain flags.
Jeff Brown92207df2014-04-16 13:16:07 -0700595 * </p><p>
596 * As of {@link android.os.Build.VERSION_CODES#KITKAT_WATCH}, the surface may
597 * be attached or detached dynamically using {@link VirtualDisplay#setSurface}.
598 * Previously, the surface had to be non-null when {@link #createVirtualDisplay}
599 * was called and could not be changed for the lifetime of the display.
600 * </p><p>
601 * Detaching the surface that backs a virtual display has a similar effect to
602 * turning off the screen.
Jeff Browna506a6e2013-06-04 00:02:38 -0700603 * </p>
604 *
605 * @param name The name of the virtual display, must be non-empty.
606 * @param width The width of the virtual display in pixels, must be greater than 0.
607 * @param height The height of the virtual display in pixels, must be greater than 0.
608 * @param densityDpi The density of the virtual display in dpi, must be greater than 0.
609 * @param surface The surface to which the content of the virtual display should
Jeff Brown92207df2014-04-16 13:16:07 -0700610 * be rendered, or null if there is none initially.
Jeff Brown7d00aff2013-08-02 19:03:49 -0700611 * @param flags A combination of virtual display flags:
Jeff Brownd14c8c92014-01-07 18:13:09 -0800612 * {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}, {@link #VIRTUAL_DISPLAY_FLAG_PRESENTATION},
Michael Wrightc39d47a2014-07-08 18:07:36 -0700613 * {@link #VIRTUAL_DISPLAY_FLAG_SECURE}, {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY},
Michael Wright6720be42014-07-29 19:14:16 -0700614 * or {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR}.
Michael Wright75ee9fc2014-09-01 19:55:22 -0700615 * @param callback Callback to call when the state of the {@link VirtualDisplay} changes
Michael Wright5438e4e2014-07-25 14:05:42 -0700616 * @param handler The handler on which the listener should be invoked, or null
617 * if the listener should be invoked on the calling thread's looper.
Jeff Browna506a6e2013-06-04 00:02:38 -0700618 * @return The newly created virtual display, or null if the application could
619 * not create the virtual display.
Jeff Brown7d00aff2013-08-02 19:03:49 -0700620 *
621 * @throws SecurityException if the caller does not have permission to create
622 * a virtual display with the specified flags.
Jeff Browna506a6e2013-06-04 00:02:38 -0700623 */
Michael Wrightc39d47a2014-07-08 18:07:36 -0700624 public VirtualDisplay createVirtualDisplay(@NonNull String name,
625 int width, int height, int densityDpi, @Nullable Surface surface, int flags,
Michael Wright75ee9fc2014-09-01 19:55:22 -0700626 @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler) {
Santos Cordonb0608632017-04-05 10:31:15 -0700627 return createVirtualDisplay(null /* projection */, name, width, height, densityDpi, surface,
628 flags, callback, handler, null /* uniqueId */);
Michael Wrightc39d47a2014-07-08 18:07:36 -0700629 }
630
631 /** @hide */
632 public VirtualDisplay createVirtualDisplay(@Nullable MediaProjection projection,
633 @NonNull String name, int width, int height, int densityDpi, @Nullable Surface surface,
Santos Cordonb0608632017-04-05 10:31:15 -0700634 int flags, @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler,
635 @Nullable String uniqueId) {
Michael Wrightc39d47a2014-07-08 18:07:36 -0700636 return mGlobal.createVirtualDisplay(mContext, projection,
Santos Cordonb0608632017-04-05 10:31:15 -0700637 name, width, height, densityDpi, surface, flags, callback, handler, uniqueId);
Jeff Browna506a6e2013-06-04 00:02:38 -0700638 }
639
640 /**
Michael Wrighteedcbf12017-08-16 23:14:54 +0100641 * Gets the stable device display size, in pixels.
642 *
643 * This should really only be used for things like server-side filtering of available
644 * applications. Most applications don't need the level of stability guaranteed by this and
645 * should instead query either the size of the display they're currently running on or the
646 * size of the default display.
647 * @hide
648 */
649 @SystemApi
Dianne Hackborn337e01a2018-02-27 17:16:37 -0800650 @TestApi
Michael Wrighteedcbf12017-08-16 23:14:54 +0100651 public Point getStableDisplaySize() {
652 return mGlobal.getStableDisplaySize();
653 }
654
655 /**
Kenny Guy22bd0442017-10-26 00:15:54 +0100656 * Fetch {@link BrightnessChangeEvent}s.
657 * @hide until we make it a system api.
658 */
Kenny Guy0ff44892018-01-10 14:06:42 +0000659 @SystemApi
660 @TestApi
Kenny Guy29aa30e2017-11-30 13:43:46 +0000661 @RequiresPermission(Manifest.permission.BRIGHTNESS_SLIDER_USAGE)
Kenny Guy22bd0442017-10-26 00:15:54 +0100662 public List<BrightnessChangeEvent> getBrightnessEvents() {
Kenny Guy29aa30e2017-11-30 13:43:46 +0000663 return mGlobal.getBrightnessEvents(mContext.getOpPackageName());
Kenny Guy22bd0442017-10-26 00:15:54 +0100664 }
665
666 /**
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000667 * Fetch {@link AmbientBrightnessDayStats}s.
668 *
669 * @hide until we make it a system api
670 */
Peeyush Agarwal90eabcc2018-03-01 12:07:59 +0000671 @SystemApi
672 @TestApi
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000673 @RequiresPermission(Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS)
674 public List<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
675 return mGlobal.getAmbientBrightnessStats();
676 }
677
678 /**
Michael Wrighteef0e132017-11-21 17:57:52 +0000679 * Sets the global display brightness configuration.
680 *
681 * @hide
682 */
Kenny Guy0ff44892018-01-10 14:06:42 +0000683 @SystemApi
684 @TestApi
685 @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
Michael Wrighteef0e132017-11-21 17:57:52 +0000686 public void setBrightnessConfiguration(BrightnessConfiguration c) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700687 setBrightnessConfigurationForUser(c, mContext.getUserId(), mContext.getPackageName());
Michael Wrighteef0e132017-11-21 17:57:52 +0000688 }
689
690 /**
691 * Sets the global display brightness configuration for a specific user.
692 *
693 * Note this requires the INTERACT_ACROSS_USERS permission if setting the configuration for a
694 * user other than the one you're currently running as.
695 *
696 * @hide
697 */
Kenny Guy05ce8092018-01-17 13:44:20 +0000698 public void setBrightnessConfigurationForUser(BrightnessConfiguration c, int userId,
699 String packageName) {
700 mGlobal.setBrightnessConfigurationForUser(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +0000701 }
702
703 /**
Kenny Guy6d1009f2018-03-14 14:28:23 +0000704 * Gets the global display brightness configuration or the default curve if one hasn't been set.
705 *
706 * @hide
707 */
708 @SystemApi
709 @TestApi
710 @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
711 public BrightnessConfiguration getBrightnessConfiguration() {
712 return getBrightnessConfigurationForUser(mContext.getUserId());
713 }
714
715 /**
716 * Gets the global display brightness configuration or the default curve if one hasn't been set
717 * for a specific user.
718 *
719 * Note this requires the INTERACT_ACROSS_USERS permission if getting the configuration for a
720 * user other than the one you're currently running as.
721 *
722 * @hide
723 */
724 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
725 return mGlobal.getBrightnessConfigurationForUser(userId);
726 }
727
728 /**
729 * Gets the default global display brightness configuration or null one hasn't
730 * been configured.
731 *
732 * @hide
733 */
734 @SystemApi
735 @TestApi
736 @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
737 @Nullable
738 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
739 return mGlobal.getDefaultBrightnessConfiguration();
740 }
741
742 /**
Michael Wrightd8460232018-01-16 18:04:59 +0000743 * Temporarily sets the brightness of the display.
744 * <p>
745 * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
746 * </p>
747 *
748 * @param brightness The brightness value from 0 to 255.
749 *
750 * @hide Requires signature permission.
751 */
752 public void setTemporaryBrightness(int brightness) {
753 mGlobal.setTemporaryBrightness(brightness);
754 }
755
756 /**
757 * Temporarily sets the auto brightness adjustment factor.
758 * <p>
759 * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
760 * </p>
761 *
762 * @param adjustment The adjustment factor from -1.0 to 1.0.
763 *
764 * @hide Requires signature permission.
765 */
766 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
767 mGlobal.setTemporaryAutoBrightnessAdjustment(adjustment);
768 }
769
770 /**
Dan Gittik122df862018-03-28 16:59:22 +0100771 * Returns the minimum brightness curve, which guarantess that any brightness curve that dips
772 * below it is rejected by the system.
773 * This prevent auto-brightness from setting the screen so dark as to prevent the user from
774 * resetting or disabling it, and maps lux to the absolute minimum nits that are still readable
775 * in that ambient brightness.
776 *
777 * @return The minimum brightness curve (as lux values and their corresponding nits values).
778 *
779 * @hide
780 */
781 @SystemApi
782 public Pair<float[], float[]> getMinimumBrightnessCurve() {
783 return mGlobal.getMinimumBrightnessCurve();
784 }
785
786 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700787 * Listens for changes in available display devices.
788 */
789 public interface DisplayListener {
790 /**
791 * Called whenever a logical display has been added to the system.
Jeff Brownbd6e1502012-08-28 03:27:37 -0700792 * Use {@link DisplayManager#getDisplay} to get more information about
793 * the display.
Jeff Brown98365d72012-08-19 20:30:52 -0700794 *
795 * @param displayId The id of the logical display that was added.
796 */
797 void onDisplayAdded(int displayId);
798
799 /**
800 * Called whenever a logical display has been removed from the system.
801 *
802 * @param displayId The id of the logical display that was removed.
803 */
804 void onDisplayRemoved(int displayId);
805
806 /**
Siarhei Vishniakou759b88f2018-08-13 09:05:19 -0700807 * Called whenever the properties of a logical {@link android.view.Display},
808 * such as size and density, have changed.
Jeff Brown98365d72012-08-19 20:30:52 -0700809 *
810 * @param displayId The id of the logical display that changed.
811 */
812 void onDisplayChanged(int displayId);
813 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700814}