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