2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.doze;
19 import android.os.Handler;
20 import android.util.Log;
21 import android.view.Display;
23 import com.android.systemui.statusbar.phone.DozeParameters;
24 import com.android.systemui.util.wakelock.SettableWakeLock;
25 import com.android.systemui.util.wakelock.WakeLock;
28 * Controls the screen when dozing.
30 public class DozeScreenState implements DozeMachine.Part {
32 private static final boolean DEBUG = DozeService.DEBUG;
33 private static final String TAG = "DozeScreenState";
36 * Delay entering low power mode when animating to make sure that we'll have
37 * time to move all elements into their final positions while still at 60 fps.
39 private static final int ENTER_DOZE_DELAY = 4000;
41 * Hide wallpaper earlier when entering low power mode. The gap between
42 * hiding the wallpaper and changing the display mode is necessary to hide
43 * the black frame that's inherent to hardware specs.
45 public static final int ENTER_DOZE_HIDE_WALLPAPER_DELAY = 2500;
47 private final DozeMachine.Service mDozeService;
48 private final Handler mHandler;
49 private final Runnable mApplyPendingScreenState = this::applyPendingScreenState;
50 private final DozeParameters mParameters;
52 private int mPendingScreenState = Display.STATE_UNKNOWN;
53 private SettableWakeLock mWakeLock;
55 public DozeScreenState(DozeMachine.Service service, Handler handler,
56 DozeParameters parameters, WakeLock wakeLock) {
57 mDozeService = service;
59 mParameters = parameters;
60 mWakeLock = new SettableWakeLock(wakeLock, TAG);
64 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
65 int screenState = newState.screenState(mParameters);
67 if (newState == DozeMachine.State.FINISH) {
68 // Make sure not to apply the screen state after DozeService was destroyed.
69 mPendingScreenState = Display.STATE_UNKNOWN;
70 mHandler.removeCallbacks(mApplyPendingScreenState);
72 applyScreenState(screenState);
73 mWakeLock.setAcquired(false);
77 if (screenState == Display.STATE_UNKNOWN) {
78 // We'll keep it in the existing state
82 boolean messagePending = mHandler.hasCallbacks(mApplyPendingScreenState);
83 boolean pulseEnding = oldState == DozeMachine.State.DOZE_PULSE_DONE
84 && newState == DozeMachine.State.DOZE_AOD;
85 boolean turningOn = (oldState == DozeMachine.State.DOZE_AOD_PAUSED
86 || oldState == DozeMachine.State.DOZE) && newState == DozeMachine.State.DOZE_AOD;
87 boolean justInitialized = oldState == DozeMachine.State.INITIALIZED;
88 if (messagePending || justInitialized || pulseEnding || turningOn) {
89 // During initialization, we hide the navigation bar. That is however only applied after
90 // a traversal; setting the screen state here is immediate however, so it can happen
91 // that the screen turns on again before the navigation bar is hidden. To work around
92 // that, wait for a traversal to happen before applying the initial screen state.
93 mPendingScreenState = screenState;
95 // Delay screen state transitions even longer while animations are running.
96 boolean shouldDelayTransition = newState == DozeMachine.State.DOZE_AOD
97 && mParameters.shouldControlScreenOff() && !turningOn;
99 if (shouldDelayTransition) {
100 mWakeLock.setAcquired(true);
103 if (!messagePending) {
105 Log.d(TAG, "Display state changed to " + screenState + " delayed by "
106 + (shouldDelayTransition ? ENTER_DOZE_DELAY : 1));
109 if (shouldDelayTransition) {
110 mHandler.postDelayed(mApplyPendingScreenState, ENTER_DOZE_DELAY);
112 mHandler.post(mApplyPendingScreenState);
115 Log.d(TAG, "Pending display state change to " + screenState);
118 applyScreenState(screenState);
122 private void applyPendingScreenState() {
123 applyScreenState(mPendingScreenState);
124 mPendingScreenState = Display.STATE_UNKNOWN;
127 private void applyScreenState(int screenState) {
128 if (screenState != Display.STATE_UNKNOWN) {
129 if (DEBUG) Log.d(TAG, "setDozeScreenState(" + screenState + ")");
130 mDozeService.setDozeScreenState(screenState);
131 mPendingScreenState = Display.STATE_UNKNOWN;
132 mWakeLock.setAcquired(false);