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 wakefulnessToString(int wakefulness)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 */ wakefulnessToProtoEnum(int wakefulness)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 */ isInteractive(int wakefulness)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 */ setScreenBrightnessOverrideFromWindowManager(float brightness)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 */ setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis)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 */ setUserInactiveOverrideFromWindowManager()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 */ setMaximumScreenOffTimeoutFromDeviceAdmin(int userId, long timeMs)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 */ setDozeOverrideFromDreamManager( int screenState, int screenBrightness)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 */ setDrawWakeLockOverrideFromSidekick(boolean keepState)151 public abstract void setDrawWakeLockOverrideFromSidekick(boolean keepState); 152 getLowPowerState(int serviceType)153 public abstract PowerSaveState getLowPowerState(int serviceType); 154 registerLowPowerModeObserver(LowPowerModeListener listener)155 public abstract void registerLowPowerModeObserver(LowPowerModeListener listener); 156 157 /** 158 * Same as {@link #registerLowPowerModeObserver} but can take a lambda. 159 */ registerLowPowerModeObserver(int serviceType, Consumer<PowerSaveState> listener)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 { getServiceType()175 int getServiceType(); onLowPowerModeChanged(PowerSaveState state)176 void onLowPowerModeChanged(PowerSaveState state); 177 } 178 setDeviceIdleMode(boolean enabled)179 public abstract boolean setDeviceIdleMode(boolean enabled); 180 setLightDeviceIdleMode(boolean enabled)181 public abstract boolean setLightDeviceIdleMode(boolean enabled); 182 setDeviceIdleWhitelist(int[] appids)183 public abstract void setDeviceIdleWhitelist(int[] appids); 184 setDeviceIdleTempWhitelist(int[] appids)185 public abstract void setDeviceIdleTempWhitelist(int[] appids); 186 startUidChanges()187 public abstract void startUidChanges(); 188 finishUidChanges()189 public abstract void finishUidChanges(); 190 updateUidProcState(int uid, int procState)191 public abstract void updateUidProcState(int uid, int procState); 192 uidGone(int uid)193 public abstract void uidGone(int uid); 194 uidActive(int uid)195 public abstract void uidActive(int uid); 196 uidIdle(int uid)197 public abstract void uidIdle(int uid); 198 199 /** 200 * The hintId sent through this method should be in-line with the 201 * PowerHint defined in android/hardware/power/<version 1.0 & up>/IPower.h 202 */ powerHint(int hintId, int data)203 public abstract void powerHint(int hintId, int data); 204 205 /** 206 * Boost: It is sent when user interacting with the device, for example, 207 * touchscreen events are incoming. 208 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl 209 */ 210 public static final int BOOST_INTERACTION = 0; 211 212 /** 213 * Boost: It indicates that the framework is likely to provide a new display 214 * frame soon. This implies that the device should ensure that the display 215 * processing path is powered up and ready to receive that update. 216 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Boost.aidl 217 */ 218 public static final int BOOST_DISPLAY_UPDATE_IMMINENT = 1; 219 220 /** 221 * SetPowerBoost() indicates the device may need to boost some resources, as 222 * the load is likely to increase before the kernel governors can react. 223 * Depending on the boost, it may be appropriate to raise the frequencies of 224 * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state. 225 * 226 * @param boost Boost which is to be set with a timeout. 227 * @param durationMs The expected duration of the user's interaction, if 228 * known, or 0 if the expected duration is unknown. 229 * a negative value indicates canceling previous boost. 230 * A given platform can choose to boost some time based on durationMs, 231 * and may also pick an appropriate timeout for 0 case. 232 */ setPowerBoost(int boost, int durationMs)233 public abstract void setPowerBoost(int boost, int durationMs); 234 235 /** 236 * Mode: It indicates that the device is to allow wake up when the screen 237 * is tapped twice. 238 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 239 */ 240 public static final int MODE_DOUBLE_TAP_TO_WAKE = 0; 241 242 /** 243 * Mode: It indicates Low power mode is activated or not. Low power mode 244 * is intended to save battery at the cost of performance. 245 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 246 */ 247 public static final int MODE_LOW_POWER = 1; 248 249 /** 250 * Mode: It indicates Sustained Performance mode is activated or not. 251 * Sustained performance mode is intended to provide a consistent level of 252 * performance for a prolonged amount of time. 253 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 254 */ 255 public static final int MODE_SUSTAINED_PERFORMANCE = 2; 256 257 /** 258 * Mode: It sets the device to a fixed performance level which can be sustained 259 * under normal indoor conditions for at least 10 minutes. 260 * Fixed performance mode puts both upper and lower bounds on performance such 261 * that any workload run while in a fixed performance mode should complete in 262 * a repeatable amount of time. 263 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 264 */ 265 public static final int MODE_FIXED_PERFORMANCE = 3; 266 267 /** 268 * Mode: It indicates VR Mode is activated or not. VR mode is intended to 269 * provide minimum guarantee for performance for the amount of time the device 270 * can sustain it. 271 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 272 */ 273 public static final int MODE_VR = 4; 274 275 /** 276 * Mode: It indicates that an application has been launched. 277 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 278 */ 279 public static final int MODE_LAUNCH = 5; 280 281 /** 282 * Mode: It indicates that the device is about to enter a period of expensive 283 * rendering. 284 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 285 */ 286 public static final int MODE_EXPENSIVE_RENDERING = 6; 287 288 /** 289 * Mode: It indicates that the device is about entering/leaving interactive 290 * state or on-interactive state. 291 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 292 */ 293 public static final int MODE_INTERACTIVE = 7; 294 295 /** 296 * Mode: It indicates the device is in device idle, externally known as doze. 297 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 298 */ 299 public static final int MODE_DEVICE_IDLE = 8; 300 301 /** 302 * Mode: It indicates that display is either off or still on but is optimized 303 * for low power. 304 * Defined in hardware/interfaces/power/aidl/android/hardware/power/Mode.aidl 305 */ 306 public static final int MODE_DISPLAY_INACTIVE = 9; 307 308 /** 309 * SetPowerMode() is called to enable/disable specific hint mode, which 310 * may result in adjustment of power/performance parameters of the 311 * cpufreq governor and other controls on device side. 312 * 313 * @param mode Mode which is to be enable/disable. 314 * @param enabled true to enable, false to disable the mode. 315 */ setPowerMode(int mode, boolean enabled)316 public abstract void setPowerMode(int mode, boolean enabled); 317 318 /** Returns whether there hasn't been a user activity event for the given number of ms. */ wasDeviceIdleFor(long ms)319 public abstract boolean wasDeviceIdleFor(long ms); 320 321 /** Returns information about the last wakeup event. */ getLastWakeup()322 public abstract PowerManager.WakeData getLastWakeup(); 323 324 /** Allows power button to intercept a power key button press. */ interceptPowerKeyDown(KeyEvent event)325 public abstract boolean interceptPowerKeyDown(KeyEvent event); 326 } 327