1 /* 2 * Copyright (C) 2021 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.power; 18 19 import android.annotation.RequiresApi; 20 import android.annotation.SystemApi; 21 import android.car.builtin.annotation.AddedIn; 22 import android.car.builtin.annotation.PlatformVersion; 23 import android.content.Context; 24 import android.os.Build; 25 import android.os.PowerManager; 26 import android.os.PowerManager.WakeLock; 27 28 /** 29 * Helper for PowerManager related operations. 30 * 31 * @hide 32 */ 33 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 34 public final class PowerManagerHelper { 35 36 /** See {@code PowerManager.BRIGHTNESS_ON} */ 37 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 38 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 39 public static final int BRIGHTNESS_ON = PowerManager.BRIGHTNESS_ON; 40 41 /** See {@code PowerManager.BRIGHTNESS_OFF} */ 42 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 43 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 44 public static final int BRIGHTNESS_OFF = PowerManager.BRIGHTNESS_OFF; 45 46 /** See {@code PowerManager.BRIGHTNESS_DEFAULT} */ 47 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 48 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 49 public static final int BRIGHTNESS_DEFAULT = PowerManager.BRIGHTNESS_DEFAULT; 50 51 /** See {@code PowerManager.BRIGHTNESS_INVALID} */ 52 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 53 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 54 public static final int BRIGHTNESS_INVALID = PowerManager.BRIGHTNESS_INVALID; 55 56 /** See {@code PowerManager.BRIGHTNESS_MAX} */ 57 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 58 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 59 public static final float BRIGHTNESS_MAX = PowerManager.BRIGHTNESS_MAX; 60 61 /** See {@code PowerManager.BRIGHTNESS_MIN} */ 62 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 63 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 64 public static final float BRIGHTNESS_MIN = PowerManager.BRIGHTNESS_MIN; 65 66 /** See {@code PowerManager.BRIGHTNESS_OFF_FLOAT} */ 67 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 68 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 69 public static final float BRIGHTNESS_OFF_FLOAT = PowerManager.BRIGHTNESS_OFF_FLOAT; 70 71 /** See {@code PowerManager.BRIGHTNESS_INVALID_FLOAT} */ 72 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 73 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) 74 public static final float BRIGHTNESS_INVALID_FLOAT = PowerManager.BRIGHTNESS_INVALID_FLOAT; 75 PowerManagerHelper()76 private PowerManagerHelper() { 77 throw new UnsupportedOperationException("contains only static members"); 78 } 79 80 /** 81 * Gets the maximum supported screen brightness setting. 82 * This wraps {@link PowerManager.getMaximumScreenBrightnessSetting}. 83 * 84 * @param context Context to use. 85 * @return The maximum value that can be set by the user. 86 */ 87 @AddedIn(PlatformVersion.TIRAMISU_0) getMaximumScreenBrightnessSetting(Context context)88 public static int getMaximumScreenBrightnessSetting(Context context) { 89 return context.getSystemService(PowerManager.class).getMaximumScreenBrightnessSetting(); 90 } 91 92 /** 93 * Gets the minimum supported screen brightness setting. 94 * This wraps {@link PowerManager.getMinimumScreenBrightnessSetting}. 95 * 96 * @param context Context to use. 97 * @return The minimum value that can be set by the user. 98 */ 99 @AddedIn(PlatformVersion.TIRAMISU_0) getMinimumScreenBrightnessSetting(Context context)100 public static int getMinimumScreenBrightnessSetting(Context context) { 101 return context.getSystemService(PowerManager.class).getMinimumScreenBrightnessSetting(); 102 } 103 104 /** 105 * Forces the {@link com.android.server.display.DisplayGroup#DEFAULT default display group} 106 * to turn on or off. 107 * 108 * @param context Context to use. 109 * @param on Whether to turn the display on or off. 110 * @param upTime The time when the request was issued, in the {@link SystemClock#uptimeMillis} 111 * time base. 112 */ 113 @AddedIn(PlatformVersion.TIRAMISU_0) setDisplayState(Context context, boolean on, long upTime)114 public static void setDisplayState(Context context, boolean on, long upTime) { 115 PowerManager powerManager = context.getSystemService(PowerManager.class); 116 if (on) { 117 powerManager.wakeUp(upTime, PowerManager.WAKE_REASON_UNKNOWN, "wake up by CarService"); 118 } else { 119 powerManager.goToSleep(upTime, 120 PowerManager.GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF, /* flags= */ 0); 121 } 122 } 123 124 /** 125 * Turns off the display of {@code displayId}. 126 * 127 * @param context Context to use. 128 * @param displayId The display ID to turn off. If {@code displayId} is 129 * {@link Display#INVALID_DISPLAY}, then all displays are turned off. 130 * @param upTime The time when the request was issued, in the {@link SystemClock#uptimeMillis} 131 * time base. 132 */ 133 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 134 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) goToSleep(Context context, int displayId, long upTime)135 public static void goToSleep(Context context, int displayId, long upTime) { 136 context.getSystemService(PowerManager.class).goToSleep(displayId, upTime, 137 PowerManager.GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF, /* flags= */ 0); 138 } 139 140 /** 141 * Turns off the device. 142 * 143 * @param context Context to use. 144 * @param confirm If {@code true}, shows a shutdown confirmation dialog. 145 * @param reason Code to pass to android_reboot() (e.g. "userrequested"), or {@code null}. 146 * @param wait If {@code true}, this call waits for the shutdown to complete and does not 147 * return. 148 */ 149 @AddedIn(PlatformVersion.TIRAMISU_0) shutdown(Context context, boolean confirm, String reason, boolean wait)150 public static void shutdown(Context context, boolean confirm, String reason, boolean wait) { 151 context.getSystemService(PowerManager.class).shutdown(confirm, reason, wait); 152 } 153 154 /** 155 * Acquires a wake lock for the givien display. 156 * 157 * <p>This wraps {@link PowerManager#newWakeLock(int, String, int)}. 158 * 159 * @param context Context to use. 160 * @param levelAndFlags Combination of wake lock level and flag values defining the requested 161 * behavior of the WakeLock. 162 * @param tag Your class name (or other tag) for debugging purposes. 163 * @param displayId The display id to which this wake lock is tied. 164 * 165 * @see PowerManager#PARTIAL_WAKE_LOCK 166 * @see PowerManager#FULL_WAKE_LOCK 167 * @see PowerManager#SCREEN_DIM_WAKE_LOCK 168 * @see PowerManager#SCREEN_BRIGHT_WAKE_LOCK 169 * @see PowerManager#PROXIMITY_SCREEN_OFF_WAKE_LOCK 170 * @see PowerManager#ACQUIRE_CAUSES_WAKEUP 171 * @see PowerManager#ON_AFTER_RELEASE 172 */ 173 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 174 @AddedIn(PlatformVersion.UPSIDE_DOWN_CAKE_0) newWakeLock(Context context, int levelAndFlags, String tag, int displayId)175 public static WakeLock newWakeLock(Context context, int levelAndFlags, String tag, 176 int displayId) { 177 PowerManager powerManager = context.getSystemService(PowerManager.class); 178 return powerManager.newWakeLock(levelAndFlags, tag, displayId); 179 } 180 } 181