PowerManager.java revision 7e9f4eb2608148436cef36c9969bf8a599b39e72
1/* 2 * Copyright (C) 2007 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 17package android.os; 18 19import android.util.Log; 20 21import com.android.internal.os.RuntimeInit; 22 23/** 24 * This class gives you control of the power state of the device. 25 * 26 * <p><b>Device battery life will be significantly affected by the use of this API.</b> Do not 27 * acquire WakeLocks unless you really need them, use the minimum levels possible, and be sure 28 * to release it as soon as you can. 29 * 30 * <p>You can obtain an instance of this class by calling 31 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. 32 * 33 * <p>The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}. This will 34 * create a {@link PowerManager.WakeLock} object. You can then use methods on this object to 35 * control the power state of the device. In practice it's quite simple: 36 * 37 * {@samplecode 38 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 39 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); 40 * wl.acquire(); 41 * ..screen will stay on during this section.. 42 * wl.release(); 43 * } 44 * 45 * <p>The following flags are defined, with varying effects on system power. <i>These flags are 46 * mutually exclusive - you may only specify one of them.</i> 47 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 48 * 49 * <thead> 50 * <tr><th>Flag Value</th> 51 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr> 52 * </thead> 53 * 54 * <tbody> 55 * <tr><th>{@link #PARTIAL_WAKE_LOCK}</th> 56 * <td>On*</td> <td>Off</td> <td>Off</td> 57 * </tr> 58 * 59 * <tr><th>{@link #SCREEN_DIM_WAKE_LOCK}</th> 60 * <td>On</td> <td>Dim</td> <td>Off</td> 61 * </tr> 62 * 63 * <tr><th>{@link #SCREEN_BRIGHT_WAKE_LOCK}</th> 64 * <td>On</td> <td>Bright</td> <td>Off</td> 65 * </tr> 66 * 67 * <tr><th>{@link #FULL_WAKE_LOCK}</th> 68 * <td>On</td> <td>Bright</td> <td>Bright</td> 69 * </tr> 70 * </tbody> 71 * </table> 72 * 73 * <p>*<i>If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers 74 * and even after the user presses the power button. In all other wakelocks, the CPU will run, but 75 * the user can still put the device to sleep using the power button.</i> 76 * 77 * <p>In addition, you can add two more flags, which affect behavior of the screen only. <i>These 78 * flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.</i> 79 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 80 * 81 * <thead> 82 * <tr><th>Flag Value</th> <th>Description</th></tr> 83 * </thead> 84 * 85 * <tbody> 86 * <tr><th>{@link #ACQUIRE_CAUSES_WAKEUP}</th> 87 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause 88 * the illumination to remain on once it turns on (e.g. from user activity). This flag 89 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is 90 * acquired. A typical use would be for notifications which are important for the user to 91 * see immediately.</td> 92 * </tr> 93 * 94 * <tr><th>{@link #ON_AFTER_RELEASE}</th> 95 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is 96 * released, causing the illumination to remain on a bit longer. This can be used to 97 * reduce flicker if you are cycling between wake lock conditions.</td> 98 * </tr> 99 * </tbody> 100 * </table> 101 * 102 * 103 */ 104public class PowerManager 105{ 106 private static final String TAG = "PowerManager"; 107 108 /** 109 * These internal values define the underlying power elements that we might 110 * want to control individually. Eventually we'd like to expose them. 111 */ 112 private static final int WAKE_BIT_CPU_STRONG = 1; 113 private static final int WAKE_BIT_CPU_WEAK = 2; 114 private static final int WAKE_BIT_SCREEN_DIM = 4; 115 private static final int WAKE_BIT_SCREEN_BRIGHT = 8; 116 private static final int WAKE_BIT_KEYBOARD_BRIGHT = 16; 117 private static final int WAKE_BIT_PROXIMITY_SCREEN_OFF = 32; 118 119 private static final int LOCK_MASK = WAKE_BIT_CPU_STRONG 120 | WAKE_BIT_CPU_WEAK 121 | WAKE_BIT_SCREEN_DIM 122 | WAKE_BIT_SCREEN_BRIGHT 123 | WAKE_BIT_KEYBOARD_BRIGHT 124 | WAKE_BIT_PROXIMITY_SCREEN_OFF; 125 126 /** 127 * Wake lock that ensures that the CPU is running. The screen might 128 * not be on. 129 */ 130 public static final int PARTIAL_WAKE_LOCK = WAKE_BIT_CPU_STRONG; 131 132 /** 133 * Wake lock that ensures that the screen and keyboard are on at 134 * full brightness. 135 */ 136 public static final int FULL_WAKE_LOCK = WAKE_BIT_CPU_WEAK | WAKE_BIT_SCREEN_BRIGHT 137 | WAKE_BIT_KEYBOARD_BRIGHT; 138 139 /** 140 * Wake lock that ensures that the screen is on at full brightness; 141 * the keyboard backlight will be allowed to go off. 142 */ 143 public static final int SCREEN_BRIGHT_WAKE_LOCK = WAKE_BIT_CPU_WEAK | WAKE_BIT_SCREEN_BRIGHT; 144 145 /** 146 * Wake lock that ensures that the screen is on (but may be dimmed); 147 * the keyboard backlight will be allowed to go off. 148 */ 149 public static final int SCREEN_DIM_WAKE_LOCK = WAKE_BIT_CPU_WEAK | WAKE_BIT_SCREEN_DIM; 150 151 /** 152 * Wake lock that turns the screen off when the proximity sensor activates. 153 * Since not all devices have proximity sensors, use 154 * {@link #getSupportedWakeLockFlags() getSupportedWakeLockFlags()} to determine if 155 * this wake lock mode is supported. 156 * 157 * {@hide} 158 */ 159 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = WAKE_BIT_PROXIMITY_SCREEN_OFF; 160 161 /** 162 * Flag for {@link WakeLock#release release(int)} to defer releasing a 163 * {@link #WAKE_BIT_PROXIMITY_SCREEN_OFF} wakelock until the proximity sensor returns 164 * a negative value. 165 * 166 * {@hide} 167 */ 168 public static final int WAIT_FOR_PROXIMITY_NEGATIVE = 1; 169 170 /** 171 * Normally wake locks don't actually wake the device, they just cause 172 * it to remain on once it's already on. Think of the video player 173 * app as the normal behavior. Notifications that pop up and want 174 * the device to be on are the exception; use this flag to be like them. 175 * <p> 176 * Does not work with PARTIAL_WAKE_LOCKs. 177 */ 178 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; 179 180 /** 181 * When this wake lock is released, poke the user activity timer 182 * so the screen stays on for a little longer. 183 * <p> 184 * Will not turn the screen on if it is not already on. See {@link #ACQUIRE_CAUSES_WAKEUP} 185 * if you want that. 186 * <p> 187 * Does not work with PARTIAL_WAKE_LOCKs. 188 */ 189 public static final int ON_AFTER_RELEASE = 0x20000000; 190 191 /** 192 * Class lets you say that you need to have the device on. 193 * 194 * <p>Call release when you are done and don't need the lock anymore. 195 */ 196 public class WakeLock 197 { 198 static final int RELEASE_WAKE_LOCK = 1; 199 200 Runnable mReleaser = new Runnable() { 201 public void run() { 202 release(); 203 } 204 }; 205 206 int mFlags; 207 String mTag; 208 IBinder mToken; 209 int mCount = 0; 210 boolean mRefCounted = true; 211 boolean mHeld = false; 212 WorkSource mWorkSource; 213 214 WakeLock(int flags, String tag) 215 { 216 switch (flags & LOCK_MASK) { 217 case PARTIAL_WAKE_LOCK: 218 case SCREEN_DIM_WAKE_LOCK: 219 case SCREEN_BRIGHT_WAKE_LOCK: 220 case FULL_WAKE_LOCK: 221 case PROXIMITY_SCREEN_OFF_WAKE_LOCK: 222 break; 223 default: 224 throw new IllegalArgumentException(); 225 } 226 227 mFlags = flags; 228 mTag = tag; 229 mToken = new Binder(); 230 } 231 232 /** 233 * Sets whether this WakeLock is ref counted. 234 * 235 * @param value true for ref counted, false for not ref counted. 236 */ 237 public void setReferenceCounted(boolean value) 238 { 239 mRefCounted = value; 240 } 241 242 /** 243 * Makes sure the device is on at the level you asked when you created 244 * the wake lock. 245 */ 246 public void acquire() 247 { 248 synchronized (mToken) { 249 if (!mRefCounted || mCount++ == 0) { 250 try { 251 mService.acquireWakeLock(mFlags, mToken, mTag, mWorkSource); 252 } catch (RemoteException e) { 253 } 254 mHeld = true; 255 } 256 } 257 } 258 259 /** 260 * Makes sure the device is on at the level you asked when you created 261 * the wake lock. The lock will be released after the given timeout. 262 * 263 * @param timeout Release the lock after the give timeout in milliseconds. 264 */ 265 public void acquire(long timeout) { 266 acquire(); 267 mHandler.postDelayed(mReleaser, timeout); 268 } 269 270 271 /** 272 * Release your claim to the CPU or screen being on. 273 * 274 * <p> 275 * It may turn off shortly after you release it, or it may not if there 276 * are other wake locks held. 277 */ 278 public void release() 279 { 280 release(0); 281 } 282 283 /** 284 * Release your claim to the CPU or screen being on. 285 * @param flags Combination of flag values to modify the release behavior. 286 * Currently only {@link #WAIT_FOR_PROXIMITY_NEGATIVE} is supported. 287 * 288 * <p> 289 * It may turn off shortly after you release it, or it may not if there 290 * are other wake locks held. 291 * 292 * {@hide} 293 */ 294 public void release(int flags) 295 { 296 synchronized (mToken) { 297 if (!mRefCounted || --mCount == 0) { 298 try { 299 mService.releaseWakeLock(mToken, flags); 300 } catch (RemoteException e) { 301 } 302 mHeld = false; 303 } 304 if (mCount < 0) { 305 throw new RuntimeException("WakeLock under-locked " + mTag); 306 } 307 } 308 } 309 310 public boolean isHeld() 311 { 312 synchronized (mToken) { 313 return mHeld; 314 } 315 } 316 317 public void setWorkSource(WorkSource ws) { 318 synchronized (mToken) { 319 if (ws != null && ws.size() == 0) { 320 ws = null; 321 } 322 boolean changed = true; 323 if (ws == null) { 324 mWorkSource = null; 325 } else if (mWorkSource == null) { 326 changed = mWorkSource != null; 327 mWorkSource = new WorkSource(ws); 328 } else { 329 changed = mWorkSource.diff(ws); 330 if (changed) { 331 mWorkSource.set(ws); 332 } 333 } 334 if (changed && mHeld) { 335 try { 336 mService.updateWakeLockWorkSource(mToken, mWorkSource); 337 } catch (RemoteException e) { 338 } 339 } 340 } 341 } 342 343 public String toString() { 344 synchronized (mToken) { 345 return "WakeLock{" 346 + Integer.toHexString(System.identityHashCode(this)) 347 + " held=" + mHeld + ", refCount=" + mCount + "}"; 348 } 349 } 350 351 @Override 352 protected void finalize() throws Throwable 353 { 354 synchronized (mToken) { 355 if (mHeld) { 356 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); 357 try { 358 mService.releaseWakeLock(mToken, 0); 359 } catch (RemoteException e) { 360 } 361 } 362 } 363 } 364 } 365 366 /** 367 * Get a wake lock at the level of the flags parameter. Call 368 * {@link WakeLock#acquire() acquire()} on the object to acquire the 369 * wake lock, and {@link WakeLock#release release()} when you are done. 370 * 371 * {@samplecode 372 *PowerManager pm = (PowerManager)mContext.getSystemService( 373 * Context.POWER_SERVICE); 374 *PowerManager.WakeLock wl = pm.newWakeLock( 375 * PowerManager.SCREEN_DIM_WAKE_LOCK 376 * | PowerManager.ON_AFTER_RELEASE, 377 * TAG); 378 *wl.acquire(); 379 * // ... 380 *wl.release(); 381 * } 382 * 383 * @param flags Combination of flag values defining the requested behavior of the WakeLock. 384 * @param tag Your class name (or other tag) for debugging purposes. 385 * 386 * @see WakeLock#acquire() 387 * @see WakeLock#release() 388 */ 389 public WakeLock newWakeLock(int flags, String tag) 390 { 391 if (tag == null) { 392 throw new NullPointerException("tag is null in PowerManager.newWakeLock"); 393 } 394 return new WakeLock(flags, tag); 395 } 396 397 /** 398 * User activity happened. 399 * <p> 400 * Turns the device from whatever state it's in to full on, and resets 401 * the auto-off timer. 402 * 403 * @param when is used to order this correctly with the wake lock calls. 404 * This time should be in the {@link SystemClock#uptimeMillis 405 * SystemClock.uptimeMillis()} time base. 406 * @param noChangeLights should be true if you don't want the lights to 407 * turn on because of this event. This is set when the power 408 * key goes down. We want the device to stay on while the button 409 * is down, but we're about to turn off. Otherwise the lights 410 * flash on and then off and it looks weird. 411 */ 412 public void userActivity(long when, boolean noChangeLights) 413 { 414 try { 415 mService.userActivity(when, noChangeLights); 416 } catch (RemoteException e) { 417 } 418 } 419 420 /** 421 * Force the device to go to sleep. Overrides all the wake locks that are 422 * held. 423 * 424 * @param time is used to order this correctly with the wake lock calls. 425 * The time should be in the {@link SystemClock#uptimeMillis 426 * SystemClock.uptimeMillis()} time base. 427 */ 428 public void goToSleep(long time) 429 { 430 try { 431 mService.goToSleep(time); 432 } catch (RemoteException e) { 433 } 434 } 435 436 /** 437 * sets the brightness of the backlights (screen, keyboard, button). 438 * 439 * @param brightness value from 0 to 255 440 * 441 * {@hide} 442 */ 443 public void setBacklightBrightness(int brightness) 444 { 445 try { 446 mService.setBacklightBrightness(brightness); 447 } catch (RemoteException e) { 448 } 449 } 450 451 /** 452 * Returns the set of flags for {@link #newWakeLock(int, String) newWakeLock()} 453 * that are supported on the device. 454 * For example, to test to see if the {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} 455 * is supported: 456 * 457 * {@samplecode 458 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 459 * int supportedFlags = pm.getSupportedWakeLockFlags(); 460 * boolean proximitySupported = ((supportedFlags & PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) 461 * == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK); 462 * } 463 * 464 * @return the set of supported WakeLock flags. 465 * 466 * {@hide} 467 */ 468 public int getSupportedWakeLockFlags() 469 { 470 try { 471 return mService.getSupportedWakeLockFlags(); 472 } catch (RemoteException e) { 473 return 0; 474 } 475 } 476 477 /** 478 * Returns whether the screen is currently on. The screen could be bright 479 * or dim. 480 * 481 * {@samplecode 482 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 483 * boolean isScreenOn = pm.isScreenOn(); 484 * } 485 * 486 * @return whether the screen is on (bright or dim). 487 */ 488 public boolean isScreenOn() 489 { 490 try { 491 return mService.isScreenOn(); 492 } catch (RemoteException e) { 493 return false; 494 } 495 } 496 497 /** 498 * Reboot the device. Will not return if the reboot is 499 * successful. Requires the {@link android.Manifest.permission#REBOOT} 500 * permission. 501 * 502 * @param reason code to pass to the kernel (e.g., "recovery") to 503 * request special boot modes, or null. 504 */ 505 public void reboot(String reason) 506 { 507 try { 508 mService.reboot(reason); 509 } catch (RemoteException e) { 510 } 511 } 512 513 private PowerManager() 514 { 515 } 516 517 /** 518 * {@hide} 519 */ 520 public PowerManager(IPowerManager service, Handler handler) 521 { 522 mService = service; 523 mHandler = handler; 524 } 525 526 /** 527 * TODO: It would be nice to be able to set the poke lock here, 528 * but I'm not sure what would be acceptable as an interface - 529 * either a PokeLock object (like WakeLock) or, possibly just a 530 * method call to set the poke lock. 531 */ 532 533 IPowerManager mService; 534 Handler mHandler; 535} 536