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