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