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