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} |
| 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 | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 208 | * 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] | 209 | * |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 210 | * @hide |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 211 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 212 | public static final int WAKE_LOCK_LEVEL_MASK = 0x0000ffff; |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 213 | |
| 214 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 215 | * Wake lock flag: Turn the screen on when the wake lock is acquired. |
| 216 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * Normally wake locks don't actually wake the device, they just cause |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 218 | * the screen to remain on once it's already on. Think of the video player |
| 219 | * 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] | 220 | * 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] | 221 | * </p><p> |
| 222 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 223 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 225 | public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | |
| 227 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 228 | * 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] | 229 | * so the screen stays on for a little longer. |
| 230 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 231 | * Will not turn the screen on if it is not already on. |
| 232 | * See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that. |
| 233 | * </p><p> |
| 234 | * Cannot be used with {@link #PARTIAL_WAKE_LOCK}. |
| 235 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | */ |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 237 | public static final int ON_AFTER_RELEASE = 0x20000000; |
| 238 | |
| 239 | /** |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 240 | * Wake lock flag: This wake lock is not important for logging events. If a later |
| 241 | * wake lock is acquired that is important, it will be considered the one to log. |
| 242 | * @hide |
| 243 | */ |
| 244 | public static final int UNIMPORTANT_FOR_LOGGING = 0x40000000; |
| 245 | |
| 246 | /** |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 247 | * Flag for {@link WakeLock#release WakeLock.release(int)}: Defer releasing a |
| 248 | * {@link #PROXIMITY_SCREEN_OFF_WAKE_LOCK} wake lock until the proximity sensor |
| 249 | * indicates that an object is not in close proximity. |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 250 | */ |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 251 | public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY = 1; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 254 | * Brightness value for fully on. |
| 255 | * @hide |
| 256 | */ |
| 257 | public static final int BRIGHTNESS_ON = 255; |
| 258 | |
| 259 | /** |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 260 | * Brightness value for fully off. |
| 261 | * @hide |
| 262 | */ |
| 263 | public static final int BRIGHTNESS_OFF = 0; |
| 264 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Brightness value for default policy handling by the system. |
| 267 | * @hide |
| 268 | */ |
| 269 | public static final int BRIGHTNESS_DEFAULT = -1; |
| 270 | |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 271 | // Note: Be sure to update android.os.BatteryStats and PowerManager.h |
| 272 | // if adding or modifying user activity event constants. |
| 273 | |
| 274 | /** |
| 275 | * User activity event type: Unspecified event type. |
| 276 | * @hide |
| 277 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 278 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 279 | public static final int USER_ACTIVITY_EVENT_OTHER = 0; |
| 280 | |
| 281 | /** |
| 282 | * User activity event type: Button or key pressed or released. |
| 283 | * @hide |
| 284 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 285 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 286 | public static final int USER_ACTIVITY_EVENT_BUTTON = 1; |
| 287 | |
| 288 | /** |
| 289 | * User activity event type: Touch down, move or up. |
| 290 | * @hide |
| 291 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 292 | @SystemApi |
Jeff Brown | b696de5 | 2012-07-27 15:38:50 -0700 | [diff] [blame] | 293 | public static final int USER_ACTIVITY_EVENT_TOUCH = 2; |
| 294 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 295 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 296 | * User activity flag: If already dimmed, extend the dim timeout |
| 297 | * but do not brighten. This flag is useful for keeping the screen on |
| 298 | * a little longer without causing a visible change such as when |
| 299 | * the power key is pressed. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 300 | * @hide |
| 301 | */ |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 302 | @SystemApi |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 303 | public static final int USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS = 1 << 0; |
| 304 | |
| 305 | /** |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 306 | * User activity flag: Note the user activity as usual but do not |
| 307 | * reset the user activity timeout. This flag is useful for applying |
| 308 | * user activity power hints when interacting with the device indirectly |
| 309 | * on a secondary screen while allowing the primary screen to go to sleep. |
| 310 | * @hide |
| 311 | */ |
| 312 | @SystemApi |
| 313 | public static final int USER_ACTIVITY_FLAG_INDIRECT = 1 << 1; |
| 314 | |
| 315 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 316 | * Go to sleep reason code: Going to sleep due by application request. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 317 | * @hide |
| 318 | */ |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 319 | public static final int GO_TO_SLEEP_REASON_APPLICATION = 0; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * Go to sleep reason code: Going to sleep due by request of the |
| 323 | * device administration policy. |
| 324 | * @hide |
| 325 | */ |
| 326 | public static final int GO_TO_SLEEP_REASON_DEVICE_ADMIN = 1; |
| 327 | |
| 328 | /** |
| 329 | * Go to sleep reason code: Going to sleep due to a screen timeout. |
| 330 | * @hide |
| 331 | */ |
| 332 | public static final int GO_TO_SLEEP_REASON_TIMEOUT = 2; |
| 333 | |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 334 | /** |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 335 | * Go to sleep reason code: Going to sleep due to the lid switch being closed. |
| 336 | * @hide |
| 337 | */ |
| 338 | public static final int GO_TO_SLEEP_REASON_LID_SWITCH = 3; |
| 339 | |
| 340 | /** |
| 341 | * Go to sleep reason code: Going to sleep due to the power button being pressed. |
| 342 | * @hide |
| 343 | */ |
| 344 | public static final int GO_TO_SLEEP_REASON_POWER_BUTTON = 4; |
| 345 | |
| 346 | /** |
| 347 | * Go to sleep reason code: Going to sleep due to HDMI. |
| 348 | * @hide |
| 349 | */ |
| 350 | public static final int GO_TO_SLEEP_REASON_HDMI = 5; |
| 351 | |
| 352 | /** |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 353 | * Go to sleep flag: Skip dozing state and directly go to full sleep. |
| 354 | * @hide |
| 355 | */ |
| 356 | public static final int GO_TO_SLEEP_FLAG_NO_DOZE = 1 << 0; |
| 357 | |
| 358 | /** |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 359 | * The value to pass as the 'reason' argument to reboot() to |
| 360 | * reboot into recovery mode (for applying system updates, doing |
| 361 | * factory resets, etc.). |
| 362 | * <p> |
| 363 | * Requires the {@link android.Manifest.permission#RECOVERY} |
| 364 | * permission (in addition to |
| 365 | * {@link android.Manifest.permission#REBOOT}). |
| 366 | * </p> |
Doug Zongker | 183415e | 2014-08-12 10:18:40 -0700 | [diff] [blame] | 367 | * @hide |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 368 | */ |
| 369 | public static final String REBOOT_RECOVERY = "recovery"; |
| 370 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 371 | final Context mContext; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 372 | final IPowerManager mService; |
| 373 | final Handler mHandler; |
| 374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 376 | * {@hide} |
| 377 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 378 | public PowerManager(Context context, IPowerManager service, Handler handler) { |
| 379 | mContext = context; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 380 | mService = service; |
| 381 | mHandler = handler; |
| 382 | } |
| 383 | |
| 384 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 385 | * Gets the minimum supported screen brightness setting. |
| 386 | * The screen may be allowed to become dimmer than this value but |
| 387 | * this is the minimum value that can be set by the user. |
| 388 | * @hide |
| 389 | */ |
| 390 | public int getMinimumScreenBrightnessSetting() { |
| 391 | return mContext.getResources().getInteger( |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 392 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Gets the maximum supported screen brightness setting. |
| 397 | * The screen may be allowed to become dimmer than this value but |
| 398 | * this is the maximum value that can be set by the user. |
| 399 | * @hide |
| 400 | */ |
| 401 | public int getMaximumScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 402 | return mContext.getResources().getInteger( |
| 403 | com.android.internal.R.integer.config_screenBrightnessSettingMaximum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Gets the default screen brightness setting. |
| 408 | * @hide |
| 409 | */ |
| 410 | public int getDefaultScreenBrightnessSetting() { |
Jeff Brown | f9bba13 | 2012-08-21 22:04:02 -0700 | [diff] [blame] | 411 | return mContext.getResources().getInteger( |
| 412 | com.android.internal.R.integer.config_screenBrightnessSettingDefault); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
Jeff Brown | db21284 | 2012-10-01 14:33:09 -0700 | [diff] [blame] | 416 | * Returns true if the twilight service should be used to adjust screen brightness |
| 417 | * policy. This setting is experimental and disabled by default. |
| 418 | * @hide |
| 419 | */ |
| 420 | public static boolean useTwilightAdjustmentFeature() { |
| 421 | return SystemProperties.getBoolean("persist.power.usetwilightadj", false); |
| 422 | } |
| 423 | |
| 424 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 425 | * Creates a new wake lock with the specified level and flags. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 426 | * <p> |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 427 | * The {@code levelAndFlags} parameter specifies a wake lock level and optional flags |
| 428 | * combined using the logical OR operator. |
| 429 | * </p><p> |
| 430 | * The wake lock levels are: {@link #PARTIAL_WAKE_LOCK}, |
| 431 | * {@link #FULL_WAKE_LOCK}, {@link #SCREEN_DIM_WAKE_LOCK} |
| 432 | * and {@link #SCREEN_BRIGHT_WAKE_LOCK}. Exactly one wake lock level must be |
| 433 | * specified as part of the {@code levelAndFlags} parameter. |
| 434 | * </p><p> |
| 435 | * The wake lock flags are: {@link #ACQUIRE_CAUSES_WAKEUP} |
| 436 | * and {@link #ON_AFTER_RELEASE}. Multiple flags can be combined as part of the |
| 437 | * {@code levelAndFlags} parameters. |
| 438 | * </p><p> |
| 439 | * Call {@link WakeLock#acquire() acquire()} on the object to acquire the |
| 440 | * wake lock, and {@link WakeLock#release release()} when you are done. |
| 441 | * </p><p> |
| 442 | * {@samplecode |
| 443 | * PowerManager pm = (PowerManager)mContext.getSystemService( |
| 444 | * Context.POWER_SERVICE); |
| 445 | * PowerManager.WakeLock wl = pm.newWakeLock( |
| 446 | * PowerManager.SCREEN_DIM_WAKE_LOCK |
| 447 | * | PowerManager.ON_AFTER_RELEASE, |
| 448 | * TAG); |
| 449 | * wl.acquire(); |
| 450 | * // ... do work... |
| 451 | * wl.release(); |
| 452 | * } |
| 453 | * </p><p> |
| 454 | * Although a wake lock can be created without special permissions, |
| 455 | * the {@link android.Manifest.permission#WAKE_LOCK} permission is |
| 456 | * required to actually acquire or release the wake lock that is returned. |
| 457 | * </p><p class="note"> |
| 458 | * If using this to keep the screen on, you should strongly consider using |
| 459 | * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead. |
| 460 | * This window flag will be correctly managed by the platform |
| 461 | * as the user moves between applications and doesn't require a special permission. |
| 462 | * </p> |
| 463 | * |
| 464 | * @param levelAndFlags Combination of wake lock level and flag values defining |
| 465 | * the requested behavior of the WakeLock. |
| 466 | * @param tag Your class name (or other tag) for debugging purposes. |
| 467 | * |
| 468 | * @see WakeLock#acquire() |
| 469 | * @see WakeLock#release() |
| 470 | * @see #PARTIAL_WAKE_LOCK |
| 471 | * @see #FULL_WAKE_LOCK |
| 472 | * @see #SCREEN_DIM_WAKE_LOCK |
| 473 | * @see #SCREEN_BRIGHT_WAKE_LOCK |
Jeff Brown | a71f03c | 2014-08-21 18:01:51 -0700 | [diff] [blame] | 474 | * @see #PROXIMITY_SCREEN_OFF_WAKE_LOCK |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 475 | * @see #ACQUIRE_CAUSES_WAKEUP |
| 476 | * @see #ON_AFTER_RELEASE |
| 477 | */ |
| 478 | public WakeLock newWakeLock(int levelAndFlags, String tag) { |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 479 | validateWakeLockParameters(levelAndFlags, tag); |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 480 | return new WakeLock(levelAndFlags, tag, mContext.getOpPackageName()); |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** @hide */ |
| 484 | public static void validateWakeLockParameters(int levelAndFlags, String tag) { |
| 485 | switch (levelAndFlags & WAKE_LOCK_LEVEL_MASK) { |
| 486 | case PARTIAL_WAKE_LOCK: |
| 487 | case SCREEN_DIM_WAKE_LOCK: |
| 488 | case SCREEN_BRIGHT_WAKE_LOCK: |
| 489 | case FULL_WAKE_LOCK: |
| 490 | case PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 491 | case DOZE_WAKE_LOCK: |
Jeff Brown | 155fc70 | 2012-07-27 12:12:15 -0700 | [diff] [blame] | 492 | break; |
| 493 | default: |
| 494 | throw new IllegalArgumentException("Must specify a valid wake lock level."); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 495 | } |
| 496 | if (tag == null) { |
| 497 | throw new IllegalArgumentException("The tag must not be null."); |
| 498 | } |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Notifies the power manager that user activity happened. |
| 503 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 504 | * Resets the auto-off timer and brightens the screen if the device |
| 505 | * is not asleep. This is what happens normally when a key or the touch |
| 506 | * screen is pressed or when some other user activity occurs. |
| 507 | * 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] | 508 | * </p><p> |
| 509 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 510 | * </p> |
| 511 | * |
| 512 | * @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] | 513 | * 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] | 514 | * other power management functions. It should be set |
| 515 | * to the timestamp of the input event that caused the user activity. |
| 516 | * @param noChangeLights If true, does not cause the keyboard backlight to turn on |
| 517 | * because of this event. This is set when the power key is pressed. |
| 518 | * We want the device to stay on while the button is down, but we're about |
| 519 | * to turn off the screen so we don't want the keyboard backlight to turn on again. |
| 520 | * Otherwise the lights flash on and then off and it looks weird. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 521 | * |
| 522 | * @see #wakeUp |
| 523 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 524 | * |
| 525 | * @removed Requires signature or system permission. |
| 526 | * @deprecated Use {@link #userActivity(long, int, int)}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 527 | */ |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 528 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 529 | public void userActivity(long when, boolean noChangeLights) { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 530 | userActivity(when, USER_ACTIVITY_EVENT_OTHER, |
| 531 | noChangeLights ? USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Notifies the power manager that user activity happened. |
| 536 | * <p> |
| 537 | * Resets the auto-off timer and brightens the screen if the device |
| 538 | * is not asleep. This is what happens normally when a key or the touch |
| 539 | * screen is pressed or when some other user activity occurs. |
| 540 | * This method does not wake up the device if it has been put to sleep. |
| 541 | * </p><p> |
| 542 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} or |
| 543 | * {@link android.Manifest.permission#USER_ACTIVITY} permission. |
| 544 | * </p> |
| 545 | * |
| 546 | * @param when The time of the user activity, in the {@link SystemClock#uptimeMillis()} |
| 547 | * time base. This timestamp is used to correctly order the user activity request with |
| 548 | * other power management functions. It should be set |
| 549 | * to the timestamp of the input event that caused the user activity. |
| 550 | * @param event The user activity event. |
| 551 | * @param flags Optional user activity flags. |
| 552 | * |
| 553 | * @see #wakeUp |
| 554 | * @see #goToSleep |
| 555 | * |
| 556 | * @hide Requires signature or system permission. |
| 557 | */ |
| 558 | @SystemApi |
| 559 | public void userActivity(long when, int event, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 560 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 561 | mService.userActivity(when, event, flags); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 562 | } catch (RemoteException e) { |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Forces the device to go to sleep. |
| 568 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 569 | * Overrides all the wake locks that are held. |
| 570 | * 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] | 571 | * </p><p> |
| 572 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 573 | * </p> |
| 574 | * |
| 575 | * @param time The time when the request to go to sleep was issued, in the |
| 576 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 577 | * 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] | 578 | * 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] | 579 | * |
| 580 | * @see #userActivity |
| 581 | * @see #wakeUp |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 582 | * |
| 583 | * @removed Requires signature permission. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 584 | */ |
| 585 | public void goToSleep(long time) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 586 | goToSleep(time, GO_TO_SLEEP_REASON_APPLICATION, 0); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 590 | * Forces the device to go to sleep. |
| 591 | * <p> |
| 592 | * Overrides all the wake locks that are held. |
| 593 | * This is what happens when the power key is pressed to turn off the screen. |
| 594 | * </p><p> |
| 595 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 596 | * </p> |
| 597 | * |
| 598 | * @param time The time when the request to go to sleep was issued, in the |
| 599 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 600 | * order the go to sleep request with other power management functions. It should be set |
| 601 | * to the timestamp of the input event that caused the request to go to sleep. |
| 602 | * @param reason The reason the device is going to sleep. |
| 603 | * @param flags Optional flags to apply when going to sleep. |
| 604 | * |
| 605 | * @see #userActivity |
| 606 | * @see #wakeUp |
| 607 | * |
| 608 | * @hide Requires signature permission. |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 609 | */ |
| 610 | public void goToSleep(long time, int reason, int flags) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 611 | try { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 612 | mService.goToSleep(time, reason, flags); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 613 | } catch (RemoteException e) { |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Forces the device to wake up from sleep. |
| 619 | * <p> |
| 620 | * If the device is currently asleep, wakes it up, otherwise does nothing. |
| 621 | * This is what happens when the power key is pressed to turn on the screen. |
| 622 | * </p><p> |
| 623 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 624 | * </p> |
| 625 | * |
| 626 | * @param time The time when the request to wake up was issued, in the |
| 627 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 628 | * 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] | 629 | * to the timestamp of the input event that caused the request to wake up. |
| 630 | * |
| 631 | * @see #userActivity |
| 632 | * @see #goToSleep |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 633 | * |
| 634 | * @removed Requires signature permission. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 635 | */ |
| 636 | public void wakeUp(long time) { |
| 637 | try { |
| 638 | mService.wakeUp(time); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 639 | } catch (RemoteException e) { |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 644 | * Forces the device to start napping. |
| 645 | * <p> |
| 646 | * If the device is currently awake, starts dreaming, otherwise does nothing. |
| 647 | * When the dream ends or if the dream cannot be started, the device will |
| 648 | * either wake up or go to sleep depending on whether there has been recent |
| 649 | * user activity. |
| 650 | * </p><p> |
| 651 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 652 | * </p> |
| 653 | * |
| 654 | * @param time The time when the request to nap was issued, in the |
| 655 | * {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly |
| 656 | * order the nap request with other power management functions. It should be set |
| 657 | * to the timestamp of the input event that caused the request to nap. |
| 658 | * |
| 659 | * @see #wakeUp |
| 660 | * @see #goToSleep |
| 661 | * |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 662 | * @hide Requires signature permission. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 663 | */ |
| 664 | public void nap(long time) { |
| 665 | try { |
| 666 | mService.nap(time); |
| 667 | } catch (RemoteException e) { |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 672 | * Sets the brightness of the backlights (screen, keyboard, button). |
| 673 | * <p> |
| 674 | * Requires the {@link android.Manifest.permission#DEVICE_POWER} permission. |
| 675 | * </p> |
| 676 | * |
| 677 | * @param brightness The brightness value from 0 to 255. |
| 678 | * |
Jeff Brown | 7d82751 | 2014-08-21 21:56:02 -0700 | [diff] [blame] | 679 | * @hide Requires signature permission. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 680 | */ |
| 681 | public void setBacklightBrightness(int brightness) { |
| 682 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 683 | mService.setTemporaryScreenBrightnessSettingOverride(brightness); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 684 | } catch (RemoteException e) { |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 689 | * Returns true if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 690 | * |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 691 | * @param level The wake lock level to check. |
| 692 | * @return True if the specified wake lock level is supported. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 693 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 694 | public boolean isWakeLockLevelSupported(int level) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 695 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 696 | return mService.isWakeLockLevelSupported(level); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 697 | } catch (RemoteException e) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 698 | return false; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | /** |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 703 | * Returns true if the device is in an interactive state. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 704 | * <p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 705 | * For historical reasons, the name of this method refers to the power state of |
| 706 | * the screen but it actually describes the overall interactive state of |
| 707 | * the device. This method has been replaced by {@link #isInteractive}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 708 | * </p><p> |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 709 | * The value returned by this method only indicates whether the device is |
| 710 | * in an interactive state which may have nothing to do with the screen being |
| 711 | * on or off. To determine the actual state of the screen, |
| 712 | * use {@link android.view.Display#getState}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 713 | * </p> |
| 714 | * |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 715 | * @return True if the device is in an interactive state. |
| 716 | * |
| 717 | * @deprecated Use {@link #isInteractive} instead. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 718 | */ |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 719 | @Deprecated |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 720 | public boolean isScreenOn() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 721 | return isInteractive(); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Returns true if the device is in an interactive state. |
| 726 | * <p> |
| 727 | * When this method returns true, the device is awake and ready to interact |
| 728 | * with the user (although this is not a guarantee that the user is actively |
| 729 | * interacting with the device just this moment). The main screen is usually |
| 730 | * turned on while in this state. Certain features, such as the proximity |
| 731 | * sensor, may temporarily turn off the screen while still leaving the device in an |
| 732 | * interactive state. Note in particular that the device is still considered |
| 733 | * to be interactive while dreaming (since dreams can be interactive) but not |
| 734 | * when it is dozing or asleep. |
| 735 | * </p><p> |
| 736 | * When this method returns false, the device is dozing or asleep and must |
| 737 | * be awoken before it will become ready to interact with the user again. The |
| 738 | * main screen is usually turned off while in this state. Certain features, |
| 739 | * such as "ambient mode" may cause the main screen to remain on (albeit in a |
| 740 | * low power state) to display system-provided content while the device dozes. |
| 741 | * </p><p> |
| 742 | * The system will send a {@link android.content.Intent#ACTION_SCREEN_ON screen on} |
| 743 | * or {@link android.content.Intent#ACTION_SCREEN_OFF screen off} broadcast |
| 744 | * whenever the interactive state of the device changes. For historical reasons, |
| 745 | * the names of these broadcasts refer to the power state of the screen |
| 746 | * but they are actually sent in response to changes in the overall interactive |
| 747 | * state of the device, as described by this method. |
| 748 | * </p><p> |
| 749 | * Services may use the non-interactive state as a hint to conserve power |
| 750 | * since the user is not present. |
| 751 | * </p> |
| 752 | * |
| 753 | * @return True if the device is in an interactive state. |
| 754 | * |
| 755 | * @see android.content.Intent#ACTION_SCREEN_ON |
| 756 | * @see android.content.Intent#ACTION_SCREEN_OFF |
| 757 | */ |
| 758 | public boolean isInteractive() { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 759 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 760 | return mService.isInteractive(); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 761 | } catch (RemoteException e) { |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Reboot the device. Will not return if the reboot is successful. |
| 768 | * <p> |
| 769 | * Requires the {@link android.Manifest.permission#REBOOT} permission. |
| 770 | * </p> |
| 771 | * |
| 772 | * @param reason code to pass to the kernel (e.g., "recovery") to |
| 773 | * request special boot modes, or null. |
| 774 | */ |
| 775 | public void reboot(String reason) { |
| 776 | try { |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 777 | mService.reboot(false, reason, true); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 778 | } catch (RemoteException e) { |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 783 | * Returns true if the device is currently in power save mode. When in this mode, |
| 784 | * applications should reduce their functionality in order to conserve battery as |
| 785 | * much as possible. You can monitor for changes to this state with |
| 786 | * {@link #ACTION_POWER_SAVE_MODE_CHANGED}. |
| 787 | * |
| 788 | * @return Returns true if currently in low power mode, else false. |
| 789 | */ |
| 790 | public boolean isPowerSaveMode() { |
| 791 | try { |
| 792 | return mService.isPowerSaveMode(); |
| 793 | } catch (RemoteException e) { |
| 794 | return false; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /** |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 799 | * Set the current power save mode. |
| 800 | * |
| 801 | * @return True if the set was allowed. |
| 802 | * |
| 803 | * @see #isPowerSaveMode() |
| 804 | * |
| 805 | * @hide |
| 806 | */ |
| 807 | public boolean setPowerSaveMode(boolean mode) { |
| 808 | try { |
| 809 | return mService.setPowerSaveMode(mode); |
| 810 | } catch (RemoteException e) { |
| 811 | return false; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /** |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 816 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} changes. |
| 817 | * This broadcast is only sent to registered receivers. |
| 818 | */ |
| 819 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 820 | public static final String ACTION_POWER_SAVE_MODE_CHANGED |
| 821 | = "android.os.action.POWER_SAVE_MODE_CHANGED"; |
| 822 | |
| 823 | /** |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 824 | * Intent that is broadcast when the state of {@link #isPowerSaveMode()} is about to change. |
| 825 | * This broadcast is only sent to registered receivers. |
| 826 | * |
| 827 | * @hide |
| 828 | */ |
| 829 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 830 | public static final String ACTION_POWER_SAVE_MODE_CHANGING |
| 831 | = "android.os.action.POWER_SAVE_MODE_CHANGING"; |
| 832 | |
| 833 | /** @hide */ |
| 834 | public static final String EXTRA_POWER_SAVE_MODE = "mode"; |
| 835 | |
| 836 | /** |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 837 | * A wake lock is a mechanism to indicate that your application needs |
| 838 | * to have the device stay on. |
Kenny Root | d710fb5 | 2011-03-15 17:39:45 -0700 | [diff] [blame] | 839 | * <p> |
| 840 | * Any application using a WakeLock must request the {@code android.permission.WAKE_LOCK} |
| 841 | * permission in an {@code <uses-permission>} element of the application's manifest. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 842 | * Obtain a wake lock by calling {@link PowerManager#newWakeLock(int, String)}. |
| 843 | * </p><p> |
| 844 | * Call {@link #acquire()} to acquire the wake lock and force the device to stay |
| 845 | * on at the level that was requested when the wake lock was created. |
| 846 | * </p><p> |
| 847 | * Call {@link #release()} when you are done and don't need the lock anymore. |
| 848 | * It is very important to do this as soon as possible to avoid running down the |
| 849 | * device's battery excessively. |
| 850 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | */ |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 852 | public final class WakeLock { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 853 | private int mFlags; |
| 854 | private String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 855 | private final String mPackageName; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 856 | private final IBinder mToken; |
| 857 | private int mCount; |
| 858 | private boolean mRefCounted = true; |
| 859 | private boolean mHeld; |
| 860 | private WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 861 | private String mHistoryTag; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 862 | private final String mTraceName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 864 | private final Runnable mReleaser = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | public void run() { |
| 866 | release(); |
| 867 | } |
| 868 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 870 | WakeLock(int flags, String tag, String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | mFlags = flags; |
| 872 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 873 | mPackageName = packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | mToken = new Binder(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 875 | mTraceName = "WakeLock (" + mTag + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | } |
| 877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | @Override |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 879 | protected void finalize() throws Throwable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | synchronized (mToken) { |
| 881 | if (mHeld) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 882 | Log.wtf(TAG, "WakeLock finalized while still held: " + mTag); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 883 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | try { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 885 | mService.releaseWakeLock(mToken, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | } catch (RemoteException e) { |
| 887 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 892 | /** |
| 893 | * Sets whether this WakeLock is reference counted. |
| 894 | * <p> |
| 895 | * Wake locks are reference counted by default. If a wake lock is |
| 896 | * reference counted, then each call to {@link #acquire()} must be |
| 897 | * balanced by an equal number of calls to {@link #release()}. If a wake |
| 898 | * lock is not reference counted, then one call to {@link #release()} is |
| 899 | * sufficient to undo the effect of all previous calls to {@link #acquire()}. |
| 900 | * </p> |
| 901 | * |
| 902 | * @param value True to make the wake lock reference counted, false to |
| 903 | * make the wake lock non-reference counted. |
| 904 | */ |
| 905 | public void setReferenceCounted(boolean value) { |
| 906 | synchronized (mToken) { |
| 907 | mRefCounted = value; |
| 908 | } |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 909 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 911 | /** |
| 912 | * Acquires the wake lock. |
| 913 | * <p> |
| 914 | * Ensures that the device is on at the level requested when |
| 915 | * the wake lock was created. |
| 916 | * </p> |
| 917 | */ |
| 918 | public void acquire() { |
| 919 | synchronized (mToken) { |
| 920 | acquireLocked(); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Acquires the wake lock with a timeout. |
| 926 | * <p> |
| 927 | * Ensures that the device is on at the level requested when |
| 928 | * the wake lock was created. The lock will be released after the given timeout |
| 929 | * expires. |
| 930 | * </p> |
| 931 | * |
| 932 | * @param timeout The timeout after which to release the wake lock, in milliseconds. |
| 933 | */ |
| 934 | public void acquire(long timeout) { |
| 935 | synchronized (mToken) { |
| 936 | acquireLocked(); |
| 937 | mHandler.postDelayed(mReleaser, timeout); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | private void acquireLocked() { |
| 942 | if (!mRefCounted || mCount++ == 0) { |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 943 | // Do this even if the wake lock is already thought to be held (mHeld == true) |
| 944 | // because non-reference counted wake locks are not always properly released. |
| 945 | // For example, the keyguard's wake lock might be forcibly released by the |
| 946 | // power manager without the keyguard knowing. A subsequent call to acquire |
| 947 | // should immediately acquire the wake lock once again despite never having |
| 948 | // been explicitly released by the keyguard. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 949 | mHandler.removeCallbacks(mReleaser); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 950 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 951 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 952 | mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource, |
| 953 | mHistoryTag); |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 954 | } catch (RemoteException e) { |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 955 | } |
Jeff Brown | ff1baef | 2012-07-19 15:01:17 -0700 | [diff] [blame] | 956 | mHeld = true; |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * Releases the wake lock. |
| 962 | * <p> |
| 963 | * This method releases your claim to the CPU or screen being on. |
| 964 | * The screen may turn off shortly after you release the wake lock, or it may |
| 965 | * not if there are other wake locks still held. |
| 966 | * </p> |
| 967 | */ |
| 968 | public void release() { |
| 969 | release(0); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Releases the wake lock with flags to modify the release behavior. |
| 974 | * <p> |
| 975 | * This method releases your claim to the CPU or screen being on. |
| 976 | * The screen may turn off shortly after you release the wake lock, or it may |
| 977 | * not if there are other wake locks still held. |
| 978 | * </p> |
| 979 | * |
| 980 | * @param flags Combination of flag values to modify the release behavior. |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 981 | * Currently only {@link #RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY} is supported. |
| 982 | * Passing 0 is equivalent to calling {@link #release()}. |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 983 | */ |
| 984 | public void release(int flags) { |
| 985 | synchronized (mToken) { |
| 986 | if (!mRefCounted || --mCount == 0) { |
| 987 | mHandler.removeCallbacks(mReleaser); |
| 988 | if (mHeld) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 989 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 990 | try { |
| 991 | mService.releaseWakeLock(mToken, flags); |
| 992 | } catch (RemoteException e) { |
| 993 | } |
| 994 | mHeld = false; |
| 995 | } |
| 996 | } |
| 997 | if (mCount < 0) { |
| 998 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * Returns true if the wake lock has been acquired but not yet released. |
| 1005 | * |
| 1006 | * @return True if the wake lock is held. |
| 1007 | */ |
| 1008 | public boolean isHeld() { |
| 1009 | synchronized (mToken) { |
| 1010 | return mHeld; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * Sets the work source associated with the wake lock. |
| 1016 | * <p> |
| 1017 | * The work source is used to determine on behalf of which application |
| 1018 | * the wake lock is being held. This is useful in the case where a |
| 1019 | * service is performing work on behalf of an application so that the |
| 1020 | * cost of that work can be accounted to the application. |
| 1021 | * </p> |
| 1022 | * |
| 1023 | * @param ws The work source, or null if none. |
| 1024 | */ |
| 1025 | public void setWorkSource(WorkSource ws) { |
| 1026 | synchronized (mToken) { |
| 1027 | if (ws != null && ws.size() == 0) { |
| 1028 | ws = null; |
| 1029 | } |
| 1030 | |
| 1031 | final boolean changed; |
| 1032 | if (ws == null) { |
| 1033 | changed = mWorkSource != null; |
| 1034 | mWorkSource = null; |
| 1035 | } else if (mWorkSource == null) { |
| 1036 | changed = true; |
| 1037 | mWorkSource = new WorkSource(ws); |
| 1038 | } else { |
| 1039 | changed = mWorkSource.diff(ws); |
| 1040 | if (changed) { |
| 1041 | mWorkSource.set(ws); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (changed && mHeld) { |
| 1046 | try { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 1047 | mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag); |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1048 | } catch (RemoteException e) { |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1054 | /** @hide */ |
| 1055 | public void setTag(String tag) { |
| 1056 | mTag = tag; |
| 1057 | } |
| 1058 | |
| 1059 | /** @hide */ |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1060 | public void setHistoryTag(String tag) { |
| 1061 | mHistoryTag = tag; |
| 1062 | } |
| 1063 | |
| 1064 | /** @hide */ |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1065 | public void setUnimportantForLogging(boolean state) { |
| 1066 | if (state) mFlags |= UNIMPORTANT_FOR_LOGGING; |
| 1067 | else mFlags &= ~UNIMPORTANT_FOR_LOGGING; |
| 1068 | } |
| 1069 | |
Jeff Brown | 1244cda | 2012-06-19 16:44:46 -0700 | [diff] [blame] | 1070 | @Override |
| 1071 | public String toString() { |
| 1072 | synchronized (mToken) { |
| 1073 | return "WakeLock{" |
| 1074 | + Integer.toHexString(System.identityHashCode(this)) |
| 1075 | + " held=" + mHeld + ", refCount=" + mCount + "}"; |
| 1076 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } |