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