blob: 93a51cc20db2a6970bc6264c3bb9cb294c76a0c5 [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;
Adrian Roosff2c4562016-11-03 12:13:36 -070030import com.android.systemui.util.Assert;
Adrian Roosc1b50322017-02-27 21:07:58 +010031import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070032
33import java.io.PrintWriter;
34import java.util.ArrayList;
35
36/**
37 * Orchestrates all things doze.
38 *
39 * DozeMachine implements a state machine that orchestrates how the UI and triggers work and
40 * interfaces with the power and screen states.
41 *
42 * During state transitions and in certain states, DozeMachine holds a wake lock.
43 */
44public class DozeMachine {
45
46 static final String TAG = "DozeMachine";
47 static final boolean DEBUG = DozeService.DEBUG;
Lucas Dupinee4c9b72019-02-18 17:04:58 -080048 private static final String REASON_CHANGE_STATE = "DozeMachine#requestState";
49 private static final String REASON_HELD_FOR_STATE = "DozeMachine#heldForState";
Adrian Roosff2c4562016-11-03 12:13:36 -070050
Adrian Roosd35d4ca2017-04-19 14:31:03 -070051 public enum State {
Adrian Roosff2c4562016-11-03 12:13:36 -070052 /** Default state. Transition to INITIALIZED to get Doze going. */
53 UNINITIALIZED,
54 /** Doze components are set up. Followed by transition to DOZE or DOZE_AOD. */
55 INITIALIZED,
56 /** Regular doze. Device is asleep and listening for pulse triggers. */
57 DOZE,
58 /** Always-on doze. Device is asleep, showing UI and listening for pulse triggers. */
59 DOZE_AOD,
60 /** Pulse has been requested. Device is awake and preparing UI */
61 DOZE_REQUEST_PULSE,
62 /** Pulse is showing. Device is awake and showing UI. */
63 DOZE_PULSING,
Lucas Dupin5f00fa52019-03-27 22:46:53 -070064 /** Pulse is showing with bright wallpaper. Device is awake and showing UI. */
65 DOZE_PULSING_BRIGHT,
Adrian Roosff2c4562016-11-03 12:13:36 -070066 /** Pulse is done showing. Followed by transition to DOZE or DOZE_AOD. */
67 DOZE_PULSE_DONE,
68 /** Doze is done. DozeService is finished. */
Adrian Roos67cca742017-04-13 16:52:51 -070069 FINISH,
70 /** AOD, but the display is temporarily off. */
Adrian Roos6023ccb2017-06-28 16:22:02 +020071 DOZE_AOD_PAUSED,
72 /** AOD, prox is near, transitions to DOZE_AOD_PAUSED after a timeout. */
73 DOZE_AOD_PAUSING;
Adrian Roosb84dc182016-12-02 09:01:09 -080074
75 boolean canPulse() {
76 switch (this) {
77 case DOZE:
78 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -070079 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +020080 case DOZE_AOD_PAUSING:
Adrian Roosb84dc182016-12-02 09:01:09 -080081 return true;
82 default:
83 return false;
84 }
85 }
86
87 boolean staysAwake() {
88 switch (this) {
89 case DOZE_REQUEST_PULSE:
90 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -070091 case DOZE_PULSING_BRIGHT:
Adrian Roosb84dc182016-12-02 09:01:09 -080092 return true;
93 default:
94 return false;
95 }
96 }
97
Lucas Dupin12663d32018-02-08 11:42:22 -080098 int screenState(DozeParameters parameters) {
Adrian Roosb84dc182016-12-02 09:01:09 -080099 switch (this) {
100 case UNINITIALIZED:
101 case INITIALIZED:
Lucas Dupin16cfe452018-02-08 13:14:50 -0800102 case DOZE_REQUEST_PULSE:
103 return parameters.shouldControlScreenOff() ? Display.STATE_ON
104 : Display.STATE_OFF;
Adrian Roos67cca742017-04-13 16:52:51 -0700105 case DOZE_AOD_PAUSED:
Lucas Dupin16cfe452018-02-08 13:14:50 -0800106 case DOZE:
Adrian Roosb84dc182016-12-02 09:01:09 -0800107 return Display.STATE_OFF;
108 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700109 case DOZE_PULSING_BRIGHT:
Adrian Roos2324d852017-04-27 15:50:14 -0700110 return Display.STATE_ON;
Adrian Roosb84dc182016-12-02 09:01:09 -0800111 case DOZE_AOD:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200112 case DOZE_AOD_PAUSING:
Adrian Roosa1e6b312017-03-28 16:20:34 -0700113 return Display.STATE_DOZE_SUSPEND;
Adrian Roosb84dc182016-12-02 09:01:09 -0800114 default:
115 return Display.STATE_UNKNOWN;
116 }
117 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700118 }
119
120 private final Service mDozeService;
Adrian Roosc1b50322017-02-27 21:07:58 +0100121 private final WakeLock mWakeLock;
Adrian Roos0261fb22017-03-07 20:20:35 +0000122 private final AmbientDisplayConfiguration mConfig;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700123 private final WakefulnessLifecycle mWakefulnessLifecycle;
Adrian Roosff2c4562016-11-03 12:13:36 -0700124 private Part[] mParts;
125
126 private final ArrayList<State> mQueuedRequests = new ArrayList<>();
127 private State mState = State.UNINITIALIZED;
Adrian Roosd7b9d102017-04-28 15:42:58 -0700128 private int mPulseReason;
Adrian Roosff2c4562016-11-03 12:13:36 -0700129 private boolean mWakeLockHeldForCurrentState = false;
130
Adrian Roos0261fb22017-03-07 20:20:35 +0000131 public DozeMachine(Service service, AmbientDisplayConfiguration config,
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700132 WakeLock wakeLock, WakefulnessLifecycle wakefulnessLifecycle) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700133 mDozeService = service;
Adrian Roos0261fb22017-03-07 20:20:35 +0000134 mConfig = config;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700135 mWakefulnessLifecycle = wakefulnessLifecycle;
Adrian Roosff2c4562016-11-03 12:13:36 -0700136 mWakeLock = wakeLock;
137 }
138
139 /** Initializes the set of {@link Part}s. Must be called exactly once after construction. */
140 public void setParts(Part[] parts) {
141 Preconditions.checkState(mParts == null);
142 mParts = parts;
143 }
144
145 /**
146 * Requests transitioning to {@code requestedState}.
147 *
148 * This can be called during a state transition, in which case it will be queued until all
149 * queued state transitions are done.
150 *
151 * A wake lock is held while the transition is happening.
152 *
153 * Note that {@link #transitionPolicy} can modify what state will be transitioned to.
154 */
155 @MainThread
156 public void requestState(State requestedState) {
Adrian Roosd7b9d102017-04-28 15:42:58 -0700157 Preconditions.checkArgument(requestedState != State.DOZE_REQUEST_PULSE);
158 requestState(requestedState, DozeLog.PULSE_REASON_NONE);
159 }
160
161 @MainThread
162 public void requestPulse(int pulseReason) {
163 // Must not be called during a transition. There's no inherent problem with that,
164 // but there's currently no need to execute from a transition and it simplifies the
165 // code to not have to worry about keeping the pulseReason in mQueuedRequests.
166 Preconditions.checkState(!isExecutingTransition());
167 requestState(State.DOZE_REQUEST_PULSE, pulseReason);
168 }
169
170 private void requestState(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700171 Assert.isMainThread();
172 if (DEBUG) {
173 Log.i(TAG, "request: current=" + mState + " req=" + requestedState,
174 new Throwable("here"));
175 }
176
177 boolean runNow = !isExecutingTransition();
178 mQueuedRequests.add(requestedState);
179 if (runNow) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800180 mWakeLock.acquire(REASON_CHANGE_STATE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700181 for (int i = 0; i < mQueuedRequests.size(); i++) {
182 // Transitions in Parts can call back into requestState, which will
183 // cause mQueuedRequests to grow.
Adrian Roosd7b9d102017-04-28 15:42:58 -0700184 transitionTo(mQueuedRequests.get(i), pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700185 }
186 mQueuedRequests.clear();
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800187 mWakeLock.release(REASON_CHANGE_STATE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700188 }
189 }
190
191 /**
192 * @return the current state.
193 *
194 * This must not be called during a transition.
195 */
196 @MainThread
197 public State getState() {
198 Assert.isMainThread();
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700199 if (isExecutingTransition()) {
200 throw new IllegalStateException("Cannot get state because there were pending "
201 + "transitions: " + mQueuedRequests.toString());
202 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700203 return mState;
204 }
205
Adrian Roosd7b9d102017-04-28 15:42:58 -0700206 /**
207 * @return the current pulse reason.
208 *
209 * This is only valid if the machine is currently in one of the pulse states.
210 */
211 @MainThread
212 public int getPulseReason() {
213 Assert.isMainThread();
214 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE
215 || mState == State.DOZE_PULSING
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700216 || mState == State.DOZE_PULSING_BRIGHT
Adrian Roosd7b9d102017-04-28 15:42:58 -0700217 || mState == State.DOZE_PULSE_DONE, "must be in pulsing state, but is " + mState);
218 return mPulseReason;
219 }
220
Adrian Roos4fb1f512017-02-14 14:01:32 +0100221 /** Requests the PowerManager to wake up now. */
222 public void wakeUp() {
223 mDozeService.requestWakeUp();
224 }
225
Lucas Dupin1946b312019-03-21 14:48:23 -0700226 public boolean isExecutingTransition() {
Adrian Roosff2c4562016-11-03 12:13:36 -0700227 return !mQueuedRequests.isEmpty();
228 }
229
Adrian Roosd7b9d102017-04-28 15:42:58 -0700230 private void transitionTo(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700231 State newState = transitionPolicy(requestedState);
232
233 if (DEBUG) {
234 Log.i(TAG, "transition: old=" + mState + " req=" + requestedState + " new=" + newState);
235 }
236
237 if (newState == mState) {
238 return;
239 }
240
241 validateTransition(newState);
242
243 State oldState = mState;
244 mState = newState;
245
Adrian Roosa6c03f82017-07-26 16:20:30 +0200246 DozeLog.traceState(newState);
247 Trace.traceCounter(Trace.TRACE_TAG_APP, "doze_machine_state", newState.ordinal());
248
Adrian Roosd7b9d102017-04-28 15:42:58 -0700249 updatePulseReason(newState, oldState, pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700250 performTransitionOnComponents(oldState, newState);
Adrian Roosff2c4562016-11-03 12:13:36 -0700251 updateWakeLockState(newState);
252
253 resolveIntermediateState(newState);
254 }
255
Adrian Roosd7b9d102017-04-28 15:42:58 -0700256 private void updatePulseReason(State newState, State oldState, int pulseReason) {
257 if (newState == State.DOZE_REQUEST_PULSE) {
258 mPulseReason = pulseReason;
259 } else if (oldState == State.DOZE_PULSE_DONE) {
260 mPulseReason = DozeLog.PULSE_REASON_NONE;
261 }
262 }
263
Adrian Roosff2c4562016-11-03 12:13:36 -0700264 private void performTransitionOnComponents(State oldState, State newState) {
265 for (Part p : mParts) {
266 p.transitionTo(oldState, newState);
267 }
268
269 switch (newState) {
270 case FINISH:
271 mDozeService.finish();
272 break;
273 default:
274 }
275 }
276
277 private void validateTransition(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800278 try {
279 switch (mState) {
280 case FINISH:
281 Preconditions.checkState(newState == State.FINISH);
282 break;
283 case UNINITIALIZED:
284 Preconditions.checkState(newState == State.INITIALIZED);
285 break;
286 }
287 switch (newState) {
288 case UNINITIALIZED:
289 throw new IllegalArgumentException("can't transition to UNINITIALIZED");
290 case INITIALIZED:
291 Preconditions.checkState(mState == State.UNINITIALIZED);
292 break;
293 case DOZE_PULSING:
294 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE);
295 break;
296 case DOZE_PULSE_DONE:
Adrian Rooscd139a62016-12-16 12:23:51 -0800297 Preconditions.checkState(
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700298 mState == State.DOZE_REQUEST_PULSE || mState == State.DOZE_PULSING
299 || mState == State.DOZE_PULSING_BRIGHT);
Adrian Roosb84dc182016-12-02 09:01:09 -0800300 break;
301 default:
302 break;
303 }
304 } catch (RuntimeException e) {
305 throw new IllegalStateException("Illegal Transition: " + mState + " -> " + newState, e);
Adrian Roosff2c4562016-11-03 12:13:36 -0700306 }
307 }
308
309 private State transitionPolicy(State requestedState) {
310 if (mState == State.FINISH) {
311 return State.FINISH;
312 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200313 if ((mState == State.DOZE_AOD_PAUSED || mState == State.DOZE_AOD_PAUSING
314 || mState == State.DOZE_AOD || mState == State.DOZE)
Adrian Roos67cca742017-04-13 16:52:51 -0700315 && requestedState == State.DOZE_PULSE_DONE) {
316 Log.i(TAG, "Dropping pulse done because current state is already done: " + mState);
317 return mState;
318 }
Adrian Roosb84dc182016-12-02 09:01:09 -0800319 if (requestedState == State.DOZE_REQUEST_PULSE && !mState.canPulse()) {
320 Log.i(TAG, "Dropping pulse request because current state can't pulse: " + mState);
321 return mState;
322 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700323 return requestedState;
324 }
325
326 private void updateWakeLockState(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800327 boolean staysAwake = newState.staysAwake();
328 if (mWakeLockHeldForCurrentState && !staysAwake) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800329 mWakeLock.release(REASON_HELD_FOR_STATE);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800330 mWakeLockHeldForCurrentState = false;
Adrian Roosb84dc182016-12-02 09:01:09 -0800331 } else if (!mWakeLockHeldForCurrentState && staysAwake) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800332 mWakeLock.acquire(REASON_HELD_FOR_STATE);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800333 mWakeLockHeldForCurrentState = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700334 }
335 }
336
Adrian Roosff2c4562016-11-03 12:13:36 -0700337 private void resolveIntermediateState(State state) {
338 switch (state) {
339 case INITIALIZED:
340 case DOZE_PULSE_DONE:
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700341 final State nextState;
342 @Wakefulness int wakefulness = mWakefulnessLifecycle.getWakefulness();
343 if (wakefulness == WakefulnessLifecycle.WAKEFULNESS_AWAKE
344 || wakefulness == WakefulnessLifecycle.WAKEFULNESS_WAKING) {
345 nextState = State.FINISH;
346 } else if (mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
347 nextState = State.DOZE_AOD;
348 } else {
349 nextState = State.DOZE;
350 }
351
352 transitionTo(nextState, DozeLog.PULSE_REASON_NONE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700353 break;
354 default:
355 break;
356 }
357 }
358
359 /** Dumps the current state */
360 public void dump(PrintWriter pw) {
361 pw.print(" state="); pw.println(mState);
362 pw.print(" wakeLockHeldForCurrentState="); pw.println(mWakeLockHeldForCurrentState);
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800363 pw.print(" wakeLock="); pw.println(mWakeLock);
Adrian Roosff2c4562016-11-03 12:13:36 -0700364 pw.println("Parts:");
365 for (Part p : mParts) {
366 p.dump(pw);
367 }
368 }
369
370 /** A part of the DozeMachine that needs to be notified about state changes. */
371 public interface Part {
372 /**
373 * Transition from {@code oldState} to {@code newState}.
374 *
375 * This method is guaranteed to only be called while a wake lock is held.
376 */
377 void transitionTo(State oldState, State newState);
378
379 /** Dump current state. For debugging only. */
380 default void dump(PrintWriter pw) {}
381 }
382
383 /** A wrapper interface for {@link android.service.dreams.DreamService} */
384 public interface Service {
385 /** Finish dreaming. */
386 void finish();
387
388 /** Request a display state. See {@link android.view.Display#STATE_DOZE}. */
389 void setDozeScreenState(int state);
Adrian Roos4fb1f512017-02-14 14:01:32 +0100390
391 /** Request waking up. */
392 void requestWakeUp();
Adrian Roos2981eb02017-05-26 18:40:09 -0700393
394 /** Set screen brightness */
395 void setDozeScreenBrightness(int brightness);
396
397 class Delegate implements Service {
398 private final Service mDelegate;
399
400 public Delegate(Service delegate) {
401 mDelegate = delegate;
402 }
403
404 @Override
405 public void finish() {
406 mDelegate.finish();
407 }
408
409 @Override
410 public void setDozeScreenState(int state) {
411 mDelegate.setDozeScreenState(state);
412 }
413
414 @Override
415 public void requestWakeUp() {
416 mDelegate.requestWakeUp();
417 }
418
419 @Override
420 public void setDozeScreenBrightness(int brightness) {
421 mDelegate.setDozeScreenBrightness(brightness);
422 }
423 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700424 }
425}