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