blob: 3d17ffb7329f18c3824dc5e49847809fc8c4337e [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
Salvador Martineza6f7b252017-04-10 10:46:15 -070019import android.annotation.IntDef;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070021import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070022import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.SystemService;
Jeff Brown96307042012-07-27 15:51:34 -070024import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080026import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070027
Salvador Martineza6f7b252017-04-10 10:46:15 -070028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
Jeff Brown1244cda2012-06-19 16:44:46 -070032 * This class gives you control of the power state of the device.
33 *
34 * <p>
35 * <b>Device battery life will be significantly affected by the use of this API.</b>
36 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
37 * possible, and be sure to release them as soon as possible.
38 * </p><p>
Jeff Brown1244cda2012-06-19 16:44:46 -070039 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}.
40 * This will create a {@link PowerManager.WakeLock} object. You can then use methods
41 * on the wake lock object to control the power state of the device.
42 * </p><p>
43 * In practice it's quite simple:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 * {@samplecode
45 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
46 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
47 * wl.acquire();
48 * ..screen will stay on during this section..
49 * wl.release();
50 * }
Jeff Brown1244cda2012-06-19 16:44:46 -070051 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070052 * The following wake lock levels are defined, with varying effects on system power.
53 * <i>These levels are mutually exclusive - you may only specify one of them.</i>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070055 * <table>
Santos Cordon3107d292016-09-20 15:50:35 -070056 * <tr><th>Flag Value</th>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070059 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070060 * <td>On*</td> <td>Off</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070062 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070063 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070064 * <td>On</td> <td>Dim</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * </tr>
66 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070067 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070068 * <td>On</td> <td>Bright</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070070 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070071 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070072 * <td>On</td> <td>Bright</td> <td>Bright</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070075 * </p><p>
76 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any
77 * display timeouts or the state of the screen and even after the user presses the power button.
78 * In all other wake locks, the CPU will run, but the user can still put the device to sleep
79 * using the power button.</i>
80 * </p><p>
81 * In addition, you can add two more flags, which affect behavior of the screen only.
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070082 * <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 -080083 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070084 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070087 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause
89 * the illumination to remain on once it turns on (e.g. from user activity). This flag
90 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is
91 * acquired. A typical use would be for notifications which are important for the user to
Santos Cordon3107d292016-09-20 15:50:35 -070092 * see immediately.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070094 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070095 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is
Santos Cordon3107d292016-09-20 15:50:35 -070097 * released, causing the illumination to remain on a bit longer. This can be used to
98 * reduce flicker if you are cycling between wake lock conditions.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700101 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -0700102 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +0000103 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700104 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600106@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -0700107public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700109
Jeff Brown155fc702012-07-27 12:12:15 -0700110 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
111 * combinations were actually supported so the bit field was removed. This explains
112 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
113 * value can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700117 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
118 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700119 * <p>
120 * If the user presses the power button, then the screen will be turned off
121 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -0700122 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
Jeff Brown155fc702012-07-27 12:12:15 -0700124 public static final int PARTIAL_WAKE_LOCK = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
126 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700127 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700128 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700129 * <p>
130 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
131 * implicitly released by the system, causing both the screen and the CPU to be turned off.
132 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
133 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700134 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700135 * @deprecated Most applications should use
136 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
137 * of this type of wake lock, as it will be correctly managed by the platform
138 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700140 @Deprecated
Jeff Brown155fc702012-07-27 12:12:15 -0700141 public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700144 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700146 * <p>
147 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
148 * implicitly released by the system, causing both the screen and the CPU to be turned off.
149 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
150 * </p>
151 *
152 * @deprecated Most applications should use
153 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
154 * of this type of wake lock, as it will be correctly managed by the platform
155 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 */
Jeff Brown155fc702012-07-27 12:12:15 -0700157 @Deprecated
158 public static final int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a;
159
160 /**
161 * Wake lock level: Ensures that the screen and keyboard backlight are on at
162 * full brightness.
163 * <p>
164 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
165 * implicitly released by the system, causing both the screen and the CPU to be turned off.
166 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
167 * </p>
168 *
169 * @deprecated Most applications should use
170 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
171 * of this type of wake lock, as it will be correctly managed by the platform
172 * as the user moves between applications and doesn't require a special permission.
173 */
174 @Deprecated
175 public static final int FULL_WAKE_LOCK = 0x0000001a;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700178 * Wake lock level: Turns the screen off when the proximity sensor activates.
179 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700180 * If the proximity sensor detects that an object is nearby, the screen turns off
181 * immediately. Shortly after the object moves away, the screen turns on again.
182 * </p><p>
183 * A proximity wake lock does not prevent the device from falling asleep
184 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
185 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
186 * wake locks are held, then the device will fall asleep (and lock) as usual.
187 * However, the device will not fall asleep while the screen has been turned off
188 * by the proximity sensor because it effectively counts as ongoing user activity.
189 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700190 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700191 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800192 * </p><p>
193 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700194 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700195 */
Jeff Brown155fc702012-07-27 12:12:15 -0700196 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700197
198 /**
Jeff Brown26875502014-01-30 21:47:47 -0800199 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
200 * if no other wake locks are held.
201 * <p>
202 * This is used by the dream manager to implement doze mode. It currently
203 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700204 * </p><p>
205 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800206 * </p>
207 *
208 * {@hide}
209 */
210 public static final int DOZE_WAKE_LOCK = 0x00000040;
211
212 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800213 * Wake lock level: Keep the device awake enough to allow drawing to occur.
214 * <p>
215 * This is used by the window manager to allow applications to draw while the
216 * system is dozing. It currently has no effect unless the power manager is in
217 * the dozing state.
218 * </p><p>
219 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
220 * </p>
221 *
222 * {@hide}
223 */
224 public static final int DRAW_WAKE_LOCK = 0x00000080;
225
226 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700227 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500228 *
Jeff Brown155fc702012-07-27 12:12:15 -0700229 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500230 */
Jeff Brown155fc702012-07-27 12:12:15 -0700231 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500232
233 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700234 * Wake lock flag: Turn the screen on when the wake lock is acquired.
235 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700237 * the screen to remain on once it's already on. Think of the video player
238 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700240 * </p><p>
241 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
242 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
Jeff Brown155fc702012-07-27 12:12:15 -0700244 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700247 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * so the screen stays on for a little longer.
249 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700250 * Will not turn the screen on if it is not already on.
251 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
252 * </p><p>
253 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
254 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
Jeff Brown155fc702012-07-27 12:12:15 -0700256 public static final int ON_AFTER_RELEASE = 0x20000000;
257
258 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800259 * Wake lock flag: This wake lock is not important for logging events. If a later
260 * wake lock is acquired that is important, it will be considered the one to log.
261 * @hide
262 */
263 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
264
265 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700266 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
267 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
268 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700269 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600270 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
271
272 /**
273 * Flag for {@link WakeLock#release(int)} when called due to timeout.
274 * @hide
275 */
276 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
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 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700321 * User activity event type: Accessibility taking action on behalf of user.
322 * @hide
323 */
324 @SystemApi
325 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
326
327 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700328 * User activity flag: If already dimmed, extend the dim timeout
329 * but do not brighten. This flag is useful for keeping the screen on
330 * a little longer without causing a visible change such as when
331 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700332 * @hide
333 */
Jeff Brown0a571122014-08-21 21:50:43 -0700334 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700335 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
336
337 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700338 * User activity flag: Note the user activity as usual but do not
339 * reset the user activity timeout. This flag is useful for applying
340 * user activity power hints when interacting with the device indirectly
341 * on a secondary screen while allowing the primary screen to go to sleep.
342 * @hide
343 */
344 @SystemApi
345 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
346
347 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700348 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700349 * @hide
350 */
Jeff Brownc12035c2014-08-13 18:52:25 -0700351 public static final int GO_TO_SLEEP_REASON_APPLICATION = 0;
Jeff Brown96307042012-07-27 15:51:34 -0700352
353 /**
354 * Go to sleep reason code: Going to sleep due by request of the
355 * device administration policy.
356 * @hide
357 */
358 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
359
360 /**
361 * Go to sleep reason code: Going to sleep due to a screen timeout.
362 * @hide
363 */
364 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
365
Doug Zongker3b0218b2014-01-14 12:29:06 -0800366 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700367 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
368 * @hide
369 */
370 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
371
372 /**
373 * Go to sleep reason code: Going to sleep due to the power button being pressed.
374 * @hide
375 */
376 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
377
378 /**
379 * Go to sleep reason code: Going to sleep due to HDMI.
380 * @hide
381 */
382 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
383
384 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700385 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
386 * @hide
387 */
388 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
389
390 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700391 * Go to sleep reason code: Going to sleep by request of an accessibility service
392 * @hide
393 */
394 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
395
396 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700397 * Go to sleep flag: Skip dozing state and directly go to full sleep.
398 * @hide
399 */
400 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
401
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 tasks other than applying system updates, such as
405 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800406 * <p>
407 * Requires the {@link android.Manifest.permission#RECOVERY}
408 * permission (in addition to
409 * {@link android.Manifest.permission#REBOOT}).
410 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700411 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800412 */
413 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700414
415 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800416 * The value to pass as the 'reason' argument to reboot() to reboot into
417 * recovery mode for applying system updates.
418 * <p>
419 * Requires the {@link android.Manifest.permission#RECOVERY}
420 * permission (in addition to
421 * {@link android.Manifest.permission#REBOOT}).
422 * </p>
423 * @hide
424 */
425 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
426
427 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000428 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
429 * the device.
430 * @hide
431 */
432 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
433
434 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700435 * The 'reason' value used when rebooting in safe mode
436 * @hide
437 */
438 public static final String REBOOT_SAFE_MODE = "safemode";
439
440 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800441 * The 'reason' value used when rebooting the device without turning on the screen.
442 * @hide
443 */
444 public static final String REBOOT_QUIESCENT = "quiescent";
445
446 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700447 * The value to pass as the 'reason' argument to android_reboot().
448 * @hide
449 */
450 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
451
Salvador Martineza6f7b252017-04-10 10:46:15 -0700452 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700453 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
454 * is too high.
455 * @hide
456 */
457 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
458
459 /**
460 * The value to pass as the 'reason' argument to android_reboot() when device is running
461 * critically low on battery.
462 * @hide
463 */
464 public static final String SHUTDOWN_LOW_BATTERY = "battery";
465
466 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700467 * @hide
468 */
469 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700470 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700471 SHUTDOWN_REASON_UNKNOWN,
472 SHUTDOWN_REASON_SHUTDOWN,
473 SHUTDOWN_REASON_REBOOT,
474 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700475 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
476 SHUTDOWN_REASON_LOW_BATTERY,
477 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700478 })
479 public @interface ShutdownReason {}
480
481 /**
482 * constant for shutdown reason being unknown.
483 * @hide
484 */
485 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
486
487 /**
488 * constant for shutdown reason being normal shutdown.
489 * @hide
490 */
491 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
492
493 /**
494 * constant for shutdown reason being reboot.
495 * @hide
496 */
497 public static final int SHUTDOWN_REASON_REBOOT = 2;
498
499 /**
500 * constant for shutdown reason being user requested.
501 * @hide
502 */
503 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
504
505 /**
506 * constant for shutdown reason being overheating.
507 * @hide
508 */
509 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
510
Sudheer Shanka292637f2017-09-25 10:36:23 -0700511 /**
512 * constant for shutdown reason being low battery.
513 * @hide
514 */
515 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
516
517 /**
518 * constant for shutdown reason being critical battery thermal state.
519 * @hide
520 */
521 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
522
Makoto Onuki2eccd022017-11-01 13:44:23 -0700523 /**
524 * @hide
525 */
526 @Retention(RetentionPolicy.SOURCE)
527 @IntDef({ServiceType.GPS,
528 ServiceType.VIBRATION,
529 ServiceType.ANIMATION,
530 ServiceType.FULL_BACKUP,
531 ServiceType.KEYVALUE_BACKUP,
532 ServiceType.NETWORK_FIREWALL,
533 ServiceType.SCREEN_BRIGHTNESS,
534 ServiceType.SOUND,
535 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800536 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800537 ServiceType.FORCE_ALL_APPS_STANDBY,
Makoto Onukiaae89532017-11-08 14:32:03 -0800538 ServiceType.OPTIONAL_SENSORS,
539 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700540 public @interface ServiceType {
541 int NULL = 0;
542 int GPS = 1;
543 int VIBRATION = 2;
544 int ANIMATION = 3;
545 int FULL_BACKUP = 4;
546 int KEYVALUE_BACKUP = 5;
547 int NETWORK_FIREWALL = 6;
548 int SCREEN_BRIGHTNESS = 7;
549 int SOUND = 8;
550 int BATTERY_STATS = 9;
551 int DATA_SAVER = 10;
Makoto Onukiaae89532017-11-08 14:32:03 -0800552
553 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800554 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800555 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800556 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800557
558 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800559 * Whether to enable background check on all apps or not.
560 */
561 int FORCE_BACKGROUND_CHECK = 12;
562
563 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800564 * Whether to disable non-essential sensors. (e.g. edge sensors.)
565 */
566 int OPTIONAL_SENSORS = 13;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700567 }
568
Makoto Onuki57f0f552017-12-11 12:22:18 -0800569 /**
570 * Either the location providers shouldn't be affected by battery saver,
571 * or battery saver is off.
572 */
573 public static final int LOCATION_MODE_NO_CHANGE = 0;
574
575 /**
576 * In this mode, the GPS based location provider should be disabled when battery saver is on and
577 * the device is non-interactive.
578 */
579 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
580
581 /**
582 * All location providers should be disabled when battery saver is on and
583 * the device is non-interactive.
584 */
585 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
586
587 /**
588 * In this mode, all the location providers will be kept available, but location fixes
589 * should only be provided to foreground apps.
590 */
591 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
592
593 /**
594 * @hide
595 */
596 @Retention(RetentionPolicy.SOURCE)
597 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
598 LOCATION_MODE_NO_CHANGE,
599 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
600 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
601 LOCATION_MODE_FOREGROUND_ONLY,
602 })
603 public @interface LocationPowerSaveMode {}
604
Jeff Brown96307042012-07-27 15:51:34 -0700605 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700606 final IPowerManager mService;
607 final Handler mHandler;
608
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700609 IDeviceIdleController mIDeviceIdleController;
610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700612 * {@hide}
613 */
Jeff Brown96307042012-07-27 15:51:34 -0700614 public PowerManager(Context context, IPowerManager service, Handler handler) {
615 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700616 mService = service;
617 mHandler = handler;
618 }
619
620 /**
Jeff Brown96307042012-07-27 15:51:34 -0700621 * Gets the minimum supported screen brightness setting.
622 * The screen may be allowed to become dimmer than this value but
623 * this is the minimum value that can be set by the user.
624 * @hide
625 */
626 public int getMinimumScreenBrightnessSetting() {
627 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700628 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700629 }
630
631 /**
632 * Gets the maximum supported screen brightness setting.
633 * The screen may be allowed to become dimmer than this value but
634 * this is the maximum value that can be set by the user.
635 * @hide
636 */
637 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700638 return mContext.getResources().getInteger(
639 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700640 }
641
642 /**
643 * Gets the default screen brightness setting.
644 * @hide
645 */
646 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700647 return mContext.getResources().getInteger(
648 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700649 }
650
651 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700652 * Gets the minimum supported screen brightness setting for VR Mode.
653 * @hide
654 */
655 public int getMinimumScreenBrightnessForVrSetting() {
656 return mContext.getResources().getInteger(
657 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
658 }
659
660 /**
661 * Gets the maximum supported screen brightness setting for VR Mode.
662 * The screen may be allowed to become dimmer than this value but
663 * this is the maximum value that can be set by the user.
664 * @hide
665 */
666 public int getMaximumScreenBrightnessForVrSetting() {
667 return mContext.getResources().getInteger(
668 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
669 }
670
671 /**
672 * Gets the default screen brightness for VR setting.
673 * @hide
674 */
675 public int getDefaultScreenBrightnessForVrSetting() {
676 return mContext.getResources().getInteger(
677 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
678 }
679
680 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700681 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700682 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700683 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
684 * combined using the logical OR operator.
685 * </p><p>
686 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
687 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
688 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
689 * specified as part of the {@code levelAndFlags} parameter.
690 * </p><p>
691 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
692 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
693 * {@code levelAndFlags} parameters.
694 * </p><p>
695 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
696 * wake lock, and {@link WakeLock#release release()} when you are done.
697 * </p><p>
698 * {@samplecode
699 * PowerManager pm = (PowerManager)mContext.getSystemService(
700 * Context.POWER_SERVICE);
701 * PowerManager.WakeLock wl = pm.newWakeLock(
702 * PowerManager.SCREEN_DIM_WAKE_LOCK
703 * | PowerManager.ON_AFTER_RELEASE,
704 * TAG);
705 * wl.acquire();
706 * // ... do work...
707 * wl.release();
708 * }
709 * </p><p>
710 * Although a wake lock can be created without special permissions,
711 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
712 * required to actually acquire or release the wake lock that is returned.
713 * </p><p class="note">
714 * If using this to keep the screen on, you should strongly consider using
715 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
716 * This window flag will be correctly managed by the platform
717 * as the user moves between applications and doesn't require a special permission.
718 * </p>
719 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000720 * <p>
721 * Recommended naming conventions for tags to make debugging easier:
722 * <ul>
723 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
724 * gmail:mytag) to make it easier to understand where the wake locks comes
725 * from. This namespace will also avoid collision for tags inside your app
726 * coming from different libraries which will make debugging easier.
727 * <li>use constants (e.g. do not include timestamps in the tag) to make it
728 * easier for tools to aggregate similar wake locks. When collecting
729 * debugging data, the platform only monitors a finite number of tags,
730 * using constants will help tools to provide better debugging data.
731 * <li>avoid using Class#getName() or similar method since this class name
732 * can be transformed by java optimizer and obfuscator tools.
733 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
734 * tags from the platform (e.g. *alarm*).
735 * <li>never include personnally identifiable information for privacy
736 * reasons.
737 * </ul>
738 * </p>
739 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700740 * @param levelAndFlags Combination of wake lock level and flag values defining
741 * the requested behavior of the WakeLock.
742 * @param tag Your class name (or other tag) for debugging purposes.
743 *
744 * @see WakeLock#acquire()
745 * @see WakeLock#release()
746 * @see #PARTIAL_WAKE_LOCK
747 * @see #FULL_WAKE_LOCK
748 * @see #SCREEN_DIM_WAKE_LOCK
749 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700750 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700751 * @see #ACQUIRE_CAUSES_WAKEUP
752 * @see #ON_AFTER_RELEASE
753 */
754 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700755 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700756 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700757 }
758
759 /** @hide */
760 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
761 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
762 case PARTIAL_WAKE_LOCK:
763 case SCREEN_DIM_WAKE_LOCK:
764 case SCREEN_BRIGHT_WAKE_LOCK:
765 case FULL_WAKE_LOCK:
766 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800767 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800768 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700769 break;
770 default:
771 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700772 }
773 if (tag == null) {
774 throw new IllegalArgumentException("The tag must not be null.");
775 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700776 }
777
778 /**
779 * Notifies the power manager that user activity happened.
780 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700781 * Resets the auto-off timer and brightens the screen if the device
782 * is not asleep. This is what happens normally when a key or the touch
783 * screen is pressed or when some other user activity occurs.
784 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700785 * </p><p>
786 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
787 * </p>
788 *
789 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700790 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700791 * other power management functions. It should be set
792 * to the timestamp of the input event that caused the user activity.
793 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
794 * because of this event. This is set when the power key is pressed.
795 * We want the device to stay on while the button is down, but we're about
796 * to turn off the screen so we don't want the keyboard backlight to turn on again.
797 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700798 *
799 * @see #wakeUp
800 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700801 *
802 * @removed Requires signature or system permission.
803 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700804 */
Jeff Brown7d827512014-08-21 21:56:02 -0700805 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700806 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -0700807 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
808 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
809 }
810
811 /**
812 * Notifies the power manager that user activity happened.
813 * <p>
814 * Resets the auto-off timer and brightens the screen if the device
815 * is not asleep. This is what happens normally when a key or the touch
816 * screen is pressed or when some other user activity occurs.
817 * This method does not wake up the device if it has been put to sleep.
818 * </p><p>
819 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
820 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
821 * </p>
822 *
823 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
824 * time base. This timestamp is used to correctly order the user activity request with
825 * other power management functions. It should be set
826 * to the timestamp of the input event that caused the user activity.
827 * @param event The user activity event.
828 * @param flags Optional user activity flags.
829 *
830 * @see #wakeUp
831 * @see #goToSleep
832 *
833 * @hide Requires signature or system permission.
834 */
835 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600836 @RequiresPermission(anyOf = {
837 android.Manifest.permission.DEVICE_POWER,
838 android.Manifest.permission.USER_ACTIVITY
839 })
Jeff Brown0a571122014-08-21 21:50:43 -0700840 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700841 try {
Jeff Brown0a571122014-08-21 21:50:43 -0700842 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -0700843 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700844 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700845 }
846 }
847
848 /**
849 * Forces the device to go to sleep.
850 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700851 * Overrides all the wake locks that are held.
852 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -0700853 * </p><p>
854 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
855 * </p>
856 *
857 * @param time The time when the request to go to sleep was issued, in the
858 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700859 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -0700860 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -0700861 *
862 * @see #userActivity
863 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -0700864 *
865 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -0700866 */
867 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -0700868 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -0700869 }
870
871 /**
Jeff Brown7d827512014-08-21 21:56:02 -0700872 * Forces the device to go to sleep.
873 * <p>
874 * Overrides all the wake locks that are held.
875 * This is what happens when the power key is pressed to turn off the screen.
876 * </p><p>
877 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
878 * </p>
879 *
880 * @param time The time when the request to go to sleep was issued, in the
881 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
882 * order the go to sleep request with other power management functions. It should be set
883 * to the timestamp of the input event that caused the request to go to sleep.
884 * @param reason The reason the device is going to sleep.
885 * @param flags Optional flags to apply when going to sleep.
886 *
887 * @see #userActivity
888 * @see #wakeUp
889 *
890 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -0700891 */
892 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -0700893 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -0700894 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -0700895 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700896 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -0700897 }
898 }
899
900 /**
901 * Forces the device to wake up from sleep.
902 * <p>
903 * If the device is currently asleep, wakes it up, otherwise does nothing.
904 * This is what happens when the power key is pressed to turn on the screen.
905 * </p><p>
906 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
907 * </p>
908 *
909 * @param time The time when the request to wake up was issued, in the
910 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -0700911 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -0700912 * to the timestamp of the input event that caused the request to wake up.
913 *
914 * @see #userActivity
915 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700916 *
917 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -0700918 */
919 public void wakeUp(long time) {
920 try {
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700921 mService.wakeUp(time, "wakeUp", mContext.getOpPackageName());
922 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700923 throw e.rethrowFromSystemServer();
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700924 }
925 }
926
927 /**
928 * @hide
929 */
930 public void wakeUp(long time, String reason) {
931 try {
932 mService.wakeUp(time, reason, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -0700933 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700934 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -0700935 }
936 }
937
938 /**
Jeff Brown62c82e42012-09-26 01:30:41 -0700939 * Forces the device to start napping.
940 * <p>
941 * If the device is currently awake, starts dreaming, otherwise does nothing.
942 * When the dream ends or if the dream cannot be started, the device will
943 * either wake up or go to sleep depending on whether there has been recent
944 * user activity.
945 * </p><p>
946 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
947 * </p>
948 *
949 * @param time The time when the request to nap was issued, in the
950 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
951 * order the nap request with other power management functions. It should be set
952 * to the timestamp of the input event that caused the request to nap.
953 *
954 * @see #wakeUp
955 * @see #goToSleep
956 *
Jeff Brown7d827512014-08-21 21:56:02 -0700957 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -0700958 */
959 public void nap(long time) {
960 try {
961 mService.nap(time);
962 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700963 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -0700964 }
965 }
966
967 /**
Jeff Browne333e672014-10-28 13:48:55 -0700968 * Boosts the brightness of the screen to maximum for a predetermined
969 * period of time. This is used to make the screen more readable in bright
970 * daylight for a short duration.
971 * <p>
972 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
973 * </p>
974 *
975 * @param time The time when the request to boost was issued, in the
976 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
977 * order the boost request with other power management functions. It should be set
978 * to the timestamp of the input event that caused the request to boost.
979 *
980 * @hide Requires signature permission.
981 */
982 public void boostScreenBrightness(long time) {
983 try {
984 mService.boostScreenBrightness(time);
985 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700986 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -0700987 }
988 }
989
990 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -0700991 * Returns whether the screen brightness is currently boosted to maximum, caused by a call
992 * to {@link #boostScreenBrightness(long)}.
993 * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise.
994 *
Julius D'souzab22da802017-06-09 10:27:14 -0700995 * @deprecated This call is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -0700996 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -0600997 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -0700998 */
Julius D'souzab22da802017-06-09 10:27:14 -0700999 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001000 public boolean isScreenBrightnessBoosted() {
Julius D'souzab22da802017-06-09 10:27:14 -07001001 return false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001002 }
1003
Jeff Brown1244cda2012-06-19 16:44:46 -07001004 /**
Jeff Brown96307042012-07-27 15:51:34 -07001005 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001006 *
Jeff Brown96307042012-07-27 15:51:34 -07001007 * @param level The wake lock level to check.
1008 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001009 */
Jeff Brown96307042012-07-27 15:51:34 -07001010 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001011 try {
Jeff Brown96307042012-07-27 15:51:34 -07001012 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001013 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001014 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001015 }
1016 }
1017
1018 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001019 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001020 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001021 * For historical reasons, the name of this method refers to the power state of
1022 * the screen but it actually describes the overall interactive state of
1023 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001024 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001025 * The value returned by this method only indicates whether the device is
1026 * in an interactive state which may have nothing to do with the screen being
1027 * on or off. To determine the actual state of the screen,
1028 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001029 * </p>
1030 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001031 * @return True if the device is in an interactive state.
1032 *
1033 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001034 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001035 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001036 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001037 return isInteractive();
1038 }
1039
1040 /**
1041 * Returns true if the device is in an interactive state.
1042 * <p>
1043 * When this method returns true, the device is awake and ready to interact
1044 * with the user (although this is not a guarantee that the user is actively
1045 * interacting with the device just this moment). The main screen is usually
1046 * turned on while in this state. Certain features, such as the proximity
1047 * sensor, may temporarily turn off the screen while still leaving the device in an
1048 * interactive state. Note in particular that the device is still considered
1049 * to be interactive while dreaming (since dreams can be interactive) but not
1050 * when it is dozing or asleep.
1051 * </p><p>
1052 * When this method returns false, the device is dozing or asleep and must
1053 * be awoken before it will become ready to interact with the user again. The
1054 * main screen is usually turned off while in this state. Certain features,
1055 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1056 * low power state) to display system-provided content while the device dozes.
1057 * </p><p>
1058 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1059 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1060 * whenever the interactive state of the device changes. For historical reasons,
1061 * the names of these broadcasts refer to the power state of the screen
1062 * but they are actually sent in response to changes in the overall interactive
1063 * state of the device, as described by this method.
1064 * </p><p>
1065 * Services may use the non-interactive state as a hint to conserve power
1066 * since the user is not present.
1067 * </p>
1068 *
1069 * @return True if the device is in an interactive state.
1070 *
1071 * @see android.content.Intent#ACTION_SCREEN_ON
1072 * @see android.content.Intent#ACTION_SCREEN_OFF
1073 */
1074 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001075 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001076 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001077 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001078 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001079 }
1080 }
1081
1082 /**
1083 * Reboot the device. Will not return if the reboot is successful.
1084 * <p>
1085 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1086 * </p>
1087 *
1088 * @param reason code to pass to the kernel (e.g., "recovery") to
1089 * request special boot modes, or null.
1090 */
1091 public void reboot(String reason) {
1092 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001093 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001094 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001095 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001096 }
1097 }
1098
1099 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001100 * Reboot the device. Will not return if the reboot is successful.
1101 * <p>
1102 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1103 * </p>
1104 * @hide
1105 */
1106 public void rebootSafeMode() {
1107 try {
1108 mService.rebootSafeMode(false, true);
1109 } catch (RemoteException e) {
1110 throw e.rethrowFromSystemServer();
1111 }
1112 }
1113
1114 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001115 * Returns true if the device is currently in power save mode. When in this mode,
1116 * applications should reduce their functionality in order to conserve battery as
1117 * much as possible. You can monitor for changes to this state with
1118 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1119 *
1120 * @return Returns true if currently in low power mode, else false.
1121 */
1122 public boolean isPowerSaveMode() {
1123 try {
1124 return mService.isPowerSaveMode();
1125 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001126 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001127 }
1128 }
1129
1130 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001131 * Set the current power save mode.
1132 *
1133 * @return True if the set was allowed.
1134 *
1135 * @see #isPowerSaveMode()
1136 *
1137 * @hide
1138 */
1139 public boolean setPowerSaveMode(boolean mode) {
1140 try {
1141 return mService.setPowerSaveMode(mode);
1142 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001143 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001144 }
1145 }
1146
1147 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001148 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001149 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001150 * @return Battery saver state data.
1151 *
1152 * @hide
1153 * @see com.android.server.power.BatterySaverPolicy
1154 * @see PowerSaveState
1155 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001156 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001157 try {
1158 return mService.getPowerSaveState(serviceType);
1159 } catch (RemoteException e) {
1160 throw e.rethrowFromSystemServer();
1161 }
1162 }
1163
1164 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001165 * Returns how location features should behave when battery saver is on. When battery saver
1166 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1167 *
1168 * <p>This API is normally only useful for components that provide location features.
1169 *
1170 * @see #isPowerSaveMode()
1171 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1172 */
1173 @LocationPowerSaveMode
1174 public int getLocationPowerSaveMode() {
1175 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.GPS);
1176 if (!powerSaveState.globalBatterySaverEnabled) {
1177 return LOCATION_MODE_NO_CHANGE;
1178 }
1179 return powerSaveState.gpsMode;
1180 }
1181
1182 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001183 * Returns true if the device is currently in idle mode. This happens when a device
1184 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1185 * it decides to go into a lower power-use state. This may involve things like turning
1186 * off network access to apps. You can monitor for changes to this state with
1187 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1188 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001189 * @return Returns true if currently in active device idle mode, else false. This is
1190 * when idle mode restrictions are being actively applied; it will return false if the
1191 * device is in a long-term idle mode but currently running a maintenance window where
1192 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001193 */
1194 public boolean isDeviceIdleMode() {
1195 try {
1196 return mService.isDeviceIdleMode();
1197 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001198 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001199 }
1200 }
1201
1202 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001203 * Returns true if the device is currently in light idle mode. This happens when a device
1204 * has had its screen off for a short time, switching it into a batching mode where we
1205 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1206 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1207 *
1208 * @return Returns true if currently in active light device idle mode, else false. This is
1209 * when light idle mode restrictions are being actively applied; it will return false if the
1210 * device is in a long-term idle mode but currently running a maintenance window where
1211 * restrictions have been lifted.
1212 * @hide
1213 */
1214 public boolean isLightDeviceIdleMode() {
1215 try {
1216 return mService.isLightDeviceIdleMode();
1217 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001218 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001219 }
1220 }
1221
1222 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001223 * Return whether the given application package name is on the device's power whitelist.
1224 * Apps can be placed on the whitelist through the settings UI invoked by
1225 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1226 */
1227 public boolean isIgnoringBatteryOptimizations(String packageName) {
1228 synchronized (this) {
1229 if (mIDeviceIdleController == null) {
1230 mIDeviceIdleController = IDeviceIdleController.Stub.asInterface(
1231 ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
1232 }
1233 }
1234 try {
1235 return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName);
1236 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001237 throw e.rethrowFromSystemServer();
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001238 }
1239 }
1240
1241 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001242 * Turn off the device.
1243 *
1244 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001245 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001246 * @param wait If true, this call waits for the shutdown to complete and does not return.
1247 *
1248 * @hide
1249 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001250 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001251 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001252 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001253 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001254 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001255 }
1256 }
1257
1258 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001259 * This function checks if the device has implemented Sustained Performance
1260 * Mode. This needs to be checked only once and is constant for a particular
1261 * device/release.
1262 *
1263 * Sustained Performance Mode is intended to provide a consistent level of
1264 * performance for prolonged amount of time.
1265 *
1266 * Applications should check if the device supports this mode, before using
1267 * {@link android.view.Window#setSustainedPerformanceMode}.
1268 *
1269 * @return Returns True if the device supports it, false otherwise.
1270 *
1271 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001272 */
1273 public boolean isSustainedPerformanceModeSupported() {
1274 return mContext.getResources().getBoolean(
1275 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1276 }
1277
1278 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001279 * Returns the reason the phone was last shutdown. Calling app must have the
1280 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1281 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1282 * not be accessed.
1283 * @hide
1284 */
1285 @ShutdownReason
1286 public int getLastShutdownReason() {
1287 try {
1288 return mService.getLastShutdownReason();
1289 } catch (RemoteException e) {
1290 throw e.rethrowFromSystemServer();
1291 }
1292 }
1293
1294 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001295 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1296 * This broadcast is only sent to registered receivers.
1297 */
1298 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1299 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1300 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1301
1302 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001303 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1304 * @hide
1305 */
1306 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1307 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1308 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1309
1310 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001311 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1312 * This broadcast is only sent to registered receivers.
1313 */
1314 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1315 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1316 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1317
1318 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001319 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1320 * This broadcast is only sent to registered receivers.
1321 * @hide
1322 */
1323 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1324 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1325 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1326
1327 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001328 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1329 * This broadcast is only sent to registered receivers.
1330 */
1331 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1332 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1333 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1334
1335 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001336 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1337 * This broadcast is only sent to registered receivers.
1338 */
1339 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1340 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1341 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1342
1343 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001344 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1345 * This broadcast is only sent to registered receivers.
1346 *
1347 * @hide
1348 */
1349 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1350 public static final String ACTION_POWER_SAVE_MODE_CHANGING
1351 = "android.os.action.POWER_SAVE_MODE_CHANGING";
1352
1353 /** @hide */
1354 public static final String EXTRA_POWER_SAVE_MODE = "mode";
1355
1356 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001357 * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed.
1358 * This broadcast is only sent to registered receivers.
1359 *
Julius D'souzab22da802017-06-09 10:27:14 -07001360 * @deprecated This intent is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001361 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001362 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001363 **/
Julius D'souzab22da802017-06-09 10:27:14 -07001364 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001365 public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED
1366 = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED";
1367
1368 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07001369 * A wake lock is a mechanism to indicate that your application needs
1370 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07001371 * <p>
1372 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00001373 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07001374 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
1375 * </p><p>
1376 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
1377 * on at the level that was requested when the wake lock was created.
1378 * </p><p>
1379 * Call {@link #release()} when you are done and don't need the lock anymore.
1380 * It is very important to do this as soon as possible to avoid running down the
1381 * device's battery excessively.
1382 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 */
Jeff Brown1244cda2012-06-19 16:44:46 -07001384 public final class WakeLock {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001385 private int mFlags;
1386 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07001387 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07001388 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001389 private int mInternalCount;
1390 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07001391 private boolean mRefCounted = true;
1392 private boolean mHeld;
1393 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001394 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07001395 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396
Jeff Brown1244cda2012-06-19 16:44:46 -07001397 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001399 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402
Dianne Hackborn713df152013-05-17 11:27:57 -07001403 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 mFlags = flags;
1405 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07001406 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07001408 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07001412 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 synchronized (mToken) {
1414 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08001415 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07001416 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05001418 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001420 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 }
1424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425
Jeff Brown1244cda2012-06-19 16:44:46 -07001426 /**
1427 * Sets whether this WakeLock is reference counted.
1428 * <p>
1429 * Wake locks are reference counted by default. If a wake lock is
1430 * reference counted, then each call to {@link #acquire()} must be
1431 * balanced by an equal number of calls to {@link #release()}. If a wake
1432 * lock is not reference counted, then one call to {@link #release()} is
1433 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
1434 * </p>
1435 *
1436 * @param value True to make the wake lock reference counted, false to
1437 * make the wake lock non-reference counted.
1438 */
1439 public void setReferenceCounted(boolean value) {
1440 synchronized (mToken) {
1441 mRefCounted = value;
1442 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444
Jeff Brown1244cda2012-06-19 16:44:46 -07001445 /**
1446 * Acquires the wake lock.
1447 * <p>
1448 * Ensures that the device is on at the level requested when
1449 * the wake lock was created.
1450 * </p>
1451 */
1452 public void acquire() {
1453 synchronized (mToken) {
1454 acquireLocked();
1455 }
1456 }
1457
1458 /**
1459 * Acquires the wake lock with a timeout.
1460 * <p>
1461 * Ensures that the device is on at the level requested when
1462 * the wake lock was created. The lock will be released after the given timeout
1463 * expires.
1464 * </p>
1465 *
1466 * @param timeout The timeout after which to release the wake lock, in milliseconds.
1467 */
1468 public void acquire(long timeout) {
1469 synchronized (mToken) {
1470 acquireLocked();
1471 mHandler.postDelayed(mReleaser, timeout);
1472 }
1473 }
1474
1475 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001476 mInternalCount++;
1477 mExternalCount++;
1478 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07001479 // Do this even if the wake lock is already thought to be held (mHeld == true)
1480 // because non-reference counted wake locks are not always properly released.
1481 // For example, the keyguard's wake lock might be forcibly released by the
1482 // power manager without the keyguard knowing. A subsequent call to acquire
1483 // should immediately acquire the wake lock once again despite never having
1484 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07001485 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07001486 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07001487 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001488 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
1489 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07001490 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001491 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001492 }
Jeff Brownff1baef2012-07-19 15:01:17 -07001493 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07001494 }
1495 }
1496
1497 /**
1498 * Releases the wake lock.
1499 * <p>
1500 * This method releases your claim to the CPU or screen being on.
1501 * The screen may turn off shortly after you release the wake lock, or it may
1502 * not if there are other wake locks still held.
1503 * </p>
1504 */
1505 public void release() {
1506 release(0);
1507 }
1508
1509 /**
1510 * Releases the wake lock with flags to modify the release behavior.
1511 * <p>
1512 * This method releases your claim to the CPU or screen being on.
1513 * The screen may turn off shortly after you release the wake lock, or it may
1514 * not if there are other wake locks still held.
1515 * </p>
1516 *
1517 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07001518 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
1519 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001520 */
1521 public void release(int flags) {
1522 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02001523 if (mInternalCount > 0) {
1524 // internal count must only be decreased if it is > 0 or state of
1525 // the WakeLock object is broken.
1526 mInternalCount--;
1527 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001528 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
1529 mExternalCount--;
1530 }
1531 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001532 mHandler.removeCallbacks(mReleaser);
1533 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07001534 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07001535 try {
1536 mService.releaseWakeLock(mToken, flags);
1537 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001538 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001539 }
1540 mHeld = false;
1541 }
1542 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001543 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001544 throw new RuntimeException("WakeLock under-locked " + mTag);
1545 }
1546 }
1547 }
1548
1549 /**
1550 * Returns true if the wake lock has been acquired but not yet released.
1551 *
1552 * @return True if the wake lock is held.
1553 */
1554 public boolean isHeld() {
1555 synchronized (mToken) {
1556 return mHeld;
1557 }
1558 }
1559
1560 /**
1561 * Sets the work source associated with the wake lock.
1562 * <p>
1563 * The work source is used to determine on behalf of which application
1564 * the wake lock is being held. This is useful in the case where a
1565 * service is performing work on behalf of an application so that the
1566 * cost of that work can be accounted to the application.
1567 * </p>
1568 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00001569 * <p>
1570 * Make sure to follow the tag naming convention when using WorkSource
1571 * to make it easier for app developers to understand wake locks
1572 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
1573 * documentation.
1574 * </p>
1575 *
Jeff Brown1244cda2012-06-19 16:44:46 -07001576 * @param ws The work source, or null if none.
1577 */
1578 public void setWorkSource(WorkSource ws) {
1579 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00001580 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001581 ws = null;
1582 }
1583
1584 final boolean changed;
1585 if (ws == null) {
1586 changed = mWorkSource != null;
1587 mWorkSource = null;
1588 } else if (mWorkSource == null) {
1589 changed = true;
1590 mWorkSource = new WorkSource(ws);
1591 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00001592 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07001593 if (changed) {
1594 mWorkSource.set(ws);
1595 }
1596 }
1597
1598 if (changed && mHeld) {
1599 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001600 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07001601 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001602 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001603 }
1604 }
1605 }
1606 }
1607
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001608 /** @hide */
1609 public void setTag(String tag) {
1610 mTag = tag;
1611 }
1612
1613 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07001614 public String getTag() {
1615 return mTag;
1616 }
1617
1618 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001619 public void setHistoryTag(String tag) {
1620 mHistoryTag = tag;
1621 }
1622
1623 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001624 public void setUnimportantForLogging(boolean state) {
1625 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
1626 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
1627 }
1628
Jeff Brown1244cda2012-06-19 16:44:46 -07001629 @Override
1630 public String toString() {
1631 synchronized (mToken) {
1632 return "WakeLock{"
1633 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06001634 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07001635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07001637
Yi Jin148d7f42017-11-28 14:23:56 -08001638 /** @hide */
1639 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1640 synchronized (mToken) {
1641 final long token = proto.start(fieldId);
1642 proto.write(PowerManagerProto.WakeLockProto.HEX_STRING,
1643 Integer.toHexString(System.identityHashCode(this)));
1644 proto.write(PowerManagerProto.WakeLockProto.HELD, mHeld);
1645 proto.write(PowerManagerProto.WakeLockProto.INTERNAL_COUNT, mInternalCount);
1646 if (mWorkSource != null) {
1647 mWorkSource.writeToProto(proto, PowerManagerProto.WakeLockProto.WORK_SOURCE);
1648 }
1649 proto.end(token);
1650 }
1651 }
1652
Adrian Roos7445c0b2016-09-06 16:45:46 -07001653 /**
1654 * Wraps a Runnable such that this method immediately acquires the wake lock and then
1655 * once the Runnable is done the wake lock is released.
1656 *
1657 * <p>Example:
1658 *
1659 * <pre>
1660 * mHandler.post(mWakeLock.wrap(() -> {
1661 * // do things on handler, lock is held while we're waiting for this
1662 * // to get scheduled and until the runnable is done executing.
1663 * });
1664 * </pre>
1665 *
1666 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
1667 * leak the wakelock!
1668 *
1669 * @hide
1670 */
1671 public Runnable wrap(Runnable r) {
1672 acquire();
1673 return () -> {
1674 try {
1675 r.run();
1676 } finally {
1677 release();
1678 }
1679 };
1680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682}