blob: 82b04a661b5485a803cb30406e6ec42d25091991 [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;
Nikita Ioffe717dbbf2019-12-18 13:45:15 +000023import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070025import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070026import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.SystemService;
Dan Gittik26b030d2018-04-16 18:50:10 +010028import android.annotation.TestApi;
Artur Satayevafdb23a2019-12-10 17:47:53 +000029import android.compat.annotation.UnsupportedAppUsage;
Jeff Brown96307042012-07-27 15:51:34 -070030import android.content.Context;
Oleg Kibirev2385b5e2018-11-13 10:43:07 -080031import android.service.dreams.Sandman;
Wei Wang59476652018-11-29 16:02:48 -080032import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080034import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070035
Wei Wang59476652018-11-29 16:02:48 -080036import com.android.internal.util.Preconditions;
37
Salvador Martineza6f7b252017-04-10 10:46:15 -070038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Wei Wang59476652018-11-29 16:02:48 -080040import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
Jeff Brown1244cda2012-06-19 16:44:46 -070043 * This class gives you control of the power state of the device.
44 *
45 * <p>
46 * <b>Device battery life will be significantly affected by the use of this API.</b>
47 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
Andrew Solovay3fea8d12019-12-05 13:05:38 -080048 * possible, and be sure to release them as soon as possible. In most cases,
49 * you'll want to use
50 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070052 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -070053 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +000054 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -070055 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060057@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -070058public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -070060
Jeff Brown155fc702012-07-27 12:12:15 -070061 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
62 * combinations were actually supported so the bit field was removed. This explains
63 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
Bookatz1a1b0462018-01-12 11:47:03 -080064 * value (in frameworks/base/core/proto/android/os/enums.proto) can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67 /**
Jeff Brown1244cda2012-06-19 16:44:46 -070068 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
69 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070070 * <p>
71 * If the user presses the power button, then the screen will be turned off
72 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -070073 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 */
Bookatz1a1b0462018-01-12 11:47:03 -080075 public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77 /**
Jeff Brown155fc702012-07-27 12:12:15 -070078 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -070079 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070080 * <p>
81 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
82 * implicitly released by the system, causing both the screen and the CPU to be turned off.
83 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
84 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -070085 *
Dianne Hackborn9567a662011-04-19 18:44:03 -070086 * @deprecated Most applications should use
87 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
88 * of this type of wake lock, as it will be correctly managed by the platform
89 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Dianne Hackborn9567a662011-04-19 18:44:03 -070091 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -080092 public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 /**
Jeff Brown155fc702012-07-27 12:12:15 -070095 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070097 * <p>
98 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
99 * implicitly released by the system, causing both the screen and the CPU to be turned off.
100 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
101 * </p>
102 *
103 * @deprecated Most applications should use
104 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
105 * of this type of wake lock, as it will be correctly managed by the platform
106 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
Jeff Brown155fc702012-07-27 12:12:15 -0700108 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800109 public static final int SCREEN_BRIGHT_WAKE_LOCK =
110 OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a
Jeff Brown155fc702012-07-27 12:12:15 -0700111
112 /**
113 * Wake lock level: Ensures that the screen and keyboard backlight are on at
114 * full brightness.
115 * <p>
116 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
117 * implicitly released by the system, causing both the screen and the CPU to be turned off.
118 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
119 * </p>
120 *
121 * @deprecated Most applications should use
122 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
123 * of this type of wake lock, as it will be correctly managed by the platform
124 * as the user moves between applications and doesn't require a special permission.
125 */
126 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800127 public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700130 * Wake lock level: Turns the screen off when the proximity sensor activates.
131 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700132 * If the proximity sensor detects that an object is nearby, the screen turns off
133 * immediately. Shortly after the object moves away, the screen turns on again.
134 * </p><p>
135 * A proximity wake lock does not prevent the device from falling asleep
136 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
137 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
138 * wake locks are held, then the device will fall asleep (and lock) as usual.
139 * However, the device will not fall asleep while the screen has been turned off
140 * by the proximity sensor because it effectively counts as ongoing user activity.
141 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700142 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700143 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800144 * </p><p>
145 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700146 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700147 */
Bookatz1a1b0462018-01-12 11:47:03 -0800148 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK =
149 OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700150
151 /**
Jeff Brown26875502014-01-30 21:47:47 -0800152 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
153 * if no other wake locks are held.
154 * <p>
155 * This is used by the dream manager to implement doze mode. It currently
156 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700157 * </p><p>
158 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800159 * </p>
160 *
161 * {@hide}
162 */
Bookatz1a1b0462018-01-12 11:47:03 -0800163 public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040
Jeff Brown26875502014-01-30 21:47:47 -0800164
165 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800166 * Wake lock level: Keep the device awake enough to allow drawing to occur.
167 * <p>
168 * This is used by the window manager to allow applications to draw while the
169 * system is dozing. It currently has no effect unless the power manager is in
170 * the dozing state.
171 * </p><p>
172 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
173 * </p>
174 *
175 * {@hide}
176 */
Bookatz1a1b0462018-01-12 11:47:03 -0800177 public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080
Jeff Brownc2932a12014-11-20 18:04:05 -0800178
179 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700180 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500181 *
Jeff Brown155fc702012-07-27 12:12:15 -0700182 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500183 */
Jeff Brown155fc702012-07-27 12:12:15 -0700184 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500185
186 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700187 * Wake lock flag: Turn the screen on when the wake lock is acquired.
188 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700190 * the screen to remain on once it's already on. Think of the video player
191 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700193 * </p><p>
194 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
195 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
Jeff Brown155fc702012-07-27 12:12:15 -0700197 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700200 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * so the screen stays on for a little longer.
202 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700203 * Will not turn the screen on if it is not already on.
204 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
205 * </p><p>
206 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
207 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 */
Jeff Brown155fc702012-07-27 12:12:15 -0700209 public static final int ON_AFTER_RELEASE = 0x20000000;
210
211 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800212 * Wake lock flag: This wake lock is not important for logging events. If a later
213 * wake lock is acquired that is important, it will be considered the one to log.
214 * @hide
215 */
216 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
217
218 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700219 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
220 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
221 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700222 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600223 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
224
225 /**
226 * Flag for {@link WakeLock#release(int)} when called due to timeout.
227 * @hide
228 */
229 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
Jeff Brown7304c342012-05-11 18:42:42 -0700230
231 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700232 * Brightness value for fully on.
233 * @hide
234 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000235 @UnsupportedAppUsage
Jeff Brown7304c342012-05-11 18:42:42 -0700236 public static final int BRIGHTNESS_ON = 255;
237
238 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700239 * Brightness value for fully off.
240 * @hide
241 */
242 public static final int BRIGHTNESS_OFF = 0;
243
Jeff Brown970d4132014-07-19 11:33:47 -0700244 /**
245 * Brightness value for default policy handling by the system.
246 * @hide
247 */
248 public static final int BRIGHTNESS_DEFAULT = -1;
249
Jeff Brownb696de52012-07-27 15:38:50 -0700250 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
251 // if adding or modifying user activity event constants.
252
253 /**
254 * User activity event type: Unspecified event type.
255 * @hide
256 */
Jeff Brown0a571122014-08-21 21:50:43 -0700257 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700258 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
259
260 /**
261 * User activity event type: Button or key pressed or released.
262 * @hide
263 */
Jeff Brown0a571122014-08-21 21:50:43 -0700264 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700265 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
266
267 /**
268 * User activity event type: Touch down, move or up.
269 * @hide
270 */
Jeff Brown0a571122014-08-21 21:50:43 -0700271 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700272 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
273
Jeff Brown96307042012-07-27 15:51:34 -0700274 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700275 * User activity event type: Accessibility taking action on behalf of user.
276 * @hide
277 */
278 @SystemApi
279 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
280
281 /**
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800282 * User activity event type: {@link android.service.attention.AttentionService} taking action
283 * on behalf of user.
284 * @hide
285 */
286 public static final int USER_ACTIVITY_EVENT_ATTENTION = 4;
287
288 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700289 * User activity flag: If already dimmed, extend the dim timeout
290 * but do not brighten. This flag is useful for keeping the screen on
291 * a little longer without causing a visible change such as when
292 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700293 * @hide
294 */
Jeff Brown0a571122014-08-21 21:50:43 -0700295 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700296 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
297
298 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700299 * User activity flag: Note the user activity as usual but do not
300 * reset the user activity timeout. This flag is useful for applying
301 * user activity power hints when interacting with the device indirectly
302 * on a secondary screen while allowing the primary screen to go to sleep.
303 * @hide
304 */
305 @SystemApi
306 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
307
308 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000309 * @hide
310 */
311 public static final int GO_TO_SLEEP_REASON_MIN = 0;
312
313 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700314 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700315 * @hide
316 */
Santos Cordon12f92eb2019-02-01 21:28:47 +0000317 public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN;
Jeff Brown96307042012-07-27 15:51:34 -0700318
319 /**
320 * Go to sleep reason code: Going to sleep due by request of the
321 * device administration policy.
322 * @hide
323 */
324 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
325
326 /**
327 * Go to sleep reason code: Going to sleep due to a screen timeout.
328 * @hide
329 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000330 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700331 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
332
Doug Zongker3b0218b2014-01-14 12:29:06 -0800333 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700334 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
335 * @hide
336 */
337 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
338
339 /**
340 * Go to sleep reason code: Going to sleep due to the power button being pressed.
341 * @hide
342 */
343 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
344
345 /**
346 * Go to sleep reason code: Going to sleep due to HDMI.
347 * @hide
348 */
349 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
350
351 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700352 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
353 * @hide
354 */
355 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
356
357 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700358 * Go to sleep reason code: Going to sleep by request of an accessibility service
359 * @hide
360 */
361 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
362
363 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000364 * Go to sleep reason code: Going to sleep due to force-suspend.
365 * @hide
366 */
367 public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8;
368
369 /**
Robert Horvath5560f382019-07-10 10:46:38 +0200370 * Go to sleep reason code: Going to sleep due to user inattentiveness.
Santos Cordon12f92eb2019-02-01 21:28:47 +0000371 * @hide
372 */
Robert Horvath5560f382019-07-10 10:46:38 +0200373 public static final int GO_TO_SLEEP_REASON_INATTENTIVE = 9;
374
375 /**
376 * @hide
377 */
378 public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_INATTENTIVE;
Santos Cordon12f92eb2019-02-01 21:28:47 +0000379
380 /**
Calin Tatarua3805722018-08-09 16:41:28 +0200381 * @hide
382 */
383 public static String sleepReasonToString(int sleepReason) {
384 switch (sleepReason) {
385 case GO_TO_SLEEP_REASON_APPLICATION: return "application";
386 case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
387 case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
388 case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
389 case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
390 case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
391 case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
392 case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
Santos Cordon12f92eb2019-02-01 21:28:47 +0000393 case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
Robert Horvath5560f382019-07-10 10:46:38 +0200394 case GO_TO_SLEEP_REASON_INATTENTIVE: return "inattentive";
Calin Tatarua3805722018-08-09 16:41:28 +0200395 default: return Integer.toString(sleepReason);
396 }
397 }
398
399 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700400 * Go to sleep flag: Skip dozing state and directly go to full sleep.
401 * @hide
402 */
403 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
404
405 /**
Michael Wrighte3001042019-02-05 00:13:14 +0000406 * @hide
407 */
408 @IntDef(prefix = { "WAKE_REASON_" }, value = {
409 WAKE_REASON_UNKNOWN,
410 WAKE_REASON_POWER_BUTTON,
411 WAKE_REASON_APPLICATION,
412 WAKE_REASON_PLUGGED_IN,
413 WAKE_REASON_GESTURE,
414 WAKE_REASON_CAMERA_LAUNCH,
415 WAKE_REASON_WAKE_KEY,
416 WAKE_REASON_WAKE_MOTION,
417 WAKE_REASON_HDMI,
418 })
419 @Retention(RetentionPolicy.SOURCE)
420 public @interface WakeReason{}
421
422 /**
423 * Wake up reason code: Waking for an unknown reason.
424 * @hide
425 */
426 public static final int WAKE_REASON_UNKNOWN = 0;
427
428 /**
429 * Wake up reason code: Waking up due to power button press.
430 * @hide
431 */
432 public static final int WAKE_REASON_POWER_BUTTON = 1;
433
434 /**
435 * Wake up reason code: Waking up because an application requested it.
436 * @hide
437 */
438 public static final int WAKE_REASON_APPLICATION = 2;
439
440 /**
441 * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger.
442 * @hide
443 */
444 public static final int WAKE_REASON_PLUGGED_IN = 3;
445
446 /**
447 * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the
448 * screen).
449 * @hide
450 */
451 public static final int WAKE_REASON_GESTURE = 4;
452
453 /**
454 * Wake up reason code: Waking up due to the camera being launched.
455 * @hide
456 */
457 public static final int WAKE_REASON_CAMERA_LAUNCH = 5;
458
459 /**
460 * Wake up reason code: Waking up because a wake key other than power was pressed.
461 * @hide
462 */
463 public static final int WAKE_REASON_WAKE_KEY = 6;
464
465 /**
466 * Wake up reason code: Waking up because a wake motion was performed.
467 *
468 * For example, a trackball that was set to wake the device up was spun.
469 * @hide
470 */
471 public static final int WAKE_REASON_WAKE_MOTION = 7;
472
473 /**
474 * Wake up reason code: Waking due to HDMI.
475 * @hide
476 */
477 public static final int WAKE_REASON_HDMI = 8;
478
479 /**
480 * Wake up reason code: Waking due to the lid being opened.
481 * @hide
482 */
483 public static final int WAKE_REASON_LID = 9;
484
485 /**
486 * Convert the wake reason to a string for debugging purposes.
487 * @hide
488 */
489 public static String wakeReasonToString(@WakeReason int wakeReason) {
490 switch (wakeReason) {
491 case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN";
492 case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON";
493 case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION";
494 case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN";
495 case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE";
496 case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH";
497 case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY";
498 case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION";
499 case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI";
500 case WAKE_REASON_LID: return "WAKE_REASON_LID";
501 default: return Integer.toString(wakeReason);
502 }
503 }
504
505 /**
Santos Cordon623526b2019-04-09 17:02:38 +0100506 * @hide
507 */
508 public static class WakeData {
509 public WakeData(long wakeTime, @WakeReason int wakeReason) {
510 this.wakeTime = wakeTime;
511 this.wakeReason = wakeReason;
512 }
513 public long wakeTime;
514 public @WakeReason int wakeReason;
515 }
516
517 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800518 * The value to pass as the 'reason' argument to reboot() to reboot into
519 * recovery mode for tasks other than applying system updates, such as
520 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800521 * <p>
522 * Requires the {@link android.Manifest.permission#RECOVERY}
523 * permission (in addition to
524 * {@link android.Manifest.permission#REBOOT}).
525 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700526 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800527 */
528 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700529
530 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800531 * The value to pass as the 'reason' argument to reboot() to reboot into
532 * recovery mode for applying system updates.
533 * <p>
534 * Requires the {@link android.Manifest.permission#RECOVERY}
535 * permission (in addition to
536 * {@link android.Manifest.permission#REBOOT}).
537 * </p>
538 * @hide
539 */
540 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
541
542 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000543 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
544 * the device.
545 * @hide
546 */
547 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
548
549 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700550 * The 'reason' value used when rebooting in safe mode
551 * @hide
552 */
553 public static final String REBOOT_SAFE_MODE = "safemode";
554
555 /**
Nikita Ioffe717dbbf2019-12-18 13:45:15 +0000556 * The 'reason' value used for rebooting userspace.
557 * @hide
558 */
559 @SystemApi
560 public static final String REBOOT_USERSPACE = "userspace";
561
562 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800563 * The 'reason' value used when rebooting the device without turning on the screen.
564 * @hide
565 */
566 public static final String REBOOT_QUIESCENT = "quiescent";
567
568 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700569 * The value to pass as the 'reason' argument to android_reboot().
570 * @hide
571 */
572 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
573
Salvador Martineza6f7b252017-04-10 10:46:15 -0700574 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700575 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
576 * is too high.
577 * @hide
578 */
579 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
580
581 /**
Wei Wangbad7c202018-11-01 11:57:39 -0700582 * The value to pass as the 'reason' argument to android_reboot() when device temperature
583 * is too high.
584 * @hide
585 */
586 public static final String SHUTDOWN_THERMAL_STATE = "thermal";
587
588 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700589 * The value to pass as the 'reason' argument to android_reboot() when device is running
590 * critically low on battery.
591 * @hide
592 */
593 public static final String SHUTDOWN_LOW_BATTERY = "battery";
594
595 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700596 * @hide
597 */
598 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700599 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700600 SHUTDOWN_REASON_UNKNOWN,
601 SHUTDOWN_REASON_SHUTDOWN,
602 SHUTDOWN_REASON_REBOOT,
603 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700604 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
605 SHUTDOWN_REASON_LOW_BATTERY,
606 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700607 })
608 public @interface ShutdownReason {}
609
610 /**
611 * constant for shutdown reason being unknown.
612 * @hide
613 */
614 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
615
616 /**
617 * constant for shutdown reason being normal shutdown.
618 * @hide
619 */
620 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
621
622 /**
623 * constant for shutdown reason being reboot.
624 * @hide
625 */
626 public static final int SHUTDOWN_REASON_REBOOT = 2;
627
628 /**
629 * constant for shutdown reason being user requested.
630 * @hide
631 */
632 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
633
634 /**
635 * constant for shutdown reason being overheating.
636 * @hide
637 */
638 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
639
Sudheer Shanka292637f2017-09-25 10:36:23 -0700640 /**
641 * constant for shutdown reason being low battery.
642 * @hide
643 */
644 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
645
646 /**
647 * constant for shutdown reason being critical battery thermal state.
648 * @hide
649 */
650 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
651
Makoto Onuki2eccd022017-11-01 13:44:23 -0700652 /**
653 * @hide
654 */
655 @Retention(RetentionPolicy.SOURCE)
Kweku Adams731a1032019-02-04 14:05:41 -0800656 @IntDef({ServiceType.LOCATION,
Makoto Onuki2eccd022017-11-01 13:44:23 -0700657 ServiceType.VIBRATION,
658 ServiceType.ANIMATION,
659 ServiceType.FULL_BACKUP,
660 ServiceType.KEYVALUE_BACKUP,
661 ServiceType.NETWORK_FIREWALL,
662 ServiceType.SCREEN_BRIGHTNESS,
663 ServiceType.SOUND,
664 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800665 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800666 ServiceType.FORCE_ALL_APPS_STANDBY,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800667 ServiceType.FORCE_BACKGROUND_CHECK,
Makoto Onukiaae89532017-11-08 14:32:03 -0800668 ServiceType.OPTIONAL_SENSORS,
Lucas Dupin92a62e52018-01-30 17:22:20 -0800669 ServiceType.AOD,
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700670 ServiceType.QUICK_DOZE,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800671 ServiceType.NIGHT_MODE,
Makoto Onukiaae89532017-11-08 14:32:03 -0800672 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700673 public @interface ServiceType {
674 int NULL = 0;
Kweku Adams731a1032019-02-04 14:05:41 -0800675 int LOCATION = 1;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700676 int VIBRATION = 2;
677 int ANIMATION = 3;
678 int FULL_BACKUP = 4;
679 int KEYVALUE_BACKUP = 5;
680 int NETWORK_FIREWALL = 6;
681 int SCREEN_BRIGHTNESS = 7;
682 int SOUND = 8;
683 int BATTERY_STATS = 9;
684 int DATA_SAVER = 10;
Lucas Dupin92a62e52018-01-30 17:22:20 -0800685 int AOD = 14;
Makoto Onukiaae89532017-11-08 14:32:03 -0800686
687 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800688 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800689 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800690 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800691
692 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800693 * Whether to enable background check on all apps or not.
694 */
695 int FORCE_BACKGROUND_CHECK = 12;
696
697 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800698 * Whether to disable non-essential sensors. (e.g. edge sensors.)
699 */
700 int OPTIONAL_SENSORS = 13;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700701
702 /**
703 * Whether to go into Deep Doze as soon as the screen turns off or not.
704 */
705 int QUICK_DOZE = 15;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800706
707 /**
708 * Whether to enable night mode when battery saver is enabled.
709 */
710 int NIGHT_MODE = 16;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700711 }
712
Makoto Onuki57f0f552017-12-11 12:22:18 -0800713 /**
714 * Either the location providers shouldn't be affected by battery saver,
715 * or battery saver is off.
716 */
717 public static final int LOCATION_MODE_NO_CHANGE = 0;
718
719 /**
720 * In this mode, the GPS based location provider should be disabled when battery saver is on and
721 * the device is non-interactive.
722 */
723 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
724
725 /**
726 * All location providers should be disabled when battery saver is on and
727 * the device is non-interactive.
728 */
729 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
730
731 /**
732 * In this mode, all the location providers will be kept available, but location fixes
733 * should only be provided to foreground apps.
734 */
735 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
736
Kweku Adams4fb074e2019-02-01 16:03:27 -0800737 /**
738 * In this mode, location will not be turned off, but LocationManager will throttle all
739 * requests to providers when the device is non-interactive.
740 */
741 public static final int LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF = 4;
742
Kweku Adamsc1d844a2019-03-28 16:05:00 -0700743 /** @hide */
744 public static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
745 /** @hide */
746 public static final int MAX_LOCATION_MODE = LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF;
Kweku Adams9f488e22019-01-14 16:25:08 -0800747
Makoto Onuki57f0f552017-12-11 12:22:18 -0800748 /**
749 * @hide
750 */
751 @Retention(RetentionPolicy.SOURCE)
752 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
753 LOCATION_MODE_NO_CHANGE,
754 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
755 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
756 LOCATION_MODE_FOREGROUND_ONLY,
Kweku Adams4fb074e2019-02-01 16:03:27 -0800757 LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF,
Makoto Onuki57f0f552017-12-11 12:22:18 -0800758 })
759 public @interface LocationPowerSaveMode {}
760
Kweku Adams4fb074e2019-02-01 16:03:27 -0800761 /** @hide */
762 public static String locationPowerSaveModeToString(@LocationPowerSaveMode int mode) {
763 switch (mode) {
764 case LOCATION_MODE_NO_CHANGE:
765 return "NO_CHANGE";
766 case LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF:
767 return "GPS_DISABLED_WHEN_SCREEN_OFF";
768 case LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF:
769 return "ALL_DISABLED_WHEN_SCREEN_OFF";
770 case LOCATION_MODE_FOREGROUND_ONLY:
771 return "FOREGROUND_ONLY";
772 case LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF:
773 return "THROTTLE_REQUESTS_WHEN_SCREEN_OFF";
774 default:
775 return Integer.toString(mode);
776 }
777 }
778
Jeff Brown96307042012-07-27 15:51:34 -0700779 final Context mContext;
Andrei Onea24ec3212019-03-15 17:35:05 +0000780 @UnsupportedAppUsage
Jeff Brown1244cda2012-06-19 16:44:46 -0700781 final IPowerManager mService;
Artur Satayev70507ed2019-07-29 13:18:27 +0100782 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Brown1244cda2012-06-19 16:44:46 -0700783 final Handler mHandler;
784
Makoto Onuki152742f2019-10-31 17:22:26 -0700785 /** We lazily initialize it.*/
786 private DeviceIdleManager mDeviceIdleManager;
787
Wei Wang59476652018-11-29 16:02:48 -0800788 IThermalService mThermalService;
Wei Wang7a49ff62019-03-04 21:13:32 -0800789 private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener>
790 mListenerMap = new ArrayMap<>();
Wei Wang59476652018-11-29 16:02:48 -0800791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700793 * {@hide}
794 */
Jeff Brown96307042012-07-27 15:51:34 -0700795 public PowerManager(Context context, IPowerManager service, Handler handler) {
796 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700797 mService = service;
798 mHandler = handler;
799 }
800
Makoto Onuki152742f2019-10-31 17:22:26 -0700801 private DeviceIdleManager getDeviceIdleManager() {
802 if (mDeviceIdleManager == null) {
803 // No need for synchronization; getSystemService() will return the same object anyway.
804 mDeviceIdleManager = mContext.getSystemService(DeviceIdleManager.class);
805 }
806 return mDeviceIdleManager;
807 }
808
Jeff Brown1244cda2012-06-19 16:44:46 -0700809 /**
Jeff Brown96307042012-07-27 15:51:34 -0700810 * Gets the minimum supported screen brightness setting.
811 * The screen may be allowed to become dimmer than this value but
812 * this is the minimum value that can be set by the user.
813 * @hide
814 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000815 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700816 public int getMinimumScreenBrightnessSetting() {
817 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700818 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700819 }
820
821 /**
822 * Gets the maximum supported screen brightness setting.
823 * The screen may be allowed to become dimmer than this value but
824 * this is the maximum value that can be set by the user.
825 * @hide
826 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000827 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700828 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700829 return mContext.getResources().getInteger(
830 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700831 }
832
833 /**
834 * Gets the default screen brightness setting.
835 * @hide
836 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000837 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700838 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700839 return mContext.getResources().getInteger(
840 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700841 }
842
843 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700844 * Gets the minimum supported screen brightness setting for VR Mode.
845 * @hide
846 */
847 public int getMinimumScreenBrightnessForVrSetting() {
848 return mContext.getResources().getInteger(
849 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
850 }
851
852 /**
853 * Gets the maximum supported screen brightness setting for VR Mode.
854 * The screen may be allowed to become dimmer than this value but
855 * this is the maximum value that can be set by the user.
856 * @hide
857 */
858 public int getMaximumScreenBrightnessForVrSetting() {
859 return mContext.getResources().getInteger(
860 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
861 }
862
863 /**
864 * Gets the default screen brightness for VR setting.
865 * @hide
866 */
867 public int getDefaultScreenBrightnessForVrSetting() {
868 return mContext.getResources().getInteger(
869 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
870 }
871
872 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700873 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700874 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700875 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
876 * combined using the logical OR operator.
877 * </p><p>
878 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
879 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
880 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
881 * specified as part of the {@code levelAndFlags} parameter.
Andrew Solovay3fea8d12019-12-05 13:05:38 -0800882 * </p>
883 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700884 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
885 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
886 * {@code levelAndFlags} parameters.
887 * </p><p>
888 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
889 * wake lock, and {@link WakeLock#release release()} when you are done.
890 * </p><p>
891 * {@samplecode
892 * PowerManager pm = (PowerManager)mContext.getSystemService(
893 * Context.POWER_SERVICE);
894 * PowerManager.WakeLock wl = pm.newWakeLock(
895 * PowerManager.SCREEN_DIM_WAKE_LOCK
896 * | PowerManager.ON_AFTER_RELEASE,
897 * TAG);
898 * wl.acquire();
899 * // ... do work...
900 * wl.release();
901 * }
902 * </p><p>
903 * Although a wake lock can be created without special permissions,
904 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
905 * required to actually acquire or release the wake lock that is returned.
906 * </p><p class="note">
907 * If using this to keep the screen on, you should strongly consider using
908 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
909 * This window flag will be correctly managed by the platform
910 * as the user moves between applications and doesn't require a special permission.
911 * </p>
912 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000913 * <p>
914 * Recommended naming conventions for tags to make debugging easier:
915 * <ul>
916 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
917 * gmail:mytag) to make it easier to understand where the wake locks comes
918 * from. This namespace will also avoid collision for tags inside your app
919 * coming from different libraries which will make debugging easier.
920 * <li>use constants (e.g. do not include timestamps in the tag) to make it
921 * easier for tools to aggregate similar wake locks. When collecting
922 * debugging data, the platform only monitors a finite number of tags,
923 * using constants will help tools to provide better debugging data.
924 * <li>avoid using Class#getName() or similar method since this class name
925 * can be transformed by java optimizer and obfuscator tools.
926 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
927 * tags from the platform (e.g. *alarm*).
928 * <li>never include personnally identifiable information for privacy
929 * reasons.
930 * </ul>
931 * </p>
932 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700933 * @param levelAndFlags Combination of wake lock level and flag values defining
934 * the requested behavior of the WakeLock.
935 * @param tag Your class name (or other tag) for debugging purposes.
936 *
937 * @see WakeLock#acquire()
938 * @see WakeLock#release()
939 * @see #PARTIAL_WAKE_LOCK
940 * @see #FULL_WAKE_LOCK
941 * @see #SCREEN_DIM_WAKE_LOCK
942 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700943 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700944 * @see #ACQUIRE_CAUSES_WAKEUP
945 * @see #ON_AFTER_RELEASE
946 */
947 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700948 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700949 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700950 }
951
952 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000953 @UnsupportedAppUsage
Jeff Brown155fc702012-07-27 12:12:15 -0700954 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
955 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
956 case PARTIAL_WAKE_LOCK:
957 case SCREEN_DIM_WAKE_LOCK:
958 case SCREEN_BRIGHT_WAKE_LOCK:
959 case FULL_WAKE_LOCK:
960 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800961 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800962 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700963 break;
964 default:
965 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700966 }
967 if (tag == null) {
968 throw new IllegalArgumentException("The tag must not be null.");
969 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700970 }
971
972 /**
973 * Notifies the power manager that user activity happened.
974 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700975 * Resets the auto-off timer and brightens the screen if the device
976 * is not asleep. This is what happens normally when a key or the touch
977 * screen is pressed or when some other user activity occurs.
978 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700979 * </p><p>
980 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
981 * </p>
982 *
983 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700984 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700985 * other power management functions. It should be set
986 * to the timestamp of the input event that caused the user activity.
987 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
988 * because of this event. This is set when the power key is pressed.
989 * We want the device to stay on while the button is down, but we're about
990 * to turn off the screen so we don't want the keyboard backlight to turn on again.
991 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700992 *
993 * @see #wakeUp
994 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700995 *
996 * @removed Requires signature or system permission.
997 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700998 */
Jeff Brown7d827512014-08-21 21:56:02 -0700999 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001000 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -07001001 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
1002 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
1003 }
1004
1005 /**
1006 * Notifies the power manager that user activity happened.
1007 * <p>
1008 * Resets the auto-off timer and brightens the screen if the device
1009 * is not asleep. This is what happens normally when a key or the touch
1010 * screen is pressed or when some other user activity occurs.
1011 * This method does not wake up the device if it has been put to sleep.
1012 * </p><p>
1013 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
1014 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
1015 * </p>
1016 *
1017 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
1018 * time base. This timestamp is used to correctly order the user activity request with
1019 * other power management functions. It should be set
1020 * to the timestamp of the input event that caused the user activity.
1021 * @param event The user activity event.
1022 * @param flags Optional user activity flags.
1023 *
1024 * @see #wakeUp
1025 * @see #goToSleep
1026 *
1027 * @hide Requires signature or system permission.
1028 */
1029 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001030 @RequiresPermission(anyOf = {
1031 android.Manifest.permission.DEVICE_POWER,
1032 android.Manifest.permission.USER_ACTIVITY
1033 })
Jeff Brown0a571122014-08-21 21:50:43 -07001034 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001035 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001036 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -07001037 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001038 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001039 }
1040 }
1041
1042 /**
1043 * Forces the device to go to sleep.
1044 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001045 * Overrides all the wake locks that are held.
1046 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -07001047 * </p><p>
1048 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1049 * </p>
1050 *
1051 * @param time The time when the request to go to sleep was issued, in the
1052 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001053 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -07001054 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -07001055 *
1056 * @see #userActivity
1057 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -07001058 *
1059 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -07001060 */
1061 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001062 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001063 }
1064
1065 /**
Jeff Brown7d827512014-08-21 21:56:02 -07001066 * Forces the device to go to sleep.
1067 * <p>
1068 * Overrides all the wake locks that are held.
1069 * This is what happens when the power key is pressed to turn off the screen.
1070 * </p><p>
1071 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1072 * </p>
1073 *
1074 * @param time The time when the request to go to sleep was issued, in the
1075 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1076 * order the go to sleep request with other power management functions. It should be set
1077 * to the timestamp of the input event that caused the request to go to sleep.
1078 * @param reason The reason the device is going to sleep.
1079 * @param flags Optional flags to apply when going to sleep.
1080 *
1081 * @see #userActivity
1082 * @see #wakeUp
1083 *
1084 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -07001085 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001086 @UnsupportedAppUsage
Jeff Brown6d8fd272014-05-20 21:24:38 -07001087 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001088 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001089 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -07001090 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001091 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -07001092 }
1093 }
1094
1095 /**
1096 * Forces the device to wake up from sleep.
1097 * <p>
1098 * If the device is currently asleep, wakes it up, otherwise does nothing.
1099 * This is what happens when the power key is pressed to turn on the screen.
1100 * </p><p>
1101 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1102 * </p>
1103 *
1104 * @param time The time when the request to wake up was issued, in the
1105 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001106 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -07001107 * to the timestamp of the input event that caused the request to wake up.
1108 *
1109 * @see #userActivity
1110 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001111 *
Michael Wrighte3001042019-02-05 00:13:14 +00001112 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Jeff Brown7d827512014-08-21 21:56:02 -07001113 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -07001114 */
Michael Wrighte3001042019-02-05 00:13:14 +00001115 @Deprecated
Jeff Brown96307042012-07-27 15:51:34 -07001116 public void wakeUp(long time) {
Michael Wrighte3001042019-02-05 00:13:14 +00001117 wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp");
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001118 }
1119
1120 /**
Michael Wrighte3001042019-02-05 00:13:14 +00001121 * Forces the device to wake up from sleep.
1122 * <p>
1123 * If the device is currently asleep, wakes it up, otherwise does nothing.
1124 * This is what happens when the power key is pressed to turn on the screen.
1125 * </p><p>
1126 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1127 * </p>
1128 *
1129 * @param time The time when the request to wake up was issued, in the
1130 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1131 * order the wake up request with other power management functions. It should be set
1132 * to the timestamp of the input event that caused the request to wake up.
1133 *
1134 * @param details A free form string to explain the specific details behind the wake up for
1135 * debugging purposes.
1136 *
1137 * @see #userActivity
1138 * @see #goToSleep
1139 *
1140 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001141 * @hide
1142 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001143 @UnsupportedAppUsage
Michael Wrighte3001042019-02-05 00:13:14 +00001144 @Deprecated
1145 public void wakeUp(long time, String details) {
1146 wakeUp(time, WAKE_REASON_UNKNOWN, details);
1147 }
1148
1149 /**
1150 * Forces the device to wake up from sleep.
1151 * <p>
1152 * If the device is currently asleep, wakes it up, otherwise does nothing.
1153 * This is what happens when the power key is pressed to turn on the screen.
1154 * </p><p>
1155 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1156 * </p>
1157 *
1158 * @param time The time when the request to wake up was issued, in the
1159 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1160 * order the wake up request with other power management functions. It should be set
1161 * to the timestamp of the input event that caused the request to wake up.
1162 *
1163 * @param reason The reason for the wake up.
1164 *
1165 * @param details A free form string to explain the specific details behind the wake up for
1166 * debugging purposes.
1167 *
1168 * @see #userActivity
1169 * @see #goToSleep
1170 * @hide
1171 */
1172 public void wakeUp(long time, @WakeReason int reason, String details) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001173 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001174 mService.wakeUp(time, reason, details, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -07001175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001176 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001177 }
1178 }
1179
1180 /**
Jeff Brown62c82e42012-09-26 01:30:41 -07001181 * Forces the device to start napping.
1182 * <p>
1183 * If the device is currently awake, starts dreaming, otherwise does nothing.
1184 * When the dream ends or if the dream cannot be started, the device will
1185 * either wake up or go to sleep depending on whether there has been recent
1186 * user activity.
1187 * </p><p>
1188 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1189 * </p>
1190 *
1191 * @param time The time when the request to nap was issued, in the
1192 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1193 * order the nap request with other power management functions. It should be set
1194 * to the timestamp of the input event that caused the request to nap.
1195 *
1196 * @see #wakeUp
1197 * @see #goToSleep
1198 *
Jeff Brown7d827512014-08-21 21:56:02 -07001199 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -07001200 */
1201 public void nap(long time) {
1202 try {
1203 mService.nap(time);
1204 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001205 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -07001206 }
1207 }
1208
1209 /**
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001210 * Requests the device to start dreaming.
1211 * <p>
1212 * If dream can not be started, for example if another {@link PowerManager} transition is in
1213 * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when
1214 * dream ends.
1215 * </p><p>
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001216 * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and
1217 * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions.
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001218 * </p>
1219 *
1220 * @param time The time when the request to nap was issued, in the
1221 * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly
1222 * order the dream request with other power management functions. It should be set
1223 * to the timestamp of the input event that caused the request to dream.
1224 *
1225 * @hide
1226 */
1227 @SystemApi
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001228 @RequiresPermission(allOf = {
1229 android.Manifest.permission.READ_DREAM_STATE,
1230 android.Manifest.permission.WRITE_DREAM_STATE })
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001231 public void dream(long time) {
1232 Sandman.startDreamByUserRequest(mContext);
1233 }
1234
1235 /**
Jeff Browne333e672014-10-28 13:48:55 -07001236 * Boosts the brightness of the screen to maximum for a predetermined
1237 * period of time. This is used to make the screen more readable in bright
1238 * daylight for a short duration.
1239 * <p>
1240 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1241 * </p>
1242 *
1243 * @param time The time when the request to boost was issued, in the
1244 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1245 * order the boost request with other power management functions. It should be set
1246 * to the timestamp of the input event that caused the request to boost.
1247 *
1248 * @hide Requires signature permission.
1249 */
1250 public void boostScreenBrightness(long time) {
1251 try {
1252 mService.boostScreenBrightness(time);
1253 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001254 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -07001255 }
1256 }
1257
Jeff Brown1244cda2012-06-19 16:44:46 -07001258 /**
Jeff Brown96307042012-07-27 15:51:34 -07001259 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001260 *
Jeff Brown96307042012-07-27 15:51:34 -07001261 * @param level The wake lock level to check.
1262 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001263 */
Jeff Brown96307042012-07-27 15:51:34 -07001264 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001265 try {
Jeff Brown96307042012-07-27 15:51:34 -07001266 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001267 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001268 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001269 }
1270 }
1271
1272 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001273 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001274 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001275 * For historical reasons, the name of this method refers to the power state of
1276 * the screen but it actually describes the overall interactive state of
1277 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001278 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001279 * The value returned by this method only indicates whether the device is
1280 * in an interactive state which may have nothing to do with the screen being
1281 * on or off. To determine the actual state of the screen,
1282 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001283 * </p>
1284 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001285 * @return True if the device is in an interactive state.
1286 *
1287 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001288 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001289 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001290 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001291 return isInteractive();
1292 }
1293
1294 /**
1295 * Returns true if the device is in an interactive state.
1296 * <p>
1297 * When this method returns true, the device is awake and ready to interact
1298 * with the user (although this is not a guarantee that the user is actively
1299 * interacting with the device just this moment). The main screen is usually
1300 * turned on while in this state. Certain features, such as the proximity
1301 * sensor, may temporarily turn off the screen while still leaving the device in an
1302 * interactive state. Note in particular that the device is still considered
1303 * to be interactive while dreaming (since dreams can be interactive) but not
1304 * when it is dozing or asleep.
1305 * </p><p>
1306 * When this method returns false, the device is dozing or asleep and must
1307 * be awoken before it will become ready to interact with the user again. The
1308 * main screen is usually turned off while in this state. Certain features,
1309 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1310 * low power state) to display system-provided content while the device dozes.
1311 * </p><p>
1312 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1313 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1314 * whenever the interactive state of the device changes. For historical reasons,
1315 * the names of these broadcasts refer to the power state of the screen
1316 * but they are actually sent in response to changes in the overall interactive
1317 * state of the device, as described by this method.
1318 * </p><p>
1319 * Services may use the non-interactive state as a hint to conserve power
1320 * since the user is not present.
1321 * </p>
1322 *
1323 * @return True if the device is in an interactive state.
1324 *
1325 * @see android.content.Intent#ACTION_SCREEN_ON
1326 * @see android.content.Intent#ACTION_SCREEN_OFF
1327 */
1328 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001329 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001330 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001331 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001332 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001333 }
1334 }
1335
1336 /**
Nikita Ioffe717dbbf2019-12-18 13:45:15 +00001337 * Returns {@code true} if this device supports rebooting userspace.
1338 */
1339 // TODO(b/138605180): add link to documentation once it's ready.
1340 public boolean isRebootingUserspaceSupported() {
1341 return SystemProperties.getBoolean("ro.init.userspace_reboot.is_supported", false);
1342 }
1343
1344 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07001345 * Reboot the device. Will not return if the reboot is successful.
1346 * <p>
1347 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1348 * </p>
1349 *
1350 * @param reason code to pass to the kernel (e.g., "recovery") to
1351 * request special boot modes, or null.
Nikita Ioffe717dbbf2019-12-18 13:45:15 +00001352 * @throws UnsupportedOperationException if userspace reboot was requested on a device that
1353 * doesn't support it.
Jeff Brown1244cda2012-06-19 16:44:46 -07001354 */
Nikita Ioffe717dbbf2019-12-18 13:45:15 +00001355 public void reboot(@Nullable String reason) {
1356 if (REBOOT_USERSPACE.equals(reason) && !isRebootingUserspaceSupported()) {
1357 throw new UnsupportedOperationException(
1358 "Attempted userspace reboot on a device that doesn't support it");
1359 }
Jeff Brown1244cda2012-06-19 16:44:46 -07001360 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001361 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001362 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001363 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001364 }
1365 }
1366
1367 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001368 * Reboot the device. Will not return if the reboot is successful.
1369 * <p>
1370 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1371 * </p>
1372 * @hide
1373 */
1374 public void rebootSafeMode() {
1375 try {
1376 mService.rebootSafeMode(false, true);
1377 } catch (RemoteException e) {
1378 throw e.rethrowFromSystemServer();
1379 }
1380 }
1381
1382 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001383 * Returns true if the device is currently in power save mode. When in this mode,
1384 * applications should reduce their functionality in order to conserve battery as
1385 * much as possible. You can monitor for changes to this state with
1386 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1387 *
1388 * @return Returns true if currently in low power mode, else false.
1389 */
1390 public boolean isPowerSaveMode() {
1391 try {
1392 return mService.isPowerSaveMode();
1393 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001394 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001395 }
1396 }
1397
1398 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001399 * Set the current power save mode.
1400 *
1401 * @return True if the set was allowed.
1402 *
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001403 * @hide
Salvador Martineza80bbab2018-09-24 10:36:11 -07001404 * @see #isPowerSaveMode()
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001405 */
Salvador Martineza80bbab2018-09-24 10:36:11 -07001406 @SystemApi
1407 @TestApi
1408 @RequiresPermission(anyOf = {
1409 android.Manifest.permission.DEVICE_POWER,
1410 android.Manifest.permission.POWER_SAVER
1411 })
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001412 public boolean setPowerSaveModeEnabled(boolean mode) {
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001413 try {
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001414 return mService.setPowerSaveModeEnabled(mode);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001415 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001416 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001417 }
1418 }
1419
1420 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001421 * Updates the current state of dynamic power savings and disable threshold. This is
1422 * a signal to the system which an app can update to serve as an indicator that
1423 * the user will be in a battery critical situation before being able to plug in.
1424 * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
1425 * This is a device global state, not a per user setting.
1426 *
1427 * <p>When enabled, the system may enact various measures for reducing power consumption in
1428 * order to help ensure that the user will make it to their next charging point. The most
1429 * visible of these will be the automatic enabling of battery saver if the user has set
1430 * their battery saver mode to "automatic". Note
1431 * that this is NOT simply an on/off switch for features, but rather a hint for the
1432 * system to consider enacting these power saving features, some of which have additional
1433 * logic around when to activate based on this signal.
1434 *
1435 * <p>The provided threshold is the percentage the system should consider itself safe at given
1436 * the current state of the device. The value is an integer representing a battery level.
1437 *
1438 * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
1439 * functionality so that the dynamic power savings itself remains a signal rather than becoming
1440 * an on/off switch for a subset of features.
1441 * @hide
1442 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001443 * @param powerSaveHint A signal indicating to the system if it believes the
Salvador Martinez812ea752018-10-19 13:03:20 -07001444 * dynamic power savings behaviors should be activated.
1445 * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
1446 * power savings behaviors.
1447 * @return True if the update was allowed and succeeded.
1448 *
1449 * @hide
1450 */
1451 @SystemApi
1452 @TestApi
1453 @RequiresPermission(permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001454 public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) {
Salvador Martinez812ea752018-10-19 13:03:20 -07001455 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001456 return mService.setDynamicPowerSaveHint(powerSaveHint, disableThreshold);
Salvador Martinez812ea752018-10-19 13:03:20 -07001457 } catch (RemoteException e) {
1458 throw e.rethrowFromSystemServer();
1459 }
1460 }
1461
1462 /**
Kweku Adams9f488e22019-01-14 16:25:08 -08001463 * Sets the policy for adaptive power save.
1464 *
1465 * @return true if there was an effectual change. If full battery saver is enabled or the
1466 * adaptive policy is not enabled, then this will return false.
1467 *
1468 * @hide
1469 */
1470 @SystemApi
1471 @RequiresPermission(anyOf = {
1472 android.Manifest.permission.DEVICE_POWER,
1473 android.Manifest.permission.POWER_SAVER
1474 })
1475 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
1476 try {
1477 return mService.setAdaptivePowerSavePolicy(config);
1478 } catch (RemoteException e) {
1479 throw e.rethrowFromSystemServer();
1480 }
1481 }
1482
1483 /**
1484 * Enables or disables adaptive power save.
1485 *
1486 * @return true if there was an effectual change. If full battery saver is enabled, then this
1487 * will return false.
1488 *
1489 * @hide
1490 */
1491 @SystemApi
1492 @RequiresPermission(anyOf = {
1493 android.Manifest.permission.DEVICE_POWER,
1494 android.Manifest.permission.POWER_SAVER
1495 })
1496 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
1497 try {
1498 return mService.setAdaptivePowerSaveEnabled(enabled);
1499 } catch (RemoteException e) {
1500 throw e.rethrowFromSystemServer();
1501 }
1502 }
1503
1504 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001505 * Indicates automatic battery saver toggling by the system will be based on percentage.
1506 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001507 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001508 *
1509 * @hide
1510 */
1511 @SystemApi
1512 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001513 public static final int POWER_SAVE_MODE_TRIGGER_PERCENTAGE = 0;
Salvador Martinez812ea752018-10-19 13:03:20 -07001514
1515 /**
1516 * Indicates automatic battery saver toggling by the system will be based on the state
1517 * of the dynamic power savings signal.
1518 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001519 * @see PowerManager#setDynamicPowerSaveHint(boolean, int)
1520 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001521 *
1522 * @hide
1523 */
1524 @SystemApi
1525 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001526 public static final int POWER_SAVE_MODE_TRIGGER_DYNAMIC = 1;
Salvador Martinez812ea752018-10-19 13:03:20 -07001527
1528 /** @hide */
1529 @Retention(RetentionPolicy.SOURCE)
1530 @IntDef(value = {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001531 POWER_SAVE_MODE_TRIGGER_PERCENTAGE,
1532 POWER_SAVE_MODE_TRIGGER_DYNAMIC
Salvador Martinez812ea752018-10-19 13:03:20 -07001533
1534 })
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001535 public @interface AutoPowerSaveModeTriggers {}
Salvador Martinez812ea752018-10-19 13:03:20 -07001536
1537
1538 /**
1539 * Returns the current battery saver control mode. Values it may return are defined in
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001540 * AutoPowerSaveModeTriggers. Note that this is a global device state, not a per user setting.
Salvador Martinez812ea752018-10-19 13:03:20 -07001541 *
1542 * @return The current value power saver mode for the system.
1543 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001544 * @see AutoPowerSaveModeTriggers
1545 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001546 * @hide
1547 */
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001548 @AutoPowerSaveModeTriggers
Salvador Martinez812ea752018-10-19 13:03:20 -07001549 @SystemApi
1550 @TestApi
1551 @RequiresPermission(android.Manifest.permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001552 public int getPowerSaveModeTrigger() {
Salvador Martinez812ea752018-10-19 13:03:20 -07001553 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001554 return mService.getPowerSaveModeTrigger();
Salvador Martinez812ea752018-10-19 13:03:20 -07001555 } catch (RemoteException e) {
1556 throw e.rethrowFromSystemServer();
1557 }
1558 }
1559
1560 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001561 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001562 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001563 * @return Battery saver state data.
1564 *
1565 * @hide
Kweku Adams7fb72a42019-01-08 16:08:49 -08001566 * @see com.android.server.power.batterysaver.BatterySaverPolicy
jackqdyulei455e90a2017-02-09 15:29:16 -08001567 * @see PowerSaveState
1568 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001569 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001570 try {
1571 return mService.getPowerSaveState(serviceType);
1572 } catch (RemoteException e) {
1573 throw e.rethrowFromSystemServer();
1574 }
1575 }
1576
1577 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001578 * Returns how location features should behave when battery saver is on. When battery saver
1579 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1580 *
1581 * <p>This API is normally only useful for components that provide location features.
1582 *
1583 * @see #isPowerSaveMode()
1584 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1585 */
1586 @LocationPowerSaveMode
1587 public int getLocationPowerSaveMode() {
Kweku Adams731a1032019-02-04 14:05:41 -08001588 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION);
Kweku Adams5e0052b2019-02-22 15:17:52 -08001589 if (!powerSaveState.batterySaverEnabled) {
Makoto Onuki57f0f552017-12-11 12:22:18 -08001590 return LOCATION_MODE_NO_CHANGE;
1591 }
Kweku Adams731a1032019-02-04 14:05:41 -08001592 return powerSaveState.locationMode;
Makoto Onuki57f0f552017-12-11 12:22:18 -08001593 }
1594
1595 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001596 * Returns true if the device is currently in idle mode. This happens when a device
1597 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1598 * it decides to go into a lower power-use state. This may involve things like turning
1599 * off network access to apps. You can monitor for changes to this state with
1600 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1601 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001602 * @return Returns true if currently in active device idle mode, else false. This is
1603 * when idle mode restrictions are being actively applied; it will return false if the
1604 * device is in a long-term idle mode but currently running a maintenance window where
1605 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001606 */
1607 public boolean isDeviceIdleMode() {
1608 try {
1609 return mService.isDeviceIdleMode();
1610 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001611 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001612 }
1613 }
1614
1615 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001616 * Returns true if the device is currently in light idle mode. This happens when a device
1617 * has had its screen off for a short time, switching it into a batching mode where we
1618 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1619 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1620 *
1621 * @return Returns true if currently in active light device idle mode, else false. This is
1622 * when light idle mode restrictions are being actively applied; it will return false if the
1623 * device is in a long-term idle mode but currently running a maintenance window where
1624 * restrictions have been lifted.
1625 * @hide
1626 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001627 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001628 public boolean isLightDeviceIdleMode() {
1629 try {
1630 return mService.isLightDeviceIdleMode();
1631 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001632 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001633 }
1634 }
1635
1636 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001637 * Return whether the given application package name is on the device's power whitelist.
1638 * Apps can be placed on the whitelist through the settings UI invoked by
1639 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1640 */
1641 public boolean isIgnoringBatteryOptimizations(String packageName) {
Makoto Onuki152742f2019-10-31 17:22:26 -07001642 return getDeviceIdleManager().isApplicationWhitelisted(packageName);
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001643 }
1644
1645 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001646 * Turn off the device.
1647 *
1648 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001649 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001650 * @param wait If true, this call waits for the shutdown to complete and does not return.
1651 *
1652 * @hide
1653 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001654 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001655 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001656 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001657 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001658 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001659 }
1660 }
1661
1662 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001663 * This function checks if the device has implemented Sustained Performance
1664 * Mode. This needs to be checked only once and is constant for a particular
1665 * device/release.
1666 *
1667 * Sustained Performance Mode is intended to provide a consistent level of
1668 * performance for prolonged amount of time.
1669 *
1670 * Applications should check if the device supports this mode, before using
1671 * {@link android.view.Window#setSustainedPerformanceMode}.
1672 *
1673 * @return Returns True if the device supports it, false otherwise.
1674 *
1675 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001676 */
1677 public boolean isSustainedPerformanceModeSupported() {
1678 return mContext.getResources().getBoolean(
1679 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1680 }
1681
1682 /**
Wei Wang59476652018-11-29 16:02:48 -08001683 * Thermal status code: Not under throttling.
1684 */
1685 public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE;
1686
1687 /**
1688 * Thermal status code: Light throttling where UX is not impacted.
1689 */
1690 public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT;
1691
1692 /**
1693 * Thermal status code: Moderate throttling where UX is not largely impacted.
1694 */
1695 public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE;
1696
1697 /**
1698 * Thermal status code: Severe throttling where UX is largely impacted.
1699 */
1700 public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE;
1701
1702 /**
1703 * Thermal status code: Platform has done everything to reduce power.
1704 */
1705 public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL;
1706
1707 /**
1708 * Thermal status code: Key components in platform are shutting down due to thermal condition.
1709 * Device functionalities will be limited.
1710 */
1711 public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY;
1712
1713 /**
1714 * Thermal status code: Need shutdown immediately.
1715 */
1716 public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN;
1717
1718 /** @hide */
1719 @IntDef(prefix = { "THERMAL_STATUS_" }, value = {
1720 THERMAL_STATUS_NONE,
1721 THERMAL_STATUS_LIGHT,
1722 THERMAL_STATUS_MODERATE,
1723 THERMAL_STATUS_SEVERE,
1724 THERMAL_STATUS_CRITICAL,
1725 THERMAL_STATUS_EMERGENCY,
1726 THERMAL_STATUS_SHUTDOWN,
1727 })
1728 @Retention(RetentionPolicy.SOURCE)
1729 public @interface ThermalStatus {}
1730
1731 /**
1732 * This function returns the current thermal status of the device.
1733 *
1734 * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under
1735 * thermal throttling.
1736 */
1737 public @ThermalStatus int getCurrentThermalStatus() {
1738 synchronized (this) {
1739 if (mThermalService == null) {
1740 mThermalService = IThermalService.Stub.asInterface(
1741 ServiceManager.getService(Context.THERMAL_SERVICE));
1742 }
1743 try {
1744 return mThermalService.getCurrentThermalStatus();
1745 } catch (RemoteException e) {
1746 throw e.rethrowFromSystemServer();
1747 }
1748 }
1749
1750 }
1751
1752 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001753 * Listener passed to
1754 * {@link PowerManager#addThermalStatusListener} and
1755 * {@link PowerManager#removeThermalStatusListener}
1756 * to notify caller of thermal status has changed.
Wei Wang59476652018-11-29 16:02:48 -08001757 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001758 public interface OnThermalStatusChangedListener {
Wei Wang59476652018-11-29 16:02:48 -08001759
1760 /**
1761 * Called when overall thermal throttling status changed.
1762 * @param status defined in {@link android.os.Temperature}.
1763 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001764 void onThermalStatusChanged(@ThermalStatus int status);
Wei Wang59476652018-11-29 16:02:48 -08001765 }
1766
Wei Wang7a49ff62019-03-04 21:13:32 -08001767
Wei Wang59476652018-11-29 16:02:48 -08001768 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001769 * This function adds a listener for thermal status change, listen call back will be
1770 * enqueued tasks on the main thread
Wei Wang59476652018-11-29 16:02:48 -08001771 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001772 * @param listener listener to be added,
Wei Wang59476652018-11-29 16:02:48 -08001773 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001774 public void addThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1775 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001776 synchronized (this) {
1777 if (mThermalService == null) {
1778 mThermalService = IThermalService.Stub.asInterface(
1779 ServiceManager.getService(Context.THERMAL_SERVICE));
1780 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001781 this.addThermalStatusListener(mContext.getMainExecutor(), listener);
1782 }
1783 }
1784
1785 /**
1786 * This function adds a listener for thermal status change.
1787 *
1788 * @param executor {@link Executor} to handle listener callback.
1789 * @param listener listener to be added.
1790 */
1791 public void addThermalStatusListener(@NonNull @CallbackExecutor Executor executor,
1792 @NonNull OnThermalStatusChangedListener listener) {
1793 Preconditions.checkNotNull(listener, "listener cannot be null");
1794 Preconditions.checkNotNull(executor, "executor cannot be null");
1795 synchronized (this) {
1796 if (mThermalService == null) {
1797 mThermalService = IThermalService.Stub.asInterface(
1798 ServiceManager.getService(Context.THERMAL_SERVICE));
1799 }
1800 Preconditions.checkArgument(!mListenerMap.containsKey(listener),
1801 "Listener already registered: " + listener);
1802 IThermalStatusListener internalListener = new IThermalStatusListener.Stub() {
1803 @Override
1804 public void onStatusChange(int status) {
1805 final long token = Binder.clearCallingIdentity();
1806 try {
Wei Wang59476652018-11-29 16:02:48 -08001807 executor.execute(() -> {
Wei Wang7a49ff62019-03-04 21:13:32 -08001808 listener.onThermalStatusChanged(status);
Wei Wang59476652018-11-29 16:02:48 -08001809 });
Wei Wang7a49ff62019-03-04 21:13:32 -08001810 } finally {
1811 Binder.restoreCallingIdentity(token);
Wei Wang59476652018-11-29 16:02:48 -08001812 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001813 }
1814 };
1815 try {
1816 if (mThermalService.registerThermalStatusListener(internalListener)) {
1817 mListenerMap.put(listener, internalListener);
Wei Wang59476652018-11-29 16:02:48 -08001818 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001819 throw new RuntimeException("Listener failed to set");
Wei Wang59476652018-11-29 16:02:48 -08001820 }
1821 } catch (RemoteException e) {
1822 throw e.rethrowFromSystemServer();
1823 }
1824 }
1825 }
1826
1827 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001828 * This function removes a listener for thermal status change
Wei Wang59476652018-11-29 16:02:48 -08001829 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001830 * @param listener listener to be removed
Wei Wang59476652018-11-29 16:02:48 -08001831 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001832 public void removeThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1833 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001834 synchronized (this) {
1835 if (mThermalService == null) {
1836 mThermalService = IThermalService.Stub.asInterface(
1837 ServiceManager.getService(Context.THERMAL_SERVICE));
1838 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001839 IThermalStatusListener internalListener = mListenerMap.get(listener);
1840 Preconditions.checkArgument(internalListener != null, "Listener was not added");
Wei Wang59476652018-11-29 16:02:48 -08001841 try {
Wei Wang7a49ff62019-03-04 21:13:32 -08001842 if (mThermalService.unregisterThermalStatusListener(internalListener)) {
1843 mListenerMap.remove(listener);
Wei Wang59476652018-11-29 16:02:48 -08001844 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001845 throw new RuntimeException("Listener failed to remove");
Wei Wang59476652018-11-29 16:02:48 -08001846 }
1847 } catch (RemoteException e) {
1848 throw e.rethrowFromSystemServer();
1849 }
1850 }
1851 }
1852
1853 /**
Lucas Dupin16cfe452018-02-08 13:14:50 -08001854 * If true, the doze component is not started until after the screen has been
1855 * turned off and the screen off animation has been performed.
1856 * @hide
1857 */
1858 public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) {
1859 try {
1860 mService.setDozeAfterScreenOff(dozeAfterScreenOf);
1861 } catch (RemoteException e) {
1862 throw e.rethrowFromSystemServer();
1863 }
1864 }
1865
1866 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001867 * Returns the reason the phone was last shutdown. Calling app must have the
1868 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1869 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1870 * not be accessed.
1871 * @hide
1872 */
1873 @ShutdownReason
1874 public int getLastShutdownReason() {
1875 try {
1876 return mService.getLastShutdownReason();
1877 } catch (RemoteException e) {
1878 throw e.rethrowFromSystemServer();
1879 }
1880 }
1881
1882 /**
Calin Tatarua3805722018-08-09 16:41:28 +02001883 * Returns the reason the device last went to sleep (i.e. the last value of
1884 * the second argument of {@link #goToSleep(long, int, int) goToSleep}).
1885 *
1886 * @return One of the {@code GO_TO_SLEEP_REASON_*} constants.
1887 *
1888 * @hide
1889 */
1890 public int getLastSleepReason() {
1891 try {
1892 return mService.getLastSleepReason();
1893 } catch (RemoteException e) {
1894 throw e.rethrowFromSystemServer();
1895 }
1896 }
1897
1898 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +00001899 * Forces the device to go to suspend, even if there are currently wakelocks being held.
1900 * <b>Caution</b>
1901 * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts
1902 * of the system will not be notified and will not have an opportunity to save state prior to
1903 * the device going to suspend.
1904 * This method should only be used in very rare circumstances where the device is intended
1905 * to appear as completely off to the user and they have a well understood, reliable way of
1906 * re-enabling it.
1907 * </p><p>
1908 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1909 * </p>
1910 *
1911 * @return true on success, false otherwise.
1912 * @hide
1913 */
1914 @SystemApi
1915 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
1916 public boolean forceSuspend() {
1917 try {
1918 return mService.forceSuspend();
1919 } catch (RemoteException e) {
1920 throw e.rethrowFromSystemServer();
1921 }
1922 }
1923
1924 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001925 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1926 * This broadcast is only sent to registered receivers.
1927 */
1928 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1929 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1930 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1931
1932 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001933 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1934 * @hide
1935 */
1936 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1937 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1938 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1939
1940 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001941 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1942 * This broadcast is only sent to registered receivers.
1943 */
1944 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1945 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1946 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1947
1948 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001949 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1950 * This broadcast is only sent to registered receivers.
1951 * @hide
1952 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001953 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001954 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1955 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1956 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1957
1958 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001959 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1960 * This broadcast is only sent to registered receivers.
1961 */
1962 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1963 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1964 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1965
1966 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001967 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1968 * This broadcast is only sent to registered receivers.
1969 */
1970 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1971 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1972 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1973
1974 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001975 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1976 * This broadcast is only sent to registered receivers.
1977 *
1978 * @hide
1979 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001980 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04001981 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1982 public static final String ACTION_POWER_SAVE_MODE_CHANGING
1983 = "android.os.action.POWER_SAVE_MODE_CHANGING";
1984
1985 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001986 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04001987 public static final String EXTRA_POWER_SAVE_MODE = "mode";
1988
1989 /**
Denny cy Leec5a7c292019-01-01 17:37:55 +08001990 * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
1991 * @hide
1992 */
1993 public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0;
1994
1995 /**
1996 * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode
1997 * longer).
1998 * @hide
1999 */
2000 public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1;
2001
2002 /**
2003 * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly)
2004 * @hide
2005 */
2006 public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;
2007
2008 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07002009 * A wake lock is a mechanism to indicate that your application needs
2010 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07002011 * <p>
2012 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00002013 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07002014 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
2015 * </p><p>
2016 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
2017 * on at the level that was requested when the wake lock was created.
2018 * </p><p>
2019 * Call {@link #release()} when you are done and don't need the lock anymore.
2020 * It is very important to do this as soon as possible to avoid running down the
2021 * device's battery excessively.
2022 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 */
Jeff Brown1244cda2012-06-19 16:44:46 -07002024 public final class WakeLock {
Andrei Onea24ec3212019-03-15 17:35:05 +00002025 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002026 private int mFlags;
Andrei Onea24ec3212019-03-15 17:35:05 +00002027 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002028 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002029 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07002030 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002031 private int mInternalCount;
2032 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07002033 private boolean mRefCounted = true;
2034 private boolean mHeld;
2035 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002036 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07002037 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038
Jeff Brown1244cda2012-06-19 16:44:46 -07002039 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002041 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044
Dianne Hackborn713df152013-05-17 11:27:57 -07002045 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 mFlags = flags;
2047 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002048 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07002050 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07002054 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 synchronized (mToken) {
2056 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08002057 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07002058 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002060 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002062 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
2065 }
2066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067
Jeff Brown1244cda2012-06-19 16:44:46 -07002068 /**
2069 * Sets whether this WakeLock is reference counted.
2070 * <p>
2071 * Wake locks are reference counted by default. If a wake lock is
2072 * reference counted, then each call to {@link #acquire()} must be
2073 * balanced by an equal number of calls to {@link #release()}. If a wake
2074 * lock is not reference counted, then one call to {@link #release()} is
2075 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
2076 * </p>
2077 *
2078 * @param value True to make the wake lock reference counted, false to
2079 * make the wake lock non-reference counted.
2080 */
2081 public void setReferenceCounted(boolean value) {
2082 synchronized (mToken) {
2083 mRefCounted = value;
2084 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04002085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086
Jeff Brown1244cda2012-06-19 16:44:46 -07002087 /**
2088 * Acquires the wake lock.
2089 * <p>
2090 * Ensures that the device is on at the level requested when
2091 * the wake lock was created.
2092 * </p>
2093 */
2094 public void acquire() {
2095 synchronized (mToken) {
2096 acquireLocked();
2097 }
2098 }
2099
2100 /**
2101 * Acquires the wake lock with a timeout.
2102 * <p>
2103 * Ensures that the device is on at the level requested when
2104 * the wake lock was created. The lock will be released after the given timeout
2105 * expires.
2106 * </p>
2107 *
2108 * @param timeout The timeout after which to release the wake lock, in milliseconds.
2109 */
2110 public void acquire(long timeout) {
2111 synchronized (mToken) {
2112 acquireLocked();
2113 mHandler.postDelayed(mReleaser, timeout);
2114 }
2115 }
2116
2117 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002118 mInternalCount++;
2119 mExternalCount++;
2120 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07002121 // Do this even if the wake lock is already thought to be held (mHeld == true)
2122 // because non-reference counted wake locks are not always properly released.
2123 // For example, the keyguard's wake lock might be forcibly released by the
2124 // power manager without the keyguard knowing. A subsequent call to acquire
2125 // should immediately acquire the wake lock once again despite never having
2126 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07002127 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07002128 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07002129 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002130 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
2131 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07002132 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002133 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002134 }
Jeff Brownff1baef2012-07-19 15:01:17 -07002135 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07002136 }
2137 }
2138
2139 /**
2140 * Releases the wake lock.
2141 * <p>
2142 * This method releases your claim to the CPU or screen being on.
2143 * The screen may turn off shortly after you release the wake lock, or it may
2144 * not if there are other wake locks still held.
2145 * </p>
2146 */
2147 public void release() {
2148 release(0);
2149 }
2150
2151 /**
2152 * Releases the wake lock with flags to modify the release behavior.
2153 * <p>
2154 * This method releases your claim to the CPU or screen being on.
2155 * The screen may turn off shortly after you release the wake lock, or it may
2156 * not if there are other wake locks still held.
2157 * </p>
2158 *
2159 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07002160 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
2161 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07002162 */
2163 public void release(int flags) {
2164 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02002165 if (mInternalCount > 0) {
2166 // internal count must only be decreased if it is > 0 or state of
2167 // the WakeLock object is broken.
2168 mInternalCount--;
2169 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002170 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
2171 mExternalCount--;
2172 }
2173 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002174 mHandler.removeCallbacks(mReleaser);
2175 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07002176 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07002177 try {
2178 mService.releaseWakeLock(mToken, flags);
2179 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002180 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002181 }
2182 mHeld = false;
2183 }
2184 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002185 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002186 throw new RuntimeException("WakeLock under-locked " + mTag);
2187 }
2188 }
2189 }
2190
2191 /**
2192 * Returns true if the wake lock has been acquired but not yet released.
2193 *
2194 * @return True if the wake lock is held.
2195 */
2196 public boolean isHeld() {
2197 synchronized (mToken) {
2198 return mHeld;
2199 }
2200 }
2201
2202 /**
2203 * Sets the work source associated with the wake lock.
2204 * <p>
2205 * The work source is used to determine on behalf of which application
2206 * the wake lock is being held. This is useful in the case where a
2207 * service is performing work on behalf of an application so that the
2208 * cost of that work can be accounted to the application.
2209 * </p>
2210 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00002211 * <p>
2212 * Make sure to follow the tag naming convention when using WorkSource
2213 * to make it easier for app developers to understand wake locks
2214 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
2215 * documentation.
2216 * </p>
2217 *
Jeff Brown1244cda2012-06-19 16:44:46 -07002218 * @param ws The work source, or null if none.
2219 */
2220 public void setWorkSource(WorkSource ws) {
2221 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00002222 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002223 ws = null;
2224 }
2225
2226 final boolean changed;
2227 if (ws == null) {
2228 changed = mWorkSource != null;
2229 mWorkSource = null;
2230 } else if (mWorkSource == null) {
2231 changed = true;
2232 mWorkSource = new WorkSource(ws);
2233 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00002234 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07002235 if (changed) {
2236 mWorkSource.set(ws);
2237 }
2238 }
2239
2240 if (changed && mHeld) {
2241 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07002242 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07002243 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002244 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002245 }
2246 }
2247 }
2248 }
2249
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002250 /** @hide */
2251 public void setTag(String tag) {
2252 mTag = tag;
2253 }
2254
2255 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07002256 public String getTag() {
2257 return mTag;
2258 }
2259
2260 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002261 public void setHistoryTag(String tag) {
2262 mHistoryTag = tag;
2263 }
2264
2265 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002266 public void setUnimportantForLogging(boolean state) {
2267 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
2268 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
2269 }
2270
Jeff Brown1244cda2012-06-19 16:44:46 -07002271 @Override
2272 public String toString() {
2273 synchronized (mToken) {
2274 return "WakeLock{"
2275 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002276 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07002277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07002279
Yi Jin148d7f42017-11-28 14:23:56 -08002280 /** @hide */
Jeffrey Huangcb782852019-12-05 11:28:11 -08002281 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Yi Jin148d7f42017-11-28 14:23:56 -08002282 synchronized (mToken) {
2283 final long token = proto.start(fieldId);
Yi Jin163967f2018-03-15 13:49:44 -07002284 proto.write(PowerManagerProto.WakeLock.TAG, mTag);
2285 proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName);
2286 proto.write(PowerManagerProto.WakeLock.HELD, mHeld);
2287 proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount);
Yi Jin148d7f42017-11-28 14:23:56 -08002288 if (mWorkSource != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08002289 mWorkSource.dumpDebug(proto, PowerManagerProto.WakeLock.WORK_SOURCE);
Yi Jin148d7f42017-11-28 14:23:56 -08002290 }
2291 proto.end(token);
2292 }
2293 }
2294
Adrian Roos7445c0b2016-09-06 16:45:46 -07002295 /**
2296 * Wraps a Runnable such that this method immediately acquires the wake lock and then
2297 * once the Runnable is done the wake lock is released.
2298 *
2299 * <p>Example:
2300 *
2301 * <pre>
2302 * mHandler.post(mWakeLock.wrap(() -> {
2303 * // do things on handler, lock is held while we're waiting for this
2304 * // to get scheduled and until the runnable is done executing.
2305 * });
2306 * </pre>
2307 *
2308 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
2309 * leak the wakelock!
2310 *
2311 * @hide
2312 */
2313 public Runnable wrap(Runnable r) {
2314 acquire();
2315 return () -> {
2316 try {
2317 r.run();
2318 } finally {
2319 release();
2320 }
2321 };
2322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324}