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