blob: ae50ddbc1447f266b7e3c21c1b34a90d92171021 [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 *
Jeff Brown1244cda2012-06-19 16:44:46 -070049 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * <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 Brown1244cda2012-06-19 16:44:46 -070073 * </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 Project9066cfe2009-03-03 19:31:44 -080081 *
Jeff Brown1244cda2012-06-19 16:44:46 -070082 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * <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 Brown1244cda2012-06-19 16:44:46 -0700103 * </p><p>
Kenny Rootd710fb52011-03-15 17:39:45 -0700104 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
105 * permission in an {@code &lt;uses-permission&gt;} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700106 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
Jeff Brown1244cda2012-06-19 16:44:46 -0700108public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700110
Jeff Brown155fc702012-07-27 12:12:15 -0700111 /* 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 Project9066cfe2009-03-03 19:31:44 -0800115 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700118 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
119 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700120 * <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 Brown1244cda2012-06-19 16:44:46 -0700123 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 */
Jeff Brown155fc702012-07-27 12:12:15 -0700125 public static final int PARTIAL_WAKE_LOCK = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700128 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700129 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700130 * <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 Brown1244cda2012-06-19 16:44:46 -0700135 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700136 * @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 Project9066cfe2009-03-03 19:31:44 -0800140 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700141 @Deprecated
Jeff Brown155fc702012-07-27 12:12:15 -0700142 public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700145 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700147 * <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 Project9066cfe2009-03-03 19:31:44 -0800157 */
Jeff Brown155fc702012-07-27 12:12:15 -0700158 @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 Project9066cfe2009-03-03 19:31:44 -0800177
178 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700179 * Wake lock level: Turns the screen off when the proximity sensor activates.
180 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700181 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700182 * to determine whether this wake lock level is supported.
183 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700184 *
185 * {@hide}
186 */
Jeff Brown155fc702012-07-27 12:12:15 -0700187 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700188
189 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700190 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500191 *
Jeff Brown155fc702012-07-27 12:12:15 -0700192 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500193 */
Jeff Brown155fc702012-07-27 12:12:15 -0700194 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500195
196 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700197 * Wake lock flag: Turn the screen on when the wake lock is acquired.
198 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700200 * the screen to remain on once it's already on. Think of the video player
201 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700203 * </p><p>
204 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
205 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
Jeff Brown155fc702012-07-27 12:12:15 -0700207 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700210 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * so the screen stays on for a little longer.
212 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700213 * Will not turn the screen on if it is not already on.
214 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
215 * </p><p>
216 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
217 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Jeff Brown155fc702012-07-27 12:12:15 -0700219 public static final int ON_AFTER_RELEASE = 0x20000000;
220
221 /**
222 * Flag for {@link WakeLock#release release(int)} to defer releasing a
223 * {@link #WAKE_BIT_PROXIMITY_SCREEN_OFF} wake lock until the proximity sensor returns
224 * a negative value.
225 *
226 * {@hide}
227 */
228 public static final int WAIT_FOR_PROXIMITY_NEGATIVE = 1;
Jeff Brown7304c342012-05-11 18:42:42 -0700229
230 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700231 * Brightness value for fully on.
232 * @hide
233 */
234 public static final int BRIGHTNESS_ON = 255;
235
236 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700237 * Brightness value for fully off.
238 * @hide
239 */
240 public static final int BRIGHTNESS_OFF = 0;
241
Jeff Brownb696de52012-07-27 15:38:50 -0700242 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
243 // if adding or modifying user activity event constants.
244
245 /**
246 * User activity event type: Unspecified event type.
247 * @hide
248 */
249 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
250
251 /**
252 * User activity event type: Button or key pressed or released.
253 * @hide
254 */
255 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
256
257 /**
258 * User activity event type: Touch down, move or up.
259 * @hide
260 */
261 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
262
Jeff Brown96307042012-07-27 15:51:34 -0700263 /**
264 * User activity flag: Do not restart the user activity timeout or brighten
265 * the display in response to user activity if it is already dimmed.
266 * @hide
267 */
268 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
269
270 /**
Jeff Brown96307042012-07-27 15:51:34 -0700271 * Go to sleep reason code: Going to sleep due by user request.
272 * @hide
273 */
274 public static final int GO_TO_SLEEP_REASON_USER = 0;
275
276 /**
277 * Go to sleep reason code: Going to sleep due by request of the
278 * device administration policy.
279 * @hide
280 */
281 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
282
283 /**
284 * Go to sleep reason code: Going to sleep due to a screen timeout.
285 * @hide
286 */
287 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
288
289 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700290 final IPowerManager mService;
291 final Handler mHandler;
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700294 * {@hide}
295 */
Jeff Brown96307042012-07-27 15:51:34 -0700296 public PowerManager(Context context, IPowerManager service, Handler handler) {
297 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700298 mService = service;
299 mHandler = handler;
300 }
301
302 /**
Jeff Brown96307042012-07-27 15:51:34 -0700303 * Gets the minimum supported screen brightness setting.
304 * The screen may be allowed to become dimmer than this value but
305 * this is the minimum value that can be set by the user.
306 * @hide
307 */
308 public int getMinimumScreenBrightnessSetting() {
309 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700310 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700311 }
312
313 /**
314 * Gets the maximum supported screen brightness setting.
315 * The screen may be allowed to become dimmer than this value but
316 * this is the maximum value that can be set by the user.
317 * @hide
318 */
319 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700320 return mContext.getResources().getInteger(
321 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700322 }
323
324 /**
325 * Gets the default screen brightness setting.
326 * @hide
327 */
328 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700329 return mContext.getResources().getInteger(
330 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700331 }
332
333 /**
Jeff Brown631938f2012-09-08 15:11:11 -0700334 * Returns true if the screen auto-brightness adjustment setting should
335 * be available in the UI. This setting is experimental and disabled by default.
336 * @hide
337 */
338 public static boolean useScreenAutoBrightnessAdjustmentFeature() {
339 return SystemProperties.getBoolean("persist.power.useautobrightadj", false);
340 }
341
342 /**
Jeff Browndb212842012-10-01 14:33:09 -0700343 * Returns true if the twilight service should be used to adjust screen brightness
344 * policy. This setting is experimental and disabled by default.
345 * @hide
346 */
347 public static boolean useTwilightAdjustmentFeature() {
348 return SystemProperties.getBoolean("persist.power.usetwilightadj", false);
349 }
350
351 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700352 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700353 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700354 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
355 * combined using the logical OR operator.
356 * </p><p>
357 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
358 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
359 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
360 * specified as part of the {@code levelAndFlags} parameter.
361 * </p><p>
362 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
363 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
364 * {@code levelAndFlags} parameters.
365 * </p><p>
366 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
367 * wake lock, and {@link WakeLock#release release()} when you are done.
368 * </p><p>
369 * {@samplecode
370 * PowerManager pm = (PowerManager)mContext.getSystemService(
371 * Context.POWER_SERVICE);
372 * PowerManager.WakeLock wl = pm.newWakeLock(
373 * PowerManager.SCREEN_DIM_WAKE_LOCK
374 * | PowerManager.ON_AFTER_RELEASE,
375 * TAG);
376 * wl.acquire();
377 * // ... do work...
378 * wl.release();
379 * }
380 * </p><p>
381 * Although a wake lock can be created without special permissions,
382 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
383 * required to actually acquire or release the wake lock that is returned.
384 * </p><p class="note">
385 * If using this to keep the screen on, you should strongly consider using
386 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
387 * This window flag will be correctly managed by the platform
388 * as the user moves between applications and doesn't require a special permission.
389 * </p>
390 *
391 * @param levelAndFlags Combination of wake lock level and flag values defining
392 * the requested behavior of the WakeLock.
393 * @param tag Your class name (or other tag) for debugging purposes.
394 *
395 * @see WakeLock#acquire()
396 * @see WakeLock#release()
397 * @see #PARTIAL_WAKE_LOCK
398 * @see #FULL_WAKE_LOCK
399 * @see #SCREEN_DIM_WAKE_LOCK
400 * @see #SCREEN_BRIGHT_WAKE_LOCK
401 * @see #ACQUIRE_CAUSES_WAKEUP
402 * @see #ON_AFTER_RELEASE
403 */
404 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700405 validateWakeLockParameters(levelAndFlags, tag);
406 return new WakeLock(levelAndFlags, tag);
407 }
408
409 /** @hide */
410 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
411 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
412 case PARTIAL_WAKE_LOCK:
413 case SCREEN_DIM_WAKE_LOCK:
414 case SCREEN_BRIGHT_WAKE_LOCK:
415 case FULL_WAKE_LOCK:
416 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
417 break;
418 default:
419 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700420 }
421 if (tag == null) {
422 throw new IllegalArgumentException("The tag must not be null.");
423 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700424 }
425
426 /**
427 * Notifies the power manager that user activity happened.
428 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700429 * Resets the auto-off timer and brightens the screen if the device
430 * is not asleep. This is what happens normally when a key or the touch
431 * screen is pressed or when some other user activity occurs.
432 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700433 * </p><p>
434 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
435 * </p>
436 *
437 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700438 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700439 * other power management functions. It should be set
440 * to the timestamp of the input event that caused the user activity.
441 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
442 * because of this event. This is set when the power key is pressed.
443 * We want the device to stay on while the button is down, but we're about
444 * to turn off the screen so we don't want the keyboard backlight to turn on again.
445 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700446 *
447 * @see #wakeUp
448 * @see #goToSleep
Jeff Brown1244cda2012-06-19 16:44:46 -0700449 */
450 public void userActivity(long when, boolean noChangeLights) {
451 try {
Jeff Brown96307042012-07-27 15:51:34 -0700452 mService.userActivity(when, USER_ACTIVITY_EVENT_OTHER,
453 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
Jeff Brown1244cda2012-06-19 16:44:46 -0700454 } catch (RemoteException e) {
455 }
456 }
457
458 /**
459 * Forces the device to go to sleep.
460 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700461 * Overrides all the wake locks that are held.
462 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -0700463 * </p><p>
464 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
465 * </p>
466 *
467 * @param time The time when the request to go to sleep was issued, in the
468 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700469 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -0700470 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -0700471 *
472 * @see #userActivity
473 * @see #wakeUp
Jeff Brown1244cda2012-06-19 16:44:46 -0700474 */
475 public void goToSleep(long time) {
476 try {
Jeff Brown96307042012-07-27 15:51:34 -0700477 mService.goToSleep(time, GO_TO_SLEEP_REASON_USER);
478 } catch (RemoteException e) {
479 }
480 }
481
482 /**
483 * Forces the device to wake up from sleep.
484 * <p>
485 * If the device is currently asleep, wakes it up, otherwise does nothing.
486 * This is what happens when the power key is pressed to turn on the screen.
487 * </p><p>
488 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
489 * </p>
490 *
491 * @param time The time when the request to wake up was issued, in the
492 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700493 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -0700494 * to the timestamp of the input event that caused the request to wake up.
495 *
496 * @see #userActivity
497 * @see #goToSleep
498 */
499 public void wakeUp(long time) {
500 try {
501 mService.wakeUp(time);
Jeff Brown1244cda2012-06-19 16:44:46 -0700502 } catch (RemoteException e) {
503 }
504 }
505
506 /**
Jeff Brown62c82e42012-09-26 01:30:41 -0700507 * Forces the device to start napping.
508 * <p>
509 * If the device is currently awake, starts dreaming, otherwise does nothing.
510 * When the dream ends or if the dream cannot be started, the device will
511 * either wake up or go to sleep depending on whether there has been recent
512 * user activity.
513 * </p><p>
514 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
515 * </p>
516 *
517 * @param time The time when the request to nap was issued, in the
518 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
519 * order the nap request with other power management functions. It should be set
520 * to the timestamp of the input event that caused the request to nap.
521 *
522 * @see #wakeUp
523 * @see #goToSleep
524 *
525 * @hide
526 */
527 public void nap(long time) {
528 try {
529 mService.nap(time);
530 } catch (RemoteException e) {
531 }
532 }
533
534 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700535 * Sets the brightness of the backlights (screen, keyboard, button).
536 * <p>
537 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
538 * </p>
539 *
540 * @param brightness The brightness value from 0 to 255.
541 *
542 * {@hide}
543 */
544 public void setBacklightBrightness(int brightness) {
545 try {
Jeff Brown96307042012-07-27 15:51:34 -0700546 mService.setTemporaryScreenBrightnessSettingOverride(brightness);
Jeff Brown1244cda2012-06-19 16:44:46 -0700547 } catch (RemoteException e) {
548 }
549 }
550
551 /**
Jeff Brown96307042012-07-27 15:51:34 -0700552 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700553 *
Jeff Brown96307042012-07-27 15:51:34 -0700554 * @param level The wake lock level to check.
555 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700556 *
557 * {@hide}
558 */
Jeff Brown96307042012-07-27 15:51:34 -0700559 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700560 try {
Jeff Brown96307042012-07-27 15:51:34 -0700561 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -0700562 } catch (RemoteException e) {
Jeff Brown96307042012-07-27 15:51:34 -0700563 return false;
Jeff Brown1244cda2012-06-19 16:44:46 -0700564 }
565 }
566
567 /**
568 * Returns whether the screen is currently on.
569 * <p>
570 * Only indicates whether the screen is on. The screen could be either bright or dim.
571 * </p><p>
572 * {@samplecode
573 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
574 * boolean isScreenOn = pm.isScreenOn();
575 * }
576 * </p>
577 *
578 * @return whether the screen is on (bright or dim).
579 */
580 public boolean isScreenOn() {
581 try {
582 return mService.isScreenOn();
583 } catch (RemoteException e) {
584 return false;
585 }
586 }
587
588 /**
589 * Reboot the device. Will not return if the reboot is successful.
590 * <p>
591 * Requires the {@link android.Manifest.permission#REBOOT} permission.
592 * </p>
593 *
594 * @param reason code to pass to the kernel (e.g., "recovery") to
595 * request special boot modes, or null.
596 */
597 public void reboot(String reason) {
598 try {
599 mService.reboot(reason);
600 } catch (RemoteException e) {
601 }
602 }
603
604 /**
605 * A wake lock is a mechanism to indicate that your application needs
606 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -0700607 * <p>
608 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
609 * permission in an {@code &lt;uses-permission&gt;} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700610 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
611 * </p><p>
612 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
613 * on at the level that was requested when the wake lock was created.
614 * </p><p>
615 * Call {@link #release()} when you are done and don't need the lock anymore.
616 * It is very important to do this as soon as possible to avoid running down the
617 * device's battery excessively.
618 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 */
Jeff Brown1244cda2012-06-19 16:44:46 -0700620 public final class WakeLock {
621 private final int mFlags;
622 private final String mTag;
623 private final IBinder mToken;
624 private int mCount;
625 private boolean mRefCounted = true;
626 private boolean mHeld;
627 private WorkSource mWorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
Jeff Brown1244cda2012-06-19 16:44:46 -0700629 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public void run() {
631 release();
632 }
633 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
Jeff Brown1244cda2012-06-19 16:44:46 -0700635 WakeLock(int flags, String tag) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mFlags = flags;
637 mTag = tag;
638 mToken = new Binder();
639 }
640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -0700642 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 synchronized (mToken) {
644 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -0800645 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500647 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 } catch (RemoteException e) {
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651 }
652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Jeff Brown1244cda2012-06-19 16:44:46 -0700654 /**
655 * Sets whether this WakeLock is reference counted.
656 * <p>
657 * Wake locks are reference counted by default. If a wake lock is
658 * reference counted, then each call to {@link #acquire()} must be
659 * balanced by an equal number of calls to {@link #release()}. If a wake
660 * lock is not reference counted, then one call to {@link #release()} is
661 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
662 * </p>
663 *
664 * @param value True to make the wake lock reference counted, false to
665 * make the wake lock non-reference counted.
666 */
667 public void setReferenceCounted(boolean value) {
668 synchronized (mToken) {
669 mRefCounted = value;
670 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
Jeff Brown1244cda2012-06-19 16:44:46 -0700673 /**
674 * Acquires the wake lock.
675 * <p>
676 * Ensures that the device is on at the level requested when
677 * the wake lock was created.
678 * </p>
679 */
680 public void acquire() {
681 synchronized (mToken) {
682 acquireLocked();
683 }
684 }
685
686 /**
687 * Acquires the wake lock with a timeout.
688 * <p>
689 * Ensures that the device is on at the level requested when
690 * the wake lock was created. The lock will be released after the given timeout
691 * expires.
692 * </p>
693 *
694 * @param timeout The timeout after which to release the wake lock, in milliseconds.
695 */
696 public void acquire(long timeout) {
697 synchronized (mToken) {
698 acquireLocked();
699 mHandler.postDelayed(mReleaser, timeout);
700 }
701 }
702
703 private void acquireLocked() {
704 if (!mRefCounted || mCount++ == 0) {
Jeff Brownff1baef2012-07-19 15:01:17 -0700705 // Do this even if the wake lock is already thought to be held (mHeld == true)
706 // because non-reference counted wake locks are not always properly released.
707 // For example, the keyguard's wake lock might be forcibly released by the
708 // power manager without the keyguard knowing. A subsequent call to acquire
709 // should immediately acquire the wake lock once again despite never having
710 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -0700711 mHandler.removeCallbacks(mReleaser);
Jeff Brownff1baef2012-07-19 15:01:17 -0700712 try {
Jeff Brown96307042012-07-27 15:51:34 -0700713 mService.acquireWakeLock(mToken, mFlags, mTag, mWorkSource);
Jeff Brownff1baef2012-07-19 15:01:17 -0700714 } catch (RemoteException e) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700715 }
Jeff Brownff1baef2012-07-19 15:01:17 -0700716 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -0700717 }
718 }
719
720 /**
721 * Releases the wake lock.
722 * <p>
723 * This method releases your claim to the CPU or screen being on.
724 * The screen may turn off shortly after you release the wake lock, or it may
725 * not if there are other wake locks still held.
726 * </p>
727 */
728 public void release() {
729 release(0);
730 }
731
732 /**
733 * Releases the wake lock with flags to modify the release behavior.
734 * <p>
735 * This method releases your claim to the CPU or screen being on.
736 * The screen may turn off shortly after you release the wake lock, or it may
737 * not if there are other wake locks still held.
738 * </p>
739 *
740 * @param flags Combination of flag values to modify the release behavior.
741 * Currently only {@link #WAIT_FOR_PROXIMITY_NEGATIVE} is supported.
742 *
743 * {@hide}
744 */
745 public void release(int flags) {
746 synchronized (mToken) {
747 if (!mRefCounted || --mCount == 0) {
748 mHandler.removeCallbacks(mReleaser);
749 if (mHeld) {
750 try {
751 mService.releaseWakeLock(mToken, flags);
752 } catch (RemoteException e) {
753 }
754 mHeld = false;
755 }
756 }
757 if (mCount < 0) {
758 throw new RuntimeException("WakeLock under-locked " + mTag);
759 }
760 }
761 }
762
763 /**
764 * Returns true if the wake lock has been acquired but not yet released.
765 *
766 * @return True if the wake lock is held.
767 */
768 public boolean isHeld() {
769 synchronized (mToken) {
770 return mHeld;
771 }
772 }
773
774 /**
775 * Sets the work source associated with the wake lock.
776 * <p>
777 * The work source is used to determine on behalf of which application
778 * the wake lock is being held. This is useful in the case where a
779 * service is performing work on behalf of an application so that the
780 * cost of that work can be accounted to the application.
781 * </p>
782 *
783 * @param ws The work source, or null if none.
784 */
785 public void setWorkSource(WorkSource ws) {
786 synchronized (mToken) {
787 if (ws != null && ws.size() == 0) {
788 ws = null;
789 }
790
791 final boolean changed;
792 if (ws == null) {
793 changed = mWorkSource != null;
794 mWorkSource = null;
795 } else if (mWorkSource == null) {
796 changed = true;
797 mWorkSource = new WorkSource(ws);
798 } else {
799 changed = mWorkSource.diff(ws);
800 if (changed) {
801 mWorkSource.set(ws);
802 }
803 }
804
805 if (changed && mHeld) {
806 try {
807 mService.updateWakeLockWorkSource(mToken, mWorkSource);
808 } catch (RemoteException e) {
809 }
810 }
811 }
812 }
813
814 @Override
815 public String toString() {
816 synchronized (mToken) {
817 return "WakeLock{"
818 + Integer.toHexString(System.identityHashCode(this))
819 + " held=" + mHeld + ", refCount=" + mCount + "}";
820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823}