PowerManager.java revision 705ffd1efe529bb291e90fa06e92c8090ee98627
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.annotation.SdkConstant; 20import android.annotation.SystemApi; 21import android.content.Context; 22import android.util.Log; 23 24/** 25 * This class gives you control of the power state of the device. 26 * 27 * <p> 28 * <b>Device battery life will be significantly affected by the use of this API.</b> 29 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels 30 * possible, and be sure to release them as soon as possible. 31 * </p><p> 32 * You can obtain an instance of this class by calling 33 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. 34 * </p><p> 35 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}. 36 * This will create a {@link PowerManager.WakeLock} object. You can then use methods 37 * on the wake lock object to control the power state of the device. 38 * </p><p> 39 * In practice it's quite simple: 40 * {@samplecode 41 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 42 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); 43 * wl.acquire(); 44 * ..screen will stay on during this section.. 45 * wl.release(); 46 * } 47 * </p><p> 48 * The following wake lock levels are defined, with varying effects on system power. 49 * <i>These levels are mutually exclusive - you may only specify one of them.</i> 50 * 51 * <table> 52 * <tr><th>Flag Value</th> 53 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr> 54 * 55 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td> 56 * <td>On*</td> <td>Off</td> <td>Off</td> 57 * </tr> 58 * 59 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td> 60 * <td>On</td> <td>Dim</td> <td>Off</td> 61 * </tr> 62 * 63 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td> 64 * <td>On</td> <td>Bright</td> <td>Off</td> 65 * </tr> 66 * 67 * <tr><td>{@link #FULL_WAKE_LOCK}</td> 68 * <td>On</td> <td>Bright</td> <td>Bright</td> 69 * </tr> 70 * </table> 71 * </p><p> 72 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any 73 * display timeouts or the state of the screen and even after the user presses the power button. 74 * In all other wake locks, the CPU will run, but the user can still put the device to sleep 75 * using the power button.</i> 76 * </p><p> 77 * In addition, you can add two more flags, which affect behavior of the screen only. 78 * <i>These flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.</i></p> 79 * 80 * <table> 81 * <tr><th>Flag Value</th> <th>Description</th></tr> 82 * 83 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td> 84 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause 85 * the illumination to remain on once it turns on (e.g. from user activity). This flag 86 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is 87 * acquired. A typical use would be for notifications which are important for the user to 88 * see immediately.</td> 89 * </tr> 90 * 91 * <tr><td>{@link #ON_AFTER_RELEASE}</td> 92 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is 93 * released, causing the illumination to remain on a bit longer. This can be used to 94 * reduce flicker if you are cycling between wake lock conditions.</td> 95 * </tr> 96 * </table> 97 * <p> 98 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK} 99 * permission in an {@code <uses-permission>} element of the application's manifest. 100 * </p> 101 */ 102public final class PowerManager { 103 private static final String TAG = "PowerManager"; 104 105 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few 106 * combinations were actually supported so the bit field was removed. This explains 107 * why the numbering scheme is so odd. If adding a new wake lock level, any unused 108 * value can be used. 109 */ 110 111 /** 112 * Wake lock level: Ensures that the CPU is running; the screen and keyboard 113 * backlight will be allowed to go off. 114 * <p> 115 * If the user presses the power button, then the screen will be turned off 116 * but the CPU will be kept on until all partial wake locks have been released. 117 * </p> 118 */ 119 public static final int PARTIAL_WAKE_LOCK = 0x00000001; 120 121 /** 122 * Wake lock level: Ensures that the screen is on (but may be dimmed); 123 * the keyboard backlight will be allowed to go off. 124 * <p> 125 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be 126 * implicitly released by the system, causing both the screen and the CPU to be turned off. 127 * Contrast with {@link #PARTIAL_WAKE_LOCK}. 128 * </p> 129 * 130 * @deprecated Most applications should use 131 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead 132 * of this type of wake lock, as it will be correctly managed by the platform 133 * as the user moves between applications and doesn't require a special permission. 134 */ 135 @Deprecated 136 public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006; 137 138 /** 139 * Wake lock level: Ensures that the screen is on at full brightness; 140 * the keyboard backlight will be allowed to go off. 141 * <p> 142 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be 143 * implicitly released by the system, causing both the screen and the CPU to be turned off. 144 * Contrast with {@link #PARTIAL_WAKE_LOCK}. 145 * </p> 146 * 147 * @deprecated Most applications should use 148 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead 149 * of this type of wake lock, as it will be correctly managed by the platform 150 * as the user moves between applications and doesn't require a special permission. 151 */ 152 @Deprecated 153 public static final int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a; 154 155 /** 156 * Wake lock level: Ensures that the screen and keyboard backlight are on at 157 * full brightness. 158 * <p> 159 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be 160 * implicitly released by the system, causing both the screen and the CPU to be turned off. 161 * Contrast with {@link #PARTIAL_WAKE_LOCK}. 162 * </p> 163 * 164 * @deprecated Most applications should use 165 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead 166 * of this type of wake lock, as it will be correctly managed by the platform 167 * as the user moves between applications and doesn't require a special permission. 168 */ 169 @Deprecated 170 public static final int FULL_WAKE_LOCK = 0x0000001a; 171 172 /** 173 * Wake lock level: Turns the screen off when the proximity sensor activates. 174 * <p> 175 * If the proximity sensor detects that an object is nearby, the screen turns off 176 * immediately. Shortly after the object moves away, the screen turns on again. 177 * </p><p> 178 * A proximity wake lock does not prevent the device from falling asleep 179 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and 180 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other 181 * wake locks are held, then the device will fall asleep (and lock) as usual. 182 * However, the device will not fall asleep while the screen has been turned off 183 * by the proximity sensor because it effectively counts as ongoing user activity. 184 * </p><p> 185 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported} 186 * to determine whether this wake lock level is supported. 187 * </p><p> 188 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}. 189 * </p> 190 */ 191 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020; 192 193 /** 194 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend 195 * if no other wake locks are held. 196 * <p> 197 * This is used by the dream manager to implement doze mode. It currently 198 * has no effect unless the power manager is in the dozing state. 199 * </p><p> 200 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 201 * </p> 202 * 203 * {@hide} 204 */ 205 public static final int DOZE_WAKE_LOCK = 0x00000040; 206 207 /** 208 * Mask for the wake lock level component of a combined wake lock level and flags integer. 209 * 210 * @hide 211 */ 212 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff; 213 214 /** 215 * Wake lock flag: Turn the screen on when the wake lock is acquired. 216 * <p> 217 * Normally wake locks don't actually wake the device, they just cause 218 * the screen to remain on once it's already on. Think of the video player 219 * application as the normal behavior. Notifications that pop up and want 220 * the device to be on are the exception; use this flag to be like them. 221 * </p><p> 222 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. 223 * </p> 224 */ 225 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; 226 227 /** 228 * Wake lock flag: When this wake lock is released, poke the user activity timer 229 * so the screen stays on for a little longer. 230 * <p> 231 * Will not turn the screen on if it is not already on. 232 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that. 233 * </p><p> 234 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. 235 * </p> 236 */ 237 public static final int ON_AFTER_RELEASE = 0x20000000; 238 239 /** 240 * Wake lock flag: This wake lock is not important for logging events. If a later 241 * wake lock is acquired that is important, it will be considered the one to log. 242 * @hide 243 */ 244 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000; 245 246 /** 247 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a 248 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor 249 * indicates that an object is not in close proximity. 250 */ 251 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1; 252 253 /** 254 * Brightness value for fully on. 255 * @hide 256 */ 257 public static final int BRIGHTNESS_ON = 255; 258 259 /** 260 * Brightness value for fully off. 261 * @hide 262 */ 263 public static final int BRIGHTNESS_OFF = 0; 264 265 /** 266 * Brightness value for default policy handling by the system. 267 * @hide 268 */ 269 public static final int BRIGHTNESS_DEFAULT = -1; 270 271 // Note: Be sure to update android.os.BatteryStats and PowerManager.h 272 // if adding or modifying user activity event constants. 273 274 /** 275 * User activity event type: Unspecified event type. 276 * @hide 277 */ 278 @SystemApi 279 public static final int USER_ACTIVITY_EVENT_OTHER = 0; 280 281 /** 282 * User activity event type: Button or key pressed or released. 283 * @hide 284 */ 285 @SystemApi 286 public static final int USER_ACTIVITY_EVENT_BUTTON = 1; 287 288 /** 289 * User activity event type: Touch down, move or up. 290 * @hide 291 */ 292 @SystemApi 293 public static final int USER_ACTIVITY_EVENT_TOUCH = 2; 294 295 /** 296 * User activity flag: If already dimmed, extend the dim timeout 297 * but do not brighten. This flag is useful for keeping the screen on 298 * a little longer without causing a visible change such as when 299 * the power key is pressed. 300 * @hide 301 */ 302 @SystemApi 303 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0; 304 305 /** 306 * User activity flag: Note the user activity as usual but do not 307 * reset the user activity timeout. This flag is useful for applying 308 * user activity power hints when interacting with the device indirectly 309 * on a secondary screen while allowing the primary screen to go to sleep. 310 * @hide 311 */ 312 @SystemApi 313 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1; 314 315 /** 316 * Go to sleep reason code: Going to sleep due by application request. 317 * @hide 318 */ 319 public static final int GO_TO_SLEEP_REASON_APPLICATION = 0; 320 321 /** 322 * Go to sleep reason code: Going to sleep due by request of the 323 * device administration policy. 324 * @hide 325 */ 326 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1; 327 328 /** 329 * Go to sleep reason code: Going to sleep due to a screen timeout. 330 * @hide 331 */ 332 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2; 333 334 /** 335 * Go to sleep reason code: Going to sleep due to the lid switch being closed. 336 * @hide 337 */ 338 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3; 339 340 /** 341 * Go to sleep reason code: Going to sleep due to the power button being pressed. 342 * @hide 343 */ 344 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4; 345 346 /** 347 * Go to sleep reason code: Going to sleep due to HDMI. 348 * @hide 349 */ 350 public static final int GO_TO_SLEEP_REASON_HDMI = 5; 351 352 /** 353 * Go to sleep flag: Skip dozing state and directly go to full sleep. 354 * @hide 355 */ 356 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0; 357 358 /** 359 * The value to pass as the 'reason' argument to reboot() to 360 * reboot into recovery mode (for applying system updates, doing 361 * factory resets, etc.). 362 * <p> 363 * Requires the {@link android.Manifest.permission#RECOVERY} 364 * permission (in addition to 365 * {@link android.Manifest.permission#REBOOT}). 366 * </p> 367 * @hide 368 */ 369 public static final String REBOOT_RECOVERY = "recovery"; 370 371 /** 372 * The value to pass as the 'reason' argument to android_reboot(). 373 * @hide 374 */ 375 public static final String SHUTDOWN_USER_REQUESTED = "userrequested"; 376 377 final Context mContext; 378 final IPowerManager mService; 379 final Handler mHandler; 380 381 /** 382 * {@hide} 383 */ 384 public PowerManager(Context context, IPowerManager service, Handler handler) { 385 mContext = context; 386 mService = service; 387 mHandler = handler; 388 } 389 390 /** 391 * Gets the minimum supported screen brightness setting. 392 * The screen may be allowed to become dimmer than this value but 393 * this is the minimum value that can be set by the user. 394 * @hide 395 */ 396 public int getMinimumScreenBrightnessSetting() { 397 return mContext.getResources().getInteger( 398 com.android.internal.R.integer.config_screenBrightnessSettingMinimum); 399 } 400 401 /** 402 * Gets the maximum supported screen brightness setting. 403 * The screen may be allowed to become dimmer than this value but 404 * this is the maximum value that can be set by the user. 405 * @hide 406 */ 407 public int getMaximumScreenBrightnessSetting() { 408 return mContext.getResources().getInteger( 409 com.android.internal.R.integer.config_screenBrightnessSettingMaximum); 410 } 411 412 /** 413 * Gets the default screen brightness setting. 414 * @hide 415 */ 416 public int getDefaultScreenBrightnessSetting() { 417 return mContext.getResources().getInteger( 418 com.android.internal.R.integer.config_screenBrightnessSettingDefault); 419 } 420 421 /** 422 * Returns true if the twilight service should be used to adjust screen brightness 423 * policy. This setting is experimental and disabled by default. 424 * @hide 425 */ 426 public static boolean useTwilightAdjustmentFeature() { 427 return SystemProperties.getBoolean("persist.power.usetwilightadj", false); 428 } 429 430 /** 431 * Creates a new wake lock with the specified level and flags. 432 * <p> 433 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags 434 * combined using the logical OR operator. 435 * </p><p> 436 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK}, 437 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK} 438 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be 439 * specified as part of the {@code levelAndFlags} parameter. 440 * </p><p> 441 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP} 442 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the 443 * {@code levelAndFlags} parameters. 444 * </p><p> 445 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the 446 * wake lock, and {@link WakeLock#release release()} when you are done. 447 * </p><p> 448 * {@samplecode 449 * PowerManager pm = (PowerManager)mContext.getSystemService( 450 * Context.POWER_SERVICE); 451 * PowerManager.WakeLock wl = pm.newWakeLock( 452 * PowerManager.SCREEN_DIM_WAKE_LOCK 453 * | PowerManager.ON_AFTER_RELEASE, 454 * TAG); 455 * wl.acquire(); 456 * // ... do work... 457 * wl.release(); 458 * } 459 * </p><p> 460 * Although a wake lock can be created without special permissions, 461 * the {@link android.Manifest.permission#WAKE_LOCK} permission is 462 * required to actually acquire or release the wake lock that is returned. 463 * </p><p class="note"> 464 * If using this to keep the screen on, you should strongly consider using 465 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead. 466 * This window flag will be correctly managed by the platform 467 * as the user moves between applications and doesn't require a special permission. 468 * </p> 469 * 470 * @param levelAndFlags Combination of wake lock level and flag values defining 471 * the requested behavior of the WakeLock. 472 * @param tag Your class name (or other tag) for debugging purposes. 473 * 474 * @see WakeLock#acquire() 475 * @see WakeLock#release() 476 * @see #PARTIAL_WAKE_LOCK 477 * @see #FULL_WAKE_LOCK 478 * @see #SCREEN_DIM_WAKE_LOCK 479 * @see #SCREEN_BRIGHT_WAKE_LOCK 480 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK 481 * @see #ACQUIRE_CAUSES_WAKEUP 482 * @see #ON_AFTER_RELEASE 483 */ 484 public WakeLock newWakeLock(int levelAndFlags, String tag) { 485 validateWakeLockParameters(levelAndFlags, tag); 486 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName()); 487 } 488 489 /** @hide */ 490 public static void validateWakeLockParameters(int levelAndFlags, String tag) { 491 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) { 492 case PARTIAL_WAKE_LOCK: 493 case SCREEN_DIM_WAKE_LOCK: 494 case SCREEN_BRIGHT_WAKE_LOCK: 495 case FULL_WAKE_LOCK: 496 case PROXIMITY_SCREEN_OFF_WAKE_LOCK: 497 case DOZE_WAKE_LOCK: 498 break; 499 default: 500 throw new IllegalArgumentException("Must specify a valid wake lock level."); 501 } 502 if (tag == null) { 503 throw new IllegalArgumentException("The tag must not be null."); 504 } 505 } 506 507 /** 508 * Notifies the power manager that user activity happened. 509 * <p> 510 * Resets the auto-off timer and brightens the screen if the device 511 * is not asleep. This is what happens normally when a key or the touch 512 * screen is pressed or when some other user activity occurs. 513 * This method does not wake up the device if it has been put to sleep. 514 * </p><p> 515 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 516 * </p> 517 * 518 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()} 519 * time base. This timestamp is used to correctly order the user activity request with 520 * other power management functions. It should be set 521 * to the timestamp of the input event that caused the user activity. 522 * @param noChangeLights If true, does not cause the keyboard backlight to turn on 523 * because of this event. This is set when the power key is pressed. 524 * We want the device to stay on while the button is down, but we're about 525 * to turn off the screen so we don't want the keyboard backlight to turn on again. 526 * Otherwise the lights flash on and then off and it looks weird. 527 * 528 * @see #wakeUp 529 * @see #goToSleep 530 * 531 * @removed Requires signature or system permission. 532 * @deprecated Use {@link #userActivity(long, int, int)}. 533 */ 534 @Deprecated 535 public void userActivity(long when, boolean noChangeLights) { 536 userActivity(when, USER_ACTIVITY_EVENT_OTHER, 537 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0); 538 } 539 540 /** 541 * Notifies the power manager that user activity happened. 542 * <p> 543 * Resets the auto-off timer and brightens the screen if the device 544 * is not asleep. This is what happens normally when a key or the touch 545 * screen is pressed or when some other user activity occurs. 546 * This method does not wake up the device if it has been put to sleep. 547 * </p><p> 548 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or 549 * {@link android.Manifest.permission#USER_ACTIVITY} permission. 550 * </p> 551 * 552 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()} 553 * time base. This timestamp is used to correctly order the user activity request with 554 * other power management functions. It should be set 555 * to the timestamp of the input event that caused the user activity. 556 * @param event The user activity event. 557 * @param flags Optional user activity flags. 558 * 559 * @see #wakeUp 560 * @see #goToSleep 561 * 562 * @hide Requires signature or system permission. 563 */ 564 @SystemApi 565 public void userActivity(long when, int event, int flags) { 566 try { 567 mService.userActivity(when, event, flags); 568 } catch (RemoteException e) { 569 } 570 } 571 572 /** 573 * Forces the device to go to sleep. 574 * <p> 575 * Overrides all the wake locks that are held. 576 * This is what happens when the power key is pressed to turn off the screen. 577 * </p><p> 578 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 579 * </p> 580 * 581 * @param time The time when the request to go to sleep was issued, in the 582 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly 583 * order the go to sleep request with other power management functions. It should be set 584 * to the timestamp of the input event that caused the request to go to sleep. 585 * 586 * @see #userActivity 587 * @see #wakeUp 588 * 589 * @removed Requires signature permission. 590 */ 591 public void goToSleep(long time) { 592 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0); 593 } 594 595 /** 596 * Forces the device to go to sleep. 597 * <p> 598 * Overrides all the wake locks that are held. 599 * This is what happens when the power key is pressed to turn off the screen. 600 * </p><p> 601 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 602 * </p> 603 * 604 * @param time The time when the request to go to sleep was issued, in the 605 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly 606 * order the go to sleep request with other power management functions. It should be set 607 * to the timestamp of the input event that caused the request to go to sleep. 608 * @param reason The reason the device is going to sleep. 609 * @param flags Optional flags to apply when going to sleep. 610 * 611 * @see #userActivity 612 * @see #wakeUp 613 * 614 * @hide Requires signature permission. 615 */ 616 public void goToSleep(long time, int reason, int flags) { 617 try { 618 mService.goToSleep(time, reason, flags); 619 } catch (RemoteException e) { 620 } 621 } 622 623 /** 624 * Forces the device to wake up from sleep. 625 * <p> 626 * If the device is currently asleep, wakes it up, otherwise does nothing. 627 * This is what happens when the power key is pressed to turn on the screen. 628 * </p><p> 629 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 630 * </p> 631 * 632 * @param time The time when the request to wake up was issued, in the 633 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly 634 * order the wake up request with other power management functions. It should be set 635 * to the timestamp of the input event that caused the request to wake up. 636 * 637 * @see #userActivity 638 * @see #goToSleep 639 * 640 * @removed Requires signature permission. 641 */ 642 public void wakeUp(long time) { 643 try { 644 mService.wakeUp(time); 645 } catch (RemoteException e) { 646 } 647 } 648 649 /** 650 * Forces the device to start napping. 651 * <p> 652 * If the device is currently awake, starts dreaming, otherwise does nothing. 653 * When the dream ends or if the dream cannot be started, the device will 654 * either wake up or go to sleep depending on whether there has been recent 655 * user activity. 656 * </p><p> 657 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 658 * </p> 659 * 660 * @param time The time when the request to nap was issued, in the 661 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly 662 * order the nap request with other power management functions. It should be set 663 * to the timestamp of the input event that caused the request to nap. 664 * 665 * @see #wakeUp 666 * @see #goToSleep 667 * 668 * @hide Requires signature permission. 669 */ 670 public void nap(long time) { 671 try { 672 mService.nap(time); 673 } catch (RemoteException e) { 674 } 675 } 676 677 /** 678 * Boosts the brightness of the screen to maximum for a predetermined 679 * period of time. This is used to make the screen more readable in bright 680 * daylight for a short duration. 681 * <p> 682 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 683 * </p> 684 * 685 * @param time The time when the request to boost was issued, in the 686 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly 687 * order the boost request with other power management functions. It should be set 688 * to the timestamp of the input event that caused the request to boost. 689 * 690 * @hide Requires signature permission. 691 */ 692 public void boostScreenBrightness(long time) { 693 try { 694 mService.boostScreenBrightness(time); 695 } catch (RemoteException e) { 696 } 697 } 698 699 /** 700 * Sets the brightness of the backlights (screen, keyboard, button). 701 * <p> 702 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. 703 * </p> 704 * 705 * @param brightness The brightness value from 0 to 255. 706 * 707 * @hide Requires signature permission. 708 */ 709 public void setBacklightBrightness(int brightness) { 710 try { 711 mService.setTemporaryScreenBrightnessSettingOverride(brightness); 712 } catch (RemoteException e) { 713 } 714 } 715 716 /** 717 * Returns true if the specified wake lock level is supported. 718 * 719 * @param level The wake lock level to check. 720 * @return True if the specified wake lock level is supported. 721 */ 722 public boolean isWakeLockLevelSupported(int level) { 723 try { 724 return mService.isWakeLockLevelSupported(level); 725 } catch (RemoteException e) { 726 return false; 727 } 728 } 729 730 /** 731 * Returns true if the device is in an interactive state. 732 * <p> 733 * For historical reasons, the name of this method refers to the power state of 734 * the screen but it actually describes the overall interactive state of 735 * the device. This method has been replaced by {@link #isInteractive}. 736 * </p><p> 737 * The value returned by this method only indicates whether the device is 738 * in an interactive state which may have nothing to do with the screen being 739 * on or off. To determine the actual state of the screen, 740 * use {@link android.view.Display#getState}. 741 * </p> 742 * 743 * @return True if the device is in an interactive state. 744 * 745 * @deprecated Use {@link #isInteractive} instead. 746 */ 747 @Deprecated 748 public boolean isScreenOn() { 749 return isInteractive(); 750 } 751 752 /** 753 * Returns true if the device is in an interactive state. 754 * <p> 755 * When this method returns true, the device is awake and ready to interact 756 * with the user (although this is not a guarantee that the user is actively 757 * interacting with the device just this moment). The main screen is usually 758 * turned on while in this state. Certain features, such as the proximity 759 * sensor, may temporarily turn off the screen while still leaving the device in an 760 * interactive state. Note in particular that the device is still considered 761 * to be interactive while dreaming (since dreams can be interactive) but not 762 * when it is dozing or asleep. 763 * </p><p> 764 * When this method returns false, the device is dozing or asleep and must 765 * be awoken before it will become ready to interact with the user again. The 766 * main screen is usually turned off while in this state. Certain features, 767 * such as "ambient mode" may cause the main screen to remain on (albeit in a 768 * low power state) to display system-provided content while the device dozes. 769 * </p><p> 770 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on} 771 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast 772 * whenever the interactive state of the device changes. For historical reasons, 773 * the names of these broadcasts refer to the power state of the screen 774 * but they are actually sent in response to changes in the overall interactive 775 * state of the device, as described by this method. 776 * </p><p> 777 * Services may use the non-interactive state as a hint to conserve power 778 * since the user is not present. 779 * </p> 780 * 781 * @return True if the device is in an interactive state. 782 * 783 * @see android.content.Intent#ACTION_SCREEN_ON 784 * @see android.content.Intent#ACTION_SCREEN_OFF 785 */ 786 public boolean isInteractive() { 787 try { 788 return mService.isInteractive(); 789 } catch (RemoteException e) { 790 return false; 791 } 792 } 793 794 /** 795 * Reboot the device. Will not return if the reboot is successful. 796 * <p> 797 * Requires the {@link android.Manifest.permission#REBOOT} permission. 798 * </p> 799 * 800 * @param reason code to pass to the kernel (e.g., "recovery") to 801 * request special boot modes, or null. 802 */ 803 public void reboot(String reason) { 804 try { 805 mService.reboot(false, reason, true); 806 } catch (RemoteException e) { 807 } 808 } 809 810 /** 811 * Returns true if the device is currently in power save mode. When in this mode, 812 * applications should reduce their functionality in order to conserve battery as 813 * much as possible. You can monitor for changes to this state with 814 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}. 815 * 816 * @return Returns true if currently in low power mode, else false. 817 */ 818 public boolean isPowerSaveMode() { 819 try { 820 return mService.isPowerSaveMode(); 821 } catch (RemoteException e) { 822 return false; 823 } 824 } 825 826 /** 827 * Set the current power save mode. 828 * 829 * @return True if the set was allowed. 830 * 831 * @see #isPowerSaveMode() 832 * 833 * @hide 834 */ 835 public boolean setPowerSaveMode(boolean mode) { 836 try { 837 return mService.setPowerSaveMode(mode); 838 } catch (RemoteException e) { 839 return false; 840 } 841 } 842 843 /** 844 * Turn off the device. 845 * 846 * @param confirm If true, shows a shutdown confirmation dialog. 847 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null. 848 * @param wait If true, this call waits for the shutdown to complete and does not return. 849 * 850 * @hide 851 */ 852 public void shutdown(boolean confirm, String reason, boolean wait) { 853 try { 854 mService.shutdown(confirm, reason, wait); 855 } catch (RemoteException e) { 856 } 857 } 858 859 /** 860 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. 861 * This broadcast is only sent to registered receivers. 862 */ 863 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) 864 public static final String ACTION_POWER_SAVE_MODE_CHANGED 865 = "android.os.action.POWER_SAVE_MODE_CHANGED"; 866 867 /** 868 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change. 869 * This broadcast is only sent to registered receivers. 870 * 871 * @hide 872 */ 873 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) 874 public static final String ACTION_POWER_SAVE_MODE_CHANGING 875 = "android.os.action.POWER_SAVE_MODE_CHANGING"; 876 877 /** @hide */ 878 public static final String EXTRA_POWER_SAVE_MODE = "mode"; 879 880 /** 881 * A wake lock is a mechanism to indicate that your application needs 882 * to have the device stay on. 883 * <p> 884 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK} 885 * permission in an {@code <uses-permission>} element of the application's manifest. 886 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}. 887 * </p><p> 888 * Call {@link #acquire()} to acquire the wake lock and force the device to stay 889 * on at the level that was requested when the wake lock was created. 890 * </p><p> 891 * Call {@link #release()} when you are done and don't need the lock anymore. 892 * It is very important to do this as soon as possible to avoid running down the 893 * device's battery excessively. 894 * </p> 895 */ 896 public final class WakeLock { 897 private int mFlags; 898 private String mTag; 899 private final String mPackageName; 900 private final IBinder mToken; 901 private int mCount; 902 private boolean mRefCounted = true; 903 private boolean mHeld; 904 private WorkSource mWorkSource; 905 private String mHistoryTag; 906 private final String mTraceName; 907 908 private final Runnable mReleaser = new Runnable() { 909 public void run() { 910 release(); 911 } 912 }; 913 914 WakeLock(int flags, String tag, String packageName) { 915 mFlags = flags; 916 mTag = tag; 917 mPackageName = packageName; 918 mToken = new Binder(); 919 mTraceName = "WakeLock (" + mTag + ")"; 920 } 921 922 @Override 923 protected void finalize() throws Throwable { 924 synchronized (mToken) { 925 if (mHeld) { 926 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); 927 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); 928 try { 929 mService.releaseWakeLock(mToken, 0); 930 } catch (RemoteException e) { 931 } 932 } 933 } 934 } 935 936 /** 937 * Sets whether this WakeLock is reference counted. 938 * <p> 939 * Wake locks are reference counted by default. If a wake lock is 940 * reference counted, then each call to {@link #acquire()} must be 941 * balanced by an equal number of calls to {@link #release()}. If a wake 942 * lock is not reference counted, then one call to {@link #release()} is 943 * sufficient to undo the effect of all previous calls to {@link #acquire()}. 944 * </p> 945 * 946 * @param value True to make the wake lock reference counted, false to 947 * make the wake lock non-reference counted. 948 */ 949 public void setReferenceCounted(boolean value) { 950 synchronized (mToken) { 951 mRefCounted = value; 952 } 953 } 954 955 /** 956 * Acquires the wake lock. 957 * <p> 958 * Ensures that the device is on at the level requested when 959 * the wake lock was created. 960 * </p> 961 */ 962 public void acquire() { 963 synchronized (mToken) { 964 acquireLocked(); 965 } 966 } 967 968 /** 969 * Acquires the wake lock with a timeout. 970 * <p> 971 * Ensures that the device is on at the level requested when 972 * the wake lock was created. The lock will be released after the given timeout 973 * expires. 974 * </p> 975 * 976 * @param timeout The timeout after which to release the wake lock, in milliseconds. 977 */ 978 public void acquire(long timeout) { 979 synchronized (mToken) { 980 acquireLocked(); 981 mHandler.postDelayed(mReleaser, timeout); 982 } 983 } 984 985 private void acquireLocked() { 986 if (!mRefCounted || mCount++ == 0) { 987 // Do this even if the wake lock is already thought to be held (mHeld == true) 988 // because non-reference counted wake locks are not always properly released. 989 // For example, the keyguard's wake lock might be forcibly released by the 990 // power manager without the keyguard knowing. A subsequent call to acquire 991 // should immediately acquire the wake lock once again despite never having 992 // been explicitly released by the keyguard. 993 mHandler.removeCallbacks(mReleaser); 994 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); 995 try { 996 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource, 997 mHistoryTag); 998 } catch (RemoteException e) { 999 } 1000 mHeld = true; 1001 } 1002 } 1003 1004 /** 1005 * Releases the wake lock. 1006 * <p> 1007 * This method releases your claim to the CPU or screen being on. 1008 * The screen may turn off shortly after you release the wake lock, or it may 1009 * not if there are other wake locks still held. 1010 * </p> 1011 */ 1012 public void release() { 1013 release(0); 1014 } 1015 1016 /** 1017 * Releases the wake lock with flags to modify the release behavior. 1018 * <p> 1019 * This method releases your claim to the CPU or screen being on. 1020 * The screen may turn off shortly after you release the wake lock, or it may 1021 * not if there are other wake locks still held. 1022 * </p> 1023 * 1024 * @param flags Combination of flag values to modify the release behavior. 1025 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported. 1026 * Passing 0 is equivalent to calling {@link #release()}. 1027 */ 1028 public void release(int flags) { 1029 synchronized (mToken) { 1030 if (!mRefCounted || --mCount == 0) { 1031 mHandler.removeCallbacks(mReleaser); 1032 if (mHeld) { 1033 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); 1034 try { 1035 mService.releaseWakeLock(mToken, flags); 1036 } catch (RemoteException e) { 1037 } 1038 mHeld = false; 1039 } 1040 } 1041 if (mCount < 0) { 1042 throw new RuntimeException("WakeLock under-locked " + mTag); 1043 } 1044 } 1045 } 1046 1047 /** 1048 * Returns true if the wake lock has been acquired but not yet released. 1049 * 1050 * @return True if the wake lock is held. 1051 */ 1052 public boolean isHeld() { 1053 synchronized (mToken) { 1054 return mHeld; 1055 } 1056 } 1057 1058 /** 1059 * Sets the work source associated with the wake lock. 1060 * <p> 1061 * The work source is used to determine on behalf of which application 1062 * the wake lock is being held. This is useful in the case where a 1063 * service is performing work on behalf of an application so that the 1064 * cost of that work can be accounted to the application. 1065 * </p> 1066 * 1067 * @param ws The work source, or null if none. 1068 */ 1069 public void setWorkSource(WorkSource ws) { 1070 synchronized (mToken) { 1071 if (ws != null && ws.size() == 0) { 1072 ws = null; 1073 } 1074 1075 final boolean changed; 1076 if (ws == null) { 1077 changed = mWorkSource != null; 1078 mWorkSource = null; 1079 } else if (mWorkSource == null) { 1080 changed = true; 1081 mWorkSource = new WorkSource(ws); 1082 } else { 1083 changed = mWorkSource.diff(ws); 1084 if (changed) { 1085 mWorkSource.set(ws); 1086 } 1087 } 1088 1089 if (changed && mHeld) { 1090 try { 1091 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag); 1092 } catch (RemoteException e) { 1093 } 1094 } 1095 } 1096 } 1097 1098 /** @hide */ 1099 public void setTag(String tag) { 1100 mTag = tag; 1101 } 1102 1103 /** @hide */ 1104 public void setHistoryTag(String tag) { 1105 mHistoryTag = tag; 1106 } 1107 1108 /** @hide */ 1109 public void setUnimportantForLogging(boolean state) { 1110 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING; 1111 else mFlags &= ~UNIMPORTANT_FOR_LOGGING; 1112 } 1113 1114 @Override 1115 public String toString() { 1116 synchronized (mToken) { 1117 return "WakeLock{" 1118 + Integer.toHexString(System.identityHashCode(this)) 1119 + " held=" + mHeld + ", refCount=" + mCount + "}"; 1120 } 1121 } 1122 } 1123} 1124