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; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 29 | import android.service.dreams.Sandman; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 30 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.util.Log; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 32 | import android.util.proto.ProtoOutputStream; |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 33 | |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 34 | import com.android.internal.util.Preconditions; |
| 35 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 38 | import java.util.concurrent.Executor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 41 | * This class gives you control of the power state of the device. |
| 42 | * |
| 43 | * <p> |
| 44 | * <b>Device battery life will be significantly affected by the use of this API.</b> |
| 45 | * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels |
| 46 | * possible, and be sure to release them as soon as possible. |
| 47 | * </p><p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 48 | * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}. |
| 49 | * This will create a {@link PowerManager.WakeLock} object. You can then use methods |
| 50 | * on the wake lock object to control the power state of the device. |
| 51 | * </p><p> |
| 52 | * In practice it's quite simple: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | * {@samplecode |
| 54 | * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 55 | * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); |
| 56 | * wl.acquire(); |
| 57 | * ..screen will stay on during this section.. |
| 58 | * wl.release(); |
| 59 | * } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 60 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 61 | * The following wake lock levels are defined, with varying effects on system power. |
| 62 | * <i>These levels are mutually exclusive - you may only specify one of them.</i> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 64 | * <table> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 65 | * <tr><th>Flag Value</th> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | * <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] | 67 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 68 | * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 69 | * <td>On*</td> <td>Off</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 71 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 72 | * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 73 | * <td>On</td> <td>Dim</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | * </tr> |
| 75 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 76 | * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 77 | * <td>On</td> <td>Bright</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 79 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 80 | * <tr><td>{@link #FULL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 81 | * <td>On</td> <td>Bright</td> <td>Bright</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * </table> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 84 | * </p><p> |
| 85 | * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any |
| 86 | * display timeouts or the state of the screen and even after the user presses the power button. |
| 87 | * In all other wake locks, the CPU will run, but the user can still put the device to sleep |
| 88 | * using the power button.</i> |
| 89 | * </p><p> |
| 90 | * In addition, you can add two more flags, which affect behavior of the screen only. |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 91 | * <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] | 92 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 93 | * <table> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | * <tr><th>Flag Value</th> <th>Description</th></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 96 | * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause |
| 98 | * the illumination to remain on once it turns on (e.g. from user activity). This flag |
| 99 | * will force the screen and/or keyboard to turn on immediately, when the WakeLock is |
| 100 | * acquired. A typical use would be for notifications which are important for the user to |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 101 | * see immediately.</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 103 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 104 | * <tr><td>{@link #ON_AFTER_RELEASE}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | * <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] | 106 | * released, causing the illumination to remain on a bit longer. This can be used to |
| 107 | * reduce flicker if you are cycling between wake lock conditions.</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * </table> |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 110 | * <p> |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 111 | * 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] | 112 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 113 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 115 | @SystemService(Context.POWER_SERVICE) |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 116 | public final class PowerManager { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private static final String TAG = "PowerManager"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 118 | |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 119 | /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few |
| 120 | * combinations were actually supported so the bit field was removed. This explains |
| 121 | * why the numbering scheme is so odd. If adding a new wake lock level, any unused |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 122 | * 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] | 123 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 126 | * Wake lock level: Ensures that the CPU is running; the screen and keyboard |
| 127 | * backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 128 | * <p> |
| 129 | * If the user presses the power button, then the screen will be turned off |
| 130 | * but the CPU will be kept on until all partial wake locks have been released. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 131 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 133 | 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] | 134 | |
| 135 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 136 | * 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] | 137 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 138 | * <p> |
| 139 | * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be |
| 140 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 141 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 142 | * </p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 143 | * |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 144 | * @deprecated Most applications should use |
| 145 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 146 | * of this type of wake lock, as it will be correctly managed by the platform |
| 147 | * as the user moves between applications and doesn't require a special permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 149 | @Deprecated |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 150 | 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] | 151 | |
| 152 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 153 | * 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] | 154 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 155 | * <p> |
| 156 | * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be |
| 157 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 158 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 159 | * </p> |
| 160 | * |
| 161 | * @deprecated Most applications should use |
| 162 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 163 | * of this type of wake lock, as it will be correctly managed by the platform |
| 164 | * as the user moves between applications and doesn't require a special permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 166 | @Deprecated |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 167 | public static final int SCREEN_BRIGHT_WAKE_LOCK = |
| 168 | OsProtoEnums.SCREEN_BRIGHT_WAKE_LOCK; // 0x0000000a |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Wake lock level: Ensures that the screen and keyboard backlight are on at |
| 172 | * full brightness. |
| 173 | * <p> |
| 174 | * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be |
| 175 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 176 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 177 | * </p> |
| 178 | * |
| 179 | * @deprecated Most applications should use |
| 180 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 181 | * of this type of wake lock, as it will be correctly managed by the platform |
| 182 | * as the user moves between applications and doesn't require a special permission. |
| 183 | */ |
| 184 | @Deprecated |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 185 | 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] | 186 | |
| 187 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 188 | * Wake lock level: Turns the screen off when the proximity sensor activates. |
| 189 | * <p> |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 190 | * If the proximity sensor detects that an object is nearby, the screen turns off |
| 191 | * immediately. Shortly after the object moves away, the screen turns on again. |
| 192 | * </p><p> |
| 193 | * A proximity wake lock does not prevent the device from falling asleep |
| 194 | * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and |
| 195 | * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other |
| 196 | * wake locks are held, then the device will fall asleep (and lock) as usual. |
| 197 | * However, the device will not fall asleep while the screen has been turned off |
| 198 | * by the proximity sensor because it effectively counts as ongoing user activity. |
| 199 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 200 | * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported} |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 201 | * to determine whether this wake lock level is supported. |
Craig Mautner | 6edb6db | 2012-11-20 18:21:12 -0800 | [diff] [blame] | 202 | * </p><p> |
| 203 | * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 204 | * </p> |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 205 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 206 | public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = |
| 207 | OsProtoEnums.PROXIMITY_SCREEN_OFF_WAKE_LOCK; // 0x00000020 |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 210 | * Wake lock level: Put the screen in a low power state and allow the CPU to suspend |
| 211 | * if no other wake locks are held. |
| 212 | * <p> |
| 213 | * This is used by the dream manager to implement doze mode. It currently |
| 214 | * has no effect unless the power manager is in the dozing state. |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 215 | * </p><p> |
| 216 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 217 | * </p> |
| 218 | * |
| 219 | * {@hide} |
| 220 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 221 | public static final int DOZE_WAKE_LOCK = OsProtoEnums.DOZE_WAKE_LOCK; // 0x00000040 |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 222 | |
| 223 | /** |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 224 | * Wake lock level: Keep the device awake enough to allow drawing to occur. |
| 225 | * <p> |
| 226 | * This is used by the window manager to allow applications to draw while the |
| 227 | * system is dozing. It currently has no effect unless the power manager is in |
| 228 | * the dozing state. |
| 229 | * </p><p> |
| 230 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 231 | * </p> |
| 232 | * |
| 233 | * {@hide} |
| 234 | */ |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 235 | public static final int DRAW_WAKE_LOCK = OsProtoEnums.DRAW_WAKE_LOCK; // 0x00000080 |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 236 | |
| 237 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 238 | * 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] | 239 | * |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 240 | * @hide |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 241 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 242 | public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff; |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 243 | |
| 244 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 245 | * Wake lock flag: Turn the screen on when the wake lock is acquired. |
| 246 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | * Normally wake locks don't actually wake the device, they just cause |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 248 | * the screen to remain on once it's already on. Think of the video player |
| 249 | * application as the normal behavior. Notifications that pop up and want |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * 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] | 251 | * </p><p> |
| 252 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 253 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 255 | public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 258 | * 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] | 259 | * so the screen stays on for a little longer. |
| 260 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 261 | * Will not turn the screen on if it is not already on. |
| 262 | * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that. |
| 263 | * </p><p> |
| 264 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 265 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 267 | public static final int ON_AFTER_RELEASE = 0x20000000; |
| 268 | |
| 269 | /** |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 270 | * Wake lock flag: This wake lock is not important for logging events. If a later |
| 271 | * wake lock is acquired that is important, it will be considered the one to log. |
| 272 | * @hide |
| 273 | */ |
| 274 | public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000; |
| 275 | |
| 276 | /** |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 277 | * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a |
| 278 | * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor |
| 279 | * indicates that an object is not in close proximity. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 280 | */ |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 281 | public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0; |
| 282 | |
| 283 | /** |
| 284 | * Flag for {@link WakeLock#release(int)} when called due to timeout. |
| 285 | * @hide |
| 286 | */ |
| 287 | public static final int RELEASE_FLAG_TIMEOUT = 1 << 16; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 290 | * Brightness value for fully on. |
| 291 | * @hide |
| 292 | */ |
| 293 | public static final int BRIGHTNESS_ON = 255; |
| 294 | |
| 295 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 296 | * Brightness value for fully off. |
| 297 | * @hide |
| 298 | */ |
| 299 | public static final int BRIGHTNESS_OFF = 0; |
| 300 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 301 | /** |
| 302 | * Brightness value for default policy handling by the system. |
| 303 | * @hide |
| 304 | */ |
| 305 | public static final int BRIGHTNESS_DEFAULT = -1; |
| 306 | |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 307 | // Note: Be sure to update android.os.BatteryStats and PowerManager.h |
| 308 | // if adding or modifying user activity event constants. |
| 309 | |
| 310 | /** |
| 311 | * User activity event type: Unspecified event type. |
| 312 | * @hide |
| 313 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 314 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 315 | public static final int USER_ACTIVITY_EVENT_OTHER = 0; |
| 316 | |
| 317 | /** |
| 318 | * User activity event type: Button or key pressed or released. |
| 319 | * @hide |
| 320 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 321 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 322 | public static final int USER_ACTIVITY_EVENT_BUTTON = 1; |
| 323 | |
| 324 | /** |
| 325 | * User activity event type: Touch down, move or up. |
| 326 | * @hide |
| 327 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 328 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 329 | public static final int USER_ACTIVITY_EVENT_TOUCH = 2; |
| 330 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 331 | /** |
Phil Weaver | da80d67 | 2016-03-15 16:25:46 -0700 | [diff] [blame] | 332 | * User activity event type: Accessibility taking action on behalf of user. |
| 333 | * @hide |
| 334 | */ |
| 335 | @SystemApi |
| 336 | public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3; |
| 337 | |
| 338 | /** |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 339 | * User activity event type: {@link android.service.attention.AttentionService} taking action |
| 340 | * on behalf of user. |
| 341 | * @hide |
| 342 | */ |
| 343 | public static final int USER_ACTIVITY_EVENT_ATTENTION = 4; |
| 344 | |
| 345 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 346 | * User activity flag: If already dimmed, extend the dim timeout |
| 347 | * but do not brighten. This flag is useful for keeping the screen on |
| 348 | * a little longer without causing a visible change such as when |
| 349 | * the power key is pressed. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 350 | * @hide |
| 351 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 352 | @SystemApi |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 353 | public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0; |
| 354 | |
| 355 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 356 | * User activity flag: Note the user activity as usual but do not |
| 357 | * reset the user activity timeout. This flag is useful for applying |
| 358 | * user activity power hints when interacting with the device indirectly |
| 359 | * on a secondary screen while allowing the primary screen to go to sleep. |
| 360 | * @hide |
| 361 | */ |
| 362 | @SystemApi |
| 363 | public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1; |
| 364 | |
| 365 | /** |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 366 | * @hide |
| 367 | */ |
| 368 | public static final int GO_TO_SLEEP_REASON_MIN = 0; |
| 369 | |
| 370 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 371 | * Go to sleep reason code: Going to sleep due by application request. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 372 | * @hide |
| 373 | */ |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 374 | 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] | 375 | |
| 376 | /** |
| 377 | * Go to sleep reason code: Going to sleep due by request of the |
| 378 | * device administration policy. |
| 379 | * @hide |
| 380 | */ |
| 381 | public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1; |
| 382 | |
| 383 | /** |
| 384 | * Go to sleep reason code: Going to sleep due to a screen timeout. |
| 385 | * @hide |
| 386 | */ |
| 387 | public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2; |
| 388 | |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 389 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 390 | * Go to sleep reason code: Going to sleep due to the lid switch being closed. |
| 391 | * @hide |
| 392 | */ |
| 393 | public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3; |
| 394 | |
| 395 | /** |
| 396 | * Go to sleep reason code: Going to sleep due to the power button being pressed. |
| 397 | * @hide |
| 398 | */ |
| 399 | public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4; |
| 400 | |
| 401 | /** |
| 402 | * Go to sleep reason code: Going to sleep due to HDMI. |
| 403 | * @hide |
| 404 | */ |
| 405 | public static final int GO_TO_SLEEP_REASON_HDMI = 5; |
| 406 | |
| 407 | /** |
Filip Gruszczynski | 9779e12 | 2015-03-13 17:39:31 -0700 | [diff] [blame] | 408 | * Go to sleep reason code: Going to sleep due to the sleep button being pressed. |
| 409 | * @hide |
| 410 | */ |
| 411 | public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6; |
| 412 | |
| 413 | /** |
Eugene Susla | f9a651d | 2017-10-11 12:06:27 -0700 | [diff] [blame] | 414 | * Go to sleep reason code: Going to sleep by request of an accessibility service |
| 415 | * @hide |
| 416 | */ |
| 417 | public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7; |
| 418 | |
| 419 | /** |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 420 | * Go to sleep reason code: Going to sleep due to force-suspend. |
| 421 | * @hide |
| 422 | */ |
| 423 | public static final int GO_TO_SLEEP_REASON_FORCE_SUSPEND = 8; |
| 424 | |
| 425 | /** |
| 426 | * @hide |
| 427 | */ |
| 428 | public static final int GO_TO_SLEEP_REASON_MAX = GO_TO_SLEEP_REASON_FORCE_SUSPEND; |
| 429 | |
| 430 | /** |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 431 | * @hide |
| 432 | */ |
| 433 | public static String sleepReasonToString(int sleepReason) { |
| 434 | switch (sleepReason) { |
| 435 | case GO_TO_SLEEP_REASON_APPLICATION: return "application"; |
| 436 | case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin"; |
| 437 | case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout"; |
| 438 | case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch"; |
| 439 | case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button"; |
| 440 | case GO_TO_SLEEP_REASON_HDMI: return "hdmi"; |
| 441 | case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button"; |
| 442 | case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility"; |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 443 | case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend"; |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 444 | default: return Integer.toString(sleepReason); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 449 | * Go to sleep flag: Skip dozing state and directly go to full sleep. |
| 450 | * @hide |
| 451 | */ |
| 452 | public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0; |
| 453 | |
| 454 | /** |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 455 | * @hide |
| 456 | */ |
| 457 | @IntDef(prefix = { "WAKE_REASON_" }, value = { |
| 458 | WAKE_REASON_UNKNOWN, |
| 459 | WAKE_REASON_POWER_BUTTON, |
| 460 | WAKE_REASON_APPLICATION, |
| 461 | WAKE_REASON_PLUGGED_IN, |
| 462 | WAKE_REASON_GESTURE, |
| 463 | WAKE_REASON_CAMERA_LAUNCH, |
| 464 | WAKE_REASON_WAKE_KEY, |
| 465 | WAKE_REASON_WAKE_MOTION, |
| 466 | WAKE_REASON_HDMI, |
| 467 | }) |
| 468 | @Retention(RetentionPolicy.SOURCE) |
| 469 | public @interface WakeReason{} |
| 470 | |
| 471 | /** |
| 472 | * Wake up reason code: Waking for an unknown reason. |
| 473 | * @hide |
| 474 | */ |
| 475 | public static final int WAKE_REASON_UNKNOWN = 0; |
| 476 | |
| 477 | /** |
| 478 | * Wake up reason code: Waking up due to power button press. |
| 479 | * @hide |
| 480 | */ |
| 481 | public static final int WAKE_REASON_POWER_BUTTON = 1; |
| 482 | |
| 483 | /** |
| 484 | * Wake up reason code: Waking up because an application requested it. |
| 485 | * @hide |
| 486 | */ |
| 487 | public static final int WAKE_REASON_APPLICATION = 2; |
| 488 | |
| 489 | /** |
| 490 | * Wake up reason code: Waking up due to being plugged in or docked on a wireless charger. |
| 491 | * @hide |
| 492 | */ |
| 493 | public static final int WAKE_REASON_PLUGGED_IN = 3; |
| 494 | |
| 495 | /** |
| 496 | * Wake up reason code: Waking up due to a user performed gesture (e.g. douple tapping on the |
| 497 | * screen). |
| 498 | * @hide |
| 499 | */ |
| 500 | public static final int WAKE_REASON_GESTURE = 4; |
| 501 | |
| 502 | /** |
| 503 | * Wake up reason code: Waking up due to the camera being launched. |
| 504 | * @hide |
| 505 | */ |
| 506 | public static final int WAKE_REASON_CAMERA_LAUNCH = 5; |
| 507 | |
| 508 | /** |
| 509 | * Wake up reason code: Waking up because a wake key other than power was pressed. |
| 510 | * @hide |
| 511 | */ |
| 512 | public static final int WAKE_REASON_WAKE_KEY = 6; |
| 513 | |
| 514 | /** |
| 515 | * Wake up reason code: Waking up because a wake motion was performed. |
| 516 | * |
| 517 | * For example, a trackball that was set to wake the device up was spun. |
| 518 | * @hide |
| 519 | */ |
| 520 | public static final int WAKE_REASON_WAKE_MOTION = 7; |
| 521 | |
| 522 | /** |
| 523 | * Wake up reason code: Waking due to HDMI. |
| 524 | * @hide |
| 525 | */ |
| 526 | public static final int WAKE_REASON_HDMI = 8; |
| 527 | |
| 528 | /** |
| 529 | * Wake up reason code: Waking due to the lid being opened. |
| 530 | * @hide |
| 531 | */ |
| 532 | public static final int WAKE_REASON_LID = 9; |
| 533 | |
| 534 | /** |
| 535 | * Convert the wake reason to a string for debugging purposes. |
| 536 | * @hide |
| 537 | */ |
| 538 | public static String wakeReasonToString(@WakeReason int wakeReason) { |
| 539 | switch (wakeReason) { |
| 540 | case WAKE_REASON_UNKNOWN: return "WAKE_REASON_UNKNOWN"; |
| 541 | case WAKE_REASON_POWER_BUTTON: return "WAKE_REASON_POWER_BUTTON"; |
| 542 | case WAKE_REASON_APPLICATION: return "WAKE_REASON_APPLICATION"; |
| 543 | case WAKE_REASON_PLUGGED_IN: return "WAKE_REASON_PLUGGED_IN"; |
| 544 | case WAKE_REASON_GESTURE: return "WAKE_REASON_GESTURE"; |
| 545 | case WAKE_REASON_CAMERA_LAUNCH: return "WAKE_REASON_CAMERA_LAUNCH"; |
| 546 | case WAKE_REASON_WAKE_KEY: return "WAKE_REASON_WAKE_KEY"; |
| 547 | case WAKE_REASON_WAKE_MOTION: return "WAKE_REASON_WAKE_MOTION"; |
| 548 | case WAKE_REASON_HDMI: return "WAKE_REASON_HDMI"; |
| 549 | case WAKE_REASON_LID: return "WAKE_REASON_LID"; |
| 550 | default: return Integer.toString(wakeReason); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 555 | * The value to pass as the 'reason' argument to reboot() to reboot into |
| 556 | * recovery mode for tasks other than applying system updates, such as |
| 557 | * doing factory resets. |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 558 | * <p> |
| 559 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 560 | * permission (in addition to |
| 561 | * {@link android.Manifest.permission#REBOOT}). |
| 562 | * </p> |
Doug Zongker | 183415e | 2014-08-12 10:18:40 -0700 | [diff] [blame] | 563 | * @hide |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 564 | */ |
| 565 | public static final String REBOOT_RECOVERY = "recovery"; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 566 | |
| 567 | /** |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 568 | * The value to pass as the 'reason' argument to reboot() to reboot into |
| 569 | * recovery mode for applying system updates. |
| 570 | * <p> |
| 571 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 572 | * permission (in addition to |
| 573 | * {@link android.Manifest.permission#REBOOT}). |
| 574 | * </p> |
| 575 | * @hide |
| 576 | */ |
| 577 | public static final String REBOOT_RECOVERY_UPDATE = "recovery-update"; |
| 578 | |
| 579 | /** |
Mahaver Chopra | 1ce53bc | 2015-12-14 13:35:14 +0000 | [diff] [blame] | 580 | * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on |
| 581 | * the device. |
| 582 | * @hide |
| 583 | */ |
| 584 | public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner"; |
| 585 | |
| 586 | /** |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 587 | * The 'reason' value used when rebooting in safe mode |
| 588 | * @hide |
| 589 | */ |
| 590 | public static final String REBOOT_SAFE_MODE = "safemode"; |
| 591 | |
| 592 | /** |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 593 | * The 'reason' value used when rebooting the device without turning on the screen. |
| 594 | * @hide |
| 595 | */ |
| 596 | public static final String REBOOT_QUIESCENT = "quiescent"; |
| 597 | |
| 598 | /** |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 599 | * The value to pass as the 'reason' argument to android_reboot(). |
| 600 | * @hide |
| 601 | */ |
| 602 | public static final String SHUTDOWN_USER_REQUESTED = "userrequested"; |
| 603 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 604 | /** |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 605 | * The value to pass as the 'reason' argument to android_reboot() when battery temperature |
| 606 | * is too high. |
| 607 | * @hide |
| 608 | */ |
| 609 | public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery"; |
| 610 | |
| 611 | /** |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 612 | * The value to pass as the 'reason' argument to android_reboot() when device temperature |
| 613 | * is too high. |
| 614 | * @hide |
| 615 | */ |
| 616 | public static final String SHUTDOWN_THERMAL_STATE = "thermal"; |
| 617 | |
| 618 | /** |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 619 | * The value to pass as the 'reason' argument to android_reboot() when device is running |
| 620 | * critically low on battery. |
| 621 | * @hide |
| 622 | */ |
| 623 | public static final String SHUTDOWN_LOW_BATTERY = "battery"; |
| 624 | |
| 625 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 626 | * @hide |
| 627 | */ |
| 628 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 629 | @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 630 | SHUTDOWN_REASON_UNKNOWN, |
| 631 | SHUTDOWN_REASON_SHUTDOWN, |
| 632 | SHUTDOWN_REASON_REBOOT, |
| 633 | SHUTDOWN_REASON_USER_REQUESTED, |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 634 | SHUTDOWN_REASON_THERMAL_SHUTDOWN, |
| 635 | SHUTDOWN_REASON_LOW_BATTERY, |
| 636 | SHUTDOWN_REASON_BATTERY_THERMAL |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 637 | }) |
| 638 | public @interface ShutdownReason {} |
| 639 | |
| 640 | /** |
| 641 | * constant for shutdown reason being unknown. |
| 642 | * @hide |
| 643 | */ |
| 644 | public static final int SHUTDOWN_REASON_UNKNOWN = 0; |
| 645 | |
| 646 | /** |
| 647 | * constant for shutdown reason being normal shutdown. |
| 648 | * @hide |
| 649 | */ |
| 650 | public static final int SHUTDOWN_REASON_SHUTDOWN = 1; |
| 651 | |
| 652 | /** |
| 653 | * constant for shutdown reason being reboot. |
| 654 | * @hide |
| 655 | */ |
| 656 | public static final int SHUTDOWN_REASON_REBOOT = 2; |
| 657 | |
| 658 | /** |
| 659 | * constant for shutdown reason being user requested. |
| 660 | * @hide |
| 661 | */ |
| 662 | public static final int SHUTDOWN_REASON_USER_REQUESTED = 3; |
| 663 | |
| 664 | /** |
| 665 | * constant for shutdown reason being overheating. |
| 666 | * @hide |
| 667 | */ |
| 668 | public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4; |
| 669 | |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 670 | /** |
| 671 | * constant for shutdown reason being low battery. |
| 672 | * @hide |
| 673 | */ |
| 674 | public static final int SHUTDOWN_REASON_LOW_BATTERY = 5; |
| 675 | |
| 676 | /** |
| 677 | * constant for shutdown reason being critical battery thermal state. |
| 678 | * @hide |
| 679 | */ |
| 680 | public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6; |
| 681 | |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 682 | /** |
| 683 | * @hide |
| 684 | */ |
| 685 | @Retention(RetentionPolicy.SOURCE) |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 686 | @IntDef({ServiceType.LOCATION, |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 687 | ServiceType.VIBRATION, |
| 688 | ServiceType.ANIMATION, |
| 689 | ServiceType.FULL_BACKUP, |
| 690 | ServiceType.KEYVALUE_BACKUP, |
| 691 | ServiceType.NETWORK_FIREWALL, |
| 692 | ServiceType.SCREEN_BRIGHTNESS, |
| 693 | ServiceType.SOUND, |
| 694 | ServiceType.BATTERY_STATS, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 695 | ServiceType.DATA_SAVER, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 696 | ServiceType.FORCE_ALL_APPS_STANDBY, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 697 | ServiceType.OPTIONAL_SENSORS, |
Lucas Dupin | 92a62e5 | 2018-01-30 17:22:20 -0800 | [diff] [blame] | 698 | ServiceType.AOD, |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 699 | ServiceType.QUICK_DOZE, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 700 | }) |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 701 | public @interface ServiceType { |
| 702 | int NULL = 0; |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 703 | int LOCATION = 1; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 704 | int VIBRATION = 2; |
| 705 | int ANIMATION = 3; |
| 706 | int FULL_BACKUP = 4; |
| 707 | int KEYVALUE_BACKUP = 5; |
| 708 | int NETWORK_FIREWALL = 6; |
| 709 | int SCREEN_BRIGHTNESS = 7; |
| 710 | int SOUND = 8; |
| 711 | int BATTERY_STATS = 9; |
| 712 | int DATA_SAVER = 10; |
Lucas Dupin | 92a62e5 | 2018-01-30 17:22:20 -0800 | [diff] [blame] | 713 | int AOD = 14; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 714 | |
| 715 | /** |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 716 | * Whether to enable force-app-standby on all apps or not. |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 717 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 718 | int FORCE_ALL_APPS_STANDBY = 11; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 719 | |
| 720 | /** |
Makoto Onuki | e7ec72a | 2017-11-22 11:16:30 -0800 | [diff] [blame] | 721 | * Whether to enable background check on all apps or not. |
| 722 | */ |
| 723 | int FORCE_BACKGROUND_CHECK = 12; |
| 724 | |
| 725 | /** |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 726 | * Whether to disable non-essential sensors. (e.g. edge sensors.) |
| 727 | */ |
| 728 | int OPTIONAL_SENSORS = 13; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 729 | |
| 730 | /** |
| 731 | * Whether to go into Deep Doze as soon as the screen turns off or not. |
| 732 | */ |
| 733 | int QUICK_DOZE = 15; |
Kweku Adams | 4db6a3c | 2019-02-04 16:06:13 -0800 | [diff] [blame] | 734 | |
| 735 | /** |
| 736 | * Whether to enable night mode when battery saver is enabled. |
| 737 | */ |
| 738 | int NIGHT_MODE = 16; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 741 | /** |
| 742 | * Either the location providers shouldn't be affected by battery saver, |
| 743 | * or battery saver is off. |
| 744 | */ |
| 745 | public static final int LOCATION_MODE_NO_CHANGE = 0; |
| 746 | |
| 747 | /** |
| 748 | * In this mode, the GPS based location provider should be disabled when battery saver is on and |
| 749 | * the device is non-interactive. |
| 750 | */ |
| 751 | public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1; |
| 752 | |
| 753 | /** |
| 754 | * All location providers should be disabled when battery saver is on and |
| 755 | * the device is non-interactive. |
| 756 | */ |
| 757 | public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2; |
| 758 | |
| 759 | /** |
| 760 | * In this mode, all the location providers will be kept available, but location fixes |
| 761 | * should only be provided to foreground apps. |
| 762 | */ |
| 763 | public static final int LOCATION_MODE_FOREGROUND_ONLY = 3; |
| 764 | |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 765 | static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE; |
| 766 | static final int MAX_LOCATION_MODE = LOCATION_MODE_FOREGROUND_ONLY; |
| 767 | |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 768 | /** |
| 769 | * @hide |
| 770 | */ |
| 771 | @Retention(RetentionPolicy.SOURCE) |
| 772 | @IntDef(prefix = {"LOCATION_MODE_"}, value = { |
| 773 | LOCATION_MODE_NO_CHANGE, |
| 774 | LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF, |
| 775 | LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF, |
| 776 | LOCATION_MODE_FOREGROUND_ONLY, |
| 777 | }) |
| 778 | public @interface LocationPowerSaveMode {} |
| 779 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 780 | final Context mContext; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 781 | final IPowerManager mService; |
| 782 | final Handler mHandler; |
| 783 | |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 784 | IThermalService mThermalService; |
| 785 | private ArrayMap<ThermalStatusCallback, IThermalStatusListener> mCallbackMap = new ArrayMap<>(); |
| 786 | |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 787 | IDeviceIdleController mIDeviceIdleController; |
| 788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 790 | * {@hide} |
| 791 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 792 | public PowerManager(Context context, IPowerManager service, Handler handler) { |
| 793 | mContext = context; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 794 | mService = service; |
| 795 | mHandler = handler; |
| 796 | } |
| 797 | |
| 798 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 799 | * Gets the minimum supported screen brightness setting. |
| 800 | * The screen may be allowed to become dimmer than this value but |
| 801 | * this is the minimum value that can be set by the user. |
| 802 | * @hide |
| 803 | */ |
| 804 | public int getMinimumScreenBrightnessSetting() { |
| 805 | return mContext.getResources().getInteger( |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 806 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Gets the maximum supported screen brightness setting. |
| 811 | * The screen may be allowed to become dimmer than this value but |
| 812 | * this is the maximum value that can be set by the user. |
| 813 | * @hide |
| 814 | */ |
| 815 | public int getMaximumScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 816 | return mContext.getResources().getInteger( |
| 817 | com.android.internal.R.integer.config_screenBrightnessSettingMaximum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Gets the default screen brightness setting. |
| 822 | * @hide |
| 823 | */ |
| 824 | public int getDefaultScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 825 | return mContext.getResources().getInteger( |
| 826 | com.android.internal.R.integer.config_screenBrightnessSettingDefault); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 830 | * Gets the minimum supported screen brightness setting for VR Mode. |
| 831 | * @hide |
| 832 | */ |
| 833 | public int getMinimumScreenBrightnessForVrSetting() { |
| 834 | return mContext.getResources().getInteger( |
| 835 | com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Gets the maximum supported screen brightness setting for VR Mode. |
| 840 | * The screen may be allowed to become dimmer than this value but |
| 841 | * this is the maximum value that can be set by the user. |
| 842 | * @hide |
| 843 | */ |
| 844 | public int getMaximumScreenBrightnessForVrSetting() { |
| 845 | return mContext.getResources().getInteger( |
| 846 | com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Gets the default screen brightness for VR setting. |
| 851 | * @hide |
| 852 | */ |
| 853 | public int getDefaultScreenBrightnessForVrSetting() { |
| 854 | return mContext.getResources().getInteger( |
| 855 | com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault); |
| 856 | } |
| 857 | |
| 858 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 859 | * Creates a new wake lock with the specified level and flags. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 860 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 861 | * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags |
| 862 | * combined using the logical OR operator. |
| 863 | * </p><p> |
| 864 | * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK}, |
| 865 | * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK} |
| 866 | * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be |
| 867 | * specified as part of the {@code levelAndFlags} parameter. |
| 868 | * </p><p> |
| 869 | * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP} |
| 870 | * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the |
| 871 | * {@code levelAndFlags} parameters. |
| 872 | * </p><p> |
| 873 | * Call {@link WakeLock#acquire() acquire()} on the object to acquire the |
| 874 | * wake lock, and {@link WakeLock#release release()} when you are done. |
| 875 | * </p><p> |
| 876 | * {@samplecode |
| 877 | * PowerManager pm = (PowerManager)mContext.getSystemService( |
| 878 | * Context.POWER_SERVICE); |
| 879 | * PowerManager.WakeLock wl = pm.newWakeLock( |
| 880 | * PowerManager.SCREEN_DIM_WAKE_LOCK |
| 881 | * | PowerManager.ON_AFTER_RELEASE, |
| 882 | * TAG); |
| 883 | * wl.acquire(); |
| 884 | * // ... do work... |
| 885 | * wl.release(); |
| 886 | * } |
| 887 | * </p><p> |
| 888 | * Although a wake lock can be created without special permissions, |
| 889 | * the {@link android.Manifest.permission#WAKE_LOCK} permission is |
| 890 | * required to actually acquire or release the wake lock that is returned. |
| 891 | * </p><p class="note"> |
| 892 | * If using this to keep the screen on, you should strongly consider using |
| 893 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead. |
| 894 | * This window flag will be correctly managed by the platform |
| 895 | * as the user moves between applications and doesn't require a special permission. |
| 896 | * </p> |
| 897 | * |
Olivier Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 898 | * <p> |
| 899 | * Recommended naming conventions for tags to make debugging easier: |
| 900 | * <ul> |
| 901 | * <li>use a unique prefix delimited by a colon for your app/library (e.g. |
| 902 | * gmail:mytag) to make it easier to understand where the wake locks comes |
| 903 | * from. This namespace will also avoid collision for tags inside your app |
| 904 | * coming from different libraries which will make debugging easier. |
| 905 | * <li>use constants (e.g. do not include timestamps in the tag) to make it |
| 906 | * easier for tools to aggregate similar wake locks. When collecting |
| 907 | * debugging data, the platform only monitors a finite number of tags, |
| 908 | * using constants will help tools to provide better debugging data. |
| 909 | * <li>avoid using Class#getName() or similar method since this class name |
| 910 | * can be transformed by java optimizer and obfuscator tools. |
| 911 | * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock |
| 912 | * tags from the platform (e.g. *alarm*). |
| 913 | * <li>never include personnally identifiable information for privacy |
| 914 | * reasons. |
| 915 | * </ul> |
| 916 | * </p> |
| 917 | * |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 918 | * @param levelAndFlags Combination of wake lock level and flag values defining |
| 919 | * the requested behavior of the WakeLock. |
| 920 | * @param tag Your class name (or other tag) for debugging purposes. |
| 921 | * |
| 922 | * @see WakeLock#acquire() |
| 923 | * @see WakeLock#release() |
| 924 | * @see #PARTIAL_WAKE_LOCK |
| 925 | * @see #FULL_WAKE_LOCK |
| 926 | * @see #SCREEN_DIM_WAKE_LOCK |
| 927 | * @see #SCREEN_BRIGHT_WAKE_LOCK |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 928 | * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 929 | * @see #ACQUIRE_CAUSES_WAKEUP |
| 930 | * @see #ON_AFTER_RELEASE |
| 931 | */ |
| 932 | public WakeLock newWakeLock(int levelAndFlags, String tag) { |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 933 | validateWakeLockParameters(levelAndFlags, tag); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 934 | return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName()); |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /** @hide */ |
| 938 | public static void validateWakeLockParameters(int levelAndFlags, String tag) { |
| 939 | switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) { |
| 940 | case PARTIAL_WAKE_LOCK: |
| 941 | case SCREEN_DIM_WAKE_LOCK: |
| 942 | case SCREEN_BRIGHT_WAKE_LOCK: |
| 943 | case FULL_WAKE_LOCK: |
| 944 | case PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 945 | case DOZE_WAKE_LOCK: |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 946 | case DRAW_WAKE_LOCK: |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 947 | break; |
| 948 | default: |
| 949 | throw new IllegalArgumentException("Must specify a valid wake lock level."); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 950 | } |
| 951 | if (tag == null) { |
| 952 | throw new IllegalArgumentException("The tag must not be null."); |
| 953 | } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Notifies the power manager that user activity happened. |
| 958 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 959 | * Resets the auto-off timer and brightens the screen if the device |
| 960 | * is not asleep. This is what happens normally when a key or the touch |
| 961 | * screen is pressed or when some other user activity occurs. |
| 962 | * 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] | 963 | * </p><p> |
| 964 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 965 | * </p> |
| 966 | * |
| 967 | * @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] | 968 | * 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] | 969 | * other power management functions. It should be set |
| 970 | * to the timestamp of the input event that caused the user activity. |
| 971 | * @param noChangeLights If true, does not cause the keyboard backlight to turn on |
| 972 | * because of this event. This is set when the power key is pressed. |
| 973 | * We want the device to stay on while the button is down, but we're about |
| 974 | * to turn off the screen so we don't want the keyboard backlight to turn on again. |
| 975 | * Otherwise the lights flash on and then off and it looks weird. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 976 | * |
| 977 | * @see #wakeUp |
| 978 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 979 | * |
| 980 | * @removed Requires signature or system permission. |
| 981 | * @deprecated Use {@link #userActivity(long, int, int)}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 982 | */ |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 983 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 984 | public void userActivity(long when, boolean noChangeLights) { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 985 | userActivity(when, USER_ACTIVITY_EVENT_OTHER, |
| 986 | noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Notifies the power manager that user activity happened. |
| 991 | * <p> |
| 992 | * Resets the auto-off timer and brightens the screen if the device |
| 993 | * is not asleep. This is what happens normally when a key or the touch |
| 994 | * screen is pressed or when some other user activity occurs. |
| 995 | * This method does not wake up the device if it has been put to sleep. |
| 996 | * </p><p> |
| 997 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} or |
| 998 | * {@link android.Manifest.permission#USER_ACTIVITY} permission. |
| 999 | * </p> |
| 1000 | * |
| 1001 | * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()} |
| 1002 | * time base. This timestamp is used to correctly order the user activity request with |
| 1003 | * other power management functions. It should be set |
| 1004 | * to the timestamp of the input event that caused the user activity. |
| 1005 | * @param event The user activity event. |
| 1006 | * @param flags Optional user activity flags. |
| 1007 | * |
| 1008 | * @see #wakeUp |
| 1009 | * @see #goToSleep |
| 1010 | * |
| 1011 | * @hide Requires signature or system permission. |
| 1012 | */ |
| 1013 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 1014 | @RequiresPermission(anyOf = { |
| 1015 | android.Manifest.permission.DEVICE_POWER, |
| 1016 | android.Manifest.permission.USER_ACTIVITY |
| 1017 | }) |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1018 | public void userActivity(long when, int event, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1019 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1020 | mService.userActivity(when, event, flags); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1021 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1022 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Forces the device to go to sleep. |
| 1028 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1029 | * Overrides all the wake locks that are held. |
| 1030 | * 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] | 1031 | * </p><p> |
| 1032 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1033 | * </p> |
| 1034 | * |
| 1035 | * @param time The time when the request to go to sleep was issued, in the |
| 1036 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1037 | * 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] | 1038 | * 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] | 1039 | * |
| 1040 | * @see #userActivity |
| 1041 | * @see #wakeUp |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1042 | * |
| 1043 | * @removed Requires signature permission. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1044 | */ |
| 1045 | public void goToSleep(long time) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1046 | goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /** |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1050 | * Forces the device to go to sleep. |
| 1051 | * <p> |
| 1052 | * Overrides all the wake locks that are held. |
| 1053 | * This is what happens when the power key is pressed to turn off the screen. |
| 1054 | * </p><p> |
| 1055 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1056 | * </p> |
| 1057 | * |
| 1058 | * @param time The time when the request to go to sleep was issued, in the |
| 1059 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 1060 | * order the go to sleep request with other power management functions. It should be set |
| 1061 | * to the timestamp of the input event that caused the request to go to sleep. |
| 1062 | * @param reason The reason the device is going to sleep. |
| 1063 | * @param flags Optional flags to apply when going to sleep. |
| 1064 | * |
| 1065 | * @see #userActivity |
| 1066 | * @see #wakeUp |
| 1067 | * |
| 1068 | * @hide Requires signature permission. |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1069 | */ |
| 1070 | public void goToSleep(long time, int reason, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1071 | try { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1072 | mService.goToSleep(time, reason, flags); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1073 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1074 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * Forces the device to wake up from sleep. |
| 1080 | * <p> |
| 1081 | * If the device is currently asleep, wakes it up, otherwise does nothing. |
| 1082 | * This is what happens when the power key is pressed to turn on the screen. |
| 1083 | * </p><p> |
| 1084 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1085 | * </p> |
| 1086 | * |
| 1087 | * @param time The time when the request to wake up was issued, in the |
| 1088 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1089 | * 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] | 1090 | * to the timestamp of the input event that caused the request to wake up. |
| 1091 | * |
| 1092 | * @see #userActivity |
| 1093 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1094 | * |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1095 | * @deprecated Use {@link #wakeUp(long, int, String)} instead. |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1096 | * @removed Requires signature permission. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1097 | */ |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1098 | @Deprecated |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1099 | public void wakeUp(long time) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1100 | wakeUp(time, WAKE_REASON_UNKNOWN, "wakeUp"); |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /** |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1104 | * Forces the device to wake up from sleep. |
| 1105 | * <p> |
| 1106 | * If the device is currently asleep, wakes it up, otherwise does nothing. |
| 1107 | * This is what happens when the power key is pressed to turn on the screen. |
| 1108 | * </p><p> |
| 1109 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1110 | * </p> |
| 1111 | * |
| 1112 | * @param time The time when the request to wake up was issued, in the |
| 1113 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 1114 | * order the wake up request with other power management functions. It should be set |
| 1115 | * to the timestamp of the input event that caused the request to wake up. |
| 1116 | * |
| 1117 | * @param details A free form string to explain the specific details behind the wake up for |
| 1118 | * debugging purposes. |
| 1119 | * |
| 1120 | * @see #userActivity |
| 1121 | * @see #goToSleep |
| 1122 | * |
| 1123 | * @deprecated Use {@link #wakeUp(long, int, String)} instead. |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1124 | * @hide |
| 1125 | */ |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1126 | @Deprecated |
| 1127 | public void wakeUp(long time, String details) { |
| 1128 | wakeUp(time, WAKE_REASON_UNKNOWN, details); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Forces the device to wake up from sleep. |
| 1133 | * <p> |
| 1134 | * If the device is currently asleep, wakes it up, otherwise does nothing. |
| 1135 | * This is what happens when the power key is pressed to turn on the screen. |
| 1136 | * </p><p> |
| 1137 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1138 | * </p> |
| 1139 | * |
| 1140 | * @param time The time when the request to wake up was issued, in the |
| 1141 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 1142 | * order the wake up request with other power management functions. It should be set |
| 1143 | * to the timestamp of the input event that caused the request to wake up. |
| 1144 | * |
| 1145 | * @param reason The reason for the wake up. |
| 1146 | * |
| 1147 | * @param details A free form string to explain the specific details behind the wake up for |
| 1148 | * debugging purposes. |
| 1149 | * |
| 1150 | * @see #userActivity |
| 1151 | * @see #goToSleep |
| 1152 | * @hide |
| 1153 | */ |
| 1154 | public void wakeUp(long time, @WakeReason int reason, String details) { |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1155 | try { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1156 | mService.wakeUp(time, reason, details, mContext.getOpPackageName()); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1157 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1158 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /** |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1163 | * Forces the device to start napping. |
| 1164 | * <p> |
| 1165 | * If the device is currently awake, starts dreaming, otherwise does nothing. |
| 1166 | * When the dream ends or if the dream cannot be started, the device will |
| 1167 | * either wake up or go to sleep depending on whether there has been recent |
| 1168 | * user activity. |
| 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 nap was issued, in the |
| 1174 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 1175 | * order the nap request with other power management functions. It should be set |
| 1176 | * to the timestamp of the input event that caused the request to nap. |
| 1177 | * |
| 1178 | * @see #wakeUp |
| 1179 | * @see #goToSleep |
| 1180 | * |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 1181 | * @hide Requires signature permission. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1182 | */ |
| 1183 | public void nap(long time) { |
| 1184 | try { |
| 1185 | mService.nap(time); |
| 1186 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1187 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /** |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1192 | * Requests the device to start dreaming. |
| 1193 | * <p> |
| 1194 | * If dream can not be started, for example if another {@link PowerManager} transition is in |
| 1195 | * progress, does nothing. Unlike {@link #nap(long)}, this does not put device to sleep when |
| 1196 | * dream ends. |
| 1197 | * </p><p> |
Oleg Kibirev | c9752eb | 2018-11-27 13:26:11 -0800 | [diff] [blame] | 1198 | * Requires the {@link android.Manifest.permission#READ_DREAM_STATE} and |
| 1199 | * {@link android.Manifest.permission#WRITE_DREAM_STATE} permissions. |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1200 | * </p> |
| 1201 | * |
| 1202 | * @param time The time when the request to nap was issued, in the |
| 1203 | * {@link SystemClock#uptimeMillis()} time base. This timestamp may be used to correctly |
| 1204 | * order the dream request with other power management functions. It should be set |
| 1205 | * to the timestamp of the input event that caused the request to dream. |
| 1206 | * |
| 1207 | * @hide |
| 1208 | */ |
| 1209 | @SystemApi |
Oleg Kibirev | c9752eb | 2018-11-27 13:26:11 -0800 | [diff] [blame] | 1210 | @RequiresPermission(allOf = { |
| 1211 | android.Manifest.permission.READ_DREAM_STATE, |
| 1212 | android.Manifest.permission.WRITE_DREAM_STATE }) |
Oleg Kibirev | 2385b5e | 2018-11-13 10:43:07 -0800 | [diff] [blame] | 1213 | public void dream(long time) { |
| 1214 | Sandman.startDreamByUserRequest(mContext); |
| 1215 | } |
| 1216 | |
| 1217 | /** |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 1218 | * Boosts the brightness of the screen to maximum for a predetermined |
| 1219 | * period of time. This is used to make the screen more readable in bright |
| 1220 | * daylight for a short duration. |
| 1221 | * <p> |
| 1222 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1223 | * </p> |
| 1224 | * |
| 1225 | * @param time The time when the request to boost was issued, in the |
| 1226 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 1227 | * order the boost request with other power management functions. It should be set |
| 1228 | * to the timestamp of the input event that caused the request to boost. |
| 1229 | * |
| 1230 | * @hide Requires signature permission. |
| 1231 | */ |
| 1232 | public void boostScreenBrightness(long time) { |
| 1233 | try { |
| 1234 | mService.boostScreenBrightness(time); |
| 1235 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1236 | throw e.rethrowFromSystemServer(); |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /** |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1241 | * Returns whether the screen brightness is currently boosted to maximum, caused by a call |
| 1242 | * to {@link #boostScreenBrightness(long)}. |
| 1243 | * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise. |
| 1244 | * |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1245 | * @deprecated This call is rarely used and will be phased out soon. |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1246 | * @hide |
Jeff Sharkey | 8478911 | 2017-08-11 14:45:43 -0600 | [diff] [blame] | 1247 | * @removed |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1248 | */ |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1249 | @SystemApi @Deprecated |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1250 | public boolean isScreenBrightnessBoosted() { |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1251 | return false; |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1254 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1255 | * Returns true if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1256 | * |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1257 | * @param level The wake lock level to check. |
| 1258 | * @return True if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1259 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1260 | public boolean isWakeLockLevelSupported(int level) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1261 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1262 | return mService.isWakeLockLevelSupported(level); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1263 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1264 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1269 | * Returns true if the device is in an interactive state. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1270 | * <p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1271 | * For historical reasons, the name of this method refers to the power state of |
| 1272 | * the screen but it actually describes the overall interactive state of |
| 1273 | * the device. This method has been replaced by {@link #isInteractive}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1274 | * </p><p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1275 | * The value returned by this method only indicates whether the device is |
| 1276 | * in an interactive state which may have nothing to do with the screen being |
| 1277 | * on or off. To determine the actual state of the screen, |
| 1278 | * use {@link android.view.Display#getState}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1279 | * </p> |
| 1280 | * |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1281 | * @return True if the device is in an interactive state. |
| 1282 | * |
| 1283 | * @deprecated Use {@link #isInteractive} instead. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1284 | */ |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1285 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1286 | public boolean isScreenOn() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1287 | return isInteractive(); |
| 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * Returns true if the device is in an interactive state. |
| 1292 | * <p> |
| 1293 | * When this method returns true, the device is awake and ready to interact |
| 1294 | * with the user (although this is not a guarantee that the user is actively |
| 1295 | * interacting with the device just this moment). The main screen is usually |
| 1296 | * turned on while in this state. Certain features, such as the proximity |
| 1297 | * sensor, may temporarily turn off the screen while still leaving the device in an |
| 1298 | * interactive state. Note in particular that the device is still considered |
| 1299 | * to be interactive while dreaming (since dreams can be interactive) but not |
| 1300 | * when it is dozing or asleep. |
| 1301 | * </p><p> |
| 1302 | * When this method returns false, the device is dozing or asleep and must |
| 1303 | * be awoken before it will become ready to interact with the user again. The |
| 1304 | * main screen is usually turned off while in this state. Certain features, |
| 1305 | * such as "ambient mode" may cause the main screen to remain on (albeit in a |
| 1306 | * low power state) to display system-provided content while the device dozes. |
| 1307 | * </p><p> |
| 1308 | * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on} |
| 1309 | * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast |
| 1310 | * whenever the interactive state of the device changes. For historical reasons, |
| 1311 | * the names of these broadcasts refer to the power state of the screen |
| 1312 | * but they are actually sent in response to changes in the overall interactive |
| 1313 | * state of the device, as described by this method. |
| 1314 | * </p><p> |
| 1315 | * Services may use the non-interactive state as a hint to conserve power |
| 1316 | * since the user is not present. |
| 1317 | * </p> |
| 1318 | * |
| 1319 | * @return True if the device is in an interactive state. |
| 1320 | * |
| 1321 | * @see android.content.Intent#ACTION_SCREEN_ON |
| 1322 | * @see android.content.Intent#ACTION_SCREEN_OFF |
| 1323 | */ |
| 1324 | public boolean isInteractive() { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1325 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1326 | return mService.isInteractive(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1327 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1328 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * Reboot the device. Will not return if the reboot is successful. |
| 1334 | * <p> |
| 1335 | * Requires the {@link android.Manifest.permission#REBOOT} permission. |
| 1336 | * </p> |
| 1337 | * |
| 1338 | * @param reason code to pass to the kernel (e.g., "recovery") to |
| 1339 | * request special boot modes, or null. |
| 1340 | */ |
| 1341 | public void reboot(String reason) { |
| 1342 | try { |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 1343 | mService.reboot(false, reason, true); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1344 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1345 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /** |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 1350 | * Reboot the device. Will not return if the reboot is successful. |
| 1351 | * <p> |
| 1352 | * Requires the {@link android.Manifest.permission#REBOOT} permission. |
| 1353 | * </p> |
| 1354 | * @hide |
| 1355 | */ |
| 1356 | public void rebootSafeMode() { |
| 1357 | try { |
| 1358 | mService.rebootSafeMode(false, true); |
| 1359 | } catch (RemoteException e) { |
| 1360 | throw e.rethrowFromSystemServer(); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1365 | * Returns true if the device is currently in power save mode. When in this mode, |
| 1366 | * applications should reduce their functionality in order to conserve battery as |
| 1367 | * much as possible. You can monitor for changes to this state with |
| 1368 | * {@link #ACTION_POWER_SAVE_MODE_CHANGED}. |
| 1369 | * |
| 1370 | * @return Returns true if currently in low power mode, else false. |
| 1371 | */ |
| 1372 | public boolean isPowerSaveMode() { |
| 1373 | try { |
| 1374 | return mService.isPowerSaveMode(); |
| 1375 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1376 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /** |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1381 | * Set the current power save mode. |
| 1382 | * |
| 1383 | * @return True if the set was allowed. |
| 1384 | * |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1385 | * @hide |
Salvador Martinez | a80bbab | 2018-09-24 10:36:11 -0700 | [diff] [blame] | 1386 | * @see #isPowerSaveMode() |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1387 | */ |
Salvador Martinez | a80bbab | 2018-09-24 10:36:11 -0700 | [diff] [blame] | 1388 | @SystemApi |
| 1389 | @TestApi |
| 1390 | @RequiresPermission(anyOf = { |
| 1391 | android.Manifest.permission.DEVICE_POWER, |
| 1392 | android.Manifest.permission.POWER_SAVER |
| 1393 | }) |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1394 | public boolean setPowerSaveMode(boolean mode) { |
| 1395 | try { |
| 1396 | return mService.setPowerSaveMode(mode); |
| 1397 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1398 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /** |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1403 | * Updates the current state of dynamic power savings and disable threshold. This is |
| 1404 | * a signal to the system which an app can update to serve as an indicator that |
| 1405 | * the user will be in a battery critical situation before being able to plug in. |
| 1406 | * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this. |
| 1407 | * This is a device global state, not a per user setting. |
| 1408 | * |
| 1409 | * <p>When enabled, the system may enact various measures for reducing power consumption in |
| 1410 | * order to help ensure that the user will make it to their next charging point. The most |
| 1411 | * visible of these will be the automatic enabling of battery saver if the user has set |
| 1412 | * their battery saver mode to "automatic". Note |
| 1413 | * that this is NOT simply an on/off switch for features, but rather a hint for the |
| 1414 | * system to consider enacting these power saving features, some of which have additional |
| 1415 | * logic around when to activate based on this signal. |
| 1416 | * |
| 1417 | * <p>The provided threshold is the percentage the system should consider itself safe at given |
| 1418 | * the current state of the device. The value is an integer representing a battery level. |
| 1419 | * |
| 1420 | * <p>The threshold is meant to set an explicit stopping point for dynamic power savings |
| 1421 | * functionality so that the dynamic power savings itself remains a signal rather than becoming |
| 1422 | * an on/off switch for a subset of features. |
| 1423 | * @hide |
| 1424 | * |
| 1425 | * @param dynamicPowerSavingsEnabled A signal indicating to the system if it believes the |
| 1426 | * dynamic power savings behaviors should be activated. |
| 1427 | * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic |
| 1428 | * power savings behaviors. |
| 1429 | * @return True if the update was allowed and succeeded. |
| 1430 | * |
| 1431 | * @hide |
| 1432 | */ |
| 1433 | @SystemApi |
| 1434 | @TestApi |
| 1435 | @RequiresPermission(permission.POWER_SAVER) |
| 1436 | public boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled, |
| 1437 | int disableThreshold) { |
| 1438 | try { |
| 1439 | return mService.setDynamicPowerSavings(dynamicPowerSavingsEnabled, disableThreshold); |
| 1440 | } catch (RemoteException e) { |
| 1441 | throw e.rethrowFromSystemServer(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /** |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 1446 | * Sets the policy for adaptive power save. |
| 1447 | * |
| 1448 | * @return true if there was an effectual change. If full battery saver is enabled or the |
| 1449 | * adaptive policy is not enabled, then this will return false. |
| 1450 | * |
| 1451 | * @hide |
| 1452 | */ |
| 1453 | @SystemApi |
| 1454 | @RequiresPermission(anyOf = { |
| 1455 | android.Manifest.permission.DEVICE_POWER, |
| 1456 | android.Manifest.permission.POWER_SAVER |
| 1457 | }) |
| 1458 | public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) { |
| 1459 | try { |
| 1460 | return mService.setAdaptivePowerSavePolicy(config); |
| 1461 | } catch (RemoteException e) { |
| 1462 | throw e.rethrowFromSystemServer(); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * Enables or disables adaptive power save. |
| 1468 | * |
| 1469 | * @return true if there was an effectual change. If full battery saver is enabled, then this |
| 1470 | * will return false. |
| 1471 | * |
| 1472 | * @hide |
| 1473 | */ |
| 1474 | @SystemApi |
| 1475 | @RequiresPermission(anyOf = { |
| 1476 | android.Manifest.permission.DEVICE_POWER, |
| 1477 | android.Manifest.permission.POWER_SAVER |
| 1478 | }) |
| 1479 | public boolean setAdaptivePowerSaveEnabled(boolean enabled) { |
| 1480 | try { |
| 1481 | return mService.setAdaptivePowerSaveEnabled(enabled); |
| 1482 | } catch (RemoteException e) { |
| 1483 | throw e.rethrowFromSystemServer(); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /** |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 1488 | * Indicates automatic battery saver toggling by the system will be based on percentage. |
| 1489 | * |
| 1490 | * @see PowerManager#getPowerSaveMode() |
| 1491 | * |
| 1492 | * @hide |
| 1493 | */ |
| 1494 | @SystemApi |
| 1495 | @TestApi |
| 1496 | public static final int POWER_SAVER_MODE_PERCENTAGE = 0; |
| 1497 | |
| 1498 | /** |
| 1499 | * Indicates automatic battery saver toggling by the system will be based on the state |
| 1500 | * of the dynamic power savings signal. |
| 1501 | * |
| 1502 | * @see PowerManager#setDynamicPowerSavings(boolean, int) |
| 1503 | * @see PowerManager#getPowerSaveMode() |
| 1504 | * |
| 1505 | * @hide |
| 1506 | */ |
| 1507 | @SystemApi |
| 1508 | @TestApi |
| 1509 | public static final int POWER_SAVER_MODE_DYNAMIC = 1; |
| 1510 | |
| 1511 | /** @hide */ |
| 1512 | @Retention(RetentionPolicy.SOURCE) |
| 1513 | @IntDef(value = { |
| 1514 | POWER_SAVER_MODE_PERCENTAGE, |
| 1515 | POWER_SAVER_MODE_DYNAMIC |
| 1516 | |
| 1517 | }) |
| 1518 | public @interface AutoPowerSaverMode{} |
| 1519 | |
| 1520 | |
| 1521 | /** |
| 1522 | * Returns the current battery saver control mode. Values it may return are defined in |
| 1523 | * AutoPowerSaverMode. Note that this is a global device state, not a per user setting. |
| 1524 | * |
| 1525 | * @return The current value power saver mode for the system. |
| 1526 | * |
| 1527 | * @see AutoPowerSaverMode |
| 1528 | * @see PowerManager#getPowerSaveMode() |
| 1529 | * @hide |
| 1530 | */ |
| 1531 | @AutoPowerSaverMode |
| 1532 | @SystemApi |
| 1533 | @TestApi |
| 1534 | @RequiresPermission(android.Manifest.permission.POWER_SAVER) |
| 1535 | public int getPowerSaveMode() { |
| 1536 | try { |
| 1537 | return mService.getPowerSaveMode(); |
| 1538 | } catch (RemoteException e) { |
| 1539 | throw e.rethrowFromSystemServer(); |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | /** |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1544 | * Get data about the battery saver mode for a specific service |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1545 | * @param serviceType unique key for the service, one of {@link ServiceType} |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1546 | * @return Battery saver state data. |
| 1547 | * |
| 1548 | * @hide |
Kweku Adams | 7fb72a4 | 2019-01-08 16:08:49 -0800 | [diff] [blame] | 1549 | * @see com.android.server.power.batterysaver.BatterySaverPolicy |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1550 | * @see PowerSaveState |
| 1551 | */ |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1552 | public PowerSaveState getPowerSaveState(@ServiceType int serviceType) { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1553 | try { |
| 1554 | return mService.getPowerSaveState(serviceType); |
| 1555 | } catch (RemoteException e) { |
| 1556 | throw e.rethrowFromSystemServer(); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /** |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1561 | * Returns how location features should behave when battery saver is on. When battery saver |
| 1562 | * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}. |
| 1563 | * |
| 1564 | * <p>This API is normally only useful for components that provide location features. |
| 1565 | * |
| 1566 | * @see #isPowerSaveMode() |
| 1567 | * @see #ACTION_POWER_SAVE_MODE_CHANGED |
| 1568 | */ |
| 1569 | @LocationPowerSaveMode |
| 1570 | public int getLocationPowerSaveMode() { |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 1571 | final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.LOCATION); |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1572 | if (!powerSaveState.batterySaverEnabled) { |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1573 | return LOCATION_MODE_NO_CHANGE; |
| 1574 | } |
Kweku Adams | 731a103 | 2019-02-04 14:05:41 -0800 | [diff] [blame] | 1575 | return powerSaveState.locationMode; |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1579 | * Returns true if the device is currently in idle mode. This happens when a device |
| 1580 | * has been sitting unused and unmoving for a sufficiently long period of time, so that |
| 1581 | * it decides to go into a lower power-use state. This may involve things like turning |
| 1582 | * off network access to apps. You can monitor for changes to this state with |
| 1583 | * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}. |
| 1584 | * |
Dianne Hackborn | ece0f4f | 2015-06-11 13:29:01 -0700 | [diff] [blame] | 1585 | * @return Returns true if currently in active device idle mode, else false. This is |
| 1586 | * when idle mode restrictions are being actively applied; it will return false if the |
| 1587 | * device is in a long-term idle mode but currently running a maintenance window where |
| 1588 | * restrictions have been lifted. |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1589 | */ |
| 1590 | public boolean isDeviceIdleMode() { |
| 1591 | try { |
| 1592 | return mService.isDeviceIdleMode(); |
| 1593 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1594 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | /** |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1599 | * Returns true if the device is currently in light idle mode. This happens when a device |
| 1600 | * has had its screen off for a short time, switching it into a batching mode where we |
| 1601 | * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to |
| 1602 | * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}. |
| 1603 | * |
| 1604 | * @return Returns true if currently in active light device idle mode, else false. This is |
| 1605 | * when light idle mode restrictions are being actively applied; it will return false if the |
| 1606 | * device is in a long-term idle mode but currently running a maintenance window where |
| 1607 | * restrictions have been lifted. |
| 1608 | * @hide |
| 1609 | */ |
| 1610 | public boolean isLightDeviceIdleMode() { |
| 1611 | try { |
| 1612 | return mService.isLightDeviceIdleMode(); |
| 1613 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1614 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | /** |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1619 | * Return whether the given application package name is on the device's power whitelist. |
| 1620 | * Apps can be placed on the whitelist through the settings UI invoked by |
| 1621 | * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}. |
| 1622 | */ |
| 1623 | public boolean isIgnoringBatteryOptimizations(String packageName) { |
| 1624 | synchronized (this) { |
| 1625 | if (mIDeviceIdleController == null) { |
| 1626 | mIDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1627 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
| 1628 | } |
| 1629 | } |
| 1630 | try { |
| 1631 | return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName); |
| 1632 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1633 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | /** |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1638 | * Turn off the device. |
| 1639 | * |
| 1640 | * @param confirm If true, shows a shutdown confirmation dialog. |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1641 | * @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] | 1642 | * @param wait If true, this call waits for the shutdown to complete and does not return. |
| 1643 | * |
| 1644 | * @hide |
| 1645 | */ |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1646 | public void shutdown(boolean confirm, String reason, boolean wait) { |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1647 | try { |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1648 | mService.shutdown(confirm, reason, wait); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1649 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1650 | throw e.rethrowFromSystemServer(); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | /** |
Ruchi Kandoi | c45908d | 2016-05-16 10:23:15 -0700 | [diff] [blame] | 1655 | * This function checks if the device has implemented Sustained Performance |
| 1656 | * Mode. This needs to be checked only once and is constant for a particular |
| 1657 | * device/release. |
| 1658 | * |
| 1659 | * Sustained Performance Mode is intended to provide a consistent level of |
| 1660 | * performance for prolonged amount of time. |
| 1661 | * |
| 1662 | * Applications should check if the device supports this mode, before using |
| 1663 | * {@link android.view.Window#setSustainedPerformanceMode}. |
| 1664 | * |
| 1665 | * @return Returns True if the device supports it, false otherwise. |
| 1666 | * |
| 1667 | * @see android.view.Window#setSustainedPerformanceMode |
Ruchi Kandoi | b4aa2e9 | 2016-03-30 12:07:31 -0700 | [diff] [blame] | 1668 | */ |
| 1669 | public boolean isSustainedPerformanceModeSupported() { |
| 1670 | return mContext.getResources().getBoolean( |
| 1671 | com.android.internal.R.bool.config_sustainedPerformanceModeSupported); |
| 1672 | } |
| 1673 | |
| 1674 | /** |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1675 | * Thermal status code: Not under throttling. |
| 1676 | */ |
| 1677 | public static final int THERMAL_STATUS_NONE = Temperature.THROTTLING_NONE; |
| 1678 | |
| 1679 | /** |
| 1680 | * Thermal status code: Light throttling where UX is not impacted. |
| 1681 | */ |
| 1682 | public static final int THERMAL_STATUS_LIGHT = Temperature.THROTTLING_LIGHT; |
| 1683 | |
| 1684 | /** |
| 1685 | * Thermal status code: Moderate throttling where UX is not largely impacted. |
| 1686 | */ |
| 1687 | public static final int THERMAL_STATUS_MODERATE = Temperature.THROTTLING_MODERATE; |
| 1688 | |
| 1689 | /** |
| 1690 | * Thermal status code: Severe throttling where UX is largely impacted. |
| 1691 | */ |
| 1692 | public static final int THERMAL_STATUS_SEVERE = Temperature.THROTTLING_SEVERE; |
| 1693 | |
| 1694 | /** |
| 1695 | * Thermal status code: Platform has done everything to reduce power. |
| 1696 | */ |
| 1697 | public static final int THERMAL_STATUS_CRITICAL = Temperature.THROTTLING_CRITICAL; |
| 1698 | |
| 1699 | /** |
| 1700 | * Thermal status code: Key components in platform are shutting down due to thermal condition. |
| 1701 | * Device functionalities will be limited. |
| 1702 | */ |
| 1703 | public static final int THERMAL_STATUS_EMERGENCY = Temperature.THROTTLING_EMERGENCY; |
| 1704 | |
| 1705 | /** |
| 1706 | * Thermal status code: Need shutdown immediately. |
| 1707 | */ |
| 1708 | public static final int THERMAL_STATUS_SHUTDOWN = Temperature.THROTTLING_SHUTDOWN; |
| 1709 | |
| 1710 | /** @hide */ |
| 1711 | @IntDef(prefix = { "THERMAL_STATUS_" }, value = { |
| 1712 | THERMAL_STATUS_NONE, |
| 1713 | THERMAL_STATUS_LIGHT, |
| 1714 | THERMAL_STATUS_MODERATE, |
| 1715 | THERMAL_STATUS_SEVERE, |
| 1716 | THERMAL_STATUS_CRITICAL, |
| 1717 | THERMAL_STATUS_EMERGENCY, |
| 1718 | THERMAL_STATUS_SHUTDOWN, |
| 1719 | }) |
| 1720 | @Retention(RetentionPolicy.SOURCE) |
| 1721 | public @interface ThermalStatus {} |
| 1722 | |
| 1723 | /** |
| 1724 | * This function returns the current thermal status of the device. |
| 1725 | * |
| 1726 | * @return thermal status as int, {@link #THERMAL_STATUS_NONE} if device in not under |
| 1727 | * thermal throttling. |
| 1728 | */ |
| 1729 | public @ThermalStatus int getCurrentThermalStatus() { |
| 1730 | synchronized (this) { |
| 1731 | if (mThermalService == null) { |
| 1732 | mThermalService = IThermalService.Stub.asInterface( |
| 1733 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 1734 | } |
| 1735 | try { |
| 1736 | return mThermalService.getCurrentThermalStatus(); |
| 1737 | } catch (RemoteException e) { |
| 1738 | throw e.rethrowFromSystemServer(); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | } |
| 1743 | |
| 1744 | /** |
| 1745 | * Callback passed to |
| 1746 | * {@link PowerManager#registerThermalStatusCallback} and |
| 1747 | * {@link PowerManager#unregisterThermalStatusCallback} |
| 1748 | * to notify caller of thermal status. |
| 1749 | */ |
| 1750 | public abstract static class ThermalStatusCallback { |
| 1751 | |
| 1752 | /** |
| 1753 | * Called when overall thermal throttling status changed. |
| 1754 | * @param status defined in {@link android.os.Temperature}. |
| 1755 | */ |
| 1756 | public void onStatusChange(@ThermalStatus int status) {} |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * This function registers a callback for thermal status change. |
| 1761 | * |
| 1762 | * @param callback callback to be registered. |
| 1763 | * @param executor {@link Executor} to handle the callbacks. |
| 1764 | */ |
| 1765 | public void registerThermalStatusCallback( |
| 1766 | @NonNull ThermalStatusCallback callback, @NonNull @CallbackExecutor Executor executor) { |
| 1767 | Preconditions.checkNotNull(callback, "callback cannnot be null"); |
| 1768 | Preconditions.checkNotNull(executor, "executor cannnot be null"); |
| 1769 | synchronized (this) { |
| 1770 | if (mThermalService == null) { |
| 1771 | mThermalService = IThermalService.Stub.asInterface( |
| 1772 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 1773 | } |
| 1774 | try { |
| 1775 | if (mCallbackMap.containsKey(callback)) { |
| 1776 | throw new IllegalArgumentException("ThermalStatusCallback already registered"); |
| 1777 | } |
| 1778 | IThermalStatusListener listener = new IThermalStatusListener.Stub() { |
| 1779 | @Override |
| 1780 | public void onStatusChange(int status) { |
| 1781 | executor.execute(() -> { |
| 1782 | callback.onStatusChange(status); |
| 1783 | }); |
| 1784 | } |
| 1785 | }; |
| 1786 | if (mThermalService.registerThermalStatusListener(listener)) { |
| 1787 | mCallbackMap.put(callback, listener); |
| 1788 | } else { |
| 1789 | throw new RuntimeException("ThermalStatusCallback failed to register"); |
| 1790 | } |
| 1791 | } catch (RemoteException e) { |
| 1792 | throw e.rethrowFromSystemServer(); |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * This function unregisters a callback for thermal status change. |
| 1799 | * |
| 1800 | * @param callback to be unregistered. |
| 1801 | * |
| 1802 | * see {@link #registerThermalStatusCallback} |
| 1803 | */ |
Wei Wang | 3dcd3f6 | 2019-02-13 15:46:31 -0800 | [diff] [blame] | 1804 | public void unregisterThermalStatusCallback(@NonNull ThermalStatusCallback callback) { |
Wei Wang | 5947665 | 2018-11-29 16:02:48 -0800 | [diff] [blame] | 1805 | Preconditions.checkNotNull(callback, "callback cannnot be null"); |
| 1806 | synchronized (this) { |
| 1807 | if (mThermalService == null) { |
| 1808 | mThermalService = IThermalService.Stub.asInterface( |
| 1809 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 1810 | } |
| 1811 | try { |
| 1812 | IThermalStatusListener listener = mCallbackMap.get(callback); |
| 1813 | if (listener == null) { |
| 1814 | throw new IllegalArgumentException("ThermalStatusCallback not registered"); |
| 1815 | } |
| 1816 | if (mThermalService.unregisterThermalStatusListener(listener)) { |
| 1817 | mCallbackMap.remove(callback); |
| 1818 | } else { |
| 1819 | throw new RuntimeException("ThermalStatusCallback failed to unregister"); |
| 1820 | } |
| 1821 | } catch (RemoteException e) { |
| 1822 | throw e.rethrowFromSystemServer(); |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /** |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 1828 | * If true, the doze component is not started until after the screen has been |
| 1829 | * turned off and the screen off animation has been performed. |
| 1830 | * @hide |
| 1831 | */ |
| 1832 | public void setDozeAfterScreenOff(boolean dozeAfterScreenOf) { |
| 1833 | try { |
| 1834 | mService.setDozeAfterScreenOff(dozeAfterScreenOf); |
| 1835 | } catch (RemoteException e) { |
| 1836 | throw e.rethrowFromSystemServer(); |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 1841 | * Returns the reason the phone was last shutdown. Calling app must have the |
| 1842 | * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information. |
| 1843 | * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could |
| 1844 | * not be accessed. |
| 1845 | * @hide |
| 1846 | */ |
| 1847 | @ShutdownReason |
| 1848 | public int getLastShutdownReason() { |
| 1849 | try { |
| 1850 | return mService.getLastShutdownReason(); |
| 1851 | } catch (RemoteException e) { |
| 1852 | throw e.rethrowFromSystemServer(); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | /** |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 1857 | * Returns the reason the device last went to sleep (i.e. the last value of |
| 1858 | * the second argument of {@link #goToSleep(long, int, int) goToSleep}). |
| 1859 | * |
| 1860 | * @return One of the {@code GO_TO_SLEEP_REASON_*} constants. |
| 1861 | * |
| 1862 | * @hide |
| 1863 | */ |
| 1864 | public int getLastSleepReason() { |
| 1865 | try { |
| 1866 | return mService.getLastSleepReason(); |
| 1867 | } catch (RemoteException e) { |
| 1868 | throw e.rethrowFromSystemServer(); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /** |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 1873 | * Forces the device to go to suspend, even if there are currently wakelocks being held. |
| 1874 | * <b>Caution</b> |
| 1875 | * This is a very dangerous command as it puts the device to sleep immediately. Apps and parts |
| 1876 | * of the system will not be notified and will not have an opportunity to save state prior to |
| 1877 | * the device going to suspend. |
| 1878 | * This method should only be used in very rare circumstances where the device is intended |
| 1879 | * to appear as completely off to the user and they have a well understood, reliable way of |
| 1880 | * re-enabling it. |
| 1881 | * </p><p> |
| 1882 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 1883 | * </p> |
| 1884 | * |
| 1885 | * @return true on success, false otherwise. |
| 1886 | * @hide |
| 1887 | */ |
| 1888 | @SystemApi |
| 1889 | @RequiresPermission(android.Manifest.permission.DEVICE_POWER) |
| 1890 | public boolean forceSuspend() { |
| 1891 | try { |
| 1892 | return mService.forceSuspend(); |
| 1893 | } catch (RemoteException e) { |
| 1894 | throw e.rethrowFromSystemServer(); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1899 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. |
| 1900 | * This broadcast is only sent to registered receivers. |
| 1901 | */ |
| 1902 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1903 | public static final String ACTION_POWER_SAVE_MODE_CHANGED |
| 1904 | = "android.os.action.POWER_SAVE_MODE_CHANGED"; |
| 1905 | |
| 1906 | /** |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 1907 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. |
| 1908 | * @hide |
| 1909 | */ |
| 1910 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1911 | public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL |
| 1912 | = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL"; |
| 1913 | |
| 1914 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1915 | * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes. |
| 1916 | * This broadcast is only sent to registered receivers. |
| 1917 | */ |
| 1918 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1919 | public static final String ACTION_DEVICE_IDLE_MODE_CHANGED |
| 1920 | = "android.os.action.DEVICE_IDLE_MODE_CHANGED"; |
| 1921 | |
| 1922 | /** |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1923 | * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes. |
| 1924 | * This broadcast is only sent to registered receivers. |
| 1925 | * @hide |
| 1926 | */ |
| 1927 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1928 | public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED |
| 1929 | = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED"; |
| 1930 | |
| 1931 | /** |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1932 | * @hide Intent that is broadcast when the set of power save whitelist apps has changed. |
| 1933 | * This broadcast is only sent to registered receivers. |
| 1934 | */ |
| 1935 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1936 | public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED |
| 1937 | = "android.os.action.POWER_SAVE_WHITELIST_CHANGED"; |
| 1938 | |
| 1939 | /** |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1940 | * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed. |
| 1941 | * This broadcast is only sent to registered receivers. |
| 1942 | */ |
| 1943 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1944 | public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED |
| 1945 | = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED"; |
| 1946 | |
| 1947 | /** |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 1948 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change. |
| 1949 | * This broadcast is only sent to registered receivers. |
| 1950 | * |
| 1951 | * @hide |
| 1952 | */ |
| 1953 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1954 | public static final String ACTION_POWER_SAVE_MODE_CHANGING |
| 1955 | = "android.os.action.POWER_SAVE_MODE_CHANGING"; |
| 1956 | |
| 1957 | /** @hide */ |
| 1958 | public static final String EXTRA_POWER_SAVE_MODE = "mode"; |
| 1959 | |
| 1960 | /** |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1961 | * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed. |
| 1962 | * This broadcast is only sent to registered receivers. |
| 1963 | * |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1964 | * @deprecated This intent is rarely used and will be phased out soon. |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1965 | * @hide |
Jeff Sharkey | 8478911 | 2017-08-11 14:45:43 -0600 | [diff] [blame] | 1966 | * @removed |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1967 | **/ |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1968 | @SystemApi @Deprecated |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1969 | public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED |
| 1970 | = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED"; |
| 1971 | |
| 1972 | /** |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1973 | * Constant for PreIdleTimeout normal mode (default mode, not short nor extend timeout) . |
| 1974 | * @hide |
| 1975 | */ |
| 1976 | public static final int PRE_IDLE_TIMEOUT_MODE_NORMAL = 0; |
| 1977 | |
| 1978 | /** |
| 1979 | * Constant for PreIdleTimeout long mode (extend timeout to keep in inactive mode |
| 1980 | * longer). |
| 1981 | * @hide |
| 1982 | */ |
| 1983 | public static final int PRE_IDLE_TIMEOUT_MODE_LONG = 1; |
| 1984 | |
| 1985 | /** |
| 1986 | * Constant for PreIdleTimeout short mode (short timeout to go to doze mode quickly) |
| 1987 | * @hide |
| 1988 | */ |
| 1989 | public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2; |
| 1990 | |
| 1991 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1992 | * A wake lock is a mechanism to indicate that your application needs |
| 1993 | * to have the device stay on. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 1994 | * <p> |
| 1995 | * 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] | 1996 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1997 | * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}. |
| 1998 | * </p><p> |
| 1999 | * Call {@link #acquire()} to acquire the wake lock and force the device to stay |
| 2000 | * on at the level that was requested when the wake lock was created. |
| 2001 | * </p><p> |
| 2002 | * Call {@link #release()} when you are done and don't need the lock anymore. |
| 2003 | * It is very important to do this as soon as possible to avoid running down the |
| 2004 | * device's battery excessively. |
| 2005 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | */ |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2007 | public final class WakeLock { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2008 | private int mFlags; |
| 2009 | private String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2010 | private final String mPackageName; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2011 | private final IBinder mToken; |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2012 | private int mInternalCount; |
| 2013 | private int mExternalCount; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2014 | private boolean mRefCounted = true; |
| 2015 | private boolean mHeld; |
| 2016 | private WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2017 | private String mHistoryTag; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2018 | private final String mTraceName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2020 | private final Runnable mReleaser = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | public void run() { |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2022 | release(RELEASE_FLAG_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | } |
| 2024 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2026 | WakeLock(int flags, String tag, String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2027 | mFlags = flags; |
| 2028 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2029 | mPackageName = packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2030 | mToken = new Binder(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2031 | mTraceName = "WakeLock (" + mTag + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2034 | @Override |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2035 | protected void finalize() throws Throwable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | synchronized (mToken) { |
| 2037 | if (mHeld) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2038 | Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2039 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | try { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 2041 | mService.releaseWakeLock(mToken, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2043 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2049 | /** |
| 2050 | * Sets whether this WakeLock is reference counted. |
| 2051 | * <p> |
| 2052 | * Wake locks are reference counted by default. If a wake lock is |
| 2053 | * reference counted, then each call to {@link #acquire()} must be |
| 2054 | * balanced by an equal number of calls to {@link #release()}. If a wake |
| 2055 | * lock is not reference counted, then one call to {@link #release()} is |
| 2056 | * sufficient to undo the effect of all previous calls to {@link #acquire()}. |
| 2057 | * </p> |
| 2058 | * |
| 2059 | * @param value True to make the wake lock reference counted, false to |
| 2060 | * make the wake lock non-reference counted. |
| 2061 | */ |
| 2062 | public void setReferenceCounted(boolean value) { |
| 2063 | synchronized (mToken) { |
| 2064 | mRefCounted = value; |
| 2065 | } |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 2066 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2068 | /** |
| 2069 | * Acquires the wake lock. |
| 2070 | * <p> |
| 2071 | * Ensures that the device is on at the level requested when |
| 2072 | * the wake lock was created. |
| 2073 | * </p> |
| 2074 | */ |
| 2075 | public void acquire() { |
| 2076 | synchronized (mToken) { |
| 2077 | acquireLocked(); |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * Acquires the wake lock with a timeout. |
| 2083 | * <p> |
| 2084 | * Ensures that the device is on at the level requested when |
| 2085 | * the wake lock was created. The lock will be released after the given timeout |
| 2086 | * expires. |
| 2087 | * </p> |
| 2088 | * |
| 2089 | * @param timeout The timeout after which to release the wake lock, in milliseconds. |
| 2090 | */ |
| 2091 | public void acquire(long timeout) { |
| 2092 | synchronized (mToken) { |
| 2093 | acquireLocked(); |
| 2094 | mHandler.postDelayed(mReleaser, timeout); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | private void acquireLocked() { |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2099 | mInternalCount++; |
| 2100 | mExternalCount++; |
| 2101 | if (!mRefCounted || mInternalCount == 1) { |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2102 | // Do this even if the wake lock is already thought to be held (mHeld == true) |
| 2103 | // because non-reference counted wake locks are not always properly released. |
| 2104 | // For example, the keyguard's wake lock might be forcibly released by the |
| 2105 | // power manager without the keyguard knowing. A subsequent call to acquire |
| 2106 | // should immediately acquire the wake lock once again despite never having |
| 2107 | // been explicitly released by the keyguard. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2108 | mHandler.removeCallbacks(mReleaser); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2109 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2110 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2111 | mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource, |
| 2112 | mHistoryTag); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2113 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2114 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2115 | } |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 2116 | mHeld = true; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * Releases the wake lock. |
| 2122 | * <p> |
| 2123 | * This method releases your claim to the CPU or screen being on. |
| 2124 | * The screen may turn off shortly after you release the wake lock, or it may |
| 2125 | * not if there are other wake locks still held. |
| 2126 | * </p> |
| 2127 | */ |
| 2128 | public void release() { |
| 2129 | release(0); |
| 2130 | } |
| 2131 | |
| 2132 | /** |
| 2133 | * Releases the wake lock with flags to modify the release behavior. |
| 2134 | * <p> |
| 2135 | * This method releases your claim to the CPU or screen being on. |
| 2136 | * The screen may turn off shortly after you release the wake lock, or it may |
| 2137 | * not if there are other wake locks still held. |
| 2138 | * </p> |
| 2139 | * |
| 2140 | * @param flags Combination of flag values to modify the release behavior. |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 2141 | * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported. |
| 2142 | * Passing 0 is equivalent to calling {@link #release()}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2143 | */ |
| 2144 | public void release(int flags) { |
| 2145 | synchronized (mToken) { |
Henrik Baard | 8d475ca | 2017-08-09 14:45:05 +0200 | [diff] [blame] | 2146 | if (mInternalCount > 0) { |
| 2147 | // internal count must only be decreased if it is > 0 or state of |
| 2148 | // the WakeLock object is broken. |
| 2149 | mInternalCount--; |
| 2150 | } |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2151 | if ((flags & RELEASE_FLAG_TIMEOUT) == 0) { |
| 2152 | mExternalCount--; |
| 2153 | } |
| 2154 | if (!mRefCounted || mInternalCount == 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2155 | mHandler.removeCallbacks(mReleaser); |
| 2156 | if (mHeld) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2157 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2158 | try { |
| 2159 | mService.releaseWakeLock(mToken, flags); |
| 2160 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2161 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2162 | } |
| 2163 | mHeld = false; |
| 2164 | } |
| 2165 | } |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2166 | if (mRefCounted && mExternalCount < 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2167 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * Returns true if the wake lock has been acquired but not yet released. |
| 2174 | * |
| 2175 | * @return True if the wake lock is held. |
| 2176 | */ |
| 2177 | public boolean isHeld() { |
| 2178 | synchronized (mToken) { |
| 2179 | return mHeld; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | /** |
| 2184 | * Sets the work source associated with the wake lock. |
| 2185 | * <p> |
| 2186 | * The work source is used to determine on behalf of which application |
| 2187 | * the wake lock is being held. This is useful in the case where a |
| 2188 | * service is performing work on behalf of an application so that the |
| 2189 | * cost of that work can be accounted to the application. |
| 2190 | * </p> |
| 2191 | * |
Olivier Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 2192 | * <p> |
| 2193 | * Make sure to follow the tag naming convention when using WorkSource |
| 2194 | * to make it easier for app developers to understand wake locks |
| 2195 | * attributed to them. See {@link PowerManager#newWakeLock(int, String)} |
| 2196 | * documentation. |
| 2197 | * </p> |
| 2198 | * |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2199 | * @param ws The work source, or null if none. |
| 2200 | */ |
| 2201 | public void setWorkSource(WorkSource ws) { |
| 2202 | synchronized (mToken) { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 2203 | if (ws != null && ws.isEmpty()) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2204 | ws = null; |
| 2205 | } |
| 2206 | |
| 2207 | final boolean changed; |
| 2208 | if (ws == null) { |
| 2209 | changed = mWorkSource != null; |
| 2210 | mWorkSource = null; |
| 2211 | } else if (mWorkSource == null) { |
| 2212 | changed = true; |
| 2213 | mWorkSource = new WorkSource(ws); |
| 2214 | } else { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 2215 | changed = !mWorkSource.equals(ws); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2216 | if (changed) { |
| 2217 | mWorkSource.set(ws); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (changed && mHeld) { |
| 2222 | try { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2223 | mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2224 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2225 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2231 | /** @hide */ |
| 2232 | public void setTag(String tag) { |
| 2233 | mTag = tag; |
| 2234 | } |
| 2235 | |
| 2236 | /** @hide */ |
Christopher Tate | 4b17e98 | 2016-09-26 12:59:10 -0700 | [diff] [blame] | 2237 | public String getTag() { |
| 2238 | return mTag; |
| 2239 | } |
| 2240 | |
| 2241 | /** @hide */ |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2242 | public void setHistoryTag(String tag) { |
| 2243 | mHistoryTag = tag; |
| 2244 | } |
| 2245 | |
| 2246 | /** @hide */ |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2247 | public void setUnimportantForLogging(boolean state) { |
| 2248 | if (state) mFlags |= UNIMPORTANT_FOR_LOGGING; |
| 2249 | else mFlags &= ~UNIMPORTANT_FOR_LOGGING; |
| 2250 | } |
| 2251 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2252 | @Override |
| 2253 | public String toString() { |
| 2254 | synchronized (mToken) { |
| 2255 | return "WakeLock{" |
| 2256 | + Integer.toHexString(System.identityHashCode(this)) |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 2257 | + " held=" + mHeld + ", refCount=" + mInternalCount + "}"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 2258 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | } |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 2260 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2261 | /** @hide */ |
| 2262 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 2263 | synchronized (mToken) { |
| 2264 | final long token = proto.start(fieldId); |
Yi Jin | 163967f | 2018-03-15 13:49:44 -0700 | [diff] [blame] | 2265 | proto.write(PowerManagerProto.WakeLock.TAG, mTag); |
| 2266 | proto.write(PowerManagerProto.WakeLock.PACKAGE_NAME, mPackageName); |
| 2267 | proto.write(PowerManagerProto.WakeLock.HELD, mHeld); |
| 2268 | proto.write(PowerManagerProto.WakeLock.INTERNAL_COUNT, mInternalCount); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2269 | if (mWorkSource != null) { |
Yi Jin | 163967f | 2018-03-15 13:49:44 -0700 | [diff] [blame] | 2270 | mWorkSource.writeToProto(proto, PowerManagerProto.WakeLock.WORK_SOURCE); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 2271 | } |
| 2272 | proto.end(token); |
| 2273 | } |
| 2274 | } |
| 2275 | |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 2276 | /** |
| 2277 | * Wraps a Runnable such that this method immediately acquires the wake lock and then |
| 2278 | * once the Runnable is done the wake lock is released. |
| 2279 | * |
| 2280 | * <p>Example: |
| 2281 | * |
| 2282 | * <pre> |
| 2283 | * mHandler.post(mWakeLock.wrap(() -> { |
| 2284 | * // do things on handler, lock is held while we're waiting for this |
| 2285 | * // to get scheduled and until the runnable is done executing. |
| 2286 | * }); |
| 2287 | * </pre> |
| 2288 | * |
| 2289 | * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll |
| 2290 | * leak the wakelock! |
| 2291 | * |
| 2292 | * @hide |
| 2293 | */ |
| 2294 | public Runnable wrap(Runnable r) { |
| 2295 | acquire(); |
| 2296 | return () -> { |
| 2297 | try { |
| 2298 | r.run(); |
| 2299 | } finally { |
| 2300 | release(); |
| 2301 | } |
| 2302 | }; |
| 2303 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | } |