Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
| 19 | import android.annotation.MainThread; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 20 | import android.hardware.display.AmbientDisplayConfiguration; |
Adrian Roos | a6c03f8 | 2017-07-26 16:20:30 +0200 | [diff] [blame] | 21 | import android.os.Trace; |
Adrian Roos | 0261fb2 | 2017-03-07 20:20:35 +0000 | [diff] [blame] | 22 | import android.os.UserHandle; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | import android.view.Display; |
| 25 | |
| 26 | import com.android.internal.util.Preconditions; |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 27 | import com.android.systemui.dock.DockManager; |
Lucas Dupin | 737b851 | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 28 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
| 29 | import com.android.systemui.keyguard.WakefulnessLifecycle.Wakefulness; |
Lucas Dupin | 12663d3 | 2018-02-08 11:42:22 -0800 | [diff] [blame] | 30 | import com.android.systemui.statusbar.phone.DozeParameters; |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 31 | import com.android.systemui.statusbar.policy.BatteryController; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 32 | import com.android.systemui.util.Assert; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 33 | import com.android.systemui.util.wakelock.WakeLock; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 34 | |
| 35 | import java.io.PrintWriter; |
| 36 | import java.util.ArrayList; |
| 37 | |
| 38 | /** |
| 39 | * Orchestrates all things doze. |
| 40 | * |
| 41 | * DozeMachine implements a state machine that orchestrates how the UI and triggers work and |
| 42 | * interfaces with the power and screen states. |
| 43 | * |
| 44 | * During state transitions and in certain states, DozeMachine holds a wake lock. |
| 45 | */ |
| 46 | public class DozeMachine { |
| 47 | |
| 48 | static final String TAG = "DozeMachine"; |
| 49 | static final boolean DEBUG = DozeService.DEBUG; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 50 | private final DozeLog mDozeLog; |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 51 | private static final String REASON_CHANGE_STATE = "DozeMachine#requestState"; |
| 52 | private static final String REASON_HELD_FOR_STATE = "DozeMachine#heldForState"; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 53 | |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 54 | public enum State { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 55 | /** Default state. Transition to INITIALIZED to get Doze going. */ |
| 56 | UNINITIALIZED, |
| 57 | /** Doze components are set up. Followed by transition to DOZE or DOZE_AOD. */ |
| 58 | INITIALIZED, |
| 59 | /** Regular doze. Device is asleep and listening for pulse triggers. */ |
| 60 | DOZE, |
| 61 | /** Always-on doze. Device is asleep, showing UI and listening for pulse triggers. */ |
| 62 | DOZE_AOD, |
| 63 | /** Pulse has been requested. Device is awake and preparing UI */ |
| 64 | DOZE_REQUEST_PULSE, |
| 65 | /** Pulse is showing. Device is awake and showing UI. */ |
| 66 | DOZE_PULSING, |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 67 | /** Pulse is showing with bright wallpaper. Device is awake and showing UI. */ |
| 68 | DOZE_PULSING_BRIGHT, |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 69 | /** Pulse is done showing. Followed by transition to DOZE or DOZE_AOD. */ |
| 70 | DOZE_PULSE_DONE, |
| 71 | /** Doze is done. DozeService is finished. */ |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 72 | FINISH, |
| 73 | /** AOD, but the display is temporarily off. */ |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 74 | DOZE_AOD_PAUSED, |
| 75 | /** AOD, prox is near, transitions to DOZE_AOD_PAUSED after a timeout. */ |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 76 | DOZE_AOD_PAUSING, |
| 77 | /** Always-on doze. Device is awake, showing docking UI and listening for pulse triggers. */ |
| 78 | DOZE_AOD_DOCKED; |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 79 | |
| 80 | boolean canPulse() { |
| 81 | switch (this) { |
| 82 | case DOZE: |
| 83 | case DOZE_AOD: |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 84 | case DOZE_AOD_PAUSED: |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 85 | case DOZE_AOD_PAUSING: |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 86 | case DOZE_AOD_DOCKED: |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 87 | return true; |
| 88 | default: |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | boolean staysAwake() { |
| 94 | switch (this) { |
| 95 | case DOZE_REQUEST_PULSE: |
| 96 | case DOZE_PULSING: |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 97 | case DOZE_PULSING_BRIGHT: |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 98 | case DOZE_AOD_DOCKED: |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 99 | return true; |
| 100 | default: |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
Lucas Dupin | 12663d3 | 2018-02-08 11:42:22 -0800 | [diff] [blame] | 105 | int screenState(DozeParameters parameters) { |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 106 | switch (this) { |
| 107 | case UNINITIALIZED: |
| 108 | case INITIALIZED: |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 109 | case DOZE_REQUEST_PULSE: |
| 110 | return parameters.shouldControlScreenOff() ? Display.STATE_ON |
| 111 | : Display.STATE_OFF; |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 112 | case DOZE_AOD_PAUSED: |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 113 | case DOZE: |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 114 | return Display.STATE_OFF; |
| 115 | case DOZE_PULSING: |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 116 | case DOZE_PULSING_BRIGHT: |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 117 | case DOZE_AOD_DOCKED: |
Adrian Roos | 2324d85 | 2017-04-27 15:50:14 -0700 | [diff] [blame] | 118 | return Display.STATE_ON; |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 119 | case DOZE_AOD: |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 120 | case DOZE_AOD_PAUSING: |
Adrian Roos | a1e6b31 | 2017-03-28 16:20:34 -0700 | [diff] [blame] | 121 | return Display.STATE_DOZE_SUSPEND; |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 122 | default: |
| 123 | return Display.STATE_UNKNOWN; |
| 124 | } |
| 125 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private final Service mDozeService; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 129 | private final WakeLock mWakeLock; |
Adrian Roos | 0261fb2 | 2017-03-07 20:20:35 +0000 | [diff] [blame] | 130 | private final AmbientDisplayConfiguration mConfig; |
Lucas Dupin | 737b851 | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 131 | private final WakefulnessLifecycle mWakefulnessLifecycle; |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 132 | private final BatteryController mBatteryController; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 133 | private Part[] mParts; |
| 134 | |
| 135 | private final ArrayList<State> mQueuedRequests = new ArrayList<>(); |
| 136 | private State mState = State.UNINITIALIZED; |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 137 | private int mPulseReason; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 138 | private boolean mWakeLockHeldForCurrentState = false; |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 139 | private DockManager mDockManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 140 | |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 141 | public DozeMachine(Service service, AmbientDisplayConfiguration config, WakeLock wakeLock, |
| 142 | WakefulnessLifecycle wakefulnessLifecycle, BatteryController batteryController, |
| 143 | DozeLog dozeLog, DockManager dockManager) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 144 | mDozeService = service; |
Adrian Roos | 0261fb2 | 2017-03-07 20:20:35 +0000 | [diff] [blame] | 145 | mConfig = config; |
Lucas Dupin | 737b851 | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 146 | mWakefulnessLifecycle = wakefulnessLifecycle; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 147 | mWakeLock = wakeLock; |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 148 | mBatteryController = batteryController; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 149 | mDozeLog = dozeLog; |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 150 | mDockManager = dockManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** Initializes the set of {@link Part}s. Must be called exactly once after construction. */ |
| 154 | public void setParts(Part[] parts) { |
| 155 | Preconditions.checkState(mParts == null); |
| 156 | mParts = parts; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Requests transitioning to {@code requestedState}. |
| 161 | * |
| 162 | * This can be called during a state transition, in which case it will be queued until all |
| 163 | * queued state transitions are done. |
| 164 | * |
| 165 | * A wake lock is held while the transition is happening. |
| 166 | * |
| 167 | * Note that {@link #transitionPolicy} can modify what state will be transitioned to. |
| 168 | */ |
| 169 | @MainThread |
| 170 | public void requestState(State requestedState) { |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 171 | Preconditions.checkArgument(requestedState != State.DOZE_REQUEST_PULSE); |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 172 | requestState(requestedState, DozeLog.PULSE_REASON_NONE); |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @MainThread |
| 176 | public void requestPulse(int pulseReason) { |
| 177 | // Must not be called during a transition. There's no inherent problem with that, |
| 178 | // but there's currently no need to execute from a transition and it simplifies the |
| 179 | // code to not have to worry about keeping the pulseReason in mQueuedRequests. |
| 180 | Preconditions.checkState(!isExecutingTransition()); |
| 181 | requestState(State.DOZE_REQUEST_PULSE, pulseReason); |
| 182 | } |
| 183 | |
| 184 | private void requestState(State requestedState, int pulseReason) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 185 | Assert.isMainThread(); |
| 186 | if (DEBUG) { |
| 187 | Log.i(TAG, "request: current=" + mState + " req=" + requestedState, |
| 188 | new Throwable("here")); |
| 189 | } |
| 190 | |
| 191 | boolean runNow = !isExecutingTransition(); |
| 192 | mQueuedRequests.add(requestedState); |
| 193 | if (runNow) { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 194 | mWakeLock.acquire(REASON_CHANGE_STATE); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 195 | for (int i = 0; i < mQueuedRequests.size(); i++) { |
| 196 | // Transitions in Parts can call back into requestState, which will |
| 197 | // cause mQueuedRequests to grow. |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 198 | transitionTo(mQueuedRequests.get(i), pulseReason); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 199 | } |
| 200 | mQueuedRequests.clear(); |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 201 | mWakeLock.release(REASON_CHANGE_STATE); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @return the current state. |
| 207 | * |
| 208 | * This must not be called during a transition. |
| 209 | */ |
| 210 | @MainThread |
| 211 | public State getState() { |
| 212 | Assert.isMainThread(); |
Lucas Dupin | b7fd1eb | 2019-03-28 12:05:17 -0700 | [diff] [blame] | 213 | if (isExecutingTransition()) { |
| 214 | throw new IllegalStateException("Cannot get state because there were pending " |
| 215 | + "transitions: " + mQueuedRequests.toString()); |
| 216 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 217 | return mState; |
| 218 | } |
| 219 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 220 | /** |
| 221 | * @return the current pulse reason. |
| 222 | * |
| 223 | * This is only valid if the machine is currently in one of the pulse states. |
| 224 | */ |
| 225 | @MainThread |
| 226 | public int getPulseReason() { |
| 227 | Assert.isMainThread(); |
| 228 | Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE |
| 229 | || mState == State.DOZE_PULSING |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 230 | || mState == State.DOZE_PULSING_BRIGHT |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 231 | || mState == State.DOZE_PULSE_DONE, "must be in pulsing state, but is " + mState); |
| 232 | return mPulseReason; |
| 233 | } |
| 234 | |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 235 | /** Requests the PowerManager to wake up now. */ |
| 236 | public void wakeUp() { |
| 237 | mDozeService.requestWakeUp(); |
| 238 | } |
| 239 | |
Lucas Dupin | 1946b31 | 2019-03-21 14:48:23 -0700 | [diff] [blame] | 240 | public boolean isExecutingTransition() { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 241 | return !mQueuedRequests.isEmpty(); |
| 242 | } |
| 243 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 244 | private void transitionTo(State requestedState, int pulseReason) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 245 | State newState = transitionPolicy(requestedState); |
| 246 | |
| 247 | if (DEBUG) { |
| 248 | Log.i(TAG, "transition: old=" + mState + " req=" + requestedState + " new=" + newState); |
| 249 | } |
| 250 | |
| 251 | if (newState == mState) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | validateTransition(newState); |
| 256 | |
| 257 | State oldState = mState; |
| 258 | mState = newState; |
| 259 | |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 260 | mDozeLog.traceState(newState); |
Adrian Roos | a6c03f8 | 2017-07-26 16:20:30 +0200 | [diff] [blame] | 261 | Trace.traceCounter(Trace.TRACE_TAG_APP, "doze_machine_state", newState.ordinal()); |
| 262 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 263 | updatePulseReason(newState, oldState, pulseReason); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 264 | performTransitionOnComponents(oldState, newState); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 265 | updateWakeLockState(newState); |
| 266 | |
| 267 | resolveIntermediateState(newState); |
| 268 | } |
| 269 | |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 270 | private void updatePulseReason(State newState, State oldState, int pulseReason) { |
| 271 | if (newState == State.DOZE_REQUEST_PULSE) { |
| 272 | mPulseReason = pulseReason; |
| 273 | } else if (oldState == State.DOZE_PULSE_DONE) { |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 274 | mPulseReason = DozeLog.PULSE_REASON_NONE; |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 278 | private void performTransitionOnComponents(State oldState, State newState) { |
| 279 | for (Part p : mParts) { |
| 280 | p.transitionTo(oldState, newState); |
| 281 | } |
| 282 | |
| 283 | switch (newState) { |
| 284 | case FINISH: |
| 285 | mDozeService.finish(); |
| 286 | break; |
| 287 | default: |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private void validateTransition(State newState) { |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 292 | try { |
| 293 | switch (mState) { |
| 294 | case FINISH: |
| 295 | Preconditions.checkState(newState == State.FINISH); |
| 296 | break; |
| 297 | case UNINITIALIZED: |
| 298 | Preconditions.checkState(newState == State.INITIALIZED); |
| 299 | break; |
| 300 | } |
| 301 | switch (newState) { |
| 302 | case UNINITIALIZED: |
| 303 | throw new IllegalArgumentException("can't transition to UNINITIALIZED"); |
| 304 | case INITIALIZED: |
| 305 | Preconditions.checkState(mState == State.UNINITIALIZED); |
| 306 | break; |
| 307 | case DOZE_PULSING: |
| 308 | Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE); |
| 309 | break; |
| 310 | case DOZE_PULSE_DONE: |
Adrian Roos | cd139a6 | 2016-12-16 12:23:51 -0800 | [diff] [blame] | 311 | Preconditions.checkState( |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 312 | mState == State.DOZE_REQUEST_PULSE || mState == State.DOZE_PULSING |
| 313 | || mState == State.DOZE_PULSING_BRIGHT); |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 314 | break; |
| 315 | default: |
| 316 | break; |
| 317 | } |
| 318 | } catch (RuntimeException e) { |
| 319 | throw new IllegalStateException("Illegal Transition: " + mState + " -> " + newState, e); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | private State transitionPolicy(State requestedState) { |
| 324 | if (mState == State.FINISH) { |
| 325 | return State.FINISH; |
| 326 | } |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 327 | if ((mState == State.DOZE_AOD_PAUSED || mState == State.DOZE_AOD_PAUSING |
| 328 | || mState == State.DOZE_AOD || mState == State.DOZE) |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 329 | && requestedState == State.DOZE_PULSE_DONE) { |
| 330 | Log.i(TAG, "Dropping pulse done because current state is already done: " + mState); |
| 331 | return mState; |
| 332 | } |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 333 | if (requestedState == State.DOZE_AOD && mBatteryController.isAodPowerSave()) { |
| 334 | return State.DOZE; |
| 335 | } |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 336 | if (requestedState == State.DOZE_REQUEST_PULSE && !mState.canPulse()) { |
| 337 | Log.i(TAG, "Dropping pulse request because current state can't pulse: " + mState); |
| 338 | return mState; |
| 339 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 340 | return requestedState; |
| 341 | } |
| 342 | |
| 343 | private void updateWakeLockState(State newState) { |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 344 | boolean staysAwake = newState.staysAwake(); |
| 345 | if (mWakeLockHeldForCurrentState && !staysAwake) { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 346 | mWakeLock.release(REASON_HELD_FOR_STATE); |
Adrian Roos | ae0c5e8 | 2016-11-16 19:56:19 -0800 | [diff] [blame] | 347 | mWakeLockHeldForCurrentState = false; |
Adrian Roos | b84dc18 | 2016-12-02 09:01:09 -0800 | [diff] [blame] | 348 | } else if (!mWakeLockHeldForCurrentState && staysAwake) { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 349 | mWakeLock.acquire(REASON_HELD_FOR_STATE); |
Adrian Roos | ae0c5e8 | 2016-11-16 19:56:19 -0800 | [diff] [blame] | 350 | mWakeLockHeldForCurrentState = true; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 354 | private void resolveIntermediateState(State state) { |
| 355 | switch (state) { |
| 356 | case INITIALIZED: |
| 357 | case DOZE_PULSE_DONE: |
Lucas Dupin | 737b851 | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 358 | final State nextState; |
| 359 | @Wakefulness int wakefulness = mWakefulnessLifecycle.getWakefulness(); |
| 360 | if (wakefulness == WakefulnessLifecycle.WAKEFULNESS_AWAKE |
| 361 | || wakefulness == WakefulnessLifecycle.WAKEFULNESS_WAKING) { |
| 362 | nextState = State.FINISH; |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 363 | } else if (mDockManager.isDocked()) { |
| 364 | nextState = mDockManager.isHidden() ? State.DOZE : State.DOZE_AOD_DOCKED; |
Lucas Dupin | 737b851 | 2019-08-09 10:35:34 -0700 | [diff] [blame] | 365 | } else if (mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) { |
| 366 | nextState = State.DOZE_AOD; |
| 367 | } else { |
| 368 | nextState = State.DOZE; |
| 369 | } |
| 370 | |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 371 | transitionTo(nextState, DozeLog.PULSE_REASON_NONE); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 372 | break; |
| 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** Dumps the current state */ |
| 379 | public void dump(PrintWriter pw) { |
| 380 | pw.print(" state="); pw.println(mState); |
| 381 | pw.print(" wakeLockHeldForCurrentState="); pw.println(mWakeLockHeldForCurrentState); |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 382 | pw.print(" wakeLock="); pw.println(mWakeLock); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 383 | pw.println("Parts:"); |
| 384 | for (Part p : mParts) { |
| 385 | p.dump(pw); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /** A part of the DozeMachine that needs to be notified about state changes. */ |
| 390 | public interface Part { |
| 391 | /** |
| 392 | * Transition from {@code oldState} to {@code newState}. |
| 393 | * |
| 394 | * This method is guaranteed to only be called while a wake lock is held. |
| 395 | */ |
| 396 | void transitionTo(State oldState, State newState); |
| 397 | |
| 398 | /** Dump current state. For debugging only. */ |
| 399 | default void dump(PrintWriter pw) {} |
| 400 | } |
| 401 | |
| 402 | /** A wrapper interface for {@link android.service.dreams.DreamService} */ |
| 403 | public interface Service { |
| 404 | /** Finish dreaming. */ |
| 405 | void finish(); |
| 406 | |
| 407 | /** Request a display state. See {@link android.view.Display#STATE_DOZE}. */ |
| 408 | void setDozeScreenState(int state); |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 409 | |
| 410 | /** Request waking up. */ |
| 411 | void requestWakeUp(); |
Adrian Roos | 2981eb0 | 2017-05-26 18:40:09 -0700 | [diff] [blame] | 412 | |
| 413 | /** Set screen brightness */ |
| 414 | void setDozeScreenBrightness(int brightness); |
| 415 | |
| 416 | class Delegate implements Service { |
| 417 | private final Service mDelegate; |
| 418 | |
| 419 | public Delegate(Service delegate) { |
| 420 | mDelegate = delegate; |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public void finish() { |
| 425 | mDelegate.finish(); |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void setDozeScreenState(int state) { |
| 430 | mDelegate.setDozeScreenState(state); |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void requestWakeUp() { |
| 435 | mDelegate.requestWakeUp(); |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public void setDozeScreenBrightness(int brightness) { |
| 440 | mDelegate.setDozeScreenBrightness(brightness); |
| 441 | } |
| 442 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 443 | } |
| 444 | } |