blob: ff5a0f967b54f9562628694d298805ece5b60f59 [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
Jeff Brown96307042012-07-27 15:51:34 -070019import android.Manifest;
Tony Mantlerb8009fd2016-03-14 15:55:35 -070020import android.annotation.IntDef;
Jason Monkafae4bd2015-12-15 14:20:06 -050021import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070028import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080029import android.database.ContentObserver;
Jeff Brown3b971592013-01-09 18:46:37 -080030import android.hardware.SensorManager;
31import android.hardware.SystemSensorManager;
Jeff Brown4ccb8232014-01-16 22:16:42 -080032import android.hardware.display.DisplayManagerInternal;
Jeff Brown131206b2014-04-08 17:27:14 -070033import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Jeff Brown96307042012-07-27 15:51:34 -070034import android.net.Uri;
Amith Yamasani8b619832010-09-22 16:11:59 -070035import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070036import android.os.BatteryManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Binder;
38import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.IBinder;
40import android.os.IPowerManager;
Jeff Brown96307042012-07-27 15:51:34 -070041import android.os.Looper;
Jim Miller92e66dd2012-02-21 18:57:12 -080042import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.PowerManager;
Jeff Brown6f357d32014-01-15 20:40:55 -080044import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Process;
46import android.os.RemoteException;
47import android.os.SystemClock;
Nick Kralevichdbcf2d72013-04-18 14:41:40 -070048import android.os.SystemProperties;
Jeff Brown3edf5272014-08-14 19:25:14 -070049import android.os.Trace;
Jeff Brownd4935962012-09-25 13:27:20 -070050import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070051import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.provider.Settings;
Jason Monk5dbd4aa2016-02-07 13:13:39 -050053import android.provider.Settings.Secure;
Daichi Hirono82ab9802016-03-02 13:23:29 +090054import android.provider.Settings.SettingNotFoundException;
Jeff Brown567f7ca2014-01-30 23:38:03 -080055import android.service.dreams.DreamManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
Jason Monkafae4bd2015-12-15 14:20:06 -050058import android.util.SparseIntArray;
Jeff Brown96307042012-07-27 15:51:34 -070059import android.util.TimeUtils;
Jeff Brown037c33e2014-04-09 00:31:55 -070060import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.WindowManagerPolicy;
Daichi Hirono82ab9802016-03-02 13:23:29 +090062
Jason Monkafae4bd2015-12-15 14:20:06 -050063import com.android.internal.app.IAppOpsService;
64import com.android.internal.app.IBatteryStats;
65import com.android.internal.os.BackgroundThread;
Jeff Sharkey3dee8d62016-03-02 13:07:56 -070066import com.android.internal.util.ArrayUtils;
Jason Monkafae4bd2015-12-15 14:20:06 -050067import com.android.server.EventLogTags;
68import com.android.server.ServiceThread;
69import com.android.server.SystemService;
70import com.android.server.Watchdog;
71import com.android.server.am.BatteryStatsService;
72import com.android.server.lights.Light;
73import com.android.server.lights.LightsManager;
Ruchi Kandoi03a04282016-03-14 17:09:17 -070074import com.android.server.vr.VrManagerInternal;
75import com.android.server.vr.VrStateListener;
Jason Monkafae4bd2015-12-15 14:20:06 -050076import libcore.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
Tony Mantlerb8009fd2016-03-14 15:55:35 -070080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -070083import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Michael Wrighta4d22d72015-09-16 23:19:26 +010085import static android.os.PowerManagerInternal.POWER_HINT_INTERACTION;
Jeff Brownfbe96702014-11-19 18:30:58 -080086import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP;
87import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE;
Jeff Brownfbe96702014-11-19 18:30:58 -080088import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING;
Jason Monkafae4bd2015-12-15 14:20:06 -050089import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
Jeff Brownfbe96702014-11-19 18:30:58 -080090
Jeff Brown96307042012-07-27 15:51:34 -070091/**
92 * The power manager service is responsible for coordinating power management
93 * functions on the device.
94 */
Jeff Brown2175e9c2014-09-12 16:11:07 -070095public final class PowerManagerService extends SystemService
Jeff Brown96307042012-07-27 15:51:34 -070096 implements Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final String TAG = "PowerManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Jeff Brown88c997a2012-06-22 13:57:45 -070099 private static final boolean DEBUG = false;
Jeff Brown96307042012-07-27 15:51:34 -0700100 private static final boolean DEBUG_SPEW = DEBUG && true;
Jeff Brown88c997a2012-06-22 13:57:45 -0700101
Jeff Brown96307042012-07-27 15:51:34 -0700102 // Message: Sent when a user activity timeout occurs to update the power state.
103 private static final int MSG_USER_ACTIVITY_TIMEOUT = 1;
Jeff Brown26875502014-01-30 21:47:47 -0800104 // Message: Sent when the device enters or exits a dreaming or dozing state.
Jeff Brown96307042012-07-27 15:51:34 -0700105 private static final int MSG_SANDMAN = 2;
Jeff Browne333e672014-10-28 13:48:55 -0700106 // Message: Sent when the screen brightness boost expires.
107 private static final int MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 3;
Jeff Brown7304c342012-05-11 18:42:42 -0700108
Jeff Brown96307042012-07-27 15:51:34 -0700109 // Dirty bit: mWakeLocks changed
110 private static final int DIRTY_WAKE_LOCKS = 1 << 0;
111 // Dirty bit: mWakefulness changed
112 private static final int DIRTY_WAKEFULNESS = 1 << 1;
113 // Dirty bit: user activity was poked or may have timed out
114 private static final int DIRTY_USER_ACTIVITY = 1 << 2;
115 // Dirty bit: actual display power state was updated asynchronously
116 private static final int DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED = 1 << 3;
117 // Dirty bit: mBootCompleted changed
118 private static final int DIRTY_BOOT_COMPLETED = 1 << 4;
119 // Dirty bit: settings changed
120 private static final int DIRTY_SETTINGS = 1 << 5;
121 // Dirty bit: mIsPowered changed
122 private static final int DIRTY_IS_POWERED = 1 << 6;
123 // Dirty bit: mStayOn changed
124 private static final int DIRTY_STAY_ON = 1 << 7;
125 // Dirty bit: battery state changed
126 private static final int DIRTY_BATTERY_STATE = 1 << 8;
Jeff Brown93cbbb22012-10-04 13:18:36 -0700127 // Dirty bit: proximity state changed
128 private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9;
Jeff Brownec6aa592012-10-17 20:30:25 -0700129 // Dirty bit: dock state changed
Jeff Brown3ee549c2014-09-22 20:14:39 -0700130 private static final int DIRTY_DOCK_STATE = 1 << 10;
Jeff Browne333e672014-10-28 13:48:55 -0700131 // Dirty bit: brightness boost changed
132 private static final int DIRTY_SCREEN_BRIGHTNESS_BOOST = 1 << 11;
Jeff Brown7304c342012-05-11 18:42:42 -0700133
Jeff Brown96307042012-07-27 15:51:34 -0700134 // Summarizes the state of all active wakelocks.
135 private static final int WAKE_LOCK_CPU = 1 << 0;
136 private static final int WAKE_LOCK_SCREEN_BRIGHT = 1 << 1;
137 private static final int WAKE_LOCK_SCREEN_DIM = 1 << 2;
138 private static final int WAKE_LOCK_BUTTON_BRIGHT = 1 << 3;
139 private static final int WAKE_LOCK_PROXIMITY_SCREEN_OFF = 1 << 4;
Jeff Brown10428742012-10-09 15:47:30 -0700140 private static final int WAKE_LOCK_STAY_AWAKE = 1 << 5; // only set if already awake
Jeff Brown26875502014-01-30 21:47:47 -0800141 private static final int WAKE_LOCK_DOZE = 1 << 6;
Jeff Brownc2932a12014-11-20 18:04:05 -0800142 private static final int WAKE_LOCK_DRAW = 1 << 7;
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800143 private static final int WAKE_LOCK_SUSTAINED_PERFORMANCE = 1 << 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Jeff Brown96307042012-07-27 15:51:34 -0700145 // Summarizes the user activity state.
146 private static final int USER_ACTIVITY_SCREEN_BRIGHT = 1 << 0;
147 private static final int USER_ACTIVITY_SCREEN_DIM = 1 << 1;
Jeff Brown05af6ad2014-09-30 20:54:30 -0700148 private static final int USER_ACTIVITY_SCREEN_DREAM = 1 << 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Jeff Brown27736f52014-05-20 17:17:10 -0700150 // Default timeout in milliseconds. This is only used until the settings
151 // provider populates the actual default value (R.integer.def_screen_off_timeout).
Jeff Brown96307042012-07-27 15:51:34 -0700152 private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
Jeff Brown05af6ad2014-09-30 20:54:30 -0700153 private static final int DEFAULT_SLEEP_TIMEOUT = -1;
Jeff Brownff532542012-10-02 21:18:04 -0700154
Jeff Browne333e672014-10-28 13:48:55 -0700155 // Screen brightness boost timeout.
156 // Hardcoded for now until we decide what the right policy should be.
157 // This should perhaps be a setting.
158 private static final int SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 5 * 1000;
159
Jeff Brown0a571122014-08-21 21:50:43 -0700160 // Power hints defined in hardware/libhardware/include/hardware/power.h.
Jeff Brown0a571122014-08-21 21:50:43 -0700161 private static final int POWER_HINT_LOW_POWER = 5;
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800162 private static final int POWER_HINT_SUSTAINED_PERFORMANCE = 6;
Ruchi Kandoi03a04282016-03-14 17:09:17 -0700163 private static final int POWER_HINT_VR_MODE = 7;
Ruchi Kandoi62b8a492014-04-17 18:01:40 -0700164
Jason Monk27bbb2d2015-03-31 16:46:39 -0400165 // Power features defined in hardware/libhardware/include/hardware/power.h.
166 private static final int POWER_FEATURE_DOUBLE_TAP_TO_WAKE = 1;
167
168 // Default setting for double tap to wake.
169 private static final int DEFAULT_DOUBLE_TAP_TO_WAKE = 0;
170
Tony Mantlerb8009fd2016-03-14 15:55:35 -0700171 /** Constants for {@link #shutdownOrRebootInternal} */
172 @Retention(RetentionPolicy.SOURCE)
173 @IntDef({HALT_MODE_SHUTDOWN, HALT_MODE_REBOOT, HALT_MODE_REBOOT_SAFE_MODE})
174 public @interface HaltMode {}
175 private static final int HALT_MODE_SHUTDOWN = 0;
176 private static final int HALT_MODE_REBOOT = 1;
177 private static final int HALT_MODE_REBOOT_SAFE_MODE = 2;
178
Jeff Brownb880d882014-02-10 19:47:07 -0800179 private final Context mContext;
Jeff Brown2c43c332014-06-12 22:38:59 -0700180 private final ServiceThread mHandlerThread;
181 private final PowerManagerHandler mHandler;
182
Adam Lesinski182f73f2013-12-05 16:48:06 -0800183 private LightsManager mLightsManager;
Jeff Brown21392762014-06-13 19:00:36 -0700184 private BatteryManagerInternal mBatteryManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800185 private DisplayManagerInternal mDisplayManagerInternal;
Jeff Brown96307042012-07-27 15:51:34 -0700186 private IBatteryStats mBatteryStats;
Dianne Hackborn713df152013-05-17 11:27:57 -0700187 private IAppOpsService mAppOps;
Jeff Brown96307042012-07-27 15:51:34 -0700188 private WindowManagerPolicy mPolicy;
189 private Notifier mNotifier;
Jeff Brown3b971592013-01-09 18:46:37 -0800190 private WirelessChargerDetector mWirelessChargerDetector;
Jeff Brown96307042012-07-27 15:51:34 -0700191 private SettingsObserver mSettingsObserver;
Jeff Brown567f7ca2014-01-30 23:38:03 -0800192 private DreamManagerInternal mDreamManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800193 private Light mAttentionLight;
Joe Onorato609695d2010-10-14 14:57:49 -0700194
Jeff Brown96307042012-07-27 15:51:34 -0700195 private final Object mLock = new Object();
196
197 // A bitfield that indicates what parts of the power state have
198 // changed and need to be recalculated.
199 private int mDirty;
200
201 // Indicates whether the device is awake or asleep or somewhere in between.
202 // This is distinct from the screen power state, which is managed separately.
203 private int mWakefulness;
Jeff Brownfbe96702014-11-19 18:30:58 -0800204 private boolean mWakefulnessChanging;
Jeff Brown96307042012-07-27 15:51:34 -0700205
Jeff Brown26875502014-01-30 21:47:47 -0800206 // True if the sandman has just been summoned for the first time since entering the
207 // dreaming or dozing state. Indicates whether a new dream should begin.
208 private boolean mSandmanSummoned;
209
Jeff Brown96307042012-07-27 15:51:34 -0700210 // True if MSG_SANDMAN has been scheduled.
211 private boolean mSandmanScheduled;
212
213 // Table of all suspend blockers.
214 // There should only be a few of these.
215 private final ArrayList<SuspendBlocker> mSuspendBlockers = new ArrayList<SuspendBlocker>();
216
217 // Table of all wake locks acquired by applications.
218 private final ArrayList<WakeLock> mWakeLocks = new ArrayList<WakeLock>();
219
220 // A bitfield that summarizes the state of all active wakelocks.
221 private int mWakeLockSummary;
222
223 // If true, instructs the display controller to wait for the proximity sensor to
224 // go negative before turning the screen on.
225 private boolean mRequestWaitForNegativeProximity;
226
227 // Timestamp of the last time the device was awoken or put to sleep.
228 private long mLastWakeTime;
229 private long mLastSleepTime;
230
Jeff Brown96307042012-07-27 15:51:34 -0700231 // Timestamp of the last call to user activity.
232 private long mLastUserActivityTime;
233 private long mLastUserActivityTimeNoChangeLights;
234
Jeff Brown0a571122014-08-21 21:50:43 -0700235 // Timestamp of last interactive power hint.
236 private long mLastInteractivePowerHintTime;
237
Jeff Browne333e672014-10-28 13:48:55 -0700238 // Timestamp of the last screen brightness boost.
239 private long mLastScreenBrightnessBoostTime;
240 private boolean mScreenBrightnessBoostInProgress;
241
Jeff Brown96307042012-07-27 15:51:34 -0700242 // A bitfield that summarizes the effect of the user activity timer.
Jeff Brown96307042012-07-27 15:51:34 -0700243 private int mUserActivitySummary;
244
245 // The desired display power state. The actual state may lag behind the
246 // requested because it is updated asynchronously by the display power controller.
247 private final DisplayPowerRequest mDisplayPowerRequest = new DisplayPowerRequest();
248
Jeff Brown96307042012-07-27 15:51:34 -0700249 // True if the display power state has been fully applied, which means the display
250 // is actually on or actually off or whatever was requested.
251 private boolean mDisplayReady;
252
Jeff Brown27f7a862012-12-12 15:43:31 -0800253 // The suspend blocker used to keep the CPU alive when an application has acquired
254 // a wake lock.
255 private final SuspendBlocker mWakeLockSuspendBlocker;
256
257 // True if the wake lock suspend blocker has been acquired.
Jeff Brown96307042012-07-27 15:51:34 -0700258 private boolean mHoldingWakeLockSuspendBlocker;
259
Jeff Brown27f7a862012-12-12 15:43:31 -0800260 // The suspend blocker used to keep the CPU alive when the display is on, the
261 // display is getting ready or there is user activity (in which case the display
262 // must be on).
263 private final SuspendBlocker mDisplaySuspendBlocker;
264
265 // True if the display suspend blocker has been acquired.
266 private boolean mHoldingDisplaySuspendBlocker;
Jeff Brown96307042012-07-27 15:51:34 -0700267
268 // True if systemReady() has been called.
269 private boolean mSystemReady;
270
271 // True if boot completed occurred. We keep the screen on until this happens.
272 private boolean mBootCompleted;
273
Jeff Sharkey3dee8d62016-03-02 13:07:56 -0700274 // Runnables that should be triggered on boot completed
275 private Runnable[] mBootCompletedRunnables;
276
Jeff Brown26875502014-01-30 21:47:47 -0800277 // True if auto-suspend mode is enabled.
278 // Refer to autosuspend.h.
Jeff Brown037c33e2014-04-09 00:31:55 -0700279 private boolean mHalAutoSuspendModeEnabled;
Jeff Brown26875502014-01-30 21:47:47 -0800280
281 // True if interactive mode is enabled.
282 // Refer to power.h.
Jeff Brown037c33e2014-04-09 00:31:55 -0700283 private boolean mHalInteractiveModeEnabled;
Jeff Brown26875502014-01-30 21:47:47 -0800284
Jeff Brown96307042012-07-27 15:51:34 -0700285 // True if the device is plugged into a power source.
286 private boolean mIsPowered;
287
Jeff Brownf3fb8952012-10-02 20:57:05 -0700288 // The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS.
289 private int mPlugType;
290
Jeff Brown016ff142012-10-15 16:47:22 -0700291 // The current battery level percentage.
292 private int mBatteryLevel;
293
294 // The battery level percentage at the time the dream started.
295 // This is used to terminate a dream and go to sleep if the battery is
296 // draining faster than it is charging and the user activity timeout has expired.
297 private int mBatteryLevelWhenDreamStarted;
298
Jeff Brownec6aa592012-10-17 20:30:25 -0700299 // The current dock state.
300 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
301
Jeff Brown26875502014-01-30 21:47:47 -0800302 // True to decouple auto-suspend mode from the display state.
Jeff Brown037c33e2014-04-09 00:31:55 -0700303 private boolean mDecoupleHalAutoSuspendModeFromDisplayConfig;
Jeff Brown26875502014-01-30 21:47:47 -0800304
305 // True to decouple interactive mode from the display state.
Jeff Brown037c33e2014-04-09 00:31:55 -0700306 private boolean mDecoupleHalInteractiveModeFromDisplayConfig;
Jeff Brown26875502014-01-30 21:47:47 -0800307
Jeff Brown96307042012-07-27 15:51:34 -0700308 // True if the device should wake up when plugged or unplugged.
309 private boolean mWakeUpWhenPluggedOrUnpluggedConfig;
310
Bryce Lee584a4452014-10-21 15:55:55 -0700311 // True if the device should wake up when plugged or unplugged in theater mode.
312 private boolean mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig;
313
Jeff Brownec083212013-09-11 20:45:25 -0700314 // True if the device should suspend when the screen is off due to proximity.
315 private boolean mSuspendWhenScreenOffDueToProximityConfig;
316
Jeff Brown96307042012-07-27 15:51:34 -0700317 // True if dreams are supported on this device.
318 private boolean mDreamsSupportedConfig;
319
John Spurlocked108f32012-10-18 16:49:24 -0400320 // Default value for dreams enabled
321 private boolean mDreamsEnabledByDefaultConfig;
322
323 // Default value for dreams activate-on-sleep
324 private boolean mDreamsActivatedOnSleepByDefaultConfig;
325
326 // Default value for dreams activate-on-dock
327 private boolean mDreamsActivatedOnDockByDefaultConfig;
328
Jeff Brown26875502014-01-30 21:47:47 -0800329 // True if dreams can run while not plugged in.
330 private boolean mDreamsEnabledOnBatteryConfig;
331
332 // Minimum battery level to allow dreaming when powered.
333 // Use -1 to disable this safety feature.
334 private int mDreamsBatteryLevelMinimumWhenPoweredConfig;
335
336 // Minimum battery level to allow dreaming when not powered.
337 // Use -1 to disable this safety feature.
338 private int mDreamsBatteryLevelMinimumWhenNotPoweredConfig;
339
340 // If the battery level drops by this percentage and the user activity timeout
341 // has expired, then assume the device is receiving insufficient current to charge
342 // effectively and terminate the dream. Use -1 to disable this safety feature.
343 private int mDreamsBatteryLevelDrainCutoffConfig;
344
Jeff Brown96307042012-07-27 15:51:34 -0700345 // True if dreams are enabled by the user.
346 private boolean mDreamsEnabledSetting;
347
John Spurlock1a868b72012-08-22 09:56:51 -0400348 // True if dreams should be activated on sleep.
349 private boolean mDreamsActivateOnSleepSetting;
350
Jeff Brownec6aa592012-10-17 20:30:25 -0700351 // True if dreams should be activated on dock.
352 private boolean mDreamsActivateOnDockSetting;
353
Jeff Brown2175e9c2014-09-12 16:11:07 -0700354 // True if doze should not be started until after the screen off transition.
355 private boolean mDozeAfterScreenOffConfig;
356
Jeff Brown27736f52014-05-20 17:17:10 -0700357 // The minimum screen off timeout, in milliseconds.
358 private int mMinimumScreenOffTimeoutConfig;
359
360 // The screen dim duration, in milliseconds.
361 // This is subtracted from the end of the screen off timeout so the
362 // minimum screen off timeout should be longer than this.
363 private int mMaximumScreenDimDurationConfig;
364
365 // The maximum screen dim time expressed as a ratio relative to the screen
366 // off timeout. If the screen off timeout is very short then we want the
367 // dim timeout to also be quite short so that most of the time is spent on.
368 // Otherwise the user won't get much screen on time before dimming occurs.
369 private float mMaximumScreenDimRatioConfig;
370
Jason Monk27bbb2d2015-03-31 16:46:39 -0400371 // Whether device supports double tap to wake.
372 private boolean mSupportsDoubleTapWakeConfig;
373
Jeff Brown96307042012-07-27 15:51:34 -0700374 // The screen off timeout setting value in milliseconds.
375 private int mScreenOffTimeoutSetting;
376
Jeff Brown05af6ad2014-09-30 20:54:30 -0700377 // The sleep timeout setting value in milliseconds.
378 private int mSleepTimeoutSetting;
379
Jeff Brown96307042012-07-27 15:51:34 -0700380 // The maximum allowable screen off timeout according to the device
381 // administration policy. Overrides other settings.
382 private int mMaximumScreenOffTimeoutFromDeviceAdmin = Integer.MAX_VALUE;
383
384 // The stay on while plugged in setting.
385 // A bitfield of battery conditions under which to make the screen stay on.
386 private int mStayOnWhilePluggedInSetting;
387
388 // True if the device should stay on.
389 private boolean mStayOn;
390
Jeff Brown93cbbb22012-10-04 13:18:36 -0700391 // True if the proximity sensor reads a positive result.
392 private boolean mProximityPositive;
393
Jeff Brown96307042012-07-27 15:51:34 -0700394 // Screen brightness setting limits.
395 private int mScreenBrightnessSettingMinimum;
396 private int mScreenBrightnessSettingMaximum;
397 private int mScreenBrightnessSettingDefault;
398
399 // The screen brightness setting, from 0 to 255.
400 // Use -1 if no value has been set.
401 private int mScreenBrightnessSetting;
402
Jeff Brown330560f2012-08-21 22:10:57 -0700403 // The screen auto-brightness adjustment setting, from -1 to 1.
404 // Use 0 if there is no adjustment.
405 private float mScreenAutoBrightnessAdjustmentSetting;
406
Jeff Brown96307042012-07-27 15:51:34 -0700407 // The screen brightness mode.
408 // One of the Settings.System.SCREEN_BRIGHTNESS_MODE_* constants.
409 private int mScreenBrightnessModeSetting;
410
411 // The screen brightness setting override from the window manager
412 // to allow the current foreground activity to override the brightness.
413 // Use -1 to disable.
414 private int mScreenBrightnessOverrideFromWindowManager = -1;
415
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -0700416 // The window manager has determined the user to be inactive via other means.
417 // Set this to false to disable.
418 private boolean mUserInactiveOverrideFromWindowManager;
419
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -0700420 // The next possible user activity timeout after being explicitly told the user is inactive.
421 // Set to -1 when not told the user is inactive since the last period spent dozing or asleep.
422 private long mOverriddenTimeout = -1;
423
Jeff Brown1e3b98d2012-09-30 18:58:59 -0700424 // The user activity timeout override from the window manager
425 // to allow the current foreground activity to override the user activity timeout.
426 // Use -1 to disable.
427 private long mUserActivityTimeoutOverrideFromWindowManager = -1;
428
Jeff Brown96307042012-07-27 15:51:34 -0700429 // The screen brightness setting override from the settings application
430 // to temporarily adjust the brightness until next updated,
431 // Use -1 to disable.
432 private int mTemporaryScreenBrightnessSettingOverride = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433
Jeff Brown330560f2012-08-21 22:10:57 -0700434 // The screen brightness adjustment setting override from the settings
435 // application to temporarily adjust the auto-brightness adjustment factor
436 // until next updated, in the range -1..1.
437 // Use NaN to disable.
438 private float mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = Float.NaN;
439
Jeff Brown970d4132014-07-19 11:33:47 -0700440 // The screen state to use while dozing.
441 private int mDozeScreenStateOverrideFromDreamManager = Display.STATE_UNKNOWN;
442
443 // The screen brightness to use while dozing.
444 private int mDozeScreenBrightnessOverrideFromDreamManager = PowerManager.BRIGHTNESS_DEFAULT;
445
Jeff Brown9ba8d782012-10-01 16:38:23 -0700446 // Time when we last logged a warning about calling userActivity() without permission.
447 private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE;
448
Ruchi Kandoi15aedf52014-05-09 19:57:51 -0700449 // If true, the device is in low power mode.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700450 private boolean mLowPowerModeEnabled;
451
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700452 // Current state of the low power mode setting.
453 private boolean mLowPowerModeSetting;
454
Dianne Hackborn14272302014-06-10 23:13:02 -0700455 // Current state of whether the settings are allowing auto low power mode.
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400456 private boolean mAutoLowPowerModeConfigured;
Dianne Hackborn14272302014-06-10 23:13:02 -0700457
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400458 // The user turned off low power mode below the trigger level
John Spurlock1bb480a2014-08-02 17:12:43 -0400459 private boolean mAutoLowPowerModeSnoozing;
460
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700461 // True if the battery level is currently considered low.
462 private boolean mBatteryLevelLow;
463
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700464 // True if we are currently in device idle mode.
465 private boolean mDeviceIdleMode;
466
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700467 // True if we are currently in light device idle mode.
468 private boolean mLightDeviceIdleMode;
469
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800470 // True if we are currently in sustained performance mode.
471 private boolean mSustainedPerformanceMode;
472
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700473 // Set of app ids that we will always respect the wake locks for.
474 int[] mDeviceIdleWhitelist = new int[0];
475
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700476 // Set of app ids that are temporarily allowed to acquire wakelocks due to high-pri message
477 int[] mDeviceIdleTempWhitelist = new int[0];
478
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700479 private final SparseIntArray mUidState = new SparseIntArray();
480
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800481 private final SparseIntArray mSustainedPerformanceUid = new SparseIntArray();
482
Bryce Lee584a4452014-10-21 15:55:55 -0700483 // True if theater mode is enabled
484 private boolean mTheaterModeEnabled;
485
Jason Monk27bbb2d2015-03-31 16:46:39 -0400486 // True if double tap to wake is enabled
487 private boolean mDoubleTapWakeEnabled;
488
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700489 private final ArrayList<PowerManagerInternal.LowPowerModeListener> mLowPowerModeListeners
490 = new ArrayList<PowerManagerInternal.LowPowerModeListener>();
Ruchi Kandoi15aedf52014-05-09 19:57:51 -0700491
Jason Monk5dbd4aa2016-02-07 13:13:39 -0500492 // True if brightness should be affected by twilight.
493 private boolean mBrightnessUseTwilight;
494
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700495 private native void nativeInit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
Jeff Brown96307042012-07-27 15:51:34 -0700497 private static native void nativeAcquireSuspendBlocker(String name);
498 private static native void nativeReleaseSuspendBlocker(String name);
Jeff Brown9e316a12012-10-08 19:17:06 -0700499 private static native void nativeSetInteractive(boolean enable);
500 private static native void nativeSetAutoSuspend(boolean enable);
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -0700501 private static native void nativeSendPowerHint(int hintId, int data);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400502 private static native void nativeSetFeature(int featureId, int data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
Jeff Brownb880d882014-02-10 19:47:07 -0800504 public PowerManagerService(Context context) {
505 super(context);
506 mContext = context;
Jeff Brown2c43c332014-06-12 22:38:59 -0700507 mHandlerThread = new ServiceThread(TAG,
508 Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/);
509 mHandlerThread.start();
510 mHandler = new PowerManagerHandler(mHandlerThread.getLooper());
511
Jeff Brown96307042012-07-27 15:51:34 -0700512 synchronized (mLock) {
Jeff Brown27f7a862012-12-12 15:43:31 -0800513 mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService.WakeLocks");
514 mDisplaySuspendBlocker = createSuspendBlockerLocked("PowerManagerService.Display");
515 mDisplaySuspendBlocker.acquire();
516 mHoldingDisplaySuspendBlocker = true;
Jeff Brown037c33e2014-04-09 00:31:55 -0700517 mHalAutoSuspendModeEnabled = false;
518 mHalInteractiveModeEnabled = true;
Jeff Brown27f7a862012-12-12 15:43:31 -0800519
Jeff Brown96307042012-07-27 15:51:34 -0700520 mWakefulness = WAKEFULNESS_AWAKE;
Jeff Brown7304c342012-05-11 18:42:42 -0700521
Jeff Brown037c33e2014-04-09 00:31:55 -0700522 nativeInit();
523 nativeSetAutoSuspend(false);
524 nativeSetInteractive(true);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400525 nativeSetFeature(POWER_FEATURE_DOUBLE_TAP_TO_WAKE, 0);
Jeff Brown037c33e2014-04-09 00:31:55 -0700526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528
Jeff Brown6f357d32014-01-15 20:40:55 -0800529 @Override
Jeff Brown6f357d32014-01-15 20:40:55 -0800530 public void onStart() {
531 publishBinderService(Context.POWER_SERVICE, new BinderService());
532 publishLocalService(PowerManagerInternal.class, new LocalService());
Jeff Brown9e316a12012-10-08 19:17:06 -0700533
534 Watchdog.getInstance().addMonitor(this);
Jeff Brown6f357d32014-01-15 20:40:55 -0800535 Watchdog.getInstance().addThread(mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700536 }
Jim Miller92e66dd2012-02-21 18:57:12 -0800537
Jeff Brown6d2a9492014-08-07 19:06:49 -0700538 @Override
539 public void onBootPhase(int phase) {
Craig Mautner6e2f3952014-09-09 14:26:41 -0700540 synchronized (mLock) {
541 if (phase == PHASE_BOOT_COMPLETED) {
542 final long now = SystemClock.uptimeMillis();
543 mBootCompleted = true;
544 mDirty |= DIRTY_BOOT_COMPLETED;
545 userActivityNoUpdateLocked(
546 now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
547 updatePowerStateLocked();
Jeff Sharkey3dee8d62016-03-02 13:07:56 -0700548
549 if (!ArrayUtils.isEmpty(mBootCompletedRunnables)) {
550 Slog.d(TAG, "Posting " + mBootCompletedRunnables.length + " delayed runnables");
551 for (Runnable r : mBootCompletedRunnables) {
552 BackgroundThread.getHandler().post(r);
553 }
554 }
555 mBootCompletedRunnables = null;
Daichi Hirono82ab9802016-03-02 13:23:29 +0900556
557 incrementBootCount();
Craig Mautner6e2f3952014-09-09 14:26:41 -0700558 }
Jeff Brown6d2a9492014-08-07 19:06:49 -0700559 }
560 }
561
Jeff Brown21392762014-06-13 19:00:36 -0700562 public void systemReady(IAppOpsService appOps) {
Jeff Brown96307042012-07-27 15:51:34 -0700563 synchronized (mLock) {
564 mSystemReady = true;
Jeff Brown2c43c332014-06-12 22:38:59 -0700565 mAppOps = appOps;
566 mDreamManager = getLocalService(DreamManagerInternal.class);
567 mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class);
568 mPolicy = getLocalService(WindowManagerPolicy.class);
Jeff Brown21392762014-06-13 19:00:36 -0700569 mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Adam Lesinski182f73f2013-12-05 16:48:06 -0800571 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Jeff Brown96307042012-07-27 15:51:34 -0700572 mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting();
573 mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting();
574 mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700576 SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper());
Jeff Brown3b971592013-01-09 18:46:37 -0800577
Jeff Brownc38c9be2012-10-04 13:16:19 -0700578 // The notifier runs on the system server's main looper so as not to interfere
579 // with the animations and other critical functions of the power manager.
Jeff Brown2c43c332014-06-12 22:38:59 -0700580 mBatteryStats = BatteryStatsService.getService();
Jeff Brownc38c9be2012-10-04 13:16:19 -0700581 mNotifier = new Notifier(Looper.getMainLooper(), mContext, mBatteryStats,
Dianne Hackborn713df152013-05-17 11:27:57 -0700582 mAppOps, createSuspendBlockerLocked("PowerManagerService.Broadcasts"),
Jeff Brown3ee549c2014-09-22 20:14:39 -0700583 mPolicy);
Jeff Brownc38c9be2012-10-04 13:16:19 -0700584
Jeff Brown3b971592013-01-09 18:46:37 -0800585 mWirelessChargerDetector = new WirelessChargerDetector(sensorManager,
Jeff Browndbcc8a22013-10-01 16:16:44 -0700586 createSuspendBlockerLocked("PowerManagerService.WirelessChargerDetector"),
587 mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700588 mSettingsObserver = new SettingsObserver(mHandler);
Jeff Brown2c43c332014-06-12 22:38:59 -0700589
590 mLightsManager = getLocalService(LightsManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800591 mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400592
Jeff Brown131206b2014-04-08 17:27:14 -0700593 // Initialize display power management.
594 mDisplayManagerInternal.initPowerManagement(
595 mDisplayPowerCallbacks, mHandler, sensorManager);
596
Jeff Brown96307042012-07-27 15:51:34 -0700597 // Register for broadcasts from other components of the system.
598 IntentFilter filter = new IntentFilter();
599 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700600 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Jeff Brownd4935962012-09-25 13:27:20 -0700601 mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler);
Joe Onoratob08a1af2010-10-11 19:28:58 -0700602
Jeff Brown96307042012-07-27 15:51:34 -0700603 filter = new IntentFilter();
Dianne Hackbornbe87e2f2012-09-28 16:31:34 -0700604 filter.addAction(Intent.ACTION_DREAMING_STARTED);
605 filter.addAction(Intent.ACTION_DREAMING_STOPPED);
Jeff Brownd4935962012-09-25 13:27:20 -0700606 mContext.registerReceiver(new DreamReceiver(), filter, null, mHandler);
607
608 filter = new IntentFilter();
609 filter.addAction(Intent.ACTION_USER_SWITCHED);
610 mContext.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
John Spurlockf4f6b4c2012-08-25 12:08:03 -0400611
Jeff Brownec6aa592012-10-17 20:30:25 -0700612 filter = new IntentFilter();
613 filter.addAction(Intent.ACTION_DOCK_EVENT);
614 mContext.registerReceiver(new DockReceiver(), filter, null, mHandler);
615
Jeff Brown96307042012-07-27 15:51:34 -0700616 // Register for settings changes.
617 final ContentResolver resolver = mContext.getContentResolver();
618 resolver.registerContentObserver(Settings.Secure.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700619 Settings.Secure.SCREENSAVER_ENABLED),
620 false, mSettingsObserver, UserHandle.USER_ALL);
John Spurlock1a868b72012-08-22 09:56:51 -0400621 resolver.registerContentObserver(Settings.Secure.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700622 Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP),
623 false, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brownec6aa592012-10-17 20:30:25 -0700624 resolver.registerContentObserver(Settings.Secure.getUriFor(
625 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK),
626 false, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown96307042012-07-27 15:51:34 -0700627 resolver.registerContentObserver(Settings.System.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700628 Settings.System.SCREEN_OFF_TIMEOUT),
629 false, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown05af6ad2014-09-30 20:54:30 -0700630 resolver.registerContentObserver(Settings.Secure.getUriFor(
631 Settings.Secure.SLEEP_TIMEOUT),
632 false, mSettingsObserver, UserHandle.USER_ALL);
Christopher Tatead735322012-09-07 14:19:43 -0700633 resolver.registerContentObserver(Settings.Global.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700634 Settings.Global.STAY_ON_WHILE_PLUGGED_IN),
635 false, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown96307042012-07-27 15:51:34 -0700636 resolver.registerContentObserver(Settings.System.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700637 Settings.System.SCREEN_BRIGHTNESS),
638 false, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown96307042012-07-27 15:51:34 -0700639 resolver.registerContentObserver(Settings.System.getUriFor(
Jeff Brownd4935962012-09-25 13:27:20 -0700640 Settings.System.SCREEN_BRIGHTNESS_MODE),
641 false, mSettingsObserver, UserHandle.USER_ALL);
Adrian Roos6dee6052014-05-14 19:44:18 +0200642 resolver.registerContentObserver(Settings.System.getUriFor(
643 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ),
644 false, mSettingsObserver, UserHandle.USER_ALL);
Ruchi Kandoi62b8a492014-04-17 18:01:40 -0700645 resolver.registerContentObserver(Settings.Global.getUriFor(
646 Settings.Global.LOW_POWER_MODE),
647 false, mSettingsObserver, UserHandle.USER_ALL);
Dianne Hackborn14272302014-06-10 23:13:02 -0700648 resolver.registerContentObserver(Settings.Global.getUriFor(
649 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
650 false, mSettingsObserver, UserHandle.USER_ALL);
Bryce Lee584a4452014-10-21 15:55:55 -0700651 resolver.registerContentObserver(Settings.Global.getUriFor(
652 Settings.Global.THEATER_MODE_ON),
653 false, mSettingsObserver, UserHandle.USER_ALL);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400654 resolver.registerContentObserver(Settings.Secure.getUriFor(
655 Settings.Secure.DOUBLE_TAP_TO_WAKE),
656 false, mSettingsObserver, UserHandle.USER_ALL);
Jason Monk5dbd4aa2016-02-07 13:13:39 -0500657 resolver.registerContentObserver(Settings.Secure.getUriFor(
658 Secure.BRIGHTNESS_USE_TWILIGHT),
659 false, mSettingsObserver, UserHandle.USER_ALL);
Ruchi Kandoi03a04282016-03-14 17:09:17 -0700660 getLocalService(VrManagerInternal.class).registerListener(mVrStateListener);
Jeff Brown96307042012-07-27 15:51:34 -0700661 // Go.
662 readConfigurationLocked();
663 updateSettingsLocked();
664 mDirty |= DIRTY_BATTERY_STATE;
665 updatePowerStateLocked();
666 }
667 }
668
669 private void readConfigurationLocked() {
670 final Resources resources = mContext.getResources();
671
Jeff Brown037c33e2014-04-09 00:31:55 -0700672 mDecoupleHalAutoSuspendModeFromDisplayConfig = resources.getBoolean(
Jeff Brown26875502014-01-30 21:47:47 -0800673 com.android.internal.R.bool.config_powerDecoupleAutoSuspendModeFromDisplay);
Jeff Brown037c33e2014-04-09 00:31:55 -0700674 mDecoupleHalInteractiveModeFromDisplayConfig = resources.getBoolean(
Jeff Brown26875502014-01-30 21:47:47 -0800675 com.android.internal.R.bool.config_powerDecoupleInteractiveModeFromDisplay);
Jeff Brown96307042012-07-27 15:51:34 -0700676 mWakeUpWhenPluggedOrUnpluggedConfig = resources.getBoolean(
Joe Onorato6d747652010-10-11 15:15:31 -0700677 com.android.internal.R.bool.config_unplugTurnsOnScreen);
Bryce Lee584a4452014-10-21 15:55:55 -0700678 mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig = resources.getBoolean(
679 com.android.internal.R.bool.config_allowTheaterModeWakeFromUnplug);
Jeff Brownec083212013-09-11 20:45:25 -0700680 mSuspendWhenScreenOffDueToProximityConfig = resources.getBoolean(
681 com.android.internal.R.bool.config_suspendWhenScreenOffDueToProximity);
Jeff Brown96307042012-07-27 15:51:34 -0700682 mDreamsSupportedConfig = resources.getBoolean(
John Spurlocked108f32012-10-18 16:49:24 -0400683 com.android.internal.R.bool.config_dreamsSupported);
684 mDreamsEnabledByDefaultConfig = resources.getBoolean(
685 com.android.internal.R.bool.config_dreamsEnabledByDefault);
686 mDreamsActivatedOnSleepByDefaultConfig = resources.getBoolean(
687 com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault);
688 mDreamsActivatedOnDockByDefaultConfig = resources.getBoolean(
689 com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);
Jeff Brown26875502014-01-30 21:47:47 -0800690 mDreamsEnabledOnBatteryConfig = resources.getBoolean(
691 com.android.internal.R.bool.config_dreamsEnabledOnBattery);
692 mDreamsBatteryLevelMinimumWhenPoweredConfig = resources.getInteger(
693 com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenPowered);
694 mDreamsBatteryLevelMinimumWhenNotPoweredConfig = resources.getInteger(
695 com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenNotPowered);
696 mDreamsBatteryLevelDrainCutoffConfig = resources.getInteger(
697 com.android.internal.R.integer.config_dreamsBatteryLevelDrainCutoff);
Jeff Brown2175e9c2014-09-12 16:11:07 -0700698 mDozeAfterScreenOffConfig = resources.getBoolean(
699 com.android.internal.R.bool.config_dozeAfterScreenOff);
Jeff Brown27736f52014-05-20 17:17:10 -0700700 mMinimumScreenOffTimeoutConfig = resources.getInteger(
701 com.android.internal.R.integer.config_minimumScreenOffTimeout);
702 mMaximumScreenDimDurationConfig = resources.getInteger(
703 com.android.internal.R.integer.config_maximumScreenDimDuration);
704 mMaximumScreenDimRatioConfig = resources.getFraction(
705 com.android.internal.R.fraction.config_maximumScreenDimRatio, 1, 1);
Jason Monk27bbb2d2015-03-31 16:46:39 -0400706 mSupportsDoubleTapWakeConfig = resources.getBoolean(
707 com.android.internal.R.bool.config_supportDoubleTapWake);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709
Jeff Brown96307042012-07-27 15:51:34 -0700710 private void updateSettingsLocked() {
711 final ContentResolver resolver = mContext.getContentResolver();
Jeff Brown7304c342012-05-11 18:42:42 -0700712
Jeff Brownd4935962012-09-25 13:27:20 -0700713 mDreamsEnabledSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400714 Settings.Secure.SCREENSAVER_ENABLED,
715 mDreamsEnabledByDefaultConfig ? 1 : 0,
Jeff Brownd4935962012-09-25 13:27:20 -0700716 UserHandle.USER_CURRENT) != 0);
717 mDreamsActivateOnSleepSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400718 Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP,
719 mDreamsActivatedOnSleepByDefaultConfig ? 1 : 0,
Jeff Brownd4935962012-09-25 13:27:20 -0700720 UserHandle.USER_CURRENT) != 0);
Jeff Brownec6aa592012-10-17 20:30:25 -0700721 mDreamsActivateOnDockSetting = (Settings.Secure.getIntForUser(resolver,
John Spurlocked108f32012-10-18 16:49:24 -0400722 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
723 mDreamsActivatedOnDockByDefaultConfig ? 1 : 0,
Jeff Brownec6aa592012-10-17 20:30:25 -0700724 UserHandle.USER_CURRENT) != 0);
Jeff Brownd4935962012-09-25 13:27:20 -0700725 mScreenOffTimeoutSetting = Settings.System.getIntForUser(resolver,
726 Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT,
727 UserHandle.USER_CURRENT);
Jeff Brown05af6ad2014-09-30 20:54:30 -0700728 mSleepTimeoutSetting = Settings.Secure.getIntForUser(resolver,
729 Settings.Secure.SLEEP_TIMEOUT, DEFAULT_SLEEP_TIMEOUT,
730 UserHandle.USER_CURRENT);
Christopher Tatead735322012-09-07 14:19:43 -0700731 mStayOnWhilePluggedInSetting = Settings.Global.getInt(resolver,
Jeff Brownd4935962012-09-25 13:27:20 -0700732 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_AC);
Bryce Lee584a4452014-10-21 15:55:55 -0700733 mTheaterModeEnabled = Settings.Global.getInt(mContext.getContentResolver(),
734 Settings.Global.THEATER_MODE_ON, 0) == 1;
Jeff Brown7304c342012-05-11 18:42:42 -0700735
Jason Monk27bbb2d2015-03-31 16:46:39 -0400736 if (mSupportsDoubleTapWakeConfig) {
737 boolean doubleTapWakeEnabled = Settings.Secure.getIntForUser(resolver,
738 Settings.Secure.DOUBLE_TAP_TO_WAKE, DEFAULT_DOUBLE_TAP_TO_WAKE,
739 UserHandle.USER_CURRENT) != 0;
740 if (doubleTapWakeEnabled != mDoubleTapWakeEnabled) {
741 mDoubleTapWakeEnabled = doubleTapWakeEnabled;
742 nativeSetFeature(POWER_FEATURE_DOUBLE_TAP_TO_WAKE, mDoubleTapWakeEnabled ? 1 : 0);
743 }
744 }
745
Jeff Brown96307042012-07-27 15:51:34 -0700746 final int oldScreenBrightnessSetting = mScreenBrightnessSetting;
Jeff Brownd4935962012-09-25 13:27:20 -0700747 mScreenBrightnessSetting = Settings.System.getIntForUser(resolver,
748 Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessSettingDefault,
749 UserHandle.USER_CURRENT);
Jeff Brown96307042012-07-27 15:51:34 -0700750 if (oldScreenBrightnessSetting != mScreenBrightnessSetting) {
751 mTemporaryScreenBrightnessSettingOverride = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
Jeff Brown96307042012-07-27 15:51:34 -0700753
Jeff Brown330560f2012-08-21 22:10:57 -0700754 final float oldScreenAutoBrightnessAdjustmentSetting =
755 mScreenAutoBrightnessAdjustmentSetting;
Jeff Brownd4935962012-09-25 13:27:20 -0700756 mScreenAutoBrightnessAdjustmentSetting = Settings.System.getFloatForUser(resolver,
757 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f,
758 UserHandle.USER_CURRENT);
Jeff Brown330560f2012-08-21 22:10:57 -0700759 if (oldScreenAutoBrightnessAdjustmentSetting != mScreenAutoBrightnessAdjustmentSetting) {
Jeff Brown5d03a532012-08-22 13:22:02 -0700760 mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = Float.NaN;
Jeff Brown330560f2012-08-21 22:10:57 -0700761 }
762
Jeff Brownd4935962012-09-25 13:27:20 -0700763 mScreenBrightnessModeSetting = Settings.System.getIntForUser(resolver,
Jeff Brown96307042012-07-27 15:51:34 -0700764 Settings.System.SCREEN_BRIGHTNESS_MODE,
Jeff Brownd4935962012-09-25 13:27:20 -0700765 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);
Jeff Brown96307042012-07-27 15:51:34 -0700766
Jason Monk5dbd4aa2016-02-07 13:13:39 -0500767 mBrightnessUseTwilight = Settings.Secure.getIntForUser(resolver,
768 Secure.BRIGHTNESS_USE_TWILIGHT, 0, UserHandle.USER_CURRENT) != 0;
769
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700770 final boolean lowPowerModeEnabled = Settings.Global.getInt(resolver,
Ruchi Kandoi15aedf52014-05-09 19:57:51 -0700771 Settings.Global.LOW_POWER_MODE, 0) != 0;
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400772 final boolean autoLowPowerModeConfigured = Settings.Global.getInt(resolver,
John Spurlock1bb480a2014-08-02 17:12:43 -0400773 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0;
Dianne Hackborn14272302014-06-10 23:13:02 -0700774 if (lowPowerModeEnabled != mLowPowerModeSetting
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400775 || autoLowPowerModeConfigured != mAutoLowPowerModeConfigured) {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700776 mLowPowerModeSetting = lowPowerModeEnabled;
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400777 mAutoLowPowerModeConfigured = autoLowPowerModeConfigured;
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700778 updateLowPowerModeLocked();
779 }
780
781 mDirty |= DIRTY_SETTINGS;
782 }
783
Jeff Sharkey3dee8d62016-03-02 13:07:56 -0700784 private void postAfterBootCompleted(Runnable r) {
785 if (mBootCompleted) {
786 BackgroundThread.getHandler().post(r);
787 } else {
788 Slog.d(TAG, "Delaying runnable until system is booted");
789 mBootCompletedRunnables = ArrayUtils.appendElement(Runnable.class,
790 mBootCompletedRunnables, r);
791 }
792 }
793
Daichi Hirono82ab9802016-03-02 13:23:29 +0900794 private void updateLowPowerModeLocked() {
John Spurlock1bb480a2014-08-02 17:12:43 -0400795 if (mIsPowered && mLowPowerModeSetting) {
796 if (DEBUG_SPEW) {
797 Slog.d(TAG, "updateLowPowerModeLocked: powered, turning setting off");
798 }
799 // Turn setting off if powered
800 Settings.Global.putInt(mContext.getContentResolver(),
801 Settings.Global.LOW_POWER_MODE, 0);
802 mLowPowerModeSetting = false;
John Spurlock1bb480a2014-08-02 17:12:43 -0400803 }
John Spurlock8d4e6cb2014-09-14 11:10:22 -0400804 final boolean autoLowPowerModeEnabled = !mIsPowered && mAutoLowPowerModeConfigured
805 && !mAutoLowPowerModeSnoozing && mBatteryLevelLow;
806 final boolean lowPowerModeEnabled = mLowPowerModeSetting || autoLowPowerModeEnabled;
807
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700808 if (mLowPowerModeEnabled != lowPowerModeEnabled) {
809 mLowPowerModeEnabled = lowPowerModeEnabled;
Jeff Brown0a571122014-08-21 21:50:43 -0700810 powerHintInternal(POWER_HINT_LOW_POWER, lowPowerModeEnabled ? 1 : 0);
Jeff Sharkey3dee8d62016-03-02 13:07:56 -0700811 postAfterBootCompleted(new Runnable() {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700812 @Override
813 public void run() {
John Spurlock1bb480a2014-08-02 17:12:43 -0400814 Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING)
815 .putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, mLowPowerModeEnabled)
816 .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
817 mContext.sendBroadcast(intent);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700818 ArrayList<PowerManagerInternal.LowPowerModeListener> listeners;
819 synchronized (mLock) {
820 listeners = new ArrayList<PowerManagerInternal.LowPowerModeListener>(
821 mLowPowerModeListeners);
822 }
823 for (int i=0; i<listeners.size(); i++) {
824 listeners.get(i).onLowPowerModeChanged(lowPowerModeEnabled);
825 }
John Spurlock1bb480a2014-08-02 17:12:43 -0400826 intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700827 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
828 mContext.sendBroadcast(intent);
Jason Monkafae4bd2015-12-15 14:20:06 -0500829 // Send internal version that requires signature permission.
830 mContext.sendBroadcastAsUser(new Intent(
831 PowerManager.ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL), UserHandle.ALL,
832 Manifest.permission.DEVICE_POWER);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700833 }
834 });
Ruchi Kandoi62b8a492014-04-17 18:01:40 -0700835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837
Jeff Brown96307042012-07-27 15:51:34 -0700838 private void handleSettingsChangedLocked() {
839 updateSettingsLocked();
840 updatePowerStateLocked();
841 }
842
Dianne Hackborn713df152013-05-17 11:27:57 -0700843 private void acquireWakeLockInternal(IBinder lock, int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800844 WorkSource ws, String historyTag, int uid, int pid) {
Jeff Brown96307042012-07-27 15:51:34 -0700845 synchronized (mLock) {
846 if (DEBUG_SPEW) {
847 Slog.d(TAG, "acquireWakeLockInternal: lock=" + Objects.hashCode(lock)
848 + ", flags=0x" + Integer.toHexString(flags)
849 + ", tag=\"" + tag + "\", ws=" + ws + ", uid=" + uid + ", pid=" + pid);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851
Jeff Brown96307042012-07-27 15:51:34 -0700852 WakeLock wakeLock;
853 int index = findWakeLockIndexLocked(lock);
Dianne Hackborn0d192a92014-07-15 16:39:47 -0700854 boolean notifyAcquire;
Jeff Brown96307042012-07-27 15:51:34 -0700855 if (index >= 0) {
856 wakeLock = mWakeLocks.get(index);
857 if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid)) {
858 // Update existing wake lock. This shouldn't happen but is harmless.
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800859 notifyWakeLockChangingLocked(wakeLock, flags, tag, packageName,
860 uid, pid, ws, historyTag);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800861 wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
Dianne Hackborn0d192a92014-07-15 16:39:47 -0700863 notifyAcquire = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 } else {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800865 wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid);
Jeff Brown96307042012-07-27 15:51:34 -0700866 try {
867 lock.linkToDeath(wakeLock, 0);
868 } catch (RemoteException ex) {
869 throw new IllegalArgumentException("Wake lock is already dead.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
Jeff Brown96307042012-07-27 15:51:34 -0700871 mWakeLocks.add(wakeLock);
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800872
873 if ((flags & PowerManager.WAKE_LOCK_LEVEL_MASK)
874 == PowerManager.SUSTAINED_PERFORMANCE_WAKE_LOCK) {
875 int numberWakelock = mSustainedPerformanceUid.get(uid);
876 mSustainedPerformanceUid.put(uid, numberWakelock + 1);
877 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700878 setWakeLockDisabledStateLocked(wakeLock);
Dianne Hackborn0d192a92014-07-15 16:39:47 -0700879 notifyAcquire = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
Jeff Brownc12035c2014-08-13 18:52:25 -0700882 applyWakeLockFlagsOnAcquireLocked(wakeLock, uid);
Jeff Brown96307042012-07-27 15:51:34 -0700883 mDirty |= DIRTY_WAKE_LOCKS;
884 updatePowerStateLocked();
Dianne Hackborn0d192a92014-07-15 16:39:47 -0700885 if (notifyAcquire) {
886 // This needs to be done last so we are sure we have acquired the
887 // kernel wake lock. Otherwise we have a race where the system may
888 // go to sleep between the time we start the accounting in battery
889 // stats and when we actually get around to telling the kernel to
890 // stay awake.
891 notifyWakeLockAcquiredLocked(wakeLock);
892 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700893 }
894 }
895
Jeff Brownec083212013-09-11 20:45:25 -0700896 @SuppressWarnings("deprecation")
Craig Mautner6edb6db2012-11-20 18:21:12 -0800897 private static boolean isScreenLock(final WakeLock wakeLock) {
898 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
899 case PowerManager.FULL_WAKE_LOCK:
900 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
901 case PowerManager.SCREEN_DIM_WAKE_LOCK:
902 return true;
903 }
904 return false;
905 }
906
Jeff Brownc12035c2014-08-13 18:52:25 -0700907 private void applyWakeLockFlagsOnAcquireLocked(WakeLock wakeLock, int uid) {
Jeff Brown6eade792013-09-10 18:45:25 -0700908 if ((wakeLock.mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0
909 && isScreenLock(wakeLock)) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700910 String opPackageName;
911 int opUid;
912 if (wakeLock.mWorkSource != null && wakeLock.mWorkSource.getName(0) != null) {
913 opPackageName = wakeLock.mWorkSource.getName(0);
914 opUid = wakeLock.mWorkSource.get(0);
915 } else {
916 opPackageName = wakeLock.mPackageName;
917 opUid = wakeLock.mWorkSource != null ? wakeLock.mWorkSource.get(0)
918 : wakeLock.mOwnerUid;
919 }
920 wakeUpNoUpdateLocked(SystemClock.uptimeMillis(), wakeLock.mTag, opUid,
921 opPackageName, opUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
923 }
924
Jeff Brown96307042012-07-27 15:51:34 -0700925 private void releaseWakeLockInternal(IBinder lock, int flags) {
926 synchronized (mLock) {
Jeff Brown96307042012-07-27 15:51:34 -0700927 int index = findWakeLockIndexLocked(lock);
928 if (index < 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -0800929 if (DEBUG_SPEW) {
930 Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
931 + " [not found], flags=0x" + Integer.toHexString(flags));
932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 return;
934 }
Mike Lockwood3333fa42009-10-26 14:50:42 -0400935
Jeff Brown96307042012-07-27 15:51:34 -0700936 WakeLock wakeLock = mWakeLocks.get(index);
Jeff Brown27f7a862012-12-12 15:43:31 -0800937 if (DEBUG_SPEW) {
938 Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
939 + " [" + wakeLock.mTag + "], flags=0x" + Integer.toHexString(flags));
940 }
941
Michael Wright1208e272014-09-08 19:57:50 -0700942 if ((flags & PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -0700943 mRequestWaitForNegativeProximity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
945
Ruchi Kandoi0abc0012016-01-12 14:39:24 -0800946
947 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
948 == PowerManager.SUSTAINED_PERFORMANCE_WAKE_LOCK) {
949 int numberWakelock = mSustainedPerformanceUid.get(wakeLock.mOwnerUid);
950 if (numberWakelock == 1) {
951 mSustainedPerformanceUid.delete(wakeLock.mOwnerUid);
952 } else {
953 mSustainedPerformanceUid.put(wakeLock.mOwnerUid, numberWakelock - 1);
954 }
955 }
956
Jeff Brown3edf5272014-08-14 19:25:14 -0700957 wakeLock.mLock.unlinkToDeath(wakeLock, 0);
958 removeWakeLockLocked(wakeLock, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -0700961
Jeff Brown96307042012-07-27 15:51:34 -0700962 private void handleWakeLockDeath(WakeLock wakeLock) {
963 synchronized (mLock) {
964 if (DEBUG_SPEW) {
Jeff Brown27f7a862012-12-12 15:43:31 -0800965 Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock)
966 + " [" + wakeLock.mTag + "]");
Jeff Brown96307042012-07-27 15:51:34 -0700967 }
968
969 int index = mWakeLocks.indexOf(wakeLock);
970 if (index < 0) {
971 return;
972 }
973
Jeff Brown3edf5272014-08-14 19:25:14 -0700974 removeWakeLockLocked(wakeLock, index);
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700975 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
Jeff Brown3edf5272014-08-14 19:25:14 -0700978 private void removeWakeLockLocked(WakeLock wakeLock, int index) {
979 mWakeLocks.remove(index);
980 notifyWakeLockReleasedLocked(wakeLock);
981
982 applyWakeLockFlagsOnReleaseLocked(wakeLock);
983 mDirty |= DIRTY_WAKE_LOCKS;
984 updatePowerStateLocked();
985 }
986
Jeff Brown96307042012-07-27 15:51:34 -0700987 private void applyWakeLockFlagsOnReleaseLocked(WakeLock wakeLock) {
Jeff Brown6eade792013-09-10 18:45:25 -0700988 if ((wakeLock.mFlags & PowerManager.ON_AFTER_RELEASE) != 0
989 && isScreenLock(wakeLock)) {
Jeff Brown96307042012-07-27 15:51:34 -0700990 userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
991 PowerManager.USER_ACTIVITY_EVENT_OTHER,
992 PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS,
993 wakeLock.mOwnerUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996
Dianne Hackbornd953c532014-08-16 18:17:38 -0700997 private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag,
998 int callingUid) {
Jeff Brown96307042012-07-27 15:51:34 -0700999 synchronized (mLock) {
1000 int index = findWakeLockIndexLocked(lock);
1001 if (index < 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -08001002 if (DEBUG_SPEW) {
1003 Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
1004 + " [not found], ws=" + ws);
1005 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001006 throw new IllegalArgumentException("Wake lock not active: " + lock
1007 + " from uid " + callingUid);
Jeff Brown96307042012-07-27 15:51:34 -07001008 }
1009
1010 WakeLock wakeLock = mWakeLocks.get(index);
Jeff Brown27f7a862012-12-12 15:43:31 -08001011 if (DEBUG_SPEW) {
1012 Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
1013 + " [" + wakeLock.mTag + "], ws=" + ws);
1014 }
1015
Jeff Brown96307042012-07-27 15:51:34 -07001016 if (!wakeLock.hasSameWorkSource(ws)) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001017 notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag,
1018 wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
Dianne Hackborn4590e522014-03-24 13:36:46 -07001019 ws, historyTag);
1020 wakeLock.mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07001021 wakeLock.updateWorkSource(ws);
Jeff Brown96307042012-07-27 15:51:34 -07001022 }
1023 }
1024 }
1025
1026 private int findWakeLockIndexLocked(IBinder lock) {
1027 final int count = mWakeLocks.size();
1028 for (int i = 0; i < count; i++) {
1029 if (mWakeLocks.get(i).mLock == lock) {
1030 return i;
1031 }
1032 }
1033 return -1;
1034 }
1035
1036 private void notifyWakeLockAcquiredLocked(WakeLock wakeLock) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07001037 if (mSystemReady && !wakeLock.mDisabled) {
Dianne Hackborn713df152013-05-17 11:27:57 -07001038 wakeLock.mNotifiedAcquired = true;
1039 mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001040 wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
1041 wakeLock.mHistoryTag);
Jeff Brown96307042012-07-27 15:51:34 -07001042 }
1043 }
1044
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001045 private void notifyWakeLockChangingLocked(WakeLock wakeLock, int flags, String tag,
1046 String packageName, int uid, int pid, WorkSource ws, String historyTag) {
1047 if (mSystemReady && wakeLock.mNotifiedAcquired) {
1048 mNotifier.onWakeLockChanging(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
1049 wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
1050 wakeLock.mHistoryTag, flags, tag, packageName, uid, pid, ws, historyTag);
1051 }
1052 }
1053
Jeff Brown96307042012-07-27 15:51:34 -07001054 private void notifyWakeLockReleasedLocked(WakeLock wakeLock) {
Dianne Hackborn713df152013-05-17 11:27:57 -07001055 if (mSystemReady && wakeLock.mNotifiedAcquired) {
1056 wakeLock.mNotifiedAcquired = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001057 mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag,
1058 wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
1059 wakeLock.mWorkSource, wakeLock.mHistoryTag);
Jeff Brown96307042012-07-27 15:51:34 -07001060 }
1061 }
1062
Jeff Brownec083212013-09-11 20:45:25 -07001063 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07001064 private boolean isWakeLockLevelSupportedInternal(int level) {
1065 synchronized (mLock) {
1066 switch (level) {
1067 case PowerManager.PARTIAL_WAKE_LOCK:
1068 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1069 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1070 case PowerManager.FULL_WAKE_LOCK:
Jeff Brown26875502014-01-30 21:47:47 -08001071 case PowerManager.DOZE_WAKE_LOCK:
Jeff Brownc4bd42c2015-06-12 18:00:11 -07001072 case PowerManager.DRAW_WAKE_LOCK:
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001073 return true;
Jeff Brown96307042012-07-27 15:51:34 -07001074
1075 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown131206b2014-04-08 17:27:14 -07001076 return mSystemReady && mDisplayManagerInternal.isProximitySensorAvailable();
Jeff Brown96307042012-07-27 15:51:34 -07001077
1078 default:
1079 return false;
1080 }
1081 }
1082 }
1083
Jeff Brown96307042012-07-27 15:51:34 -07001084 // Called from native code.
1085 private void userActivityFromNative(long eventTime, int event, int flags) {
1086 userActivityInternal(eventTime, event, flags, Process.SYSTEM_UID);
1087 }
1088
1089 private void userActivityInternal(long eventTime, int event, int flags, int uid) {
1090 synchronized (mLock) {
1091 if (userActivityNoUpdateLocked(eventTime, event, flags, uid)) {
1092 updatePowerStateLocked();
1093 }
1094 }
1095 }
1096
1097 private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) {
1098 if (DEBUG_SPEW) {
1099 Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime
1100 + ", event=" + event + ", flags=0x" + Integer.toHexString(flags)
1101 + ", uid=" + uid);
1102 }
1103
1104 if (eventTime < mLastSleepTime || eventTime < mLastWakeTime
Jeff Brown26875502014-01-30 21:47:47 -08001105 || !mBootCompleted || !mSystemReady) {
Jeff Brown96307042012-07-27 15:51:34 -07001106 return false;
1107 }
1108
Jeff Brown3edf5272014-08-14 19:25:14 -07001109 Trace.traceBegin(Trace.TRACE_TAG_POWER, "userActivity");
1110 try {
Jeff Brown0a571122014-08-21 21:50:43 -07001111 if (eventTime > mLastInteractivePowerHintTime) {
1112 powerHintInternal(POWER_HINT_INTERACTION, 0);
1113 mLastInteractivePowerHintTime = eventTime;
1114 }
1115
Jeff Brown3edf5272014-08-14 19:25:14 -07001116 mNotifier.onUserActivity(event, uid);
Jeff Brown96307042012-07-27 15:51:34 -07001117
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001118 if (mUserInactiveOverrideFromWindowManager) {
1119 mUserInactiveOverrideFromWindowManager = false;
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001120 mOverriddenTimeout = -1;
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001121 }
1122
Jeff Brown0a571122014-08-21 21:50:43 -07001123 if (mWakefulness == WAKEFULNESS_ASLEEP
1124 || mWakefulness == WAKEFULNESS_DOZING
1125 || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) {
1126 return false;
1127 }
1128
Jeff Brown3edf5272014-08-14 19:25:14 -07001129 if ((flags & PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS) != 0) {
1130 if (eventTime > mLastUserActivityTimeNoChangeLights
1131 && eventTime > mLastUserActivityTime) {
1132 mLastUserActivityTimeNoChangeLights = eventTime;
1133 mDirty |= DIRTY_USER_ACTIVITY;
1134 return true;
1135 }
1136 } else {
1137 if (eventTime > mLastUserActivityTime) {
1138 mLastUserActivityTime = eventTime;
1139 mDirty |= DIRTY_USER_ACTIVITY;
1140 return true;
1141 }
Jeff Brown96307042012-07-27 15:51:34 -07001142 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001143 } finally {
1144 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001145 }
1146 return false;
1147 }
1148
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001149 private void wakeUpInternal(long eventTime, String reason, int uid, String opPackageName,
1150 int opUid) {
Jeff Brown96307042012-07-27 15:51:34 -07001151 synchronized (mLock) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001152 if (wakeUpNoUpdateLocked(eventTime, reason, uid, opPackageName, opUid)) {
Jeff Brown96307042012-07-27 15:51:34 -07001153 updatePowerStateLocked();
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001154 }
1155 }
Jeff Brown96307042012-07-27 15:51:34 -07001156 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001157
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001158 private boolean wakeUpNoUpdateLocked(long eventTime, String reason, int reasonUid,
1159 String opPackageName, int opUid) {
Jeff Brown96307042012-07-27 15:51:34 -07001160 if (DEBUG_SPEW) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001161 Slog.d(TAG, "wakeUpNoUpdateLocked: eventTime=" + eventTime + ", uid=" + reasonUid);
Joe Onorato60607a92010-10-23 14:49:30 -07001162 }
Jeff Brown96307042012-07-27 15:51:34 -07001163
1164 if (eventTime < mLastSleepTime || mWakefulness == WAKEFULNESS_AWAKE
1165 || !mBootCompleted || !mSystemReady) {
1166 return false;
1167 }
1168
Jeff Brown3edf5272014-08-14 19:25:14 -07001169 Trace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp");
1170 try {
1171 switch (mWakefulness) {
1172 case WAKEFULNESS_ASLEEP:
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001173 Slog.i(TAG, "Waking up from sleep (uid " + reasonUid +")...");
Jeff Brown3edf5272014-08-14 19:25:14 -07001174 break;
1175 case WAKEFULNESS_DREAMING:
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001176 Slog.i(TAG, "Waking up from dream (uid " + reasonUid +")...");
Jeff Brown3edf5272014-08-14 19:25:14 -07001177 break;
1178 case WAKEFULNESS_DOZING:
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001179 Slog.i(TAG, "Waking up from dozing (uid " + reasonUid +")...");
Jeff Brown3edf5272014-08-14 19:25:14 -07001180 break;
1181 }
1182
1183 mLastWakeTime = eventTime;
Jeff Brownfbe96702014-11-19 18:30:58 -08001184 setWakefulnessLocked(WAKEFULNESS_AWAKE, 0);
Jeff Brown3edf5272014-08-14 19:25:14 -07001185
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001186 mNotifier.onWakeUp(reason, reasonUid, opPackageName, opUid);
Jeff Brown3edf5272014-08-14 19:25:14 -07001187 userActivityNoUpdateLocked(
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001188 eventTime, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, reasonUid);
Jeff Brown3edf5272014-08-14 19:25:14 -07001189 } finally {
1190 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Brown96307042012-07-27 15:51:34 -07001191 }
Jeff Brown96307042012-07-27 15:51:34 -07001192 return true;
1193 }
1194
Jeff Brownc12035c2014-08-13 18:52:25 -07001195 private void goToSleepInternal(long eventTime, int reason, int flags, int uid) {
Jeff Brown96307042012-07-27 15:51:34 -07001196 synchronized (mLock) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001197 if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) {
Jeff Brown96307042012-07-27 15:51:34 -07001198 updatePowerStateLocked();
1199 }
1200 }
1201 }
1202
Jeff Brown26875502014-01-30 21:47:47 -08001203 // This method is called goToSleep for historical reasons but we actually start
1204 // dozing before really going to sleep.
Jeff Brownec083212013-09-11 20:45:25 -07001205 @SuppressWarnings("deprecation")
Jeff Brownc12035c2014-08-13 18:52:25 -07001206 private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) {
Jeff Brown96307042012-07-27 15:51:34 -07001207 if (DEBUG_SPEW) {
Jeff Brown6d8fd272014-05-20 21:24:38 -07001208 Slog.d(TAG, "goToSleepNoUpdateLocked: eventTime=" + eventTime
Jeff Brownc12035c2014-08-13 18:52:25 -07001209 + ", reason=" + reason + ", flags=" + flags + ", uid=" + uid);
Jeff Brown96307042012-07-27 15:51:34 -07001210 }
1211
Jeff Brown26875502014-01-30 21:47:47 -08001212 if (eventTime < mLastWakeTime
1213 || mWakefulness == WAKEFULNESS_ASLEEP
1214 || mWakefulness == WAKEFULNESS_DOZING
Jeff Brown96307042012-07-27 15:51:34 -07001215 || !mBootCompleted || !mSystemReady) {
1216 return false;
1217 }
1218
Jeff Brown3edf5272014-08-14 19:25:14 -07001219 Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep");
1220 try {
1221 switch (reason) {
1222 case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN:
1223 Slog.i(TAG, "Going to sleep due to device administration policy "
1224 + "(uid " + uid +")...");
1225 break;
1226 case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT:
1227 Slog.i(TAG, "Going to sleep due to screen timeout (uid " + uid +")...");
1228 break;
1229 case PowerManager.GO_TO_SLEEP_REASON_LID_SWITCH:
1230 Slog.i(TAG, "Going to sleep due to lid switch (uid " + uid +")...");
1231 break;
1232 case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON:
1233 Slog.i(TAG, "Going to sleep due to power button (uid " + uid +")...");
1234 break;
Filip Gruszczynski9779e122015-03-13 17:39:31 -07001235 case PowerManager.GO_TO_SLEEP_REASON_SLEEP_BUTTON:
1236 Slog.i(TAG, "Going to sleep due to sleep button (uid " + uid +")...");
1237 break;
Jeff Brown3edf5272014-08-14 19:25:14 -07001238 case PowerManager.GO_TO_SLEEP_REASON_HDMI:
1239 Slog.i(TAG, "Going to sleep due to HDMI standby (uid " + uid +")...");
1240 break;
1241 default:
1242 Slog.i(TAG, "Going to sleep by application request (uid " + uid +")...");
1243 reason = PowerManager.GO_TO_SLEEP_REASON_APPLICATION;
Jeff Brown96307042012-07-27 15:51:34 -07001244 break;
1245 }
Jeff Brown6d8fd272014-05-20 21:24:38 -07001246
Jeff Brown3edf5272014-08-14 19:25:14 -07001247 mLastSleepTime = eventTime;
Jeff Brown3edf5272014-08-14 19:25:14 -07001248 mSandmanSummoned = true;
Jeff Brownfbe96702014-11-19 18:30:58 -08001249 setWakefulnessLocked(WAKEFULNESS_DOZING, reason);
Jeff Brown3edf5272014-08-14 19:25:14 -07001250
1251 // Report the number of wake locks that will be cleared by going to sleep.
1252 int numWakeLocksCleared = 0;
1253 final int numWakeLocks = mWakeLocks.size();
1254 for (int i = 0; i < numWakeLocks; i++) {
1255 final WakeLock wakeLock = mWakeLocks.get(i);
1256 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
1257 case PowerManager.FULL_WAKE_LOCK:
1258 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
1259 case PowerManager.SCREEN_DIM_WAKE_LOCK:
1260 numWakeLocksCleared += 1;
1261 break;
1262 }
1263 }
1264 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numWakeLocksCleared);
1265
1266 // Skip dozing if requested.
1267 if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) {
1268 reallyGoToSleepNoUpdateLocked(eventTime, uid);
1269 }
1270 } finally {
1271 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Brown6d8fd272014-05-20 21:24:38 -07001272 }
Jeff Brown96307042012-07-27 15:51:34 -07001273 return true;
1274 }
1275
Jeff Brownc12035c2014-08-13 18:52:25 -07001276 private void napInternal(long eventTime, int uid) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001277 synchronized (mLock) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001278 if (napNoUpdateLocked(eventTime, uid)) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001279 updatePowerStateLocked();
1280 }
1281 }
1282 }
1283
Jeff Brownc12035c2014-08-13 18:52:25 -07001284 private boolean napNoUpdateLocked(long eventTime, int uid) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001285 if (DEBUG_SPEW) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001286 Slog.d(TAG, "napNoUpdateLocked: eventTime=" + eventTime + ", uid=" + uid);
Jeff Brown62c82e42012-09-26 01:30:41 -07001287 }
1288
1289 if (eventTime < mLastWakeTime || mWakefulness != WAKEFULNESS_AWAKE
1290 || !mBootCompleted || !mSystemReady) {
1291 return false;
1292 }
1293
Jeff Brown3edf5272014-08-14 19:25:14 -07001294 Trace.traceBegin(Trace.TRACE_TAG_POWER, "nap");
1295 try {
1296 Slog.i(TAG, "Nap time (uid " + uid +")...");
Jeff Brown62c82e42012-09-26 01:30:41 -07001297
Jeff Brown3edf5272014-08-14 19:25:14 -07001298 mSandmanSummoned = true;
Jeff Brownfbe96702014-11-19 18:30:58 -08001299 setWakefulnessLocked(WAKEFULNESS_DREAMING, 0);
Jeff Brown3edf5272014-08-14 19:25:14 -07001300 } finally {
1301 Trace.traceEnd(Trace.TRACE_TAG_POWER);
1302 }
Jeff Brown26875502014-01-30 21:47:47 -08001303 return true;
1304 }
1305
1306 // Done dozing, drop everything and go to sleep.
Jeff Brownc12035c2014-08-13 18:52:25 -07001307 private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) {
Jeff Brown26875502014-01-30 21:47:47 -08001308 if (DEBUG_SPEW) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001309 Slog.d(TAG, "reallyGoToSleepNoUpdateLocked: eventTime=" + eventTime
1310 + ", uid=" + uid);
Jeff Brown26875502014-01-30 21:47:47 -08001311 }
1312
1313 if (eventTime < mLastWakeTime || mWakefulness == WAKEFULNESS_ASLEEP
1314 || !mBootCompleted || !mSystemReady) {
1315 return false;
1316 }
1317
Jeff Brown3edf5272014-08-14 19:25:14 -07001318 Trace.traceBegin(Trace.TRACE_TAG_POWER, "reallyGoToSleep");
1319 try {
1320 Slog.i(TAG, "Sleeping (uid " + uid +")...");
Jeff Brown26875502014-01-30 21:47:47 -08001321
Jeff Brownfbe96702014-11-19 18:30:58 -08001322 setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
Jeff Brown3edf5272014-08-14 19:25:14 -07001323 } finally {
1324 Trace.traceEnd(Trace.TRACE_TAG_POWER);
1325 }
Jeff Brown62c82e42012-09-26 01:30:41 -07001326 return true;
1327 }
1328
Jeff Brownfbe96702014-11-19 18:30:58 -08001329 private void setWakefulnessLocked(int wakefulness, int reason) {
1330 if (mWakefulness != wakefulness) {
Jeff Brownfbe96702014-11-19 18:30:58 -08001331 mWakefulness = wakefulness;
1332 mWakefulnessChanging = true;
1333 mDirty |= DIRTY_WAKEFULNESS;
1334 mNotifier.onWakefulnessChangeStarted(wakefulness, reason);
Jeff Brown037c33e2014-04-09 00:31:55 -07001335 }
1336 }
1337
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001338 /**
1339 * Logs the time the device would have spent awake before user activity timeout,
1340 * had the system not been told the user was inactive.
1341 */
1342 private void logSleepTimeoutRecapturedLocked() {
1343 final long now = SystemClock.uptimeMillis();
1344 final long savedWakeTimeMs = mOverriddenTimeout - now;
1345 if (savedWakeTimeMs >= 0) {
1346 EventLog.writeEvent(EventLogTags.POWER_SOFT_SLEEP_REQUESTED, savedWakeTimeMs);
1347 mOverriddenTimeout = -1;
1348 }
1349 }
1350
Jeff Brown416c49c2015-05-26 19:50:18 -07001351 private void finishWakefulnessChangeIfNeededLocked() {
1352 if (mWakefulnessChanging && mDisplayReady) {
1353 if (mWakefulness == WAKEFULNESS_DOZING
1354 && (mWakeLockSummary & WAKE_LOCK_DOZE) == 0) {
1355 return; // wait until dream has enabled dozing
1356 }
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001357 if (mWakefulness == WAKEFULNESS_DOZING || mWakefulness == WAKEFULNESS_ASLEEP) {
1358 logSleepTimeoutRecapturedLocked();
1359 }
Jeff Brownfbe96702014-11-19 18:30:58 -08001360 mWakefulnessChanging = false;
Jeff Brown416c49c2015-05-26 19:50:18 -07001361 mNotifier.onWakefulnessChangeFinished();
Jeff Brown037c33e2014-04-09 00:31:55 -07001362 }
1363 }
1364
Jeff Brown96307042012-07-27 15:51:34 -07001365 /**
1366 * Updates the global power state based on dirty bits recorded in mDirty.
1367 *
1368 * This is the main function that performs power state transitions.
1369 * We centralize them here so that we can recompute the power state completely
1370 * each time something important changes, and ensure that we do it the same
1371 * way each time. The point is to gather all of the transition logic here.
1372 */
1373 private void updatePowerStateLocked() {
1374 if (!mSystemReady || mDirty == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001375 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
Jeff Brown2d8a3902014-03-11 23:02:35 -07001377 if (!Thread.holdsLock(mLock)) {
1378 Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked");
1379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
Jeff Brown3edf5272014-08-14 19:25:14 -07001381 Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState");
1382 try {
1383 // Phase 0: Basic state updates.
1384 updateIsPoweredLocked(mDirty);
1385 updateStayOnLocked(mDirty);
Jeff Brown7b5be5e2014-11-12 18:45:31 -08001386 updateScreenBrightnessBoostLocked(mDirty);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387
Jeff Brown3edf5272014-08-14 19:25:14 -07001388 // Phase 1: Update wakefulness.
1389 // Loop because the wake lock and user activity computations are influenced
1390 // by changes in wakefulness.
1391 final long now = SystemClock.uptimeMillis();
1392 int dirtyPhase2 = 0;
1393 for (;;) {
1394 int dirtyPhase1 = mDirty;
1395 dirtyPhase2 |= dirtyPhase1;
1396 mDirty = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
Jeff Brown3edf5272014-08-14 19:25:14 -07001398 updateWakeLockSummaryLocked(dirtyPhase1);
1399 updateUserActivitySummaryLocked(now, dirtyPhase1);
1400 if (!updateWakefulnessLocked(dirtyPhase1)) {
1401 break;
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
Jeff Brown3edf5272014-08-14 19:25:14 -07001404
Jeff Brown2175e9c2014-09-12 16:11:07 -07001405 // Phase 2: Update display power state.
1406 boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2);
Jeff Brown3edf5272014-08-14 19:25:14 -07001407
Jeff Brown2175e9c2014-09-12 16:11:07 -07001408 // Phase 3: Update dream state (depends on display ready signal).
1409 updateDreamLocked(dirtyPhase2, displayBecameReady);
1410
1411 // Phase 4: Send notifications, if needed.
Jeff Brown416c49c2015-05-26 19:50:18 -07001412 finishWakefulnessChangeIfNeededLocked();
Jeff Brown3edf5272014-08-14 19:25:14 -07001413
Jeff Brown2175e9c2014-09-12 16:11:07 -07001414 // Phase 5: Update suspend blocker.
Jeff Brown3edf5272014-08-14 19:25:14 -07001415 // Because we might release the last suspend blocker here, we need to make sure
1416 // we finished everything else first!
1417 updateSuspendBlockerLocked();
1418 } finally {
1419 Trace.traceEnd(Trace.TRACE_TAG_POWER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
Jeff Brown96307042012-07-27 15:51:34 -07001421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
Jim Miller92e66dd2012-02-21 18:57:12 -08001423 /**
Jeff Brown96307042012-07-27 15:51:34 -07001424 * Updates the value of mIsPowered.
1425 * Sets DIRTY_IS_POWERED if a change occurred.
Jim Miller92e66dd2012-02-21 18:57:12 -08001426 */
Jeff Brown96307042012-07-27 15:51:34 -07001427 private void updateIsPoweredLocked(int dirty) {
1428 if ((dirty & DIRTY_BATTERY_STATE) != 0) {
Jeff Brownf3fb8952012-10-02 20:57:05 -07001429 final boolean wasPowered = mIsPowered;
1430 final int oldPlugType = mPlugType;
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001431 final boolean oldLevelLow = mBatteryLevelLow;
Jeff Brown21392762014-06-13 19:00:36 -07001432 mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);
1433 mPlugType = mBatteryManagerInternal.getPlugType();
1434 mBatteryLevel = mBatteryManagerInternal.getBatteryLevel();
1435 mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow();
Jeff Browna4d82042012-10-02 19:11:19 -07001436
Jeff Brown26875502014-01-30 21:47:47 -08001437 if (DEBUG_SPEW) {
Jeff Browna4d82042012-10-02 19:11:19 -07001438 Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered
Jeff Brownf3fb8952012-10-02 20:57:05 -07001439 + ", mIsPowered=" + mIsPowered
1440 + ", oldPlugType=" + oldPlugType
Jeff Brown016ff142012-10-15 16:47:22 -07001441 + ", mPlugType=" + mPlugType
1442 + ", mBatteryLevel=" + mBatteryLevel);
Jeff Browna4d82042012-10-02 19:11:19 -07001443 }
Jim Miller92e66dd2012-02-21 18:57:12 -08001444
Jeff Brownf3fb8952012-10-02 20:57:05 -07001445 if (wasPowered != mIsPowered || oldPlugType != mPlugType) {
Jeff Brown96307042012-07-27 15:51:34 -07001446 mDirty |= DIRTY_IS_POWERED;
1447
Jeff Brown3b971592013-01-09 18:46:37 -08001448 // Update wireless dock detection state.
1449 final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update(
1450 mIsPowered, mPlugType, mBatteryLevel);
1451
Jeff Brown96307042012-07-27 15:51:34 -07001452 // Treat plugging and unplugging the devices as a user activity.
1453 // Users find it disconcerting when they plug or unplug the device
1454 // and it shuts off right away.
1455 // Some devices also wake the device when plugged or unplugged because
1456 // they don't have a charging LED.
1457 final long now = SystemClock.uptimeMillis();
Jeff Brown3b971592013-01-09 18:46:37 -08001458 if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType,
1459 dockedOnWirelessCharger)) {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001460 wakeUpNoUpdateLocked(now, "android.server.power:POWER", Process.SYSTEM_UID,
1461 mContext.getOpPackageName(), Process.SYSTEM_UID);
Jeff Brown96307042012-07-27 15:51:34 -07001462 }
1463 userActivityNoUpdateLocked(
1464 now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
Jeff Brown84e27562012-12-07 13:56:34 -08001465
1466 // Tell the notifier whether wireless charging has started so that
Jeff Brown3b971592013-01-09 18:46:37 -08001467 // it can provide feedback to the user.
1468 if (dockedOnWirelessCharger) {
Jeff Brown84e27562012-12-07 13:56:34 -08001469 mNotifier.onWirelessChargingStarted();
1470 }
Jeff Brown96307042012-07-27 15:51:34 -07001471 }
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001472
Dianne Hackborn14272302014-06-10 23:13:02 -07001473 if (wasPowered != mIsPowered || oldLevelLow != mBatteryLevelLow) {
John Spurlock1bb480a2014-08-02 17:12:43 -04001474 if (oldLevelLow != mBatteryLevelLow && !mBatteryLevelLow) {
1475 if (DEBUG_SPEW) {
1476 Slog.d(TAG, "updateIsPoweredLocked: resetting low power snooze");
1477 }
1478 mAutoLowPowerModeSnoozing = false;
1479 }
Dianne Hackborneb94fa72014-06-03 17:48:12 -07001480 updateLowPowerModeLocked();
1481 }
Jeff Brown96307042012-07-27 15:51:34 -07001482 }
1483 }
1484
Jeff Brown3b971592013-01-09 18:46:37 -08001485 private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(
1486 boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001487 // Don't wake when powered unless configured to do so.
1488 if (!mWakeUpWhenPluggedOrUnpluggedConfig) {
1489 return false;
Jeff Brownf3fb8952012-10-02 20:57:05 -07001490 }
Jeff Brown9fca9e92012-10-05 14:42:56 -07001491
Jeff Brown3b971592013-01-09 18:46:37 -08001492 // Don't wake when undocked from wireless charger.
1493 // See WirelessChargerDetector for justification.
Jeff Brown9fca9e92012-10-05 14:42:56 -07001494 if (wasPowered && !mIsPowered
1495 && oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
1496 return false;
1497 }
Jeff Brown3b971592013-01-09 18:46:37 -08001498
1499 // Don't wake when docked on wireless charger unless we are certain of it.
1500 // See WirelessChargerDetector for justification.
Jeff Brown9fca9e92012-10-05 14:42:56 -07001501 if (!wasPowered && mIsPowered
1502 && mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
Jeff Brown3b971592013-01-09 18:46:37 -08001503 && !dockedOnWirelessCharger) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001504 return false;
1505 }
1506
1507 // If already dreaming and becoming powered, then don't wake.
Jeff Brown26875502014-01-30 21:47:47 -08001508 if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) {
Jeff Brown9fca9e92012-10-05 14:42:56 -07001509 return false;
1510 }
1511
Bryce Lee584a4452014-10-21 15:55:55 -07001512 // Don't wake while theater mode is enabled.
1513 if (mTheaterModeEnabled && !mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig) {
1514 return false;
1515 }
1516
Jeff Brown9fca9e92012-10-05 14:42:56 -07001517 // Otherwise wake up!
1518 return true;
Jeff Brownf3fb8952012-10-02 20:57:05 -07001519 }
1520
Jeff Brown96307042012-07-27 15:51:34 -07001521 /**
1522 * Updates the value of mStayOn.
1523 * Sets DIRTY_STAY_ON if a change occurred.
1524 */
1525 private void updateStayOnLocked(int dirty) {
1526 if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) {
Jeff Brown93cbbb22012-10-04 13:18:36 -07001527 final boolean wasStayOn = mStayOn;
Jeff Brown96307042012-07-27 15:51:34 -07001528 if (mStayOnWhilePluggedInSetting != 0
1529 && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
Jeff Brown21392762014-06-13 19:00:36 -07001530 mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting);
Jeff Brown96307042012-07-27 15:51:34 -07001531 } else {
1532 mStayOn = false;
1533 }
Jeff Brown93cbbb22012-10-04 13:18:36 -07001534
1535 if (mStayOn != wasStayOn) {
1536 mDirty |= DIRTY_STAY_ON;
1537 }
Jeff Brown96307042012-07-27 15:51:34 -07001538 }
1539 }
1540
1541 /**
1542 * Updates the value of mWakeLockSummary to summarize the state of all active wake locks.
1543 * Note that most wake-locks are ignored when the system is asleep.
1544 *
1545 * This function must have no other side-effects.
1546 */
Jeff Brownec083212013-09-11 20:45:25 -07001547 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07001548 private void updateWakeLockSummaryLocked(int dirty) {
1549 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_WAKEFULNESS)) != 0) {
1550 mWakeLockSummary = 0;
1551
1552 final int numWakeLocks = mWakeLocks.size();
1553 for (int i = 0; i < numWakeLocks; i++) {
1554 final WakeLock wakeLock = mWakeLocks.get(i);
1555 switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
1556 case PowerManager.PARTIAL_WAKE_LOCK:
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07001557 if (!wakeLock.mDisabled) {
1558 // We only respect this if the wake lock is not disabled.
1559 mWakeLockSummary |= WAKE_LOCK_CPU;
1560 }
Jeff Brown96307042012-07-27 15:51:34 -07001561 break;
1562 case PowerManager.FULL_WAKE_LOCK:
Jeff Brown037c33e2014-04-09 00:31:55 -07001563 mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_BUTTON_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07001564 break;
1565 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
Jeff Brown037c33e2014-04-09 00:31:55 -07001566 mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07001567 break;
1568 case PowerManager.SCREEN_DIM_WAKE_LOCK:
Jeff Brown037c33e2014-04-09 00:31:55 -07001569 mWakeLockSummary |= WAKE_LOCK_SCREEN_DIM;
Jeff Brown96307042012-07-27 15:51:34 -07001570 break;
1571 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
Jeff Brown037c33e2014-04-09 00:31:55 -07001572 mWakeLockSummary |= WAKE_LOCK_PROXIMITY_SCREEN_OFF;
Jeff Brown96307042012-07-27 15:51:34 -07001573 break;
Jeff Brown26875502014-01-30 21:47:47 -08001574 case PowerManager.DOZE_WAKE_LOCK:
Jeff Brown037c33e2014-04-09 00:31:55 -07001575 mWakeLockSummary |= WAKE_LOCK_DOZE;
Jeff Brown26875502014-01-30 21:47:47 -08001576 break;
Jeff Brownc2932a12014-11-20 18:04:05 -08001577 case PowerManager.DRAW_WAKE_LOCK:
1578 mWakeLockSummary |= WAKE_LOCK_DRAW;
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08001579 case PowerManager.SUSTAINED_PERFORMANCE_WAKE_LOCK:
1580 if (!wakeLock.mDisabled) {
1581 mWakeLockSummary |= WAKE_LOCK_SUSTAINED_PERFORMANCE;
1582 }
Jeff Brownc2932a12014-11-20 18:04:05 -08001583 break;
Jeff Brown96307042012-07-27 15:51:34 -07001584 }
1585 }
1586
Jeff Brown037c33e2014-04-09 00:31:55 -07001587 // Cancel wake locks that make no sense based on the current state.
1588 if (mWakefulness != WAKEFULNESS_DOZING) {
Jeff Brownc2932a12014-11-20 18:04:05 -08001589 mWakeLockSummary &= ~(WAKE_LOCK_DOZE | WAKE_LOCK_DRAW);
Jeff Brown037c33e2014-04-09 00:31:55 -07001590 }
1591 if (mWakefulness == WAKEFULNESS_ASLEEP
1592 || (mWakeLockSummary & WAKE_LOCK_DOZE) != 0) {
1593 mWakeLockSummary &= ~(WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM
1594 | WAKE_LOCK_BUTTON_BRIGHT);
1595 if (mWakefulness == WAKEFULNESS_ASLEEP) {
1596 mWakeLockSummary &= ~WAKE_LOCK_PROXIMITY_SCREEN_OFF;
1597 }
1598 }
1599
1600 // Infer implied wake locks where necessary based on the current state.
1601 if ((mWakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0) {
1602 if (mWakefulness == WAKEFULNESS_AWAKE) {
1603 mWakeLockSummary |= WAKE_LOCK_CPU | WAKE_LOCK_STAY_AWAKE;
1604 } else if (mWakefulness == WAKEFULNESS_DREAMING) {
1605 mWakeLockSummary |= WAKE_LOCK_CPU;
1606 }
1607 }
Jeff Brownc2932a12014-11-20 18:04:05 -08001608 if ((mWakeLockSummary & WAKE_LOCK_DRAW) != 0) {
1609 mWakeLockSummary |= WAKE_LOCK_CPU;
1610 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001611
Jeff Brown96307042012-07-27 15:51:34 -07001612 if (DEBUG_SPEW) {
1613 Slog.d(TAG, "updateWakeLockSummaryLocked: mWakefulness="
Jeff Brownfbe96702014-11-19 18:30:58 -08001614 + PowerManagerInternal.wakefulnessToString(mWakefulness)
Jeff Brown96307042012-07-27 15:51:34 -07001615 + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));
1616 }
1617 }
1618 }
1619
1620 /**
1621 * Updates the value of mUserActivitySummary to summarize the user requested
1622 * state of the system such as whether the screen should be bright or dim.
1623 * Note that user activity is ignored when the system is asleep.
1624 *
1625 * This function must have no other side-effects.
1626 */
1627 private void updateUserActivitySummaryLocked(long now, int dirty) {
1628 // Update the status of the user activity timeout timer.
Jeff Brown037c33e2014-04-09 00:31:55 -07001629 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY
Jeff Browna191aa92014-11-12 23:01:12 -08001630 | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001631 mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);
1632
1633 long nextTimeout = 0;
Jeff Brown26875502014-01-30 21:47:47 -08001634 if (mWakefulness == WAKEFULNESS_AWAKE
Jeff Brown037c33e2014-04-09 00:31:55 -07001635 || mWakefulness == WAKEFULNESS_DREAMING
1636 || mWakefulness == WAKEFULNESS_DOZING) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07001637 final int sleepTimeout = getSleepTimeoutLocked();
1638 final int screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
Jeff Brownff532542012-10-02 21:18:04 -07001639 final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001640 final boolean userInactiveOverride = mUserInactiveOverrideFromWindowManager;
Jeff Brown96307042012-07-27 15:51:34 -07001641
1642 mUserActivitySummary = 0;
1643 if (mLastUserActivityTime >= mLastWakeTime) {
1644 nextTimeout = mLastUserActivityTime
1645 + screenOffTimeout - screenDimDuration;
1646 if (now < nextTimeout) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07001647 mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07001648 } else {
1649 nextTimeout = mLastUserActivityTime + screenOffTimeout;
1650 if (now < nextTimeout) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07001651 mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
Jeff Brown96307042012-07-27 15:51:34 -07001652 }
1653 }
1654 }
1655 if (mUserActivitySummary == 0
1656 && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {
1657 nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;
Jeff Brown970d4132014-07-19 11:33:47 -07001658 if (now < nextTimeout) {
1659 if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT) {
1660 mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
1661 } else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
1662 mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
1663 }
Jeff Brown96307042012-07-27 15:51:34 -07001664 }
1665 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001666
Jeff Brown05af6ad2014-09-30 20:54:30 -07001667 if (mUserActivitySummary == 0) {
1668 if (sleepTimeout >= 0) {
1669 final long anyUserActivity = Math.max(mLastUserActivityTime,
1670 mLastUserActivityTimeNoChangeLights);
1671 if (anyUserActivity >= mLastWakeTime) {
1672 nextTimeout = anyUserActivity + sleepTimeout;
1673 if (now < nextTimeout) {
1674 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
1675 }
1676 }
1677 } else {
1678 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
1679 nextTimeout = -1;
1680 }
1681 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001682
1683 if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) {
Mojtaba2d80edc2015-09-18 16:35:24 -07001684 if ((mUserActivitySummary &
1685 (USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0) {
Nick Armstrong-Crews024872e2015-09-30 16:49:19 -07001686 // Device is being kept awake by recent user activity
1687 if (nextTimeout >= now && mOverriddenTimeout == -1) {
1688 // Save when the next timeout would have occurred
1689 mOverriddenTimeout = nextTimeout;
1690 }
Mojtaba2d80edc2015-09-18 16:35:24 -07001691 }
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07001692 mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;
1693 nextTimeout = -1;
1694 }
1695
Jeff Brown05af6ad2014-09-30 20:54:30 -07001696 if (mUserActivitySummary != 0 && nextTimeout >= 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001697 Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
1698 msg.setAsynchronous(true);
1699 mHandler.sendMessageAtTime(msg, nextTimeout);
1700 }
1701 } else {
1702 mUserActivitySummary = 0;
1703 }
1704
1705 if (DEBUG_SPEW) {
1706 Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
Jeff Brownfbe96702014-11-19 18:30:58 -08001707 + PowerManagerInternal.wakefulnessToString(mWakefulness)
Jeff Brown96307042012-07-27 15:51:34 -07001708 + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
1709 + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
1710 }
1711 }
1712 }
1713
1714 /**
1715 * Called when a user activity timeout has occurred.
1716 * Simply indicates that something about user activity has changed so that the new
1717 * state can be recomputed when the power state is updated.
1718 *
1719 * This function must have no other side-effects besides setting the dirty
1720 * bit and calling update power state. Wakefulness transitions are handled elsewhere.
1721 */
1722 private void handleUserActivityTimeout() { // runs on handler thread
1723 synchronized (mLock) {
1724 if (DEBUG_SPEW) {
1725 Slog.d(TAG, "handleUserActivityTimeout");
1726 }
1727
1728 mDirty |= DIRTY_USER_ACTIVITY;
1729 updatePowerStateLocked();
1730 }
1731 }
1732
Jeff Brown05af6ad2014-09-30 20:54:30 -07001733 private int getSleepTimeoutLocked() {
1734 int timeout = mSleepTimeoutSetting;
1735 if (timeout <= 0) {
1736 return -1;
1737 }
1738 return Math.max(timeout, mMinimumScreenOffTimeoutConfig);
1739 }
1740
1741 private int getScreenOffTimeoutLocked(int sleepTimeout) {
Jeff Brown96307042012-07-27 15:51:34 -07001742 int timeout = mScreenOffTimeoutSetting;
1743 if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
1744 timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
1745 }
Jeff Brown1e3b98d2012-09-30 18:58:59 -07001746 if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
1747 timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
1748 }
Jeff Brown05af6ad2014-09-30 20:54:30 -07001749 if (sleepTimeout >= 0) {
1750 timeout = Math.min(timeout, sleepTimeout);
1751 }
Jeff Brown27736f52014-05-20 17:17:10 -07001752 return Math.max(timeout, mMinimumScreenOffTimeoutConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001753 }
1754
Jeff Brownff532542012-10-02 21:18:04 -07001755 private int getScreenDimDurationLocked(int screenOffTimeout) {
Jeff Brown27736f52014-05-20 17:17:10 -07001756 return Math.min(mMaximumScreenDimDurationConfig,
1757 (int)(screenOffTimeout * mMaximumScreenDimRatioConfig));
Jeff Brown96307042012-07-27 15:51:34 -07001758 }
1759
1760 /**
1761 * Updates the wakefulness of the device.
1762 *
Jeff Brown26875502014-01-30 21:47:47 -08001763 * This is the function that decides whether the device should start dreaming
Jeff Brown96307042012-07-27 15:51:34 -07001764 * based on the current wake locks and user activity state. It may modify mDirty
1765 * if the wakefulness changes.
1766 *
1767 * Returns true if the wakefulness changed and we need to restart power state calculation.
1768 */
1769 private boolean updateWakefulnessLocked(int dirty) {
1770 boolean changed = false;
1771 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED
Jeff Brownec6aa592012-10-17 20:30:25 -07001772 | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE
1773 | DIRTY_DOCK_STATE)) != 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001774 if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {
1775 if (DEBUG_SPEW) {
Jeff Brown62c82e42012-09-26 01:30:41 -07001776 Slog.d(TAG, "updateWakefulnessLocked: Bed time...");
Jeff Brown96307042012-07-27 15:51:34 -07001777 }
Jeff Brown62c82e42012-09-26 01:30:41 -07001778 final long time = SystemClock.uptimeMillis();
Jeff Brownec6aa592012-10-17 20:30:25 -07001779 if (shouldNapAtBedTimeLocked()) {
Jeff Brownc12035c2014-08-13 18:52:25 -07001780 changed = napNoUpdateLocked(time, Process.SYSTEM_UID);
Jeff Brown62c82e42012-09-26 01:30:41 -07001781 } else {
1782 changed = goToSleepNoUpdateLocked(time,
Jeff Brownc12035c2014-08-13 18:52:25 -07001783 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);
Jeff Brown62c82e42012-09-26 01:30:41 -07001784 }
Jeff Brown96307042012-07-27 15:51:34 -07001785 }
1786 }
1787 return changed;
1788 }
1789
Jeff Brown645832d2012-10-03 14:57:03 -07001790 /**
Jeff Brownec6aa592012-10-17 20:30:25 -07001791 * Returns true if the device should automatically nap and start dreaming when the user
1792 * activity timeout has expired and it's bedtime.
1793 */
1794 private boolean shouldNapAtBedTimeLocked() {
1795 return mDreamsActivateOnSleepSetting
1796 || (mDreamsActivateOnDockSetting
1797 && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED);
1798 }
1799
1800 /**
Jeff Brown645832d2012-10-03 14:57:03 -07001801 * Returns true if the device should go to sleep now.
1802 * Also used when exiting a dream to determine whether we should go back
1803 * to being fully awake or else go to sleep for good.
1804 */
Jeff Brown96307042012-07-27 15:51:34 -07001805 private boolean isItBedTimeYetLocked() {
Jeff Brown93cbbb22012-10-04 13:18:36 -07001806 return mBootCompleted && !isBeingKeptAwakeLocked();
Jeff Brown645832d2012-10-03 14:57:03 -07001807 }
1808
1809 /**
Jeff Brown93cbbb22012-10-04 13:18:36 -07001810 * Returns true if the device is being kept awake by a wake lock, user activity
Jeff Brownec083212013-09-11 20:45:25 -07001811 * or the stay on while powered setting. We also keep the phone awake when
1812 * the proximity sensor returns a positive result so that the device does not
1813 * lock while in a phone call. This function only controls whether the device
1814 * will go to sleep or dream which is independent of whether it will be allowed
1815 * to suspend.
Jeff Brown645832d2012-10-03 14:57:03 -07001816 */
Jeff Brown93cbbb22012-10-04 13:18:36 -07001817 private boolean isBeingKeptAwakeLocked() {
Jeff Brown645832d2012-10-03 14:57:03 -07001818 return mStayOn
Jeff Brown93cbbb22012-10-04 13:18:36 -07001819 || mProximityPositive
Jeff Brown10428742012-10-09 15:47:30 -07001820 || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0
Jeff Brown645832d2012-10-03 14:57:03 -07001821 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
Jeff Brown7b5be5e2014-11-12 18:45:31 -08001822 | USER_ACTIVITY_SCREEN_DIM)) != 0
1823 || mScreenBrightnessBoostInProgress;
Jeff Brown96307042012-07-27 15:51:34 -07001824 }
1825
1826 /**
1827 * Determines whether to post a message to the sandman to update the dream state.
1828 */
Jeff Brown2175e9c2014-09-12 16:11:07 -07001829 private void updateDreamLocked(int dirty, boolean displayBecameReady) {
John Spurlockf4f6b4c2012-08-25 12:08:03 -04001830 if ((dirty & (DIRTY_WAKEFULNESS
Jeff Brown645832d2012-10-03 14:57:03 -07001831 | DIRTY_USER_ACTIVITY
1832 | DIRTY_WAKE_LOCKS
1833 | DIRTY_BOOT_COMPLETED
John Spurlockf4f6b4c2012-08-25 12:08:03 -04001834 | DIRTY_SETTINGS
1835 | DIRTY_IS_POWERED
1836 | DIRTY_STAY_ON
Jeff Brown93cbbb22012-10-04 13:18:36 -07001837 | DIRTY_PROXIMITY_POSITIVE
Jeff Brown2175e9c2014-09-12 16:11:07 -07001838 | DIRTY_BATTERY_STATE)) != 0 || displayBecameReady) {
1839 if (mDisplayReady) {
1840 scheduleSandmanLocked();
1841 }
Jeff Brown96307042012-07-27 15:51:34 -07001842 }
1843 }
1844
1845 private void scheduleSandmanLocked() {
1846 if (!mSandmanScheduled) {
1847 mSandmanScheduled = true;
1848 Message msg = mHandler.obtainMessage(MSG_SANDMAN);
1849 msg.setAsynchronous(true);
1850 mHandler.sendMessage(msg);
1851 }
1852 }
1853
1854 /**
Jeff Brown26875502014-01-30 21:47:47 -08001855 * Called when the device enters or exits a dreaming or dozing state.
Jeff Brown96307042012-07-27 15:51:34 -07001856 *
1857 * We do this asynchronously because we must call out of the power manager to start
1858 * the dream and we don't want to hold our lock while doing so. There is a risk that
1859 * the device will wake or go to sleep in the meantime so we have to handle that case.
1860 */
1861 private void handleSandman() { // runs on handler thread
1862 // Handle preconditions.
Jeff Brown26875502014-01-30 21:47:47 -08001863 final boolean startDreaming;
1864 final int wakefulness;
Jeff Brown96307042012-07-27 15:51:34 -07001865 synchronized (mLock) {
1866 mSandmanScheduled = false;
Jeff Brown26875502014-01-30 21:47:47 -08001867 wakefulness = mWakefulness;
Jeff Brown2175e9c2014-09-12 16:11:07 -07001868 if (mSandmanSummoned && mDisplayReady) {
Jeff Brown05af6ad2014-09-30 20:54:30 -07001869 startDreaming = canDreamLocked() || canDozeLocked();
Jeff Brown26875502014-01-30 21:47:47 -08001870 mSandmanSummoned = false;
1871 } else {
1872 startDreaming = false;
Jeff Brown96307042012-07-27 15:51:34 -07001873 }
1874 }
1875
Jeff Brown96307042012-07-27 15:51:34 -07001876 // Start dreaming if needed.
1877 // We only control the dream on the handler thread, so we don't need to worry about
1878 // concurrent attempts to start or stop the dream.
Jeff Brown26875502014-01-30 21:47:47 -08001879 final boolean isDreaming;
Jeff Brown96307042012-07-27 15:51:34 -07001880 if (mDreamManager != null) {
Jeff Brown26875502014-01-30 21:47:47 -08001881 // Restart the dream whenever the sandman is summoned.
Jeff Brown62c82e42012-09-26 01:30:41 -07001882 if (startDreaming) {
Jeff Brownf6d46682014-07-17 22:44:20 -07001883 mDreamManager.stopDream(false /*immediate*/);
Jeff Brown26875502014-01-30 21:47:47 -08001884 mDreamManager.startDream(wakefulness == WAKEFULNESS_DOZING);
Jeff Brown96307042012-07-27 15:51:34 -07001885 }
Jeff Brown62c82e42012-09-26 01:30:41 -07001886 isDreaming = mDreamManager.isDreaming();
Jeff Brown26875502014-01-30 21:47:47 -08001887 } else {
1888 isDreaming = false;
Jeff Brown96307042012-07-27 15:51:34 -07001889 }
1890
1891 // Update dream state.
Jeff Brown96307042012-07-27 15:51:34 -07001892 synchronized (mLock) {
Jeff Brown26875502014-01-30 21:47:47 -08001893 // Remember the initial battery level when the dream started.
1894 if (startDreaming && isDreaming) {
1895 mBatteryLevelWhenDreamStarted = mBatteryLevel;
1896 if (wakefulness == WAKEFULNESS_DOZING) {
1897 Slog.i(TAG, "Dozing...");
1898 } else {
1899 Slog.i(TAG, "Dreaming...");
1900 }
1901 }
1902
1903 // If preconditions changed, wait for the next iteration to determine
1904 // whether the dream should continue (or be restarted).
1905 if (mSandmanSummoned || mWakefulness != wakefulness) {
1906 return; // wait for next cycle
1907 }
1908
1909 // Determine whether the dream should continue.
1910 if (wakefulness == WAKEFULNESS_DREAMING) {
1911 if (isDreaming && canDreamLocked()) {
1912 if (mDreamsBatteryLevelDrainCutoffConfig >= 0
Jeff Brown016ff142012-10-15 16:47:22 -07001913 && mBatteryLevel < mBatteryLevelWhenDreamStarted
Jeff Brown26875502014-01-30 21:47:47 -08001914 - mDreamsBatteryLevelDrainCutoffConfig
1915 && !isBeingKeptAwakeLocked()) {
Jeff Brown016ff142012-10-15 16:47:22 -07001916 // If the user activity timeout expired and the battery appears
1917 // to be draining faster than it is charging then stop dreaming
1918 // and go to sleep.
1919 Slog.i(TAG, "Stopping dream because the battery appears to "
1920 + "be draining faster than it is charging. "
1921 + "Battery level when dream started: "
1922 + mBatteryLevelWhenDreamStarted + "%. "
1923 + "Battery level now: " + mBatteryLevel + "%.");
1924 } else {
Jeff Brown26875502014-01-30 21:47:47 -08001925 return; // continue dreaming
Jeff Brown016ff142012-10-15 16:47:22 -07001926 }
Jeff Brown96307042012-07-27 15:51:34 -07001927 }
Jeff Brown26875502014-01-30 21:47:47 -08001928
1929 // Dream has ended or will be stopped. Update the power state.
1930 if (isItBedTimeYetLocked()) {
1931 goToSleepNoUpdateLocked(SystemClock.uptimeMillis(),
Jeff Brownc12035c2014-08-13 18:52:25 -07001932 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08001933 updatePowerStateLocked();
1934 } else {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001935 wakeUpNoUpdateLocked(SystemClock.uptimeMillis(), "android.server.power:DREAM",
1936 Process.SYSTEM_UID, mContext.getOpPackageName(), Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08001937 updatePowerStateLocked();
1938 }
1939 } else if (wakefulness == WAKEFULNESS_DOZING) {
1940 if (isDreaming) {
1941 return; // continue dozing
1942 }
1943
1944 // Doze has ended or will be stopped. Update the power state.
Jeff Brownc12035c2014-08-13 18:52:25 -07001945 reallyGoToSleepNoUpdateLocked(SystemClock.uptimeMillis(), Process.SYSTEM_UID);
Jeff Brown26875502014-01-30 21:47:47 -08001946 updatePowerStateLocked();
Jeff Brown96307042012-07-27 15:51:34 -07001947 }
Jeff Brown96307042012-07-27 15:51:34 -07001948 }
1949
Jeff Brown26875502014-01-30 21:47:47 -08001950 // Stop dream.
1951 if (isDreaming) {
Jeff Brownf6d46682014-07-17 22:44:20 -07001952 mDreamManager.stopDream(false /*immediate*/);
Jeff Brown96307042012-07-27 15:51:34 -07001953 }
1954 }
1955
1956 /**
Jeff Brown26875502014-01-30 21:47:47 -08001957 * Returns true if the device is allowed to dream in its current state.
Jeff Brown96307042012-07-27 15:51:34 -07001958 */
1959 private boolean canDreamLocked() {
Jeff Brown26875502014-01-30 21:47:47 -08001960 if (mWakefulness != WAKEFULNESS_DREAMING
1961 || !mDreamsSupportedConfig
1962 || !mDreamsEnabledSetting
Jeff Brown970d4132014-07-19 11:33:47 -07001963 || !mDisplayPowerRequest.isBrightOrDim()
Jeff Brown05af6ad2014-09-30 20:54:30 -07001964 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
1965 | USER_ACTIVITY_SCREEN_DIM | USER_ACTIVITY_SCREEN_DREAM)) == 0
Jeff Brown26875502014-01-30 21:47:47 -08001966 || !mBootCompleted) {
1967 return false;
1968 }
1969 if (!isBeingKeptAwakeLocked()) {
Jeff Brown966604f2014-02-24 16:19:51 -08001970 if (!mIsPowered && !mDreamsEnabledOnBatteryConfig) {
Jeff Brown26875502014-01-30 21:47:47 -08001971 return false;
1972 }
1973 if (!mIsPowered
1974 && mDreamsBatteryLevelMinimumWhenNotPoweredConfig >= 0
1975 && mBatteryLevel < mDreamsBatteryLevelMinimumWhenNotPoweredConfig) {
1976 return false;
1977 }
1978 if (mIsPowered
1979 && mDreamsBatteryLevelMinimumWhenPoweredConfig >= 0
1980 && mBatteryLevel < mDreamsBatteryLevelMinimumWhenPoweredConfig) {
1981 return false;
Jeff Brown96307042012-07-27 15:51:34 -07001982 }
1983 }
Jeff Brown26875502014-01-30 21:47:47 -08001984 return true;
Jeff Brown96307042012-07-27 15:51:34 -07001985 }
1986
Jeff Brown96307042012-07-27 15:51:34 -07001987 /**
Jeff Brown05af6ad2014-09-30 20:54:30 -07001988 * Returns true if the device is allowed to doze in its current state.
1989 */
1990 private boolean canDozeLocked() {
1991 return mWakefulness == WAKEFULNESS_DOZING;
1992 }
1993
1994 /**
Jeff Brown96307042012-07-27 15:51:34 -07001995 * Updates the display power state asynchronously.
1996 * When the update is finished, mDisplayReady will be set to true. The display
1997 * controller posts a message to tell us when the actual display power state
1998 * has been updated so we come back here to double-check and finish up.
1999 *
2000 * This function recalculates the display power state each time.
Jeff Brown2175e9c2014-09-12 16:11:07 -07002001 *
2002 * @return True if the display became ready.
Jeff Brown96307042012-07-27 15:51:34 -07002003 */
Jeff Brown2175e9c2014-09-12 16:11:07 -07002004 private boolean updateDisplayPowerStateLocked(int dirty) {
2005 final boolean oldDisplayReady = mDisplayReady;
Jeff Brown96307042012-07-27 15:51:34 -07002006 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS
2007 | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED
Jeff Browne333e672014-10-28 13:48:55 -07002008 | DIRTY_SETTINGS | DIRTY_SCREEN_BRIGHTNESS_BOOST)) != 0) {
Jeff Brown970d4132014-07-19 11:33:47 -07002009 mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked();
Jeff Brown96307042012-07-27 15:51:34 -07002010
Jeff Browne333e672014-10-28 13:48:55 -07002011 // Determine appropriate screen brightness and auto-brightness adjustments.
Jeff Browna576b4d2015-04-23 19:58:06 -07002012 boolean brightnessSetByUser = true;
Jeff Brown96307042012-07-27 15:51:34 -07002013 int screenBrightness = mScreenBrightnessSettingDefault;
Jeff Brown330560f2012-08-21 22:10:57 -07002014 float screenAutoBrightnessAdjustment = 0.0f;
Jeff Brown96307042012-07-27 15:51:34 -07002015 boolean autoBrightness = (mScreenBrightnessModeSetting ==
2016 Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
2017 if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) {
2018 screenBrightness = mScreenBrightnessOverrideFromWindowManager;
2019 autoBrightness = false;
Jeff Browna576b4d2015-04-23 19:58:06 -07002020 brightnessSetByUser = false;
Jeff Brown96307042012-07-27 15:51:34 -07002021 } else if (isValidBrightness(mTemporaryScreenBrightnessSettingOverride)) {
2022 screenBrightness = mTemporaryScreenBrightnessSettingOverride;
2023 } else if (isValidBrightness(mScreenBrightnessSetting)) {
Jeff Brown330560f2012-08-21 22:10:57 -07002024 screenBrightness = mScreenBrightnessSetting;
Jeff Brown96307042012-07-27 15:51:34 -07002025 }
2026 if (autoBrightness) {
2027 screenBrightness = mScreenBrightnessSettingDefault;
Jeff Brown330560f2012-08-21 22:10:57 -07002028 if (isValidAutoBrightnessAdjustment(
2029 mTemporaryScreenAutoBrightnessAdjustmentSettingOverride)) {
2030 screenAutoBrightnessAdjustment =
2031 mTemporaryScreenAutoBrightnessAdjustmentSettingOverride;
2032 } else if (isValidAutoBrightnessAdjustment(
2033 mScreenAutoBrightnessAdjustmentSetting)) {
2034 screenAutoBrightnessAdjustment = mScreenAutoBrightnessAdjustmentSetting;
2035 }
Jeff Brown96307042012-07-27 15:51:34 -07002036 }
2037 screenBrightness = Math.max(Math.min(screenBrightness,
2038 mScreenBrightnessSettingMaximum), mScreenBrightnessSettingMinimum);
Jeff Brown330560f2012-08-21 22:10:57 -07002039 screenAutoBrightnessAdjustment = Math.max(Math.min(
2040 screenAutoBrightnessAdjustment, 1.0f), -1.0f);
Jeff Browne333e672014-10-28 13:48:55 -07002041
2042 // Update display power request.
Jeff Brown96307042012-07-27 15:51:34 -07002043 mDisplayPowerRequest.screenBrightness = screenBrightness;
Jeff Brown330560f2012-08-21 22:10:57 -07002044 mDisplayPowerRequest.screenAutoBrightnessAdjustment =
2045 screenAutoBrightnessAdjustment;
Jeff Browna576b4d2015-04-23 19:58:06 -07002046 mDisplayPowerRequest.brightnessSetByUser = brightnessSetByUser;
Jeff Brown96307042012-07-27 15:51:34 -07002047 mDisplayPowerRequest.useAutoBrightness = autoBrightness;
Jeff Brown96307042012-07-27 15:51:34 -07002048 mDisplayPowerRequest.useProximitySensor = shouldUseProximitySensorLocked();
Ruchi Kandoif974cc82014-05-01 11:25:10 -07002049 mDisplayPowerRequest.lowPowerMode = mLowPowerModeEnabled;
Jeff Browne333e672014-10-28 13:48:55 -07002050 mDisplayPowerRequest.boostScreenBrightness = mScreenBrightnessBoostInProgress;
Jason Monk5dbd4aa2016-02-07 13:13:39 -05002051 mDisplayPowerRequest.useTwilight = mBrightnessUseTwilight;
Ruchi Kandoif974cc82014-05-01 11:25:10 -07002052
Jeff Brown970d4132014-07-19 11:33:47 -07002053 if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE) {
2054 mDisplayPowerRequest.dozeScreenState = mDozeScreenStateOverrideFromDreamManager;
Jeff Brownc2932a12014-11-20 18:04:05 -08002055 if (mDisplayPowerRequest.dozeScreenState == Display.STATE_DOZE_SUSPEND
2056 && (mWakeLockSummary & WAKE_LOCK_DRAW) != 0) {
2057 mDisplayPowerRequest.dozeScreenState = Display.STATE_DOZE;
2058 }
Jeff Brown970d4132014-07-19 11:33:47 -07002059 mDisplayPowerRequest.dozeScreenBrightness =
2060 mDozeScreenBrightnessOverrideFromDreamManager;
2061 } else {
2062 mDisplayPowerRequest.dozeScreenState = Display.STATE_UNKNOWN;
2063 mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
2064 }
2065
Jeff Brown131206b2014-04-08 17:27:14 -07002066 mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest,
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002067 mRequestWaitForNegativeProximity);
Jeff Brown96307042012-07-27 15:51:34 -07002068 mRequestWaitForNegativeProximity = false;
2069
2070 if (DEBUG_SPEW) {
Jeff Browne333e672014-10-28 13:48:55 -07002071 Slog.d(TAG, "updateDisplayPowerStateLocked: mDisplayReady=" + mDisplayReady
Jeff Brown970d4132014-07-19 11:33:47 -07002072 + ", policy=" + mDisplayPowerRequest.policy
Jeff Brown96307042012-07-27 15:51:34 -07002073 + ", mWakefulness=" + mWakefulness
2074 + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)
2075 + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
Jeff Browne333e672014-10-28 13:48:55 -07002076 + ", mBootCompleted=" + mBootCompleted
2077 + ", mScreenBrightnessBoostInProgress="
2078 + mScreenBrightnessBoostInProgress);
Jeff Brown96307042012-07-27 15:51:34 -07002079 }
2080 }
Jeff Brown2175e9c2014-09-12 16:11:07 -07002081 return mDisplayReady && !oldDisplayReady;
Jeff Brown96307042012-07-27 15:51:34 -07002082 }
2083
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002084 private void updateScreenBrightnessBoostLocked(int dirty) {
2085 if ((dirty & DIRTY_SCREEN_BRIGHTNESS_BOOST) != 0) {
2086 if (mScreenBrightnessBoostInProgress) {
2087 final long now = SystemClock.uptimeMillis();
2088 mHandler.removeMessages(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT);
2089 if (mLastScreenBrightnessBoostTime > mLastSleepTime) {
2090 final long boostTimeout = mLastScreenBrightnessBoostTime +
2091 SCREEN_BRIGHTNESS_BOOST_TIMEOUT;
2092 if (boostTimeout > now) {
2093 Message msg = mHandler.obtainMessage(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT);
2094 msg.setAsynchronous(true);
2095 mHandler.sendMessageAtTime(msg, boostTimeout);
2096 return;
2097 }
Jeff Browne333e672014-10-28 13:48:55 -07002098 }
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002099 mScreenBrightnessBoostInProgress = false;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002100 mNotifier.onScreenBrightnessBoostChanged();
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002101 userActivityNoUpdateLocked(now,
2102 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
Jeff Browne333e672014-10-28 13:48:55 -07002103 }
Jeff Browne333e672014-10-28 13:48:55 -07002104 }
2105 }
2106
Jeff Brown96307042012-07-27 15:51:34 -07002107 private static boolean isValidBrightness(int value) {
2108 return value >= 0 && value <= 255;
2109 }
2110
Jeff Brown330560f2012-08-21 22:10:57 -07002111 private static boolean isValidAutoBrightnessAdjustment(float value) {
Jeff Brown5d03a532012-08-22 13:22:02 -07002112 // Handles NaN by always returning false.
2113 return value >= -1.0f && value <= 1.0f;
Jeff Brown330560f2012-08-21 22:10:57 -07002114 }
2115
Jeff Brown970d4132014-07-19 11:33:47 -07002116 private int getDesiredScreenPolicyLocked() {
Jeff Brown96307042012-07-27 15:51:34 -07002117 if (mWakefulness == WAKEFULNESS_ASLEEP) {
Jeff Brown970d4132014-07-19 11:33:47 -07002118 return DisplayPowerRequest.POLICY_OFF;
Jeff Brown96307042012-07-27 15:51:34 -07002119 }
2120
Jeff Brown2175e9c2014-09-12 16:11:07 -07002121 if (mWakefulness == WAKEFULNESS_DOZING) {
2122 if ((mWakeLockSummary & WAKE_LOCK_DOZE) != 0) {
2123 return DisplayPowerRequest.POLICY_DOZE;
2124 }
2125 if (mDozeAfterScreenOffConfig) {
2126 return DisplayPowerRequest.POLICY_OFF;
2127 }
2128 // Fall through and preserve the current screen policy if not configured to
2129 // doze after screen off. This causes the screen off transition to be skipped.
Jeff Brown26875502014-01-30 21:47:47 -08002130 }
2131
Jeff Brown96307042012-07-27 15:51:34 -07002132 if ((mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0
2133 || (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002134 || !mBootCompleted
2135 || mScreenBrightnessBoostInProgress) {
Jeff Brown970d4132014-07-19 11:33:47 -07002136 return DisplayPowerRequest.POLICY_BRIGHT;
Jeff Brown96307042012-07-27 15:51:34 -07002137 }
2138
Jeff Brown970d4132014-07-19 11:33:47 -07002139 return DisplayPowerRequest.POLICY_DIM;
Jeff Brown96307042012-07-27 15:51:34 -07002140 }
2141
Jeff Brown131206b2014-04-08 17:27:14 -07002142 private final DisplayManagerInternal.DisplayPowerCallbacks mDisplayPowerCallbacks =
2143 new DisplayManagerInternal.DisplayPowerCallbacks() {
Jeff Brown037c33e2014-04-09 00:31:55 -07002144 private int mDisplayState = Display.STATE_UNKNOWN;
Jeff Brown131206b2014-04-08 17:27:14 -07002145
Jeff Brown96307042012-07-27 15:51:34 -07002146 @Override
2147 public void onStateChanged() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002148 synchronized (mLock) {
2149 mDirty |= DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED;
2150 updatePowerStateLocked();
2151 }
Jim Miller92e66dd2012-02-21 18:57:12 -08002152 }
2153
2154 @Override
Jeff Brown93cbbb22012-10-04 13:18:36 -07002155 public void onProximityPositive() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002156 synchronized (mLock) {
2157 mProximityPositive = true;
2158 mDirty |= DIRTY_PROXIMITY_POSITIVE;
2159 updatePowerStateLocked();
2160 }
Jeff Brown93cbbb22012-10-04 13:18:36 -07002161 }
2162
2163 @Override
Jeff Brown96307042012-07-27 15:51:34 -07002164 public void onProximityNegative() {
Jeff Brownd91e4172013-07-16 15:18:19 -07002165 synchronized (mLock) {
2166 mProximityPositive = false;
2167 mDirty |= DIRTY_PROXIMITY_POSITIVE;
2168 userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
2169 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
2170 updatePowerStateLocked();
2171 }
Jeff Brown96307042012-07-27 15:51:34 -07002172 }
Jeff Brown131206b2014-04-08 17:27:14 -07002173
2174 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002175 public void onDisplayStateChange(int state) {
2176 // This method is only needed to support legacy display blanking behavior
2177 // where the display's power state is coupled to suspend or to the power HAL.
2178 // The order of operations matters here.
2179 synchronized (mLock) {
2180 if (mDisplayState != state) {
2181 mDisplayState = state;
2182 if (state == Display.STATE_OFF) {
2183 if (!mDecoupleHalInteractiveModeFromDisplayConfig) {
2184 setHalInteractiveModeLocked(false);
2185 }
2186 if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2187 setHalAutoSuspendModeLocked(true);
2188 }
2189 } else {
2190 if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2191 setHalAutoSuspendModeLocked(false);
2192 }
2193 if (!mDecoupleHalInteractiveModeFromDisplayConfig) {
2194 setHalInteractiveModeLocked(true);
2195 }
2196 }
2197 }
2198 }
2199 }
2200
2201 @Override
Jeff Brown131206b2014-04-08 17:27:14 -07002202 public void acquireSuspendBlocker() {
2203 mDisplaySuspendBlocker.acquire();
2204 }
2205
2206 @Override
2207 public void releaseSuspendBlocker() {
2208 mDisplaySuspendBlocker.release();
2209 }
2210
2211 @Override
Jeff Brown131206b2014-04-08 17:27:14 -07002212 public String toString() {
2213 synchronized (this) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002214 return "state=" + Display.stateToString(mDisplayState);
Jeff Brown131206b2014-04-08 17:27:14 -07002215 }
2216 }
Jeff Brown96307042012-07-27 15:51:34 -07002217 };
Jim Miller92e66dd2012-02-21 18:57:12 -08002218
Jeff Brown96307042012-07-27 15:51:34 -07002219 private boolean shouldUseProximitySensorLocked() {
2220 return (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0;
2221 }
Jim Miller92e66dd2012-02-21 18:57:12 -08002222
Jeff Brown96307042012-07-27 15:51:34 -07002223 /**
2224 * Updates the suspend blocker that keeps the CPU alive.
2225 *
2226 * This function must have no other side-effects.
2227 */
2228 private void updateSuspendBlockerLocked() {
Jeff Brownec083212013-09-11 20:45:25 -07002229 final boolean needWakeLockSuspendBlocker = ((mWakeLockSummary & WAKE_LOCK_CPU) != 0);
Jeff Brown26875502014-01-30 21:47:47 -08002230 final boolean needDisplaySuspendBlocker = needDisplaySuspendBlockerLocked();
2231 final boolean autoSuspend = !needDisplaySuspendBlocker;
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002232 final boolean interactive = mDisplayPowerRequest.isBrightOrDim();
Jeff Brown26875502014-01-30 21:47:47 -08002233
2234 // Disable auto-suspend if needed.
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002235 // FIXME We should consider just leaving auto-suspend enabled forever since
2236 // we already hold the necessary wakelocks.
2237 if (!autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2238 setHalAutoSuspendModeLocked(false);
Jeff Brown26875502014-01-30 21:47:47 -08002239 }
Jeff Brown27f7a862012-12-12 15:43:31 -08002240
2241 // First acquire suspend blockers if needed.
2242 if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) {
2243 mWakeLockSuspendBlocker.acquire();
2244 mHoldingWakeLockSuspendBlocker = true;
2245 }
2246 if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) {
2247 mDisplaySuspendBlocker.acquire();
2248 mHoldingDisplaySuspendBlocker = true;
2249 }
2250
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002251 // Inform the power HAL about interactive mode.
2252 // Although we could set interactive strictly based on the wakefulness
2253 // as reported by isInteractive(), it is actually more desirable to track
2254 // the display policy state instead so that the interactive state observed
2255 // by the HAL more accurately tracks transitions between AWAKE and DOZING.
2256 // Refer to getDesiredScreenPolicyLocked() for details.
2257 if (mDecoupleHalInteractiveModeFromDisplayConfig) {
2258 // When becoming non-interactive, we want to defer sending this signal
2259 // until the display is actually ready so that all transitions have
2260 // completed. This is probably a good sign that things have gotten
2261 // too tangled over here...
2262 if (interactive || mDisplayReady) {
2263 setHalInteractiveModeLocked(interactive);
2264 }
2265 }
2266
Jeff Brown27f7a862012-12-12 15:43:31 -08002267 // Then release suspend blockers if needed.
2268 if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) {
2269 mWakeLockSuspendBlocker.release();
2270 mHoldingWakeLockSuspendBlocker = false;
2271 }
2272 if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) {
2273 mDisplaySuspendBlocker.release();
2274 mHoldingDisplaySuspendBlocker = false;
2275 }
Jeff Brown26875502014-01-30 21:47:47 -08002276
2277 // Enable auto-suspend if needed.
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002278 if (autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
2279 setHalAutoSuspendModeLocked(true);
Jeff Brown26875502014-01-30 21:47:47 -08002280 }
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002281
2282 if (mSustainedPerformanceMode
2283 && (mWakeLockSummary & WAKE_LOCK_SUSTAINED_PERFORMANCE) == 0) {
2284 setSustainedPerformanceModeLocked(false);
2285 } else if (!mSustainedPerformanceMode
2286 && (mWakeLockSummary & WAKE_LOCK_SUSTAINED_PERFORMANCE) != 0) {
2287 setSustainedPerformanceModeLocked(true);
2288 }
Jeff Brown96307042012-07-27 15:51:34 -07002289 }
2290
Jeff Brownec083212013-09-11 20:45:25 -07002291 /**
2292 * Return true if we must keep a suspend blocker active on behalf of the display.
2293 * We do so if the screen is on or is in transition between states.
2294 */
Jeff Brown26875502014-01-30 21:47:47 -08002295 private boolean needDisplaySuspendBlockerLocked() {
Jeff Brownec083212013-09-11 20:45:25 -07002296 if (!mDisplayReady) {
2297 return true;
2298 }
Jeff Brown970d4132014-07-19 11:33:47 -07002299 if (mDisplayPowerRequest.isBrightOrDim()) {
Jeff Brownec083212013-09-11 20:45:25 -07002300 // If we asked for the screen to be on but it is off due to the proximity
2301 // sensor then we may suspend but only if the configuration allows it.
2302 // On some hardware it may not be safe to suspend because the proximity
2303 // sensor may not be correctly configured as a wake-up source.
2304 if (!mDisplayPowerRequest.useProximitySensor || !mProximityPositive
2305 || !mSuspendWhenScreenOffDueToProximityConfig) {
2306 return true;
2307 }
2308 }
Jeff Brown7b5be5e2014-11-12 18:45:31 -08002309 if (mScreenBrightnessBoostInProgress) {
2310 return true;
2311 }
Jeff Brown26875502014-01-30 21:47:47 -08002312 // Let the system suspend if the screen is off or dozing.
Jeff Brownec083212013-09-11 20:45:25 -07002313 return false;
2314 }
2315
Jeff Brown037c33e2014-04-09 00:31:55 -07002316 private void setHalAutoSuspendModeLocked(boolean enable) {
2317 if (enable != mHalAutoSuspendModeEnabled) {
Jeff Brown26875502014-01-30 21:47:47 -08002318 if (DEBUG) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002319 Slog.d(TAG, "Setting HAL auto-suspend mode to " + enable);
Jeff Brown26875502014-01-30 21:47:47 -08002320 }
Jeff Brown037c33e2014-04-09 00:31:55 -07002321 mHalAutoSuspendModeEnabled = enable;
Jeff Brown3edf5272014-08-14 19:25:14 -07002322 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalAutoSuspend(" + enable + ")");
2323 try {
2324 nativeSetAutoSuspend(enable);
2325 } finally {
2326 Trace.traceEnd(Trace.TRACE_TAG_POWER);
2327 }
Jeff Brown26875502014-01-30 21:47:47 -08002328 }
2329 }
2330
Jeff Brown037c33e2014-04-09 00:31:55 -07002331 private void setHalInteractiveModeLocked(boolean enable) {
2332 if (enable != mHalInteractiveModeEnabled) {
Jeff Brown26875502014-01-30 21:47:47 -08002333 if (DEBUG) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002334 Slog.d(TAG, "Setting HAL interactive mode to " + enable);
Jeff Brown26875502014-01-30 21:47:47 -08002335 }
Jeff Brown037c33e2014-04-09 00:31:55 -07002336 mHalInteractiveModeEnabled = enable;
Jeff Brown3edf5272014-08-14 19:25:14 -07002337 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalInteractive(" + enable + ")");
2338 try {
2339 nativeSetInteractive(enable);
2340 } finally {
2341 Trace.traceEnd(Trace.TRACE_TAG_POWER);
2342 }
Jeff Brown26875502014-01-30 21:47:47 -08002343 }
2344 }
2345
Jeff Brown037c33e2014-04-09 00:31:55 -07002346 private boolean isInteractiveInternal() {
Jeff Brown96307042012-07-27 15:51:34 -07002347 synchronized (mLock) {
Jeff Brownfbe96702014-11-19 18:30:58 -08002348 return PowerManagerInternal.isInteractive(mWakefulness);
Mike Lockwoodb2865412010-02-02 22:40:33 -05002349 }
2350 }
2351
Dianne Hackborneb94fa72014-06-03 17:48:12 -07002352 private boolean isLowPowerModeInternal() {
2353 synchronized (mLock) {
2354 return mLowPowerModeEnabled;
2355 }
2356 }
2357
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002358 private boolean setLowPowerModeInternal(boolean mode) {
2359 synchronized (mLock) {
2360 if (DEBUG) Slog.d(TAG, "setLowPowerModeInternal " + mode + " mIsPowered=" + mIsPowered);
2361 if (mIsPowered) {
2362 return false;
2363 }
2364 Settings.Global.putInt(mContext.getContentResolver(),
2365 Settings.Global.LOW_POWER_MODE, mode ? 1 : 0);
2366 mLowPowerModeSetting = mode;
2367
2368 if (mAutoLowPowerModeConfigured && mBatteryLevelLow) {
2369 if (mode && mAutoLowPowerModeSnoozing) {
2370 if (DEBUG_SPEW) {
2371 Slog.d(TAG, "setLowPowerModeInternal: clearing low power mode snooze");
2372 }
2373 mAutoLowPowerModeSnoozing = false;
2374 } else if (!mode && !mAutoLowPowerModeSnoozing) {
2375 if (DEBUG_SPEW) {
2376 Slog.d(TAG, "setLowPowerModeInternal: snoozing low power mode");
2377 }
2378 mAutoLowPowerModeSnoozing = true;
2379 }
2380 }
2381
2382 updateLowPowerModeLocked();
2383 return true;
2384 }
2385 }
2386
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002387 private void setSustainedPerformanceModeLocked(boolean mode) {
2388 mSustainedPerformanceMode = mode;
2389 powerHintInternal(POWER_HINT_SUSTAINED_PERFORMANCE,
2390 mSustainedPerformanceMode ? 1 : 0);
2391 }
2392
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002393 boolean isDeviceIdleModeInternal() {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002394 synchronized (mLock) {
2395 return mDeviceIdleMode;
2396 }
2397 }
2398
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002399 boolean isLightDeviceIdleModeInternal() {
2400 synchronized (mLock) {
2401 return mLightDeviceIdleMode;
2402 }
2403 }
2404
Jeff Brown96307042012-07-27 15:51:34 -07002405 private void handleBatteryStateChangedLocked() {
2406 mDirty |= DIRTY_BATTERY_STATE;
2407 updatePowerStateLocked();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002408 }
2409
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002410 private void shutdownOrRebootInternal(final @HaltMode int haltMode, final boolean confirm,
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002411 final String reason, boolean wait) {
Jeff Brown96307042012-07-27 15:51:34 -07002412 if (mHandler == null || !mSystemReady) {
Jeff Brownab887a02012-10-15 16:00:40 -07002413 throw new IllegalStateException("Too early to call shutdown() or reboot()");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002414 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002415
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002416 Runnable runnable = new Runnable() {
Jeff Brownab887a02012-10-15 16:00:40 -07002417 @Override
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002418 public void run() {
2419 synchronized (this) {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002420 if (haltMode == HALT_MODE_REBOOT_SAFE_MODE) {
2421 ShutdownThread.rebootSafeMode(mContext, confirm);
2422 } else if (haltMode == HALT_MODE_REBOOT) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002423 ShutdownThread.reboot(mContext, reason, confirm);
Tony Mantlerb8009fd2016-03-14 15:55:35 -07002424 } else {
2425 ShutdownThread.shutdown(mContext, reason, confirm);
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002426 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002427 }
San Mehat1e512792010-01-07 10:40:29 -08002428 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002429 };
Jeff Brown96307042012-07-27 15:51:34 -07002430
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002431 // ShutdownThread must run on a looper capable of displaying the UI.
Jeff Brown96307042012-07-27 15:51:34 -07002432 Message msg = Message.obtain(mHandler, runnable);
2433 msg.setAsynchronous(true);
2434 mHandler.sendMessage(msg);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002435
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002436 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Dianne Hackbornc428aae2012-10-03 16:38:22 -07002437 if (wait) {
2438 synchronized (runnable) {
2439 while (true) {
2440 try {
2441 runnable.wait();
2442 } catch (InterruptedException e) {
2443 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002444 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002445 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002446 }
2447 }
2448
Jeff Brown96307042012-07-27 15:51:34 -07002449 private void crashInternal(final String message) {
Dan Egnor60d87622009-12-16 16:32:58 -08002450 Thread t = new Thread("PowerManagerService.crash()") {
Jeff Brownab887a02012-10-15 16:00:40 -07002451 @Override
Jeff Brown96307042012-07-27 15:51:34 -07002452 public void run() {
2453 throw new RuntimeException(message);
2454 }
Dan Egnor60d87622009-12-16 16:32:58 -08002455 };
2456 try {
2457 t.start();
2458 t.join();
2459 } catch (InterruptedException e) {
Dianne Hackborn8d051722014-10-01 14:59:58 -07002460 Slog.wtf(TAG, e);
Dan Egnor60d87622009-12-16 16:32:58 -08002461 }
2462 }
2463
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002464 void setStayOnSettingInternal(int val) {
Christopher Tatead735322012-09-07 14:19:43 -07002465 Settings.Global.putInt(mContext.getContentResolver(),
2466 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002469 void setMaximumScreenOffTimeoutFromDeviceAdminInternal(int timeMs) {
Jeff Brown96307042012-07-27 15:51:34 -07002470 synchronized (mLock) {
2471 mMaximumScreenOffTimeoutFromDeviceAdmin = timeMs;
2472 mDirty |= DIRTY_SETTINGS;
2473 updatePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475 }
2476
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002477 boolean setDeviceIdleModeInternal(boolean enabled) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002478 synchronized (mLock) {
2479 if (mDeviceIdleMode != enabled) {
2480 mDeviceIdleMode = enabled;
2481 updateWakeLockDisabledStatesLocked();
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07002482 if (enabled) {
2483 EventLogTags.writeDeviceIdleOnPhase("power");
2484 } else {
2485 EventLogTags.writeDeviceIdleOffPhase("power");
2486 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002487 return true;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002488 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002489 return false;
2490 }
2491 }
2492
2493 boolean setLightDeviceIdleModeInternal(boolean enabled) {
2494 synchronized (mLock) {
2495 if (mLightDeviceIdleMode != enabled) {
2496 mLightDeviceIdleMode = enabled;
2497 return true;
2498 }
2499 return false;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002500 }
2501 }
2502
2503 void setDeviceIdleWhitelistInternal(int[] appids) {
2504 synchronized (mLock) {
2505 mDeviceIdleWhitelist = appids;
2506 if (mDeviceIdleMode) {
2507 updateWakeLockDisabledStatesLocked();
2508 }
2509 }
2510 }
2511
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002512 void setDeviceIdleTempWhitelistInternal(int[] appids) {
2513 synchronized (mLock) {
2514 mDeviceIdleTempWhitelist = appids;
2515 if (mDeviceIdleMode) {
2516 updateWakeLockDisabledStatesLocked();
2517 }
2518 }
2519 }
2520
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002521 void updateUidProcStateInternal(int uid, int procState) {
2522 synchronized (mLock) {
2523 mUidState.put(uid, procState);
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002524 if (mDeviceIdleMode || mSustainedPerformanceUid.get(uid) != 0) {
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002525 updateWakeLockDisabledStatesLocked();
2526 }
2527 }
2528 }
2529
2530 void uidGoneInternal(int uid) {
2531 synchronized (mLock) {
2532 mUidState.delete(uid);
2533 if (mDeviceIdleMode) {
2534 updateWakeLockDisabledStatesLocked();
2535 }
2536 }
2537 }
2538
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002539 private void updateWakeLockDisabledStatesLocked() {
2540 boolean changed = false;
2541 final int numWakeLocks = mWakeLocks.size();
2542 for (int i = 0; i < numWakeLocks; i++) {
2543 final WakeLock wakeLock = mWakeLocks.get(i);
2544 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002545 == PowerManager.PARTIAL_WAKE_LOCK
2546 || (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
2547 == PowerManager.SUSTAINED_PERFORMANCE_WAKE_LOCK) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002548 if (setWakeLockDisabledStateLocked(wakeLock)) {
2549 changed = true;
2550 if (wakeLock.mDisabled) {
2551 // This wake lock is no longer being respected.
2552 notifyWakeLockReleasedLocked(wakeLock);
2553 } else {
2554 notifyWakeLockAcquiredLocked(wakeLock);
2555 }
2556 }
2557 }
2558 }
2559 if (changed) {
2560 mDirty |= DIRTY_WAKE_LOCKS;
2561 updatePowerStateLocked();
2562 }
2563 }
2564
2565 private boolean setWakeLockDisabledStateLocked(WakeLock wakeLock) {
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002566 boolean disabled = false;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002567 if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
2568 == PowerManager.PARTIAL_WAKE_LOCK) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002569 if (mDeviceIdleMode) {
2570 final int appid = UserHandle.getAppId(wakeLock.mOwnerUid);
2571 // If we are in idle mode, we will ignore all partial wake locks that are
2572 // for application uids that are not whitelisted.
2573 if (appid >= Process.FIRST_APPLICATION_UID &&
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002574 Arrays.binarySearch(mDeviceIdleWhitelist, appid) < 0 &&
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002575 Arrays.binarySearch(mDeviceIdleTempWhitelist, appid) < 0 &&
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002576 mUidState.get(wakeLock.mOwnerUid,
2577 ActivityManager.PROCESS_STATE_CACHED_EMPTY)
2578 > ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002579 disabled = true;
2580 }
2581 }
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002582 } else if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
2583 == PowerManager.SUSTAINED_PERFORMANCE_WAKE_LOCK
2584 && mUidState.get(wakeLock.mOwnerUid,
2585 ActivityManager.PROCESS_STATE_CACHED_EMPTY)
2586 > ActivityManager.PROCESS_STATE_TOP) {
2587 disabled = true;
2588 }
2589 if (wakeLock.mDisabled != disabled) {
2590 wakeLock.mDisabled = disabled;
2591 return true;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002592 }
2593 return false;
2594 }
2595
Jeff Brown96307042012-07-27 15:51:34 -07002596 private boolean isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() {
2597 return mMaximumScreenOffTimeoutFromDeviceAdmin >= 0
2598 && mMaximumScreenOffTimeoutFromDeviceAdmin < Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002600
Jeff Brown96307042012-07-27 15:51:34 -07002601 private void setAttentionLightInternal(boolean on, int color) {
Adam Lesinski182f73f2013-12-05 16:48:06 -08002602 Light light;
Jeff Brown96307042012-07-27 15:51:34 -07002603 synchronized (mLock) {
2604 if (!mSystemReady) {
2605 return;
2606 }
2607 light = mAttentionLight;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002608 }
Jeff Brown96307042012-07-27 15:51:34 -07002609
2610 // Control light outside of lock.
Adam Lesinski182f73f2013-12-05 16:48:06 -08002611 light.setFlashing(color, Light.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Jeff Brown96307042012-07-27 15:51:34 -07002612 }
2613
Jeff Browne333e672014-10-28 13:48:55 -07002614 private void boostScreenBrightnessInternal(long eventTime, int uid) {
2615 synchronized (mLock) {
2616 if (!mSystemReady || mWakefulness == WAKEFULNESS_ASLEEP
2617 || eventTime < mLastScreenBrightnessBoostTime) {
2618 return;
2619 }
2620
2621 Slog.i(TAG, "Brightness boost activated (uid " + uid +")...");
2622 mLastScreenBrightnessBoostTime = eventTime;
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002623 if (!mScreenBrightnessBoostInProgress) {
2624 mScreenBrightnessBoostInProgress = true;
2625 mNotifier.onScreenBrightnessBoostChanged();
2626 }
Jeff Browne333e672014-10-28 13:48:55 -07002627 mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST;
2628
2629 userActivityNoUpdateLocked(eventTime,
2630 PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, uid);
2631 updatePowerStateLocked();
2632 }
2633 }
2634
Bryce Lee84d6c0f2015-03-17 10:43:08 -07002635 private boolean isScreenBrightnessBoostedInternal() {
2636 synchronized (mLock) {
2637 return mScreenBrightnessBoostInProgress;
2638 }
2639 }
2640
Jeff Browne333e672014-10-28 13:48:55 -07002641 /**
2642 * Called when a screen brightness boost timeout has occurred.
2643 *
2644 * This function must have no other side-effects besides setting the dirty
2645 * bit and calling update power state.
2646 */
2647 private void handleScreenBrightnessBoostTimeout() { // runs on handler thread
2648 synchronized (mLock) {
2649 if (DEBUG_SPEW) {
2650 Slog.d(TAG, "handleScreenBrightnessBoostTimeout");
2651 }
2652
2653 mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST;
2654 updatePowerStateLocked();
2655 }
2656 }
2657
Jeff Brown96307042012-07-27 15:51:34 -07002658 private void setScreenBrightnessOverrideFromWindowManagerInternal(int brightness) {
2659 synchronized (mLock) {
2660 if (mScreenBrightnessOverrideFromWindowManager != brightness) {
2661 mScreenBrightnessOverrideFromWindowManager = brightness;
2662 mDirty |= DIRTY_SETTINGS;
2663 updatePowerStateLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002664 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002665 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002666 }
2667
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002668 private void setUserInactiveOverrideFromWindowManagerInternal() {
2669 synchronized (mLock) {
2670 mUserInactiveOverrideFromWindowManager = true;
2671 mDirty |= DIRTY_USER_ACTIVITY;
2672 updatePowerStateLocked();
2673 }
2674 }
2675
Jeff Brown1e3b98d2012-09-30 18:58:59 -07002676 private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) {
2677 synchronized (mLock) {
2678 if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) {
2679 mUserActivityTimeoutOverrideFromWindowManager = timeoutMillis;
2680 mDirty |= DIRTY_SETTINGS;
2681 updatePowerStateLocked();
2682 }
2683 }
2684 }
2685
Jeff Brown96307042012-07-27 15:51:34 -07002686 private void setTemporaryScreenBrightnessSettingOverrideInternal(int brightness) {
2687 synchronized (mLock) {
2688 if (mTemporaryScreenBrightnessSettingOverride != brightness) {
2689 mTemporaryScreenBrightnessSettingOverride = brightness;
2690 mDirty |= DIRTY_SETTINGS;
2691 updatePowerStateLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002692 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002693 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002694 }
2695
Jeff Brown330560f2012-08-21 22:10:57 -07002696 private void setTemporaryScreenAutoBrightnessAdjustmentSettingOverrideInternal(float adj) {
2697 synchronized (mLock) {
2698 // Note: This condition handles NaN because NaN is not equal to any other
2699 // value, including itself.
2700 if (mTemporaryScreenAutoBrightnessAdjustmentSettingOverride != adj) {
2701 mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = adj;
2702 mDirty |= DIRTY_SETTINGS;
2703 updatePowerStateLocked();
2704 }
2705 }
Jeff Brown96307042012-07-27 15:51:34 -07002706 }
2707
Jeff Brown970d4132014-07-19 11:33:47 -07002708 private void setDozeOverrideFromDreamManagerInternal(
2709 int screenState, int screenBrightness) {
2710 synchronized (mLock) {
2711 if (mDozeScreenStateOverrideFromDreamManager != screenState
2712 || mDozeScreenBrightnessOverrideFromDreamManager != screenBrightness) {
2713 mDozeScreenStateOverrideFromDreamManager = screenState;
2714 mDozeScreenBrightnessOverrideFromDreamManager = screenBrightness;
2715 mDirty |= DIRTY_SETTINGS;
2716 updatePowerStateLocked();
2717 }
2718 }
2719 }
2720
Ruchi Kandoi15aedf52014-05-09 19:57:51 -07002721 private void powerHintInternal(int hintId, int data) {
2722 nativeSendPowerHint(hintId, data);
2723 }
2724
Jeff Brown96307042012-07-27 15:51:34 -07002725 /**
2726 * Low-level function turn the device off immediately, without trying
2727 * to be clean. Most people should use {@link ShutdownThread} for a clean shutdown.
Yusuke Sato705ffd12015-07-21 15:52:11 -07002728 *
2729 * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
Jeff Brown96307042012-07-27 15:51:34 -07002730 */
Yusuke Sato705ffd12015-07-21 15:52:11 -07002731 public static void lowLevelShutdown(String reason) {
2732 if (reason == null) {
2733 reason = "";
2734 }
2735 SystemProperties.set("sys.powerctl", "shutdown," + reason);
Jeff Brown96307042012-07-27 15:51:34 -07002736 }
2737
2738 /**
Doug Zongker3b0218b2014-01-14 12:29:06 -08002739 * Low-level function to reboot the device. On success, this
2740 * function doesn't return. If more than 20 seconds passes from
Tao Bao90237f72015-05-21 16:25:19 -07002741 * the time a reboot is requested, this method returns.
Jeff Brown96307042012-07-27 15:51:34 -07002742 *
2743 * @param reason code to pass to the kernel (e.g. "recovery"), or null.
Jeff Brown96307042012-07-27 15:51:34 -07002744 */
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07002745 public static void lowLevelReboot(String reason) {
2746 if (reason == null) {
2747 reason = "";
2748 }
Tao Baoe8a403d2015-12-31 07:44:55 -08002749 if (reason.equals(PowerManager.REBOOT_RECOVERY)
2750 || reason.equals(PowerManager.REBOOT_RECOVERY_UPDATE)) {
2751 SystemProperties.set("sys.powerctl", "reboot,recovery");
Doug Zongker3b0218b2014-01-14 12:29:06 -08002752 } else {
2753 SystemProperties.set("sys.powerctl", "reboot," + reason);
Doug Zongker3b0218b2014-01-14 12:29:06 -08002754 }
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07002755 try {
Tao Bao90237f72015-05-21 16:25:19 -07002756 Thread.sleep(20 * 1000L);
Nick Kralevichdbcf2d72013-04-18 14:41:40 -07002757 } catch (InterruptedException e) {
2758 Thread.currentThread().interrupt();
2759 }
Tao Bao90237f72015-05-21 16:25:19 -07002760 Slog.wtf(TAG, "Unexpected return from lowLevelReboot!");
Jeff Brown96307042012-07-27 15:51:34 -07002761 }
2762
2763 @Override // Watchdog.Monitor implementation
2764 public void monitor() {
2765 // Grab and release lock for watchdog monitor to detect deadlocks.
2766 synchronized (mLock) {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002767 }
Jeff Brown96307042012-07-27 15:51:34 -07002768 }
2769
Jeff Brown6f357d32014-01-15 20:40:55 -08002770 private void dumpInternal(PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07002771 pw.println("POWER MANAGER (dumpsys power)\n");
2772
Jeff Brown3b971592013-01-09 18:46:37 -08002773 final WirelessChargerDetector wcd;
Jeff Brown96307042012-07-27 15:51:34 -07002774 synchronized (mLock) {
2775 pw.println("Power Manager State:");
2776 pw.println(" mDirty=0x" + Integer.toHexString(mDirty));
Jeff Brownfbe96702014-11-19 18:30:58 -08002777 pw.println(" mWakefulness=" + PowerManagerInternal.wakefulnessToString(mWakefulness));
2778 pw.println(" mWakefulnessChanging=" + mWakefulnessChanging);
Jeff Brown96307042012-07-27 15:51:34 -07002779 pw.println(" mIsPowered=" + mIsPowered);
Jeff Brownf3fb8952012-10-02 20:57:05 -07002780 pw.println(" mPlugType=" + mPlugType);
Jeff Brown016ff142012-10-15 16:47:22 -07002781 pw.println(" mBatteryLevel=" + mBatteryLevel);
2782 pw.println(" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted);
Jeff Brownec6aa592012-10-17 20:30:25 -07002783 pw.println(" mDockState=" + mDockState);
Jeff Brown96307042012-07-27 15:51:34 -07002784 pw.println(" mStayOn=" + mStayOn);
Jeff Brown93cbbb22012-10-04 13:18:36 -07002785 pw.println(" mProximityPositive=" + mProximityPositive);
Jeff Brown96307042012-07-27 15:51:34 -07002786 pw.println(" mBootCompleted=" + mBootCompleted);
2787 pw.println(" mSystemReady=" + mSystemReady);
Jeff Brown037c33e2014-04-09 00:31:55 -07002788 pw.println(" mHalAutoSuspendModeEnabled=" + mHalAutoSuspendModeEnabled);
2789 pw.println(" mHalInteractiveModeEnabled=" + mHalInteractiveModeEnabled);
Jeff Brown96307042012-07-27 15:51:34 -07002790 pw.println(" mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));
2791 pw.println(" mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary));
2792 pw.println(" mRequestWaitForNegativeProximity=" + mRequestWaitForNegativeProximity);
2793 pw.println(" mSandmanScheduled=" + mSandmanScheduled);
Jeff Brown26875502014-01-30 21:47:47 -08002794 pw.println(" mSandmanSummoned=" + mSandmanSummoned);
Dianne Hackborn14272302014-06-10 23:13:02 -07002795 pw.println(" mLowPowerModeEnabled=" + mLowPowerModeEnabled);
2796 pw.println(" mBatteryLevelLow=" + mBatteryLevelLow);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002797 pw.println(" mLightDeviceIdleMode=" + mLightDeviceIdleMode);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002798 pw.println(" mDeviceIdleMode=" + mDeviceIdleMode);
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002799 pw.println(" mSustainedPerformanceMode=" + mSustainedPerformanceMode);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002800 pw.println(" mDeviceIdleWhitelist=" + Arrays.toString(mDeviceIdleWhitelist));
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002801 pw.println(" mDeviceIdleTempWhitelist=" + Arrays.toString(mDeviceIdleTempWhitelist));
Jeff Brown96307042012-07-27 15:51:34 -07002802 pw.println(" mLastWakeTime=" + TimeUtils.formatUptime(mLastWakeTime));
2803 pw.println(" mLastSleepTime=" + TimeUtils.formatUptime(mLastSleepTime));
Jeff Brown96307042012-07-27 15:51:34 -07002804 pw.println(" mLastUserActivityTime=" + TimeUtils.formatUptime(mLastUserActivityTime));
2805 pw.println(" mLastUserActivityTimeNoChangeLights="
2806 + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights));
Jeff Brown0a571122014-08-21 21:50:43 -07002807 pw.println(" mLastInteractivePowerHintTime="
2808 + TimeUtils.formatUptime(mLastInteractivePowerHintTime));
Jeff Browne333e672014-10-28 13:48:55 -07002809 pw.println(" mLastScreenBrightnessBoostTime="
2810 + TimeUtils.formatUptime(mLastScreenBrightnessBoostTime));
2811 pw.println(" mScreenBrightnessBoostInProgress="
2812 + mScreenBrightnessBoostInProgress);
Jeff Brown96307042012-07-27 15:51:34 -07002813 pw.println(" mDisplayReady=" + mDisplayReady);
2814 pw.println(" mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker);
Jeff Brown27f7a862012-12-12 15:43:31 -08002815 pw.println(" mHoldingDisplaySuspendBlocker=" + mHoldingDisplaySuspendBlocker);
Jeff Brown96307042012-07-27 15:51:34 -07002816
2817 pw.println();
2818 pw.println("Settings and Configuration:");
Jeff Brown037c33e2014-04-09 00:31:55 -07002819 pw.println(" mDecoupleHalAutoSuspendModeFromDisplayConfig="
2820 + mDecoupleHalAutoSuspendModeFromDisplayConfig);
2821 pw.println(" mDecoupleHalInteractiveModeFromDisplayConfig="
2822 + mDecoupleHalInteractiveModeFromDisplayConfig);
Jeff Brownec083212013-09-11 20:45:25 -07002823 pw.println(" mWakeUpWhenPluggedOrUnpluggedConfig="
2824 + mWakeUpWhenPluggedOrUnpluggedConfig);
Bryce Lee584a4452014-10-21 15:55:55 -07002825 pw.println(" mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig="
2826 + mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig);
2827 pw.println(" mTheaterModeEnabled="
2828 + mTheaterModeEnabled);
Jeff Brownec083212013-09-11 20:45:25 -07002829 pw.println(" mSuspendWhenScreenOffDueToProximityConfig="
2830 + mSuspendWhenScreenOffDueToProximityConfig);
Jeff Brown96307042012-07-27 15:51:34 -07002831 pw.println(" mDreamsSupportedConfig=" + mDreamsSupportedConfig);
Jeff Brownec083212013-09-11 20:45:25 -07002832 pw.println(" mDreamsEnabledByDefaultConfig=" + mDreamsEnabledByDefaultConfig);
2833 pw.println(" mDreamsActivatedOnSleepByDefaultConfig="
2834 + mDreamsActivatedOnSleepByDefaultConfig);
2835 pw.println(" mDreamsActivatedOnDockByDefaultConfig="
2836 + mDreamsActivatedOnDockByDefaultConfig);
Jeff Brown26875502014-01-30 21:47:47 -08002837 pw.println(" mDreamsEnabledOnBatteryConfig="
2838 + mDreamsEnabledOnBatteryConfig);
2839 pw.println(" mDreamsBatteryLevelMinimumWhenPoweredConfig="
2840 + mDreamsBatteryLevelMinimumWhenPoweredConfig);
2841 pw.println(" mDreamsBatteryLevelMinimumWhenNotPoweredConfig="
2842 + mDreamsBatteryLevelMinimumWhenNotPoweredConfig);
2843 pw.println(" mDreamsBatteryLevelDrainCutoffConfig="
2844 + mDreamsBatteryLevelDrainCutoffConfig);
Jeff Brown96307042012-07-27 15:51:34 -07002845 pw.println(" mDreamsEnabledSetting=" + mDreamsEnabledSetting);
John Spurlock1a868b72012-08-22 09:56:51 -04002846 pw.println(" mDreamsActivateOnSleepSetting=" + mDreamsActivateOnSleepSetting);
Jeff Brownec6aa592012-10-17 20:30:25 -07002847 pw.println(" mDreamsActivateOnDockSetting=" + mDreamsActivateOnDockSetting);
Jeff Brown2175e9c2014-09-12 16:11:07 -07002848 pw.println(" mDozeAfterScreenOffConfig=" + mDozeAfterScreenOffConfig);
Dianne Hackborn14272302014-06-10 23:13:02 -07002849 pw.println(" mLowPowerModeSetting=" + mLowPowerModeSetting);
John Spurlock8d4e6cb2014-09-14 11:10:22 -04002850 pw.println(" mAutoLowPowerModeConfigured=" + mAutoLowPowerModeConfigured);
John Spurlock1bb480a2014-08-02 17:12:43 -04002851 pw.println(" mAutoLowPowerModeSnoozing=" + mAutoLowPowerModeSnoozing);
Jeff Brown27736f52014-05-20 17:17:10 -07002852 pw.println(" mMinimumScreenOffTimeoutConfig=" + mMinimumScreenOffTimeoutConfig);
2853 pw.println(" mMaximumScreenDimDurationConfig=" + mMaximumScreenDimDurationConfig);
2854 pw.println(" mMaximumScreenDimRatioConfig=" + mMaximumScreenDimRatioConfig);
Jeff Brown96307042012-07-27 15:51:34 -07002855 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting);
Jeff Brown05af6ad2014-09-30 20:54:30 -07002856 pw.println(" mSleepTimeoutSetting=" + mSleepTimeoutSetting);
Jeff Brown96307042012-07-27 15:51:34 -07002857 pw.println(" mMaximumScreenOffTimeoutFromDeviceAdmin="
2858 + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced="
2859 + isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() + ")");
2860 pw.println(" mStayOnWhilePluggedInSetting=" + mStayOnWhilePluggedInSetting);
2861 pw.println(" mScreenBrightnessSetting=" + mScreenBrightnessSetting);
Jeff Brown330560f2012-08-21 22:10:57 -07002862 pw.println(" mScreenAutoBrightnessAdjustmentSetting="
2863 + mScreenAutoBrightnessAdjustmentSetting);
Jeff Brown96307042012-07-27 15:51:34 -07002864 pw.println(" mScreenBrightnessModeSetting=" + mScreenBrightnessModeSetting);
2865 pw.println(" mScreenBrightnessOverrideFromWindowManager="
2866 + mScreenBrightnessOverrideFromWindowManager);
Jeff Brown1e3b98d2012-09-30 18:58:59 -07002867 pw.println(" mUserActivityTimeoutOverrideFromWindowManager="
2868 + mUserActivityTimeoutOverrideFromWindowManager);
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07002869 pw.println(" mUserInactiveOverrideFromWindowManager="
2870 + mUserInactiveOverrideFromWindowManager);
Jeff Brown96307042012-07-27 15:51:34 -07002871 pw.println(" mTemporaryScreenBrightnessSettingOverride="
2872 + mTemporaryScreenBrightnessSettingOverride);
Jeff Brown330560f2012-08-21 22:10:57 -07002873 pw.println(" mTemporaryScreenAutoBrightnessAdjustmentSettingOverride="
2874 + mTemporaryScreenAutoBrightnessAdjustmentSettingOverride);
Jeff Brown970d4132014-07-19 11:33:47 -07002875 pw.println(" mDozeScreenStateOverrideFromDreamManager="
2876 + mDozeScreenStateOverrideFromDreamManager);
2877 pw.println(" mDozeScreenBrightnessOverrideFromDreamManager="
2878 + mDozeScreenBrightnessOverrideFromDreamManager);
Jeff Brown96307042012-07-27 15:51:34 -07002879 pw.println(" mScreenBrightnessSettingMinimum=" + mScreenBrightnessSettingMinimum);
2880 pw.println(" mScreenBrightnessSettingMaximum=" + mScreenBrightnessSettingMaximum);
2881 pw.println(" mScreenBrightnessSettingDefault=" + mScreenBrightnessSettingDefault);
Jason Monk27bbb2d2015-03-31 16:46:39 -04002882 pw.println(" mDoubleTapWakeEnabled=" + mDoubleTapWakeEnabled);
Jeff Brown96307042012-07-27 15:51:34 -07002883
Jeff Brown05af6ad2014-09-30 20:54:30 -07002884 final int sleepTimeout = getSleepTimeoutLocked();
2885 final int screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);
Jeff Brownff532542012-10-02 21:18:04 -07002886 final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);
2887 pw.println();
Jeff Brown05af6ad2014-09-30 20:54:30 -07002888 pw.println("Sleep timeout: " + sleepTimeout + " ms");
Jeff Brownff532542012-10-02 21:18:04 -07002889 pw.println("Screen off timeout: " + screenOffTimeout + " ms");
2890 pw.println("Screen dim duration: " + screenDimDuration + " ms");
2891
Jeff Brown96307042012-07-27 15:51:34 -07002892 pw.println();
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07002893 pw.println("UID states:");
2894 for (int i=0; i<mUidState.size(); i++) {
2895 pw.print(" UID "); UserHandle.formatUid(pw, mUidState.keyAt(i));
2896 pw.print(": "); pw.println(mUidState.valueAt(i));
2897 }
2898
2899 pw.println();
Jeff Brown96307042012-07-27 15:51:34 -07002900 pw.println("Wake Locks: size=" + mWakeLocks.size());
2901 for (WakeLock wl : mWakeLocks) {
2902 pw.println(" " + wl);
Joe Onorato8274a0e2010-10-05 17:38:09 -04002903 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002904
Jeff Brown96307042012-07-27 15:51:34 -07002905 pw.println();
2906 pw.println("Suspend Blockers: size=" + mSuspendBlockers.size());
2907 for (SuspendBlocker sb : mSuspendBlockers) {
2908 pw.println(" " + sb);
2909 }
2910
Jeff Brownc38c9be2012-10-04 13:16:19 -07002911 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07002912 pw.println("Display Power: " + mDisplayPowerCallbacks);
Jeff Brown9e316a12012-10-08 19:17:06 -07002913
Ruchi Kandoi0abc0012016-01-12 14:39:24 -08002914 pw.println();
2915 pw.println("Sustained Performance UIDs:");
2916 for (int i=0; i<mSustainedPerformanceUid.size(); i++) {
2917 pw.print(" UID "); UserHandle.formatUid(pw, mSustainedPerformanceUid.keyAt(i));
2918 pw.print(": "); pw.println(mSustainedPerformanceUid.valueAt(i));
2919 }
2920
2921
Jeff Brown3b971592013-01-09 18:46:37 -08002922 wcd = mWirelessChargerDetector;
Jeff Brown96307042012-07-27 15:51:34 -07002923 }
2924
Jeff Brown3b971592013-01-09 18:46:37 -08002925 if (wcd != null) {
2926 wcd.dump(pw);
2927 }
Jeff Brown96307042012-07-27 15:51:34 -07002928 }
2929
2930 private SuspendBlocker createSuspendBlockerLocked(String name) {
2931 SuspendBlocker suspendBlocker = new SuspendBlockerImpl(name);
2932 mSuspendBlockers.add(suspendBlocker);
2933 return suspendBlocker;
2934 }
2935
Daichi Hirono82ab9802016-03-02 13:23:29 +09002936 private void incrementBootCount() {
2937 synchronized (mLock) {
2938 int count;
2939 try {
2940 count = Settings.Global.getInt(
2941 getContext().getContentResolver(), Settings.Global.BOOT_COUNT);
2942 } catch (SettingNotFoundException e) {
2943 count = 0;
2944 }
2945 Settings.Global.putInt(
2946 getContext().getContentResolver(), Settings.Global.BOOT_COUNT, count + 1);
2947 }
2948 }
2949
Jeff Brown96307042012-07-27 15:51:34 -07002950 private static WorkSource copyWorkSource(WorkSource workSource) {
2951 return workSource != null ? new WorkSource(workSource) : null;
2952 }
2953
2954 private final class BatteryReceiver extends BroadcastReceiver {
2955 @Override
2956 public void onReceive(Context context, Intent intent) {
2957 synchronized (mLock) {
2958 handleBatteryStateChangedLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002959 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002960 }
2961 }
2962
John Spurlockf4f6b4c2012-08-25 12:08:03 -04002963 private final class DreamReceiver extends BroadcastReceiver {
2964 @Override
2965 public void onReceive(Context context, Intent intent) {
2966 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -07002967 scheduleSandmanLocked();
John Spurlockf4f6b4c2012-08-25 12:08:03 -04002968 }
2969 }
2970 }
2971
Jeff Brownd4935962012-09-25 13:27:20 -07002972 private final class UserSwitchedReceiver extends BroadcastReceiver {
2973 @Override
2974 public void onReceive(Context context, Intent intent) {
2975 synchronized (mLock) {
2976 handleSettingsChangedLocked();
2977 }
2978 }
2979 }
2980
Jeff Brownec6aa592012-10-17 20:30:25 -07002981 private final class DockReceiver extends BroadcastReceiver {
2982 @Override
2983 public void onReceive(Context context, Intent intent) {
2984 synchronized (mLock) {
2985 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
2986 Intent.EXTRA_DOCK_STATE_UNDOCKED);
2987 if (mDockState != dockState) {
2988 mDockState = dockState;
2989 mDirty |= DIRTY_DOCK_STATE;
2990 updatePowerStateLocked();
2991 }
2992 }
2993 }
2994 }
2995
Jeff Brown96307042012-07-27 15:51:34 -07002996 private final class SettingsObserver extends ContentObserver {
2997 public SettingsObserver(Handler handler) {
2998 super(handler);
2999 }
3000
3001 @Override
3002 public void onChange(boolean selfChange, Uri uri) {
3003 synchronized (mLock) {
3004 handleSettingsChangedLocked();
3005 }
3006 }
3007 }
3008
Ruchi Kandoi03a04282016-03-14 17:09:17 -07003009 private final VrStateListener mVrStateListener = new VrStateListener() {
3010 @Override
3011 public void onVrStateChanged(boolean enabled) {
3012 powerHintInternal(POWER_HINT_VR_MODE, enabled ? 1 : 0);
3013 }
3014 };
3015
Jeff Brown96307042012-07-27 15:51:34 -07003016 /**
3017 * Handler for asynchronous operations performed by the power manager.
3018 */
3019 private final class PowerManagerHandler extends Handler {
3020 public PowerManagerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07003021 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07003022 }
3023
3024 @Override
3025 public void handleMessage(Message msg) {
3026 switch (msg.what) {
3027 case MSG_USER_ACTIVITY_TIMEOUT:
3028 handleUserActivityTimeout();
3029 break;
3030 case MSG_SANDMAN:
3031 handleSandman();
3032 break;
Jeff Browne333e672014-10-28 13:48:55 -07003033 case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT:
3034 handleScreenBrightnessBoostTimeout();
3035 break;
Jeff Brown96307042012-07-27 15:51:34 -07003036 }
3037 }
3038 }
3039
3040 /**
3041 * Represents a wake lock that has been acquired by an application.
3042 */
3043 private final class WakeLock implements IBinder.DeathRecipient {
3044 public final IBinder mLock;
3045 public int mFlags;
3046 public String mTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07003047 public final String mPackageName;
Jeff Brown96307042012-07-27 15:51:34 -07003048 public WorkSource mWorkSource;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003049 public String mHistoryTag;
Dianne Hackborn713df152013-05-17 11:27:57 -07003050 public final int mOwnerUid;
3051 public final int mOwnerPid;
3052 public boolean mNotifiedAcquired;
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003053 public boolean mDisabled;
Jeff Brown96307042012-07-27 15:51:34 -07003054
Dianne Hackborn713df152013-05-17 11:27:57 -07003055 public WakeLock(IBinder lock, int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003056 WorkSource workSource, String historyTag, int ownerUid, int ownerPid) {
Jeff Brown96307042012-07-27 15:51:34 -07003057 mLock = lock;
3058 mFlags = flags;
3059 mTag = tag;
Dianne Hackborn713df152013-05-17 11:27:57 -07003060 mPackageName = packageName;
Jeff Brown96307042012-07-27 15:51:34 -07003061 mWorkSource = copyWorkSource(workSource);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003062 mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07003063 mOwnerUid = ownerUid;
3064 mOwnerPid = ownerPid;
3065 }
3066
3067 @Override
3068 public void binderDied() {
3069 PowerManagerService.this.handleWakeLockDeath(this);
3070 }
3071
3072 public boolean hasSameProperties(int flags, String tag, WorkSource workSource,
3073 int ownerUid, int ownerPid) {
3074 return mFlags == flags
3075 && mTag.equals(tag)
3076 && hasSameWorkSource(workSource)
3077 && mOwnerUid == ownerUid
3078 && mOwnerPid == ownerPid;
3079 }
3080
Dianne Hackborn713df152013-05-17 11:27:57 -07003081 public void updateProperties(int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003082 WorkSource workSource, String historyTag, int ownerUid, int ownerPid) {
Dianne Hackborn713df152013-05-17 11:27:57 -07003083 if (!mPackageName.equals(packageName)) {
3084 throw new IllegalStateException("Existing wake lock package name changed: "
3085 + mPackageName + " to " + packageName);
3086 }
3087 if (mOwnerUid != ownerUid) {
3088 throw new IllegalStateException("Existing wake lock uid changed: "
3089 + mOwnerUid + " to " + ownerUid);
3090 }
3091 if (mOwnerPid != ownerPid) {
3092 throw new IllegalStateException("Existing wake lock pid changed: "
3093 + mOwnerPid + " to " + ownerPid);
3094 }
Jeff Brown96307042012-07-27 15:51:34 -07003095 mFlags = flags;
3096 mTag = tag;
3097 updateWorkSource(workSource);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003098 mHistoryTag = historyTag;
Jeff Brown96307042012-07-27 15:51:34 -07003099 }
3100
3101 public boolean hasSameWorkSource(WorkSource workSource) {
3102 return Objects.equal(mWorkSource, workSource);
3103 }
3104
3105 public void updateWorkSource(WorkSource workSource) {
3106 mWorkSource = copyWorkSource(workSource);
3107 }
3108
3109 @Override
3110 public String toString() {
3111 return getLockLevelString()
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003112 + " '" + mTag + "'" + getLockFlagsString() + (mDisabled ? " DISABLED" : "")
Jeff Brown96307042012-07-27 15:51:34 -07003113 + " (uid=" + mOwnerUid + ", pid=" + mOwnerPid + ", ws=" + mWorkSource + ")";
3114 }
3115
Jeff Brown26875502014-01-30 21:47:47 -08003116 @SuppressWarnings("deprecation")
Jeff Brown96307042012-07-27 15:51:34 -07003117 private String getLockLevelString() {
3118 switch (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
3119 case PowerManager.FULL_WAKE_LOCK:
3120 return "FULL_WAKE_LOCK ";
3121 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
3122 return "SCREEN_BRIGHT_WAKE_LOCK ";
3123 case PowerManager.SCREEN_DIM_WAKE_LOCK:
3124 return "SCREEN_DIM_WAKE_LOCK ";
3125 case PowerManager.PARTIAL_WAKE_LOCK:
3126 return "PARTIAL_WAKE_LOCK ";
3127 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
3128 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
Jeff Brown26875502014-01-30 21:47:47 -08003129 case PowerManager.DOZE_WAKE_LOCK:
3130 return "DOZE_WAKE_LOCK ";
Jeff Brownc2932a12014-11-20 18:04:05 -08003131 case PowerManager.DRAW_WAKE_LOCK:
3132 return "DRAW_WAKE_LOCK ";
Jeff Brown96307042012-07-27 15:51:34 -07003133 default:
3134 return "??? ";
3135 }
3136 }
3137
3138 private String getLockFlagsString() {
3139 String result = "";
3140 if ((mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
3141 result += " ACQUIRE_CAUSES_WAKEUP";
3142 }
3143 if ((mFlags & PowerManager.ON_AFTER_RELEASE) != 0) {
3144 result += " ON_AFTER_RELEASE";
3145 }
3146 return result;
3147 }
3148 }
3149
3150 private final class SuspendBlockerImpl implements SuspendBlocker {
3151 private final String mName;
Jeff Brown3edf5272014-08-14 19:25:14 -07003152 private final String mTraceName;
Jeff Brown96307042012-07-27 15:51:34 -07003153 private int mReferenceCount;
3154
3155 public SuspendBlockerImpl(String name) {
3156 mName = name;
Jeff Brown3edf5272014-08-14 19:25:14 -07003157 mTraceName = "SuspendBlocker (" + name + ")";
Jeff Brown96307042012-07-27 15:51:34 -07003158 }
3159
3160 @Override
3161 protected void finalize() throws Throwable {
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003162 try {
Jeff Brown96307042012-07-27 15:51:34 -07003163 if (mReferenceCount != 0) {
Jeff Brown3edf5272014-08-14 19:25:14 -07003164 Slog.wtf(TAG, "Suspend blocker \"" + mName
Jeff Brown96307042012-07-27 15:51:34 -07003165 + "\" was finalized without being released!");
3166 mReferenceCount = 0;
3167 nativeReleaseSuspendBlocker(mName);
Jeff Brown3edf5272014-08-14 19:25:14 -07003168 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003169 }
3170 } finally {
Jeff Brown96307042012-07-27 15:51:34 -07003171 super.finalize();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003172 }
3173 }
3174
Craig Mautner75fc9de2012-06-18 16:53:27 -07003175 @Override
Jeff Brown96307042012-07-27 15:51:34 -07003176 public void acquire() {
3177 synchronized (this) {
3178 mReferenceCount += 1;
3179 if (mReferenceCount == 1) {
Jeff Brown27f7a862012-12-12 15:43:31 -08003180 if (DEBUG_SPEW) {
3181 Slog.d(TAG, "Acquiring suspend blocker \"" + mName + "\".");
3182 }
Jeff Brown3edf5272014-08-14 19:25:14 -07003183 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown96307042012-07-27 15:51:34 -07003184 nativeAcquireSuspendBlocker(mName);
Craig Mautner37933682012-06-06 14:13:39 -07003185 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003186 }
3187 }
3188
Craig Mautner75fc9de2012-06-18 16:53:27 -07003189 @Override
Jeff Brown96307042012-07-27 15:51:34 -07003190 public void release() {
3191 synchronized (this) {
3192 mReferenceCount -= 1;
3193 if (mReferenceCount == 0) {
Jeff Brown27f7a862012-12-12 15:43:31 -08003194 if (DEBUG_SPEW) {
3195 Slog.d(TAG, "Releasing suspend blocker \"" + mName + "\".");
3196 }
Jeff Brown96307042012-07-27 15:51:34 -07003197 nativeReleaseSuspendBlocker(mName);
Jeff Brown3edf5272014-08-14 19:25:14 -07003198 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0);
Jeff Brown96307042012-07-27 15:51:34 -07003199 } else if (mReferenceCount < 0) {
Jeff Brown3edf5272014-08-14 19:25:14 -07003200 Slog.wtf(TAG, "Suspend blocker \"" + mName
Jeff Brown96307042012-07-27 15:51:34 -07003201 + "\" was released without being acquired!", new Throwable());
3202 mReferenceCount = 0;
3203 }
3204 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003205 }
Jeff Brown96307042012-07-27 15:51:34 -07003206
3207 @Override
3208 public String toString() {
3209 synchronized (this) {
3210 return mName + ": ref count=" + mReferenceCount;
3211 }
3212 }
3213 }
Jeff Brownc38c9be2012-10-04 13:16:19 -07003214
Jeff Brown6f357d32014-01-15 20:40:55 -08003215 private final class BinderService extends IPowerManager.Stub {
3216 @Override // Binder call
3217 public void acquireWakeLockWithUid(IBinder lock, int flags, String tag,
3218 String packageName, int uid) {
Dianne Hackbornef640cd2014-03-25 14:41:05 -07003219 if (uid < 0) {
3220 uid = Binder.getCallingUid();
3221 }
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003222 acquireWakeLock(lock, flags, tag, packageName, new WorkSource(uid), null);
Jeff Brown6f357d32014-01-15 20:40:55 -08003223 }
3224
3225 @Override // Binder call
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07003226 public void powerHint(int hintId, int data) {
Dianne Hackbornddef7e72014-07-09 16:39:14 -07003227 if (!mSystemReady) {
3228 // Service not ready yet, so who the heck cares about power hints, bah.
3229 return;
3230 }
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07003231 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Ruchi Kandoi15aedf52014-05-09 19:57:51 -07003232 powerHintInternal(hintId, data);
Ruchi Kandoif20a5eb2014-04-01 17:39:20 -07003233 }
3234
3235 @Override // Binder call
Jeff Brown6f357d32014-01-15 20:40:55 -08003236 public void acquireWakeLock(IBinder lock, int flags, String tag, String packageName,
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003237 WorkSource ws, String historyTag) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003238 if (lock == null) {
3239 throw new IllegalArgumentException("lock must not be null");
3240 }
3241 if (packageName == null) {
3242 throw new IllegalArgumentException("packageName must not be null");
3243 }
3244 PowerManager.validateWakeLockParameters(flags, tag);
3245
3246 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
Jeff Brown72671fb2014-08-21 21:41:09 -07003247 if ((flags & PowerManager.DOZE_WAKE_LOCK) != 0) {
3248 mContext.enforceCallingOrSelfPermission(
3249 android.Manifest.permission.DEVICE_POWER, null);
3250 }
Jeff Brown6f357d32014-01-15 20:40:55 -08003251 if (ws != null && ws.size() != 0) {
3252 mContext.enforceCallingOrSelfPermission(
3253 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
3254 } else {
3255 ws = null;
3256 }
3257
3258 final int uid = Binder.getCallingUid();
3259 final int pid = Binder.getCallingPid();
3260 final long ident = Binder.clearCallingIdentity();
3261 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003262 acquireWakeLockInternal(lock, flags, tag, packageName, ws, historyTag, uid, pid);
Jeff Brown6f357d32014-01-15 20:40:55 -08003263 } finally {
3264 Binder.restoreCallingIdentity(ident);
3265 }
3266 }
3267
3268 @Override // Binder call
3269 public void releaseWakeLock(IBinder lock, int flags) {
3270 if (lock == null) {
3271 throw new IllegalArgumentException("lock must not be null");
3272 }
3273
3274 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
3275
3276 final long ident = Binder.clearCallingIdentity();
3277 try {
3278 releaseWakeLockInternal(lock, flags);
3279 } finally {
3280 Binder.restoreCallingIdentity(ident);
3281 }
3282 }
3283
3284 @Override // Binder call
3285 public void updateWakeLockUids(IBinder lock, int[] uids) {
3286 WorkSource ws = null;
3287
3288 if (uids != null) {
3289 ws = new WorkSource();
3290 // XXX should WorkSource have a way to set uids as an int[] instead of adding them
3291 // one at a time?
3292 for (int i = 0; i < uids.length; i++) {
3293 ws.add(uids[i]);
3294 }
3295 }
Dianne Hackborn4590e522014-03-24 13:36:46 -07003296 updateWakeLockWorkSource(lock, ws, null);
Jeff Brown6f357d32014-01-15 20:40:55 -08003297 }
3298
3299 @Override // Binder call
Dianne Hackborn4590e522014-03-24 13:36:46 -07003300 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003301 if (lock == null) {
3302 throw new IllegalArgumentException("lock must not be null");
3303 }
3304
3305 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
3306 if (ws != null && ws.size() != 0) {
3307 mContext.enforceCallingOrSelfPermission(
3308 android.Manifest.permission.UPDATE_DEVICE_STATS, null);
3309 } else {
3310 ws = null;
3311 }
3312
Dianne Hackbornd953c532014-08-16 18:17:38 -07003313 final int callingUid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08003314 final long ident = Binder.clearCallingIdentity();
3315 try {
Dianne Hackbornd953c532014-08-16 18:17:38 -07003316 updateWakeLockWorkSourceInternal(lock, ws, historyTag, callingUid);
Jeff Brown6f357d32014-01-15 20:40:55 -08003317 } finally {
3318 Binder.restoreCallingIdentity(ident);
3319 }
3320 }
3321
3322 @Override // Binder call
3323 public boolean isWakeLockLevelSupported(int level) {
3324 final long ident = Binder.clearCallingIdentity();
3325 try {
3326 return isWakeLockLevelSupportedInternal(level);
3327 } finally {
3328 Binder.restoreCallingIdentity(ident);
3329 }
3330 }
3331
3332 @Override // Binder call
3333 public void userActivity(long eventTime, int event, int flags) {
3334 final long now = SystemClock.uptimeMillis();
3335 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
Jeff Brown0a571122014-08-21 21:50:43 -07003336 != PackageManager.PERMISSION_GRANTED
3337 && mContext.checkCallingOrSelfPermission(
3338 android.Manifest.permission.USER_ACTIVITY)
3339 != PackageManager.PERMISSION_GRANTED) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003340 // Once upon a time applications could call userActivity().
3341 // Now we require the DEVICE_POWER permission. Log a warning and ignore the
3342 // request instead of throwing a SecurityException so we don't break old apps.
3343 synchronized (mLock) {
3344 if (now >= mLastWarningAboutUserActivityPermission + (5 * 60 * 1000)) {
3345 mLastWarningAboutUserActivityPermission = now;
3346 Slog.w(TAG, "Ignoring call to PowerManager.userActivity() because the "
Jeff Brown0a571122014-08-21 21:50:43 -07003347 + "caller does not have DEVICE_POWER or USER_ACTIVITY "
3348 + "permission. Please fix your app! "
Jeff Brown6f357d32014-01-15 20:40:55 -08003349 + " pid=" + Binder.getCallingPid()
3350 + " uid=" + Binder.getCallingUid());
3351 }
3352 }
3353 return;
3354 }
3355
Jim Millerdca15d22015-06-16 20:55:13 -07003356 if (eventTime > now) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003357 throw new IllegalArgumentException("event time must not be in the future");
3358 }
3359
3360 final int uid = Binder.getCallingUid();
3361 final long ident = Binder.clearCallingIdentity();
3362 try {
3363 userActivityInternal(eventTime, event, flags, uid);
3364 } finally {
3365 Binder.restoreCallingIdentity(ident);
3366 }
3367 }
3368
3369 @Override // Binder call
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003370 public void wakeUp(long eventTime, String reason, String opPackageName) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003371 if (eventTime > SystemClock.uptimeMillis()) {
3372 throw new IllegalArgumentException("event time must not be in the future");
3373 }
3374
3375 mContext.enforceCallingOrSelfPermission(
3376 android.Manifest.permission.DEVICE_POWER, null);
3377
Jeff Brownc12035c2014-08-13 18:52:25 -07003378 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08003379 final long ident = Binder.clearCallingIdentity();
3380 try {
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003381 wakeUpInternal(eventTime, reason, uid, opPackageName, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08003382 } finally {
3383 Binder.restoreCallingIdentity(ident);
3384 }
3385 }
3386
3387 @Override // Binder call
Jeff Brown6d8fd272014-05-20 21:24:38 -07003388 public void goToSleep(long eventTime, int reason, int flags) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003389 if (eventTime > SystemClock.uptimeMillis()) {
3390 throw new IllegalArgumentException("event time must not be in the future");
3391 }
3392
3393 mContext.enforceCallingOrSelfPermission(
3394 android.Manifest.permission.DEVICE_POWER, null);
3395
Jeff Brownc12035c2014-08-13 18:52:25 -07003396 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08003397 final long ident = Binder.clearCallingIdentity();
3398 try {
Jeff Brownc12035c2014-08-13 18:52:25 -07003399 goToSleepInternal(eventTime, reason, flags, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08003400 } finally {
3401 Binder.restoreCallingIdentity(ident);
3402 }
3403 }
3404
3405 @Override // Binder call
3406 public void nap(long eventTime) {
3407 if (eventTime > SystemClock.uptimeMillis()) {
3408 throw new IllegalArgumentException("event time must not be in the future");
3409 }
3410
3411 mContext.enforceCallingOrSelfPermission(
3412 android.Manifest.permission.DEVICE_POWER, null);
3413
Jeff Brownc12035c2014-08-13 18:52:25 -07003414 final int uid = Binder.getCallingUid();
Jeff Brown6f357d32014-01-15 20:40:55 -08003415 final long ident = Binder.clearCallingIdentity();
3416 try {
Jeff Brownc12035c2014-08-13 18:52:25 -07003417 napInternal(eventTime, uid);
Jeff Brown6f357d32014-01-15 20:40:55 -08003418 } finally {
3419 Binder.restoreCallingIdentity(ident);
3420 }
3421 }
3422
3423 @Override // Binder call
Jeff Brown037c33e2014-04-09 00:31:55 -07003424 public boolean isInteractive() {
Jeff Brown6f357d32014-01-15 20:40:55 -08003425 final long ident = Binder.clearCallingIdentity();
3426 try {
Jeff Brown037c33e2014-04-09 00:31:55 -07003427 return isInteractiveInternal();
Jeff Brown6f357d32014-01-15 20:40:55 -08003428 } finally {
3429 Binder.restoreCallingIdentity(ident);
3430 }
3431 }
3432
Dianne Hackborneb94fa72014-06-03 17:48:12 -07003433 @Override // Binder call
3434 public boolean isPowerSaveMode() {
3435 final long ident = Binder.clearCallingIdentity();
3436 try {
3437 return isLowPowerModeInternal();
3438 } finally {
3439 Binder.restoreCallingIdentity(ident);
3440 }
3441 }
3442
John Spurlock8d4e6cb2014-09-14 11:10:22 -04003443 @Override // Binder call
3444 public boolean setPowerSaveMode(boolean mode) {
3445 mContext.enforceCallingOrSelfPermission(
3446 android.Manifest.permission.DEVICE_POWER, null);
3447 final long ident = Binder.clearCallingIdentity();
3448 try {
3449 return setLowPowerModeInternal(mode);
3450 } finally {
3451 Binder.restoreCallingIdentity(ident);
3452 }
3453 }
3454
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003455 @Override // Binder call
3456 public boolean isDeviceIdleMode() {
3457 final long ident = Binder.clearCallingIdentity();
3458 try {
3459 return isDeviceIdleModeInternal();
3460 } finally {
3461 Binder.restoreCallingIdentity(ident);
3462 }
3463 }
3464
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003465 @Override // Binder call
3466 public boolean isLightDeviceIdleMode() {
3467 final long ident = Binder.clearCallingIdentity();
3468 try {
3469 return isLightDeviceIdleModeInternal();
3470 } finally {
3471 Binder.restoreCallingIdentity(ident);
3472 }
3473 }
3474
Jeff Brown6f357d32014-01-15 20:40:55 -08003475 /**
3476 * Reboots the device.
3477 *
3478 * @param confirm If true, shows a reboot confirmation dialog.
3479 * @param reason The reason for the reboot, or null if none.
3480 * @param wait If true, this call waits for the reboot to complete and does not return.
3481 */
3482 @Override // Binder call
3483 public void reboot(boolean confirm, String reason, boolean wait) {
3484 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
Tao Baoe8a403d2015-12-31 07:44:55 -08003485 if (PowerManager.REBOOT_RECOVERY.equals(reason)
3486 || PowerManager.REBOOT_RECOVERY_UPDATE.equals(reason)) {
Doug Zongker3b0218b2014-01-14 12:29:06 -08003487 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null);
3488 }
Jeff Brown6f357d32014-01-15 20:40:55 -08003489
3490 final long ident = Binder.clearCallingIdentity();
3491 try {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07003492 shutdownOrRebootInternal(HALT_MODE_REBOOT, confirm, reason, wait);
3493 } finally {
3494 Binder.restoreCallingIdentity(ident);
3495 }
3496 }
3497
3498 /**
3499 * Reboots the device into safe mode
3500 *
3501 * @param confirm If true, shows a reboot confirmation dialog.
3502 * @param wait If true, this call waits for the reboot to complete and does not return.
3503 */
3504 @Override // Binder call
3505 public void rebootSafeMode(boolean confirm, boolean wait) {
3506 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3507
3508 final long ident = Binder.clearCallingIdentity();
3509 try {
3510 shutdownOrRebootInternal(HALT_MODE_REBOOT_SAFE_MODE, confirm,
3511 PowerManager.REBOOT_SAFE_MODE, wait);
Jeff Brown6f357d32014-01-15 20:40:55 -08003512 } finally {
3513 Binder.restoreCallingIdentity(ident);
3514 }
3515 }
3516
3517 /**
3518 * Shuts down the device.
3519 *
3520 * @param confirm If true, shows a shutdown confirmation dialog.
3521 * @param wait If true, this call waits for the shutdown to complete and does not return.
3522 */
3523 @Override // Binder call
Yusuke Sato705ffd12015-07-21 15:52:11 -07003524 public void shutdown(boolean confirm, String reason, boolean wait) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003525 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3526
3527 final long ident = Binder.clearCallingIdentity();
3528 try {
Tony Mantlerb8009fd2016-03-14 15:55:35 -07003529 shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, confirm, reason, wait);
Jeff Brown6f357d32014-01-15 20:40:55 -08003530 } finally {
3531 Binder.restoreCallingIdentity(ident);
3532 }
3533 }
3534
3535 /**
3536 * Crash the runtime (causing a complete restart of the Android framework).
3537 * Requires REBOOT permission. Mostly for testing. Should not return.
3538 */
3539 @Override // Binder call
3540 public void crash(String message) {
3541 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3542
3543 final long ident = Binder.clearCallingIdentity();
3544 try {
3545 crashInternal(message);
3546 } finally {
3547 Binder.restoreCallingIdentity(ident);
3548 }
3549 }
3550
3551 /**
3552 * Set the setting that determines whether the device stays on when plugged in.
3553 * The argument is a bit string, with each bit specifying a power source that,
3554 * when the device is connected to that source, causes the device to stay on.
3555 * See {@link android.os.BatteryManager} for the list of power sources that
3556 * can be specified. Current values include
3557 * {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
3558 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
3559 *
3560 * Used by "adb shell svc power stayon ..."
3561 *
3562 * @param val an {@code int} containing the bits that specify which power sources
3563 * should cause the device to stay on.
3564 */
3565 @Override // Binder call
3566 public void setStayOnSetting(int val) {
Billy Lau6ad2d662015-07-18 00:26:58 +01003567 int uid = Binder.getCallingUid();
3568 // if uid is of root's, we permit this operation straight away
3569 if (uid != Process.ROOT_UID) {
3570 if (!Settings.checkAndNoteWriteSettingsOperation(mContext, uid,
3571 Settings.getPackageNameForUid(mContext, uid), true)) {
3572 return;
3573 }
3574 }
Jeff Brown6f357d32014-01-15 20:40:55 -08003575
3576 final long ident = Binder.clearCallingIdentity();
3577 try {
3578 setStayOnSettingInternal(val);
3579 } finally {
3580 Binder.restoreCallingIdentity(ident);
3581 }
3582 }
3583
3584 /**
Jeff Brown6f357d32014-01-15 20:40:55 -08003585 * Used by the settings application and brightness control widgets to
3586 * temporarily override the current screen brightness setting so that the
3587 * user can observe the effect of an intended settings change without applying
3588 * it immediately.
3589 *
3590 * The override will be canceled when the setting value is next updated.
3591 *
3592 * @param brightness The overridden brightness.
3593 *
3594 * @see android.provider.Settings.System#SCREEN_BRIGHTNESS
3595 */
3596 @Override // Binder call
3597 public void setTemporaryScreenBrightnessSettingOverride(int brightness) {
3598 mContext.enforceCallingOrSelfPermission(
3599 android.Manifest.permission.DEVICE_POWER, null);
3600
3601 final long ident = Binder.clearCallingIdentity();
3602 try {
3603 setTemporaryScreenBrightnessSettingOverrideInternal(brightness);
3604 } finally {
3605 Binder.restoreCallingIdentity(ident);
3606 }
3607 }
3608
3609 /**
3610 * Used by the settings application and brightness control widgets to
3611 * temporarily override the current screen auto-brightness adjustment setting so that the
3612 * user can observe the effect of an intended settings change without applying
3613 * it immediately.
3614 *
3615 * The override will be canceled when the setting value is next updated.
3616 *
3617 * @param adj The overridden brightness, or Float.NaN to disable the override.
3618 *
Jeff Brown131206b2014-04-08 17:27:14 -07003619 * @see android.provider.Settings.System#SCREEN_AUTO_BRIGHTNESS_ADJ
Jeff Brown6f357d32014-01-15 20:40:55 -08003620 */
3621 @Override // Binder call
3622 public void setTemporaryScreenAutoBrightnessAdjustmentSettingOverride(float adj) {
3623 mContext.enforceCallingOrSelfPermission(
3624 android.Manifest.permission.DEVICE_POWER, null);
3625
3626 final long ident = Binder.clearCallingIdentity();
3627 try {
3628 setTemporaryScreenAutoBrightnessAdjustmentSettingOverrideInternal(adj);
3629 } finally {
3630 Binder.restoreCallingIdentity(ident);
3631 }
3632 }
3633
3634 /**
3635 * Used by the phone application to make the attention LED flash when ringing.
3636 */
3637 @Override // Binder call
3638 public void setAttentionLight(boolean on, int color) {
3639 mContext.enforceCallingOrSelfPermission(
3640 android.Manifest.permission.DEVICE_POWER, null);
3641
3642 final long ident = Binder.clearCallingIdentity();
3643 try {
3644 setAttentionLightInternal(on, color);
3645 } finally {
3646 Binder.restoreCallingIdentity(ident);
3647 }
3648 }
3649
3650 @Override // Binder call
Jeff Browne333e672014-10-28 13:48:55 -07003651 public void boostScreenBrightness(long eventTime) {
3652 if (eventTime > SystemClock.uptimeMillis()) {
3653 throw new IllegalArgumentException("event time must not be in the future");
3654 }
3655
3656 mContext.enforceCallingOrSelfPermission(
3657 android.Manifest.permission.DEVICE_POWER, null);
3658
3659 final int uid = Binder.getCallingUid();
3660 final long ident = Binder.clearCallingIdentity();
3661 try {
3662 boostScreenBrightnessInternal(eventTime, uid);
3663 } finally {
3664 Binder.restoreCallingIdentity(ident);
3665 }
3666 }
3667
3668 @Override // Binder call
Bryce Lee84d6c0f2015-03-17 10:43:08 -07003669 public boolean isScreenBrightnessBoosted() {
3670 final long ident = Binder.clearCallingIdentity();
3671 try {
3672 return isScreenBrightnessBoostedInternal();
3673 } finally {
3674 Binder.restoreCallingIdentity(ident);
3675 }
3676 }
3677
3678 @Override // Binder call
Jeff Brown6f357d32014-01-15 20:40:55 -08003679 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3680 if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
3681 != PackageManager.PERMISSION_GRANTED) {
3682 pw.println("Permission Denial: can't dump PowerManager from from pid="
3683 + Binder.getCallingPid()
3684 + ", uid=" + Binder.getCallingUid());
3685 return;
3686 }
3687
3688 final long ident = Binder.clearCallingIdentity();
3689 try {
3690 dumpInternal(pw);
3691 } finally {
3692 Binder.restoreCallingIdentity(ident);
3693 }
3694 }
3695 }
3696
Jeff Brown4ccb8232014-01-16 22:16:42 -08003697 private final class LocalService extends PowerManagerInternal {
Jeff Brown6f357d32014-01-15 20:40:55 -08003698 @Override
Jeff Brown970d4132014-07-19 11:33:47 -07003699 public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) {
3700 if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT
3701 || screenBrightness > PowerManager.BRIGHTNESS_ON) {
3702 screenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown6f357d32014-01-15 20:40:55 -08003703 }
Jeff Brown970d4132014-07-19 11:33:47 -07003704 setScreenBrightnessOverrideFromWindowManagerInternal(screenBrightness);
Jeff Brown6f357d32014-01-15 20:40:55 -08003705 }
3706
Jeff Brown6f357d32014-01-15 20:40:55 -08003707 @Override
Jeff Brown970d4132014-07-19 11:33:47 -07003708 public void setButtonBrightnessOverrideFromWindowManager(int screenBrightness) {
Jeff Brown6f357d32014-01-15 20:40:55 -08003709 // Do nothing.
3710 // Button lights are not currently supported in the new implementation.
Jeff Brown6f357d32014-01-15 20:40:55 -08003711 }
3712
Jeff Brown970d4132014-07-19 11:33:47 -07003713 @Override
3714 public void setDozeOverrideFromDreamManager(int screenState, int screenBrightness) {
3715 switch (screenState) {
3716 case Display.STATE_UNKNOWN:
3717 case Display.STATE_OFF:
3718 case Display.STATE_DOZE:
3719 case Display.STATE_DOZE_SUSPEND:
3720 case Display.STATE_ON:
3721 break;
3722 default:
3723 screenState = Display.STATE_UNKNOWN;
3724 break;
3725 }
3726 if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT
3727 || screenBrightness > PowerManager.BRIGHTNESS_ON) {
3728 screenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
3729 }
3730 setDozeOverrideFromDreamManagerInternal(screenState, screenBrightness);
3731 }
3732
Jeff Brown6f357d32014-01-15 20:40:55 -08003733 @Override
Nick Armstrong-Crews56ecfcc2015-09-07 21:46:50 -07003734 public void setUserInactiveOverrideFromWindowManager() {
3735 setUserInactiveOverrideFromWindowManagerInternal();
3736 }
3737
3738 @Override
Jeff Brown6f357d32014-01-15 20:40:55 -08003739 public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) {
Jeff Brown970d4132014-07-19 11:33:47 -07003740 setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis);
Jeff Brown6f357d32014-01-15 20:40:55 -08003741 }
3742
3743 @Override
Jeff Brown5ce1cb22014-11-06 19:05:33 -08003744 public void setMaximumScreenOffTimeoutFromDeviceAdmin(int timeMs) {
3745 setMaximumScreenOffTimeoutFromDeviceAdminInternal(timeMs);
3746 }
3747
3748 @Override
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003749 public boolean getLowPowerModeEnabled() {
3750 synchronized (mLock) {
3751 return mLowPowerModeEnabled;
3752 }
3753 }
3754
3755 @Override
3756 public void registerLowPowerModeObserver(LowPowerModeListener listener) {
3757 synchronized (mLock) {
3758 mLowPowerModeListeners.add(listener);
3759 }
3760 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003761
3762 @Override
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003763 public boolean setDeviceIdleMode(boolean enabled) {
3764 return setDeviceIdleModeInternal(enabled);
3765 }
3766
3767 @Override
3768 public boolean setLightDeviceIdleMode(boolean enabled) {
3769 return setLightDeviceIdleModeInternal(enabled);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07003770 }
3771
3772 @Override
3773 public void setDeviceIdleWhitelist(int[] appids) {
3774 setDeviceIdleWhitelistInternal(appids);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003775 }
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003776
3777 @Override
Amith Yamasaniaf575b92015-05-29 15:35:26 -07003778 public void setDeviceIdleTempWhitelist(int[] appids) {
3779 setDeviceIdleTempWhitelistInternal(appids);
3780 }
3781
3782 @Override
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07003783 public void updateUidProcState(int uid, int procState) {
3784 updateUidProcStateInternal(uid, procState);
3785 }
3786
3787 @Override
3788 public void uidGone(int uid) {
3789 uidGoneInternal(uid);
3790 }
Michael Wrighta4d22d72015-09-16 23:19:26 +01003791
3792 @Override
3793 public void powerHint(int hintId, int data) {
3794 powerHintInternal(hintId, data);
3795 }
Jeff Brown6f357d32014-01-15 20:40:55 -08003796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797}