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