blob: 7f2d52780a2392fe44b8cf78d40ee28569b346de [file] [log] [blame]
Adrian Roosff2c4562016-11-03 12:13:36 -07001/*
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
17package com.android.systemui.doze;
18
19import android.annotation.MainThread;
Issei Suzukica19e6e2019-02-26 12:39:11 +010020import android.hardware.display.AmbientDisplayConfiguration;
Adrian Roosa6c03f82017-07-26 16:20:30 +020021import android.os.Trace;
Adrian Roos0261fb22017-03-07 20:20:35 +000022import android.os.UserHandle;
Adrian Roosff2c4562016-11-03 12:13:36 -070023import android.util.Log;
24import android.view.Display;
25
26import com.android.internal.util.Preconditions;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070027import com.android.systemui.keyguard.WakefulnessLifecycle;
28import com.android.systemui.keyguard.WakefulnessLifecycle.Wakefulness;
Lucas Dupin12663d32018-02-08 11:42:22 -080029import com.android.systemui.statusbar.phone.DozeParameters;
Lucas Dupin6edeb182019-09-25 13:39:21 -070030import com.android.systemui.statusbar.policy.BatteryController;
Adrian Roosff2c4562016-11-03 12:13:36 -070031import com.android.systemui.util.Assert;
Adrian Roosc1b50322017-02-27 21:07:58 +010032import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070033
34import java.io.PrintWriter;
35import java.util.ArrayList;
36
37/**
38 * Orchestrates all things doze.
39 *
40 * DozeMachine implements a state machine that orchestrates how the UI and triggers work and
41 * interfaces with the power and screen states.
42 *
43 * During state transitions and in certain states, DozeMachine holds a wake lock.
44 */
45public class DozeMachine {
46
47 static final String TAG = "DozeMachine";
48 static final boolean DEBUG = DozeService.DEBUG;
Lucas Dupinee4c9b72019-02-18 17:04:58 -080049 private static final String REASON_CHANGE_STATE = "DozeMachine#requestState";
50 private static final String REASON_HELD_FOR_STATE = "DozeMachine#heldForState";
Adrian Roosff2c4562016-11-03 12:13:36 -070051
Adrian Roosd35d4ca2017-04-19 14:31:03 -070052 public enum State {
Adrian Roosff2c4562016-11-03 12:13:36 -070053 /** Default state. Transition to INITIALIZED to get Doze going. */
54 UNINITIALIZED,
55 /** Doze components are set up. Followed by transition to DOZE or DOZE_AOD. */
56 INITIALIZED,
57 /** Regular doze. Device is asleep and listening for pulse triggers. */
58 DOZE,
59 /** Always-on doze. Device is asleep, showing UI and listening for pulse triggers. */
60 DOZE_AOD,
61 /** Pulse has been requested. Device is awake and preparing UI */
62 DOZE_REQUEST_PULSE,
63 /** Pulse is showing. Device is awake and showing UI. */
64 DOZE_PULSING,
Lucas Dupin5f00fa52019-03-27 22:46:53 -070065 /** Pulse is showing with bright wallpaper. Device is awake and showing UI. */
66 DOZE_PULSING_BRIGHT,
Adrian Roosff2c4562016-11-03 12:13:36 -070067 /** Pulse is done showing. Followed by transition to DOZE or DOZE_AOD. */
68 DOZE_PULSE_DONE,
69 /** Doze is done. DozeService is finished. */
Adrian Roos67cca742017-04-13 16:52:51 -070070 FINISH,
71 /** AOD, but the display is temporarily off. */
Adrian Roos6023ccb2017-06-28 16:22:02 +020072 DOZE_AOD_PAUSED,
73 /** AOD, prox is near, transitions to DOZE_AOD_PAUSED after a timeout. */
74 DOZE_AOD_PAUSING;
Adrian Roosb84dc182016-12-02 09:01:09 -080075
76 boolean canPulse() {
77 switch (this) {
78 case DOZE:
79 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -070080 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +020081 case DOZE_AOD_PAUSING:
Adrian Roosb84dc182016-12-02 09:01:09 -080082 return true;
83 default:
84 return false;
85 }
86 }
87
88 boolean staysAwake() {
89 switch (this) {
90 case DOZE_REQUEST_PULSE:
91 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -070092 case DOZE_PULSING_BRIGHT:
Adrian Roosb84dc182016-12-02 09:01:09 -080093 return true;
94 default:
95 return false;
96 }
97 }
98
Lucas Dupin12663d32018-02-08 11:42:22 -080099 int screenState(DozeParameters parameters) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800100 switch (this) {
101 case UNINITIALIZED:
102 case INITIALIZED:
Lucas Dupin16cfe452018-02-08 13:14:50 -0800103 case DOZE_REQUEST_PULSE:
104 return parameters.shouldControlScreenOff() ? Display.STATE_ON
105 : Display.STATE_OFF;
Adrian Roos67cca742017-04-13 16:52:51 -0700106 case DOZE_AOD_PAUSED:
Lucas Dupin16cfe452018-02-08 13:14:50 -0800107 case DOZE:
Adrian Roosb84dc182016-12-02 09:01:09 -0800108 return Display.STATE_OFF;
109 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700110 case DOZE_PULSING_BRIGHT:
Adrian Roos2324d852017-04-27 15:50:14 -0700111 return Display.STATE_ON;
Adrian Roosb84dc182016-12-02 09:01:09 -0800112 case DOZE_AOD:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200113 case DOZE_AOD_PAUSING:
Adrian Roosa1e6b312017-03-28 16:20:34 -0700114 return Display.STATE_DOZE_SUSPEND;
Adrian Roosb84dc182016-12-02 09:01:09 -0800115 default:
116 return Display.STATE_UNKNOWN;
117 }
118 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700119 }
120
121 private final Service mDozeService;
Adrian Roosc1b50322017-02-27 21:07:58 +0100122 private final WakeLock mWakeLock;
Adrian Roos0261fb22017-03-07 20:20:35 +0000123 private final AmbientDisplayConfiguration mConfig;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700124 private final WakefulnessLifecycle mWakefulnessLifecycle;
Lucas Dupin6edeb182019-09-25 13:39:21 -0700125 private final BatteryController mBatteryController;
Adrian Roosff2c4562016-11-03 12:13:36 -0700126 private Part[] mParts;
127
128 private final ArrayList<State> mQueuedRequests = new ArrayList<>();
129 private State mState = State.UNINITIALIZED;
Adrian Roosd7b9d102017-04-28 15:42:58 -0700130 private int mPulseReason;
Adrian Roosff2c4562016-11-03 12:13:36 -0700131 private boolean mWakeLockHeldForCurrentState = false;
132
Adrian Roos0261fb22017-03-07 20:20:35 +0000133 public DozeMachine(Service service, AmbientDisplayConfiguration config,
Lucas Dupin6edeb182019-09-25 13:39:21 -0700134 WakeLock wakeLock, WakefulnessLifecycle wakefulnessLifecycle,
135 BatteryController batteryController) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700136 mDozeService = service;
Adrian Roos0261fb22017-03-07 20:20:35 +0000137 mConfig = config;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700138 mWakefulnessLifecycle = wakefulnessLifecycle;
Adrian Roosff2c4562016-11-03 12:13:36 -0700139 mWakeLock = wakeLock;
Lucas Dupin6edeb182019-09-25 13:39:21 -0700140 mBatteryController = batteryController;
Adrian Roosff2c4562016-11-03 12:13:36 -0700141 }
142
143 /** Initializes the set of {@link Part}s. Must be called exactly once after construction. */
144 public void setParts(Part[] parts) {
145 Preconditions.checkState(mParts == null);
146 mParts = parts;
147 }
148
149 /**
150 * Requests transitioning to {@code requestedState}.
151 *
152 * This can be called during a state transition, in which case it will be queued until all
153 * queued state transitions are done.
154 *
155 * A wake lock is held while the transition is happening.
156 *
157 * Note that {@link #transitionPolicy} can modify what state will be transitioned to.
158 */
159 @MainThread
160 public void requestState(State requestedState) {
Adrian Roosd7b9d102017-04-28 15:42:58 -0700161 Preconditions.checkArgument(requestedState != State.DOZE_REQUEST_PULSE);
162 requestState(requestedState, DozeLog.PULSE_REASON_NONE);
163 }
164
165 @MainThread
166 public void requestPulse(int pulseReason) {
167 // Must not be called during a transition. There's no inherent problem with that,
168 // but there's currently no need to execute from a transition and it simplifies the
169 // code to not have to worry about keeping the pulseReason in mQueuedRequests.
170 Preconditions.checkState(!isExecutingTransition());
171 requestState(State.DOZE_REQUEST_PULSE, pulseReason);
172 }
173
174 private void requestState(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700175 Assert.isMainThread();
176 if (DEBUG) {
177 Log.i(TAG, "request: current=" + mState + " req=" + requestedState,
178 new Throwable("here"));
179 }
180
181 boolean runNow = !isExecutingTransition();
182 mQueuedRequests.add(requestedState);
183 if (runNow) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800184 mWakeLock.acquire(REASON_CHANGE_STATE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700185 for (int i = 0; i < mQueuedRequests.size(); i++) {
186 // Transitions in Parts can call back into requestState, which will
187 // cause mQueuedRequests to grow.
Adrian Roosd7b9d102017-04-28 15:42:58 -0700188 transitionTo(mQueuedRequests.get(i), pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700189 }
190 mQueuedRequests.clear();
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800191 mWakeLock.release(REASON_CHANGE_STATE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700192 }
193 }
194
195 /**
196 * @return the current state.
197 *
198 * This must not be called during a transition.
199 */
200 @MainThread
201 public State getState() {
202 Assert.isMainThread();
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700203 if (isExecutingTransition()) {
204 throw new IllegalStateException("Cannot get state because there were pending "
205 + "transitions: " + mQueuedRequests.toString());
206 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700207 return mState;
208 }
209
Adrian Roosd7b9d102017-04-28 15:42:58 -0700210 /**
211 * @return the current pulse reason.
212 *
213 * This is only valid if the machine is currently in one of the pulse states.
214 */
215 @MainThread
216 public int getPulseReason() {
217 Assert.isMainThread();
218 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE
219 || mState == State.DOZE_PULSING
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700220 || mState == State.DOZE_PULSING_BRIGHT
Adrian Roosd7b9d102017-04-28 15:42:58 -0700221 || mState == State.DOZE_PULSE_DONE, "must be in pulsing state, but is " + mState);
222 return mPulseReason;
223 }
224
Adrian Roos4fb1f512017-02-14 14:01:32 +0100225 /** Requests the PowerManager to wake up now. */
226 public void wakeUp() {
227 mDozeService.requestWakeUp();
228 }
229
Lucas Dupin1946b312019-03-21 14:48:23 -0700230 public boolean isExecutingTransition() {
Adrian Roosff2c4562016-11-03 12:13:36 -0700231 return !mQueuedRequests.isEmpty();
232 }
233
Adrian Roosd7b9d102017-04-28 15:42:58 -0700234 private void transitionTo(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700235 State newState = transitionPolicy(requestedState);
236
237 if (DEBUG) {
238 Log.i(TAG, "transition: old=" + mState + " req=" + requestedState + " new=" + newState);
239 }
240
241 if (newState == mState) {
242 return;
243 }
244
245 validateTransition(newState);
246
247 State oldState = mState;
248 mState = newState;
249
Adrian Roosa6c03f82017-07-26 16:20:30 +0200250 DozeLog.traceState(newState);
251 Trace.traceCounter(Trace.TRACE_TAG_APP, "doze_machine_state", newState.ordinal());
252
Adrian Roosd7b9d102017-04-28 15:42:58 -0700253 updatePulseReason(newState, oldState, pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700254 performTransitionOnComponents(oldState, newState);
Adrian Roosff2c4562016-11-03 12:13:36 -0700255 updateWakeLockState(newState);
256
257 resolveIntermediateState(newState);
258 }
259
Adrian Roosd7b9d102017-04-28 15:42:58 -0700260 private void updatePulseReason(State newState, State oldState, int pulseReason) {
261 if (newState == State.DOZE_REQUEST_PULSE) {
262 mPulseReason = pulseReason;
263 } else if (oldState == State.DOZE_PULSE_DONE) {
264 mPulseReason = DozeLog.PULSE_REASON_NONE;
265 }
266 }
267
Adrian Roosff2c4562016-11-03 12:13:36 -0700268 private void performTransitionOnComponents(State oldState, State newState) {
269 for (Part p : mParts) {
270 p.transitionTo(oldState, newState);
271 }
272
273 switch (newState) {
274 case FINISH:
275 mDozeService.finish();
276 break;
277 default:
278 }
279 }
280
281 private void validateTransition(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800282 try {
283 switch (mState) {
284 case FINISH:
285 Preconditions.checkState(newState == State.FINISH);
286 break;
287 case UNINITIALIZED:
288 Preconditions.checkState(newState == State.INITIALIZED);
289 break;
290 }
291 switch (newState) {
292 case UNINITIALIZED:
293 throw new IllegalArgumentException("can't transition to UNINITIALIZED");
294 case INITIALIZED:
295 Preconditions.checkState(mState == State.UNINITIALIZED);
296 break;
297 case DOZE_PULSING:
298 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE);
299 break;
300 case DOZE_PULSE_DONE:
Adrian Rooscd139a62016-12-16 12:23:51 -0800301 Preconditions.checkState(
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700302 mState == State.DOZE_REQUEST_PULSE || mState == State.DOZE_PULSING
303 || mState == State.DOZE_PULSING_BRIGHT);
Adrian Roosb84dc182016-12-02 09:01:09 -0800304 break;
305 default:
306 break;
307 }
308 } catch (RuntimeException e) {
309 throw new IllegalStateException("Illegal Transition: " + mState + " -> " + newState, e);
Adrian Roosff2c4562016-11-03 12:13:36 -0700310 }
311 }
312
313 private State transitionPolicy(State requestedState) {
314 if (mState == State.FINISH) {
315 return State.FINISH;
316 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200317 if ((mState == State.DOZE_AOD_PAUSED || mState == State.DOZE_AOD_PAUSING
318 || mState == State.DOZE_AOD || mState == State.DOZE)
Adrian Roos67cca742017-04-13 16:52:51 -0700319 && requestedState == State.DOZE_PULSE_DONE) {
320 Log.i(TAG, "Dropping pulse done because current state is already done: " + mState);
321 return mState;
322 }
Lucas Dupin6edeb182019-09-25 13:39:21 -0700323 if (requestedState == State.DOZE_AOD && mBatteryController.isAodPowerSave()) {
324 return State.DOZE;
325 }
Adrian Roosb84dc182016-12-02 09:01:09 -0800326 if (requestedState == State.DOZE_REQUEST_PULSE && !mState.canPulse()) {
327 Log.i(TAG, "Dropping pulse request because current state can't pulse: " + mState);
328 return mState;
329 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700330 return requestedState;
331 }
332
333 private void updateWakeLockState(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800334 boolean staysAwake = newState.staysAwake();
335 if (mWakeLockHeldForCurrentState && !staysAwake) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800336 mWakeLock.release(REASON_HELD_FOR_STATE);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800337 mWakeLockHeldForCurrentState = false;
Adrian Roosb84dc182016-12-02 09:01:09 -0800338 } else if (!mWakeLockHeldForCurrentState && staysAwake) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800339 mWakeLock.acquire(REASON_HELD_FOR_STATE);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800340 mWakeLockHeldForCurrentState = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700341 }
342 }
343
Adrian Roosff2c4562016-11-03 12:13:36 -0700344 private void resolveIntermediateState(State state) {
345 switch (state) {
346 case INITIALIZED:
347 case DOZE_PULSE_DONE:
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700348 final State nextState;
349 @Wakefulness int wakefulness = mWakefulnessLifecycle.getWakefulness();
350 if (wakefulness == WakefulnessLifecycle.WAKEFULNESS_AWAKE
351 || wakefulness == WakefulnessLifecycle.WAKEFULNESS_WAKING) {
352 nextState = State.FINISH;
353 } else if (mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
354 nextState = State.DOZE_AOD;
355 } else {
356 nextState = State.DOZE;
357 }
358
359 transitionTo(nextState, DozeLog.PULSE_REASON_NONE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700360 break;
361 default:
362 break;
363 }
364 }
365
366 /** Dumps the current state */
367 public void dump(PrintWriter pw) {
368 pw.print(" state="); pw.println(mState);
369 pw.print(" wakeLockHeldForCurrentState="); pw.println(mWakeLockHeldForCurrentState);
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800370 pw.print(" wakeLock="); pw.println(mWakeLock);
Adrian Roosff2c4562016-11-03 12:13:36 -0700371 pw.println("Parts:");
372 for (Part p : mParts) {
373 p.dump(pw);
374 }
375 }
376
377 /** A part of the DozeMachine that needs to be notified about state changes. */
378 public interface Part {
379 /**
380 * Transition from {@code oldState} to {@code newState}.
381 *
382 * This method is guaranteed to only be called while a wake lock is held.
383 */
384 void transitionTo(State oldState, State newState);
385
386 /** Dump current state. For debugging only. */
387 default void dump(PrintWriter pw) {}
388 }
389
390 /** A wrapper interface for {@link android.service.dreams.DreamService} */
391 public interface Service {
392 /** Finish dreaming. */
393 void finish();
394
395 /** Request a display state. See {@link android.view.Display#STATE_DOZE}. */
396 void setDozeScreenState(int state);
Adrian Roos4fb1f512017-02-14 14:01:32 +0100397
398 /** Request waking up. */
399 void requestWakeUp();
Adrian Roos2981eb02017-05-26 18:40:09 -0700400
401 /** Set screen brightness */
402 void setDozeScreenBrightness(int brightness);
403
404 class Delegate implements Service {
405 private final Service mDelegate;
406
407 public Delegate(Service delegate) {
408 mDelegate = delegate;
409 }
410
411 @Override
412 public void finish() {
413 mDelegate.finish();
414 }
415
416 @Override
417 public void setDozeScreenState(int state) {
418 mDelegate.setDozeScreenState(state);
419 }
420
421 @Override
422 public void requestWakeUp() {
423 mDelegate.requestWakeUp();
424 }
425
426 @Override
427 public void setDozeScreenBrightness(int brightness) {
428 mDelegate.setDozeScreenBrightness(brightness);
429 }
430 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700431 }
432}