The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 17 | package com.android.server.power; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 19 | import com.android.internal.app.IAppOpsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.app.IBatteryStats; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 21 | import com.android.internal.os.BackgroundThread; |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 22 | import com.android.server.EventLogTags; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 23 | import com.android.server.ServiceThread; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 24 | import com.android.server.SystemService; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 25 | import com.android.server.am.BatteryStatsService; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 26 | import com.android.server.lights.Light; |
| 27 | import com.android.server.lights.LightsManager; |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 28 | import com.android.server.Watchdog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 30 | import android.Manifest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| 36 | import android.content.pm.PackageManager; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 37 | import android.content.res.Resources; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 38 | import android.database.ContentObserver; |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 39 | import android.hardware.SensorManager; |
| 40 | import android.hardware.SystemSensorManager; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 41 | import android.hardware.display.DisplayManagerInternal; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 42 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 43 | import android.net.Uri; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 44 | import android.os.BatteryManager; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 45 | import android.os.BatteryManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
| 47 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.IPowerManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 50 | import android.os.Looper; |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 51 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.PowerManager; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 53 | import android.os.PowerManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Process; |
| 55 | import android.os.RemoteException; |
| 56 | import android.os.SystemClock; |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 57 | import android.os.SystemProperties; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 58 | import android.os.Trace; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 60 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.provider.Settings; |
Jeff Brown | 567f7ca | 2014-01-30 23:38:03 -0800 | [diff] [blame] | 62 | import android.service.dreams.DreamManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.util.EventLog; |
| 64 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 65 | import android.util.Slog; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 66 | import android.util.TimeUtils; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 67 | import android.view.Display; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.view.WindowManagerPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | import java.io.FileDescriptor; |
| 71 | import java.io.PrintWriter; |
| 72 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 74 | import libcore.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 76 | /** |
| 77 | * The power manager service is responsible for coordinating power management |
| 78 | * functions on the device. |
| 79 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 80 | public final class PowerManagerService extends SystemService |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 81 | implements Watchdog.Monitor { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | private static final String TAG = "PowerManagerService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
Jeff Brown | 88c997a | 2012-06-22 13:57:45 -0700 | [diff] [blame] | 84 | private static final boolean DEBUG = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 85 | private static final boolean DEBUG_SPEW = DEBUG && true; |
Jeff Brown | 88c997a | 2012-06-22 13:57:45 -0700 | [diff] [blame] | 86 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 87 | // Message: Sent when a user activity timeout occurs to update the power state. |
| 88 | private static final int MSG_USER_ACTIVITY_TIMEOUT = 1; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 89 | // Message: Sent when the device enters or exits a dreaming or dozing state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 90 | private static final int MSG_SANDMAN = 2; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 91 | // Message: Sent when the screen on blocker is released. |
| 92 | private static final int MSG_SCREEN_ON_BLOCKER_RELEASED = 3; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 93 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 94 | // Dirty bit: mWakeLocks changed |
| 95 | private static final int DIRTY_WAKE_LOCKS = 1 << 0; |
| 96 | // Dirty bit: mWakefulness changed |
| 97 | private static final int DIRTY_WAKEFULNESS = 1 << 1; |
| 98 | // Dirty bit: user activity was poked or may have timed out |
| 99 | private static final int DIRTY_USER_ACTIVITY = 1 << 2; |
| 100 | // Dirty bit: actual display power state was updated asynchronously |
| 101 | private static final int DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED = 1 << 3; |
| 102 | // Dirty bit: mBootCompleted changed |
| 103 | private static final int DIRTY_BOOT_COMPLETED = 1 << 4; |
| 104 | // Dirty bit: settings changed |
| 105 | private static final int DIRTY_SETTINGS = 1 << 5; |
| 106 | // Dirty bit: mIsPowered changed |
| 107 | private static final int DIRTY_IS_POWERED = 1 << 6; |
| 108 | // Dirty bit: mStayOn changed |
| 109 | private static final int DIRTY_STAY_ON = 1 << 7; |
| 110 | // Dirty bit: battery state changed |
| 111 | private static final int DIRTY_BATTERY_STATE = 1 << 8; |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 112 | // Dirty bit: proximity state changed |
| 113 | private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 114 | // Dirty bit: screen on blocker state became held or unheld |
| 115 | private static final int DIRTY_SCREEN_ON_BLOCKER_RELEASED = 1 << 10; |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 116 | // Dirty bit: dock state changed |
| 117 | private static final int DIRTY_DOCK_STATE = 1 << 11; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 118 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 119 | // Wakefulness: The device is asleep and can only be awoken by a call to wakeUp(). |
| 120 | // The screen should be off or in the process of being turned off by the display controller. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 121 | // The device typically passes through the dozing state first. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 122 | private static final int WAKEFULNESS_ASLEEP = 0; |
| 123 | // Wakefulness: The device is fully awake. It can be put to sleep by a call to goToSleep(). |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 124 | // When the user activity timeout expires, the device may start dreaming or go to sleep. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 125 | private static final int WAKEFULNESS_AWAKE = 1; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 126 | // Wakefulness: The device is dreaming. It can be awoken by a call to wakeUp(), |
| 127 | // which ends the dream. The device goes to sleep when goToSleep() is called, when |
| 128 | // the dream ends or when unplugged. |
| 129 | // User activity may brighten the screen but does not end the dream. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 130 | private static final int WAKEFULNESS_DREAMING = 2; |
| 131 | // Wakefulness: The device is dozing. It is almost asleep but is allowing a special |
| 132 | // low-power "doze" dream to run which keeps the display on but lets the application |
| 133 | // processor be suspended. It can be awoken by a call to wakeUp() which ends the dream. |
| 134 | // The device fully goes to sleep if the dream cannot be started or ends on its own. |
| 135 | private static final int WAKEFULNESS_DOZING = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 137 | // Summarizes the state of all active wakelocks. |
| 138 | private static final int WAKE_LOCK_CPU = 1 << 0; |
| 139 | private static final int WAKE_LOCK_SCREEN_BRIGHT = 1 << 1; |
| 140 | private static final int WAKE_LOCK_SCREEN_DIM = 1 << 2; |
| 141 | private static final int WAKE_LOCK_BUTTON_BRIGHT = 1 << 3; |
| 142 | private static final int WAKE_LOCK_PROXIMITY_SCREEN_OFF = 1 << 4; |
Jeff Brown | 1042874 | 2012-10-09 15:47:30 -0700 | [diff] [blame] | 143 | private static final int WAKE_LOCK_STAY_AWAKE = 1 << 5; // only set if already awake |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 144 | private static final int WAKE_LOCK_DOZE = 1 << 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 146 | // Summarizes the user activity state. |
| 147 | private static final int USER_ACTIVITY_SCREEN_BRIGHT = 1 << 0; |
| 148 | private static final int USER_ACTIVITY_SCREEN_DIM = 1 << 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 150 | // 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 Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 152 | private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000; |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 153 | |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 154 | // Power hints defined in hardware/libhardware/include/hardware/power.h. |
| 155 | private static final int POWER_HINT_INTERACTION = 2; |
| 156 | private static final int POWER_HINT_LOW_POWER = 5; |
Ruchi Kandoi | 62b8a49 | 2014-04-17 18:01:40 -0700 | [diff] [blame] | 157 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 158 | private final Context mContext; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 159 | private final ServiceThread mHandlerThread; |
| 160 | private final PowerManagerHandler mHandler; |
| 161 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 162 | private LightsManager mLightsManager; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 163 | private BatteryManagerInternal mBatteryManagerInternal; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 164 | private DisplayManagerInternal mDisplayManagerInternal; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 165 | private IBatteryStats mBatteryStats; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 166 | private IAppOpsService mAppOps; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 167 | private WindowManagerPolicy mPolicy; |
| 168 | private Notifier mNotifier; |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 169 | private WirelessChargerDetector mWirelessChargerDetector; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 170 | private SettingsObserver mSettingsObserver; |
Jeff Brown | 567f7ca | 2014-01-30 23:38:03 -0800 | [diff] [blame] | 171 | private DreamManagerInternal mDreamManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 172 | private Light mAttentionLight; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 173 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 174 | private final Object mLock = new Object(); |
| 175 | |
| 176 | // A bitfield that indicates what parts of the power state have |
| 177 | // changed and need to be recalculated. |
| 178 | private int mDirty; |
| 179 | |
| 180 | // Indicates whether the device is awake or asleep or somewhere in between. |
| 181 | // This is distinct from the screen power state, which is managed separately. |
| 182 | private int mWakefulness; |
| 183 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 184 | // True if the sandman has just been summoned for the first time since entering the |
| 185 | // dreaming or dozing state. Indicates whether a new dream should begin. |
| 186 | private boolean mSandmanSummoned; |
| 187 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 188 | // True if MSG_SANDMAN has been scheduled. |
| 189 | private boolean mSandmanScheduled; |
| 190 | |
| 191 | // Table of all suspend blockers. |
| 192 | // There should only be a few of these. |
| 193 | private final ArrayList<SuspendBlocker> mSuspendBlockers = new ArrayList<SuspendBlocker>(); |
| 194 | |
| 195 | // Table of all wake locks acquired by applications. |
| 196 | private final ArrayList<WakeLock> mWakeLocks = new ArrayList<WakeLock>(); |
| 197 | |
| 198 | // A bitfield that summarizes the state of all active wakelocks. |
| 199 | private int mWakeLockSummary; |
| 200 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 201 | // True if the device is in an interactive state. |
| 202 | private boolean mInteractive; |
| 203 | private boolean mInteractiveChanging; |
| 204 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 205 | // If true, instructs the display controller to wait for the proximity sensor to |
| 206 | // go negative before turning the screen on. |
| 207 | private boolean mRequestWaitForNegativeProximity; |
| 208 | |
| 209 | // Timestamp of the last time the device was awoken or put to sleep. |
| 210 | private long mLastWakeTime; |
| 211 | private long mLastSleepTime; |
| 212 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 213 | // Timestamp of the last call to user activity. |
| 214 | private long mLastUserActivityTime; |
| 215 | private long mLastUserActivityTimeNoChangeLights; |
| 216 | |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 217 | // Timestamp of last interactive power hint. |
| 218 | private long mLastInteractivePowerHintTime; |
| 219 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 220 | // A bitfield that summarizes the effect of the user activity timer. |
| 221 | // A zero value indicates that the user activity timer has expired. |
| 222 | private int mUserActivitySummary; |
| 223 | |
| 224 | // The desired display power state. The actual state may lag behind the |
| 225 | // requested because it is updated asynchronously by the display power controller. |
| 226 | private final DisplayPowerRequest mDisplayPowerRequest = new DisplayPowerRequest(); |
| 227 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 228 | // True if the display power state has been fully applied, which means the display |
| 229 | // is actually on or actually off or whatever was requested. |
| 230 | private boolean mDisplayReady; |
| 231 | |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 232 | // The suspend blocker used to keep the CPU alive when an application has acquired |
| 233 | // a wake lock. |
| 234 | private final SuspendBlocker mWakeLockSuspendBlocker; |
| 235 | |
| 236 | // True if the wake lock suspend blocker has been acquired. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 237 | private boolean mHoldingWakeLockSuspendBlocker; |
| 238 | |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 239 | // The suspend blocker used to keep the CPU alive when the display is on, the |
| 240 | // display is getting ready or there is user activity (in which case the display |
| 241 | // must be on). |
| 242 | private final SuspendBlocker mDisplaySuspendBlocker; |
| 243 | |
| 244 | // True if the display suspend blocker has been acquired. |
| 245 | private boolean mHoldingDisplaySuspendBlocker; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 246 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 247 | // The screen on blocker used to keep the screen from turning on while the lock |
| 248 | // screen is coming up. |
| 249 | private final ScreenOnBlockerImpl mScreenOnBlocker; |
| 250 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 251 | // True if systemReady() has been called. |
| 252 | private boolean mSystemReady; |
| 253 | |
| 254 | // True if boot completed occurred. We keep the screen on until this happens. |
| 255 | private boolean mBootCompleted; |
| 256 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 257 | // True if auto-suspend mode is enabled. |
| 258 | // Refer to autosuspend.h. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 259 | private boolean mHalAutoSuspendModeEnabled; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 260 | |
| 261 | // True if interactive mode is enabled. |
| 262 | // Refer to power.h. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 263 | private boolean mHalInteractiveModeEnabled; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 264 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 265 | // True if the device is plugged into a power source. |
| 266 | private boolean mIsPowered; |
| 267 | |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 268 | // The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS. |
| 269 | private int mPlugType; |
| 270 | |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 271 | // The current battery level percentage. |
| 272 | private int mBatteryLevel; |
| 273 | |
| 274 | // The battery level percentage at the time the dream started. |
| 275 | // This is used to terminate a dream and go to sleep if the battery is |
| 276 | // draining faster than it is charging and the user activity timeout has expired. |
| 277 | private int mBatteryLevelWhenDreamStarted; |
| 278 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 279 | // The current dock state. |
| 280 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 281 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 282 | // True to decouple auto-suspend mode from the display state. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 283 | private boolean mDecoupleHalAutoSuspendModeFromDisplayConfig; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 284 | |
| 285 | // True to decouple interactive mode from the display state. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 286 | private boolean mDecoupleHalInteractiveModeFromDisplayConfig; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 287 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 288 | // True if the device should wake up when plugged or unplugged. |
| 289 | private boolean mWakeUpWhenPluggedOrUnpluggedConfig; |
| 290 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 291 | // True if the device should suspend when the screen is off due to proximity. |
| 292 | private boolean mSuspendWhenScreenOffDueToProximityConfig; |
| 293 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 294 | // True if dreams are supported on this device. |
| 295 | private boolean mDreamsSupportedConfig; |
| 296 | |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 297 | // Default value for dreams enabled |
| 298 | private boolean mDreamsEnabledByDefaultConfig; |
| 299 | |
| 300 | // Default value for dreams activate-on-sleep |
| 301 | private boolean mDreamsActivatedOnSleepByDefaultConfig; |
| 302 | |
| 303 | // Default value for dreams activate-on-dock |
| 304 | private boolean mDreamsActivatedOnDockByDefaultConfig; |
| 305 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 306 | // True if dreams can run while not plugged in. |
| 307 | private boolean mDreamsEnabledOnBatteryConfig; |
| 308 | |
| 309 | // Minimum battery level to allow dreaming when powered. |
| 310 | // Use -1 to disable this safety feature. |
| 311 | private int mDreamsBatteryLevelMinimumWhenPoweredConfig; |
| 312 | |
| 313 | // Minimum battery level to allow dreaming when not powered. |
| 314 | // Use -1 to disable this safety feature. |
| 315 | private int mDreamsBatteryLevelMinimumWhenNotPoweredConfig; |
| 316 | |
| 317 | // If the battery level drops by this percentage and the user activity timeout |
| 318 | // has expired, then assume the device is receiving insufficient current to charge |
| 319 | // effectively and terminate the dream. Use -1 to disable this safety feature. |
| 320 | private int mDreamsBatteryLevelDrainCutoffConfig; |
| 321 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 322 | // True if dreams are enabled by the user. |
| 323 | private boolean mDreamsEnabledSetting; |
| 324 | |
John Spurlock | 1a868b7 | 2012-08-22 09:56:51 -0400 | [diff] [blame] | 325 | // True if dreams should be activated on sleep. |
| 326 | private boolean mDreamsActivateOnSleepSetting; |
| 327 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 328 | // True if dreams should be activated on dock. |
| 329 | private boolean mDreamsActivateOnDockSetting; |
| 330 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 331 | // True if doze should not be started until after the screen off transition. |
| 332 | private boolean mDozeAfterScreenOffConfig; |
| 333 | |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 334 | // The minimum screen off timeout, in milliseconds. |
| 335 | private int mMinimumScreenOffTimeoutConfig; |
| 336 | |
| 337 | // The screen dim duration, in milliseconds. |
| 338 | // This is subtracted from the end of the screen off timeout so the |
| 339 | // minimum screen off timeout should be longer than this. |
| 340 | private int mMaximumScreenDimDurationConfig; |
| 341 | |
| 342 | // The maximum screen dim time expressed as a ratio relative to the screen |
| 343 | // off timeout. If the screen off timeout is very short then we want the |
| 344 | // dim timeout to also be quite short so that most of the time is spent on. |
| 345 | // Otherwise the user won't get much screen on time before dimming occurs. |
| 346 | private float mMaximumScreenDimRatioConfig; |
| 347 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 348 | // The screen off timeout setting value in milliseconds. |
| 349 | private int mScreenOffTimeoutSetting; |
| 350 | |
| 351 | // The maximum allowable screen off timeout according to the device |
| 352 | // administration policy. Overrides other settings. |
| 353 | private int mMaximumScreenOffTimeoutFromDeviceAdmin = Integer.MAX_VALUE; |
| 354 | |
| 355 | // The stay on while plugged in setting. |
| 356 | // A bitfield of battery conditions under which to make the screen stay on. |
| 357 | private int mStayOnWhilePluggedInSetting; |
| 358 | |
| 359 | // True if the device should stay on. |
| 360 | private boolean mStayOn; |
| 361 | |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 362 | // True if the proximity sensor reads a positive result. |
| 363 | private boolean mProximityPositive; |
| 364 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 365 | // Screen brightness setting limits. |
| 366 | private int mScreenBrightnessSettingMinimum; |
| 367 | private int mScreenBrightnessSettingMaximum; |
| 368 | private int mScreenBrightnessSettingDefault; |
| 369 | |
| 370 | // The screen brightness setting, from 0 to 255. |
| 371 | // Use -1 if no value has been set. |
| 372 | private int mScreenBrightnessSetting; |
| 373 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 374 | // The screen auto-brightness adjustment setting, from -1 to 1. |
| 375 | // Use 0 if there is no adjustment. |
| 376 | private float mScreenAutoBrightnessAdjustmentSetting; |
| 377 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 378 | // The screen brightness mode. |
| 379 | // One of the Settings.System.SCREEN_BRIGHTNESS_MODE_* constants. |
| 380 | private int mScreenBrightnessModeSetting; |
| 381 | |
| 382 | // The screen brightness setting override from the window manager |
| 383 | // to allow the current foreground activity to override the brightness. |
| 384 | // Use -1 to disable. |
| 385 | private int mScreenBrightnessOverrideFromWindowManager = -1; |
| 386 | |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 387 | // The user activity timeout override from the window manager |
| 388 | // to allow the current foreground activity to override the user activity timeout. |
| 389 | // Use -1 to disable. |
| 390 | private long mUserActivityTimeoutOverrideFromWindowManager = -1; |
| 391 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 392 | // The screen brightness setting override from the settings application |
| 393 | // to temporarily adjust the brightness until next updated, |
| 394 | // Use -1 to disable. |
| 395 | private int mTemporaryScreenBrightnessSettingOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 397 | // The screen brightness adjustment setting override from the settings |
| 398 | // application to temporarily adjust the auto-brightness adjustment factor |
| 399 | // until next updated, in the range -1..1. |
| 400 | // Use NaN to disable. |
| 401 | private float mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = Float.NaN; |
| 402 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 403 | // The screen state to use while dozing. |
| 404 | private int mDozeScreenStateOverrideFromDreamManager = Display.STATE_UNKNOWN; |
| 405 | |
| 406 | // The screen brightness to use while dozing. |
| 407 | private int mDozeScreenBrightnessOverrideFromDreamManager = PowerManager.BRIGHTNESS_DEFAULT; |
| 408 | |
Jeff Brown | 9ba8d78 | 2012-10-01 16:38:23 -0700 | [diff] [blame] | 409 | // Time when we last logged a warning about calling userActivity() without permission. |
| 410 | private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE; |
| 411 | |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 412 | // If true, the device is in low power mode. |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 413 | private boolean mLowPowerModeEnabled; |
| 414 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 415 | // Current state of the low power mode setting. |
| 416 | private boolean mLowPowerModeSetting; |
| 417 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 418 | // Current state of whether the settings are allowing auto low power mode. |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 419 | private boolean mAutoLowPowerModeConfigured; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 420 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 421 | // The user turned off low power mode below the trigger level |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 422 | private boolean mAutoLowPowerModeSnoozing; |
| 423 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 424 | // True if the battery level is currently considered low. |
| 425 | private boolean mBatteryLevelLow; |
| 426 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 427 | private final ArrayList<PowerManagerInternal.LowPowerModeListener> mLowPowerModeListeners |
| 428 | = new ArrayList<PowerManagerInternal.LowPowerModeListener>(); |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 429 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 430 | private native void nativeInit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 432 | private static native void nativeAcquireSuspendBlocker(String name); |
| 433 | private static native void nativeReleaseSuspendBlocker(String name); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 434 | private static native void nativeSetInteractive(boolean enable); |
| 435 | private static native void nativeSetAutoSuspend(boolean enable); |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 436 | private static native void nativeSendPowerHint(int hintId, int data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 438 | public PowerManagerService(Context context) { |
| 439 | super(context); |
| 440 | mContext = context; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 441 | mHandlerThread = new ServiceThread(TAG, |
| 442 | Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/); |
| 443 | mHandlerThread.start(); |
| 444 | mHandler = new PowerManagerHandler(mHandlerThread.getLooper()); |
| 445 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 446 | synchronized (mLock) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 447 | mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService.WakeLocks"); |
| 448 | mDisplaySuspendBlocker = createSuspendBlockerLocked("PowerManagerService.Display"); |
| 449 | mDisplaySuspendBlocker.acquire(); |
| 450 | mHoldingDisplaySuspendBlocker = true; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 451 | mHalAutoSuspendModeEnabled = false; |
| 452 | mHalInteractiveModeEnabled = true; |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 453 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 454 | mScreenOnBlocker = new ScreenOnBlockerImpl(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 455 | mWakefulness = WAKEFULNESS_AWAKE; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 456 | mInteractive = true; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 457 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 458 | nativeInit(); |
| 459 | nativeSetAutoSuspend(false); |
| 460 | nativeSetInteractive(true); |
| 461 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 464 | @Override |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 465 | public void onStart() { |
| 466 | publishBinderService(Context.POWER_SERVICE, new BinderService()); |
| 467 | publishLocalService(PowerManagerInternal.class, new LocalService()); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 468 | |
| 469 | Watchdog.getInstance().addMonitor(this); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 470 | Watchdog.getInstance().addThread(mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 471 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 472 | |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 473 | @Override |
| 474 | public void onBootPhase(int phase) { |
Craig Mautner | 6e2f395 | 2014-09-09 14:26:41 -0700 | [diff] [blame] | 475 | synchronized (mLock) { |
| 476 | if (phase == PHASE_BOOT_COMPLETED) { |
| 477 | final long now = SystemClock.uptimeMillis(); |
| 478 | mBootCompleted = true; |
| 479 | mDirty |= DIRTY_BOOT_COMPLETED; |
| 480 | userActivityNoUpdateLocked( |
| 481 | now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
| 482 | updatePowerStateLocked(); |
| 483 | } |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 487 | public void systemReady(IAppOpsService appOps) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 488 | synchronized (mLock) { |
| 489 | mSystemReady = true; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 490 | mAppOps = appOps; |
| 491 | mDreamManager = getLocalService(DreamManagerInternal.class); |
| 492 | mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class); |
| 493 | mPolicy = getLocalService(WindowManagerPolicy.class); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 494 | mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 496 | PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 497 | mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting(); |
| 498 | mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting(); |
| 499 | mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 501 | SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper()); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 502 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 503 | // The notifier runs on the system server's main looper so as not to interfere |
| 504 | // with the animations and other critical functions of the power manager. |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 505 | mBatteryStats = BatteryStatsService.getService(); |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 506 | mNotifier = new Notifier(Looper.getMainLooper(), mContext, mBatteryStats, |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 507 | mAppOps, createSuspendBlockerLocked("PowerManagerService.Broadcasts"), |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 508 | mScreenOnBlocker, mPolicy); |
| 509 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 510 | mWirelessChargerDetector = new WirelessChargerDetector(sensorManager, |
Jeff Brown | dbcc8a2 | 2013-10-01 16:16:44 -0700 | [diff] [blame] | 511 | createSuspendBlockerLocked("PowerManagerService.WirelessChargerDetector"), |
| 512 | mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 513 | mSettingsObserver = new SettingsObserver(mHandler); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 514 | |
| 515 | mLightsManager = getLocalService(LightsManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 516 | mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 517 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 518 | // Initialize display power management. |
| 519 | mDisplayManagerInternal.initPowerManagement( |
| 520 | mDisplayPowerCallbacks, mHandler, sensorManager); |
| 521 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 522 | // Register for broadcasts from other components of the system. |
| 523 | IntentFilter filter = new IntentFilter(); |
| 524 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 525 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 526 | mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 527 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 528 | filter = new IntentFilter(); |
Dianne Hackborn | be87e2f | 2012-09-28 16:31:34 -0700 | [diff] [blame] | 529 | filter.addAction(Intent.ACTION_DREAMING_STARTED); |
| 530 | filter.addAction(Intent.ACTION_DREAMING_STOPPED); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 531 | mContext.registerReceiver(new DreamReceiver(), filter, null, mHandler); |
| 532 | |
| 533 | filter = new IntentFilter(); |
| 534 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| 535 | mContext.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 536 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 537 | filter = new IntentFilter(); |
| 538 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
| 539 | mContext.registerReceiver(new DockReceiver(), filter, null, mHandler); |
| 540 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 541 | // Register for settings changes. |
| 542 | final ContentResolver resolver = mContext.getContentResolver(); |
| 543 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 544 | Settings.Secure.SCREENSAVER_ENABLED), |
| 545 | false, mSettingsObserver, UserHandle.USER_ALL); |
John Spurlock | 1a868b7 | 2012-08-22 09:56:51 -0400 | [diff] [blame] | 546 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 547 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP), |
| 548 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 549 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 550 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK), |
| 551 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 552 | resolver.registerContentObserver(Settings.System.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 553 | Settings.System.SCREEN_OFF_TIMEOUT), |
| 554 | false, mSettingsObserver, UserHandle.USER_ALL); |
Christopher Tate | ad73532 | 2012-09-07 14:19:43 -0700 | [diff] [blame] | 555 | resolver.registerContentObserver(Settings.Global.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 556 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN), |
| 557 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 558 | resolver.registerContentObserver(Settings.System.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 559 | Settings.System.SCREEN_BRIGHTNESS), |
| 560 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 561 | resolver.registerContentObserver(Settings.System.getUriFor( |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 562 | Settings.System.SCREEN_BRIGHTNESS_MODE), |
| 563 | false, mSettingsObserver, UserHandle.USER_ALL); |
Adrian Roos | 6dee605 | 2014-05-14 19:44:18 +0200 | [diff] [blame] | 564 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 565 | Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ), |
| 566 | false, mSettingsObserver, UserHandle.USER_ALL); |
Ruchi Kandoi | 62b8a49 | 2014-04-17 18:01:40 -0700 | [diff] [blame] | 567 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 568 | Settings.Global.LOW_POWER_MODE), |
| 569 | false, mSettingsObserver, UserHandle.USER_ALL); |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 570 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 571 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL), |
| 572 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 573 | // Go. |
| 574 | readConfigurationLocked(); |
| 575 | updateSettingsLocked(); |
| 576 | mDirty |= DIRTY_BATTERY_STATE; |
| 577 | updatePowerStateLocked(); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | private void readConfigurationLocked() { |
| 582 | final Resources resources = mContext.getResources(); |
| 583 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 584 | mDecoupleHalAutoSuspendModeFromDisplayConfig = resources.getBoolean( |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 585 | com.android.internal.R.bool.config_powerDecoupleAutoSuspendModeFromDisplay); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 586 | mDecoupleHalInteractiveModeFromDisplayConfig = resources.getBoolean( |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 587 | com.android.internal.R.bool.config_powerDecoupleInteractiveModeFromDisplay); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 588 | mWakeUpWhenPluggedOrUnpluggedConfig = resources.getBoolean( |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 589 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 590 | mSuspendWhenScreenOffDueToProximityConfig = resources.getBoolean( |
| 591 | com.android.internal.R.bool.config_suspendWhenScreenOffDueToProximity); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 592 | mDreamsSupportedConfig = resources.getBoolean( |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 593 | com.android.internal.R.bool.config_dreamsSupported); |
| 594 | mDreamsEnabledByDefaultConfig = resources.getBoolean( |
| 595 | com.android.internal.R.bool.config_dreamsEnabledByDefault); |
| 596 | mDreamsActivatedOnSleepByDefaultConfig = resources.getBoolean( |
| 597 | com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault); |
| 598 | mDreamsActivatedOnDockByDefaultConfig = resources.getBoolean( |
| 599 | com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 600 | mDreamsEnabledOnBatteryConfig = resources.getBoolean( |
| 601 | com.android.internal.R.bool.config_dreamsEnabledOnBattery); |
| 602 | mDreamsBatteryLevelMinimumWhenPoweredConfig = resources.getInteger( |
| 603 | com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenPowered); |
| 604 | mDreamsBatteryLevelMinimumWhenNotPoweredConfig = resources.getInteger( |
| 605 | com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenNotPowered); |
| 606 | mDreamsBatteryLevelDrainCutoffConfig = resources.getInteger( |
| 607 | com.android.internal.R.integer.config_dreamsBatteryLevelDrainCutoff); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 608 | mDozeAfterScreenOffConfig = resources.getBoolean( |
| 609 | com.android.internal.R.bool.config_dozeAfterScreenOff); |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 610 | mMinimumScreenOffTimeoutConfig = resources.getInteger( |
| 611 | com.android.internal.R.integer.config_minimumScreenOffTimeout); |
| 612 | mMaximumScreenDimDurationConfig = resources.getInteger( |
| 613 | com.android.internal.R.integer.config_maximumScreenDimDuration); |
| 614 | mMaximumScreenDimRatioConfig = resources.getFraction( |
| 615 | com.android.internal.R.fraction.config_maximumScreenDimRatio, 1, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 618 | private void updateSettingsLocked() { |
| 619 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 620 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 621 | mDreamsEnabledSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 622 | Settings.Secure.SCREENSAVER_ENABLED, |
| 623 | mDreamsEnabledByDefaultConfig ? 1 : 0, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 624 | UserHandle.USER_CURRENT) != 0); |
| 625 | mDreamsActivateOnSleepSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 626 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP, |
| 627 | mDreamsActivatedOnSleepByDefaultConfig ? 1 : 0, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 628 | UserHandle.USER_CURRENT) != 0); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 629 | mDreamsActivateOnDockSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 630 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, |
| 631 | mDreamsActivatedOnDockByDefaultConfig ? 1 : 0, |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 632 | UserHandle.USER_CURRENT) != 0); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 633 | mScreenOffTimeoutSetting = Settings.System.getIntForUser(resolver, |
| 634 | Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT, |
| 635 | UserHandle.USER_CURRENT); |
Christopher Tate | ad73532 | 2012-09-07 14:19:43 -0700 | [diff] [blame] | 636 | mStayOnWhilePluggedInSetting = Settings.Global.getInt(resolver, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 637 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_AC); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 638 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 639 | final int oldScreenBrightnessSetting = mScreenBrightnessSetting; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 640 | mScreenBrightnessSetting = Settings.System.getIntForUser(resolver, |
| 641 | Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessSettingDefault, |
| 642 | UserHandle.USER_CURRENT); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 643 | if (oldScreenBrightnessSetting != mScreenBrightnessSetting) { |
| 644 | mTemporaryScreenBrightnessSettingOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 646 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 647 | final float oldScreenAutoBrightnessAdjustmentSetting = |
| 648 | mScreenAutoBrightnessAdjustmentSetting; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 649 | mScreenAutoBrightnessAdjustmentSetting = Settings.System.getFloatForUser(resolver, |
| 650 | Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f, |
| 651 | UserHandle.USER_CURRENT); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 652 | if (oldScreenAutoBrightnessAdjustmentSetting != mScreenAutoBrightnessAdjustmentSetting) { |
Jeff Brown | 5d03a53 | 2012-08-22 13:22:02 -0700 | [diff] [blame] | 653 | mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = Float.NaN; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 656 | mScreenBrightnessModeSetting = Settings.System.getIntForUser(resolver, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 657 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 658 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 659 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 660 | final boolean lowPowerModeEnabled = Settings.Global.getInt(resolver, |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 661 | Settings.Global.LOW_POWER_MODE, 0) != 0; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 662 | final boolean autoLowPowerModeConfigured = Settings.Global.getInt(resolver, |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 663 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 664 | if (lowPowerModeEnabled != mLowPowerModeSetting |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 665 | || autoLowPowerModeConfigured != mAutoLowPowerModeConfigured) { |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 666 | mLowPowerModeSetting = lowPowerModeEnabled; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 667 | mAutoLowPowerModeConfigured = autoLowPowerModeConfigured; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 668 | updateLowPowerModeLocked(); |
| 669 | } |
| 670 | |
| 671 | mDirty |= DIRTY_SETTINGS; |
| 672 | } |
| 673 | |
| 674 | void updateLowPowerModeLocked() { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 675 | if (mIsPowered && mLowPowerModeSetting) { |
| 676 | if (DEBUG_SPEW) { |
| 677 | Slog.d(TAG, "updateLowPowerModeLocked: powered, turning setting off"); |
| 678 | } |
| 679 | // Turn setting off if powered |
| 680 | Settings.Global.putInt(mContext.getContentResolver(), |
| 681 | Settings.Global.LOW_POWER_MODE, 0); |
| 682 | mLowPowerModeSetting = false; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 683 | } |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 684 | final boolean autoLowPowerModeEnabled = !mIsPowered && mAutoLowPowerModeConfigured |
| 685 | && !mAutoLowPowerModeSnoozing && mBatteryLevelLow; |
| 686 | final boolean lowPowerModeEnabled = mLowPowerModeSetting || autoLowPowerModeEnabled; |
| 687 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 688 | if (mLowPowerModeEnabled != lowPowerModeEnabled) { |
| 689 | mLowPowerModeEnabled = lowPowerModeEnabled; |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 690 | powerHintInternal(POWER_HINT_LOW_POWER, lowPowerModeEnabled ? 1 : 0); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 691 | BackgroundThread.getHandler().post(new Runnable() { |
| 692 | @Override |
| 693 | public void run() { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 694 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING) |
| 695 | .putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, mLowPowerModeEnabled) |
| 696 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 697 | mContext.sendBroadcast(intent); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 698 | ArrayList<PowerManagerInternal.LowPowerModeListener> listeners; |
| 699 | synchronized (mLock) { |
| 700 | listeners = new ArrayList<PowerManagerInternal.LowPowerModeListener>( |
| 701 | mLowPowerModeListeners); |
| 702 | } |
| 703 | for (int i=0; i<listeners.size(); i++) { |
| 704 | listeners.get(i).onLowPowerModeChanged(lowPowerModeEnabled); |
| 705 | } |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 706 | intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 707 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 708 | mContext.sendBroadcast(intent); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 709 | } |
| 710 | }); |
Ruchi Kandoi | 62b8a49 | 2014-04-17 18:01:40 -0700 | [diff] [blame] | 711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 714 | private void handleSettingsChangedLocked() { |
| 715 | updateSettingsLocked(); |
| 716 | updatePowerStateLocked(); |
| 717 | } |
| 718 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 719 | private void acquireWakeLockInternal(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 720 | WorkSource ws, String historyTag, int uid, int pid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 721 | synchronized (mLock) { |
| 722 | if (DEBUG_SPEW) { |
| 723 | Slog.d(TAG, "acquireWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 724 | + ", flags=0x" + Integer.toHexString(flags) |
| 725 | + ", tag=\"" + tag + "\", ws=" + ws + ", uid=" + uid + ", pid=" + pid); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 726 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 728 | WakeLock wakeLock; |
| 729 | int index = findWakeLockIndexLocked(lock); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 730 | boolean notifyAcquire; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 731 | if (index >= 0) { |
| 732 | wakeLock = mWakeLocks.get(index); |
| 733 | if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid)) { |
| 734 | // Update existing wake lock. This shouldn't happen but is harmless. |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 735 | notifyWakeLockChangingLocked(wakeLock, flags, tag, packageName, |
| 736 | uid, pid, ws, historyTag); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 737 | wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 739 | notifyAcquire = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | } else { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 741 | wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 742 | try { |
| 743 | lock.linkToDeath(wakeLock, 0); |
| 744 | } catch (RemoteException ex) { |
| 745 | throw new IllegalArgumentException("Wake lock is already dead."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 747 | mWakeLocks.add(wakeLock); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 748 | notifyAcquire = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 751 | applyWakeLockFlagsOnAcquireLocked(wakeLock, uid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 752 | mDirty |= DIRTY_WAKE_LOCKS; |
| 753 | updatePowerStateLocked(); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 754 | if (notifyAcquire) { |
| 755 | // This needs to be done last so we are sure we have acquired the |
| 756 | // kernel wake lock. Otherwise we have a race where the system may |
| 757 | // go to sleep between the time we start the accounting in battery |
| 758 | // stats and when we actually get around to telling the kernel to |
| 759 | // stay awake. |
| 760 | notifyWakeLockAcquiredLocked(wakeLock); |
| 761 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 765 | @SuppressWarnings("deprecation") |
Craig Mautner | 6edb6db | 2012-11-20 18:21:12 -0800 | [diff] [blame] | 766 | private static boolean isScreenLock(final WakeLock wakeLock) { |
| 767 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 768 | case PowerManager.FULL_WAKE_LOCK: |
| 769 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 770 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 771 | return true; |
| 772 | } |
| 773 | return false; |
| 774 | } |
| 775 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 776 | private void applyWakeLockFlagsOnAcquireLocked(WakeLock wakeLock, int uid) { |
Jeff Brown | 6eade79 | 2013-09-10 18:45:25 -0700 | [diff] [blame] | 777 | if ((wakeLock.mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0 |
| 778 | && isScreenLock(wakeLock)) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 779 | wakeUpNoUpdateLocked(SystemClock.uptimeMillis(), uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 783 | private void releaseWakeLockInternal(IBinder lock, int flags) { |
| 784 | synchronized (mLock) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 785 | int index = findWakeLockIndexLocked(lock); |
| 786 | if (index < 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 787 | if (DEBUG_SPEW) { |
| 788 | Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 789 | + " [not found], flags=0x" + Integer.toHexString(flags)); |
| 790 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | return; |
| 792 | } |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 793 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 794 | WakeLock wakeLock = mWakeLocks.get(index); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 795 | if (DEBUG_SPEW) { |
| 796 | Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 797 | + " [" + wakeLock.mTag + "], flags=0x" + Integer.toHexString(flags)); |
| 798 | } |
| 799 | |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 800 | if ((flags & PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 801 | mRequestWaitForNegativeProximity = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 804 | wakeLock.mLock.unlinkToDeath(wakeLock, 0); |
| 805 | removeWakeLockLocked(wakeLock, index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | } |
| 807 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 808 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 809 | private void handleWakeLockDeath(WakeLock wakeLock) { |
| 810 | synchronized (mLock) { |
| 811 | if (DEBUG_SPEW) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 812 | Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock) |
| 813 | + " [" + wakeLock.mTag + "]"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | int index = mWakeLocks.indexOf(wakeLock); |
| 817 | if (index < 0) { |
| 818 | return; |
| 819 | } |
| 820 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 821 | removeWakeLockLocked(wakeLock, index); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 822 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 823 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 824 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 825 | private void removeWakeLockLocked(WakeLock wakeLock, int index) { |
| 826 | mWakeLocks.remove(index); |
| 827 | notifyWakeLockReleasedLocked(wakeLock); |
| 828 | |
| 829 | applyWakeLockFlagsOnReleaseLocked(wakeLock); |
| 830 | mDirty |= DIRTY_WAKE_LOCKS; |
| 831 | updatePowerStateLocked(); |
| 832 | } |
| 833 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 834 | private void applyWakeLockFlagsOnReleaseLocked(WakeLock wakeLock) { |
Jeff Brown | 6eade79 | 2013-09-10 18:45:25 -0700 | [diff] [blame] | 835 | if ((wakeLock.mFlags & PowerManager.ON_AFTER_RELEASE) != 0 |
| 836 | && isScreenLock(wakeLock)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 837 | userActivityNoUpdateLocked(SystemClock.uptimeMillis(), |
| 838 | PowerManager.USER_ACTIVITY_EVENT_OTHER, |
| 839 | PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS, |
| 840 | wakeLock.mOwnerUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 844 | private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag, |
| 845 | int callingUid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 846 | synchronized (mLock) { |
| 847 | int index = findWakeLockIndexLocked(lock); |
| 848 | if (index < 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 849 | if (DEBUG_SPEW) { |
| 850 | Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock) |
| 851 | + " [not found], ws=" + ws); |
| 852 | } |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 853 | throw new IllegalArgumentException("Wake lock not active: " + lock |
| 854 | + " from uid " + callingUid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | WakeLock wakeLock = mWakeLocks.get(index); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 858 | if (DEBUG_SPEW) { |
| 859 | Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock) |
| 860 | + " [" + wakeLock.mTag + "], ws=" + ws); |
| 861 | } |
| 862 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 863 | if (!wakeLock.hasSameWorkSource(ws)) { |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 864 | notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag, |
| 865 | wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid, |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 866 | ws, historyTag); |
| 867 | wakeLock.mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 868 | wakeLock.updateWorkSource(ws); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | private int findWakeLockIndexLocked(IBinder lock) { |
| 874 | final int count = mWakeLocks.size(); |
| 875 | for (int i = 0; i < count; i++) { |
| 876 | if (mWakeLocks.get(i).mLock == lock) { |
| 877 | return i; |
| 878 | } |
| 879 | } |
| 880 | return -1; |
| 881 | } |
| 882 | |
| 883 | private void notifyWakeLockAcquiredLocked(WakeLock wakeLock) { |
| 884 | if (mSystemReady) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 885 | wakeLock.mNotifiedAcquired = true; |
| 886 | mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 887 | wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource, |
| 888 | wakeLock.mHistoryTag); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 892 | private void notifyWakeLockChangingLocked(WakeLock wakeLock, int flags, String tag, |
| 893 | String packageName, int uid, int pid, WorkSource ws, String historyTag) { |
| 894 | if (mSystemReady && wakeLock.mNotifiedAcquired) { |
| 895 | mNotifier.onWakeLockChanging(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName, |
| 896 | wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource, |
| 897 | wakeLock.mHistoryTag, flags, tag, packageName, uid, pid, ws, historyTag); |
| 898 | } |
| 899 | } |
| 900 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 901 | private void notifyWakeLockReleasedLocked(WakeLock wakeLock) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 902 | if (mSystemReady && wakeLock.mNotifiedAcquired) { |
| 903 | wakeLock.mNotifiedAcquired = false; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 904 | mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag, |
| 905 | wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid, |
| 906 | wakeLock.mWorkSource, wakeLock.mHistoryTag); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 910 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 911 | private boolean isWakeLockLevelSupportedInternal(int level) { |
| 912 | synchronized (mLock) { |
| 913 | switch (level) { |
| 914 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 915 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 916 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 917 | case PowerManager.FULL_WAKE_LOCK: |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 918 | case PowerManager.DOZE_WAKE_LOCK: |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 919 | return true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 920 | |
| 921 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 922 | return mSystemReady && mDisplayManagerInternal.isProximitySensorAvailable(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 923 | |
| 924 | default: |
| 925 | return false; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 930 | // Called from native code. |
| 931 | private void userActivityFromNative(long eventTime, int event, int flags) { |
| 932 | userActivityInternal(eventTime, event, flags, Process.SYSTEM_UID); |
| 933 | } |
| 934 | |
| 935 | private void userActivityInternal(long eventTime, int event, int flags, int uid) { |
| 936 | synchronized (mLock) { |
| 937 | if (userActivityNoUpdateLocked(eventTime, event, flags, uid)) { |
| 938 | updatePowerStateLocked(); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) { |
| 944 | if (DEBUG_SPEW) { |
| 945 | Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime |
| 946 | + ", event=" + event + ", flags=0x" + Integer.toHexString(flags) |
| 947 | + ", uid=" + uid); |
| 948 | } |
| 949 | |
| 950 | if (eventTime < mLastSleepTime || eventTime < mLastWakeTime |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 951 | || !mBootCompleted || !mSystemReady) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 952 | return false; |
| 953 | } |
| 954 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 955 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "userActivity"); |
| 956 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 957 | if (eventTime > mLastInteractivePowerHintTime) { |
| 958 | powerHintInternal(POWER_HINT_INTERACTION, 0); |
| 959 | mLastInteractivePowerHintTime = eventTime; |
| 960 | } |
| 961 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 962 | mNotifier.onUserActivity(event, uid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 963 | |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 964 | if (mWakefulness == WAKEFULNESS_ASLEEP |
| 965 | || mWakefulness == WAKEFULNESS_DOZING |
| 966 | || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) { |
| 967 | return false; |
| 968 | } |
| 969 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 970 | if ((flags & PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS) != 0) { |
| 971 | if (eventTime > mLastUserActivityTimeNoChangeLights |
| 972 | && eventTime > mLastUserActivityTime) { |
| 973 | mLastUserActivityTimeNoChangeLights = eventTime; |
| 974 | mDirty |= DIRTY_USER_ACTIVITY; |
| 975 | return true; |
| 976 | } |
| 977 | } else { |
| 978 | if (eventTime > mLastUserActivityTime) { |
| 979 | mLastUserActivityTime = eventTime; |
| 980 | mDirty |= DIRTY_USER_ACTIVITY; |
| 981 | return true; |
| 982 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 983 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 984 | } finally { |
| 985 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 986 | } |
| 987 | return false; |
| 988 | } |
| 989 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 990 | private void wakeUpInternal(long eventTime, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 991 | synchronized (mLock) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 992 | if (wakeUpNoUpdateLocked(eventTime, uid)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 993 | updatePowerStateLocked(); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 996 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 997 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 998 | private boolean wakeUpNoUpdateLocked(long eventTime, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 999 | if (DEBUG_SPEW) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1000 | Slog.d(TAG, "wakeUpNoUpdateLocked: eventTime=" + eventTime + ", uid=" + uid); |
Joe Onorato | 60607a9 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1001 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1002 | |
| 1003 | if (eventTime < mLastSleepTime || mWakefulness == WAKEFULNESS_AWAKE |
| 1004 | || !mBootCompleted || !mSystemReady) { |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1008 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp"); |
| 1009 | try { |
| 1010 | switch (mWakefulness) { |
| 1011 | case WAKEFULNESS_ASLEEP: |
| 1012 | Slog.i(TAG, "Waking up from sleep (uid " + uid +")..."); |
| 1013 | break; |
| 1014 | case WAKEFULNESS_DREAMING: |
| 1015 | Slog.i(TAG, "Waking up from dream (uid " + uid +")..."); |
| 1016 | break; |
| 1017 | case WAKEFULNESS_DOZING: |
| 1018 | Slog.i(TAG, "Waking up from dozing (uid " + uid +")..."); |
| 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | mLastWakeTime = eventTime; |
| 1023 | mDirty |= DIRTY_WAKEFULNESS; |
| 1024 | mWakefulness = WAKEFULNESS_AWAKE; |
| 1025 | setInteractiveStateLocked(true, 0); |
| 1026 | |
| 1027 | userActivityNoUpdateLocked( |
| 1028 | eventTime, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, uid); |
| 1029 | } finally { |
| 1030 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1031 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1032 | return true; |
| 1033 | } |
| 1034 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1035 | private void goToSleepInternal(long eventTime, int reason, int flags, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1036 | synchronized (mLock) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1037 | if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1038 | updatePowerStateLocked(); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1043 | // This method is called goToSleep for historical reasons but we actually start |
| 1044 | // dozing before really going to sleep. |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1045 | @SuppressWarnings("deprecation") |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1046 | private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1047 | if (DEBUG_SPEW) { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1048 | Slog.d(TAG, "goToSleepNoUpdateLocked: eventTime=" + eventTime |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1049 | + ", reason=" + reason + ", flags=" + flags + ", uid=" + uid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1052 | if (eventTime < mLastWakeTime |
| 1053 | || mWakefulness == WAKEFULNESS_ASLEEP |
| 1054 | || mWakefulness == WAKEFULNESS_DOZING |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1055 | || !mBootCompleted || !mSystemReady) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1059 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep"); |
| 1060 | try { |
| 1061 | switch (reason) { |
| 1062 | case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN: |
| 1063 | Slog.i(TAG, "Going to sleep due to device administration policy " |
| 1064 | + "(uid " + uid +")..."); |
| 1065 | break; |
| 1066 | case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT: |
| 1067 | Slog.i(TAG, "Going to sleep due to screen timeout (uid " + uid +")..."); |
| 1068 | break; |
| 1069 | case PowerManager.GO_TO_SLEEP_REASON_LID_SWITCH: |
| 1070 | Slog.i(TAG, "Going to sleep due to lid switch (uid " + uid +")..."); |
| 1071 | break; |
| 1072 | case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON: |
| 1073 | Slog.i(TAG, "Going to sleep due to power button (uid " + uid +")..."); |
| 1074 | break; |
| 1075 | case PowerManager.GO_TO_SLEEP_REASON_HDMI: |
| 1076 | Slog.i(TAG, "Going to sleep due to HDMI standby (uid " + uid +")..."); |
| 1077 | break; |
| 1078 | default: |
| 1079 | Slog.i(TAG, "Going to sleep by application request (uid " + uid +")..."); |
| 1080 | reason = PowerManager.GO_TO_SLEEP_REASON_APPLICATION; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1081 | break; |
| 1082 | } |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1083 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1084 | mLastSleepTime = eventTime; |
| 1085 | mDirty |= DIRTY_WAKEFULNESS; |
| 1086 | mWakefulness = WAKEFULNESS_DOZING; |
| 1087 | mSandmanSummoned = true; |
| 1088 | setInteractiveStateLocked(false, reason); |
| 1089 | |
| 1090 | // Report the number of wake locks that will be cleared by going to sleep. |
| 1091 | int numWakeLocksCleared = 0; |
| 1092 | final int numWakeLocks = mWakeLocks.size(); |
| 1093 | for (int i = 0; i < numWakeLocks; i++) { |
| 1094 | final WakeLock wakeLock = mWakeLocks.get(i); |
| 1095 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 1096 | case PowerManager.FULL_WAKE_LOCK: |
| 1097 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 1098 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 1099 | numWakeLocksCleared += 1; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numWakeLocksCleared); |
| 1104 | |
| 1105 | // Skip dozing if requested. |
| 1106 | if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) { |
| 1107 | reallyGoToSleepNoUpdateLocked(eventTime, uid); |
| 1108 | } |
| 1109 | } finally { |
| 1110 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1111 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1112 | return true; |
| 1113 | } |
| 1114 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1115 | private void napInternal(long eventTime, int uid) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1116 | synchronized (mLock) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1117 | if (napNoUpdateLocked(eventTime, uid)) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1118 | updatePowerStateLocked(); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1123 | private boolean napNoUpdateLocked(long eventTime, int uid) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1124 | if (DEBUG_SPEW) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1125 | Slog.d(TAG, "napNoUpdateLocked: eventTime=" + eventTime + ", uid=" + uid); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | if (eventTime < mLastWakeTime || mWakefulness != WAKEFULNESS_AWAKE |
| 1129 | || !mBootCompleted || !mSystemReady) { |
| 1130 | return false; |
| 1131 | } |
| 1132 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1133 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "nap"); |
| 1134 | try { |
| 1135 | Slog.i(TAG, "Nap time (uid " + uid +")..."); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1136 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1137 | mDirty |= DIRTY_WAKEFULNESS; |
| 1138 | mWakefulness = WAKEFULNESS_DREAMING; |
| 1139 | mSandmanSummoned = true; |
| 1140 | setInteractiveStateLocked(true, 0); |
| 1141 | } finally { |
| 1142 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 1143 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | // Done dozing, drop everything and go to sleep. |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1148 | private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1149 | if (DEBUG_SPEW) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1150 | Slog.d(TAG, "reallyGoToSleepNoUpdateLocked: eventTime=" + eventTime |
| 1151 | + ", uid=" + uid); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | if (eventTime < mLastWakeTime || mWakefulness == WAKEFULNESS_ASLEEP |
| 1155 | || !mBootCompleted || !mSystemReady) { |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1159 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "reallyGoToSleep"); |
| 1160 | try { |
| 1161 | Slog.i(TAG, "Sleeping (uid " + uid +")..."); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1162 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1163 | mDirty |= DIRTY_WAKEFULNESS; |
| 1164 | mWakefulness = WAKEFULNESS_ASLEEP; |
| 1165 | setInteractiveStateLocked(false, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT); |
| 1166 | } finally { |
| 1167 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 1168 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1169 | return true; |
| 1170 | } |
| 1171 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1172 | private void setInteractiveStateLocked(boolean interactive, int reason) { |
| 1173 | if (mInteractive != interactive) { |
| 1174 | finishInteractiveStateChangeLocked(); |
| 1175 | |
| 1176 | mInteractive = interactive; |
| 1177 | mInteractiveChanging = true; |
| 1178 | mNotifier.onInteractiveStateChangeStarted(interactive, reason); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | private void finishInteractiveStateChangeLocked() { |
| 1183 | if (mInteractiveChanging) { |
| 1184 | mNotifier.onInteractiveStateChangeFinished(mInteractive); |
| 1185 | mInteractiveChanging = false; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1189 | /** |
| 1190 | * Updates the global power state based on dirty bits recorded in mDirty. |
| 1191 | * |
| 1192 | * This is the main function that performs power state transitions. |
| 1193 | * We centralize them here so that we can recompute the power state completely |
| 1194 | * each time something important changes, and ensure that we do it the same |
| 1195 | * way each time. The point is to gather all of the transition logic here. |
| 1196 | */ |
| 1197 | private void updatePowerStateLocked() { |
| 1198 | if (!mSystemReady || mDirty == 0) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1199 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | } |
Jeff Brown | 2d8a390 | 2014-03-11 23:02:35 -0700 | [diff] [blame] | 1201 | if (!Thread.holdsLock(mLock)) { |
| 1202 | Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked"); |
| 1203 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1204 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1205 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState"); |
| 1206 | try { |
| 1207 | // Phase 0: Basic state updates. |
| 1208 | updateIsPoweredLocked(mDirty); |
| 1209 | updateStayOnLocked(mDirty); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1211 | // Phase 1: Update wakefulness. |
| 1212 | // Loop because the wake lock and user activity computations are influenced |
| 1213 | // by changes in wakefulness. |
| 1214 | final long now = SystemClock.uptimeMillis(); |
| 1215 | int dirtyPhase2 = 0; |
| 1216 | for (;;) { |
| 1217 | int dirtyPhase1 = mDirty; |
| 1218 | dirtyPhase2 |= dirtyPhase1; |
| 1219 | mDirty = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1220 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1221 | updateWakeLockSummaryLocked(dirtyPhase1); |
| 1222 | updateUserActivitySummaryLocked(now, dirtyPhase1); |
| 1223 | if (!updateWakefulnessLocked(dirtyPhase1)) { |
| 1224 | break; |
| 1225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1227 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1228 | // Phase 2: Update display power state. |
| 1229 | boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1230 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1231 | // Phase 3: Update dream state (depends on display ready signal). |
| 1232 | updateDreamLocked(dirtyPhase2, displayBecameReady); |
| 1233 | |
| 1234 | // Phase 4: Send notifications, if needed. |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1235 | if (mDisplayReady) { |
| 1236 | finishInteractiveStateChangeLocked(); |
| 1237 | } |
| 1238 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1239 | // Phase 5: Update suspend blocker. |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1240 | // Because we might release the last suspend blocker here, we need to make sure |
| 1241 | // we finished everything else first! |
| 1242 | updateSuspendBlockerLocked(); |
| 1243 | } finally { |
| 1244 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1246 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1247 | |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1248 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1249 | * Updates the value of mIsPowered. |
| 1250 | * Sets DIRTY_IS_POWERED if a change occurred. |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1251 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1252 | private void updateIsPoweredLocked(int dirty) { |
| 1253 | if ((dirty & DIRTY_BATTERY_STATE) != 0) { |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1254 | final boolean wasPowered = mIsPowered; |
| 1255 | final int oldPlugType = mPlugType; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1256 | final boolean oldLevelLow = mBatteryLevelLow; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1257 | mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); |
| 1258 | mPlugType = mBatteryManagerInternal.getPlugType(); |
| 1259 | mBatteryLevel = mBatteryManagerInternal.getBatteryLevel(); |
| 1260 | mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow(); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1261 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1262 | if (DEBUG_SPEW) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1263 | Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1264 | + ", mIsPowered=" + mIsPowered |
| 1265 | + ", oldPlugType=" + oldPlugType |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 1266 | + ", mPlugType=" + mPlugType |
| 1267 | + ", mBatteryLevel=" + mBatteryLevel); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1268 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1269 | |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1270 | if (wasPowered != mIsPowered || oldPlugType != mPlugType) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1271 | mDirty |= DIRTY_IS_POWERED; |
| 1272 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1273 | // Update wireless dock detection state. |
| 1274 | final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update( |
| 1275 | mIsPowered, mPlugType, mBatteryLevel); |
| 1276 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1277 | // Treat plugging and unplugging the devices as a user activity. |
| 1278 | // Users find it disconcerting when they plug or unplug the device |
| 1279 | // and it shuts off right away. |
| 1280 | // Some devices also wake the device when plugged or unplugged because |
| 1281 | // they don't have a charging LED. |
| 1282 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1283 | if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType, |
| 1284 | dockedOnWirelessCharger)) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1285 | wakeUpNoUpdateLocked(now, Process.SYSTEM_UID); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1286 | } |
| 1287 | userActivityNoUpdateLocked( |
| 1288 | now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 1289 | |
| 1290 | // Tell the notifier whether wireless charging has started so that |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1291 | // it can provide feedback to the user. |
| 1292 | if (dockedOnWirelessCharger) { |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 1293 | mNotifier.onWirelessChargingStarted(); |
| 1294 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1295 | } |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1296 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 1297 | if (wasPowered != mIsPowered || oldLevelLow != mBatteryLevelLow) { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 1298 | if (oldLevelLow != mBatteryLevelLow && !mBatteryLevelLow) { |
| 1299 | if (DEBUG_SPEW) { |
| 1300 | Slog.d(TAG, "updateIsPoweredLocked: resetting low power snooze"); |
| 1301 | } |
| 1302 | mAutoLowPowerModeSnoozing = false; |
| 1303 | } |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1304 | updateLowPowerModeLocked(); |
| 1305 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1309 | private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked( |
| 1310 | boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1311 | // Don't wake when powered unless configured to do so. |
| 1312 | if (!mWakeUpWhenPluggedOrUnpluggedConfig) { |
| 1313 | return false; |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1314 | } |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1315 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1316 | // Don't wake when undocked from wireless charger. |
| 1317 | // See WirelessChargerDetector for justification. |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1318 | if (wasPowered && !mIsPowered |
| 1319 | && oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { |
| 1320 | return false; |
| 1321 | } |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1322 | |
| 1323 | // Don't wake when docked on wireless charger unless we are certain of it. |
| 1324 | // See WirelessChargerDetector for justification. |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1325 | if (!wasPowered && mIsPowered |
| 1326 | && mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1327 | && !dockedOnWirelessCharger) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | // If already dreaming and becoming powered, then don't wake. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1332 | if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | // Otherwise wake up! |
| 1337 | return true; |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1340 | /** |
| 1341 | * Updates the value of mStayOn. |
| 1342 | * Sets DIRTY_STAY_ON if a change occurred. |
| 1343 | */ |
| 1344 | private void updateStayOnLocked(int dirty) { |
| 1345 | if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) { |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1346 | final boolean wasStayOn = mStayOn; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1347 | if (mStayOnWhilePluggedInSetting != 0 |
| 1348 | && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) { |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1349 | mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1350 | } else { |
| 1351 | mStayOn = false; |
| 1352 | } |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | if (mStayOn != wasStayOn) { |
| 1355 | mDirty |= DIRTY_STAY_ON; |
| 1356 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Updates the value of mWakeLockSummary to summarize the state of all active wake locks. |
| 1362 | * Note that most wake-locks are ignored when the system is asleep. |
| 1363 | * |
| 1364 | * This function must have no other side-effects. |
| 1365 | */ |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1366 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1367 | private void updateWakeLockSummaryLocked(int dirty) { |
| 1368 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_WAKEFULNESS)) != 0) { |
| 1369 | mWakeLockSummary = 0; |
| 1370 | |
| 1371 | final int numWakeLocks = mWakeLocks.size(); |
| 1372 | for (int i = 0; i < numWakeLocks; i++) { |
| 1373 | final WakeLock wakeLock = mWakeLocks.get(i); |
| 1374 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 1375 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 1376 | mWakeLockSummary |= WAKE_LOCK_CPU; |
| 1377 | break; |
| 1378 | case PowerManager.FULL_WAKE_LOCK: |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1379 | mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_BUTTON_BRIGHT; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1380 | break; |
| 1381 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1382 | mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1383 | break; |
| 1384 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1385 | mWakeLockSummary |= WAKE_LOCK_SCREEN_DIM; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1386 | break; |
| 1387 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1388 | mWakeLockSummary |= WAKE_LOCK_PROXIMITY_SCREEN_OFF; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1389 | break; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1390 | case PowerManager.DOZE_WAKE_LOCK: |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1391 | mWakeLockSummary |= WAKE_LOCK_DOZE; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1392 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1396 | // Cancel wake locks that make no sense based on the current state. |
| 1397 | if (mWakefulness != WAKEFULNESS_DOZING) { |
| 1398 | mWakeLockSummary &= ~WAKE_LOCK_DOZE; |
| 1399 | } |
| 1400 | if (mWakefulness == WAKEFULNESS_ASLEEP |
| 1401 | || (mWakeLockSummary & WAKE_LOCK_DOZE) != 0) { |
| 1402 | mWakeLockSummary &= ~(WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM |
| 1403 | | WAKE_LOCK_BUTTON_BRIGHT); |
| 1404 | if (mWakefulness == WAKEFULNESS_ASLEEP) { |
| 1405 | mWakeLockSummary &= ~WAKE_LOCK_PROXIMITY_SCREEN_OFF; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | // Infer implied wake locks where necessary based on the current state. |
| 1410 | if ((mWakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0) { |
| 1411 | if (mWakefulness == WAKEFULNESS_AWAKE) { |
| 1412 | mWakeLockSummary |= WAKE_LOCK_CPU | WAKE_LOCK_STAY_AWAKE; |
| 1413 | } else if (mWakefulness == WAKEFULNESS_DREAMING) { |
| 1414 | mWakeLockSummary |= WAKE_LOCK_CPU; |
| 1415 | } |
| 1416 | } |
| 1417 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1418 | if (DEBUG_SPEW) { |
| 1419 | Slog.d(TAG, "updateWakeLockSummaryLocked: mWakefulness=" |
| 1420 | + wakefulnessToString(mWakefulness) |
| 1421 | + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)); |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * Updates the value of mUserActivitySummary to summarize the user requested |
| 1428 | * state of the system such as whether the screen should be bright or dim. |
| 1429 | * Note that user activity is ignored when the system is asleep. |
| 1430 | * |
| 1431 | * This function must have no other side-effects. |
| 1432 | */ |
| 1433 | private void updateUserActivitySummaryLocked(long now, int dirty) { |
| 1434 | // Update the status of the user activity timeout timer. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1435 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY |
| 1436 | | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1437 | mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT); |
| 1438 | |
| 1439 | long nextTimeout = 0; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1440 | if (mWakefulness == WAKEFULNESS_AWAKE |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1441 | || mWakefulness == WAKEFULNESS_DREAMING |
| 1442 | || mWakefulness == WAKEFULNESS_DOZING) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1443 | final int screenOffTimeout = getScreenOffTimeoutLocked(); |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 1444 | final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1445 | |
| 1446 | mUserActivitySummary = 0; |
| 1447 | if (mLastUserActivityTime >= mLastWakeTime) { |
| 1448 | nextTimeout = mLastUserActivityTime |
| 1449 | + screenOffTimeout - screenDimDuration; |
| 1450 | if (now < nextTimeout) { |
| 1451 | mUserActivitySummary |= USER_ACTIVITY_SCREEN_BRIGHT; |
| 1452 | } else { |
| 1453 | nextTimeout = mLastUserActivityTime + screenOffTimeout; |
| 1454 | if (now < nextTimeout) { |
| 1455 | mUserActivitySummary |= USER_ACTIVITY_SCREEN_DIM; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | if (mUserActivitySummary == 0 |
| 1460 | && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) { |
| 1461 | nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1462 | if (now < nextTimeout) { |
| 1463 | if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT) { |
| 1464 | mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT; |
| 1465 | } else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) { |
| 1466 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM; |
| 1467 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | if (mUserActivitySummary != 0) { |
| 1471 | Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT); |
| 1472 | msg.setAsynchronous(true); |
| 1473 | mHandler.sendMessageAtTime(msg, nextTimeout); |
| 1474 | } |
| 1475 | } else { |
| 1476 | mUserActivitySummary = 0; |
| 1477 | } |
| 1478 | |
| 1479 | if (DEBUG_SPEW) { |
| 1480 | Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness=" |
| 1481 | + wakefulnessToString(mWakefulness) |
| 1482 | + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary) |
| 1483 | + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout)); |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * Called when a user activity timeout has occurred. |
| 1490 | * Simply indicates that something about user activity has changed so that the new |
| 1491 | * state can be recomputed when the power state is updated. |
| 1492 | * |
| 1493 | * This function must have no other side-effects besides setting the dirty |
| 1494 | * bit and calling update power state. Wakefulness transitions are handled elsewhere. |
| 1495 | */ |
| 1496 | private void handleUserActivityTimeout() { // runs on handler thread |
| 1497 | synchronized (mLock) { |
| 1498 | if (DEBUG_SPEW) { |
| 1499 | Slog.d(TAG, "handleUserActivityTimeout"); |
| 1500 | } |
| 1501 | |
| 1502 | mDirty |= DIRTY_USER_ACTIVITY; |
| 1503 | updatePowerStateLocked(); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | private int getScreenOffTimeoutLocked() { |
| 1508 | int timeout = mScreenOffTimeoutSetting; |
| 1509 | if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) { |
| 1510 | timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin); |
| 1511 | } |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 1512 | if (mUserActivityTimeoutOverrideFromWindowManager >= 0) { |
| 1513 | timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager); |
| 1514 | } |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 1515 | return Math.max(timeout, mMinimumScreenOffTimeoutConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 1518 | private int getScreenDimDurationLocked(int screenOffTimeout) { |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 1519 | return Math.min(mMaximumScreenDimDurationConfig, |
| 1520 | (int)(screenOffTimeout * mMaximumScreenDimRatioConfig)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * Updates the wakefulness of the device. |
| 1525 | * |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1526 | * This is the function that decides whether the device should start dreaming |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1527 | * based on the current wake locks and user activity state. It may modify mDirty |
| 1528 | * if the wakefulness changes. |
| 1529 | * |
| 1530 | * Returns true if the wakefulness changed and we need to restart power state calculation. |
| 1531 | */ |
| 1532 | private boolean updateWakefulnessLocked(int dirty) { |
| 1533 | boolean changed = false; |
| 1534 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 1535 | | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE |
| 1536 | | DIRTY_DOCK_STATE)) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1537 | if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) { |
| 1538 | if (DEBUG_SPEW) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1539 | Slog.d(TAG, "updateWakefulnessLocked: Bed time..."); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1540 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1541 | final long time = SystemClock.uptimeMillis(); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 1542 | if (shouldNapAtBedTimeLocked()) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1543 | changed = napNoUpdateLocked(time, Process.SYSTEM_UID); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1544 | } else { |
| 1545 | changed = goToSleepNoUpdateLocked(time, |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1546 | PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1547 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | return changed; |
| 1551 | } |
| 1552 | |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1553 | /** |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 1554 | * Returns true if the device should automatically nap and start dreaming when the user |
| 1555 | * activity timeout has expired and it's bedtime. |
| 1556 | */ |
| 1557 | private boolean shouldNapAtBedTimeLocked() { |
| 1558 | return mDreamsActivateOnSleepSetting |
| 1559 | || (mDreamsActivateOnDockSetting |
| 1560 | && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 1561 | } |
| 1562 | |
| 1563 | /** |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1564 | * Returns true if the device should go to sleep now. |
| 1565 | * Also used when exiting a dream to determine whether we should go back |
| 1566 | * to being fully awake or else go to sleep for good. |
| 1567 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1568 | private boolean isItBedTimeYetLocked() { |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1569 | return mBootCompleted && !isBeingKeptAwakeLocked(); |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /** |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1573 | * Returns true if the device is being kept awake by a wake lock, user activity |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1574 | * or the stay on while powered setting. We also keep the phone awake when |
| 1575 | * the proximity sensor returns a positive result so that the device does not |
| 1576 | * lock while in a phone call. This function only controls whether the device |
| 1577 | * will go to sleep or dream which is independent of whether it will be allowed |
| 1578 | * to suspend. |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1579 | */ |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1580 | private boolean isBeingKeptAwakeLocked() { |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1581 | return mStayOn |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1582 | || mProximityPositive |
Jeff Brown | 1042874 | 2012-10-09 15:47:30 -0700 | [diff] [blame] | 1583 | || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0 |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1584 | || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT |
| 1585 | | USER_ACTIVITY_SCREEN_DIM)) != 0; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * Determines whether to post a message to the sandman to update the dream state. |
| 1590 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1591 | private void updateDreamLocked(int dirty, boolean displayBecameReady) { |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 1592 | if ((dirty & (DIRTY_WAKEFULNESS |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 1593 | | DIRTY_USER_ACTIVITY |
| 1594 | | DIRTY_WAKE_LOCKS |
| 1595 | | DIRTY_BOOT_COMPLETED |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 1596 | | DIRTY_SETTINGS |
| 1597 | | DIRTY_IS_POWERED |
| 1598 | | DIRTY_STAY_ON |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1599 | | DIRTY_PROXIMITY_POSITIVE |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1600 | | DIRTY_BATTERY_STATE)) != 0 || displayBecameReady) { |
| 1601 | if (mDisplayReady) { |
| 1602 | scheduleSandmanLocked(); |
| 1603 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | private void scheduleSandmanLocked() { |
| 1608 | if (!mSandmanScheduled) { |
| 1609 | mSandmanScheduled = true; |
| 1610 | Message msg = mHandler.obtainMessage(MSG_SANDMAN); |
| 1611 | msg.setAsynchronous(true); |
| 1612 | mHandler.sendMessage(msg); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1617 | * Called when the device enters or exits a dreaming or dozing state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1618 | * |
| 1619 | * We do this asynchronously because we must call out of the power manager to start |
| 1620 | * the dream and we don't want to hold our lock while doing so. There is a risk that |
| 1621 | * the device will wake or go to sleep in the meantime so we have to handle that case. |
| 1622 | */ |
| 1623 | private void handleSandman() { // runs on handler thread |
| 1624 | // Handle preconditions. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1625 | final boolean startDreaming; |
| 1626 | final int wakefulness; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1627 | synchronized (mLock) { |
| 1628 | mSandmanScheduled = false; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1629 | wakefulness = mWakefulness; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1630 | if (mSandmanSummoned && mDisplayReady) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1631 | startDreaming = ((wakefulness == WAKEFULNESS_DREAMING && canDreamLocked()) |
| 1632 | || wakefulness == WAKEFULNESS_DOZING); |
| 1633 | mSandmanSummoned = false; |
| 1634 | } else { |
| 1635 | startDreaming = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1639 | // Start dreaming if needed. |
| 1640 | // We only control the dream on the handler thread, so we don't need to worry about |
| 1641 | // concurrent attempts to start or stop the dream. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1642 | final boolean isDreaming; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1643 | if (mDreamManager != null) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1644 | // Restart the dream whenever the sandman is summoned. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1645 | if (startDreaming) { |
Jeff Brown | f6d4668 | 2014-07-17 22:44:20 -0700 | [diff] [blame] | 1646 | mDreamManager.stopDream(false /*immediate*/); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1647 | mDreamManager.startDream(wakefulness == WAKEFULNESS_DOZING); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1648 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1649 | isDreaming = mDreamManager.isDreaming(); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1650 | } else { |
| 1651 | isDreaming = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | // Update dream state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1655 | synchronized (mLock) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1656 | // Remember the initial battery level when the dream started. |
| 1657 | if (startDreaming && isDreaming) { |
| 1658 | mBatteryLevelWhenDreamStarted = mBatteryLevel; |
| 1659 | if (wakefulness == WAKEFULNESS_DOZING) { |
| 1660 | Slog.i(TAG, "Dozing..."); |
| 1661 | } else { |
| 1662 | Slog.i(TAG, "Dreaming..."); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | // If preconditions changed, wait for the next iteration to determine |
| 1667 | // whether the dream should continue (or be restarted). |
| 1668 | if (mSandmanSummoned || mWakefulness != wakefulness) { |
| 1669 | return; // wait for next cycle |
| 1670 | } |
| 1671 | |
| 1672 | // Determine whether the dream should continue. |
| 1673 | if (wakefulness == WAKEFULNESS_DREAMING) { |
| 1674 | if (isDreaming && canDreamLocked()) { |
| 1675 | if (mDreamsBatteryLevelDrainCutoffConfig >= 0 |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 1676 | && mBatteryLevel < mBatteryLevelWhenDreamStarted |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1677 | - mDreamsBatteryLevelDrainCutoffConfig |
| 1678 | && !isBeingKeptAwakeLocked()) { |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 1679 | // If the user activity timeout expired and the battery appears |
| 1680 | // to be draining faster than it is charging then stop dreaming |
| 1681 | // and go to sleep. |
| 1682 | Slog.i(TAG, "Stopping dream because the battery appears to " |
| 1683 | + "be draining faster than it is charging. " |
| 1684 | + "Battery level when dream started: " |
| 1685 | + mBatteryLevelWhenDreamStarted + "%. " |
| 1686 | + "Battery level now: " + mBatteryLevel + "%."); |
| 1687 | } else { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1688 | return; // continue dreaming |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 1689 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1690 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1691 | |
| 1692 | // Dream has ended or will be stopped. Update the power state. |
| 1693 | if (isItBedTimeYetLocked()) { |
| 1694 | goToSleepNoUpdateLocked(SystemClock.uptimeMillis(), |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1695 | PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1696 | updatePowerStateLocked(); |
| 1697 | } else { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1698 | wakeUpNoUpdateLocked(SystemClock.uptimeMillis(), Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1699 | updatePowerStateLocked(); |
| 1700 | } |
| 1701 | } else if (wakefulness == WAKEFULNESS_DOZING) { |
| 1702 | if (isDreaming) { |
| 1703 | return; // continue dozing |
| 1704 | } |
| 1705 | |
| 1706 | // Doze has ended or will be stopped. Update the power state. |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1707 | reallyGoToSleepNoUpdateLocked(SystemClock.uptimeMillis(), Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1708 | updatePowerStateLocked(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1709 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1712 | // Stop dream. |
| 1713 | if (isDreaming) { |
Jeff Brown | f6d4668 | 2014-07-17 22:44:20 -0700 | [diff] [blame] | 1714 | mDreamManager.stopDream(false /*immediate*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1719 | * Returns true if the device is allowed to dream in its current state. |
| 1720 | * This function is not called when dozing. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1721 | */ |
| 1722 | private boolean canDreamLocked() { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1723 | if (mWakefulness != WAKEFULNESS_DREAMING |
| 1724 | || !mDreamsSupportedConfig |
| 1725 | || !mDreamsEnabledSetting |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1726 | || !mDisplayPowerRequest.isBrightOrDim() |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1727 | || !mBootCompleted) { |
| 1728 | return false; |
| 1729 | } |
| 1730 | if (!isBeingKeptAwakeLocked()) { |
Jeff Brown | 966604f | 2014-02-24 16:19:51 -0800 | [diff] [blame] | 1731 | if (!mIsPowered && !mDreamsEnabledOnBatteryConfig) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1732 | return false; |
| 1733 | } |
| 1734 | if (!mIsPowered |
| 1735 | && mDreamsBatteryLevelMinimumWhenNotPoweredConfig >= 0 |
| 1736 | && mBatteryLevel < mDreamsBatteryLevelMinimumWhenNotPoweredConfig) { |
| 1737 | return false; |
| 1738 | } |
| 1739 | if (mIsPowered |
| 1740 | && mDreamsBatteryLevelMinimumWhenPoweredConfig >= 0 |
| 1741 | && mBatteryLevel < mDreamsBatteryLevelMinimumWhenPoweredConfig) { |
| 1742 | return false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1745 | return true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 1748 | private void handleScreenOnBlockerReleased() { |
| 1749 | synchronized (mLock) { |
| 1750 | mDirty |= DIRTY_SCREEN_ON_BLOCKER_RELEASED; |
| 1751 | updatePowerStateLocked(); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1755 | /** |
| 1756 | * Updates the display power state asynchronously. |
| 1757 | * When the update is finished, mDisplayReady will be set to true. The display |
| 1758 | * controller posts a message to tell us when the actual display power state |
| 1759 | * has been updated so we come back here to double-check and finish up. |
| 1760 | * |
| 1761 | * This function recalculates the display power state each time. |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1762 | * |
| 1763 | * @return True if the display became ready. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1764 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1765 | private boolean updateDisplayPowerStateLocked(int dirty) { |
| 1766 | final boolean oldDisplayReady = mDisplayReady; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1767 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS |
| 1768 | | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 1769 | | DIRTY_SETTINGS | DIRTY_SCREEN_ON_BLOCKER_RELEASED)) != 0) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1770 | mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1771 | |
| 1772 | int screenBrightness = mScreenBrightnessSettingDefault; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1773 | float screenAutoBrightnessAdjustment = 0.0f; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1774 | boolean autoBrightness = (mScreenBrightnessModeSetting == |
| 1775 | Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); |
| 1776 | if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) { |
| 1777 | screenBrightness = mScreenBrightnessOverrideFromWindowManager; |
| 1778 | autoBrightness = false; |
| 1779 | } else if (isValidBrightness(mTemporaryScreenBrightnessSettingOverride)) { |
| 1780 | screenBrightness = mTemporaryScreenBrightnessSettingOverride; |
| 1781 | } else if (isValidBrightness(mScreenBrightnessSetting)) { |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1782 | screenBrightness = mScreenBrightnessSetting; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1783 | } |
| 1784 | if (autoBrightness) { |
| 1785 | screenBrightness = mScreenBrightnessSettingDefault; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1786 | if (isValidAutoBrightnessAdjustment( |
| 1787 | mTemporaryScreenAutoBrightnessAdjustmentSettingOverride)) { |
| 1788 | screenAutoBrightnessAdjustment = |
| 1789 | mTemporaryScreenAutoBrightnessAdjustmentSettingOverride; |
| 1790 | } else if (isValidAutoBrightnessAdjustment( |
| 1791 | mScreenAutoBrightnessAdjustmentSetting)) { |
| 1792 | screenAutoBrightnessAdjustment = mScreenAutoBrightnessAdjustmentSetting; |
| 1793 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1794 | } |
| 1795 | screenBrightness = Math.max(Math.min(screenBrightness, |
| 1796 | mScreenBrightnessSettingMaximum), mScreenBrightnessSettingMinimum); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1797 | screenAutoBrightnessAdjustment = Math.max(Math.min( |
| 1798 | screenAutoBrightnessAdjustment, 1.0f), -1.0f); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1799 | mDisplayPowerRequest.screenBrightness = screenBrightness; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1800 | mDisplayPowerRequest.screenAutoBrightnessAdjustment = |
| 1801 | screenAutoBrightnessAdjustment; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1802 | mDisplayPowerRequest.useAutoBrightness = autoBrightness; |
| 1803 | |
| 1804 | mDisplayPowerRequest.useProximitySensor = shouldUseProximitySensorLocked(); |
| 1805 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 1806 | mDisplayPowerRequest.blockScreenOn = mScreenOnBlocker.isHeld(); |
| 1807 | |
Ruchi Kandoi | f974cc8 | 2014-05-01 11:25:10 -0700 | [diff] [blame] | 1808 | mDisplayPowerRequest.lowPowerMode = mLowPowerModeEnabled; |
| 1809 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1810 | if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE) { |
| 1811 | mDisplayPowerRequest.dozeScreenState = mDozeScreenStateOverrideFromDreamManager; |
| 1812 | mDisplayPowerRequest.dozeScreenBrightness = |
| 1813 | mDozeScreenBrightnessOverrideFromDreamManager; |
| 1814 | } else { |
| 1815 | mDisplayPowerRequest.dozeScreenState = Display.STATE_UNKNOWN; |
| 1816 | mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
| 1817 | } |
| 1818 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1819 | mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1820 | mRequestWaitForNegativeProximity); |
| 1821 | mRequestWaitForNegativeProximity = false; |
| 1822 | |
| 1823 | if (DEBUG_SPEW) { |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 1824 | Slog.d(TAG, "updateScreenStateLocked: mDisplayReady=" + mDisplayReady |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1825 | + ", policy=" + mDisplayPowerRequest.policy |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1826 | + ", mWakefulness=" + mWakefulness |
| 1827 | + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary) |
| 1828 | + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary) |
| 1829 | + ", mBootCompleted=" + mBootCompleted); |
| 1830 | } |
| 1831 | } |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1832 | return mDisplayReady && !oldDisplayReady; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | private static boolean isValidBrightness(int value) { |
| 1836 | return value >= 0 && value <= 255; |
| 1837 | } |
| 1838 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1839 | private static boolean isValidAutoBrightnessAdjustment(float value) { |
Jeff Brown | 5d03a53 | 2012-08-22 13:22:02 -0700 | [diff] [blame] | 1840 | // Handles NaN by always returning false. |
| 1841 | return value >= -1.0f && value <= 1.0f; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1844 | private int getDesiredScreenPolicyLocked() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1845 | if (mWakefulness == WAKEFULNESS_ASLEEP) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1846 | return DisplayPowerRequest.POLICY_OFF; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1849 | if (mWakefulness == WAKEFULNESS_DOZING) { |
| 1850 | if ((mWakeLockSummary & WAKE_LOCK_DOZE) != 0) { |
| 1851 | return DisplayPowerRequest.POLICY_DOZE; |
| 1852 | } |
| 1853 | if (mDozeAfterScreenOffConfig) { |
| 1854 | return DisplayPowerRequest.POLICY_OFF; |
| 1855 | } |
| 1856 | // Fall through and preserve the current screen policy if not configured to |
| 1857 | // doze after screen off. This causes the screen off transition to be skipped. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1858 | } |
| 1859 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1860 | if ((mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0 |
| 1861 | || (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0 |
| 1862 | || !mBootCompleted) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1863 | return DisplayPowerRequest.POLICY_BRIGHT; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1866 | return DisplayPowerRequest.POLICY_DIM; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1869 | private final DisplayManagerInternal.DisplayPowerCallbacks mDisplayPowerCallbacks = |
| 1870 | new DisplayManagerInternal.DisplayPowerCallbacks() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1871 | private int mDisplayState = Display.STATE_UNKNOWN; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1872 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1873 | @Override |
| 1874 | public void onStateChanged() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 1875 | synchronized (mLock) { |
| 1876 | mDirty |= DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED; |
| 1877 | updatePowerStateLocked(); |
| 1878 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | @Override |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1882 | public void onProximityPositive() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 1883 | synchronized (mLock) { |
| 1884 | mProximityPositive = true; |
| 1885 | mDirty |= DIRTY_PROXIMITY_POSITIVE; |
| 1886 | updatePowerStateLocked(); |
| 1887 | } |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1891 | public void onProximityNegative() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 1892 | synchronized (mLock) { |
| 1893 | mProximityPositive = false; |
| 1894 | mDirty |= DIRTY_PROXIMITY_POSITIVE; |
| 1895 | userActivityNoUpdateLocked(SystemClock.uptimeMillis(), |
| 1896 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
| 1897 | updatePowerStateLocked(); |
| 1898 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1899 | } |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1900 | |
| 1901 | @Override |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1902 | public void onDisplayStateChange(int state) { |
| 1903 | // This method is only needed to support legacy display blanking behavior |
| 1904 | // where the display's power state is coupled to suspend or to the power HAL. |
| 1905 | // The order of operations matters here. |
| 1906 | synchronized (mLock) { |
| 1907 | if (mDisplayState != state) { |
| 1908 | mDisplayState = state; |
| 1909 | if (state == Display.STATE_OFF) { |
| 1910 | if (!mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 1911 | setHalInteractiveModeLocked(false); |
| 1912 | } |
| 1913 | if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 1914 | setHalAutoSuspendModeLocked(true); |
| 1915 | } |
| 1916 | } else { |
| 1917 | if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 1918 | setHalAutoSuspendModeLocked(false); |
| 1919 | } |
| 1920 | if (!mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 1921 | setHalInteractiveModeLocked(true); |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | @Override |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1929 | public void acquireSuspendBlocker() { |
| 1930 | mDisplaySuspendBlocker.acquire(); |
| 1931 | } |
| 1932 | |
| 1933 | @Override |
| 1934 | public void releaseSuspendBlocker() { |
| 1935 | mDisplaySuspendBlocker.release(); |
| 1936 | } |
| 1937 | |
| 1938 | @Override |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1939 | public String toString() { |
| 1940 | synchronized (this) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1941 | return "state=" + Display.stateToString(mDisplayState); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1942 | } |
| 1943 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1944 | }; |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1945 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1946 | private boolean shouldUseProximitySensorLocked() { |
| 1947 | return (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0; |
| 1948 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1949 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1950 | /** |
| 1951 | * Updates the suspend blocker that keeps the CPU alive. |
| 1952 | * |
| 1953 | * This function must have no other side-effects. |
| 1954 | */ |
| 1955 | private void updateSuspendBlockerLocked() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1956 | final boolean needWakeLockSuspendBlocker = ((mWakeLockSummary & WAKE_LOCK_CPU) != 0); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1957 | final boolean needDisplaySuspendBlocker = needDisplaySuspendBlockerLocked(); |
| 1958 | final boolean autoSuspend = !needDisplaySuspendBlocker; |
| 1959 | |
| 1960 | // Disable auto-suspend if needed. |
| 1961 | if (!autoSuspend) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1962 | if (mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 1963 | setHalAutoSuspendModeLocked(false); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1964 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1965 | if (mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 1966 | setHalInteractiveModeLocked(true); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1967 | } |
| 1968 | } |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1969 | |
| 1970 | // First acquire suspend blockers if needed. |
| 1971 | if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) { |
| 1972 | mWakeLockSuspendBlocker.acquire(); |
| 1973 | mHoldingWakeLockSuspendBlocker = true; |
| 1974 | } |
| 1975 | if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) { |
| 1976 | mDisplaySuspendBlocker.acquire(); |
| 1977 | mHoldingDisplaySuspendBlocker = true; |
| 1978 | } |
| 1979 | |
| 1980 | // Then release suspend blockers if needed. |
| 1981 | if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) { |
| 1982 | mWakeLockSuspendBlocker.release(); |
| 1983 | mHoldingWakeLockSuspendBlocker = false; |
| 1984 | } |
| 1985 | if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) { |
| 1986 | mDisplaySuspendBlocker.release(); |
| 1987 | mHoldingDisplaySuspendBlocker = false; |
| 1988 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1989 | |
| 1990 | // Enable auto-suspend if needed. |
| 1991 | if (autoSuspend) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1992 | if (mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 1993 | setHalInteractiveModeLocked(false); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1994 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1995 | if (mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 1996 | setHalAutoSuspendModeLocked(true); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1997 | } |
| 1998 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2001 | /** |
| 2002 | * Return true if we must keep a suspend blocker active on behalf of the display. |
| 2003 | * We do so if the screen is on or is in transition between states. |
| 2004 | */ |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2005 | private boolean needDisplaySuspendBlockerLocked() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2006 | if (!mDisplayReady) { |
| 2007 | return true; |
| 2008 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2009 | if (mDisplayPowerRequest.isBrightOrDim()) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2010 | // If we asked for the screen to be on but it is off due to the proximity |
| 2011 | // sensor then we may suspend but only if the configuration allows it. |
| 2012 | // On some hardware it may not be safe to suspend because the proximity |
| 2013 | // sensor may not be correctly configured as a wake-up source. |
| 2014 | if (!mDisplayPowerRequest.useProximitySensor || !mProximityPositive |
| 2015 | || !mSuspendWhenScreenOffDueToProximityConfig) { |
| 2016 | return true; |
| 2017 | } |
| 2018 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2019 | // Let the system suspend if the screen is off or dozing. |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2020 | return false; |
| 2021 | } |
| 2022 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2023 | private void setHalAutoSuspendModeLocked(boolean enable) { |
| 2024 | if (enable != mHalAutoSuspendModeEnabled) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2025 | if (DEBUG) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2026 | Slog.d(TAG, "Setting HAL auto-suspend mode to " + enable); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2027 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2028 | mHalAutoSuspendModeEnabled = enable; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2029 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalAutoSuspend(" + enable + ")"); |
| 2030 | try { |
| 2031 | nativeSetAutoSuspend(enable); |
| 2032 | } finally { |
| 2033 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 2034 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2038 | private void setHalInteractiveModeLocked(boolean enable) { |
| 2039 | if (enable != mHalInteractiveModeEnabled) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2040 | if (DEBUG) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2041 | Slog.d(TAG, "Setting HAL interactive mode to " + enable); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2042 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2043 | mHalInteractiveModeEnabled = enable; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2044 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalInteractive(" + enable + ")"); |
| 2045 | try { |
| 2046 | nativeSetInteractive(enable); |
| 2047 | } finally { |
| 2048 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 2049 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2053 | private boolean isInteractiveInternal() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2054 | synchronized (mLock) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2055 | return mInteractive; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 2059 | private boolean isLowPowerModeInternal() { |
| 2060 | synchronized (mLock) { |
| 2061 | return mLowPowerModeEnabled; |
| 2062 | } |
| 2063 | } |
| 2064 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2065 | private boolean setLowPowerModeInternal(boolean mode) { |
| 2066 | synchronized (mLock) { |
| 2067 | if (DEBUG) Slog.d(TAG, "setLowPowerModeInternal " + mode + " mIsPowered=" + mIsPowered); |
| 2068 | if (mIsPowered) { |
| 2069 | return false; |
| 2070 | } |
| 2071 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2072 | Settings.Global.LOW_POWER_MODE, mode ? 1 : 0); |
| 2073 | mLowPowerModeSetting = mode; |
| 2074 | |
| 2075 | if (mAutoLowPowerModeConfigured && mBatteryLevelLow) { |
| 2076 | if (mode && mAutoLowPowerModeSnoozing) { |
| 2077 | if (DEBUG_SPEW) { |
| 2078 | Slog.d(TAG, "setLowPowerModeInternal: clearing low power mode snooze"); |
| 2079 | } |
| 2080 | mAutoLowPowerModeSnoozing = false; |
| 2081 | } else if (!mode && !mAutoLowPowerModeSnoozing) { |
| 2082 | if (DEBUG_SPEW) { |
| 2083 | Slog.d(TAG, "setLowPowerModeInternal: snoozing low power mode"); |
| 2084 | } |
| 2085 | mAutoLowPowerModeSnoozing = true; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | updateLowPowerModeLocked(); |
| 2090 | return true; |
| 2091 | } |
| 2092 | } |
| 2093 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2094 | private void handleBatteryStateChangedLocked() { |
| 2095 | mDirty |= DIRTY_BATTERY_STATE; |
| 2096 | updatePowerStateLocked(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2097 | } |
| 2098 | |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 2099 | private void shutdownOrRebootInternal(final boolean shutdown, final boolean confirm, |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 2100 | final String reason, boolean wait) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2101 | if (mHandler == null || !mSystemReady) { |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 2102 | throw new IllegalStateException("Too early to call shutdown() or reboot()"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2103 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2104 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2105 | Runnable runnable = new Runnable() { |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 2106 | @Override |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2107 | public void run() { |
| 2108 | synchronized (this) { |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 2109 | if (shutdown) { |
| 2110 | ShutdownThread.shutdown(mContext, confirm); |
| 2111 | } else { |
| 2112 | ShutdownThread.reboot(mContext, reason, confirm); |
| 2113 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2114 | } |
San Mehat | 1e51279 | 2010-01-07 10:40:29 -0800 | [diff] [blame] | 2115 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2116 | }; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2117 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2118 | // ShutdownThread must run on a looper capable of displaying the UI. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2119 | Message msg = Message.obtain(mHandler, runnable); |
| 2120 | msg.setAsynchronous(true); |
| 2121 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2122 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2123 | // PowerManager.reboot() is documented not to return so just wait for the inevitable. |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 2124 | if (wait) { |
| 2125 | synchronized (runnable) { |
| 2126 | while (true) { |
| 2127 | try { |
| 2128 | runnable.wait(); |
| 2129 | } catch (InterruptedException e) { |
| 2130 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2131 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2132 | } |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2136 | private void crashInternal(final String message) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2137 | Thread t = new Thread("PowerManagerService.crash()") { |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 2138 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2139 | public void run() { |
| 2140 | throw new RuntimeException(message); |
| 2141 | } |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2142 | }; |
| 2143 | try { |
| 2144 | t.start(); |
| 2145 | t.join(); |
| 2146 | } catch (InterruptedException e) { |
| 2147 | Log.wtf(TAG, e); |
| 2148 | } |
| 2149 | } |
| 2150 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2151 | private void setStayOnSettingInternal(int val) { |
Christopher Tate | ad73532 | 2012-09-07 14:19:43 -0700 | [diff] [blame] | 2152 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2153 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2156 | private void setMaximumScreenOffTimeoutFromDeviceAdminInternal(int timeMs) { |
| 2157 | synchronized (mLock) { |
| 2158 | mMaximumScreenOffTimeoutFromDeviceAdmin = timeMs; |
| 2159 | mDirty |= DIRTY_SETTINGS; |
| 2160 | updatePowerStateLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2164 | private boolean isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() { |
| 2165 | return mMaximumScreenOffTimeoutFromDeviceAdmin >= 0 |
| 2166 | && mMaximumScreenOffTimeoutFromDeviceAdmin < Integer.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2167 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2168 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2169 | private void setAttentionLightInternal(boolean on, int color) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2170 | Light light; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2171 | synchronized (mLock) { |
| 2172 | if (!mSystemReady) { |
| 2173 | return; |
| 2174 | } |
| 2175 | light = mAttentionLight; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2176 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2177 | |
| 2178 | // Control light outside of lock. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2179 | light.setFlashing(color, Light.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2182 | private void setScreenBrightnessOverrideFromWindowManagerInternal(int brightness) { |
| 2183 | synchronized (mLock) { |
| 2184 | if (mScreenBrightnessOverrideFromWindowManager != brightness) { |
| 2185 | mScreenBrightnessOverrideFromWindowManager = brightness; |
| 2186 | mDirty |= DIRTY_SETTINGS; |
| 2187 | updatePowerStateLocked(); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2188 | } |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2189 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 2192 | private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) { |
| 2193 | synchronized (mLock) { |
| 2194 | if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) { |
| 2195 | mUserActivityTimeoutOverrideFromWindowManager = timeoutMillis; |
| 2196 | mDirty |= DIRTY_SETTINGS; |
| 2197 | updatePowerStateLocked(); |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2202 | private void setTemporaryScreenBrightnessSettingOverrideInternal(int brightness) { |
| 2203 | synchronized (mLock) { |
| 2204 | if (mTemporaryScreenBrightnessSettingOverride != brightness) { |
| 2205 | mTemporaryScreenBrightnessSettingOverride = brightness; |
| 2206 | mDirty |= DIRTY_SETTINGS; |
| 2207 | updatePowerStateLocked(); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2208 | } |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2209 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 2212 | private void setTemporaryScreenAutoBrightnessAdjustmentSettingOverrideInternal(float adj) { |
| 2213 | synchronized (mLock) { |
| 2214 | // Note: This condition handles NaN because NaN is not equal to any other |
| 2215 | // value, including itself. |
| 2216 | if (mTemporaryScreenAutoBrightnessAdjustmentSettingOverride != adj) { |
| 2217 | mTemporaryScreenAutoBrightnessAdjustmentSettingOverride = adj; |
| 2218 | mDirty |= DIRTY_SETTINGS; |
| 2219 | updatePowerStateLocked(); |
| 2220 | } |
| 2221 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2224 | private void setDozeOverrideFromDreamManagerInternal( |
| 2225 | int screenState, int screenBrightness) { |
| 2226 | synchronized (mLock) { |
| 2227 | if (mDozeScreenStateOverrideFromDreamManager != screenState |
| 2228 | || mDozeScreenBrightnessOverrideFromDreamManager != screenBrightness) { |
| 2229 | mDozeScreenStateOverrideFromDreamManager = screenState; |
| 2230 | mDozeScreenBrightnessOverrideFromDreamManager = screenBrightness; |
| 2231 | mDirty |= DIRTY_SETTINGS; |
| 2232 | updatePowerStateLocked(); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 2237 | private void powerHintInternal(int hintId, int data) { |
| 2238 | nativeSendPowerHint(hintId, data); |
| 2239 | } |
| 2240 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2241 | /** |
| 2242 | * Low-level function turn the device off immediately, without trying |
| 2243 | * to be clean. Most people should use {@link ShutdownThread} for a clean shutdown. |
| 2244 | */ |
| 2245 | public static void lowLevelShutdown() { |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 2246 | SystemProperties.set("sys.powerctl", "shutdown"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /** |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 2250 | * Low-level function to reboot the device. On success, this |
| 2251 | * function doesn't return. If more than 20 seconds passes from |
| 2252 | * the time a reboot is requested (120 seconds for reboot to |
| 2253 | * recovery), this method returns. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2254 | * |
| 2255 | * @param reason code to pass to the kernel (e.g. "recovery"), or null. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2256 | */ |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 2257 | public static void lowLevelReboot(String reason) { |
| 2258 | if (reason == null) { |
| 2259 | reason = ""; |
| 2260 | } |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 2261 | long duration; |
| 2262 | if (reason.equals(PowerManager.REBOOT_RECOVERY)) { |
| 2263 | // If we are rebooting to go into recovery, instead of |
| 2264 | // setting sys.powerctl directly we'll start the |
| 2265 | // pre-recovery service which will do some preparation for |
| 2266 | // recovery and then reboot for us. |
| 2267 | // |
| 2268 | // This preparation can take more than 20 seconds if |
| 2269 | // there's a very large update package, so lengthen the |
| 2270 | // timeout. |
| 2271 | SystemProperties.set("ctl.start", "pre-recovery"); |
| 2272 | duration = 120 * 1000L; |
| 2273 | } else { |
| 2274 | SystemProperties.set("sys.powerctl", "reboot," + reason); |
| 2275 | duration = 20 * 1000L; |
| 2276 | } |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 2277 | try { |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 2278 | Thread.sleep(duration); |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 2279 | } catch (InterruptedException e) { |
| 2280 | Thread.currentThread().interrupt(); |
| 2281 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | @Override // Watchdog.Monitor implementation |
| 2285 | public void monitor() { |
| 2286 | // Grab and release lock for watchdog monitor to detect deadlocks. |
| 2287 | synchronized (mLock) { |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2288 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2291 | private void dumpInternal(PrintWriter pw) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2292 | pw.println("POWER MANAGER (dumpsys power)\n"); |
| 2293 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 2294 | final WirelessChargerDetector wcd; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2295 | synchronized (mLock) { |
| 2296 | pw.println("Power Manager State:"); |
| 2297 | pw.println(" mDirty=0x" + Integer.toHexString(mDirty)); |
| 2298 | pw.println(" mWakefulness=" + wakefulnessToString(mWakefulness)); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2299 | pw.println(" mInteractive=" + mInteractive); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2300 | pw.println(" mIsPowered=" + mIsPowered); |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 2301 | pw.println(" mPlugType=" + mPlugType); |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 2302 | pw.println(" mBatteryLevel=" + mBatteryLevel); |
| 2303 | pw.println(" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 2304 | pw.println(" mDockState=" + mDockState); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2305 | pw.println(" mStayOn=" + mStayOn); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2306 | pw.println(" mProximityPositive=" + mProximityPositive); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2307 | pw.println(" mBootCompleted=" + mBootCompleted); |
| 2308 | pw.println(" mSystemReady=" + mSystemReady); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2309 | pw.println(" mHalAutoSuspendModeEnabled=" + mHalAutoSuspendModeEnabled); |
| 2310 | pw.println(" mHalInteractiveModeEnabled=" + mHalInteractiveModeEnabled); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2311 | pw.println(" mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)); |
| 2312 | pw.println(" mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)); |
| 2313 | pw.println(" mRequestWaitForNegativeProximity=" + mRequestWaitForNegativeProximity); |
| 2314 | pw.println(" mSandmanScheduled=" + mSandmanScheduled); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2315 | pw.println(" mSandmanSummoned=" + mSandmanSummoned); |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 2316 | pw.println(" mLowPowerModeEnabled=" + mLowPowerModeEnabled); |
| 2317 | pw.println(" mBatteryLevelLow=" + mBatteryLevelLow); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2318 | pw.println(" mLastWakeTime=" + TimeUtils.formatUptime(mLastWakeTime)); |
| 2319 | pw.println(" mLastSleepTime=" + TimeUtils.formatUptime(mLastSleepTime)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2320 | pw.println(" mLastUserActivityTime=" + TimeUtils.formatUptime(mLastUserActivityTime)); |
| 2321 | pw.println(" mLastUserActivityTimeNoChangeLights=" |
| 2322 | + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights)); |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 2323 | pw.println(" mLastInteractivePowerHintTime=" |
| 2324 | + TimeUtils.formatUptime(mLastInteractivePowerHintTime)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2325 | pw.println(" mDisplayReady=" + mDisplayReady); |
| 2326 | pw.println(" mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 2327 | pw.println(" mHoldingDisplaySuspendBlocker=" + mHoldingDisplaySuspendBlocker); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2328 | |
| 2329 | pw.println(); |
| 2330 | pw.println("Settings and Configuration:"); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2331 | pw.println(" mDecoupleHalAutoSuspendModeFromDisplayConfig=" |
| 2332 | + mDecoupleHalAutoSuspendModeFromDisplayConfig); |
| 2333 | pw.println(" mDecoupleHalInteractiveModeFromDisplayConfig=" |
| 2334 | + mDecoupleHalInteractiveModeFromDisplayConfig); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2335 | pw.println(" mWakeUpWhenPluggedOrUnpluggedConfig=" |
| 2336 | + mWakeUpWhenPluggedOrUnpluggedConfig); |
| 2337 | pw.println(" mSuspendWhenScreenOffDueToProximityConfig=" |
| 2338 | + mSuspendWhenScreenOffDueToProximityConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2339 | pw.println(" mDreamsSupportedConfig=" + mDreamsSupportedConfig); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2340 | pw.println(" mDreamsEnabledByDefaultConfig=" + mDreamsEnabledByDefaultConfig); |
| 2341 | pw.println(" mDreamsActivatedOnSleepByDefaultConfig=" |
| 2342 | + mDreamsActivatedOnSleepByDefaultConfig); |
| 2343 | pw.println(" mDreamsActivatedOnDockByDefaultConfig=" |
| 2344 | + mDreamsActivatedOnDockByDefaultConfig); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2345 | pw.println(" mDreamsEnabledOnBatteryConfig=" |
| 2346 | + mDreamsEnabledOnBatteryConfig); |
| 2347 | pw.println(" mDreamsBatteryLevelMinimumWhenPoweredConfig=" |
| 2348 | + mDreamsBatteryLevelMinimumWhenPoweredConfig); |
| 2349 | pw.println(" mDreamsBatteryLevelMinimumWhenNotPoweredConfig=" |
| 2350 | + mDreamsBatteryLevelMinimumWhenNotPoweredConfig); |
| 2351 | pw.println(" mDreamsBatteryLevelDrainCutoffConfig=" |
| 2352 | + mDreamsBatteryLevelDrainCutoffConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2353 | pw.println(" mDreamsEnabledSetting=" + mDreamsEnabledSetting); |
John Spurlock | 1a868b7 | 2012-08-22 09:56:51 -0400 | [diff] [blame] | 2354 | pw.println(" mDreamsActivateOnSleepSetting=" + mDreamsActivateOnSleepSetting); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 2355 | pw.println(" mDreamsActivateOnDockSetting=" + mDreamsActivateOnDockSetting); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2356 | pw.println(" mDozeAfterScreenOffConfig=" + mDozeAfterScreenOffConfig); |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 2357 | pw.println(" mLowPowerModeSetting=" + mLowPowerModeSetting); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2358 | pw.println(" mAutoLowPowerModeConfigured=" + mAutoLowPowerModeConfigured); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 2359 | pw.println(" mAutoLowPowerModeSnoozing=" + mAutoLowPowerModeSnoozing); |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 2360 | pw.println(" mMinimumScreenOffTimeoutConfig=" + mMinimumScreenOffTimeoutConfig); |
| 2361 | pw.println(" mMaximumScreenDimDurationConfig=" + mMaximumScreenDimDurationConfig); |
| 2362 | pw.println(" mMaximumScreenDimRatioConfig=" + mMaximumScreenDimRatioConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2363 | pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting); |
| 2364 | pw.println(" mMaximumScreenOffTimeoutFromDeviceAdmin=" |
| 2365 | + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced=" |
| 2366 | + isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() + ")"); |
| 2367 | pw.println(" mStayOnWhilePluggedInSetting=" + mStayOnWhilePluggedInSetting); |
| 2368 | pw.println(" mScreenBrightnessSetting=" + mScreenBrightnessSetting); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 2369 | pw.println(" mScreenAutoBrightnessAdjustmentSetting=" |
| 2370 | + mScreenAutoBrightnessAdjustmentSetting); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2371 | pw.println(" mScreenBrightnessModeSetting=" + mScreenBrightnessModeSetting); |
| 2372 | pw.println(" mScreenBrightnessOverrideFromWindowManager=" |
| 2373 | + mScreenBrightnessOverrideFromWindowManager); |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 2374 | pw.println(" mUserActivityTimeoutOverrideFromWindowManager=" |
| 2375 | + mUserActivityTimeoutOverrideFromWindowManager); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2376 | pw.println(" mTemporaryScreenBrightnessSettingOverride=" |
| 2377 | + mTemporaryScreenBrightnessSettingOverride); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 2378 | pw.println(" mTemporaryScreenAutoBrightnessAdjustmentSettingOverride=" |
| 2379 | + mTemporaryScreenAutoBrightnessAdjustmentSettingOverride); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2380 | pw.println(" mDozeScreenStateOverrideFromDreamManager=" |
| 2381 | + mDozeScreenStateOverrideFromDreamManager); |
| 2382 | pw.println(" mDozeScreenBrightnessOverrideFromDreamManager=" |
| 2383 | + mDozeScreenBrightnessOverrideFromDreamManager); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2384 | pw.println(" mScreenBrightnessSettingMinimum=" + mScreenBrightnessSettingMinimum); |
| 2385 | pw.println(" mScreenBrightnessSettingMaximum=" + mScreenBrightnessSettingMaximum); |
| 2386 | pw.println(" mScreenBrightnessSettingDefault=" + mScreenBrightnessSettingDefault); |
| 2387 | |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 2388 | final int screenOffTimeout = getScreenOffTimeoutLocked(); |
| 2389 | final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout); |
| 2390 | pw.println(); |
| 2391 | pw.println("Screen off timeout: " + screenOffTimeout + " ms"); |
| 2392 | pw.println("Screen dim duration: " + screenDimDuration + " ms"); |
| 2393 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2394 | pw.println(); |
| 2395 | pw.println("Wake Locks: size=" + mWakeLocks.size()); |
| 2396 | for (WakeLock wl : mWakeLocks) { |
| 2397 | pw.println(" " + wl); |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2398 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2399 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2400 | pw.println(); |
| 2401 | pw.println("Suspend Blockers: size=" + mSuspendBlockers.size()); |
| 2402 | for (SuspendBlocker sb : mSuspendBlockers) { |
| 2403 | pw.println(" " + sb); |
| 2404 | } |
| 2405 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2406 | pw.println(); |
| 2407 | pw.println("Screen On Blocker: " + mScreenOnBlocker); |
| 2408 | |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 2409 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2410 | pw.println("Display Power: " + mDisplayPowerCallbacks); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 2411 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 2412 | wcd = mWirelessChargerDetector; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 2415 | if (wcd != null) { |
| 2416 | wcd.dump(pw); |
| 2417 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | private SuspendBlocker createSuspendBlockerLocked(String name) { |
| 2421 | SuspendBlocker suspendBlocker = new SuspendBlockerImpl(name); |
| 2422 | mSuspendBlockers.add(suspendBlocker); |
| 2423 | return suspendBlocker; |
| 2424 | } |
| 2425 | |
| 2426 | private static String wakefulnessToString(int wakefulness) { |
| 2427 | switch (wakefulness) { |
| 2428 | case WAKEFULNESS_ASLEEP: |
| 2429 | return "Asleep"; |
| 2430 | case WAKEFULNESS_AWAKE: |
| 2431 | return "Awake"; |
| 2432 | case WAKEFULNESS_DREAMING: |
| 2433 | return "Dreaming"; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2434 | case WAKEFULNESS_DOZING: |
| 2435 | return "Dozing"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2436 | default: |
| 2437 | return Integer.toString(wakefulness); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | private static WorkSource copyWorkSource(WorkSource workSource) { |
| 2442 | return workSource != null ? new WorkSource(workSource) : null; |
| 2443 | } |
| 2444 | |
| 2445 | private final class BatteryReceiver extends BroadcastReceiver { |
| 2446 | @Override |
| 2447 | public void onReceive(Context context, Intent intent) { |
| 2448 | synchronized (mLock) { |
| 2449 | handleBatteryStateChangedLocked(); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2450 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 2454 | private final class DreamReceiver extends BroadcastReceiver { |
| 2455 | @Override |
| 2456 | public void onReceive(Context context, Intent intent) { |
| 2457 | synchronized (mLock) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2458 | scheduleSandmanLocked(); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 2463 | private final class UserSwitchedReceiver extends BroadcastReceiver { |
| 2464 | @Override |
| 2465 | public void onReceive(Context context, Intent intent) { |
| 2466 | synchronized (mLock) { |
| 2467 | handleSettingsChangedLocked(); |
| 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 2472 | private final class DockReceiver extends BroadcastReceiver { |
| 2473 | @Override |
| 2474 | public void onReceive(Context context, Intent intent) { |
| 2475 | synchronized (mLock) { |
| 2476 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 2477 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 2478 | if (mDockState != dockState) { |
| 2479 | mDockState = dockState; |
| 2480 | mDirty |= DIRTY_DOCK_STATE; |
| 2481 | updatePowerStateLocked(); |
| 2482 | } |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2487 | private final class SettingsObserver extends ContentObserver { |
| 2488 | public SettingsObserver(Handler handler) { |
| 2489 | super(handler); |
| 2490 | } |
| 2491 | |
| 2492 | @Override |
| 2493 | public void onChange(boolean selfChange, Uri uri) { |
| 2494 | synchronized (mLock) { |
| 2495 | handleSettingsChangedLocked(); |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2500 | /** |
| 2501 | * Handler for asynchronous operations performed by the power manager. |
| 2502 | */ |
| 2503 | private final class PowerManagerHandler extends Handler { |
| 2504 | public PowerManagerHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 2505 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | @Override |
| 2509 | public void handleMessage(Message msg) { |
| 2510 | switch (msg.what) { |
| 2511 | case MSG_USER_ACTIVITY_TIMEOUT: |
| 2512 | handleUserActivityTimeout(); |
| 2513 | break; |
| 2514 | case MSG_SANDMAN: |
| 2515 | handleSandman(); |
| 2516 | break; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2517 | case MSG_SCREEN_ON_BLOCKER_RELEASED: |
| 2518 | handleScreenOnBlockerReleased(); |
| 2519 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * Represents a wake lock that has been acquired by an application. |
| 2526 | */ |
| 2527 | private final class WakeLock implements IBinder.DeathRecipient { |
| 2528 | public final IBinder mLock; |
| 2529 | public int mFlags; |
| 2530 | public String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2531 | public final String mPackageName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2532 | public WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2533 | public String mHistoryTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2534 | public final int mOwnerUid; |
| 2535 | public final int mOwnerPid; |
| 2536 | public boolean mNotifiedAcquired; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2537 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2538 | public WakeLock(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2539 | WorkSource workSource, String historyTag, int ownerUid, int ownerPid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2540 | mLock = lock; |
| 2541 | mFlags = flags; |
| 2542 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2543 | mPackageName = packageName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2544 | mWorkSource = copyWorkSource(workSource); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2545 | mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2546 | mOwnerUid = ownerUid; |
| 2547 | mOwnerPid = ownerPid; |
| 2548 | } |
| 2549 | |
| 2550 | @Override |
| 2551 | public void binderDied() { |
| 2552 | PowerManagerService.this.handleWakeLockDeath(this); |
| 2553 | } |
| 2554 | |
| 2555 | public boolean hasSameProperties(int flags, String tag, WorkSource workSource, |
| 2556 | int ownerUid, int ownerPid) { |
| 2557 | return mFlags == flags |
| 2558 | && mTag.equals(tag) |
| 2559 | && hasSameWorkSource(workSource) |
| 2560 | && mOwnerUid == ownerUid |
| 2561 | && mOwnerPid == ownerPid; |
| 2562 | } |
| 2563 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2564 | public void updateProperties(int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2565 | WorkSource workSource, String historyTag, int ownerUid, int ownerPid) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2566 | if (!mPackageName.equals(packageName)) { |
| 2567 | throw new IllegalStateException("Existing wake lock package name changed: " |
| 2568 | + mPackageName + " to " + packageName); |
| 2569 | } |
| 2570 | if (mOwnerUid != ownerUid) { |
| 2571 | throw new IllegalStateException("Existing wake lock uid changed: " |
| 2572 | + mOwnerUid + " to " + ownerUid); |
| 2573 | } |
| 2574 | if (mOwnerPid != ownerPid) { |
| 2575 | throw new IllegalStateException("Existing wake lock pid changed: " |
| 2576 | + mOwnerPid + " to " + ownerPid); |
| 2577 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2578 | mFlags = flags; |
| 2579 | mTag = tag; |
| 2580 | updateWorkSource(workSource); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2581 | mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | public boolean hasSameWorkSource(WorkSource workSource) { |
| 2585 | return Objects.equal(mWorkSource, workSource); |
| 2586 | } |
| 2587 | |
| 2588 | public void updateWorkSource(WorkSource workSource) { |
| 2589 | mWorkSource = copyWorkSource(workSource); |
| 2590 | } |
| 2591 | |
| 2592 | @Override |
| 2593 | public String toString() { |
| 2594 | return getLockLevelString() |
| 2595 | + " '" + mTag + "'" + getLockFlagsString() |
| 2596 | + " (uid=" + mOwnerUid + ", pid=" + mOwnerPid + ", ws=" + mWorkSource + ")"; |
| 2597 | } |
| 2598 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2599 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2600 | private String getLockLevelString() { |
| 2601 | switch (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 2602 | case PowerManager.FULL_WAKE_LOCK: |
| 2603 | return "FULL_WAKE_LOCK "; |
| 2604 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 2605 | return "SCREEN_BRIGHT_WAKE_LOCK "; |
| 2606 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 2607 | return "SCREEN_DIM_WAKE_LOCK "; |
| 2608 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 2609 | return "PARTIAL_WAKE_LOCK "; |
| 2610 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 2611 | return "PROXIMITY_SCREEN_OFF_WAKE_LOCK"; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2612 | case PowerManager.DOZE_WAKE_LOCK: |
| 2613 | return "DOZE_WAKE_LOCK "; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2614 | default: |
| 2615 | return "??? "; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | private String getLockFlagsString() { |
| 2620 | String result = ""; |
| 2621 | if ((mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 2622 | result += " ACQUIRE_CAUSES_WAKEUP"; |
| 2623 | } |
| 2624 | if ((mFlags & PowerManager.ON_AFTER_RELEASE) != 0) { |
| 2625 | result += " ON_AFTER_RELEASE"; |
| 2626 | } |
| 2627 | return result; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | private final class SuspendBlockerImpl implements SuspendBlocker { |
| 2632 | private final String mName; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2633 | private final String mTraceName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2634 | private int mReferenceCount; |
| 2635 | |
| 2636 | public SuspendBlockerImpl(String name) { |
| 2637 | mName = name; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2638 | mTraceName = "SuspendBlocker (" + name + ")"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | @Override |
| 2642 | protected void finalize() throws Throwable { |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2643 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2644 | if (mReferenceCount != 0) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2645 | Slog.wtf(TAG, "Suspend blocker \"" + mName |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2646 | + "\" was finalized without being released!"); |
| 2647 | mReferenceCount = 0; |
| 2648 | nativeReleaseSuspendBlocker(mName); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2649 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2650 | } |
| 2651 | } finally { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2652 | super.finalize(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
Craig Mautner | 75fc9de | 2012-06-18 16:53:27 -0700 | [diff] [blame] | 2656 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2657 | public void acquire() { |
| 2658 | synchronized (this) { |
| 2659 | mReferenceCount += 1; |
| 2660 | if (mReferenceCount == 1) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 2661 | if (DEBUG_SPEW) { |
| 2662 | Slog.d(TAG, "Acquiring suspend blocker \"" + mName + "\"."); |
| 2663 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2664 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2665 | nativeAcquireSuspendBlocker(mName); |
Craig Mautner | 3793368 | 2012-06-06 14:13:39 -0700 | [diff] [blame] | 2666 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | |
Craig Mautner | 75fc9de | 2012-06-18 16:53:27 -0700 | [diff] [blame] | 2670 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2671 | public void release() { |
| 2672 | synchronized (this) { |
| 2673 | mReferenceCount -= 1; |
| 2674 | if (mReferenceCount == 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 2675 | if (DEBUG_SPEW) { |
| 2676 | Slog.d(TAG, "Releasing suspend blocker \"" + mName + "\"."); |
| 2677 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2678 | nativeReleaseSuspendBlocker(mName); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2679 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2680 | } else if (mReferenceCount < 0) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2681 | Slog.wtf(TAG, "Suspend blocker \"" + mName |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2682 | + "\" was released without being acquired!", new Throwable()); |
| 2683 | mReferenceCount = 0; |
| 2684 | } |
| 2685 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2686 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2687 | |
| 2688 | @Override |
| 2689 | public String toString() { |
| 2690 | synchronized (this) { |
| 2691 | return mName + ": ref count=" + mReferenceCount; |
| 2692 | } |
| 2693 | } |
| 2694 | } |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2695 | |
| 2696 | private final class ScreenOnBlockerImpl implements ScreenOnBlocker { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2697 | private static final String TRACE_NAME = "ScreenOnBlocker"; |
| 2698 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2699 | private int mNestCount; |
| 2700 | |
| 2701 | public boolean isHeld() { |
| 2702 | synchronized (this) { |
| 2703 | return mNestCount != 0; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | @Override |
| 2708 | public void acquire() { |
| 2709 | synchronized (this) { |
| 2710 | mNestCount += 1; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2711 | if (DEBUG || true) { |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2712 | Slog.d(TAG, "Screen on blocked: mNestCount=" + mNestCount); |
| 2713 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2714 | if (mNestCount == 1) { |
| 2715 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, TRACE_NAME, 0); |
| 2716 | } |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | @Override |
| 2721 | public void release() { |
| 2722 | synchronized (this) { |
| 2723 | mNestCount -= 1; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2724 | if (mNestCount == 0) { |
| 2725 | if (DEBUG || true) { |
| 2726 | Slog.d(TAG, "Screen on unblocked: mNestCount=" + mNestCount); |
| 2727 | } |
| 2728 | mHandler.sendEmptyMessage(MSG_SCREEN_ON_BLOCKER_RELEASED); |
| 2729 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, TRACE_NAME, 0); |
| 2730 | } else if (mNestCount < 0) { |
| 2731 | Slog.wtf(TAG, "Screen on blocker was released without being acquired!", |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2732 | new Throwable()); |
| 2733 | mNestCount = 0; |
| 2734 | } |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | @Override |
| 2739 | public String toString() { |
| 2740 | synchronized (this) { |
| 2741 | return "held=" + (mNestCount != 0) + ", mNestCount=" + mNestCount; |
| 2742 | } |
| 2743 | } |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2746 | private final class BinderService extends IPowerManager.Stub { |
| 2747 | @Override // Binder call |
| 2748 | public void acquireWakeLockWithUid(IBinder lock, int flags, String tag, |
| 2749 | String packageName, int uid) { |
Dianne Hackborn | ef640cd | 2014-03-25 14:41:05 -0700 | [diff] [blame] | 2750 | if (uid < 0) { |
| 2751 | uid = Binder.getCallingUid(); |
| 2752 | } |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2753 | acquireWakeLock(lock, flags, tag, packageName, new WorkSource(uid), null); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | @Override // Binder call |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 2757 | public void powerHint(int hintId, int data) { |
Dianne Hackborn | ddef7e7 | 2014-07-09 16:39:14 -0700 | [diff] [blame] | 2758 | if (!mSystemReady) { |
| 2759 | // Service not ready yet, so who the heck cares about power hints, bah. |
| 2760 | return; |
| 2761 | } |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 2762 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 2763 | powerHintInternal(hintId, data); |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | @Override // Binder call |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2767 | public void acquireWakeLock(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2768 | WorkSource ws, String historyTag) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2769 | if (lock == null) { |
| 2770 | throw new IllegalArgumentException("lock must not be null"); |
| 2771 | } |
| 2772 | if (packageName == null) { |
| 2773 | throw new IllegalArgumentException("packageName must not be null"); |
| 2774 | } |
| 2775 | PowerManager.validateWakeLockParameters(flags, tag); |
| 2776 | |
| 2777 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 2778 | if ((flags & PowerManager.DOZE_WAKE_LOCK) != 0) { |
| 2779 | mContext.enforceCallingOrSelfPermission( |
| 2780 | android.Manifest.permission.DEVICE_POWER, null); |
| 2781 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2782 | if (ws != null && ws.size() != 0) { |
| 2783 | mContext.enforceCallingOrSelfPermission( |
| 2784 | android.Manifest.permission.UPDATE_DEVICE_STATS, null); |
| 2785 | } else { |
| 2786 | ws = null; |
| 2787 | } |
| 2788 | |
| 2789 | final int uid = Binder.getCallingUid(); |
| 2790 | final int pid = Binder.getCallingPid(); |
| 2791 | final long ident = Binder.clearCallingIdentity(); |
| 2792 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 2793 | acquireWakeLockInternal(lock, flags, tag, packageName, ws, historyTag, uid, pid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2794 | } finally { |
| 2795 | Binder.restoreCallingIdentity(ident); |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | @Override // Binder call |
| 2800 | public void releaseWakeLock(IBinder lock, int flags) { |
| 2801 | if (lock == null) { |
| 2802 | throw new IllegalArgumentException("lock must not be null"); |
| 2803 | } |
| 2804 | |
| 2805 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2806 | |
| 2807 | final long ident = Binder.clearCallingIdentity(); |
| 2808 | try { |
| 2809 | releaseWakeLockInternal(lock, flags); |
| 2810 | } finally { |
| 2811 | Binder.restoreCallingIdentity(ident); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | @Override // Binder call |
| 2816 | public void updateWakeLockUids(IBinder lock, int[] uids) { |
| 2817 | WorkSource ws = null; |
| 2818 | |
| 2819 | if (uids != null) { |
| 2820 | ws = new WorkSource(); |
| 2821 | // XXX should WorkSource have a way to set uids as an int[] instead of adding them |
| 2822 | // one at a time? |
| 2823 | for (int i = 0; i < uids.length; i++) { |
| 2824 | ws.add(uids[i]); |
| 2825 | } |
| 2826 | } |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2827 | updateWakeLockWorkSource(lock, ws, null); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | @Override // Binder call |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2831 | public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2832 | if (lock == null) { |
| 2833 | throw new IllegalArgumentException("lock must not be null"); |
| 2834 | } |
| 2835 | |
| 2836 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 2837 | if (ws != null && ws.size() != 0) { |
| 2838 | mContext.enforceCallingOrSelfPermission( |
| 2839 | android.Manifest.permission.UPDATE_DEVICE_STATS, null); |
| 2840 | } else { |
| 2841 | ws = null; |
| 2842 | } |
| 2843 | |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 2844 | final int callingUid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2845 | final long ident = Binder.clearCallingIdentity(); |
| 2846 | try { |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 2847 | updateWakeLockWorkSourceInternal(lock, ws, historyTag, callingUid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2848 | } finally { |
| 2849 | Binder.restoreCallingIdentity(ident); |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | @Override // Binder call |
| 2854 | public boolean isWakeLockLevelSupported(int level) { |
| 2855 | final long ident = Binder.clearCallingIdentity(); |
| 2856 | try { |
| 2857 | return isWakeLockLevelSupportedInternal(level); |
| 2858 | } finally { |
| 2859 | Binder.restoreCallingIdentity(ident); |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | @Override // Binder call |
| 2864 | public void userActivity(long eventTime, int event, int flags) { |
| 2865 | final long now = SystemClock.uptimeMillis(); |
| 2866 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER) |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 2867 | != PackageManager.PERMISSION_GRANTED |
| 2868 | && mContext.checkCallingOrSelfPermission( |
| 2869 | android.Manifest.permission.USER_ACTIVITY) |
| 2870 | != PackageManager.PERMISSION_GRANTED) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2871 | // Once upon a time applications could call userActivity(). |
| 2872 | // Now we require the DEVICE_POWER permission. Log a warning and ignore the |
| 2873 | // request instead of throwing a SecurityException so we don't break old apps. |
| 2874 | synchronized (mLock) { |
| 2875 | if (now >= mLastWarningAboutUserActivityPermission + (5 * 60 * 1000)) { |
| 2876 | mLastWarningAboutUserActivityPermission = now; |
| 2877 | Slog.w(TAG, "Ignoring call to PowerManager.userActivity() because the " |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 2878 | + "caller does not have DEVICE_POWER or USER_ACTIVITY " |
| 2879 | + "permission. Please fix your app! " |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2880 | + " pid=" + Binder.getCallingPid() |
| 2881 | + " uid=" + Binder.getCallingUid()); |
| 2882 | } |
| 2883 | } |
| 2884 | return; |
| 2885 | } |
| 2886 | |
| 2887 | if (eventTime > SystemClock.uptimeMillis()) { |
| 2888 | throw new IllegalArgumentException("event time must not be in the future"); |
| 2889 | } |
| 2890 | |
| 2891 | final int uid = Binder.getCallingUid(); |
| 2892 | final long ident = Binder.clearCallingIdentity(); |
| 2893 | try { |
| 2894 | userActivityInternal(eventTime, event, flags, uid); |
| 2895 | } finally { |
| 2896 | Binder.restoreCallingIdentity(ident); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | @Override // Binder call |
| 2901 | public void wakeUp(long eventTime) { |
| 2902 | if (eventTime > SystemClock.uptimeMillis()) { |
| 2903 | throw new IllegalArgumentException("event time must not be in the future"); |
| 2904 | } |
| 2905 | |
| 2906 | mContext.enforceCallingOrSelfPermission( |
| 2907 | android.Manifest.permission.DEVICE_POWER, null); |
| 2908 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2909 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2910 | final long ident = Binder.clearCallingIdentity(); |
| 2911 | try { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2912 | wakeUpInternal(eventTime, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2913 | } finally { |
| 2914 | Binder.restoreCallingIdentity(ident); |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | @Override // Binder call |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 2919 | public void goToSleep(long eventTime, int reason, int flags) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2920 | if (eventTime > SystemClock.uptimeMillis()) { |
| 2921 | throw new IllegalArgumentException("event time must not be in the future"); |
| 2922 | } |
| 2923 | |
| 2924 | mContext.enforceCallingOrSelfPermission( |
| 2925 | android.Manifest.permission.DEVICE_POWER, null); |
| 2926 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2927 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2928 | final long ident = Binder.clearCallingIdentity(); |
| 2929 | try { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2930 | goToSleepInternal(eventTime, reason, flags, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2931 | } finally { |
| 2932 | Binder.restoreCallingIdentity(ident); |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | @Override // Binder call |
| 2937 | public void nap(long eventTime) { |
| 2938 | if (eventTime > SystemClock.uptimeMillis()) { |
| 2939 | throw new IllegalArgumentException("event time must not be in the future"); |
| 2940 | } |
| 2941 | |
| 2942 | mContext.enforceCallingOrSelfPermission( |
| 2943 | android.Manifest.permission.DEVICE_POWER, null); |
| 2944 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2945 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2946 | final long ident = Binder.clearCallingIdentity(); |
| 2947 | try { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2948 | napInternal(eventTime, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2949 | } finally { |
| 2950 | Binder.restoreCallingIdentity(ident); |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | @Override // Binder call |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2955 | public boolean isInteractive() { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2956 | final long ident = Binder.clearCallingIdentity(); |
| 2957 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2958 | return isInteractiveInternal(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2959 | } finally { |
| 2960 | Binder.restoreCallingIdentity(ident); |
| 2961 | } |
| 2962 | } |
| 2963 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 2964 | @Override // Binder call |
| 2965 | public boolean isPowerSaveMode() { |
| 2966 | final long ident = Binder.clearCallingIdentity(); |
| 2967 | try { |
| 2968 | return isLowPowerModeInternal(); |
| 2969 | } finally { |
| 2970 | Binder.restoreCallingIdentity(ident); |
| 2971 | } |
| 2972 | } |
| 2973 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2974 | @Override // Binder call |
| 2975 | public boolean setPowerSaveMode(boolean mode) { |
| 2976 | mContext.enforceCallingOrSelfPermission( |
| 2977 | android.Manifest.permission.DEVICE_POWER, null); |
| 2978 | final long ident = Binder.clearCallingIdentity(); |
| 2979 | try { |
| 2980 | return setLowPowerModeInternal(mode); |
| 2981 | } finally { |
| 2982 | Binder.restoreCallingIdentity(ident); |
| 2983 | } |
| 2984 | } |
| 2985 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2986 | /** |
| 2987 | * Reboots the device. |
| 2988 | * |
| 2989 | * @param confirm If true, shows a reboot confirmation dialog. |
| 2990 | * @param reason The reason for the reboot, or null if none. |
| 2991 | * @param wait If true, this call waits for the reboot to complete and does not return. |
| 2992 | */ |
| 2993 | @Override // Binder call |
| 2994 | public void reboot(boolean confirm, String reason, boolean wait) { |
| 2995 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 2996 | if (PowerManager.REBOOT_RECOVERY.equals(reason)) { |
| 2997 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null); |
| 2998 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 2999 | |
| 3000 | final long ident = Binder.clearCallingIdentity(); |
| 3001 | try { |
| 3002 | shutdownOrRebootInternal(false, confirm, reason, wait); |
| 3003 | } finally { |
| 3004 | Binder.restoreCallingIdentity(ident); |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | /** |
| 3009 | * Shuts down the device. |
| 3010 | * |
| 3011 | * @param confirm If true, shows a shutdown confirmation dialog. |
| 3012 | * @param wait If true, this call waits for the shutdown to complete and does not return. |
| 3013 | */ |
| 3014 | @Override // Binder call |
| 3015 | public void shutdown(boolean confirm, boolean wait) { |
| 3016 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 3017 | |
| 3018 | final long ident = Binder.clearCallingIdentity(); |
| 3019 | try { |
| 3020 | shutdownOrRebootInternal(true, confirm, null, wait); |
| 3021 | } finally { |
| 3022 | Binder.restoreCallingIdentity(ident); |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | /** |
| 3027 | * Crash the runtime (causing a complete restart of the Android framework). |
| 3028 | * Requires REBOOT permission. Mostly for testing. Should not return. |
| 3029 | */ |
| 3030 | @Override // Binder call |
| 3031 | public void crash(String message) { |
| 3032 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 3033 | |
| 3034 | final long ident = Binder.clearCallingIdentity(); |
| 3035 | try { |
| 3036 | crashInternal(message); |
| 3037 | } finally { |
| 3038 | Binder.restoreCallingIdentity(ident); |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | /** |
| 3043 | * Set the setting that determines whether the device stays on when plugged in. |
| 3044 | * The argument is a bit string, with each bit specifying a power source that, |
| 3045 | * when the device is connected to that source, causes the device to stay on. |
| 3046 | * See {@link android.os.BatteryManager} for the list of power sources that |
| 3047 | * can be specified. Current values include |
| 3048 | * {@link android.os.BatteryManager#BATTERY_PLUGGED_AC} |
| 3049 | * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB} |
| 3050 | * |
| 3051 | * Used by "adb shell svc power stayon ..." |
| 3052 | * |
| 3053 | * @param val an {@code int} containing the bits that specify which power sources |
| 3054 | * should cause the device to stay on. |
| 3055 | */ |
| 3056 | @Override // Binder call |
| 3057 | public void setStayOnSetting(int val) { |
| 3058 | mContext.enforceCallingOrSelfPermission( |
| 3059 | android.Manifest.permission.WRITE_SETTINGS, null); |
| 3060 | |
| 3061 | final long ident = Binder.clearCallingIdentity(); |
| 3062 | try { |
| 3063 | setStayOnSettingInternal(val); |
| 3064 | } finally { |
| 3065 | Binder.restoreCallingIdentity(ident); |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /** |
| 3070 | * Used by device administration to set the maximum screen off timeout. |
| 3071 | * |
| 3072 | * This method must only be called by the device administration policy manager. |
| 3073 | */ |
| 3074 | @Override // Binder call |
| 3075 | public void setMaximumScreenOffTimeoutFromDeviceAdmin(int timeMs) { |
| 3076 | final long ident = Binder.clearCallingIdentity(); |
| 3077 | try { |
| 3078 | setMaximumScreenOffTimeoutFromDeviceAdminInternal(timeMs); |
| 3079 | } finally { |
| 3080 | Binder.restoreCallingIdentity(ident); |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /** |
| 3085 | * Used by the settings application and brightness control widgets to |
| 3086 | * temporarily override the current screen brightness setting so that the |
| 3087 | * user can observe the effect of an intended settings change without applying |
| 3088 | * it immediately. |
| 3089 | * |
| 3090 | * The override will be canceled when the setting value is next updated. |
| 3091 | * |
| 3092 | * @param brightness The overridden brightness. |
| 3093 | * |
| 3094 | * @see android.provider.Settings.System#SCREEN_BRIGHTNESS |
| 3095 | */ |
| 3096 | @Override // Binder call |
| 3097 | public void setTemporaryScreenBrightnessSettingOverride(int brightness) { |
| 3098 | mContext.enforceCallingOrSelfPermission( |
| 3099 | android.Manifest.permission.DEVICE_POWER, null); |
| 3100 | |
| 3101 | final long ident = Binder.clearCallingIdentity(); |
| 3102 | try { |
| 3103 | setTemporaryScreenBrightnessSettingOverrideInternal(brightness); |
| 3104 | } finally { |
| 3105 | Binder.restoreCallingIdentity(ident); |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | /** |
| 3110 | * Used by the settings application and brightness control widgets to |
| 3111 | * temporarily override the current screen auto-brightness adjustment setting so that the |
| 3112 | * user can observe the effect of an intended settings change without applying |
| 3113 | * it immediately. |
| 3114 | * |
| 3115 | * The override will be canceled when the setting value is next updated. |
| 3116 | * |
| 3117 | * @param adj The overridden brightness, or Float.NaN to disable the override. |
| 3118 | * |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 3119 | * @see android.provider.Settings.System#SCREEN_AUTO_BRIGHTNESS_ADJ |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3120 | */ |
| 3121 | @Override // Binder call |
| 3122 | public void setTemporaryScreenAutoBrightnessAdjustmentSettingOverride(float adj) { |
| 3123 | mContext.enforceCallingOrSelfPermission( |
| 3124 | android.Manifest.permission.DEVICE_POWER, null); |
| 3125 | |
| 3126 | final long ident = Binder.clearCallingIdentity(); |
| 3127 | try { |
| 3128 | setTemporaryScreenAutoBrightnessAdjustmentSettingOverrideInternal(adj); |
| 3129 | } finally { |
| 3130 | Binder.restoreCallingIdentity(ident); |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | /** |
| 3135 | * Used by the phone application to make the attention LED flash when ringing. |
| 3136 | */ |
| 3137 | @Override // Binder call |
| 3138 | public void setAttentionLight(boolean on, int color) { |
| 3139 | mContext.enforceCallingOrSelfPermission( |
| 3140 | android.Manifest.permission.DEVICE_POWER, null); |
| 3141 | |
| 3142 | final long ident = Binder.clearCallingIdentity(); |
| 3143 | try { |
| 3144 | setAttentionLightInternal(on, color); |
| 3145 | } finally { |
| 3146 | Binder.restoreCallingIdentity(ident); |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | @Override // Binder call |
| 3151 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 3152 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP) |
| 3153 | != PackageManager.PERMISSION_GRANTED) { |
| 3154 | pw.println("Permission Denial: can't dump PowerManager from from pid=" |
| 3155 | + Binder.getCallingPid() |
| 3156 | + ", uid=" + Binder.getCallingUid()); |
| 3157 | return; |
| 3158 | } |
| 3159 | |
| 3160 | final long ident = Binder.clearCallingIdentity(); |
| 3161 | try { |
| 3162 | dumpInternal(pw); |
| 3163 | } finally { |
| 3164 | Binder.restoreCallingIdentity(ident); |
| 3165 | } |
| 3166 | } |
| 3167 | } |
| 3168 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 3169 | private final class LocalService extends PowerManagerInternal { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3170 | @Override |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3171 | public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) { |
| 3172 | if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT |
| 3173 | || screenBrightness > PowerManager.BRIGHTNESS_ON) { |
| 3174 | screenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3175 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3176 | setScreenBrightnessOverrideFromWindowManagerInternal(screenBrightness); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3177 | } |
| 3178 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3179 | @Override |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3180 | public void setButtonBrightnessOverrideFromWindowManager(int screenBrightness) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3181 | // Do nothing. |
| 3182 | // Button lights are not currently supported in the new implementation. |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3183 | } |
| 3184 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3185 | @Override |
| 3186 | public void setDozeOverrideFromDreamManager(int screenState, int screenBrightness) { |
| 3187 | switch (screenState) { |
| 3188 | case Display.STATE_UNKNOWN: |
| 3189 | case Display.STATE_OFF: |
| 3190 | case Display.STATE_DOZE: |
| 3191 | case Display.STATE_DOZE_SUSPEND: |
| 3192 | case Display.STATE_ON: |
| 3193 | break; |
| 3194 | default: |
| 3195 | screenState = Display.STATE_UNKNOWN; |
| 3196 | break; |
| 3197 | } |
| 3198 | if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT |
| 3199 | || screenBrightness > PowerManager.BRIGHTNESS_ON) { |
| 3200 | screenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
| 3201 | } |
| 3202 | setDozeOverrideFromDreamManagerInternal(screenState, screenBrightness); |
| 3203 | } |
| 3204 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3205 | @Override |
| 3206 | public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3207 | setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | @Override |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 3211 | public boolean getLowPowerModeEnabled() { |
| 3212 | synchronized (mLock) { |
| 3213 | return mLowPowerModeEnabled; |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | @Override |
| 3218 | public void registerLowPowerModeObserver(LowPowerModeListener listener) { |
| 3219 | synchronized (mLock) { |
| 3220 | mLowPowerModeListeners.add(listener); |
| 3221 | } |
| 3222 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | } |