blob: 90140ff2d7414ba44727367f782b1945e9ebe296 [file] [log] [blame]
Adrian Roos2981eb02017-05-26 18:40:09 -07001/*
2 * Copyright (C) 2017 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
Adrian Roos8db93112017-08-11 19:43:17 +020019import android.os.Handler;
Lucas Dupin58543842018-02-15 14:00:54 -080020import android.util.Log;
Adrian Roos2981eb02017-05-26 18:40:09 -070021import android.view.Display;
22
Lucas Dupin12663d32018-02-08 11:42:22 -080023import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roos39880be2018-04-23 17:41:17 +020024import com.android.systemui.util.wakelock.SettableWakeLock;
Lucas Dupin16cfe452018-02-08 13:14:50 -080025import com.android.systemui.util.wakelock.WakeLock;
Lucas Dupin12663d32018-02-08 11:42:22 -080026
Adrian Roos2981eb02017-05-26 18:40:09 -070027/**
28 * Controls the screen when dozing.
29 */
30public class DozeScreenState implements DozeMachine.Part {
Lucas Dupin58543842018-02-15 14:00:54 -080031
32 private static final boolean DEBUG = DozeService.DEBUG;
33 private static final String TAG = "DozeScreenState";
34
Lucas Dupin16cfe452018-02-08 13:14:50 -080035 /**
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.
38 */
Lucas Dupincbe05962018-04-26 16:44:05 -070039 private static final int ENTER_DOZE_DELAY = 6000;
40 /**
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.
44 */
45 public static final int ENTER_DOZE_HIDE_WALLPAPER_DELAY = 2000;
Lucas Dupin16cfe452018-02-08 13:14:50 -080046
Adrian Roos2981eb02017-05-26 18:40:09 -070047 private final DozeMachine.Service mDozeService;
Adrian Roos8db93112017-08-11 19:43:17 +020048 private final Handler mHandler;
Adrian Roosa79ad592017-08-31 15:17:17 +020049 private final Runnable mApplyPendingScreenState = this::applyPendingScreenState;
Lucas Dupin12663d32018-02-08 11:42:22 -080050 private final DozeParameters mParameters;
Adrian Roosa79ad592017-08-31 15:17:17 +020051
Adrian Roos8db93112017-08-11 19:43:17 +020052 private int mPendingScreenState = Display.STATE_UNKNOWN;
Adrian Roos39880be2018-04-23 17:41:17 +020053 private SettableWakeLock mWakeLock;
Adrian Roos2981eb02017-05-26 18:40:09 -070054
Lucas Dupin12663d32018-02-08 11:42:22 -080055 public DozeScreenState(DozeMachine.Service service, Handler handler,
Lucas Dupin16cfe452018-02-08 13:14:50 -080056 DozeParameters parameters, WakeLock wakeLock) {
Adrian Roos2981eb02017-05-26 18:40:09 -070057 mDozeService = service;
Adrian Roos8db93112017-08-11 19:43:17 +020058 mHandler = handler;
Lucas Dupin12663d32018-02-08 11:42:22 -080059 mParameters = parameters;
Adrian Roos39880be2018-04-23 17:41:17 +020060 mWakeLock = new SettableWakeLock(wakeLock);
Adrian Roos2981eb02017-05-26 18:40:09 -070061 }
62
63 @Override
64 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
Lucas Dupin12663d32018-02-08 11:42:22 -080065 int screenState = newState.screenState(mParameters);
Adrian Roos5502b892017-08-22 15:42:12 +020066
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);
71
72 applyScreenState(screenState);
Adrian Roos39880be2018-04-23 17:41:17 +020073 mWakeLock.setAcquired(false);
Adrian Roos5502b892017-08-22 15:42:12 +020074 return;
75 }
76
Adrian Roos8db93112017-08-11 19:43:17 +020077 if (screenState == Display.STATE_UNKNOWN) {
78 // We'll keep it in the existing state
79 return;
80 }
Adrian Roos5502b892017-08-22 15:42:12 +020081
Adrian Roos8db93112017-08-11 19:43:17 +020082 boolean messagePending = mHandler.hasCallbacks(mApplyPendingScreenState);
Lucas Dupin3d7ccaf2018-04-02 21:19:23 -070083 boolean pulseEnding = oldState == DozeMachine.State.DOZE_PULSE_DONE
84 && newState == DozeMachine.State.DOZE_AOD;
85 if (messagePending || oldState == DozeMachine.State.INITIALIZED || pulseEnding) {
Adrian Roos8db93112017-08-11 19:43:17 +020086 // During initialization, we hide the navigation bar. That is however only applied after
87 // a traversal; setting the screen state here is immediate however, so it can happen
88 // that the screen turns on again before the navigation bar is hidden. To work around
89 // that, wait for a traversal to happen before applying the initial screen state.
90 mPendingScreenState = screenState;
Lucas Dupin16cfe452018-02-08 13:14:50 -080091
92 // Delay screen state transitions even longer while animations are running.
93 boolean shouldDelayTransition = newState == DozeMachine.State.DOZE_AOD
94 && mParameters.shouldControlScreenOff();
95
Adrian Roos39880be2018-04-23 17:41:17 +020096 if (shouldDelayTransition) {
97 mWakeLock.setAcquired(true);
Adrian Roos8db93112017-08-11 19:43:17 +020098 }
Lucas Dupin16cfe452018-02-08 13:14:50 -080099
100 if (!messagePending) {
101 if (DEBUG) {
102 Log.d(TAG, "Display state changed to " + screenState + " delayed by "
103 + (shouldDelayTransition ? ENTER_DOZE_DELAY : 1));
104 }
105
106 if (shouldDelayTransition) {
107 mHandler.postDelayed(mApplyPendingScreenState, ENTER_DOZE_DELAY);
108 } else {
109 mHandler.post(mApplyPendingScreenState);
110 }
111 } else if (DEBUG) {
112 Log.d(TAG, "Pending display state change to " + screenState);
113 }
114 } else {
115 applyScreenState(screenState);
Adrian Roos8db93112017-08-11 19:43:17 +0200116 }
Adrian Roos8db93112017-08-11 19:43:17 +0200117 }
118
119 private void applyPendingScreenState() {
120 applyScreenState(mPendingScreenState);
121 mPendingScreenState = Display.STATE_UNKNOWN;
122 }
123
124 private void applyScreenState(int screenState) {
Adrian Roos2981eb02017-05-26 18:40:09 -0700125 if (screenState != Display.STATE_UNKNOWN) {
Lucas Dupin58543842018-02-15 14:00:54 -0800126 if (DEBUG) Log.d(TAG, "setDozeScreenState(" + screenState + ")");
Adrian Roos2981eb02017-05-26 18:40:09 -0700127 mDozeService.setDozeScreenState(screenState);
Adrian Roos8db93112017-08-11 19:43:17 +0200128 mPendingScreenState = Display.STATE_UNKNOWN;
Adrian Roos39880be2018-04-23 17:41:17 +0200129 mWakeLock.setAcquired(false);
Adrian Roos2981eb02017-05-26 18:40:09 -0700130 }
131 }
132}