blob: 5b2c8db112094651edef71b5e8e491f761dceeba [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Jeff Brown96307042012-07-27 15:51:34 -070019import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.util.Log;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022/**
Jeff Brown1244cda2012-06-19 16:44:46 -070023 * 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 Project9066cfe2009-03-03 19:31:44 -080031 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
Jeff Brown1244cda2012-06-19 16:44:46 -070032 * </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 Project9066cfe2009-03-03 19:31:44 -080038 * {@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 Brown1244cda2012-06-19 16:44:46 -070045 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070046 * 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 Project9066cfe2009-03-03 19:31:44 -080048 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070049 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * <tr><th>Flag Value</th>
51 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070053 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * <td>On*</td> <td>Off</td> <td>Off</td>
55 * </tr>
56 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070057 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 * <td>On</td> <td>Dim</td> <td>Off</td>
59 * </tr>
60 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070061 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 * <td>On</td> <td>Bright</td> <td>Off</td>
63 * </tr>
64 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070065 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 * <td>On</td> <td>Bright</td> <td>Bright</td>
67 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070069 * </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 Dougherty7b9a2882012-10-28 12:07:08 -070076 * <i>These flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.</i></p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070078 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070081 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * <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 Dougherty7b9a2882012-10-28 12:07:08 -070089 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * <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 Project9066cfe2009-03-03 19:31:44 -080094 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070095 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -070096 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
97 * permission in an {@code &lt;uses-permission&gt;} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -070098 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
Jeff Brown1244cda2012-06-19 16:44:46 -0700100public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700102
Jeff Brown155fc702012-07-27 12:12:15 -0700103 /* 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 Project9066cfe2009-03-03 19:31:44 -0800107 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700110 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
111 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700112 * <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 Brown1244cda2012-06-19 16:44:46 -0700115 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
Jeff Brown155fc702012-07-27 12:12:15 -0700117 public static final int PARTIAL_WAKE_LOCK = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700120 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700121 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700122 * <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 Brown1244cda2012-06-19 16:44:46 -0700127 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700128 * @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 Project9066cfe2009-03-03 19:31:44 -0800132 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700133 @Deprecated
Jeff Brown155fc702012-07-27 12:12:15 -0700134 public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700137 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700139 * <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 Project9066cfe2009-03-03 19:31:44 -0800149 */
Jeff Brown155fc702012-07-27 12:12:15 -0700150 @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 Project9066cfe2009-03-03 19:31:44 -0800169
170 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700171 * Wake lock level: Turns the screen off when the proximity sensor activates.
172 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700173 * 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 Brown96307042012-07-27 15:51:34 -0700183 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700184 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800185 * </p><p>
186 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700187 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700188 *
189 * {@hide}
190 */
Jeff Brown155fc702012-07-27 12:12:15 -0700191 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700192
193 /**
Jeff Brown26875502014-01-30 21:47:47 -0800194 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
195 * if no other wake locks are held.
196 * <p>
197 * This is used by the dream manager to implement doze mode. It currently
198 * has no effect unless the power manager is in the dozing state.
199 * </p>
200 *
201 * {@hide}
202 */
203 public static final int DOZE_WAKE_LOCK = 0x00000040;
204
205 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700206 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500207 *
Jeff Brown155fc702012-07-27 12:12:15 -0700208 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500209 */
Jeff Brown155fc702012-07-27 12:12:15 -0700210 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500211
212 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700213 * Wake lock flag: Turn the screen on when the wake lock is acquired.
214 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700216 * the screen to remain on once it's already on. Think of the video player
217 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700219 * </p><p>
220 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
221 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
Jeff Brown155fc702012-07-27 12:12:15 -0700223 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
225 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700226 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * so the screen stays on for a little longer.
228 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700229 * Will not turn the screen on if it is not already on.
230 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
231 * </p><p>
232 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
233 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 */
Jeff Brown155fc702012-07-27 12:12:15 -0700235 public static final int ON_AFTER_RELEASE = 0x20000000;
236
237 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800238 * Wake lock flag: This wake lock is not important for logging events. If a later
239 * wake lock is acquired that is important, it will be considered the one to log.
240 * @hide
241 */
242 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
243
244 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700245 * Flag for {@link WakeLock#release release(int)} to defer releasing a
Dianne Hackborn713df152013-05-17 11:27:57 -0700246 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor returns
Jeff Brown155fc702012-07-27 12:12:15 -0700247 * a negative value.
248 *
249 * {@hide}
250 */
251 public static final int WAIT_FOR_PROXIMITY_NEGATIVE = 1;
Jeff Brown7304c342012-05-11 18:42:42 -0700252
253 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700254 * Brightness value for fully on.
255 * @hide
256 */
257 public static final int BRIGHTNESS_ON = 255;
258
259 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700260 * Brightness value for fully off.
261 * @hide
262 */
263 public static final int BRIGHTNESS_OFF = 0;
264
Jeff Brownb696de52012-07-27 15:38:50 -0700265 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
266 // if adding or modifying user activity event constants.
267
268 /**
269 * User activity event type: Unspecified event type.
270 * @hide
271 */
272 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
273
274 /**
275 * User activity event type: Button or key pressed or released.
276 * @hide
277 */
278 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
279
280 /**
281 * User activity event type: Touch down, move or up.
282 * @hide
283 */
284 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
285
Jeff Brown96307042012-07-27 15:51:34 -0700286 /**
287 * User activity flag: Do not restart the user activity timeout or brighten
288 * the display in response to user activity if it is already dimmed.
289 * @hide
290 */
291 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
292
293 /**
Jeff Brown96307042012-07-27 15:51:34 -0700294 * Go to sleep reason code: Going to sleep due by user request.
295 * @hide
296 */
297 public static final int GO_TO_SLEEP_REASON_USER = 0;
298
299 /**
300 * Go to sleep reason code: Going to sleep due by request of the
301 * device administration policy.
302 * @hide
303 */
304 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
305
306 /**
307 * Go to sleep reason code: Going to sleep due to a screen timeout.
308 * @hide
309 */
310 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
311
Doug Zongker3b0218b2014-01-14 12:29:06 -0800312 /**
313 * The value to pass as the 'reason' argument to reboot() to
314 * reboot into recovery mode (for applying system updates, doing
315 * factory resets, etc.).
316 * <p>
317 * Requires the {@link android.Manifest.permission#RECOVERY}
318 * permission (in addition to
319 * {@link android.Manifest.permission#REBOOT}).
320 * </p>
321 */
322 public static final String REBOOT_RECOVERY = "recovery";
323
Jeff Brown96307042012-07-27 15:51:34 -0700324 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700325 final IPowerManager mService;
326 final Handler mHandler;
327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700329 * {@hide}
330 */
Jeff Brown96307042012-07-27 15:51:34 -0700331 public PowerManager(Context context, IPowerManager service, Handler handler) {
332 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700333 mService = service;
334 mHandler = handler;
335 }
336
337 /**
Jeff Brown96307042012-07-27 15:51:34 -0700338 * Gets the minimum supported screen brightness setting.
339 * The screen may be allowed to become dimmer than this value but
340 * this is the minimum value that can be set by the user.
341 * @hide
342 */
343 public int getMinimumScreenBrightnessSetting() {
344 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700345 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700346 }
347
348 /**
349 * Gets the maximum supported screen brightness setting.
350 * The screen may be allowed to become dimmer than this value but
351 * this is the maximum value that can be set by the user.
352 * @hide
353 */
354 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700355 return mContext.getResources().getInteger(
356 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700357 }
358
359 /**
360 * Gets the default screen brightness setting.
361 * @hide
362 */
363 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700364 return mContext.getResources().getInteger(
365 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700366 }
367
368 /**
Jeff Browndb212842012-10-01 14:33:09 -0700369 * Returns true if the twilight service should be used to adjust screen brightness
370 * policy. This setting is experimental and disabled by default.
371 * @hide
372 */
373 public static boolean useTwilightAdjustmentFeature() {
374 return SystemProperties.getBoolean("persist.power.usetwilightadj", false);
375 }
376
377 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700378 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700379 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700380 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
381 * combined using the logical OR operator.
382 * </p><p>
383 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
384 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
385 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
386 * specified as part of the {@code levelAndFlags} parameter.
387 * </p><p>
388 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
389 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
390 * {@code levelAndFlags} parameters.
391 * </p><p>
392 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
393 * wake lock, and {@link WakeLock#release release()} when you are done.
394 * </p><p>
395 * {@samplecode
396 * PowerManager pm = (PowerManager)mContext.getSystemService(
397 * Context.POWER_SERVICE);
398 * PowerManager.WakeLock wl = pm.newWakeLock(
399 * PowerManager.SCREEN_DIM_WAKE_LOCK
400 * | PowerManager.ON_AFTER_RELEASE,
401 * TAG);
402 * wl.acquire();
403 * // ... do work...
404 * wl.release();
405 * }
406 * </p><p>
407 * Although a wake lock can be created without special permissions,
408 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
409 * required to actually acquire or release the wake lock that is returned.
410 * </p><p class="note">
411 * If using this to keep the screen on, you should strongly consider using
412 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
413 * This window flag will be correctly managed by the platform
414 * as the user moves between applications and doesn't require a special permission.
415 * </p>
416 *
417 * @param levelAndFlags Combination of wake lock level and flag values defining
418 * the requested behavior of the WakeLock.
419 * @param tag Your class name (or other tag) for debugging purposes.
420 *
421 * @see WakeLock#acquire()
422 * @see WakeLock#release()
423 * @see #PARTIAL_WAKE_LOCK
424 * @see #FULL_WAKE_LOCK
425 * @see #SCREEN_DIM_WAKE_LOCK
426 * @see #SCREEN_BRIGHT_WAKE_LOCK
427 * @see #ACQUIRE_CAUSES_WAKEUP
428 * @see #ON_AFTER_RELEASE
429 */
430 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700431 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700432 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700433 }
434
435 /** @hide */
436 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
437 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
438 case PARTIAL_WAKE_LOCK:
439 case SCREEN_DIM_WAKE_LOCK:
440 case SCREEN_BRIGHT_WAKE_LOCK:
441 case FULL_WAKE_LOCK:
442 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800443 case DOZE_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700444 break;
445 default:
446 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700447 }
448 if (tag == null) {
449 throw new IllegalArgumentException("The tag must not be null.");
450 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700451 }
452
453 /**
454 * Notifies the power manager that user activity happened.
455 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700456 * Resets the auto-off timer and brightens the screen if the device
457 * is not asleep. This is what happens normally when a key or the touch
458 * screen is pressed or when some other user activity occurs.
459 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700460 * </p><p>
461 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
462 * </p>
463 *
464 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700465 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700466 * other power management functions. It should be set
467 * to the timestamp of the input event that caused the user activity.
468 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
469 * because of this event. This is set when the power key is pressed.
470 * We want the device to stay on while the button is down, but we're about
471 * to turn off the screen so we don't want the keyboard backlight to turn on again.
472 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700473 *
474 * @see #wakeUp
475 * @see #goToSleep
Jeff Brown1244cda2012-06-19 16:44:46 -0700476 */
477 public void userActivity(long when, boolean noChangeLights) {
478 try {
Jeff Brown96307042012-07-27 15:51:34 -0700479 mService.userActivity(when, USER_ACTIVITY_EVENT_OTHER,
480 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
Jeff Brown1244cda2012-06-19 16:44:46 -0700481 } catch (RemoteException e) {
482 }
483 }
484
485 /**
486 * Forces the device to go to sleep.
487 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700488 * Overrides all the wake locks that are held.
489 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -0700490 * </p><p>
491 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
492 * </p>
493 *
494 * @param time The time when the request to go to sleep was issued, in the
495 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700496 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -0700497 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -0700498 *
499 * @see #userActivity
500 * @see #wakeUp
Jeff Brown1244cda2012-06-19 16:44:46 -0700501 */
502 public void goToSleep(long time) {
503 try {
Jeff Brown96307042012-07-27 15:51:34 -0700504 mService.goToSleep(time, GO_TO_SLEEP_REASON_USER);
505 } catch (RemoteException e) {
506 }
507 }
508
509 /**
510 * Forces the device to wake up from sleep.
511 * <p>
512 * If the device is currently asleep, wakes it up, otherwise does nothing.
513 * This is what happens when the power key is pressed to turn on the screen.
514 * </p><p>
515 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
516 * </p>
517 *
518 * @param time The time when the request to wake up was issued, in the
519 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700520 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -0700521 * to the timestamp of the input event that caused the request to wake up.
522 *
523 * @see #userActivity
524 * @see #goToSleep
525 */
526 public void wakeUp(long time) {
527 try {
528 mService.wakeUp(time);
Jeff Brown1244cda2012-06-19 16:44:46 -0700529 } catch (RemoteException e) {
530 }
531 }
532
533 /**
Jeff Brown62c82e42012-09-26 01:30:41 -0700534 * Forces the device to start napping.
535 * <p>
536 * If the device is currently awake, starts dreaming, otherwise does nothing.
537 * When the dream ends or if the dream cannot be started, the device will
538 * either wake up or go to sleep depending on whether there has been recent
539 * user activity.
540 * </p><p>
541 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
542 * </p>
543 *
544 * @param time The time when the request to nap was issued, in the
545 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
546 * order the nap request with other power management functions. It should be set
547 * to the timestamp of the input event that caused the request to nap.
548 *
549 * @see #wakeUp
550 * @see #goToSleep
551 *
552 * @hide
553 */
554 public void nap(long time) {
555 try {
556 mService.nap(time);
557 } catch (RemoteException e) {
558 }
559 }
560
561 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700562 * Sets the brightness of the backlights (screen, keyboard, button).
563 * <p>
564 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
565 * </p>
566 *
567 * @param brightness The brightness value from 0 to 255.
568 *
569 * {@hide}
570 */
571 public void setBacklightBrightness(int brightness) {
572 try {
Jeff Brown96307042012-07-27 15:51:34 -0700573 mService.setTemporaryScreenBrightnessSettingOverride(brightness);
Jeff Brown1244cda2012-06-19 16:44:46 -0700574 } catch (RemoteException e) {
575 }
576 }
577
578 /**
Jeff Brown96307042012-07-27 15:51:34 -0700579 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700580 *
Jeff Brown96307042012-07-27 15:51:34 -0700581 * @param level The wake lock level to check.
582 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700583 *
584 * {@hide}
585 */
Jeff Brown96307042012-07-27 15:51:34 -0700586 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700587 try {
Jeff Brown96307042012-07-27 15:51:34 -0700588 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -0700589 } catch (RemoteException e) {
Jeff Brown96307042012-07-27 15:51:34 -0700590 return false;
Jeff Brown1244cda2012-06-19 16:44:46 -0700591 }
592 }
593
594 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700595 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -0700596 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -0700597 * For historical reasons, the name of this method refers to the power state of
598 * the screen but it actually describes the overall interactive state of
599 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700600 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -0700601 * The value returned by this method only indicates whether the device is
602 * in an interactive state which may have nothing to do with the screen being
603 * on or off. To determine the actual state of the screen,
604 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700605 * </p>
606 *
Jeff Brown037c33e2014-04-09 00:31:55 -0700607 * @return True if the device is in an interactive state.
608 *
609 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -0700610 */
Jeff Brown037c33e2014-04-09 00:31:55 -0700611 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700612 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700613 return isInteractive();
614 }
615
616 /**
617 * Returns true if the device is in an interactive state.
618 * <p>
619 * When this method returns true, the device is awake and ready to interact
620 * with the user (although this is not a guarantee that the user is actively
621 * interacting with the device just this moment). The main screen is usually
622 * turned on while in this state. Certain features, such as the proximity
623 * sensor, may temporarily turn off the screen while still leaving the device in an
624 * interactive state. Note in particular that the device is still considered
625 * to be interactive while dreaming (since dreams can be interactive) but not
626 * when it is dozing or asleep.
627 * </p><p>
628 * When this method returns false, the device is dozing or asleep and must
629 * be awoken before it will become ready to interact with the user again. The
630 * main screen is usually turned off while in this state. Certain features,
631 * such as "ambient mode" may cause the main screen to remain on (albeit in a
632 * low power state) to display system-provided content while the device dozes.
633 * </p><p>
634 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
635 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
636 * whenever the interactive state of the device changes. For historical reasons,
637 * the names of these broadcasts refer to the power state of the screen
638 * but they are actually sent in response to changes in the overall interactive
639 * state of the device, as described by this method.
640 * </p><p>
641 * Services may use the non-interactive state as a hint to conserve power
642 * since the user is not present.
643 * </p>
644 *
645 * @return True if the device is in an interactive state.
646 *
647 * @see android.content.Intent#ACTION_SCREEN_ON
648 * @see android.content.Intent#ACTION_SCREEN_OFF
649 */
650 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -0700651 try {
Jeff Brown037c33e2014-04-09 00:31:55 -0700652 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -0700653 } catch (RemoteException e) {
654 return false;
655 }
656 }
657
658 /**
659 * Reboot the device. Will not return if the reboot is successful.
660 * <p>
661 * Requires the {@link android.Manifest.permission#REBOOT} permission.
662 * </p>
663 *
664 * @param reason code to pass to the kernel (e.g., "recovery") to
665 * request special boot modes, or null.
666 */
667 public void reboot(String reason) {
668 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700669 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -0700670 } catch (RemoteException e) {
671 }
672 }
673
674 /**
675 * A wake lock is a mechanism to indicate that your application needs
676 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -0700677 * <p>
678 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
679 * permission in an {@code &lt;uses-permission&gt;} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700680 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
681 * </p><p>
682 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
683 * on at the level that was requested when the wake lock was created.
684 * </p><p>
685 * Call {@link #release()} when you are done and don't need the lock anymore.
686 * It is very important to do this as soon as possible to avoid running down the
687 * device's battery excessively.
688 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
Jeff Brown1244cda2012-06-19 16:44:46 -0700690 public final class WakeLock {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800691 private int mFlags;
692 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -0700693 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -0700694 private final IBinder mToken;
695 private int mCount;
696 private boolean mRefCounted = true;
697 private boolean mHeld;
698 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800699 private String mHistoryTag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
Jeff Brown1244cda2012-06-19 16:44:46 -0700701 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 public void run() {
703 release();
704 }
705 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706
Dianne Hackborn713df152013-05-17 11:27:57 -0700707 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 mFlags = flags;
709 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -0700710 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 mToken = new Binder();
712 }
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -0700715 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 synchronized (mToken) {
717 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -0800718 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500720 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 } catch (RemoteException e) {
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724 }
725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
Jeff Brown1244cda2012-06-19 16:44:46 -0700727 /**
728 * Sets whether this WakeLock is reference counted.
729 * <p>
730 * Wake locks are reference counted by default. If a wake lock is
731 * reference counted, then each call to {@link #acquire()} must be
732 * balanced by an equal number of calls to {@link #release()}. If a wake
733 * lock is not reference counted, then one call to {@link #release()} is
734 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
735 * </p>
736 *
737 * @param value True to make the wake lock reference counted, false to
738 * make the wake lock non-reference counted.
739 */
740 public void setReferenceCounted(boolean value) {
741 synchronized (mToken) {
742 mRefCounted = value;
743 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745
Jeff Brown1244cda2012-06-19 16:44:46 -0700746 /**
747 * Acquires the wake lock.
748 * <p>
749 * Ensures that the device is on at the level requested when
750 * the wake lock was created.
751 * </p>
752 */
753 public void acquire() {
754 synchronized (mToken) {
755 acquireLocked();
756 }
757 }
758
759 /**
760 * Acquires the wake lock with a timeout.
761 * <p>
762 * Ensures that the device is on at the level requested when
763 * the wake lock was created. The lock will be released after the given timeout
764 * expires.
765 * </p>
766 *
767 * @param timeout The timeout after which to release the wake lock, in milliseconds.
768 */
769 public void acquire(long timeout) {
770 synchronized (mToken) {
771 acquireLocked();
772 mHandler.postDelayed(mReleaser, timeout);
773 }
774 }
775
776 private void acquireLocked() {
777 if (!mRefCounted || mCount++ == 0) {
Jeff Brownff1baef2012-07-19 15:01:17 -0700778 // Do this even if the wake lock is already thought to be held (mHeld == true)
779 // because non-reference counted wake locks are not always properly released.
780 // For example, the keyguard's wake lock might be forcibly released by the
781 // power manager without the keyguard knowing. A subsequent call to acquire
782 // should immediately acquire the wake lock once again despite never having
783 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -0700784 mHandler.removeCallbacks(mReleaser);
Jeff Brownff1baef2012-07-19 15:01:17 -0700785 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800786 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
787 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -0700788 } catch (RemoteException e) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700789 }
Jeff Brownff1baef2012-07-19 15:01:17 -0700790 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -0700791 }
792 }
793
794 /**
795 * Releases the wake lock.
796 * <p>
797 * This method releases your claim to the CPU or screen being on.
798 * The screen may turn off shortly after you release the wake lock, or it may
799 * not if there are other wake locks still held.
800 * </p>
801 */
802 public void release() {
803 release(0);
804 }
805
806 /**
807 * Releases the wake lock with flags to modify the release behavior.
808 * <p>
809 * This method releases your claim to the CPU or screen being on.
810 * The screen may turn off shortly after you release the wake lock, or it may
811 * not if there are other wake locks still held.
812 * </p>
813 *
814 * @param flags Combination of flag values to modify the release behavior.
815 * Currently only {@link #WAIT_FOR_PROXIMITY_NEGATIVE} is supported.
816 *
817 * {@hide}
818 */
819 public void release(int flags) {
820 synchronized (mToken) {
821 if (!mRefCounted || --mCount == 0) {
822 mHandler.removeCallbacks(mReleaser);
823 if (mHeld) {
824 try {
825 mService.releaseWakeLock(mToken, flags);
826 } catch (RemoteException e) {
827 }
828 mHeld = false;
829 }
830 }
831 if (mCount < 0) {
832 throw new RuntimeException("WakeLock under-locked " + mTag);
833 }
834 }
835 }
836
837 /**
838 * Returns true if the wake lock has been acquired but not yet released.
839 *
840 * @return True if the wake lock is held.
841 */
842 public boolean isHeld() {
843 synchronized (mToken) {
844 return mHeld;
845 }
846 }
847
848 /**
849 * Sets the work source associated with the wake lock.
850 * <p>
851 * The work source is used to determine on behalf of which application
852 * the wake lock is being held. This is useful in the case where a
853 * service is performing work on behalf of an application so that the
854 * cost of that work can be accounted to the application.
855 * </p>
856 *
857 * @param ws The work source, or null if none.
858 */
859 public void setWorkSource(WorkSource ws) {
860 synchronized (mToken) {
861 if (ws != null && ws.size() == 0) {
862 ws = null;
863 }
864
865 final boolean changed;
866 if (ws == null) {
867 changed = mWorkSource != null;
868 mWorkSource = null;
869 } else if (mWorkSource == null) {
870 changed = true;
871 mWorkSource = new WorkSource(ws);
872 } else {
873 changed = mWorkSource.diff(ws);
874 if (changed) {
875 mWorkSource.set(ws);
876 }
877 }
878
879 if (changed && mHeld) {
880 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700881 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -0700882 } catch (RemoteException e) {
883 }
884 }
885 }
886 }
887
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800888 /** @hide */
889 public void setTag(String tag) {
890 mTag = tag;
891 }
892
893 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800894 public void setHistoryTag(String tag) {
895 mHistoryTag = tag;
896 }
897
898 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800899 public void setUnimportantForLogging(boolean state) {
900 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
901 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
902 }
903
Jeff Brown1244cda2012-06-19 16:44:46 -0700904 @Override
905 public String toString() {
906 synchronized (mToken) {
907 return "WakeLock{"
908 + Integer.toHexString(System.identityHashCode(this))
909 + " held=" + mHeld + ", refCount=" + mCount + "}";
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913}