| 1 | /* |
| 2 | * Copyright (C) 2014 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.os; |
| 18 | |
| 19 | import android.view.Display; |
| 20 | import android.view.KeyEvent; |
| 21 | |
| 22 | import java.util.function.Consumer; |
| 23 | |
| 24 | /** |
| 25 | * Power manager local system service interface. |
| 26 | * |
| 27 | * @hide Only for use within the system server. |
| 28 | */ |
| 29 | public abstract class PowerManagerInternal { |
| 30 | /** |
| 31 | * Wakefulness: The device is asleep. It can only be awoken by a call to wakeUp(). |
| 32 | * The screen should be off or in the process of being turned off by the display controller. |
| 33 | * The device typically passes through the dozing state first. |
| 34 | */ |
| 35 | public static final int WAKEFULNESS_ASLEEP = 0; |
| 36 | |
| 37 | /** |
| 38 | * Wakefulness: The device is fully awake. It can be put to sleep by a call to goToSleep(). |
| 39 | * When the user activity timeout expires, the device may start dreaming or go to sleep. |
| 40 | */ |
| 41 | public static final int WAKEFULNESS_AWAKE = 1; |
| 42 | |
| 43 | /** |
| 44 | * Wakefulness: The device is dreaming. It can be awoken by a call to wakeUp(), |
| 45 | * which ends the dream. The device goes to sleep when goToSleep() is called, when |
| 46 | * the dream ends or when unplugged. |
| 47 | * User activity may brighten the screen but does not end the dream. |
| 48 | */ |
| 49 | public static final int WAKEFULNESS_DREAMING = 2; |
| 50 | |
| 51 | /** |
| 52 | * Wakefulness: The device is dozing. It is almost asleep but is allowing a special |
| 53 | * low-power "doze" dream to run which keeps the display on but lets the application |
| 54 | * processor be suspended. It can be awoken by a call to wakeUp() which ends the dream. |
| 55 | * The device fully goes to sleep if the dream cannot be started or ends on its own. |
| 56 | */ |
| 57 | public static final int WAKEFULNESS_DOZING = 3; |
| 58 | |
| 59 | public static String wakefulnessToString(int wakefulness) { |
| 60 | switch (wakefulness) { |
| 61 | case WAKEFULNESS_ASLEEP: |
| 62 | return "Asleep"; |
| 63 | case WAKEFULNESS_AWAKE: |
| 64 | return "Awake"; |
| 65 | case WAKEFULNESS_DREAMING: |
| 66 | return "Dreaming"; |
| 67 | case WAKEFULNESS_DOZING: |
| 68 | return "Dozing"; |
| 69 | default: |
| 70 | return Integer.toString(wakefulness); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Converts platform constants to proto enums. |
| 76 | */ |
| 77 | public static int wakefulnessToProtoEnum(int wakefulness) { |
| 78 | switch (wakefulness) { |
| 79 | case WAKEFULNESS_ASLEEP: |
| 80 | return PowerManagerInternalProto.WAKEFULNESS_ASLEEP; |
| 81 | case WAKEFULNESS_AWAKE: |
| 82 | return PowerManagerInternalProto.WAKEFULNESS_AWAKE; |
| 83 | case WAKEFULNESS_DREAMING: |
| 84 | return PowerManagerInternalProto.WAKEFULNESS_DREAMING; |
| 85 | case WAKEFULNESS_DOZING: |
| 86 | return PowerManagerInternalProto.WAKEFULNESS_DOZING; |
| 87 | default: |
| 88 | return wakefulness; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns true if the wakefulness state represents an interactive state |
| 94 | * as defined by {@link android.os.PowerManager#isInteractive}. |
| 95 | */ |
| 96 | public static boolean isInteractive(int wakefulness) { |
| 97 | return wakefulness == WAKEFULNESS_AWAKE || wakefulness == WAKEFULNESS_DREAMING; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Used by the window manager to override the screen brightness based on the |
| 102 | * current foreground activity. |
| 103 | * |
| 104 | * This method must only be called by the window manager. |
| 105 | * |
| 106 | * @param brightness The overridden brightness, or Float.NaN to disable the override. |
| 107 | */ |
| 108 | public abstract void setScreenBrightnessOverrideFromWindowManager(float brightness); |
| 109 | |
| 110 | /** |
| 111 | * Used by the window manager to override the user activity timeout based on the |
| 112 | * current foreground activity. It can only be used to make the timeout shorter |
| 113 | * than usual, not longer. |
| 114 | * |
| 115 | * This method must only be called by the window manager. |
| 116 | * |
| 117 | * @param timeoutMillis The overridden timeout, or -1 to disable the override. |
| 118 | */ |
| 119 | public abstract void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis); |
| 120 | |
| 121 | /** |
| 122 | * Used by the window manager to tell the power manager that the user is no longer actively |
| 123 | * using the device. |
| 124 | */ |
| 125 | public abstract void setUserInactiveOverrideFromWindowManager(); |
| 126 | |
| 127 | /** |
| 128 | * Used by device administration to set the maximum screen off timeout. |
| 129 | * |
| 130 | * This method must only be called by the device administration policy manager. |
| 131 | */ |
| 132 | public abstract void setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs); |
| 133 | |
| 134 | /** |
| 135 | * Used by the dream manager to override certain properties while dozing. |
| 136 | * |
| 137 | * @param screenState The overridden screen state, or {@link Display#STATE_UNKNOWN} |
| 138 | * to disable the override. |
| 139 | * @param screenBrightness The overridden screen brightness, or |
| 140 | * {@link PowerManager#BRIGHTNESS_DEFAULT} to disable the override. |
| 141 | */ |
| 142 | public abstract void setDozeOverrideFromDreamManager( |
| 143 | int screenState, int screenBrightness); |
| 144 | |
| 145 | /** |
| 146 | * Used by sidekick manager to tell the power manager if it shouldn't change the display state |
| 147 | * when a draw wake lock is acquired. Some processes may grab such a wake lock to do some work |
| 148 | * in a powered-up state, but we shouldn't give up sidekick control over the display until this |
| 149 | * override is lifted. |
| 150 | */ |
| 151 | public abstract void setDrawWakeLockOverrideFromSidekick(boolean keepState); |
| 152 | |
| 153 | public abstract PowerSaveState getLowPowerState(int serviceType); |
| 154 | |
| 155 | public abstract void registerLowPowerModeObserver(LowPowerModeListener listener); |
| 156 | |
| 157 | /** |
| 158 | * Same as {@link #registerLowPowerModeObserver} but can take a lambda. |
| 159 | */ |
| 160 | public void registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener) { |
| 161 | registerLowPowerModeObserver(new LowPowerModeListener() { |
| 162 | @Override |
| 163 | public int getServiceType() { |
| 164 | return serviceType; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onLowPowerModeChanged(PowerSaveState state) { |
| 169 | listener.accept(state); |
| 170 | } |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | public interface LowPowerModeListener { |
| 175 | int getServiceType(); |
| 176 | void onLowPowerModeChanged(PowerSaveState state); |
| 177 | } |
| 178 | |
| 179 | public abstract boolean setDeviceIdleMode(boolean enabled); |
| 180 | |
| 181 | public abstract boolean setLightDeviceIdleMode(boolean enabled); |
| 182 | |
| 183 | public abstract void setDeviceIdleWhitelist(int[] appids); |
| 184 | |
| 185 | public abstract void setDeviceIdleTempWhitelist(int[] appids); |
| 186 | |
| 187 | /** |
| 188 | * Updates the Low Power Standby allowlist. |
| 189 | * |
| 190 | * @param uids UIDs that are exempt from Low Power Standby restrictions |
| 191 | */ |
| 192 | public abstract void setLowPowerStandbyAllowlist(int[] uids); |
| 193 | |
| 194 | /** |
| 195 | * Used by LowPowerStandbyController to notify the power manager that Low Power Standby's |
| 196 | * active state has changed. |
| 197 | * |
| 198 | * @param active {@code true} to activate Low Power Standby, {@code false} to turn it off. |
| 199 | */ |
| 200 | public abstract void setLowPowerStandbyActive(boolean active); |
| 201 | |
| 202 | public abstract void startUidChanges(); |
| 203 | |
| 204 | public abstract void finishUidChanges(); |
| 205 | |
| 206 | public abstract void updateUidProcState(int uid, int procState); |
| 207 | |
| 208 | public abstract void uidGone(int uid); |
| 209 | |
| 210 | public abstract void uidActive(int uid); |
| 211 | |
| 212 | public abstract void uidIdle(int uid); |
| 213 | |
| 214 | /** |
| 215 | * Boost: It is sent when user interacting with the device, for example, |
| 216 | * touchscreen events are incoming. |
| 217 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl |
| 218 | */ |
| 219 | public static final int BOOST_INTERACTION = 0; |
| 220 | |
| 221 | /** |
| 222 | * Boost: It indicates that the framework is likely to provide a new display |
| 223 | * frame soon. This implies that the device should ensure that the display |
| 224 | * processing path is powered up and ready to receive that update. |
| 225 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl |
| 226 | */ |
| 227 | public static final int BOOST_DISPLAY_UPDATE_IMMINENT = 1; |
| 228 | |
| 229 | /** |
| 230 | * SetPowerBoost() indicates the device may need to boost some resources, as |
| 231 | * the load is likely to increase before the kernel governors can react. |
| 232 | * Depending on the boost, it may be appropriate to raise the frequencies of |
| 233 | * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state. |
| 234 | * |
| 235 | * @param boost Boost which is to be set with a timeout. |
| 236 | * @param durationMs The expected duration of the user's interaction, if |
| 237 | * known, or 0 if the expected duration is unknown. |
| 238 | * a negative value indicates canceling previous boost. |
| 239 | * A given platform can choose to boost some time based on durationMs, |
| 240 | * and may also pick an appropriate timeout for 0 case. |
| 241 | */ |
| 242 | public abstract void setPowerBoost(int boost, int durationMs); |
| 243 | |
| 244 | /** |
| 245 | * Mode: It indicates that the device is to allow wake up when the screen |
| 246 | * is tapped twice. |
| 247 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 248 | */ |
| 249 | public static final int MODE_DOUBLE_TAP_TO_WAKE = 0; |
| 250 | |
| 251 | /** |
| 252 | * Mode: It indicates Low power mode is activated or not. Low power mode |
| 253 | * is intended to save battery at the cost of performance. |
| 254 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 255 | */ |
| 256 | public static final int MODE_LOW_POWER = 1; |
| 257 | |
| 258 | /** |
| 259 | * Mode: It indicates Sustained Performance mode is activated or not. |
| 260 | * Sustained performance mode is intended to provide a consistent level of |
| 261 | * performance for a prolonged amount of time. |
| 262 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 263 | */ |
| 264 | public static final int MODE_SUSTAINED_PERFORMANCE = 2; |
| 265 | |
| 266 | /** |
| 267 | * Mode: It sets the device to a fixed performance level which can be sustained |
| 268 | * under normal indoor conditions for at least 10 minutes. |
| 269 | * Fixed performance mode puts both upper and lower bounds on performance such |
| 270 | * that any workload run while in a fixed performance mode should complete in |
| 271 | * a repeatable amount of time. |
| 272 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 273 | */ |
| 274 | public static final int MODE_FIXED_PERFORMANCE = 3; |
| 275 | |
| 276 | /** |
| 277 | * Mode: It indicates VR Mode is activated or not. VR mode is intended to |
| 278 | * provide minimum guarantee for performance for the amount of time the device |
| 279 | * can sustain it. |
| 280 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 281 | */ |
| 282 | public static final int MODE_VR = 4; |
| 283 | |
| 284 | /** |
| 285 | * Mode: It indicates that an application has been launched. |
| 286 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 287 | */ |
| 288 | public static final int MODE_LAUNCH = 5; |
| 289 | |
| 290 | /** |
| 291 | * Mode: It indicates that the device is about to enter a period of expensive |
| 292 | * rendering. |
| 293 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 294 | */ |
| 295 | public static final int MODE_EXPENSIVE_RENDERING = 6; |
| 296 | |
| 297 | /** |
| 298 | * Mode: It indicates that the device is about entering/leaving interactive |
| 299 | * state or on-interactive state. |
| 300 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 301 | */ |
| 302 | public static final int MODE_INTERACTIVE = 7; |
| 303 | |
| 304 | /** |
| 305 | * Mode: It indicates the device is in device idle, externally known as doze. |
| 306 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 307 | */ |
| 308 | public static final int MODE_DEVICE_IDLE = 8; |
| 309 | |
| 310 | /** |
| 311 | * Mode: It indicates that display is either off or still on but is optimized |
| 312 | * for low power. |
| 313 | * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl |
| 314 | */ |
| 315 | public static final int MODE_DISPLAY_INACTIVE = 9; |
| 316 | |
| 317 | /** |
| 318 | * SetPowerMode() is called to enable/disable specific hint mode, which |
| 319 | * may result in adjustment of power/performance parameters of the |
| 320 | * cpufreq governor and other controls on device side. |
| 321 | * |
| 322 | * @param mode Mode which is to be enable/disable. |
| 323 | * @param enabled true to enable, false to disable the mode. |
| 324 | */ |
| 325 | public abstract void setPowerMode(int mode, boolean enabled); |
| 326 | |
| 327 | /** Returns whether there hasn't been a user activity event for the given number of ms. */ |
| 328 | public abstract boolean wasDeviceIdleFor(long ms); |
| 329 | |
| 330 | /** Returns information about the last wakeup event. */ |
| 331 | public abstract PowerManager.WakeData getLastWakeup(); |
| 332 | |
| 333 | /** Returns information about the last event to go to sleep. */ |
| 334 | public abstract PowerManager.SleepData getLastGoToSleep(); |
| 335 | |
| 336 | /** Allows power button to intercept a power key button press. */ |
| 337 | public abstract boolean interceptPowerKeyDown(KeyEvent event); |
| 338 | |
| 339 | /** |
| 340 | * Internal version of {@link android.os.PowerManager#nap} which allows for napping while the |
| 341 | * device is not awake. |
| 342 | */ |
| 343 | public abstract void nap(long eventTime, boolean allowWake); |
| 344 | |
| 345 | /** |
| 346 | * Returns true if ambient display is suppressed by any app with any token. This method will |
| 347 | * return false if ambient display is not available. |
| 348 | */ |
| 349 | public abstract boolean isAmbientDisplaySuppressed(); |
| 350 | } |