blob: 45e777c7d01bc51beee0ee50fd00c7b05c576c95 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Salvador Martinez812ea752018-10-19 13:03:20 -070019import android.Manifest.permission;
Wei Wang59476652018-11-29 16:02:48 -080020import android.annotation.CallbackExecutor;
Salvador Martineza6f7b252017-04-10 10:46:15 -070021import android.annotation.IntDef;
Wei Wang59476652018-11-29 16:02:48 -080022import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070024import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Dan Gittik26b030d2018-04-16 18:50:10 +010027import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000028import android.annotation.UnsupportedAppUsage;
Jeff Brown96307042012-07-27 15:51:34 -070029import android.content.Context;
Oleg Kibirev2385b5e2018-11-13 10:43:07 -080030import android.service.dreams.Sandman;
Wei Wang59476652018-11-29 16:02:48 -080031import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080033import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070034
Wei Wang59476652018-11-29 16:02:48 -080035import com.android.internal.util.Preconditions;
36
Salvador Martineza6f7b252017-04-10 10:46:15 -070037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Wei Wang59476652018-11-29 16:02:48 -080039import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
Jeff Brown1244cda2012-06-19 16:44:46 -070042 * This class gives you control of the power state of the device.
43 *
44 * <p>
45 * <b>Device battery life will be significantly affected by the use of this API.</b>
46 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
Andrew Solovay3fea8d12019-12-05 13:05:38 -080047 * possible, and be sure to release them as soon as possible. In most cases,
48 * you'll want to use
49 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070051 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -070052 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +000053 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -070054 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060056@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -070057public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -070059
Jeff Brown155fc702012-07-27 12:12:15 -070060 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
61 * combinations were actually supported so the bit field was removed. This explains
62 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
Bookatz1a1b0462018-01-12 11:47:03 -080063 * value (in frameworks/base/core/proto/android/os/enums.proto) can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66 /**
Jeff Brown1244cda2012-06-19 16:44:46 -070067 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
68 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070069 * <p>
70 * If the user presses the power button, then the screen will be turned off
71 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -070072 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 */
Bookatz1a1b0462018-01-12 11:47:03 -080074 public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 /**
Jeff Brown155fc702012-07-27 12:12:15 -070077 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -070078 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070079 * <p>
80 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
81 * implicitly released by the system, causing both the screen and the CPU to be turned off.
82 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
83 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -070084 *
Dianne Hackborn9567a662011-04-19 18:44:03 -070085 * @deprecated Most applications should use
86 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
87 * of this type of wake lock, as it will be correctly managed by the platform
88 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 */
Dianne Hackborn9567a662011-04-19 18:44:03 -070090 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -080091 public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 /**
Jeff Brown155fc702012-07-27 12:12:15 -070094 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -070096 * <p>
97 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
98 * implicitly released by the system, causing both the screen and the CPU to be turned off.
99 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
100 * </p>
101 *
102 * @deprecated Most applications should use
103 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
104 * of this type of wake lock, as it will be correctly managed by the platform
105 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
Jeff Brown155fc702012-07-27 12:12:15 -0700107 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800108 public static final int SCREEN_BRIGHT_WAKE_LOCK =
109 OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a
Jeff Brown155fc702012-07-27 12:12:15 -0700110
111 /**
112 * Wake lock level: Ensures that the screen and keyboard backlight are on at
113 * full brightness.
114 * <p>
115 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
116 * implicitly released by the system, causing both the screen and the CPU to be turned off.
117 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
118 * </p>
119 *
120 * @deprecated Most applications should use
121 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
122 * of this type of wake lock, as it will be correctly managed by the platform
123 * as the user moves between applications and doesn't require a special permission.
124 */
125 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800126 public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700129 * Wake lock level: Turns the screen off when the proximity sensor activates.
130 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700131 * If the proximity sensor detects that an object is nearby, the screen turns off
132 * immediately. Shortly after the object moves away, the screen turns on again.
133 * </p><p>
134 * A proximity wake lock does not prevent the device from falling asleep
135 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
136 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
137 * wake locks are held, then the device will fall asleep (and lock) as usual.
138 * However, the device will not fall asleep while the screen has been turned off
139 * by the proximity sensor because it effectively counts as ongoing user activity.
140 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700141 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700142 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800143 * </p><p>
144 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700145 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700146 */
Bookatz1a1b0462018-01-12 11:47:03 -0800147 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK =
148 OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700149
150 /**
Jeff Brown26875502014-01-30 21:47:47 -0800151 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
152 * if no other wake locks are held.
153 * <p>
154 * This is used by the dream manager to implement doze mode. It currently
155 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700156 * </p><p>
157 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800158 * </p>
159 *
160 * {@hide}
161 */
Bookatz1a1b0462018-01-12 11:47:03 -0800162 public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040
Jeff Brown26875502014-01-30 21:47:47 -0800163
164 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800165 * Wake lock level: Keep the device awake enough to allow drawing to occur.
166 * <p>
167 * This is used by the window manager to allow applications to draw while the
168 * system is dozing. It currently has no effect unless the power manager is in
169 * the dozing state.
170 * </p><p>
171 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
172 * </p>
173 *
174 * {@hide}
175 */
Bookatz1a1b0462018-01-12 11:47:03 -0800176 public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080
Jeff Brownc2932a12014-11-20 18:04:05 -0800177
178 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700179 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500180 *
Jeff Brown155fc702012-07-27 12:12:15 -0700181 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500182 */
Jeff Brown155fc702012-07-27 12:12:15 -0700183 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500184
185 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700186 * Wake lock flag: Turn the screen on when the wake lock is acquired.
187 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700189 * the screen to remain on once it's already on. Think of the video player
190 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700192 * </p><p>
193 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
194 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 */
Jeff Brown155fc702012-07-27 12:12:15 -0700196 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700199 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * so the screen stays on for a little longer.
201 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700202 * Will not turn the screen on if it is not already on.
203 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
204 * </p><p>
205 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
206 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 */
Jeff Brown155fc702012-07-27 12:12:15 -0700208 public static final int ON_AFTER_RELEASE = 0x20000000;
209
210 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800211 * Wake lock flag: This wake lock is not important for logging events. If a later
212 * wake lock is acquired that is important, it will be considered the one to log.
213 * @hide
214 */
215 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
216
217 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700218 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
219 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
220 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700221 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600222 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
223
224 /**
225 * Flag for {@link WakeLock#release(int)} when called due to timeout.
226 * @hide
227 */
228 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
Jeff Brown7304c342012-05-11 18:42:42 -0700229
230 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700231 * Brightness value for fully on.
232 * @hide
233 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000234 @UnsupportedAppUsage
Jeff Brown7304c342012-05-11 18:42:42 -0700235 public static final int BRIGHTNESS_ON = 255;
236
237 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700238 * Brightness value for fully off.
239 * @hide
240 */
241 public static final int BRIGHTNESS_OFF = 0;
242
Jeff Brown970d4132014-07-19 11:33:47 -0700243 /**
244 * Brightness value for default policy handling by the system.
245 * @hide
246 */
247 public static final int BRIGHTNESS_DEFAULT = -1;
248
Jeff Brownb696de52012-07-27 15:38:50 -0700249 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
250 // if adding or modifying user activity event constants.
251
252 /**
253 * User activity event type: Unspecified event type.
254 * @hide
255 */
Jeff Brown0a571122014-08-21 21:50:43 -0700256 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700257 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
258
259 /**
260 * User activity event type: Button or key pressed or released.
261 * @hide
262 */
Jeff Brown0a571122014-08-21 21:50:43 -0700263 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700264 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
265
266 /**
267 * User activity event type: Touch down, move or up.
268 * @hide
269 */
Jeff Brown0a571122014-08-21 21:50:43 -0700270 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700271 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
272
Jeff Brown96307042012-07-27 15:51:34 -0700273 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700274 * User activity event type: Accessibility taking action on behalf of user.
275 * @hide
276 */
277 @SystemApi
278 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
279
280 /**
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800281 * User activity event type: {@link android.service.attention.AttentionService} taking action
282 * on behalf of user.
283 * @hide
284 */
285 public static final int USER_ACTIVITY_EVENT_ATTENTION = 4;
286
287 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700288 * User activity flag: If already dimmed, extend the dim timeout
289 * but do not brighten. This flag is useful for keeping the screen on
290 * a little longer without causing a visible change such as when
291 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700292 * @hide
293 */
Jeff Brown0a571122014-08-21 21:50:43 -0700294 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700295 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
296
297 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700298 * User activity flag: Note the user activity as usual but do not
299 * reset the user activity timeout. This flag is useful for applying
300 * user activity power hints when interacting with the device indirectly
301 * on a secondary screen while allowing the primary screen to go to sleep.
302 * @hide
303 */
304 @SystemApi
305 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
306
307 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000308 * @hide
309 */
310 public static final int GO_TO_SLEEP_REASON_MIN = 0;
311
312 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700313 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700314 * @hide
315 */
Santos Cordon12f92eb2019-02-01 21:28:47 +0000316 public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN;
Jeff Brown96307042012-07-27 15:51:34 -0700317
318 /**
319 * Go to sleep reason code: Going to sleep due by request of the
320 * device administration policy.
321 * @hide
322 */
323 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
324
325 /**
326 * Go to sleep reason code: Going to sleep due to a screen timeout.
327 * @hide
328 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000329 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700330 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
331
Doug Zongker3b0218b2014-01-14 12:29:06 -0800332 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700333 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
334 * @hide
335 */
336 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
337
338 /**
339 * Go to sleep reason code: Going to sleep due to the power button being pressed.
340 * @hide
341 */
342 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
343
344 /**
345 * Go to sleep reason code: Going to sleep due to HDMI.
346 * @hide
347 */
348 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
349
350 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700351 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
352 * @hide
353 */
354 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
355
356 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700357 * Go to sleep reason code: Going to sleep by request of an accessibility service
358 * @hide
359 */
360 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
361
362 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000363 * Go to sleep reason code: Going to sleep due to force-suspend.
364 * @hide
365 */
366 public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8;
367
368 /**
369 * @hide
370 */
371 public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_FORCE_SUSPEND;
372
373 /**
Calin Tatarua3805722018-08-09 16:41:28 +0200374 * @hide
375 */
376 public static String sleepReasonToString(int sleepReason) {
377 switch (sleepReason) {
378 case GO_TO_SLEEP_REASON_APPLICATION: return "application";
379 case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
380 case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
381 case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
382 case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
383 case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
384 case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
385 case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
Santos Cordon12f92eb2019-02-01 21:28:47 +0000386 case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
Calin Tatarua3805722018-08-09 16:41:28 +0200387 default: return Integer.toString(sleepReason);
388 }
389 }
390
391 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700392 * Go to sleep flag: Skip dozing state and directly go to full sleep.
393 * @hide
394 */
395 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
396
397 /**
Michael Wrighte3001042019-02-05 00:13:14 +0000398 * @hide
399 */
400 @IntDef(prefix = { "WAKE_REASON_" }, value = {
401 WAKE_REASON_UNKNOWN,
402 WAKE_REASON_POWER_BUTTON,
403 WAKE_REASON_APPLICATION,
404 WAKE_REASON_PLUGGED_IN,
405 WAKE_REASON_GESTURE,
406 WAKE_REASON_CAMERA_LAUNCH,
407 WAKE_REASON_WAKE_KEY,
408 WAKE_REASON_WAKE_MOTION,
409 WAKE_REASON_HDMI,
410 })
411 @Retention(RetentionPolicy.SOURCE)
412 public @interface WakeReason{}
413
414 /**
415 * Wake up reason code: Waking for an unknown reason.
416 * @hide
417 */
418 public static final int WAKE_REASON_UNKNOWN = 0;
419
420 /**
421 * Wake up reason code: Waking up due to power button press.
422 * @hide
423 */
424 public static final int WAKE_REASON_POWER_BUTTON = 1;
425
426 /**
427 * Wake up reason code: Waking up because an application requested it.
428 * @hide
429 */
430 public static final int WAKE_REASON_APPLICATION = 2;
431
432 /**
433 * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger.
434 * @hide
435 */
436 public static final int WAKE_REASON_PLUGGED_IN = 3;
437
438 /**
439 * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the
440 * screen).
441 * @hide
442 */
443 public static final int WAKE_REASON_GESTURE = 4;
444
445 /**
446 * Wake up reason code: Waking up due to the camera being launched.
447 * @hide
448 */
449 public static final int WAKE_REASON_CAMERA_LAUNCH = 5;
450
451 /**
452 * Wake up reason code: Waking up because a wake key other than power was pressed.
453 * @hide
454 */
455 public static final int WAKE_REASON_WAKE_KEY = 6;
456
457 /**
458 * Wake up reason code: Waking up because a wake motion was performed.
459 *
460 * For example, a trackball that was set to wake the device up was spun.
461 * @hide
462 */
463 public static final int WAKE_REASON_WAKE_MOTION = 7;
464
465 /**
466 * Wake up reason code: Waking due to HDMI.
467 * @hide
468 */
469 public static final int WAKE_REASON_HDMI = 8;
470
471 /**
472 * Wake up reason code: Waking due to the lid being opened.
473 * @hide
474 */
475 public static final int WAKE_REASON_LID = 9;
476
477 /**
478 * Convert the wake reason to a string for debugging purposes.
479 * @hide
480 */
481 public static String wakeReasonToString(@WakeReason int wakeReason) {
482 switch (wakeReason) {
483 case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN";
484 case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON";
485 case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION";
486 case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN";
487 case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE";
488 case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH";
489 case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY";
490 case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION";
491 case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI";
492 case WAKE_REASON_LID: return "WAKE_REASON_LID";
493 default: return Integer.toString(wakeReason);
494 }
495 }
496
497 /**
Santos Cordon623526b2019-04-09 17:02:38 +0100498 * @hide
499 */
500 public static class WakeData {
501 public WakeData(long wakeTime, @WakeReason int wakeReason) {
502 this.wakeTime = wakeTime;
503 this.wakeReason = wakeReason;
504 }
505 public long wakeTime;
506 public @WakeReason int wakeReason;
507 }
508
509 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800510 * The value to pass as the 'reason' argument to reboot() to reboot into
511 * recovery mode for tasks other than applying system updates, such as
512 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800513 * <p>
514 * Requires the {@link android.Manifest.permission#RECOVERY}
515 * permission (in addition to
516 * {@link android.Manifest.permission#REBOOT}).
517 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700518 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800519 */
520 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700521
522 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800523 * The value to pass as the 'reason' argument to reboot() to reboot into
524 * recovery mode for applying system updates.
525 * <p>
526 * Requires the {@link android.Manifest.permission#RECOVERY}
527 * permission (in addition to
528 * {@link android.Manifest.permission#REBOOT}).
529 * </p>
530 * @hide
531 */
532 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
533
534 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000535 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
536 * the device.
537 * @hide
538 */
539 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
540
541 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700542 * The 'reason' value used when rebooting in safe mode
543 * @hide
544 */
545 public static final String REBOOT_SAFE_MODE = "safemode";
546
547 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800548 * The 'reason' value used when rebooting the device without turning on the screen.
549 * @hide
550 */
551 public static final String REBOOT_QUIESCENT = "quiescent";
552
553 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700554 * The value to pass as the 'reason' argument to android_reboot().
555 * @hide
556 */
557 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
558
Salvador Martineza6f7b252017-04-10 10:46:15 -0700559 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700560 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
561 * is too high.
562 * @hide
563 */
564 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
565
566 /**
Wei Wangbad7c202018-11-01 11:57:39 -0700567 * The value to pass as the 'reason' argument to android_reboot() when device temperature
568 * is too high.
569 * @hide
570 */
571 public static final String SHUTDOWN_THERMAL_STATE = "thermal";
572
573 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700574 * The value to pass as the 'reason' argument to android_reboot() when device is running
575 * critically low on battery.
576 * @hide
577 */
578 public static final String SHUTDOWN_LOW_BATTERY = "battery";
579
580 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700581 * @hide
582 */
583 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700584 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700585 SHUTDOWN_REASON_UNKNOWN,
586 SHUTDOWN_REASON_SHUTDOWN,
587 SHUTDOWN_REASON_REBOOT,
588 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700589 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
590 SHUTDOWN_REASON_LOW_BATTERY,
591 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700592 })
593 public @interface ShutdownReason {}
594
595 /**
596 * constant for shutdown reason being unknown.
597 * @hide
598 */
599 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
600
601 /**
602 * constant for shutdown reason being normal shutdown.
603 * @hide
604 */
605 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
606
607 /**
608 * constant for shutdown reason being reboot.
609 * @hide
610 */
611 public static final int SHUTDOWN_REASON_REBOOT = 2;
612
613 /**
614 * constant for shutdown reason being user requested.
615 * @hide
616 */
617 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
618
619 /**
620 * constant for shutdown reason being overheating.
621 * @hide
622 */
623 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
624
Sudheer Shanka292637f2017-09-25 10:36:23 -0700625 /**
626 * constant for shutdown reason being low battery.
627 * @hide
628 */
629 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
630
631 /**
632 * constant for shutdown reason being critical battery thermal state.
633 * @hide
634 */
635 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
636
Makoto Onuki2eccd022017-11-01 13:44:23 -0700637 /**
638 * @hide
639 */
640 @Retention(RetentionPolicy.SOURCE)
Kweku Adams731a1032019-02-04 14:05:41 -0800641 @IntDef({ServiceType.LOCATION,
Makoto Onuki2eccd022017-11-01 13:44:23 -0700642 ServiceType.VIBRATION,
643 ServiceType.ANIMATION,
644 ServiceType.FULL_BACKUP,
645 ServiceType.KEYVALUE_BACKUP,
646 ServiceType.NETWORK_FIREWALL,
647 ServiceType.SCREEN_BRIGHTNESS,
648 ServiceType.SOUND,
649 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800650 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800651 ServiceType.FORCE_ALL_APPS_STANDBY,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800652 ServiceType.FORCE_BACKGROUND_CHECK,
Makoto Onukiaae89532017-11-08 14:32:03 -0800653 ServiceType.OPTIONAL_SENSORS,
Lucas Dupin92a62e52018-01-30 17:22:20 -0800654 ServiceType.AOD,
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700655 ServiceType.QUICK_DOZE,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800656 ServiceType.NIGHT_MODE,
Makoto Onukiaae89532017-11-08 14:32:03 -0800657 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700658 public @interface ServiceType {
659 int NULL = 0;
Kweku Adams731a1032019-02-04 14:05:41 -0800660 int LOCATION = 1;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700661 int VIBRATION = 2;
662 int ANIMATION = 3;
663 int FULL_BACKUP = 4;
664 int KEYVALUE_BACKUP = 5;
665 int NETWORK_FIREWALL = 6;
666 int SCREEN_BRIGHTNESS = 7;
667 int SOUND = 8;
668 int BATTERY_STATS = 9;
669 int DATA_SAVER = 10;
Lucas Dupin92a62e52018-01-30 17:22:20 -0800670 int AOD = 14;
Makoto Onukiaae89532017-11-08 14:32:03 -0800671
672 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800673 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800674 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800675 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800676
677 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800678 * Whether to enable background check on all apps or not.
679 */
680 int FORCE_BACKGROUND_CHECK = 12;
681
682 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800683 * Whether to disable non-essential sensors. (e.g. edge sensors.)
684 */
685 int OPTIONAL_SENSORS = 13;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700686
687 /**
688 * Whether to go into Deep Doze as soon as the screen turns off or not.
689 */
690 int QUICK_DOZE = 15;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800691
692 /**
693 * Whether to enable night mode when battery saver is enabled.
694 */
695 int NIGHT_MODE = 16;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700696 }
697
Makoto Onuki57f0f552017-12-11 12:22:18 -0800698 /**
699 * Either the location providers shouldn't be affected by battery saver,
700 * or battery saver is off.
701 */
702 public static final int LOCATION_MODE_NO_CHANGE = 0;
703
704 /**
705 * In this mode, the GPS based location provider should be disabled when battery saver is on and
706 * the device is non-interactive.
707 */
708 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
709
710 /**
711 * All location providers should be disabled when battery saver is on and
712 * the device is non-interactive.
713 */
714 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
715
716 /**
717 * In this mode, all the location providers will be kept available, but location fixes
718 * should only be provided to foreground apps.
719 */
720 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
721
Kweku Adams4fb074e2019-02-01 16:03:27 -0800722 /**
723 * In this mode, location will not be turned off, but LocationManager will throttle all
724 * requests to providers when the device is non-interactive.
725 */
726 public static final int LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF = 4;
727
Kweku Adamsc1d844a2019-03-28 16:05:00 -0700728 /** @hide */
729 public static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
730 /** @hide */
731 public static final int MAX_LOCATION_MODE = LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF;
Kweku Adams9f488e22019-01-14 16:25:08 -0800732
Makoto Onuki57f0f552017-12-11 12:22:18 -0800733 /**
734 * @hide
735 */
736 @Retention(RetentionPolicy.SOURCE)
737 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
738 LOCATION_MODE_NO_CHANGE,
739 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
740 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
741 LOCATION_MODE_FOREGROUND_ONLY,
Kweku Adams4fb074e2019-02-01 16:03:27 -0800742 LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF,
Makoto Onuki57f0f552017-12-11 12:22:18 -0800743 })
744 public @interface LocationPowerSaveMode {}
745
Kweku Adams4fb074e2019-02-01 16:03:27 -0800746 /** @hide */
747 public static String locationPowerSaveModeToString(@LocationPowerSaveMode int mode) {
748 switch (mode) {
749 case LOCATION_MODE_NO_CHANGE:
750 return "NO_CHANGE";
751 case LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF:
752 return "GPS_DISABLED_WHEN_SCREEN_OFF";
753 case LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF:
754 return "ALL_DISABLED_WHEN_SCREEN_OFF";
755 case LOCATION_MODE_FOREGROUND_ONLY:
756 return "FOREGROUND_ONLY";
757 case LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF:
758 return "THROTTLE_REQUESTS_WHEN_SCREEN_OFF";
759 default:
760 return Integer.toString(mode);
761 }
762 }
763
Jeff Brown96307042012-07-27 15:51:34 -0700764 final Context mContext;
Andrei Onea24ec3212019-03-15 17:35:05 +0000765 @UnsupportedAppUsage
Jeff Brown1244cda2012-06-19 16:44:46 -0700766 final IPowerManager mService;
767 final Handler mHandler;
768
Wei Wang59476652018-11-29 16:02:48 -0800769 IThermalService mThermalService;
Wei Wang7a49ff62019-03-04 21:13:32 -0800770 private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener>
771 mListenerMap = new ArrayMap<>();
Wei Wang59476652018-11-29 16:02:48 -0800772
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700773 IDeviceIdleController mIDeviceIdleController;
774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700776 * {@hide}
777 */
Jeff Brown96307042012-07-27 15:51:34 -0700778 public PowerManager(Context context, IPowerManager service, Handler handler) {
779 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700780 mService = service;
781 mHandler = handler;
782 }
783
784 /**
Jeff Brown96307042012-07-27 15:51:34 -0700785 * Gets the minimum supported screen brightness setting.
786 * The screen may be allowed to become dimmer than this value but
787 * this is the minimum value that can be set by the user.
788 * @hide
789 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000790 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700791 public int getMinimumScreenBrightnessSetting() {
792 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700793 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700794 }
795
796 /**
797 * Gets the maximum supported screen brightness setting.
798 * The screen may be allowed to become dimmer than this value but
799 * this is the maximum value that can be set by the user.
800 * @hide
801 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000802 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700803 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700804 return mContext.getResources().getInteger(
805 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700806 }
807
808 /**
809 * Gets the default screen brightness setting.
810 * @hide
811 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000812 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700813 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700814 return mContext.getResources().getInteger(
815 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700816 }
817
818 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700819 * Gets the minimum supported screen brightness setting for VR Mode.
820 * @hide
821 */
822 public int getMinimumScreenBrightnessForVrSetting() {
823 return mContext.getResources().getInteger(
824 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
825 }
826
827 /**
828 * Gets the maximum supported screen brightness setting for VR Mode.
829 * The screen may be allowed to become dimmer than this value but
830 * this is the maximum value that can be set by the user.
831 * @hide
832 */
833 public int getMaximumScreenBrightnessForVrSetting() {
834 return mContext.getResources().getInteger(
835 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
836 }
837
838 /**
839 * Gets the default screen brightness for VR setting.
840 * @hide
841 */
842 public int getDefaultScreenBrightnessForVrSetting() {
843 return mContext.getResources().getInteger(
844 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
845 }
846
847 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700848 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700849 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700850 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
851 * combined using the logical OR operator.
852 * </p><p>
853 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
854 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
855 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
856 * specified as part of the {@code levelAndFlags} parameter.
Andrew Solovay3fea8d12019-12-05 13:05:38 -0800857 * </p>
858 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700859 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
860 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
861 * {@code levelAndFlags} parameters.
862 * </p><p>
863 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
864 * wake lock, and {@link WakeLock#release release()} when you are done.
865 * </p><p>
866 * {@samplecode
867 * PowerManager pm = (PowerManager)mContext.getSystemService(
868 * Context.POWER_SERVICE);
869 * PowerManager.WakeLock wl = pm.newWakeLock(
870 * PowerManager.SCREEN_DIM_WAKE_LOCK
871 * | PowerManager.ON_AFTER_RELEASE,
872 * TAG);
873 * wl.acquire();
874 * // ... do work...
875 * wl.release();
876 * }
877 * </p><p>
878 * Although a wake lock can be created without special permissions,
879 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
880 * required to actually acquire or release the wake lock that is returned.
881 * </p><p class="note">
882 * If using this to keep the screen on, you should strongly consider using
883 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
884 * This window flag will be correctly managed by the platform
885 * as the user moves between applications and doesn't require a special permission.
886 * </p>
887 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000888 * <p>
889 * Recommended naming conventions for tags to make debugging easier:
890 * <ul>
891 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
892 * gmail:mytag) to make it easier to understand where the wake locks comes
893 * from. This namespace will also avoid collision for tags inside your app
894 * coming from different libraries which will make debugging easier.
895 * <li>use constants (e.g. do not include timestamps in the tag) to make it
896 * easier for tools to aggregate similar wake locks. When collecting
897 * debugging data, the platform only monitors a finite number of tags,
898 * using constants will help tools to provide better debugging data.
899 * <li>avoid using Class#getName() or similar method since this class name
900 * can be transformed by java optimizer and obfuscator tools.
901 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
902 * tags from the platform (e.g. *alarm*).
903 * <li>never include personnally identifiable information for privacy
904 * reasons.
905 * </ul>
906 * </p>
907 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700908 * @param levelAndFlags Combination of wake lock level and flag values defining
909 * the requested behavior of the WakeLock.
910 * @param tag Your class name (or other tag) for debugging purposes.
911 *
912 * @see WakeLock#acquire()
913 * @see WakeLock#release()
914 * @see #PARTIAL_WAKE_LOCK
915 * @see #FULL_WAKE_LOCK
916 * @see #SCREEN_DIM_WAKE_LOCK
917 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700918 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700919 * @see #ACQUIRE_CAUSES_WAKEUP
920 * @see #ON_AFTER_RELEASE
921 */
922 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700923 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700924 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700925 }
926
927 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000928 @UnsupportedAppUsage
Jeff Brown155fc702012-07-27 12:12:15 -0700929 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
930 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
931 case PARTIAL_WAKE_LOCK:
932 case SCREEN_DIM_WAKE_LOCK:
933 case SCREEN_BRIGHT_WAKE_LOCK:
934 case FULL_WAKE_LOCK:
935 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800936 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800937 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700938 break;
939 default:
940 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700941 }
942 if (tag == null) {
943 throw new IllegalArgumentException("The tag must not be null.");
944 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700945 }
946
947 /**
948 * Notifies the power manager that user activity happened.
949 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700950 * Resets the auto-off timer and brightens the screen if the device
951 * is not asleep. This is what happens normally when a key or the touch
952 * screen is pressed or when some other user activity occurs.
953 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700954 * </p><p>
955 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
956 * </p>
957 *
958 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700959 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700960 * other power management functions. It should be set
961 * to the timestamp of the input event that caused the user activity.
962 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
963 * because of this event. This is set when the power key is pressed.
964 * We want the device to stay on while the button is down, but we're about
965 * to turn off the screen so we don't want the keyboard backlight to turn on again.
966 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -0700967 *
968 * @see #wakeUp
969 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -0700970 *
971 * @removed Requires signature or system permission.
972 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700973 */
Jeff Brown7d827512014-08-21 21:56:02 -0700974 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -0700975 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -0700976 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
977 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
978 }
979
980 /**
981 * Notifies the power manager that user activity happened.
982 * <p>
983 * Resets the auto-off timer and brightens the screen if the device
984 * is not asleep. This is what happens normally when a key or the touch
985 * screen is pressed or when some other user activity occurs.
986 * This method does not wake up the device if it has been put to sleep.
987 * </p><p>
988 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
989 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
990 * </p>
991 *
992 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
993 * time base. This timestamp is used to correctly order the user activity request with
994 * other power management functions. It should be set
995 * to the timestamp of the input event that caused the user activity.
996 * @param event The user activity event.
997 * @param flags Optional user activity flags.
998 *
999 * @see #wakeUp
1000 * @see #goToSleep
1001 *
1002 * @hide Requires signature or system permission.
1003 */
1004 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001005 @RequiresPermission(anyOf = {
1006 android.Manifest.permission.DEVICE_POWER,
1007 android.Manifest.permission.USER_ACTIVITY
1008 })
Jeff Brown0a571122014-08-21 21:50:43 -07001009 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001010 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001011 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -07001012 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001013 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001014 }
1015 }
1016
1017 /**
1018 * Forces the device to go to sleep.
1019 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001020 * Overrides all the wake locks that are held.
1021 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -07001022 * </p><p>
1023 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1024 * </p>
1025 *
1026 * @param time The time when the request to go to sleep was issued, in the
1027 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001028 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -07001029 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -07001030 *
1031 * @see #userActivity
1032 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -07001033 *
1034 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -07001035 */
1036 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001037 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001038 }
1039
1040 /**
Jeff Brown7d827512014-08-21 21:56:02 -07001041 * Forces the device to go to sleep.
1042 * <p>
1043 * Overrides all the wake locks that are held.
1044 * This is what happens when the power key is pressed to turn off the screen.
1045 * </p><p>
1046 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1047 * </p>
1048 *
1049 * @param time The time when the request to go to sleep was issued, in the
1050 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1051 * order the go to sleep request with other power management functions. It should be set
1052 * to the timestamp of the input event that caused the request to go to sleep.
1053 * @param reason The reason the device is going to sleep.
1054 * @param flags Optional flags to apply when going to sleep.
1055 *
1056 * @see #userActivity
1057 * @see #wakeUp
1058 *
1059 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -07001060 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001061 @UnsupportedAppUsage
Jeff Brown6d8fd272014-05-20 21:24:38 -07001062 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001063 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001064 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -07001065 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001066 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -07001067 }
1068 }
1069
1070 /**
1071 * Forces the device to wake up from sleep.
1072 * <p>
1073 * If the device is currently asleep, wakes it up, otherwise does nothing.
1074 * This is what happens when the power key is pressed to turn on the screen.
1075 * </p><p>
1076 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1077 * </p>
1078 *
1079 * @param time The time when the request to wake up was issued, in the
1080 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001081 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -07001082 * to the timestamp of the input event that caused the request to wake up.
1083 *
1084 * @see #userActivity
1085 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001086 *
Michael Wrighte3001042019-02-05 00:13:14 +00001087 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Jeff Brown7d827512014-08-21 21:56:02 -07001088 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -07001089 */
Michael Wrighte3001042019-02-05 00:13:14 +00001090 @Deprecated
Jeff Brown96307042012-07-27 15:51:34 -07001091 public void wakeUp(long time) {
Michael Wrighte3001042019-02-05 00:13:14 +00001092 wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp");
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001093 }
1094
1095 /**
Michael Wrighte3001042019-02-05 00:13:14 +00001096 * 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
1106 * order the wake up request with other power management functions. It should be set
1107 * to the timestamp of the input event that caused the request to wake up.
1108 *
1109 * @param details A free form string to explain the specific details behind the wake up for
1110 * debugging purposes.
1111 *
1112 * @see #userActivity
1113 * @see #goToSleep
1114 *
1115 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001116 * @hide
1117 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001118 @UnsupportedAppUsage
Michael Wrighte3001042019-02-05 00:13:14 +00001119 @Deprecated
1120 public void wakeUp(long time, String details) {
1121 wakeUp(time, WAKE_REASON_UNKNOWN, details);
1122 }
1123
1124 /**
1125 * Forces the device to wake up from sleep.
1126 * <p>
1127 * If the device is currently asleep, wakes it up, otherwise does nothing.
1128 * This is what happens when the power key is pressed to turn on the screen.
1129 * </p><p>
1130 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1131 * </p>
1132 *
1133 * @param time The time when the request to wake up was issued, in the
1134 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1135 * order the wake up request with other power management functions. It should be set
1136 * to the timestamp of the input event that caused the request to wake up.
1137 *
1138 * @param reason The reason for the wake up.
1139 *
1140 * @param details A free form string to explain the specific details behind the wake up for
1141 * debugging purposes.
1142 *
1143 * @see #userActivity
1144 * @see #goToSleep
1145 * @hide
1146 */
1147 public void wakeUp(long time, @WakeReason int reason, String details) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001148 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001149 mService.wakeUp(time, reason, details, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -07001150 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001151 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001152 }
1153 }
1154
1155 /**
Jeff Brown62c82e42012-09-26 01:30:41 -07001156 * Forces the device to start napping.
1157 * <p>
1158 * If the device is currently awake, starts dreaming, otherwise does nothing.
1159 * When the dream ends or if the dream cannot be started, the device will
1160 * either wake up or go to sleep depending on whether there has been recent
1161 * user activity.
1162 * </p><p>
1163 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1164 * </p>
1165 *
1166 * @param time The time when the request to nap was issued, in the
1167 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1168 * order the nap request with other power management functions. It should be set
1169 * to the timestamp of the input event that caused the request to nap.
1170 *
1171 * @see #wakeUp
1172 * @see #goToSleep
1173 *
Jeff Brown7d827512014-08-21 21:56:02 -07001174 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -07001175 */
1176 public void nap(long time) {
1177 try {
1178 mService.nap(time);
1179 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001180 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -07001181 }
1182 }
1183
1184 /**
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001185 * Requests the device to start dreaming.
1186 * <p>
1187 * If dream can not be started, for example if another {@link PowerManager} transition is in
1188 * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when
1189 * dream ends.
1190 * </p><p>
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001191 * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and
1192 * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions.
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001193 * </p>
1194 *
1195 * @param time The time when the request to nap was issued, in the
1196 * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly
1197 * order the dream request with other power management functions. It should be set
1198 * to the timestamp of the input event that caused the request to dream.
1199 *
1200 * @hide
1201 */
1202 @SystemApi
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001203 @RequiresPermission(allOf = {
1204 android.Manifest.permission.READ_DREAM_STATE,
1205 android.Manifest.permission.WRITE_DREAM_STATE })
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001206 public void dream(long time) {
1207 Sandman.startDreamByUserRequest(mContext);
1208 }
1209
1210 /**
Jeff Browne333e672014-10-28 13:48:55 -07001211 * Boosts the brightness of the screen to maximum for a predetermined
1212 * period of time. This is used to make the screen more readable in bright
1213 * daylight for a short duration.
1214 * <p>
1215 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1216 * </p>
1217 *
1218 * @param time The time when the request to boost was issued, in the
1219 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1220 * order the boost request with other power management functions. It should be set
1221 * to the timestamp of the input event that caused the request to boost.
1222 *
1223 * @hide Requires signature permission.
1224 */
1225 public void boostScreenBrightness(long time) {
1226 try {
1227 mService.boostScreenBrightness(time);
1228 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001229 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -07001230 }
1231 }
1232
1233 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001234 * Returns whether the screen brightness is currently boosted to maximum, caused by a call
1235 * to {@link #boostScreenBrightness(long)}.
1236 * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise.
1237 *
Julius D'souzab22da802017-06-09 10:27:14 -07001238 * @deprecated This call is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001239 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001240 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001241 */
Julius D'souzab22da802017-06-09 10:27:14 -07001242 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001243 public boolean isScreenBrightnessBoosted() {
Julius D'souzab22da802017-06-09 10:27:14 -07001244 return false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001245 }
1246
Jeff Brown1244cda2012-06-19 16:44:46 -07001247 /**
Jeff Brown96307042012-07-27 15:51:34 -07001248 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001249 *
Jeff Brown96307042012-07-27 15:51:34 -07001250 * @param level The wake lock level to check.
1251 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001252 */
Jeff Brown96307042012-07-27 15:51:34 -07001253 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001254 try {
Jeff Brown96307042012-07-27 15:51:34 -07001255 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001256 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001257 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001258 }
1259 }
1260
1261 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001262 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001263 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001264 * For historical reasons, the name of this method refers to the power state of
1265 * the screen but it actually describes the overall interactive state of
1266 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001267 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001268 * The value returned by this method only indicates whether the device is
1269 * in an interactive state which may have nothing to do with the screen being
1270 * on or off. To determine the actual state of the screen,
1271 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001272 * </p>
1273 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001274 * @return True if the device is in an interactive state.
1275 *
1276 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001277 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001278 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001279 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001280 return isInteractive();
1281 }
1282
1283 /**
1284 * Returns true if the device is in an interactive state.
1285 * <p>
1286 * When this method returns true, the device is awake and ready to interact
1287 * with the user (although this is not a guarantee that the user is actively
1288 * interacting with the device just this moment). The main screen is usually
1289 * turned on while in this state. Certain features, such as the proximity
1290 * sensor, may temporarily turn off the screen while still leaving the device in an
1291 * interactive state. Note in particular that the device is still considered
1292 * to be interactive while dreaming (since dreams can be interactive) but not
1293 * when it is dozing or asleep.
1294 * </p><p>
1295 * When this method returns false, the device is dozing or asleep and must
1296 * be awoken before it will become ready to interact with the user again. The
1297 * main screen is usually turned off while in this state. Certain features,
1298 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1299 * low power state) to display system-provided content while the device dozes.
1300 * </p><p>
1301 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1302 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1303 * whenever the interactive state of the device changes. For historical reasons,
1304 * the names of these broadcasts refer to the power state of the screen
1305 * but they are actually sent in response to changes in the overall interactive
1306 * state of the device, as described by this method.
1307 * </p><p>
1308 * Services may use the non-interactive state as a hint to conserve power
1309 * since the user is not present.
1310 * </p>
1311 *
1312 * @return True if the device is in an interactive state.
1313 *
1314 * @see android.content.Intent#ACTION_SCREEN_ON
1315 * @see android.content.Intent#ACTION_SCREEN_OFF
1316 */
1317 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001318 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001319 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001320 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001321 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001322 }
1323 }
1324
1325 /**
1326 * Reboot the device. Will not return if the reboot is successful.
1327 * <p>
1328 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1329 * </p>
1330 *
1331 * @param reason code to pass to the kernel (e.g., "recovery") to
1332 * request special boot modes, or null.
1333 */
1334 public void reboot(String reason) {
1335 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001336 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001337 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001338 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001339 }
1340 }
1341
1342 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001343 * Reboot the device. Will not return if the reboot is successful.
1344 * <p>
1345 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1346 * </p>
1347 * @hide
1348 */
1349 public void rebootSafeMode() {
1350 try {
1351 mService.rebootSafeMode(false, true);
1352 } catch (RemoteException e) {
1353 throw e.rethrowFromSystemServer();
1354 }
1355 }
1356
1357 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001358 * Returns true if the device is currently in power save mode. When in this mode,
1359 * applications should reduce their functionality in order to conserve battery as
1360 * much as possible. You can monitor for changes to this state with
1361 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1362 *
1363 * @return Returns true if currently in low power mode, else false.
1364 */
1365 public boolean isPowerSaveMode() {
1366 try {
1367 return mService.isPowerSaveMode();
1368 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001369 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001370 }
1371 }
1372
1373 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001374 * Set the current power save mode.
1375 *
1376 * @return True if the set was allowed.
1377 *
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001378 * @hide
Salvador Martineza80bbab2018-09-24 10:36:11 -07001379 * @see #isPowerSaveMode()
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001380 */
Salvador Martineza80bbab2018-09-24 10:36:11 -07001381 @SystemApi
1382 @TestApi
1383 @RequiresPermission(anyOf = {
1384 android.Manifest.permission.DEVICE_POWER,
1385 android.Manifest.permission.POWER_SAVER
1386 })
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001387 public boolean setPowerSaveModeEnabled(boolean mode) {
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001388 try {
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001389 return mService.setPowerSaveModeEnabled(mode);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001390 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001391 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001392 }
1393 }
1394
1395 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001396 * Updates the current state of dynamic power savings and disable threshold. This is
1397 * a signal to the system which an app can update to serve as an indicator that
1398 * the user will be in a battery critical situation before being able to plug in.
1399 * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
1400 * This is a device global state, not a per user setting.
1401 *
1402 * <p>When enabled, the system may enact various measures for reducing power consumption in
1403 * order to help ensure that the user will make it to their next charging point. The most
1404 * visible of these will be the automatic enabling of battery saver if the user has set
1405 * their battery saver mode to "automatic". Note
1406 * that this is NOT simply an on/off switch for features, but rather a hint for the
1407 * system to consider enacting these power saving features, some of which have additional
1408 * logic around when to activate based on this signal.
1409 *
1410 * <p>The provided threshold is the percentage the system should consider itself safe at given
1411 * the current state of the device. The value is an integer representing a battery level.
1412 *
1413 * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
1414 * functionality so that the dynamic power savings itself remains a signal rather than becoming
1415 * an on/off switch for a subset of features.
1416 * @hide
1417 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001418 * @param powerSaveHint A signal indicating to the system if it believes the
Salvador Martinez812ea752018-10-19 13:03:20 -07001419 * dynamic power savings behaviors should be activated.
1420 * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
1421 * power savings behaviors.
1422 * @return True if the update was allowed and succeeded.
1423 *
1424 * @hide
1425 */
1426 @SystemApi
1427 @TestApi
1428 @RequiresPermission(permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001429 public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) {
Salvador Martinez812ea752018-10-19 13:03:20 -07001430 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001431 return mService.setDynamicPowerSaveHint(powerSaveHint, disableThreshold);
Salvador Martinez812ea752018-10-19 13:03:20 -07001432 } catch (RemoteException e) {
1433 throw e.rethrowFromSystemServer();
1434 }
1435 }
1436
1437 /**
Kweku Adams9f488e22019-01-14 16:25:08 -08001438 * Sets the policy for adaptive power save.
1439 *
1440 * @return true if there was an effectual change. If full battery saver is enabled or the
1441 * adaptive policy is not enabled, then this will return false.
1442 *
1443 * @hide
1444 */
1445 @SystemApi
1446 @RequiresPermission(anyOf = {
1447 android.Manifest.permission.DEVICE_POWER,
1448 android.Manifest.permission.POWER_SAVER
1449 })
1450 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
1451 try {
1452 return mService.setAdaptivePowerSavePolicy(config);
1453 } catch (RemoteException e) {
1454 throw e.rethrowFromSystemServer();
1455 }
1456 }
1457
1458 /**
1459 * Enables or disables adaptive power save.
1460 *
1461 * @return true if there was an effectual change. If full battery saver is enabled, then this
1462 * will return false.
1463 *
1464 * @hide
1465 */
1466 @SystemApi
1467 @RequiresPermission(anyOf = {
1468 android.Manifest.permission.DEVICE_POWER,
1469 android.Manifest.permission.POWER_SAVER
1470 })
1471 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
1472 try {
1473 return mService.setAdaptivePowerSaveEnabled(enabled);
1474 } catch (RemoteException e) {
1475 throw e.rethrowFromSystemServer();
1476 }
1477 }
1478
1479 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001480 * Indicates automatic battery saver toggling by the system will be based on percentage.
1481 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001482 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001483 *
1484 * @hide
1485 */
1486 @SystemApi
1487 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001488 public static final int POWER_SAVE_MODE_TRIGGER_PERCENTAGE = 0;
Salvador Martinez812ea752018-10-19 13:03:20 -07001489
1490 /**
1491 * Indicates automatic battery saver toggling by the system will be based on the state
1492 * of the dynamic power savings signal.
1493 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001494 * @see PowerManager#setDynamicPowerSaveHint(boolean, int)
1495 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001496 *
1497 * @hide
1498 */
1499 @SystemApi
1500 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001501 public static final int POWER_SAVE_MODE_TRIGGER_DYNAMIC = 1;
Salvador Martinez812ea752018-10-19 13:03:20 -07001502
1503 /** @hide */
1504 @Retention(RetentionPolicy.SOURCE)
1505 @IntDef(value = {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001506 POWER_SAVE_MODE_TRIGGER_PERCENTAGE,
1507 POWER_SAVE_MODE_TRIGGER_DYNAMIC
Salvador Martinez812ea752018-10-19 13:03:20 -07001508
1509 })
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001510 public @interface AutoPowerSaveModeTriggers {}
Salvador Martinez812ea752018-10-19 13:03:20 -07001511
1512
1513 /**
1514 * Returns the current battery saver control mode. Values it may return are defined in
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001515 * AutoPowerSaveModeTriggers. Note that this is a global device state, not a per user setting.
Salvador Martinez812ea752018-10-19 13:03:20 -07001516 *
1517 * @return The current value power saver mode for the system.
1518 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001519 * @see AutoPowerSaveModeTriggers
1520 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001521 * @hide
1522 */
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001523 @AutoPowerSaveModeTriggers
Salvador Martinez812ea752018-10-19 13:03:20 -07001524 @SystemApi
1525 @TestApi
1526 @RequiresPermission(android.Manifest.permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001527 public int getPowerSaveModeTrigger() {
Salvador Martinez812ea752018-10-19 13:03:20 -07001528 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001529 return mService.getPowerSaveModeTrigger();
Salvador Martinez812ea752018-10-19 13:03:20 -07001530 } catch (RemoteException e) {
1531 throw e.rethrowFromSystemServer();
1532 }
1533 }
1534
1535 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001536 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001537 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001538 * @return Battery saver state data.
1539 *
1540 * @hide
Kweku Adams7fb72a42019-01-08 16:08:49 -08001541 * @see com.android.server.power.batterysaver.BatterySaverPolicy
jackqdyulei455e90a2017-02-09 15:29:16 -08001542 * @see PowerSaveState
1543 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001544 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001545 try {
1546 return mService.getPowerSaveState(serviceType);
1547 } catch (RemoteException e) {
1548 throw e.rethrowFromSystemServer();
1549 }
1550 }
1551
1552 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001553 * Returns how location features should behave when battery saver is on. When battery saver
1554 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1555 *
1556 * <p>This API is normally only useful for components that provide location features.
1557 *
1558 * @see #isPowerSaveMode()
1559 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1560 */
1561 @LocationPowerSaveMode
1562 public int getLocationPowerSaveMode() {
Kweku Adams731a1032019-02-04 14:05:41 -08001563 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION);
Kweku Adams5e0052b2019-02-22 15:17:52 -08001564 if (!powerSaveState.batterySaverEnabled) {
Makoto Onuki57f0f552017-12-11 12:22:18 -08001565 return LOCATION_MODE_NO_CHANGE;
1566 }
Kweku Adams731a1032019-02-04 14:05:41 -08001567 return powerSaveState.locationMode;
Makoto Onuki57f0f552017-12-11 12:22:18 -08001568 }
1569
1570 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001571 * Returns true if the device is currently in idle mode. This happens when a device
1572 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1573 * it decides to go into a lower power-use state. This may involve things like turning
1574 * off network access to apps. You can monitor for changes to this state with
1575 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1576 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001577 * @return Returns true if currently in active device idle mode, else false. This is
1578 * when idle mode restrictions are being actively applied; it will return false if the
1579 * device is in a long-term idle mode but currently running a maintenance window where
1580 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001581 */
1582 public boolean isDeviceIdleMode() {
1583 try {
1584 return mService.isDeviceIdleMode();
1585 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001586 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001587 }
1588 }
1589
1590 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001591 * Returns true if the device is currently in light idle mode. This happens when a device
1592 * has had its screen off for a short time, switching it into a batching mode where we
1593 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1594 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1595 *
1596 * @return Returns true if currently in active light device idle mode, else false. This is
1597 * when light idle mode restrictions are being actively applied; it will return false if the
1598 * device is in a long-term idle mode but currently running a maintenance window where
1599 * restrictions have been lifted.
1600 * @hide
1601 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001602 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001603 public boolean isLightDeviceIdleMode() {
1604 try {
1605 return mService.isLightDeviceIdleMode();
1606 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001607 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001608 }
1609 }
1610
1611 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001612 * Return whether the given application package name is on the device's power whitelist.
1613 * Apps can be placed on the whitelist through the settings UI invoked by
1614 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1615 */
1616 public boolean isIgnoringBatteryOptimizations(String packageName) {
1617 synchronized (this) {
1618 if (mIDeviceIdleController == null) {
1619 mIDeviceIdleController = IDeviceIdleController.Stub.asInterface(
1620 ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
1621 }
1622 }
1623 try {
1624 return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName);
1625 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001626 throw e.rethrowFromSystemServer();
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001627 }
1628 }
1629
1630 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001631 * Turn off the device.
1632 *
1633 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001634 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001635 * @param wait If true, this call waits for the shutdown to complete and does not return.
1636 *
1637 * @hide
1638 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001639 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001640 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001641 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001642 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001643 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001644 }
1645 }
1646
1647 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001648 * This function checks if the device has implemented Sustained Performance
1649 * Mode. This needs to be checked only once and is constant for a particular
1650 * device/release.
1651 *
1652 * Sustained Performance Mode is intended to provide a consistent level of
1653 * performance for prolonged amount of time.
1654 *
1655 * Applications should check if the device supports this mode, before using
1656 * {@link android.view.Window#setSustainedPerformanceMode}.
1657 *
1658 * @return Returns True if the device supports it, false otherwise.
1659 *
1660 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001661 */
1662 public boolean isSustainedPerformanceModeSupported() {
1663 return mContext.getResources().getBoolean(
1664 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1665 }
1666
1667 /**
Wei Wang59476652018-11-29 16:02:48 -08001668 * Thermal status code: Not under throttling.
1669 */
1670 public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE;
1671
1672 /**
1673 * Thermal status code: Light throttling where UX is not impacted.
1674 */
1675 public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT;
1676
1677 /**
1678 * Thermal status code: Moderate throttling where UX is not largely impacted.
1679 */
1680 public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE;
1681
1682 /**
1683 * Thermal status code: Severe throttling where UX is largely impacted.
1684 */
1685 public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE;
1686
1687 /**
1688 * Thermal status code: Platform has done everything to reduce power.
1689 */
1690 public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL;
1691
1692 /**
1693 * Thermal status code: Key components in platform are shutting down due to thermal condition.
1694 * Device functionalities will be limited.
1695 */
1696 public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY;
1697
1698 /**
1699 * Thermal status code: Need shutdown immediately.
1700 */
1701 public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN;
1702
1703 /** @hide */
1704 @IntDef(prefix = { "THERMAL_STATUS_" }, value = {
1705 THERMAL_STATUS_NONE,
1706 THERMAL_STATUS_LIGHT,
1707 THERMAL_STATUS_MODERATE,
1708 THERMAL_STATUS_SEVERE,
1709 THERMAL_STATUS_CRITICAL,
1710 THERMAL_STATUS_EMERGENCY,
1711 THERMAL_STATUS_SHUTDOWN,
1712 })
1713 @Retention(RetentionPolicy.SOURCE)
1714 public @interface ThermalStatus {}
1715
1716 /**
1717 * This function returns the current thermal status of the device.
1718 *
1719 * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under
1720 * thermal throttling.
1721 */
1722 public @ThermalStatus int getCurrentThermalStatus() {
1723 synchronized (this) {
1724 if (mThermalService == null) {
1725 mThermalService = IThermalService.Stub.asInterface(
1726 ServiceManager.getService(Context.THERMAL_SERVICE));
1727 }
1728 try {
1729 return mThermalService.getCurrentThermalStatus();
1730 } catch (RemoteException e) {
1731 throw e.rethrowFromSystemServer();
1732 }
1733 }
1734
1735 }
1736
1737 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001738 * Listener passed to
1739 * {@link PowerManager#addThermalStatusListener} and
1740 * {@link PowerManager#removeThermalStatusListener}
1741 * to notify caller of thermal status has changed.
Wei Wang59476652018-11-29 16:02:48 -08001742 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001743 public interface OnThermalStatusChangedListener {
Wei Wang59476652018-11-29 16:02:48 -08001744
1745 /**
1746 * Called when overall thermal throttling status changed.
1747 * @param status defined in {@link android.os.Temperature}.
1748 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001749 void onThermalStatusChanged(@ThermalStatus int status);
Wei Wang59476652018-11-29 16:02:48 -08001750 }
1751
Wei Wang7a49ff62019-03-04 21:13:32 -08001752
Wei Wang59476652018-11-29 16:02:48 -08001753 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001754 * This function adds a listener for thermal status change, listen call back will be
1755 * enqueued tasks on the main thread
Wei Wang59476652018-11-29 16:02:48 -08001756 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001757 * @param listener listener to be added,
Wei Wang59476652018-11-29 16:02:48 -08001758 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001759 public void addThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1760 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001761 synchronized (this) {
1762 if (mThermalService == null) {
1763 mThermalService = IThermalService.Stub.asInterface(
1764 ServiceManager.getService(Context.THERMAL_SERVICE));
1765 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001766 this.addThermalStatusListener(mContext.getMainExecutor(), listener);
1767 }
1768 }
1769
1770 /**
1771 * This function adds a listener for thermal status change.
1772 *
1773 * @param executor {@link Executor} to handle listener callback.
1774 * @param listener listener to be added.
1775 */
1776 public void addThermalStatusListener(@NonNull @CallbackExecutor Executor executor,
1777 @NonNull OnThermalStatusChangedListener listener) {
1778 Preconditions.checkNotNull(listener, "listener cannot be null");
1779 Preconditions.checkNotNull(executor, "executor cannot be null");
1780 synchronized (this) {
1781 if (mThermalService == null) {
1782 mThermalService = IThermalService.Stub.asInterface(
1783 ServiceManager.getService(Context.THERMAL_SERVICE));
1784 }
1785 Preconditions.checkArgument(!mListenerMap.containsKey(listener),
1786 "Listener already registered: " + listener);
1787 IThermalStatusListener internalListener = new IThermalStatusListener.Stub() {
1788 @Override
1789 public void onStatusChange(int status) {
1790 final long token = Binder.clearCallingIdentity();
1791 try {
Wei Wang59476652018-11-29 16:02:48 -08001792 executor.execute(() -> {
Wei Wang7a49ff62019-03-04 21:13:32 -08001793 listener.onThermalStatusChanged(status);
Wei Wang59476652018-11-29 16:02:48 -08001794 });
Wei Wang7a49ff62019-03-04 21:13:32 -08001795 } finally {
1796 Binder.restoreCallingIdentity(token);
Wei Wang59476652018-11-29 16:02:48 -08001797 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001798 }
1799 };
1800 try {
1801 if (mThermalService.registerThermalStatusListener(internalListener)) {
1802 mListenerMap.put(listener, internalListener);
Wei Wang59476652018-11-29 16:02:48 -08001803 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001804 throw new RuntimeException("Listener failed to set");
Wei Wang59476652018-11-29 16:02:48 -08001805 }
1806 } catch (RemoteException e) {
1807 throw e.rethrowFromSystemServer();
1808 }
1809 }
1810 }
1811
1812 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001813 * This function removes a listener for thermal status change
Wei Wang59476652018-11-29 16:02:48 -08001814 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001815 * @param listener listener to be removed
Wei Wang59476652018-11-29 16:02:48 -08001816 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001817 public void removeThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1818 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001819 synchronized (this) {
1820 if (mThermalService == null) {
1821 mThermalService = IThermalService.Stub.asInterface(
1822 ServiceManager.getService(Context.THERMAL_SERVICE));
1823 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001824 IThermalStatusListener internalListener = mListenerMap.get(listener);
1825 Preconditions.checkArgument(internalListener != null, "Listener was not added");
Wei Wang59476652018-11-29 16:02:48 -08001826 try {
Wei Wang7a49ff62019-03-04 21:13:32 -08001827 if (mThermalService.unregisterThermalStatusListener(internalListener)) {
1828 mListenerMap.remove(listener);
Wei Wang59476652018-11-29 16:02:48 -08001829 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001830 throw new RuntimeException("Listener failed to remove");
Wei Wang59476652018-11-29 16:02:48 -08001831 }
1832 } catch (RemoteException e) {
1833 throw e.rethrowFromSystemServer();
1834 }
1835 }
1836 }
1837
1838 /**
Lucas Dupin16cfe452018-02-08 13:14:50 -08001839 * If true, the doze component is not started until after the screen has been
1840 * turned off and the screen off animation has been performed.
1841 * @hide
1842 */
1843 public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) {
1844 try {
1845 mService.setDozeAfterScreenOff(dozeAfterScreenOf);
1846 } catch (RemoteException e) {
1847 throw e.rethrowFromSystemServer();
1848 }
1849 }
1850
1851 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001852 * Returns the reason the phone was last shutdown. Calling app must have the
1853 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1854 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1855 * not be accessed.
1856 * @hide
1857 */
1858 @ShutdownReason
1859 public int getLastShutdownReason() {
1860 try {
1861 return mService.getLastShutdownReason();
1862 } catch (RemoteException e) {
1863 throw e.rethrowFromSystemServer();
1864 }
1865 }
1866
1867 /**
Calin Tatarua3805722018-08-09 16:41:28 +02001868 * Returns the reason the device last went to sleep (i.e. the last value of
1869 * the second argument of {@link #goToSleep(long, int, int) goToSleep}).
1870 *
1871 * @return One of the {@code GO_TO_SLEEP_REASON_*} constants.
1872 *
1873 * @hide
1874 */
1875 public int getLastSleepReason() {
1876 try {
1877 return mService.getLastSleepReason();
1878 } catch (RemoteException e) {
1879 throw e.rethrowFromSystemServer();
1880 }
1881 }
1882
1883 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +00001884 * Forces the device to go to suspend, even if there are currently wakelocks being held.
1885 * <b>Caution</b>
1886 * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts
1887 * of the system will not be notified and will not have an opportunity to save state prior to
1888 * the device going to suspend.
1889 * This method should only be used in very rare circumstances where the device is intended
1890 * to appear as completely off to the user and they have a well understood, reliable way of
1891 * re-enabling it.
1892 * </p><p>
1893 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1894 * </p>
1895 *
1896 * @return true on success, false otherwise.
1897 * @hide
1898 */
1899 @SystemApi
1900 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
1901 public boolean forceSuspend() {
1902 try {
1903 return mService.forceSuspend();
1904 } catch (RemoteException e) {
1905 throw e.rethrowFromSystemServer();
1906 }
1907 }
1908
1909 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001910 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1911 * This broadcast is only sent to registered receivers.
1912 */
1913 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1914 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1915 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1916
1917 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001918 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1919 * @hide
1920 */
1921 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1922 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1923 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1924
1925 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001926 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1927 * This broadcast is only sent to registered receivers.
1928 */
1929 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1930 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1931 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1932
1933 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001934 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1935 * This broadcast is only sent to registered receivers.
1936 * @hide
1937 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001938 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001939 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1940 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1941 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1942
1943 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001944 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1945 * This broadcast is only sent to registered receivers.
1946 */
1947 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1948 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1949 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1950
1951 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001952 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1953 * This broadcast is only sent to registered receivers.
1954 */
1955 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1956 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1957 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1958
1959 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001960 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1961 * This broadcast is only sent to registered receivers.
1962 *
1963 * @hide
1964 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001965 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04001966 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1967 public static final String ACTION_POWER_SAVE_MODE_CHANGING
1968 = "android.os.action.POWER_SAVE_MODE_CHANGING";
1969
1970 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001971 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04001972 public static final String EXTRA_POWER_SAVE_MODE = "mode";
1973
1974 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001975 * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed.
1976 * This broadcast is only sent to registered receivers.
1977 *
Julius D'souzab22da802017-06-09 10:27:14 -07001978 * @deprecated This intent is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001979 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001980 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001981 **/
Julius D'souzab22da802017-06-09 10:27:14 -07001982 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001983 public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED
1984 = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED";
1985
1986 /**
Denny cy Leec5a7c292019-01-01 17:37:55 +08001987 * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
1988 * @hide
1989 */
1990 public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0;
1991
1992 /**
1993 * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode
1994 * longer).
1995 * @hide
1996 */
1997 public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1;
1998
1999 /**
2000 * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly)
2001 * @hide
2002 */
2003 public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;
2004
2005 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07002006 * A wake lock is a mechanism to indicate that your application needs
2007 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07002008 * <p>
2009 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00002010 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07002011 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
2012 * </p><p>
2013 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
2014 * on at the level that was requested when the wake lock was created.
2015 * </p><p>
2016 * Call {@link #release()} when you are done and don't need the lock anymore.
2017 * It is very important to do this as soon as possible to avoid running down the
2018 * device's battery excessively.
2019 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 */
Jeff Brown1244cda2012-06-19 16:44:46 -07002021 public final class WakeLock {
Andrei Onea24ec3212019-03-15 17:35:05 +00002022 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002023 private int mFlags;
Andrei Onea24ec3212019-03-15 17:35:05 +00002024 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002025 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002026 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07002027 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002028 private int mInternalCount;
2029 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07002030 private boolean mRefCounted = true;
2031 private boolean mHeld;
2032 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002033 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07002034 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035
Jeff Brown1244cda2012-06-19 16:44:46 -07002036 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002038 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041
Dianne Hackborn713df152013-05-17 11:27:57 -07002042 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 mFlags = flags;
2044 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002045 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07002047 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07002051 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 synchronized (mToken) {
2053 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08002054 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07002055 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002057 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002059 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 }
2063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064
Jeff Brown1244cda2012-06-19 16:44:46 -07002065 /**
2066 * Sets whether this WakeLock is reference counted.
2067 * <p>
2068 * Wake locks are reference counted by default. If a wake lock is
2069 * reference counted, then each call to {@link #acquire()} must be
2070 * balanced by an equal number of calls to {@link #release()}. If a wake
2071 * lock is not reference counted, then one call to {@link #release()} is
2072 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
2073 * </p>
2074 *
2075 * @param value True to make the wake lock reference counted, false to
2076 * make the wake lock non-reference counted.
2077 */
2078 public void setReferenceCounted(boolean value) {
2079 synchronized (mToken) {
2080 mRefCounted = value;
2081 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04002082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083
Jeff Brown1244cda2012-06-19 16:44:46 -07002084 /**
2085 * Acquires the wake lock.
2086 * <p>
2087 * Ensures that the device is on at the level requested when
2088 * the wake lock was created.
2089 * </p>
2090 */
2091 public void acquire() {
2092 synchronized (mToken) {
2093 acquireLocked();
2094 }
2095 }
2096
2097 /**
2098 * Acquires the wake lock with a timeout.
2099 * <p>
2100 * Ensures that the device is on at the level requested when
2101 * the wake lock was created. The lock will be released after the given timeout
2102 * expires.
2103 * </p>
2104 *
2105 * @param timeout The timeout after which to release the wake lock, in milliseconds.
2106 */
2107 public void acquire(long timeout) {
2108 synchronized (mToken) {
2109 acquireLocked();
2110 mHandler.postDelayed(mReleaser, timeout);
2111 }
2112 }
2113
2114 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002115 mInternalCount++;
2116 mExternalCount++;
2117 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07002118 // Do this even if the wake lock is already thought to be held (mHeld == true)
2119 // because non-reference counted wake locks are not always properly released.
2120 // For example, the keyguard's wake lock might be forcibly released by the
2121 // power manager without the keyguard knowing. A subsequent call to acquire
2122 // should immediately acquire the wake lock once again despite never having
2123 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07002124 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07002125 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07002126 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002127 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
2128 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07002129 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002130 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002131 }
Jeff Brownff1baef2012-07-19 15:01:17 -07002132 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07002133 }
2134 }
2135
2136 /**
2137 * Releases the wake lock.
2138 * <p>
2139 * This method releases your claim to the CPU or screen being on.
2140 * The screen may turn off shortly after you release the wake lock, or it may
2141 * not if there are other wake locks still held.
2142 * </p>
2143 */
2144 public void release() {
2145 release(0);
2146 }
2147
2148 /**
2149 * Releases the wake lock with flags to modify the release behavior.
2150 * <p>
2151 * This method releases your claim to the CPU or screen being on.
2152 * The screen may turn off shortly after you release the wake lock, or it may
2153 * not if there are other wake locks still held.
2154 * </p>
2155 *
2156 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07002157 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
2158 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07002159 */
2160 public void release(int flags) {
2161 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02002162 if (mInternalCount > 0) {
2163 // internal count must only be decreased if it is > 0 or state of
2164 // the WakeLock object is broken.
2165 mInternalCount--;
2166 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002167 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
2168 mExternalCount--;
2169 }
2170 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002171 mHandler.removeCallbacks(mReleaser);
2172 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07002173 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07002174 try {
2175 mService.releaseWakeLock(mToken, flags);
2176 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002177 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002178 }
2179 mHeld = false;
2180 }
2181 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002182 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002183 throw new RuntimeException("WakeLock under-locked " + mTag);
2184 }
2185 }
2186 }
2187
2188 /**
2189 * Returns true if the wake lock has been acquired but not yet released.
2190 *
2191 * @return True if the wake lock is held.
2192 */
2193 public boolean isHeld() {
2194 synchronized (mToken) {
2195 return mHeld;
2196 }
2197 }
2198
2199 /**
2200 * Sets the work source associated with the wake lock.
2201 * <p>
2202 * The work source is used to determine on behalf of which application
2203 * the wake lock is being held. This is useful in the case where a
2204 * service is performing work on behalf of an application so that the
2205 * cost of that work can be accounted to the application.
2206 * </p>
2207 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00002208 * <p>
2209 * Make sure to follow the tag naming convention when using WorkSource
2210 * to make it easier for app developers to understand wake locks
2211 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
2212 * documentation.
2213 * </p>
2214 *
Jeff Brown1244cda2012-06-19 16:44:46 -07002215 * @param ws The work source, or null if none.
2216 */
2217 public void setWorkSource(WorkSource ws) {
2218 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00002219 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002220 ws = null;
2221 }
2222
2223 final boolean changed;
2224 if (ws == null) {
2225 changed = mWorkSource != null;
2226 mWorkSource = null;
2227 } else if (mWorkSource == null) {
2228 changed = true;
2229 mWorkSource = new WorkSource(ws);
2230 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00002231 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07002232 if (changed) {
2233 mWorkSource.set(ws);
2234 }
2235 }
2236
2237 if (changed && mHeld) {
2238 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07002239 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07002240 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002241 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002242 }
2243 }
2244 }
2245 }
2246
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002247 /** @hide */
2248 public void setTag(String tag) {
2249 mTag = tag;
2250 }
2251
2252 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07002253 public String getTag() {
2254 return mTag;
2255 }
2256
2257 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002258 public void setHistoryTag(String tag) {
2259 mHistoryTag = tag;
2260 }
2261
2262 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002263 public void setUnimportantForLogging(boolean state) {
2264 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
2265 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
2266 }
2267
Jeff Brown1244cda2012-06-19 16:44:46 -07002268 @Override
2269 public String toString() {
2270 synchronized (mToken) {
2271 return "WakeLock{"
2272 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002273 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07002274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07002276
Yi Jin148d7f42017-11-28 14:23:56 -08002277 /** @hide */
2278 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2279 synchronized (mToken) {
2280 final long token = proto.start(fieldId);
Yi Jin163967f2018-03-15 13:49:44 -07002281 proto.write(PowerManagerProto.WakeLock.TAG, mTag);
2282 proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName);
2283 proto.write(PowerManagerProto.WakeLock.HELD, mHeld);
2284 proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount);
Yi Jin148d7f42017-11-28 14:23:56 -08002285 if (mWorkSource != null) {
Yi Jin163967f2018-03-15 13:49:44 -07002286 mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE);
Yi Jin148d7f42017-11-28 14:23:56 -08002287 }
2288 proto.end(token);
2289 }
2290 }
2291
Adrian Roos7445c0b2016-09-06 16:45:46 -07002292 /**
2293 * Wraps a Runnable such that this method immediately acquires the wake lock and then
2294 * once the Runnable is done the wake lock is released.
2295 *
2296 * <p>Example:
2297 *
2298 * <pre>
2299 * mHandler.post(mWakeLock.wrap(() -> {
2300 * // do things on handler, lock is held while we're waiting for this
2301 * // to get scheduled and until the runnable is done executing.
2302 * });
2303 * </pre>
2304 *
2305 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
2306 * leak the wakelock!
2307 *
2308 * @hide
2309 */
2310 public Runnable wrap(Runnable r) {
2311 acquire();
2312 return () -> {
2313 try {
2314 r.run();
2315 } finally {
2316 release();
2317 }
2318 };
2319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321}