blob: aa49ba62f48b1b4251488abf787c04e46170d945 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Jeff Brown4f8ecd82012-06-18 18:29:13 -070017package com.android.server.power;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Beverlyae79ab92017-12-11 09:20:02 -050019import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP;
20import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE;
21import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING;
22import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
23
Tony Mantlerb8009fd2016-03-14 15:55:35 -070024import android.annotation.IntDef;
Kweku Adams9f488e22019-01-14 16:25:08 -080025import android.annotation.NonNull;
Pavel Grafov28939982017-10-03 15:11:52 +010026import android.annotation.UserIdInt;
Jason Monkafae4bd2015-12-15 14:20:06 -050027import android.app.ActivityManager;
Pavel Grafov28939982017-10-03 15:11:52 +010028import android.app.SynchronousUserSwitchObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.ContentResolver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070035import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080036import android.database.ContentObserver;
Jeff Brown3b971592013-01-09 18:46:37 -080037import android.hardware.SensorManager;
38import android.hardware.SystemSensorManager;
Issei Suzukica19e6e2019-02-26 12:39:11 +010039import android.hardware.display.AmbientDisplayConfiguration;
Jeff Brown4ccb8232014-01-16 22:16:42 -080040import android.hardware.display.DisplayManagerInternal;
Jeff Brown131206b2014-04-08 17:27:14 -070041import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070042import android.hardware.power.V1_0.PowerHint;
Jeff Brown96307042012-07-27 15:51:34 -070043import android.net.Uri;
Amith Yamasani8b619832010-09-22 16:11:59 -070044import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070045import android.os.BatteryManagerInternal;
Kweku Adams9f488e22019-01-14 16:25:08 -080046import android.os.BatterySaverPolicyConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
48import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.IBinder;
50import android.os.IPowerManager;
Jeff Brown96307042012-07-27 15:51:34 -070051import android.os.Looper;
Jim Miller92e66dd2012-02-21 18:57:12 -080052import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.PowerManager;
Makoto Onuki2eccd022017-11-01 13:44:23 -070054import android.os.PowerManager.ServiceType;
Santos Cordon623526b2019-04-09 17:02:38 +010055import android.os.PowerManager.WakeData;
Michael Wrighte3001042019-02-05 00:13:14 +000056import android.os.PowerManager.WakeReason;
Jeff Brown6f357d32014-01-15 20:40:55 -080057import android.os.PowerManagerInternal;
jackqdyulei92681e82017-02-28 11:26:28 -080058import android.os.PowerSaveState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
60import android.os.RemoteException;
Jocelyn Dangf2c38c12017-03-31 14:03:37 -070061import android.os.ResultReceiver;
62import android.os.ShellCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.SystemClock;
Nick Kralevichdbcf2d72013-04-18 14:41:40 -070064import android.os.SystemProperties;
Jeff Brown3edf5272014-08-14 19:25:14 -070065import android.os.Trace;
Jeff Brownd4935962012-09-25 13:27:20 -070066import android.os.UserHandle;
Christine Franks732c0432017-06-23 18:12:46 -070067import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070068import android.os.WorkSource;
Narayan Kamath2f916ed2017-12-29 13:02:15 +000069import android.os.WorkSource.WorkChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.provider.Settings;
Daichi Hirono82ab9802016-03-02 13:23:29 +090071import android.provider.Settings.SettingNotFoundException;
Jeff Brown567f7ca2014-01-30 23:38:03 -080072import android.service.dreams.DreamManagerInternal;
Ruben Brunkc7be3be2016-04-01 17:07:51 -070073import android.service.vr.IVrManager;
74import android.service.vr.IVrStateCallbacks;
Dianne Hackborn0ef403e2017-01-24 18:22:15 -080075import android.util.KeyValueListParser;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -070076import android.util.PrintWriterPrinter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080077import android.util.Slog;
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -070078import android.util.SparseArray;
Jeff Brown96307042012-07-27 15:51:34 -070079import android.util.TimeUtils;
Netta P958d0a52017-02-07 11:20:55 -080080import android.util.proto.ProtoOutputStream;
Jeff Brown037c33e2014-04-09 00:31:55 -070081import android.view.Display;
Makoto Onukiaae89532017-11-08 14:32:03 -080082
jackqdyulei92681e82017-02-28 11:26:28 -080083import com.android.internal.annotations.VisibleForTesting;
Jason Monkafae4bd2015-12-15 14:20:06 -050084import com.android.internal.app.IAppOpsService;
85import com.android.internal.app.IBatteryStats;
86import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060087import com.android.internal.util.DumpUtils;
Jason Monkafae4bd2015-12-15 14:20:06 -050088import com.android.server.EventLogTags;
Jeff Sharkey5f3e9342017-03-13 14:53:11 -060089import com.android.server.LockGuard;
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -070090import com.android.server.RescueParty;
Jason Monkafae4bd2015-12-15 14:20:06 -050091import com.android.server.ServiceThread;
92import com.android.server.SystemService;
Michael Wright64c820d2017-03-21 12:36:55 +000093import com.android.server.UiThread;
Jason Monkafae4bd2015-12-15 14:20:06 -050094import com.android.server.Watchdog;
95import com.android.server.am.BatteryStatsService;
96import com.android.server.lights.Light;
97import com.android.server.lights.LightsManager;
Adrian Roose99bc052017-11-20 17:55:31 +010098import com.android.server.policy.WindowManagerPolicy;
Makoto Onuki66a78122017-11-14 15:03:21 -080099import com.android.server.power.batterysaver.BatterySaverController;
Kweku Adams7fb72a42019-01-08 16:08:49 -0800100import com.android.server.power.batterysaver.BatterySaverPolicy;
Makoto Onukia3cd7b92018-03-19 14:47:05 -0700101import com.android.server.power.batterysaver.BatterySaverStateMachine;
Makoto Onukibd7a6252018-05-10 13:41:39 -0700102import com.android.server.power.batterysaver.BatterySavingStats;
Makoto Onuki66a78122017-11-14 15:03:21 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import java.io.FileDescriptor;
105import java.io.PrintWriter;
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700106import java.lang.annotation.Retention;
107import java.lang.annotation.RetentionPolicy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import java.util.ArrayList;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700109import java.util.Arrays;
Narayan Kamath607223f2018-02-19 14:09:02 +0000110import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Jeff Brown96307042012-07-27 15:51:34 -0700112/**
113 * The power manager service is responsible for coordinating power management
114 * functions on the device.
115 */
Jeff Brown2175e9c2014-09-12 16:11:07 -0700116public final class PowerManagerService extends SystemService
Jeff Brown96307042012-07-27 15:51:34 -0700117 implements Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static final String TAG = "PowerManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Santos Cordon4ddb8012017-09-20 15:25:44 -0700120 private static final boolean DEBUG = false;
Jeff Brown96307042012-07-27 15:51:34 -0700121 private static final boolean DEBUG_SPEW = DEBUG && true;
Jeff Brown88c997a2012-06-22 13:57:45 -0700122
Jeff Brown96307042012-07-27 15:51:34 -0700123 // Message: Sent when a user activity timeout occurs to update the power state.
124 private static final int MSG_USER_ACTIVITY_TIMEOUT = 1;
Jeff Brown26875502014-01-30 21:47:47 -0800125 // Message: Sent when the device enters or exits a dreaming or dozing state.
Jeff Brown96307042012-07-27 15:51:34 -0700126 private static final int MSG_SANDMAN = 2;
Jeff Browne333e672014-10-28 13:48:55 -0700127 // Message: Sent when the screen brightness boost expires.
128 private static final int MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 3;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -0700129 // Message: Polling to look for long held wake locks.
130 private static final int MSG_CHECK_FOR_LONG_WAKELOCKS = 4;
Jeff Brown7304c342012-05-11 18:42:42 -0700131
Jeff Brown96307042012-07-27 15:51:34 -0700132 // Dirty bit: mWakeLocks changed
133 private static final int DIRTY_WAKE_LOCKS = 1 << 0;
134 // Dirty bit: mWakefulness changed
135 private static final int DIRTY_WAKEFULNESS = 1 << 1;
136 // Dirty bit: user activity was poked or may have timed out
137 private static final int DIRTY_USER_ACTIVITY = 1 << 2;
138 // Dirty bit: actual display power state was updated asynchronously
139 private static final int DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED = 1 << 3;
140 // Dirty bit: mBootCompleted changed
141 private static final int DIRTY_BOOT_COMPLETED = 1 << 4;
142 // Dirty bit: settings changed
143 private static final int DIRTY_SETTINGS = 1 << 5;
144 // Dirty bit: mIsPowered changed
145 private static final int DIRTY_IS_POWERED = 1 << 6;
146 // Dirty bit: mStayOn changed
147 private static final int DIRTY_STAY_ON = 1 << 7;
148 // Dirty bit: battery state changed
149 private static final int DIRTY_BATTERY_STATE = 1 << 8;
Jeff Brown93cbbb22012-10-04 13:18:36 -0700150 // Dirty bit: proximity state changed
151 private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9;
Jeff Brownec6aa592012-10-17 20:30:25 -0700152 // Dirty bit: dock state changed
Jeff Brown3ee549c2014-09-22 20:14:39 -0700153 private static final int DIRTY_DOCK_STATE = 1 << 10;
Jeff Browne333e672014-10-28 13:48:55 -0700154 // Dirty bit: brightness boost changed
155 private static final int DIRTY_SCREEN_BRIGHTNESS_BOOST = 1 << 11;
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800156 // Dirty bit: sQuiescent changed
157 private static final int DIRTY_QUIESCENT = 1 << 12;
Santos Cordon3107d292016-09-20 15:50:35 -0700158 // Dirty bit: VR Mode enabled changed
159 private static final int DIRTY_VR_MODE_CHANGED = 1 << 13;
Jeff Brown7304c342012-05-11 18:42:42 -0700160
Jeff Brown96307042012-07-27 15:51:34 -0700161 // Summarizes the state of all active wakelocks.
162 private static final int WAKE_LOCK_CPU = 1 << 0;
163 private static final int WAKE_LOCK_SCREEN_BRIGHT = 1 << 1;
164 private static final int WAKE_LOCK_SCREEN_DIM = 1 << 2;
165 private static final int WAKE_LOCK_BUTTON_BRIGHT = 1 << 3;
166 private static final int WAKE_LOCK_PROXIMITY_SCREEN_OFF = 1 << 4;
Jeff Brown10428742012-10-09 15:47:30 -0700167 private static final int WAKE_LOCK_STAY_AWAKE = 1 << 5; // only set if already awake
Jeff Brown26875502014-01-30 21:47:47 -0800168 private static final int WAKE_LOCK_DOZE = 1 << 6;
Jeff Brownc2932a12014-11-20 18:04:05 -0800169 private static final int WAKE_LOCK_DRAW = 1 << 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Jeff Brown96307042012-07-27 15:51:34 -0700171 // Summarizes the user activity state.
172 private static final int USER_ACTIVITY_SCREEN_BRIGHT = 1 << 0;
173 private static final int USER_ACTIVITY_SCREEN_DIM = 1 << 1;
Jeff Brown05af6ad2014-09-30 20:54:30 -0700174 private static final int USER_ACTIVITY_SCREEN_DREAM = 1 << 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Jeff Brown27736f52014-05-20 17:17:10 -0700176 // Default timeout in milliseconds. This is only used until the settings
177 // provider populates the actual default value (R.integer.def_screen_off_timeout).
Jeff Brown96307042012-07-27 15:51:34 -0700178 private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
Jeff Brown05af6ad2014-09-30 20:54:30 -0700179 private static final int DEFAULT_SLEEP_TIMEOUT = -1;
Jeff Brownff532542012-10-02 21:18:04 -0700180
Jeff Browne333e672014-10-28 13:48:55 -0700181 // Screen brightness boost timeout.
182 // Hardcoded for now until we decide what the right policy should be.
183 // This should perhaps be a setting.
184 private static final int SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 5 * 1000;
185
Dianne Hackbornd0db6f02016-07-18 14:14:20 -0700186 // How long a partial wake lock must be held until we consider it a long wake lock.
187 static final long MIN_LONG_WAKE_CHECK_INTERVAL = 60*1000;
188
Jason Monk27bbb2d2015-03-31 16:46:39 -0400189 // Power features defined in hardware/libhardware/include/hardware/power.h.
190 private static final int POWER_FEATURE_DOUBLE_TAP_TO_WAKE = 1;
191
192 // Default setting for double tap to wake.
193 private static final int DEFAULT_DOUBLE_TAP_TO_WAKE = 0;
194
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800195 // System property indicating that the screen should remain off until an explicit user action
196 private static final String SYSTEM_PROPERTY_QUIESCENT = "ro.boot.quiescent";
197
Christine Franks732c0432017-06-23 18:12:46 -0700198 // System Property indicating that retail demo mode is currently enabled.
199 private static final String SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED = "sys.retaildemo.enabled";
200
Wei Wang72b77cc2019-02-07 14:10:23 -0800201 // Possible reasons for shutting down or reboot for use in REBOOT_PROPERTY(sys.boot.reason)
202 // which is set by bootstat
Salvador Martineza6f7b252017-04-10 10:46:15 -0700203 private static final String REASON_SHUTDOWN = "shutdown";
204 private static final String REASON_REBOOT = "reboot";
Mark Salyzyne65c0c62017-08-15 07:53:47 -0700205 private static final String REASON_USERREQUESTED = "shutdown,userrequested";
206 private static final String REASON_THERMAL_SHUTDOWN = "shutdown,thermal";
Sudheer Shanka292637f2017-09-25 10:36:23 -0700207 private static final String REASON_LOW_BATTERY = "shutdown,battery";
208 private static final String REASON_BATTERY_THERMAL_STATE = "shutdown,thermal,battery";
Salvador Martineza6f7b252017-04-10 10:46:15 -0700209
Makoto Onuki5ab5e1d2017-04-14 17:04:26 -0700210 private static final String TRACE_SCREEN_ON = "Screen turning on";
211
212 /** If turning screen on takes more than this long, we show a warning on logcat. */
213 private static final int SCREEN_ON_LATENCY_WARNING_MS = 200;
214
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700215 /** Constants for {@link #shutdownOrRebootInternal} */
216 @Retention(RetentionPolicy.SOURCE)
217 @IntDef({HALT_MODE_SHUTDOWN, HALT_MODE_REBOOT, HALT_MODE_REBOOT_SAFE_MODE})
218 public @interface HaltMode {}
219 private static final int HALT_MODE_SHUTDOWN = 0;
220 private static final int HALT_MODE_REBOOT = 1;
221 private static final int HALT_MODE_REBOOT_SAFE_MODE = 2;
222
Mark Salyzyn74ce8b32018-06-05 09:06:54 -0700223 // property for last reboot reason
224 private static final String REBOOT_PROPERTY = "sys.boot.reason";
Salvador Martineza6f7b252017-04-10 10:46:15 -0700225
Jeff Brownb880d882014-02-10 19:47:07 -0800226 private final Context mContext;
Jeff Brown2c43c332014-06-12 22:38:59 -0700227 private final ServiceThread mHandlerThread;
228 private final PowerManagerHandler mHandler;
Adrian Roos56021892017-02-27 20:25:09 +0100229 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
jackqdyulei455e90a2017-02-09 15:29:16 -0800230 private final BatterySaverPolicy mBatterySaverPolicy;
Makoto Onuki66a78122017-11-14 15:03:21 -0800231 private final BatterySaverController mBatterySaverController;
Makoto Onukia3cd7b92018-03-19 14:47:05 -0700232 private final BatterySaverStateMachine mBatterySaverStateMachine;
Makoto Onukibd7a6252018-05-10 13:41:39 -0700233 private final BatterySavingStats mBatterySavingStats;
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800234 private final AttentionDetector mAttentionDetector;
Santos Cordon64a6e612018-08-22 19:27:04 +0100235 private final BinderService mBinderService;
236 private final LocalService mLocalService;
237 private final NativeWrapper mNativeWrapper;
238 private final Injector mInjector;
Jeff Brown2c43c332014-06-12 22:38:59 -0700239
Adam Lesinski182f73f2013-12-05 16:48:06 -0800240 private LightsManager mLightsManager;
Jeff Brown21392762014-06-13 19:00:36 -0700241 private BatteryManagerInternal mBatteryManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800242 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brown96307042012-07-27 15:51:34 -0700243 private IBatteryStats mBatteryStats;
Dianne Hackborn713df152013-05-17 11:27:57 -0700244 private IAppOpsService mAppOps;
Jeff Brown96307042012-07-27 15:51:34 -0700245 private WindowManagerPolicy mPolicy;
246 private Notifier mNotifier;
Jeff Brown3b971592013-01-09 18:46:37 -0800247 private WirelessChargerDetector mWirelessChargerDetector;
Jeff Brown96307042012-07-27 15:51:34 -0700248 private SettingsObserver mSettingsObserver;
Jeff Brown567f7ca2014-01-30 23:38:03 -0800249 private DreamManagerInternal mDreamManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 private Light mAttentionLight;
Joe Onorato609695d2010-10-14 14:57:49 -0700251
Jeff Sharkey5f3e9342017-03-13 14:53:11 -0600252 private final Object mLock = LockGuard.installNewLock(LockGuard.INDEX_POWER);
Jeff Brown96307042012-07-27 15:51:34 -0700253
254 // A bitfield that indicates what parts of the power state have
255 // changed and need to be recalculated.
256 private int mDirty;
257
258 // Indicates whether the device is awake or asleep or somewhere in between.
259 // This is distinct from the screen power state, which is managed separately.
260 private int mWakefulness;
Jeff Brownfbe96702014-11-19 18:30:58 -0800261 private boolean mWakefulnessChanging;
Jeff Brown96307042012-07-27 15:51:34 -0700262
Jeff Brown26875502014-01-30 21:47:47 -0800263 // True if the sandman has just been summoned for the first time since entering the
264 // dreaming or dozing state. Indicates whether a new dream should begin.
265 private boolean mSandmanSummoned;
266
Jeff Brown96307042012-07-27 15:51:34 -0700267 // True if MSG_SANDMAN has been scheduled.
268 private boolean mSandmanScheduled;
269
270 // Table of all suspend blockers.
271 // There should only be a few of these.
272 private final ArrayList<SuspendBlocker> mSuspendBlockers = new ArrayList<SuspendBlocker>();
273
274 // Table of all wake locks acquired by applications.
275 private final ArrayList<WakeLock> mWakeLocks = new ArrayList<WakeLock>();
276
277 // A bitfield that summarizes the state of all active wakelocks.
278 private int mWakeLockSummary;
279
Dianne Hackbornd0db6f02016-07-18 14:14:20 -0700280 // Have we scheduled a message to check for long wake locks? This is when we will check.
281 private long mNotifyLongScheduled;
282
283 // Last time we checked for long wake locks.
284 private long mNotifyLongDispatched;
285
286 // The time we decided to do next long check.
287 private long mNotifyLongNextCheck;
288
Jeff Brown96307042012-07-27 15:51:34 -0700289 // If true, instructs the display controller to wait for the proximity sensor to
290 // go negative before turning the screen on.
291 private boolean mRequestWaitForNegativeProximity;
292
293 // Timestamp of the last time the device was awoken or put to sleep.
294 private long mLastWakeTime;
295 private long mLastSleepTime;
296
Calin Tatarua3805722018-08-09 16:41:28 +0200297 // Last reason the device went to sleep.
Michael Wrighte3001042019-02-05 00:13:14 +0000298 private @WakeReason int mLastWakeReason;
Calin Tatarua3805722018-08-09 16:41:28 +0200299 private int mLastSleepReason;
300
Jeff Brown96307042012-07-27 15:51:34 -0700301 // Timestamp of the last call to user activity.
302 private long mLastUserActivityTime;
303 private long mLastUserActivityTimeNoChangeLights;
304
Jeff Brown0a571122014-08-21 21:50:43 -0700305 // Timestamp of last interactive power hint.
306 private long mLastInteractivePowerHintTime;
307
Jeff Browne333e672014-10-28 13:48:55 -0700308 // Timestamp of the last screen brightness boost.
309 private long mLastScreenBrightnessBoostTime;
310 private boolean mScreenBrightnessBoostInProgress;
311
Jeff Brown96307042012-07-27 15:51:34 -0700312 // A bitfield that summarizes the effect of the user activity timer.
Jeff Brown96307042012-07-27 15:51:34 -0700313 private int mUserActivitySummary;
314
315 // The desired display power state. The actual state may lag behind the
316 // requested because it is updated asynchronously by the display power controller.
317 private final DisplayPowerRequest mDisplayPowerRequest = new DisplayPowerRequest();
318
Jeff Brown96307042012-07-27 15:51:34 -0700319 // True if the display power state has been fully applied, which means the display
320 // is actually on or actually off or whatever was requested.
321 private boolean mDisplayReady;
322
Jeff Brown27f7a862012-12-12 15:43:31 -0800323 // The suspend blocker used to keep the CPU alive when an application has acquired
324 // a wake lock.
325 private final SuspendBlocker mWakeLockSuspendBlocker;
326
327 // True if the wake lock suspend blocker has been acquired.
Jeff Brown96307042012-07-27 15:51:34 -0700328 private boolean mHoldingWakeLockSuspendBlocker;
329
Jeff Brown27f7a862012-12-12 15:43:31 -0800330 // The suspend blocker used to keep the CPU alive when the display is on, the
331 // display is getting ready or there is user activity (in which case the display
332 // must be on).
333 private final SuspendBlocker mDisplaySuspendBlocker;
334
335 // True if the display suspend blocker has been acquired.
336 private boolean mHoldingDisplaySuspendBlocker;
Jeff Brown96307042012-07-27 15:51:34 -0700337
338 // True if systemReady() has been called.
339 private boolean mSystemReady;
340
341 // True if boot completed occurred. We keep the screen on until this happens.
342 private boolean mBootCompleted;
343
Jeff Brown26875502014-01-30 21:47:47 -0800344 // True if auto-suspend mode is enabled.
345 // Refer to autosuspend.h.
Jeff Brown037c33e2014-04-09 00:31:55 -0700346 private boolean mHalAutoSuspendModeEnabled;
Jeff Brown26875502014-01-30 21:47:47 -0800347
348 // True if interactive mode is enabled.
349 // Refer to power.h.
Jeff Brown037c33e2014-04-09 00:31:55 -0700350 private boolean mHalInteractiveModeEnabled;
Jeff Brown26875502014-01-30 21:47:47 -0800351
Jeff Brown96307042012-07-27 15:51:34 -0700352 // True if the device is plugged into a power source.
353 private boolean mIsPowered;
354
Jeff Brownf3fb8952012-10-02 20:57:05 -0700355 // The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS.
356 private int mPlugType;
357
Jeff Brown016ff142012-10-15 16:47:22 -0700358 // The current battery level percentage.
359 private int mBatteryLevel;
360
361 // The battery level percentage at the time the dream started.
362 // This is used to terminate a dream and go to sleep if the battery is
363 // draining faster than it is charging and the user activity timeout has expired.
364 private int mBatteryLevelWhenDreamStarted;
365
Jeff Brownec6aa592012-10-17 20:30:25 -0700366 // The current dock state.
367 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
368
Jeff Brown26875502014-01-30 21:47:47 -0800369 // True to decouple auto-suspend mode from the display state.
Jeff Brown037c33e2014-04-09 00:31:55 -0700370 private boolean mDecoupleHalAutoSuspendModeFromDisplayConfig;
Jeff Brown26875502014-01-30 21:47:47 -0800371
372 // True to decouple interactive mode from the display state.
Jeff Brown037c33e2014-04-09 00:31:55 -0700373 private boolean mDecoupleHalInteractiveModeFromDisplayConfig;
Jeff Brown26875502014-01-30 21:47:47 -0800374
Jeff Brown96307042012-07-27 15:51:34 -0700375 // True if the device should wake up when plugged or unplugged.
376 private boolean mWakeUpWhenPluggedOrUnpluggedConfig;
377
Bryce Lee584a4452014-10-21 15:55:55 -0700378 // True if the device should wake up when plugged or unplugged in theater mode.
379 private boolean mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig;
380
Jeff Brownec083212013-09-11 20:45:25 -0700381 // True if the device should suspend when the screen is off due to proximity.
382 private boolean mSuspendWhenScreenOffDueToProximityConfig;
383
Jeff Brown96307042012-07-27 15:51:34 -0700384 // True if dreams are supported on this device.
385 private boolean mDreamsSupportedConfig;
386
John Spurlocked108f32012-10-18 16:49:24 -0400387 // Default value for dreams enabled
388 private boolean mDreamsEnabledByDefaultConfig;
389
390 // Default value for dreams activate-on-sleep
391 private boolean mDreamsActivatedOnSleepByDefaultConfig;
392
393 // Default value for dreams activate-on-dock
394 private boolean mDreamsActivatedOnDockByDefaultConfig;
395
Jeff Brown26875502014-01-30 21:47:47 -0800396 // True if dreams can run while not plugged in.
397 private boolean mDreamsEnabledOnBatteryConfig;
398
399 // Minimum battery level to allow dreaming when powered.
400 // Use -1 to disable this safety feature.
401 private int mDreamsBatteryLevelMinimumWhenPoweredConfig;
402
403 // Minimum battery level to allow dreaming when not powered.
404 // Use -1 to disable this safety feature.
405 private int mDreamsBatteryLevelMinimumWhenNotPoweredConfig;
406
407 // If the battery level drops by this percentage and the user activity timeout
408 // has expired, then assume the device is receiving insufficient current to charge
409 // effectively and terminate the dream. Use -1 to disable this safety feature.
410 private int mDreamsBatteryLevelDrainCutoffConfig;
411
Jeff Brown96307042012-07-27 15:51:34 -0700412 // True if dreams are enabled by the user.
413 private boolean mDreamsEnabledSetting;
414
John Spurlock1a868b72012-08-22 09:56:51 -0400415 // True if dreams should be activated on sleep.
416 private boolean mDreamsActivateOnSleepSetting;
417
Jeff Brownec6aa592012-10-17 20:30:25 -0700418 // True if dreams should be activated on dock.
419 private boolean mDreamsActivateOnDockSetting;
420
Jeff Brown2175e9c2014-09-12 16:11:07 -0700421 // True if doze should not be started until after the screen off transition.
Lucas Dupin16cfe452018-02-08 13:14:50 -0800422 private boolean mDozeAfterScreenOff;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700423
Jeff Brown27736f52014-05-20 17:17:10 -0700424 // The minimum screen off timeout, in milliseconds.
Pavel Grafov28939982017-10-03 15:11:52 +0100425 private long mMinimumScreenOffTimeoutConfig;
Jeff Brown27736f52014-05-20 17:17:10 -0700426
427 // The screen dim duration, in milliseconds.
428 // This is subtracted from the end of the screen off timeout so the
429 // minimum screen off timeout should be longer than this.
Pavel Grafov28939982017-10-03 15:11:52 +0100430 private long mMaximumScreenDimDurationConfig;
Jeff Brown27736f52014-05-20 17:17:10 -0700431
432 // The maximum screen dim time expressed as a ratio relative to the screen
433 // off timeout. If the screen off timeout is very short then we want the
434 // dim timeout to also be quite short so that most of the time is spent on.
435 // Otherwise the user won't get much screen on time before dimming occurs.
436 private float mMaximumScreenDimRatioConfig;
437
Jason Monk27bbb2d2015-03-31 16:46:39 -0400438 // Whether device supports double tap to wake.
439 private boolean mSupportsDoubleTapWakeConfig;
440
Jeff Brown96307042012-07-27 15:51:34 -0700441 // The screen off timeout setting value in milliseconds.
Pavel Grafov28939982017-10-03 15:11:52 +0100442 private long mScreenOffTimeoutSetting;
Jeff Brown96307042012-07-27 15:51:34 -0700443
Jeff Brown05af6ad2014-09-30 20:54:30 -0700444 // The sleep timeout setting value in milliseconds.
Pavel Grafov28939982017-10-03 15:11:52 +0100445 private long mSleepTimeoutSetting;
Jeff Brown05af6ad2014-09-30 20:54:30 -0700446
Jeff Brown96307042012-07-27 15:51:34 -0700447 // The maximum allowable screen off timeout according to the device
448 // administration policy. Overrides other settings.
Pavel Grafov28939982017-10-03 15:11:52 +0100449 private long mMaximumScreenOffTimeoutFromDeviceAdmin = Long.MAX_VALUE;
Jeff Brown96307042012-07-27 15:51:34 -0700450
451 // The stay on while plugged in setting.
452 // A bitfield of battery conditions under which to make the screen stay on.
453 private int mStayOnWhilePluggedInSetting;
454
455 // True if the device should stay on.
456 private boolean mStayOn;
457
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800458 // True if the lights should stay off until an explicit user action.
459 private static boolean sQuiescent;
460
Jeff Brown93cbbb22012-10-04 13:18:36 -0700461 // True if the proximity sensor reads a positive result.
462 private boolean mProximityPositive;
463
Jeff Brown96307042012-07-27 15:51:34 -0700464 // Screen brightness setting limits.
465 private int mScreenBrightnessSettingMinimum;
466 private int mScreenBrightnessSettingMaximum;
467 private int mScreenBrightnessSettingDefault;
468
469 // The screen brightness setting, from 0 to 255.
470 // Use -1 if no value has been set.
471 private int mScreenBrightnessSetting;
472
473 // The screen brightness mode.
474 // One of the Settings.System.SCREEN_BRIGHTNESS_MODE_* constants.
475 private int mScreenBrightnessModeSetting;
476
477 // The screen brightness setting override from the window manager
478 // to allow the current foreground activity to override the brightness.
479 // Use -1 to disable.
480 private int mScreenBrightnessOverrideFromWindowManager = -1;
481
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700482 // The window manager has determined the user to be inactive via other means.
483 // Set this to false to disable.
484 private boolean mUserInactiveOverrideFromWindowManager;
485
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -0700486 // The next possible user activity timeout after being explicitly told the user is inactive.
487 // Set to -1 when not told the user is inactive since the last period spent dozing or asleep.
488 private long mOverriddenTimeout = -1;
489
Jeff Brown1e3b98d2012-09-30 18:58:59 -0700490 // The user activity timeout override from the window manager
491 // to allow the current foreground activity to override the user activity timeout.
492 // Use -1 to disable.
493 private long mUserActivityTimeoutOverrideFromWindowManager = -1;
494
Jeff Brown970d4132014-07-19 11:33:47 -0700495 // The screen state to use while dozing.
496 private int mDozeScreenStateOverrideFromDreamManager = Display.STATE_UNKNOWN;
497
498 // The screen brightness to use while dozing.
499 private int mDozeScreenBrightnessOverrideFromDreamManager = PowerManager.BRIGHTNESS_DEFAULT;
500
Ivan Podogov56bc6d02018-02-26 16:04:24 +0000501 // Keep display state when dozing.
502 private boolean mDrawWakeLockOverrideFromSidekick;
503
Jeff Brown9ba8d782012-10-01 16:38:23 -0700504 // Time when we last logged a warning about calling userActivity() without permission.
505 private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE;
506
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700507 // True if the battery level is currently considered low.
508 private boolean mBatteryLevelLow;
509
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700510 // True if we are currently in device idle mode.
511 private boolean mDeviceIdleMode;
512
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700513 // True if we are currently in light device idle mode.
514 private boolean mLightDeviceIdleMode;
515
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700516 // Set of app ids that we will always respect the wake locks for.
517 int[] mDeviceIdleWhitelist = new int[0];
518
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700519 // Set of app ids that are temporarily allowed to acquire wakelocks due to high-pri message
520 int[] mDeviceIdleTempWhitelist = new int[0];
521
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -0700522 private final SparseArray<UidState> mUidState = new SparseArray<>();
523
524 // We are currently in the middle of a batch change of uids.
525 private boolean mUidsChanging;
526
527 // Some uids have actually changed while mUidsChanging was true.
528 private boolean mUidsChanged;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700529
Bryce Lee584a4452014-10-21 15:55:55 -0700530 // True if theater mode is enabled
531 private boolean mTheaterModeEnabled;
532
Adrian Roos56021892017-02-27 20:25:09 +0100533 // True if always on display is enabled
534 private boolean mAlwaysOnEnabled;
535
Jason Monk27bbb2d2015-03-31 16:46:39 -0400536 // True if double tap to wake is enabled
537 private boolean mDoubleTapWakeEnabled;
538
Santos Cordon3107d292016-09-20 15:50:35 -0700539 // True if we are currently in VR Mode.
540 private boolean mIsVrModeEnabled;
541
Santos Cordon12f92eb2019-02-01 21:28:47 +0000542 // True if we in the process of performing a forceSuspend
543 private boolean mForceSuspendActive;
544
Pavel Grafov28939982017-10-03 15:11:52 +0100545 private final class ForegroundProfileObserver extends SynchronousUserSwitchObserver {
546 @Override
547 public void onUserSwitching(int newUserId) throws RemoteException {}
548
549 @Override
550 public void onForegroundProfileSwitch(@UserIdInt int newProfileId) throws RemoteException {
551 final long now = SystemClock.uptimeMillis();
552 synchronized(mLock) {
553 mForegroundProfile = newProfileId;
554 maybeUpdateForegroundProfileLastActivityLocked(now);
555 }
556 }
557 }
558
559 // User id corresponding to activity the user is currently interacting with.
560 private @UserIdInt int mForegroundProfile;
561
562 // Per-profile state to track when a profile should be locked.
563 private final SparseArray<ProfilePowerState> mProfilePowerState = new SparseArray<>();
564
565 private static final class ProfilePowerState {
566 // Profile user id.
567 final @UserIdInt int mUserId;
568 // Maximum time to lock set by admin.
569 long mScreenOffTimeout;
570 // Like top-level mWakeLockSummary, but only for wake locks that affect current profile.
571 int mWakeLockSummary;
572 // Last user activity that happened in an app running in the profile.
573 long mLastUserActivityTime;
574 // Whether profile has been locked last time it timed out.
575 boolean mLockingNotified;
576
577 public ProfilePowerState(@UserIdInt int userId, long screenOffTimeout) {
578 mUserId = userId;
579 mScreenOffTimeout = screenOffTimeout;
580 // Not accurate but at least won't cause immediate locking of the profile.
581 mLastUserActivityTime = SystemClock.uptimeMillis();
582 }
583 }
584
Dianne Hackborn0ef403e2017-01-24 18:22:15 -0800585 /**
586 * All times are in milliseconds. These constants are kept synchronized with the system
587 * global Settings. Any access to this class or its fields should be done while
588 * holding the PowerManagerService.mLock lock.
589 */
590 private final class Constants extends ContentObserver {
591 // Key names stored in the settings value.
592 private static final String KEY_NO_CACHED_WAKE_LOCKS = "no_cached_wake_locks";
593
594 private static final boolean DEFAULT_NO_CACHED_WAKE_LOCKS = true;
595
596 // Prevent processes that are cached from holding wake locks?
597 public boolean NO_CACHED_WAKE_LOCKS = DEFAULT_NO_CACHED_WAKE_LOCKS;
598
599 private ContentResolver mResolver;
600 private final KeyValueListParser mParser = new KeyValueListParser(',');
601
602 public Constants(Handler handler) {
603 super(handler);
604 }
605
606 public void start(ContentResolver resolver) {
607 mResolver = resolver;
608 mResolver.registerContentObserver(Settings.Global.getUriFor(
609 Settings.Global.POWER_MANAGER_CONSTANTS), false, this);
610 updateConstants();
611 }
612
613 @Override
614 public void onChange(boolean selfChange, Uri uri) {
615 updateConstants();
616 }
617
618 private void updateConstants() {
619 synchronized (mLock) {
620 try {
621 mParser.setString(Settings.Global.getString(mResolver,
622 Settings.Global.POWER_MANAGER_CONSTANTS));
623 } catch (IllegalArgumentException e) {
624 // Failed to parse the settings string, log this and move on
625 // with defaults.
626 Slog.e(TAG, "Bad alarm manager settings", e);
627 }
628
629 NO_CACHED_WAKE_LOCKS = mParser.getBoolean(KEY_NO_CACHED_WAKE_LOCKS,
630 DEFAULT_NO_CACHED_WAKE_LOCKS);
631 }
632 }
633
634 void dump(PrintWriter pw) {
635 pw.println(" Settings " + Settings.Global.POWER_MANAGER_CONSTANTS + ":");
636
637 pw.print(" "); pw.print(KEY_NO_CACHED_WAKE_LOCKS); pw.print("=");
638 pw.println(NO_CACHED_WAKE_LOCKS);
639 }
Netta P958d0a52017-02-07 11:20:55 -0800640
641 void dumpProto(ProtoOutputStream proto) {
Kweku Adamse6b00c22017-10-23 16:46:45 -0700642 final long constantsToken = proto.start(PowerManagerServiceDumpProto.CONSTANTS);
643 proto.write(PowerManagerServiceDumpProto.ConstantsProto.IS_NO_CACHED_WAKE_LOCKS,
Netta P958d0a52017-02-07 11:20:55 -0800644 NO_CACHED_WAKE_LOCKS);
645 proto.end(constantsToken);
646 }
Dianne Hackborn0ef403e2017-01-24 18:22:15 -0800647 }
648
Santos Cordon64a6e612018-08-22 19:27:04 +0100649 /**
650 * Wrapper around the static-native methods of PowerManagerService.
651 *
652 * This class exists to allow us to mock static native methods in our tests. If mocking static
653 * methods becomes easier than this in the future, we can delete this class.
654 */
655 @VisibleForTesting
656 public static class NativeWrapper {
657 /** Wrapper for PowerManager.nativeInit */
658 public void nativeInit(PowerManagerService service) {
659 service.nativeInit();
660 }
661
662 /** Wrapper for PowerManager.nativeAcquireSuspectBlocker */
663 public void nativeAcquireSuspendBlocker(String name) {
664 PowerManagerService.nativeAcquireSuspendBlocker(name);
665 }
666
667 /** Wrapper for PowerManager.nativeReleaseSuspendBlocker */
668 public void nativeReleaseSuspendBlocker(String name) {
669 PowerManagerService.nativeReleaseSuspendBlocker(name);
670 }
671
672 /** Wrapper for PowerManager.nativeSetInteractive */
673 public void nativeSetInteractive(boolean enable) {
674 PowerManagerService.nativeSetInteractive(enable);
675 }
676
677 /** Wrapper for PowerManager.nativeSetAutoSuspend */
678 public void nativeSetAutoSuspend(boolean enable) {
679 PowerManagerService.nativeSetAutoSuspend(enable);
680 }
681
682 /** Wrapper for PowerManager.nativeSendPowerHint */
683 public void nativeSendPowerHint(int hintId, int data) {
684 PowerManagerService.nativeSendPowerHint(hintId, data);
685 }
686
687 /** Wrapper for PowerManager.nativeSetFeature */
688 public void nativeSetFeature(int featureId, int data) {
689 PowerManagerService.nativeSetFeature(featureId, data);
690 }
Santos Cordon12f92eb2019-02-01 21:28:47 +0000691
692 /** Wrapper for PowerManager.nativeForceSuspend */
693 public boolean nativeForceSuspend() {
694 return PowerManagerService.nativeForceSuspend();
695 }
Santos Cordon64a6e612018-08-22 19:27:04 +0100696 }
697
698 @VisibleForTesting
699 static class Injector {
700 Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats,
701 SuspendBlocker suspendBlocker, WindowManagerPolicy policy) {
702 return new Notifier(looper, context, batteryStats, suspendBlocker, policy);
703 }
704
705 SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) {
706 SuspendBlocker suspendBlocker = service.new SuspendBlockerImpl(name);
707 service.mSuspendBlockers.add(suspendBlocker);
708 return suspendBlocker;
709 }
710
711 BatterySaverPolicy createBatterySaverPolicy(
712 Object lock, Context context, BatterySavingStats batterySavingStats) {
713 return new BatterySaverPolicy(lock, context, batterySavingStats);
714 }
715
716 NativeWrapper createNativeWrapper() {
717 return new NativeWrapper();
718 }
Santos Cordon9b510a22018-08-24 16:42:54 +0100719
720 WirelessChargerDetector createWirelessChargerDetector(
721 SensorManager sensorManager, SuspendBlocker suspendBlocker, Handler handler) {
722 return new WirelessChargerDetector(sensorManager, suspendBlocker, handler);
723 }
724
725 AmbientDisplayConfiguration createAmbientDisplayConfiguration(Context context) {
726 return new AmbientDisplayConfiguration(context);
727 }
Santos Cordon64a6e612018-08-22 19:27:04 +0100728 }
729
Dianne Hackborn0ef403e2017-01-24 18:22:15 -0800730 final Constants mConstants;
731
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700732 private native void nativeInit();
Jeff Brown96307042012-07-27 15:51:34 -0700733 private static native void nativeAcquireSuspendBlocker(String name);
734 private static native void nativeReleaseSuspendBlocker(String name);
Jeff Brown9e316a12012-10-08 19:17:06 -0700735 private static native void nativeSetInteractive(boolean enable);
736 private static native void nativeSetAutoSuspend(boolean enable);
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -0700737 private static native void nativeSendPowerHint(int hintId, int data);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400738 private static native void nativeSetFeature(int featureId, int data);
Santos Cordon12f92eb2019-02-01 21:28:47 +0000739 private static native boolean nativeForceSuspend();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740
Jeff Brownb880d882014-02-10 19:47:07 -0800741 public PowerManagerService(Context context) {
Santos Cordon64a6e612018-08-22 19:27:04 +0100742 this(context, new Injector());
743 }
744
745 @VisibleForTesting
746 PowerManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800747 super(context);
Santos Cordon64a6e612018-08-22 19:27:04 +0100748
Jeff Brownb880d882014-02-10 19:47:07 -0800749 mContext = context;
Santos Cordon64a6e612018-08-22 19:27:04 +0100750 mBinderService = new BinderService();
751 mLocalService = new LocalService();
752 mNativeWrapper = injector.createNativeWrapper();
753 mInjector = injector;
754
Jeff Brown2c43c332014-06-12 22:38:59 -0700755 mHandlerThread = new ServiceThread(TAG,
756 Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/);
757 mHandlerThread.start();
758 mHandler = new PowerManagerHandler(mHandlerThread.getLooper());
Dianne Hackborn0ef403e2017-01-24 18:22:15 -0800759 mConstants = new Constants(mHandler);
Santos Cordon9b510a22018-08-24 16:42:54 +0100760 mAmbientDisplayConfiguration = mInjector.createAmbientDisplayConfiguration(context);
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800761 mAttentionDetector = new AttentionDetector(this::onUserAttention, mLock);
Makoto Onuki66a78122017-11-14 15:03:21 -0800762
Makoto Onukibd7a6252018-05-10 13:41:39 -0700763 mBatterySavingStats = new BatterySavingStats(mLock);
Santos Cordon64a6e612018-08-22 19:27:04 +0100764 mBatterySaverPolicy =
765 mInjector.createBatterySaverPolicy(mLock, mContext, mBatterySavingStats);
Makoto Onukibd7a6252018-05-10 13:41:39 -0700766 mBatterySaverController = new BatterySaverController(mLock, mContext,
Santos Cordon64a6e612018-08-22 19:27:04 +0100767 BackgroundThread.get().getLooper(), mBatterySaverPolicy,
768 mBatterySavingStats);
Makoto Onukibd7a6252018-05-10 13:41:39 -0700769 mBatterySaverStateMachine = new BatterySaverStateMachine(
770 mLock, mContext, mBatterySaverController);
Jeff Brown2c43c332014-06-12 22:38:59 -0700771
Jeff Brown96307042012-07-27 15:51:34 -0700772 synchronized (mLock) {
Santos Cordon64a6e612018-08-22 19:27:04 +0100773 mWakeLockSuspendBlocker =
774 mInjector.createSuspendBlocker(this, "PowerManagerService.WakeLocks");
775 mDisplaySuspendBlocker =
776 mInjector.createSuspendBlocker(this, "PowerManagerService.Display");
777 if (mDisplaySuspendBlocker != null) {
778 mDisplaySuspendBlocker.acquire();
779 mHoldingDisplaySuspendBlocker = true;
780 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700781 mHalAutoSuspendModeEnabled = false;
782 mHalInteractiveModeEnabled = true;
Jeff Brown27f7a862012-12-12 15:43:31 -0800783
Jeff Brown96307042012-07-27 15:51:34 -0700784 mWakefulness = WAKEFULNESS_AWAKE;
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -0800785 sQuiescent = SystemProperties.get(SYSTEM_PROPERTY_QUIESCENT, "0").equals("1");
786
Santos Cordon64a6e612018-08-22 19:27:04 +0100787 mNativeWrapper.nativeInit(this);
788 mNativeWrapper.nativeSetAutoSuspend(false);
789 mNativeWrapper.nativeSetInteractive(true);
790 mNativeWrapper.nativeSetFeature(POWER_FEATURE_DOUBLE_TAP_TO_WAKE, 0);
Jeff Brown037c33e2014-04-09 00:31:55 -0700791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793
Jeff Brown6f357d32014-01-15 20:40:55 -0800794 @Override
Jeff Brown6f357d32014-01-15 20:40:55 -0800795 public void onStart() {
Santos Cordon64a6e612018-08-22 19:27:04 +0100796 publishBinderService(Context.POWER_SERVICE, mBinderService);
797 publishLocalService(PowerManagerInternal.class, mLocalService);
Jeff Brown9e316a12012-10-08 19:17:06 -0700798
799 Watchdog.getInstance().addMonitor(this);
Jeff Brown6f357d32014-01-15 20:40:55 -0800800 Watchdog.getInstance().addThread(mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700801 }
Jim Miller92e66dd2012-02-21 18:57:12 -0800802
Jeff Brown6d2a9492014-08-07 19:06:49 -0700803 @Override
804 public void onBootPhase(int phase) {
Craig Mautner6e2f3952014-09-09 14:26:41 -0700805 synchronized (mLock) {
Jeff Sharkeyb5e89c62016-04-01 23:20:31 -0600806 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
807 incrementBootCount();
808
809 } else if (phase == PHASE_BOOT_COMPLETED) {
Craig Mautner6e2f3952014-09-09 14:26:41 -0700810 final long now = SystemClock.uptimeMillis();
811 mBootCompleted = true;
812 mDirty |= DIRTY_BOOT_COMPLETED;
Makoto Onukia3cd7b92018-03-19 14:47:05 -0700813
814 mBatterySaverStateMachine.onBootCompleted();
Craig Mautner6e2f3952014-09-09 14:26:41 -0700815 userActivityNoUpdateLocked(
816 now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
817 updatePowerStateLocked();
818 }
Jeff Brown6d2a9492014-08-07 19:06:49 -0700819 }
820 }
821
Jeff Brown21392762014-06-13 19:00:36 -0700822 public void systemReady(IAppOpsService appOps) {
Jeff Brown96307042012-07-27 15:51:34 -0700823 synchronized (mLock) {
824 mSystemReady = true;
Jeff Brown2c43c332014-06-12 22:38:59 -0700825 mAppOps = appOps;
826 mDreamManager = getLocalService(DreamManagerInternal.class);
827 mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class);
828 mPolicy = getLocalService(WindowManagerPolicy.class);
Jeff Brown21392762014-06-13 19:00:36 -0700829 mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800830 mAttentionDetector.systemReady(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831
Adam Lesinski182f73f2013-12-05 16:48:06 -0800832 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Jeff Brown96307042012-07-27 15:51:34 -0700833 mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting();
834 mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting();
835 mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700837 SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper());
Jeff Brown3b971592013-01-09 18:46:37 -0800838
Jeff Brownc38c9be2012-10-04 13:16:19 -0700839 // The notifier runs on the system server's main looper so as not to interfere
840 // with the animations and other critical functions of the power manager.
Jeff Brown2c43c332014-06-12 22:38:59 -0700841 mBatteryStats = BatteryStatsService.getService();
Santos Cordon64a6e612018-08-22 19:27:04 +0100842 mNotifier = mInjector.createNotifier(Looper.getMainLooper(), mContext, mBatteryStats,
843 mInjector.createSuspendBlocker(this, "PowerManagerService.Broadcasts"),
844 mPolicy);
Jeff Brownc38c9be2012-10-04 13:16:19 -0700845
Santos Cordon9b510a22018-08-24 16:42:54 +0100846 mWirelessChargerDetector = mInjector.createWirelessChargerDetector(sensorManager,
Santos Cordon64a6e612018-08-22 19:27:04 +0100847 mInjector.createSuspendBlocker(
848 this, "PowerManagerService.WirelessChargerDetector"),
Jeff Browndbcc8a22013-10-01 16:16:44 -0700849 mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700850 mSettingsObserver = new SettingsObserver(mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700851
852 mLightsManager = getLocalService(LightsManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800853 mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400854
Jeff Brown131206b2014-04-08 17:27:14 -0700855 // Initialize display power management.
856 mDisplayManagerInternal.initPowerManagement(
857 mDisplayPowerCallbacks, mHandler, sensorManager);
858
Pavel Grafov28939982017-10-03 15:11:52 +0100859 try {
860 final ForegroundProfileObserver observer = new ForegroundProfileObserver();
861 ActivityManager.getService().registerUserSwitchObserver(observer, TAG);
862 } catch (RemoteException e) {
863 // Shouldn't happen since in-process.
864 }
Makoto Onuki66a78122017-11-14 15:03:21 -0800865
Jeff Brown96307042012-07-27 15:51:34 -0700866 // Go.
867 readConfigurationLocked();
868 updateSettingsLocked();
869 mDirty |= DIRTY_BATTERY_STATE;
870 updatePowerStateLocked();
871 }
Shibin George43f5de02016-07-06 02:12:10 +0530872
Jeff Sharkey6e544612017-02-20 11:02:26 -0700873 final ContentResolver resolver = mContext.getContentResolver();
874 mConstants.start(resolver);
Makoto Onuki66a78122017-11-14 15:03:21 -0800875
876 mBatterySaverController.systemReady();
Makoto Onukibd7a6252018-05-10 13:41:39 -0700877 mBatterySaverPolicy.systemReady();
Jeff Sharkey6e544612017-02-20 11:02:26 -0700878
879 // Register for settings changes.
880 resolver.registerContentObserver(Settings.Secure.getUriFor(
881 Settings.Secure.SCREENSAVER_ENABLED),
882 false, mSettingsObserver, UserHandle.USER_ALL);
883 resolver.registerContentObserver(Settings.Secure.getUriFor(
884 Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP),
885 false, mSettingsObserver, UserHandle.USER_ALL);
886 resolver.registerContentObserver(Settings.Secure.getUriFor(
887 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK),
888 false, mSettingsObserver, UserHandle.USER_ALL);
889 resolver.registerContentObserver(Settings.System.getUriFor(
890 Settings.System.SCREEN_OFF_TIMEOUT),
891 false, mSettingsObserver, UserHandle.USER_ALL);
892 resolver.registerContentObserver(Settings.Secure.getUriFor(
893 Settings.Secure.SLEEP_TIMEOUT),
894 false, mSettingsObserver, UserHandle.USER_ALL);
895 resolver.registerContentObserver(Settings.Global.getUriFor(
896 Settings.Global.STAY_ON_WHILE_PLUGGED_IN),
897 false, mSettingsObserver, UserHandle.USER_ALL);
898 resolver.registerContentObserver(Settings.System.getUriFor(
Jeff Sharkey6e544612017-02-20 11:02:26 -0700899 Settings.System.SCREEN_BRIGHTNESS_MODE),
900 false, mSettingsObserver, UserHandle.USER_ALL);
901 resolver.registerContentObserver(Settings.System.getUriFor(
902 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ),
903 false, mSettingsObserver, UserHandle.USER_ALL);
904 resolver.registerContentObserver(Settings.Global.getUriFor(
Jeff Sharkey6e544612017-02-20 11:02:26 -0700905 Settings.Global.THEATER_MODE_ON),
906 false, mSettingsObserver, UserHandle.USER_ALL);
907 resolver.registerContentObserver(Settings.Secure.getUriFor(
Adrian Roos56021892017-02-27 20:25:09 +0100908 Settings.Secure.DOZE_ALWAYS_ON),
909 false, mSettingsObserver, UserHandle.USER_ALL);
910 resolver.registerContentObserver(Settings.Secure.getUriFor(
Jeff Sharkey6e544612017-02-20 11:02:26 -0700911 Settings.Secure.DOUBLE_TAP_TO_WAKE),
912 false, mSettingsObserver, UserHandle.USER_ALL);
Christine Franks732c0432017-06-23 18:12:46 -0700913 resolver.registerContentObserver(Settings.Global.getUriFor(
914 Settings.Global.DEVICE_DEMO_MODE),
915 false, mSettingsObserver, UserHandle.USER_SYSTEM);
Santos Cordon64a6e612018-08-22 19:27:04 +0100916 IVrManager vrManager = IVrManager.Stub.asInterface(getBinderService(Context.VR_SERVICE));
Jeff Sharkey6e544612017-02-20 11:02:26 -0700917 if (vrManager != null) {
918 try {
919 vrManager.registerListener(mVrStateCallbacks);
920 } catch (RemoteException e) {
921 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
922 }
923 }
924
Shibin George43f5de02016-07-06 02:12:10 +0530925 // Register for broadcasts from other components of the system.
926 IntentFilter filter = new IntentFilter();
927 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
928 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
929 mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler);
930
931 filter = new IntentFilter();
932 filter.addAction(Intent.ACTION_DREAMING_STARTED);
933 filter.addAction(Intent.ACTION_DREAMING_STOPPED);
934 mContext.registerReceiver(new DreamReceiver(), filter, null, mHandler);
935
936 filter = new IntentFilter();
937 filter.addAction(Intent.ACTION_USER_SWITCHED);
938 mContext.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
939
940 filter = new IntentFilter();
941 filter.addAction(Intent.ACTION_DOCK_EVENT);
942 mContext.registerReceiver(new DockReceiver(), filter, null, mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700943 }
944
Santos Cordon9b510a22018-08-24 16:42:54 +0100945 @VisibleForTesting
946 void readConfigurationLocked() {
Jeff Brown96307042012-07-27 15:51:34 -0700947 final Resources resources = mContext.getResources();
948
Jeff Brown037c33e2014-04-09 00:31:55 -0700949 mDecoupleHalAutoSuspendModeFromDisplayConfig = resources.getBoolean(
Jeff Brown26875502014-01-30 21:47:47 -0800950 com.android.internal.R.bool.config_powerDecoupleAutoSuspendModeFromDisplay);
Jeff Brown037c33e2014-04-09 00:31:55 -0700951 mDecoupleHalInteractiveModeFromDisplayConfig = resources.getBoolean(
Jeff Brown26875502014-01-30 21:47:47 -0800952 com.android.internal.R.bool.config_powerDecoupleInteractiveModeFromDisplay);
Jeff Brown96307042012-07-27 15:51:34 -0700953 mWakeUpWhenPluggedOrUnpluggedConfig = resources.getBoolean(
Joe Onorato6d747652010-10-11 15:15:31 -0700954 com.android.internal.R.bool.config_unplugTurnsOnScreen);
Bryce Lee584a4452014-10-21 15:55:55 -0700955 mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig = resources.getBoolean(
956 com.android.internal.R.bool.config_allowTheaterModeWakeFromUnplug);
Jeff Brownec083212013-09-11 20:45:25 -0700957 mSuspendWhenScreenOffDueToProximityConfig = resources.getBoolean(
958 com.android.internal.R.bool.config_suspendWhenScreenOffDueToProximity);
Jeff Brown96307042012-07-27 15:51:34 -0700959 mDreamsSupportedConfig = resources.getBoolean(
John Spurlocked108f32012-10-18 16:49:24 -0400960 com.android.internal.R.bool.config_dreamsSupported);
961 mDreamsEnabledByDefaultConfig = resources.getBoolean(
962 com.android.internal.R.bool.config_dreamsEnabledByDefault);
963 mDreamsActivatedOnSleepByDefaultConfig = resources.getBoolean(
964 com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault);
965 mDreamsActivatedOnDockByDefaultConfig = resources.getBoolean(
966 com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);
Jeff Brown26875502014-01-30 21:47:47 -0800967 mDreamsEnabledOnBatteryConfig = resources.getBoolean(
968 com.android.internal.R.bool.config_dreamsEnabledOnBattery);
969 mDreamsBatteryLevelMinimumWhenPoweredConfig = resources.getInteger(
970 com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenPowered);
971 mDreamsBatteryLevelMinimumWhenNotPoweredConfig = resources.getInteger(
972 com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenNotPowered);
973 mDreamsBatteryLevelDrainCutoffConfig = resources.getInteger(
974 com.android.internal.R.integer.config_dreamsBatteryLevelDrainCutoff);
Lucas Dupin16cfe452018-02-08 13:14:50 -0800975 mDozeAfterScreenOff = resources.getBoolean(
Lucas Dupin5354cc02018-03-20 16:09:34 -0700976 com.android.internal.R.bool.config_dozeAfterScreenOffByDefault);
Jeff Brown27736f52014-05-20 17:17:10 -0700977 mMinimumScreenOffTimeoutConfig = resources.getInteger(
978 com.android.internal.R.integer.config_minimumScreenOffTimeout);
979 mMaximumScreenDimDurationConfig = resources.getInteger(
980 com.android.internal.R.integer.config_maximumScreenDimDuration);
981 mMaximumScreenDimRatioConfig = resources.getFraction(
982 com.android.internal.R.fraction.config_maximumScreenDimRatio, 1, 1);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400983 mSupportsDoubleTapWakeConfig = resources.getBoolean(
984 com.android.internal.R.bool.config_supportDoubleTapWake);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986
Jeff Brown96307042012-07-27 15:51:34 -0700987 private void updateSettingsLocked() {
988 final ContentResolver resolver = mContext.getContentResolver();
Jeff Brown7304c342012-05-11 18:42:42 -0700989
Jeff Brownd4935962012-09-25 13:27:20 -0700990 mDreamsEnabledSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400991 Settings.Secure.SCREENSAVER_ENABLED,
992 mDreamsEnabledByDefaultConfig ? 1 : 0,
Jeff Brownd4935962012-09-25 13:27:20 -0700993 UserHandle.USER_CURRENT) != 0);
994 mDreamsActivateOnSleepSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400995 Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP,
996 mDreamsActivatedOnSleepByDefaultConfig ? 1 : 0,
Jeff Brownd4935962012-09-25 13:27:20 -0700997 UserHandle.USER_CURRENT) != 0);
Jeff Brownec6aa592012-10-17 20:30:25 -0700998 mDreamsActivateOnDockSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400999 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
1000 mDreamsActivatedOnDockByDefaultConfig ? 1 : 0,
Jeff Brownec6aa592012-10-17 20:30:25 -07001001 UserHandle.USER_CURRENT) != 0);
Jeff Brownd4935962012-09-25 13:27:20 -07001002 mScreenOffTimeoutSetting = Settings.System.getIntForUser(resolver,
1003 Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT,
1004 UserHandle.USER_CURRENT);
Jeff Brown05af6ad2014-09-30 20:54:30 -07001005 mSleepTimeoutSetting = Settings.Secure.getIntForUser(resolver,
1006 Settings.Secure.SLEEP_TIMEOUT, DEFAULT_SLEEP_TIMEOUT,
1007 UserHandle.USER_CURRENT);
Christopher Tatead735322012-09-07 14:19:43 -07001008 mStayOnWhilePluggedInSetting = Settings.Global.getInt(resolver,
Jeff Brownd4935962012-09-25 13:27:20 -07001009 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_AC);
Bryce Lee584a4452014-10-21 15:55:55 -07001010 mTheaterModeEnabled = Settings.Global.getInt(mContext.getContentResolver(),
1011 Settings.Global.THEATER_MODE_ON, 0) == 1;
Adrian Roos56021892017-02-27 20:25:09 +01001012 mAlwaysOnEnabled = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
Jeff Brown7304c342012-05-11 18:42:42 -07001013
Jason Monk27bbb2d2015-03-31 16:46:39 -04001014 if (mSupportsDoubleTapWakeConfig) {
1015 boolean doubleTapWakeEnabled = Settings.Secure.getIntForUser(resolver,
1016 Settings.Secure.DOUBLE_TAP_TO_WAKE, DEFAULT_DOUBLE_TAP_TO_WAKE,
1017 UserHandle.USER_CURRENT) != 0;
1018 if (doubleTapWakeEnabled != mDoubleTapWakeEnabled) {
1019 mDoubleTapWakeEnabled = doubleTapWakeEnabled;
Santos Cordon64a6e612018-08-22 19:27:04 +01001020 mNativeWrapper.nativeSetFeature(
1021 POWER_FEATURE_DOUBLE_TAP_TO_WAKE, mDoubleTapWakeEnabled ? 1 : 0);
Jason Monk27bbb2d2015-03-31 16:46:39 -04001022 }
1023 }
1024
Christine Franks732c0432017-06-23 18:12:46 -07001025 final String retailDemoValue = UserManager.isDeviceInDemoMode(mContext) ? "1" : "0";
1026 if (!retailDemoValue.equals(SystemProperties.get(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED))) {
1027 SystemProperties.set(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED, retailDemoValue);
1028 }
1029
Jeff Brownd4935962012-09-25 13:27:20 -07001030 mScreenBrightnessModeSetting = Settings.System.getIntForUser(resolver,
Jeff Brown96307042012-07-27 15:51:34 -07001031 Settings.System.SCREEN_BRIGHTNESS_MODE,
Jeff Brownd4935962012-09-25 13:27:20 -07001032 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);
Jeff Brown96307042012-07-27 15:51:34 -07001033
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001034 mDirty |= DIRTY_SETTINGS;
1035 }
1036
Jeff Brown96307042012-07-27 15:51:34 -07001037 private void handleSettingsChangedLocked() {
1038 updateSettingsLocked();
1039 updatePowerStateLocked();
1040 }
1041
Dianne Hackborn713df152013-05-17 11:27:57 -07001042 private void acquireWakeLockInternal(IBinder lock, int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001043 WorkSource ws, String historyTag, int uid, int pid) {
Jeff Brown96307042012-07-27 15:51:34 -07001044 synchronized (mLock) {
1045 if (DEBUG_SPEW) {
1046 Slog.d(TAG, "acquireWakeLockInternal: lock=" + Objects.hashCode(lock)
1047 + ", flags=0x" + Integer.toHexString(flags)
1048 + ", tag=\"" + tag + "\", ws=" + ws + ", uid=" + uid + ", pid=" + pid);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050
Jeff Brown96307042012-07-27 15:51:34 -07001051 WakeLock wakeLock;
1052 int index = findWakeLockIndexLocked(lock);
Dianne Hackborn0d192a92014-07-15 16:39:47 -07001053 boolean notifyAcquire;
Jeff Brown96307042012-07-27 15:51:34 -07001054 if (index >= 0) {
1055 wakeLock = mWakeLocks.get(index);
1056 if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid)) {
1057 // Update existing wake lock. This shouldn't happen but is harmless.
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001058 notifyWakeLockChangingLocked(wakeLock, flags, tag, packageName,
1059 uid, pid, ws, historyTag);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001060 wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
Dianne Hackborn0d192a92014-07-15 16:39:47 -07001062 notifyAcquire = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 } else {
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07001064 UidState state = mUidState.get(uid);
1065 if (state == null) {
1066 state = new UidState(uid);
1067 state.mProcState = ActivityManager.PROCESS_STATE_NONEXISTENT;
1068 mUidState.put(uid, state);
1069 }
1070 state.mNumWakeLocks++;
1071 wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid,
1072 state);
Jeff Brown96307042012-07-27 15:51:34 -07001073 try {
1074 lock.linkToDeath(wakeLock, 0);
1075 } catch (RemoteException ex) {
1076 throw new IllegalArgumentException("Wake lock is already dead.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
Jeff Brown96307042012-07-27 15:51:34 -07001078 mWakeLocks.add(wakeLock);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07001079 setWakeLockDisabledStateLocked(wakeLock);
Dianne Hackborn0d192a92014-07-15 16:39:47 -07001080 notifyAcquire = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082
Jeff Brownc12035c2014-08-13 18:52:25 -07001083 applyWakeLockFlagsOnAcquireLocked(wakeLock, uid);
Jeff Brown96307042012-07-27 15:51:34 -07001084 mDirty |= DIRTY_WAKE_LOCKS;
1085 updatePowerStateLocked();
Dianne Hackborn0d192a92014-07-15 16:39:47 -07001086 if (notifyAcquire) {
1087 // This needs to be done last so we are sure we have acquired the
1088 // kernel wake lock. Otherwise we have a race where the system may
1089 // go to sleep between the time we start the accounting in battery
1090 // stats and when we actually get around to telling the kernel to
1091 // stay awake.
1092 notifyWakeLockAcquiredLocked(wakeLock);
1093 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001094 }
1095 }
1096
Jeff Brownec083212013-09-11 20:45:25 -07001097 @SuppressWarnings("deprecation")
Craig Mautner6edb6db2012-11-20 18:21:12 -08001098 private static boolean isScreenLock(final WakeLock wakeLock) {
1099 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
1100 case PowerManager.FULL_WAKE_LOCK:
1101 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1102 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1103 return true;
1104 }
1105 return false;
1106 }
1107
Artem Iglikovc474bfd02018-09-25 11:56:09 -07001108 private static WorkChain getFirstNonEmptyWorkChain(WorkSource workSource) {
1109 if (workSource.getWorkChains() == null) {
1110 return null;
1111 }
1112
1113 for (WorkChain workChain: workSource.getWorkChains()) {
1114 if (workChain.getSize() > 0) {
1115 return workChain;
1116 }
1117 }
1118
1119 return null;
1120 }
1121
Jeff Brownc12035c2014-08-13 18:52:25 -07001122 private void applyWakeLockFlagsOnAcquireLocked(WakeLock wakeLock, int uid) {
Jeff Brown6eade792013-09-10 18:45:25 -07001123 if ((wakeLock.mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0
1124 && isScreenLock(wakeLock)) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001125 String opPackageName;
1126 int opUid;
Artem Iglikovc474bfd02018-09-25 11:56:09 -07001127 if (wakeLock.mWorkSource != null && !wakeLock.mWorkSource.isEmpty()) {
1128 WorkSource workSource = wakeLock.mWorkSource;
1129 WorkChain workChain = getFirstNonEmptyWorkChain(workSource);
1130 if (workChain != null) {
1131 opPackageName = workChain.getAttributionTag();
1132 opUid = workChain.getAttributionUid();
1133 } else {
1134 opPackageName = workSource.getName(0) != null
1135 ? workSource.getName(0) : wakeLock.mPackageName;
1136 opUid = workSource.get(0);
1137 }
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001138 } else {
1139 opPackageName = wakeLock.mPackageName;
Artem Iglikovc474bfd02018-09-25 11:56:09 -07001140 opUid = wakeLock.mOwnerUid;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001141 }
Michael Wrighte3001042019-02-05 00:13:14 +00001142 wakeUpNoUpdateLocked(SystemClock.uptimeMillis(),
1143 PowerManager.WAKE_REASON_APPLICATION, wakeLock.mTag,
1144 opUid, opPackageName, opUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
1147
Jeff Brown96307042012-07-27 15:51:34 -07001148 private void releaseWakeLockInternal(IBinder lock, int flags) {
1149 synchronized (mLock) {
Jeff Brown96307042012-07-27 15:51:34 -07001150 int index = findWakeLockIndexLocked(lock);
1151 if (index < 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -08001152 if (DEBUG_SPEW) {
1153 Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
1154 + " [not found], flags=0x" + Integer.toHexString(flags));
1155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 return;
1157 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001158
Jeff Brown96307042012-07-27 15:51:34 -07001159 WakeLock wakeLock = mWakeLocks.get(index);
Jeff Brown27f7a862012-12-12 15:43:31 -08001160 if (DEBUG_SPEW) {
1161 Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
1162 + " [" + wakeLock.mTag + "], flags=0x" + Integer.toHexString(flags));
1163 }
1164
Michael Wright1208e272014-09-08 19:57:50 -07001165 if ((flags & PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001166 mRequestWaitForNegativeProximity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168
Jeff Brown3edf5272014-08-14 19:25:14 -07001169 wakeLock.mLock.unlinkToDeath(wakeLock, 0);
1170 removeWakeLockLocked(wakeLock, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001173
Jeff Brown96307042012-07-27 15:51:34 -07001174 private void handleWakeLockDeath(WakeLock wakeLock) {
1175 synchronized (mLock) {
1176 if (DEBUG_SPEW) {
Jeff Brown27f7a862012-12-12 15:43:31 -08001177 Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock)
1178 + " [" + wakeLock.mTag + "]");
Jeff Brown96307042012-07-27 15:51:34 -07001179 }
1180
1181 int index = mWakeLocks.indexOf(wakeLock);
1182 if (index < 0) {
1183 return;
1184 }
1185
Jeff Brown3edf5272014-08-14 19:25:14 -07001186 removeWakeLockLocked(wakeLock, index);
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001187 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189
Jeff Brown3edf5272014-08-14 19:25:14 -07001190 private void removeWakeLockLocked(WakeLock wakeLock, int index) {
1191 mWakeLocks.remove(index);
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07001192 UidState state = wakeLock.mUidState;
1193 state.mNumWakeLocks--;
1194 if (state.mNumWakeLocks <= 0 &&
1195 state.mProcState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
1196 mUidState.remove(state.mUid);
1197 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001198 notifyWakeLockReleasedLocked(wakeLock);
1199
1200 applyWakeLockFlagsOnReleaseLocked(wakeLock);
1201 mDirty |= DIRTY_WAKE_LOCKS;
1202 updatePowerStateLocked();
1203 }
1204
Jeff Brown96307042012-07-27 15:51:34 -07001205 private void applyWakeLockFlagsOnReleaseLocked(WakeLock wakeLock) {
Jeff Brown6eade792013-09-10 18:45:25 -07001206 if ((wakeLock.mFlags & PowerManager.ON_AFTER_RELEASE) != 0
1207 && isScreenLock(wakeLock)) {
Jeff Brown96307042012-07-27 15:51:34 -07001208 userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
1209 PowerManager.USER_ACTIVITY_EVENT_OTHER,
1210 PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS,
1211 wakeLock.mOwnerUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214
Dianne Hackbornd953c532014-08-16 18:17:38 -07001215 private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag,
1216 int callingUid) {
Jeff Brown96307042012-07-27 15:51:34 -07001217 synchronized (mLock) {
1218 int index = findWakeLockIndexLocked(lock);
1219 if (index < 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -08001220 if (DEBUG_SPEW) {
1221 Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
1222 + " [not found], ws=" + ws);
1223 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001224 throw new IllegalArgumentException("Wake lock not active: " + lock
1225 + " from uid " + callingUid);
Jeff Brown96307042012-07-27 15:51:34 -07001226 }
1227
1228 WakeLock wakeLock = mWakeLocks.get(index);
Jeff Brown27f7a862012-12-12 15:43:31 -08001229 if (DEBUG_SPEW) {
1230 Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
1231 + " [" + wakeLock.mTag + "], ws=" + ws);
1232 }
1233
Jeff Brown96307042012-07-27 15:51:34 -07001234 if (!wakeLock.hasSameWorkSource(ws)) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001235 notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag,
1236 wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
Dianne Hackborn4590e522014-03-24 13:36:46 -07001237 ws, historyTag);
1238 wakeLock.mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07001239 wakeLock.updateWorkSource(ws);
Jeff Brown96307042012-07-27 15:51:34 -07001240 }
1241 }
1242 }
1243
1244 private int findWakeLockIndexLocked(IBinder lock) {
1245 final int count = mWakeLocks.size();
1246 for (int i = 0; i < count; i++) {
1247 if (mWakeLocks.get(i).mLock == lock) {
1248 return i;
1249 }
1250 }
1251 return -1;
1252 }
1253
1254 private void notifyWakeLockAcquiredLocked(WakeLock wakeLock) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07001255 if (mSystemReady && !wakeLock.mDisabled) {
Dianne Hackborn713df152013-05-17 11:27:57 -07001256 wakeLock.mNotifiedAcquired = true;
1257 mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001258 wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
1259 wakeLock.mHistoryTag);
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001260 restartNofifyLongTimerLocked(wakeLock);
1261 }
1262 }
1263
1264 private void enqueueNotifyLongMsgLocked(long time) {
1265 mNotifyLongScheduled = time;
1266 Message msg = mHandler.obtainMessage(MSG_CHECK_FOR_LONG_WAKELOCKS);
1267 msg.setAsynchronous(true);
1268 mHandler.sendMessageAtTime(msg, time);
1269 }
1270
1271 private void restartNofifyLongTimerLocked(WakeLock wakeLock) {
1272 wakeLock.mAcquireTime = SystemClock.uptimeMillis();
1273 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
1274 == PowerManager.PARTIAL_WAKE_LOCK && mNotifyLongScheduled == 0) {
1275 enqueueNotifyLongMsgLocked(wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL);
1276 }
1277 }
1278
1279 private void notifyWakeLockLongStartedLocked(WakeLock wakeLock) {
1280 if (mSystemReady && !wakeLock.mDisabled) {
1281 wakeLock.mNotifiedLong = true;
1282 mNotifier.onLongPartialWakeLockStart(wakeLock.mTag, wakeLock.mOwnerUid,
1283 wakeLock.mWorkSource, wakeLock.mHistoryTag);
1284 }
1285 }
1286
1287 private void notifyWakeLockLongFinishedLocked(WakeLock wakeLock) {
1288 if (wakeLock.mNotifiedLong) {
1289 wakeLock.mNotifiedLong = false;
1290 mNotifier.onLongPartialWakeLockFinish(wakeLock.mTag, wakeLock.mOwnerUid,
1291 wakeLock.mWorkSource, wakeLock.mHistoryTag);
Jeff Brown96307042012-07-27 15:51:34 -07001292 }
1293 }
1294
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001295 private void notifyWakeLockChangingLocked(WakeLock wakeLock, int flags, String tag,
1296 String packageName, int uid, int pid, WorkSource ws, String historyTag) {
1297 if (mSystemReady && wakeLock.mNotifiedAcquired) {
1298 mNotifier.onWakeLockChanging(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
1299 wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
1300 wakeLock.mHistoryTag, flags, tag, packageName, uid, pid, ws, historyTag);
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001301 notifyWakeLockLongFinishedLocked(wakeLock);
1302 // Changing the wake lock will count as releasing the old wake lock(s) and
1303 // acquiring the new ones... we do this because otherwise once a wakelock
1304 // becomes long, if we just continued to treat it as long we can get in to
1305 // situations where we spam battery stats with every following change to it.
1306 restartNofifyLongTimerLocked(wakeLock);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001307 }
1308 }
1309
Jeff Brown96307042012-07-27 15:51:34 -07001310 private void notifyWakeLockReleasedLocked(WakeLock wakeLock) {
Dianne Hackborn713df152013-05-17 11:27:57 -07001311 if (mSystemReady && wakeLock.mNotifiedAcquired) {
1312 wakeLock.mNotifiedAcquired = false;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001313 wakeLock.mAcquireTime = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001314 mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag,
1315 wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
1316 wakeLock.mWorkSource, wakeLock.mHistoryTag);
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001317 notifyWakeLockLongFinishedLocked(wakeLock);
Jeff Brown96307042012-07-27 15:51:34 -07001318 }
1319 }
1320
Jeff Brownec083212013-09-11 20:45:25 -07001321 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07001322 private boolean isWakeLockLevelSupportedInternal(int level) {
1323 synchronized (mLock) {
1324 switch (level) {
1325 case PowerManager.PARTIAL_WAKE_LOCK:
1326 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1327 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1328 case PowerManager.FULL_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -08001329 case PowerManager.DOZE_WAKE_LOCK:
Jeff Brownc4bd42c2015-06-12 18:00:11 -07001330 case PowerManager.DRAW_WAKE_LOCK:
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001331 return true;
Jeff Brown96307042012-07-27 15:51:34 -07001332
1333 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown131206b2014-04-08 17:27:14 -07001334 return mSystemReady && mDisplayManagerInternal.isProximitySensorAvailable();
Jeff Brown96307042012-07-27 15:51:34 -07001335
1336 default:
1337 return false;
1338 }
1339 }
1340 }
1341
Jeff Brown96307042012-07-27 15:51:34 -07001342 // Called from native code.
1343 private void userActivityFromNative(long eventTime, int event, int flags) {
1344 userActivityInternal(eventTime, event, flags, Process.SYSTEM_UID);
1345 }
1346
1347 private void userActivityInternal(long eventTime, int event, int flags, int uid) {
1348 synchronized (mLock) {
1349 if (userActivityNoUpdateLocked(eventTime, event, flags, uid)) {
1350 updatePowerStateLocked();
1351 }
1352 }
1353 }
1354
Lucas Dupin0a5d7972019-01-16 18:52:30 -08001355 private void onUserAttention() {
1356 synchronized (mLock) {
1357 if (userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
1358 PowerManager.USER_ACTIVITY_EVENT_ATTENTION, 0 /* flags */,
1359 Process.SYSTEM_UID)) {
1360 updatePowerStateLocked();
1361 }
1362 }
1363 }
1364
Jeff Brown96307042012-07-27 15:51:34 -07001365 private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) {
1366 if (DEBUG_SPEW) {
1367 Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime
1368 + ", event=" + event + ", flags=0x" + Integer.toHexString(flags)
1369 + ", uid=" + uid);
1370 }
1371
1372 if (eventTime < mLastSleepTime || eventTime < mLastWakeTime
Jeff Brown26875502014-01-30 21:47:47 -08001373 || !mBootCompleted || !mSystemReady) {
Jeff Brown96307042012-07-27 15:51:34 -07001374 return false;
1375 }
1376
Jeff Brown3edf5272014-08-14 19:25:14 -07001377 Trace.traceBegin(Trace.TRACE_TAG_POWER, "userActivity");
1378 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001379 if (eventTime > mLastInteractivePowerHintTime) {
Ruchi Kandoi0d434042016-10-03 09:12:02 -07001380 powerHintInternal(PowerHint.INTERACTION, 0);
Jeff Brown0a571122014-08-21 21:50:43 -07001381 mLastInteractivePowerHintTime = eventTime;
1382 }
1383
Jeff Brown3edf5272014-08-14 19:25:14 -07001384 mNotifier.onUserActivity(event, uid);
Lucas Dupin0a5d7972019-01-16 18:52:30 -08001385 mAttentionDetector.onUserActivity(eventTime, event);
Jeff Brown96307042012-07-27 15:51:34 -07001386
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001387 if (mUserInactiveOverrideFromWindowManager) {
1388 mUserInactiveOverrideFromWindowManager = false;
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001389 mOverriddenTimeout = -1;
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001390 }
1391
Jeff Brown0a571122014-08-21 21:50:43 -07001392 if (mWakefulness == WAKEFULNESS_ASLEEP
1393 || mWakefulness == WAKEFULNESS_DOZING
1394 || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) {
1395 return false;
1396 }
1397
Pavel Grafov28939982017-10-03 15:11:52 +01001398 maybeUpdateForegroundProfileLastActivityLocked(eventTime);
1399
Jeff Brown3edf5272014-08-14 19:25:14 -07001400 if ((flags & PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS) != 0) {
1401 if (eventTime > mLastUserActivityTimeNoChangeLights
1402 && eventTime > mLastUserActivityTime) {
1403 mLastUserActivityTimeNoChangeLights = eventTime;
1404 mDirty |= DIRTY_USER_ACTIVITY;
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08001405 if (event == PowerManager.USER_ACTIVITY_EVENT_BUTTON) {
1406 mDirty |= DIRTY_QUIESCENT;
1407 }
1408
Jeff Brown3edf5272014-08-14 19:25:14 -07001409 return true;
1410 }
1411 } else {
1412 if (eventTime > mLastUserActivityTime) {
1413 mLastUserActivityTime = eventTime;
1414 mDirty |= DIRTY_USER_ACTIVITY;
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08001415 if (event == PowerManager.USER_ACTIVITY_EVENT_BUTTON) {
1416 mDirty |= DIRTY_QUIESCENT;
1417 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001418 return true;
1419 }
Jeff Brown96307042012-07-27 15:51:34 -07001420 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001421 } finally {
1422 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001423 }
1424 return false;
1425 }
1426
Pavel Grafov28939982017-10-03 15:11:52 +01001427 private void maybeUpdateForegroundProfileLastActivityLocked(long eventTime) {
1428 final ProfilePowerState profile = mProfilePowerState.get(mForegroundProfile);
1429 if (profile != null && eventTime > profile.mLastUserActivityTime) {
1430 profile.mLastUserActivityTime = eventTime;
1431 }
1432 }
1433
Michael Wrighte3001042019-02-05 00:13:14 +00001434 private void wakeUpInternal(long eventTime, @WakeReason int reason, String details, int uid,
1435 String opPackageName, int opUid) {
Jeff Brown96307042012-07-27 15:51:34 -07001436 synchronized (mLock) {
Michael Wrighte3001042019-02-05 00:13:14 +00001437 if (wakeUpNoUpdateLocked(eventTime, reason, details, uid, opPackageName, opUid)) {
Jeff Brown96307042012-07-27 15:51:34 -07001438 updatePowerStateLocked();
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001439 }
1440 }
Jeff Brown96307042012-07-27 15:51:34 -07001441 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001442
Michael Wrighte3001042019-02-05 00:13:14 +00001443 private boolean wakeUpNoUpdateLocked(long eventTime, @WakeReason int reason, String details,
1444 int reasonUid, String opPackageName, int opUid) {
Jeff Brown96307042012-07-27 15:51:34 -07001445 if (DEBUG_SPEW) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001446 Slog.d(TAG, "wakeUpNoUpdateLocked: eventTime=" + eventTime + ", uid=" + reasonUid);
Joe Onorato60607a92010-10-23 14:49:30 -07001447 }
Jeff Brown96307042012-07-27 15:51:34 -07001448
1449 if (eventTime < mLastSleepTime || mWakefulness == WAKEFULNESS_AWAKE
Santos Cordon12f92eb2019-02-01 21:28:47 +00001450 || !mBootCompleted || !mSystemReady || mForceSuspendActive) {
Jeff Brown96307042012-07-27 15:51:34 -07001451 return false;
1452 }
1453
Makoto Onuki5ab5e1d2017-04-14 17:04:26 -07001454 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, TRACE_SCREEN_ON, 0);
1455
Jeff Brown3edf5272014-08-14 19:25:14 -07001456 Trace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp");
1457 try {
Michael Wrighte3001042019-02-05 00:13:14 +00001458 Slog.i(TAG, "Waking up from "
1459 + PowerManagerInternal.wakefulnessToString(mWakefulness)
1460 + " (uid=" + reasonUid
1461 + ", reason=" + PowerManager.wakeReasonToString(reason)
1462 + ", details=" + details
1463 + ")...");
Jeff Brown3edf5272014-08-14 19:25:14 -07001464
1465 mLastWakeTime = eventTime;
Michael Wrighte3001042019-02-05 00:13:14 +00001466 mLastWakeReason = reason;
1467 setWakefulnessLocked(WAKEFULNESS_AWAKE, reason, eventTime);
Jeff Brown3edf5272014-08-14 19:25:14 -07001468
Michael Wrighte3001042019-02-05 00:13:14 +00001469 mNotifier.onWakeUp(reason, details, reasonUid, opPackageName, opUid);
Jeff Brown3edf5272014-08-14 19:25:14 -07001470 userActivityNoUpdateLocked(
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001471 eventTime, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, reasonUid);
Jeff Brown3edf5272014-08-14 19:25:14 -07001472 } finally {
1473 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Brown96307042012-07-27 15:51:34 -07001474 }
Jeff Brown96307042012-07-27 15:51:34 -07001475 return true;
1476 }
1477
Jeff Brownc12035c2014-08-13 18:52:25 -07001478 private void goToSleepInternal(long eventTime, int reason, int flags, int uid) {
Jeff Brown96307042012-07-27 15:51:34 -07001479 synchronized (mLock) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001480 if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) {
Jeff Brown96307042012-07-27 15:51:34 -07001481 updatePowerStateLocked();
1482 }
1483 }
1484 }
1485
Santos Cordon12f92eb2019-02-01 21:28:47 +00001486 /**
1487 * Puts the system in doze.
1488 *
1489 * This method is called goToSleep for historical reasons but actually attempts to DOZE,
1490 * and only tucks itself in to SLEEP if requested with the flag
1491 * {@link PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE}.
1492 */
Jeff Brownec083212013-09-11 20:45:25 -07001493 @SuppressWarnings("deprecation")
Jeff Brownc12035c2014-08-13 18:52:25 -07001494 private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) {
Jeff Brown96307042012-07-27 15:51:34 -07001495 if (DEBUG_SPEW) {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001496 Slog.d(TAG, "goToSleepNoUpdateLocked: eventTime=" + eventTime
Jeff Brownc12035c2014-08-13 18:52:25 -07001497 + ", reason=" + reason + ", flags=" + flags + ", uid=" + uid);
Jeff Brown96307042012-07-27 15:51:34 -07001498 }
1499
Jeff Brown26875502014-01-30 21:47:47 -08001500 if (eventTime < mLastWakeTime
1501 || mWakefulness == WAKEFULNESS_ASLEEP
1502 || mWakefulness == WAKEFULNESS_DOZING
Jeff Brown96307042012-07-27 15:51:34 -07001503 || !mBootCompleted || !mSystemReady) {
1504 return false;
1505 }
1506
Jeff Brown3edf5272014-08-14 19:25:14 -07001507 Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep");
1508 try {
Santos Cordon12f92eb2019-02-01 21:28:47 +00001509 reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX,
1510 Math.max(reason, PowerManager.GO_TO_SLEEP_REASON_MIN));
1511 Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason)
1512 + " (uid " + uid + ")...");
Jeff Brown6d8fd272014-05-20 21:24:38 -07001513
Jeff Brown3edf5272014-08-14 19:25:14 -07001514 mLastSleepTime = eventTime;
Calin Tatarua3805722018-08-09 16:41:28 +02001515 mLastSleepReason = reason;
Jeff Brown3edf5272014-08-14 19:25:14 -07001516 mSandmanSummoned = true;
Michael Wrighte3001042019-02-05 00:13:14 +00001517 setWakefulnessLocked(WAKEFULNESS_DOZING, reason, eventTime);
Jeff Brown3edf5272014-08-14 19:25:14 -07001518
1519 // Report the number of wake locks that will be cleared by going to sleep.
1520 int numWakeLocksCleared = 0;
1521 final int numWakeLocks = mWakeLocks.size();
1522 for (int i = 0; i < numWakeLocks; i++) {
1523 final WakeLock wakeLock = mWakeLocks.get(i);
1524 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
1525 case PowerManager.FULL_WAKE_LOCK:
1526 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1527 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1528 numWakeLocksCleared += 1;
1529 break;
1530 }
1531 }
Michael Wrightb55e3a12018-03-06 15:14:06 +00001532 EventLogTags.writePowerSleepRequested(numWakeLocksCleared);
Jeff Brown3edf5272014-08-14 19:25:14 -07001533
1534 // Skip dozing if requested.
1535 if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) {
1536 reallyGoToSleepNoUpdateLocked(eventTime, uid);
1537 }
1538 } finally {
1539 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001540 }
Jeff Brown96307042012-07-27 15:51:34 -07001541 return true;
1542 }
1543
Jeff Brownc12035c2014-08-13 18:52:25 -07001544 private void napInternal(long eventTime, int uid) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001545 synchronized (mLock) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001546 if (napNoUpdateLocked(eventTime, uid)) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001547 updatePowerStateLocked();
1548 }
1549 }
1550 }
1551
Jeff Brownc12035c2014-08-13 18:52:25 -07001552 private boolean napNoUpdateLocked(long eventTime, int uid) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001553 if (DEBUG_SPEW) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001554 Slog.d(TAG, "napNoUpdateLocked: eventTime=" + eventTime + ", uid=" + uid);
Jeff Brown62c82e42012-09-26 01:30:41 -07001555 }
1556
1557 if (eventTime < mLastWakeTime || mWakefulness != WAKEFULNESS_AWAKE
1558 || !mBootCompleted || !mSystemReady) {
1559 return false;
1560 }
1561
Jeff Brown3edf5272014-08-14 19:25:14 -07001562 Trace.traceBegin(Trace.TRACE_TAG_POWER, "nap");
1563 try {
1564 Slog.i(TAG, "Nap time (uid " + uid +")...");
Jeff Brown62c82e42012-09-26 01:30:41 -07001565
Jeff Brown3edf5272014-08-14 19:25:14 -07001566 mSandmanSummoned = true;
Michael Wrighte3001042019-02-05 00:13:14 +00001567 setWakefulnessLocked(WAKEFULNESS_DREAMING, 0, eventTime);
Jeff Brown3edf5272014-08-14 19:25:14 -07001568 } finally {
1569 Trace.traceEnd(Trace.TRACE_TAG_POWER);
1570 }
Jeff Brown26875502014-01-30 21:47:47 -08001571 return true;
1572 }
1573
1574 // Done dozing, drop everything and go to sleep.
Jeff Brownc12035c2014-08-13 18:52:25 -07001575 private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) {
Jeff Brown26875502014-01-30 21:47:47 -08001576 if (DEBUG_SPEW) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001577 Slog.d(TAG, "reallyGoToSleepNoUpdateLocked: eventTime=" + eventTime
1578 + ", uid=" + uid);
Jeff Brown26875502014-01-30 21:47:47 -08001579 }
1580
1581 if (eventTime < mLastWakeTime || mWakefulness == WAKEFULNESS_ASLEEP
1582 || !mBootCompleted || !mSystemReady) {
1583 return false;
1584 }
1585
Jeff Brown3edf5272014-08-14 19:25:14 -07001586 Trace.traceBegin(Trace.TRACE_TAG_POWER, "reallyGoToSleep");
1587 try {
1588 Slog.i(TAG, "Sleeping (uid " + uid +")...");
Jeff Brown26875502014-01-30 21:47:47 -08001589
Michael Wrighte3001042019-02-05 00:13:14 +00001590 setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT,
1591 eventTime);
Jeff Brown3edf5272014-08-14 19:25:14 -07001592 } finally {
1593 Trace.traceEnd(Trace.TRACE_TAG_POWER);
1594 }
Jeff Brown62c82e42012-09-26 01:30:41 -07001595 return true;
1596 }
1597
Santos Cordon21e9f2b2017-09-13 11:59:39 -07001598 @VisibleForTesting
Michael Wrighte3001042019-02-05 00:13:14 +00001599 void setWakefulnessLocked(int wakefulness, int reason, long eventTime) {
Jeff Brownfbe96702014-11-19 18:30:58 -08001600 if (mWakefulness != wakefulness) {
Jeff Brownfbe96702014-11-19 18:30:58 -08001601 mWakefulness = wakefulness;
1602 mWakefulnessChanging = true;
1603 mDirty |= DIRTY_WAKEFULNESS;
Santos Cordon21e9f2b2017-09-13 11:59:39 -07001604 if (mNotifier != null) {
Michael Wrighte3001042019-02-05 00:13:14 +00001605 mNotifier.onWakefulnessChangeStarted(wakefulness, reason, eventTime);
Santos Cordon21e9f2b2017-09-13 11:59:39 -07001606 }
Lucas Dupin0a5d7972019-01-16 18:52:30 -08001607 mAttentionDetector.onWakefulnessChangeStarted(wakefulness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001608 }
1609 }
1610
Santos Cordon64a6e612018-08-22 19:27:04 +01001611 @VisibleForTesting
1612 int getWakefulness() {
1613 return mWakefulness;
1614 }
1615
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001616 /**
1617 * Logs the time the device would have spent awake before user activity timeout,
1618 * had the system not been told the user was inactive.
1619 */
1620 private void logSleepTimeoutRecapturedLocked() {
1621 final long now = SystemClock.uptimeMillis();
1622 final long savedWakeTimeMs = mOverriddenTimeout - now;
1623 if (savedWakeTimeMs >= 0) {
Michael Wrightb55e3a12018-03-06 15:14:06 +00001624 EventLogTags.writePowerSoftSleepRequested(savedWakeTimeMs);
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001625 mOverriddenTimeout = -1;
1626 }
1627 }
1628
Jeff Brown416c49c2015-05-26 19:50:18 -07001629 private void finishWakefulnessChangeIfNeededLocked() {
1630 if (mWakefulnessChanging && mDisplayReady) {
1631 if (mWakefulness == WAKEFULNESS_DOZING
1632 && (mWakeLockSummary & WAKE_LOCK_DOZE) == 0) {
1633 return; // wait until dream has enabled dozing
1634 }
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001635 if (mWakefulness == WAKEFULNESS_DOZING || mWakefulness == WAKEFULNESS_ASLEEP) {
1636 logSleepTimeoutRecapturedLocked();
1637 }
Makoto Onuki5ab5e1d2017-04-14 17:04:26 -07001638 if (mWakefulness == WAKEFULNESS_AWAKE) {
Michael Wrighte3001042019-02-05 00:13:14 +00001639 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, TRACE_SCREEN_ON, 0);
1640 final int latencyMs = (int) (SystemClock.uptimeMillis() - mLastWakeTime);
1641 if (latencyMs >= SCREEN_ON_LATENCY_WARNING_MS) {
1642 Slog.w(TAG, "Screen on took " + latencyMs + " ms");
1643 }
Makoto Onuki5ab5e1d2017-04-14 17:04:26 -07001644 }
Jeff Brownfbe96702014-11-19 18:30:58 -08001645 mWakefulnessChanging = false;
Jeff Brown416c49c2015-05-26 19:50:18 -07001646 mNotifier.onWakefulnessChangeFinished();
Jeff Brown037c33e2014-04-09 00:31:55 -07001647 }
1648 }
1649
Jeff Brown96307042012-07-27 15:51:34 -07001650 /**
1651 * Updates the global power state based on dirty bits recorded in mDirty.
1652 *
1653 * This is the main function that performs power state transitions.
1654 * We centralize them here so that we can recompute the power state completely
1655 * each time something important changes, and ensure that we do it the same
1656 * way each time. The point is to gather all of the transition logic here.
1657 */
1658 private void updatePowerStateLocked() {
1659 if (!mSystemReady || mDirty == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001660 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
Jeff Brown2d8a3902014-03-11 23:02:35 -07001662 if (!Thread.holdsLock(mLock)) {
1663 Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked");
1664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
Jeff Brown3edf5272014-08-14 19:25:14 -07001666 Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState");
1667 try {
1668 // Phase 0: Basic state updates.
1669 updateIsPoweredLocked(mDirty);
1670 updateStayOnLocked(mDirty);
Jeff Brown7b5be5e2014-11-12 18:45:31 -08001671 updateScreenBrightnessBoostLocked(mDirty);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672
Jeff Brown3edf5272014-08-14 19:25:14 -07001673 // Phase 1: Update wakefulness.
1674 // Loop because the wake lock and user activity computations are influenced
1675 // by changes in wakefulness.
1676 final long now = SystemClock.uptimeMillis();
1677 int dirtyPhase2 = 0;
1678 for (;;) {
1679 int dirtyPhase1 = mDirty;
1680 dirtyPhase2 |= dirtyPhase1;
1681 mDirty = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
Jeff Brown3edf5272014-08-14 19:25:14 -07001683 updateWakeLockSummaryLocked(dirtyPhase1);
1684 updateUserActivitySummaryLocked(now, dirtyPhase1);
1685 if (!updateWakefulnessLocked(dirtyPhase1)) {
1686 break;
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001689
Pavel Grafov28939982017-10-03 15:11:52 +01001690 // Phase 2: Lock profiles that became inactive/not kept awake.
1691 updateProfilesLocked(now);
Jeff Brown3edf5272014-08-14 19:25:14 -07001692
Pavel Grafov28939982017-10-03 15:11:52 +01001693 // Phase 3: Update display power state.
1694 final boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2);
1695
1696 // Phase 4: Update dream state (depends on display ready signal).
Jeff Brown2175e9c2014-09-12 16:11:07 -07001697 updateDreamLocked(dirtyPhase2, displayBecameReady);
1698
Pavel Grafov28939982017-10-03 15:11:52 +01001699 // Phase 5: Send notifications, if needed.
Jeff Brown416c49c2015-05-26 19:50:18 -07001700 finishWakefulnessChangeIfNeededLocked();
Jeff Brown3edf5272014-08-14 19:25:14 -07001701
Pavel Grafov28939982017-10-03 15:11:52 +01001702 // Phase 6: Update suspend blocker.
Jeff Brown3edf5272014-08-14 19:25:14 -07001703 // Because we might release the last suspend blocker here, we need to make sure
1704 // we finished everything else first!
1705 updateSuspendBlockerLocked();
1706 } finally {
1707 Trace.traceEnd(Trace.TRACE_TAG_POWER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
Jeff Brown96307042012-07-27 15:51:34 -07001709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
Jim Miller92e66dd2012-02-21 18:57:12 -08001711 /**
Pavel Grafov28939982017-10-03 15:11:52 +01001712 * Check profile timeouts and notify profiles that should be locked.
1713 */
1714 private void updateProfilesLocked(long now) {
1715 final int numProfiles = mProfilePowerState.size();
1716 for (int i = 0; i < numProfiles; i++) {
1717 final ProfilePowerState profile = mProfilePowerState.valueAt(i);
1718 if (isProfileBeingKeptAwakeLocked(profile, now)) {
1719 profile.mLockingNotified = false;
1720 } else if (!profile.mLockingNotified) {
1721 profile.mLockingNotified = true;
1722 mNotifier.onProfileTimeout(profile.mUserId);
1723 }
1724 }
1725 }
1726
1727 private boolean isProfileBeingKeptAwakeLocked(ProfilePowerState profile, long now) {
1728 return (profile.mLastUserActivityTime + profile.mScreenOffTimeout > now)
1729 || (profile.mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0
1730 || (mProximityPositive &&
1731 (profile.mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0);
1732 }
1733
1734 /**
Jeff Brown96307042012-07-27 15:51:34 -07001735 * Updates the value of mIsPowered.
1736 * Sets DIRTY_IS_POWERED if a change occurred.
Jim Miller92e66dd2012-02-21 18:57:12 -08001737 */
Jeff Brown96307042012-07-27 15:51:34 -07001738 private void updateIsPoweredLocked(int dirty) {
1739 if ((dirty & DIRTY_BATTERY_STATE) != 0) {
Jeff Brownf3fb8952012-10-02 20:57:05 -07001740 final boolean wasPowered = mIsPowered;
1741 final int oldPlugType = mPlugType;
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001742 final boolean oldLevelLow = mBatteryLevelLow;
Jeff Brown21392762014-06-13 19:00:36 -07001743 mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);
1744 mPlugType = mBatteryManagerInternal.getPlugType();
1745 mBatteryLevel = mBatteryManagerInternal.getBatteryLevel();
1746 mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow();
Jeff Browna4d82042012-10-02 19:11:19 -07001747
Jeff Brown26875502014-01-30 21:47:47 -08001748 if (DEBUG_SPEW) {
Jeff Browna4d82042012-10-02 19:11:19 -07001749 Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered
Jeff Brownf3fb8952012-10-02 20:57:05 -07001750 + ", mIsPowered=" + mIsPowered
1751 + ", oldPlugType=" + oldPlugType
Jeff Brown016ff142012-10-15 16:47:22 -07001752 + ", mPlugType=" + mPlugType
1753 + ", mBatteryLevel=" + mBatteryLevel);
Jeff Browna4d82042012-10-02 19:11:19 -07001754 }
Jim Miller92e66dd2012-02-21 18:57:12 -08001755
Jeff Brownf3fb8952012-10-02 20:57:05 -07001756 if (wasPowered != mIsPowered || oldPlugType != mPlugType) {
Jeff Brown96307042012-07-27 15:51:34 -07001757 mDirty |= DIRTY_IS_POWERED;
1758
Jeff Brown3b971592013-01-09 18:46:37 -08001759 // Update wireless dock detection state.
1760 final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update(
Beverlyfcaab292018-03-19 10:42:06 -04001761 mIsPowered, mPlugType);
Jeff Brown3b971592013-01-09 18:46:37 -08001762
Jeff Brown96307042012-07-27 15:51:34 -07001763 // Treat plugging and unplugging the devices as a user activity.
1764 // Users find it disconcerting when they plug or unplug the device
1765 // and it shuts off right away.
1766 // Some devices also wake the device when plugged or unplugged because
1767 // they don't have a charging LED.
1768 final long now = SystemClock.uptimeMillis();
Jeff Brown3b971592013-01-09 18:46:37 -08001769 if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType,
1770 dockedOnWirelessCharger)) {
Michael Wrighte3001042019-02-05 00:13:14 +00001771 wakeUpNoUpdateLocked(now, PowerManager.WAKE_REASON_PLUGGED_IN,
1772 "android.server.power:PLUGGED:" + mIsPowered, Process.SYSTEM_UID,
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001773 mContext.getOpPackageName(), Process.SYSTEM_UID);
Jeff Brown96307042012-07-27 15:51:34 -07001774 }
1775 userActivityNoUpdateLocked(
1776 now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
Jeff Brown84e27562012-12-07 13:56:34 -08001777
Beverlyac32c9a2018-01-31 16:10:41 -05001778 // only play charging sounds if boot is completed so charging sounds don't play
1779 // with potential notification sounds
1780 if (mBootCompleted) {
1781 if (mIsPowered && !BatteryManager.isPlugWired(oldPlugType)
1782 && BatteryManager.isPlugWired(mPlugType)) {
Beverly91d0a632018-07-02 16:45:00 -04001783 mNotifier.onWiredChargingStarted(mForegroundProfile);
Beverlyac32c9a2018-01-31 16:10:41 -05001784 } else if (dockedOnWirelessCharger) {
Beverly91d0a632018-07-02 16:45:00 -04001785 mNotifier.onWirelessChargingStarted(mBatteryLevel, mForegroundProfile);
Beverlyac32c9a2018-01-31 16:10:41 -05001786 }
Jeff Brown84e27562012-12-07 13:56:34 -08001787 }
Jeff Brown96307042012-07-27 15:51:34 -07001788 }
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001789
Makoto Onukia3cd7b92018-03-19 14:47:05 -07001790 mBatterySaverStateMachine.setBatteryStatus(mIsPowered, mBatteryLevel, mBatteryLevelLow);
Jeff Brown96307042012-07-27 15:51:34 -07001791 }
1792 }
1793
Jeff Brown3b971592013-01-09 18:46:37 -08001794 private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(
1795 boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001796 // Don't wake when powered unless configured to do so.
1797 if (!mWakeUpWhenPluggedOrUnpluggedConfig) {
1798 return false;
Jeff Brownf3fb8952012-10-02 20:57:05 -07001799 }
Jeff Brown9fca9e92012-10-05 14:42:56 -07001800
Jeff Brown3b971592013-01-09 18:46:37 -08001801 // Don't wake when undocked from wireless charger.
1802 // See WirelessChargerDetector for justification.
Jeff Brown9fca9e92012-10-05 14:42:56 -07001803 if (wasPowered && !mIsPowered
1804 && oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
1805 return false;
1806 }
Jeff Brown3b971592013-01-09 18:46:37 -08001807
1808 // Don't wake when docked on wireless charger unless we are certain of it.
1809 // See WirelessChargerDetector for justification.
Jeff Brown9fca9e92012-10-05 14:42:56 -07001810 if (!wasPowered && mIsPowered
1811 && mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
Jeff Brown3b971592013-01-09 18:46:37 -08001812 && !dockedOnWirelessCharger) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001813 return false;
1814 }
1815
1816 // If already dreaming and becoming powered, then don't wake.
Jeff Brown26875502014-01-30 21:47:47 -08001817 if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001818 return false;
1819 }
1820
Bryce Lee584a4452014-10-21 15:55:55 -07001821 // Don't wake while theater mode is enabled.
1822 if (mTheaterModeEnabled && !mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig) {
1823 return false;
1824 }
1825
Adrian Roos56021892017-02-27 20:25:09 +01001826 // On Always On Display, SystemUI shows the charging indicator
1827 if (mAlwaysOnEnabled && mWakefulness == WAKEFULNESS_DOZING) {
1828 return false;
1829 }
1830
Jeff Brown9fca9e92012-10-05 14:42:56 -07001831 // Otherwise wake up!
1832 return true;
Jeff Brownf3fb8952012-10-02 20:57:05 -07001833 }
1834
Jeff Brown96307042012-07-27 15:51:34 -07001835 /**
1836 * Updates the value of mStayOn.
1837 * Sets DIRTY_STAY_ON if a change occurred.
1838 */
1839 private void updateStayOnLocked(int dirty) {
1840 if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) {
Jeff Brown93cbbb22012-10-04 13:18:36 -07001841 final boolean wasStayOn = mStayOn;
Jeff Brown96307042012-07-27 15:51:34 -07001842 if (mStayOnWhilePluggedInSetting != 0
1843 && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
Jeff Brown21392762014-06-13 19:00:36 -07001844 mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting);
Jeff Brown96307042012-07-27 15:51:34 -07001845 } else {
1846 mStayOn = false;
1847 }
Jeff Brown93cbbb22012-10-04 13:18:36 -07001848
1849 if (mStayOn != wasStayOn) {
1850 mDirty |= DIRTY_STAY_ON;
1851 }
Jeff Brown96307042012-07-27 15:51:34 -07001852 }
1853 }
1854
1855 /**
1856 * Updates the value of mWakeLockSummary to summarize the state of all active wake locks.
1857 * Note that most wake-locks are ignored when the system is asleep.
1858 *
1859 * This function must have no other side-effects.
1860 */
Jeff Brownec083212013-09-11 20:45:25 -07001861 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07001862 private void updateWakeLockSummaryLocked(int dirty) {
1863 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_WAKEFULNESS)) != 0) {
1864 mWakeLockSummary = 0;
1865
Pavel Grafov28939982017-10-03 15:11:52 +01001866 final int numProfiles = mProfilePowerState.size();
1867 for (int i = 0; i < numProfiles; i++) {
1868 mProfilePowerState.valueAt(i).mWakeLockSummary = 0;
1869 }
1870
Jeff Brown96307042012-07-27 15:51:34 -07001871 final int numWakeLocks = mWakeLocks.size();
1872 for (int i = 0; i < numWakeLocks; i++) {
1873 final WakeLock wakeLock = mWakeLocks.get(i);
Pavel Grafov28939982017-10-03 15:11:52 +01001874 final int wakeLockFlags = getWakeLockSummaryFlags(wakeLock);
1875 mWakeLockSummary |= wakeLockFlags;
1876 for (int j = 0; j < numProfiles; j++) {
1877 final ProfilePowerState profile = mProfilePowerState.valueAt(j);
1878 if (wakeLockAffectsUser(wakeLock, profile.mUserId)) {
1879 profile.mWakeLockSummary |= wakeLockFlags;
1880 }
Jeff Brown96307042012-07-27 15:51:34 -07001881 }
1882 }
1883
Pavel Grafov28939982017-10-03 15:11:52 +01001884 mWakeLockSummary = adjustWakeLockSummaryLocked(mWakeLockSummary);
1885 for (int i = 0; i < numProfiles; i++) {
1886 final ProfilePowerState profile = mProfilePowerState.valueAt(i);
1887 profile.mWakeLockSummary = adjustWakeLockSummaryLocked(profile.mWakeLockSummary);
Jeff Brownc2932a12014-11-20 18:04:05 -08001888 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001889
Jeff Brown96307042012-07-27 15:51:34 -07001890 if (DEBUG_SPEW) {
1891 Slog.d(TAG, "updateWakeLockSummaryLocked: mWakefulness="
Jeff Brownfbe96702014-11-19 18:30:58 -08001892 + PowerManagerInternal.wakefulnessToString(mWakefulness)
Jeff Brown96307042012-07-27 15:51:34 -07001893 + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));
1894 }
1895 }
1896 }
1897
Pavel Grafov28939982017-10-03 15:11:52 +01001898 private int adjustWakeLockSummaryLocked(int wakeLockSummary) {
1899 // Cancel wake locks that make no sense based on the current state.
1900 if (mWakefulness != WAKEFULNESS_DOZING) {
1901 wakeLockSummary &= ~(WAKE_LOCK_DOZE | WAKE_LOCK_DRAW);
1902 }
1903 if (mWakefulness == WAKEFULNESS_ASLEEP
1904 || (wakeLockSummary & WAKE_LOCK_DOZE) != 0) {
1905 wakeLockSummary &= ~(WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM
1906 | WAKE_LOCK_BUTTON_BRIGHT);
1907 if (mWakefulness == WAKEFULNESS_ASLEEP) {
1908 wakeLockSummary &= ~WAKE_LOCK_PROXIMITY_SCREEN_OFF;
1909 }
1910 }
1911
1912 // Infer implied wake locks where necessary based on the current state.
1913 if ((wakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0) {
1914 if (mWakefulness == WAKEFULNESS_AWAKE) {
1915 wakeLockSummary |= WAKE_LOCK_CPU | WAKE_LOCK_STAY_AWAKE;
1916 } else if (mWakefulness == WAKEFULNESS_DREAMING) {
1917 wakeLockSummary |= WAKE_LOCK_CPU;
1918 }
1919 }
1920 if ((wakeLockSummary & WAKE_LOCK_DRAW) != 0) {
1921 wakeLockSummary |= WAKE_LOCK_CPU;
1922 }
1923
1924 return wakeLockSummary;
1925 }
1926
1927 /** Get wake lock summary flags that correspond to the given wake lock. */
1928 private int getWakeLockSummaryFlags(WakeLock wakeLock) {
1929 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
1930 case PowerManager.PARTIAL_WAKE_LOCK:
1931 if (!wakeLock.mDisabled) {
1932 // We only respect this if the wake lock is not disabled.
1933 return WAKE_LOCK_CPU;
1934 }
1935 break;
1936 case PowerManager.FULL_WAKE_LOCK:
1937 return WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_BUTTON_BRIGHT;
1938 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1939 return WAKE_LOCK_SCREEN_BRIGHT;
1940 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1941 return WAKE_LOCK_SCREEN_DIM;
1942 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
1943 return WAKE_LOCK_PROXIMITY_SCREEN_OFF;
1944 case PowerManager.DOZE_WAKE_LOCK:
1945 return WAKE_LOCK_DOZE;
1946 case PowerManager.DRAW_WAKE_LOCK:
1947 return WAKE_LOCK_DRAW;
1948 }
1949 return 0;
1950 }
1951
1952 private boolean wakeLockAffectsUser(WakeLock wakeLock, @UserIdInt int userId) {
1953 if (wakeLock.mWorkSource != null) {
1954 for (int k = 0; k < wakeLock.mWorkSource.size(); k++) {
1955 final int uid = wakeLock.mWorkSource.get(k);
1956 if (userId == UserHandle.getUserId(uid)) {
1957 return true;
1958 }
1959 }
Narayan Kamath2f916ed2017-12-29 13:02:15 +00001960
1961 final ArrayList<WorkChain> workChains = wakeLock.mWorkSource.getWorkChains();
1962 if (workChains != null) {
1963 for (int k = 0; k < workChains.size(); k++) {
1964 final int uid = workChains.get(k).getAttributionUid();
1965 if (userId == UserHandle.getUserId(uid)) {
1966 return true;
1967 }
1968 }
1969 }
Pavel Grafov28939982017-10-03 15:11:52 +01001970 }
1971 return userId == UserHandle.getUserId(wakeLock.mOwnerUid);
1972 }
1973
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001974 void checkForLongWakeLocks() {
1975 synchronized (mLock) {
1976 final long now = SystemClock.uptimeMillis();
1977 mNotifyLongDispatched = now;
1978 final long when = now - MIN_LONG_WAKE_CHECK_INTERVAL;
1979 long nextCheckTime = Long.MAX_VALUE;
1980 final int numWakeLocks = mWakeLocks.size();
1981 for (int i = 0; i < numWakeLocks; i++) {
1982 final WakeLock wakeLock = mWakeLocks.get(i);
1983 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
1984 == PowerManager.PARTIAL_WAKE_LOCK) {
1985 if (wakeLock.mNotifiedAcquired && !wakeLock.mNotifiedLong) {
1986 if (wakeLock.mAcquireTime < when) {
1987 // This wake lock has exceeded the long acquire time, report!
1988 notifyWakeLockLongStartedLocked(wakeLock);
1989 } else {
1990 // This wake lock could still become a long one, at this time.
1991 long checkTime = wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL;
1992 if (checkTime < nextCheckTime) {
1993 nextCheckTime = checkTime;
1994 }
1995 }
1996 }
1997 }
1998 }
1999 mNotifyLongScheduled = 0;
2000 mHandler.removeMessages(MSG_CHECK_FOR_LONG_WAKELOCKS);
2001 if (nextCheckTime != Long.MAX_VALUE) {
2002 mNotifyLongNextCheck = nextCheckTime;
2003 enqueueNotifyLongMsgLocked(nextCheckTime);
2004 } else {
2005 mNotifyLongNextCheck = 0;
2006 }
2007 }
2008 }
2009
Jeff Brown96307042012-07-27 15:51:34 -07002010 /**
2011 * Updates the value of mUserActivitySummary to summarize the user requested
2012 * state of the system such as whether the screen should be bright or dim.
2013 * Note that user activity is ignored when the system is asleep.
2014 *
2015 * This function must have no other side-effects.
2016 */
2017 private void updateUserActivitySummaryLocked(long now, int dirty) {
2018 // Update the status of the user activity timeout timer.
Jeff Brown037c33e2014-04-09 00:31:55 -07002019 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY
Jeff Browna191aa92014-11-12 23:01:12 -08002020 | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -07002021 mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);
2022
2023 long nextTimeout = 0;
Jeff Brown26875502014-01-30 21:47:47 -08002024 if (mWakefulness == WAKEFULNESS_AWAKE
Jeff Brown037c33e2014-04-09 00:31:55 -07002025 || mWakefulness == WAKEFULNESS_DREAMING
2026 || mWakefulness == WAKEFULNESS_DOZING) {
Pavel Grafov28939982017-10-03 15:11:52 +01002027 final long sleepTimeout = getSleepTimeoutLocked();
2028 final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
2029 final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002030 final boolean userInactiveOverride = mUserInactiveOverrideFromWindowManager;
Pavel Grafov28939982017-10-03 15:11:52 +01002031 final long nextProfileTimeout = getNextProfileTimeoutLocked(now);
Jeff Brown96307042012-07-27 15:51:34 -07002032
2033 mUserActivitySummary = 0;
2034 if (mLastUserActivityTime >= mLastWakeTime) {
2035 nextTimeout = mLastUserActivityTime
2036 + screenOffTimeout - screenDimDuration;
2037 if (now < nextTimeout) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07002038 mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07002039 } else {
2040 nextTimeout = mLastUserActivityTime + screenOffTimeout;
2041 if (now < nextTimeout) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07002042 mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
Jeff Brown96307042012-07-27 15:51:34 -07002043 }
2044 }
2045 }
2046 if (mUserActivitySummary == 0
2047 && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {
2048 nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;
Jeff Brown970d4132014-07-19 11:33:47 -07002049 if (now < nextTimeout) {
Andrii Kulian9407a6b2017-05-24 12:24:09 -07002050 if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT
2051 || mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_VR) {
Jeff Brown970d4132014-07-19 11:33:47 -07002052 mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
2053 } else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
2054 mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
2055 }
Jeff Brown96307042012-07-27 15:51:34 -07002056 }
2057 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002058
Jeff Brown05af6ad2014-09-30 20:54:30 -07002059 if (mUserActivitySummary == 0) {
2060 if (sleepTimeout >= 0) {
2061 final long anyUserActivity = Math.max(mLastUserActivityTime,
2062 mLastUserActivityTimeNoChangeLights);
2063 if (anyUserActivity >= mLastWakeTime) {
2064 nextTimeout = anyUserActivity + sleepTimeout;
2065 if (now < nextTimeout) {
2066 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
2067 }
2068 }
2069 } else {
2070 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
2071 nextTimeout = -1;
2072 }
2073 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002074
2075 if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) {
Mojtaba2d80edc2015-09-18 16:35:24 -07002076 if ((mUserActivitySummary &
2077 (USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0) {
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07002078 // Device is being kept awake by recent user activity
2079 if (nextTimeout >= now && mOverriddenTimeout == -1) {
2080 // Save when the next timeout would have occurred
2081 mOverriddenTimeout = nextTimeout;
2082 }
Mojtaba2d80edc2015-09-18 16:35:24 -07002083 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002084 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
2085 nextTimeout = -1;
2086 }
2087
Alex Salo9e242232019-05-14 19:27:17 -07002088 if ((mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0
2089 && (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) == 0) {
Lucas Dupin0a5d7972019-01-16 18:52:30 -08002090 nextTimeout = mAttentionDetector.updateUserActivity(nextTimeout);
2091 }
2092
Pavel Grafov28939982017-10-03 15:11:52 +01002093 if (nextProfileTimeout > 0) {
2094 nextTimeout = Math.min(nextTimeout, nextProfileTimeout);
2095 }
2096
Jeff Brown05af6ad2014-09-30 20:54:30 -07002097 if (mUserActivitySummary != 0 && nextTimeout >= 0) {
Pavel Grafov28939982017-10-03 15:11:52 +01002098 scheduleUserInactivityTimeout(nextTimeout);
Jeff Brown96307042012-07-27 15:51:34 -07002099 }
2100 } else {
2101 mUserActivitySummary = 0;
2102 }
2103
2104 if (DEBUG_SPEW) {
2105 Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
Jeff Brownfbe96702014-11-19 18:30:58 -08002106 + PowerManagerInternal.wakefulnessToString(mWakefulness)
Jeff Brown96307042012-07-27 15:51:34 -07002107 + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
2108 + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
2109 }
2110 }
2111 }
2112
Pavel Grafov28939982017-10-03 15:11:52 +01002113 private void scheduleUserInactivityTimeout(long timeMs) {
2114 final Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
2115 msg.setAsynchronous(true);
2116 mHandler.sendMessageAtTime(msg, timeMs);
2117 }
2118
2119 /**
2120 * Finds the next profile timeout time or returns -1 if there are no profiles to be locked.
2121 */
2122 private long getNextProfileTimeoutLocked(long now) {
2123 long nextTimeout = -1;
2124 final int numProfiles = mProfilePowerState.size();
2125 for (int i = 0; i < numProfiles; i++) {
2126 final ProfilePowerState profile = mProfilePowerState.valueAt(i);
2127 final long timeout = profile.mLastUserActivityTime + profile.mScreenOffTimeout;
2128 if (timeout > now && (nextTimeout == -1 || timeout < nextTimeout)) {
2129 nextTimeout = timeout;
2130 }
2131 }
2132 return nextTimeout;
2133 }
2134
Jeff Brown96307042012-07-27 15:51:34 -07002135 /**
2136 * Called when a user activity timeout has occurred.
2137 * Simply indicates that something about user activity has changed so that the new
2138 * state can be recomputed when the power state is updated.
2139 *
2140 * This function must have no other side-effects besides setting the dirty
2141 * bit and calling update power state. Wakefulness transitions are handled elsewhere.
2142 */
2143 private void handleUserActivityTimeout() { // runs on handler thread
2144 synchronized (mLock) {
2145 if (DEBUG_SPEW) {
2146 Slog.d(TAG, "handleUserActivityTimeout");
2147 }
2148
2149 mDirty |= DIRTY_USER_ACTIVITY;
2150 updatePowerStateLocked();
2151 }
2152 }
2153
Pavel Grafov28939982017-10-03 15:11:52 +01002154 private long getSleepTimeoutLocked() {
2155 final long timeout = mSleepTimeoutSetting;
Jeff Brown05af6ad2014-09-30 20:54:30 -07002156 if (timeout <= 0) {
2157 return -1;
2158 }
2159 return Math.max(timeout, mMinimumScreenOffTimeoutConfig);
2160 }
2161
Pavel Grafov28939982017-10-03 15:11:52 +01002162 private long getScreenOffTimeoutLocked(long sleepTimeout) {
2163 long timeout = mScreenOffTimeoutSetting;
Jeff Brown96307042012-07-27 15:51:34 -07002164 if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
2165 timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
2166 }
Jeff Brown1e3b98d2012-09-30 18:58:59 -07002167 if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
Pavel Grafov28939982017-10-03 15:11:52 +01002168 timeout = Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
Jeff Brown1e3b98d2012-09-30 18:58:59 -07002169 }
Jeff Brown05af6ad2014-09-30 20:54:30 -07002170 if (sleepTimeout >= 0) {
2171 timeout = Math.min(timeout, sleepTimeout);
2172 }
Jeff Brown27736f52014-05-20 17:17:10 -07002173 return Math.max(timeout, mMinimumScreenOffTimeoutConfig);
Jeff Brown96307042012-07-27 15:51:34 -07002174 }
2175
Pavel Grafov28939982017-10-03 15:11:52 +01002176 private long getScreenDimDurationLocked(long screenOffTimeout) {
Jeff Brown27736f52014-05-20 17:17:10 -07002177 return Math.min(mMaximumScreenDimDurationConfig,
Pavel Grafov28939982017-10-03 15:11:52 +01002178 (long)(screenOffTimeout * mMaximumScreenDimRatioConfig));
Jeff Brown96307042012-07-27 15:51:34 -07002179 }
2180
2181 /**
2182 * Updates the wakefulness of the device.
2183 *
Jeff Brown26875502014-01-30 21:47:47 -08002184 * This is the function that decides whether the device should start dreaming
Jeff Brown96307042012-07-27 15:51:34 -07002185 * based on the current wake locks and user activity state. It may modify mDirty
2186 * if the wakefulness changes.
2187 *
2188 * Returns true if the wakefulness changed and we need to restart power state calculation.
2189 */
2190 private boolean updateWakefulnessLocked(int dirty) {
2191 boolean changed = false;
2192 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED
Jeff Brownec6aa592012-10-17 20:30:25 -07002193 | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE
2194 | DIRTY_DOCK_STATE)) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -07002195 if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {
2196 if (DEBUG_SPEW) {
Jeff Brown62c82e42012-09-26 01:30:41 -07002197 Slog.d(TAG, "updateWakefulnessLocked: Bed time...");
Jeff Brown96307042012-07-27 15:51:34 -07002198 }
Jeff Brown62c82e42012-09-26 01:30:41 -07002199 final long time = SystemClock.uptimeMillis();
Jeff Brownec6aa592012-10-17 20:30:25 -07002200 if (shouldNapAtBedTimeLocked()) {
Jeff Brownc12035c2014-08-13 18:52:25 -07002201 changed = napNoUpdateLocked(time, Process.SYSTEM_UID);
Jeff Brown62c82e42012-09-26 01:30:41 -07002202 } else {
2203 changed = goToSleepNoUpdateLocked(time,
Jeff Brownc12035c2014-08-13 18:52:25 -07002204 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);
Jeff Brown62c82e42012-09-26 01:30:41 -07002205 }
Jeff Brown96307042012-07-27 15:51:34 -07002206 }
2207 }
2208 return changed;
2209 }
2210
Jeff Brown645832d2012-10-03 14:57:03 -07002211 /**
Jeff Brownec6aa592012-10-17 20:30:25 -07002212 * Returns true if the device should automatically nap and start dreaming when the user
2213 * activity timeout has expired and it's bedtime.
2214 */
2215 private boolean shouldNapAtBedTimeLocked() {
2216 return mDreamsActivateOnSleepSetting
2217 || (mDreamsActivateOnDockSetting
2218 && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2219 }
2220
2221 /**
Jeff Brown645832d2012-10-03 14:57:03 -07002222 * Returns true if the device should go to sleep now.
2223 * Also used when exiting a dream to determine whether we should go back
2224 * to being fully awake or else go to sleep for good.
2225 */
Jeff Brown96307042012-07-27 15:51:34 -07002226 private boolean isItBedTimeYetLocked() {
Jeff Brown93cbbb22012-10-04 13:18:36 -07002227 return mBootCompleted && !isBeingKeptAwakeLocked();
Jeff Brown645832d2012-10-03 14:57:03 -07002228 }
2229
2230 /**
Jeff Brown93cbbb22012-10-04 13:18:36 -07002231 * Returns true if the device is being kept awake by a wake lock, user activity
Jeff Brownec083212013-09-11 20:45:25 -07002232 * or the stay on while powered setting. We also keep the phone awake when
2233 * the proximity sensor returns a positive result so that the device does not
2234 * lock while in a phone call. This function only controls whether the device
2235 * will go to sleep or dream which is independent of whether it will be allowed
2236 * to suspend.
Jeff Brown645832d2012-10-03 14:57:03 -07002237 */
Jeff Brown93cbbb22012-10-04 13:18:36 -07002238 private boolean isBeingKeptAwakeLocked() {
Jeff Brown645832d2012-10-03 14:57:03 -07002239 return mStayOn
Jeff Brown93cbbb22012-10-04 13:18:36 -07002240 || mProximityPositive
Jeff Brown10428742012-10-09 15:47:30 -07002241 || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0
Jeff Brown645832d2012-10-03 14:57:03 -07002242 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002243 | USER_ACTIVITY_SCREEN_DIM)) != 0
2244 || mScreenBrightnessBoostInProgress;
Jeff Brown96307042012-07-27 15:51:34 -07002245 }
2246
2247 /**
2248 * Determines whether to post a message to the sandman to update the dream state.
2249 */
Jeff Brown2175e9c2014-09-12 16:11:07 -07002250 private void updateDreamLocked(int dirty, boolean displayBecameReady) {
John Spurlockf4f6b4c2012-08-25 12:08:03 -04002251 if ((dirty & (DIRTY_WAKEFULNESS
Jeff Brown645832d2012-10-03 14:57:03 -07002252 | DIRTY_USER_ACTIVITY
2253 | DIRTY_WAKE_LOCKS
2254 | DIRTY_BOOT_COMPLETED
John Spurlockf4f6b4c2012-08-25 12:08:03 -04002255 | DIRTY_SETTINGS
2256 | DIRTY_IS_POWERED
2257 | DIRTY_STAY_ON
Jeff Brown93cbbb22012-10-04 13:18:36 -07002258 | DIRTY_PROXIMITY_POSITIVE
Jeff Brown2175e9c2014-09-12 16:11:07 -07002259 | DIRTY_BATTERY_STATE)) != 0 || displayBecameReady) {
2260 if (mDisplayReady) {
2261 scheduleSandmanLocked();
2262 }
Jeff Brown96307042012-07-27 15:51:34 -07002263 }
2264 }
2265
2266 private void scheduleSandmanLocked() {
2267 if (!mSandmanScheduled) {
2268 mSandmanScheduled = true;
2269 Message msg = mHandler.obtainMessage(MSG_SANDMAN);
2270 msg.setAsynchronous(true);
2271 mHandler.sendMessage(msg);
2272 }
2273 }
2274
2275 /**
Jeff Brown26875502014-01-30 21:47:47 -08002276 * Called when the device enters or exits a dreaming or dozing state.
Jeff Brown96307042012-07-27 15:51:34 -07002277 *
2278 * We do this asynchronously because we must call out of the power manager to start
2279 * the dream and we don't want to hold our lock while doing so. There is a risk that
2280 * the device will wake or go to sleep in the meantime so we have to handle that case.
2281 */
2282 private void handleSandman() { // runs on handler thread
2283 // Handle preconditions.
Jeff Brown26875502014-01-30 21:47:47 -08002284 final boolean startDreaming;
2285 final int wakefulness;
Jeff Brown96307042012-07-27 15:51:34 -07002286 synchronized (mLock) {
2287 mSandmanScheduled = false;
Jeff Brown26875502014-01-30 21:47:47 -08002288 wakefulness = mWakefulness;
Jeff Brown2175e9c2014-09-12 16:11:07 -07002289 if (mSandmanSummoned && mDisplayReady) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07002290 startDreaming = canDreamLocked() || canDozeLocked();
Jeff Brown26875502014-01-30 21:47:47 -08002291 mSandmanSummoned = false;
2292 } else {
2293 startDreaming = false;
Jeff Brown96307042012-07-27 15:51:34 -07002294 }
2295 }
2296
Jeff Brown96307042012-07-27 15:51:34 -07002297 // Start dreaming if needed.
2298 // We only control the dream on the handler thread, so we don't need to worry about
2299 // concurrent attempts to start or stop the dream.
Jeff Brown26875502014-01-30 21:47:47 -08002300 final boolean isDreaming;
Jeff Brown96307042012-07-27 15:51:34 -07002301 if (mDreamManager != null) {
Jeff Brown26875502014-01-30 21:47:47 -08002302 // Restart the dream whenever the sandman is summoned.
Jeff Brown62c82e42012-09-26 01:30:41 -07002303 if (startDreaming) {
Jeff Brownf6d46682014-07-17 22:44:20 -07002304 mDreamManager.stopDream(false /*immediate*/);
Jeff Brown26875502014-01-30 21:47:47 -08002305 mDreamManager.startDream(wakefulness == WAKEFULNESS_DOZING);
Jeff Brown96307042012-07-27 15:51:34 -07002306 }
Jeff Brown62c82e42012-09-26 01:30:41 -07002307 isDreaming = mDreamManager.isDreaming();
Jeff Brown26875502014-01-30 21:47:47 -08002308 } else {
2309 isDreaming = false;
Jeff Brown96307042012-07-27 15:51:34 -07002310 }
2311
2312 // Update dream state.
Jeff Brown96307042012-07-27 15:51:34 -07002313 synchronized (mLock) {
Jeff Brown26875502014-01-30 21:47:47 -08002314 // Remember the initial battery level when the dream started.
2315 if (startDreaming && isDreaming) {
2316 mBatteryLevelWhenDreamStarted = mBatteryLevel;
2317 if (wakefulness == WAKEFULNESS_DOZING) {
2318 Slog.i(TAG, "Dozing...");
2319 } else {
2320 Slog.i(TAG, "Dreaming...");
2321 }
2322 }
2323
2324 // If preconditions changed, wait for the next iteration to determine
2325 // whether the dream should continue (or be restarted).
2326 if (mSandmanSummoned || mWakefulness != wakefulness) {
2327 return; // wait for next cycle
2328 }
2329
2330 // Determine whether the dream should continue.
2331 if (wakefulness == WAKEFULNESS_DREAMING) {
2332 if (isDreaming && canDreamLocked()) {
2333 if (mDreamsBatteryLevelDrainCutoffConfig >= 0
Jeff Brown016ff142012-10-15 16:47:22 -07002334 && mBatteryLevel < mBatteryLevelWhenDreamStarted
Jeff Brown26875502014-01-30 21:47:47 -08002335 - mDreamsBatteryLevelDrainCutoffConfig
2336 && !isBeingKeptAwakeLocked()) {
Jeff Brown016ff142012-10-15 16:47:22 -07002337 // If the user activity timeout expired and the battery appears
2338 // to be draining faster than it is charging then stop dreaming
2339 // and go to sleep.
2340 Slog.i(TAG, "Stopping dream because the battery appears to "
2341 + "be draining faster than it is charging. "
2342 + "Battery level when dream started: "
2343 + mBatteryLevelWhenDreamStarted + "%. "
2344 + "Battery level now: " + mBatteryLevel + "%.");
2345 } else {
Jeff Brown26875502014-01-30 21:47:47 -08002346 return; // continue dreaming
Jeff Brown016ff142012-10-15 16:47:22 -07002347 }
Jeff Brown96307042012-07-27 15:51:34 -07002348 }
Jeff Brown26875502014-01-30 21:47:47 -08002349
2350 // Dream has ended or will be stopped. Update the power state.
2351 if (isItBedTimeYetLocked()) {
2352 goToSleepNoUpdateLocked(SystemClock.uptimeMillis(),
Jeff Brownc12035c2014-08-13 18:52:25 -07002353 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08002354 updatePowerStateLocked();
2355 } else {
Michael Wrighte3001042019-02-05 00:13:14 +00002356 wakeUpNoUpdateLocked(SystemClock.uptimeMillis(),
2357 PowerManager.WAKE_REASON_UNKNOWN,
2358 "android.server.power:DREAM_FINISHED", Process.SYSTEM_UID,
2359 mContext.getOpPackageName(), Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08002360 updatePowerStateLocked();
2361 }
2362 } else if (wakefulness == WAKEFULNESS_DOZING) {
2363 if (isDreaming) {
2364 return; // continue dozing
2365 }
2366
2367 // Doze has ended or will be stopped. Update the power state.
Jeff Brownc12035c2014-08-13 18:52:25 -07002368 reallyGoToSleepNoUpdateLocked(SystemClock.uptimeMillis(), Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08002369 updatePowerStateLocked();
Jeff Brown96307042012-07-27 15:51:34 -07002370 }
Jeff Brown96307042012-07-27 15:51:34 -07002371 }
2372
Jeff Brown26875502014-01-30 21:47:47 -08002373 // Stop dream.
2374 if (isDreaming) {
Jeff Brownf6d46682014-07-17 22:44:20 -07002375 mDreamManager.stopDream(false /*immediate*/);
Jeff Brown96307042012-07-27 15:51:34 -07002376 }
2377 }
2378
2379 /**
Jeff Brown26875502014-01-30 21:47:47 -08002380 * Returns true if the device is allowed to dream in its current state.
Jeff Brown96307042012-07-27 15:51:34 -07002381 */
2382 private boolean canDreamLocked() {
Jeff Brown26875502014-01-30 21:47:47 -08002383 if (mWakefulness != WAKEFULNESS_DREAMING
2384 || !mDreamsSupportedConfig
2385 || !mDreamsEnabledSetting
Jeff Brown970d4132014-07-19 11:33:47 -07002386 || !mDisplayPowerRequest.isBrightOrDim()
Santos Cordon3107d292016-09-20 15:50:35 -07002387 || mDisplayPowerRequest.isVr()
Jeff Brown05af6ad2014-09-30 20:54:30 -07002388 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
2389 | USER_ACTIVITY_SCREEN_DIM | USER_ACTIVITY_SCREEN_DREAM)) == 0
Jeff Brown26875502014-01-30 21:47:47 -08002390 || !mBootCompleted) {
2391 return false;
2392 }
2393 if (!isBeingKeptAwakeLocked()) {
Jeff Brown966604f2014-02-24 16:19:51 -08002394 if (!mIsPowered && !mDreamsEnabledOnBatteryConfig) {
Jeff Brown26875502014-01-30 21:47:47 -08002395 return false;
2396 }
2397 if (!mIsPowered
2398 && mDreamsBatteryLevelMinimumWhenNotPoweredConfig >= 0
2399 && mBatteryLevel < mDreamsBatteryLevelMinimumWhenNotPoweredConfig) {
2400 return false;
2401 }
2402 if (mIsPowered
2403 && mDreamsBatteryLevelMinimumWhenPoweredConfig >= 0
2404 && mBatteryLevel < mDreamsBatteryLevelMinimumWhenPoweredConfig) {
2405 return false;
Jeff Brown96307042012-07-27 15:51:34 -07002406 }
2407 }
Jeff Brown26875502014-01-30 21:47:47 -08002408 return true;
Jeff Brown96307042012-07-27 15:51:34 -07002409 }
2410
Jeff Brown96307042012-07-27 15:51:34 -07002411 /**
Jeff Brown05af6ad2014-09-30 20:54:30 -07002412 * Returns true if the device is allowed to doze in its current state.
2413 */
2414 private boolean canDozeLocked() {
2415 return mWakefulness == WAKEFULNESS_DOZING;
2416 }
2417
2418 /**
Jeff Brown96307042012-07-27 15:51:34 -07002419 * Updates the display power state asynchronously.
2420 * When the update is finished, mDisplayReady will be set to true. The display
2421 * controller posts a message to tell us when the actual display power state
2422 * has been updated so we come back here to double-check and finish up.
2423 *
2424 * This function recalculates the display power state each time.
Jeff Brown2175e9c2014-09-12 16:11:07 -07002425 *
2426 * @return True if the display became ready.
Jeff Brown96307042012-07-27 15:51:34 -07002427 */
Jeff Brown2175e9c2014-09-12 16:11:07 -07002428 private boolean updateDisplayPowerStateLocked(int dirty) {
2429 final boolean oldDisplayReady = mDisplayReady;
Jeff Brown96307042012-07-27 15:51:34 -07002430 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS
2431 | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED
Santos Cordon3107d292016-09-20 15:50:35 -07002432 | DIRTY_SETTINGS | DIRTY_SCREEN_BRIGHTNESS_BOOST | DIRTY_VR_MODE_CHANGED |
2433 DIRTY_QUIESCENT)) != 0) {
Jeff Brown970d4132014-07-19 11:33:47 -07002434 mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked();
Jeff Brown96307042012-07-27 15:51:34 -07002435
Jeff Browne333e672014-10-28 13:48:55 -07002436 // Determine appropriate screen brightness and auto-brightness adjustments.
Michael Wrightd8460232018-01-16 18:04:59 +00002437 final boolean autoBrightness;
2438 final int screenBrightnessOverride;
Michael Wright9f818ea2016-07-29 19:59:39 +01002439 if (!mBootCompleted) {
2440 // Keep the brightness steady during boot. This requires the
2441 // bootloader brightness and the default brightness to be identical.
2442 autoBrightness = false;
Michael Wrightd8460232018-01-16 18:04:59 +00002443 screenBrightnessOverride = mScreenBrightnessSettingDefault;
Michael Wright9f818ea2016-07-29 19:59:39 +01002444 } else if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) {
Jeff Brown96307042012-07-27 15:51:34 -07002445 autoBrightness = false;
Michael Wrightd8460232018-01-16 18:04:59 +00002446 screenBrightnessOverride = mScreenBrightnessOverrideFromWindowManager;
2447 } else {
2448 autoBrightness = (mScreenBrightnessModeSetting ==
2449 Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
2450 screenBrightnessOverride = -1;
Jeff Brown96307042012-07-27 15:51:34 -07002451 }
Jeff Browne333e672014-10-28 13:48:55 -07002452
2453 // Update display power request.
Michael Wrightd8460232018-01-16 18:04:59 +00002454 mDisplayPowerRequest.screenBrightnessOverride = screenBrightnessOverride;
Jeff Brown96307042012-07-27 15:51:34 -07002455 mDisplayPowerRequest.useAutoBrightness = autoBrightness;
Jeff Brown96307042012-07-27 15:51:34 -07002456 mDisplayPowerRequest.useProximitySensor = shouldUseProximitySensorLocked();
Santos Cordon3107d292016-09-20 15:50:35 -07002457 mDisplayPowerRequest.boostScreenBrightness = shouldBoostScreenBrightness();
Ruchi Kandoif974cc82014-05-01 11:25:10 -07002458
jackqdyulei92681e82017-02-28 11:26:28 -08002459 updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest);
2460
Jeff Brown970d4132014-07-19 11:33:47 -07002461 if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE) {
2462 mDisplayPowerRequest.dozeScreenState = mDozeScreenStateOverrideFromDreamManager;
Ivan Podogov56bc6d02018-02-26 16:04:24 +00002463 if ((mWakeLockSummary & WAKE_LOCK_DRAW) != 0
2464 && !mDrawWakeLockOverrideFromSidekick) {
Chris Phoenix10a4a642017-09-25 13:21:00 -07002465 if (mDisplayPowerRequest.dozeScreenState == Display.STATE_DOZE_SUSPEND) {
2466 mDisplayPowerRequest.dozeScreenState = Display.STATE_DOZE;
2467 }
2468 if (mDisplayPowerRequest.dozeScreenState == Display.STATE_ON_SUSPEND) {
2469 mDisplayPowerRequest.dozeScreenState = Display.STATE_ON;
2470 }
Jeff Brownc2932a12014-11-20 18:04:05 -08002471 }
Jeff Brown970d4132014-07-19 11:33:47 -07002472 mDisplayPowerRequest.dozeScreenBrightness =
2473 mDozeScreenBrightnessOverrideFromDreamManager;
2474 } else {
2475 mDisplayPowerRequest.dozeScreenState = Display.STATE_UNKNOWN;
2476 mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
2477 }
2478
Jeff Brown131206b2014-04-08 17:27:14 -07002479 mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest,
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002480 mRequestWaitForNegativeProximity);
Jeff Brown96307042012-07-27 15:51:34 -07002481 mRequestWaitForNegativeProximity = false;
2482
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08002483 if ((dirty & DIRTY_QUIESCENT) != 0) {
2484 sQuiescent = false;
2485 }
Jeff Brown96307042012-07-27 15:51:34 -07002486 if (DEBUG_SPEW) {
Jeff Browne333e672014-10-28 13:48:55 -07002487 Slog.d(TAG, "updateDisplayPowerStateLocked: mDisplayReady=" + mDisplayReady
Jeff Brown970d4132014-07-19 11:33:47 -07002488 + ", policy=" + mDisplayPowerRequest.policy
Jeff Brown96307042012-07-27 15:51:34 -07002489 + ", mWakefulness=" + mWakefulness
2490 + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)
2491 + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
Jeff Browne333e672014-10-28 13:48:55 -07002492 + ", mBootCompleted=" + mBootCompleted
Michael Wrightd8460232018-01-16 18:04:59 +00002493 + ", screenBrightnessOverride=" + screenBrightnessOverride
2494 + ", useAutoBrightness=" + autoBrightness
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08002495 + ", mScreenBrightnessBoostInProgress=" + mScreenBrightnessBoostInProgress
Santos Cordon3107d292016-09-20 15:50:35 -07002496 + ", mIsVrModeEnabled= " + mIsVrModeEnabled
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08002497 + ", sQuiescent=" + sQuiescent);
Jeff Brown96307042012-07-27 15:51:34 -07002498 }
2499 }
Jeff Brown2175e9c2014-09-12 16:11:07 -07002500 return mDisplayReady && !oldDisplayReady;
Jeff Brown96307042012-07-27 15:51:34 -07002501 }
2502
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002503 private void updateScreenBrightnessBoostLocked(int dirty) {
2504 if ((dirty & DIRTY_SCREEN_BRIGHTNESS_BOOST) != 0) {
2505 if (mScreenBrightnessBoostInProgress) {
2506 final long now = SystemClock.uptimeMillis();
2507 mHandler.removeMessages(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT);
2508 if (mLastScreenBrightnessBoostTime > mLastSleepTime) {
2509 final long boostTimeout = mLastScreenBrightnessBoostTime +
2510 SCREEN_BRIGHTNESS_BOOST_TIMEOUT;
2511 if (boostTimeout > now) {
2512 Message msg = mHandler.obtainMessage(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT);
2513 msg.setAsynchronous(true);
2514 mHandler.sendMessageAtTime(msg, boostTimeout);
2515 return;
2516 }
Jeff Browne333e672014-10-28 13:48:55 -07002517 }
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002518 mScreenBrightnessBoostInProgress = false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002519 mNotifier.onScreenBrightnessBoostChanged();
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002520 userActivityNoUpdateLocked(now,
2521 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
Jeff Browne333e672014-10-28 13:48:55 -07002522 }
Jeff Browne333e672014-10-28 13:48:55 -07002523 }
2524 }
2525
Santos Cordon3107d292016-09-20 15:50:35 -07002526 private boolean shouldBoostScreenBrightness() {
2527 return !mIsVrModeEnabled && mScreenBrightnessBoostInProgress;
2528 }
2529
Jeff Brown96307042012-07-27 15:51:34 -07002530 private static boolean isValidBrightness(int value) {
2531 return value >= 0 && value <= 255;
2532 }
2533
Santos Cordon21e9f2b2017-09-13 11:59:39 -07002534 @VisibleForTesting
2535 int getDesiredScreenPolicyLocked() {
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08002536 if (mWakefulness == WAKEFULNESS_ASLEEP || sQuiescent) {
Jeff Brown970d4132014-07-19 11:33:47 -07002537 return DisplayPowerRequest.POLICY_OFF;
Jeff Brown96307042012-07-27 15:51:34 -07002538 }
2539
Jeff Brown2175e9c2014-09-12 16:11:07 -07002540 if (mWakefulness == WAKEFULNESS_DOZING) {
2541 if ((mWakeLockSummary & WAKE_LOCK_DOZE) != 0) {
2542 return DisplayPowerRequest.POLICY_DOZE;
2543 }
Lucas Dupin16cfe452018-02-08 13:14:50 -08002544 if (mDozeAfterScreenOff) {
Jeff Brown2175e9c2014-09-12 16:11:07 -07002545 return DisplayPowerRequest.POLICY_OFF;
2546 }
2547 // Fall through and preserve the current screen policy if not configured to
2548 // doze after screen off. This causes the screen off transition to be skipped.
Jeff Brown26875502014-01-30 21:47:47 -08002549 }
2550
Santos Cordon21e9f2b2017-09-13 11:59:39 -07002551 // It is important that POLICY_VR check happens after the wakefulness checks above so
2552 // that VR-mode does not prevent displays from transitioning to the correct state when
2553 // dozing or sleeping.
2554 if (mIsVrModeEnabled) {
2555 return DisplayPowerRequest.POLICY_VR;
2556 }
2557
Jeff Brown96307042012-07-27 15:51:34 -07002558 if ((mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0
2559 || (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002560 || !mBootCompleted
2561 || mScreenBrightnessBoostInProgress) {
Jeff Brown970d4132014-07-19 11:33:47 -07002562 return DisplayPowerRequest.POLICY_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07002563 }
2564
Jeff Brown970d4132014-07-19 11:33:47 -07002565 return DisplayPowerRequest.POLICY_DIM;
Jeff Brown96307042012-07-27 15:51:34 -07002566 }
2567
Jeff Brown131206b2014-04-08 17:27:14 -07002568 private final DisplayManagerInternal.DisplayPowerCallbacks mDisplayPowerCallbacks =
2569 new DisplayManagerInternal.DisplayPowerCallbacks() {
Jeff Brown037c33e2014-04-09 00:31:55 -07002570 private int mDisplayState = Display.STATE_UNKNOWN;
Jeff Brown131206b2014-04-08 17:27:14 -07002571
Jeff Brown96307042012-07-27 15:51:34 -07002572 @Override
2573 public void onStateChanged() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002574 synchronized (mLock) {
2575 mDirty |= DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED;
2576 updatePowerStateLocked();
2577 }
Jim Miller92e66dd2012-02-21 18:57:12 -08002578 }
2579
2580 @Override
Jeff Brown93cbbb22012-10-04 13:18:36 -07002581 public void onProximityPositive() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002582 synchronized (mLock) {
2583 mProximityPositive = true;
2584 mDirty |= DIRTY_PROXIMITY_POSITIVE;
2585 updatePowerStateLocked();
2586 }
Jeff Brown93cbbb22012-10-04 13:18:36 -07002587 }
2588
2589 @Override
Jeff Brown96307042012-07-27 15:51:34 -07002590 public void onProximityNegative() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002591 synchronized (mLock) {
2592 mProximityPositive = false;
2593 mDirty |= DIRTY_PROXIMITY_POSITIVE;
2594 userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
2595 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
2596 updatePowerStateLocked();
2597 }
Jeff Brown96307042012-07-27 15:51:34 -07002598 }
Jeff Brown131206b2014-04-08 17:27:14 -07002599
2600 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002601 public void onDisplayStateChange(int state) {
2602 // This method is only needed to support legacy display blanking behavior
2603 // where the display's power state is coupled to suspend or to the power HAL.
2604 // The order of operations matters here.
2605 synchronized (mLock) {
2606 if (mDisplayState != state) {
2607 mDisplayState = state;
2608 if (state == Display.STATE_OFF) {
2609 if (!mDecoupleHalInteractiveModeFromDisplayConfig) {
2610 setHalInteractiveModeLocked(false);
2611 }
2612 if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2613 setHalAutoSuspendModeLocked(true);
2614 }
2615 } else {
2616 if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2617 setHalAutoSuspendModeLocked(false);
2618 }
2619 if (!mDecoupleHalInteractiveModeFromDisplayConfig) {
2620 setHalInteractiveModeLocked(true);
2621 }
2622 }
2623 }
2624 }
2625 }
2626
2627 @Override
Jeff Brown131206b2014-04-08 17:27:14 -07002628 public void acquireSuspendBlocker() {
2629 mDisplaySuspendBlocker.acquire();
2630 }
2631
2632 @Override
2633 public void releaseSuspendBlocker() {
2634 mDisplaySuspendBlocker.release();
2635 }
2636
2637 @Override
Jeff Brown131206b2014-04-08 17:27:14 -07002638 public String toString() {
2639 synchronized (this) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002640 return "state=" + Display.stateToString(mDisplayState);
Jeff Brown131206b2014-04-08 17:27:14 -07002641 }
2642 }
Jeff Brown96307042012-07-27 15:51:34 -07002643 };
Jim Miller92e66dd2012-02-21 18:57:12 -08002644
Jeff Brown96307042012-07-27 15:51:34 -07002645 private boolean shouldUseProximitySensorLocked() {
Santos Cordon3107d292016-09-20 15:50:35 -07002646 return !mIsVrModeEnabled && (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0;
Jeff Brown96307042012-07-27 15:51:34 -07002647 }
Jim Miller92e66dd2012-02-21 18:57:12 -08002648
Jeff Brown96307042012-07-27 15:51:34 -07002649 /**
2650 * Updates the suspend blocker that keeps the CPU alive.
2651 *
2652 * This function must have no other side-effects.
2653 */
2654 private void updateSuspendBlockerLocked() {
Jeff Brownec083212013-09-11 20:45:25 -07002655 final boolean needWakeLockSuspendBlocker = ((mWakeLockSummary & WAKE_LOCK_CPU) != 0);
Jeff Brown26875502014-01-30 21:47:47 -08002656 final boolean needDisplaySuspendBlocker = needDisplaySuspendBlockerLocked();
2657 final boolean autoSuspend = !needDisplaySuspendBlocker;
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002658 final boolean interactive = mDisplayPowerRequest.isBrightOrDim();
Jeff Brown26875502014-01-30 21:47:47 -08002659
2660 // Disable auto-suspend if needed.
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002661 // FIXME We should consider just leaving auto-suspend enabled forever since
2662 // we already hold the necessary wakelocks.
2663 if (!autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2664 setHalAutoSuspendModeLocked(false);
Jeff Brown26875502014-01-30 21:47:47 -08002665 }
Jeff Brown27f7a862012-12-12 15:43:31 -08002666
2667 // First acquire suspend blockers if needed.
2668 if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) {
2669 mWakeLockSuspendBlocker.acquire();
2670 mHoldingWakeLockSuspendBlocker = true;
2671 }
2672 if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) {
2673 mDisplaySuspendBlocker.acquire();
2674 mHoldingDisplaySuspendBlocker = true;
2675 }
2676
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002677 // Inform the power HAL about interactive mode.
2678 // Although we could set interactive strictly based on the wakefulness
2679 // as reported by isInteractive(), it is actually more desirable to track
2680 // the display policy state instead so that the interactive state observed
2681 // by the HAL more accurately tracks transitions between AWAKE and DOZING.
2682 // Refer to getDesiredScreenPolicyLocked() for details.
2683 if (mDecoupleHalInteractiveModeFromDisplayConfig) {
2684 // When becoming non-interactive, we want to defer sending this signal
2685 // until the display is actually ready so that all transitions have
2686 // completed. This is probably a good sign that things have gotten
2687 // too tangled over here...
2688 if (interactive || mDisplayReady) {
2689 setHalInteractiveModeLocked(interactive);
2690 }
2691 }
2692
Jeff Brown27f7a862012-12-12 15:43:31 -08002693 // Then release suspend blockers if needed.
2694 if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) {
2695 mWakeLockSuspendBlocker.release();
2696 mHoldingWakeLockSuspendBlocker = false;
2697 }
2698 if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) {
2699 mDisplaySuspendBlocker.release();
2700 mHoldingDisplaySuspendBlocker = false;
2701 }
Jeff Brown26875502014-01-30 21:47:47 -08002702
2703 // Enable auto-suspend if needed.
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002704 if (autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2705 setHalAutoSuspendModeLocked(true);
Jeff Brown26875502014-01-30 21:47:47 -08002706 }
Jeff Brown96307042012-07-27 15:51:34 -07002707 }
2708
Jeff Brownec083212013-09-11 20:45:25 -07002709 /**
2710 * Return true if we must keep a suspend blocker active on behalf of the display.
2711 * We do so if the screen is on or is in transition between states.
2712 */
Jeff Brown26875502014-01-30 21:47:47 -08002713 private boolean needDisplaySuspendBlockerLocked() {
Jeff Brownec083212013-09-11 20:45:25 -07002714 if (!mDisplayReady) {
2715 return true;
2716 }
Jeff Brown970d4132014-07-19 11:33:47 -07002717 if (mDisplayPowerRequest.isBrightOrDim()) {
Jeff Brownec083212013-09-11 20:45:25 -07002718 // If we asked for the screen to be on but it is off due to the proximity
2719 // sensor then we may suspend but only if the configuration allows it.
2720 // On some hardware it may not be safe to suspend because the proximity
2721 // sensor may not be correctly configured as a wake-up source.
2722 if (!mDisplayPowerRequest.useProximitySensor || !mProximityPositive
2723 || !mSuspendWhenScreenOffDueToProximityConfig) {
2724 return true;
2725 }
2726 }
Santos Cordond9701ab2019-08-01 18:33:20 +01002727
2728 if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE
2729 && mDisplayPowerRequest.dozeScreenState == Display.STATE_ON) {
2730 // Although we are in DOZE and would normally allow the device to suspend,
2731 // the doze service has explicitly requested the display to remain in the ON
2732 // state which means we should hold the display suspend blocker.
2733 return true;
2734 }
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002735 if (mScreenBrightnessBoostInProgress) {
2736 return true;
2737 }
Jeff Brown26875502014-01-30 21:47:47 -08002738 // Let the system suspend if the screen is off or dozing.
Jeff Brownec083212013-09-11 20:45:25 -07002739 return false;
2740 }
2741
Jeff Brown037c33e2014-04-09 00:31:55 -07002742 private void setHalAutoSuspendModeLocked(boolean enable) {
2743 if (enable != mHalAutoSuspendModeEnabled) {
Jeff Brown26875502014-01-30 21:47:47 -08002744 if (DEBUG) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002745 Slog.d(TAG, "Setting HAL auto-suspend mode to " + enable);
Jeff Brown26875502014-01-30 21:47:47 -08002746 }
Jeff Brown037c33e2014-04-09 00:31:55 -07002747 mHalAutoSuspendModeEnabled = enable;
Jeff Brown3edf5272014-08-14 19:25:14 -07002748 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalAutoSuspend(" + enable + ")");
2749 try {
Santos Cordon64a6e612018-08-22 19:27:04 +01002750 mNativeWrapper.nativeSetAutoSuspend(enable);
Jeff Brown3edf5272014-08-14 19:25:14 -07002751 } finally {
2752 Trace.traceEnd(Trace.TRACE_TAG_POWER);
2753 }
Jeff Brown26875502014-01-30 21:47:47 -08002754 }
2755 }
2756
Jeff Brown037c33e2014-04-09 00:31:55 -07002757 private void setHalInteractiveModeLocked(boolean enable) {
2758 if (enable != mHalInteractiveModeEnabled) {
Jeff Brown26875502014-01-30 21:47:47 -08002759 if (DEBUG) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002760 Slog.d(TAG, "Setting HAL interactive mode to " + enable);
Jeff Brown26875502014-01-30 21:47:47 -08002761 }
Jeff Brown037c33e2014-04-09 00:31:55 -07002762 mHalInteractiveModeEnabled = enable;
Jeff Brown3edf5272014-08-14 19:25:14 -07002763 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalInteractive(" + enable + ")");
2764 try {
Santos Cordon64a6e612018-08-22 19:27:04 +01002765 mNativeWrapper.nativeSetInteractive(enable);
Jeff Brown3edf5272014-08-14 19:25:14 -07002766 } finally {
2767 Trace.traceEnd(Trace.TRACE_TAG_POWER);
2768 }
Jeff Brown26875502014-01-30 21:47:47 -08002769 }
2770 }
2771
Jeff Brown037c33e2014-04-09 00:31:55 -07002772 private boolean isInteractiveInternal() {
Jeff Brown96307042012-07-27 15:51:34 -07002773 synchronized (mLock) {
Jeff Brownfbe96702014-11-19 18:30:58 -08002774 return PowerManagerInternal.isInteractive(mWakefulness);
Mike Lockwoodb2865412010-02-02 22:40:33 -05002775 }
2776 }
2777
Makoto Onukia3cd7b92018-03-19 14:47:05 -07002778 private boolean setLowPowerModeInternal(boolean enabled) {
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002779 synchronized (mLock) {
Makoto Onukia3cd7b92018-03-19 14:47:05 -07002780 if (DEBUG) {
2781 Slog.d(TAG, "setLowPowerModeInternal " + enabled + " mIsPowered=" + mIsPowered);
2782 }
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002783 if (mIsPowered) {
2784 return false;
2785 }
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002786
Makoto Onukia3cd7b92018-03-19 14:47:05 -07002787 mBatterySaverStateMachine.setBatterySaverEnabledManually(enabled);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002788
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002789 return true;
2790 }
2791 }
2792
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002793 boolean isDeviceIdleModeInternal() {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002794 synchronized (mLock) {
2795 return mDeviceIdleMode;
2796 }
2797 }
2798
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002799 boolean isLightDeviceIdleModeInternal() {
2800 synchronized (mLock) {
2801 return mLightDeviceIdleMode;
2802 }
2803 }
2804
Jeff Brown96307042012-07-27 15:51:34 -07002805 private void handleBatteryStateChangedLocked() {
2806 mDirty |= DIRTY_BATTERY_STATE;
2807 updatePowerStateLocked();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002808 }
2809
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002810 private void shutdownOrRebootInternal(final @HaltMode int haltMode, final boolean confirm,
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002811 final String reason, boolean wait) {
Jeff Brown96307042012-07-27 15:51:34 -07002812 if (mHandler == null || !mSystemReady) {
Jeff Sharkeyfe6f85c2017-01-20 10:42:57 -07002813 if (RescueParty.isAttemptingFactoryReset()) {
2814 // If we're stuck in a really low-level reboot loop, and a
2815 // rescue party is trying to prompt the user for a factory data
2816 // reset, we must GET TO DA CHOPPA!
2817 PowerManagerService.lowLevelReboot(reason);
2818 } else {
2819 throw new IllegalStateException("Too early to call shutdown() or reboot()");
2820 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002821 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002822
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002823 Runnable runnable = new Runnable() {
Jeff Brownab887a02012-10-15 16:00:40 -07002824 @Override
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002825 public void run() {
2826 synchronized (this) {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002827 if (haltMode == HALT_MODE_REBOOT_SAFE_MODE) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07002828 ShutdownThread.rebootSafeMode(getUiContext(), confirm);
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002829 } else if (haltMode == HALT_MODE_REBOOT) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07002830 ShutdownThread.reboot(getUiContext(), reason, confirm);
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002831 } else {
Adam Lesinskia82b6262017-03-21 16:56:17 -07002832 ShutdownThread.shutdown(getUiContext(), reason, confirm);
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002833 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002834 }
San Mehat1e512792010-01-07 10:40:29 -08002835 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002836 };
Jeff Brown96307042012-07-27 15:51:34 -07002837
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002838 // ShutdownThread must run on a looper capable of displaying the UI.
Michael Wright64c820d2017-03-21 12:36:55 +00002839 Message msg = Message.obtain(UiThread.getHandler(), runnable);
Jeff Brown96307042012-07-27 15:51:34 -07002840 msg.setAsynchronous(true);
Michael Wright64c820d2017-03-21 12:36:55 +00002841 UiThread.getHandler().sendMessage(msg);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002842
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002843 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002844 if (wait) {
2845 synchronized (runnable) {
2846 while (true) {
2847 try {
2848 runnable.wait();
2849 } catch (InterruptedException e) {
2850 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002851 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002852 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002853 }
2854 }
2855
Jeff Brown96307042012-07-27 15:51:34 -07002856 private void crashInternal(final String message) {
Dan Egnor60d87622009-12-16 16:32:58 -08002857 Thread t = new Thread("PowerManagerService.crash()") {
Jeff Brownab887a02012-10-15 16:00:40 -07002858 @Override
Jeff Brown96307042012-07-27 15:51:34 -07002859 public void run() {
2860 throw new RuntimeException(message);
2861 }
Dan Egnor60d87622009-12-16 16:32:58 -08002862 };
2863 try {
2864 t.start();
2865 t.join();
2866 } catch (InterruptedException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002867 Slog.wtf(TAG, e);
Dan Egnor60d87622009-12-16 16:32:58 -08002868 }
2869 }
2870
jackqdyulei92681e82017-02-28 11:26:28 -08002871 @VisibleForTesting
2872 void updatePowerRequestFromBatterySaverPolicy(DisplayPowerRequest displayPowerRequest) {
2873 PowerSaveState state = mBatterySaverPolicy.
Kweku Adams9f488e22019-01-14 16:25:08 -08002874 getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS);
jackqdyulei92681e82017-02-28 11:26:28 -08002875 displayPowerRequest.lowPowerMode = state.batterySaverEnabled;
2876 displayPowerRequest.screenLowPowerBrightnessFactor = state.brightnessFactor;
2877 }
2878
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002879 void setStayOnSettingInternal(int val) {
Christopher Tatead735322012-09-07 14:19:43 -07002880 Settings.Global.putInt(mContext.getContentResolver(),
2881 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
2883
Pavel Grafov28939982017-10-03 15:11:52 +01002884 void setMaximumScreenOffTimeoutFromDeviceAdminInternal(@UserIdInt int userId, long timeMs) {
2885 if (userId < 0) {
2886 Slog.wtf(TAG, "Attempt to set screen off timeout for invalid user: " + userId);
2887 return;
2888 }
Jeff Brown96307042012-07-27 15:51:34 -07002889 synchronized (mLock) {
Pavel Grafov28939982017-10-03 15:11:52 +01002890 // System-wide timeout
2891 if (userId == UserHandle.USER_SYSTEM) {
2892 mMaximumScreenOffTimeoutFromDeviceAdmin = timeMs;
2893 } else if (timeMs == Long.MAX_VALUE || timeMs == 0) {
2894 mProfilePowerState.delete(userId);
2895 } else {
2896 final ProfilePowerState profile = mProfilePowerState.get(userId);
2897 if (profile != null) {
2898 profile.mScreenOffTimeout = timeMs;
2899 } else {
2900 mProfilePowerState.put(userId, new ProfilePowerState(userId, timeMs));
2901 // We need to recalculate wake locks for the new profile state.
2902 mDirty |= DIRTY_WAKE_LOCKS;
2903 }
2904 }
Jeff Brown96307042012-07-27 15:51:34 -07002905 mDirty |= DIRTY_SETTINGS;
2906 updatePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 }
2908 }
2909
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002910 boolean setDeviceIdleModeInternal(boolean enabled) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002911 synchronized (mLock) {
Felipe Lemeea014392016-09-06 13:59:54 -07002912 if (mDeviceIdleMode == enabled) {
2913 return false;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002914 }
Felipe Lemeea014392016-09-06 13:59:54 -07002915 mDeviceIdleMode = enabled;
2916 updateWakeLockDisabledStatesLocked();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002917 }
Felipe Lemeea014392016-09-06 13:59:54 -07002918 if (enabled) {
2919 EventLogTags.writeDeviceIdleOnPhase("power");
2920 } else {
2921 EventLogTags.writeDeviceIdleOffPhase("power");
2922 }
2923 return true;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002924 }
2925
2926 boolean setLightDeviceIdleModeInternal(boolean enabled) {
2927 synchronized (mLock) {
2928 if (mLightDeviceIdleMode != enabled) {
2929 mLightDeviceIdleMode = enabled;
2930 return true;
2931 }
2932 return false;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002933 }
2934 }
2935
2936 void setDeviceIdleWhitelistInternal(int[] appids) {
2937 synchronized (mLock) {
2938 mDeviceIdleWhitelist = appids;
2939 if (mDeviceIdleMode) {
2940 updateWakeLockDisabledStatesLocked();
2941 }
2942 }
2943 }
2944
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002945 void setDeviceIdleTempWhitelistInternal(int[] appids) {
2946 synchronized (mLock) {
2947 mDeviceIdleTempWhitelist = appids;
2948 if (mDeviceIdleMode) {
2949 updateWakeLockDisabledStatesLocked();
2950 }
2951 }
2952 }
2953
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07002954 void startUidChangesInternal() {
2955 synchronized (mLock) {
2956 mUidsChanging = true;
2957 }
2958 }
2959
2960 void finishUidChangesInternal() {
2961 synchronized (mLock) {
2962 mUidsChanging = false;
2963 if (mUidsChanged) {
2964 updateWakeLockDisabledStatesLocked();
2965 mUidsChanged = false;
2966 }
2967 }
2968 }
2969
2970 private void handleUidStateChangeLocked() {
2971 if (mUidsChanging) {
2972 mUidsChanged = true;
2973 } else {
2974 updateWakeLockDisabledStatesLocked();
2975 }
2976 }
2977
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002978 void updateUidProcStateInternal(int uid, int procState) {
2979 synchronized (mLock) {
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07002980 UidState state = mUidState.get(uid);
2981 if (state == null) {
2982 state = new UidState(uid);
2983 mUidState.put(uid, state);
2984 }
Dianne Hackborn951ea692016-11-02 10:41:53 -07002985 final boolean oldShouldAllow = state.mProcState
2986 <= ActivityManager.PROCESS_STATE_RECEIVER;
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07002987 state.mProcState = procState;
Dianne Hackborn951ea692016-11-02 10:41:53 -07002988 if (state.mNumWakeLocks > 0) {
2989 if (mDeviceIdleMode) {
2990 handleUidStateChangeLocked();
2991 } else if (!state.mActive && oldShouldAllow !=
2992 (procState <= ActivityManager.PROCESS_STATE_RECEIVER)) {
2993 // If this uid is not active, but the process state has changed such
2994 // that we may still want to allow it to hold a wake lock, then take care of it.
2995 handleUidStateChangeLocked();
2996 }
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002997 }
2998 }
2999 }
3000
3001 void uidGoneInternal(int uid) {
3002 synchronized (mLock) {
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003003 final int index = mUidState.indexOfKey(uid);
3004 if (index >= 0) {
3005 UidState state = mUidState.valueAt(index);
3006 state.mProcState = ActivityManager.PROCESS_STATE_NONEXISTENT;
3007 state.mActive = false;
3008 mUidState.removeAt(index);
3009 if (mDeviceIdleMode && state.mNumWakeLocks > 0) {
3010 handleUidStateChangeLocked();
3011 }
3012 }
3013 }
3014 }
3015
3016 void uidActiveInternal(int uid) {
3017 synchronized (mLock) {
3018 UidState state = mUidState.get(uid);
3019 if (state == null) {
3020 state = new UidState(uid);
3021 state.mProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
3022 mUidState.put(uid, state);
3023 }
3024 state.mActive = true;
3025 if (state.mNumWakeLocks > 0) {
3026 handleUidStateChangeLocked();
3027 }
3028 }
3029 }
3030
3031 void uidIdleInternal(int uid) {
3032 synchronized (mLock) {
3033 UidState state = mUidState.get(uid);
3034 if (state != null) {
3035 state.mActive = false;
3036 if (state.mNumWakeLocks > 0) {
3037 handleUidStateChangeLocked();
3038 }
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003039 }
3040 }
3041 }
3042
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003043 private void updateWakeLockDisabledStatesLocked() {
3044 boolean changed = false;
3045 final int numWakeLocks = mWakeLocks.size();
3046 for (int i = 0; i < numWakeLocks; i++) {
3047 final WakeLock wakeLock = mWakeLocks.get(i);
3048 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
Ruchi Kandoi9c36c022016-04-20 13:42:01 -07003049 == PowerManager.PARTIAL_WAKE_LOCK) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003050 if (setWakeLockDisabledStateLocked(wakeLock)) {
3051 changed = true;
3052 if (wakeLock.mDisabled) {
3053 // This wake lock is no longer being respected.
3054 notifyWakeLockReleasedLocked(wakeLock);
3055 } else {
3056 notifyWakeLockAcquiredLocked(wakeLock);
3057 }
3058 }
3059 }
3060 }
3061 if (changed) {
3062 mDirty |= DIRTY_WAKE_LOCKS;
3063 updatePowerStateLocked();
3064 }
3065 }
3066
3067 private boolean setWakeLockDisabledStateLocked(WakeLock wakeLock) {
3068 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
3069 == PowerManager.PARTIAL_WAKE_LOCK) {
Ruchi Kandoi9c36c022016-04-20 13:42:01 -07003070 boolean disabled = false;
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003071 final int appid = UserHandle.getAppId(wakeLock.mOwnerUid);
3072 if (appid >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborn3e99f652017-07-05 16:33:56 -07003073 // Cached inactive processes are never allowed to hold wake locks.
3074 if (mConstants.NO_CACHED_WAKE_LOCKS) {
Santos Cordon12f92eb2019-02-01 21:28:47 +00003075 disabled = mForceSuspendActive
3076 || (!wakeLock.mUidState.mActive && wakeLock.mUidState.mProcState
Dianne Hackborn3e99f652017-07-05 16:33:56 -07003077 != ActivityManager.PROCESS_STATE_NONEXISTENT &&
Santos Cordon12f92eb2019-02-01 21:28:47 +00003078 wakeLock.mUidState.mProcState > ActivityManager.PROCESS_STATE_RECEIVER);
Dianne Hackborn3e99f652017-07-05 16:33:56 -07003079 }
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003080 if (mDeviceIdleMode) {
Dianne Hackborn3e99f652017-07-05 16:33:56 -07003081 // If we are in idle mode, we will also ignore all partial wake locks that are
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003082 // for application uids that are not whitelisted.
3083 final UidState state = wakeLock.mUidState;
3084 if (Arrays.binarySearch(mDeviceIdleWhitelist, appid) < 0 &&
3085 Arrays.binarySearch(mDeviceIdleTempWhitelist, appid) < 0 &&
3086 state.mProcState != ActivityManager.PROCESS_STATE_NONEXISTENT &&
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -08003087 state.mProcState >
3088 ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003089 disabled = true;
3090 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003091 }
3092 }
Ruchi Kandoi9c36c022016-04-20 13:42:01 -07003093 if (wakeLock.mDisabled != disabled) {
3094 wakeLock.mDisabled = disabled;
3095 return true;
3096 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003097 }
3098 return false;
3099 }
3100
Jeff Brown96307042012-07-27 15:51:34 -07003101 private boolean isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() {
3102 return mMaximumScreenOffTimeoutFromDeviceAdmin >= 0
Pavel Grafov28939982017-10-03 15:11:52 +01003103 && mMaximumScreenOffTimeoutFromDeviceAdmin < Long.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003105
Jeff Brown96307042012-07-27 15:51:34 -07003106 private void setAttentionLightInternal(boolean on, int color) {
Adam Lesinski182f73f2013-12-05 16:48:06 -08003107 Light light;
Jeff Brown96307042012-07-27 15:51:34 -07003108 synchronized (mLock) {
3109 if (!mSystemReady) {
3110 return;
3111 }
3112 light = mAttentionLight;
Mike Lockwood36fc3022009-08-25 16:49:06 -07003113 }
Jeff Brown96307042012-07-27 15:51:34 -07003114
3115 // Control light outside of lock.
Adam Lesinski182f73f2013-12-05 16:48:06 -08003116 light.setFlashing(color, Light.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Jeff Brown96307042012-07-27 15:51:34 -07003117 }
3118
Lucas Dupin16cfe452018-02-08 13:14:50 -08003119 private void setDozeAfterScreenOffInternal(boolean on) {
3120 synchronized (mLock) {
3121 mDozeAfterScreenOff = on;
3122 }
3123 }
3124
Jeff Browne333e672014-10-28 13:48:55 -07003125 private void boostScreenBrightnessInternal(long eventTime, int uid) {
3126 synchronized (mLock) {
3127 if (!mSystemReady || mWakefulness == WAKEFULNESS_ASLEEP
3128 || eventTime < mLastScreenBrightnessBoostTime) {
3129 return;
3130 }
3131
3132 Slog.i(TAG, "Brightness boost activated (uid " + uid +")...");
3133 mLastScreenBrightnessBoostTime = eventTime;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07003134 if (!mScreenBrightnessBoostInProgress) {
3135 mScreenBrightnessBoostInProgress = true;
3136 mNotifier.onScreenBrightnessBoostChanged();
3137 }
Jeff Browne333e672014-10-28 13:48:55 -07003138 mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST;
3139
3140 userActivityNoUpdateLocked(eventTime,
3141 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, uid);
3142 updatePowerStateLocked();
3143 }
3144 }
3145
Bryce Lee84d6c0f2015-03-17 10:43:08 -07003146 private boolean isScreenBrightnessBoostedInternal() {
3147 synchronized (mLock) {
3148 return mScreenBrightnessBoostInProgress;
3149 }
3150 }
3151
Jeff Browne333e672014-10-28 13:48:55 -07003152 /**
3153 * Called when a screen brightness boost timeout has occurred.
3154 *
3155 * This function must have no other side-effects besides setting the dirty
3156 * bit and calling update power state.
3157 */
3158 private void handleScreenBrightnessBoostTimeout() { // runs on handler thread
3159 synchronized (mLock) {
3160 if (DEBUG_SPEW) {
3161 Slog.d(TAG, "handleScreenBrightnessBoostTimeout");
3162 }
3163
3164 mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST;
3165 updatePowerStateLocked();
3166 }
3167 }
3168
Jeff Brown96307042012-07-27 15:51:34 -07003169 private void setScreenBrightnessOverrideFromWindowManagerInternal(int brightness) {
3170 synchronized (mLock) {
3171 if (mScreenBrightnessOverrideFromWindowManager != brightness) {
3172 mScreenBrightnessOverrideFromWindowManager = brightness;
3173 mDirty |= DIRTY_SETTINGS;
3174 updatePowerStateLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003175 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003176 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003177 }
3178
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07003179 private void setUserInactiveOverrideFromWindowManagerInternal() {
3180 synchronized (mLock) {
3181 mUserInactiveOverrideFromWindowManager = true;
3182 mDirty |= DIRTY_USER_ACTIVITY;
3183 updatePowerStateLocked();
3184 }
3185 }
3186
Jeff Brown1e3b98d2012-09-30 18:58:59 -07003187 private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) {
3188 synchronized (mLock) {
3189 if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) {
3190 mUserActivityTimeoutOverrideFromWindowManager = timeoutMillis;
Michael Wrightb55e3a12018-03-06 15:14:06 +00003191 EventLogTags.writeUserActivityTimeoutOverride(timeoutMillis);
Jeff Brown1e3b98d2012-09-30 18:58:59 -07003192 mDirty |= DIRTY_SETTINGS;
3193 updatePowerStateLocked();
3194 }
3195 }
3196 }
3197
Jeff Brown970d4132014-07-19 11:33:47 -07003198 private void setDozeOverrideFromDreamManagerInternal(
3199 int screenState, int screenBrightness) {
3200 synchronized (mLock) {
3201 if (mDozeScreenStateOverrideFromDreamManager != screenState
3202 || mDozeScreenBrightnessOverrideFromDreamManager != screenBrightness) {
3203 mDozeScreenStateOverrideFromDreamManager = screenState;
3204 mDozeScreenBrightnessOverrideFromDreamManager = screenBrightness;
3205 mDirty |= DIRTY_SETTINGS;
3206 updatePowerStateLocked();
3207 }
3208 }
3209 }
3210
Ivan Podogov56bc6d02018-02-26 16:04:24 +00003211 private void setDrawWakeLockOverrideFromSidekickInternal(boolean keepState) {
3212 synchronized (mLock) {
3213 if (mDrawWakeLockOverrideFromSidekick != keepState) {
3214 mDrawWakeLockOverrideFromSidekick = keepState;
3215 mDirty |= DIRTY_SETTINGS;
3216 updatePowerStateLocked();
3217 }
3218 }
3219 }
3220
Santos Cordon21e9f2b2017-09-13 11:59:39 -07003221 @VisibleForTesting
3222 void setVrModeEnabled(boolean enabled) {
3223 mIsVrModeEnabled = enabled;
3224 }
3225
Makoto Onukia7d8c4d2017-11-20 15:20:16 -08003226 private void powerHintInternal(int hintId, int data) {
3227 // Maybe filter the event.
3228 switch (hintId) {
3229 case PowerHint.LAUNCH: // 1: activate launch boost 0: deactivate.
3230 if (data == 1 && mBatterySaverController.isLaunchBoostDisabled()) {
3231 return;
3232 }
3233 break;
3234 }
3235
Santos Cordon64a6e612018-08-22 19:27:04 +01003236 mNativeWrapper.nativeSendPowerHint(hintId, data);
Ruchi Kandoi15aedf52014-05-09 19:57:51 -07003237 }
3238
Alex Kershaw2418ea92018-10-19 17:17:49 +01003239 @VisibleForTesting
3240 boolean wasDeviceIdleForInternal(long ms) {
3241 synchronized (mLock) {
3242 return mLastUserActivityTime + ms < SystemClock.uptimeMillis();
3243 }
3244 }
3245
3246 @VisibleForTesting
3247 void onUserActivity() {
3248 synchronized (mLock) {
3249 mLastUserActivityTime = SystemClock.uptimeMillis();
3250 }
3251 }
3252
Santos Cordon12f92eb2019-02-01 21:28:47 +00003253 private boolean forceSuspendInternal(int uid) {
3254 try {
3255 synchronized (mLock) {
3256 mForceSuspendActive = true;
3257 // Place the system in an non-interactive state
3258 goToSleepInternal(SystemClock.uptimeMillis(),
3259 PowerManager.GO_TO_SLEEP_REASON_FORCE_SUSPEND,
3260 PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE, uid);
3261
3262 // Disable all the partial wake locks as well
3263 updateWakeLockDisabledStatesLocked();
3264 }
3265
3266 Slog.i(TAG, "Force-Suspending (uid " + uid + ")...");
3267 boolean success = mNativeWrapper.nativeForceSuspend();
3268 if (!success) {
3269 Slog.i(TAG, "Force-Suspending failed in native.");
3270 }
3271 return success;
3272 } finally {
3273 synchronized (mLock) {
3274 mForceSuspendActive = false;
3275 // Re-enable wake locks once again.
3276 updateWakeLockDisabledStatesLocked();
3277 }
3278 }
3279 }
3280
Jeff Brown96307042012-07-27 15:51:34 -07003281 /**
3282 * Low-level function turn the device off immediately, without trying
3283 * to be clean. Most people should use {@link ShutdownThread} for a clean shutdown.
Yusuke Sato705ffd12015-07-21 15:52:11 -07003284 *
3285 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Jeff Brown96307042012-07-27 15:51:34 -07003286 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07003287 public static void lowLevelShutdown(String reason) {
3288 if (reason == null) {
3289 reason = "";
3290 }
3291 SystemProperties.set("sys.powerctl", "shutdown," + reason);
Jeff Brown96307042012-07-27 15:51:34 -07003292 }
3293
3294 /**
Doug Zongker3b0218b2014-01-14 12:29:06 -08003295 * Low-level function to reboot the device. On success, this
3296 * function doesn't return. If more than 20 seconds passes from
Tao Bao90237f72015-05-21 16:25:19 -07003297 * the time a reboot is requested, this method returns.
Jeff Brown96307042012-07-27 15:51:34 -07003298 *
3299 * @param reason code to pass to the kernel (e.g. "recovery"), or null.
Jeff Brown96307042012-07-27 15:51:34 -07003300 */
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07003301 public static void lowLevelReboot(String reason) {
3302 if (reason == null) {
3303 reason = "";
3304 }
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08003305
3306 // If the reason is "quiescent", it means that the boot process should proceed
3307 // without turning on the screen/lights.
3308 // The "quiescent" property is sticky, meaning that any number
3309 // of subsequent reboots should honor the property until it is reset.
3310 if (reason.equals(PowerManager.REBOOT_QUIESCENT)) {
3311 sQuiescent = true;
3312 reason = "";
Dmitri Plotnikov690c6bd2017-05-10 16:26:38 -07003313 } else if (reason.endsWith("," + PowerManager.REBOOT_QUIESCENT)) {
3314 sQuiescent = true;
3315 reason = reason.substring(0,
3316 reason.length() - PowerManager.REBOOT_QUIESCENT.length() - 1);
3317 }
3318
3319 if (reason.equals(PowerManager.REBOOT_RECOVERY)
3320 || reason.equals(PowerManager.REBOOT_RECOVERY_UPDATE)) {
3321 reason = "recovery";
Dmitri Plotnikova8d2c642016-12-08 10:49:24 -08003322 }
3323
3324 if (sQuiescent) {
3325 // Pass the optional "quiescent" argument to the bootloader to let it know
3326 // that it should not turn the screen/lights on.
3327 reason = reason + ",quiescent";
3328 }
3329
3330 SystemProperties.set("sys.powerctl", "reboot," + reason);
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07003331 try {
Tao Bao90237f72015-05-21 16:25:19 -07003332 Thread.sleep(20 * 1000L);
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07003333 } catch (InterruptedException e) {
3334 Thread.currentThread().interrupt();
3335 }
Tao Bao90237f72015-05-21 16:25:19 -07003336 Slog.wtf(TAG, "Unexpected return from lowLevelReboot!");
Jeff Brown96307042012-07-27 15:51:34 -07003337 }
3338
3339 @Override // Watchdog.Monitor implementation
3340 public void monitor() {
3341 // Grab and release lock for watchdog monitor to detect deadlocks.
3342 synchronized (mLock) {
Mike Lockwood20f87d72009-11-05 16:08:51 -05003343 }
Jeff Brown96307042012-07-27 15:51:34 -07003344 }
3345
Jeff Brown6f357d32014-01-15 20:40:55 -08003346 private void dumpInternal(PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07003347 pw.println("POWER MANAGER (dumpsys power)\n");
3348
Jeff Brown3b971592013-01-09 18:46:37 -08003349 final WirelessChargerDetector wcd;
Jeff Brown96307042012-07-27 15:51:34 -07003350 synchronized (mLock) {
3351 pw.println("Power Manager State:");
Dianne Hackborn0ef403e2017-01-24 18:22:15 -08003352 mConstants.dump(pw);
Jeff Brown96307042012-07-27 15:51:34 -07003353 pw.println(" mDirty=0x" + Integer.toHexString(mDirty));
Jeff Brownfbe96702014-11-19 18:30:58 -08003354 pw.println(" mWakefulness=" + PowerManagerInternal.wakefulnessToString(mWakefulness));
3355 pw.println(" mWakefulnessChanging=" + mWakefulnessChanging);
Jeff Brown96307042012-07-27 15:51:34 -07003356 pw.println(" mIsPowered=" + mIsPowered);
Jeff Brownf3fb8952012-10-02 20:57:05 -07003357 pw.println(" mPlugType=" + mPlugType);
Jeff Brown016ff142012-10-15 16:47:22 -07003358 pw.println(" mBatteryLevel=" + mBatteryLevel);
3359 pw.println(" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted);
Jeff Brownec6aa592012-10-17 20:30:25 -07003360 pw.println(" mDockState=" + mDockState);
Jeff Brown96307042012-07-27 15:51:34 -07003361 pw.println(" mStayOn=" + mStayOn);
Jeff Brown93cbbb22012-10-04 13:18:36 -07003362 pw.println(" mProximityPositive=" + mProximityPositive);
Jeff Brown96307042012-07-27 15:51:34 -07003363 pw.println(" mBootCompleted=" + mBootCompleted);
3364 pw.println(" mSystemReady=" + mSystemReady);
Jeff Brown037c33e2014-04-09 00:31:55 -07003365 pw.println(" mHalAutoSuspendModeEnabled=" + mHalAutoSuspendModeEnabled);
3366 pw.println(" mHalInteractiveModeEnabled=" + mHalInteractiveModeEnabled);
Jeff Brown96307042012-07-27 15:51:34 -07003367 pw.println(" mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003368 pw.print(" mNotifyLongScheduled=");
3369 if (mNotifyLongScheduled == 0) {
3370 pw.print("(none)");
3371 } else {
3372 TimeUtils.formatDuration(mNotifyLongScheduled, SystemClock.uptimeMillis(), pw);
3373 }
3374 pw.println();
3375 pw.print(" mNotifyLongDispatched=");
3376 if (mNotifyLongDispatched == 0) {
3377 pw.print("(none)");
3378 } else {
3379 TimeUtils.formatDuration(mNotifyLongDispatched, SystemClock.uptimeMillis(), pw);
3380 }
3381 pw.println();
3382 pw.print(" mNotifyLongNextCheck=");
3383 if (mNotifyLongNextCheck == 0) {
3384 pw.print("(none)");
3385 } else {
3386 TimeUtils.formatDuration(mNotifyLongNextCheck, SystemClock.uptimeMillis(), pw);
3387 }
3388 pw.println();
Jeff Brown96307042012-07-27 15:51:34 -07003389 pw.println(" mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary));
3390 pw.println(" mRequestWaitForNegativeProximity=" + mRequestWaitForNegativeProximity);
3391 pw.println(" mSandmanScheduled=" + mSandmanScheduled);
Jeff Brown26875502014-01-30 21:47:47 -08003392 pw.println(" mSandmanSummoned=" + mSandmanSummoned);
Dianne Hackborn14272302014-06-10 23:13:02 -07003393 pw.println(" mBatteryLevelLow=" + mBatteryLevelLow);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003394 pw.println(" mLightDeviceIdleMode=" + mLightDeviceIdleMode);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003395 pw.println(" mDeviceIdleMode=" + mDeviceIdleMode);
3396 pw.println(" mDeviceIdleWhitelist=" + Arrays.toString(mDeviceIdleWhitelist));
Amith Yamasaniaf575b92015-05-29 15:35:26 -07003397 pw.println(" mDeviceIdleTempWhitelist=" + Arrays.toString(mDeviceIdleTempWhitelist));
Jeff Brown96307042012-07-27 15:51:34 -07003398 pw.println(" mLastWakeTime=" + TimeUtils.formatUptime(mLastWakeTime));
3399 pw.println(" mLastSleepTime=" + TimeUtils.formatUptime(mLastSleepTime));
Calin Tatarua3805722018-08-09 16:41:28 +02003400 pw.println(" mLastSleepReason=" + PowerManager.sleepReasonToString(mLastSleepReason));
Jeff Brown96307042012-07-27 15:51:34 -07003401 pw.println(" mLastUserActivityTime=" + TimeUtils.formatUptime(mLastUserActivityTime));
3402 pw.println(" mLastUserActivityTimeNoChangeLights="
3403 + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights));
Jeff Brown0a571122014-08-21 21:50:43 -07003404 pw.println(" mLastInteractivePowerHintTime="
3405 + TimeUtils.formatUptime(mLastInteractivePowerHintTime));
Jeff Browne333e672014-10-28 13:48:55 -07003406 pw.println(" mLastScreenBrightnessBoostTime="
3407 + TimeUtils.formatUptime(mLastScreenBrightnessBoostTime));
3408 pw.println(" mScreenBrightnessBoostInProgress="
3409 + mScreenBrightnessBoostInProgress);
Jeff Brown96307042012-07-27 15:51:34 -07003410 pw.println(" mDisplayReady=" + mDisplayReady);
3411 pw.println(" mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker);
Jeff Brown27f7a862012-12-12 15:43:31 -08003412 pw.println(" mHoldingDisplaySuspendBlocker=" + mHoldingDisplaySuspendBlocker);
Jeff Brown96307042012-07-27 15:51:34 -07003413
3414 pw.println();
3415 pw.println("Settings and Configuration:");
Jeff Brown037c33e2014-04-09 00:31:55 -07003416 pw.println(" mDecoupleHalAutoSuspendModeFromDisplayConfig="
3417 + mDecoupleHalAutoSuspendModeFromDisplayConfig);
3418 pw.println(" mDecoupleHalInteractiveModeFromDisplayConfig="
3419 + mDecoupleHalInteractiveModeFromDisplayConfig);
Jeff Brownec083212013-09-11 20:45:25 -07003420 pw.println(" mWakeUpWhenPluggedOrUnpluggedConfig="
3421 + mWakeUpWhenPluggedOrUnpluggedConfig);
Bryce Lee584a4452014-10-21 15:55:55 -07003422 pw.println(" mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig="
3423 + mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig);
3424 pw.println(" mTheaterModeEnabled="
3425 + mTheaterModeEnabled);
Jeff Brownec083212013-09-11 20:45:25 -07003426 pw.println(" mSuspendWhenScreenOffDueToProximityConfig="
3427 + mSuspendWhenScreenOffDueToProximityConfig);
Jeff Brown96307042012-07-27 15:51:34 -07003428 pw.println(" mDreamsSupportedConfig=" + mDreamsSupportedConfig);
Jeff Brownec083212013-09-11 20:45:25 -07003429 pw.println(" mDreamsEnabledByDefaultConfig=" + mDreamsEnabledByDefaultConfig);
3430 pw.println(" mDreamsActivatedOnSleepByDefaultConfig="
3431 + mDreamsActivatedOnSleepByDefaultConfig);
3432 pw.println(" mDreamsActivatedOnDockByDefaultConfig="
3433 + mDreamsActivatedOnDockByDefaultConfig);
Jeff Brown26875502014-01-30 21:47:47 -08003434 pw.println(" mDreamsEnabledOnBatteryConfig="
3435 + mDreamsEnabledOnBatteryConfig);
3436 pw.println(" mDreamsBatteryLevelMinimumWhenPoweredConfig="
3437 + mDreamsBatteryLevelMinimumWhenPoweredConfig);
3438 pw.println(" mDreamsBatteryLevelMinimumWhenNotPoweredConfig="
3439 + mDreamsBatteryLevelMinimumWhenNotPoweredConfig);
3440 pw.println(" mDreamsBatteryLevelDrainCutoffConfig="
3441 + mDreamsBatteryLevelDrainCutoffConfig);
Jeff Brown96307042012-07-27 15:51:34 -07003442 pw.println(" mDreamsEnabledSetting=" + mDreamsEnabledSetting);
John Spurlock1a868b72012-08-22 09:56:51 -04003443 pw.println(" mDreamsActivateOnSleepSetting=" + mDreamsActivateOnSleepSetting);
Jeff Brownec6aa592012-10-17 20:30:25 -07003444 pw.println(" mDreamsActivateOnDockSetting=" + mDreamsActivateOnDockSetting);
Lucas Dupin16cfe452018-02-08 13:14:50 -08003445 pw.println(" mDozeAfterScreenOff=" + mDozeAfterScreenOff);
Jeff Brown27736f52014-05-20 17:17:10 -07003446 pw.println(" mMinimumScreenOffTimeoutConfig=" + mMinimumScreenOffTimeoutConfig);
3447 pw.println(" mMaximumScreenDimDurationConfig=" + mMaximumScreenDimDurationConfig);
3448 pw.println(" mMaximumScreenDimRatioConfig=" + mMaximumScreenDimRatioConfig);
Jeff Brown96307042012-07-27 15:51:34 -07003449 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting);
Jeff Brown05af6ad2014-09-30 20:54:30 -07003450 pw.println(" mSleepTimeoutSetting=" + mSleepTimeoutSetting);
Jeff Brown96307042012-07-27 15:51:34 -07003451 pw.println(" mMaximumScreenOffTimeoutFromDeviceAdmin="
3452 + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced="
3453 + isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() + ")");
3454 pw.println(" mStayOnWhilePluggedInSetting=" + mStayOnWhilePluggedInSetting);
3455 pw.println(" mScreenBrightnessSetting=" + mScreenBrightnessSetting);
3456 pw.println(" mScreenBrightnessModeSetting=" + mScreenBrightnessModeSetting);
3457 pw.println(" mScreenBrightnessOverrideFromWindowManager="
3458 + mScreenBrightnessOverrideFromWindowManager);
Jeff Brown1e3b98d2012-09-30 18:58:59 -07003459 pw.println(" mUserActivityTimeoutOverrideFromWindowManager="
3460 + mUserActivityTimeoutOverrideFromWindowManager);
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07003461 pw.println(" mUserInactiveOverrideFromWindowManager="
3462 + mUserInactiveOverrideFromWindowManager);
Jeff Brown970d4132014-07-19 11:33:47 -07003463 pw.println(" mDozeScreenStateOverrideFromDreamManager="
3464 + mDozeScreenStateOverrideFromDreamManager);
Ivan Podogov56bc6d02018-02-26 16:04:24 +00003465 pw.println(" mDrawWakeLockOverrideFromSidekick=" + mDrawWakeLockOverrideFromSidekick);
Jeff Brown970d4132014-07-19 11:33:47 -07003466 pw.println(" mDozeScreenBrightnessOverrideFromDreamManager="
3467 + mDozeScreenBrightnessOverrideFromDreamManager);
Jeff Brown96307042012-07-27 15:51:34 -07003468 pw.println(" mScreenBrightnessSettingMinimum=" + mScreenBrightnessSettingMinimum);
3469 pw.println(" mScreenBrightnessSettingMaximum=" + mScreenBrightnessSettingMaximum);
3470 pw.println(" mScreenBrightnessSettingDefault=" + mScreenBrightnessSettingDefault);
Jason Monk27bbb2d2015-03-31 16:46:39 -04003471 pw.println(" mDoubleTapWakeEnabled=" + mDoubleTapWakeEnabled);
Santos Cordon3107d292016-09-20 15:50:35 -07003472 pw.println(" mIsVrModeEnabled=" + mIsVrModeEnabled);
Pavel Grafov28939982017-10-03 15:11:52 +01003473 pw.println(" mForegroundProfile=" + mForegroundProfile);
Jeff Brown96307042012-07-27 15:51:34 -07003474
Pavel Grafov28939982017-10-03 15:11:52 +01003475 final long sleepTimeout = getSleepTimeoutLocked();
3476 final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
3477 final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
Jeff Brownff532542012-10-02 21:18:04 -07003478 pw.println();
Jeff Brown05af6ad2014-09-30 20:54:30 -07003479 pw.println("Sleep timeout: " + sleepTimeout + " ms");
Jeff Brownff532542012-10-02 21:18:04 -07003480 pw.println("Screen off timeout: " + screenOffTimeout + " ms");
3481 pw.println("Screen dim duration: " + screenDimDuration + " ms");
3482
Jeff Brown96307042012-07-27 15:51:34 -07003483 pw.println();
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003484 pw.print("UID states (changing=");
3485 pw.print(mUidsChanging);
3486 pw.print(" changed=");
3487 pw.print(mUidsChanged);
3488 pw.println("):");
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003489 for (int i=0; i<mUidState.size(); i++) {
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003490 final UidState state = mUidState.valueAt(i);
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003491 pw.print(" UID "); UserHandle.formatUid(pw, mUidState.keyAt(i));
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003492 pw.print(": ");
3493 if (state.mActive) pw.print(" ACTIVE ");
3494 else pw.print("INACTIVE ");
3495 pw.print(" count=");
3496 pw.print(state.mNumWakeLocks);
3497 pw.print(" state=");
3498 pw.println(state.mProcState);
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003499 }
3500
3501 pw.println();
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003502 pw.println("Looper state:");
3503 mHandler.getLooper().dump(new PrintWriterPrinter(pw), " ");
3504
3505 pw.println();
Jeff Brown96307042012-07-27 15:51:34 -07003506 pw.println("Wake Locks: size=" + mWakeLocks.size());
3507 for (WakeLock wl : mWakeLocks) {
3508 pw.println(" " + wl);
Joe Onorato8274a0e2010-10-05 17:38:09 -04003509 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003510
Jeff Brown96307042012-07-27 15:51:34 -07003511 pw.println();
3512 pw.println("Suspend Blockers: size=" + mSuspendBlockers.size());
3513 for (SuspendBlocker sb : mSuspendBlockers) {
3514 pw.println(" " + sb);
3515 }
3516
Jeff Brownc38c9be2012-10-04 13:16:19 -07003517 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07003518 pw.println("Display Power: " + mDisplayPowerCallbacks);
Jeff Brown9e316a12012-10-08 19:17:06 -07003519
jackqdyulei455e90a2017-02-09 15:29:16 -08003520 mBatterySaverPolicy.dump(pw);
Makoto Onukia3cd7b92018-03-19 14:47:05 -07003521 mBatterySaverStateMachine.dump(pw);
Lucas Dupin0a5d7972019-01-16 18:52:30 -08003522 mAttentionDetector.dump(pw);
jackqdyulei455e90a2017-02-09 15:29:16 -08003523
Pavel Grafov28939982017-10-03 15:11:52 +01003524 pw.println();
3525 final int numProfiles = mProfilePowerState.size();
3526 pw.println("Profile power states: size=" + numProfiles);
3527 for (int i = 0; i < numProfiles; i++) {
3528 final ProfilePowerState profile = mProfilePowerState.valueAt(i);
3529 pw.print(" mUserId=");
3530 pw.print(profile.mUserId);
3531 pw.print(" mScreenOffTimeout=");
3532 pw.print(profile.mScreenOffTimeout);
3533 pw.print(" mWakeLockSummary=");
3534 pw.print(profile.mWakeLockSummary);
3535 pw.print(" mLastUserActivityTime=");
3536 pw.print(profile.mLastUserActivityTime);
3537 pw.print(" mLockingNotified=");
3538 pw.println(profile.mLockingNotified);
3539 }
3540
Jeff Brown3b971592013-01-09 18:46:37 -08003541 wcd = mWirelessChargerDetector;
Jeff Brown96307042012-07-27 15:51:34 -07003542 }
3543
Jeff Brown3b971592013-01-09 18:46:37 -08003544 if (wcd != null) {
3545 wcd.dump(pw);
3546 }
Jeff Brown96307042012-07-27 15:51:34 -07003547 }
3548
Netta P958d0a52017-02-07 11:20:55 -08003549 private void dumpProto(FileDescriptor fd) {
3550 final WirelessChargerDetector wcd;
3551 final ProtoOutputStream proto = new ProtoOutputStream(fd);
3552
3553 synchronized (mLock) {
3554 mConstants.dumpProto(proto);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003555 proto.write(PowerManagerServiceDumpProto.DIRTY, mDirty);
3556 proto.write(PowerManagerServiceDumpProto.WAKEFULNESS, mWakefulness);
3557 proto.write(PowerManagerServiceDumpProto.IS_WAKEFULNESS_CHANGING, mWakefulnessChanging);
3558 proto.write(PowerManagerServiceDumpProto.IS_POWERED, mIsPowered);
3559 proto.write(PowerManagerServiceDumpProto.PLUG_TYPE, mPlugType);
3560 proto.write(PowerManagerServiceDumpProto.BATTERY_LEVEL, mBatteryLevel);
Netta P958d0a52017-02-07 11:20:55 -08003561 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003562 PowerManagerServiceDumpProto.BATTERY_LEVEL_WHEN_DREAM_STARTED,
Netta P958d0a52017-02-07 11:20:55 -08003563 mBatteryLevelWhenDreamStarted);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003564 proto.write(PowerManagerServiceDumpProto.DOCK_STATE, mDockState);
3565 proto.write(PowerManagerServiceDumpProto.IS_STAY_ON, mStayOn);
3566 proto.write(PowerManagerServiceDumpProto.IS_PROXIMITY_POSITIVE, mProximityPositive);
3567 proto.write(PowerManagerServiceDumpProto.IS_BOOT_COMPLETED, mBootCompleted);
3568 proto.write(PowerManagerServiceDumpProto.IS_SYSTEM_READY, mSystemReady);
Netta P958d0a52017-02-07 11:20:55 -08003569 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003570 PowerManagerServiceDumpProto.IS_HAL_AUTO_SUSPEND_MODE_ENABLED,
Netta P958d0a52017-02-07 11:20:55 -08003571 mHalAutoSuspendModeEnabled);
3572 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003573 PowerManagerServiceDumpProto.IS_HAL_AUTO_INTERACTIVE_MODE_ENABLED,
Netta P958d0a52017-02-07 11:20:55 -08003574 mHalInteractiveModeEnabled);
3575
Kweku Adamse6b00c22017-10-23 16:46:45 -07003576 final long activeWakeLocksToken = proto.start(PowerManagerServiceDumpProto.ACTIVE_WAKE_LOCKS);
Netta P958d0a52017-02-07 11:20:55 -08003577 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003578 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_CPU,
Netta P958d0a52017-02-07 11:20:55 -08003579 (mWakeLockSummary & WAKE_LOCK_CPU) != 0);
3580 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003581 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_SCREEN_BRIGHT,
Netta P958d0a52017-02-07 11:20:55 -08003582 (mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0);
3583 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003584 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_SCREEN_DIM,
Netta P958d0a52017-02-07 11:20:55 -08003585 (mWakeLockSummary & WAKE_LOCK_SCREEN_DIM) != 0);
3586 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003587 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_BUTTON_BRIGHT,
Netta P958d0a52017-02-07 11:20:55 -08003588 (mWakeLockSummary & WAKE_LOCK_BUTTON_BRIGHT) != 0);
3589 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003590 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_PROXIMITY_SCREEN_OFF,
Netta P958d0a52017-02-07 11:20:55 -08003591 (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0);
3592 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003593 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_STAY_AWAKE,
Netta P958d0a52017-02-07 11:20:55 -08003594 (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0);
3595 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003596 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_DOZE,
Netta P958d0a52017-02-07 11:20:55 -08003597 (mWakeLockSummary & WAKE_LOCK_DOZE) != 0);
3598 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003599 PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_DRAW,
Netta P958d0a52017-02-07 11:20:55 -08003600 (mWakeLockSummary & WAKE_LOCK_DRAW) != 0);
3601 proto.end(activeWakeLocksToken);
3602
Kweku Adamse6b00c22017-10-23 16:46:45 -07003603 proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_SCHEDULED_MS, mNotifyLongScheduled);
3604 proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_DISPATCHED_MS, mNotifyLongDispatched);
3605 proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_NEXT_CHECK_MS, mNotifyLongNextCheck);
Netta P958d0a52017-02-07 11:20:55 -08003606
Kweku Adamse6b00c22017-10-23 16:46:45 -07003607 final long userActivityToken = proto.start(PowerManagerServiceDumpProto.USER_ACTIVITY);
Netta P958d0a52017-02-07 11:20:55 -08003608 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003609 PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_BRIGHT,
Netta P958d0a52017-02-07 11:20:55 -08003610 (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0);
3611 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003612 PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_DIM,
Netta P958d0a52017-02-07 11:20:55 -08003613 (mUserActivitySummary & USER_ACTIVITY_SCREEN_DIM) != 0);
3614 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003615 PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_DREAM,
Netta P958d0a52017-02-07 11:20:55 -08003616 (mUserActivitySummary & USER_ACTIVITY_SCREEN_DREAM) != 0);
3617 proto.end(userActivityToken);
3618
3619 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003620 PowerManagerServiceDumpProto.IS_REQUEST_WAIT_FOR_NEGATIVE_PROXIMITY,
Netta P958d0a52017-02-07 11:20:55 -08003621 mRequestWaitForNegativeProximity);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003622 proto.write(PowerManagerServiceDumpProto.IS_SANDMAN_SCHEDULED, mSandmanScheduled);
3623 proto.write(PowerManagerServiceDumpProto.IS_SANDMAN_SUMMONED, mSandmanSummoned);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003624 proto.write(PowerManagerServiceDumpProto.IS_BATTERY_LEVEL_LOW, mBatteryLevelLow);
3625 proto.write(PowerManagerServiceDumpProto.IS_LIGHT_DEVICE_IDLE_MODE, mLightDeviceIdleMode);
3626 proto.write(PowerManagerServiceDumpProto.IS_DEVICE_IDLE_MODE, mDeviceIdleMode);
Netta P958d0a52017-02-07 11:20:55 -08003627
3628 for (int id : mDeviceIdleWhitelist) {
Kweku Adamse6b00c22017-10-23 16:46:45 -07003629 proto.write(PowerManagerServiceDumpProto.DEVICE_IDLE_WHITELIST, id);
Netta P958d0a52017-02-07 11:20:55 -08003630 }
3631 for (int id : mDeviceIdleTempWhitelist) {
Kweku Adamse6b00c22017-10-23 16:46:45 -07003632 proto.write(PowerManagerServiceDumpProto.DEVICE_IDLE_TEMP_WHITELIST, id);
Netta P958d0a52017-02-07 11:20:55 -08003633 }
3634
Kweku Adamse6b00c22017-10-23 16:46:45 -07003635 proto.write(PowerManagerServiceDumpProto.LAST_WAKE_TIME_MS, mLastWakeTime);
3636 proto.write(PowerManagerServiceDumpProto.LAST_SLEEP_TIME_MS, mLastSleepTime);
3637 proto.write(PowerManagerServiceDumpProto.LAST_USER_ACTIVITY_TIME_MS, mLastUserActivityTime);
Netta P958d0a52017-02-07 11:20:55 -08003638 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003639 PowerManagerServiceDumpProto.LAST_USER_ACTIVITY_TIME_NO_CHANGE_LIGHTS_MS,
Netta P958d0a52017-02-07 11:20:55 -08003640 mLastUserActivityTimeNoChangeLights);
3641 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003642 PowerManagerServiceDumpProto.LAST_INTERACTIVE_POWER_HINT_TIME_MS,
Netta P958d0a52017-02-07 11:20:55 -08003643 mLastInteractivePowerHintTime);
3644 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003645 PowerManagerServiceDumpProto.LAST_SCREEN_BRIGHTNESS_BOOST_TIME_MS,
Netta P958d0a52017-02-07 11:20:55 -08003646 mLastScreenBrightnessBoostTime);
3647 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003648 PowerManagerServiceDumpProto.IS_SCREEN_BRIGHTNESS_BOOST_IN_PROGRESS,
Netta P958d0a52017-02-07 11:20:55 -08003649 mScreenBrightnessBoostInProgress);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003650 proto.write(PowerManagerServiceDumpProto.IS_DISPLAY_READY, mDisplayReady);
Netta P958d0a52017-02-07 11:20:55 -08003651 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003652 PowerManagerServiceDumpProto.IS_HOLDING_WAKE_LOCK_SUSPEND_BLOCKER,
Netta P958d0a52017-02-07 11:20:55 -08003653 mHoldingWakeLockSuspendBlocker);
3654 proto.write(
Kweku Adamse6b00c22017-10-23 16:46:45 -07003655 PowerManagerServiceDumpProto.IS_HOLDING_DISPLAY_SUSPEND_BLOCKER,
Netta P958d0a52017-02-07 11:20:55 -08003656 mHoldingDisplaySuspendBlocker);
3657
3658 final long settingsAndConfigurationToken =
Kweku Adamse6b00c22017-10-23 16:46:45 -07003659 proto.start(PowerManagerServiceDumpProto.SETTINGS_AND_CONFIGURATION);
Netta P958d0a52017-02-07 11:20:55 -08003660 proto.write(
3661 PowerServiceSettingsAndConfigurationDumpProto
3662 .IS_DECOUPLE_HAL_AUTO_SUSPEND_MODE_FROM_DISPLAY_CONFIG,
3663 mDecoupleHalAutoSuspendModeFromDisplayConfig);
3664 proto.write(
3665 PowerServiceSettingsAndConfigurationDumpProto
3666 .IS_DECOUPLE_HAL_INTERACTIVE_MODE_FROM_DISPLAY_CONFIG,
3667 mDecoupleHalInteractiveModeFromDisplayConfig);
3668 proto.write(
3669 PowerServiceSettingsAndConfigurationDumpProto
3670 .IS_WAKE_UP_WHEN_PLUGGED_OR_UNPLUGGED_CONFIG,
3671 mWakeUpWhenPluggedOrUnpluggedConfig);
3672 proto.write(
3673 PowerServiceSettingsAndConfigurationDumpProto
3674 .IS_WAKE_UP_WHEN_PLUGGED_OR_UNPLUGGED_IN_THEATER_MODE_CONFIG,
3675 mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig);
3676 proto.write(
3677 PowerServiceSettingsAndConfigurationDumpProto.IS_THEATER_MODE_ENABLED,
3678 mTheaterModeEnabled);
3679 proto.write(
3680 PowerServiceSettingsAndConfigurationDumpProto
3681 .IS_SUSPEND_WHEN_SCREEN_OFF_DUE_TO_PROXIMITY_CONFIG,
3682 mSuspendWhenScreenOffDueToProximityConfig);
3683 proto.write(
3684 PowerServiceSettingsAndConfigurationDumpProto.ARE_DREAMS_SUPPORTED_CONFIG,
3685 mDreamsSupportedConfig);
3686 proto.write(
3687 PowerServiceSettingsAndConfigurationDumpProto
3688 .ARE_DREAMS_ENABLED_BY_DEFAULT_CONFIG,
3689 mDreamsEnabledByDefaultConfig);
3690 proto.write(
3691 PowerServiceSettingsAndConfigurationDumpProto
3692 .ARE_DREAMS_ACTIVATED_ON_SLEEP_BY_DEFAULT_CONFIG,
3693 mDreamsActivatedOnSleepByDefaultConfig);
3694 proto.write(
3695 PowerServiceSettingsAndConfigurationDumpProto
3696 .ARE_DREAMS_ACTIVATED_ON_DOCK_BY_DEFAULT_CONFIG,
3697 mDreamsActivatedOnDockByDefaultConfig);
3698 proto.write(
3699 PowerServiceSettingsAndConfigurationDumpProto
3700 .ARE_DREAMS_ENABLED_ON_BATTERY_CONFIG,
3701 mDreamsEnabledOnBatteryConfig);
3702 proto.write(
3703 PowerServiceSettingsAndConfigurationDumpProto
3704 .DREAMS_BATTERY_LEVEL_MINIMUM_WHEN_POWERED_CONFIG,
3705 mDreamsBatteryLevelMinimumWhenPoweredConfig);
3706 proto.write(
3707 PowerServiceSettingsAndConfigurationDumpProto
3708 .DREAMS_BATTERY_LEVEL_MINIMUM_WHEN_NOT_POWERED_CONFIG,
3709 mDreamsBatteryLevelMinimumWhenNotPoweredConfig);
3710 proto.write(
3711 PowerServiceSettingsAndConfigurationDumpProto
3712 .DREAMS_BATTERY_LEVEL_DRAIN_CUTOFF_CONFIG,
3713 mDreamsBatteryLevelDrainCutoffConfig);
3714 proto.write(
3715 PowerServiceSettingsAndConfigurationDumpProto.ARE_DREAMS_ENABLED_SETTING,
3716 mDreamsEnabledSetting);
3717 proto.write(
3718 PowerServiceSettingsAndConfigurationDumpProto
3719 .ARE_DREAMS_ACTIVATE_ON_SLEEP_SETTING,
3720 mDreamsActivateOnSleepSetting);
3721 proto.write(
3722 PowerServiceSettingsAndConfigurationDumpProto
3723 .ARE_DREAMS_ACTIVATE_ON_DOCK_SETTING,
3724 mDreamsActivateOnDockSetting);
3725 proto.write(
3726 PowerServiceSettingsAndConfigurationDumpProto.IS_DOZE_AFTER_SCREEN_OFF_CONFIG,
Lucas Dupin16cfe452018-02-08 13:14:50 -08003727 mDozeAfterScreenOff);
Netta P958d0a52017-02-07 11:20:55 -08003728 proto.write(
Netta P958d0a52017-02-07 11:20:55 -08003729 PowerServiceSettingsAndConfigurationDumpProto
3730 .MINIMUM_SCREEN_OFF_TIMEOUT_CONFIG_MS,
3731 mMinimumScreenOffTimeoutConfig);
3732 proto.write(
3733 PowerServiceSettingsAndConfigurationDumpProto
3734 .MAXIMUM_SCREEN_DIM_DURATION_CONFIG_MS,
3735 mMaximumScreenDimDurationConfig);
3736 proto.write(
3737 PowerServiceSettingsAndConfigurationDumpProto.MAXIMUM_SCREEN_DIM_RATIO_CONFIG,
3738 mMaximumScreenDimRatioConfig);
3739 proto.write(
3740 PowerServiceSettingsAndConfigurationDumpProto.SCREEN_OFF_TIMEOUT_SETTING_MS,
3741 mScreenOffTimeoutSetting);
3742 proto.write(
3743 PowerServiceSettingsAndConfigurationDumpProto.SLEEP_TIMEOUT_SETTING_MS,
3744 mSleepTimeoutSetting);
3745 proto.write(
3746 PowerServiceSettingsAndConfigurationDumpProto
3747 .MAXIMUM_SCREEN_OFF_TIMEOUT_FROM_DEVICE_ADMIN_MS,
Pavel Grafov28939982017-10-03 15:11:52 +01003748 // Clamp to int32
3749 Math.min(mMaximumScreenOffTimeoutFromDeviceAdmin, Integer.MAX_VALUE));
Netta P958d0a52017-02-07 11:20:55 -08003750 proto.write(
3751 PowerServiceSettingsAndConfigurationDumpProto
3752 .IS_MAXIMUM_SCREEN_OFF_TIMEOUT_FROM_DEVICE_ADMIN_ENFORCED_LOCKED,
3753 isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked());
3754
3755 final long stayOnWhilePluggedInToken =
3756 proto.start(
3757 PowerServiceSettingsAndConfigurationDumpProto.STAY_ON_WHILE_PLUGGED_IN);
3758 proto.write(
3759 PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto
3760 .IS_STAY_ON_WHILE_PLUGGED_IN_AC,
3761 ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_AC) != 0));
3762 proto.write(
3763 PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto
3764 .IS_STAY_ON_WHILE_PLUGGED_IN_USB,
3765 ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_USB) != 0));
3766 proto.write(
3767 PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto
3768 .IS_STAY_ON_WHILE_PLUGGED_IN_WIRELESS,
3769 ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_WIRELESS)
3770 != 0));
3771 proto.end(stayOnWhilePluggedInToken);
3772
3773 proto.write(
Netta P958d0a52017-02-07 11:20:55 -08003774 PowerServiceSettingsAndConfigurationDumpProto.SCREEN_BRIGHTNESS_MODE_SETTING,
3775 mScreenBrightnessModeSetting);
3776 proto.write(
3777 PowerServiceSettingsAndConfigurationDumpProto
3778 .SCREEN_BRIGHTNESS_OVERRIDE_FROM_WINDOW_MANAGER,
3779 mScreenBrightnessOverrideFromWindowManager);
3780 proto.write(
3781 PowerServiceSettingsAndConfigurationDumpProto
3782 .USER_ACTIVITY_TIMEOUT_OVERRIDE_FROM_WINDOW_MANAGER_MS,
3783 mUserActivityTimeoutOverrideFromWindowManager);
3784 proto.write(
3785 PowerServiceSettingsAndConfigurationDumpProto
3786 .IS_USER_INACTIVE_OVERRIDE_FROM_WINDOW_MANAGER,
3787 mUserInactiveOverrideFromWindowManager);
3788 proto.write(
3789 PowerServiceSettingsAndConfigurationDumpProto
Netta P958d0a52017-02-07 11:20:55 -08003790 .DOZE_SCREEN_STATE_OVERRIDE_FROM_DREAM_MANAGER,
3791 mDozeScreenStateOverrideFromDreamManager);
3792 proto.write(
3793 PowerServiceSettingsAndConfigurationDumpProto
Ivan Podogov56bc6d02018-02-26 16:04:24 +00003794 .DRAW_WAKE_LOCK_OVERRIDE_FROM_SIDEKICK,
3795 mDrawWakeLockOverrideFromSidekick);
3796 proto.write(
3797 PowerServiceSettingsAndConfigurationDumpProto
Netta P958d0a52017-02-07 11:20:55 -08003798 .DOZED_SCREEN_BRIGHTNESS_OVERRIDE_FROM_DREAM_MANAGER,
3799 mDozeScreenBrightnessOverrideFromDreamManager);
3800
3801 final long screenBrightnessSettingLimitsToken =
3802 proto.start(
3803 PowerServiceSettingsAndConfigurationDumpProto
3804 .SCREEN_BRIGHTNESS_SETTING_LIMITS);
3805 proto.write(
3806 PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto
3807 .SETTING_MINIMUM,
3808 mScreenBrightnessSettingMinimum);
3809 proto.write(
3810 PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto
3811 .SETTING_MAXIMUM,
3812 mScreenBrightnessSettingMaximum);
3813 proto.write(
3814 PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto
3815 .SETTING_DEFAULT,
3816 mScreenBrightnessSettingDefault);
Netta P958d0a52017-02-07 11:20:55 -08003817 proto.end(screenBrightnessSettingLimitsToken);
3818
3819 proto.write(
Netta P958d0a52017-02-07 11:20:55 -08003820 PowerServiceSettingsAndConfigurationDumpProto.IS_DOUBLE_TAP_WAKE_ENABLED,
3821 mDoubleTapWakeEnabled);
3822 proto.write(
3823 PowerServiceSettingsAndConfigurationDumpProto.IS_VR_MODE_ENABLED,
3824 mIsVrModeEnabled);
3825 proto.end(settingsAndConfigurationToken);
3826
Pavel Grafov28939982017-10-03 15:11:52 +01003827 final long sleepTimeout = getSleepTimeoutLocked();
3828 final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
3829 final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
Kweku Adamse6b00c22017-10-23 16:46:45 -07003830 proto.write(PowerManagerServiceDumpProto.SLEEP_TIMEOUT_MS, sleepTimeout);
3831 proto.write(PowerManagerServiceDumpProto.SCREEN_OFF_TIMEOUT_MS, screenOffTimeout);
3832 proto.write(PowerManagerServiceDumpProto.SCREEN_DIM_DURATION_MS, screenDimDuration);
3833 proto.write(PowerManagerServiceDumpProto.ARE_UIDS_CHANGING, mUidsChanging);
3834 proto.write(PowerManagerServiceDumpProto.ARE_UIDS_CHANGED, mUidsChanged);
Netta P958d0a52017-02-07 11:20:55 -08003835
3836 for (int i = 0; i < mUidState.size(); i++) {
3837 final UidState state = mUidState.valueAt(i);
Kweku Adams32198522017-10-25 15:12:34 -07003838 final long uIDToken = proto.start(PowerManagerServiceDumpProto.UID_STATES);
Netta P958d0a52017-02-07 11:20:55 -08003839 final int uid = mUidState.keyAt(i);
Kweku Adams32198522017-10-25 15:12:34 -07003840 proto.write(PowerManagerServiceDumpProto.UidStateProto.UID, uid);
3841 proto.write(PowerManagerServiceDumpProto.UidStateProto.UID_STRING, UserHandle.formatUid(uid));
3842 proto.write(PowerManagerServiceDumpProto.UidStateProto.IS_ACTIVE, state.mActive);
3843 proto.write(PowerManagerServiceDumpProto.UidStateProto.NUM_WAKE_LOCKS, state.mNumWakeLocks);
Bookatzdb026a22018-01-10 19:01:56 -08003844 proto.write(PowerManagerServiceDumpProto.UidStateProto.PROCESS_STATE,
3845 ActivityManager.processStateAmToProto(state.mProcState));
Netta P958d0a52017-02-07 11:20:55 -08003846 proto.end(uIDToken);
3847 }
3848
Makoto Onukia3cd7b92018-03-19 14:47:05 -07003849 mBatterySaverStateMachine.dumpProto(proto,
3850 PowerManagerServiceDumpProto.BATTERY_SAVER_STATE_MACHINE);
3851
Kweku Adamse6b00c22017-10-23 16:46:45 -07003852 mHandler.getLooper().writeToProto(proto, PowerManagerServiceDumpProto.LOOPER);
Netta P958d0a52017-02-07 11:20:55 -08003853
3854 for (WakeLock wl : mWakeLocks) {
Kweku Adamse6b00c22017-10-23 16:46:45 -07003855 wl.writeToProto(proto, PowerManagerServiceDumpProto.WAKE_LOCKS);
Netta P958d0a52017-02-07 11:20:55 -08003856 }
3857
3858 for (SuspendBlocker sb : mSuspendBlockers) {
Kweku Adamse6b00c22017-10-23 16:46:45 -07003859 sb.writeToProto(proto, PowerManagerServiceDumpProto.SUSPEND_BLOCKERS);
Netta P958d0a52017-02-07 11:20:55 -08003860 }
3861 wcd = mWirelessChargerDetector;
3862 }
3863
3864 if (wcd != null) {
Kweku Adamse6b00c22017-10-23 16:46:45 -07003865 wcd.writeToProto(proto, PowerManagerServiceDumpProto.WIRELESS_CHARGER_DETECTOR);
Netta P958d0a52017-02-07 11:20:55 -08003866 }
3867 proto.flush();
3868 }
3869
Daichi Hirono82ab9802016-03-02 13:23:29 +09003870 private void incrementBootCount() {
3871 synchronized (mLock) {
3872 int count;
3873 try {
3874 count = Settings.Global.getInt(
3875 getContext().getContentResolver(), Settings.Global.BOOT_COUNT);
3876 } catch (SettingNotFoundException e) {
3877 count = 0;
3878 }
3879 Settings.Global.putInt(
3880 getContext().getContentResolver(), Settings.Global.BOOT_COUNT, count + 1);
3881 }
3882 }
3883
Jeff Brown96307042012-07-27 15:51:34 -07003884 private static WorkSource copyWorkSource(WorkSource workSource) {
3885 return workSource != null ? new WorkSource(workSource) : null;
3886 }
3887
Santos Cordon9b510a22018-08-24 16:42:54 +01003888 @VisibleForTesting
3889 final class BatteryReceiver extends BroadcastReceiver {
Jeff Brown96307042012-07-27 15:51:34 -07003890 @Override
3891 public void onReceive(Context context, Intent intent) {
3892 synchronized (mLock) {
3893 handleBatteryStateChangedLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003894 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003895 }
3896 }
3897
John Spurlockf4f6b4c2012-08-25 12:08:03 -04003898 private final class DreamReceiver extends BroadcastReceiver {
3899 @Override
3900 public void onReceive(Context context, Intent intent) {
3901 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -07003902 scheduleSandmanLocked();
John Spurlockf4f6b4c2012-08-25 12:08:03 -04003903 }
3904 }
3905 }
3906
Santos Cordon9b510a22018-08-24 16:42:54 +01003907 @VisibleForTesting
3908 final class UserSwitchedReceiver extends BroadcastReceiver {
Jeff Brownd4935962012-09-25 13:27:20 -07003909 @Override
3910 public void onReceive(Context context, Intent intent) {
3911 synchronized (mLock) {
3912 handleSettingsChangedLocked();
3913 }
3914 }
3915 }
3916
Jeff Brownec6aa592012-10-17 20:30:25 -07003917 private final class DockReceiver extends BroadcastReceiver {
3918 @Override
3919 public void onReceive(Context context, Intent intent) {
3920 synchronized (mLock) {
3921 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
3922 Intent.EXTRA_DOCK_STATE_UNDOCKED);
3923 if (mDockState != dockState) {
3924 mDockState = dockState;
3925 mDirty |= DIRTY_DOCK_STATE;
3926 updatePowerStateLocked();
3927 }
3928 }
3929 }
3930 }
3931
Jeff Brown96307042012-07-27 15:51:34 -07003932 private final class SettingsObserver extends ContentObserver {
3933 public SettingsObserver(Handler handler) {
3934 super(handler);
3935 }
3936
3937 @Override
3938 public void onChange(boolean selfChange, Uri uri) {
3939 synchronized (mLock) {
3940 handleSettingsChangedLocked();
3941 }
3942 }
3943 }
3944
Ruben Brunkc7be3be2016-04-01 17:07:51 -07003945 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
Ruchi Kandoi03a04282016-03-14 17:09:17 -07003946 @Override
3947 public void onVrStateChanged(boolean enabled) {
Ruchi Kandoi0d434042016-10-03 09:12:02 -07003948 powerHintInternal(PowerHint.VR_MODE, enabled ? 1 : 0);
Santos Cordon3107d292016-09-20 15:50:35 -07003949
3950 synchronized (mLock) {
3951 if (mIsVrModeEnabled != enabled) {
Santos Cordon21e9f2b2017-09-13 11:59:39 -07003952 setVrModeEnabled(enabled);
Santos Cordon3107d292016-09-20 15:50:35 -07003953 mDirty |= DIRTY_VR_MODE_CHANGED;
3954 updatePowerStateLocked();
3955 }
3956 }
Ruchi Kandoi03a04282016-03-14 17:09:17 -07003957 }
3958 };
3959
Jeff Brown96307042012-07-27 15:51:34 -07003960 /**
3961 * Handler for asynchronous operations performed by the power manager.
3962 */
3963 private final class PowerManagerHandler extends Handler {
3964 public PowerManagerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07003965 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07003966 }
3967
3968 @Override
3969 public void handleMessage(Message msg) {
3970 switch (msg.what) {
3971 case MSG_USER_ACTIVITY_TIMEOUT:
3972 handleUserActivityTimeout();
3973 break;
3974 case MSG_SANDMAN:
3975 handleSandman();
3976 break;
Jeff Browne333e672014-10-28 13:48:55 -07003977 case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT:
3978 handleScreenBrightnessBoostTimeout();
3979 break;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003980 case MSG_CHECK_FOR_LONG_WAKELOCKS:
3981 checkForLongWakeLocks();
3982 break;
Jeff Brown96307042012-07-27 15:51:34 -07003983 }
3984 }
3985 }
3986
3987 /**
3988 * Represents a wake lock that has been acquired by an application.
3989 */
3990 private final class WakeLock implements IBinder.DeathRecipient {
3991 public final IBinder mLock;
3992 public int mFlags;
3993 public String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07003994 public final String mPackageName;
Jeff Brown96307042012-07-27 15:51:34 -07003995 public WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003996 public String mHistoryTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07003997 public final int mOwnerUid;
3998 public final int mOwnerPid;
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07003999 public final UidState mUidState;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004000 public long mAcquireTime;
Dianne Hackborn713df152013-05-17 11:27:57 -07004001 public boolean mNotifiedAcquired;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004002 public boolean mNotifiedLong;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07004003 public boolean mDisabled;
Jeff Brown96307042012-07-27 15:51:34 -07004004
Dianne Hackborn713df152013-05-17 11:27:57 -07004005 public WakeLock(IBinder lock, int flags, String tag, String packageName,
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07004006 WorkSource workSource, String historyTag, int ownerUid, int ownerPid,
4007 UidState uidState) {
Jeff Brown96307042012-07-27 15:51:34 -07004008 mLock = lock;
4009 mFlags = flags;
4010 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07004011 mPackageName = packageName;
Jeff Brown96307042012-07-27 15:51:34 -07004012 mWorkSource = copyWorkSource(workSource);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004013 mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07004014 mOwnerUid = ownerUid;
4015 mOwnerPid = ownerPid;
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07004016 mUidState = uidState;
Jeff Brown96307042012-07-27 15:51:34 -07004017 }
4018
4019 @Override
4020 public void binderDied() {
4021 PowerManagerService.this.handleWakeLockDeath(this);
4022 }
4023
4024 public boolean hasSameProperties(int flags, String tag, WorkSource workSource,
4025 int ownerUid, int ownerPid) {
4026 return mFlags == flags
4027 && mTag.equals(tag)
4028 && hasSameWorkSource(workSource)
4029 && mOwnerUid == ownerUid
4030 && mOwnerPid == ownerPid;
4031 }
4032
Dianne Hackborn713df152013-05-17 11:27:57 -07004033 public void updateProperties(int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004034 WorkSource workSource, String historyTag, int ownerUid, int ownerPid) {
Dianne Hackborn713df152013-05-17 11:27:57 -07004035 if (!mPackageName.equals(packageName)) {
4036 throw new IllegalStateException("Existing wake lock package name changed: "
4037 + mPackageName + " to " + packageName);
4038 }
4039 if (mOwnerUid != ownerUid) {
4040 throw new IllegalStateException("Existing wake lock uid changed: "
4041 + mOwnerUid + " to " + ownerUid);
4042 }
4043 if (mOwnerPid != ownerPid) {
4044 throw new IllegalStateException("Existing wake lock pid changed: "
4045 + mOwnerPid + " to " + ownerPid);
4046 }
Jeff Brown96307042012-07-27 15:51:34 -07004047 mFlags = flags;
4048 mTag = tag;
4049 updateWorkSource(workSource);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004050 mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07004051 }
4052
4053 public boolean hasSameWorkSource(WorkSource workSource) {
Narayan Kamath607223f2018-02-19 14:09:02 +00004054 return Objects.equals(mWorkSource, workSource);
Jeff Brown96307042012-07-27 15:51:34 -07004055 }
4056
4057 public void updateWorkSource(WorkSource workSource) {
4058 mWorkSource = copyWorkSource(workSource);
4059 }
4060
4061 @Override
4062 public String toString() {
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004063 StringBuilder sb = new StringBuilder();
4064 sb.append(getLockLevelString());
4065 sb.append(" '");
4066 sb.append(mTag);
4067 sb.append("'");
4068 sb.append(getLockFlagsString());
4069 if (mDisabled) {
4070 sb.append(" DISABLED");
4071 }
4072 if (mNotifiedAcquired) {
4073 sb.append(" ACQ=");
4074 TimeUtils.formatDuration(mAcquireTime-SystemClock.uptimeMillis(), sb);
4075 }
4076 if (mNotifiedLong) {
4077 sb.append(" LONG");
4078 }
4079 sb.append(" (uid=");
4080 sb.append(mOwnerUid);
4081 if (mOwnerPid != 0) {
4082 sb.append(" pid=");
4083 sb.append(mOwnerPid);
4084 }
4085 if (mWorkSource != null) {
4086 sb.append(" ws=");
4087 sb.append(mWorkSource);
4088 }
4089 sb.append(")");
4090 return sb.toString();
Jeff Brown96307042012-07-27 15:51:34 -07004091 }
4092
Netta P958d0a52017-02-07 11:20:55 -08004093 public void writeToProto(ProtoOutputStream proto, long fieldId) {
4094 final long wakeLockToken = proto.start(fieldId);
4095 proto.write(WakeLockProto.LOCK_LEVEL, (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK));
4096 proto.write(WakeLockProto.TAG, mTag);
4097
4098 final long wakeLockFlagsToken = proto.start(WakeLockProto.FLAGS);
4099 proto.write(WakeLockProto.WakeLockFlagsProto.IS_ACQUIRE_CAUSES_WAKEUP,
4100 (mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP)!=0);
4101 proto.write(WakeLockProto.WakeLockFlagsProto.IS_ON_AFTER_RELEASE,
4102 (mFlags & PowerManager.ON_AFTER_RELEASE)!=0);
4103 proto.end(wakeLockFlagsToken);
4104
4105 proto.write(WakeLockProto.IS_DISABLED, mDisabled);
4106 if (mNotifiedAcquired) {
4107 proto.write(WakeLockProto.ACQ_MS, mAcquireTime);
4108 }
4109 proto.write(WakeLockProto.IS_NOTIFIED_LONG, mNotifiedLong);
4110 proto.write(WakeLockProto.UID, mOwnerUid);
4111 proto.write(WakeLockProto.PID, mOwnerPid);
4112
4113 if (mWorkSource != null) {
4114 mWorkSource.writeToProto(proto, WakeLockProto.WORK_SOURCE);
4115 }
4116 proto.end(wakeLockToken);
4117 }
4118
Jeff Brown26875502014-01-30 21:47:47 -08004119 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07004120 private String getLockLevelString() {
4121 switch (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
4122 case PowerManager.FULL_WAKE_LOCK:
4123 return "FULL_WAKE_LOCK ";
4124 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
4125 return "SCREEN_BRIGHT_WAKE_LOCK ";
4126 case PowerManager.SCREEN_DIM_WAKE_LOCK:
4127 return "SCREEN_DIM_WAKE_LOCK ";
4128 case PowerManager.PARTIAL_WAKE_LOCK:
4129 return "PARTIAL_WAKE_LOCK ";
4130 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
4131 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
Jeff Brown26875502014-01-30 21:47:47 -08004132 case PowerManager.DOZE_WAKE_LOCK:
4133 return "DOZE_WAKE_LOCK ";
Jeff Brownc2932a12014-11-20 18:04:05 -08004134 case PowerManager.DRAW_WAKE_LOCK:
4135 return "DRAW_WAKE_LOCK ";
Jeff Brown96307042012-07-27 15:51:34 -07004136 default:
4137 return "??? ";
4138 }
4139 }
4140
4141 private String getLockFlagsString() {
4142 String result = "";
4143 if ((mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
4144 result += " ACQUIRE_CAUSES_WAKEUP";
4145 }
4146 if ((mFlags & PowerManager.ON_AFTER_RELEASE) != 0) {
4147 result += " ON_AFTER_RELEASE";
4148 }
4149 return result;
4150 }
4151 }
4152
4153 private final class SuspendBlockerImpl implements SuspendBlocker {
4154 private final String mName;
Jeff Brown3edf5272014-08-14 19:25:14 -07004155 private final String mTraceName;
Jeff Brown96307042012-07-27 15:51:34 -07004156 private int mReferenceCount;
4157
4158 public SuspendBlockerImpl(String name) {
4159 mName = name;
Jeff Brown3edf5272014-08-14 19:25:14 -07004160 mTraceName = "SuspendBlocker (" + name + ")";
Jeff Brown96307042012-07-27 15:51:34 -07004161 }
4162
4163 @Override
4164 protected void finalize() throws Throwable {
Mike Lockwood809ad0f2009-10-26 22:10:33 -04004165 try {
Jeff Brown96307042012-07-27 15:51:34 -07004166 if (mReferenceCount != 0) {
Jeff Brown3edf5272014-08-14 19:25:14 -07004167 Slog.wtf(TAG, "Suspend blocker \"" + mName
Jeff Brown96307042012-07-27 15:51:34 -07004168 + "\" was finalized without being released!");
4169 mReferenceCount = 0;
Santos Cordon64a6e612018-08-22 19:27:04 +01004170 mNativeWrapper.nativeReleaseSuspendBlocker(mName);
Jeff Brown3edf5272014-08-14 19:25:14 -07004171 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Mike Lockwood809ad0f2009-10-26 22:10:33 -04004172 }
4173 } finally {
Jeff Brown96307042012-07-27 15:51:34 -07004174 super.finalize();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04004175 }
4176 }
4177
Craig Mautner75fc9de2012-06-18 16:53:27 -07004178 @Override
Jeff Brown96307042012-07-27 15:51:34 -07004179 public void acquire() {
4180 synchronized (this) {
4181 mReferenceCount += 1;
4182 if (mReferenceCount == 1) {
Jeff Brown27f7a862012-12-12 15:43:31 -08004183 if (DEBUG_SPEW) {
4184 Slog.d(TAG, "Acquiring suspend blocker \"" + mName + "\".");
4185 }
Jeff Brown3edf5272014-08-14 19:25:14 -07004186 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Santos Cordon64a6e612018-08-22 19:27:04 +01004187 mNativeWrapper.nativeAcquireSuspendBlocker(mName);
Craig Mautner37933682012-06-06 14:13:39 -07004188 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04004189 }
4190 }
4191
Craig Mautner75fc9de2012-06-18 16:53:27 -07004192 @Override
Jeff Brown96307042012-07-27 15:51:34 -07004193 public void release() {
4194 synchronized (this) {
4195 mReferenceCount -= 1;
4196 if (mReferenceCount == 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -08004197 if (DEBUG_SPEW) {
4198 Slog.d(TAG, "Releasing suspend blocker \"" + mName + "\".");
4199 }
Santos Cordon64a6e612018-08-22 19:27:04 +01004200 mNativeWrapper.nativeReleaseSuspendBlocker(mName);
Jeff Brown3edf5272014-08-14 19:25:14 -07004201 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown96307042012-07-27 15:51:34 -07004202 } else if (mReferenceCount < 0) {
Jeff Brown3edf5272014-08-14 19:25:14 -07004203 Slog.wtf(TAG, "Suspend blocker \"" + mName
Jeff Brown96307042012-07-27 15:51:34 -07004204 + "\" was released without being acquired!", new Throwable());
4205 mReferenceCount = 0;
4206 }
4207 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04004208 }
Jeff Brown96307042012-07-27 15:51:34 -07004209
4210 @Override
4211 public String toString() {
4212 synchronized (this) {
4213 return mName + ": ref count=" + mReferenceCount;
4214 }
4215 }
Netta P958d0a52017-02-07 11:20:55 -08004216
4217 public void writeToProto(ProtoOutputStream proto, long fieldId) {
4218 final long sbToken = proto.start(fieldId);
4219 synchronized (this) {
4220 proto.write(SuspendBlockerProto.NAME, mName);
4221 proto.write(SuspendBlockerProto.REFERENCE_COUNT, mReferenceCount);
4222 }
4223 proto.end(sbToken);
4224 }
Jeff Brown96307042012-07-27 15:51:34 -07004225 }
Jeff Brownc38c9be2012-10-04 13:16:19 -07004226
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07004227 static final class UidState {
4228 final int mUid;
4229 int mNumWakeLocks;
4230 int mProcState;
4231 boolean mActive;
4232
4233 UidState(int uid) {
4234 mUid = uid;
4235 }
4236 }
4237
Santos Cordon64a6e612018-08-22 19:27:04 +01004238 @VisibleForTesting
4239 final class BinderService extends IPowerManager.Stub {
Jocelyn Dangf2c38c12017-03-31 14:03:37 -07004240 @Override
4241 public void onShellCommand(FileDescriptor in, FileDescriptor out,
4242 FileDescriptor err, String[] args, ShellCallback callback,
4243 ResultReceiver resultReceiver) {
4244 (new PowerManagerShellCommand(this)).exec(
4245 this, in, out, err, args, callback, resultReceiver);
4246 }
4247
Jeff Brown6f357d32014-01-15 20:40:55 -08004248 @Override // Binder call
4249 public void acquireWakeLockWithUid(IBinder lock, int flags, String tag,
4250 String packageName, int uid) {
Dianne Hackbornef640cd2014-03-25 14:41:05 -07004251 if (uid < 0) {
4252 uid = Binder.getCallingUid();
4253 }
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004254 acquireWakeLock(lock, flags, tag, packageName, new WorkSource(uid), null);
Jeff Brown6f357d32014-01-15 20:40:55 -08004255 }
4256
4257 @Override // Binder call
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07004258 public void powerHint(int hintId, int data) {
Dianne Hackbornddef7e72014-07-09 16:39:14 -07004259 if (!mSystemReady) {
4260 // Service not ready yet, so who the heck cares about power hints, bah.
4261 return;
4262 }
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07004263 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Ruchi Kandoi15aedf52014-05-09 19:57:51 -07004264 powerHintInternal(hintId, data);
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07004265 }
4266
4267 @Override // Binder call
Jeff Brown6f357d32014-01-15 20:40:55 -08004268 public void acquireWakeLock(IBinder lock, int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004269 WorkSource ws, String historyTag) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004270 if (lock == null) {
4271 throw new IllegalArgumentException("lock must not be null");
4272 }
4273 if (packageName == null) {
4274 throw new IllegalArgumentException("packageName must not be null");
4275 }
4276 PowerManager.validateWakeLockParameters(flags, tag);
4277
4278 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Jeff Brown72671fb2014-08-21 21:41:09 -07004279 if ((flags & PowerManager.DOZE_WAKE_LOCK) != 0) {
4280 mContext.enforceCallingOrSelfPermission(
4281 android.Manifest.permission.DEVICE_POWER, null);
4282 }
Narayan Kamath81822022017-12-08 11:56:01 +00004283 if (ws != null && !ws.isEmpty()) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004284 mContext.enforceCallingOrSelfPermission(
4285 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
4286 } else {
4287 ws = null;
4288 }
4289
4290 final int uid = Binder.getCallingUid();
4291 final int pid = Binder.getCallingPid();
4292 final long ident = Binder.clearCallingIdentity();
4293 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08004294 acquireWakeLockInternal(lock, flags, tag, packageName, ws, historyTag, uid, pid);
Jeff Brown6f357d32014-01-15 20:40:55 -08004295 } finally {
4296 Binder.restoreCallingIdentity(ident);
4297 }
4298 }
4299
4300 @Override // Binder call
4301 public void releaseWakeLock(IBinder lock, int flags) {
4302 if (lock == null) {
4303 throw new IllegalArgumentException("lock must not be null");
4304 }
4305
4306 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
4307
4308 final long ident = Binder.clearCallingIdentity();
4309 try {
4310 releaseWakeLockInternal(lock, flags);
4311 } finally {
4312 Binder.restoreCallingIdentity(ident);
4313 }
4314 }
4315
4316 @Override // Binder call
4317 public void updateWakeLockUids(IBinder lock, int[] uids) {
4318 WorkSource ws = null;
4319
4320 if (uids != null) {
4321 ws = new WorkSource();
4322 // XXX should WorkSource have a way to set uids as an int[] instead of adding them
4323 // one at a time?
4324 for (int i = 0; i < uids.length; i++) {
4325 ws.add(uids[i]);
4326 }
4327 }
Dianne Hackborn4590e522014-03-24 13:36:46 -07004328 updateWakeLockWorkSource(lock, ws, null);
Jeff Brown6f357d32014-01-15 20:40:55 -08004329 }
4330
4331 @Override // Binder call
Dianne Hackborn4590e522014-03-24 13:36:46 -07004332 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004333 if (lock == null) {
4334 throw new IllegalArgumentException("lock must not be null");
4335 }
4336
4337 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Narayan Kamath81822022017-12-08 11:56:01 +00004338 if (ws != null && !ws.isEmpty()) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004339 mContext.enforceCallingOrSelfPermission(
4340 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
4341 } else {
4342 ws = null;
4343 }
4344
Dianne Hackbornd953c532014-08-16 18:17:38 -07004345 final int callingUid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08004346 final long ident = Binder.clearCallingIdentity();
4347 try {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004348 updateWakeLockWorkSourceInternal(lock, ws, historyTag, callingUid);
Jeff Brown6f357d32014-01-15 20:40:55 -08004349 } finally {
4350 Binder.restoreCallingIdentity(ident);
4351 }
4352 }
4353
4354 @Override // Binder call
4355 public boolean isWakeLockLevelSupported(int level) {
4356 final long ident = Binder.clearCallingIdentity();
4357 try {
4358 return isWakeLockLevelSupportedInternal(level);
4359 } finally {
4360 Binder.restoreCallingIdentity(ident);
4361 }
4362 }
4363
4364 @Override // Binder call
4365 public void userActivity(long eventTime, int event, int flags) {
4366 final long now = SystemClock.uptimeMillis();
4367 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
Jeff Brown0a571122014-08-21 21:50:43 -07004368 != PackageManager.PERMISSION_GRANTED
4369 && mContext.checkCallingOrSelfPermission(
4370 android.Manifest.permission.USER_ACTIVITY)
4371 != PackageManager.PERMISSION_GRANTED) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004372 // Once upon a time applications could call userActivity().
4373 // Now we require the DEVICE_POWER permission. Log a warning and ignore the
4374 // request instead of throwing a SecurityException so we don't break old apps.
4375 synchronized (mLock) {
4376 if (now >= mLastWarningAboutUserActivityPermission + (5 * 60 * 1000)) {
4377 mLastWarningAboutUserActivityPermission = now;
4378 Slog.w(TAG, "Ignoring call to PowerManager.userActivity() because the "
Jeff Brown0a571122014-08-21 21:50:43 -07004379 + "caller does not have DEVICE_POWER or USER_ACTIVITY "
4380 + "permission. Please fix your app! "
Jeff Brown6f357d32014-01-15 20:40:55 -08004381 + " pid=" + Binder.getCallingPid()
4382 + " uid=" + Binder.getCallingUid());
4383 }
4384 }
4385 return;
4386 }
4387
Jim Millerdca15d22015-06-16 20:55:13 -07004388 if (eventTime > now) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004389 throw new IllegalArgumentException("event time must not be in the future");
4390 }
4391
4392 final int uid = Binder.getCallingUid();
4393 final long ident = Binder.clearCallingIdentity();
4394 try {
4395 userActivityInternal(eventTime, event, flags, uid);
4396 } finally {
4397 Binder.restoreCallingIdentity(ident);
4398 }
4399 }
4400
4401 @Override // Binder call
Michael Wrighte3001042019-02-05 00:13:14 +00004402 public void wakeUp(long eventTime, @WakeReason int reason, String details,
4403 String opPackageName) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004404 if (eventTime > SystemClock.uptimeMillis()) {
4405 throw new IllegalArgumentException("event time must not be in the future");
4406 }
4407
4408 mContext.enforceCallingOrSelfPermission(
4409 android.Manifest.permission.DEVICE_POWER, null);
4410
Jeff Brownc12035c2014-08-13 18:52:25 -07004411 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08004412 final long ident = Binder.clearCallingIdentity();
4413 try {
Michael Wrighte3001042019-02-05 00:13:14 +00004414 wakeUpInternal(eventTime, reason, details, uid, opPackageName, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08004415 } finally {
4416 Binder.restoreCallingIdentity(ident);
4417 }
4418 }
4419
4420 @Override // Binder call
Jeff Brown6d8fd272014-05-20 21:24:38 -07004421 public void goToSleep(long eventTime, int reason, int flags) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004422 if (eventTime > SystemClock.uptimeMillis()) {
4423 throw new IllegalArgumentException("event time must not be in the future");
4424 }
4425
4426 mContext.enforceCallingOrSelfPermission(
4427 android.Manifest.permission.DEVICE_POWER, null);
4428
Jeff Brownc12035c2014-08-13 18:52:25 -07004429 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08004430 final long ident = Binder.clearCallingIdentity();
4431 try {
Jeff Brownc12035c2014-08-13 18:52:25 -07004432 goToSleepInternal(eventTime, reason, flags, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08004433 } finally {
4434 Binder.restoreCallingIdentity(ident);
4435 }
4436 }
4437
4438 @Override // Binder call
4439 public void nap(long eventTime) {
4440 if (eventTime > SystemClock.uptimeMillis()) {
4441 throw new IllegalArgumentException("event time must not be in the future");
4442 }
4443
4444 mContext.enforceCallingOrSelfPermission(
4445 android.Manifest.permission.DEVICE_POWER, null);
4446
Jeff Brownc12035c2014-08-13 18:52:25 -07004447 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08004448 final long ident = Binder.clearCallingIdentity();
4449 try {
Jeff Brownc12035c2014-08-13 18:52:25 -07004450 napInternal(eventTime, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08004451 } finally {
4452 Binder.restoreCallingIdentity(ident);
4453 }
4454 }
4455
4456 @Override // Binder call
Jeff Brown037c33e2014-04-09 00:31:55 -07004457 public boolean isInteractive() {
Jeff Brown6f357d32014-01-15 20:40:55 -08004458 final long ident = Binder.clearCallingIdentity();
4459 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07004460 return isInteractiveInternal();
Jeff Brown6f357d32014-01-15 20:40:55 -08004461 } finally {
4462 Binder.restoreCallingIdentity(ident);
4463 }
4464 }
4465
Dianne Hackborneb94fa72014-06-03 17:48:12 -07004466 @Override // Binder call
4467 public boolean isPowerSaveMode() {
4468 final long ident = Binder.clearCallingIdentity();
4469 try {
Makoto Onukibd7a6252018-05-10 13:41:39 -07004470 return mBatterySaverController.isEnabled();
Dianne Hackborneb94fa72014-06-03 17:48:12 -07004471 } finally {
4472 Binder.restoreCallingIdentity(ident);
4473 }
4474 }
4475
jackqdyulei455e90a2017-02-09 15:29:16 -08004476 // Binder call
4477 public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
4478 final long ident = Binder.clearCallingIdentity();
4479 try {
Kweku Adams9f488e22019-01-14 16:25:08 -08004480 return mBatterySaverPolicy.getBatterySaverPolicy(serviceType);
jackqdyulei455e90a2017-02-09 15:29:16 -08004481 } finally {
4482 Binder.restoreCallingIdentity(ident);
4483 }
4484 }
4485
John Spurlock8d4e6cb2014-09-14 11:10:22 -04004486 @Override // Binder call
Salvador Martinezc8c4c5d2019-03-11 11:11:37 -07004487 public boolean setPowerSaveModeEnabled(boolean enabled) {
Salvador Martineza80bbab2018-09-24 10:36:11 -07004488 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
4489 != PackageManager.PERMISSION_GRANTED) {
4490 mContext.enforceCallingOrSelfPermission(
4491 android.Manifest.permission.DEVICE_POWER, null);
4492 }
John Spurlock8d4e6cb2014-09-14 11:10:22 -04004493 final long ident = Binder.clearCallingIdentity();
4494 try {
Makoto Onukia3cd7b92018-03-19 14:47:05 -07004495 return setLowPowerModeInternal(enabled);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04004496 } finally {
4497 Binder.restoreCallingIdentity(ident);
4498 }
4499 }
4500
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004501 @Override // Binder call
Salvador Martinezb85a9f82019-03-20 16:21:27 -07004502 public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) {
Salvador Martinez812ea752018-10-19 13:03:20 -07004503 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER,
4504 "updateDynamicPowerSavings");
4505 final long ident = Binder.clearCallingIdentity();
4506 try {
4507 final ContentResolver resolver = mContext.getContentResolver();
4508 boolean success = Settings.Global.putInt(resolver,
4509 Settings.Global.DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD,
4510 disableThreshold);
4511 if (success) {
4512 // abort updating if we weren't able to succeed on the threshold
4513 success &= Settings.Global.putInt(resolver,
4514 Settings.Global.DYNAMIC_POWER_SAVINGS_ENABLED,
Salvador Martinezb85a9f82019-03-20 16:21:27 -07004515 powerSaveHint ? 1 : 0);
Salvador Martinez812ea752018-10-19 13:03:20 -07004516 }
4517 return success;
4518 } finally {
4519 Binder.restoreCallingIdentity(ident);
4520 }
4521 }
4522
4523 @Override // Binder call
Kweku Adams9f488e22019-01-14 16:25:08 -08004524 public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
4525 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
4526 != PackageManager.PERMISSION_GRANTED) {
4527 mContext.enforceCallingOrSelfPermission(
4528 android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSavePolicy");
4529 }
4530 final long ident = Binder.clearCallingIdentity();
4531 try {
4532 return mBatterySaverStateMachine.setAdaptiveBatterySaverPolicy(config);
4533 } finally {
4534 Binder.restoreCallingIdentity(ident);
4535 }
4536 }
4537
4538 @Override // Binder call
4539 public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
4540 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
4541 != PackageManager.PERMISSION_GRANTED) {
4542 mContext.enforceCallingOrSelfPermission(
4543 android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSaveEnabled");
4544 }
4545 final long ident = Binder.clearCallingIdentity();
4546 try {
4547 return mBatterySaverStateMachine.setAdaptiveBatterySaverEnabled(enabled);
4548 } finally {
4549 Binder.restoreCallingIdentity(ident);
4550 }
4551 }
4552
4553 @Override // Binder call
Salvador Martinezb85a9f82019-03-20 16:21:27 -07004554 public int getPowerSaveModeTrigger() {
Salvador Martinez812ea752018-10-19 13:03:20 -07004555 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER, null);
4556 final long ident = Binder.clearCallingIdentity();
4557 try {
4558 return Settings.Global.getInt(mContext.getContentResolver(),
Salvador Martinezb85a9f82019-03-20 16:21:27 -07004559 Settings.Global.AUTOMATIC_POWER_SAVE_MODE,
4560 PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE);
Salvador Martinez812ea752018-10-19 13:03:20 -07004561 } finally {
4562 Binder.restoreCallingIdentity(ident);
4563 }
4564 }
4565
4566 @Override // Binder call
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004567 public boolean isDeviceIdleMode() {
4568 final long ident = Binder.clearCallingIdentity();
4569 try {
4570 return isDeviceIdleModeInternal();
4571 } finally {
4572 Binder.restoreCallingIdentity(ident);
4573 }
4574 }
4575
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004576 @Override // Binder call
4577 public boolean isLightDeviceIdleMode() {
4578 final long ident = Binder.clearCallingIdentity();
4579 try {
4580 return isLightDeviceIdleModeInternal();
4581 } finally {
4582 Binder.restoreCallingIdentity(ident);
4583 }
4584 }
4585
Jeff Brown6f357d32014-01-15 20:40:55 -08004586 /**
Salvador Martineza6f7b252017-04-10 10:46:15 -07004587 * Gets the reason for the last time the phone had to reboot.
4588 *
4589 * @return The reason the phone last shut down as an int or
Makoto Onuki66a78122017-11-14 15:03:21 -08004590 * {@link PowerManager#SHUTDOWN_REASON_UNKNOWN} if the file could not be opened.
Salvador Martineza6f7b252017-04-10 10:46:15 -07004591 */
4592 @Override // Binder call
4593 public int getLastShutdownReason() {
4594 mContext.enforceCallingOrSelfPermission(
4595 android.Manifest.permission.DEVICE_POWER, null);
4596
4597 final long ident = Binder.clearCallingIdentity();
4598 try {
Mark Salyzyn74ce8b32018-06-05 09:06:54 -07004599 return getLastShutdownReasonInternal(REBOOT_PROPERTY);
Salvador Martineza6f7b252017-04-10 10:46:15 -07004600 } finally {
4601 Binder.restoreCallingIdentity(ident);
4602 }
4603 }
4604
Calin Tatarua3805722018-08-09 16:41:28 +02004605 @Override // Binder call
4606 public int getLastSleepReason() {
4607 mContext.enforceCallingOrSelfPermission(
4608 android.Manifest.permission.DEVICE_POWER, null);
4609
4610 final long ident = Binder.clearCallingIdentity();
4611 try {
4612 return getLastSleepReasonInternal();
4613 } finally {
4614 Binder.restoreCallingIdentity(ident);
4615 }
4616 }
4617
Salvador Martineza6f7b252017-04-10 10:46:15 -07004618 /**
Jeff Brown6f357d32014-01-15 20:40:55 -08004619 * Reboots the device.
4620 *
4621 * @param confirm If true, shows a reboot confirmation dialog.
4622 * @param reason The reason for the reboot, or null if none.
4623 * @param wait If true, this call waits for the reboot to complete and does not return.
4624 */
4625 @Override // Binder call
4626 public void reboot(boolean confirm, String reason, boolean wait) {
4627 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
Tao Baoe8a403d2015-12-31 07:44:55 -08004628 if (PowerManager.REBOOT_RECOVERY.equals(reason)
4629 || PowerManager.REBOOT_RECOVERY_UPDATE.equals(reason)) {
Doug Zongker3b0218b2014-01-14 12:29:06 -08004630 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null);
4631 }
Jeff Brown6f357d32014-01-15 20:40:55 -08004632
4633 final long ident = Binder.clearCallingIdentity();
4634 try {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07004635 shutdownOrRebootInternal(HALT_MODE_REBOOT, confirm, reason, wait);
4636 } finally {
4637 Binder.restoreCallingIdentity(ident);
4638 }
4639 }
4640
4641 /**
4642 * Reboots the device into safe mode
4643 *
4644 * @param confirm If true, shows a reboot confirmation dialog.
4645 * @param wait If true, this call waits for the reboot to complete and does not return.
4646 */
4647 @Override // Binder call
4648 public void rebootSafeMode(boolean confirm, boolean wait) {
4649 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
4650
4651 final long ident = Binder.clearCallingIdentity();
4652 try {
4653 shutdownOrRebootInternal(HALT_MODE_REBOOT_SAFE_MODE, confirm,
4654 PowerManager.REBOOT_SAFE_MODE, wait);
Jeff Brown6f357d32014-01-15 20:40:55 -08004655 } finally {
4656 Binder.restoreCallingIdentity(ident);
4657 }
4658 }
4659
4660 /**
4661 * Shuts down the device.
4662 *
4663 * @param confirm If true, shows a shutdown confirmation dialog.
4664 * @param wait If true, this call waits for the shutdown to complete and does not return.
4665 */
4666 @Override // Binder call
Yusuke Sato705ffd12015-07-21 15:52:11 -07004667 public void shutdown(boolean confirm, String reason, boolean wait) {
Jeff Brown6f357d32014-01-15 20:40:55 -08004668 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
4669
4670 final long ident = Binder.clearCallingIdentity();
4671 try {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07004672 shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, confirm, reason, wait);
Jeff Brown6f357d32014-01-15 20:40:55 -08004673 } finally {
4674 Binder.restoreCallingIdentity(ident);
4675 }
4676 }
4677
4678 /**
4679 * Crash the runtime (causing a complete restart of the Android framework).
4680 * Requires REBOOT permission. Mostly for testing. Should not return.
4681 */
4682 @Override // Binder call
4683 public void crash(String message) {
4684 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
4685
4686 final long ident = Binder.clearCallingIdentity();
4687 try {
4688 crashInternal(message);
4689 } finally {
4690 Binder.restoreCallingIdentity(ident);
4691 }
4692 }
4693
4694 /**
4695 * Set the setting that determines whether the device stays on when plugged in.
4696 * The argument is a bit string, with each bit specifying a power source that,
4697 * when the device is connected to that source, causes the device to stay on.
4698 * See {@link android.os.BatteryManager} for the list of power sources that
4699 * can be specified. Current values include
4700 * {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
4701 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
4702 *
4703 * Used by "adb shell svc power stayon ..."
4704 *
4705 * @param val an {@code int} containing the bits that specify which power sources
4706 * should cause the device to stay on.
4707 */
4708 @Override // Binder call
4709 public void setStayOnSetting(int val) {
Billy Lau6ad2d662015-07-18 00:26:58 +01004710 int uid = Binder.getCallingUid();
4711 // if uid is of root's, we permit this operation straight away
4712 if (uid != Process.ROOT_UID) {
4713 if (!Settings.checkAndNoteWriteSettingsOperation(mContext, uid,
4714 Settings.getPackageNameForUid(mContext, uid), true)) {
4715 return;
4716 }
4717 }
Jeff Brown6f357d32014-01-15 20:40:55 -08004718
4719 final long ident = Binder.clearCallingIdentity();
4720 try {
4721 setStayOnSettingInternal(val);
4722 } finally {
4723 Binder.restoreCallingIdentity(ident);
4724 }
4725 }
4726
4727 /**
Jeff Brown6f357d32014-01-15 20:40:55 -08004728 * Used by the phone application to make the attention LED flash when ringing.
4729 */
4730 @Override // Binder call
4731 public void setAttentionLight(boolean on, int color) {
4732 mContext.enforceCallingOrSelfPermission(
4733 android.Manifest.permission.DEVICE_POWER, null);
4734
4735 final long ident = Binder.clearCallingIdentity();
4736 try {
4737 setAttentionLightInternal(on, color);
4738 } finally {
4739 Binder.restoreCallingIdentity(ident);
4740 }
4741 }
4742
4743 @Override // Binder call
Lucas Dupin16cfe452018-02-08 13:14:50 -08004744 public void setDozeAfterScreenOff(boolean on) {
4745 mContext.enforceCallingOrSelfPermission(
4746 android.Manifest.permission.DEVICE_POWER, null);
4747
4748 final long ident = Binder.clearCallingIdentity();
4749 try {
4750 setDozeAfterScreenOffInternal(on);
4751 } finally {
4752 Binder.restoreCallingIdentity(ident);
4753 }
4754 }
4755
4756 @Override // Binder call
Jeff Browne333e672014-10-28 13:48:55 -07004757 public void boostScreenBrightness(long eventTime) {
4758 if (eventTime > SystemClock.uptimeMillis()) {
4759 throw new IllegalArgumentException("event time must not be in the future");
4760 }
4761
4762 mContext.enforceCallingOrSelfPermission(
4763 android.Manifest.permission.DEVICE_POWER, null);
4764
4765 final int uid = Binder.getCallingUid();
4766 final long ident = Binder.clearCallingIdentity();
4767 try {
4768 boostScreenBrightnessInternal(eventTime, uid);
4769 } finally {
4770 Binder.restoreCallingIdentity(ident);
4771 }
4772 }
4773
4774 @Override // Binder call
Bryce Lee84d6c0f2015-03-17 10:43:08 -07004775 public boolean isScreenBrightnessBoosted() {
4776 final long ident = Binder.clearCallingIdentity();
4777 try {
4778 return isScreenBrightnessBoostedInternal();
4779 } finally {
4780 Binder.restoreCallingIdentity(ident);
4781 }
4782 }
4783
Santos Cordon12f92eb2019-02-01 21:28:47 +00004784 @Override // binder call
4785 public boolean forceSuspend() {
4786 mContext.enforceCallingOrSelfPermission(
4787 android.Manifest.permission.DEVICE_POWER, null);
4788
4789 final int uid = Binder.getCallingUid();
4790 final long ident = Binder.clearCallingIdentity();
4791 try {
4792 return forceSuspendInternal(uid);
4793 } finally {
4794 Binder.restoreCallingIdentity(ident);
4795 }
4796 }
4797
Bryce Lee84d6c0f2015-03-17 10:43:08 -07004798 @Override // Binder call
Jeff Brown6f357d32014-01-15 20:40:55 -08004799 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004800 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown6f357d32014-01-15 20:40:55 -08004801
4802 final long ident = Binder.clearCallingIdentity();
Netta P958d0a52017-02-07 11:20:55 -08004803
4804 boolean isDumpProto = false;
4805 for (String arg : args) {
4806 if (arg.equals("--proto")) {
4807 isDumpProto = true;
4808 }
4809 }
Jeff Brown6f357d32014-01-15 20:40:55 -08004810 try {
Netta P958d0a52017-02-07 11:20:55 -08004811 if (isDumpProto) {
4812 dumpProto(fd);
4813 } else {
4814 dumpInternal(pw);
4815 }
Jeff Brown6f357d32014-01-15 20:40:55 -08004816 } finally {
4817 Binder.restoreCallingIdentity(ident);
4818 }
4819 }
4820 }
4821
Salvador Martineza6f7b252017-04-10 10:46:15 -07004822 @VisibleForTesting
Santos Cordon64a6e612018-08-22 19:27:04 +01004823 BinderService getBinderServiceInstance() {
4824 return mBinderService;
4825 }
4826
4827 @VisibleForTesting
4828 LocalService getLocalServiceInstance() {
4829 return mLocalService;
4830 }
4831
4832 @VisibleForTesting
Mark Salyzyne65c0c62017-08-15 07:53:47 -07004833 // lastRebootReasonProperty argument to permit testing
4834 int getLastShutdownReasonInternal(String lastRebootReasonProperty) {
4835 String line = SystemProperties.get(lastRebootReasonProperty);
Michael Wright9199d0c2017-05-10 21:35:13 +01004836 if (line == null) {
4837 return PowerManager.SHUTDOWN_REASON_UNKNOWN;
4838 }
Salvador Martineza6f7b252017-04-10 10:46:15 -07004839 switch (line) {
4840 case REASON_SHUTDOWN:
4841 return PowerManager.SHUTDOWN_REASON_SHUTDOWN;
4842 case REASON_REBOOT:
4843 return PowerManager.SHUTDOWN_REASON_REBOOT;
4844 case REASON_USERREQUESTED:
4845 return PowerManager.SHUTDOWN_REASON_USER_REQUESTED;
4846 case REASON_THERMAL_SHUTDOWN:
4847 return PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN;
Sudheer Shanka292637f2017-09-25 10:36:23 -07004848 case REASON_LOW_BATTERY:
4849 return PowerManager.SHUTDOWN_REASON_LOW_BATTERY;
4850 case REASON_BATTERY_THERMAL_STATE:
4851 return PowerManager.SHUTDOWN_REASON_BATTERY_THERMAL;
Salvador Martineza6f7b252017-04-10 10:46:15 -07004852 default:
4853 return PowerManager.SHUTDOWN_REASON_UNKNOWN;
4854 }
4855 }
4856
Calin Tatarua3805722018-08-09 16:41:28 +02004857 private int getLastSleepReasonInternal() {
4858 synchronized (mLock) {
4859 return mLastSleepReason;
4860 }
4861 }
4862
Santos Cordon623526b2019-04-09 17:02:38 +01004863 private PowerManager.WakeData getLastWakeupInternal() {
4864 synchronized (mLock) {
4865 return new WakeData(mLastWakeTime, mLastWakeReason);
4866 }
4867 }
4868
Santos Cordond9701ab2019-08-01 18:33:20 +01004869 @VisibleForTesting
4870 final class LocalService extends PowerManagerInternal {
Jeff Brown6f357d32014-01-15 20:40:55 -08004871 @Override
Jeff Brown970d4132014-07-19 11:33:47 -07004872 public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) {
4873 if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT
4874 || screenBrightness > PowerManager.BRIGHTNESS_ON) {
4875 screenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown6f357d32014-01-15 20:40:55 -08004876 }
Jeff Brown970d4132014-07-19 11:33:47 -07004877 setScreenBrightnessOverrideFromWindowManagerInternal(screenBrightness);
Jeff Brown6f357d32014-01-15 20:40:55 -08004878 }
4879
Jeff Brown6f357d32014-01-15 20:40:55 -08004880 @Override
Jeff Brown970d4132014-07-19 11:33:47 -07004881 public void setDozeOverrideFromDreamManager(int screenState, int screenBrightness) {
4882 switch (screenState) {
4883 case Display.STATE_UNKNOWN:
4884 case Display.STATE_OFF:
4885 case Display.STATE_DOZE:
4886 case Display.STATE_DOZE_SUSPEND:
Chris Phoenix10a4a642017-09-25 13:21:00 -07004887 case Display.STATE_ON_SUSPEND:
Jeff Brown970d4132014-07-19 11:33:47 -07004888 case Display.STATE_ON:
Santos Cordon3107d292016-09-20 15:50:35 -07004889 case Display.STATE_VR:
Jeff Brown970d4132014-07-19 11:33:47 -07004890 break;
4891 default:
4892 screenState = Display.STATE_UNKNOWN;
4893 break;
4894 }
4895 if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT
4896 || screenBrightness > PowerManager.BRIGHTNESS_ON) {
4897 screenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
4898 }
4899 setDozeOverrideFromDreamManagerInternal(screenState, screenBrightness);
4900 }
4901
Jeff Brown6f357d32014-01-15 20:40:55 -08004902 @Override
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07004903 public void setUserInactiveOverrideFromWindowManager() {
4904 setUserInactiveOverrideFromWindowManagerInternal();
4905 }
4906
4907 @Override
Jeff Brown6f357d32014-01-15 20:40:55 -08004908 public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) {
Jeff Brown970d4132014-07-19 11:33:47 -07004909 setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis);
Jeff Brown6f357d32014-01-15 20:40:55 -08004910 }
4911
4912 @Override
Ivan Podogov56bc6d02018-02-26 16:04:24 +00004913 public void setDrawWakeLockOverrideFromSidekick(boolean keepState) {
4914 setDrawWakeLockOverrideFromSidekickInternal(keepState);
4915 }
4916
4917 @Override
Pavel Grafov28939982017-10-03 15:11:52 +01004918 public void setMaximumScreenOffTimeoutFromDeviceAdmin(@UserIdInt int userId, long timeMs) {
4919 setMaximumScreenOffTimeoutFromDeviceAdminInternal(userId, timeMs);
Jeff Brown5ce1cb22014-11-06 19:05:33 -08004920 }
4921
4922 @Override
jackqdyulei455e90a2017-02-09 15:29:16 -08004923 public PowerSaveState getLowPowerState(@ServiceType int serviceType) {
Kweku Adams9f488e22019-01-14 16:25:08 -08004924 return mBatterySaverPolicy.getBatterySaverPolicy(serviceType);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004925 }
4926
4927 @Override
4928 public void registerLowPowerModeObserver(LowPowerModeListener listener) {
Makoto Onuki66a78122017-11-14 15:03:21 -08004929 mBatterySaverController.addListener(listener);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004930 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004931
4932 @Override
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004933 public boolean setDeviceIdleMode(boolean enabled) {
4934 return setDeviceIdleModeInternal(enabled);
4935 }
4936
4937 @Override
4938 public boolean setLightDeviceIdleMode(boolean enabled) {
4939 return setLightDeviceIdleModeInternal(enabled);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07004940 }
4941
4942 @Override
4943 public void setDeviceIdleWhitelist(int[] appids) {
4944 setDeviceIdleWhitelistInternal(appids);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004945 }
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07004946
4947 @Override
Amith Yamasaniaf575b92015-05-29 15:35:26 -07004948 public void setDeviceIdleTempWhitelist(int[] appids) {
4949 setDeviceIdleTempWhitelistInternal(appids);
4950 }
4951
4952 @Override
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07004953 public void startUidChanges() {
4954 startUidChangesInternal();
4955 }
4956
4957 @Override
4958 public void finishUidChanges() {
4959 finishUidChangesInternal();
4960 }
4961
4962 @Override
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07004963 public void updateUidProcState(int uid, int procState) {
4964 updateUidProcStateInternal(uid, procState);
4965 }
4966
4967 @Override
4968 public void uidGone(int uid) {
4969 uidGoneInternal(uid);
4970 }
Michael Wrighta4d22d72015-09-16 23:19:26 +01004971
4972 @Override
Dianne Hackbornd33c7cd2016-10-25 17:13:24 -07004973 public void uidActive(int uid) {
4974 uidActiveInternal(uid);
4975 }
4976
4977 @Override
4978 public void uidIdle(int uid) {
4979 uidIdleInternal(uid);
4980 }
4981
4982 @Override
Michael Wrighta4d22d72015-09-16 23:19:26 +01004983 public void powerHint(int hintId, int data) {
4984 powerHintInternal(hintId, data);
4985 }
Alex Kershaw2418ea92018-10-19 17:17:49 +01004986
4987 @Override
4988 public boolean wasDeviceIdleFor(long ms) {
4989 return wasDeviceIdleForInternal(ms);
4990 }
Santos Cordon623526b2019-04-09 17:02:38 +01004991
4992 @Override
4993 public WakeData getLastWakeup() {
4994 return getLastWakeupInternal();
4995 }
Jeff Brown6f357d32014-01-15 20:40:55 -08004996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997}