1 /*
2  * Copyright (C) 2023 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 
17 package android.car.builtin.display;
18 
19 import android.annotation.FloatRange;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.content.Context;
24 import android.hardware.display.DisplayManager;
25 import android.hardware.display.DisplayManager.DisplayListener;
26 import android.hardware.display.DisplayManager.EventsMask;
27 import android.os.Handler;
28 import android.view.Display;
29 
30 /**
31  * Helper for DisplayManager related operations.
32  *
33  * @hide
34  */
35 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
36 public final class DisplayManagerHelper {
37 
38     /**
39      * Event type for when a new display is added.
40      *
41      * @see #registerDisplayListener(DisplayListener, Handler, long)
42      */
43     public static final long EVENT_FLAG_DISPLAY_ADDED = DisplayManager.EVENT_FLAG_DISPLAY_ADDED;
44 
45     /**
46      * Event type for when a display is removed.
47      *
48      * @see #registerDisplayListener(DisplayListener, Handler, long)
49      */
50     public static final long EVENT_FLAG_DISPLAY_REMOVED = DisplayManager.EVENT_FLAG_DISPLAY_REMOVED;
51 
52     /**
53      * Event type for when a display is changed.
54      *
55      * @see #registerDisplayListener(DisplayListener, Handler, long)
56      */
57     public static final long EVENT_FLAG_DISPLAY_CHANGED = DisplayManager.EVENT_FLAG_DISPLAY_CHANGED;
58 
59     /**
60      * Event flag to register for a display's brightness changes. This notification is sent
61      * through the {@link DisplayListener#onDisplayChanged} callback method. New brightness
62      * values can be retrieved via {@link android.view.Display#getBrightnessInfo()}.
63      *
64      * @see #registerDisplayListener(DisplayListener, Handler, long)
65      */
66     public static final long EVENT_FLAG_DISPLAY_BRIGHTNESS =
67             DisplayManager.EVENT_FLAG_DISPLAY_BRIGHTNESS;
68 
DisplayManagerHelper()69     private DisplayManagerHelper() {
70         throw new UnsupportedOperationException("contains only static members");
71     }
72 
73     /**
74      * Registers a display listener to receive notifications about given display event types.
75      *
76      * @param context The context to use.
77      * @param listener The listener to register.
78      * @param handler The handler on which the listener should be invoked, or null
79      * if the listener should be invoked on the calling thread's looper.
80      * @param eventsMask A bitmask of the event types for which this listener is subscribed.
81      *
82      * @see DisplayManager#EVENT_FLAG_DISPLAY_ADDED
83      * @see DisplayManager#EVENT_FLAG_DISPLAY_CHANGED
84      * @see DisplayManager#EVENT_FLAG_DISPLAY_REMOVED
85      * @see DisplayManager#EVENT_FLAG_DISPLAY_BRIGHTNESS
86      * @see DisplayManager#registerDisplayListener(DisplayListener, Handler)
87      * @see DisplayManager#unregisterDisplayListener
88      */
registerDisplayListener(Context context, DisplayListener listener, Handler handler, @EventsMask long eventsMask)89     public static void registerDisplayListener(Context context, DisplayListener listener,
90             Handler handler, @EventsMask long eventsMask) {
91         DisplayManager displayManager = context.getSystemService(DisplayManager.class);
92         displayManager.registerDisplayListener(listener, handler, eventsMask);
93     }
94 
95     /**
96      * Gets the brightness of the specified display.
97      *
98      * @param context Context to use.
99      * @param displayId The display of which brightness value to get from.
100      */
getBrightness(Context context, int displayId)101     public static float getBrightness(Context context, int displayId) {
102         DisplayManager displayManager = context.getSystemService(DisplayManager.class);
103         return displayManager.getBrightness(displayId);
104     }
105 
106     /**
107      * Sets the brightness of the specified display.
108      *
109      * @param context Context to use.
110      * @param displayId the logical display id
111      * @param brightness The brightness value from 0.0f to 1.0f.
112      */
setBrightness(Context context, int displayId, @FloatRange(from = 0f, to = 1f) float brightness)113     public static void setBrightness(Context context, int displayId,
114             @FloatRange(from = 0f, to = 1f) float brightness) {
115         DisplayManager displayManager = context.getSystemService(DisplayManager.class);
116         displayManager.setBrightness(displayId, brightness);
117     }
118 
119     /**
120      * See {@link Display#getUniqueId()}.
121      */
122     @Nullable
getUniqueId(@onNull Display display)123     public static String getUniqueId(@NonNull Display display) {
124         return display.getUniqueId();
125     }
126 }
127