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