The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 19 | import android.Manifest.permission; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 20 | import android.annotation.CallbackExecutor; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 23 | import android.annotation.RequiresPermission; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 24 | import android.annotation.SdkConstant; |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Dan Gittik | 26b030d | 2018-04-16 18:50:10 +0100 | [diff] [blame] | 27 | import android.annotation.TestApi; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 28 | import android.annotation.UnsupportedAppUsage; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 30 | import android.service.dreams.Sandman; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 31 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.util.Log; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 33 | import android.util.proto.ProtoOutputStream; |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 34 | |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 35 | import com.android.internal.util.Preconditions; |
| 36 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 37 | import java.lang.annotation.Retention; |
| 38 | import java.lang.annotation.RetentionPolicy; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 39 | import java.util.concurrent.Executor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 42 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 49 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * {@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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 61 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 62 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 65 | * <table> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 66 | * <tr><th>Flag Value</th> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | * <th>CPU</th> <th>Screen</th> <th>Keyboard</th></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 69 | * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 70 | * <td>On*</td> <td>Off</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 72 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 73 | * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 74 | * <td>On</td> <td>Dim</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * </tr> |
| 76 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 77 | * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 78 | * <td>On</td> <td>Bright</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 80 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 81 | * <tr><td>{@link #FULL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 82 | * <td>On</td> <td>Bright</td> <td>Bright</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | * </table> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 85 | * </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 Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 92 | * <i>These flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.</i></p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 94 | * <table> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | * <tr><th>Flag Value</th> <th>Description</th></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 97 | * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * <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 Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 102 | * see immediately.</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 104 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 105 | * <tr><td>{@link #ON_AFTER_RELEASE}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * <td>If this flag is set, the user activity timer will be reset when the WakeLock is |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 107 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | * </table> |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 111 | * <p> |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 112 | * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK} |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 113 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 114 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 116 | @SystemService(Context.POWER_SERVICE) |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 117 | public final class PowerManager { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | private static final String TAG = "PowerManager"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 119 | |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 120 | /* 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 |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 123 | * value (in frameworks/base/core/proto/android/os/enums.proto) can be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 127 | * Wake lock level: Ensures that the CPU is running; the screen and keyboard |
| 128 | * backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 129 | * <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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 132 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 134 | public static final int PARTIAL_WAKE_LOCK = OsProtoEnums.PARTIAL_WAKE_LOCK; // 0x00000001 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | |
| 136 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 137 | * Wake lock level: Ensures that the screen is on (but may be dimmed); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 138 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 139 | * <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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 144 | * |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 145 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 150 | @Deprecated |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 151 | public static final int SCREEN_DIM_WAKE_LOCK = OsProtoEnums.SCREEN_DIM_WAKE_LOCK; // 0x00000006 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | |
| 153 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 154 | * Wake lock level: Ensures that the screen is on at full brightness; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 156 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 167 | @Deprecated |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 168 | public static final int SCREEN_BRIGHT_WAKE_LOCK = |
| 169 | OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 170 | |
| 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 |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 186 | public static final int FULL_WAKE_LOCK = OsProtoEnums.FULL_WAKE_LOCK; // 0x0000001a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
| 188 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 189 | * Wake lock level: Turns the screen off when the proximity sensor activates. |
| 190 | * <p> |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 191 | * 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 Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 201 | * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported} |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 202 | * to determine whether this wake lock level is supported. |
Craig Mautner | 6edb6db | 2012-11-20 18:21:12 -0800 | [diff] [blame] | 203 | * </p><p> |
| 204 | * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 205 | * </p> |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 206 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 207 | public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = |
| 208 | OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020 |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 209 | |
| 210 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 211 | * 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 Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 216 | * </p><p> |
| 217 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 218 | * </p> |
| 219 | * |
| 220 | * {@hide} |
| 221 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 222 | public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040 |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 223 | |
| 224 | /** |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 225 | * 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 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 236 | public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080 |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 237 | |
| 238 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 239 | * Mask for the wake lock level component of a combined wake lock level and flags integer. |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 240 | * |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 241 | * @hide |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 242 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 243 | public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff; |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 244 | |
| 245 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 246 | * Wake lock flag: Turn the screen on when the wake lock is acquired. |
| 247 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * Normally wake locks don't actually wake the device, they just cause |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 249 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | * the device to be on are the exception; use this flag to be like them. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 252 | * </p><p> |
| 253 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 254 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 256 | public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | |
| 258 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 259 | * Wake lock flag: When this wake lock is released, poke the user activity timer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | * so the screen stays on for a little longer. |
| 261 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 262 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 268 | public static final int ON_AFTER_RELEASE = 0x20000000; |
| 269 | |
| 270 | /** |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 271 | * 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 Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 278 | * 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 Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 281 | */ |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 282 | 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 Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 291 | * Brightness value for fully on. |
| 292 | * @hide |
| 293 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 294 | @UnsupportedAppUsage |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 295 | public static final int BRIGHTNESS_ON = 255; |
| 296 | |
| 297 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 298 | * Brightness value for fully off. |
| 299 | * @hide |
| 300 | */ |
| 301 | public static final int BRIGHTNESS_OFF = 0; |
| 302 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Brightness value for default policy handling by the system. |
| 305 | * @hide |
| 306 | */ |
| 307 | public static final int BRIGHTNESS_DEFAULT = -1; |
| 308 | |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 309 | // 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 Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 316 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 317 | 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 Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 323 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 324 | 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 Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 330 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 331 | public static final int USER_ACTIVITY_EVENT_TOUCH = 2; |
| 332 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 333 | /** |
Phil Weaver | da80d67 | 2016-03-15 16:25:46 -0700 | [diff] [blame] | 334 | * 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 Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 341 | * 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 Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 348 | * 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 Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 352 | * @hide |
| 353 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 354 | @SystemApi |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 355 | public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0; |
| 356 | |
| 357 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 358 | * 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 Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 368 | * @hide |
| 369 | */ |
| 370 | public static final int GO_TO_SLEEP_REASON_MIN = 0; |
| 371 | |
| 372 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 373 | * Go to sleep reason code: Going to sleep due by application request. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 374 | * @hide |
| 375 | */ |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 376 | public static final int GO_TO_SLEEP_REASON_APPLICATION = GO_TO_SLEEP_REASON_MIN; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 377 | |
| 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 389 | @UnsupportedAppUsage |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 390 | public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2; |
| 391 | |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 392 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 393 | * 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 Gruszczynski | 9779e12 | 2015-03-13 17:39:31 -0700 | [diff] [blame] | 411 | * 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 Susla | f9a651d | 2017-10-11 12:06:27 -0700 | [diff] [blame] | 417 | * 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 Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 423 | * 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 Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 434 | * @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 Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 446 | case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend"; |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 447 | default: return Integer.toString(sleepReason); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 452 | * 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 Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 458 | * @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 Cordon | 623526b | 2019-04-09 17:02:38 +0100 | [diff] [blame] | 558 | * @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 Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 570 | * 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 Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 573 | * <p> |
| 574 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 575 | * permission (in addition to |
| 576 | * {@link android.Manifest.permission#REBOOT}). |
| 577 | * </p> |
Doug Zongker | 183415e | 2014-08-12 10:18:40 -0700 | [diff] [blame] | 578 | * @hide |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 579 | */ |
| 580 | public static final String REBOOT_RECOVERY = "recovery"; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 581 | |
| 582 | /** |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 583 | * 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 Chopra | 1ce53bc | 2015-12-14 13:35:14 +0000 | [diff] [blame] | 595 | * 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 Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 602 | * The 'reason' value used when rebooting in safe mode |
| 603 | * @hide |
| 604 | */ |
| 605 | public static final String REBOOT_SAFE_MODE = "safemode"; |
| 606 | |
| 607 | /** |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 608 | * 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 Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 614 | * 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 Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 619 | /** |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 620 | * 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 Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 627 | * 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 Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 634 | * 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 Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 641 | * @hide |
| 642 | */ |
| 643 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 644 | @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 645 | SHUTDOWN_REASON_UNKNOWN, |
| 646 | SHUTDOWN_REASON_SHUTDOWN, |
| 647 | SHUTDOWN_REASON_REBOOT, |
| 648 | SHUTDOWN_REASON_USER_REQUESTED, |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 649 | SHUTDOWN_REASON_THERMAL_SHUTDOWN, |
| 650 | SHUTDOWN_REASON_LOW_BATTERY, |
| 651 | SHUTDOWN_REASON_BATTERY_THERMAL |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 652 | }) |
| 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 Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 685 | /** |
| 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 Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 697 | /** |
| 698 | * @hide |
| 699 | */ |
| 700 | @Retention(RetentionPolicy.SOURCE) |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 701 | @IntDef({ServiceType.LOCATION, |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 702 | 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 Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 710 | ServiceType.DATA_SAVER, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 711 | ServiceType.FORCE_ALL_APPS_STANDBY, |
Kweku Adams | 27c65cc | 2019-02-28 15:32:31 -0800 | [diff] [blame] | 712 | ServiceType.FORCE_BACKGROUND_CHECK, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 713 | ServiceType.OPTIONAL_SENSORS, |
Lucas Dupin | 92a62e5 | 2018-01-30 17:22:20 -0800 | [diff] [blame] | 714 | ServiceType.AOD, |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 715 | ServiceType.QUICK_DOZE, |
Kweku Adams | 27c65cc | 2019-02-28 15:32:31 -0800 | [diff] [blame] | 716 | ServiceType.NIGHT_MODE, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 717 | }) |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 718 | public @interface ServiceType { |
| 719 | int NULL = 0; |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 720 | int LOCATION = 1; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 721 | 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 Dupin | 92a62e5 | 2018-01-30 17:22:20 -0800 | [diff] [blame] | 730 | int AOD = 14; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 731 | |
| 732 | /** |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 733 | * Whether to enable force-app-standby on all apps or not. |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 734 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 735 | int FORCE_ALL_APPS_STANDBY = 11; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 736 | |
| 737 | /** |
Makoto Onuki | e7ec72a | 2017-11-22 11:16:30 -0800 | [diff] [blame] | 738 | * Whether to enable background check on all apps or not. |
| 739 | */ |
| 740 | int FORCE_BACKGROUND_CHECK = 12; |
| 741 | |
| 742 | /** |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 743 | * Whether to disable non-essential sensors. (e.g. edge sensors.) |
| 744 | */ |
| 745 | int OPTIONAL_SENSORS = 13; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 746 | |
| 747 | /** |
| 748 | * Whether to go into Deep Doze as soon as the screen turns off or not. |
| 749 | */ |
| 750 | int QUICK_DOZE = 15; |
Kweku Adams | 4db6a3c | 2019-02-04 16:06:13 -0800 | [diff] [blame] | 751 | |
| 752 | /** |
| 753 | * Whether to enable night mode when battery saver is enabled. |
| 754 | */ |
| 755 | int NIGHT_MODE = 16; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 758 | /** |
| 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 Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 782 | /** |
| 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 Adams | c1d844a | 2019-03-28 16:05:00 -0700 | [diff] [blame] | 788 | /** @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 Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 792 | |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 793 | /** |
| 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 Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 802 | LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF, |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 803 | }) |
| 804 | public @interface LocationPowerSaveMode {} |
| 805 | |
Kweku Adams | 4fb074e | 2019-02-01 16:03:27 -0800 | [diff] [blame] | 806 | /** @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 Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 824 | final Context mContext; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 825 | @UnsupportedAppUsage |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 826 | final IPowerManager mService; |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 827 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 828 | final Handler mHandler; |
| 829 | |
Makoto Onuki | 152742f | 2019-10-31 17:22:26 -0700 | [diff] [blame] | 830 | /** We lazily initialize it.*/ |
| 831 | private DeviceIdleManager mDeviceIdleManager; |
| 832 | |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 833 | IThermalService mThermalService; |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 834 | private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener> |
| 835 | mListenerMap = new ArrayMap<>(); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 838 | * {@hide} |
| 839 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 840 | public PowerManager(Context context, IPowerManager service, Handler handler) { |
| 841 | mContext = context; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 842 | mService = service; |
| 843 | mHandler = handler; |
| 844 | } |
| 845 | |
Makoto Onuki | 152742f | 2019-10-31 17:22:26 -0700 | [diff] [blame] | 846 | 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 854 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 855 | * 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 860 | @UnsupportedAppUsage |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 861 | public int getMinimumScreenBrightnessSetting() { |
| 862 | return mContext.getResources().getInteger( |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 863 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 864 | } |
| 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 872 | @UnsupportedAppUsage |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 873 | public int getMaximumScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 874 | return mContext.getResources().getInteger( |
| 875 | com.android.internal.R.integer.config_screenBrightnessSettingMaximum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Gets the default screen brightness setting. |
| 880 | * @hide |
| 881 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 882 | @UnsupportedAppUsage |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 883 | public int getDefaultScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 884 | return mContext.getResources().getInteger( |
| 885 | com.android.internal.R.integer.config_screenBrightnessSettingDefault); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 889 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 918 | * Creates a new wake lock with the specified level and flags. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 919 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 920 | * 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 Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 957 | * <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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 977 | * @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 Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 987 | * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 988 | * @see #ACQUIRE_CAUSES_WAKEUP |
| 989 | * @see #ON_AFTER_RELEASE |
| 990 | */ |
| 991 | public WakeLock newWakeLock(int levelAndFlags, String tag) { |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 992 | validateWakeLockParameters(levelAndFlags, tag); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 993 | return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName()); |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 997 | @UnsupportedAppUsage |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 998 | 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 Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1005 | case DOZE_WAKE_LOCK: |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 1006 | case DRAW_WAKE_LOCK: |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 1007 | break; |
| 1008 | default: |
| 1009 | throw new IllegalArgumentException("Must specify a valid wake lock level."); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1010 | } |
| 1011 | if (tag == null) { |
| 1012 | throw new IllegalArgumentException("The tag must not be null."); |
| 1013 | } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * Notifies the power manager that user activity happened. |
| 1018 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1019 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1023 | * </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 Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1028 | * time base. This timestamp is used to correctly order the user activity request with |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1029 | * 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 Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1036 | * |
| 1037 | * @see #wakeUp |
| 1038 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1039 | * |
| 1040 | * @removed Requires signature or system permission. |
| 1041 | * @deprecated Use {@link #userActivity(long, int, int)}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1042 | */ |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1043 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1044 | public void userActivity(long when, boolean noChangeLights) { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1045 | 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 Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 1074 | @RequiresPermission(anyOf = { |
| 1075 | android.Manifest.permission.DEVICE_POWER, |
| 1076 | android.Manifest.permission.USER_ACTIVITY |
| 1077 | }) |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1078 | public void userActivity(long when, int event, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1079 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1080 | mService.userActivity(when, event, flags); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1081 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1082 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * Forces the device to go to sleep. |
| 1088 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1089 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1091 | * </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 Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1097 | * order the go to sleep request with other power management functions. It should be set |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1098 | * to the timestamp of the input event that caused the request to go to sleep. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1099 | * |
| 1100 | * @see #userActivity |
| 1101 | * @see #wakeUp |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1102 | * |
| 1103 | * @removed Requires signature permission. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1104 | */ |
| 1105 | public void goToSleep(long time) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1106 | goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1110 | * 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 Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1129 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 1130 | @UnsupportedAppUsage |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1131 | public void goToSleep(long time, int reason, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1132 | try { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1133 | mService.goToSleep(time, reason, flags); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1134 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1135 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1136 | } |
| 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 Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1150 | * order the wake up request with other power management functions. It should be set |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1151 | * to the timestamp of the input event that caused the request to wake up. |
| 1152 | * |
| 1153 | * @see #userActivity |
| 1154 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1155 | * |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1156 | * @deprecated Use {@link #wakeUp(long, int, String)} instead. |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1157 | * @removed Requires signature permission. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1158 | */ |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1159 | @Deprecated |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1160 | public void wakeUp(long time) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1161 | wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp"); |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1165 | * 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 Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1185 | * @hide |
| 1186 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 1187 | @UnsupportedAppUsage |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1188 | @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 Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1217 | try { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1218 | mService.wakeUp(time, reason, details, mContext.getOpPackageName()); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1219 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1220 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /** |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1225 | * 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 Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1243 | * @hide Requires signature permission. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1244 | */ |
| 1245 | public void nap(long time) { |
| 1246 | try { |
| 1247 | mService.nap(time); |
| 1248 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1249 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1254 | * 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 Kibirev | c9752eb | 2018-11-27 13:26:11 -0800 | [diff] [blame] | 1260 | * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and |
| 1261 | * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions. |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1262 | * </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 Kibirev | c9752eb | 2018-11-27 13:26:11 -0800 | [diff] [blame] | 1272 | @RequiresPermission(allOf = { |
| 1273 | android.Manifest.permission.READ_DREAM_STATE, |
| 1274 | android.Manifest.permission.WRITE_DREAM_STATE }) |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1275 | public void dream(long time) { |
| 1276 | Sandman.startDreamByUserRequest(mContext); |
| 1277 | } |
| 1278 | |
| 1279 | /** |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 1280 | * 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1298 | throw e.rethrowFromSystemServer(); |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1302 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1303 | * Returns true if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1304 | * |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1305 | * @param level The wake lock level to check. |
| 1306 | * @return True if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1307 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1308 | public boolean isWakeLockLevelSupported(int level) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1309 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1310 | return mService.isWakeLockLevelSupported(level); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1311 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1312 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1317 | * Returns true if the device is in an interactive state. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1318 | * <p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1319 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1322 | * </p><p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1323 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1327 | * </p> |
| 1328 | * |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1329 | * @return True if the device is in an interactive state. |
| 1330 | * |
| 1331 | * @deprecated Use {@link #isInteractive} instead. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1332 | */ |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1333 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1334 | public boolean isScreenOn() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1335 | 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1373 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1374 | return mService.isInteractive(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1375 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1376 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1377 | } |
| 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 Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 1391 | mService.reboot(false, reason, true); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1392 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1393 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 1398 | * 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 Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1413 | * 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1424 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | /** |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1429 | * Set the current power save mode. |
| 1430 | * |
| 1431 | * @return True if the set was allowed. |
| 1432 | * |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1433 | * @hide |
Salvador Martinez | a80bbab | 2018-09-24 10:36:11 -0700 | [diff] [blame] | 1434 | * @see #isPowerSaveMode() |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1435 | */ |
Salvador Martinez | a80bbab | 2018-09-24 10:36:11 -0700 | [diff] [blame] | 1436 | @SystemApi |
| 1437 | @TestApi |
| 1438 | @RequiresPermission(anyOf = { |
| 1439 | android.Manifest.permission.DEVICE_POWER, |
| 1440 | android.Manifest.permission.POWER_SAVER |
| 1441 | }) |
Salvador Martinez | c8c4c5d | 2019-03-11 11:11:37 -0700 | [diff] [blame] | 1442 | public boolean setPowerSaveModeEnabled(boolean mode) { |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1443 | try { |
Salvador Martinez | c8c4c5d | 2019-03-11 11:11:37 -0700 | [diff] [blame] | 1444 | return mService.setPowerSaveModeEnabled(mode); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1445 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1446 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /** |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1451 | * 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 Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1473 | * @param powerSaveHint A signal indicating to the system if it believes the |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1474 | * 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 Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1484 | public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) { |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1485 | try { |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1486 | return mService.setDynamicPowerSaveHint(powerSaveHint, disableThreshold); |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1487 | } catch (RemoteException e) { |
| 1488 | throw e.rethrowFromSystemServer(); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /** |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 1493 | * 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 Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1535 | * Indicates automatic battery saver toggling by the system will be based on percentage. |
| 1536 | * |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1537 | * @see PowerManager#getPowerSaveModeTrigger() |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1538 | * |
| 1539 | * @hide |
| 1540 | */ |
| 1541 | @SystemApi |
| 1542 | @TestApi |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1543 | public static final int POWER_SAVE_MODE_TRIGGER_PERCENTAGE = 0; |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1544 | |
| 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 Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1549 | * @see PowerManager#setDynamicPowerSaveHint(boolean, int) |
| 1550 | * @see PowerManager#getPowerSaveModeTrigger() |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1551 | * |
| 1552 | * @hide |
| 1553 | */ |
| 1554 | @SystemApi |
| 1555 | @TestApi |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1556 | public static final int POWER_SAVE_MODE_TRIGGER_DYNAMIC = 1; |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1557 | |
| 1558 | /** @hide */ |
| 1559 | @Retention(RetentionPolicy.SOURCE) |
| 1560 | @IntDef(value = { |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1561 | POWER_SAVE_MODE_TRIGGER_PERCENTAGE, |
| 1562 | POWER_SAVE_MODE_TRIGGER_DYNAMIC |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1563 | |
| 1564 | }) |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1565 | public @interface AutoPowerSaveModeTriggers {} |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1566 | |
| 1567 | |
| 1568 | /** |
| 1569 | * Returns the current battery saver control mode. Values it may return are defined in |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1570 | * AutoPowerSaveModeTriggers. Note that this is a global device state, not a per user setting. |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1571 | * |
| 1572 | * @return The current value power saver mode for the system. |
| 1573 | * |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1574 | * @see AutoPowerSaveModeTriggers |
| 1575 | * @see PowerManager#getPowerSaveModeTrigger() |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1576 | * @hide |
| 1577 | */ |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1578 | @AutoPowerSaveModeTriggers |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1579 | @SystemApi |
| 1580 | @TestApi |
| 1581 | @RequiresPermission(android.Manifest.permission.POWER_SAVER) |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1582 | public int getPowerSaveModeTrigger() { |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1583 | try { |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 1584 | return mService.getPowerSaveModeTrigger(); |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1585 | } catch (RemoteException e) { |
| 1586 | throw e.rethrowFromSystemServer(); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | /** |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1591 | * Get data about the battery saver mode for a specific service |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1592 | * @param serviceType unique key for the service, one of {@link ServiceType} |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1593 | * @return Battery saver state data. |
| 1594 | * |
| 1595 | * @hide |
Kweku Adams | 7fb72a4 | 2019-01-08 16:08:49 -0800 | [diff] [blame] | 1596 | * @see com.android.server.power.batterysaver.BatterySaverPolicy |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1597 | * @see PowerSaveState |
| 1598 | */ |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1599 | public PowerSaveState getPowerSaveState(@ServiceType int serviceType) { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1600 | try { |
| 1601 | return mService.getPowerSaveState(serviceType); |
| 1602 | } catch (RemoteException e) { |
| 1603 | throw e.rethrowFromSystemServer(); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | /** |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1608 | * 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 Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 1618 | final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION); |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1619 | if (!powerSaveState.batterySaverEnabled) { |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1620 | return LOCATION_MODE_NO_CHANGE; |
| 1621 | } |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 1622 | return powerSaveState.locationMode; |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1626 | * 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 Hackborn | ece0f4f | 2015-06-11 13:29:01 -0700 | [diff] [blame] | 1632 | * @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 Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1636 | */ |
| 1637 | public boolean isDeviceIdleMode() { |
| 1638 | try { |
| 1639 | return mService.isDeviceIdleMode(); |
| 1640 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1641 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /** |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1646 | * 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 1657 | @UnsupportedAppUsage |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1658 | public boolean isLightDeviceIdleMode() { |
| 1659 | try { |
| 1660 | return mService.isLightDeviceIdleMode(); |
| 1661 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1662 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | /** |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1667 | * 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 Onuki | 152742f | 2019-10-31 17:22:26 -0700 | [diff] [blame] | 1672 | return getDeviceIdleManager().isApplicationWhitelisted(packageName); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /** |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1676 | * Turn off the device. |
| 1677 | * |
| 1678 | * @param confirm If true, shows a shutdown confirmation dialog. |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1679 | * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null. |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1680 | * @param wait If true, this call waits for the shutdown to complete and does not return. |
| 1681 | * |
| 1682 | * @hide |
| 1683 | */ |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1684 | public void shutdown(boolean confirm, String reason, boolean wait) { |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1685 | try { |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1686 | mService.shutdown(confirm, reason, wait); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1687 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1688 | throw e.rethrowFromSystemServer(); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /** |
Ruchi Kandoi | c45908d | 2016-05-16 10:23:15 -0700 | [diff] [blame] | 1693 | * 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 Kandoi | b4aa2e9 | 2016-03-30 12:07:31 -0700 | [diff] [blame] | 1706 | */ |
| 1707 | public boolean isSustainedPerformanceModeSupported() { |
| 1708 | return mContext.getResources().getBoolean( |
| 1709 | com.android.internal.R.bool.config_sustainedPerformanceModeSupported); |
| 1710 | } |
| 1711 | |
| 1712 | /** |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1713 | * 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 Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1783 | * Listener passed to |
| 1784 | * {@link PowerManager#addThermalStatusListener} and |
| 1785 | * {@link PowerManager#removeThermalStatusListener} |
| 1786 | * to notify caller of thermal status has changed. |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1787 | */ |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1788 | public interface OnThermalStatusChangedListener { |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1789 | |
| 1790 | /** |
| 1791 | * Called when overall thermal throttling status changed. |
| 1792 | * @param status defined in {@link android.os.Temperature}. |
| 1793 | */ |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1794 | void onThermalStatusChanged(@ThermalStatus int status); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1795 | } |
| 1796 | |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1797 | |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1798 | /** |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1799 | * This function adds a listener for thermal status change, listen call back will be |
| 1800 | * enqueued tasks on the main thread |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1801 | * |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1802 | * @param listener listener to be added, |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1803 | */ |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1804 | public void addThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) { |
| 1805 | Preconditions.checkNotNull(listener, "listener cannot be null"); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1806 | synchronized (this) { |
| 1807 | if (mThermalService == null) { |
| 1808 | mThermalService = IThermalService.Stub.asInterface( |
| 1809 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 1810 | } |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1811 | 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 Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1837 | executor.execute(() -> { |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1838 | listener.onThermalStatusChanged(status); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1839 | }); |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1840 | } finally { |
| 1841 | Binder.restoreCallingIdentity(token); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1842 | } |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1843 | } |
| 1844 | }; |
| 1845 | try { |
| 1846 | if (mThermalService.registerThermalStatusListener(internalListener)) { |
| 1847 | mListenerMap.put(listener, internalListener); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1848 | } else { |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1849 | throw new RuntimeException("Listener failed to set"); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1850 | } |
| 1851 | } catch (RemoteException e) { |
| 1852 | throw e.rethrowFromSystemServer(); |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | /** |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1858 | * This function removes a listener for thermal status change |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1859 | * |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1860 | * @param listener listener to be removed |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1861 | */ |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1862 | public void removeThermalStatusListener(@NonNull OnThermalStatusChangedListener listener) { |
| 1863 | Preconditions.checkNotNull(listener, "listener cannot be null"); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1864 | synchronized (this) { |
| 1865 | if (mThermalService == null) { |
| 1866 | mThermalService = IThermalService.Stub.asInterface( |
| 1867 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 1868 | } |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1869 | IThermalStatusListener internalListener = mListenerMap.get(listener); |
| 1870 | Preconditions.checkArgument(internalListener != null, "Listener was not added"); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1871 | try { |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1872 | if (mThermalService.unregisterThermalStatusListener(internalListener)) { |
| 1873 | mListenerMap.remove(listener); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1874 | } else { |
Wei Wang | 7a49ff6 | 2019-03-04 21:13:32 -0800 | [diff] [blame] | 1875 | throw new RuntimeException("Listener failed to remove"); |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1876 | } |
| 1877 | } catch (RemoteException e) { |
| 1878 | throw e.rethrowFromSystemServer(); |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /** |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 1884 | * 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 Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 1897 | * 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 Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 1913 | * 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 Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 1929 | * 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 Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1955 | * 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 Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 1963 | * 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 Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1971 | * 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 Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1979 | * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes. |
| 1980 | * This broadcast is only sent to registered receivers. |
| 1981 | * @hide |
| 1982 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 1983 | @UnsupportedAppUsage |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1984 | @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 Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1989 | * @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 Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1997 | * @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 Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 2005 | * 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2010 | @UnsupportedAppUsage |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 2011 | @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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2016 | @UnsupportedAppUsage |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 2017 | public static final String EXTRA_POWER_SAVE_MODE = "mode"; |
| 2018 | |
| 2019 | /** |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2020 | * 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2039 | * A wake lock is a mechanism to indicate that your application needs |
| 2040 | * to have the device stay on. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 2041 | * <p> |
| 2042 | * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK} |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 2043 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2044 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2053 | */ |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2054 | public final class WakeLock { |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2055 | @UnsupportedAppUsage |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2056 | private int mFlags; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 2057 | @UnsupportedAppUsage |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2058 | private String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2059 | private final String mPackageName; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2060 | private final IBinder mToken; |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2061 | private int mInternalCount; |
| 2062 | private int mExternalCount; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2063 | private boolean mRefCounted = true; |
| 2064 | private boolean mHeld; |
| 2065 | private WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2066 | private String mHistoryTag; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2067 | private final String mTraceName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2068 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2069 | private final Runnable mReleaser = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2070 | public void run() { |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2071 | release(RELEASE_FLAG_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | } |
| 2073 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2075 | WakeLock(int flags, String tag, String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | mFlags = flags; |
| 2077 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2078 | mPackageName = packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | mToken = new Binder(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2080 | mTraceName = "WakeLock (" + mTag + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2083 | @Override |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2084 | protected void finalize() throws Throwable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2085 | synchronized (mToken) { |
| 2086 | if (mHeld) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2087 | Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2088 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2089 | try { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 2090 | mService.releaseWakeLock(mToken, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2091 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2092 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2097 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2098 | /** |
| 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 Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 2115 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2117 | /** |
| 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 Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2148 | mInternalCount++; |
| 2149 | mExternalCount++; |
| 2150 | if (!mRefCounted || mInternalCount == 1) { |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2151 | // 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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2157 | mHandler.removeCallbacks(mReleaser); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2158 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2159 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2160 | mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource, |
| 2161 | mHistoryTag); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2162 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2163 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2164 | } |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2165 | mHeld = true; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2166 | } |
| 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 Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 2190 | * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported. |
| 2191 | * Passing 0 is equivalent to calling {@link #release()}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2192 | */ |
| 2193 | public void release(int flags) { |
| 2194 | synchronized (mToken) { |
Henrik Baard | 8d475ca | 2017-08-09 14:45:05 +0200 | [diff] [blame] | 2195 | 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 Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2200 | if ((flags & RELEASE_FLAG_TIMEOUT) == 0) { |
| 2201 | mExternalCount--; |
| 2202 | } |
| 2203 | if (!mRefCounted || mInternalCount == 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2204 | mHandler.removeCallbacks(mReleaser); |
| 2205 | if (mHeld) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2206 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2207 | try { |
| 2208 | mService.releaseWakeLock(mToken, flags); |
| 2209 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2210 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2211 | } |
| 2212 | mHeld = false; |
| 2213 | } |
| 2214 | } |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2215 | if (mRefCounted && mExternalCount < 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2216 | 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 Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 2241 | * <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 Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2248 | * @param ws The work source, or null if none. |
| 2249 | */ |
| 2250 | public void setWorkSource(WorkSource ws) { |
| 2251 | synchronized (mToken) { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 2252 | if (ws != null && ws.isEmpty()) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2253 | 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 Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 2264 | changed = !mWorkSource.equals(ws); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2265 | if (changed) { |
| 2266 | mWorkSource.set(ws); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | if (changed && mHeld) { |
| 2271 | try { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2272 | mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2273 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2274 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2280 | /** @hide */ |
| 2281 | public void setTag(String tag) { |
| 2282 | mTag = tag; |
| 2283 | } |
| 2284 | |
| 2285 | /** @hide */ |
Christopher Tate | 4b17e98 | 2016-09-26 12:59:10 -0700 | [diff] [blame] | 2286 | public String getTag() { |
| 2287 | return mTag; |
| 2288 | } |
| 2289 | |
| 2290 | /** @hide */ |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2291 | public void setHistoryTag(String tag) { |
| 2292 | mHistoryTag = tag; |
| 2293 | } |
| 2294 | |
| 2295 | /** @hide */ |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2296 | public void setUnimportantForLogging(boolean state) { |
| 2297 | if (state) mFlags |= UNIMPORTANT_FOR_LOGGING; |
| 2298 | else mFlags &= ~UNIMPORTANT_FOR_LOGGING; |
| 2299 | } |
| 2300 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2301 | @Override |
| 2302 | public String toString() { |
| 2303 | synchronized (mToken) { |
| 2304 | return "WakeLock{" |
| 2305 | + Integer.toHexString(System.identityHashCode(this)) |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2306 | + " held=" + mHeld + ", refCount=" + mInternalCount + "}"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2308 | } |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 2309 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2310 | /** @hide */ |
| 2311 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 2312 | synchronized (mToken) { |
| 2313 | final long token = proto.start(fieldId); |
Yi Jin | 163967f | 2018-03-15 13:49:44 -0700 | [diff] [blame] | 2314 | 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 Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2318 | if (mWorkSource != null) { |
Yi Jin | 163967f | 2018-03-15 13:49:44 -0700 | [diff] [blame] | 2319 | mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2320 | } |
| 2321 | proto.end(token); |
| 2322 | } |
| 2323 | } |
| 2324 | |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 2325 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2354 | } |