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