blob: 2a4507ca027ed33542d346f5da613a57c6198341 [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
Dianne Hackborneb94fa72014-06-03 17:48:12 -070019import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070020import android.annotation.SystemApi;
Jeff Brown96307042012-07-27 15:51:34 -070021import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024/**
Jeff Brown1244cda2012-06-19 16:44:46 -070025 * This class gives you control of the power state of the device.
26 *
27 * <p>
28 * <b>Device battery life will be significantly affected by the use of this API.</b>
29 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
30 * possible, and be sure to release them as soon as possible.
31 * </p><p>
32 * You can obtain an instance of this class by calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
Jeff Brown1244cda2012-06-19 16:44:46 -070034 * </p><p>
35 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}.
36 * This will create a {@link PowerManager.WakeLock} object. You can then use methods
37 * on the wake lock object to control the power state of the device.
38 * </p><p>
39 * In practice it's quite simple:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 * {@samplecode
41 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
42 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
43 * wl.acquire();
44 * ..screen will stay on during this section..
45 * wl.release();
46 * }
Jeff Brown1244cda2012-06-19 16:44:46 -070047 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070048 * The following wake lock levels are defined, with varying effects on system power.
49 * <i>These levels are mutually exclusive - you may only specify one of them.</i>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070051 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 * <tr><th>Flag Value</th>
53 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070055 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * <td>On*</td> <td>Off</td> <td>Off</td>
57 * </tr>
58 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070059 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 * <td>On</td> <td>Dim</td> <td>Off</td>
61 * </tr>
62 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070063 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 * <td>On</td> <td>Bright</td> <td>Off</td>
65 * </tr>
66 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070067 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * <td>On</td> <td>Bright</td> <td>Bright</td>
69 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070071 * </p><p>
72 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any
73 * display timeouts or the state of the screen and even after the user presses the power button.
74 * In all other wake locks, the CPU will run, but the user can still put the device to sleep
75 * using the power button.</i>
76 * </p><p>
77 * In addition, you can add two more flags, which affect behavior of the screen only.
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070078 * <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 -080079 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070080 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070083 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause
85 * the illumination to remain on once it turns on (e.g. from user activity). This flag
86 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is
87 * acquired. A typical use would be for notifications which are important for the user to
88 * see immediately.</td>
89 * </tr>
90 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070091 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is
93 * released, causing the illumination to remain on a bit longer. This can be used to
94 * reduce flicker if you are cycling between wake lock conditions.</td>
95 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070097 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -070098 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +000099 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700100 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 */
Jeff Brown1244cda2012-06-19 16:44:46 -0700102public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700104
Jeff Brown155fc702012-07-27 12:12:15 -0700105 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
106 * combinations were actually supported so the bit field was removed. This explains
107 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
108 * value can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700112 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
113 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700114 * <p>
115 * If the user presses the power button, then the screen will be turned off
116 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -0700117 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 */
Jeff Brown155fc702012-07-27 12:12:15 -0700119 public static final int PARTIAL_WAKE_LOCK = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700122 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700123 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700124 * <p>
125 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
126 * implicitly released by the system, causing both the screen and the CPU to be turned off.
127 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
128 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700129 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700130 * @deprecated Most applications should use
131 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
132 * of this type of wake lock, as it will be correctly managed by the platform
133 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700135 @Deprecated
Jeff Brown155fc702012-07-27 12:12:15 -0700136 public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700139 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700141 * <p>
142 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
143 * implicitly released by the system, causing both the screen and the CPU to be turned off.
144 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
145 * </p>
146 *
147 * @deprecated Most applications should use
148 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
149 * of this type of wake lock, as it will be correctly managed by the platform
150 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 */
Jeff Brown155fc702012-07-27 12:12:15 -0700152 @Deprecated
153 public static final int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a;
154
155 /**
156 * Wake lock level: Ensures that the screen and keyboard backlight are on at
157 * full brightness.
158 * <p>
159 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
160 * implicitly released by the system, causing both the screen and the CPU to be turned off.
161 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
162 * </p>
163 *
164 * @deprecated Most applications should use
165 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
166 * of this type of wake lock, as it will be correctly managed by the platform
167 * as the user moves between applications and doesn't require a special permission.
168 */
169 @Deprecated
170 public static final int FULL_WAKE_LOCK = 0x0000001a;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700173 * Wake lock level: Turns the screen off when the proximity sensor activates.
174 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700175 * If the proximity sensor detects that an object is nearby, the screen turns off
176 * immediately. Shortly after the object moves away, the screen turns on again.
177 * </p><p>
178 * A proximity wake lock does not prevent the device from falling asleep
179 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
180 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
181 * wake locks are held, then the device will fall asleep (and lock) as usual.
182 * However, the device will not fall asleep while the screen has been turned off
183 * by the proximity sensor because it effectively counts as ongoing user activity.
184 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700185 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700186 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800187 * </p><p>
188 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700189 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700190 */
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.
Jeff Brown72671fb2014-08-21 21:41:09 -0700199 * </p><p>
200 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800201 * </p>
202 *
203 * {@hide}
204 */
205 public static final int DOZE_WAKE_LOCK = 0x00000040;
206
207 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800208 * Wake lock level: Keep the device awake enough to allow drawing to occur.
209 * <p>
210 * This is used by the window manager to allow applications to draw while the
211 * system is dozing. It currently has no effect unless the power manager is in
212 * the dozing state.
213 * </p><p>
214 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
215 * </p>
216 *
217 * {@hide}
218 */
219 public static final int DRAW_WAKE_LOCK = 0x00000080;
220
221 /**
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800222 * Wake lock level: Enables Sustained Performance Mode.
223 * <p>
224 * This is used by Gaming and VR applications to ensure the device provides
225 * will provide consistent performance over a large amount of time.
226 * </p>
Ruchi Kandoi302710d2016-02-25 16:22:54 -0800227 *
228 * {@hide}
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800229 */
230 public static final int SUSTAINED_PERFORMANCE_WAKE_LOCK = 0x00000100;
231
232 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700233 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500234 *
Jeff Brown155fc702012-07-27 12:12:15 -0700235 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500236 */
Jeff Brown155fc702012-07-27 12:12:15 -0700237 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500238
239 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700240 * Wake lock flag: Turn the screen on when the wake lock is acquired.
241 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700243 * the screen to remain on once it's already on. Think of the video player
244 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700246 * </p><p>
247 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
248 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
Jeff Brown155fc702012-07-27 12:12:15 -0700250 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700253 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * so the screen stays on for a little longer.
255 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700256 * Will not turn the screen on if it is not already on.
257 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
258 * </p><p>
259 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
260 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
Jeff Brown155fc702012-07-27 12:12:15 -0700262 public static final int ON_AFTER_RELEASE = 0x20000000;
263
264 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800265 * Wake lock flag: This wake lock is not important for logging events. If a later
266 * wake lock is acquired that is important, it will be considered the one to log.
267 * @hide
268 */
269 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
270
271 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700272 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
273 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
274 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700275 */
Michael Wright1208e272014-09-08 19:57:50 -0700276 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1;
Jeff Brown7304c342012-05-11 18:42:42 -0700277
278 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700279 * Brightness value for fully on.
280 * @hide
281 */
282 public static final int BRIGHTNESS_ON = 255;
283
284 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700285 * Brightness value for fully off.
286 * @hide
287 */
288 public static final int BRIGHTNESS_OFF = 0;
289
Jeff Brown970d4132014-07-19 11:33:47 -0700290 /**
291 * Brightness value for default policy handling by the system.
292 * @hide
293 */
294 public static final int BRIGHTNESS_DEFAULT = -1;
295
Jeff Brownb696de52012-07-27 15:38:50 -0700296 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
297 // if adding or modifying user activity event constants.
298
299 /**
300 * User activity event type: Unspecified event type.
301 * @hide
302 */
Jeff Brown0a571122014-08-21 21:50:43 -0700303 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700304 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
305
306 /**
307 * User activity event type: Button or key pressed or released.
308 * @hide
309 */
Jeff Brown0a571122014-08-21 21:50:43 -0700310 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700311 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
312
313 /**
314 * User activity event type: Touch down, move or up.
315 * @hide
316 */
Jeff Brown0a571122014-08-21 21:50:43 -0700317 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700318 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
319
Jeff Brown96307042012-07-27 15:51:34 -0700320 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700321 * User activity flag: If already dimmed, extend the dim timeout
322 * but do not brighten. This flag is useful for keeping the screen on
323 * a little longer without causing a visible change such as when
324 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700325 * @hide
326 */
Jeff Brown0a571122014-08-21 21:50:43 -0700327 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700328 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
329
330 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700331 * User activity flag: Note the user activity as usual but do not
332 * reset the user activity timeout. This flag is useful for applying
333 * user activity power hints when interacting with the device indirectly
334 * on a secondary screen while allowing the primary screen to go to sleep.
335 * @hide
336 */
337 @SystemApi
338 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
339
340 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700341 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700342 * @hide
343 */
Jeff Brownc12035c2014-08-13 18:52:25 -0700344 public static final int GO_TO_SLEEP_REASON_APPLICATION = 0;
Jeff Brown96307042012-07-27 15:51:34 -0700345
346 /**
347 * Go to sleep reason code: Going to sleep due by request of the
348 * device administration policy.
349 * @hide
350 */
351 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
352
353 /**
354 * Go to sleep reason code: Going to sleep due to a screen timeout.
355 * @hide
356 */
357 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
358
Doug Zongker3b0218b2014-01-14 12:29:06 -0800359 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700360 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
361 * @hide
362 */
363 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
364
365 /**
366 * Go to sleep reason code: Going to sleep due to the power button being pressed.
367 * @hide
368 */
369 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
370
371 /**
372 * Go to sleep reason code: Going to sleep due to HDMI.
373 * @hide
374 */
375 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
376
377 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700378 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
379 * @hide
380 */
381 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
382
383 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700384 * Go to sleep flag: Skip dozing state and directly go to full sleep.
385 * @hide
386 */
387 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
388
389 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800390 * The value to pass as the 'reason' argument to reboot() to reboot into
391 * recovery mode for tasks other than applying system updates, such as
392 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800393 * <p>
394 * Requires the {@link android.Manifest.permission#RECOVERY}
395 * permission (in addition to
396 * {@link android.Manifest.permission#REBOOT}).
397 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700398 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800399 */
400 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700401
402 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800403 * The value to pass as the 'reason' argument to reboot() to reboot into
404 * recovery mode for applying system updates.
405 * <p>
406 * Requires the {@link android.Manifest.permission#RECOVERY}
407 * permission (in addition to
408 * {@link android.Manifest.permission#REBOOT}).
409 * </p>
410 * @hide
411 */
412 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
413
414 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000415 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
416 * the device.
417 * @hide
418 */
419 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
420
421 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700422 * The value to pass as the 'reason' argument to android_reboot().
423 * @hide
424 */
425 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
426
Jeff Brown96307042012-07-27 15:51:34 -0700427 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700428 final IPowerManager mService;
429 final Handler mHandler;
430
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700431 IDeviceIdleController mIDeviceIdleController;
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700434 * {@hide}
435 */
Jeff Brown96307042012-07-27 15:51:34 -0700436 public PowerManager(Context context, IPowerManager service, Handler handler) {
437 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700438 mService = service;
439 mHandler = handler;
440 }
441
442 /**
Jeff Brown96307042012-07-27 15:51:34 -0700443 * Gets the minimum supported screen brightness setting.
444 * The screen may be allowed to become dimmer than this value but
445 * this is the minimum value that can be set by the user.
446 * @hide
447 */
448 public int getMinimumScreenBrightnessSetting() {
449 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700450 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700451 }
452
453 /**
454 * Gets the maximum supported screen brightness setting.
455 * The screen may be allowed to become dimmer than this value but
456 * this is the maximum value that can be set by the user.
457 * @hide
458 */
459 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700460 return mContext.getResources().getInteger(
461 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700462 }
463
464 /**
465 * Gets the default screen brightness setting.
466 * @hide
467 */
468 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700469 return mContext.getResources().getInteger(
470 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700471 }
472
473 /**
Jeff Browndb212842012-10-01 14:33:09 -0700474 * Returns true if the twilight service should be used to adjust screen brightness
475 * policy. This setting is experimental and disabled by default.
476 * @hide
477 */
478 public static boolean useTwilightAdjustmentFeature() {
479 return SystemProperties.getBoolean("persist.power.usetwilightadj", false);
480 }
481
482 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700483 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700484 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700485 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
486 * combined using the logical OR operator.
487 * </p><p>
488 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
489 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
490 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
491 * specified as part of the {@code levelAndFlags} parameter.
492 * </p><p>
493 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
494 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
495 * {@code levelAndFlags} parameters.
496 * </p><p>
497 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
498 * wake lock, and {@link WakeLock#release release()} when you are done.
499 * </p><p>
500 * {@samplecode
501 * PowerManager pm = (PowerManager)mContext.getSystemService(
502 * Context.POWER_SERVICE);
503 * PowerManager.WakeLock wl = pm.newWakeLock(
504 * PowerManager.SCREEN_DIM_WAKE_LOCK
505 * | PowerManager.ON_AFTER_RELEASE,
506 * TAG);
507 * wl.acquire();
508 * // ... do work...
509 * wl.release();
510 * }
511 * </p><p>
512 * Although a wake lock can be created without special permissions,
513 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
514 * required to actually acquire or release the wake lock that is returned.
515 * </p><p class="note">
516 * If using this to keep the screen on, you should strongly consider using
517 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
518 * This window flag will be correctly managed by the platform
519 * as the user moves between applications and doesn't require a special permission.
520 * </p>
521 *
522 * @param levelAndFlags Combination of wake lock level and flag values defining
523 * the requested behavior of the WakeLock.
524 * @param tag Your class name (or other tag) for debugging purposes.
525 *
526 * @see WakeLock#acquire()
527 * @see WakeLock#release()
528 * @see #PARTIAL_WAKE_LOCK
529 * @see #FULL_WAKE_LOCK
530 * @see #SCREEN_DIM_WAKE_LOCK
531 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700532 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700533 * @see #ACQUIRE_CAUSES_WAKEUP
534 * @see #ON_AFTER_RELEASE
535 */
536 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700537 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700538 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700539 }
540
541 /** @hide */
542 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
543 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
544 case PARTIAL_WAKE_LOCK:
545 case SCREEN_DIM_WAKE_LOCK:
546 case SCREEN_BRIGHT_WAKE_LOCK:
547 case FULL_WAKE_LOCK:
548 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800549 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800550 case DRAW_WAKE_LOCK:
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800551 case SUSTAINED_PERFORMANCE_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700552 break;
553 default:
554 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700555 }
556 if (tag == null) {
557 throw new IllegalArgumentException("The tag must not be null.");
558 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700559 }
560
561 /**
562 * Notifies the power manager that user activity happened.
563 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700564 * Resets the auto-off timer and brightens the screen if the device
565 * is not asleep. This is what happens normally when a key or the touch
566 * screen is pressed or when some other user activity occurs.
567 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700568 * </p><p>
569 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
570 * </p>
571 *
572 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700573 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700574 * other power management functions. It should be set
575 * to the timestamp of the input event that caused the user activity.
576 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
577 * because of this event. This is set when the power key is pressed.
578 * We want the device to stay on while the button is down, but we're about
579 * to turn off the screen so we don't want the keyboard backlight to turn on again.
580 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700581 *
582 * @see #wakeUp
583 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700584 *
585 * @removed Requires signature or system permission.
586 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700587 */
Jeff Brown7d827512014-08-21 21:56:02 -0700588 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700589 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -0700590 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
591 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
592 }
593
594 /**
595 * Notifies the power manager that user activity happened.
596 * <p>
597 * Resets the auto-off timer and brightens the screen if the device
598 * is not asleep. This is what happens normally when a key or the touch
599 * screen is pressed or when some other user activity occurs.
600 * This method does not wake up the device if it has been put to sleep.
601 * </p><p>
602 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
603 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
604 * </p>
605 *
606 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
607 * time base. This timestamp is used to correctly order the user activity request with
608 * other power management functions. It should be set
609 * to the timestamp of the input event that caused the user activity.
610 * @param event The user activity event.
611 * @param flags Optional user activity flags.
612 *
613 * @see #wakeUp
614 * @see #goToSleep
615 *
616 * @hide Requires signature or system permission.
617 */
618 @SystemApi
619 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700620 try {
Jeff Brown0a571122014-08-21 21:50:43 -0700621 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -0700622 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700623 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700624 }
625 }
626
627 /**
628 * Forces the device to go to sleep.
629 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700630 * Overrides all the wake locks that are held.
631 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -0700632 * </p><p>
633 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
634 * </p>
635 *
636 * @param time The time when the request to go to sleep was issued, in the
637 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700638 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -0700639 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -0700640 *
641 * @see #userActivity
642 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -0700643 *
644 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -0700645 */
646 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -0700647 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -0700648 }
649
650 /**
Jeff Brown7d827512014-08-21 21:56:02 -0700651 * Forces the device to go to sleep.
652 * <p>
653 * Overrides all the wake locks that are held.
654 * This is what happens when the power key is pressed to turn off the screen.
655 * </p><p>
656 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
657 * </p>
658 *
659 * @param time The time when the request to go to sleep was issued, in the
660 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
661 * order the go to sleep request with other power management functions. It should be set
662 * to the timestamp of the input event that caused the request to go to sleep.
663 * @param reason The reason the device is going to sleep.
664 * @param flags Optional flags to apply when going to sleep.
665 *
666 * @see #userActivity
667 * @see #wakeUp
668 *
669 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -0700670 */
671 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700672 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -0700673 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -0700674 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700675 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -0700676 }
677 }
678
679 /**
680 * Forces the device to wake up from sleep.
681 * <p>
682 * If the device is currently asleep, wakes it up, otherwise does nothing.
683 * This is what happens when the power key is pressed to turn on the screen.
684 * </p><p>
685 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
686 * </p>
687 *
688 * @param time The time when the request to wake up was issued, in the
689 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700690 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -0700691 * to the timestamp of the input event that caused the request to wake up.
692 *
693 * @see #userActivity
694 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700695 *
696 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -0700697 */
698 public void wakeUp(long time) {
699 try {
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700700 mService.wakeUp(time, "wakeUp", mContext.getOpPackageName());
701 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700702 throw e.rethrowFromSystemServer();
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700703 }
704 }
705
706 /**
707 * @hide
708 */
709 public void wakeUp(long time, String reason) {
710 try {
711 mService.wakeUp(time, reason, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -0700712 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700713 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700714 }
715 }
716
717 /**
Jeff Brown62c82e42012-09-26 01:30:41 -0700718 * Forces the device to start napping.
719 * <p>
720 * If the device is currently awake, starts dreaming, otherwise does nothing.
721 * When the dream ends or if the dream cannot be started, the device will
722 * either wake up or go to sleep depending on whether there has been recent
723 * user activity.
724 * </p><p>
725 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
726 * </p>
727 *
728 * @param time The time when the request to nap was issued, in the
729 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
730 * order the nap request with other power management functions. It should be set
731 * to the timestamp of the input event that caused the request to nap.
732 *
733 * @see #wakeUp
734 * @see #goToSleep
735 *
Jeff Brown7d827512014-08-21 21:56:02 -0700736 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -0700737 */
738 public void nap(long time) {
739 try {
740 mService.nap(time);
741 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700742 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -0700743 }
744 }
745
746 /**
Jeff Browne333e672014-10-28 13:48:55 -0700747 * Boosts the brightness of the screen to maximum for a predetermined
748 * period of time. This is used to make the screen more readable in bright
749 * daylight for a short duration.
750 * <p>
751 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
752 * </p>
753 *
754 * @param time The time when the request to boost was issued, in the
755 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
756 * order the boost request with other power management functions. It should be set
757 * to the timestamp of the input event that caused the request to boost.
758 *
759 * @hide Requires signature permission.
760 */
761 public void boostScreenBrightness(long time) {
762 try {
763 mService.boostScreenBrightness(time);
764 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700765 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -0700766 }
767 }
768
769 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -0700770 * Returns whether the screen brightness is currently boosted to maximum, caused by a call
771 * to {@link #boostScreenBrightness(long)}.
772 * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise.
773 *
774 * @hide
775 */
776 @SystemApi
777 public boolean isScreenBrightnessBoosted() {
778 try {
779 return mService.isScreenBrightnessBoosted();
780 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700781 throw e.rethrowFromSystemServer();
Bryce Lee84d6c0f2015-03-17 10:43:08 -0700782 }
783 }
784
785 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700786 * Sets the brightness of the backlights (screen, keyboard, button).
787 * <p>
788 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
789 * </p>
790 *
791 * @param brightness The brightness value from 0 to 255.
792 *
Jeff Brown7d827512014-08-21 21:56:02 -0700793 * @hide Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -0700794 */
795 public void setBacklightBrightness(int brightness) {
796 try {
Jeff Brown96307042012-07-27 15:51:34 -0700797 mService.setTemporaryScreenBrightnessSettingOverride(brightness);
Jeff Brown1244cda2012-06-19 16:44:46 -0700798 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700799 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700800 }
801 }
802
803 /**
Jeff Brown96307042012-07-27 15:51:34 -0700804 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700805 *
Jeff Brown96307042012-07-27 15:51:34 -0700806 * @param level The wake lock level to check.
807 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -0700808 */
Jeff Brown96307042012-07-27 15:51:34 -0700809 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700810 try {
Jeff Brown96307042012-07-27 15:51:34 -0700811 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -0700812 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700813 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700814 }
815 }
816
817 /**
Jeff Brown037c33e2014-04-09 00:31:55 -0700818 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -0700819 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -0700820 * For historical reasons, the name of this method refers to the power state of
821 * the screen but it actually describes the overall interactive state of
822 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700823 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -0700824 * The value returned by this method only indicates whether the device is
825 * in an interactive state which may have nothing to do with the screen being
826 * on or off. To determine the actual state of the screen,
827 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700828 * </p>
829 *
Jeff Brown037c33e2014-04-09 00:31:55 -0700830 * @return True if the device is in an interactive state.
831 *
832 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -0700833 */
Jeff Brown037c33e2014-04-09 00:31:55 -0700834 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700835 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700836 return isInteractive();
837 }
838
839 /**
840 * Returns true if the device is in an interactive state.
841 * <p>
842 * When this method returns true, the device is awake and ready to interact
843 * with the user (although this is not a guarantee that the user is actively
844 * interacting with the device just this moment). The main screen is usually
845 * turned on while in this state. Certain features, such as the proximity
846 * sensor, may temporarily turn off the screen while still leaving the device in an
847 * interactive state. Note in particular that the device is still considered
848 * to be interactive while dreaming (since dreams can be interactive) but not
849 * when it is dozing or asleep.
850 * </p><p>
851 * When this method returns false, the device is dozing or asleep and must
852 * be awoken before it will become ready to interact with the user again. The
853 * main screen is usually turned off while in this state. Certain features,
854 * such as "ambient mode" may cause the main screen to remain on (albeit in a
855 * low power state) to display system-provided content while the device dozes.
856 * </p><p>
857 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
858 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
859 * whenever the interactive state of the device changes. For historical reasons,
860 * the names of these broadcasts refer to the power state of the screen
861 * but they are actually sent in response to changes in the overall interactive
862 * state of the device, as described by this method.
863 * </p><p>
864 * Services may use the non-interactive state as a hint to conserve power
865 * since the user is not present.
866 * </p>
867 *
868 * @return True if the device is in an interactive state.
869 *
870 * @see android.content.Intent#ACTION_SCREEN_ON
871 * @see android.content.Intent#ACTION_SCREEN_OFF
872 */
873 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -0700874 try {
Jeff Brown037c33e2014-04-09 00:31:55 -0700875 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -0700876 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700877 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700878 }
879 }
880
881 /**
882 * Reboot the device. Will not return if the reboot is successful.
883 * <p>
884 * Requires the {@link android.Manifest.permission#REBOOT} permission.
885 * </p>
886 *
887 * @param reason code to pass to the kernel (e.g., "recovery") to
888 * request special boot modes, or null.
889 */
890 public void reboot(String reason) {
891 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700892 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -0700893 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700894 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700895 }
896 }
897
898 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700899 * Returns true if the device is currently in power save mode. When in this mode,
900 * applications should reduce their functionality in order to conserve battery as
901 * much as possible. You can monitor for changes to this state with
902 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
903 *
904 * @return Returns true if currently in low power mode, else false.
905 */
906 public boolean isPowerSaveMode() {
907 try {
908 return mService.isPowerSaveMode();
909 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700910 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700911 }
912 }
913
914 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400915 * Set the current power save mode.
916 *
917 * @return True if the set was allowed.
918 *
919 * @see #isPowerSaveMode()
920 *
921 * @hide
922 */
923 public boolean setPowerSaveMode(boolean mode) {
924 try {
925 return mService.setPowerSaveMode(mode);
926 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700927 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400928 }
929 }
930
931 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700932 * Returns true if the device is currently in idle mode. This happens when a device
933 * has been sitting unused and unmoving for a sufficiently long period of time, so that
934 * it decides to go into a lower power-use state. This may involve things like turning
935 * off network access to apps. You can monitor for changes to this state with
936 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
937 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -0700938 * @return Returns true if currently in active device idle mode, else false. This is
939 * when idle mode restrictions are being actively applied; it will return false if the
940 * device is in a long-term idle mode but currently running a maintenance window where
941 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700942 */
943 public boolean isDeviceIdleMode() {
944 try {
945 return mService.isDeviceIdleMode();
946 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700947 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700948 }
949 }
950
951 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700952 * Returns true if the device is currently in light idle mode. This happens when a device
953 * has had its screen off for a short time, switching it into a batching mode where we
954 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
955 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
956 *
957 * @return Returns true if currently in active light device idle mode, else false. This is
958 * when light idle mode restrictions are being actively applied; it will return false if the
959 * device is in a long-term idle mode but currently running a maintenance window where
960 * restrictions have been lifted.
961 * @hide
962 */
963 public boolean isLightDeviceIdleMode() {
964 try {
965 return mService.isLightDeviceIdleMode();
966 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700967 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700968 }
969 }
970
971 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700972 * Return whether the given application package name is on the device's power whitelist.
973 * Apps can be placed on the whitelist through the settings UI invoked by
974 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
975 */
976 public boolean isIgnoringBatteryOptimizations(String packageName) {
977 synchronized (this) {
978 if (mIDeviceIdleController == null) {
979 mIDeviceIdleController = IDeviceIdleController.Stub.asInterface(
980 ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
981 }
982 }
983 try {
984 return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName);
985 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700986 throw e.rethrowFromSystemServer();
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700987 }
988 }
989
990 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -0800991 * Turn off the device.
992 *
993 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -0700994 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -0800995 * @param wait If true, this call waits for the shutdown to complete and does not return.
996 *
997 * @hide
998 */
Yusuke Sato705ffd12015-07-21 15:52:11 -0700999 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001000 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001001 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001002 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001003 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001004 }
1005 }
1006
1007 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001008 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1009 * This broadcast is only sent to registered receivers.
1010 */
1011 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1012 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1013 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1014
1015 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001016 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1017 * @hide
1018 */
1019 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1020 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1021 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1022
1023 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001024 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1025 * This broadcast is only sent to registered receivers.
1026 */
1027 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1028 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1029 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1030
1031 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001032 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1033 * This broadcast is only sent to registered receivers.
1034 * @hide
1035 */
1036 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1037 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1038 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1039
1040 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001041 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1042 * This broadcast is only sent to registered receivers.
1043 */
1044 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1045 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1046 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1047
1048 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001049 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1050 * This broadcast is only sent to registered receivers.
1051 */
1052 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1053 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1054 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1055
1056 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001057 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1058 * This broadcast is only sent to registered receivers.
1059 *
1060 * @hide
1061 */
1062 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1063 public static final String ACTION_POWER_SAVE_MODE_CHANGING
1064 = "android.os.action.POWER_SAVE_MODE_CHANGING";
1065
1066 /** @hide */
1067 public static final String EXTRA_POWER_SAVE_MODE = "mode";
1068
1069 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001070 * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed.
1071 * This broadcast is only sent to registered receivers.
1072 *
1073 * @hide
1074 **/
1075 @SystemApi
1076 public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED
1077 = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED";
1078
1079 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07001080 * A wake lock is a mechanism to indicate that your application needs
1081 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07001082 * <p>
1083 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00001084 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07001085 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
1086 * </p><p>
1087 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
1088 * on at the level that was requested when the wake lock was created.
1089 * </p><p>
1090 * Call {@link #release()} when you are done and don't need the lock anymore.
1091 * It is very important to do this as soon as possible to avoid running down the
1092 * device's battery excessively.
1093 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 */
Jeff Brown1244cda2012-06-19 16:44:46 -07001095 public final class WakeLock {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001096 private int mFlags;
1097 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07001098 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07001099 private final IBinder mToken;
1100 private int mCount;
1101 private boolean mRefCounted = true;
1102 private boolean mHeld;
1103 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001104 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07001105 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106
Jeff Brown1244cda2012-06-19 16:44:46 -07001107 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 public void run() {
1109 release();
1110 }
1111 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
Dianne Hackborn713df152013-05-17 11:27:57 -07001113 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 mFlags = flags;
1115 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07001116 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07001118 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07001122 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 synchronized (mToken) {
1124 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08001125 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07001126 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05001128 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001130 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
1133 }
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135
Jeff Brown1244cda2012-06-19 16:44:46 -07001136 /**
1137 * Sets whether this WakeLock is reference counted.
1138 * <p>
1139 * Wake locks are reference counted by default. If a wake lock is
1140 * reference counted, then each call to {@link #acquire()} must be
1141 * balanced by an equal number of calls to {@link #release()}. If a wake
1142 * lock is not reference counted, then one call to {@link #release()} is
1143 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
1144 * </p>
1145 *
1146 * @param value True to make the wake lock reference counted, false to
1147 * make the wake lock non-reference counted.
1148 */
1149 public void setReferenceCounted(boolean value) {
1150 synchronized (mToken) {
1151 mRefCounted = value;
1152 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154
Jeff Brown1244cda2012-06-19 16:44:46 -07001155 /**
1156 * Acquires the wake lock.
1157 * <p>
1158 * Ensures that the device is on at the level requested when
1159 * the wake lock was created.
1160 * </p>
1161 */
1162 public void acquire() {
1163 synchronized (mToken) {
1164 acquireLocked();
1165 }
1166 }
1167
1168 /**
1169 * Acquires the wake lock with a timeout.
1170 * <p>
1171 * Ensures that the device is on at the level requested when
1172 * the wake lock was created. The lock will be released after the given timeout
1173 * expires.
1174 * </p>
1175 *
1176 * @param timeout The timeout after which to release the wake lock, in milliseconds.
1177 */
1178 public void acquire(long timeout) {
1179 synchronized (mToken) {
1180 acquireLocked();
1181 mHandler.postDelayed(mReleaser, timeout);
1182 }
1183 }
1184
1185 private void acquireLocked() {
1186 if (!mRefCounted || mCount++ == 0) {
Jeff Brownff1baef2012-07-19 15:01:17 -07001187 // Do this even if the wake lock is already thought to be held (mHeld == true)
1188 // because non-reference counted wake locks are not always properly released.
1189 // For example, the keyguard's wake lock might be forcibly released by the
1190 // power manager without the keyguard knowing. A subsequent call to acquire
1191 // should immediately acquire the wake lock once again despite never having
1192 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07001193 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07001194 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07001195 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001196 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
1197 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07001198 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001199 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001200 }
Jeff Brownff1baef2012-07-19 15:01:17 -07001201 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07001202 }
1203 }
1204
1205 /**
1206 * Releases the wake lock.
1207 * <p>
1208 * This method releases your claim to the CPU or screen being on.
1209 * The screen may turn off shortly after you release the wake lock, or it may
1210 * not if there are other wake locks still held.
1211 * </p>
1212 */
1213 public void release() {
1214 release(0);
1215 }
1216
1217 /**
1218 * Releases the wake lock with flags to modify the release behavior.
1219 * <p>
1220 * This method releases your claim to the CPU or screen being on.
1221 * The screen may turn off shortly after you release the wake lock, or it may
1222 * not if there are other wake locks still held.
1223 * </p>
1224 *
1225 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07001226 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
1227 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001228 */
1229 public void release(int flags) {
1230 synchronized (mToken) {
1231 if (!mRefCounted || --mCount == 0) {
1232 mHandler.removeCallbacks(mReleaser);
1233 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07001234 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07001235 try {
1236 mService.releaseWakeLock(mToken, flags);
1237 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001238 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001239 }
1240 mHeld = false;
1241 }
1242 }
1243 if (mCount < 0) {
1244 throw new RuntimeException("WakeLock under-locked " + mTag);
1245 }
1246 }
1247 }
1248
1249 /**
1250 * Returns true if the wake lock has been acquired but not yet released.
1251 *
1252 * @return True if the wake lock is held.
1253 */
1254 public boolean isHeld() {
1255 synchronized (mToken) {
1256 return mHeld;
1257 }
1258 }
1259
1260 /**
1261 * Sets the work source associated with the wake lock.
1262 * <p>
1263 * The work source is used to determine on behalf of which application
1264 * the wake lock is being held. This is useful in the case where a
1265 * service is performing work on behalf of an application so that the
1266 * cost of that work can be accounted to the application.
1267 * </p>
1268 *
1269 * @param ws The work source, or null if none.
1270 */
1271 public void setWorkSource(WorkSource ws) {
1272 synchronized (mToken) {
1273 if (ws != null && ws.size() == 0) {
1274 ws = null;
1275 }
1276
1277 final boolean changed;
1278 if (ws == null) {
1279 changed = mWorkSource != null;
1280 mWorkSource = null;
1281 } else if (mWorkSource == null) {
1282 changed = true;
1283 mWorkSource = new WorkSource(ws);
1284 } else {
1285 changed = mWorkSource.diff(ws);
1286 if (changed) {
1287 mWorkSource.set(ws);
1288 }
1289 }
1290
1291 if (changed && mHeld) {
1292 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001293 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07001294 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001295 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001296 }
1297 }
1298 }
1299 }
1300
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001301 /** @hide */
1302 public void setTag(String tag) {
1303 mTag = tag;
1304 }
1305
1306 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001307 public void setHistoryTag(String tag) {
1308 mHistoryTag = tag;
1309 }
1310
1311 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001312 public void setUnimportantForLogging(boolean state) {
1313 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
1314 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
1315 }
1316
Jeff Brown1244cda2012-06-19 16:44:46 -07001317 @Override
1318 public String toString() {
1319 synchronized (mToken) {
1320 return "WakeLock{"
1321 + Integer.toHexString(System.identityHashCode(this))
1322 + " held=" + mHeld + ", refCount=" + mCount + "}";
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326}