DisplayManager.java revision 462e29da9ba854eb3651dd9664b09a2852a05141
1/*
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
19import android.Manifest;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.annotation.RequiresPermission;
23import android.annotation.SystemApi;
24import android.annotation.SystemService;
25import android.annotation.TestApi;
26import android.app.KeyguardManager;
27import android.content.Context;
28import android.graphics.Point;
29import android.media.projection.MediaProjection;
30import android.os.Handler;
31import android.util.SparseArray;
32import android.view.Display;
33import android.view.Surface;
34
35import java.util.ArrayList;
36import java.util.List;
37
38/**
39 * Manages the properties of attached displays.
40 */
41@SystemService(Context.DISPLAY_SERVICE)
42public final class DisplayManager {
43    private static final String TAG = "DisplayManager";
44    private static final boolean DEBUG = false;
45
46    private final Context mContext;
47    private final DisplayManagerGlobal mGlobal;
48
49    private final Object mLock = new Object();
50    private final SparseArray<Display> mDisplays = new SparseArray<Display>();
51
52    private final ArrayList<Display> mTempDisplays = new ArrayList<Display>();
53
54    /**
55     * Broadcast receiver that indicates when the Wifi display status changes.
56     * <p>
57     * The status is provided as a {@link WifiDisplayStatus} object in the
58     * {@link #EXTRA_WIFI_DISPLAY_STATUS} extra.
59     * </p><p>
60     * This broadcast is only sent to registered receivers and can only be sent by the system.
61     * </p>
62     * @hide
63     */
64    public static final String ACTION_WIFI_DISPLAY_STATUS_CHANGED =
65            "android.hardware.display.action.WIFI_DISPLAY_STATUS_CHANGED";
66
67    /**
68     * Contains a {@link WifiDisplayStatus} object.
69     * @hide
70     */
71    public static final String EXTRA_WIFI_DISPLAY_STATUS =
72            "android.hardware.display.extra.WIFI_DISPLAY_STATUS";
73
74    /**
75     * Display category: Presentation displays.
76     * <p>
77     * This category can be used to identify secondary displays that are suitable for
78     * use as presentation displays such as HDMI or Wireless displays.  Applications
79     * may automatically project their content to presentation displays to provide
80     * richer second screen experiences.
81     * </p>
82     *
83     * @see android.app.Presentation
84     * @see Display#FLAG_PRESENTATION
85     * @see #getDisplays(String)
86     */
87    public static final String DISPLAY_CATEGORY_PRESENTATION =
88            "android.hardware.display.category.PRESENTATION";
89
90    /**
91     * Virtual display flag: Create a public display.
92     *
93     * <h3>Public virtual displays</h3>
94     * <p>
95     * When this flag is set, the virtual display is public.
96     * </p><p>
97     * A public virtual display behaves just like most any other display that is connected
98     * to the system such as an HDMI or Wireless display.  Applications can open
99     * windows on the display and the system may mirror the contents of other displays
100     * onto it.
101     * </p><p>
102     * Creating a public virtual display that isn't restricted to own-content only implicitly
103     * creates an auto-mirroring display. See {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR} for
104     * restrictions on who is allowed to create an auto-mirroring display.
105     * </p>
106     *
107     * <h3>Private virtual displays</h3>
108     * <p>
109     * When this flag is not set, the virtual display is private as defined by the
110     * {@link Display#FLAG_PRIVATE} display flag.
111     * </p>
112     *
113     * <p>
114     * A private virtual display belongs to the application that created it.  Only the a owner of a
115     * private virtual display and the apps that are already on that display are allowed to place
116     * windows upon it.  The private virtual display also does not participate in display mirroring:
117     * it will neither receive mirrored content from another display nor allow its own content to be
118     * mirrored elsewhere.  More precisely, the only processes that are allowed to enumerate or
119     * interact with the private display are those that have the same UID as the application that
120     * originally created the private virtual display or as the activities that are already on that
121     * display.
122     * </p>
123     *
124     * @see #createVirtualDisplay
125     * @see #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
126     * @see #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
127     */
128    public static final int VIRTUAL_DISPLAY_FLAG_PUBLIC = 1 << 0;
129
130    /**
131     * Virtual display flag: Create a presentation display.
132     *
133     * <h3>Presentation virtual displays</h3>
134     * <p>
135     * When this flag is set, the virtual display is registered as a presentation
136     * display in the {@link #DISPLAY_CATEGORY_PRESENTATION presentation display category}.
137     * Applications may automatically project their content to presentation displays
138     * to provide richer second screen experiences.
139     * </p>
140     *
141     * <h3>Non-presentation virtual displays</h3>
142     * <p>
143     * When this flag is not set, the virtual display is not registered as a presentation
144     * display.  Applications can still project their content on the display but they
145     * will typically not do so automatically.  This option is appropriate for
146     * more special-purpose displays.
147     * </p>
148     *
149     * @see android.app.Presentation
150     * @see #createVirtualDisplay
151     * @see #DISPLAY_CATEGORY_PRESENTATION
152     * @see Display#FLAG_PRESENTATION
153     */
154    public static final int VIRTUAL_DISPLAY_FLAG_PRESENTATION = 1 << 1;
155
156    /**
157     * Virtual display flag: Create a secure display.
158     *
159     * <h3>Secure virtual displays</h3>
160     * <p>
161     * When this flag is set, the virtual display is considered secure as defined
162     * by the {@link Display#FLAG_SECURE} display flag.  The caller promises to take
163     * reasonable measures, such as over-the-air encryption, to prevent the contents
164     * of the display from being intercepted or recorded on a persistent medium.
165     * </p><p>
166     * Creating a secure virtual display requires the
167     * {@link android.Manifest.permission#CAPTURE_SECURE_VIDEO_OUTPUT} permission.
168     * This permission is reserved for use by system components and is not available to
169     * third-party applications.
170     * </p>
171     *
172     * <h3>Non-secure virtual displays</h3>
173     * <p>
174     * When this flag is not set, the virtual display is considered unsecure.
175     * The content of secure windows will be blanked if shown on this display.
176     * </p>
177     *
178     * @see Display#FLAG_SECURE
179     * @see #createVirtualDisplay
180     */
181    public static final int VIRTUAL_DISPLAY_FLAG_SECURE = 1 << 2;
182
183    /**
184     * Virtual display flag: Only show this display's own content; do not mirror
185     * the content of another display.
186     *
187     * <p>
188     * This flag is used in conjunction with {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}.
189     * Ordinarily public virtual displays will automatically mirror the content of the
190     * default display if they have no windows of their own.  When this flag is
191     * specified, the virtual display will only ever show its own content and
192     * will be blanked instead if it has no windows.
193     * </p>
194     *
195     * <p>
196     * This flag is mutually exclusive with {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR}.  If both
197     * flags are specified then the own-content only behavior will be applied.
198     * </p>
199     *
200     * <p>
201     * This behavior of this flag is implied whenever neither {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}
202     * nor {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR} have been set.  This flag is only required to
203     * override the default behavior when creating a public display.
204     * </p>
205     *
206     * @see #createVirtualDisplay
207     */
208    public static final int VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY = 1 << 3;
209
210
211    /**
212     * Virtual display flag: Allows content to be mirrored on private displays when no content is
213     * being shown.
214     *
215     * <p>
216     * This flag is mutually exclusive with {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY}.
217     * If both flags are specified then the own-content only behavior will be applied.
218     * </p>
219     *
220     * <p>
221     * The behavior of this flag is implied whenever {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC} is set
222     * and {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY} has not been set.   This flag is only
223     * required to override the default behavior when creating a private display.
224     * </p>
225     *
226     * <p>
227     * Creating an auto-mirroing virtual display requires the
228     * {@link android.Manifest.permission#CAPTURE_VIDEO_OUTPUT}
229     * or {@link android.Manifest.permission#CAPTURE_SECURE_VIDEO_OUTPUT} permission.
230     * These permissions are reserved for use by system components and are not available to
231     * third-party applications.
232     *
233     * Alternatively, an appropriate {@link MediaProjection} may be used to create an
234     * auto-mirroring virtual display.
235     * </p>
236     *
237     * @see #createVirtualDisplay
238     */
239    public static final int VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR = 1 << 4;
240
241    /**
242     * Virtual display flag: Allows content to be displayed on private virtual displays when
243     * keyguard is shown but is insecure.
244     *
245     * <p>
246     * This might be used in a case when the content of a virtual display is captured and sent to an
247     * external hardware display that is not visible to the system directly. This flag will allow
248     * the continued display of content while other displays will be covered by a keyguard which
249     * doesn't require providing credentials to unlock. This means that there is either no password
250     * or other authentication method set, or the device is in a trusted state -
251     * {@link android.service.trust.TrustAgentService} has available and active trust agent.
252     * </p><p>
253     * This flag can only be applied to private displays as defined by the
254     * {@link Display#FLAG_PRIVATE} display flag. It is mutually exclusive with
255     * {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}. If both flags are specified then this flag's behavior
256     * will not be applied.
257     * </p>
258     *
259     * @see #createVirtualDisplay
260     * @see KeyguardManager#isDeviceSecure()
261     * @see KeyguardManager#isDeviceLocked()
262     * @hide
263     */
264    // TODO: Update name and documentation and un-hide the flag. Don't change the value before that.
265    public static final int VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD = 1 << 5;
266
267    /**
268     * Virtual display flag: Specifies that the virtual display can be associated with a
269     * touchpad device that matches its uniqueId.
270     *
271     * @see #createVirtualDisplay
272     * @hide
273     */
274    public static final int VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH = 1 << 6;
275
276    /**
277     * Virtual display flag: Indicates that the orientation of this display device is coupled to
278     * the rotation of its associated logical display.
279     *
280     * @see #createVirtualDisplay
281     * @hide
282     */
283    public static final int VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT = 1 << 7;
284
285    /**
286     * Virtual display flag: Indicates that the contents will be destroyed once
287     * the display is removed.
288     *
289     * Public virtual displays without this flag will move their content to main display
290     * stack once they're removed. Private vistual displays will always destroy their
291     * content on removal even without this flag.
292     *
293     * @see #createVirtualDisplay
294     * @hide
295     */
296    public static final int VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL = 1 << 8;
297
298    /** @hide */
299    public DisplayManager(Context context) {
300        mContext = context;
301        mGlobal = DisplayManagerGlobal.getInstance();
302    }
303
304    /**
305     * Gets information about a logical display.
306     *
307     * The display metrics may be adjusted to provide compatibility
308     * for legacy applications.
309     *
310     * @param displayId The logical display id.
311     * @return The display object, or null if there is no valid display with the given id.
312     */
313    public Display getDisplay(int displayId) {
314        synchronized (mLock) {
315            return getOrCreateDisplayLocked(displayId, false /*assumeValid*/);
316        }
317    }
318
319    /**
320     * Gets all currently valid logical displays.
321     *
322     * @return An array containing all displays.
323     */
324    public Display[] getDisplays() {
325        return getDisplays(null);
326    }
327
328    /**
329     * Gets all currently valid logical displays of the specified category.
330     * <p>
331     * When there are multiple displays in a category the returned displays are sorted
332     * of preference.  For example, if the requested category is
333     * {@link #DISPLAY_CATEGORY_PRESENTATION} and there are multiple presentation displays
334     * then the displays are sorted so that the first display in the returned array
335     * is the most preferred presentation display.  The application may simply
336     * use the first display or allow the user to choose.
337     * </p>
338     *
339     * @param category The requested display category or null to return all displays.
340     * @return An array containing all displays sorted by order of preference.
341     *
342     * @see #DISPLAY_CATEGORY_PRESENTATION
343     */
344    public Display[] getDisplays(String category) {
345        final int[] displayIds = mGlobal.getDisplayIds();
346        synchronized (mLock) {
347            try {
348                if (category == null) {
349                    addAllDisplaysLocked(mTempDisplays, displayIds);
350                } else if (category.equals(DISPLAY_CATEGORY_PRESENTATION)) {
351                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_WIFI);
352                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_HDMI);
353                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_OVERLAY);
354                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_VIRTUAL);
355                }
356                return mTempDisplays.toArray(new Display[mTempDisplays.size()]);
357            } finally {
358                mTempDisplays.clear();
359            }
360        }
361    }
362
363    private void addAllDisplaysLocked(ArrayList<Display> displays, int[] displayIds) {
364        for (int i = 0; i < displayIds.length; i++) {
365            Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/);
366            if (display != null) {
367                displays.add(display);
368            }
369        }
370    }
371
372    private void addPresentationDisplaysLocked(
373            ArrayList<Display> displays, int[] displayIds, int matchType) {
374        for (int i = 0; i < displayIds.length; i++) {
375            Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/);
376            if (display != null
377                    && (display.getFlags() & Display.FLAG_PRESENTATION) != 0
378                    && display.getType() == matchType) {
379                displays.add(display);
380            }
381        }
382    }
383
384    private Display getOrCreateDisplayLocked(int displayId, boolean assumeValid) {
385        Display display = mDisplays.get(displayId);
386        if (display == null) {
387            // TODO: We cannot currently provide any override configurations for metrics on displays
388            // other than the display the context is associated with.
389            final Context context = mContext.getDisplay().getDisplayId() == displayId
390                    ? mContext : mContext.getApplicationContext();
391
392            display = mGlobal.getCompatibleDisplay(displayId, context.getResources());
393            if (display != null) {
394                mDisplays.put(displayId, display);
395            }
396        } else if (!assumeValid && !display.isValid()) {
397            display = null;
398        }
399        return display;
400    }
401
402    /**
403     * Registers an display listener to receive notifications about when
404     * displays are added, removed or changed.
405     *
406     * @param listener The listener to register.
407     * @param handler The handler on which the listener should be invoked, or null
408     * if the listener should be invoked on the calling thread's looper.
409     *
410     * @see #unregisterDisplayListener
411     */
412    public void registerDisplayListener(DisplayListener listener, Handler handler) {
413        mGlobal.registerDisplayListener(listener, handler);
414    }
415
416    /**
417     * Unregisters a display listener.
418     *
419     * @param listener The listener to unregister.
420     *
421     * @see #registerDisplayListener
422     */
423    public void unregisterDisplayListener(DisplayListener listener) {
424        mGlobal.unregisterDisplayListener(listener);
425    }
426
427    /**
428     * Starts scanning for available Wifi displays.
429     * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
430     * <p>
431     * Calls to this method nest and must be matched by an equal number of calls to
432     * {@link #stopWifiDisplayScan()}.
433     * </p><p>
434     * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
435     * </p>
436     *
437     * @hide
438     */
439    public void startWifiDisplayScan() {
440        mGlobal.startWifiDisplayScan();
441    }
442
443    /**
444     * Stops scanning for available Wifi displays.
445     * <p>
446     * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
447     * </p>
448     *
449     * @hide
450     */
451    public void stopWifiDisplayScan() {
452        mGlobal.stopWifiDisplayScan();
453    }
454
455    /**
456     * Connects to a Wifi display.
457     * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
458     * <p>
459     * Automatically remembers the display after a successful connection, if not
460     * already remembered.
461     * </p><p>
462     * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
463     * </p>
464     *
465     * @param deviceAddress The MAC address of the device to which we should connect.
466     * @hide
467     */
468    public void connectWifiDisplay(String deviceAddress) {
469        mGlobal.connectWifiDisplay(deviceAddress);
470    }
471
472    /** @hide */
473    public void pauseWifiDisplay() {
474        mGlobal.pauseWifiDisplay();
475    }
476
477    /** @hide */
478    public void resumeWifiDisplay() {
479        mGlobal.resumeWifiDisplay();
480    }
481
482    /**
483     * Disconnects from the current Wifi display.
484     * The results are sent as a {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED} broadcast.
485     * @hide
486     */
487    public void disconnectWifiDisplay() {
488        mGlobal.disconnectWifiDisplay();
489    }
490
491    /**
492     * Renames a Wifi display.
493     * <p>
494     * The display must already be remembered for this call to succeed.  In other words,
495     * we must already have successfully connected to the display at least once and then
496     * not forgotten it.
497     * </p><p>
498     * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
499     * </p>
500     *
501     * @param deviceAddress The MAC address of the device to rename.
502     * @param alias The alias name by which to remember the device, or null
503     * or empty if no alias should be used.
504     * @hide
505     */
506    public void renameWifiDisplay(String deviceAddress, String alias) {
507        mGlobal.renameWifiDisplay(deviceAddress, alias);
508    }
509
510    /**
511     * Forgets a previously remembered Wifi display.
512     * <p>
513     * Automatically disconnects from the display if currently connected to it.
514     * </p><p>
515     * Requires {@link android.Manifest.permission#CONFIGURE_WIFI_DISPLAY}.
516     * </p>
517     *
518     * @param deviceAddress The MAC address of the device to forget.
519     * @hide
520     */
521    public void forgetWifiDisplay(String deviceAddress) {
522        mGlobal.forgetWifiDisplay(deviceAddress);
523    }
524
525    /**
526     * Gets the current Wifi display status.
527     * Watch for changes in the status by registering a broadcast receiver for
528     * {@link #ACTION_WIFI_DISPLAY_STATUS_CHANGED}.
529     *
530     * @return The current Wifi display status.
531     * @hide
532     */
533    public WifiDisplayStatus getWifiDisplayStatus() {
534        return mGlobal.getWifiDisplayStatus();
535    }
536
537    /**
538     * Set the level of color saturation to apply to the display.
539     * @param level The amount of saturation to apply, between 0 and 1 inclusive.
540     * 0 produces a grayscale image, 1 is normal.
541     *
542     * @hide
543     */
544    @SystemApi
545    @RequiresPermission(Manifest.permission.CONTROL_DISPLAY_SATURATION)
546    public void setSaturationLevel(float level) {
547        mGlobal.setSaturationLevel(level);
548    }
549
550    /**
551     * Creates a virtual display.
552     *
553     * @see #createVirtualDisplay(String, int, int, int, Surface, int,
554     * VirtualDisplay.Callback, Handler)
555     */
556    public VirtualDisplay createVirtualDisplay(@NonNull String name,
557            int width, int height, int densityDpi, @Nullable Surface surface, int flags) {
558        return createVirtualDisplay(name, width, height, densityDpi, surface, flags, null, null);
559    }
560
561    /**
562     * Creates a virtual display.
563     * <p>
564     * The content of a virtual display is rendered to a {@link Surface} provided
565     * by the application.
566     * </p><p>
567     * The virtual display should be {@link VirtualDisplay#release released}
568     * when no longer needed.  Because a virtual display renders to a surface
569     * provided by the application, it will be released automatically when the
570     * process terminates and all remaining windows on it will be forcibly removed.
571     * </p><p>
572     * The behavior of the virtual display depends on the flags that are provided
573     * to this method.  By default, virtual displays are created to be private,
574     * non-presentation and unsecure.  Permissions may be required to use certain flags.
575     * </p><p>
576     * As of {@link android.os.Build.VERSION_CODES#KITKAT_WATCH}, the surface may
577     * be attached or detached dynamically using {@link VirtualDisplay#setSurface}.
578     * Previously, the surface had to be non-null when {@link #createVirtualDisplay}
579     * was called and could not be changed for the lifetime of the display.
580     * </p><p>
581     * Detaching the surface that backs a virtual display has a similar effect to
582     * turning off the screen.
583     * </p>
584     *
585     * @param name The name of the virtual display, must be non-empty.
586     * @param width The width of the virtual display in pixels, must be greater than 0.
587     * @param height The height of the virtual display in pixels, must be greater than 0.
588     * @param densityDpi The density of the virtual display in dpi, must be greater than 0.
589     * @param surface The surface to which the content of the virtual display should
590     * be rendered, or null if there is none initially.
591     * @param flags A combination of virtual display flags:
592     * {@link #VIRTUAL_DISPLAY_FLAG_PUBLIC}, {@link #VIRTUAL_DISPLAY_FLAG_PRESENTATION},
593     * {@link #VIRTUAL_DISPLAY_FLAG_SECURE}, {@link #VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY},
594     * or {@link #VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR}.
595     * @param callback Callback to call when the state of the {@link VirtualDisplay} changes
596     * @param handler The handler on which the listener should be invoked, or null
597     * if the listener should be invoked on the calling thread's looper.
598     * @return The newly created virtual display, or null if the application could
599     * not create the virtual display.
600     *
601     * @throws SecurityException if the caller does not have permission to create
602     * a virtual display with the specified flags.
603     */
604    public VirtualDisplay createVirtualDisplay(@NonNull String name,
605            int width, int height, int densityDpi, @Nullable Surface surface, int flags,
606            @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler) {
607        return createVirtualDisplay(null /* projection */, name, width, height, densityDpi, surface,
608                flags, callback, handler, null /* uniqueId */);
609    }
610
611    /** @hide */
612    public VirtualDisplay createVirtualDisplay(@Nullable MediaProjection projection,
613            @NonNull String name, int width, int height, int densityDpi, @Nullable Surface surface,
614            int flags, @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler,
615            @Nullable String uniqueId) {
616        return mGlobal.createVirtualDisplay(mContext, projection,
617                name, width, height, densityDpi, surface, flags, callback, handler, uniqueId);
618    }
619
620    /**
621     * Gets the stable device display size, in pixels.
622     *
623     * This should really only be used for things like server-side filtering of available
624     * applications. Most applications don't need the level of stability guaranteed by this and
625     * should instead query either the size of the display they're currently running on or the
626     * size of the default display.
627     * @hide
628     */
629    @SystemApi
630    @TestApi
631    public Point getStableDisplaySize() {
632        return mGlobal.getStableDisplaySize();
633    }
634
635    /**
636     * Fetch {@link BrightnessChangeEvent}s.
637     * @hide until we make it a system api.
638     */
639    @SystemApi
640    @TestApi
641    @RequiresPermission(Manifest.permission.BRIGHTNESS_SLIDER_USAGE)
642    public List<BrightnessChangeEvent> getBrightnessEvents() {
643        return mGlobal.getBrightnessEvents(mContext.getOpPackageName());
644    }
645
646    /**
647     * Fetch {@link AmbientBrightnessDayStats}s.
648     *
649     * @hide until we make it a system api
650     */
651    @SystemApi
652    @TestApi
653    @RequiresPermission(Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS)
654    public List<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
655        return mGlobal.getAmbientBrightnessStats();
656    }
657
658    /**
659     * Sets the global display brightness configuration.
660     *
661     * @hide
662     */
663    @SystemApi
664    @TestApi
665    @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
666    public void setBrightnessConfiguration(BrightnessConfiguration c) {
667        setBrightnessConfigurationForUser(c, mContext.getUserId(), mContext.getPackageName());
668    }
669
670    /**
671     * Sets the global display brightness configuration for a specific user.
672     *
673     * Note this requires the INTERACT_ACROSS_USERS permission if setting the configuration for a
674     * user other than the one you're currently running as.
675     *
676     * @hide
677     */
678    public void setBrightnessConfigurationForUser(BrightnessConfiguration c, int userId,
679            String packageName) {
680        mGlobal.setBrightnessConfigurationForUser(c, userId, packageName);
681    }
682
683    /**
684     * Gets the global display brightness configuration or the default curve if one hasn't been set.
685     *
686     * @hide
687     */
688    @SystemApi
689    @TestApi
690    @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
691    public BrightnessConfiguration getBrightnessConfiguration() {
692        return getBrightnessConfigurationForUser(mContext.getUserId());
693    }
694
695    /**
696     * Gets the global display brightness configuration or the default curve if one hasn't been set
697     * for a specific user.
698     *
699     * Note this requires the INTERACT_ACROSS_USERS permission if getting the configuration for a
700     * user other than the one you're currently running as.
701     *
702     * @hide
703     */
704    public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
705        return mGlobal.getBrightnessConfigurationForUser(userId);
706    }
707
708    /**
709     * Gets the default global display brightness configuration or null one hasn't
710     * been configured.
711     *
712     * @hide
713     */
714    @SystemApi
715    @TestApi
716    @RequiresPermission(Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS)
717    @Nullable
718    public BrightnessConfiguration getDefaultBrightnessConfiguration() {
719        return mGlobal.getDefaultBrightnessConfiguration();
720    }
721
722    /**
723     * Temporarily sets the brightness of the display.
724     * <p>
725     * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
726     * </p>
727     *
728     * @param brightness The brightness value from 0 to 255.
729     *
730     * @hide Requires signature permission.
731     */
732    public void setTemporaryBrightness(int brightness) {
733        mGlobal.setTemporaryBrightness(brightness);
734    }
735
736    /**
737     * Temporarily sets the auto brightness adjustment factor.
738     * <p>
739     * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
740     * </p>
741     *
742     * @param adjustment The adjustment factor from -1.0 to 1.0.
743     *
744     * @hide Requires signature permission.
745     */
746    public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
747        mGlobal.setTemporaryAutoBrightnessAdjustment(adjustment);
748    }
749
750    /**
751     * Listens for changes in available display devices.
752     */
753    public interface DisplayListener {
754        /**
755         * Called whenever a logical display has been added to the system.
756         * Use {@link DisplayManager#getDisplay} to get more information about
757         * the display.
758         *
759         * @param displayId The id of the logical display that was added.
760         */
761        void onDisplayAdded(int displayId);
762
763        /**
764         * Called whenever a logical display has been removed from the system.
765         *
766         * @param displayId The id of the logical display that was removed.
767         */
768        void onDisplayRemoved(int displayId);
769
770        /**
771         * Called whenever the properties of a logical display have changed.
772         *
773         * @param displayId The id of the logical display that changed.
774         */
775        void onDisplayChanged(int displayId);
776    }
777}
778