blob: e7d6eba1dcb3096e0017b2bbfde553acb30949de [file] [log] [blame]
Jorim Jaggi048af1f2014-11-11 22:51:10 +01001/*
2 * Copyright (C) 2014 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.statusbar.phone;
18
Jorim Jaggi048af1f2014-11-11 22:51:10 +010019import android.annotation.NonNull;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010020import android.os.Handler;
21import android.util.Log;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010022
Evan Laird878c8532018-10-15 15:54:29 -040023import com.android.internal.annotations.VisibleForTesting;
24import com.android.systemui.Dependency;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010025import com.android.systemui.doze.DozeHost;
26import com.android.systemui.doze.DozeLog;
Beverly8fdb5332019-02-04 14:29:49 -050027import com.android.systemui.plugins.statusbar.StatusBarStateController;
28import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010029
Beverlycc4a62f2019-09-26 14:55:28 -040030import javax.inject.Inject;
Dave Mankoff1193aa42019-10-28 17:51:26 -040031import javax.inject.Singleton;
Beverlycc4a62f2019-09-26 14:55:28 -040032
Jorim Jaggi048af1f2014-11-11 22:51:10 +010033/**
34 * Controller which handles all the doze animations of the scrims.
35 */
Dave Mankoff1193aa42019-10-28 17:51:26 -040036@Singleton
Evan Laird878c8532018-10-15 15:54:29 -040037public class DozeScrimController implements StateListener {
Jorim Jaggi048af1f2014-11-11 22:51:10 +010038 private static final String TAG = "DozeScrimController";
39 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
40
Beverlycc4a62f2019-09-26 14:55:28 -040041 private final DozeLog mDozeLog;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010042 private final DozeParameters mDozeParameters;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010043 private final Handler mHandler = new Handler();
Jorim Jaggi048af1f2014-11-11 22:51:10 +010044
45 private boolean mDozing;
46 private DozeHost.PulseCallback mPulseCallback;
John Spurlockeab28e62014-11-29 11:33:49 -050047 private int mPulseReason;
Adrian Roos4042e2f2017-07-07 17:10:11 +020048 private boolean mFullyPulsing;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010049
Lucas Dupin9e3fa102017-11-08 17:16:55 -080050 private final ScrimController.Callback mScrimCallback = new ScrimController.Callback() {
51 @Override
52 public void onDisplayBlanked() {
53 if (DEBUG) {
54 Log.d(TAG, "Pulse in, mDozing=" + mDozing + " mPulseReason="
Ned Burns30d67702020-01-28 12:58:45 -050055 + DozeLog.reasonToString(mPulseReason));
Lucas Dupin9e3fa102017-11-08 17:16:55 -080056 }
57 if (!mDozing) {
58 return;
59 }
60
61 // Signal that the pulse is ready to turn the screen on and draw.
62 pulseStarted();
63 }
64
65 @Override
66 public void onFinished() {
67 if (DEBUG) {
68 Log.d(TAG, "Pulse in finished, mDozing=" + mDozing);
69 }
70 if (!mDozing) {
71 return;
72 }
TYM Tsai2fc027d2018-12-04 19:28:19 +080073 // Notifications should time out on their own. Pulses due to notifications should
74 // instead be managed externally based off the notification's lifetime.
75 // Dock also controls the time out by self.
Ned Burns30d67702020-01-28 12:58:45 -050076 if (mPulseReason != DozeLog.PULSE_REASON_NOTIFICATION
77 && mPulseReason != DozeLog.PULSE_REASON_DOCKING) {
Kevina97ea052018-09-11 13:53:18 -070078 mHandler.postDelayed(mPulseOut, mDozeParameters.getPulseVisibleDuration());
79 mHandler.postDelayed(mPulseOutExtended,
80 mDozeParameters.getPulseVisibleDurationExtended());
81 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -080082 mFullyPulsing = true;
83 }
84
85 /**
86 * Transition was aborted before it was over.
87 */
88 @Override
89 public void onCancelled() {
90 pulseFinished();
91 }
92 };
Adrian Roosc8e29e72017-08-03 18:25:42 +020093
Beverlycc4a62f2019-09-26 14:55:28 -040094 @Inject
95 public DozeScrimController(DozeParameters dozeParameters, DozeLog dozeLog) {
Lucas Dupin16cfe452018-02-08 13:14:50 -080096 mDozeParameters = dozeParameters;
Evan Laird878c8532018-10-15 15:54:29 -040097 //Never expected to be destroyed
Jason Monkaf08c152018-12-04 11:12:39 -050098 Dependency.get(StatusBarStateController.class).addCallback(this);
Beverlycc4a62f2019-09-26 14:55:28 -040099 mDozeLog = dozeLog;
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100100 }
101
Evan Laird878c8532018-10-15 15:54:29 -0400102 @VisibleForTesting
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800103 public void setDozing(boolean dozing) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100104 if (mDozing == dozing) return;
105 mDozing = dozing;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800106 if (!mDozing) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100107 cancelPulsing();
Adrian Roos88e61aa2017-05-23 16:16:50 -0700108 }
109 }
110
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100111 /** When dozing, fade screen contents in and out using the front scrim. */
John Spurlockeab28e62014-11-29 11:33:49 -0500112 public void pulse(@NonNull DozeHost.PulseCallback callback, int reason) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100113 if (callback == null) {
114 throw new IllegalArgumentException("callback must not be null");
115 }
116
117 if (!mDozing || mPulseCallback != null) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800118 if (DEBUG) {
119 Log.d(TAG, "Pulse supressed. Dozing: " + mDozeParameters + " had callback? "
120 + (mPulseCallback != null));
121 }
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100122 // Pulse suppressed.
123 callback.onPulseFinished();
124 return;
125 }
126
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800127 // Begin pulse. Note that it's very important that the pulse finished callback
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100128 // be invoked when we're done so that the caller can drop the pulse wakelock.
129 mPulseCallback = callback;
John Spurlockeab28e62014-11-29 11:33:49 -0500130 mPulseReason = reason;
Adrian Roos67cca742017-04-13 16:52:51 -0700131 }
132
Adrian Roos4042e2f2017-07-07 17:10:11 +0200133 public void pulseOutNow() {
134 if (mPulseCallback != null && mFullyPulsing) {
135 mPulseOut.run();
136 }
137 }
138
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100139 public boolean isPulsing() {
140 return mPulseCallback != null;
141 }
142
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700143 public boolean isDozing() {
144 return mDozing;
145 }
146
Adrian Roos67cca742017-04-13 16:52:51 -0700147 public void extendPulse() {
148 mHandler.removeCallbacks(mPulseOut);
149 }
150
Lucas Dupin940503e2019-06-25 18:22:14 -0700151 /**
152 * When pulsing, cancel any timeouts that would take you out of the pulsing state.
153 */
154 public void cancelPendingPulseTimeout() {
155 mHandler.removeCallbacks(mPulseOut);
156 mHandler.removeCallbacks(mPulseOutExtended);
157 }
158
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100159 private void cancelPulsing() {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100160 if (mPulseCallback != null) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800161 if (DEBUG) Log.d(TAG, "Cancel pulsing");
Adrian Roos4042e2f2017-07-07 17:10:11 +0200162 mFullyPulsing = false;
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100163 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700164 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100165 pulseFinished();
166 }
167 }
168
169 private void pulseStarted() {
Beverlycc4a62f2019-09-26 14:55:28 -0400170 mDozeLog.tracePulseStart(mPulseReason);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100171 if (mPulseCallback != null) {
172 mPulseCallback.onPulseStarted();
173 }
174 }
175
176 private void pulseFinished() {
Beverlycc4a62f2019-09-26 14:55:28 -0400177 mDozeLog.tracePulseFinish();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100178 if (mPulseCallback != null) {
179 mPulseCallback.onPulseFinished();
180 mPulseCallback = null;
181 }
182 }
183
Adrian Roos67cca742017-04-13 16:52:51 -0700184 private final Runnable mPulseOutExtended = new Runnable() {
185 @Override
186 public void run() {
187 mHandler.removeCallbacks(mPulseOut);
188 mPulseOut.run();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100189 }
190 };
191
192 private final Runnable mPulseOut = new Runnable() {
193 @Override
194 public void run() {
Adrian Roos4042e2f2017-07-07 17:10:11 +0200195 mFullyPulsing = false;
196 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700197 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100198 if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing);
199 if (!mDozing) return;
Lucas Dupin61331ce2018-10-02 17:57:17 -0700200 pulseFinished();
Adrian Roos65072282017-08-02 19:10:24 +0200201 }
202 };
Lucas Dupin61331ce2018-10-02 17:57:17 -0700203
204 public ScrimController.Callback getScrimCallback() {
205 return mScrimCallback;
206 }
Evan Laird878c8532018-10-15 15:54:29 -0400207
208 @Override
209 public void onStateChanged(int newState) {
210 // don't care
211 }
212
213 @Override
214 public void onDozingChanged(boolean isDozing) {
215 setDozing(isDozing);
216 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800217}