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 | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 23 | import android.annotation.SystemService; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 24 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 26 | import android.util.proto.ProtoOutputStream; |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 27 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 32 | * This class gives you control of the power state of the device. |
| 33 | * |
| 34 | * <p> |
| 35 | * <b>Device battery life will be significantly affected by the use of this API.</b> |
| 36 | * Do not acquire {@link WakeLock}s unless you really need them, use the minimum levels |
| 37 | * possible, and be sure to release them as soon as possible. |
| 38 | * </p><p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 39 | * The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}. |
| 40 | * This will create a {@link PowerManager.WakeLock} object. You can then use methods |
| 41 | * on the wake lock object to control the power state of the device. |
| 42 | * </p><p> |
| 43 | * In practice it's quite simple: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * {@samplecode |
| 45 | * PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 46 | * PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); |
| 47 | * wl.acquire(); |
| 48 | * ..screen will stay on during this section.. |
| 49 | * wl.release(); |
| 50 | * } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 51 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 52 | * The following wake lock levels are defined, with varying effects on system power. |
| 53 | * <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] | 54 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 55 | * <table> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 56 | * <tr><th>Flag Value</th> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * <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] | 58 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 59 | * <tr><td>{@link #PARTIAL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 60 | * <td>On*</td> <td>Off</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 62 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 63 | * <tr><td>{@link #SCREEN_DIM_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 64 | * <td>On</td> <td>Dim</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * </tr> |
| 66 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 67 | * <tr><td>{@link #SCREEN_BRIGHT_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 68 | * <td>On</td> <td>Bright</td> <td>Off</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 70 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 71 | * <tr><td>{@link #FULL_WAKE_LOCK}</td> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 72 | * <td>On</td> <td>Bright</td> <td>Bright</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | * </table> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 75 | * </p><p> |
| 76 | * *<i>If you hold a partial wake lock, the CPU will continue to run, regardless of any |
| 77 | * display timeouts or the state of the screen and even after the user presses the power button. |
| 78 | * In all other wake locks, the CPU will run, but the user can still put the device to sleep |
| 79 | * using the power button.</i> |
| 80 | * </p><p> |
| 81 | * 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] | 82 | * <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] | 83 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 84 | * <table> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | * <tr><th>Flag Value</th> <th>Description</th></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 87 | * <tr><td>{@link #ACQUIRE_CAUSES_WAKEUP}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | * <td>Normal wake locks don't actually turn on the illumination. Instead, they cause |
| 89 | * the illumination to remain on once it turns on (e.g. from user activity). This flag |
| 90 | * will force the screen and/or keyboard to turn on immediately, when the WakeLock is |
| 91 | * 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] | 92 | * see immediately.</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * </tr> |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 94 | * |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 95 | * <tr><td>{@link #ON_AFTER_RELEASE}</td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | * <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] | 97 | * released, causing the illumination to remain on a bit longer. This can be used to |
| 98 | * 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] | 99 | * </tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * </table> |
Dirk Dougherty | 7b9a288 | 2012-10-28 12:07:08 -0700 | [diff] [blame] | 101 | * <p> |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 102 | * 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] | 103 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 104 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 106 | @SystemService(Context.POWER_SERVICE) |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 107 | public final class PowerManager { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | private static final String TAG = "PowerManager"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 109 | |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 110 | /* NOTE: Wake lock levels were previously defined as a bit field, except that only a few |
| 111 | * combinations were actually supported so the bit field was removed. This explains |
| 112 | * why the numbering scheme is so odd. If adding a new wake lock level, any unused |
| 113 | * value can be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 117 | * Wake lock level: Ensures that the CPU is running; the screen and keyboard |
| 118 | * backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 119 | * <p> |
| 120 | * If the user presses the power button, then the screen will be turned off |
| 121 | * 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] | 122 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 124 | public static final int PARTIAL_WAKE_LOCK = 0x00000001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 127 | * 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] | 128 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 129 | * <p> |
| 130 | * If the user presses the power button, then the {@link #SCREEN_DIM_WAKE_LOCK} will be |
| 131 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 132 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 133 | * </p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 134 | * |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 135 | * @deprecated Most applications should use |
| 136 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 137 | * of this type of wake lock, as it will be correctly managed by the platform |
| 138 | * 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] | 139 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 140 | @Deprecated |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 141 | public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 144 | * 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] | 145 | * the keyboard backlight will be allowed to go off. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 146 | * <p> |
| 147 | * If the user presses the power button, then the {@link #SCREEN_BRIGHT_WAKE_LOCK} will be |
| 148 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 149 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 150 | * </p> |
| 151 | * |
| 152 | * @deprecated Most applications should use |
| 153 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 154 | * of this type of wake lock, as it will be correctly managed by the platform |
| 155 | * 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] | 156 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 157 | @Deprecated |
| 158 | public static final int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a; |
| 159 | |
| 160 | /** |
| 161 | * Wake lock level: Ensures that the screen and keyboard backlight are on at |
| 162 | * full brightness. |
| 163 | * <p> |
| 164 | * If the user presses the power button, then the {@link #FULL_WAKE_LOCK} will be |
| 165 | * implicitly released by the system, causing both the screen and the CPU to be turned off. |
| 166 | * Contrast with {@link #PARTIAL_WAKE_LOCK}. |
| 167 | * </p> |
| 168 | * |
| 169 | * @deprecated Most applications should use |
| 170 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead |
| 171 | * of this type of wake lock, as it will be correctly managed by the platform |
| 172 | * as the user moves between applications and doesn't require a special permission. |
| 173 | */ |
| 174 | @Deprecated |
| 175 | public static final int FULL_WAKE_LOCK = 0x0000001a; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 178 | * Wake lock level: Turns the screen off when the proximity sensor activates. |
| 179 | * <p> |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 180 | * If the proximity sensor detects that an object is nearby, the screen turns off |
| 181 | * immediately. Shortly after the object moves away, the screen turns on again. |
| 182 | * </p><p> |
| 183 | * A proximity wake lock does not prevent the device from falling asleep |
| 184 | * unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and |
| 185 | * {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other |
| 186 | * wake locks are held, then the device will fall asleep (and lock) as usual. |
| 187 | * However, the device will not fall asleep while the screen has been turned off |
| 188 | * by the proximity sensor because it effectively counts as ongoing user activity. |
| 189 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 190 | * Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported} |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 191 | * to determine whether this wake lock level is supported. |
Craig Mautner | 6edb6db | 2012-11-20 18:21:12 -0800 | [diff] [blame] | 192 | * </p><p> |
| 193 | * Cannot be used with {@link #ACQUIRE_CAUSES_WAKEUP}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 194 | * </p> |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 195 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 196 | public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 0x00000020; |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 199 | * Wake lock level: Put the screen in a low power state and allow the CPU to suspend |
| 200 | * if no other wake locks are held. |
| 201 | * <p> |
| 202 | * This is used by the dream manager to implement doze mode. It currently |
| 203 | * has no effect unless the power manager is in the dozing state. |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 204 | * </p><p> |
| 205 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 206 | * </p> |
| 207 | * |
| 208 | * {@hide} |
| 209 | */ |
| 210 | public static final int DOZE_WAKE_LOCK = 0x00000040; |
| 211 | |
| 212 | /** |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 213 | * Wake lock level: Keep the device awake enough to allow drawing to occur. |
| 214 | * <p> |
| 215 | * This is used by the window manager to allow applications to draw while the |
| 216 | * system is dozing. It currently has no effect unless the power manager is in |
| 217 | * the dozing state. |
| 218 | * </p><p> |
| 219 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 220 | * </p> |
| 221 | * |
| 222 | * {@hide} |
| 223 | */ |
| 224 | public static final int DRAW_WAKE_LOCK = 0x00000080; |
| 225 | |
| 226 | /** |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 227 | * 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] | 228 | * |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 229 | * @hide |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 230 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 231 | public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff; |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 232 | |
| 233 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 234 | * Wake lock flag: Turn the screen on when the wake lock is acquired. |
| 235 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | * Normally wake locks don't actually wake the device, they just cause |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 237 | * the screen to remain on once it's already on. Think of the video player |
| 238 | * 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] | 239 | * 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] | 240 | * </p><p> |
| 241 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 242 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 244 | public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
| 246 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 247 | * 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] | 248 | * so the screen stays on for a little longer. |
| 249 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 250 | * Will not turn the screen on if it is not already on. |
| 251 | * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that. |
| 252 | * </p><p> |
| 253 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 254 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 256 | public static final int ON_AFTER_RELEASE = 0x20000000; |
| 257 | |
| 258 | /** |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 259 | * Wake lock flag: This wake lock is not important for logging events. If a later |
| 260 | * wake lock is acquired that is important, it will be considered the one to log. |
| 261 | * @hide |
| 262 | */ |
| 263 | public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000; |
| 264 | |
| 265 | /** |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 266 | * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a |
| 267 | * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor |
| 268 | * indicates that an object is not in close proximity. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 269 | */ |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 270 | public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1 << 0; |
| 271 | |
| 272 | /** |
| 273 | * Flag for {@link WakeLock#release(int)} when called due to timeout. |
| 274 | * @hide |
| 275 | */ |
| 276 | public static final int RELEASE_FLAG_TIMEOUT = 1 << 16; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 279 | * Brightness value for fully on. |
| 280 | * @hide |
| 281 | */ |
| 282 | public static final int BRIGHTNESS_ON = 255; |
| 283 | |
| 284 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 285 | * Brightness value for fully off. |
| 286 | * @hide |
| 287 | */ |
| 288 | public static final int BRIGHTNESS_OFF = 0; |
| 289 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Brightness value for default policy handling by the system. |
| 292 | * @hide |
| 293 | */ |
| 294 | public static final int BRIGHTNESS_DEFAULT = -1; |
| 295 | |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 296 | // Note: Be sure to update android.os.BatteryStats and PowerManager.h |
| 297 | // if adding or modifying user activity event constants. |
| 298 | |
| 299 | /** |
| 300 | * User activity event type: Unspecified event type. |
| 301 | * @hide |
| 302 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 303 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 304 | public static final int USER_ACTIVITY_EVENT_OTHER = 0; |
| 305 | |
| 306 | /** |
| 307 | * User activity event type: Button or key pressed or released. |
| 308 | * @hide |
| 309 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 310 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 311 | public static final int USER_ACTIVITY_EVENT_BUTTON = 1; |
| 312 | |
| 313 | /** |
| 314 | * User activity event type: Touch down, move or up. |
| 315 | * @hide |
| 316 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 317 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 318 | public static final int USER_ACTIVITY_EVENT_TOUCH = 2; |
| 319 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 320 | /** |
Phil Weaver | da80d67 | 2016-03-15 16:25:46 -0700 | [diff] [blame] | 321 | * User activity event type: Accessibility taking action on behalf of user. |
| 322 | * @hide |
| 323 | */ |
| 324 | @SystemApi |
| 325 | public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3; |
| 326 | |
| 327 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 328 | * User activity flag: If already dimmed, extend the dim timeout |
| 329 | * but do not brighten. This flag is useful for keeping the screen on |
| 330 | * a little longer without causing a visible change such as when |
| 331 | * the power key is pressed. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 332 | * @hide |
| 333 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 334 | @SystemApi |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 335 | public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0; |
| 336 | |
| 337 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 338 | * User activity flag: Note the user activity as usual but do not |
| 339 | * reset the user activity timeout. This flag is useful for applying |
| 340 | * user activity power hints when interacting with the device indirectly |
| 341 | * on a secondary screen while allowing the primary screen to go to sleep. |
| 342 | * @hide |
| 343 | */ |
| 344 | @SystemApi |
| 345 | public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1; |
| 346 | |
| 347 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 348 | * Go to sleep reason code: Going to sleep due by application request. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 349 | * @hide |
| 350 | */ |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 351 | public static final int GO_TO_SLEEP_REASON_APPLICATION = 0; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * Go to sleep reason code: Going to sleep due by request of the |
| 355 | * device administration policy. |
| 356 | * @hide |
| 357 | */ |
| 358 | public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1; |
| 359 | |
| 360 | /** |
| 361 | * Go to sleep reason code: Going to sleep due to a screen timeout. |
| 362 | * @hide |
| 363 | */ |
| 364 | public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2; |
| 365 | |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 366 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 367 | * Go to sleep reason code: Going to sleep due to the lid switch being closed. |
| 368 | * @hide |
| 369 | */ |
| 370 | public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3; |
| 371 | |
| 372 | /** |
| 373 | * Go to sleep reason code: Going to sleep due to the power button being pressed. |
| 374 | * @hide |
| 375 | */ |
| 376 | public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4; |
| 377 | |
| 378 | /** |
| 379 | * Go to sleep reason code: Going to sleep due to HDMI. |
| 380 | * @hide |
| 381 | */ |
| 382 | public static final int GO_TO_SLEEP_REASON_HDMI = 5; |
| 383 | |
| 384 | /** |
Filip Gruszczynski | 9779e12 | 2015-03-13 17:39:31 -0700 | [diff] [blame] | 385 | * Go to sleep reason code: Going to sleep due to the sleep button being pressed. |
| 386 | * @hide |
| 387 | */ |
| 388 | public static final int GO_TO_SLEEP_REASON_SLEEP_BUTTON = 6; |
| 389 | |
| 390 | /** |
Eugene Susla | f9a651d | 2017-10-11 12:06:27 -0700 | [diff] [blame] | 391 | * Go to sleep reason code: Going to sleep by request of an accessibility service |
| 392 | * @hide |
| 393 | */ |
| 394 | public static final int GO_TO_SLEEP_REASON_ACCESSIBILITY = 7; |
| 395 | |
| 396 | /** |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 397 | * Go to sleep flag: Skip dozing state and directly go to full sleep. |
| 398 | * @hide |
| 399 | */ |
| 400 | public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0; |
| 401 | |
| 402 | /** |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 403 | * The value to pass as the 'reason' argument to reboot() to reboot into |
| 404 | * recovery mode for tasks other than applying system updates, such as |
| 405 | * doing factory resets. |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 406 | * <p> |
| 407 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 408 | * permission (in addition to |
| 409 | * {@link android.Manifest.permission#REBOOT}). |
| 410 | * </p> |
Doug Zongker | 183415e | 2014-08-12 10:18:40 -0700 | [diff] [blame] | 411 | * @hide |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 412 | */ |
| 413 | public static final String REBOOT_RECOVERY = "recovery"; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 416 | * The value to pass as the 'reason' argument to reboot() to reboot into |
| 417 | * recovery mode for applying system updates. |
| 418 | * <p> |
| 419 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 420 | * permission (in addition to |
| 421 | * {@link android.Manifest.permission#REBOOT}). |
| 422 | * </p> |
| 423 | * @hide |
| 424 | */ |
| 425 | public static final String REBOOT_RECOVERY_UPDATE = "recovery-update"; |
| 426 | |
| 427 | /** |
Mahaver Chopra | 1ce53bc | 2015-12-14 13:35:14 +0000 | [diff] [blame] | 428 | * The value to pass as the 'reason' argument to reboot() when device owner requests a reboot on |
| 429 | * the device. |
| 430 | * @hide |
| 431 | */ |
| 432 | public static final String REBOOT_REQUESTED_BY_DEVICE_OWNER = "deviceowner"; |
| 433 | |
| 434 | /** |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 435 | * The 'reason' value used when rebooting in safe mode |
| 436 | * @hide |
| 437 | */ |
| 438 | public static final String REBOOT_SAFE_MODE = "safemode"; |
| 439 | |
| 440 | /** |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 441 | * The 'reason' value used when rebooting the device without turning on the screen. |
| 442 | * @hide |
| 443 | */ |
| 444 | public static final String REBOOT_QUIESCENT = "quiescent"; |
| 445 | |
| 446 | /** |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 447 | * The value to pass as the 'reason' argument to android_reboot(). |
| 448 | * @hide |
| 449 | */ |
| 450 | public static final String SHUTDOWN_USER_REQUESTED = "userrequested"; |
| 451 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 452 | /** |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 453 | * The value to pass as the 'reason' argument to android_reboot() when battery temperature |
| 454 | * is too high. |
| 455 | * @hide |
| 456 | */ |
| 457 | public static final String SHUTDOWN_BATTERY_THERMAL_STATE = "thermal,battery"; |
| 458 | |
| 459 | /** |
| 460 | * The value to pass as the 'reason' argument to android_reboot() when device is running |
| 461 | * critically low on battery. |
| 462 | * @hide |
| 463 | */ |
| 464 | public static final String SHUTDOWN_LOW_BATTERY = "battery"; |
| 465 | |
| 466 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 467 | * @hide |
| 468 | */ |
| 469 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 470 | @IntDef(prefix = { "SHUTDOWN_REASON_" }, value = { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 471 | SHUTDOWN_REASON_UNKNOWN, |
| 472 | SHUTDOWN_REASON_SHUTDOWN, |
| 473 | SHUTDOWN_REASON_REBOOT, |
| 474 | SHUTDOWN_REASON_USER_REQUESTED, |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 475 | SHUTDOWN_REASON_THERMAL_SHUTDOWN, |
| 476 | SHUTDOWN_REASON_LOW_BATTERY, |
| 477 | SHUTDOWN_REASON_BATTERY_THERMAL |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 478 | }) |
| 479 | public @interface ShutdownReason {} |
| 480 | |
| 481 | /** |
| 482 | * constant for shutdown reason being unknown. |
| 483 | * @hide |
| 484 | */ |
| 485 | public static final int SHUTDOWN_REASON_UNKNOWN = 0; |
| 486 | |
| 487 | /** |
| 488 | * constant for shutdown reason being normal shutdown. |
| 489 | * @hide |
| 490 | */ |
| 491 | public static final int SHUTDOWN_REASON_SHUTDOWN = 1; |
| 492 | |
| 493 | /** |
| 494 | * constant for shutdown reason being reboot. |
| 495 | * @hide |
| 496 | */ |
| 497 | public static final int SHUTDOWN_REASON_REBOOT = 2; |
| 498 | |
| 499 | /** |
| 500 | * constant for shutdown reason being user requested. |
| 501 | * @hide |
| 502 | */ |
| 503 | public static final int SHUTDOWN_REASON_USER_REQUESTED = 3; |
| 504 | |
| 505 | /** |
| 506 | * constant for shutdown reason being overheating. |
| 507 | * @hide |
| 508 | */ |
| 509 | public static final int SHUTDOWN_REASON_THERMAL_SHUTDOWN = 4; |
| 510 | |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 511 | /** |
| 512 | * constant for shutdown reason being low battery. |
| 513 | * @hide |
| 514 | */ |
| 515 | public static final int SHUTDOWN_REASON_LOW_BATTERY = 5; |
| 516 | |
| 517 | /** |
| 518 | * constant for shutdown reason being critical battery thermal state. |
| 519 | * @hide |
| 520 | */ |
| 521 | public static final int SHUTDOWN_REASON_BATTERY_THERMAL = 6; |
| 522 | |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 523 | /** |
| 524 | * @hide |
| 525 | */ |
| 526 | @Retention(RetentionPolicy.SOURCE) |
| 527 | @IntDef({ServiceType.GPS, |
| 528 | ServiceType.VIBRATION, |
| 529 | ServiceType.ANIMATION, |
| 530 | ServiceType.FULL_BACKUP, |
| 531 | ServiceType.KEYVALUE_BACKUP, |
| 532 | ServiceType.NETWORK_FIREWALL, |
| 533 | ServiceType.SCREEN_BRIGHTNESS, |
| 534 | ServiceType.SOUND, |
| 535 | ServiceType.BATTERY_STATS, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 536 | ServiceType.DATA_SAVER, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 537 | ServiceType.FORCE_ALL_APPS_STANDBY, |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 538 | ServiceType.OPTIONAL_SENSORS, |
| 539 | }) |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 540 | public @interface ServiceType { |
| 541 | int NULL = 0; |
| 542 | int GPS = 1; |
| 543 | int VIBRATION = 2; |
| 544 | int ANIMATION = 3; |
| 545 | int FULL_BACKUP = 4; |
| 546 | int KEYVALUE_BACKUP = 5; |
| 547 | int NETWORK_FIREWALL = 6; |
| 548 | int SCREEN_BRIGHTNESS = 7; |
| 549 | int SOUND = 8; |
| 550 | int BATTERY_STATS = 9; |
| 551 | int DATA_SAVER = 10; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 552 | |
| 553 | /** |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 554 | * Whether to enable force-app-standby on all apps or not. |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 555 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 556 | int FORCE_ALL_APPS_STANDBY = 11; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 557 | |
| 558 | /** |
Makoto Onuki | e7ec72a | 2017-11-22 11:16:30 -0800 | [diff] [blame] | 559 | * Whether to enable background check on all apps or not. |
| 560 | */ |
| 561 | int FORCE_BACKGROUND_CHECK = 12; |
| 562 | |
| 563 | /** |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 564 | * Whether to disable non-essential sensors. (e.g. edge sensors.) |
| 565 | */ |
| 566 | int OPTIONAL_SENSORS = 13; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 569 | /** |
| 570 | * Either the location providers shouldn't be affected by battery saver, |
| 571 | * or battery saver is off. |
| 572 | */ |
| 573 | public static final int LOCATION_MODE_NO_CHANGE = 0; |
| 574 | |
| 575 | /** |
| 576 | * In this mode, the GPS based location provider should be disabled when battery saver is on and |
| 577 | * the device is non-interactive. |
| 578 | */ |
| 579 | public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF = 1; |
| 580 | |
| 581 | /** |
| 582 | * All location providers should be disabled when battery saver is on and |
| 583 | * the device is non-interactive. |
| 584 | */ |
| 585 | public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF = 2; |
| 586 | |
| 587 | /** |
| 588 | * In this mode, all the location providers will be kept available, but location fixes |
| 589 | * should only be provided to foreground apps. |
| 590 | */ |
| 591 | public static final int LOCATION_MODE_FOREGROUND_ONLY = 3; |
| 592 | |
| 593 | /** |
| 594 | * @hide |
| 595 | */ |
| 596 | @Retention(RetentionPolicy.SOURCE) |
| 597 | @IntDef(prefix = {"LOCATION_MODE_"}, value = { |
| 598 | LOCATION_MODE_NO_CHANGE, |
| 599 | LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF, |
| 600 | LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF, |
| 601 | LOCATION_MODE_FOREGROUND_ONLY, |
| 602 | }) |
| 603 | public @interface LocationPowerSaveMode {} |
| 604 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 605 | final Context mContext; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 606 | final IPowerManager mService; |
| 607 | final Handler mHandler; |
| 608 | |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 609 | IDeviceIdleController mIDeviceIdleController; |
| 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 612 | * {@hide} |
| 613 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 614 | public PowerManager(Context context, IPowerManager service, Handler handler) { |
| 615 | mContext = context; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 616 | mService = service; |
| 617 | mHandler = handler; |
| 618 | } |
| 619 | |
| 620 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 621 | * Gets the minimum supported screen brightness setting. |
| 622 | * The screen may be allowed to become dimmer than this value but |
| 623 | * this is the minimum value that can be set by the user. |
| 624 | * @hide |
| 625 | */ |
| 626 | public int getMinimumScreenBrightnessSetting() { |
| 627 | return mContext.getResources().getInteger( |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 628 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Gets the maximum supported screen brightness setting. |
| 633 | * The screen may be allowed to become dimmer than this value but |
| 634 | * this is the maximum value that can be set by the user. |
| 635 | * @hide |
| 636 | */ |
| 637 | public int getMaximumScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 638 | return mContext.getResources().getInteger( |
| 639 | com.android.internal.R.integer.config_screenBrightnessSettingMaximum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Gets the default screen brightness setting. |
| 644 | * @hide |
| 645 | */ |
| 646 | public int getDefaultScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 647 | return mContext.getResources().getInteger( |
| 648 | com.android.internal.R.integer.config_screenBrightnessSettingDefault); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 652 | * Gets the minimum supported screen brightness setting for VR Mode. |
| 653 | * @hide |
| 654 | */ |
| 655 | public int getMinimumScreenBrightnessForVrSetting() { |
| 656 | return mContext.getResources().getInteger( |
| 657 | com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Gets the maximum supported screen brightness setting for VR Mode. |
| 662 | * The screen may be allowed to become dimmer than this value but |
| 663 | * this is the maximum value that can be set by the user. |
| 664 | * @hide |
| 665 | */ |
| 666 | public int getMaximumScreenBrightnessForVrSetting() { |
| 667 | return mContext.getResources().getInteger( |
| 668 | com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Gets the default screen brightness for VR setting. |
| 673 | * @hide |
| 674 | */ |
| 675 | public int getDefaultScreenBrightnessForVrSetting() { |
| 676 | return mContext.getResources().getInteger( |
| 677 | com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault); |
| 678 | } |
| 679 | |
| 680 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 681 | * Creates a new wake lock with the specified level and flags. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 682 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 683 | * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags |
| 684 | * combined using the logical OR operator. |
| 685 | * </p><p> |
| 686 | * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK}, |
| 687 | * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK} |
| 688 | * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be |
| 689 | * specified as part of the {@code levelAndFlags} parameter. |
| 690 | * </p><p> |
| 691 | * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP} |
| 692 | * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the |
| 693 | * {@code levelAndFlags} parameters. |
| 694 | * </p><p> |
| 695 | * Call {@link WakeLock#acquire() acquire()} on the object to acquire the |
| 696 | * wake lock, and {@link WakeLock#release release()} when you are done. |
| 697 | * </p><p> |
| 698 | * {@samplecode |
| 699 | * PowerManager pm = (PowerManager)mContext.getSystemService( |
| 700 | * Context.POWER_SERVICE); |
| 701 | * PowerManager.WakeLock wl = pm.newWakeLock( |
| 702 | * PowerManager.SCREEN_DIM_WAKE_LOCK |
| 703 | * | PowerManager.ON_AFTER_RELEASE, |
| 704 | * TAG); |
| 705 | * wl.acquire(); |
| 706 | * // ... do work... |
| 707 | * wl.release(); |
| 708 | * } |
| 709 | * </p><p> |
| 710 | * Although a wake lock can be created without special permissions, |
| 711 | * the {@link android.Manifest.permission#WAKE_LOCK} permission is |
| 712 | * required to actually acquire or release the wake lock that is returned. |
| 713 | * </p><p class="note"> |
| 714 | * If using this to keep the screen on, you should strongly consider using |
| 715 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead. |
| 716 | * This window flag will be correctly managed by the platform |
| 717 | * as the user moves between applications and doesn't require a special permission. |
| 718 | * </p> |
| 719 | * |
Olivier Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 720 | * <p> |
| 721 | * Recommended naming conventions for tags to make debugging easier: |
| 722 | * <ul> |
| 723 | * <li>use a unique prefix delimited by a colon for your app/library (e.g. |
| 724 | * gmail:mytag) to make it easier to understand where the wake locks comes |
| 725 | * from. This namespace will also avoid collision for tags inside your app |
| 726 | * coming from different libraries which will make debugging easier. |
| 727 | * <li>use constants (e.g. do not include timestamps in the tag) to make it |
| 728 | * easier for tools to aggregate similar wake locks. When collecting |
| 729 | * debugging data, the platform only monitors a finite number of tags, |
| 730 | * using constants will help tools to provide better debugging data. |
| 731 | * <li>avoid using Class#getName() or similar method since this class name |
| 732 | * can be transformed by java optimizer and obfuscator tools. |
| 733 | * <li>avoid wrapping the tag or a prefix to avoid collision with wake lock |
| 734 | * tags from the platform (e.g. *alarm*). |
| 735 | * <li>never include personnally identifiable information for privacy |
| 736 | * reasons. |
| 737 | * </ul> |
| 738 | * </p> |
| 739 | * |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 740 | * @param levelAndFlags Combination of wake lock level and flag values defining |
| 741 | * the requested behavior of the WakeLock. |
| 742 | * @param tag Your class name (or other tag) for debugging purposes. |
| 743 | * |
| 744 | * @see WakeLock#acquire() |
| 745 | * @see WakeLock#release() |
| 746 | * @see #PARTIAL_WAKE_LOCK |
| 747 | * @see #FULL_WAKE_LOCK |
| 748 | * @see #SCREEN_DIM_WAKE_LOCK |
| 749 | * @see #SCREEN_BRIGHT_WAKE_LOCK |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 750 | * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 751 | * @see #ACQUIRE_CAUSES_WAKEUP |
| 752 | * @see #ON_AFTER_RELEASE |
| 753 | */ |
| 754 | public WakeLock newWakeLock(int levelAndFlags, String tag) { |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 755 | validateWakeLockParameters(levelAndFlags, tag); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 756 | return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName()); |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** @hide */ |
| 760 | public static void validateWakeLockParameters(int levelAndFlags, String tag) { |
| 761 | switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) { |
| 762 | case PARTIAL_WAKE_LOCK: |
| 763 | case SCREEN_DIM_WAKE_LOCK: |
| 764 | case SCREEN_BRIGHT_WAKE_LOCK: |
| 765 | case FULL_WAKE_LOCK: |
| 766 | case PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 767 | case DOZE_WAKE_LOCK: |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 768 | case DRAW_WAKE_LOCK: |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 769 | break; |
| 770 | default: |
| 771 | throw new IllegalArgumentException("Must specify a valid wake lock level."); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 772 | } |
| 773 | if (tag == null) { |
| 774 | throw new IllegalArgumentException("The tag must not be null."); |
| 775 | } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Notifies the power manager that user activity happened. |
| 780 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 781 | * Resets the auto-off timer and brightens the screen if the device |
| 782 | * is not asleep. This is what happens normally when a key or the touch |
| 783 | * screen is pressed or when some other user activity occurs. |
| 784 | * 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] | 785 | * </p><p> |
| 786 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 787 | * </p> |
| 788 | * |
| 789 | * @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] | 790 | * 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] | 791 | * other power management functions. It should be set |
| 792 | * to the timestamp of the input event that caused the user activity. |
| 793 | * @param noChangeLights If true, does not cause the keyboard backlight to turn on |
| 794 | * because of this event. This is set when the power key is pressed. |
| 795 | * We want the device to stay on while the button is down, but we're about |
| 796 | * to turn off the screen so we don't want the keyboard backlight to turn on again. |
| 797 | * Otherwise the lights flash on and then off and it looks weird. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 798 | * |
| 799 | * @see #wakeUp |
| 800 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 801 | * |
| 802 | * @removed Requires signature or system permission. |
| 803 | * @deprecated Use {@link #userActivity(long, int, int)}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 804 | */ |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 805 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 806 | public void userActivity(long when, boolean noChangeLights) { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 807 | userActivity(when, USER_ACTIVITY_EVENT_OTHER, |
| 808 | noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Notifies the power manager that user activity happened. |
| 813 | * <p> |
| 814 | * Resets the auto-off timer and brightens the screen if the device |
| 815 | * is not asleep. This is what happens normally when a key or the touch |
| 816 | * screen is pressed or when some other user activity occurs. |
| 817 | * This method does not wake up the device if it has been put to sleep. |
| 818 | * </p><p> |
| 819 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} or |
| 820 | * {@link android.Manifest.permission#USER_ACTIVITY} permission. |
| 821 | * </p> |
| 822 | * |
| 823 | * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()} |
| 824 | * time base. This timestamp is used to correctly order the user activity request with |
| 825 | * other power management functions. It should be set |
| 826 | * to the timestamp of the input event that caused the user activity. |
| 827 | * @param event The user activity event. |
| 828 | * @param flags Optional user activity flags. |
| 829 | * |
| 830 | * @see #wakeUp |
| 831 | * @see #goToSleep |
| 832 | * |
| 833 | * @hide Requires signature or system permission. |
| 834 | */ |
| 835 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 836 | @RequiresPermission(anyOf = { |
| 837 | android.Manifest.permission.DEVICE_POWER, |
| 838 | android.Manifest.permission.USER_ACTIVITY |
| 839 | }) |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 840 | public void userActivity(long when, int event, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 841 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 842 | mService.userActivity(when, event, flags); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 843 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 844 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Forces the device to go to sleep. |
| 850 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 851 | * Overrides all the wake locks that are held. |
| 852 | * 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] | 853 | * </p><p> |
| 854 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 855 | * </p> |
| 856 | * |
| 857 | * @param time The time when the request to go to sleep was issued, in the |
| 858 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 859 | * 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] | 860 | * 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] | 861 | * |
| 862 | * @see #userActivity |
| 863 | * @see #wakeUp |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 864 | * |
| 865 | * @removed Requires signature permission. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 866 | */ |
| 867 | public void goToSleep(long time) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 868 | goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /** |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 872 | * Forces the device to go to sleep. |
| 873 | * <p> |
| 874 | * Overrides all the wake locks that are held. |
| 875 | * This is what happens when the power key is pressed to turn off the screen. |
| 876 | * </p><p> |
| 877 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 878 | * </p> |
| 879 | * |
| 880 | * @param time The time when the request to go to sleep was issued, in the |
| 881 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 882 | * order the go to sleep request with other power management functions. It should be set |
| 883 | * to the timestamp of the input event that caused the request to go to sleep. |
| 884 | * @param reason The reason the device is going to sleep. |
| 885 | * @param flags Optional flags to apply when going to sleep. |
| 886 | * |
| 887 | * @see #userActivity |
| 888 | * @see #wakeUp |
| 889 | * |
| 890 | * @hide Requires signature permission. |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 891 | */ |
| 892 | public void goToSleep(long time, int reason, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 893 | try { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 894 | mService.goToSleep(time, reason, flags); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 895 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 896 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Forces the device to wake up from sleep. |
| 902 | * <p> |
| 903 | * If the device is currently asleep, wakes it up, otherwise does nothing. |
| 904 | * This is what happens when the power key is pressed to turn on the screen. |
| 905 | * </p><p> |
| 906 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 907 | * </p> |
| 908 | * |
| 909 | * @param time The time when the request to wake up was issued, in the |
| 910 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 911 | * 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] | 912 | * to the timestamp of the input event that caused the request to wake up. |
| 913 | * |
| 914 | * @see #userActivity |
| 915 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 916 | * |
| 917 | * @removed Requires signature permission. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 918 | */ |
| 919 | public void wakeUp(long time) { |
| 920 | try { |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 921 | mService.wakeUp(time, "wakeUp", mContext.getOpPackageName()); |
| 922 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 923 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * @hide |
| 929 | */ |
| 930 | public void wakeUp(long time, String reason) { |
| 931 | try { |
| 932 | mService.wakeUp(time, reason, mContext.getOpPackageName()); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 933 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 934 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
| 938 | /** |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 939 | * Forces the device to start napping. |
| 940 | * <p> |
| 941 | * If the device is currently awake, starts dreaming, otherwise does nothing. |
| 942 | * When the dream ends or if the dream cannot be started, the device will |
| 943 | * either wake up or go to sleep depending on whether there has been recent |
| 944 | * user activity. |
| 945 | * </p><p> |
| 946 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 947 | * </p> |
| 948 | * |
| 949 | * @param time The time when the request to nap was issued, in the |
| 950 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 951 | * order the nap request with other power management functions. It should be set |
| 952 | * to the timestamp of the input event that caused the request to nap. |
| 953 | * |
| 954 | * @see #wakeUp |
| 955 | * @see #goToSleep |
| 956 | * |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 957 | * @hide Requires signature permission. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 958 | */ |
| 959 | public void nap(long time) { |
| 960 | try { |
| 961 | mService.nap(time); |
| 962 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 963 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | /** |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 968 | * Boosts the brightness of the screen to maximum for a predetermined |
| 969 | * period of time. This is used to make the screen more readable in bright |
| 970 | * daylight for a short duration. |
| 971 | * <p> |
| 972 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 973 | * </p> |
| 974 | * |
| 975 | * @param time The time when the request to boost was issued, in the |
| 976 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 977 | * order the boost request with other power management functions. It should be set |
| 978 | * to the timestamp of the input event that caused the request to boost. |
| 979 | * |
| 980 | * @hide Requires signature permission. |
| 981 | */ |
| 982 | public void boostScreenBrightness(long time) { |
| 983 | try { |
| 984 | mService.boostScreenBrightness(time); |
| 985 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 986 | throw e.rethrowFromSystemServer(); |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | /** |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 991 | * Returns whether the screen brightness is currently boosted to maximum, caused by a call |
| 992 | * to {@link #boostScreenBrightness(long)}. |
| 993 | * @return {@code True} if the screen brightness is currently boosted. {@code False} otherwise. |
| 994 | * |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 995 | * @deprecated This call is rarely used and will be phased out soon. |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 996 | * @hide |
Jeff Sharkey | 8478911 | 2017-08-11 14:45:43 -0600 | [diff] [blame] | 997 | * @removed |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 998 | */ |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 999 | @SystemApi @Deprecated |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1000 | public boolean isScreenBrightnessBoosted() { |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1001 | return false; |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1004 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1005 | * Returns true if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1006 | * |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1007 | * @param level The wake lock level to check. |
| 1008 | * @return True if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1009 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1010 | public boolean isWakeLockLevelSupported(int level) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1011 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1012 | return mService.isWakeLockLevelSupported(level); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1013 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1014 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1019 | * Returns true if the device is in an interactive state. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1020 | * <p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1021 | * For historical reasons, the name of this method refers to the power state of |
| 1022 | * the screen but it actually describes the overall interactive state of |
| 1023 | * the device. This method has been replaced by {@link #isInteractive}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1024 | * </p><p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1025 | * The value returned by this method only indicates whether the device is |
| 1026 | * in an interactive state which may have nothing to do with the screen being |
| 1027 | * on or off. To determine the actual state of the screen, |
| 1028 | * use {@link android.view.Display#getState}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1029 | * </p> |
| 1030 | * |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1031 | * @return True if the device is in an interactive state. |
| 1032 | * |
| 1033 | * @deprecated Use {@link #isInteractive} instead. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1034 | */ |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1035 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1036 | public boolean isScreenOn() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1037 | return isInteractive(); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Returns true if the device is in an interactive state. |
| 1042 | * <p> |
| 1043 | * When this method returns true, the device is awake and ready to interact |
| 1044 | * with the user (although this is not a guarantee that the user is actively |
| 1045 | * interacting with the device just this moment). The main screen is usually |
| 1046 | * turned on while in this state. Certain features, such as the proximity |
| 1047 | * sensor, may temporarily turn off the screen while still leaving the device in an |
| 1048 | * interactive state. Note in particular that the device is still considered |
| 1049 | * to be interactive while dreaming (since dreams can be interactive) but not |
| 1050 | * when it is dozing or asleep. |
| 1051 | * </p><p> |
| 1052 | * When this method returns false, the device is dozing or asleep and must |
| 1053 | * be awoken before it will become ready to interact with the user again. The |
| 1054 | * main screen is usually turned off while in this state. Certain features, |
| 1055 | * such as "ambient mode" may cause the main screen to remain on (albeit in a |
| 1056 | * low power state) to display system-provided content while the device dozes. |
| 1057 | * </p><p> |
| 1058 | * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on} |
| 1059 | * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast |
| 1060 | * whenever the interactive state of the device changes. For historical reasons, |
| 1061 | * the names of these broadcasts refer to the power state of the screen |
| 1062 | * but they are actually sent in response to changes in the overall interactive |
| 1063 | * state of the device, as described by this method. |
| 1064 | * </p><p> |
| 1065 | * Services may use the non-interactive state as a hint to conserve power |
| 1066 | * since the user is not present. |
| 1067 | * </p> |
| 1068 | * |
| 1069 | * @return True if the device is in an interactive state. |
| 1070 | * |
| 1071 | * @see android.content.Intent#ACTION_SCREEN_ON |
| 1072 | * @see android.content.Intent#ACTION_SCREEN_OFF |
| 1073 | */ |
| 1074 | public boolean isInteractive() { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1075 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1076 | return mService.isInteractive(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1077 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1078 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Reboot the device. Will not return if the reboot is successful. |
| 1084 | * <p> |
| 1085 | * Requires the {@link android.Manifest.permission#REBOOT} permission. |
| 1086 | * </p> |
| 1087 | * |
| 1088 | * @param reason code to pass to the kernel (e.g., "recovery") to |
| 1089 | * request special boot modes, or null. |
| 1090 | */ |
| 1091 | public void reboot(String reason) { |
| 1092 | try { |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 1093 | mService.reboot(false, reason, true); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1094 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1095 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 1100 | * Reboot the device. Will not return if the reboot is successful. |
| 1101 | * <p> |
| 1102 | * Requires the {@link android.Manifest.permission#REBOOT} permission. |
| 1103 | * </p> |
| 1104 | * @hide |
| 1105 | */ |
| 1106 | public void rebootSafeMode() { |
| 1107 | try { |
| 1108 | mService.rebootSafeMode(false, true); |
| 1109 | } catch (RemoteException e) { |
| 1110 | throw e.rethrowFromSystemServer(); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1115 | * Returns true if the device is currently in power save mode. When in this mode, |
| 1116 | * applications should reduce their functionality in order to conserve battery as |
| 1117 | * much as possible. You can monitor for changes to this state with |
| 1118 | * {@link #ACTION_POWER_SAVE_MODE_CHANGED}. |
| 1119 | * |
| 1120 | * @return Returns true if currently in low power mode, else false. |
| 1121 | */ |
| 1122 | public boolean isPowerSaveMode() { |
| 1123 | try { |
| 1124 | return mService.isPowerSaveMode(); |
| 1125 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1126 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /** |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1131 | * Set the current power save mode. |
| 1132 | * |
| 1133 | * @return True if the set was allowed. |
| 1134 | * |
| 1135 | * @see #isPowerSaveMode() |
| 1136 | * |
| 1137 | * @hide |
| 1138 | */ |
| 1139 | public boolean setPowerSaveMode(boolean mode) { |
| 1140 | try { |
| 1141 | return mService.setPowerSaveMode(mode); |
| 1142 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1143 | throw e.rethrowFromSystemServer(); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /** |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1148 | * Get data about the battery saver mode for a specific service |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1149 | * @param serviceType unique key for the service, one of {@link ServiceType} |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1150 | * @return Battery saver state data. |
| 1151 | * |
| 1152 | * @hide |
| 1153 | * @see com.android.server.power.BatterySaverPolicy |
| 1154 | * @see PowerSaveState |
| 1155 | */ |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 1156 | public PowerSaveState getPowerSaveState(@ServiceType int serviceType) { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 1157 | try { |
| 1158 | return mService.getPowerSaveState(serviceType); |
| 1159 | } catch (RemoteException e) { |
| 1160 | throw e.rethrowFromSystemServer(); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /** |
Makoto Onuki | 57f0f55 | 2017-12-11 12:22:18 -0800 | [diff] [blame] | 1165 | * Returns how location features should behave when battery saver is on. When battery saver |
| 1166 | * is off, this will always return {@link #LOCATION_MODE_NO_CHANGE}. |
| 1167 | * |
| 1168 | * <p>This API is normally only useful for components that provide location features. |
| 1169 | * |
| 1170 | * @see #isPowerSaveMode() |
| 1171 | * @see #ACTION_POWER_SAVE_MODE_CHANGED |
| 1172 | */ |
| 1173 | @LocationPowerSaveMode |
| 1174 | public int getLocationPowerSaveMode() { |
| 1175 | final PowerSaveState powerSaveState = getPowerSaveState(ServiceType.GPS); |
| 1176 | if (!powerSaveState.globalBatterySaverEnabled) { |
| 1177 | return LOCATION_MODE_NO_CHANGE; |
| 1178 | } |
| 1179 | return powerSaveState.gpsMode; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1183 | * Returns true if the device is currently in idle mode. This happens when a device |
| 1184 | * has been sitting unused and unmoving for a sufficiently long period of time, so that |
| 1185 | * it decides to go into a lower power-use state. This may involve things like turning |
| 1186 | * off network access to apps. You can monitor for changes to this state with |
| 1187 | * {@link #ACTION_DEVICE_IDLE_MODE_CHANGED}. |
| 1188 | * |
Dianne Hackborn | ece0f4f | 2015-06-11 13:29:01 -0700 | [diff] [blame] | 1189 | * @return Returns true if currently in active device idle mode, else false. This is |
| 1190 | * when idle mode restrictions are being actively applied; it will return false if the |
| 1191 | * device is in a long-term idle mode but currently running a maintenance window where |
| 1192 | * restrictions have been lifted. |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1193 | */ |
| 1194 | public boolean isDeviceIdleMode() { |
| 1195 | try { |
| 1196 | return mService.isDeviceIdleMode(); |
| 1197 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1198 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /** |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1203 | * Returns true if the device is currently in light idle mode. This happens when a device |
| 1204 | * has had its screen off for a short time, switching it into a batching mode where we |
| 1205 | * execute jobs, syncs, networking on a batching schedule. You can monitor for changes to |
| 1206 | * this state with {@link #ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED}. |
| 1207 | * |
| 1208 | * @return Returns true if currently in active light device idle mode, else false. This is |
| 1209 | * when light idle mode restrictions are being actively applied; it will return false if the |
| 1210 | * device is in a long-term idle mode but currently running a maintenance window where |
| 1211 | * restrictions have been lifted. |
| 1212 | * @hide |
| 1213 | */ |
| 1214 | public boolean isLightDeviceIdleMode() { |
| 1215 | try { |
| 1216 | return mService.isLightDeviceIdleMode(); |
| 1217 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1218 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /** |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1223 | * Return whether the given application package name is on the device's power whitelist. |
| 1224 | * Apps can be placed on the whitelist through the settings UI invoked by |
| 1225 | * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}. |
| 1226 | */ |
| 1227 | public boolean isIgnoringBatteryOptimizations(String packageName) { |
| 1228 | synchronized (this) { |
| 1229 | if (mIDeviceIdleController == null) { |
| 1230 | mIDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1231 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
| 1232 | } |
| 1233 | } |
| 1234 | try { |
| 1235 | return mIDeviceIdleController.isPowerSaveWhitelistApp(packageName); |
| 1236 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1237 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | /** |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1242 | * Turn off the device. |
| 1243 | * |
| 1244 | * @param confirm If true, shows a shutdown confirmation dialog. |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1245 | * @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] | 1246 | * @param wait If true, this call waits for the shutdown to complete and does not return. |
| 1247 | * |
| 1248 | * @hide |
| 1249 | */ |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1250 | public void shutdown(boolean confirm, String reason, boolean wait) { |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1251 | try { |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 1252 | mService.shutdown(confirm, reason, wait); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1253 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1254 | throw e.rethrowFromSystemServer(); |
Filip Gruszczynski | d05af86 | 2015-02-04 09:48:47 -0800 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /** |
Ruchi Kandoi | c45908d | 2016-05-16 10:23:15 -0700 | [diff] [blame] | 1259 | * This function checks if the device has implemented Sustained Performance |
| 1260 | * Mode. This needs to be checked only once and is constant for a particular |
| 1261 | * device/release. |
| 1262 | * |
| 1263 | * Sustained Performance Mode is intended to provide a consistent level of |
| 1264 | * performance for prolonged amount of time. |
| 1265 | * |
| 1266 | * Applications should check if the device supports this mode, before using |
| 1267 | * {@link android.view.Window#setSustainedPerformanceMode}. |
| 1268 | * |
| 1269 | * @return Returns True if the device supports it, false otherwise. |
| 1270 | * |
| 1271 | * @see android.view.Window#setSustainedPerformanceMode |
Ruchi Kandoi | b4aa2e9 | 2016-03-30 12:07:31 -0700 | [diff] [blame] | 1272 | */ |
| 1273 | public boolean isSustainedPerformanceModeSupported() { |
| 1274 | return mContext.getResources().getBoolean( |
| 1275 | com.android.internal.R.bool.config_sustainedPerformanceModeSupported); |
| 1276 | } |
| 1277 | |
| 1278 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 1279 | * Returns the reason the phone was last shutdown. Calling app must have the |
| 1280 | * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information. |
| 1281 | * @return Reason for shutdown as an int, {@link #SHUTDOWN_REASON_UNKNOWN} if the file could |
| 1282 | * not be accessed. |
| 1283 | * @hide |
| 1284 | */ |
| 1285 | @ShutdownReason |
| 1286 | public int getLastShutdownReason() { |
| 1287 | try { |
| 1288 | return mService.getLastShutdownReason(); |
| 1289 | } catch (RemoteException e) { |
| 1290 | throw e.rethrowFromSystemServer(); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1295 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. |
| 1296 | * This broadcast is only sent to registered receivers. |
| 1297 | */ |
| 1298 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1299 | public static final String ACTION_POWER_SAVE_MODE_CHANGED |
| 1300 | = "android.os.action.POWER_SAVE_MODE_CHANGED"; |
| 1301 | |
| 1302 | /** |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 1303 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. |
| 1304 | * @hide |
| 1305 | */ |
| 1306 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1307 | public static final String ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL |
| 1308 | = "android.os.action.POWER_SAVE_MODE_CHANGED_INTERNAL"; |
| 1309 | |
| 1310 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1311 | * Intent that is broadcast when the state of {@link #isDeviceIdleMode()} changes. |
| 1312 | * This broadcast is only sent to registered receivers. |
| 1313 | */ |
| 1314 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1315 | public static final String ACTION_DEVICE_IDLE_MODE_CHANGED |
| 1316 | = "android.os.action.DEVICE_IDLE_MODE_CHANGED"; |
| 1317 | |
| 1318 | /** |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1319 | * Intent that is broadcast when the state of {@link #isLightDeviceIdleMode()} changes. |
| 1320 | * This broadcast is only sent to registered receivers. |
| 1321 | * @hide |
| 1322 | */ |
| 1323 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1324 | public static final String ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED |
| 1325 | = "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED"; |
| 1326 | |
| 1327 | /** |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1328 | * @hide Intent that is broadcast when the set of power save whitelist apps has changed. |
| 1329 | * This broadcast is only sent to registered receivers. |
| 1330 | */ |
| 1331 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1332 | public static final String ACTION_POWER_SAVE_WHITELIST_CHANGED |
| 1333 | = "android.os.action.POWER_SAVE_WHITELIST_CHANGED"; |
| 1334 | |
| 1335 | /** |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1336 | * @hide Intent that is broadcast when the set of temporarily whitelisted apps has changed. |
| 1337 | * This broadcast is only sent to registered receivers. |
| 1338 | */ |
| 1339 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1340 | public static final String ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED |
| 1341 | = "android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED"; |
| 1342 | |
| 1343 | /** |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 1344 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change. |
| 1345 | * This broadcast is only sent to registered receivers. |
| 1346 | * |
| 1347 | * @hide |
| 1348 | */ |
| 1349 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 1350 | public static final String ACTION_POWER_SAVE_MODE_CHANGING |
| 1351 | = "android.os.action.POWER_SAVE_MODE_CHANGING"; |
| 1352 | |
| 1353 | /** @hide */ |
| 1354 | public static final String EXTRA_POWER_SAVE_MODE = "mode"; |
| 1355 | |
| 1356 | /** |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1357 | * Intent that is broadcast when the state of {@link #isScreenBrightnessBoosted()} has changed. |
| 1358 | * This broadcast is only sent to registered receivers. |
| 1359 | * |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1360 | * @deprecated This intent is rarely used and will be phased out soon. |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1361 | * @hide |
Jeff Sharkey | 8478911 | 2017-08-11 14:45:43 -0600 | [diff] [blame] | 1362 | * @removed |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1363 | **/ |
Julius D'souza | b22da80 | 2017-06-09 10:27:14 -0700 | [diff] [blame] | 1364 | @SystemApi @Deprecated |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 1365 | public static final String ACTION_SCREEN_BRIGHTNESS_BOOST_CHANGED |
| 1366 | = "android.os.action.SCREEN_BRIGHTNESS_BOOST_CHANGED"; |
| 1367 | |
| 1368 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1369 | * A wake lock is a mechanism to indicate that your application needs |
| 1370 | * to have the device stay on. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 1371 | * <p> |
| 1372 | * 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] | 1373 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1374 | * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}. |
| 1375 | * </p><p> |
| 1376 | * Call {@link #acquire()} to acquire the wake lock and force the device to stay |
| 1377 | * on at the level that was requested when the wake lock was created. |
| 1378 | * </p><p> |
| 1379 | * Call {@link #release()} when you are done and don't need the lock anymore. |
| 1380 | * It is very important to do this as soon as possible to avoid running down the |
| 1381 | * device's battery excessively. |
| 1382 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | */ |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1384 | public final class WakeLock { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1385 | private int mFlags; |
| 1386 | private String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1387 | private final String mPackageName; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1388 | private final IBinder mToken; |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1389 | private int mInternalCount; |
| 1390 | private int mExternalCount; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1391 | private boolean mRefCounted = true; |
| 1392 | private boolean mHeld; |
| 1393 | private WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1394 | private String mHistoryTag; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1395 | private final String mTraceName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1397 | private final Runnable mReleaser = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | public void run() { |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1399 | release(RELEASE_FLAG_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1403 | WakeLock(int flags, String tag, String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | mFlags = flags; |
| 1405 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1406 | mPackageName = packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | mToken = new Binder(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1408 | mTraceName = "WakeLock (" + mTag + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | @Override |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1412 | protected void finalize() throws Throwable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | synchronized (mToken) { |
| 1414 | if (mHeld) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 1415 | Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1416 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | try { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 1418 | mService.releaseWakeLock(mToken, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1420 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1426 | /** |
| 1427 | * Sets whether this WakeLock is reference counted. |
| 1428 | * <p> |
| 1429 | * Wake locks are reference counted by default. If a wake lock is |
| 1430 | * reference counted, then each call to {@link #acquire()} must be |
| 1431 | * balanced by an equal number of calls to {@link #release()}. If a wake |
| 1432 | * lock is not reference counted, then one call to {@link #release()} is |
| 1433 | * sufficient to undo the effect of all previous calls to {@link #acquire()}. |
| 1434 | * </p> |
| 1435 | * |
| 1436 | * @param value True to make the wake lock reference counted, false to |
| 1437 | * make the wake lock non-reference counted. |
| 1438 | */ |
| 1439 | public void setReferenceCounted(boolean value) { |
| 1440 | synchronized (mToken) { |
| 1441 | mRefCounted = value; |
| 1442 | } |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 1443 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1445 | /** |
| 1446 | * Acquires the wake lock. |
| 1447 | * <p> |
| 1448 | * Ensures that the device is on at the level requested when |
| 1449 | * the wake lock was created. |
| 1450 | * </p> |
| 1451 | */ |
| 1452 | public void acquire() { |
| 1453 | synchronized (mToken) { |
| 1454 | acquireLocked(); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * Acquires the wake lock with a timeout. |
| 1460 | * <p> |
| 1461 | * Ensures that the device is on at the level requested when |
| 1462 | * the wake lock was created. The lock will be released after the given timeout |
| 1463 | * expires. |
| 1464 | * </p> |
| 1465 | * |
| 1466 | * @param timeout The timeout after which to release the wake lock, in milliseconds. |
| 1467 | */ |
| 1468 | public void acquire(long timeout) { |
| 1469 | synchronized (mToken) { |
| 1470 | acquireLocked(); |
| 1471 | mHandler.postDelayed(mReleaser, timeout); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | private void acquireLocked() { |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1476 | mInternalCount++; |
| 1477 | mExternalCount++; |
| 1478 | if (!mRefCounted || mInternalCount == 1) { |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 1479 | // Do this even if the wake lock is already thought to be held (mHeld == true) |
| 1480 | // because non-reference counted wake locks are not always properly released. |
| 1481 | // For example, the keyguard's wake lock might be forcibly released by the |
| 1482 | // power manager without the keyguard knowing. A subsequent call to acquire |
| 1483 | // should immediately acquire the wake lock once again despite never having |
| 1484 | // been explicitly released by the keyguard. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1485 | mHandler.removeCallbacks(mReleaser); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1486 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 1487 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1488 | mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource, |
| 1489 | mHistoryTag); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 1490 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1491 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1492 | } |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 1493 | mHeld = true; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * Releases the wake lock. |
| 1499 | * <p> |
| 1500 | * This method releases your claim to the CPU or screen being on. |
| 1501 | * The screen may turn off shortly after you release the wake lock, or it may |
| 1502 | * not if there are other wake locks still held. |
| 1503 | * </p> |
| 1504 | */ |
| 1505 | public void release() { |
| 1506 | release(0); |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * Releases the wake lock with flags to modify the release behavior. |
| 1511 | * <p> |
| 1512 | * This method releases your claim to the CPU or screen being on. |
| 1513 | * The screen may turn off shortly after you release the wake lock, or it may |
| 1514 | * not if there are other wake locks still held. |
| 1515 | * </p> |
| 1516 | * |
| 1517 | * @param flags Combination of flag values to modify the release behavior. |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 1518 | * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported. |
| 1519 | * Passing 0 is equivalent to calling {@link #release()}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1520 | */ |
| 1521 | public void release(int flags) { |
| 1522 | synchronized (mToken) { |
Henrik Baard | 8d475ca | 2017-08-09 14:45:05 +0200 | [diff] [blame] | 1523 | if (mInternalCount > 0) { |
| 1524 | // internal count must only be decreased if it is > 0 or state of |
| 1525 | // the WakeLock object is broken. |
| 1526 | mInternalCount--; |
| 1527 | } |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1528 | if ((flags & RELEASE_FLAG_TIMEOUT) == 0) { |
| 1529 | mExternalCount--; |
| 1530 | } |
| 1531 | if (!mRefCounted || mInternalCount == 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1532 | mHandler.removeCallbacks(mReleaser); |
| 1533 | if (mHeld) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1534 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1535 | try { |
| 1536 | mService.releaseWakeLock(mToken, flags); |
| 1537 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1538 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1539 | } |
| 1540 | mHeld = false; |
| 1541 | } |
| 1542 | } |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1543 | if (mRefCounted && mExternalCount < 0) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1544 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /** |
| 1550 | * Returns true if the wake lock has been acquired but not yet released. |
| 1551 | * |
| 1552 | * @return True if the wake lock is held. |
| 1553 | */ |
| 1554 | public boolean isHeld() { |
| 1555 | synchronized (mToken) { |
| 1556 | return mHeld; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Sets the work source associated with the wake lock. |
| 1562 | * <p> |
| 1563 | * The work source is used to determine on behalf of which application |
| 1564 | * the wake lock is being held. This is useful in the case where a |
| 1565 | * service is performing work on behalf of an application so that the |
| 1566 | * cost of that work can be accounted to the application. |
| 1567 | * </p> |
| 1568 | * |
Olivier Gaillard | 24a0c13 | 2017-11-22 15:07:31 +0000 | [diff] [blame] | 1569 | * <p> |
| 1570 | * Make sure to follow the tag naming convention when using WorkSource |
| 1571 | * to make it easier for app developers to understand wake locks |
| 1572 | * attributed to them. See {@link PowerManager#newWakeLock(int, String)} |
| 1573 | * documentation. |
| 1574 | * </p> |
| 1575 | * |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1576 | * @param ws The work source, or null if none. |
| 1577 | */ |
| 1578 | public void setWorkSource(WorkSource ws) { |
| 1579 | synchronized (mToken) { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 1580 | if (ws != null && ws.isEmpty()) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1581 | ws = null; |
| 1582 | } |
| 1583 | |
| 1584 | final boolean changed; |
| 1585 | if (ws == null) { |
| 1586 | changed = mWorkSource != null; |
| 1587 | mWorkSource = null; |
| 1588 | } else if (mWorkSource == null) { |
| 1589 | changed = true; |
| 1590 | mWorkSource = new WorkSource(ws); |
| 1591 | } else { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 1592 | changed = !mWorkSource.equals(ws); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1593 | if (changed) { |
| 1594 | mWorkSource.set(ws); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | if (changed && mHeld) { |
| 1599 | try { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 1600 | mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1601 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1602 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1608 | /** @hide */ |
| 1609 | public void setTag(String tag) { |
| 1610 | mTag = tag; |
| 1611 | } |
| 1612 | |
| 1613 | /** @hide */ |
Christopher Tate | 4b17e98 | 2016-09-26 12:59:10 -0700 | [diff] [blame] | 1614 | public String getTag() { |
| 1615 | return mTag; |
| 1616 | } |
| 1617 | |
| 1618 | /** @hide */ |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1619 | public void setHistoryTag(String tag) { |
| 1620 | mHistoryTag = tag; |
| 1621 | } |
| 1622 | |
| 1623 | /** @hide */ |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1624 | public void setUnimportantForLogging(boolean state) { |
| 1625 | if (state) mFlags |= UNIMPORTANT_FOR_LOGGING; |
| 1626 | else mFlags &= ~UNIMPORTANT_FOR_LOGGING; |
| 1627 | } |
| 1628 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1629 | @Override |
| 1630 | public String toString() { |
| 1631 | synchronized (mToken) { |
| 1632 | return "WakeLock{" |
| 1633 | + Integer.toHexString(System.identityHashCode(this)) |
Jeff Sharkey | 291c32a | 2017-07-05 12:34:04 -0600 | [diff] [blame] | 1634 | + " held=" + mHeld + ", refCount=" + mInternalCount + "}"; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 1637 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 1638 | /** @hide */ |
| 1639 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1640 | synchronized (mToken) { |
| 1641 | final long token = proto.start(fieldId); |
| 1642 | proto.write(PowerManagerProto.WakeLockProto.HEX_STRING, |
| 1643 | Integer.toHexString(System.identityHashCode(this))); |
| 1644 | proto.write(PowerManagerProto.WakeLockProto.HELD, mHeld); |
| 1645 | proto.write(PowerManagerProto.WakeLockProto.INTERNAL_COUNT, mInternalCount); |
| 1646 | if (mWorkSource != null) { |
| 1647 | mWorkSource.writeToProto(proto, PowerManagerProto.WakeLockProto.WORK_SOURCE); |
| 1648 | } |
| 1649 | proto.end(token); |
| 1650 | } |
| 1651 | } |
| 1652 | |
Adrian Roos | 7445c0b | 2016-09-06 16:45:46 -0700 | [diff] [blame] | 1653 | /** |
| 1654 | * Wraps a Runnable such that this method immediately acquires the wake lock and then |
| 1655 | * once the Runnable is done the wake lock is released. |
| 1656 | * |
| 1657 | * <p>Example: |
| 1658 | * |
| 1659 | * <pre> |
| 1660 | * mHandler.post(mWakeLock.wrap(() -> { |
| 1661 | * // do things on handler, lock is held while we're waiting for this |
| 1662 | * // to get scheduled and until the runnable is done executing. |
| 1663 | * }); |
| 1664 | * </pre> |
| 1665 | * |
| 1666 | * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll |
| 1667 | * leak the wakelock! |
| 1668 | * |
| 1669 | * @hide |
| 1670 | */ |
| 1671 | public Runnable wrap(Runnable r) { |
| 1672 | acquire(); |
| 1673 | return () -> { |
| 1674 | try { |
| 1675 | r.run(); |
| 1676 | } finally { |
| 1677 | release(); |
| 1678 | } |
| 1679 | }; |
| 1680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | } |