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