blob: 152b9fc3db4d92581e93c0796bc5016632adfeec [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;
Adrian Roosa6c03f82017-07-26 16:20:30 +020020import android.os.Trace;
Adrian Roos0261fb22017-03-07 20:20:35 +000021import android.os.UserHandle;
Adrian Roosff2c4562016-11-03 12:13:36 -070022import android.util.Log;
23import android.view.Display;
24
Adrian Roos0261fb22017-03-07 20:20:35 +000025import com.android.internal.hardware.AmbientDisplayConfiguration;
Adrian Roosff2c4562016-11-03 12:13:36 -070026import com.android.internal.util.Preconditions;
Lucas Dupin12663d32018-02-08 11:42:22 -080027import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roosff2c4562016-11-03 12:13:36 -070028import com.android.systemui.util.Assert;
Adrian Roosc1b50322017-02-27 21:07:58 +010029import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070030
31import java.io.PrintWriter;
32import java.util.ArrayList;
33
34/**
35 * Orchestrates all things doze.
36 *
37 * DozeMachine implements a state machine that orchestrates how the UI and triggers work and
38 * interfaces with the power and screen states.
39 *
40 * During state transitions and in certain states, DozeMachine holds a wake lock.
41 */
42public class DozeMachine {
43
44 static final String TAG = "DozeMachine";
45 static final boolean DEBUG = DozeService.DEBUG;
46
Adrian Roosd35d4ca2017-04-19 14:31:03 -070047 public enum State {
Adrian Roosff2c4562016-11-03 12:13:36 -070048 /** Default state. Transition to INITIALIZED to get Doze going. */
49 UNINITIALIZED,
50 /** Doze components are set up. Followed by transition to DOZE or DOZE_AOD. */
51 INITIALIZED,
52 /** Regular doze. Device is asleep and listening for pulse triggers. */
53 DOZE,
54 /** Always-on doze. Device is asleep, showing UI and listening for pulse triggers. */
55 DOZE_AOD,
56 /** Pulse has been requested. Device is awake and preparing UI */
57 DOZE_REQUEST_PULSE,
58 /** Pulse is showing. Device is awake and showing UI. */
59 DOZE_PULSING,
60 /** Pulse is done showing. Followed by transition to DOZE or DOZE_AOD. */
61 DOZE_PULSE_DONE,
62 /** Doze is done. DozeService is finished. */
Adrian Roos67cca742017-04-13 16:52:51 -070063 FINISH,
64 /** AOD, but the display is temporarily off. */
Adrian Roos6023ccb2017-06-28 16:22:02 +020065 DOZE_AOD_PAUSED,
66 /** AOD, prox is near, transitions to DOZE_AOD_PAUSED after a timeout. */
67 DOZE_AOD_PAUSING;
Adrian Roosb84dc182016-12-02 09:01:09 -080068
69 boolean canPulse() {
70 switch (this) {
71 case DOZE:
72 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -070073 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +020074 case DOZE_AOD_PAUSING:
Adrian Roosb84dc182016-12-02 09:01:09 -080075 return true;
76 default:
77 return false;
78 }
79 }
80
81 boolean staysAwake() {
82 switch (this) {
83 case DOZE_REQUEST_PULSE:
84 case DOZE_PULSING:
85 return true;
86 default:
87 return false;
88 }
89 }
90
Lucas Dupin12663d32018-02-08 11:42:22 -080091 int screenState(DozeParameters parameters) {
Adrian Roosb84dc182016-12-02 09:01:09 -080092 switch (this) {
93 case UNINITIALIZED:
94 case INITIALIZED:
Lucas Dupin16cfe452018-02-08 13:14:50 -080095 case DOZE_REQUEST_PULSE:
96 return parameters.shouldControlScreenOff() ? Display.STATE_ON
97 : Display.STATE_OFF;
Adrian Roos67cca742017-04-13 16:52:51 -070098 case DOZE_AOD_PAUSED:
Lucas Dupin16cfe452018-02-08 13:14:50 -080099 case DOZE:
Adrian Roosb84dc182016-12-02 09:01:09 -0800100 return Display.STATE_OFF;
101 case DOZE_PULSING:
Adrian Roos2324d852017-04-27 15:50:14 -0700102 return Display.STATE_ON;
Adrian Roosb84dc182016-12-02 09:01:09 -0800103 case DOZE_AOD:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200104 case DOZE_AOD_PAUSING:
Adrian Roosa1e6b312017-03-28 16:20:34 -0700105 return Display.STATE_DOZE_SUSPEND;
Adrian Roosb84dc182016-12-02 09:01:09 -0800106 default:
107 return Display.STATE_UNKNOWN;
108 }
109 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700110 }
111
112 private final Service mDozeService;
Adrian Roosc1b50322017-02-27 21:07:58 +0100113 private final WakeLock mWakeLock;
Adrian Roos0261fb22017-03-07 20:20:35 +0000114 private final AmbientDisplayConfiguration mConfig;
Adrian Roosff2c4562016-11-03 12:13:36 -0700115 private Part[] mParts;
116
117 private final ArrayList<State> mQueuedRequests = new ArrayList<>();
118 private State mState = State.UNINITIALIZED;
Adrian Roosd7b9d102017-04-28 15:42:58 -0700119 private int mPulseReason;
Adrian Roosff2c4562016-11-03 12:13:36 -0700120 private boolean mWakeLockHeldForCurrentState = false;
121
Adrian Roos0261fb22017-03-07 20:20:35 +0000122 public DozeMachine(Service service, AmbientDisplayConfiguration config,
Adrian Roosc1b50322017-02-27 21:07:58 +0100123 WakeLock wakeLock) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700124 mDozeService = service;
Adrian Roos0261fb22017-03-07 20:20:35 +0000125 mConfig = config;
Adrian Roosff2c4562016-11-03 12:13:36 -0700126 mWakeLock = wakeLock;
127 }
128
129 /** Initializes the set of {@link Part}s. Must be called exactly once after construction. */
130 public void setParts(Part[] parts) {
131 Preconditions.checkState(mParts == null);
132 mParts = parts;
133 }
134
135 /**
136 * Requests transitioning to {@code requestedState}.
137 *
138 * This can be called during a state transition, in which case it will be queued until all
139 * queued state transitions are done.
140 *
141 * A wake lock is held while the transition is happening.
142 *
143 * Note that {@link #transitionPolicy} can modify what state will be transitioned to.
144 */
145 @MainThread
146 public void requestState(State requestedState) {
Adrian Roosd7b9d102017-04-28 15:42:58 -0700147 Preconditions.checkArgument(requestedState != State.DOZE_REQUEST_PULSE);
148 requestState(requestedState, DozeLog.PULSE_REASON_NONE);
149 }
150
151 @MainThread
152 public void requestPulse(int pulseReason) {
153 // Must not be called during a transition. There's no inherent problem with that,
154 // but there's currently no need to execute from a transition and it simplifies the
155 // code to not have to worry about keeping the pulseReason in mQueuedRequests.
156 Preconditions.checkState(!isExecutingTransition());
157 requestState(State.DOZE_REQUEST_PULSE, pulseReason);
158 }
159
160 private void requestState(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700161 Assert.isMainThread();
162 if (DEBUG) {
163 Log.i(TAG, "request: current=" + mState + " req=" + requestedState,
164 new Throwable("here"));
165 }
166
167 boolean runNow = !isExecutingTransition();
168 mQueuedRequests.add(requestedState);
169 if (runNow) {
170 mWakeLock.acquire();
171 for (int i = 0; i < mQueuedRequests.size(); i++) {
172 // Transitions in Parts can call back into requestState, which will
173 // cause mQueuedRequests to grow.
Adrian Roosd7b9d102017-04-28 15:42:58 -0700174 transitionTo(mQueuedRequests.get(i), pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700175 }
176 mQueuedRequests.clear();
177 mWakeLock.release();
178 }
179 }
180
181 /**
182 * @return the current state.
183 *
184 * This must not be called during a transition.
185 */
186 @MainThread
187 public State getState() {
188 Assert.isMainThread();
189 Preconditions.checkState(!isExecutingTransition());
190 return mState;
191 }
192
Adrian Roosd7b9d102017-04-28 15:42:58 -0700193 /**
194 * @return the current pulse reason.
195 *
196 * This is only valid if the machine is currently in one of the pulse states.
197 */
198 @MainThread
199 public int getPulseReason() {
200 Assert.isMainThread();
201 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE
202 || mState == State.DOZE_PULSING
203 || mState == State.DOZE_PULSE_DONE, "must be in pulsing state, but is " + mState);
204 return mPulseReason;
205 }
206
Adrian Roos4fb1f512017-02-14 14:01:32 +0100207 /** Requests the PowerManager to wake up now. */
208 public void wakeUp() {
209 mDozeService.requestWakeUp();
210 }
211
Adrian Roosff2c4562016-11-03 12:13:36 -0700212 private boolean isExecutingTransition() {
213 return !mQueuedRequests.isEmpty();
214 }
215
Adrian Roosd7b9d102017-04-28 15:42:58 -0700216 private void transitionTo(State requestedState, int pulseReason) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700217 State newState = transitionPolicy(requestedState);
218
219 if (DEBUG) {
220 Log.i(TAG, "transition: old=" + mState + " req=" + requestedState + " new=" + newState);
221 }
222
223 if (newState == mState) {
224 return;
225 }
226
227 validateTransition(newState);
228
229 State oldState = mState;
230 mState = newState;
231
Adrian Roosa6c03f82017-07-26 16:20:30 +0200232 DozeLog.traceState(newState);
233 Trace.traceCounter(Trace.TRACE_TAG_APP, "doze_machine_state", newState.ordinal());
234
Adrian Roosd7b9d102017-04-28 15:42:58 -0700235 updatePulseReason(newState, oldState, pulseReason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700236 performTransitionOnComponents(oldState, newState);
Adrian Roosff2c4562016-11-03 12:13:36 -0700237 updateWakeLockState(newState);
238
239 resolveIntermediateState(newState);
240 }
241
Adrian Roosd7b9d102017-04-28 15:42:58 -0700242 private void updatePulseReason(State newState, State oldState, int pulseReason) {
243 if (newState == State.DOZE_REQUEST_PULSE) {
244 mPulseReason = pulseReason;
245 } else if (oldState == State.DOZE_PULSE_DONE) {
246 mPulseReason = DozeLog.PULSE_REASON_NONE;
247 }
248 }
249
Adrian Roosff2c4562016-11-03 12:13:36 -0700250 private void performTransitionOnComponents(State oldState, State newState) {
251 for (Part p : mParts) {
252 p.transitionTo(oldState, newState);
253 }
254
255 switch (newState) {
256 case FINISH:
257 mDozeService.finish();
258 break;
259 default:
260 }
261 }
262
263 private void validateTransition(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800264 try {
265 switch (mState) {
266 case FINISH:
267 Preconditions.checkState(newState == State.FINISH);
268 break;
269 case UNINITIALIZED:
270 Preconditions.checkState(newState == State.INITIALIZED);
271 break;
272 }
273 switch (newState) {
274 case UNINITIALIZED:
275 throw new IllegalArgumentException("can't transition to UNINITIALIZED");
276 case INITIALIZED:
277 Preconditions.checkState(mState == State.UNINITIALIZED);
278 break;
279 case DOZE_PULSING:
280 Preconditions.checkState(mState == State.DOZE_REQUEST_PULSE);
281 break;
282 case DOZE_PULSE_DONE:
Adrian Rooscd139a62016-12-16 12:23:51 -0800283 Preconditions.checkState(
284 mState == State.DOZE_REQUEST_PULSE || mState == State.DOZE_PULSING);
Adrian Roosb84dc182016-12-02 09:01:09 -0800285 break;
286 default:
287 break;
288 }
289 } catch (RuntimeException e) {
290 throw new IllegalStateException("Illegal Transition: " + mState + " -> " + newState, e);
Adrian Roosff2c4562016-11-03 12:13:36 -0700291 }
292 }
293
294 private State transitionPolicy(State requestedState) {
295 if (mState == State.FINISH) {
296 return State.FINISH;
297 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200298 if ((mState == State.DOZE_AOD_PAUSED || mState == State.DOZE_AOD_PAUSING
299 || mState == State.DOZE_AOD || mState == State.DOZE)
Adrian Roos67cca742017-04-13 16:52:51 -0700300 && requestedState == State.DOZE_PULSE_DONE) {
301 Log.i(TAG, "Dropping pulse done because current state is already done: " + mState);
302 return mState;
303 }
Adrian Roosb84dc182016-12-02 09:01:09 -0800304 if (requestedState == State.DOZE_REQUEST_PULSE && !mState.canPulse()) {
305 Log.i(TAG, "Dropping pulse request because current state can't pulse: " + mState);
306 return mState;
307 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700308 return requestedState;
309 }
310
311 private void updateWakeLockState(State newState) {
Adrian Roosb84dc182016-12-02 09:01:09 -0800312 boolean staysAwake = newState.staysAwake();
313 if (mWakeLockHeldForCurrentState && !staysAwake) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700314 mWakeLock.release();
Adrian Roosae0c5e82016-11-16 19:56:19 -0800315 mWakeLockHeldForCurrentState = false;
Adrian Roosb84dc182016-12-02 09:01:09 -0800316 } else if (!mWakeLockHeldForCurrentState && staysAwake) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700317 mWakeLock.acquire();
Adrian Roosae0c5e82016-11-16 19:56:19 -0800318 mWakeLockHeldForCurrentState = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700319 }
320 }
321
Adrian Roosff2c4562016-11-03 12:13:36 -0700322 private void resolveIntermediateState(State state) {
323 switch (state) {
324 case INITIALIZED:
325 case DOZE_PULSE_DONE:
Adrian Roos0261fb22017-03-07 20:20:35 +0000326 transitionTo(mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)
Adrian Roosd7b9d102017-04-28 15:42:58 -0700327 ? DozeMachine.State.DOZE_AOD : DozeMachine.State.DOZE,
328 DozeLog.PULSE_REASON_NONE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700329 break;
330 default:
331 break;
332 }
333 }
334
335 /** Dumps the current state */
336 public void dump(PrintWriter pw) {
337 pw.print(" state="); pw.println(mState);
338 pw.print(" wakeLockHeldForCurrentState="); pw.println(mWakeLockHeldForCurrentState);
339 pw.println("Parts:");
340 for (Part p : mParts) {
341 p.dump(pw);
342 }
343 }
344
345 /** A part of the DozeMachine that needs to be notified about state changes. */
346 public interface Part {
347 /**
348 * Transition from {@code oldState} to {@code newState}.
349 *
350 * This method is guaranteed to only be called while a wake lock is held.
351 */
352 void transitionTo(State oldState, State newState);
353
354 /** Dump current state. For debugging only. */
355 default void dump(PrintWriter pw) {}
356 }
357
358 /** A wrapper interface for {@link android.service.dreams.DreamService} */
359 public interface Service {
360 /** Finish dreaming. */
361 void finish();
362
363 /** Request a display state. See {@link android.view.Display#STATE_DOZE}. */
364 void setDozeScreenState(int state);
Adrian Roos4fb1f512017-02-14 14:01:32 +0100365
366 /** Request waking up. */
367 void requestWakeUp();
Adrian Roos2981eb02017-05-26 18:40:09 -0700368
369 /** Set screen brightness */
370 void setDozeScreenBrightness(int brightness);
371
372 class Delegate implements Service {
373 private final Service mDelegate;
374
375 public Delegate(Service delegate) {
376 mDelegate = delegate;
377 }
378
379 @Override
380 public void finish() {
381 mDelegate.finish();
382 }
383
384 @Override
385 public void setDozeScreenState(int state) {
386 mDelegate.setDozeScreenState(state);
387 }
388
389 @Override
390 public void requestWakeUp() {
391 mDelegate.requestWakeUp();
392 }
393
394 @Override
395 public void setDozeScreenBrightness(int brightness) {
396 mDelegate.setDozeScreenBrightness(brightness);
397 }
398 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700399 }
400}