blob: afaa593b3bb991f9b0ee0610d9082c899160ab77 [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;
Beverlycc4a62f2019-09-26 14:55:28 -040025import com.android.systemui.doze.DozeEvent;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010026import com.android.systemui.doze.DozeHost;
27import com.android.systemui.doze.DozeLog;
Beverly8fdb5332019-02-04 14:29:49 -050028import com.android.systemui.plugins.statusbar.StatusBarStateController;
29import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010030
Beverlycc4a62f2019-09-26 14:55:28 -040031import javax.inject.Inject;
Dave Mankoff1193aa42019-10-28 17:51:26 -040032import javax.inject.Singleton;
Beverlycc4a62f2019-09-26 14:55:28 -040033
Jorim Jaggi048af1f2014-11-11 22:51:10 +010034/**
35 * Controller which handles all the doze animations of the scrims.
36 */
Dave Mankoff1193aa42019-10-28 17:51:26 -040037@Singleton
Evan Laird878c8532018-10-15 15:54:29 -040038public class DozeScrimController implements StateListener {
Jorim Jaggi048af1f2014-11-11 22:51:10 +010039 private static final String TAG = "DozeScrimController";
40 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
41
Beverlycc4a62f2019-09-26 14:55:28 -040042 private final DozeLog mDozeLog;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010043 private final DozeParameters mDozeParameters;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010044 private final Handler mHandler = new Handler();
Jorim Jaggi048af1f2014-11-11 22:51:10 +010045
46 private boolean mDozing;
47 private DozeHost.PulseCallback mPulseCallback;
John Spurlockeab28e62014-11-29 11:33:49 -050048 private int mPulseReason;
Adrian Roos4042e2f2017-07-07 17:10:11 +020049 private boolean mFullyPulsing;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010050
Lucas Dupin9e3fa102017-11-08 17:16:55 -080051 private final ScrimController.Callback mScrimCallback = new ScrimController.Callback() {
52 @Override
53 public void onDisplayBlanked() {
54 if (DEBUG) {
55 Log.d(TAG, "Pulse in, mDozing=" + mDozing + " mPulseReason="
Beverlycc4a62f2019-09-26 14:55:28 -040056 + DozeEvent.reasonToString(mPulseReason));
Lucas Dupin9e3fa102017-11-08 17:16:55 -080057 }
58 if (!mDozing) {
59 return;
60 }
61
62 // Signal that the pulse is ready to turn the screen on and draw.
63 pulseStarted();
64 }
65
66 @Override
67 public void onFinished() {
68 if (DEBUG) {
69 Log.d(TAG, "Pulse in finished, mDozing=" + mDozing);
70 }
71 if (!mDozing) {
72 return;
73 }
TYM Tsai2fc027d2018-12-04 19:28:19 +080074 // Notifications should time out on their own. Pulses due to notifications should
75 // instead be managed externally based off the notification's lifetime.
76 // Dock also controls the time out by self.
Beverlycc4a62f2019-09-26 14:55:28 -040077 if (mPulseReason != DozeEvent.PULSE_REASON_NOTIFICATION
78 && mPulseReason != DozeEvent.PULSE_REASON_DOCKING) {
Kevina97ea052018-09-11 13:53:18 -070079 mHandler.postDelayed(mPulseOut, mDozeParameters.getPulseVisibleDuration());
80 mHandler.postDelayed(mPulseOutExtended,
81 mDozeParameters.getPulseVisibleDurationExtended());
82 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -080083 mFullyPulsing = true;
84 }
85
86 /**
87 * Transition was aborted before it was over.
88 */
89 @Override
90 public void onCancelled() {
91 pulseFinished();
92 }
93 };
Adrian Roosc8e29e72017-08-03 18:25:42 +020094
Beverlycc4a62f2019-09-26 14:55:28 -040095 @Inject
96 public DozeScrimController(DozeParameters dozeParameters, DozeLog dozeLog) {
Lucas Dupin16cfe452018-02-08 13:14:50 -080097 mDozeParameters = dozeParameters;
Evan Laird878c8532018-10-15 15:54:29 -040098 //Never expected to be destroyed
Jason Monkaf08c152018-12-04 11:12:39 -050099 Dependency.get(StatusBarStateController.class).addCallback(this);
Beverlycc4a62f2019-09-26 14:55:28 -0400100 mDozeLog = dozeLog;
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100101 }
102
Evan Laird878c8532018-10-15 15:54:29 -0400103 @VisibleForTesting
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800104 public void setDozing(boolean dozing) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100105 if (mDozing == dozing) return;
106 mDozing = dozing;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800107 if (!mDozing) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100108 cancelPulsing();
Adrian Roos88e61aa2017-05-23 16:16:50 -0700109 }
110 }
111
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100112 /** When dozing, fade screen contents in and out using the front scrim. */
John Spurlockeab28e62014-11-29 11:33:49 -0500113 public void pulse(@NonNull DozeHost.PulseCallback callback, int reason) {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100114 if (callback == null) {
115 throw new IllegalArgumentException("callback must not be null");
116 }
117
118 if (!mDozing || mPulseCallback != null) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800119 if (DEBUG) {
120 Log.d(TAG, "Pulse supressed. Dozing: " + mDozeParameters + " had callback? "
121 + (mPulseCallback != null));
122 }
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100123 // Pulse suppressed.
124 callback.onPulseFinished();
125 return;
126 }
127
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800128 // Begin pulse. Note that it's very important that the pulse finished callback
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100129 // be invoked when we're done so that the caller can drop the pulse wakelock.
130 mPulseCallback = callback;
John Spurlockeab28e62014-11-29 11:33:49 -0500131 mPulseReason = reason;
Adrian Roos67cca742017-04-13 16:52:51 -0700132 }
133
Adrian Roos4042e2f2017-07-07 17:10:11 +0200134 public void pulseOutNow() {
135 if (mPulseCallback != null && mFullyPulsing) {
136 mPulseOut.run();
137 }
138 }
139
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100140 public boolean isPulsing() {
141 return mPulseCallback != null;
142 }
143
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700144 public boolean isDozing() {
145 return mDozing;
146 }
147
Adrian Roos67cca742017-04-13 16:52:51 -0700148 public void extendPulse() {
149 mHandler.removeCallbacks(mPulseOut);
150 }
151
Lucas Dupin940503e2019-06-25 18:22:14 -0700152 /**
153 * When pulsing, cancel any timeouts that would take you out of the pulsing state.
154 */
155 public void cancelPendingPulseTimeout() {
156 mHandler.removeCallbacks(mPulseOut);
157 mHandler.removeCallbacks(mPulseOutExtended);
158 }
159
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100160 private void cancelPulsing() {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100161 if (mPulseCallback != null) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800162 if (DEBUG) Log.d(TAG, "Cancel pulsing");
Adrian Roos4042e2f2017-07-07 17:10:11 +0200163 mFullyPulsing = false;
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100164 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700165 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100166 pulseFinished();
167 }
168 }
169
170 private void pulseStarted() {
Beverlycc4a62f2019-09-26 14:55:28 -0400171 mDozeLog.tracePulseStart(mPulseReason);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100172 if (mPulseCallback != null) {
173 mPulseCallback.onPulseStarted();
174 }
175 }
176
177 private void pulseFinished() {
Beverlycc4a62f2019-09-26 14:55:28 -0400178 mDozeLog.tracePulseFinish();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100179 if (mPulseCallback != null) {
180 mPulseCallback.onPulseFinished();
181 mPulseCallback = null;
182 }
183 }
184
Adrian Roos67cca742017-04-13 16:52:51 -0700185 private final Runnable mPulseOutExtended = new Runnable() {
186 @Override
187 public void run() {
188 mHandler.removeCallbacks(mPulseOut);
189 mPulseOut.run();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100190 }
191 };
192
193 private final Runnable mPulseOut = new Runnable() {
194 @Override
195 public void run() {
Adrian Roos4042e2f2017-07-07 17:10:11 +0200196 mFullyPulsing = false;
197 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700198 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100199 if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing);
200 if (!mDozing) return;
Lucas Dupin61331ce2018-10-02 17:57:17 -0700201 pulseFinished();
Adrian Roos65072282017-08-02 19:10:24 +0200202 }
203 };
Lucas Dupin61331ce2018-10-02 17:57:17 -0700204
205 public ScrimController.Callback getScrimCallback() {
206 return mScrimCallback;
207 }
Evan Laird878c8532018-10-15 15:54:29 -0400208
209 @Override
210 public void onStateChanged(int newState) {
211 // don't care
212 }
213
214 @Override
215 public void onDozingChanged(boolean isDozing) {
216 setDozing(isDozing);
217 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800218}