Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.util.Log; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 22 | |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | import com.android.systemui.Dependency; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 25 | import com.android.systemui.doze.DozeHost; |
| 26 | import com.android.systemui.doze.DozeLog; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 27 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 28 | import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 29 | |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 30 | import javax.inject.Inject; |
Dave Mankoff | 1193aa4 | 2019-10-28 17:51:26 -0400 | [diff] [blame] | 31 | import javax.inject.Singleton; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 32 | |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 33 | /** |
| 34 | * Controller which handles all the doze animations of the scrims. |
| 35 | */ |
Dave Mankoff | 1193aa4 | 2019-10-28 17:51:26 -0400 | [diff] [blame] | 36 | @Singleton |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 37 | public class DozeScrimController implements StateListener { |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 38 | private static final String TAG = "DozeScrimController"; |
| 39 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 40 | |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 41 | private final DozeLog mDozeLog; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 42 | private final DozeParameters mDozeParameters; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 43 | private final Handler mHandler = new Handler(); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 44 | |
| 45 | private boolean mDozing; |
| 46 | private DozeHost.PulseCallback mPulseCallback; |
John Spurlock | eab28e6 | 2014-11-29 11:33:49 -0500 | [diff] [blame] | 47 | private int mPulseReason; |
Adrian Roos | 4042e2f | 2017-07-07 17:10:11 +0200 | [diff] [blame] | 48 | private boolean mFullyPulsing; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 49 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 50 | 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 Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 55 | + DozeLog.reasonToString(mPulseReason)); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 56 | } |
| 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 Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 73 | // 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 Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 76 | if (mPulseReason != DozeLog.PULSE_REASON_NOTIFICATION |
| 77 | && mPulseReason != DozeLog.PULSE_REASON_DOCKING) { |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 78 | mHandler.postDelayed(mPulseOut, mDozeParameters.getPulseVisibleDuration()); |
| 79 | mHandler.postDelayed(mPulseOutExtended, |
| 80 | mDozeParameters.getPulseVisibleDurationExtended()); |
| 81 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 82 | 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 Roos | c8e29e7 | 2017-08-03 18:25:42 +0200 | [diff] [blame] | 93 | |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 94 | @Inject |
| 95 | public DozeScrimController(DozeParameters dozeParameters, DozeLog dozeLog) { |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 96 | mDozeParameters = dozeParameters; |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 97 | //Never expected to be destroyed |
Jason Monk | af08c15 | 2018-12-04 11:12:39 -0500 | [diff] [blame] | 98 | Dependency.get(StatusBarStateController.class).addCallback(this); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 99 | mDozeLog = dozeLog; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 102 | @VisibleForTesting |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 103 | public void setDozing(boolean dozing) { |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 104 | if (mDozing == dozing) return; |
| 105 | mDozing = dozing; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 106 | if (!mDozing) { |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 107 | cancelPulsing(); |
Adrian Roos | 88e61aa | 2017-05-23 16:16:50 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 111 | /** When dozing, fade screen contents in and out using the front scrim. */ |
John Spurlock | eab28e6 | 2014-11-29 11:33:49 -0500 | [diff] [blame] | 112 | public void pulse(@NonNull DozeHost.PulseCallback callback, int reason) { |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 113 | if (callback == null) { |
| 114 | throw new IllegalArgumentException("callback must not be null"); |
| 115 | } |
| 116 | |
| 117 | if (!mDozing || mPulseCallback != null) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 118 | if (DEBUG) { |
| 119 | Log.d(TAG, "Pulse supressed. Dozing: " + mDozeParameters + " had callback? " |
| 120 | + (mPulseCallback != null)); |
| 121 | } |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 122 | // Pulse suppressed. |
| 123 | callback.onPulseFinished(); |
| 124 | return; |
| 125 | } |
| 126 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 127 | // Begin pulse. Note that it's very important that the pulse finished callback |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 128 | // be invoked when we're done so that the caller can drop the pulse wakelock. |
| 129 | mPulseCallback = callback; |
John Spurlock | eab28e6 | 2014-11-29 11:33:49 -0500 | [diff] [blame] | 130 | mPulseReason = reason; |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Adrian Roos | 4042e2f | 2017-07-07 17:10:11 +0200 | [diff] [blame] | 133 | public void pulseOutNow() { |
| 134 | if (mPulseCallback != null && mFullyPulsing) { |
| 135 | mPulseOut.run(); |
| 136 | } |
| 137 | } |
| 138 | |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 139 | public boolean isPulsing() { |
| 140 | return mPulseCallback != null; |
| 141 | } |
| 142 | |
Jorim Jaggi | 83eb6bb | 2015-08-17 17:38:58 -0700 | [diff] [blame] | 143 | public boolean isDozing() { |
| 144 | return mDozing; |
| 145 | } |
| 146 | |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 147 | public void extendPulse() { |
| 148 | mHandler.removeCallbacks(mPulseOut); |
| 149 | } |
| 150 | |
Lucas Dupin | 940503e | 2019-06-25 18:22:14 -0700 | [diff] [blame] | 151 | /** |
| 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 Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 159 | private void cancelPulsing() { |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 160 | if (mPulseCallback != null) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 161 | if (DEBUG) Log.d(TAG, "Cancel pulsing"); |
Adrian Roos | 4042e2f | 2017-07-07 17:10:11 +0200 | [diff] [blame] | 162 | mFullyPulsing = false; |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 163 | mHandler.removeCallbacks(mPulseOut); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 164 | mHandler.removeCallbacks(mPulseOutExtended); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 165 | pulseFinished(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private void pulseStarted() { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 170 | mDozeLog.tracePulseStart(mPulseReason); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 171 | if (mPulseCallback != null) { |
| 172 | mPulseCallback.onPulseStarted(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private void pulseFinished() { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 177 | mDozeLog.tracePulseFinish(); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 178 | if (mPulseCallback != null) { |
| 179 | mPulseCallback.onPulseFinished(); |
| 180 | mPulseCallback = null; |
| 181 | } |
| 182 | } |
| 183 | |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 184 | private final Runnable mPulseOutExtended = new Runnable() { |
| 185 | @Override |
| 186 | public void run() { |
| 187 | mHandler.removeCallbacks(mPulseOut); |
| 188 | mPulseOut.run(); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 189 | } |
| 190 | }; |
| 191 | |
| 192 | private final Runnable mPulseOut = new Runnable() { |
| 193 | @Override |
| 194 | public void run() { |
Adrian Roos | 4042e2f | 2017-07-07 17:10:11 +0200 | [diff] [blame] | 195 | mFullyPulsing = false; |
| 196 | mHandler.removeCallbacks(mPulseOut); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 197 | mHandler.removeCallbacks(mPulseOutExtended); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 198 | if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing); |
| 199 | if (!mDozing) return; |
Lucas Dupin | 61331ce | 2018-10-02 17:57:17 -0700 | [diff] [blame] | 200 | pulseFinished(); |
Adrian Roos | 6507228 | 2017-08-02 19:10:24 +0200 | [diff] [blame] | 201 | } |
| 202 | }; |
Lucas Dupin | 61331ce | 2018-10-02 17:57:17 -0700 | [diff] [blame] | 203 | |
| 204 | public ScrimController.Callback getScrimCallback() { |
| 205 | return mScrimCallback; |
| 206 | } |
Evan Laird | 878c853 | 2018-10-15 15:54:29 -0400 | [diff] [blame] | 207 | |
| 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 Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 217 | } |