blob: dd1f8c31ebb13525ebc045cd3e3762e57681adf1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Salvador Martinez812ea752018-10-19 13:03:20 -070019import android.Manifest.permission;
Wei Wang59476652018-11-29 16:02:48 -080020import android.annotation.CallbackExecutor;
Salvador Martineza6f7b252017-04-10 10:46:15 -070021import android.annotation.IntDef;
Wei Wang59476652018-11-29 16:02:48 -080022import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.RequiresPermission;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070024import android.annotation.SdkConstant;
Jeff Brown0a571122014-08-21 21:50:43 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Dan Gittik26b030d2018-04-16 18:50:10 +010027import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000028import android.annotation.UnsupportedAppUsage;
Jeff Brown96307042012-07-27 15:51:34 -070029import android.content.Context;
Oleg Kibirev2385b5e2018-11-13 10:43:07 -080030import android.service.dreams.Sandman;
Wei Wang59476652018-11-29 16:02:48 -080031import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
Yi Jin148d7f42017-11-28 14:23:56 -080033import android.util.proto.ProtoOutputStream;
Julius D'souzab22da802017-06-09 10:27:14 -070034
Wei Wang59476652018-11-29 16:02:48 -080035import com.android.internal.util.Preconditions;
36
Salvador Martineza6f7b252017-04-10 10:46:15 -070037import java.lang.annotation.Retention;
38import java.lang.annotation.RetentionPolicy;
Wei Wang59476652018-11-29 16:02:48 -080039import java.util.concurrent.Executor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
Jeff Brown1244cda2012-06-19 16:44:46 -070042 * This class gives you control of the power state of the device.
43 *
44 * <p>
45 * <b>Device battery life will be significantly affected by the use of this API.</b>
46 * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels
47 * possible, and be sure to release them as soon as possible.
48 * </p><p>
Jeff Brown1244cda2012-06-19 16:44:46 -070049 * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}.
50 * This will create a {@link PowerManager.WakeLock} object. You can then use methods
51 * on the wake lock object to control the power state of the device.
52 * </p><p>
53 * In practice it's quite simple:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * {@samplecode
55 * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
56 * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
57 * wl.acquire();
58 * ..screen will stay on during this section..
59 * wl.release();
60 * }
Jeff Brown1244cda2012-06-19 16:44:46 -070061 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -070062 * The following wake lock levels are defined, with varying effects on system power.
63 * <i>These levels are mutually exclusive - you may only specify one of them.</i>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070065 * <table>
Santos Cordon3107d292016-09-20 15:50:35 -070066 * <tr><th>Flag Value</th>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070069 * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070070 * <td>On*</td> <td>Off</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070072 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070073 * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070074 * <td>On</td> <td>Dim</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * </tr>
76 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070077 * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070078 * <td>On</td> <td>Bright</td> <td>Off</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -070080 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070081 * <tr><td>{@link #FULL_WAKE_LOCK}</td>
Santos Cordon3107d292016-09-20 15:50:35 -070082 * <td>On</td> <td>Bright</td> <td>Bright</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * </table>
Jeff Brown1244cda2012-06-19 16:44:46 -070085 * </p><p>
86 * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any
87 * display timeouts or the state of the screen and even after the user presses the power button.
88 * In all other wake locks, the CPU will run, but the user can still put the device to sleep
89 * using the power button.</i>
90 * </p><p>
91 * In addition, you can add two more flags, which affect behavior of the screen only.
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070092 * <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 -080093 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070094 * <table>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * <tr><th>Flag Value</th> <th>Description</th></tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -070097 * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause
99 * the illumination to remain on once it turns on (e.g. from user activity). This flag
100 * will force the screen and/or keyboard to turn on immediately, when the WakeLock is
101 * acquired. A typical use would be for notifications which are important for the user to
Santos Cordon3107d292016-09-20 15:50:35 -0700102 * see immediately.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * </tr>
Santos Cordon3107d292016-09-20 15:50:35 -0700104 *
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700105 * <tr><td>{@link #ON_AFTER_RELEASE}</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * <td>If this flag is set, the user activity timer will be reset when the WakeLock is
Santos Cordon3107d292016-09-20 15:50:35 -0700107 * released, causing the illumination to remain on a bit longer. This can be used to
108 * reduce flicker if you are cycling between wake lock conditions.</td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </tr>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * </table>
Dirk Dougherty7b9a2882012-10-28 12:07:08 -0700111 * <p>
Kenny Rootd710fb52011-03-15 17:39:45 -0700112 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +0000113 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -0700114 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600116@SystemService(Context.POWER_SERVICE)
Jeff Brown1244cda2012-06-19 16:44:46 -0700117public final class PowerManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static final String TAG = "PowerManager";
Jeff Brown1244cda2012-06-19 16:44:46 -0700119
Jeff Brown155fc702012-07-27 12:12:15 -0700120 /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few
121 * combinations were actually supported so the bit field was removed. This explains
122 * why the numbering scheme is so odd. If adding a new wake lock level, any unused
Bookatz1a1b0462018-01-12 11:47:03 -0800123 * value (in frameworks/base/core/proto/android/os/enums.proto) can be used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
126 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700127 * Wake lock level: Ensures that the CPU is running; the screen and keyboard
128 * backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700129 * <p>
130 * If the user presses the power button, then the screen will be turned off
131 * but the CPU will be kept on until all partial wake locks have been released.
Jeff Brown1244cda2012-06-19 16:44:46 -0700132 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 */
Bookatz1a1b0462018-01-12 11:47:03 -0800134 public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700137 * Wake lock level: Ensures that the screen is on (but may be dimmed);
Jeff Brown1244cda2012-06-19 16:44:46 -0700138 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700139 * <p>
140 * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be
141 * implicitly released by the system, causing both the screen and the CPU to be turned off.
142 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
143 * </p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700144 *
Dianne Hackborn9567a662011-04-19 18:44:03 -0700145 * @deprecated Most applications should use
146 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
147 * of this type of wake lock, as it will be correctly managed by the platform
148 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700150 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800151 public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
153 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700154 * Wake lock level: Ensures that the screen is on at full brightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * the keyboard backlight will be allowed to go off.
Jeff Brown155fc702012-07-27 12:12:15 -0700156 * <p>
157 * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be
158 * implicitly released by the system, causing both the screen and the CPU to be turned off.
159 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
160 * </p>
161 *
162 * @deprecated Most applications should use
163 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
164 * of this type of wake lock, as it will be correctly managed by the platform
165 * as the user moves between applications and doesn't require a special permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Jeff Brown155fc702012-07-27 12:12:15 -0700167 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800168 public static final int SCREEN_BRIGHT_WAKE_LOCK =
169 OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a
Jeff Brown155fc702012-07-27 12:12:15 -0700170
171 /**
172 * Wake lock level: Ensures that the screen and keyboard backlight are on at
173 * full brightness.
174 * <p>
175 * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be
176 * implicitly released by the system, causing both the screen and the CPU to be turned off.
177 * Contrast with {@link #PARTIAL_WAKE_LOCK}.
178 * </p>
179 *
180 * @deprecated Most applications should use
181 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
182 * of this type of wake lock, as it will be correctly managed by the platform
183 * as the user moves between applications and doesn't require a special permission.
184 */
185 @Deprecated
Bookatz1a1b0462018-01-12 11:47:03 -0800186 public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
188 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700189 * Wake lock level: Turns the screen off when the proximity sensor activates.
190 * <p>
Jeff Brown93cbbb22012-10-04 13:18:36 -0700191 * If the proximity sensor detects that an object is nearby, the screen turns off
192 * immediately. Shortly after the object moves away, the screen turns on again.
193 * </p><p>
194 * A proximity wake lock does not prevent the device from falling asleep
195 * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
196 * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
197 * wake locks are held, then the device will fall asleep (and lock) as usual.
198 * However, the device will not fall asleep while the screen has been turned off
199 * by the proximity sensor because it effectively counts as ongoing user activity.
200 * </p><p>
Jeff Brown96307042012-07-27 15:51:34 -0700201 * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
Jeff Brown1244cda2012-06-19 16:44:46 -0700202 * to determine whether this wake lock level is supported.
Craig Mautner6edb6db2012-11-20 18:21:12 -0800203 * </p><p>
204 * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}.
Jeff Brown1244cda2012-06-19 16:44:46 -0700205 * </p>
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700206 */
Bookatz1a1b0462018-01-12 11:47:03 -0800207 public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK =
208 OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700209
210 /**
Jeff Brown26875502014-01-30 21:47:47 -0800211 * Wake lock level: Put the screen in a low power state and allow the CPU to suspend
212 * if no other wake locks are held.
213 * <p>
214 * This is used by the dream manager to implement doze mode. It currently
215 * has no effect unless the power manager is in the dozing state.
Jeff Brown72671fb2014-08-21 21:41:09 -0700216 * </p><p>
217 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
Jeff Brown26875502014-01-30 21:47:47 -0800218 * </p>
219 *
220 * {@hide}
221 */
Bookatz1a1b0462018-01-12 11:47:03 -0800222 public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040
Jeff Brown26875502014-01-30 21:47:47 -0800223
224 /**
Jeff Brownc2932a12014-11-20 18:04:05 -0800225 * Wake lock level: Keep the device awake enough to allow drawing to occur.
226 * <p>
227 * This is used by the window manager to allow applications to draw while the
228 * system is dozing. It currently has no effect unless the power manager is in
229 * the dozing state.
230 * </p><p>
231 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
232 * </p>
233 *
234 * {@hide}
235 */
Bookatz1a1b0462018-01-12 11:47:03 -0800236 public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080
Jeff Brownc2932a12014-11-20 18:04:05 -0800237
238 /**
Jeff Brown155fc702012-07-27 12:12:15 -0700239 * Mask for the wake lock level component of a combined wake lock level and flags integer.
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500240 *
Jeff Brown155fc702012-07-27 12:12:15 -0700241 * @hide
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500242 */
Jeff Brown155fc702012-07-27 12:12:15 -0700243 public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff;
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500244
245 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700246 * Wake lock flag: Turn the screen on when the wake lock is acquired.
247 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * Normally wake locks don't actually wake the device, they just cause
Jeff Brown1244cda2012-06-19 16:44:46 -0700249 * the screen to remain on once it's already on. Think of the video player
250 * application as the normal behavior. Notifications that pop up and want
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * the device to be on are the exception; use this flag to be like them.
Jeff Brown1244cda2012-06-19 16:44:46 -0700252 * </p><p>
253 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
254 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
Jeff Brown155fc702012-07-27 12:12:15 -0700256 public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
258 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700259 * Wake lock flag: When this wake lock is released, poke the user activity timer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * so the screen stays on for a little longer.
261 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700262 * Will not turn the screen on if it is not already on.
263 * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.
264 * </p><p>
265 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
266 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
Jeff Brown155fc702012-07-27 12:12:15 -0700268 public static final int ON_AFTER_RELEASE = 0x20000000;
269
270 /**
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800271 * Wake lock flag: This wake lock is not important for logging events. If a later
272 * wake lock is acquired that is important, it will be considered the one to log.
273 * @hide
274 */
275 public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000;
276
277 /**
Jeff Browna71f03c2014-08-21 18:01:51 -0700278 * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a
279 * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor
280 * indicates that an object is not in close proximity.
Jeff Brown155fc702012-07-27 12:12:15 -0700281 */
Jeff Sharkey291c32a2017-07-05 12:34:04 -0600282 public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0;
283
284 /**
285 * Flag for {@link WakeLock#release(int)} when called due to timeout.
286 * @hide
287 */
288 public static final int RELEASE_FLAG_TIMEOUT = 1 << 16;
Jeff Brown7304c342012-05-11 18:42:42 -0700289
290 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700291 * Brightness value for fully on.
292 * @hide
293 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000294 @UnsupportedAppUsage
Jeff Brown7304c342012-05-11 18:42:42 -0700295 public static final int BRIGHTNESS_ON = 255;
296
297 /**
Jeff Brown7304c342012-05-11 18:42:42 -0700298 * Brightness value for fully off.
299 * @hide
300 */
301 public static final int BRIGHTNESS_OFF = 0;
302
Jeff Brown970d4132014-07-19 11:33:47 -0700303 /**
304 * Brightness value for default policy handling by the system.
305 * @hide
306 */
307 public static final int BRIGHTNESS_DEFAULT = -1;
308
Jeff Brownb696de52012-07-27 15:38:50 -0700309 // Note: Be sure to update android.os.BatteryStats and PowerManager.h
310 // if adding or modifying user activity event constants.
311
312 /**
313 * User activity event type: Unspecified event type.
314 * @hide
315 */
Jeff Brown0a571122014-08-21 21:50:43 -0700316 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700317 public static final int USER_ACTIVITY_EVENT_OTHER = 0;
318
319 /**
320 * User activity event type: Button or key pressed or released.
321 * @hide
322 */
Jeff Brown0a571122014-08-21 21:50:43 -0700323 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700324 public static final int USER_ACTIVITY_EVENT_BUTTON = 1;
325
326 /**
327 * User activity event type: Touch down, move or up.
328 * @hide
329 */
Jeff Brown0a571122014-08-21 21:50:43 -0700330 @SystemApi
Jeff Brownb696de52012-07-27 15:38:50 -0700331 public static final int USER_ACTIVITY_EVENT_TOUCH = 2;
332
Jeff Brown96307042012-07-27 15:51:34 -0700333 /**
Phil Weaverda80d672016-03-15 16:25:46 -0700334 * User activity event type: Accessibility taking action on behalf of user.
335 * @hide
336 */
337 @SystemApi
338 public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3;
339
340 /**
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800341 * User activity event type: {@link android.service.attention.AttentionService} taking action
342 * on behalf of user.
343 * @hide
344 */
345 public static final int USER_ACTIVITY_EVENT_ATTENTION = 4;
346
347 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700348 * User activity flag: If already dimmed, extend the dim timeout
349 * but do not brighten. This flag is useful for keeping the screen on
350 * a little longer without causing a visible change such as when
351 * the power key is pressed.
Jeff Brown96307042012-07-27 15:51:34 -0700352 * @hide
353 */
Jeff Brown0a571122014-08-21 21:50:43 -0700354 @SystemApi
Jeff Brown96307042012-07-27 15:51:34 -0700355 public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0;
356
357 /**
Jeff Brown0a571122014-08-21 21:50:43 -0700358 * User activity flag: Note the user activity as usual but do not
359 * reset the user activity timeout. This flag is useful for applying
360 * user activity power hints when interacting with the device indirectly
361 * on a secondary screen while allowing the primary screen to go to sleep.
362 * @hide
363 */
364 @SystemApi
365 public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1;
366
367 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000368 * @hide
369 */
370 public static final int GO_TO_SLEEP_REASON_MIN = 0;
371
372 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700373 * Go to sleep reason code: Going to sleep due by application request.
Jeff Brown96307042012-07-27 15:51:34 -0700374 * @hide
375 */
Santos Cordon12f92eb2019-02-01 21:28:47 +0000376 public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN;
Jeff Brown96307042012-07-27 15:51:34 -0700377
378 /**
379 * Go to sleep reason code: Going to sleep due by request of the
380 * device administration policy.
381 * @hide
382 */
383 public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1;
384
385 /**
386 * Go to sleep reason code: Going to sleep due to a screen timeout.
387 * @hide
388 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000389 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700390 public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2;
391
Doug Zongker3b0218b2014-01-14 12:29:06 -0800392 /**
Jeff Brownc12035c2014-08-13 18:52:25 -0700393 * Go to sleep reason code: Going to sleep due to the lid switch being closed.
394 * @hide
395 */
396 public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3;
397
398 /**
399 * Go to sleep reason code: Going to sleep due to the power button being pressed.
400 * @hide
401 */
402 public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4;
403
404 /**
405 * Go to sleep reason code: Going to sleep due to HDMI.
406 * @hide
407 */
408 public static final int GO_TO_SLEEP_REASON_HDMI = 5;
409
410 /**
Filip Gruszczynski9779e122015-03-13 17:39:31 -0700411 * Go to sleep reason code: Going to sleep due to the sleep button being pressed.
412 * @hide
413 */
414 public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6;
415
416 /**
Eugene Suslaf9a651d2017-10-11 12:06:27 -0700417 * Go to sleep reason code: Going to sleep by request of an accessibility service
418 * @hide
419 */
420 public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7;
421
422 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +0000423 * Go to sleep reason code: Going to sleep due to force-suspend.
424 * @hide
425 */
426 public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8;
427
428 /**
429 * @hide
430 */
431 public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_FORCE_SUSPEND;
432
433 /**
Calin Tatarua3805722018-08-09 16:41:28 +0200434 * @hide
435 */
436 public static String sleepReasonToString(int sleepReason) {
437 switch (sleepReason) {
438 case GO_TO_SLEEP_REASON_APPLICATION: return "application";
439 case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
440 case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
441 case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
442 case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
443 case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
444 case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
445 case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
Santos Cordon12f92eb2019-02-01 21:28:47 +0000446 case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
Calin Tatarua3805722018-08-09 16:41:28 +0200447 default: return Integer.toString(sleepReason);
448 }
449 }
450
451 /**
Jeff Brown72671fb2014-08-21 21:41:09 -0700452 * Go to sleep flag: Skip dozing state and directly go to full sleep.
453 * @hide
454 */
455 public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0;
456
457 /**
Michael Wrighte3001042019-02-05 00:13:14 +0000458 * @hide
459 */
460 @IntDef(prefix = { "WAKE_REASON_" }, value = {
461 WAKE_REASON_UNKNOWN,
462 WAKE_REASON_POWER_BUTTON,
463 WAKE_REASON_APPLICATION,
464 WAKE_REASON_PLUGGED_IN,
465 WAKE_REASON_GESTURE,
466 WAKE_REASON_CAMERA_LAUNCH,
467 WAKE_REASON_WAKE_KEY,
468 WAKE_REASON_WAKE_MOTION,
469 WAKE_REASON_HDMI,
470 })
471 @Retention(RetentionPolicy.SOURCE)
472 public @interface WakeReason{}
473
474 /**
475 * Wake up reason code: Waking for an unknown reason.
476 * @hide
477 */
478 public static final int WAKE_REASON_UNKNOWN = 0;
479
480 /**
481 * Wake up reason code: Waking up due to power button press.
482 * @hide
483 */
484 public static final int WAKE_REASON_POWER_BUTTON = 1;
485
486 /**
487 * Wake up reason code: Waking up because an application requested it.
488 * @hide
489 */
490 public static final int WAKE_REASON_APPLICATION = 2;
491
492 /**
493 * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger.
494 * @hide
495 */
496 public static final int WAKE_REASON_PLUGGED_IN = 3;
497
498 /**
499 * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the
500 * screen).
501 * @hide
502 */
503 public static final int WAKE_REASON_GESTURE = 4;
504
505 /**
506 * Wake up reason code: Waking up due to the camera being launched.
507 * @hide
508 */
509 public static final int WAKE_REASON_CAMERA_LAUNCH = 5;
510
511 /**
512 * Wake up reason code: Waking up because a wake key other than power was pressed.
513 * @hide
514 */
515 public static final int WAKE_REASON_WAKE_KEY = 6;
516
517 /**
518 * Wake up reason code: Waking up because a wake motion was performed.
519 *
520 * For example, a trackball that was set to wake the device up was spun.
521 * @hide
522 */
523 public static final int WAKE_REASON_WAKE_MOTION = 7;
524
525 /**
526 * Wake up reason code: Waking due to HDMI.
527 * @hide
528 */
529 public static final int WAKE_REASON_HDMI = 8;
530
531 /**
532 * Wake up reason code: Waking due to the lid being opened.
533 * @hide
534 */
535 public static final int WAKE_REASON_LID = 9;
536
537 /**
538 * Convert the wake reason to a string for debugging purposes.
539 * @hide
540 */
541 public static String wakeReasonToString(@WakeReason int wakeReason) {
542 switch (wakeReason) {
543 case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN";
544 case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON";
545 case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION";
546 case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN";
547 case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE";
548 case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH";
549 case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY";
550 case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION";
551 case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI";
552 case WAKE_REASON_LID: return "WAKE_REASON_LID";
553 default: return Integer.toString(wakeReason);
554 }
555 }
556
557 /**
Santos Cordon623526b2019-04-09 17:02:38 +0100558 * @hide
559 */
560 public static class WakeData {
561 public WakeData(long wakeTime, @WakeReason int wakeReason) {
562 this.wakeTime = wakeTime;
563 this.wakeReason = wakeReason;
564 }
565 public long wakeTime;
566 public @WakeReason int wakeReason;
567 }
568
569 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800570 * The value to pass as the 'reason' argument to reboot() to reboot into
571 * recovery mode for tasks other than applying system updates, such as
572 * doing factory resets.
Doug Zongker3b0218b2014-01-14 12:29:06 -0800573 * <p>
574 * Requires the {@link android.Manifest.permission#RECOVERY}
575 * permission (in addition to
576 * {@link android.Manifest.permission#REBOOT}).
577 * </p>
Doug Zongker183415e2014-08-12 10:18:40 -0700578 * @hide
Doug Zongker3b0218b2014-01-14 12:29:06 -0800579 */
580 public static final String REBOOT_RECOVERY = "recovery";
Yusuke Sato705ffd12015-07-21 15:52:11 -0700581
582 /**
Tao Baoe8a403d2015-12-31 07:44:55 -0800583 * The value to pass as the 'reason' argument to reboot() to reboot into
584 * recovery mode for applying system updates.
585 * <p>
586 * Requires the {@link android.Manifest.permission#RECOVERY}
587 * permission (in addition to
588 * {@link android.Manifest.permission#REBOOT}).
589 * </p>
590 * @hide
591 */
592 public static final String REBOOT_RECOVERY_UPDATE = "recovery-update";
593
594 /**
Mahaver Chopra1ce53bc2015-12-14 13:35:14 +0000595 * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on
596 * the device.
597 * @hide
598 */
599 public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner";
600
601 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700602 * The 'reason' value used when rebooting in safe mode
603 * @hide
604 */
605 public static final String REBOOT_SAFE_MODE = "safemode";
606
607 /**
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800608 * The 'reason' value used when rebooting the device without turning on the screen.
609 * @hide
610 */
611 public static final String REBOOT_QUIESCENT = "quiescent";
612
613 /**
Yusuke Sato705ffd12015-07-21 15:52:11 -0700614 * The value to pass as the 'reason' argument to android_reboot().
615 * @hide
616 */
617 public static final String SHUTDOWN_USER_REQUESTED = "userrequested";
618
Salvador Martineza6f7b252017-04-10 10:46:15 -0700619 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700620 * The value to pass as the 'reason' argument to android_reboot() when battery temperature
621 * is too high.
622 * @hide
623 */
624 public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery";
625
626 /**
Wei Wangbad7c202018-11-01 11:57:39 -0700627 * The value to pass as the 'reason' argument to android_reboot() when device temperature
628 * is too high.
629 * @hide
630 */
631 public static final String SHUTDOWN_THERMAL_STATE = "thermal";
632
633 /**
Sudheer Shanka292637f2017-09-25 10:36:23 -0700634 * The value to pass as the 'reason' argument to android_reboot() when device is running
635 * critically low on battery.
636 * @hide
637 */
638 public static final String SHUTDOWN_LOW_BATTERY = "battery";
639
640 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -0700641 * @hide
642 */
643 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700644 @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = {
Salvador Martineza6f7b252017-04-10 10:46:15 -0700645 SHUTDOWN_REASON_UNKNOWN,
646 SHUTDOWN_REASON_SHUTDOWN,
647 SHUTDOWN_REASON_REBOOT,
648 SHUTDOWN_REASON_USER_REQUESTED,
Sudheer Shanka292637f2017-09-25 10:36:23 -0700649 SHUTDOWN_REASON_THERMAL_SHUTDOWN,
650 SHUTDOWN_REASON_LOW_BATTERY,
651 SHUTDOWN_REASON_BATTERY_THERMAL
Salvador Martineza6f7b252017-04-10 10:46:15 -0700652 })
653 public @interface ShutdownReason {}
654
655 /**
656 * constant for shutdown reason being unknown.
657 * @hide
658 */
659 public static final int SHUTDOWN_REASON_UNKNOWN = 0;
660
661 /**
662 * constant for shutdown reason being normal shutdown.
663 * @hide
664 */
665 public static final int SHUTDOWN_REASON_SHUTDOWN = 1;
666
667 /**
668 * constant for shutdown reason being reboot.
669 * @hide
670 */
671 public static final int SHUTDOWN_REASON_REBOOT = 2;
672
673 /**
674 * constant for shutdown reason being user requested.
675 * @hide
676 */
677 public static final int SHUTDOWN_REASON_USER_REQUESTED = 3;
678
679 /**
680 * constant for shutdown reason being overheating.
681 * @hide
682 */
683 public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4;
684
Sudheer Shanka292637f2017-09-25 10:36:23 -0700685 /**
686 * constant for shutdown reason being low battery.
687 * @hide
688 */
689 public static final int SHUTDOWN_REASON_LOW_BATTERY = 5;
690
691 /**
692 * constant for shutdown reason being critical battery thermal state.
693 * @hide
694 */
695 public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6;
696
Makoto Onuki2eccd022017-11-01 13:44:23 -0700697 /**
698 * @hide
699 */
700 @Retention(RetentionPolicy.SOURCE)
Kweku Adams731a1032019-02-04 14:05:41 -0800701 @IntDef({ServiceType.LOCATION,
Makoto Onuki2eccd022017-11-01 13:44:23 -0700702 ServiceType.VIBRATION,
703 ServiceType.ANIMATION,
704 ServiceType.FULL_BACKUP,
705 ServiceType.KEYVALUE_BACKUP,
706 ServiceType.NETWORK_FIREWALL,
707 ServiceType.SCREEN_BRIGHTNESS,
708 ServiceType.SOUND,
709 ServiceType.BATTERY_STATS,
Makoto Onukiaae89532017-11-08 14:32:03 -0800710 ServiceType.DATA_SAVER,
Makoto Onuki9be01402017-11-10 13:22:26 -0800711 ServiceType.FORCE_ALL_APPS_STANDBY,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800712 ServiceType.FORCE_BACKGROUND_CHECK,
Makoto Onukiaae89532017-11-08 14:32:03 -0800713 ServiceType.OPTIONAL_SENSORS,
Lucas Dupin92a62e52018-01-30 17:22:20 -0800714 ServiceType.AOD,
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700715 ServiceType.QUICK_DOZE,
Kweku Adams27c65cc2019-02-28 15:32:31 -0800716 ServiceType.NIGHT_MODE,
Makoto Onukiaae89532017-11-08 14:32:03 -0800717 })
Makoto Onuki2eccd022017-11-01 13:44:23 -0700718 public @interface ServiceType {
719 int NULL = 0;
Kweku Adams731a1032019-02-04 14:05:41 -0800720 int LOCATION = 1;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700721 int VIBRATION = 2;
722 int ANIMATION = 3;
723 int FULL_BACKUP = 4;
724 int KEYVALUE_BACKUP = 5;
725 int NETWORK_FIREWALL = 6;
726 int SCREEN_BRIGHTNESS = 7;
727 int SOUND = 8;
728 int BATTERY_STATS = 9;
729 int DATA_SAVER = 10;
Lucas Dupin92a62e52018-01-30 17:22:20 -0800730 int AOD = 14;
Makoto Onukiaae89532017-11-08 14:32:03 -0800731
732 /**
Makoto Onuki9be01402017-11-10 13:22:26 -0800733 * Whether to enable force-app-standby on all apps or not.
Makoto Onukiaae89532017-11-08 14:32:03 -0800734 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800735 int FORCE_ALL_APPS_STANDBY = 11;
Makoto Onukiaae89532017-11-08 14:32:03 -0800736
737 /**
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800738 * Whether to enable background check on all apps or not.
739 */
740 int FORCE_BACKGROUND_CHECK = 12;
741
742 /**
Makoto Onukiaae89532017-11-08 14:32:03 -0800743 * Whether to disable non-essential sensors. (e.g. edge sensors.)
744 */
745 int OPTIONAL_SENSORS = 13;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700746
747 /**
748 * Whether to go into Deep Doze as soon as the screen turns off or not.
749 */
750 int QUICK_DOZE = 15;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800751
752 /**
753 * Whether to enable night mode when battery saver is enabled.
754 */
755 int NIGHT_MODE = 16;
Makoto Onuki2eccd022017-11-01 13:44:23 -0700756 }
757
Makoto Onuki57f0f552017-12-11 12:22:18 -0800758 /**
759 * Either the location providers shouldn't be affected by battery saver,
760 * or battery saver is off.
761 */
762 public static final int LOCATION_MODE_NO_CHANGE = 0;
763
764 /**
765 * In this mode, the GPS based location provider should be disabled when battery saver is on and
766 * the device is non-interactive.
767 */
768 public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1;
769
770 /**
771 * All location providers should be disabled when battery saver is on and
772 * the device is non-interactive.
773 */
774 public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2;
775
776 /**
777 * In this mode, all the location providers will be kept available, but location fixes
778 * should only be provided to foreground apps.
779 */
780 public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
781
Kweku Adams4fb074e2019-02-01 16:03:27 -0800782 /**
783 * In this mode, location will not be turned off, but LocationManager will throttle all
784 * requests to providers when the device is non-interactive.
785 */
786 public static final int LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF = 4;
787
Kweku Adamsc1d844a2019-03-28 16:05:00 -0700788 /** @hide */
789 public static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
790 /** @hide */
791 public static final int MAX_LOCATION_MODE = LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF;
Kweku Adams9f488e22019-01-14 16:25:08 -0800792
Makoto Onuki57f0f552017-12-11 12:22:18 -0800793 /**
794 * @hide
795 */
796 @Retention(RetentionPolicy.SOURCE)
797 @IntDef(prefix = {"LOCATION_MODE_"}, value = {
798 LOCATION_MODE_NO_CHANGE,
799 LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF,
800 LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
801 LOCATION_MODE_FOREGROUND_ONLY,
Kweku Adams4fb074e2019-02-01 16:03:27 -0800802 LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF,
Makoto Onuki57f0f552017-12-11 12:22:18 -0800803 })
804 public @interface LocationPowerSaveMode {}
805
Kweku Adams4fb074e2019-02-01 16:03:27 -0800806 /** @hide */
807 public static String locationPowerSaveModeToString(@LocationPowerSaveMode int mode) {
808 switch (mode) {
809 case LOCATION_MODE_NO_CHANGE:
810 return "NO_CHANGE";
811 case LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF:
812 return "GPS_DISABLED_WHEN_SCREEN_OFF";
813 case LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF:
814 return "ALL_DISABLED_WHEN_SCREEN_OFF";
815 case LOCATION_MODE_FOREGROUND_ONLY:
816 return "FOREGROUND_ONLY";
817 case LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF:
818 return "THROTTLE_REQUESTS_WHEN_SCREEN_OFF";
819 default:
820 return Integer.toString(mode);
821 }
822 }
823
Jeff Brown96307042012-07-27 15:51:34 -0700824 final Context mContext;
Andrei Onea24ec3212019-03-15 17:35:05 +0000825 @UnsupportedAppUsage
Jeff Brown1244cda2012-06-19 16:44:46 -0700826 final IPowerManager mService;
Artur Satayev70507ed2019-07-29 13:18:27 +0100827 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Jeff Brown1244cda2012-06-19 16:44:46 -0700828 final Handler mHandler;
829
Makoto Onuki152742f2019-10-31 17:22:26 -0700830 /** We lazily initialize it.*/
831 private DeviceIdleManager mDeviceIdleManager;
832
Wei Wang59476652018-11-29 16:02:48 -0800833 IThermalService mThermalService;
Wei Wang7a49ff62019-03-04 21:13:32 -0800834 private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener>
835 mListenerMap = new ArrayMap<>();
Wei Wang59476652018-11-29 16:02:48 -0800836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700838 * {@hide}
839 */
Jeff Brown96307042012-07-27 15:51:34 -0700840 public PowerManager(Context context, IPowerManager service, Handler handler) {
841 mContext = context;
Jeff Brown1244cda2012-06-19 16:44:46 -0700842 mService = service;
843 mHandler = handler;
844 }
845
Makoto Onuki152742f2019-10-31 17:22:26 -0700846 private DeviceIdleManager getDeviceIdleManager() {
847 if (mDeviceIdleManager == null) {
848 // No need for synchronization; getSystemService() will return the same object anyway.
849 mDeviceIdleManager = mContext.getSystemService(DeviceIdleManager.class);
850 }
851 return mDeviceIdleManager;
852 }
853
Jeff Brown1244cda2012-06-19 16:44:46 -0700854 /**
Jeff Brown96307042012-07-27 15:51:34 -0700855 * Gets the minimum supported screen brightness setting.
856 * The screen may be allowed to become dimmer than this value but
857 * this is the minimum value that can be set by the user.
858 * @hide
859 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000860 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700861 public int getMinimumScreenBrightnessSetting() {
862 return mContext.getResources().getInteger(
Jeff Brownf9bba132012-08-21 22:04:02 -0700863 com.android.internal.R.integer.config_screenBrightnessSettingMinimum);
Jeff Brown96307042012-07-27 15:51:34 -0700864 }
865
866 /**
867 * Gets the maximum supported screen brightness setting.
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 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000872 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700873 public int getMaximumScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700874 return mContext.getResources().getInteger(
875 com.android.internal.R.integer.config_screenBrightnessSettingMaximum);
Jeff Brown96307042012-07-27 15:51:34 -0700876 }
877
878 /**
879 * Gets the default screen brightness setting.
880 * @hide
881 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000882 @UnsupportedAppUsage
Jeff Brown96307042012-07-27 15:51:34 -0700883 public int getDefaultScreenBrightnessSetting() {
Jeff Brownf9bba132012-08-21 22:04:02 -0700884 return mContext.getResources().getInteger(
885 com.android.internal.R.integer.config_screenBrightnessSettingDefault);
Jeff Brown96307042012-07-27 15:51:34 -0700886 }
887
888 /**
Santos Cordon3107d292016-09-20 15:50:35 -0700889 * Gets the minimum supported screen brightness setting for VR Mode.
890 * @hide
891 */
892 public int getMinimumScreenBrightnessForVrSetting() {
893 return mContext.getResources().getInteger(
894 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum);
895 }
896
897 /**
898 * Gets the maximum supported screen brightness setting for VR Mode.
899 * The screen may be allowed to become dimmer than this value but
900 * this is the maximum value that can be set by the user.
901 * @hide
902 */
903 public int getMaximumScreenBrightnessForVrSetting() {
904 return mContext.getResources().getInteger(
905 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum);
906 }
907
908 /**
909 * Gets the default screen brightness for VR setting.
910 * @hide
911 */
912 public int getDefaultScreenBrightnessForVrSetting() {
913 return mContext.getResources().getInteger(
914 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
915 }
916
917 /**
Jeff Brown1244cda2012-06-19 16:44:46 -0700918 * Creates a new wake lock with the specified level and flags.
Kenny Rootd710fb52011-03-15 17:39:45 -0700919 * <p>
Jeff Brown1244cda2012-06-19 16:44:46 -0700920 * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags
921 * combined using the logical OR operator.
922 * </p><p>
923 * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK},
924 * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK}
925 * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be
926 * specified as part of the {@code levelAndFlags} parameter.
927 * </p><p>
928 * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP}
929 * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the
930 * {@code levelAndFlags} parameters.
931 * </p><p>
932 * Call {@link WakeLock#acquire() acquire()} on the object to acquire the
933 * wake lock, and {@link WakeLock#release release()} when you are done.
934 * </p><p>
935 * {@samplecode
936 * PowerManager pm = (PowerManager)mContext.getSystemService(
937 * Context.POWER_SERVICE);
938 * PowerManager.WakeLock wl = pm.newWakeLock(
939 * PowerManager.SCREEN_DIM_WAKE_LOCK
940 * | PowerManager.ON_AFTER_RELEASE,
941 * TAG);
942 * wl.acquire();
943 * // ... do work...
944 * wl.release();
945 * }
946 * </p><p>
947 * Although a wake lock can be created without special permissions,
948 * the {@link android.Manifest.permission#WAKE_LOCK} permission is
949 * required to actually acquire or release the wake lock that is returned.
950 * </p><p class="note">
951 * If using this to keep the screen on, you should strongly consider using
952 * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead.
953 * This window flag will be correctly managed by the platform
954 * as the user moves between applications and doesn't require a special permission.
955 * </p>
956 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +0000957 * <p>
958 * Recommended naming conventions for tags to make debugging easier:
959 * <ul>
960 * <li>use a unique prefix delimited by a colon for your app/library (e.g.
961 * gmail:mytag) to make it easier to understand where the wake locks comes
962 * from. This namespace will also avoid collision for tags inside your app
963 * coming from different libraries which will make debugging easier.
964 * <li>use constants (e.g. do not include timestamps in the tag) to make it
965 * easier for tools to aggregate similar wake locks. When collecting
966 * debugging data, the platform only monitors a finite number of tags,
967 * using constants will help tools to provide better debugging data.
968 * <li>avoid using Class#getName() or similar method since this class name
969 * can be transformed by java optimizer and obfuscator tools.
970 * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock
971 * tags from the platform (e.g. *alarm*).
972 * <li>never include personnally identifiable information for privacy
973 * reasons.
974 * </ul>
975 * </p>
976 *
Jeff Brown1244cda2012-06-19 16:44:46 -0700977 * @param levelAndFlags Combination of wake lock level and flag values defining
978 * the requested behavior of the WakeLock.
979 * @param tag Your class name (or other tag) for debugging purposes.
980 *
981 * @see WakeLock#acquire()
982 * @see WakeLock#release()
983 * @see #PARTIAL_WAKE_LOCK
984 * @see #FULL_WAKE_LOCK
985 * @see #SCREEN_DIM_WAKE_LOCK
986 * @see #SCREEN_BRIGHT_WAKE_LOCK
Jeff Browna71f03c2014-08-21 18:01:51 -0700987 * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK
Jeff Brown1244cda2012-06-19 16:44:46 -0700988 * @see #ACQUIRE_CAUSES_WAKEUP
989 * @see #ON_AFTER_RELEASE
990 */
991 public WakeLock newWakeLock(int levelAndFlags, String tag) {
Jeff Brown155fc702012-07-27 12:12:15 -0700992 validateWakeLockParameters(levelAndFlags, tag);
Dianne Hackborn95d78532013-09-11 09:51:14 -0700993 return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName());
Jeff Brown155fc702012-07-27 12:12:15 -0700994 }
995
996 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000997 @UnsupportedAppUsage
Jeff Brown155fc702012-07-27 12:12:15 -0700998 public static void validateWakeLockParameters(int levelAndFlags, String tag) {
999 switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) {
1000 case PARTIAL_WAKE_LOCK:
1001 case SCREEN_DIM_WAKE_LOCK:
1002 case SCREEN_BRIGHT_WAKE_LOCK:
1003 case FULL_WAKE_LOCK:
1004 case PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -08001005 case DOZE_WAKE_LOCK:
Jeff Brownc2932a12014-11-20 18:04:05 -08001006 case DRAW_WAKE_LOCK:
Jeff Brown155fc702012-07-27 12:12:15 -07001007 break;
1008 default:
1009 throw new IllegalArgumentException("Must specify a valid wake lock level.");
Jeff Brown1244cda2012-06-19 16:44:46 -07001010 }
1011 if (tag == null) {
1012 throw new IllegalArgumentException("The tag must not be null.");
1013 }
Jeff Brown1244cda2012-06-19 16:44:46 -07001014 }
1015
1016 /**
1017 * Notifies the power manager that user activity happened.
1018 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001019 * Resets the auto-off timer and brightens the screen if the device
1020 * is not asleep. This is what happens normally when a key or the touch
1021 * screen is pressed or when some other user activity occurs.
1022 * This method does not wake up the device if it has been put to sleep.
Jeff Brown1244cda2012-06-19 16:44:46 -07001023 * </p><p>
1024 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1025 * </p>
1026 *
1027 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
Jeff Brown62c82e42012-09-26 01:30:41 -07001028 * time base. This timestamp is used to correctly order the user activity request with
Jeff Brown1244cda2012-06-19 16:44:46 -07001029 * other power management functions. It should be set
1030 * to the timestamp of the input event that caused the user activity.
1031 * @param noChangeLights If true, does not cause the keyboard backlight to turn on
1032 * because of this event. This is set when the power key is pressed.
1033 * We want the device to stay on while the button is down, but we're about
1034 * to turn off the screen so we don't want the keyboard backlight to turn on again.
1035 * Otherwise the lights flash on and then off and it looks weird.
Jeff Brown96307042012-07-27 15:51:34 -07001036 *
1037 * @see #wakeUp
1038 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001039 *
1040 * @removed Requires signature or system permission.
1041 * @deprecated Use {@link #userActivity(long, int, int)}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001042 */
Jeff Brown7d827512014-08-21 21:56:02 -07001043 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001044 public void userActivity(long when, boolean noChangeLights) {
Jeff Brown0a571122014-08-21 21:50:43 -07001045 userActivity(when, USER_ACTIVITY_EVENT_OTHER,
1046 noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0);
1047 }
1048
1049 /**
1050 * Notifies the power manager that user activity happened.
1051 * <p>
1052 * Resets the auto-off timer and brightens the screen if the device
1053 * is not asleep. This is what happens normally when a key or the touch
1054 * screen is pressed or when some other user activity occurs.
1055 * This method does not wake up the device if it has been put to sleep.
1056 * </p><p>
1057 * Requires the {@link android.Manifest.permission#DEVICE_POWER} or
1058 * {@link android.Manifest.permission#USER_ACTIVITY} permission.
1059 * </p>
1060 *
1061 * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()}
1062 * time base. This timestamp is used to correctly order the user activity request with
1063 * other power management functions. It should be set
1064 * to the timestamp of the input event that caused the user activity.
1065 * @param event The user activity event.
1066 * @param flags Optional user activity flags.
1067 *
1068 * @see #wakeUp
1069 * @see #goToSleep
1070 *
1071 * @hide Requires signature or system permission.
1072 */
1073 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001074 @RequiresPermission(anyOf = {
1075 android.Manifest.permission.DEVICE_POWER,
1076 android.Manifest.permission.USER_ACTIVITY
1077 })
Jeff Brown0a571122014-08-21 21:50:43 -07001078 public void userActivity(long when, int event, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001079 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001080 mService.userActivity(when, event, flags);
Jeff Brown1244cda2012-06-19 16:44:46 -07001081 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001082 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001083 }
1084 }
1085
1086 /**
1087 * Forces the device to go to sleep.
1088 * <p>
Jeff Brown96307042012-07-27 15:51:34 -07001089 * Overrides all the wake locks that are held.
1090 * This is what happens when the power key is pressed to turn off the screen.
Jeff Brown1244cda2012-06-19 16:44:46 -07001091 * </p><p>
1092 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1093 * </p>
1094 *
1095 * @param time The time when the request to go to sleep was issued, in the
1096 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001097 * order the go to sleep request with other power management functions. It should be set
Jeff Brown1244cda2012-06-19 16:44:46 -07001098 * to the timestamp of the input event that caused the request to go to sleep.
Jeff Brown96307042012-07-27 15:51:34 -07001099 *
1100 * @see #userActivity
1101 * @see #wakeUp
Jeff Brown7d827512014-08-21 21:56:02 -07001102 *
1103 * @removed Requires signature permission.
Jeff Brown1244cda2012-06-19 16:44:46 -07001104 */
1105 public void goToSleep(long time) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001106 goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001107 }
1108
1109 /**
Jeff Brown7d827512014-08-21 21:56:02 -07001110 * Forces the device to go to sleep.
1111 * <p>
1112 * Overrides all the wake locks that are held.
1113 * This is what happens when the power key is pressed to turn off the screen.
1114 * </p><p>
1115 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1116 * </p>
1117 *
1118 * @param time The time when the request to go to sleep was issued, in the
1119 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1120 * order the go to sleep request with other power management functions. It should be set
1121 * to the timestamp of the input event that caused the request to go to sleep.
1122 * @param reason The reason the device is going to sleep.
1123 * @param flags Optional flags to apply when going to sleep.
1124 *
1125 * @see #userActivity
1126 * @see #wakeUp
1127 *
1128 * @hide Requires signature permission.
Jeff Brown6d8fd272014-05-20 21:24:38 -07001129 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001130 @UnsupportedAppUsage
Jeff Brown6d8fd272014-05-20 21:24:38 -07001131 public void goToSleep(long time, int reason, int flags) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001132 try {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001133 mService.goToSleep(time, reason, flags);
Jeff Brown96307042012-07-27 15:51:34 -07001134 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001135 throw e.rethrowFromSystemServer();
Jeff Brown96307042012-07-27 15:51:34 -07001136 }
1137 }
1138
1139 /**
1140 * Forces the device to wake up from sleep.
1141 * <p>
1142 * If the device is currently asleep, wakes it up, otherwise does nothing.
1143 * This is what happens when the power key is pressed to turn on the screen.
1144 * </p><p>
1145 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1146 * </p>
1147 *
1148 * @param time The time when the request to wake up was issued, in the
1149 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
Jeff Brown62c82e42012-09-26 01:30:41 -07001150 * order the wake up request with other power management functions. It should be set
Jeff Brown96307042012-07-27 15:51:34 -07001151 * to the timestamp of the input event that caused the request to wake up.
1152 *
1153 * @see #userActivity
1154 * @see #goToSleep
Jeff Brown7d827512014-08-21 21:56:02 -07001155 *
Michael Wrighte3001042019-02-05 00:13:14 +00001156 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Jeff Brown7d827512014-08-21 21:56:02 -07001157 * @removed Requires signature permission.
Jeff Brown96307042012-07-27 15:51:34 -07001158 */
Michael Wrighte3001042019-02-05 00:13:14 +00001159 @Deprecated
Jeff Brown96307042012-07-27 15:51:34 -07001160 public void wakeUp(long time) {
Michael Wrighte3001042019-02-05 00:13:14 +00001161 wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp");
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001162 }
1163
1164 /**
Michael Wrighte3001042019-02-05 00:13:14 +00001165 * Forces the device to wake up from sleep.
1166 * <p>
1167 * If the device is currently asleep, wakes it up, otherwise does nothing.
1168 * This is what happens when the power key is pressed to turn on the screen.
1169 * </p><p>
1170 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1171 * </p>
1172 *
1173 * @param time The time when the request to wake up was issued, in the
1174 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1175 * order the wake up request with other power management functions. It should be set
1176 * to the timestamp of the input event that caused the request to wake up.
1177 *
1178 * @param details A free form string to explain the specific details behind the wake up for
1179 * debugging purposes.
1180 *
1181 * @see #userActivity
1182 * @see #goToSleep
1183 *
1184 * @deprecated Use {@link #wakeUp(long, int, String)} instead.
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001185 * @hide
1186 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001187 @UnsupportedAppUsage
Michael Wrighte3001042019-02-05 00:13:14 +00001188 @Deprecated
1189 public void wakeUp(long time, String details) {
1190 wakeUp(time, WAKE_REASON_UNKNOWN, details);
1191 }
1192
1193 /**
1194 * Forces the device to wake up from sleep.
1195 * <p>
1196 * If the device is currently asleep, wakes it up, otherwise does nothing.
1197 * This is what happens when the power key is pressed to turn on the screen.
1198 * </p><p>
1199 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1200 * </p>
1201 *
1202 * @param time The time when the request to wake up was issued, in the
1203 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1204 * order the wake up request with other power management functions. It should be set
1205 * to the timestamp of the input event that caused the request to wake up.
1206 *
1207 * @param reason The reason for the wake up.
1208 *
1209 * @param details A free form string to explain the specific details behind the wake up for
1210 * debugging purposes.
1211 *
1212 * @see #userActivity
1213 * @see #goToSleep
1214 * @hide
1215 */
1216 public void wakeUp(long time, @WakeReason int reason, String details) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001217 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001218 mService.wakeUp(time, reason, details, mContext.getOpPackageName());
Jeff Brown1244cda2012-06-19 16:44:46 -07001219 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001220 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001221 }
1222 }
1223
1224 /**
Jeff Brown62c82e42012-09-26 01:30:41 -07001225 * Forces the device to start napping.
1226 * <p>
1227 * If the device is currently awake, starts dreaming, otherwise does nothing.
1228 * When the dream ends or if the dream cannot be started, the device will
1229 * either wake up or go to sleep depending on whether there has been recent
1230 * user activity.
1231 * </p><p>
1232 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1233 * </p>
1234 *
1235 * @param time The time when the request to nap was issued, in the
1236 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1237 * order the nap request with other power management functions. It should be set
1238 * to the timestamp of the input event that caused the request to nap.
1239 *
1240 * @see #wakeUp
1241 * @see #goToSleep
1242 *
Jeff Brown7d827512014-08-21 21:56:02 -07001243 * @hide Requires signature permission.
Jeff Brown62c82e42012-09-26 01:30:41 -07001244 */
1245 public void nap(long time) {
1246 try {
1247 mService.nap(time);
1248 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001249 throw e.rethrowFromSystemServer();
Jeff Brown62c82e42012-09-26 01:30:41 -07001250 }
1251 }
1252
1253 /**
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001254 * Requests the device to start dreaming.
1255 * <p>
1256 * If dream can not be started, for example if another {@link PowerManager} transition is in
1257 * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when
1258 * dream ends.
1259 * </p><p>
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001260 * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and
1261 * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions.
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001262 * </p>
1263 *
1264 * @param time The time when the request to nap was issued, in the
1265 * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly
1266 * order the dream request with other power management functions. It should be set
1267 * to the timestamp of the input event that caused the request to dream.
1268 *
1269 * @hide
1270 */
1271 @SystemApi
Oleg Kibirevc9752eb2018-11-27 13:26:11 -08001272 @RequiresPermission(allOf = {
1273 android.Manifest.permission.READ_DREAM_STATE,
1274 android.Manifest.permission.WRITE_DREAM_STATE })
Oleg Kibirev2385b5e2018-11-13 10:43:07 -08001275 public void dream(long time) {
1276 Sandman.startDreamByUserRequest(mContext);
1277 }
1278
1279 /**
Jeff Browne333e672014-10-28 13:48:55 -07001280 * Boosts the brightness of the screen to maximum for a predetermined
1281 * period of time. This is used to make the screen more readable in bright
1282 * daylight for a short duration.
1283 * <p>
1284 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1285 * </p>
1286 *
1287 * @param time The time when the request to boost was issued, in the
1288 * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
1289 * order the boost request with other power management functions. It should be set
1290 * to the timestamp of the input event that caused the request to boost.
1291 *
1292 * @hide Requires signature permission.
1293 */
1294 public void boostScreenBrightness(long time) {
1295 try {
1296 mService.boostScreenBrightness(time);
1297 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001298 throw e.rethrowFromSystemServer();
Jeff Browne333e672014-10-28 13:48:55 -07001299 }
1300 }
1301
Jeff Brown1244cda2012-06-19 16:44:46 -07001302 /**
Jeff Brown96307042012-07-27 15:51:34 -07001303 * Returns true if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001304 *
Jeff Brown96307042012-07-27 15:51:34 -07001305 * @param level The wake lock level to check.
1306 * @return True if the specified wake lock level is supported.
Jeff Brown1244cda2012-06-19 16:44:46 -07001307 */
Jeff Brown96307042012-07-27 15:51:34 -07001308 public boolean isWakeLockLevelSupported(int level) {
Jeff Brown1244cda2012-06-19 16:44:46 -07001309 try {
Jeff Brown96307042012-07-27 15:51:34 -07001310 return mService.isWakeLockLevelSupported(level);
Jeff Brown1244cda2012-06-19 16:44:46 -07001311 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001312 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001313 }
1314 }
1315
1316 /**
Jeff Brown037c33e2014-04-09 00:31:55 -07001317 * Returns true if the device is in an interactive state.
Jeff Brown1244cda2012-06-19 16:44:46 -07001318 * <p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001319 * For historical reasons, the name of this method refers to the power state of
1320 * the screen but it actually describes the overall interactive state of
1321 * the device. This method has been replaced by {@link #isInteractive}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001322 * </p><p>
Jeff Brown037c33e2014-04-09 00:31:55 -07001323 * The value returned by this method only indicates whether the device is
1324 * in an interactive state which may have nothing to do with the screen being
1325 * on or off. To determine the actual state of the screen,
1326 * use {@link android.view.Display#getState}.
Jeff Brown1244cda2012-06-19 16:44:46 -07001327 * </p>
1328 *
Jeff Brown037c33e2014-04-09 00:31:55 -07001329 * @return True if the device is in an interactive state.
1330 *
1331 * @deprecated Use {@link #isInteractive} instead.
Jeff Brown1244cda2012-06-19 16:44:46 -07001332 */
Jeff Brown037c33e2014-04-09 00:31:55 -07001333 @Deprecated
Jeff Brown1244cda2012-06-19 16:44:46 -07001334 public boolean isScreenOn() {
Jeff Brown037c33e2014-04-09 00:31:55 -07001335 return isInteractive();
1336 }
1337
1338 /**
1339 * Returns true if the device is in an interactive state.
1340 * <p>
1341 * When this method returns true, the device is awake and ready to interact
1342 * with the user (although this is not a guarantee that the user is actively
1343 * interacting with the device just this moment). The main screen is usually
1344 * turned on while in this state. Certain features, such as the proximity
1345 * sensor, may temporarily turn off the screen while still leaving the device in an
1346 * interactive state. Note in particular that the device is still considered
1347 * to be interactive while dreaming (since dreams can be interactive) but not
1348 * when it is dozing or asleep.
1349 * </p><p>
1350 * When this method returns false, the device is dozing or asleep and must
1351 * be awoken before it will become ready to interact with the user again. The
1352 * main screen is usually turned off while in this state. Certain features,
1353 * such as "ambient mode" may cause the main screen to remain on (albeit in a
1354 * low power state) to display system-provided content while the device dozes.
1355 * </p><p>
1356 * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on}
1357 * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast
1358 * whenever the interactive state of the device changes. For historical reasons,
1359 * the names of these broadcasts refer to the power state of the screen
1360 * but they are actually sent in response to changes in the overall interactive
1361 * state of the device, as described by this method.
1362 * </p><p>
1363 * Services may use the non-interactive state as a hint to conserve power
1364 * since the user is not present.
1365 * </p>
1366 *
1367 * @return True if the device is in an interactive state.
1368 *
1369 * @see android.content.Intent#ACTION_SCREEN_ON
1370 * @see android.content.Intent#ACTION_SCREEN_OFF
1371 */
1372 public boolean isInteractive() {
Jeff Brown1244cda2012-06-19 16:44:46 -07001373 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07001374 return mService.isInteractive();
Jeff Brown1244cda2012-06-19 16:44:46 -07001375 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001376 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001377 }
1378 }
1379
1380 /**
1381 * Reboot the device. Will not return if the reboot is successful.
1382 * <p>
1383 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1384 * </p>
1385 *
1386 * @param reason code to pass to the kernel (e.g., "recovery") to
1387 * request special boot modes, or null.
1388 */
1389 public void reboot(String reason) {
1390 try {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07001391 mService.reboot(false, reason, true);
Jeff Brown1244cda2012-06-19 16:44:46 -07001392 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001393 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07001394 }
1395 }
1396
1397 /**
Tony Mantlerb8009fd2016-03-14 15:55:35 -07001398 * Reboot the device. Will not return if the reboot is successful.
1399 * <p>
1400 * Requires the {@link android.Manifest.permission#REBOOT} permission.
1401 * </p>
1402 * @hide
1403 */
1404 public void rebootSafeMode() {
1405 try {
1406 mService.rebootSafeMode(false, true);
1407 } catch (RemoteException e) {
1408 throw e.rethrowFromSystemServer();
1409 }
1410 }
1411
1412 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001413 * Returns true if the device is currently in power save mode. When in this mode,
1414 * applications should reduce their functionality in order to conserve battery as
1415 * much as possible. You can monitor for changes to this state with
1416 * {@link #ACTION_POWER_SAVE_MODE_CHANGED}.
1417 *
1418 * @return Returns true if currently in low power mode, else false.
1419 */
1420 public boolean isPowerSaveMode() {
1421 try {
1422 return mService.isPowerSaveMode();
1423 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001424 throw e.rethrowFromSystemServer();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001425 }
1426 }
1427
1428 /**
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001429 * Set the current power save mode.
1430 *
1431 * @return True if the set was allowed.
1432 *
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001433 * @hide
Salvador Martineza80bbab2018-09-24 10:36:11 -07001434 * @see #isPowerSaveMode()
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001435 */
Salvador Martineza80bbab2018-09-24 10:36:11 -07001436 @SystemApi
1437 @TestApi
1438 @RequiresPermission(anyOf = {
1439 android.Manifest.permission.DEVICE_POWER,
1440 android.Manifest.permission.POWER_SAVER
1441 })
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001442 public boolean setPowerSaveModeEnabled(boolean mode) {
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001443 try {
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07001444 return mService.setPowerSaveModeEnabled(mode);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001445 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001446 throw e.rethrowFromSystemServer();
John Spurlock8d4e6cb2014-09-14 11:10:22 -04001447 }
1448 }
1449
1450 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001451 * Updates the current state of dynamic power savings and disable threshold. This is
1452 * a signal to the system which an app can update to serve as an indicator that
1453 * the user will be in a battery critical situation before being able to plug in.
1454 * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
1455 * This is a device global state, not a per user setting.
1456 *
1457 * <p>When enabled, the system may enact various measures for reducing power consumption in
1458 * order to help ensure that the user will make it to their next charging point. The most
1459 * visible of these will be the automatic enabling of battery saver if the user has set
1460 * their battery saver mode to "automatic". Note
1461 * that this is NOT simply an on/off switch for features, but rather a hint for the
1462 * system to consider enacting these power saving features, some of which have additional
1463 * logic around when to activate based on this signal.
1464 *
1465 * <p>The provided threshold is the percentage the system should consider itself safe at given
1466 * the current state of the device. The value is an integer representing a battery level.
1467 *
1468 * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
1469 * functionality so that the dynamic power savings itself remains a signal rather than becoming
1470 * an on/off switch for a subset of features.
1471 * @hide
1472 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001473 * @param powerSaveHint A signal indicating to the system if it believes the
Salvador Martinez812ea752018-10-19 13:03:20 -07001474 * dynamic power savings behaviors should be activated.
1475 * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
1476 * power savings behaviors.
1477 * @return True if the update was allowed and succeeded.
1478 *
1479 * @hide
1480 */
1481 @SystemApi
1482 @TestApi
1483 @RequiresPermission(permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001484 public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) {
Salvador Martinez812ea752018-10-19 13:03:20 -07001485 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001486 return mService.setDynamicPowerSaveHint(powerSaveHint, disableThreshold);
Salvador Martinez812ea752018-10-19 13:03:20 -07001487 } catch (RemoteException e) {
1488 throw e.rethrowFromSystemServer();
1489 }
1490 }
1491
1492 /**
Kweku Adams9f488e22019-01-14 16:25:08 -08001493 * Sets the policy for adaptive power save.
1494 *
1495 * @return true if there was an effectual change. If full battery saver is enabled or the
1496 * adaptive policy is not enabled, then this will return false.
1497 *
1498 * @hide
1499 */
1500 @SystemApi
1501 @RequiresPermission(anyOf = {
1502 android.Manifest.permission.DEVICE_POWER,
1503 android.Manifest.permission.POWER_SAVER
1504 })
1505 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
1506 try {
1507 return mService.setAdaptivePowerSavePolicy(config);
1508 } catch (RemoteException e) {
1509 throw e.rethrowFromSystemServer();
1510 }
1511 }
1512
1513 /**
1514 * Enables or disables adaptive power save.
1515 *
1516 * @return true if there was an effectual change. If full battery saver is enabled, then this
1517 * will return false.
1518 *
1519 * @hide
1520 */
1521 @SystemApi
1522 @RequiresPermission(anyOf = {
1523 android.Manifest.permission.DEVICE_POWER,
1524 android.Manifest.permission.POWER_SAVER
1525 })
1526 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
1527 try {
1528 return mService.setAdaptivePowerSaveEnabled(enabled);
1529 } catch (RemoteException e) {
1530 throw e.rethrowFromSystemServer();
1531 }
1532 }
1533
1534 /**
Salvador Martinez812ea752018-10-19 13:03:20 -07001535 * Indicates automatic battery saver toggling by the system will be based on percentage.
1536 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001537 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001538 *
1539 * @hide
1540 */
1541 @SystemApi
1542 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001543 public static final int POWER_SAVE_MODE_TRIGGER_PERCENTAGE = 0;
Salvador Martinez812ea752018-10-19 13:03:20 -07001544
1545 /**
1546 * Indicates automatic battery saver toggling by the system will be based on the state
1547 * of the dynamic power savings signal.
1548 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001549 * @see PowerManager#setDynamicPowerSaveHint(boolean, int)
1550 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001551 *
1552 * @hide
1553 */
1554 @SystemApi
1555 @TestApi
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001556 public static final int POWER_SAVE_MODE_TRIGGER_DYNAMIC = 1;
Salvador Martinez812ea752018-10-19 13:03:20 -07001557
1558 /** @hide */
1559 @Retention(RetentionPolicy.SOURCE)
1560 @IntDef(value = {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001561 POWER_SAVE_MODE_TRIGGER_PERCENTAGE,
1562 POWER_SAVE_MODE_TRIGGER_DYNAMIC
Salvador Martinez812ea752018-10-19 13:03:20 -07001563
1564 })
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001565 public @interface AutoPowerSaveModeTriggers {}
Salvador Martinez812ea752018-10-19 13:03:20 -07001566
1567
1568 /**
1569 * Returns the current battery saver control mode. Values it may return are defined in
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001570 * AutoPowerSaveModeTriggers. Note that this is a global device state, not a per user setting.
Salvador Martinez812ea752018-10-19 13:03:20 -07001571 *
1572 * @return The current value power saver mode for the system.
1573 *
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001574 * @see AutoPowerSaveModeTriggers
1575 * @see PowerManager#getPowerSaveModeTrigger()
Salvador Martinez812ea752018-10-19 13:03:20 -07001576 * @hide
1577 */
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001578 @AutoPowerSaveModeTriggers
Salvador Martinez812ea752018-10-19 13:03:20 -07001579 @SystemApi
1580 @TestApi
1581 @RequiresPermission(android.Manifest.permission.POWER_SAVER)
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001582 public int getPowerSaveModeTrigger() {
Salvador Martinez812ea752018-10-19 13:03:20 -07001583 try {
Salvador Martinezb85a9f82019-03-20 16:21:27 -07001584 return mService.getPowerSaveModeTrigger();
Salvador Martinez812ea752018-10-19 13:03:20 -07001585 } catch (RemoteException e) {
1586 throw e.rethrowFromSystemServer();
1587 }
1588 }
1589
1590 /**
jackqdyulei455e90a2017-02-09 15:29:16 -08001591 * Get data about the battery saver mode for a specific service
Makoto Onuki2eccd022017-11-01 13:44:23 -07001592 * @param serviceType unique key for the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -08001593 * @return Battery saver state data.
1594 *
1595 * @hide
Kweku Adams7fb72a42019-01-08 16:08:49 -08001596 * @see com.android.server.power.batterysaver.BatterySaverPolicy
jackqdyulei455e90a2017-02-09 15:29:16 -08001597 * @see PowerSaveState
1598 */
Makoto Onuki2eccd022017-11-01 13:44:23 -07001599 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
jackqdyulei455e90a2017-02-09 15:29:16 -08001600 try {
1601 return mService.getPowerSaveState(serviceType);
1602 } catch (RemoteException e) {
1603 throw e.rethrowFromSystemServer();
1604 }
1605 }
1606
1607 /**
Makoto Onuki57f0f552017-12-11 12:22:18 -08001608 * Returns how location features should behave when battery saver is on. When battery saver
1609 * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}.
1610 *
1611 * <p>This API is normally only useful for components that provide location features.
1612 *
1613 * @see #isPowerSaveMode()
1614 * @see #ACTION_POWER_SAVE_MODE_CHANGED
1615 */
1616 @LocationPowerSaveMode
1617 public int getLocationPowerSaveMode() {
Kweku Adams731a1032019-02-04 14:05:41 -08001618 final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION);
Kweku Adams5e0052b2019-02-22 15:17:52 -08001619 if (!powerSaveState.batterySaverEnabled) {
Makoto Onuki57f0f552017-12-11 12:22:18 -08001620 return LOCATION_MODE_NO_CHANGE;
1621 }
Kweku Adams731a1032019-02-04 14:05:41 -08001622 return powerSaveState.locationMode;
Makoto Onuki57f0f552017-12-11 12:22:18 -08001623 }
1624
1625 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001626 * Returns true if the device is currently in idle mode. This happens when a device
1627 * has been sitting unused and unmoving for a sufficiently long period of time, so that
1628 * it decides to go into a lower power-use state. This may involve things like turning
1629 * off network access to apps. You can monitor for changes to this state with
1630 * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}.
1631 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07001632 * @return Returns true if currently in active device idle mode, else false. This is
1633 * when 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.
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001636 */
1637 public boolean isDeviceIdleMode() {
1638 try {
1639 return mService.isDeviceIdleMode();
1640 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001641 throw e.rethrowFromSystemServer();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001642 }
1643 }
1644
1645 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001646 * Returns true if the device is currently in light idle mode. This happens when a device
1647 * has had its screen off for a short time, switching it into a batching mode where we
1648 * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to
1649 * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}.
1650 *
1651 * @return Returns true if currently in active light device idle mode, else false. This is
1652 * when light idle mode restrictions are being actively applied; it will return false if the
1653 * device is in a long-term idle mode but currently running a maintenance window where
1654 * restrictions have been lifted.
1655 * @hide
1656 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001657 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001658 public boolean isLightDeviceIdleMode() {
1659 try {
1660 return mService.isLightDeviceIdleMode();
1661 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001662 throw e.rethrowFromSystemServer();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001663 }
1664 }
1665
1666 /**
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001667 * Return whether the given application package name is on the device's power whitelist.
1668 * Apps can be placed on the whitelist through the settings UI invoked by
1669 * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}.
1670 */
1671 public boolean isIgnoringBatteryOptimizations(String packageName) {
Makoto Onuki152742f2019-10-31 17:22:26 -07001672 return getDeviceIdleManager().isApplicationWhitelisted(packageName);
Dianne Hackborn1958e5e2015-06-12 18:11:41 -07001673 }
1674
1675 /**
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001676 * Turn off the device.
1677 *
1678 * @param confirm If true, shows a shutdown confirmation dialog.
Yusuke Sato705ffd12015-07-21 15:52:11 -07001679 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001680 * @param wait If true, this call waits for the shutdown to complete and does not return.
1681 *
1682 * @hide
1683 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07001684 public void shutdown(boolean confirm, String reason, boolean wait) {
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001685 try {
Yusuke Sato705ffd12015-07-21 15:52:11 -07001686 mService.shutdown(confirm, reason, wait);
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001687 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001688 throw e.rethrowFromSystemServer();
Filip Gruszczynskid05af862015-02-04 09:48:47 -08001689 }
1690 }
1691
1692 /**
Ruchi Kandoic45908d2016-05-16 10:23:15 -07001693 * This function checks if the device has implemented Sustained Performance
1694 * Mode. This needs to be checked only once and is constant for a particular
1695 * device/release.
1696 *
1697 * Sustained Performance Mode is intended to provide a consistent level of
1698 * performance for prolonged amount of time.
1699 *
1700 * Applications should check if the device supports this mode, before using
1701 * {@link android.view.Window#setSustainedPerformanceMode}.
1702 *
1703 * @return Returns True if the device supports it, false otherwise.
1704 *
1705 * @see android.view.Window#setSustainedPerformanceMode
Ruchi Kandoib4aa2e92016-03-30 12:07:31 -07001706 */
1707 public boolean isSustainedPerformanceModeSupported() {
1708 return mContext.getResources().getBoolean(
1709 com.android.internal.R.bool.config_sustainedPerformanceModeSupported);
1710 }
1711
1712 /**
Wei Wang59476652018-11-29 16:02:48 -08001713 * Thermal status code: Not under throttling.
1714 */
1715 public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE;
1716
1717 /**
1718 * Thermal status code: Light throttling where UX is not impacted.
1719 */
1720 public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT;
1721
1722 /**
1723 * Thermal status code: Moderate throttling where UX is not largely impacted.
1724 */
1725 public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE;
1726
1727 /**
1728 * Thermal status code: Severe throttling where UX is largely impacted.
1729 */
1730 public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE;
1731
1732 /**
1733 * Thermal status code: Platform has done everything to reduce power.
1734 */
1735 public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL;
1736
1737 /**
1738 * Thermal status code: Key components in platform are shutting down due to thermal condition.
1739 * Device functionalities will be limited.
1740 */
1741 public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY;
1742
1743 /**
1744 * Thermal status code: Need shutdown immediately.
1745 */
1746 public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN;
1747
1748 /** @hide */
1749 @IntDef(prefix = { "THERMAL_STATUS_" }, value = {
1750 THERMAL_STATUS_NONE,
1751 THERMAL_STATUS_LIGHT,
1752 THERMAL_STATUS_MODERATE,
1753 THERMAL_STATUS_SEVERE,
1754 THERMAL_STATUS_CRITICAL,
1755 THERMAL_STATUS_EMERGENCY,
1756 THERMAL_STATUS_SHUTDOWN,
1757 })
1758 @Retention(RetentionPolicy.SOURCE)
1759 public @interface ThermalStatus {}
1760
1761 /**
1762 * This function returns the current thermal status of the device.
1763 *
1764 * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under
1765 * thermal throttling.
1766 */
1767 public @ThermalStatus int getCurrentThermalStatus() {
1768 synchronized (this) {
1769 if (mThermalService == null) {
1770 mThermalService = IThermalService.Stub.asInterface(
1771 ServiceManager.getService(Context.THERMAL_SERVICE));
1772 }
1773 try {
1774 return mThermalService.getCurrentThermalStatus();
1775 } catch (RemoteException e) {
1776 throw e.rethrowFromSystemServer();
1777 }
1778 }
1779
1780 }
1781
1782 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001783 * Listener passed to
1784 * {@link PowerManager#addThermalStatusListener} and
1785 * {@link PowerManager#removeThermalStatusListener}
1786 * to notify caller of thermal status has changed.
Wei Wang59476652018-11-29 16:02:48 -08001787 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001788 public interface OnThermalStatusChangedListener {
Wei Wang59476652018-11-29 16:02:48 -08001789
1790 /**
1791 * Called when overall thermal throttling status changed.
1792 * @param status defined in {@link android.os.Temperature}.
1793 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001794 void onThermalStatusChanged(@ThermalStatus int status);
Wei Wang59476652018-11-29 16:02:48 -08001795 }
1796
Wei Wang7a49ff62019-03-04 21:13:32 -08001797
Wei Wang59476652018-11-29 16:02:48 -08001798 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001799 * This function adds a listener for thermal status change, listen call back will be
1800 * enqueued tasks on the main thread
Wei Wang59476652018-11-29 16:02:48 -08001801 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001802 * @param listener listener to be added,
Wei Wang59476652018-11-29 16:02:48 -08001803 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001804 public void addThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1805 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001806 synchronized (this) {
1807 if (mThermalService == null) {
1808 mThermalService = IThermalService.Stub.asInterface(
1809 ServiceManager.getService(Context.THERMAL_SERVICE));
1810 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001811 this.addThermalStatusListener(mContext.getMainExecutor(), listener);
1812 }
1813 }
1814
1815 /**
1816 * This function adds a listener for thermal status change.
1817 *
1818 * @param executor {@link Executor} to handle listener callback.
1819 * @param listener listener to be added.
1820 */
1821 public void addThermalStatusListener(@NonNull @CallbackExecutor Executor executor,
1822 @NonNull OnThermalStatusChangedListener listener) {
1823 Preconditions.checkNotNull(listener, "listener cannot be null");
1824 Preconditions.checkNotNull(executor, "executor cannot be null");
1825 synchronized (this) {
1826 if (mThermalService == null) {
1827 mThermalService = IThermalService.Stub.asInterface(
1828 ServiceManager.getService(Context.THERMAL_SERVICE));
1829 }
1830 Preconditions.checkArgument(!mListenerMap.containsKey(listener),
1831 "Listener already registered: " + listener);
1832 IThermalStatusListener internalListener = new IThermalStatusListener.Stub() {
1833 @Override
1834 public void onStatusChange(int status) {
1835 final long token = Binder.clearCallingIdentity();
1836 try {
Wei Wang59476652018-11-29 16:02:48 -08001837 executor.execute(() -> {
Wei Wang7a49ff62019-03-04 21:13:32 -08001838 listener.onThermalStatusChanged(status);
Wei Wang59476652018-11-29 16:02:48 -08001839 });
Wei Wang7a49ff62019-03-04 21:13:32 -08001840 } finally {
1841 Binder.restoreCallingIdentity(token);
Wei Wang59476652018-11-29 16:02:48 -08001842 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001843 }
1844 };
1845 try {
1846 if (mThermalService.registerThermalStatusListener(internalListener)) {
1847 mListenerMap.put(listener, internalListener);
Wei Wang59476652018-11-29 16:02:48 -08001848 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001849 throw new RuntimeException("Listener failed to set");
Wei Wang59476652018-11-29 16:02:48 -08001850 }
1851 } catch (RemoteException e) {
1852 throw e.rethrowFromSystemServer();
1853 }
1854 }
1855 }
1856
1857 /**
Wei Wang7a49ff62019-03-04 21:13:32 -08001858 * This function removes a listener for thermal status change
Wei Wang59476652018-11-29 16:02:48 -08001859 *
Wei Wang7a49ff62019-03-04 21:13:32 -08001860 * @param listener listener to be removed
Wei Wang59476652018-11-29 16:02:48 -08001861 */
Wei Wang7a49ff62019-03-04 21:13:32 -08001862 public void removeThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) {
1863 Preconditions.checkNotNull(listener, "listener cannot be null");
Wei Wang59476652018-11-29 16:02:48 -08001864 synchronized (this) {
1865 if (mThermalService == null) {
1866 mThermalService = IThermalService.Stub.asInterface(
1867 ServiceManager.getService(Context.THERMAL_SERVICE));
1868 }
Wei Wang7a49ff62019-03-04 21:13:32 -08001869 IThermalStatusListener internalListener = mListenerMap.get(listener);
1870 Preconditions.checkArgument(internalListener != null, "Listener was not added");
Wei Wang59476652018-11-29 16:02:48 -08001871 try {
Wei Wang7a49ff62019-03-04 21:13:32 -08001872 if (mThermalService.unregisterThermalStatusListener(internalListener)) {
1873 mListenerMap.remove(listener);
Wei Wang59476652018-11-29 16:02:48 -08001874 } else {
Wei Wang7a49ff62019-03-04 21:13:32 -08001875 throw new RuntimeException("Listener failed to remove");
Wei Wang59476652018-11-29 16:02:48 -08001876 }
1877 } catch (RemoteException e) {
1878 throw e.rethrowFromSystemServer();
1879 }
1880 }
1881 }
1882
1883 /**
Lucas Dupin16cfe452018-02-08 13:14:50 -08001884 * If true, the doze component is not started until after the screen has been
1885 * turned off and the screen off animation has been performed.
1886 * @hide
1887 */
1888 public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) {
1889 try {
1890 mService.setDozeAfterScreenOff(dozeAfterScreenOf);
1891 } catch (RemoteException e) {
1892 throw e.rethrowFromSystemServer();
1893 }
1894 }
1895
1896 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07001897 * Returns the reason the phone was last shutdown. Calling app must have the
1898 * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information.
1899 * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could
1900 * not be accessed.
1901 * @hide
1902 */
1903 @ShutdownReason
1904 public int getLastShutdownReason() {
1905 try {
1906 return mService.getLastShutdownReason();
1907 } catch (RemoteException e) {
1908 throw e.rethrowFromSystemServer();
1909 }
1910 }
1911
1912 /**
Calin Tatarua3805722018-08-09 16:41:28 +02001913 * Returns the reason the device last went to sleep (i.e. the last value of
1914 * the second argument of {@link #goToSleep(long, int, int) goToSleep}).
1915 *
1916 * @return One of the {@code GO_TO_SLEEP_REASON_*} constants.
1917 *
1918 * @hide
1919 */
1920 public int getLastSleepReason() {
1921 try {
1922 return mService.getLastSleepReason();
1923 } catch (RemoteException e) {
1924 throw e.rethrowFromSystemServer();
1925 }
1926 }
1927
1928 /**
Santos Cordon12f92eb2019-02-01 21:28:47 +00001929 * Forces the device to go to suspend, even if there are currently wakelocks being held.
1930 * <b>Caution</b>
1931 * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts
1932 * of the system will not be notified and will not have an opportunity to save state prior to
1933 * the device going to suspend.
1934 * This method should only be used in very rare circumstances where the device is intended
1935 * to appear as completely off to the user and they have a well understood, reliable way of
1936 * re-enabling it.
1937 * </p><p>
1938 * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
1939 * </p>
1940 *
1941 * @return true on success, false otherwise.
1942 * @hide
1943 */
1944 @SystemApi
1945 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
1946 public boolean forceSuspend() {
1947 try {
1948 return mService.forceSuspend();
1949 } catch (RemoteException e) {
1950 throw e.rethrowFromSystemServer();
1951 }
1952 }
1953
1954 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001955 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1956 * This broadcast is only sent to registered receivers.
1957 */
1958 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1959 public static final String ACTION_POWER_SAVE_MODE_CHANGED
1960 = "android.os.action.POWER_SAVE_MODE_CHANGED";
1961
1962 /**
Jason Monkafae4bd2015-12-15 14:20:06 -05001963 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes.
1964 * @hide
1965 */
1966 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1967 public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL
1968 = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL";
1969
1970 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001971 * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes.
1972 * This broadcast is only sent to registered receivers.
1973 */
1974 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1975 public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
1976 = "android.os.action.DEVICE_IDLE_MODE_CHANGED";
1977
1978 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001979 * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes.
1980 * This broadcast is only sent to registered receivers.
1981 * @hide
1982 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001983 @UnsupportedAppUsage
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001984 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1985 public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED
1986 = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED";
1987
1988 /**
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001989 * @hide Intent that is broadcast when the set of power save whitelist apps has changed.
1990 * This broadcast is only sent to registered receivers.
1991 */
1992 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
1993 public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED
1994 = "android.os.action.POWER_SAVE_WHITELIST_CHANGED";
1995
1996 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001997 * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed.
1998 * This broadcast is only sent to registered receivers.
1999 */
2000 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
2001 public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED
2002 = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED";
2003
2004 /**
John Spurlock1bb480a2014-08-02 17:12:43 -04002005 * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change.
2006 * This broadcast is only sent to registered receivers.
2007 *
2008 * @hide
2009 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002010 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04002011 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
2012 public static final String ACTION_POWER_SAVE_MODE_CHANGING
2013 = "android.os.action.POWER_SAVE_MODE_CHANGING";
2014
2015 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002016 @UnsupportedAppUsage
John Spurlock1bb480a2014-08-02 17:12:43 -04002017 public static final String EXTRA_POWER_SAVE_MODE = "mode";
2018
2019 /**
Denny cy Leec5a7c292019-01-01 17:37:55 +08002020 * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) .
2021 * @hide
2022 */
2023 public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0;
2024
2025 /**
2026 * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode
2027 * longer).
2028 * @hide
2029 */
2030 public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1;
2031
2032 /**
2033 * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly)
2034 * @hide
2035 */
2036 public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;
2037
2038 /**
Jeff Brown1244cda2012-06-19 16:44:46 -07002039 * A wake lock is a mechanism to indicate that your application needs
2040 * to have the device stay on.
Kenny Rootd710fb52011-03-15 17:39:45 -07002041 * <p>
2042 * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK}
Neil Fuller71fbb812015-11-30 09:51:33 +00002043 * permission in an {@code <uses-permission>} element of the application's manifest.
Jeff Brown1244cda2012-06-19 16:44:46 -07002044 * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}.
2045 * </p><p>
2046 * Call {@link #acquire()} to acquire the wake lock and force the device to stay
2047 * on at the level that was requested when the wake lock was created.
2048 * </p><p>
2049 * Call {@link #release()} when you are done and don't need the lock anymore.
2050 * It is very important to do this as soon as possible to avoid running down the
2051 * device's battery excessively.
2052 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 */
Jeff Brown1244cda2012-06-19 16:44:46 -07002054 public final class WakeLock {
Andrei Onea24ec3212019-03-15 17:35:05 +00002055 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002056 private int mFlags;
Andrei Onea24ec3212019-03-15 17:35:05 +00002057 @UnsupportedAppUsage
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002058 private String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002059 private final String mPackageName;
Jeff Brown1244cda2012-06-19 16:44:46 -07002060 private final IBinder mToken;
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002061 private int mInternalCount;
2062 private int mExternalCount;
Jeff Brown1244cda2012-06-19 16:44:46 -07002063 private boolean mRefCounted = true;
2064 private boolean mHeld;
2065 private WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002066 private String mHistoryTag;
Jeff Brown3edf5272014-08-14 19:25:14 -07002067 private final String mTraceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068
Jeff Brown1244cda2012-06-19 16:44:46 -07002069 private final Runnable mReleaser = new Runnable() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 public void run() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002071 release(RELEASE_FLAG_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074
Dianne Hackborn713df152013-05-17 11:27:57 -07002075 WakeLock(int flags, String tag, String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 mFlags = flags;
2077 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07002078 mPackageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 mToken = new Binder();
Jeff Brown3edf5272014-08-14 19:25:14 -07002080 mTraceName = "WakeLock (" + mTag + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 }
2082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 @Override
Jeff Brown1244cda2012-06-19 16:44:46 -07002084 protected void finalize() throws Throwable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 synchronized (mToken) {
2086 if (mHeld) {
Dan Egnor60d87622009-12-16 16:32:58 -08002087 Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);
Jeff Brown3edf5272014-08-14 19:25:14 -07002088 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 try {
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002090 mService.releaseWakeLock(mToken, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002092 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095 }
2096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097
Jeff Brown1244cda2012-06-19 16:44:46 -07002098 /**
2099 * Sets whether this WakeLock is reference counted.
2100 * <p>
2101 * Wake locks are reference counted by default. If a wake lock is
2102 * reference counted, then each call to {@link #acquire()} must be
2103 * balanced by an equal number of calls to {@link #release()}. If a wake
2104 * lock is not reference counted, then one call to {@link #release()} is
2105 * sufficient to undo the effect of all previous calls to {@link #acquire()}.
2106 * </p>
2107 *
2108 * @param value True to make the wake lock reference counted, false to
2109 * make the wake lock non-reference counted.
2110 */
2111 public void setReferenceCounted(boolean value) {
2112 synchronized (mToken) {
2113 mRefCounted = value;
2114 }
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04002115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116
Jeff Brown1244cda2012-06-19 16:44:46 -07002117 /**
2118 * Acquires the wake lock.
2119 * <p>
2120 * Ensures that the device is on at the level requested when
2121 * the wake lock was created.
2122 * </p>
2123 */
2124 public void acquire() {
2125 synchronized (mToken) {
2126 acquireLocked();
2127 }
2128 }
2129
2130 /**
2131 * Acquires the wake lock with a timeout.
2132 * <p>
2133 * Ensures that the device is on at the level requested when
2134 * the wake lock was created. The lock will be released after the given timeout
2135 * expires.
2136 * </p>
2137 *
2138 * @param timeout The timeout after which to release the wake lock, in milliseconds.
2139 */
2140 public void acquire(long timeout) {
2141 synchronized (mToken) {
2142 acquireLocked();
2143 mHandler.postDelayed(mReleaser, timeout);
2144 }
2145 }
2146
2147 private void acquireLocked() {
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002148 mInternalCount++;
2149 mExternalCount++;
2150 if (!mRefCounted || mInternalCount == 1) {
Jeff Brownff1baef2012-07-19 15:01:17 -07002151 // Do this even if the wake lock is already thought to be held (mHeld == true)
2152 // because non-reference counted wake locks are not always properly released.
2153 // For example, the keyguard's wake lock might be forcibly released by the
2154 // power manager without the keyguard knowing. A subsequent call to acquire
2155 // should immediately acquire the wake lock once again despite never having
2156 // been explicitly released by the keyguard.
Jeff Brown1244cda2012-06-19 16:44:46 -07002157 mHandler.removeCallbacks(mReleaser);
Jeff Brown3edf5272014-08-14 19:25:14 -07002158 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brownff1baef2012-07-19 15:01:17 -07002159 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002160 mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
2161 mHistoryTag);
Jeff Brownff1baef2012-07-19 15:01:17 -07002162 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002163 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002164 }
Jeff Brownff1baef2012-07-19 15:01:17 -07002165 mHeld = true;
Jeff Brown1244cda2012-06-19 16:44:46 -07002166 }
2167 }
2168
2169 /**
2170 * Releases the wake lock.
2171 * <p>
2172 * This method releases your claim to the CPU or screen being on.
2173 * The screen may turn off shortly after you release the wake lock, or it may
2174 * not if there are other wake locks still held.
2175 * </p>
2176 */
2177 public void release() {
2178 release(0);
2179 }
2180
2181 /**
2182 * Releases the wake lock with flags to modify the release behavior.
2183 * <p>
2184 * This method releases your claim to the CPU or screen being on.
2185 * The screen may turn off shortly after you release the wake lock, or it may
2186 * not if there are other wake locks still held.
2187 * </p>
2188 *
2189 * @param flags Combination of flag values to modify the release behavior.
Michael Wright1208e272014-09-08 19:57:50 -07002190 * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported.
2191 * Passing 0 is equivalent to calling {@link #release()}.
Jeff Brown1244cda2012-06-19 16:44:46 -07002192 */
2193 public void release(int flags) {
2194 synchronized (mToken) {
Henrik Baard8d475ca2017-08-09 14:45:05 +02002195 if (mInternalCount > 0) {
2196 // internal count must only be decreased if it is > 0 or state of
2197 // the WakeLock object is broken.
2198 mInternalCount--;
2199 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002200 if ((flags & RELEASE_FLAG_TIMEOUT) == 0) {
2201 mExternalCount--;
2202 }
2203 if (!mRefCounted || mInternalCount == 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002204 mHandler.removeCallbacks(mReleaser);
2205 if (mHeld) {
Jeff Brown3edf5272014-08-14 19:25:14 -07002206 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown1244cda2012-06-19 16:44:46 -07002207 try {
2208 mService.releaseWakeLock(mToken, flags);
2209 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002210 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002211 }
2212 mHeld = false;
2213 }
2214 }
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002215 if (mRefCounted && mExternalCount < 0) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002216 throw new RuntimeException("WakeLock under-locked " + mTag);
2217 }
2218 }
2219 }
2220
2221 /**
2222 * Returns true if the wake lock has been acquired but not yet released.
2223 *
2224 * @return True if the wake lock is held.
2225 */
2226 public boolean isHeld() {
2227 synchronized (mToken) {
2228 return mHeld;
2229 }
2230 }
2231
2232 /**
2233 * Sets the work source associated with the wake lock.
2234 * <p>
2235 * The work source is used to determine on behalf of which application
2236 * the wake lock is being held. This is useful in the case where a
2237 * service is performing work on behalf of an application so that the
2238 * cost of that work can be accounted to the application.
2239 * </p>
2240 *
Olivier Gaillard24a0c132017-11-22 15:07:31 +00002241 * <p>
2242 * Make sure to follow the tag naming convention when using WorkSource
2243 * to make it easier for app developers to understand wake locks
2244 * attributed to them. See {@link PowerManager#newWakeLock(int, String)}
2245 * documentation.
2246 * </p>
2247 *
Jeff Brown1244cda2012-06-19 16:44:46 -07002248 * @param ws The work source, or null if none.
2249 */
2250 public void setWorkSource(WorkSource ws) {
2251 synchronized (mToken) {
Narayan Kamath81822022017-12-08 11:56:01 +00002252 if (ws != null && ws.isEmpty()) {
Jeff Brown1244cda2012-06-19 16:44:46 -07002253 ws = null;
2254 }
2255
2256 final boolean changed;
2257 if (ws == null) {
2258 changed = mWorkSource != null;
2259 mWorkSource = null;
2260 } else if (mWorkSource == null) {
2261 changed = true;
2262 mWorkSource = new WorkSource(ws);
2263 } else {
Narayan Kamath81822022017-12-08 11:56:01 +00002264 changed = !mWorkSource.equals(ws);
Jeff Brown1244cda2012-06-19 16:44:46 -07002265 if (changed) {
2266 mWorkSource.set(ws);
2267 }
2268 }
2269
2270 if (changed && mHeld) {
2271 try {
Dianne Hackborn4590e522014-03-24 13:36:46 -07002272 mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
Jeff Brown1244cda2012-06-19 16:44:46 -07002273 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002274 throw e.rethrowFromSystemServer();
Jeff Brown1244cda2012-06-19 16:44:46 -07002275 }
2276 }
2277 }
2278 }
2279
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002280 /** @hide */
2281 public void setTag(String tag) {
2282 mTag = tag;
2283 }
2284
2285 /** @hide */
Christopher Tate4b17e982016-09-26 12:59:10 -07002286 public String getTag() {
2287 return mTag;
2288 }
2289
2290 /** @hide */
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002291 public void setHistoryTag(String tag) {
2292 mHistoryTag = tag;
2293 }
2294
2295 /** @hide */
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002296 public void setUnimportantForLogging(boolean state) {
2297 if (state) mFlags |= UNIMPORTANT_FOR_LOGGING;
2298 else mFlags &= ~UNIMPORTANT_FOR_LOGGING;
2299 }
2300
Jeff Brown1244cda2012-06-19 16:44:46 -07002301 @Override
2302 public String toString() {
2303 synchronized (mToken) {
2304 return "WakeLock{"
2305 + Integer.toHexString(System.identityHashCode(this))
Jeff Sharkey291c32a2017-07-05 12:34:04 -06002306 + " held=" + mHeld + ", refCount=" + mInternalCount + "}";
Jeff Brown1244cda2012-06-19 16:44:46 -07002307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
Adrian Roos7445c0b2016-09-06 16:45:46 -07002309
Yi Jin148d7f42017-11-28 14:23:56 -08002310 /** @hide */
2311 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2312 synchronized (mToken) {
2313 final long token = proto.start(fieldId);
Yi Jin163967f2018-03-15 13:49:44 -07002314 proto.write(PowerManagerProto.WakeLock.TAG, mTag);
2315 proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName);
2316 proto.write(PowerManagerProto.WakeLock.HELD, mHeld);
2317 proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount);
Yi Jin148d7f42017-11-28 14:23:56 -08002318 if (mWorkSource != null) {
Yi Jin163967f2018-03-15 13:49:44 -07002319 mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE);
Yi Jin148d7f42017-11-28 14:23:56 -08002320 }
2321 proto.end(token);
2322 }
2323 }
2324
Adrian Roos7445c0b2016-09-06 16:45:46 -07002325 /**
2326 * Wraps a Runnable such that this method immediately acquires the wake lock and then
2327 * once the Runnable is done the wake lock is released.
2328 *
2329 * <p>Example:
2330 *
2331 * <pre>
2332 * mHandler.post(mWakeLock.wrap(() -> {
2333 * // do things on handler, lock is held while we're waiting for this
2334 * // to get scheduled and until the runnable is done executing.
2335 * });
2336 * </pre>
2337 *
2338 * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
2339 * leak the wakelock!
2340 *
2341 * @hide
2342 */
2343 public Runnable wrap(Runnable r) {
2344 acquire();
2345 return () -> {
2346 try {
2347 r.run();
2348 } finally {
2349 release();
2350 }
2351 };
2352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354}