blob: a2438552e8af6d7c5f3108896c438f1442c89463 [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
30/**
31 * Controller which handles all the doze animations of the scrims.
32 */
Evan Laird878c8532018-10-15 15:54:29 -040033public class DozeScrimController implements StateListener {
Jorim Jaggi048af1f2014-11-11 22:51:10 +010034 private static final String TAG = "DozeScrimController";
35 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
36
37 private final DozeParameters mDozeParameters;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010038 private final Handler mHandler = new Handler();
Jorim Jaggi048af1f2014-11-11 22:51:10 +010039
40 private boolean mDozing;
41 private DozeHost.PulseCallback mPulseCallback;
John Spurlockeab28e62014-11-29 11:33:49 -050042 private int mPulseReason;
Adrian Roos4042e2f2017-07-07 17:10:11 +020043 private boolean mFullyPulsing;
Jorim Jaggi048af1f2014-11-11 22:51:10 +010044
Lucas Dupin9e3fa102017-11-08 17:16:55 -080045 private final ScrimController.Callback mScrimCallback = new ScrimController.Callback() {
46 @Override
47 public void onDisplayBlanked() {
48 if (DEBUG) {
49 Log.d(TAG, "Pulse in, mDozing=" + mDozing + " mPulseReason="
Lucas Dupin323f9ff2018-08-27 16:55:56 -070050 + DozeLog.reasonToString(mPulseReason));
Lucas Dupin9e3fa102017-11-08 17:16:55 -080051 }
52 if (!mDozing) {
53 return;
54 }
55
56 // Signal that the pulse is ready to turn the screen on and draw.
57 pulseStarted();
58 }
59
60 @Override
61 public void onFinished() {
62 if (DEBUG) {
63 Log.d(TAG, "Pulse in finished, mDozing=" + mDozing);
64 }
65 if (!mDozing) {
66 return;
67 }
TYM Tsai2fc027d2018-12-04 19:28:19 +080068 // Notifications should time out on their own. Pulses due to notifications should
69 // instead be managed externally based off the notification's lifetime.
70 // Dock also controls the time out by self.
71 if (mPulseReason != DozeLog.PULSE_REASON_NOTIFICATION
72 && mPulseReason != DozeLog.PULSE_REASON_DOCKING) {
Kevina97ea052018-09-11 13:53:18 -070073 mHandler.postDelayed(mPulseOut, mDozeParameters.getPulseVisibleDuration());
74 mHandler.postDelayed(mPulseOutExtended,
75 mDozeParameters.getPulseVisibleDurationExtended());
76 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -080077 mFullyPulsing = true;
78 }
79
80 /**
81 * Transition was aborted before it was over.
82 */
83 @Override
84 public void onCancelled() {
85 pulseFinished();
86 }
Jerry Changbde4c0c2019-06-13 16:58:41 +080087
88 /**
89 * Whether to timeout wallpaper or not.
90 */
91 @Override
92 public boolean shouldTimeoutWallpaper() {
93 return mPulseReason == DozeLog.PULSE_REASON_DOCKING;
94 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -080095 };
Adrian Roosc8e29e72017-08-03 18:25:42 +020096
Lucas Dupin61331ce2018-10-02 17:57:17 -070097 public DozeScrimController(DozeParameters dozeParameters) {
Lucas Dupin16cfe452018-02-08 13:14:50 -080098 mDozeParameters = dozeParameters;
Evan Laird878c8532018-10-15 15:54:29 -040099 //Never expected to be destroyed
Jason Monkaf08c152018-12-04 11:12:39 -0500100 Dependency.get(StatusBarStateController.class).addCallback(this);
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
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100152 private void cancelPulsing() {
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100153 if (mPulseCallback != null) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800154 if (DEBUG) Log.d(TAG, "Cancel pulsing");
Adrian Roos4042e2f2017-07-07 17:10:11 +0200155 mFullyPulsing = false;
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100156 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700157 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100158 pulseFinished();
159 }
160 }
161
162 private void pulseStarted() {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800163 DozeLog.tracePulseStart(mPulseReason);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100164 if (mPulseCallback != null) {
165 mPulseCallback.onPulseStarted();
166 }
167 }
168
169 private void pulseFinished() {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800170 DozeLog.tracePulseFinish();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100171 if (mPulseCallback != null) {
172 mPulseCallback.onPulseFinished();
173 mPulseCallback = null;
174 }
175 }
176
Adrian Roos67cca742017-04-13 16:52:51 -0700177 private final Runnable mPulseOutExtended = new Runnable() {
178 @Override
179 public void run() {
180 mHandler.removeCallbacks(mPulseOut);
181 mPulseOut.run();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100182 }
183 };
184
185 private final Runnable mPulseOut = new Runnable() {
186 @Override
187 public void run() {
Adrian Roos4042e2f2017-07-07 17:10:11 +0200188 mFullyPulsing = false;
189 mHandler.removeCallbacks(mPulseOut);
Adrian Roos67cca742017-04-13 16:52:51 -0700190 mHandler.removeCallbacks(mPulseOutExtended);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100191 if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing);
192 if (!mDozing) return;
Lucas Dupin61331ce2018-10-02 17:57:17 -0700193 pulseFinished();
Adrian Roos65072282017-08-02 19:10:24 +0200194 }
195 };
Lucas Dupin61331ce2018-10-02 17:57:17 -0700196
197 public ScrimController.Callback getScrimCallback() {
198 return mScrimCallback;
199 }
Evan Laird878c8532018-10-15 15:54:29 -0400200
201 @Override
202 public void onStateChanged(int newState) {
203 // don't care
204 }
205
206 @Override
207 public void onDozingChanged(boolean isDozing) {
208 setDozing(isDozing);
209 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800210}