blob: 728b215a75cb02aaef0ab99c2245ab6169547c64 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Salvador Martinez812ea752018-10-19 13:03:20 -070019import android.Manifest.permission;
Wei Wang59476652018-11-29 16:02:48 -080020import android.annotation.CallbackExecutor;
Salvador Martineza6f7b252017-04-10 10:46:15 -070021import android.annotation.IntDef;
Wei Wang59476652018-11-29 16:02:48 -080022import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070024import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Dan Gittik26b030d2018-04-16 18:50:10 +010027import android.annotation.TestApi;
Jeff Brown96307042012-07-27 15:51:34 -070028import android.content.Context;
Oleg Kibirev2385b5e2018-11-13 10:43:07 -080029import android.service.dreams.Sandman;
Wei Wang59476652018-11-29 16:02:48 -080030import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080032import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070033
Wei Wang59476652018-11-29 16:02:48 -080034import com.android.internal.util.Preconditions;
35
Salvador Martineza6f7b252017-04-10 10:46:15 -070036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Wei Wang59476652018-11-29 16:02:48 -080038import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040/**
Jeff Brown1244cda2012-06-19 16:44:46 -070041 * This class gives you control of the power state of the device.
42 *
43 * <p>
44 * <b>Device battery life will be significantly affected by the use of this API.</b>
45 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
46 * possible, and be sure to release them as soon as possible.
47 * </p><p>
Jeff Brown1244cda2012-06-19 16:44:46 -070048 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}.
49 * This will create a {@link PowerManager.WakeLock} object. You can then use methods
50 * on the wake lock object to control the power state of the device.
51 * </p><p>
52 * In practice it's quite simple:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * {@samplecode
54 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
55 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
56 * wl.acquire();
57 * ..screen will stay on during this section..
58 * wl.release();
59 * }
Jeff Brown1244cda2012-06-19 16:44:46 -070060 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070061 * The following wake lock levels are defined, with varying effects on system power.
62 * <i>These levels are mutually exclusive - you may only specify one of them.</i>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070064 * <table>
Santos Cordon3107d292016-09-20 15:50:35 -070065 * <tr><th>Flag Value</th>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070068 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070069 * <td>On*</td> <td>Off</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070071 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070072 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070073 * <td>On</td> <td>Dim</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * </tr>
75 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070076 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070077 * <td>On</td> <td>Bright</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070079 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070080 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070081 * <td>On</td> <td>Bright</td> <td>Bright</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070084 * </p><p>
85 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any
86 * display timeouts or the state of the screen and even after the user presses the power button.
87 * In all other wake locks, the CPU will run, but the user can still put the device to sleep
88 * using the power button.</i>
89 * </p><p>
90 * In addition, you can add two more flags, which affect behavior of the screen only.
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070091 * <i>These flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.</i></p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070093 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070096 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause
98 * the illumination to remain on once it turns on (e.g. from user activity). This flag
99 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is
100 * acquired. A typical use would be for notifications which are important for the user to
Santos Cordon3107d292016-09-20 15:50:35 -0700101 * see immediately.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -0700103 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700104 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is
Santos Cordon3107d292016-09-20 15:50:35 -0700106 * released, causing the illumination to remain on a bit longer. This can be used to
107 * reduce flicker if you are cycling between wake lock conditions.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700110 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -0700111 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +0000112 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700113 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600115@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -0700116public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700118
Jeff Brown155fc702012-07-27 12:12:15 -0700119 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
120 * combinations were actually supported so the bit field was removed. This explains
121 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
Bookatz1a1b0462018-01-12 11:47:03 -0800122 * value (in frameworks/base/core/proto/android/os/enums.proto) can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700126 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
127 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700128 * <p>
129 * If the user presses the power button, then the screen will be turned off
130 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -0700131 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
Bookatz1a1b0462018-01-12 11:47:03 -0800133 public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700136 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700137 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700138 * <p>
139 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
140 * implicitly released by the system, causing both the screen and the CPU to be turned off.
141 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
142 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700143 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700144 * @deprecated Most applications should use
145 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
146 * of this type of wake lock, as it will be correctly managed by the platform
147 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700149 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800150 public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700153 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700155 * <p>
156 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
157 * implicitly released by the system, causing both the screen and the CPU to be turned off.
158 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
159 * </p>
160 *
161 * @deprecated Most applications should use
162 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
163 * of this type of wake lock, as it will be correctly managed by the platform
164 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 */
Jeff Brown155fc702012-07-27 12:12:15 -0700166 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800167 public static final int SCREEN_BRIGHT_WAKE_LOCK =
168 OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a
Jeff Brown155fc702012-07-27 12:12:15 -0700169
170 /**
171 * Wake lock level: Ensures that the screen and keyboard backlight are on at
172 * full brightness.
173 * <p>
174 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
175 * implicitly released by the system, causing both the screen and the CPU to be turned off.
176 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
177 * </p>
178 *
179 * @deprecated Most applications should use
180 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
181 * of this type of wake lock, as it will be correctly managed by the platform
182 * as the user moves between applications and doesn't require a special permission.
183 */
184 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800185 public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
187 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700188 * Wake lock level: Turns the screen off when the proximity sensor activates.
189 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700190 * If the proximity sensor detects that an object is nearby, the screen turns off
191 * immediately. Shortly after the object moves away, the screen turns on again.
192 * </p><p>
193 * A proximity wake lock does not prevent the device from falling asleep
194 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
195 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
196 * wake locks are held, then the device will fall asleep (and lock) as usual.
197 * However, the device will not fall asleep while the screen has been turned off
198 * by the proximity sensor because it effectively counts as ongoing user activity.
199 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700200 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700201 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800202 * </p><p>
203 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700204 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700205 */
Bookatz1a1b0462018-01-12 11:47:03 -0800206 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK =
207 OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700208
209 /**
Jeff Brown26875502014-01-30 21:47:47 -0800210 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
211 * if no other wake locks are held.
212 * <p>
213 * This is used by the dream manager to implement doze mode. It currently
214 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700215 * </p><p>
216 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800217 * </p>
218 *
219 * {@hide}
220 */
Bookatz1a1b0462018-01-12 11:47:03 -0800221 public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040
Jeff Brown26875502014-01-30 21:47:47 -0800222
223 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800224 * Wake lock level: Keep the device awake enough to allow drawing to occur.
225 * <p>
226 * This is used by the window manager to allow applications to draw while the
227 * system is dozing. It currently has no effect unless the power manager is in
228 * the dozing state.
229 * </p><p>
230 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
231 * </p>
232 *
233 * {@hide}
234 */
Bookatz1a1b0462018-01-12 11:47:03 -0800235 public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080
Jeff Brownc2932a12014-11-20 18:04:05 -0800236
237 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700238 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500239 *
Jeff Brown155fc702012-07-27 12:12:15 -0700240 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500241 */
Jeff Brown155fc702012-07-27 12:12:15 -0700242 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500243
244 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700245 * Wake lock flag: Turn the screen on when the wake lock is acquired.
246 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700248 * the screen to remain on once it's already on. Think of the video player
249 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700251 * </p><p>
252 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
253 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
Jeff Brown155fc702012-07-27 12:12:15 -0700255 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700258 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * so the screen stays on for a little longer.
260 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700261 * Will not turn the screen on if it is not already on.
262 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
263 * </p><p>
264 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
265 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 */
Jeff Brown155fc702012-07-27 12:12:15 -0700267 public static final int ON_AFTER_RELEASE = 0x20000000;
268
269 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800270 * Wake lock flag: This wake lock is not important for logging events. If a later
271 * wake lock is acquired that is important, it will be considered the one to log.
272 * @hide
273 */
274 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
275
276 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700277 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
278 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
279 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700280 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600281 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
282
283 /**
284 * Flag for {@link WakeLock#release(int)} when called due to timeout.
285 * @hide
286 */
287 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
Jeff Brown7304c342012-05-11 18:42:42 -0700288
289 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700290 * Brightness value for fully on.
291 * @hide
292 */
293 public static final int BRIGHTNESS_ON = 255;
294
295 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700296 * Brightness value for fully off.
297 * @hide
298 */
299 public static final int BRIGHTNESS_OFF = 0;
300
Jeff Brown970d4132014-07-19 11:33:47 -0700301 /**
302 * Brightness value for default policy handling by the system.
303 * @hide
304 */
305 public static final int BRIGHTNESS_DEFAULT = -1;
306
Jeff Brownb696de52012-07-27 15:38:50 -0700307 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
308 // if adding or modifying user activity event constants.
309
310 /**
311 * User activity event type: Unspecified event type.
312 * @hide
313 */
Jeff Brown0a571122014-08-21 21:50:43 -0700314 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700315 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
316
317 /**
318 * User activity event type: Button or key pressed or released.
319 * @hide
320 */
Jeff Brown0a571122014-08-21 21:50:43 -0700321 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700322 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
323
324 /**
325 * User activity event type: Touch down, move or up.
326 * @hide
327 */
Jeff Brown0a571122014-08-21 21:50:43 -0700328 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700329 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
330
Jeff Brown96307042012-07-27 15:51:34 -0700331 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700332 * User activity event type: Accessibility taking action on behalf of user.
333 * @hide
334 */
335 @SystemApi
336 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
337
338 /**
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800339 * User activity event type: {@link android.service.attention.AttentionService} taking action
340 * on behalf of user.
341 * @hide
342 */
343 public static final int USER_ACTIVITY_EVENT_ATTENTION = 4;
344
345 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700346 * User activity flag: If already dimmed, extend the dim timeout
347 * but do not brighten. This flag is useful for keeping the screen on
348 * a little longer without causing a visible change such as when
349 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700350 * @hide
351 */
Jeff Brown0a571122014-08-21 21:50:43 -0700352 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700353 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
354
355 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700356 * User activity flag: Note the user activity as usual but do not
357 * reset the user activity timeout. This flag is useful for applying
358 * user activity power hints when interacting with the device indirectly
359 * on a secondary screen while allowing the primary screen to go to sleep.
360 * @hide
361 */
362 @SystemApi
363 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
364
365 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000366 * @hide
367 */
368 public static final int GO_TO_SLEEP_REASON_MIN = 0;
369
370 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700371 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700372 * @hide
373 */
Santos Cordon12f92eb2019-02-01 21:28:47 +0000374 public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN;
Jeff Brown96307042012-07-27 15:51:34 -0700375
376 /**
377 * Go to sleep reason code: Going to sleep due by request of the
378 * device administration policy.
379 * @hide
380 */
381 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
382
383 /**
384 * Go to sleep reason code: Going to sleep due to a screen timeout.
385 * @hide
386 */
387 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
388
Doug Zongker3b0218b2014-01-14 12:29:06 -0800389 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700390 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
391 * @hide
392 */
393 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
394
395 /**
396 * Go to sleep reason code: Going to sleep due to the power button being pressed.
397 * @hide
398 */
399 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
400
401 /**
402 * Go to sleep reason code: Going to sleep due to HDMI.
403 * @hide
404 */
405 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
406
407 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700408 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
409 * @hide
410 */
411 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
412
413 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700414 * Go to sleep reason code: Going to sleep by request of an accessibility service
415 * @hide
416 */
417 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
418
419 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000420 * Go to sleep reason code: Going to sleep due to force-suspend.
421 * @hide
422 */
423 public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8;
424
425 /**
426 * @hide
427 */
428 public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_FORCE_SUSPEND;
429
430 /**
Calin Tatarua3805722018-08-09 16:41:28 +0200431 * @hide
432 */
433 public static String sleepReasonToString(int sleepReason) {
434 switch (sleepReason) {
435 case GO_TO_SLEEP_REASON_APPLICATION: return "application";
436 case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
437 case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
438 case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
439 case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
440 case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
441 case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
442 case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
Santos Cordon12f92eb2019-02-01 21:28:47 +0000443 case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
Calin Tatarua3805722018-08-09 16:41:28 +0200444 default: return Integer.toString(sleepReason);
445 }
446 }
447
448 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700449 * Go to sleep flag: Skip dozing state and directly go to full sleep.
450 * @hide
451 */
452 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
453
454 /**
Michael Wrighte3001042019-02-05 00:13:14 +0000455 * @hide
456 */
457 @IntDef(prefix = { "WAKE_REASON_" }, value = {
458 WAKE_REASON_UNKNOWN,
459 WAKE_REASON_POWER_BUTTON,
460 WAKE_REASON_APPLICATION,
461 WAKE_REASON_PLUGGED_IN,
462 WAKE_REASON_GESTURE,
463 WAKE_REASON_CAMERA_LAUNCH,
464 WAKE_REASON_WAKE_KEY,
465 WAKE_REASON_WAKE_MOTION,
466 WAKE_REASON_HDMI,
467 })
468 @Retention(RetentionPolicy.SOURCE)
469 public @interface WakeReason{}
470
471 /**
472 * Wake up reason code: Waking for an unknown reason.
473 * @hide
474 */
475 public static final int WAKE_REASON_UNKNOWN = 0;
476
477 /**
478 * Wake up reason code: Waking up due to power button press.
479 * @hide
480 */
481 public static final int WAKE_REASON_POWER_BUTTON = 1;
482
483 /**
484 * Wake up reason code: Waking up because an application requested it.
485 * @hide
486 */
487 public static final int WAKE_REASON_APPLICATION = 2;
488
489 /**
490 * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger.
491 * @hide
492 */
493 public static final int WAKE_REASON_PLUGGED_IN = 3;
494
495 /**
496 * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the
497 * screen).
498 * @hide
499 */
500 public static final int WAKE_REASON_GESTURE = 4;
501
502 /**
503 * Wake up reason code: Waking up due to the camera being launched.
504 * @hide
505 */
506 public static final int WAKE_REASON_CAMERA_LAUNCH = 5;
507
508 /**
509 * Wake up reason code: Waking up because a wake key other than power was pressed.
510 * @hide
511 */
512 public static final int WAKE_REASON_WAKE_KEY = 6;
513
514 /**
515 * Wake up reason code: Waking up because a wake motion was performed.
516 *
517 * For example, a trackball that was set to wake the device up was spun.
518 * @hide
519 */
520 public static final int WAKE_REASON_WAKE_MOTION = 7;
521
522 /**
523 * Wake up reason code: Waking due to HDMI.
524 * @hide
525 */
526 public static final int WAKE_REASON_HDMI = 8;
527
528 /**
529 * Wake up reason code: Waking due to the lid being opened.
530 * @hide
531 */
532 public static final int WAKE_REASON_LID = 9;
533
534 /**
535 * Convert the wake reason to a string for debugging purposes.
536 * @hide
537 */
538 public static String wakeReasonToString(@WakeReason int wakeReason) {
539 switch (wakeReason) {
540 case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN";
541 case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON";
542 case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION";
543 case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN";
544 case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE";
545 case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH";
546 case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY";
547 case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION";
548 case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI";
549 case WAKE_REASON_LID: return "WAKE_REASON_LID";
550 default: return Integer.toString(wakeReason);
551 }
552 }
553
554 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800555 * The value to pass as the 'reason' argument to reboot() to reboot into
556 * recovery mode for tasks other than applying system updates, such as
557 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800558 * <p>
559 * Requires the {@link android.Manifest.permission#RECOVERY}
560 * permission (in addition to
561 * {@link android.Manifest.permission#REBOOT}).
562 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700563 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800564 */
565 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700566
567 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800568 * The value to pass as the 'reason' argument to reboot() to reboot into
569 * recovery mode for applying system updates.
570 * <p>
571 * Requires the {@link android.Manifest.permission#RECOVERY}
572 * permission (in addition to
573 * {@link android.Manifest.permission#REBOOT}).
574 * </p>
575 * @hide
576 */
577 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
578
579 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000580 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
581 * the device.
582 * @hide
583 */
584 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
585
586 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700587 * The 'reason' value used when rebooting in safe mode
588 * @hide
589 */
590 public static final String REBOOT_SAFE_MODE = "safemode";
591
592 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800593 * The 'reason' value used when rebooting the device without turning on the screen.
594 * @hide
595 */
596 public static final String REBOOT_QUIESCENT = "quiescent";
597
598 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700599 * The value to pass as the 'reason' argument to android_reboot().
600 * @hide
601 */
602 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
603
Salvador Martineza6f7b252017-04-10 10:46:15 -0700604 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700605 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
606 * is too high.
607 * @hide
608 */
609 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
610
611 /**
Wei Wangbad7c202018-11-01 11:57:39 -0700612 * The value to pass as the 'reason' argument to android_reboot() when device temperature
613 * is too high.
614 * @hide
615 */
616 public static final String SHUTDOWN_THERMAL_STATE = "thermal";
617
618 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700619 * The value to pass as the 'reason' argument to android_reboot() when device is running
620 * critically low on battery.
621 * @hide
622 */
623 public static final String SHUTDOWN_LOW_BATTERY = "battery";
624
625 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700626 * @hide
627 */
628 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700629 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700630 SHUTDOWN_REASON_UNKNOWN,
631 SHUTDOWN_REASON_SHUTDOWN,
632 SHUTDOWN_REASON_REBOOT,
633 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700634 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
635 SHUTDOWN_REASON_LOW_BATTERY,
636 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700637 })
638 public @interface ShutdownReason {}
639
640 /**
641 * constant for shutdown reason being unknown.
642 * @hide
643 */
644 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
645
646 /**
647 * constant for shutdown reason being normal shutdown.
648 * @hide
649 */
650 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
651
652 /**
653 * constant for shutdown reason being reboot.
654 * @hide
655 */
656 public static final int SHUTDOWN_REASON_REBOOT = 2;
657
658 /**
659 * constant for shutdown reason being user requested.
660 * @hide
661 */
662 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
663
664 /**
665 * constant for shutdown reason being overheating.
666 * @hide
667 */
668 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
669
Sudheer Shanka292637f2017-09-25 10:36:23 -0700670 /**
671 * constant for shutdown reason being low battery.
672 * @hide
673 */
674 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
675
676 /**
677 * constant for shutdown reason being critical battery thermal state.
678 * @hide
679 */
680 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
681
Makoto Onuki2eccd022017-11-01 13:44:23 -0700682 /**
683 * @hide
684 */
685 @Retention(RetentionPolicy.SOURCE)
Kweku Adams731a1032019-02-04 14:05:41 -0800686 @IntDef({ServiceType.LOCATION,
Makoto Onuki2eccd022017-11-01 13:44:23 -0700687 ServiceType.VIBRATION,
688 ServiceType.ANIMATION,
689 ServiceType.FULL_BACKUP,
690 ServiceType.KEYVALUE_BACKUP,
691 ServiceType.NETWORK_FIREWALL,
692 ServiceType.SCREEN_BRIGHTNESS,
693 ServiceType.SOUND,
694 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800695 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800696 ServiceType.FORCE_ALL_APPS_STANDBY,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800697 ServiceType.FORCE_BACKGROUND_CHECK,
Makoto Onukiaae89532017-11-08 14:32:03 -0800698 ServiceType.OPTIONAL_SENSORS,
Lucas Dupin92a62e52018-01-30 17:22:20 -0800699 ServiceType.AOD,
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700700 ServiceType.QUICK_DOZE,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800701 ServiceType.NIGHT_MODE,
Makoto Onukiaae89532017-11-08 14:32:03 -0800702 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700703 public @interface ServiceType {
704 int NULL = 0;
Kweku Adams731a1032019-02-04 14:05:41 -0800705 int LOCATION = 1;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700706 int VIBRATION = 2;
707 int ANIMATION = 3;
708 int FULL_BACKUP = 4;
709 int KEYVALUE_BACKUP = 5;
710 int NETWORK_FIREWALL = 6;
711 int SCREEN_BRIGHTNESS = 7;
712 int SOUND = 8;
713 int BATTERY_STATS = 9;
714 int DATA_SAVER = 10;
Lucas Dupin92a62e52018-01-30 17:22:20 -0800715 int AOD = 14;
Makoto Onukiaae89532017-11-08 14:32:03 -0800716
717 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800718 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800719 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800720 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800721
722 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800723 * Whether to enable background check on all apps or not.
724 */
725 int FORCE_BACKGROUND_CHECK = 12;
726
727 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800728 * Whether to disable non-essential sensors. (e.g. edge sensors.)
729 */
730 int OPTIONAL_SENSORS = 13;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700731
732 /**
733 * Whether to go into Deep Doze as soon as the screen turns off or not.
734 */
735 int QUICK_DOZE = 15;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800736
737 /**
738 * Whether to enable night mode when battery saver is enabled.
739 */
740 int NIGHT_MODE = 16;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700741 }
742
Makoto Onuki57f0f552017-12-11 12:22:18 -0800743 /**
744 * Either the location providers shouldn't be affected by battery saver,
745 * or battery saver is off.
746 */
747 public static final int LOCATION_MODE_NO_CHANGE = 0;
748
749 /**
750 * In this mode, the GPS based location provider should be disabled when battery saver is on and
751 * the device is non-interactive.
752 */
753 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
754
755 /**
756 * All location providers should be disabled when battery saver is on and
757 * the device is non-interactive.
758 */
759 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
760
761 /**
762 * In this mode, all the location providers will be kept available, but location fixes
763 * should only be provided to foreground apps.
764 */
765 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
766
Kweku Adams4fb074e2019-02-01 16:03:27 -0800767 /**
768 * In this mode, location will not be turned off, but LocationManager will throttle all
769 * requests to providers when the device is non-interactive.
770 */
771 public static final int LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF = 4;
772
Kweku Adams9f488e22019-01-14 16:25:08 -0800773 static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
Kweku Adams4fb074e2019-02-01 16:03:27 -0800774 static final int MAX_LOCATION_MODE = LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF;
Kweku Adams9f488e22019-01-14 16:25:08 -0800775
Makoto Onuki57f0f552017-12-11 12:22:18 -0800776 /**
777 * @hide
778 */
779 @Retention(RetentionPolicy.SOURCE)
780 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
781 LOCATION_MODE_NO_CHANGE,
782 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
783 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
784 LOCATION_MODE_FOREGROUND_ONLY,
Kweku Adams4fb074e2019-02-01 16:03:27 -0800785 LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF,
Makoto Onuki57f0f552017-12-11 12:22:18 -0800786 })
787 public @interface LocationPowerSaveMode {}
788
Kweku Adams4fb074e2019-02-01 16:03:27 -0800789 /** @hide */
790 public static String locationPowerSaveModeToString(@LocationPowerSaveMode int mode) {
791 switch (mode) {
792 case LOCATION_MODE_NO_CHANGE:
793 return "NO_CHANGE";
794 case LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF:
795 return "GPS_DISABLED_WHEN_SCREEN_OFF";
796 case LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF:
797 return "ALL_DISABLED_WHEN_SCREEN_OFF";
798 case LOCATION_MODE_FOREGROUND_ONLY:
799 return "FOREGROUND_ONLY";
800 case LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF:
801 return "THROTTLE_REQUESTS_WHEN_SCREEN_OFF";
802 default:
803 return Integer.toString(mode);
804 }
805 }
806
Jeff Brown96307042012-07-27 15:51:34 -0700807 final Context mContext;
Jeff Brown1244cda2012-06-19 16:44:46 -0700808 final IPowerManager mService;
809 final Handler mHandler;
810
Wei Wang59476652018-11-29 16:02:48 -0800811 IThermalService mThermalService;
Wei Wang7a49ff62019-03-04 21:13:32 -0800812 private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener>
813 mListenerMap = new ArrayMap<>();
Wei Wang59476652018-11-29 16:02:48 -0800814
Dianne Hackborn1958e5e2015-06-12 18:11:41 -0700815 IDeviceIdleController mIDeviceIdleController;
816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700818 * {@hide}
819 */
Jeff Brown96307042012-07-27 15:51:34 -0700820 public PowerManager(Context context, IPowerManager service, Handler handler) {
821 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700822 mService = service;
823 mHandler = handler;
824 }
825
826 /**
Jeff Brown96307042012-07-27 15:51:34 -0700827 * Gets the minimum supported screen brightness setting.
828 * The screen may be allowed to become dimmer than this value but
829 * this is the minimum value that can be set by the user.
830 * @hide
831 */
832 public int getMinimumScreenBrightnessSetting() {
833 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700834 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700835 }
836
837 /**
838 * Gets the maximum supported screen brightness setting.
839 * The screen may be allowed to become dimmer than this value but
840 * this is the maximum value that can be set by the user.
841 * @hide
842 */
843 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700844 return mContext.getResources().getInteger(
845 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700846 }
847
848 /**
849 * Gets the default screen brightness setting.
850 * @hide
851 */
852 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700853 return mContext.getResources().getInteger(
854 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700855 }
856
857 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700858 * Gets the minimum supported screen brightness setting for VR Mode.
859 * @hide
860 */
861 public int getMinimumScreenBrightnessForVrSetting() {
862 return mContext.getResources().getInteger(
863 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
864 }
865
866 /**
867 * Gets the maximum supported screen brightness setting for VR Mode.
868 * The screen may be allowed to become dimmer than this value but
869 * this is the maximum value that can be set by the user.
870 * @hide
871 */
872 public int getMaximumScreenBrightnessForVrSetting() {
873 return mContext.getResources().getInteger(
874 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
875 }
876
877 /**
878 * Gets the default screen brightness for VR setting.
879 * @hide
880 */
881 public int getDefaultScreenBrightnessForVrSetting() {
882 return mContext.getResources().getInteger(
883 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
884 }
885
886 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700887 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700888 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700889 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
890 * combined using the logical OR operator.
891 * </p><p>
892 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
893 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
894 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
895 * specified as part of the {@code levelAndFlags} parameter.
896 * </p><p>
897 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
898 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
899 * {@code levelAndFlags} parameters.
900 * </p><p>
901 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
902 * wake lock, and {@link WakeLock#release release()} when you are done.
903 * </p><p>
904 * {@samplecode
905 * PowerManager pm = (PowerManager)mContext.getSystemService(
906 * Context.POWER_SERVICE);
907 * PowerManager.WakeLock wl = pm.newWakeLock(
908 * PowerManager.SCREEN_DIM_WAKE_LOCK
909 * | PowerManager.ON_AFTER_RELEASE,
910 * TAG);
911 * wl.acquire();
912 * // ... do work...
913 * wl.release();
914 * }
915 * </p><p>
916 * Although a wake lock can be created without special permissions,
917 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
918 * required to actually acquire or release the wake lock that is returned.
919 * </p><p class="note">
920 * If using this to keep the screen on, you should strongly consider using
921 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
922 * This window flag will be correctly managed by the platform
923 * as the user moves between applications and doesn't require a special permission.
924 * </p>
925 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000926 * <p>
927 * Recommended naming conventions for tags to make debugging easier:
928 * <ul>
929 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
930 * gmail:mytag) to make it easier to understand where the wake locks comes
931 * from. This namespace will also avoid collision for tags inside your app
932 * coming from different libraries which will make debugging easier.
933 * <li>use constants (e.g. do not include timestamps in the tag) to make it
934 * easier for tools to aggregate similar wake locks. When collecting
935 * debugging data, the platform only monitors a finite number of tags,
936 * using constants will help tools to provide better debugging data.
937 * <li>avoid using Class#getName() or similar method since this class name
938 * can be transformed by java optimizer and obfuscator tools.
939 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
940 * tags from the platform (e.g. *alarm*).
941 * <li>never include personnally identifiable information for privacy
942 * reasons.
943 * </ul>
944 * </p>
945 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700946 * @param levelAndFlags Combination of wake lock level and flag values defining
947 * the requested behavior of the WakeLock.
948 * @param tag Your class name (or other tag) for debugging purposes.
949 *
950 * @see WakeLock#acquire()
951 * @see WakeLock#release()
952 * @see #PARTIAL_WAKE_LOCK
953 * @see #FULL_WAKE_LOCK
954 * @see #SCREEN_DIM_WAKE_LOCK
955 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700956 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700957 * @see #ACQUIRE_CAUSES_WAKEUP
958 * @see #ON_AFTER_RELEASE
959 */
960 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700961 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700962 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700963 }
964
965 /** @hide */
966 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
967 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
968 case PARTIAL_WAKE_LOCK:
969 case SCREEN_DIM_WAKE_LOCK:
970 case SCREEN_BRIGHT_WAKE_LOCK:
971 case FULL_WAKE_LOCK:
972 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -0800973 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -0800974 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -0700975 break;
976 default:
977 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -0700978 }
979 if (tag == null) {
980 throw new IllegalArgumentException("The tag must not be null.");
981 }
Jeff Brown1244cda2012-06-19 16:44:46 -0700982 }
983
984 /**
985 * Notifies the power manager that user activity happened.
986 * <p>
Jeff Brown96307042012-07-27 15:51:34 -0700987 * Resets the auto-off timer and brightens the screen if the device
988 * is not asleep. This is what happens normally when a key or the touch
989 * screen is pressed or when some other user activity occurs.
990 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -0700991 * </p><p>
992 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
993 * </p>
994 *
995 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -0700996 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -0700997 * other power management functions. It should be set
998 * to the timestamp of the input event that caused the user activity.
999 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
1000 * because of this event. This is set when the power key is pressed.
1001 * We want the device to stay on while the button is down, but we're about
1002 * to turn off the screen so we don't want the keyboard backlight to turn on again.
1003 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -07001004 *
1005 * @see #wakeUp
1006 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001007 *
1008 * @removed Requires signature or system permission.
1009 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001010 */
Jeff Brown7d827512014-08-21 21:56:02 -07001011 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001012 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -07001013 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
1014 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
1015 }
1016
1017 /**
1018 * Notifies the power manager that user activity happened.
1019 * <p>
1020 * Resets the auto-off timer and brightens the screen if the device
1021 * is not asleep. This is what happens normally when a key or the touch
1022 * screen is pressed or when some other user activity occurs.
1023 * This method does not wake up the device if it has been put to sleep.
1024 * </p><p>
1025 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
1026 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
1027 * </p>
1028 *
1029 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
1030 * time base. This timestamp is used to correctly order the user activity request with
1031 * other power management functions. It should be set
1032 * to the timestamp of the input event that caused the user activity.
1033 * @param event The user activity event.
1034 * @param flags Optional user activity flags.
1035 *
1036 * @see #wakeUp
1037 * @see #goToSleep
1038 *
1039 * @hide Requires signature or system permission.
1040 */
1041 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001042 @RequiresPermission(anyOf = {
1043 android.Manifest.permission.DEVICE_POWER,
1044 android.Manifest.permission.USER_ACTIVITY
1045 })
Jeff Brown0a571122014-08-21 21:50:43 -07001046 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001047 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001048 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -07001049 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001050 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001051 }
1052 }
1053
1054 /**
1055 * Forces the device to go to sleep.
1056 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001057 * Overrides all the wake locks that are held.
1058 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -07001059 * </p><p>
1060 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1061 * </p>
1062 *
1063 * @param time The time when the request to go to sleep was issued, in the
1064 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001065 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -07001066 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -07001067 *
1068 * @see #userActivity
1069 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -07001070 *
1071 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -07001072 */
1073 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001074 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001075 }
1076
1077 /**
Jeff Brown7d827512014-08-21 21:56:02 -07001078 * Forces the device to go to sleep.
1079 * <p>
1080 * Overrides all the wake locks that are held.
1081 * This is what happens when the power key is pressed to turn off the screen.
1082 * </p><p>
1083 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1084 * </p>
1085 *
1086 * @param time The time when the request to go to sleep was issued, in the
1087 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1088 * order the go to sleep request with other power management functions. It should be set
1089 * to the timestamp of the input event that caused the request to go to sleep.
1090 * @param reason The reason the device is going to sleep.
1091 * @param flags Optional flags to apply when going to sleep.
1092 *
1093 * @see #userActivity
1094 * @see #wakeUp
1095 *
1096 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -07001097 */
1098 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001099 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001100 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -07001101 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001102 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -07001103 }
1104 }
1105
1106 /**
1107 * Forces the device to wake up from sleep.
1108 * <p>
1109 * If the device is currently asleep, wakes it up, otherwise does nothing.
1110 * This is what happens when the power key is pressed to turn on the screen.
1111 * </p><p>
1112 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1113 * </p>
1114 *
1115 * @param time The time when the request to wake up was issued, in the
1116 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001117 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -07001118 * to the timestamp of the input event that caused the request to wake up.
1119 *
1120 * @see #userActivity
1121 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001122 *
Michael Wrighte3001042019-02-05 00:13:14 +00001123 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Jeff Brown7d827512014-08-21 21:56:02 -07001124 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -07001125 */
Michael Wrighte3001042019-02-05 00:13:14 +00001126 @Deprecated
Jeff Brown96307042012-07-27 15:51:34 -07001127 public void wakeUp(long time) {
Michael Wrighte3001042019-02-05 00:13:14 +00001128 wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp");
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001129 }
1130
1131 /**
Michael Wrighte3001042019-02-05 00:13:14 +00001132 * Forces the device to wake up from sleep.
1133 * <p>
1134 * If the device is currently asleep, wakes it up, otherwise does nothing.
1135 * This is what happens when the power key is pressed to turn on the screen.
1136 * </p><p>
1137 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1138 * </p>
1139 *
1140 * @param time The time when the request to wake up was issued, in the
1141 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1142 * order the wake up request with other power management functions. It should be set
1143 * to the timestamp of the input event that caused the request to wake up.
1144 *
1145 * @param details A free form string to explain the specific details behind the wake up for
1146 * debugging purposes.
1147 *
1148 * @see #userActivity
1149 * @see #goToSleep
1150 *
1151 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001152 * @hide
1153 */
Michael Wrighte3001042019-02-05 00:13:14 +00001154 @Deprecated
1155 public void wakeUp(long time, String details) {
1156 wakeUp(time, WAKE_REASON_UNKNOWN, details);
1157 }
1158
1159 /**
1160 * Forces the device to wake up from sleep.
1161 * <p>
1162 * If the device is currently asleep, wakes it up, otherwise does nothing.
1163 * This is what happens when the power key is pressed to turn on the screen.
1164 * </p><p>
1165 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1166 * </p>
1167 *
1168 * @param time The time when the request to wake up was issued, in the
1169 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1170 * order the wake up request with other power management functions. It should be set
1171 * to the timestamp of the input event that caused the request to wake up.
1172 *
1173 * @param reason The reason for the wake up.
1174 *
1175 * @param details A free form string to explain the specific details behind the wake up for
1176 * debugging purposes.
1177 *
1178 * @see #userActivity
1179 * @see #goToSleep
1180 * @hide
1181 */
1182 public void wakeUp(long time, @WakeReason int reason, String details) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001183 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001184 mService.wakeUp(time, reason, details, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -07001185 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001186 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001187 }
1188 }
1189
1190 /**
Jeff Brown62c82e42012-09-26 01:30:41 -07001191 * Forces the device to start napping.
1192 * <p>
1193 * If the device is currently awake, starts dreaming, otherwise does nothing.
1194 * When the dream ends or if the dream cannot be started, the device will
1195 * either wake up or go to sleep depending on whether there has been recent
1196 * user activity.
1197 * </p><p>
1198 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1199 * </p>
1200 *
1201 * @param time The time when the request to nap was issued, in the
1202 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1203 * order the nap request with other power management functions. It should be set
1204 * to the timestamp of the input event that caused the request to nap.
1205 *
1206 * @see #wakeUp
1207 * @see #goToSleep
1208 *
Jeff Brown7d827512014-08-21 21:56:02 -07001209 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -07001210 */
1211 public void nap(long time) {
1212 try {
1213 mService.nap(time);
1214 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001215 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -07001216 }
1217 }
1218
1219 /**
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001220 * Requests the device to start dreaming.
1221 * <p>
1222 * If dream can not be started, for example if another {@link PowerManager} transition is in
1223 * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when
1224 * dream ends.
1225 * </p><p>
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001226 * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and
1227 * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions.
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001228 * </p>
1229 *
1230 * @param time The time when the request to nap was issued, in the
1231 * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly
1232 * order the dream request with other power management functions. It should be set
1233 * to the timestamp of the input event that caused the request to dream.
1234 *
1235 * @hide
1236 */
1237 @SystemApi
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001238 @RequiresPermission(allOf = {
1239 android.Manifest.permission.READ_DREAM_STATE,
1240 android.Manifest.permission.WRITE_DREAM_STATE })
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001241 public void dream(long time) {
1242 Sandman.startDreamByUserRequest(mContext);
1243 }
1244
1245 /**
Jeff Browne333e672014-10-28 13:48:55 -07001246 * Boosts the brightness of the screen to maximum for a predetermined
1247 * period of time. This is used to make the screen more readable in bright
1248 * daylight for a short duration.
1249 * <p>
1250 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1251 * </p>
1252 *
1253 * @param time The time when the request to boost was issued, in the
1254 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1255 * order the boost request with other power management functions. It should be set
1256 * to the timestamp of the input event that caused the request to boost.
1257 *
1258 * @hide Requires signature permission.
1259 */
1260 public void boostScreenBrightness(long time) {
1261 try {
1262 mService.boostScreenBrightness(time);
1263 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001264 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -07001265 }
1266 }
1267
1268 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001269 * Returns whether the screen brightness is currently boosted to maximum, caused by a call
1270 * to {@link #boostScreenBrightness(long)}.
1271 * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise.
1272 *
Julius D'souzab22da802017-06-09 10:27:14 -07001273 * @deprecated This call is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001274 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06001275 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001276 */
Julius D'souzab22da802017-06-09 10:27:14 -07001277 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001278 public boolean isScreenBrightnessBoosted() {
Julius D'souzab22da802017-06-09 10:27:14 -07001279 return false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07001280 }
1281
Jeff Brown1244cda2012-06-19 16:44:46 -07001282 /**
Jeff Brown96307042012-07-27 15:51:34 -07001283 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001284 *
Jeff Brown96307042012-07-27 15:51:34 -07001285 * @param level The wake lock level to check.
1286 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001287 */
Jeff Brown96307042012-07-27 15:51:34 -07001288 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001289 try {
Jeff Brown96307042012-07-27 15:51:34 -07001290 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001291 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001292 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001293 }
1294 }
1295
1296 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001297 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001298 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001299 * For historical reasons, the name of this method refers to the power state of
1300 * the screen but it actually describes the overall interactive state of
1301 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001302 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001303 * The value returned by this method only indicates whether the device is
1304 * in an interactive state which may have nothing to do with the screen being
1305 * on or off. To determine the actual state of the screen,
1306 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001307 * </p>
1308 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001309 * @return True if the device is in an interactive state.
1310 *
1311 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001312 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001313 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001314 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001315 return isInteractive();
1316 }
1317
1318 /**
1319 * Returns true if the device is in an interactive state.
1320 * <p>
1321 * When this method returns true, the device is awake and ready to interact
1322 * with the user (although this is not a guarantee that the user is actively
1323 * interacting with the device just this moment). The main screen is usually
1324 * turned on while in this state. Certain features, such as the proximity
1325 * sensor, may temporarily turn off the screen while still leaving the device in an
1326 * interactive state. Note in particular that the device is still considered
1327 * to be interactive while dreaming (since dreams can be interactive) but not
1328 * when it is dozing or asleep.
1329 * </p><p>
1330 * When this method returns false, the device is dozing or asleep and must
1331 * be awoken before it will become ready to interact with the user again. The
1332 * main screen is usually turned off while in this state. Certain features,
1333 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1334 * low power state) to display system-provided content while the device dozes.
1335 * </p><p>
1336 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1337 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1338 * whenever the interactive state of the device changes. For historical reasons,
1339 * the names of these broadcasts refer to the power state of the screen
1340 * but they are actually sent in response to changes in the overall interactive
1341 * state of the device, as described by this method.
1342 * </p><p>
1343 * Services may use the non-interactive state as a hint to conserve power
1344 * since the user is not present.
1345 * </p>
1346 *
1347 * @return True if the device is in an interactive state.
1348 *
1349 * @see android.content.Intent#ACTION_SCREEN_ON
1350 * @see android.content.Intent#ACTION_SCREEN_OFF
1351 */
1352 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001353 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001354 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001355 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001356 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001357 }
1358 }
1359
1360 /**
1361 * Reboot the device. Will not return if the reboot is successful.
1362 * <p>
1363 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1364 * </p>
1365 *
1366 * @param reason code to pass to the kernel (e.g., "recovery") to
1367 * request special boot modes, or null.
1368 */
1369 public void reboot(String reason) {
1370 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001371 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001372 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001373 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001374 }
1375 }
1376
1377 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001378 * Reboot the device. Will not return if the reboot is successful.
1379 * <p>
1380 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1381 * </p>
1382 * @hide
1383 */
1384 public void rebootSafeMode() {
1385 try {
1386 mService.rebootSafeMode(false, true);
1387 } catch (RemoteException e) {
1388 throw e.rethrowFromSystemServer();
1389 }
1390 }
1391
1392 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001393 * Returns true if the device is currently in power save mode. When in this mode,
1394 * applications should reduce their functionality in order to conserve battery as
1395 * much as possible. You can monitor for changes to this state with
1396 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1397 *
1398 * @return Returns true if currently in low power mode, else false.
1399 */
1400 public boolean isPowerSaveMode() {
1401 try {
1402 return mService.isPowerSaveMode();
1403 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001404 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001405 }
1406 }
1407
1408 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001409 * Set the current power save mode.
1410 *
1411 * @return True if the set was allowed.
1412 *
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001413 * @hide
Salvador Martineza80bbab2018-09-24 10:36:11 -07001414 * @see #isPowerSaveMode()
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001415 */
Salvador Martineza80bbab2018-09-24 10:36:11 -07001416 @SystemApi
1417 @TestApi
1418 @RequiresPermission(anyOf = {
1419 android.Manifest.permission.DEVICE_POWER,
1420 android.Manifest.permission.POWER_SAVER
1421 })
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001422 public boolean setPowerSaveMode(boolean mode) {
1423 try {
1424 return mService.setPowerSaveMode(mode);
1425 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001426 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001427 }
1428 }
1429
1430 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001431 * Updates the current state of dynamic power savings and disable threshold. This is
1432 * a signal to the system which an app can update to serve as an indicator that
1433 * the user will be in a battery critical situation before being able to plug in.
1434 * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
1435 * This is a device global state, not a per user setting.
1436 *
1437 * <p>When enabled, the system may enact various measures for reducing power consumption in
1438 * order to help ensure that the user will make it to their next charging point. The most
1439 * visible of these will be the automatic enabling of battery saver if the user has set
1440 * their battery saver mode to "automatic". Note
1441 * that this is NOT simply an on/off switch for features, but rather a hint for the
1442 * system to consider enacting these power saving features, some of which have additional
1443 * logic around when to activate based on this signal.
1444 *
1445 * <p>The provided threshold is the percentage the system should consider itself safe at given
1446 * the current state of the device. The value is an integer representing a battery level.
1447 *
1448 * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
1449 * functionality so that the dynamic power savings itself remains a signal rather than becoming
1450 * an on/off switch for a subset of features.
1451 * @hide
1452 *
1453 * @param dynamicPowerSavingsEnabled A signal indicating to the system if it believes the
1454 * dynamic power savings behaviors should be activated.
1455 * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
1456 * power savings behaviors.
1457 * @return True if the update was allowed and succeeded.
1458 *
1459 * @hide
1460 */
1461 @SystemApi
1462 @TestApi
1463 @RequiresPermission(permission.POWER_SAVER)
1464 public boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled,
1465 int disableThreshold) {
1466 try {
1467 return mService.setDynamicPowerSavings(dynamicPowerSavingsEnabled, disableThreshold);
1468 } catch (RemoteException e) {
1469 throw e.rethrowFromSystemServer();
1470 }
1471 }
1472
1473 /**
Kweku Adams9f488e22019-01-14 16:25:08 -08001474 * Sets the policy for adaptive power save.
1475 *
1476 * @return true if there was an effectual change. If full battery saver is enabled or the
1477 * adaptive policy is not enabled, then this will return false.
1478 *
1479 * @hide
1480 */
1481 @SystemApi
1482 @RequiresPermission(anyOf = {
1483 android.Manifest.permission.DEVICE_POWER,
1484 android.Manifest.permission.POWER_SAVER
1485 })
1486 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
1487 try {
1488 return mService.setAdaptivePowerSavePolicy(config);
1489 } catch (RemoteException e) {
1490 throw e.rethrowFromSystemServer();
1491 }
1492 }
1493
1494 /**
1495 * Enables or disables adaptive power save.
1496 *
1497 * @return true if there was an effectual change. If full battery saver is enabled, then this
1498 * will return false.
1499 *
1500 * @hide
1501 */
1502 @SystemApi
1503 @RequiresPermission(anyOf = {
1504 android.Manifest.permission.DEVICE_POWER,
1505 android.Manifest.permission.POWER_SAVER
1506 })
1507 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
1508 try {
1509 return mService.setAdaptivePowerSaveEnabled(enabled);
1510 } catch (RemoteException e) {
1511 throw e.rethrowFromSystemServer();
1512 }
1513 }
1514
1515 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001516 * Indicates automatic battery saver toggling by the system will be based on percentage.
1517 *
1518 * @see PowerManager#getPowerSaveMode()
1519 *
1520 * @hide
1521 */
1522 @SystemApi
1523 @TestApi
1524 public static final int POWER_SAVER_MODE_PERCENTAGE = 0;
1525
1526 /**
1527 * Indicates automatic battery saver toggling by the system will be based on the state
1528 * of the dynamic power savings signal.
1529 *
1530 * @see PowerManager#setDynamicPowerSavings(boolean, int)
1531 * @see PowerManager#getPowerSaveMode()
1532 *
1533 * @hide
1534 */
1535 @SystemApi
1536 @TestApi
1537 public static final int POWER_SAVER_MODE_DYNAMIC = 1;
1538
1539 /** @hide */
1540 @Retention(RetentionPolicy.SOURCE)
1541 @IntDef(value = {
1542 POWER_SAVER_MODE_PERCENTAGE,
1543 POWER_SAVER_MODE_DYNAMIC
1544
1545 })
1546 public @interface AutoPowerSaverMode{}
1547
1548
1549 /**
1550 * Returns the current battery saver control mode. Values it may return are defined in
1551 * AutoPowerSaverMode. Note that this is a global device state, not a per user setting.
1552 *
1553 * @return The current value power saver mode for the system.
1554 *
1555 * @see AutoPowerSaverMode
1556 * @see PowerManager#getPowerSaveMode()
1557 * @hide
1558 */
1559 @AutoPowerSaverMode
1560 @SystemApi
1561 @TestApi
1562 @RequiresPermission(android.Manifest.permission.POWER_SAVER)
1563 public int getPowerSaveMode() {
1564 try {
1565 return mService.getPowerSaveMode();
1566 } catch (RemoteException e) {
1567 throw e.rethrowFromSystemServer();
1568 }
1569 }
1570
1571 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001572 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001573 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001574 * @return Battery saver state data.
1575 *
1576 * @hide
Kweku Adams7fb72a42019-01-08 16:08:49 -08001577 * @see com.android.server.power.batterysaver.BatterySaverPolicy
jackqdyulei455e90a2017-02-09 15:29:16 -08001578 * @see PowerSaveState
1579 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001580 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001581 try {
1582 return mService.getPowerSaveState(serviceType);
1583 } catch (RemoteException e) {
1584 throw e.rethrowFromSystemServer();
1585 }
1586 }
1587
1588 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001589 * Returns how location features should behave when battery saver is on. When battery saver
1590 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1591 *
1592 * <p>This API is normally only useful for components that provide location features.
1593 *
1594 * @see #isPowerSaveMode()
1595 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1596 */
1597 @LocationPowerSaveMode
1598 public int getLocationPowerSaveMode() {
Kweku Adams731a1032019-02-04 14:05:41 -08001599 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION);
Kweku Adams5e0052b2019-02-22 15:17:52 -08001600 if (!powerSaveState.batterySaverEnabled) {
Makoto Onuki57f0f552017-12-11 12:22:18 -08001601 return LOCATION_MODE_NO_CHANGE;
1602 }
Kweku Adams731a1032019-02-04 14:05:41 -08001603 return powerSaveState.locationMode;
Makoto Onuki57f0f552017-12-11 12:22:18 -08001604 }
1605
1606 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001607 * Returns true if the device is currently in idle mode. This happens when a device
1608 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1609 * it decides to go into a lower power-use state. This may involve things like turning
1610 * off network access to apps. You can monitor for changes to this state with
1611 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1612 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001613 * @return Returns true if currently in active device idle mode, else false. This is
1614 * when idle mode restrictions are being actively applied; it will return false if the
1615 * device is in a long-term idle mode but currently running a maintenance window where
1616 * restrictions have been lifted.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001617 */
1618 public boolean isDeviceIdleMode() {
1619 try {
1620 return mService.isDeviceIdleMode();
1621 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001622 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001623 }
1624 }
1625
1626 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001627 * Returns true if the device is currently in light idle mode. This happens when a device
1628 * has had its screen off for a short time, switching it into a batching mode where we
1629 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1630 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1631 *
1632 * @return Returns true if currently in active light device idle mode, else false. This is
1633 * when light idle mode restrictions are being actively applied; it will return false if the
1634 * device is in a long-term idle mode but currently running a maintenance window where
1635 * restrictions have been lifted.
1636 * @hide
1637 */
1638 public boolean isLightDeviceIdleMode() {
1639 try {
1640 return mService.isLightDeviceIdleMode();
1641 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001642 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001643 }
1644 }
1645
1646 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001647 * Return whether the given application package name is on the device's power whitelist.
1648 * Apps can be placed on the whitelist through the settings UI invoked by
1649 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1650 */
1651 public boolean isIgnoringBatteryOptimizations(String packageName) {
1652 synchronized (this) {
1653 if (mIDeviceIdleController == null) {
1654 mIDeviceIdleController = IDeviceIdleController.Stub.asInterface(
1655 ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
1656 }
1657 }
1658 try {
1659 return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName);
1660 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001661 throw e.rethrowFromSystemServer();
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001662 }
1663 }
1664
1665 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001666 * Turn off the device.
1667 *
1668 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001669 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001670 * @param wait If true, this call waits for the shutdown to complete and does not return.
1671 *
1672 * @hide
1673 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001674 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001675 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001676 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001677 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001678 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001679 }
1680 }
1681
1682 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001683 * This function checks if the device has implemented Sustained Performance
1684 * Mode. This needs to be checked only once and is constant for a particular
1685 * device/release.
1686 *
1687 * Sustained Performance Mode is intended to provide a consistent level of
1688 * performance for prolonged amount of time.
1689 *
1690 * Applications should check if the device supports this mode, before using
1691 * {@link android.view.Window#setSustainedPerformanceMode}.
1692 *
1693 * @return Returns True if the device supports it, false otherwise.
1694 *
1695 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001696 */
1697 public boolean isSustainedPerformanceModeSupported() {
1698 return mContext.getResources().getBoolean(
1699 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1700 }
1701
1702 /**
Wei Wang59476652018-11-29 16:02:48 -08001703 * Thermal status code: Not under throttling.
1704 */
1705 public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE;
1706
1707 /**
1708 * Thermal status code: Light throttling where UX is not impacted.
1709 */
1710 public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT;
1711
1712 /**
1713 * Thermal status code: Moderate throttling where UX is not largely impacted.
1714 */
1715 public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE;
1716
1717 /**
1718 * Thermal status code: Severe throttling where UX is largely impacted.
1719 */
1720 public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE;
1721
1722 /**
1723 * Thermal status code: Platform has done everything to reduce power.
1724 */
1725 public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL;
1726
1727 /**
1728 * Thermal status code: Key components in platform are shutting down due to thermal condition.
1729 * Device functionalities will be limited.
1730 */
1731 public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY;
1732
1733 /**
1734 * Thermal status code: Need shutdown immediately.
1735 */
1736 public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN;
1737
1738 /** @hide */
1739 @IntDef(prefix = { "THERMAL_STATUS_" }, value = {
1740 THERMAL_STATUS_NONE,
1741 THERMAL_STATUS_LIGHT,
1742 THERMAL_STATUS_MODERATE,
1743 THERMAL_STATUS_SEVERE,
1744 THERMAL_STATUS_CRITICAL,
1745 THERMAL_STATUS_EMERGENCY,
1746 THERMAL_STATUS_SHUTDOWN,
1747 })
1748 @Retention(RetentionPolicy.SOURCE)
1749 public @interface ThermalStatus {}
1750
1751 /**
1752 * This function returns the current thermal status of the device.
1753 *
1754 * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under
1755 * thermal throttling.
1756 */
1757 public @ThermalStatus int getCurrentThermalStatus() {
1758 synchronized (this) {
1759 if (mThermalService == null) {
1760 mThermalService = IThermalService.Stub.asInterface(
1761 ServiceManager.getService(Context.THERMAL_SERVICE));
1762 }
1763 try {
1764 return mThermalService.getCurrentThermalStatus();
1765 } catch (RemoteException e) {
1766 throw e.rethrowFromSystemServer();
1767 }
1768 }
1769
1770 }
1771
1772 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001773 * Listener passed to
1774 * {@link PowerManager#addThermalStatusListener} and
1775 * {@link PowerManager#removeThermalStatusListener}
1776 * to notify caller of thermal status has changed.
Wei Wang59476652018-11-29 16:02:48 -08001777 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001778 public interface OnThermalStatusChangedListener {
Wei Wang59476652018-11-29 16:02:48 -08001779
1780 /**
1781 * Called when overall thermal throttling status changed.
1782 * @param status defined in {@link android.os.Temperature}.
1783 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001784 void onThermalStatusChanged(@ThermalStatus int status);
Wei Wang59476652018-11-29 16:02:48 -08001785 }
1786
Wei Wang7a49ff62019-03-04 21:13:32 -08001787
Wei Wang59476652018-11-29 16:02:48 -08001788 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001789 * This function adds a listener for thermal status change, listen call back will be
1790 * enqueued tasks on the main thread
Wei Wang59476652018-11-29 16:02:48 -08001791 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001792 * @param listener listener to be added,
Wei Wang59476652018-11-29 16:02:48 -08001793 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001794 public void addThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1795 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001796 synchronized (this) {
1797 if (mThermalService == null) {
1798 mThermalService = IThermalService.Stub.asInterface(
1799 ServiceManager.getService(Context.THERMAL_SERVICE));
1800 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001801 this.addThermalStatusListener(mContext.getMainExecutor(), listener);
1802 }
1803 }
1804
1805 /**
1806 * This function adds a listener for thermal status change.
1807 *
1808 * @param executor {@link Executor} to handle listener callback.
1809 * @param listener listener to be added.
1810 */
1811 public void addThermalStatusListener(@NonNull @CallbackExecutor Executor executor,
1812 @NonNull OnThermalStatusChangedListener listener) {
1813 Preconditions.checkNotNull(listener, "listener cannot be null");
1814 Preconditions.checkNotNull(executor, "executor cannot be null");
1815 synchronized (this) {
1816 if (mThermalService == null) {
1817 mThermalService = IThermalService.Stub.asInterface(
1818 ServiceManager.getService(Context.THERMAL_SERVICE));
1819 }
1820 Preconditions.checkArgument(!mListenerMap.containsKey(listener),
1821 "Listener already registered: " + listener);
1822 IThermalStatusListener internalListener = new IThermalStatusListener.Stub() {
1823 @Override
1824 public void onStatusChange(int status) {
1825 final long token = Binder.clearCallingIdentity();
1826 try {
Wei Wang59476652018-11-29 16:02:48 -08001827 executor.execute(() -> {
Wei Wang7a49ff62019-03-04 21:13:32 -08001828 listener.onThermalStatusChanged(status);
Wei Wang59476652018-11-29 16:02:48 -08001829 });
Wei Wang7a49ff62019-03-04 21:13:32 -08001830 } finally {
1831 Binder.restoreCallingIdentity(token);
Wei Wang59476652018-11-29 16:02:48 -08001832 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001833 }
1834 };
1835 try {
1836 if (mThermalService.registerThermalStatusListener(internalListener)) {
1837 mListenerMap.put(listener, internalListener);
Wei Wang59476652018-11-29 16:02:48 -08001838 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001839 throw new RuntimeException("Listener failed to set");
Wei Wang59476652018-11-29 16:02:48 -08001840 }
1841 } catch (RemoteException e) {
1842 throw e.rethrowFromSystemServer();
1843 }
1844 }
1845 }
1846
1847 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001848 * This function removes a listener for thermal status change
Wei Wang59476652018-11-29 16:02:48 -08001849 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001850 * @param listener listener to be removed
Wei Wang59476652018-11-29 16:02:48 -08001851 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001852 public void removeThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1853 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001854 synchronized (this) {
1855 if (mThermalService == null) {
1856 mThermalService = IThermalService.Stub.asInterface(
1857 ServiceManager.getService(Context.THERMAL_SERVICE));
1858 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001859 IThermalStatusListener internalListener = mListenerMap.get(listener);
1860 Preconditions.checkArgument(internalListener != null, "Listener was not added");
Wei Wang59476652018-11-29 16:02:48 -08001861 try {
Wei Wang7a49ff62019-03-04 21:13:32 -08001862 if (mThermalService.unregisterThermalStatusListener(internalListener)) {
1863 mListenerMap.remove(listener);
Wei Wang59476652018-11-29 16:02:48 -08001864 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001865 throw new RuntimeException("Listener failed to remove");
Wei Wang59476652018-11-29 16:02:48 -08001866 }
1867 } catch (RemoteException e) {
1868 throw e.rethrowFromSystemServer();
1869 }
1870 }
1871 }
1872
1873 /**
Lucas Dupin16cfe452018-02-08 13:14:50 -08001874 * If true, the doze component is not started until after the screen has been
1875 * turned off and the screen off animation has been performed.
1876 * @hide
1877 */
1878 public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) {
1879 try {
1880 mService.setDozeAfterScreenOff(dozeAfterScreenOf);
1881 } catch (RemoteException e) {
1882 throw e.rethrowFromSystemServer();
1883 }
1884 }
1885
1886 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001887 * Returns the reason the phone was last shutdown. Calling app must have the
1888 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1889 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1890 * not be accessed.
1891 * @hide
1892 */
1893 @ShutdownReason
1894 public int getLastShutdownReason() {
1895 try {
1896 return mService.getLastShutdownReason();
1897 } catch (RemoteException e) {
1898 throw e.rethrowFromSystemServer();
1899 }
1900 }
1901
1902 /**
Calin Tatarua3805722018-08-09 16:41:28 +02001903 * Returns the reason the device last went to sleep (i.e. the last value of
1904 * the second argument of {@link #goToSleep(long, int, int) goToSleep}).
1905 *
1906 * @return One of the {@code GO_TO_SLEEP_REASON_*} constants.
1907 *
1908 * @hide
1909 */
1910 public int getLastSleepReason() {
1911 try {
1912 return mService.getLastSleepReason();
1913 } catch (RemoteException e) {
1914 throw e.rethrowFromSystemServer();
1915 }
1916 }
1917
1918 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +00001919 * Forces the device to go to suspend, even if there are currently wakelocks being held.
1920 * <b>Caution</b>
1921 * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts
1922 * of the system will not be notified and will not have an opportunity to save state prior to
1923 * the device going to suspend.
1924 * This method should only be used in very rare circumstances where the device is intended
1925 * to appear as completely off to the user and they have a well understood, reliable way of
1926 * re-enabling it.
1927 * </p><p>
1928 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1929 * </p>
1930 *
1931 * @return true on success, false otherwise.
1932 * @hide
1933 */
1934 @SystemApi
1935 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
1936 public boolean forceSuspend() {
1937 try {
1938 return mService.forceSuspend();
1939 } catch (RemoteException e) {
1940 throw e.rethrowFromSystemServer();
1941 }
1942 }
1943
1944 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001945 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1946 * This broadcast is only sent to registered receivers.
1947 */
1948 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1949 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1950 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1951
1952 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001953 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1954 * @hide
1955 */
1956 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1957 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1958 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1959
1960 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001961 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1962 * This broadcast is only sent to registered receivers.
1963 */
1964 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1965 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1966 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1967
1968 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001969 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1970 * This broadcast is only sent to registered receivers.
1971 * @hide
1972 */
1973 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1974 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1975 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1976
1977 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001978 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1979 * This broadcast is only sent to registered receivers.
1980 */
1981 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1982 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1983 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1984
1985 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001986 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1987 * This broadcast is only sent to registered receivers.
1988 */
1989 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1990 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
1991 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
1992
1993 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04001994 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
1995 * This broadcast is only sent to registered receivers.
1996 *
1997 * @hide
1998 */
1999 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
2000 public static final String ACTION_POWER_SAVE_MODE_CHANGING
2001 = "android.os.action.POWER_SAVE_MODE_CHANGING";
2002
2003 /** @hide */
2004 public static final String EXTRA_POWER_SAVE_MODE = "mode";
2005
2006 /**
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002007 * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed.
2008 * This broadcast is only sent to registered receivers.
2009 *
Julius D'souzab22da802017-06-09 10:27:14 -07002010 * @deprecated This intent is rarely used and will be phased out soon.
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002011 * @hide
Jeff Sharkey84789112017-08-11 14:45:43 -06002012 * @removed
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002013 **/
Julius D'souzab22da802017-06-09 10:27:14 -07002014 @SystemApi @Deprecated
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002015 public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED
2016 = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED";
2017
2018 /**
Denny cy Leec5a7c292019-01-01 17:37:55 +08002019 * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
2020 * @hide
2021 */
2022 public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0;
2023
2024 /**
2025 * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode
2026 * longer).
2027 * @hide
2028 */
2029 public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1;
2030
2031 /**
2032 * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly)
2033 * @hide
2034 */
2035 public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;
2036
2037 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07002038 * A wake lock is a mechanism to indicate that your application needs
2039 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07002040 * <p>
2041 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00002042 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07002043 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
2044 * </p><p>
2045 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
2046 * on at the level that was requested when the wake lock was created.
2047 * </p><p>
2048 * Call {@link #release()} when you are done and don't need the lock anymore.
2049 * It is very important to do this as soon as possible to avoid running down the
2050 * device's battery excessively.
2051 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 */
Jeff Brown1244cda2012-06-19 16:44:46 -07002053 public final class WakeLock {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002054 private int mFlags;
2055 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002056 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07002057 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002058 private int mInternalCount;
2059 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07002060 private boolean mRefCounted = true;
2061 private boolean mHeld;
2062 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002063 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07002064 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065
Jeff Brown1244cda2012-06-19 16:44:46 -07002066 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002068 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071
Dianne Hackborn713df152013-05-17 11:27:57 -07002072 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 mFlags = flags;
2074 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002075 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07002077 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07002081 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 synchronized (mToken) {
2083 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08002084 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07002085 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002087 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002089 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 }
2093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094
Jeff Brown1244cda2012-06-19 16:44:46 -07002095 /**
2096 * Sets whether this WakeLock is reference counted.
2097 * <p>
2098 * Wake locks are reference counted by default. If a wake lock is
2099 * reference counted, then each call to {@link #acquire()} must be
2100 * balanced by an equal number of calls to {@link #release()}. If a wake
2101 * lock is not reference counted, then one call to {@link #release()} is
2102 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
2103 * </p>
2104 *
2105 * @param value True to make the wake lock reference counted, false to
2106 * make the wake lock non-reference counted.
2107 */
2108 public void setReferenceCounted(boolean value) {
2109 synchronized (mToken) {
2110 mRefCounted = value;
2111 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04002112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113
Jeff Brown1244cda2012-06-19 16:44:46 -07002114 /**
2115 * Acquires the wake lock.
2116 * <p>
2117 * Ensures that the device is on at the level requested when
2118 * the wake lock was created.
2119 * </p>
2120 */
2121 public void acquire() {
2122 synchronized (mToken) {
2123 acquireLocked();
2124 }
2125 }
2126
2127 /**
2128 * Acquires the wake lock with a timeout.
2129 * <p>
2130 * Ensures that the device is on at the level requested when
2131 * the wake lock was created. The lock will be released after the given timeout
2132 * expires.
2133 * </p>
2134 *
2135 * @param timeout The timeout after which to release the wake lock, in milliseconds.
2136 */
2137 public void acquire(long timeout) {
2138 synchronized (mToken) {
2139 acquireLocked();
2140 mHandler.postDelayed(mReleaser, timeout);
2141 }
2142 }
2143
2144 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002145 mInternalCount++;
2146 mExternalCount++;
2147 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07002148 // Do this even if the wake lock is already thought to be held (mHeld == true)
2149 // because non-reference counted wake locks are not always properly released.
2150 // For example, the keyguard's wake lock might be forcibly released by the
2151 // power manager without the keyguard knowing. A subsequent call to acquire
2152 // should immediately acquire the wake lock once again despite never having
2153 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07002154 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07002155 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07002156 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002157 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
2158 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07002159 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002160 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002161 }
Jeff Brownff1baef2012-07-19 15:01:17 -07002162 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07002163 }
2164 }
2165
2166 /**
2167 * Releases the wake lock.
2168 * <p>
2169 * This method releases your claim to the CPU or screen being on.
2170 * The screen may turn off shortly after you release the wake lock, or it may
2171 * not if there are other wake locks still held.
2172 * </p>
2173 */
2174 public void release() {
2175 release(0);
2176 }
2177
2178 /**
2179 * Releases the wake lock with flags to modify the release behavior.
2180 * <p>
2181 * This method releases your claim to the CPU or screen being on.
2182 * The screen may turn off shortly after you release the wake lock, or it may
2183 * not if there are other wake locks still held.
2184 * </p>
2185 *
2186 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07002187 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
2188 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07002189 */
2190 public void release(int flags) {
2191 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02002192 if (mInternalCount > 0) {
2193 // internal count must only be decreased if it is > 0 or state of
2194 // the WakeLock object is broken.
2195 mInternalCount--;
2196 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002197 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
2198 mExternalCount--;
2199 }
2200 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002201 mHandler.removeCallbacks(mReleaser);
2202 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07002203 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07002204 try {
2205 mService.releaseWakeLock(mToken, flags);
2206 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002207 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002208 }
2209 mHeld = false;
2210 }
2211 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002212 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002213 throw new RuntimeException("WakeLock under-locked " + mTag);
2214 }
2215 }
2216 }
2217
2218 /**
2219 * Returns true if the wake lock has been acquired but not yet released.
2220 *
2221 * @return True if the wake lock is held.
2222 */
2223 public boolean isHeld() {
2224 synchronized (mToken) {
2225 return mHeld;
2226 }
2227 }
2228
2229 /**
2230 * Sets the work source associated with the wake lock.
2231 * <p>
2232 * The work source is used to determine on behalf of which application
2233 * the wake lock is being held. This is useful in the case where a
2234 * service is performing work on behalf of an application so that the
2235 * cost of that work can be accounted to the application.
2236 * </p>
2237 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00002238 * <p>
2239 * Make sure to follow the tag naming convention when using WorkSource
2240 * to make it easier for app developers to understand wake locks
2241 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
2242 * documentation.
2243 * </p>
2244 *
Jeff Brown1244cda2012-06-19 16:44:46 -07002245 * @param ws The work source, or null if none.
2246 */
2247 public void setWorkSource(WorkSource ws) {
2248 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00002249 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002250 ws = null;
2251 }
2252
2253 final boolean changed;
2254 if (ws == null) {
2255 changed = mWorkSource != null;
2256 mWorkSource = null;
2257 } else if (mWorkSource == null) {
2258 changed = true;
2259 mWorkSource = new WorkSource(ws);
2260 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00002261 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07002262 if (changed) {
2263 mWorkSource.set(ws);
2264 }
2265 }
2266
2267 if (changed && mHeld) {
2268 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07002269 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07002270 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002271 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002272 }
2273 }
2274 }
2275 }
2276
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002277 /** @hide */
2278 public void setTag(String tag) {
2279 mTag = tag;
2280 }
2281
2282 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07002283 public String getTag() {
2284 return mTag;
2285 }
2286
2287 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002288 public void setHistoryTag(String tag) {
2289 mHistoryTag = tag;
2290 }
2291
2292 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002293 public void setUnimportantForLogging(boolean state) {
2294 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
2295 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
2296 }
2297
Jeff Brown1244cda2012-06-19 16:44:46 -07002298 @Override
2299 public String toString() {
2300 synchronized (mToken) {
2301 return "WakeLock{"
2302 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002303 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07002304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07002306
Yi Jin148d7f42017-11-28 14:23:56 -08002307 /** @hide */
2308 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2309 synchronized (mToken) {
2310 final long token = proto.start(fieldId);
Yi Jin163967f2018-03-15 13:49:44 -07002311 proto.write(PowerManagerProto.WakeLock.TAG, mTag);
2312 proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName);
2313 proto.write(PowerManagerProto.WakeLock.HELD, mHeld);
2314 proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount);
Yi Jin148d7f42017-11-28 14:23:56 -08002315 if (mWorkSource != null) {
Yi Jin163967f2018-03-15 13:49:44 -07002316 mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE);
Yi Jin148d7f42017-11-28 14:23:56 -08002317 }
2318 proto.end(token);
2319 }
2320 }
2321
Adrian Roos7445c0b2016-09-06 16:45:46 -07002322 /**
2323 * Wraps a Runnable such that this method immediately acquires the wake lock and then
2324 * once the Runnable is done the wake lock is released.
2325 *
2326 * <p>Example:
2327 *
2328 * <pre>
2329 * mHandler.post(mWakeLock.wrap(() -> {
2330 * // do things on handler, lock is held while we're waiting for this
2331 * // to get scheduled and until the runnable is done executing.
2332 * });
2333 * </pre>
2334 *
2335 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
2336 * leak the wakelock!
2337 *
2338 * @hide
2339 */
2340 public Runnable wrap(Runnable r) {
2341 acquire();
2342 return () -> {
2343 try {
2344 r.run();
2345 } finally {
2346 release();
2347 }
2348 };
2349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351}