blob: 91ddf82b54004f887cfb02ddd682cc7603c1ea91 [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 Martinez812ea752018-10-19 13:03:20 -070019import android.Manifest.permission;
Wei Wang59476652018-11-29 16:02:48 -080020import android.annotation.CallbackExecutor;
Salvador Martineza6f7b252017-04-10 10:46:15 -070021import android.annotation.IntDef;
Wei Wang59476652018-11-29 16:02:48 -080022import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070024import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Dan Gittik26b030d2018-04-16 18:50:10 +010027import android.annotation.TestApi;
Jeff Brown96307042012-07-27 15:51:34 -070028import android.content.Context;
Oleg Kibirev2385b5e2018-11-13 10:43:07 -080029import android.service.dreams.Sandman;
Wei Wang59476652018-11-29 16:02:48 -080030import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080032import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070033
Wei Wang59476652018-11-29 16:02:48 -080034import com.android.internal.util.Preconditions;
35
Salvador Martineza6f7b252017-04-10 10:46:15 -070036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Wei Wang59476652018-11-29 16:02:48 -080038import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040/**
Jeff Brown1244cda2012-06-19 16:44:46 -070041 * This class gives you control of the power state of the device.
42 *
43 * <p>
44 * <b>Device battery life will be significantly affected by the use of this API.</b>
45 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
46 * possible, and be sure to release them as soon as possible.
47 * </p><p>
Jeff Brown1244cda2012-06-19 16:44:46 -070048 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}.
49 * This will create a {@link PowerManager.WakeLock} object. You can then use methods
50 * on the wake lock object to control the power state of the device.
51 * </p><p>
52 * In practice it's quite simple:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * {@samplecode
54 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
55 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
56 * wl.acquire();
57 * ..screen will stay on during this section..
58 * wl.release();
59 * }
Jeff Brown1244cda2012-06-19 16:44:46 -070060 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070061 * The following wake lock levels are defined, with varying effects on system power.
62 * <i>These levels are mutually exclusive - you may only specify one of them.</i>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070064 * <table>
Santos Cordon3107d292016-09-20 15:50:35 -070065 * <tr><th>Flag Value</th>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070068 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070069 * <td>On*</td> <td>Off</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070071 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070072 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070073 * <td>On</td> <td>Dim</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * </tr>
75 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070076 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070077 * <td>On</td> <td>Bright</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070079 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070080 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070081 * <td>On</td> <td>Bright</td> <td>Bright</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070084 * </p><p>
85 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any
86 * display timeouts or the state of the screen and even after the user presses the power button.
87 * In all other wake locks, the CPU will run, but the user can still put the device to sleep
88 * using the power button.</i>
89 * </p><p>
90 * In addition, you can add two more flags, which affect behavior of the screen only.
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070091 * <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 -080092 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070093 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070096 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause
98 * the illumination to remain on once it turns on (e.g. from user activity). This flag
99 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is
100 * acquired. A typical use would be for notifications which are important for the user to
Santos Cordon3107d292016-09-20 15:50:35 -0700101 * see immediately.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -0700103 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700104 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is
Santos Cordon3107d292016-09-20 15:50:35 -0700106 * released, causing the illumination to remain on a bit longer. This can be used to
107 * reduce flicker if you are cycling between wake lock conditions.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700110 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -0700111 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +0000112 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700113 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600115@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -0700116public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700118
Jeff Brown155fc702012-07-27 12:12:15 -0700119 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
120 * combinations were actually supported so the bit field was removed. This explains
121 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
Bookatz1a1b0462018-01-12 11:47:03 -0800122 * value (in frameworks/base/core/proto/android/os/enums.proto) can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700126 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
127 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700128 * <p>
129 * If the user presses the power button, then the screen will be turned off
130 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -0700131 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
Bookatz1a1b0462018-01-12 11:47:03 -0800133 public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700136 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700137 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700138 * <p>
139 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
140 * implicitly released by the system, causing both the screen and the CPU to be turned off.
141 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
142 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700143 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700144 * @deprecated Most applications should use
145 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
146 * of this type of wake lock, as it will be correctly managed by the platform
147 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700149 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800150 public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700153 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700155 * <p>
156 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
157 * implicitly released by the system, causing both the screen and the CPU to be turned off.
158 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
159 * </p>
160 *
161 * @deprecated Most applications should use
162 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
163 * of this type of wake lock, as it will be correctly managed by the platform
164 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 */
Jeff Brown155fc702012-07-27 12:12:15 -0700166 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800167 public static final int SCREEN_BRIGHT_WAKE_LOCK =
168 OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a
Jeff Brown155fc702012-07-27 12:12:15 -0700169
170 /**
171 * Wake lock level: Ensures that the screen and keyboard backlight are on at
172 * full brightness.
173 * <p>
174 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
175 * implicitly released by the system, causing both the screen and the CPU to be turned off.
176 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
177 * </p>
178 *
179 * @deprecated Most applications should use
180 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
181 * of this type of wake lock, as it will be correctly managed by the platform
182 * as the user moves between applications and doesn't require a special permission.
183 */
184 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800185 public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700188 * Wake lock level: Turns the screen off when the proximity sensor activates.
189 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700190 * If the proximity sensor detects that an object is nearby, the screen turns off
191 * immediately. Shortly after the object moves away, the screen turns on again.
192 * </p><p>
193 * A proximity wake lock does not prevent the device from falling asleep
194 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
195 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
196 * wake locks are held, then the device will fall asleep (and lock) as usual.
197 * However, the device will not fall asleep while the screen has been turned off
198 * by the proximity sensor because it effectively counts as ongoing user activity.
199 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700200 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700201 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800202 * </p><p>
203 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700204 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700205 */
Bookatz1a1b0462018-01-12 11:47:03 -0800206 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK =
207 OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700208
209 /**
Jeff Brown26875502014-01-30 21:47:47 -0800210 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
211 * if no other wake locks are held.
212 * <p>
213 * This is used by the dream manager to implement doze mode. It currently
214 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700215 * </p><p>
216 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800217 * </p>
218 *
219 * {@hide}
220 */
Bookatz1a1b0462018-01-12 11:47:03 -0800221 public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040
Jeff Brown26875502014-01-30 21:47:47 -0800222
223 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800224 * Wake lock level: Keep the device awake enough to allow drawing to occur.
225 * <p>
226 * This is used by the window manager to allow applications to draw while the
227 * system is dozing. It currently has no effect unless the power manager is in
228 * the dozing state.
229 * </p><p>
230 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
231 * </p>
232 *
233 * {@hide}
234 */
Bookatz1a1b0462018-01-12 11:47:03 -0800235 public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080
Jeff Brownc2932a12014-11-20 18:04:05 -0800236
237 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700238 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500239 *
Jeff Brown155fc702012-07-27 12:12:15 -0700240 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500241 */
Jeff Brown155fc702012-07-27 12:12:15 -0700242 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500243
244 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700245 * Wake lock flag: Turn the screen on when the wake lock is acquired.
246 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700248 * the screen to remain on once it's already on. Think of the video player
249 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700251 * </p><p>
252 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
253 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
Jeff Brown155fc702012-07-27 12:12:15 -0700255 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700258 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * so the screen stays on for a little longer.
260 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700261 * Will not turn the screen on if it is not already on.
262 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
263 * </p><p>
264 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
265 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 */
Jeff Brown155fc702012-07-27 12:12:15 -0700267 public static final int ON_AFTER_RELEASE = 0x20000000;
268
269 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800270 * Wake lock flag: This wake lock is not important for logging events. If a later
271 * wake lock is acquired that is important, it will be considered the one to log.
272 * @hide
273 */
274 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
275
276 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700277 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
278 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
279 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700280 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600281 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
282
283 /**
284 * Flag for {@link WakeLock#release(int)} when called due to timeout.
285 * @hide
286 */
287 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
Jeff Brown7304c342012-05-11 18:42:42 -0700288
289 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700290 * Brightness value for fully on.
291 * @hide
292 */
293 public static final int BRIGHTNESS_ON = 255;
294
295 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700296 * Brightness value for fully off.
297 * @hide
298 */
299 public static final int BRIGHTNESS_OFF = 0;
300
Jeff Brown970d4132014-07-19 11:33:47 -0700301 /**
302 * Brightness value for default policy handling by the system.
303 * @hide
304 */
305 public static final int BRIGHTNESS_DEFAULT = -1;
306
Jeff Brownb696de52012-07-27 15:38:50 -0700307 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
308 // if adding or modifying user activity event constants.
309
310 /**
311 * User activity event type: Unspecified event type.
312 * @hide
313 */
Jeff Brown0a571122014-08-21 21:50:43 -0700314 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700315 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
316
317 /**
318 * User activity event type: Button or key pressed or released.
319 * @hide
320 */
Jeff Brown0a571122014-08-21 21:50:43 -0700321 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700322 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
323
324 /**
325 * User activity event type: Touch down, move or up.
326 * @hide
327 */
Jeff Brown0a571122014-08-21 21:50:43 -0700328 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700329 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
330
Jeff Brown96307042012-07-27 15:51:34 -0700331 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700332 * User activity event type: Accessibility taking action on behalf of user.
333 * @hide
334 */
335 @SystemApi
336 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
337
338 /**
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800339 * User activity event type: {@link android.service.attention.AttentionService} taking action
340 * on behalf of user.
341 * @hide
342 */
343 public static final int USER_ACTIVITY_EVENT_ATTENTION = 4;
344
345 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700346 * User activity flag: If already dimmed, extend the dim timeout
347 * but do not brighten. This flag is useful for keeping the screen on
348 * a little longer without causing a visible change such as when
349 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700350 * @hide
351 */
Jeff Brown0a571122014-08-21 21:50:43 -0700352 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700353 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
354
355 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700356 * User activity flag: Note the user activity as usual but do not
357 * reset the user activity timeout. This flag is useful for applying
358 * user activity power hints when interacting with the device indirectly
359 * on a secondary screen while allowing the primary screen to go to sleep.
360 * @hide
361 */
362 @SystemApi
363 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
364
365 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000366 * @hide
367 */
368 public static final int GO_TO_SLEEP_REASON_MIN = 0;
369
370 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700371 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700372 * @hide
373 */
Santos Cordon12f92eb2019-02-01 21:28:47 +0000374 public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN;
Jeff Brown96307042012-07-27 15:51:34 -0700375
376 /**
377 * Go to sleep reason code: Going to sleep due by request of the
378 * device administration policy.
379 * @hide
380 */
381 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
382
383 /**
384 * Go to sleep reason code: Going to sleep due to a screen timeout.
385 * @hide
386 */
387 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
388
Doug Zongker3b0218b2014-01-14 12:29:06 -0800389 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700390 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
391 * @hide
392 */
393 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
394
395 /**
396 * Go to sleep reason code: Going to sleep due to the power button being pressed.
397 * @hide
398 */
399 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
400
401 /**
402 * Go to sleep reason code: Going to sleep due to HDMI.
403 * @hide
404 */
405 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
406
407 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700408 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
409 * @hide
410 */
411 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
412
413 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700414 * Go to sleep reason code: Going to sleep by request of an accessibility service
415 * @hide
416 */
417 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
418
419 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000420 * Go to sleep reason code: Going to sleep due to force-suspend.
421 * @hide
422 */
423 public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8;
424
425 /**
426 * @hide
427 */
428 public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_FORCE_SUSPEND;
429
430 /**
Calin Tatarua3805722018-08-09 16:41:28 +0200431 * @hide
432 */
433 public static String sleepReasonToString(int sleepReason) {
434 switch (sleepReason) {
435 case GO_TO_SLEEP_REASON_APPLICATION: return "application";
436 case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
437 case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
438 case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
439 case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
440 case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
441 case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
442 case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
Santos Cordon12f92eb2019-02-01 21:28:47 +0000443 case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
Calin Tatarua3805722018-08-09 16:41:28 +0200444 default: return Integer.toString(sleepReason);
445 }
446 }
447
448 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700449 * Go to sleep flag: Skip dozing state and directly go to full sleep.
450 * @hide
451 */
452 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
453
454 /**
Michael Wrighte3001042019-02-05 00:13:14 +0000455 * @hide
456 */
457 @IntDef(prefix = { "WAKE_REASON_" }, value = {
458 WAKE_REASON_UNKNOWN,
459 WAKE_REASON_POWER_BUTTON,
460 WAKE_REASON_APPLICATION,
461 WAKE_REASON_PLUGGED_IN,
462 WAKE_REASON_GESTURE,
463 WAKE_REASON_CAMERA_LAUNCH,
464 WAKE_REASON_WAKE_KEY,
465 WAKE_REASON_WAKE_MOTION,
466 WAKE_REASON_HDMI,
467 })
468 @Retention(RetentionPolicy.SOURCE)
469 public @interface WakeReason{}
470
471 /**
472 * Wake up reason code: Waking for an unknown reason.
473 * @hide
474 */
475 public static final int WAKE_REASON_UNKNOWN = 0;
476
477 /**
478 * Wake up reason code: Waking up due to power button press.
479 * @hide
480 */
481 public static final int WAKE_REASON_POWER_BUTTON = 1;
482
483 /**
484 * Wake up reason code: Waking up because an application requested it.
485 * @hide
486 */
487 public static final int WAKE_REASON_APPLICATION = 2;
488
489 /**
490 * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger.
491 * @hide
492 */
493 public static final int WAKE_REASON_PLUGGED_IN = 3;
494
495 /**
496 * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the
497 * screen).
498 * @hide
499 */
500 public static final int WAKE_REASON_GESTURE = 4;
501
502 /**
503 * Wake up reason code: Waking up due to the camera being launched.
504 * @hide
505 */
506 public static final int WAKE_REASON_CAMERA_LAUNCH = 5;
507
508 /**
509 * Wake up reason code: Waking up because a wake key other than power was pressed.
510 * @hide
511 */
512 public static final int WAKE_REASON_WAKE_KEY = 6;
513
514 /**
515 * Wake up reason code: Waking up because a wake motion was performed.
516 *
517 * For example, a trackball that was set to wake the device up was spun.
518 * @hide
519 */
520 public static final int WAKE_REASON_WAKE_MOTION = 7;
521
522 /**
523 * Wake up reason code: Waking due to HDMI.
524 * @hide
525 */
526 public static final int WAKE_REASON_HDMI = 8;
527
528 /**
529 * Wake up reason code: Waking due to the lid being opened.
530 * @hide
531 */
532 public static final int WAKE_REASON_LID = 9;
533
534 /**
535 * Convert the wake reason to a string for debugging purposes.
536 * @hide
537 */
538 public static String wakeReasonToString(@WakeReason int wakeReason) {
539 switch (wakeReason) {
540 case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN";
541 case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON";
542 case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION";
543 case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN";
544 case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE";
545 case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH";
546 case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY";
547 case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION";
548 case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI";
549 case WAKE_REASON_LID: return "WAKE_REASON_LID";
550 default: return Integer.toString(wakeReason);
551 }
552 }
553
554 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800555 * The value to pass as the 'reason' argument to reboot() to reboot into
556 * recovery mode for tasks other than applying system updates, such as
557 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800558 * <p>
559 * Requires the {@link android.Manifest.permission#RECOVERY}
560 * permission (in addition to
561 * {@link android.Manifest.permission#REBOOT}).
562 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700563 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800564 */
565 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700566
567 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800568 * The value to pass as the 'reason' argument to reboot() to reboot into
569 * recovery mode for applying system updates.
570 * <p>
571 * Requires the {@link android.Manifest.permission#RECOVERY}
572 * permission (in addition to
573 * {@link android.Manifest.permission#REBOOT}).
574 * </p>
575 * @hide
576 */
577 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
578
579 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000580 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
581 * the device.
582 * @hide
583 */
584 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
585
586 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700587 * The 'reason' value used when rebooting in safe mode
588 * @hide
589 */
590 public static final String REBOOT_SAFE_MODE = "safemode";
591
592 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800593 * The 'reason' value used when rebooting the device without turning on the screen.
594 * @hide
595 */
596 public static final String REBOOT_QUIESCENT = "quiescent";
597
598 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700599 * The value to pass as the 'reason' argument to android_reboot().
600 * @hide
601 */
602 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
603
Salvador Martineza6f7b252017-04-10 10:46:15 -0700604 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700605 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
606 * is too high.
607 * @hide
608 */
609 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
610
611 /**
Wei Wangbad7c202018-11-01 11:57:39 -0700612 * The value to pass as the 'reason' argument to android_reboot() when device temperature
613 * is too high.
614 * @hide
615 */
616 public static final String SHUTDOWN_THERMAL_STATE = "thermal";
617
618 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700619 * The value to pass as the 'reason' argument to android_reboot() when device is running
620 * critically low on battery.
621 * @hide
622 */
623 public static final String SHUTDOWN_LOW_BATTERY = "battery";
624
625 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700626 * @hide
627 */
628 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700629 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700630 SHUTDOWN_REASON_UNKNOWN,
631 SHUTDOWN_REASON_SHUTDOWN,
632 SHUTDOWN_REASON_REBOOT,
633 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700634 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
635 SHUTDOWN_REASON_LOW_BATTERY,
636 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700637 })
638 public @interface ShutdownReason {}
639
640 /**
641 * constant for shutdown reason being unknown.
642 * @hide
643 */
644 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
645
646 /**
647 * constant for shutdown reason being normal shutdown.
648 * @hide
649 */
650 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
651
652 /**
653 * constant for shutdown reason being reboot.
654 * @hide
655 */
656 public static final int SHUTDOWN_REASON_REBOOT = 2;
657
658 /**
659 * constant for shutdown reason being user requested.
660 * @hide
661 */
662 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
663
664 /**
665 * constant for shutdown reason being overheating.
666 * @hide
667 */
668 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
669
Sudheer Shanka292637f2017-09-25 10:36:23 -0700670 /**
671 * constant for shutdown reason being low battery.
672 * @hide
673 */
674 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
675
676 /**
677 * constant for shutdown reason being critical battery thermal state.
678 * @hide
679 */
680 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
681
Makoto Onuki2eccd022017-11-01 13:44:23 -0700682 /**
683 * @hide
684 */
685 @Retention(RetentionPolicy.SOURCE)
Kweku Adams731a1032019-02-04 14:05:41 -0800686 @IntDef({ServiceType.LOCATION,
Makoto Onuki2eccd022017-11-01 13:44:23 -0700687 ServiceType.VIBRATION,
688 ServiceType.ANIMATION,
689 ServiceType.FULL_BACKUP,
690 ServiceType.KEYVALUE_BACKUP,
691 ServiceType.NETWORK_FIREWALL,
692 ServiceType.SCREEN_BRIGHTNESS,
693 ServiceType.SOUND,
694 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800695 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800696 ServiceType.FORCE_ALL_APPS_STANDBY,
Makoto Onukiaae89532017-11-08 14:32:03 -0800697 ServiceType.OPTIONAL_SENSORS,
Lucas Dupin92a62e52018-01-30 17:22:20 -0800698 ServiceType.AOD,
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700699 ServiceType.QUICK_DOZE,
Makoto Onukiaae89532017-11-08 14:32:03 -0800700 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700701 public @interface ServiceType {
702 int NULL = 0;
Kweku Adams731a1032019-02-04 14:05:41 -0800703 int LOCATION = 1;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700704 int VIBRATION = 2;
705 int ANIMATION = 3;
706 int FULL_BACKUP = 4;
707 int KEYVALUE_BACKUP = 5;
708 int NETWORK_FIREWALL = 6;
709 int SCREEN_BRIGHTNESS = 7;
710 int SOUND = 8;
711 int BATTERY_STATS = 9;
712 int DATA_SAVER = 10;
Lucas Dupin92a62e52018-01-30 17:22:20 -0800713 int AOD = 14;
Makoto Onukiaae89532017-11-08 14:32:03 -0800714
715 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800716 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800717 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800718 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800719
720 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800721 * Whether to enable background check on all apps or not.
722 */
723 int FORCE_BACKGROUND_CHECK = 12;
724
725 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800726 * Whether to disable non-essential sensors. (e.g. edge sensors.)
727 */
728 int OPTIONAL_SENSORS = 13;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700729
730 /**
731 * Whether to go into Deep Doze as soon as the screen turns off or not.
732 */
733 int QUICK_DOZE = 15;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800734
735 /**
736 * Whether to enable night mode when battery saver is enabled.
737 */
738 int NIGHT_MODE = 16;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700739 }
740
Makoto Onuki57f0f552017-12-11 12:22:18 -0800741 /**
742 * Either the location providers shouldn't be affected by battery saver,
743 * or battery saver is off.
744 */
745 public static final int LOCATION_MODE_NO_CHANGE = 0;
746
747 /**
748 * In this mode, the GPS based location provider should be disabled when battery saver is on and
749 * the device is non-interactive.
750 */
751 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
752
753 /**
754 * All location providers should be disabled when battery saver is on and
755 * the device is non-interactive.
756 */
757 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
758
759 /**
760 * In this mode, all the location providers will be kept available, but location fixes
761 * should only be provided to foreground apps.
762 */
763 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
764
Kweku Adams9f488e22019-01-14 16:25:08 -0800765 static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
766 static final int MAX_LOCATION_MODE = LOCATION_MODE_FOREGROUND_ONLY;
767
Makoto Onuki57f0f552017-12-11 12:22:18 -0800768 /**
769 * @hide
770 */
771 @Retention(RetentionPolicy.SOURCE)
772 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
773 LOCATION_MODE_NO_CHANGE,
774 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
775 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
776 LOCATION_MODE_FOREGROUND_ONLY,
777 })
778 public @interface LocationPowerSaveMode {}
779
Jeff Brown96307042012-07-27 15:51:34 -0700780 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700781 final IPowerManager mService;
782 final Handler mHandler;
783
Wei Wang59476652018-11-29 16:02:48 -0800784 IThermalService mThermalService;
785 private ArrayMap<ThermalStatusCallback, IThermalStatusListener> mCallbackMap = new ArrayMap<>();
786
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700787 IDeviceIdleController mIDeviceIdleController;
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700790 * {@hide}
791 */
Jeff Brown96307042012-07-27 15:51:34 -0700792 public PowerManager(Context context, IPowerManager service, Handler handler) {
793 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700794 mService = service;
795 mHandler = handler;
796 }
797
798 /**
Jeff Brown96307042012-07-27 15:51:34 -0700799 * Gets the minimum supported screen brightness setting.
800 * The screen may be allowed to become dimmer than this value but
801 * this is the minimum value that can be set by the user.
802 * @hide
803 */
804 public int getMinimumScreenBrightnessSetting() {
805 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700806 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700807 }
808
809 /**
810 * Gets the maximum supported screen brightness setting.
811 * The screen may be allowed to become dimmer than this value but
812 * this is the maximum value that can be set by the user.
813 * @hide
814 */
815 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700816 return mContext.getResources().getInteger(
817 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700818 }
819
820 /**
821 * Gets the default screen brightness setting.
822 * @hide
823 */
824 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700825 return mContext.getResources().getInteger(
826 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700827 }
828
829 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700830 * Gets the minimum supported screen brightness setting for VR Mode.
831 * @hide
832 */
833 public int getMinimumScreenBrightnessForVrSetting() {
834 return mContext.getResources().getInteger(
835 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
836 }
837
838 /**
839 * Gets the maximum supported screen brightness setting for VR Mode.
840 * The screen may be allowed to become dimmer than this value but
841 * this is the maximum value that can be set by the user.
842 * @hide
843 */
844 public int getMaximumScreenBrightnessForVrSetting() {
845 return mContext.getResources().getInteger(
846 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
847 }
848
849 /**
850 * Gets the default screen brightness for VR setting.
851 * @hide
852 */
853 public int getDefaultScreenBrightnessForVrSetting() {
854 return mContext.getResources().getInteger(
855 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
856 }
857
858 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700859 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700860 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700861 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
862 * combined using the logical OR operator.
863 * </p><p>
864 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
865 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
866 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
867 * specified as part of the {@code levelAndFlags} parameter.
868 * </p><p>
869 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
870 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
871 * {@code levelAndFlags} parameters.
872 * </p><p>
873 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
874 * wake lock, and {@link WakeLock#release release()} when you are done.
875 * </p><p>
876 * {@samplecode
877 * PowerManager pm = (PowerManager)mContext.getSystemService(
878 * Context.POWER_SERVICE);
879 * PowerManager.WakeLock wl = pm.newWakeLock(
880 * PowerManager.SCREEN_DIM_WAKE_LOCK
881 * | PowerManager.ON_AFTER_RELEASE,
882 * TAG);
883 * wl.acquire();
884 * // ... do work...
885 * wl.release();
886 * }
887 * </p><p>
888 * Although a wake lock can be created without special permissions,
889 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
890 * required to actually acquire or release the wake lock that is returned.
891 * </p><p class="note">
892 * If using this to keep the screen on, you should strongly consider using
893 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
894 * This window flag will be correctly managed by the platform
895 * as the user moves between applications and doesn't require a special permission.
896 * </p>
897 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000898 * <p>
899 * Recommended naming conventions for tags to make debugging easier:
900 * <ul>
901 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
902 * gmail:mytag) to make it easier to understand where the wake locks comes
903 * from. This namespace will also avoid collision for tags inside your app
904 * coming from different libraries which will make debugging easier.
905 * <li>use constants (e.g. do not include timestamps in the tag) to make it
906 * easier for tools to aggregate similar wake locks. When collecting
907 * debugging data, the platform only monitors a finite number of tags,
908 * using constants will help tools to provide better debugging data.
909 * <li>avoid using Class#getName() or similar method since this class name
910 * can be transformed by java optimizer and obfuscator tools.
911 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
912 * tags from the platform (e.g. *alarm*).
913 * <li>never include personnally identifiable information for privacy
914 * reasons.
915 * </ul>
916 * </p>
917 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700918 * @param levelAndFlags Combination of wake lock level and flag values defining
919 * the requested behavior of the WakeLock.
920 * @param tag Your class name (or other tag) for debugging purposes.
921 *
922 * @see WakeLock#acquire()
923 * @see WakeLock#release()
924 * @see #PARTIAL_WAKE_LOCK
925 * @see #FULL_WAKE_LOCK
926 * @see #SCREEN_DIM_WAKE_LOCK
927 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700928 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700929 * @see #ACQUIRE_CAUSES_WAKEUP
930 * @see #ON_AFTER_RELEASE
931 */
932 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700933 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700934 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700935 }
936
937 /** @hide */
938 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
939 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
940 case PARTIAL_WAKE_LOCK:
941 case SCREEN_DIM_WAKE_LOCK:
942 case SCREEN_BRIGHT_WAKE_LOCK:
943 case FULL_WAKE_LOCK:
944 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800945 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800946 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700947 break;
948 default:
949 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700950 }
951 if (tag == null) {
952 throw new IllegalArgumentException("The tag must not be null.");
953 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700954 }
955
956 /**
957 * Notifies the power manager that user activity happened.
958 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700959 * Resets the auto-off timer and brightens the screen if the device
960 * is not asleep. This is what happens normally when a key or the touch
961 * screen is pressed or when some other user activity occurs.
962 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700963 * </p><p>
964 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
965 * </p>
966 *
967 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700968 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700969 * other power management functions. It should be set
970 * to the timestamp of the input event that caused the user activity.
971 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
972 * because of this event. This is set when the power key is pressed.
973 * We want the device to stay on while the button is down, but we're about
974 * to turn off the screen so we don't want the keyboard backlight to turn on again.
975 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700976 *
977 * @see #wakeUp
978 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700979 *
980 * @removed Requires signature or system permission.
981 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700982 */
Jeff Brown7d827512014-08-21 21:56:02 -0700983 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700984 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -0700985 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
986 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
987 }
988
989 /**
990 * Notifies the power manager that user activity happened.
991 * <p>
992 * Resets the auto-off timer and brightens the screen if the device
993 * is not asleep. This is what happens normally when a key or the touch
994 * screen is pressed or when some other user activity occurs.
995 * This method does not wake up the device if it has been put to sleep.
996 * </p><p>
997 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
998 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
999 * </p>
1000 *
1001 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
1002 * time base. This timestamp is used to correctly order the user activity request with
1003 * other power management functions. It should be set
1004 * to the timestamp of the input event that caused the user activity.
1005 * @param event The user activity event.
1006 * @param flags Optional user activity flags.
1007 *
1008 * @see #wakeUp
1009 * @see #goToSleep
1010 *
1011 * @hide Requires signature or system permission.
1012 */
1013 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001014 @RequiresPermission(anyOf = {
1015 android.Manifest.permission.DEVICE_POWER,
1016 android.Manifest.permission.USER_ACTIVITY
1017 })
Jeff Brown0a571122014-08-21 21:50:43 -07001018 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001019 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001020 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -07001021 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001022 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001023 }
1024 }
1025
1026 /**
1027 * Forces the device to go to sleep.
1028 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001029 * Overrides all the wake locks that are held.
1030 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -07001031 * </p><p>
1032 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1033 * </p>
1034 *
1035 * @param time The time when the request to go to sleep was issued, in the
1036 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001037 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -07001038 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -07001039 *
1040 * @see #userActivity
1041 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -07001042 *
1043 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -07001044 */
1045 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001046 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001047 }
1048
1049 /**
Jeff Brown7d827512014-08-21 21:56:02 -07001050 * Forces the device to go to sleep.
1051 * <p>
1052 * Overrides all the wake locks that are held.
1053 * This is what happens when the power key is pressed to turn off the screen.
1054 * </p><p>
1055 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1056 * </p>
1057 *
1058 * @param time The time when the request to go to sleep was issued, in the
1059 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1060 * order the go to sleep request with other power management functions. It should be set
1061 * to the timestamp of the input event that caused the request to go to sleep.
1062 * @param reason The reason the device is going to sleep.
1063 * @param flags Optional flags to apply when going to sleep.
1064 *
1065 * @see #userActivity
1066 * @see #wakeUp
1067 *
1068 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -07001069 */
1070 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001071 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001072 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -07001073 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001074 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -07001075 }
1076 }
1077
1078 /**
1079 * Forces the device to wake up from sleep.
1080 * <p>
1081 * If the device is currently asleep, wakes it up, otherwise does nothing.
1082 * This is what happens when the power key is pressed to turn on the screen.
1083 * </p><p>
1084 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1085 * </p>
1086 *
1087 * @param time The time when the request to wake up was issued, in the
1088 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001089 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -07001090 * to the timestamp of the input event that caused the request to wake up.
1091 *
1092 * @see #userActivity
1093 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001094 *
Michael Wrighte3001042019-02-05 00:13:14 +00001095 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Jeff Brown7d827512014-08-21 21:56:02 -07001096 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -07001097 */
Michael Wrighte3001042019-02-05 00:13:14 +00001098 @Deprecated
Jeff Brown96307042012-07-27 15:51:34 -07001099 public void wakeUp(long time) {
Michael Wrighte3001042019-02-05 00:13:14 +00001100 wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp");
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001101 }
1102
1103 /**
Michael Wrighte3001042019-02-05 00:13:14 +00001104 * Forces the device to wake up from sleep.
1105 * <p>
1106 * If the device is currently asleep, wakes it up, otherwise does nothing.
1107 * This is what happens when the power key is pressed to turn on the screen.
1108 * </p><p>
1109 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1110 * </p>
1111 *
1112 * @param time The time when the request to wake up was issued, in the
1113 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1114 * order the wake up request with other power management functions. It should be set
1115 * to the timestamp of the input event that caused the request to wake up.
1116 *
1117 * @param details A free form string to explain the specific details behind the wake up for
1118 * debugging purposes.
1119 *
1120 * @see #userActivity
1121 * @see #goToSleep
1122 *
1123 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001124 * @hide
1125 */
Michael Wrighte3001042019-02-05 00:13:14 +00001126 @Deprecated
1127 public void wakeUp(long time, String details) {
1128 wakeUp(time, WAKE_REASON_UNKNOWN, details);
1129 }
1130
1131 /**
1132 * Forces the device to wake up from sleep.
1133 * <p>
1134 * If the device is currently asleep, wakes it up, otherwise does nothing.
1135 * This is what happens when the power key is pressed to turn on the screen.
1136 * </p><p>
1137 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1138 * </p>
1139 *
1140 * @param time The time when the request to wake up was issued, in the
1141 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1142 * order the wake up request with other power management functions. It should be set
1143 * to the timestamp of the input event that caused the request to wake up.
1144 *
1145 * @param reason The reason for the wake up.
1146 *
1147 * @param details A free form string to explain the specific details behind the wake up for
1148 * debugging purposes.
1149 *
1150 * @see #userActivity
1151 * @see #goToSleep
1152 * @hide
1153 */
1154 public void wakeUp(long time, @WakeReason int reason, String details) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001155 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001156 mService.wakeUp(time, reason, details, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -07001157 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001158 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001159 }
1160 }
1161
1162 /**
Jeff Brown62c82e42012-09-26 01:30:41 -07001163 * Forces the device to start napping.
1164 * <p>
1165 * If the device is currently awake, starts dreaming, otherwise does nothing.
1166 * When the dream ends or if the dream cannot be started, the device will
1167 * either wake up or go to sleep depending on whether there has been recent
1168 * user activity.
1169 * </p><p>
1170 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1171 * </p>
1172 *
1173 * @param time The time when the request to nap was issued, in the
1174 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1175 * order the nap request with other power management functions. It should be set
1176 * to the timestamp of the input event that caused the request to nap.
1177 *
1178 * @see #wakeUp
1179 * @see #goToSleep
1180 *
Jeff Brown7d827512014-08-21 21:56:02 -07001181 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -07001182 */
1183 public void nap(long time) {
1184 try {
1185 mService.nap(time);
1186 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001187 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -07001188 }
1189 }
1190
1191 /**
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001192 * Requests the device to start dreaming.
1193 * <p>
1194 * If dream can not be started, for example if another {@link PowerManager} transition is in
1195 * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when
1196 * dream ends.
1197 * </p><p>
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001198 * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and
1199 * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions.
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001200 * </p>
1201 *
1202 * @param time The time when the request to nap was issued, in the
1203 * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly
1204 * order the dream request with other power management functions. It should be set
1205 * to the timestamp of the input event that caused the request to dream.
1206 *
1207 * @hide
1208 */
1209 @SystemApi
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001210 @RequiresPermission(allOf = {
1211 android.Manifest.permission.READ_DREAM_STATE,
1212 android.Manifest.permission.WRITE_DREAM_STATE })
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001213 public void dream(long time) {
1214 Sandman.startDreamByUserRequest(mContext);
1215 }
1216
1217 /**
Jeff Browne333e672014-10-28 13:48:55 -07001218 * Boosts the brightness of the screen to maximum for a predetermined
1219 * period of time. This is used to make the screen more readable in bright
1220 * daylight for a short duration.
1221 * <p>
1222 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1223 * </p>
1224 *
1225 * @param time The time when the request to boost was issued, in the
1226 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1227 * order the boost request with other power management functions. It should be set
1228 * to the timestamp of the input event that caused the request to boost.
1229 *
1230 * @hide Requires signature permission.
1231 */
1232 public void boostScreenBrightness(long time) {
1233 try {
1234 mService.boostScreenBrightness(time);
1235 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001236 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -07001237 }
1238 }
1239
1240 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001241 * Returns whether the screen brightness is currently boosted to maximum, caused by a call
1242 * to {@link #boostScreenBrightness(long)}.
1243 * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise.
1244 *
Julius D'souzab22da802017-06-09 10:27:14 -07001245 * @deprecated This call is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001246 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001247 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001248 */
Julius D'souzab22da802017-06-09 10:27:14 -07001249 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001250 public boolean isScreenBrightnessBoosted() {
Julius D'souzab22da802017-06-09 10:27:14 -07001251 return false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001252 }
1253
Jeff Brown1244cda2012-06-19 16:44:46 -07001254 /**
Jeff Brown96307042012-07-27 15:51:34 -07001255 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001256 *
Jeff Brown96307042012-07-27 15:51:34 -07001257 * @param level The wake lock level to check.
1258 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001259 */
Jeff Brown96307042012-07-27 15:51:34 -07001260 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001261 try {
Jeff Brown96307042012-07-27 15:51:34 -07001262 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001263 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001264 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001265 }
1266 }
1267
1268 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001269 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001270 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001271 * For historical reasons, the name of this method refers to the power state of
1272 * the screen but it actually describes the overall interactive state of
1273 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001274 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001275 * The value returned by this method only indicates whether the device is
1276 * in an interactive state which may have nothing to do with the screen being
1277 * on or off. To determine the actual state of the screen,
1278 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001279 * </p>
1280 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001281 * @return True if the device is in an interactive state.
1282 *
1283 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001284 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001285 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001286 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001287 return isInteractive();
1288 }
1289
1290 /**
1291 * Returns true if the device is in an interactive state.
1292 * <p>
1293 * When this method returns true, the device is awake and ready to interact
1294 * with the user (although this is not a guarantee that the user is actively
1295 * interacting with the device just this moment). The main screen is usually
1296 * turned on while in this state. Certain features, such as the proximity
1297 * sensor, may temporarily turn off the screen while still leaving the device in an
1298 * interactive state. Note in particular that the device is still considered
1299 * to be interactive while dreaming (since dreams can be interactive) but not
1300 * when it is dozing or asleep.
1301 * </p><p>
1302 * When this method returns false, the device is dozing or asleep and must
1303 * be awoken before it will become ready to interact with the user again. The
1304 * main screen is usually turned off while in this state. Certain features,
1305 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1306 * low power state) to display system-provided content while the device dozes.
1307 * </p><p>
1308 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1309 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1310 * whenever the interactive state of the device changes. For historical reasons,
1311 * the names of these broadcasts refer to the power state of the screen
1312 * but they are actually sent in response to changes in the overall interactive
1313 * state of the device, as described by this method.
1314 * </p><p>
1315 * Services may use the non-interactive state as a hint to conserve power
1316 * since the user is not present.
1317 * </p>
1318 *
1319 * @return True if the device is in an interactive state.
1320 *
1321 * @see android.content.Intent#ACTION_SCREEN_ON
1322 * @see android.content.Intent#ACTION_SCREEN_OFF
1323 */
1324 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001325 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001326 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001327 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001328 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001329 }
1330 }
1331
1332 /**
1333 * Reboot the device. Will not return if the reboot is successful.
1334 * <p>
1335 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1336 * </p>
1337 *
1338 * @param reason code to pass to the kernel (e.g., "recovery") to
1339 * request special boot modes, or null.
1340 */
1341 public void reboot(String reason) {
1342 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001343 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001344 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001345 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001346 }
1347 }
1348
1349 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001350 * Reboot the device. Will not return if the reboot is successful.
1351 * <p>
1352 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1353 * </p>
1354 * @hide
1355 */
1356 public void rebootSafeMode() {
1357 try {
1358 mService.rebootSafeMode(false, true);
1359 } catch (RemoteException e) {
1360 throw e.rethrowFromSystemServer();
1361 }
1362 }
1363
1364 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001365 * Returns true if the device is currently in power save mode. When in this mode,
1366 * applications should reduce their functionality in order to conserve battery as
1367 * much as possible. You can monitor for changes to this state with
1368 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1369 *
1370 * @return Returns true if currently in low power mode, else false.
1371 */
1372 public boolean isPowerSaveMode() {
1373 try {
1374 return mService.isPowerSaveMode();
1375 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001376 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001377 }
1378 }
1379
1380 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001381 * Set the current power save mode.
1382 *
1383 * @return True if the set was allowed.
1384 *
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001385 * @hide
Salvador Martineza80bbab2018-09-24 10:36:11 -07001386 * @see #isPowerSaveMode()
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001387 */
Salvador Martineza80bbab2018-09-24 10:36:11 -07001388 @SystemApi
1389 @TestApi
1390 @RequiresPermission(anyOf = {
1391 android.Manifest.permission.DEVICE_POWER,
1392 android.Manifest.permission.POWER_SAVER
1393 })
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001394 public boolean setPowerSaveMode(boolean mode) {
1395 try {
1396 return mService.setPowerSaveMode(mode);
1397 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001398 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001399 }
1400 }
1401
1402 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001403 * Updates the current state of dynamic power savings and disable threshold. This is
1404 * a signal to the system which an app can update to serve as an indicator that
1405 * the user will be in a battery critical situation before being able to plug in.
1406 * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
1407 * This is a device global state, not a per user setting.
1408 *
1409 * <p>When enabled, the system may enact various measures for reducing power consumption in
1410 * order to help ensure that the user will make it to their next charging point. The most
1411 * visible of these will be the automatic enabling of battery saver if the user has set
1412 * their battery saver mode to "automatic". Note
1413 * that this is NOT simply an on/off switch for features, but rather a hint for the
1414 * system to consider enacting these power saving features, some of which have additional
1415 * logic around when to activate based on this signal.
1416 *
1417 * <p>The provided threshold is the percentage the system should consider itself safe at given
1418 * the current state of the device. The value is an integer representing a battery level.
1419 *
1420 * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
1421 * functionality so that the dynamic power savings itself remains a signal rather than becoming
1422 * an on/off switch for a subset of features.
1423 * @hide
1424 *
1425 * @param dynamicPowerSavingsEnabled A signal indicating to the system if it believes the
1426 * dynamic power savings behaviors should be activated.
1427 * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
1428 * power savings behaviors.
1429 * @return True if the update was allowed and succeeded.
1430 *
1431 * @hide
1432 */
1433 @SystemApi
1434 @TestApi
1435 @RequiresPermission(permission.POWER_SAVER)
1436 public boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled,
1437 int disableThreshold) {
1438 try {
1439 return mService.setDynamicPowerSavings(dynamicPowerSavingsEnabled, disableThreshold);
1440 } catch (RemoteException e) {
1441 throw e.rethrowFromSystemServer();
1442 }
1443 }
1444
1445 /**
Kweku Adams9f488e22019-01-14 16:25:08 -08001446 * Sets the policy for adaptive power save.
1447 *
1448 * @return true if there was an effectual change. If full battery saver is enabled or the
1449 * adaptive policy is not enabled, then this will return false.
1450 *
1451 * @hide
1452 */
1453 @SystemApi
1454 @RequiresPermission(anyOf = {
1455 android.Manifest.permission.DEVICE_POWER,
1456 android.Manifest.permission.POWER_SAVER
1457 })
1458 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
1459 try {
1460 return mService.setAdaptivePowerSavePolicy(config);
1461 } catch (RemoteException e) {
1462 throw e.rethrowFromSystemServer();
1463 }
1464 }
1465
1466 /**
1467 * Enables or disables adaptive power save.
1468 *
1469 * @return true if there was an effectual change. If full battery saver is enabled, then this
1470 * will return false.
1471 *
1472 * @hide
1473 */
1474 @SystemApi
1475 @RequiresPermission(anyOf = {
1476 android.Manifest.permission.DEVICE_POWER,
1477 android.Manifest.permission.POWER_SAVER
1478 })
1479 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
1480 try {
1481 return mService.setAdaptivePowerSaveEnabled(enabled);
1482 } catch (RemoteException e) {
1483 throw e.rethrowFromSystemServer();
1484 }
1485 }
1486
1487 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001488 * Indicates automatic battery saver toggling by the system will be based on percentage.
1489 *
1490 * @see PowerManager#getPowerSaveMode()
1491 *
1492 * @hide
1493 */
1494 @SystemApi
1495 @TestApi
1496 public static final int POWER_SAVER_MODE_PERCENTAGE = 0;
1497
1498 /**
1499 * Indicates automatic battery saver toggling by the system will be based on the state
1500 * of the dynamic power savings signal.
1501 *
1502 * @see PowerManager#setDynamicPowerSavings(boolean, int)
1503 * @see PowerManager#getPowerSaveMode()
1504 *
1505 * @hide
1506 */
1507 @SystemApi
1508 @TestApi
1509 public static final int POWER_SAVER_MODE_DYNAMIC = 1;
1510
1511 /** @hide */
1512 @Retention(RetentionPolicy.SOURCE)
1513 @IntDef(value = {
1514 POWER_SAVER_MODE_PERCENTAGE,
1515 POWER_SAVER_MODE_DYNAMIC
1516
1517 })
1518 public @interface AutoPowerSaverMode{}
1519
1520
1521 /**
1522 * Returns the current battery saver control mode. Values it may return are defined in
1523 * AutoPowerSaverMode. Note that this is a global device state, not a per user setting.
1524 *
1525 * @return The current value power saver mode for the system.
1526 *
1527 * @see AutoPowerSaverMode
1528 * @see PowerManager#getPowerSaveMode()
1529 * @hide
1530 */
1531 @AutoPowerSaverMode
1532 @SystemApi
1533 @TestApi
1534 @RequiresPermission(android.Manifest.permission.POWER_SAVER)
1535 public int getPowerSaveMode() {
1536 try {
1537 return mService.getPowerSaveMode();
1538 } catch (RemoteException e) {
1539 throw e.rethrowFromSystemServer();
1540 }
1541 }
1542
1543 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001544 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001545 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001546 * @return Battery saver state data.
1547 *
1548 * @hide
Kweku Adams7fb72a42019-01-08 16:08:49 -08001549 * @see com.android.server.power.batterysaver.BatterySaverPolicy
jackqdyulei455e90a2017-02-09 15:29:16 -08001550 * @see PowerSaveState
1551 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001552 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001553 try {
1554 return mService.getPowerSaveState(serviceType);
1555 } catch (RemoteException e) {
1556 throw e.rethrowFromSystemServer();
1557 }
1558 }
1559
1560 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001561 * Returns how location features should behave when battery saver is on. When battery saver
1562 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1563 *
1564 * <p>This API is normally only useful for components that provide location features.
1565 *
1566 * @see #isPowerSaveMode()
1567 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1568 */
1569 @LocationPowerSaveMode
1570 public int getLocationPowerSaveMode() {
Kweku Adams731a1032019-02-04 14:05:41 -08001571 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION);
Kweku Adams5e0052b2019-02-22 15:17:52 -08001572 if (!powerSaveState.batterySaverEnabled) {
Makoto Onuki57f0f552017-12-11 12:22:18 -08001573 return LOCATION_MODE_NO_CHANGE;
1574 }
Kweku Adams731a1032019-02-04 14:05:41 -08001575 return powerSaveState.locationMode;
Makoto Onuki57f0f552017-12-11 12:22:18 -08001576 }
1577
1578 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001579 * Returns true if the device is currently in idle mode. This happens when a device
1580 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1581 * it decides to go into a lower power-use state. This may involve things like turning
1582 * off network access to apps. You can monitor for changes to this state with
1583 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1584 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001585 * @return Returns true if currently in active device idle mode, else false. This is
1586 * when idle mode restrictions are being actively applied; it will return false if the
1587 * device is in a long-term idle mode but currently running a maintenance window where
1588 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001589 */
1590 public boolean isDeviceIdleMode() {
1591 try {
1592 return mService.isDeviceIdleMode();
1593 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001594 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001595 }
1596 }
1597
1598 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001599 * Returns true if the device is currently in light idle mode. This happens when a device
1600 * has had its screen off for a short time, switching it into a batching mode where we
1601 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1602 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1603 *
1604 * @return Returns true if currently in active light device idle mode, else false. This is
1605 * when light idle mode restrictions are being actively applied; it will return false if the
1606 * device is in a long-term idle mode but currently running a maintenance window where
1607 * restrictions have been lifted.
1608 * @hide
1609 */
1610 public boolean isLightDeviceIdleMode() {
1611 try {
1612 return mService.isLightDeviceIdleMode();
1613 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001614 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001615 }
1616 }
1617
1618 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001619 * Return whether the given application package name is on the device's power whitelist.
1620 * Apps can be placed on the whitelist through the settings UI invoked by
1621 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1622 */
1623 public boolean isIgnoringBatteryOptimizations(String packageName) {
1624 synchronized (this) {
1625 if (mIDeviceIdleController == null) {
1626 mIDeviceIdleController = IDeviceIdleController.Stub.asInterface(
1627 ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
1628 }
1629 }
1630 try {
1631 return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName);
1632 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001633 throw e.rethrowFromSystemServer();
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001634 }
1635 }
1636
1637 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001638 * Turn off the device.
1639 *
1640 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001641 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001642 * @param wait If true, this call waits for the shutdown to complete and does not return.
1643 *
1644 * @hide
1645 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001646 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001647 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001648 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001649 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001650 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001651 }
1652 }
1653
1654 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001655 * This function checks if the device has implemented Sustained Performance
1656 * Mode. This needs to be checked only once and is constant for a particular
1657 * device/release.
1658 *
1659 * Sustained Performance Mode is intended to provide a consistent level of
1660 * performance for prolonged amount of time.
1661 *
1662 * Applications should check if the device supports this mode, before using
1663 * {@link android.view.Window#setSustainedPerformanceMode}.
1664 *
1665 * @return Returns True if the device supports it, false otherwise.
1666 *
1667 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001668 */
1669 public boolean isSustainedPerformanceModeSupported() {
1670 return mContext.getResources().getBoolean(
1671 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1672 }
1673
1674 /**
Wei Wang59476652018-11-29 16:02:48 -08001675 * Thermal status code: Not under throttling.
1676 */
1677 public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE;
1678
1679 /**
1680 * Thermal status code: Light throttling where UX is not impacted.
1681 */
1682 public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT;
1683
1684 /**
1685 * Thermal status code: Moderate throttling where UX is not largely impacted.
1686 */
1687 public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE;
1688
1689 /**
1690 * Thermal status code: Severe throttling where UX is largely impacted.
1691 */
1692 public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE;
1693
1694 /**
1695 * Thermal status code: Platform has done everything to reduce power.
1696 */
1697 public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL;
1698
1699 /**
1700 * Thermal status code: Key components in platform are shutting down due to thermal condition.
1701 * Device functionalities will be limited.
1702 */
1703 public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY;
1704
1705 /**
1706 * Thermal status code: Need shutdown immediately.
1707 */
1708 public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN;
1709
1710 /** @hide */
1711 @IntDef(prefix = { "THERMAL_STATUS_" }, value = {
1712 THERMAL_STATUS_NONE,
1713 THERMAL_STATUS_LIGHT,
1714 THERMAL_STATUS_MODERATE,
1715 THERMAL_STATUS_SEVERE,
1716 THERMAL_STATUS_CRITICAL,
1717 THERMAL_STATUS_EMERGENCY,
1718 THERMAL_STATUS_SHUTDOWN,
1719 })
1720 @Retention(RetentionPolicy.SOURCE)
1721 public @interface ThermalStatus {}
1722
1723 /**
1724 * This function returns the current thermal status of the device.
1725 *
1726 * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under
1727 * thermal throttling.
1728 */
1729 public @ThermalStatus int getCurrentThermalStatus() {
1730 synchronized (this) {
1731 if (mThermalService == null) {
1732 mThermalService = IThermalService.Stub.asInterface(
1733 ServiceManager.getService(Context.THERMAL_SERVICE));
1734 }
1735 try {
1736 return mThermalService.getCurrentThermalStatus();
1737 } catch (RemoteException e) {
1738 throw e.rethrowFromSystemServer();
1739 }
1740 }
1741
1742 }
1743
1744 /**
1745 * Callback passed to
1746 * {@link PowerManager#registerThermalStatusCallback} and
1747 * {@link PowerManager#unregisterThermalStatusCallback}
1748 * to notify caller of thermal status.
1749 */
1750 public abstract static class ThermalStatusCallback {
1751
1752 /**
1753 * Called when overall thermal throttling status changed.
1754 * @param status defined in {@link android.os.Temperature}.
1755 */
1756 public void onStatusChange(@ThermalStatus int status) {}
1757 }
1758
1759 /**
1760 * This function registers a callback for thermal status change.
1761 *
1762 * @param callback callback to be registered.
1763 * @param executor {@link Executor} to handle the callbacks.
1764 */
1765 public void registerThermalStatusCallback(
1766 @NonNull ThermalStatusCallback callback, @NonNull @CallbackExecutor Executor executor) {
1767 Preconditions.checkNotNull(callback, "callback cannnot be null");
1768 Preconditions.checkNotNull(executor, "executor cannnot be null");
1769 synchronized (this) {
1770 if (mThermalService == null) {
1771 mThermalService = IThermalService.Stub.asInterface(
1772 ServiceManager.getService(Context.THERMAL_SERVICE));
1773 }
1774 try {
1775 if (mCallbackMap.containsKey(callback)) {
1776 throw new IllegalArgumentException("ThermalStatusCallback already registered");
1777 }
1778 IThermalStatusListener listener = new IThermalStatusListener.Stub() {
1779 @Override
1780 public void onStatusChange(int status) {
1781 executor.execute(() -> {
1782 callback.onStatusChange(status);
1783 });
1784 }
1785 };
1786 if (mThermalService.registerThermalStatusListener(listener)) {
1787 mCallbackMap.put(callback, listener);
1788 } else {
1789 throw new RuntimeException("ThermalStatusCallback failed to register");
1790 }
1791 } catch (RemoteException e) {
1792 throw e.rethrowFromSystemServer();
1793 }
1794 }
1795 }
1796
1797 /**
1798 * This function unregisters a callback for thermal status change.
1799 *
1800 * @param callback to be unregistered.
1801 *
1802 * see {@link #registerThermalStatusCallback}
1803 */
Wei Wang3dcd3f62019-02-13 15:46:31 -08001804 public void unregisterThermalStatusCallback(@NonNull ThermalStatusCallback callback) {
Wei Wang59476652018-11-29 16:02:48 -08001805 Preconditions.checkNotNull(callback, "callback cannnot be null");
1806 synchronized (this) {
1807 if (mThermalService == null) {
1808 mThermalService = IThermalService.Stub.asInterface(
1809 ServiceManager.getService(Context.THERMAL_SERVICE));
1810 }
1811 try {
1812 IThermalStatusListener listener = mCallbackMap.get(callback);
1813 if (listener == null) {
1814 throw new IllegalArgumentException("ThermalStatusCallback not registered");
1815 }
1816 if (mThermalService.unregisterThermalStatusListener(listener)) {
1817 mCallbackMap.remove(callback);
1818 } else {
1819 throw new RuntimeException("ThermalStatusCallback failed to unregister");
1820 }
1821 } catch (RemoteException e) {
1822 throw e.rethrowFromSystemServer();
1823 }
1824 }
1825 }
1826
1827 /**
Lucas Dupin16cfe452018-02-08 13:14:50 -08001828 * If true, the doze component is not started until after the screen has been
1829 * turned off and the screen off animation has been performed.
1830 * @hide
1831 */
1832 public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) {
1833 try {
1834 mService.setDozeAfterScreenOff(dozeAfterScreenOf);
1835 } catch (RemoteException e) {
1836 throw e.rethrowFromSystemServer();
1837 }
1838 }
1839
1840 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001841 * Returns the reason the phone was last shutdown. Calling app must have the
1842 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1843 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1844 * not be accessed.
1845 * @hide
1846 */
1847 @ShutdownReason
1848 public int getLastShutdownReason() {
1849 try {
1850 return mService.getLastShutdownReason();
1851 } catch (RemoteException e) {
1852 throw e.rethrowFromSystemServer();
1853 }
1854 }
1855
1856 /**
Calin Tatarua3805722018-08-09 16:41:28 +02001857 * Returns the reason the device last went to sleep (i.e. the last value of
1858 * the second argument of {@link #goToSleep(long, int, int) goToSleep}).
1859 *
1860 * @return One of the {@code GO_TO_SLEEP_REASON_*} constants.
1861 *
1862 * @hide
1863 */
1864 public int getLastSleepReason() {
1865 try {
1866 return mService.getLastSleepReason();
1867 } catch (RemoteException e) {
1868 throw e.rethrowFromSystemServer();
1869 }
1870 }
1871
1872 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +00001873 * Forces the device to go to suspend, even if there are currently wakelocks being held.
1874 * <b>Caution</b>
1875 * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts
1876 * of the system will not be notified and will not have an opportunity to save state prior to
1877 * the device going to suspend.
1878 * This method should only be used in very rare circumstances where the device is intended
1879 * to appear as completely off to the user and they have a well understood, reliable way of
1880 * re-enabling it.
1881 * </p><p>
1882 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1883 * </p>
1884 *
1885 * @return true on success, false otherwise.
1886 * @hide
1887 */
1888 @SystemApi
1889 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
1890 public boolean forceSuspend() {
1891 try {
1892 return mService.forceSuspend();
1893 } catch (RemoteException e) {
1894 throw e.rethrowFromSystemServer();
1895 }
1896 }
1897
1898 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001899 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1900 * This broadcast is only sent to registered receivers.
1901 */
1902 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1903 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1904 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1905
1906 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001907 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1908 * @hide
1909 */
1910 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1911 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1912 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1913
1914 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001915 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1916 * This broadcast is only sent to registered receivers.
1917 */
1918 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1919 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1920 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1921
1922 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001923 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1924 * This broadcast is only sent to registered receivers.
1925 * @hide
1926 */
1927 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1928 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1929 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1930
1931 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001932 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1933 * This broadcast is only sent to registered receivers.
1934 */
1935 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1936 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1937 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1938
1939 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001940 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1941 * This broadcast is only sent to registered receivers.
1942 */
1943 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1944 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1945 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1946
1947 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001948 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1949 * This broadcast is only sent to registered receivers.
1950 *
1951 * @hide
1952 */
1953 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1954 public static final String ACTION_POWER_SAVE_MODE_CHANGING
1955 = "android.os.action.POWER_SAVE_MODE_CHANGING";
1956
1957 /** @hide */
1958 public static final String EXTRA_POWER_SAVE_MODE = "mode";
1959
1960 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001961 * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed.
1962 * This broadcast is only sent to registered receivers.
1963 *
Julius D'souzab22da802017-06-09 10:27:14 -07001964 * @deprecated This intent is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001965 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001966 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001967 **/
Julius D'souzab22da802017-06-09 10:27:14 -07001968 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001969 public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED
1970 = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED";
1971
1972 /**
Denny cy Leec5a7c292019-01-01 17:37:55 +08001973 * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
1974 * @hide
1975 */
1976 public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0;
1977
1978 /**
1979 * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode
1980 * longer).
1981 * @hide
1982 */
1983 public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1;
1984
1985 /**
1986 * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly)
1987 * @hide
1988 */
1989 public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;
1990
1991 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07001992 * A wake lock is a mechanism to indicate that your application needs
1993 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07001994 * <p>
1995 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00001996 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07001997 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
1998 * </p><p>
1999 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
2000 * on at the level that was requested when the wake lock was created.
2001 * </p><p>
2002 * Call {@link #release()} when you are done and don't need the lock anymore.
2003 * It is very important to do this as soon as possible to avoid running down the
2004 * device's battery excessively.
2005 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 */
Jeff Brown1244cda2012-06-19 16:44:46 -07002007 public final class WakeLock {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002008 private int mFlags;
2009 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002010 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07002011 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002012 private int mInternalCount;
2013 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07002014 private boolean mRefCounted = true;
2015 private boolean mHeld;
2016 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002017 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07002018 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019
Jeff Brown1244cda2012-06-19 16:44:46 -07002020 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002022 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025
Dianne Hackborn713df152013-05-17 11:27:57 -07002026 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 mFlags = flags;
2028 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002029 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07002031 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
2033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07002035 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 synchronized (mToken) {
2037 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08002038 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07002039 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002041 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002043 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
2046 }
2047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048
Jeff Brown1244cda2012-06-19 16:44:46 -07002049 /**
2050 * Sets whether this WakeLock is reference counted.
2051 * <p>
2052 * Wake locks are reference counted by default. If a wake lock is
2053 * reference counted, then each call to {@link #acquire()} must be
2054 * balanced by an equal number of calls to {@link #release()}. If a wake
2055 * lock is not reference counted, then one call to {@link #release()} is
2056 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
2057 * </p>
2058 *
2059 * @param value True to make the wake lock reference counted, false to
2060 * make the wake lock non-reference counted.
2061 */
2062 public void setReferenceCounted(boolean value) {
2063 synchronized (mToken) {
2064 mRefCounted = value;
2065 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04002066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067
Jeff Brown1244cda2012-06-19 16:44:46 -07002068 /**
2069 * Acquires the wake lock.
2070 * <p>
2071 * Ensures that the device is on at the level requested when
2072 * the wake lock was created.
2073 * </p>
2074 */
2075 public void acquire() {
2076 synchronized (mToken) {
2077 acquireLocked();
2078 }
2079 }
2080
2081 /**
2082 * Acquires the wake lock with a timeout.
2083 * <p>
2084 * Ensures that the device is on at the level requested when
2085 * the wake lock was created. The lock will be released after the given timeout
2086 * expires.
2087 * </p>
2088 *
2089 * @param timeout The timeout after which to release the wake lock, in milliseconds.
2090 */
2091 public void acquire(long timeout) {
2092 synchronized (mToken) {
2093 acquireLocked();
2094 mHandler.postDelayed(mReleaser, timeout);
2095 }
2096 }
2097
2098 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002099 mInternalCount++;
2100 mExternalCount++;
2101 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07002102 // Do this even if the wake lock is already thought to be held (mHeld == true)
2103 // because non-reference counted wake locks are not always properly released.
2104 // For example, the keyguard's wake lock might be forcibly released by the
2105 // power manager without the keyguard knowing. A subsequent call to acquire
2106 // should immediately acquire the wake lock once again despite never having
2107 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07002108 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07002109 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07002110 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002111 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
2112 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07002113 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002114 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002115 }
Jeff Brownff1baef2012-07-19 15:01:17 -07002116 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07002117 }
2118 }
2119
2120 /**
2121 * Releases the wake lock.
2122 * <p>
2123 * This method releases your claim to the CPU or screen being on.
2124 * The screen may turn off shortly after you release the wake lock, or it may
2125 * not if there are other wake locks still held.
2126 * </p>
2127 */
2128 public void release() {
2129 release(0);
2130 }
2131
2132 /**
2133 * Releases the wake lock with flags to modify the release behavior.
2134 * <p>
2135 * This method releases your claim to the CPU or screen being on.
2136 * The screen may turn off shortly after you release the wake lock, or it may
2137 * not if there are other wake locks still held.
2138 * </p>
2139 *
2140 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07002141 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
2142 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07002143 */
2144 public void release(int flags) {
2145 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02002146 if (mInternalCount > 0) {
2147 // internal count must only be decreased if it is > 0 or state of
2148 // the WakeLock object is broken.
2149 mInternalCount--;
2150 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002151 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
2152 mExternalCount--;
2153 }
2154 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002155 mHandler.removeCallbacks(mReleaser);
2156 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07002157 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07002158 try {
2159 mService.releaseWakeLock(mToken, flags);
2160 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002161 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002162 }
2163 mHeld = false;
2164 }
2165 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002166 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002167 throw new RuntimeException("WakeLock under-locked " + mTag);
2168 }
2169 }
2170 }
2171
2172 /**
2173 * Returns true if the wake lock has been acquired but not yet released.
2174 *
2175 * @return True if the wake lock is held.
2176 */
2177 public boolean isHeld() {
2178 synchronized (mToken) {
2179 return mHeld;
2180 }
2181 }
2182
2183 /**
2184 * Sets the work source associated with the wake lock.
2185 * <p>
2186 * The work source is used to determine on behalf of which application
2187 * the wake lock is being held. This is useful in the case where a
2188 * service is performing work on behalf of an application so that the
2189 * cost of that work can be accounted to the application.
2190 * </p>
2191 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00002192 * <p>
2193 * Make sure to follow the tag naming convention when using WorkSource
2194 * to make it easier for app developers to understand wake locks
2195 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
2196 * documentation.
2197 * </p>
2198 *
Jeff Brown1244cda2012-06-19 16:44:46 -07002199 * @param ws The work source, or null if none.
2200 */
2201 public void setWorkSource(WorkSource ws) {
2202 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00002203 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002204 ws = null;
2205 }
2206
2207 final boolean changed;
2208 if (ws == null) {
2209 changed = mWorkSource != null;
2210 mWorkSource = null;
2211 } else if (mWorkSource == null) {
2212 changed = true;
2213 mWorkSource = new WorkSource(ws);
2214 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00002215 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07002216 if (changed) {
2217 mWorkSource.set(ws);
2218 }
2219 }
2220
2221 if (changed && mHeld) {
2222 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07002223 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07002224 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002225 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002226 }
2227 }
2228 }
2229 }
2230
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002231 /** @hide */
2232 public void setTag(String tag) {
2233 mTag = tag;
2234 }
2235
2236 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07002237 public String getTag() {
2238 return mTag;
2239 }
2240
2241 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002242 public void setHistoryTag(String tag) {
2243 mHistoryTag = tag;
2244 }
2245
2246 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002247 public void setUnimportantForLogging(boolean state) {
2248 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
2249 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
2250 }
2251
Jeff Brown1244cda2012-06-19 16:44:46 -07002252 @Override
2253 public String toString() {
2254 synchronized (mToken) {
2255 return "WakeLock{"
2256 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002257 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07002258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07002260
Yi Jin148d7f42017-11-28 14:23:56 -08002261 /** @hide */
2262 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2263 synchronized (mToken) {
2264 final long token = proto.start(fieldId);
Yi Jin163967f2018-03-15 13:49:44 -07002265 proto.write(PowerManagerProto.WakeLock.TAG, mTag);
2266 proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName);
2267 proto.write(PowerManagerProto.WakeLock.HELD, mHeld);
2268 proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount);
Yi Jin148d7f42017-11-28 14:23:56 -08002269 if (mWorkSource != null) {
Yi Jin163967f2018-03-15 13:49:44 -07002270 mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE);
Yi Jin148d7f42017-11-28 14:23:56 -08002271 }
2272 proto.end(token);
2273 }
2274 }
2275
Adrian Roos7445c0b2016-09-06 16:45:46 -07002276 /**
2277 * Wraps a Runnable such that this method immediately acquires the wake lock and then
2278 * once the Runnable is done the wake lock is released.
2279 *
2280 * <p>Example:
2281 *
2282 * <pre>
2283 * mHandler.post(mWakeLock.wrap(() -> {
2284 * // do things on handler, lock is held while we're waiting for this
2285 * // to get scheduled and until the runnable is done executing.
2286 * });
2287 * </pre>
2288 *
2289 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
2290 * leak the wakelock!
2291 *
2292 * @hide
2293 */
2294 public Runnable wrap(Runnable r) {
2295 acquire();
2296 return () -> {
2297 try {
2298 r.run();
2299 } finally {
2300 release();
2301 }
2302 };
2303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305}