blob: b12bf5c39970820ac9bcb49b922ae8b9e8747594 [file] [log] [blame]
Jorim Jaggiecc798e2014-05-26 18:14:37 +02001/*
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
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070022import android.annotation.IntDef;
Lucas Dupin82aa1632017-12-13 00:13:57 -080023import android.app.AlarmManager;
John Spurlockd06aa572014-09-10 10:40:49 -040024import android.content.Context;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -070025import android.graphics.Color;
Anthony Chene658cc22017-04-27 11:17:35 -070026import android.graphics.drawable.Drawable;
Lucas Dupin9e3fa102017-11-08 17:16:55 -080027import android.os.Handler;
Adrian Roosa5c63222017-07-27 16:33:39 +020028import android.os.Trace;
Lucas Dupin9e3fa102017-11-08 17:16:55 -080029import android.util.Log;
Lucas Dupina0bf8512017-05-24 17:04:47 -070030import android.util.MathUtils;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020031import android.view.View;
32import android.view.ViewTreeObserver;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020033import android.view.animation.DecelerateInterpolator;
34import android.view.animation.Interpolator;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -070035
Lucas Dupin9e3fa102017-11-08 17:16:55 -080036import com.android.internal.annotations.VisibleForTesting;
Lucas Dupine2292a92017-07-06 14:35:30 -070037import com.android.internal.colorextraction.ColorExtractor;
38import com.android.internal.colorextraction.ColorExtractor.GradientColors;
39import com.android.internal.colorextraction.ColorExtractor.OnColorsChangedListener;
Lucas Dupin9324aa92017-07-26 20:29:38 -070040import com.android.internal.graphics.ColorUtils;
Yohei Yukawa795f0102018-04-13 14:55:30 -070041import com.android.internal.util.function.TriConsumer;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070042import com.android.keyguard.KeyguardUpdateMonitor;
Lucas Dupinf8463ee2018-06-11 16:18:15 -070043import com.android.keyguard.KeyguardUpdateMonitorCallback;
Lucas Dupin314d41f2017-05-08 15:52:58 -070044import com.android.systemui.Dependency;
Lucas Dupin9e3fa102017-11-08 17:16:55 -080045import com.android.systemui.Dumpable;
John Spurlockbf370992014-06-17 13:58:31 -040046import com.android.systemui.R;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070047import com.android.systemui.colorextraction.SysuiColorExtractor;
Selim Cineka0fad3b2014-09-19 17:20:05 +020048import com.android.systemui.statusbar.ScrimView;
Rohan Shah20790b82018-07-02 17:21:04 -070049import com.android.systemui.statusbar.notification.stack.ViewState;
Lucas Dupin82aa1632017-12-13 00:13:57 -080050import com.android.systemui.util.AlarmTimeout;
Lucas Dupin9e3fa102017-11-08 17:16:55 -080051import com.android.systemui.util.wakelock.DelayedWakeLock;
52import com.android.systemui.util.wakelock.WakeLock;
John Spurlockbf370992014-06-17 13:58:31 -040053
Lucas Dupin9e3fa102017-11-08 17:16:55 -080054import java.io.FileDescriptor;
Adrian Roosba7ca592017-08-15 17:48:05 +020055import java.io.PrintWriter;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070056import java.lang.annotation.Retention;
57import java.lang.annotation.RetentionPolicy;
Adrian Roosa5c63222017-07-27 16:33:39 +020058import java.util.function.Consumer;
59
Jorim Jaggiecc798e2014-05-26 18:14:37 +020060/**
61 * Controls both the scrim behind the notifications and in front of the notifications (when a
62 * security method gets shown).
63 */
Selim Cinek99e9adf2018-03-15 09:17:47 -070064public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnColorsChangedListener,
65 Dumpable {
Lucas Dupin9e3fa102017-11-08 17:16:55 -080066
Lucas Dupin55c6e802018-09-27 18:07:36 -070067 static final String TAG = "ScrimController";
Lucas Dupin9e3fa102017-11-08 17:16:55 -080068 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
69
Lucas Dupin4749f1b2018-04-04 15:09:06 -070070 /**
71 * General scrim animation duration.
72 */
John Spurlock8b12f222014-09-09 11:54:11 -040073 public static final long ANIMATION_DURATION = 220;
Lucas Dupin4749f1b2018-04-04 15:09:06 -070074 /**
75 * Longer duration, currently only used when going to AOD.
76 */
77 public static final long ANIMATION_DURATION_LONG = 1000;
Lucas Dupin82aa1632017-12-13 00:13:57 -080078 /**
79 * When both scrims have 0 alpha.
80 */
81 public static final int VISIBILITY_FULLY_TRANSPARENT = 0;
82 /**
83 * When scrims aren't transparent (alpha 0) but also not opaque (alpha 1.)
84 */
85 public static final int VISIBILITY_SEMI_TRANSPARENT = 1;
86 /**
87 * When at least 1 scrim is fully opaque (alpha set to 1.)
88 */
89 public static final int VISIBILITY_FULLY_OPAQUE = 2;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070090
91 @IntDef(prefix = { "VISIBILITY_" }, value = {
92 VISIBILITY_FULLY_TRANSPARENT,
93 VISIBILITY_SEMI_TRANSPARENT,
94 VISIBILITY_FULLY_OPAQUE
95 })
96 @Retention(RetentionPolicy.SOURCE)
97 public @interface ScrimVisibility {}
98
Lucas Dupin9e3fa102017-11-08 17:16:55 -080099 /**
100 * Default alpha value for most scrims.
101 */
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800102 public static final float GRADIENT_SCRIM_ALPHA = 0.2f;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800103 /**
Lucas Dupin3113db62019-03-08 18:21:27 -0800104 * Scrim opacity when the phone is about to wake-up.
105 */
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700106 public static final float WAKE_SENSOR_SCRIM_ALPHA = 0.6f;
Lucas Dupin3113db62019-03-08 18:21:27 -0800107 /**
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800108 * A scrim varies its opacity based on a busyness factor, for example
109 * how many notifications are currently visible.
110 */
Lucas Dupinde64ee02018-12-21 14:45:12 -0800111 public static final float GRADIENT_SCRIM_ALPHA_BUSY = 0.7f;
Lucas Dupinde64ee02018-12-21 14:45:12 -0800112
Lucas Dupin55c6e802018-09-27 18:07:36 -0700113 /**
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800114 * The most common scrim, the one under the keyguard.
115 */
Lucas Dupina0bf8512017-05-24 17:04:47 -0700116 protected static final float SCRIM_BEHIND_ALPHA_KEYGUARD = GRADIENT_SCRIM_ALPHA;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800117
118 static final int TAG_KEY_ANIM = R.id.scrim;
Selim Cinek5104a6d2015-12-18 18:38:31 -0800119 private static final int TAG_START_ALPHA = R.id.scrim_alpha_start;
120 private static final int TAG_END_ALPHA = R.id.scrim_alpha_end;
Selim Cinek511f2702017-04-10 19:53:01 -0700121 private static final float NOT_INITIALIZED = -1;
John Spurlockbf370992014-06-17 13:58:31 -0400122
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800123 private ScrimState mState = ScrimState.UNINITIALIZED;
124 private final Context mContext;
Xiaohui Chen5da71352016-02-22 10:04:41 -0800125 protected final ScrimView mScrimBehind;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700126 protected final ScrimView mScrimInFront;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800127 private final UnlockMethodCache mUnlockMethodCache;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700128 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800129 private final DozeParameters mDozeParameters;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800130 private final AlarmTimeout mTimeTicker;
Lucas Dupin690c6f52018-07-10 15:28:57 -0700131 private final KeyguardVisibilityCallback mKeyguardVisibilityCallback;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800132 private final Handler mHandler;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200133
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700134 private final SysuiColorExtractor mColorExtractor;
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700135 private GradientColors mColors;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700136 private boolean mNeedsDrawableColorUpdate;
137
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800138 protected float mScrimBehindAlpha;
Lucas Dupin9324aa92017-07-26 20:29:38 -0700139 protected float mScrimBehindAlphaResValue;
Evan Rosky9a895012016-04-21 11:26:15 -0700140 protected float mScrimBehindAlphaKeyguard = SCRIM_BEHIND_ALPHA_KEYGUARD;
Xiaohui Chen4dab4b52016-03-07 12:31:14 -0800141
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800142 // Assuming the shade is expanded during initialization
143 private float mExpansionFraction = 1f;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200144
145 private boolean mDarkenWhileDragging;
Lucas Dupin67f02632018-03-12 11:08:31 -0700146 private boolean mExpansionAffectsAlpha = true;
Evan Rosky9a895012016-04-21 11:26:15 -0700147 protected boolean mAnimateChange;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200148 private boolean mUpdatePending;
Jorim Jaggie93698b2016-11-11 18:24:38 -0800149 private boolean mTracking;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800150 protected long mAnimationDuration = -1;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200151 private long mAnimationDelay;
152 private Runnable mOnAnimationFinished;
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200153 private boolean mDeferFinishedListener;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200154 private final Interpolator mInterpolator = new DecelerateInterpolator();
Selim Cinek511f2702017-04-10 19:53:01 -0700155 private float mCurrentInFrontAlpha = NOT_INITIALIZED;
156 private float mCurrentBehindAlpha = NOT_INITIALIZED;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800157 private int mCurrentInFrontTint;
158 private int mCurrentBehindTint;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800159 private boolean mWallpaperVisibilityTimedOut;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800160 private int mScrimsVisibility;
Yohei Yukawa795f0102018-04-13 14:55:30 -0700161 private final TriConsumer<ScrimState, Float, GradientColors> mScrimStateListener;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800162 private final Consumer<Integer> mScrimVisibleListener;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800163 private boolean mBlankScreen;
164 private boolean mScreenBlankingCallbackCalled;
165 private Callback mCallback;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800166 private boolean mWallpaperSupportsAmbientMode;
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800167 private boolean mScreenOn;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800168
169 // Scrim blanking callbacks
Lucas Dupin0791d972018-03-26 13:32:16 -0700170 private Runnable mPendingFrameCallback;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800171 private Runnable mBlankingTransitionRunnable;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800172
173 private final WakeLock mWakeLock;
174 private boolean mWakeLockHeld;
Lucas Dupind5107302018-03-19 15:30:29 -0700175 private boolean mKeyguardOccluded;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200176
shawnlin317db372018-04-09 19:49:48 +0800177 public ScrimController(ScrimView scrimBehind, ScrimView scrimInFront,
Yohei Yukawa795f0102018-04-13 14:55:30 -0700178 TriConsumer<ScrimState, Float, GradientColors> scrimStateListener,
shawnlin317db372018-04-09 19:49:48 +0800179 Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters,
180 AlarmManager alarmManager) {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200181 mScrimBehind = scrimBehind;
182 mScrimInFront = scrimInFront;
Yohei Yukawa795f0102018-04-13 14:55:30 -0700183 mScrimStateListener = scrimStateListener;
Adrian Roosa5c63222017-07-27 16:33:39 +0200184 mScrimVisibleListener = scrimVisibleListener;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800185 mContext = scrimBehind.getContext();
186 mUnlockMethodCache = UnlockMethodCache.getInstance(mContext);
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800187 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800188 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Lucas Dupin690c6f52018-07-10 15:28:57 -0700189 mKeyguardVisibilityCallback = new KeyguardVisibilityCallback();
190 mKeyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800191 mScrimBehindAlphaResValue = mContext.getResources().getFloat(R.dimen.scrim_behind_alpha);
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800192 mHandler = getHandler();
Lucas Dupin82aa1632017-12-13 00:13:57 -0800193 mTimeTicker = new AlarmTimeout(alarmManager, this::onHideWallpaperTimeout,
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800194 "hide_aod_wallpaper", mHandler);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800195 mWakeLock = createWakeLock();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700196 // Scrim alpha is initially set to the value on the resource but might be changed
197 // to make sure that text on top of it is legible.
198 mScrimBehindAlpha = mScrimBehindAlphaResValue;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800199 mDozeParameters = dozeParameters;
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800200
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700201 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupin314d41f2017-05-08 15:52:58 -0700202 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700203 mColors = mColorExtractor.getNeutralColors();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700204 mNeedsDrawableColorUpdate = true;
205
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800206 final ScrimState[] states = ScrimState.values();
207 for (int i = 0; i < states.length; i++) {
208 states[i].init(mScrimInFront, mScrimBehind, mDozeParameters);
209 states[i].setScrimBehindAlphaKeyguard(mScrimBehindAlphaKeyguard);
210 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800211
Lucas Dupin373356b2018-04-07 10:50:25 -0700212 mScrimBehind.setDefaultFocusHighlightEnabled(false);
213 mScrimInFront.setDefaultFocusHighlightEnabled(false);
214
Selim Cinek511f2702017-04-10 19:53:01 -0700215 updateScrims();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200216 }
217
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800218 public void transitionTo(ScrimState state) {
219 transitionTo(state, null);
220 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700221
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800222 public void transitionTo(ScrimState state, Callback callback) {
223 if (state == mState) {
Lucas Dupin19aba8e2017-12-11 12:42:26 -0800224 // Call the callback anyway, unless it's already enqueued
225 if (callback != null && mCallback != callback) {
226 callback.onFinished();
227 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800228 return;
229 } else if (DEBUG) {
230 Log.d(TAG, "State changed to: " + state);
231 }
232
233 if (state == ScrimState.UNINITIALIZED) {
234 throw new IllegalArgumentException("Cannot change to UNINITIALIZED.");
235 }
236
Lucas Dupin8635c442017-12-08 10:36:28 -0800237 final ScrimState oldState = mState;
238 mState = state;
Lucas Dupin5866aaf2018-02-02 14:45:31 -0800239 Trace.traceCounter(Trace.TRACE_TAG_APP, "scrim_state", mState.getIndex());
Lucas Dupin8635c442017-12-08 10:36:28 -0800240
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800241 if (mCallback != null) {
242 mCallback.onCancelled();
243 }
244 mCallback = callback;
245
Lucas Dupin8635c442017-12-08 10:36:28 -0800246 state.prepare(oldState);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800247 mScreenBlankingCallbackCalled = false;
248 mAnimationDelay = 0;
249 mBlankScreen = state.getBlanksScreen();
250 mAnimateChange = state.getAnimateChange();
251 mAnimationDuration = state.getAnimationDuration();
252 mCurrentInFrontTint = state.getFrontTint();
253 mCurrentBehindTint = state.getBehindTint();
254 mCurrentInFrontAlpha = state.getFrontAlpha();
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800255 mCurrentBehindAlpha = state.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800256 applyExpansionToAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800257
Lucas Dupin38962d72018-03-14 12:41:39 -0700258 // Scrim might acquire focus when user is navigating with a D-pad or a keyboard.
259 // We need to disable focus otherwise AOD would end up with a gray overlay.
260 mScrimInFront.setFocusable(!state.isLowPowerState());
261 mScrimBehind.setFocusable(!state.isLowPowerState());
262
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800263 // Cancel blanking transitions that were pending before we requested a new state
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800264 if (mPendingFrameCallback != null) {
Lucas Dupin0791d972018-03-26 13:32:16 -0700265 mScrimBehind.removeCallbacks(mPendingFrameCallback);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800266 mPendingFrameCallback = null;
267 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800268 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
269 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800270 mBlankingTransitionRunnable = null;
271 }
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800272
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800273 // Showing/hiding the keyguard means that scrim colors have to be switched, not necessary
274 // to do the same when you're just showing the brightness mirror.
275 mNeedsDrawableColorUpdate = state != ScrimState.BRIGHTNESS_MIRROR;
276
Lucas Dupineea53b32017-12-18 13:47:14 -0800277 // The device might sleep if it's entering AOD, we need to make sure that
278 // the animation plays properly until the last frame.
279 // It's important to avoid holding the wakelock unless necessary because
280 // WakeLock#aqcuire will trigger an IPC and will cause jank.
Lucas Dupin38962d72018-03-14 12:41:39 -0700281 if (mState.isLowPowerState()) {
Lucas Dupineea53b32017-12-18 13:47:14 -0800282 holdWakeLock();
283 }
Lucas Dupin82aa1632017-12-13 00:13:57 -0800284
TYM Tsaia71c8922019-01-07 15:57:53 +0800285 // AOD wallpapers should fade away after a while.
286 // Docking pulses may take a long time, wallpapers should also fade away after a while.
Jerry Changbde4c0c2019-06-13 16:58:41 +0800287 mWallpaperVisibilityTimedOut = false;
288 if (shouldFadeAwayWallpaper()) {
289 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
290 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
Lucas Dupinc7804042018-12-21 12:26:33 -0800291 } else {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800292 mTimeTicker.cancel();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800293 }
294
Lucas Dupinff307d52018-02-05 21:16:39 -0800295 if (mKeyguardUpdateMonitor.needsSlowUnlockTransition() && mState == ScrimState.UNLOCKED) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800296 // In case the user isn't unlocked, make sure to delay a bit because the system is hosed
297 // with too many things at this case, in order to not skip the initial frames.
298 mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16);
299 mAnimationDelay = StatusBar.FADE_KEYGUARD_START_DELAY;
Lucas Dupin0791d972018-03-26 13:32:16 -0700300 } else if ((!mDozeParameters.getAlwaysOn() && oldState == ScrimState.AOD)
Lucas Dupin16cfe452018-02-08 13:14:50 -0800301 || (mState == ScrimState.AOD && !mDozeParameters.getDisplayNeedsBlanking())) {
302 // Scheduling a frame isn't enough when:
303 // • Leaving doze and we need to modify scrim color immediately
304 // • ColorFade will not kick-in and scrim cannot wait for pre-draw.
Lucas Dupinff307d52018-02-05 21:16:39 -0800305 onPreDraw();
306 } else {
307 scheduleUpdate();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800308 }
Yohei Yukawa795f0102018-04-13 14:55:30 -0700309
310 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800311 }
312
Jerry Changbde4c0c2019-06-13 16:58:41 +0800313 private boolean shouldFadeAwayWallpaper() {
314 if (!mWallpaperSupportsAmbientMode) {
315 return false;
316 }
317
318 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()) {
319 return true;
320 }
321
322 if (mState == ScrimState.PULSING
323 && mCallback != null && mCallback.shouldTimeoutWallpaper()) {
324 return true;
325 }
326
327 return false;
328 }
329
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800330 public ScrimState getState() {
331 return mState;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200332 }
333
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800334 protected void setScrimBehindValues(float scrimBehindAlphaKeyguard) {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700335 mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800336 ScrimState[] states = ScrimState.values();
337 for (int i = 0; i < states.length; i++) {
338 states[i].setScrimBehindAlphaKeyguard(scrimBehindAlphaKeyguard);
339 }
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700340 scheduleUpdate();
341 }
342
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200343 public void onTrackingStarted() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800344 mTracking = true;
Selim Cineke8bae622015-07-15 13:24:06 -0700345 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200346 }
347
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200348 public void onExpandingFinished() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800349 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200350 }
351
Lucas Dupin82aa1632017-12-13 00:13:57 -0800352 @VisibleForTesting
353 protected void onHideWallpaperTimeout() {
TYM Tsaia71c8922019-01-07 15:57:53 +0800354 if (mState != ScrimState.AOD && mState != ScrimState.PULSING) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800355 return;
356 }
357
358 holdWakeLock();
359 mWallpaperVisibilityTimedOut = true;
360 mAnimateChange = true;
361 mAnimationDuration = mDozeParameters.getWallpaperFadeOutDuration();
362 scheduleUpdate();
363 }
364
365 private void holdWakeLock() {
366 if (!mWakeLockHeld) {
367 if (mWakeLock != null) {
368 mWakeLockHeld = true;
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800369 mWakeLock.acquire(TAG);
Lucas Dupin82aa1632017-12-13 00:13:57 -0800370 } else {
371 Log.w(TAG, "Cannot hold wake lock, it has not been set yet");
372 }
373 }
374 }
375
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800376 /**
377 * Current state of the shade expansion when pulling it from the top.
378 * This value is 1 when on top of the keyguard and goes to 0 as the user drags up.
379 *
380 * The expansion fraction is tied to the scrim opacity.
381 *
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800382 * @param fraction From 0 to 1 where 0 means collapsed and 1 expanded.
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800383 */
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200384 public void setPanelExpansion(float fraction) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800385 if (mExpansionFraction != fraction) {
386 mExpansionFraction = fraction;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800387
Lucas Dupin067136c2018-03-27 18:03:25 -0700388 final boolean keyguardOrUnlocked = mState == ScrimState.UNLOCKED
Lucas Dupin1c327432019-01-03 13:37:53 -0800389 || mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING;
Lucas Dupin067136c2018-03-27 18:03:25 -0700390 if (!keyguardOrUnlocked || !mExpansionAffectsAlpha) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800391 return;
392 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800393
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800394 applyExpansionToAlpha();
395
396 if (mUpdatePending) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800397 return;
398 }
399
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800400 setOrAdaptCurrentAnimation(mScrimBehind);
401 setOrAdaptCurrentAnimation(mScrimInFront);
shawnlin317db372018-04-09 19:49:48 +0800402
Yohei Yukawa795f0102018-04-13 14:55:30 -0700403 dispatchScrimState(mScrimBehind.getViewAlpha());
Jerry Changbde4c0c2019-06-13 16:58:41 +0800404
405 // Reset wallpaper timeout if it's already timeout like expanding panel while PULSING
406 // and docking.
407 if (mWallpaperVisibilityTimedOut) {
408 mWallpaperVisibilityTimedOut = false;
409 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
410 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
411 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800412 }
413 }
414
415 private void setOrAdaptCurrentAnimation(View scrim) {
416 if (!isAnimating(scrim)) {
417 updateScrimColor(scrim, getCurrentScrimAlpha(scrim), getCurrentScrimTint(scrim));
418 } else {
419 ValueAnimator previousAnimator = (ValueAnimator) scrim.getTag(TAG_KEY_ANIM);
420 float alpha = getCurrentScrimAlpha(scrim);
421 float previousEndValue = (Float) scrim.getTag(TAG_END_ALPHA);
422 float previousStartValue = (Float) scrim.getTag(TAG_START_ALPHA);
423 float relativeDiff = alpha - previousEndValue;
424 float newStartValue = previousStartValue + relativeDiff;
425 scrim.setTag(TAG_START_ALPHA, newStartValue);
426 scrim.setTag(TAG_END_ALPHA, alpha);
427 previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
Jorim Jaggi93439da2014-06-30 23:53:39 +0200428 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200429 }
430
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800431 private void applyExpansionToAlpha() {
Lucas Dupin67f02632018-03-12 11:08:31 -0700432 if (!mExpansionAffectsAlpha) {
433 return;
434 }
435
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800436 if (mState == ScrimState.UNLOCKED) {
437 // Darken scrim as you pull down the shade when unlocked
438 float behindFraction = getInterpolatedFraction();
439 behindFraction = (float) Math.pow(behindFraction, 0.8f);
Lucas Dupind7c44eb2018-04-05 10:00:31 -0700440 mCurrentBehindAlpha = behindFraction * GRADIENT_SCRIM_ALPHA_BUSY;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800441 mCurrentInFrontAlpha = 0;
Lucas Dupin1c327432019-01-03 13:37:53 -0800442 } else if (mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800443 // Either darken of make the scrim transparent when you
444 // pull down the shade
445 float interpolatedFract = getInterpolatedFraction();
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800446 float alphaBehind = mState.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800447 if (mDarkenWhileDragging) {
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800448 mCurrentBehindAlpha = MathUtils.lerp(GRADIENT_SCRIM_ALPHA_BUSY, alphaBehind,
Lucas Dupinb380c882018-02-25 21:57:17 -0800449 interpolatedFract);
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800450 mCurrentInFrontAlpha = 0;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800451 } else {
Lucas Dupinb380c882018-02-25 21:57:17 -0800452 mCurrentBehindAlpha = MathUtils.lerp(0 /* start */, alphaBehind,
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800453 interpolatedFract);
454 mCurrentInFrontAlpha = 0;
455 }
Lucas Dupin4cf01062019-03-08 17:07:49 -0800456 mCurrentBehindTint = ColorUtils.blendARGB(ScrimState.BOUNCER.getBehindTint(),
457 mState.getBehindTint(), interpolatedFract);
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800458 }
459 }
460
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800461 /**
Anthony Chene658cc22017-04-27 11:17:35 -0700462 * Sets the given drawable as the background of the scrim that shows up behind the
463 * notifications.
464 */
465 public void setScrimBehindDrawable(Drawable drawable) {
466 mScrimBehind.setDrawable(drawable);
467 }
468
Michael Wrightcae88752018-04-16 23:13:54 +0100469 /**
Lucas Dupin69bda602018-05-18 17:24:52 -0700470 * Sets the front scrim opacity in AOD so it's not as bright.
471 * <p>
472 * Displays usually don't support multiple dimming settings when in low power mode.
473 * The workaround is to modify the front scrim opacity when in AOD, so it's not as
474 * bright when you're at the movies or lying down on bed.
475 * <p>
476 * This value will be lost during transitions and only updated again after the the
477 * device is dozing when the light sensor is on.
Michael Wrightcae88752018-04-16 23:13:54 +0100478 */
479 public void setAodFrontScrimAlpha(float alpha) {
Lucas Dupin69bda602018-05-18 17:24:52 -0700480 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()
481 && mCurrentInFrontAlpha != alpha) {
Michael Wrightcae88752018-04-16 23:13:54 +0100482 mCurrentInFrontAlpha = alpha;
Lucas Dupin8523d132018-05-31 17:34:32 -0700483 updateScrims();
Michael Wrightcae88752018-04-16 23:13:54 +0100484 }
485
486 mState.AOD.setAodFrontScrimAlpha(alpha);
487 }
488
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700489 /**
Lucas Dupin34306c32019-07-16 11:56:53 -0700490 * Set front scrim to black, cancelling animations, in order to prepare to fade them
491 * away once the display turns on.
492 */
493 public void prepareForGentleWakeUp() {
Lucas Dupina7eacf92019-07-24 12:40:34 -0700494 if (mState == ScrimState.AOD) {
Lucas Dupin34306c32019-07-16 11:56:53 -0700495 mCurrentInFrontAlpha = 1f;
Lucas Dupina7eacf92019-07-24 12:40:34 -0700496 mCurrentInFrontTint = Color.BLACK;
497 mCurrentBehindTint = Color.BLACK;
Lucas Dupin34306c32019-07-16 11:56:53 -0700498 mAnimateChange = false;
499 updateScrims();
500 mAnimateChange = true;
501 mAnimationDuration = ANIMATION_DURATION_LONG;
502 }
503 }
504
505 /**
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700506 * If the lock screen sensor is active.
507 */
508 public void setWakeLockScreenSensorActive(boolean active) {
509 for (ScrimState state : ScrimState.values()) {
510 state.setWakeLockScreenSensorActive(active);
511 }
512
513 if (mState == ScrimState.PULSING) {
514 float newBehindAlpha = mState.getBehindAlpha();
515 if (mCurrentBehindAlpha != newBehindAlpha) {
516 mCurrentBehindAlpha = newBehindAlpha;
517 updateScrims();
518 }
519 }
520 }
521
Evan Rosky9a895012016-04-21 11:26:15 -0700522 protected void scheduleUpdate() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200523 if (mUpdatePending) return;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200524
525 // Make sure that a frame gets scheduled.
526 mScrimBehind.invalidate();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200527 mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this);
528 mUpdatePending = true;
529 }
530
Xiaohui Chen5da71352016-02-22 10:04:41 -0800531 protected void updateScrims() {
Lucas Dupin9324aa92017-07-26 20:29:38 -0700532 // Make sure we have the right gradients and their opacities will satisfy GAR.
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700533 if (mNeedsDrawableColorUpdate) {
534 mNeedsDrawableColorUpdate = false;
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700535 // Only animate scrim color if the scrim view is actually visible
536 boolean animateScrimInFront = mScrimInFront.getViewAlpha() != 0 && !mBlankScreen;
537 boolean animateScrimBehind = mScrimBehind.getViewAlpha() != 0 && !mBlankScreen;
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700538 mScrimInFront.setColors(mColors, animateScrimInFront);
539 mScrimBehind.setColors(mColors, animateScrimBehind);
Lucas Dupin9324aa92017-07-26 20:29:38 -0700540
541 // Calculate minimum scrim opacity for white or black text.
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700542 int textColor = mColors.supportsDarkText() ? Color.BLACK : Color.WHITE;
543 int mainColor = mColors.getMainColor();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700544 float minOpacity = ColorUtils.calculateMinimumBackgroundAlpha(textColor, mainColor,
545 4.5f /* minimumContrast */) / 255f;
546 mScrimBehindAlpha = Math.max(mScrimBehindAlphaResValue, minOpacity);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700547 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700548 }
549
Lucas Dupind5107302018-03-19 15:30:29 -0700550 // We want to override the back scrim opacity for the AOD state
Lucas Dupin82aa1632017-12-13 00:13:57 -0800551 // when it's time to fade the wallpaper away.
TYM Tsaia71c8922019-01-07 15:57:53 +0800552 boolean aodWallpaperTimeout = (mState == ScrimState.AOD || mState == ScrimState.PULSING)
553 && mWallpaperVisibilityTimedOut;
Lucas Dupind5107302018-03-19 15:30:29 -0700554 // We also want to hide FLAG_SHOW_WHEN_LOCKED activities under the scrim.
555 boolean occludedKeyguard = (mState == ScrimState.PULSING || mState == ScrimState.AOD)
556 && mKeyguardOccluded;
557 if (aodWallpaperTimeout || occludedKeyguard) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800558 mCurrentBehindAlpha = 1;
559 }
560
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800561 setScrimInFrontAlpha(mCurrentInFrontAlpha);
562 setScrimBehindAlpha(mCurrentBehindAlpha);
563
Adrian Roosa5c63222017-07-27 16:33:39 +0200564 dispatchScrimsVisible();
565 }
566
Yohei Yukawa795f0102018-04-13 14:55:30 -0700567 private void dispatchScrimState(float alpha) {
568 mScrimStateListener.accept(mState, alpha, mScrimInFront.getColors());
569 }
570
Adrian Roosa5c63222017-07-27 16:33:39 +0200571 private void dispatchScrimsVisible() {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800572 final int currentScrimVisibility;
573 if (mScrimInFront.getViewAlpha() == 1 || mScrimBehind.getViewAlpha() == 1) {
574 currentScrimVisibility = VISIBILITY_FULLY_OPAQUE;
575 } else if (mScrimInFront.getViewAlpha() == 0 && mScrimBehind.getViewAlpha() == 0) {
576 currentScrimVisibility = VISIBILITY_FULLY_TRANSPARENT;
577 } else {
578 currentScrimVisibility = VISIBILITY_SEMI_TRANSPARENT;
579 }
Adrian Roosa5c63222017-07-27 16:33:39 +0200580
Lucas Dupin82aa1632017-12-13 00:13:57 -0800581 if (mScrimsVisibility != currentScrimVisibility) {
582 mScrimsVisibility = currentScrimVisibility;
583 mScrimVisibleListener.accept(currentScrimVisibility);
Adrian Roosa5c63222017-07-27 16:33:39 +0200584 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200585 }
586
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800587 private float getInterpolatedFraction() {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800588 float frac = mExpansionFraction;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200589 // let's start this 20% of the way down the screen
590 frac = frac * 1.2f - 0.2f;
591 if (frac <= 0) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800592 return 0;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200593 } else {
594 // woo, special effects
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800595 return (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f))));
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200596 }
597 }
598
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700599 private void setScrimBehindAlpha(float alpha) {
600 setScrimAlpha(mScrimBehind, alpha);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200601 }
602
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700603 private void setScrimInFrontAlpha(float alpha) {
604 setScrimAlpha(mScrimInFront, alpha);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200605 }
606
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800607 private void setScrimAlpha(ScrimView scrim, float alpha) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800608 if (alpha == 0f) {
609 scrim.setClickable(false);
Selim Cinekaac93252015-04-14 20:04:12 -0700610 } else {
Lucas Dupin1c327432019-01-03 13:37:53 -0800611 // Eat touch events (unless dozing).
612 scrim.setClickable(mState != ScrimState.AOD);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200613 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800614 updateScrim(scrim, alpha);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100615 }
616
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800617 private void updateScrimColor(View scrim, float alpha, int tint) {
618 alpha = Math.max(0, Math.min(1.0f, alpha));
Selim Cinekaac93252015-04-14 20:04:12 -0700619 if (scrim instanceof ScrimView) {
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700620 ScrimView scrimView = (ScrimView) scrim;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700621
Adrian Roosa5c63222017-07-27 16:33:39 +0200622 Trace.traceCounter(Trace.TRACE_TAG_APP,
623 scrim == mScrimInFront ? "front_scrim_alpha" : "back_scrim_alpha",
624 (int) (alpha * 255));
625
Adrian Roosa5c63222017-07-27 16:33:39 +0200626 Trace.traceCounter(Trace.TRACE_TAG_APP,
627 scrim == mScrimInFront ? "front_scrim_tint" : "back_scrim_tint",
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800628 Color.alpha(tint));
Adrian Roosa5c63222017-07-27 16:33:39 +0200629
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800630 scrimView.setTint(tint);
631 scrimView.setViewAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700632 } else {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800633 scrim.setAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700634 }
Selim Cinek8cd80402017-08-01 10:44:29 -0700635 dispatchScrimsVisible();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100636 }
637
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800638 private void startScrimAnimation(final View scrim, float current) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800639 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
640 final int initialScrimTint = scrim instanceof ScrimView ? ((ScrimView) scrim).getTint() :
641 Color.TRANSPARENT;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700642 anim.addUpdateListener(animation -> {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800643 final float startAlpha = (Float) scrim.getTag(TAG_START_ALPHA);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800644 final float animAmount = (float) animation.getAnimatedValue();
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800645 final int finalScrimTint = getCurrentScrimTint(scrim);
646 final float finalScrimAlpha = getCurrentScrimAlpha(scrim);
647 float alpha = MathUtils.lerp(startAlpha, finalScrimAlpha, animAmount);
648 alpha = MathUtils.constrain(alpha, 0f, 1f);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800649 int tint = ColorUtils.blendARGB(initialScrimTint, finalScrimTint, animAmount);
650 updateScrimColor(scrim, alpha, tint);
Adrian Roosa5c63222017-07-27 16:33:39 +0200651 dispatchScrimsVisible();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200652 });
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800653 anim.setInterpolator(mInterpolator);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200654 anim.setStartDelay(mAnimationDelay);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800655 anim.setDuration(mAnimationDuration);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200656 anim.addListener(new AnimatorListenerAdapter() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800657 private Callback lastCallback = mCallback;
658
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200659 @Override
660 public void onAnimationEnd(Animator animation) {
Joanne Chunga393ad62019-01-16 21:41:31 +0800661 onFinished(lastCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800662
663 scrim.setTag(TAG_KEY_ANIM, null);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800664 dispatchScrimsVisible();
665
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200666 if (!mDeferFinishedListener && mOnAnimationFinished != null) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200667 mOnAnimationFinished.run();
668 mOnAnimationFinished = null;
669 }
670 }
671 });
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800672
673 // Cache alpha values because we might want to update this animator in the future if
674 // the user expands the panel while the animation is still running.
675 scrim.setTag(TAG_START_ALPHA, current);
676 scrim.setTag(TAG_END_ALPHA, getCurrentScrimAlpha(scrim));
677
John Spurlockbf370992014-06-17 13:58:31 -0400678 scrim.setTag(TAG_KEY_ANIM, anim);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800679 anim.start();
680 }
681
682 private float getCurrentScrimAlpha(View scrim) {
683 if (scrim == mScrimInFront) {
684 return mCurrentInFrontAlpha;
685 } else if (scrim == mScrimBehind) {
686 return mCurrentBehindAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800687 } else {
688 throw new IllegalArgumentException("Unknown scrim view");
689 }
690 }
691
692 private int getCurrentScrimTint(View scrim) {
693 if (scrim == mScrimInFront) {
694 return mCurrentInFrontTint;
695 } else if (scrim == mScrimBehind) {
696 return mCurrentBehindTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800697 } else {
698 throw new IllegalArgumentException("Unknown scrim view");
699 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200700 }
701
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200702 @Override
703 public boolean onPreDraw() {
704 mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this);
705 mUpdatePending = false;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800706 if (mCallback != null) {
707 mCallback.onStart();
Selim Cinek372d1bd2015-08-14 13:19:37 -0700708 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200709 updateScrims();
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800710 if (mOnAnimationFinished != null && !isAnimating(mScrimInFront)
711 && !isAnimating(mScrimBehind)) {
712 mOnAnimationFinished.run();
713 mOnAnimationFinished = null;
714 }
Selim Cinekedd32b82015-06-23 22:05:58 -0400715 return true;
716 }
717
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800718 private void onFinished() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800719 onFinished(mCallback);
720 }
721
722 private void onFinished(Callback callback) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800723 if (mWakeLockHeld) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800724 mWakeLock.release(TAG);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800725 mWakeLockHeld = false;
726 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800727
728 if (callback != null) {
729 callback.onFinished();
730
731 if (callback == mCallback) {
732 mCallback = null;
733 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800734 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800735
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800736 // When unlocking with fingerprint, we'll fade the scrims from black to transparent.
737 // At the end of the animation we need to remove the tint.
738 if (mState == ScrimState.UNLOCKED) {
739 mCurrentInFrontTint = Color.TRANSPARENT;
740 mCurrentBehindTint = Color.TRANSPARENT;
741 }
742 }
743
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700744 private boolean isAnimating(View scrim) {
745 return scrim.getTag(TAG_KEY_ANIM) != null;
746 }
747
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800748 @VisibleForTesting
749 void setOnAnimationFinished(Runnable onAnimationFinished) {
750 mOnAnimationFinished = onAnimationFinished;
751 }
Adrian Roos5f72c922016-03-08 15:54:26 -0800752
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800753 private void updateScrim(ScrimView scrim, float alpha) {
754 final float currentAlpha = scrim.getViewAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800755
756 ValueAnimator previousAnimator = ViewState.getChildTag(scrim, TAG_KEY_ANIM);
Selim Cinekaac93252015-04-14 20:04:12 -0700757 if (previousAnimator != null) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800758 if (mAnimateChange) {
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200759 // We are not done yet! Defer calling the finished listener.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800760 mDeferFinishedListener = true;
Selim Cinekaac93252015-04-14 20:04:12 -0700761 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800762 // Previous animators should always be cancelled. Not doing so would cause
763 // overlap, especially on states that don't animate, leading to flickering,
764 // and in the worst case, an internal state that doesn't represent what
765 // transitionTo requested.
766 cancelAnimator(previousAnimator);
767 mDeferFinishedListener = false;
Selim Cinekaac93252015-04-14 20:04:12 -0700768 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800769
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800770 if (mPendingFrameCallback != null) {
771 // Display is off and we're waiting.
772 return;
773 } else if (mBlankScreen) {
774 // Need to blank the display before continuing.
775 blankDisplay();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800776 return;
777 } else if (!mScreenBlankingCallbackCalled) {
778 // Not blanking the screen. Letting the callback know that we're ready
779 // to replace what was on the screen before.
780 if (mCallback != null) {
781 mCallback.onDisplayBlanked();
782 mScreenBlankingCallbackCalled = true;
783 }
784 }
785
Lucas Dupin6afae372017-11-30 13:09:22 -0500786 if (scrim == mScrimBehind) {
Yohei Yukawa795f0102018-04-13 14:55:30 -0700787 dispatchScrimState(alpha);
Lucas Dupin6afae372017-11-30 13:09:22 -0500788 }
789
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800790 final boolean wantsAlphaUpdate = alpha != currentAlpha;
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800791 final boolean wantsTintUpdate = scrim.getTint() != getCurrentScrimTint(scrim);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800792
793 if (wantsAlphaUpdate || wantsTintUpdate) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800794 if (mAnimateChange) {
795 startScrimAnimation(scrim, currentAlpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700796 } else {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800797 // update the alpha directly
798 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
799 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700800 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800801 } else {
802 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700803 }
804 }
805
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800806 @VisibleForTesting
807 protected void cancelAnimator(ValueAnimator previousAnimator) {
808 if (previousAnimator != null) {
809 previousAnimator.cancel();
810 }
811 }
812
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800813 private void blankDisplay() {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800814 updateScrimColor(mScrimInFront, 1, Color.BLACK);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800815
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800816 // Notify callback that the screen is completely black and we're
817 // ready to change the display power mode
Lucas Dupin0791d972018-03-26 13:32:16 -0700818 mPendingFrameCallback = () -> {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800819 if (mCallback != null) {
820 mCallback.onDisplayBlanked();
821 mScreenBlankingCallbackCalled = true;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800822 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800823
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800824 mBlankingTransitionRunnable = () -> {
825 mBlankingTransitionRunnable = null;
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800826 mPendingFrameCallback = null;
827 mBlankScreen = false;
828 // Try again.
829 updateScrims();
830 };
831
832 // Setting power states can happen after we push out the frame. Make sure we
833 // stay fully opaque until the power state request reaches the lower levels.
Lucas Dupin0791d972018-03-26 13:32:16 -0700834 final int delay = mScreenOn ? 32 : 500;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800835 if (DEBUG) {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800836 Log.d(TAG, "Fading out scrims with delay: " + delay);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800837 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800838 mHandler.postDelayed(mBlankingTransitionRunnable, delay);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800839 };
840 doOnTheNextFrame(mPendingFrameCallback);
841 }
842
Lucas Dupin0791d972018-03-26 13:32:16 -0700843 /**
844 * Executes a callback after the frame has hit the display.
845 * @param callback What to run.
846 */
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800847 @VisibleForTesting
Lucas Dupin0791d972018-03-26 13:32:16 -0700848 protected void doOnTheNextFrame(Runnable callback) {
849 // Just calling View#postOnAnimation isn't enough because the frame might not have reached
850 // the display yet. A timeout is the safest solution.
851 mScrimBehind.postOnAnimationDelayed(callback, 32 /* delayMillis */);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800852 }
853
854 @VisibleForTesting
855 protected Handler getHandler() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800856 return new Handler();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800857 }
858
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700859 public int getBackgroundColor() {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700860 int color = mColors.getMainColor();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800861 return Color.argb((int) (mScrimBehind.getViewAlpha() * Color.alpha(color)),
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700862 Color.red(color), Color.green(color), Color.blue(color));
Selim Cinekd35c2792016-01-21 13:20:57 -0800863 }
864
865 public void setScrimBehindChangeRunnable(Runnable changeRunnable) {
866 mScrimBehind.setChangeRunnable(changeRunnable);
867 }
Selim Cineke803491c2016-04-09 21:24:45 -0700868
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700869 public void setCurrentUser(int currentUser) {
870 // Don't care in the base class.
871 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700872
873 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700874 public void onColorsChanged(ColorExtractor colorExtractor, int which) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700875 mColors = mColorExtractor.getNeutralColors();
876 mNeedsDrawableColorUpdate = true;
877 scheduleUpdate();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700878 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200879
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800880 @VisibleForTesting
881 protected WakeLock createWakeLock() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800882 return new DelayedWakeLock(mHandler, WakeLock.createPartial(mContext, "Scrims"));
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800883 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200884
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800885 @Override
886 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800887 pw.println(" ScrimController: ");
888 pw.print(" state: "); pw.println(mState);
889 pw.print(" frontScrim:"); pw.print(" viewAlpha="); pw.print(mScrimInFront.getViewAlpha());
Adrian Roosba7ca592017-08-15 17:48:05 +0200890 pw.print(" alpha="); pw.print(mCurrentInFrontAlpha);
Adrian Roosba7ca592017-08-15 17:48:05 +0200891 pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimInFront.getTint()));
892
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800893 pw.print(" backScrim:"); pw.print(" viewAlpha="); pw.print(mScrimBehind.getViewAlpha());
Adrian Roosba7ca592017-08-15 17:48:05 +0200894 pw.print(" alpha="); pw.print(mCurrentBehindAlpha);
Adrian Roosba7ca592017-08-15 17:48:05 +0200895 pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimBehind.getTint()));
896
Adrian Roosba7ca592017-08-15 17:48:05 +0200897 pw.print(" mTracking="); pw.println(mTracking);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800898 }
899
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700900 public void setWallpaperSupportsAmbientMode(boolean wallpaperSupportsAmbientMode) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800901 mWallpaperSupportsAmbientMode = wallpaperSupportsAmbientMode;
902 ScrimState[] states = ScrimState.values();
903 for (int i = 0; i < states.length; i++) {
904 states[i].setWallpaperSupportsAmbientMode(wallpaperSupportsAmbientMode);
905 }
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700906 }
907
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800908 /**
909 * Interrupts blanking transitions once the display notifies that it's already on.
910 */
911 public void onScreenTurnedOn() {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800912 mScreenOn = true;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800913 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800914 if (DEBUG) {
915 Log.d(TAG, "Shorter blanking because screen turned on. All good.");
916 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800917 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800918 mBlankingTransitionRunnable.run();
919 }
920 }
921
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800922 public void onScreenTurnedOff() {
923 mScreenOn = false;
924 }
925
Lucas Dupin67f02632018-03-12 11:08:31 -0700926 public void setExpansionAffectsAlpha(boolean expansionAffectsAlpha) {
927 mExpansionAffectsAlpha = expansionAffectsAlpha;
928 }
929
Lucas Dupind5107302018-03-19 15:30:29 -0700930 public void setKeyguardOccluded(boolean keyguardOccluded) {
931 mKeyguardOccluded = keyguardOccluded;
Lucas Dupin63d72172018-06-06 11:42:55 -0700932 updateScrims();
Lucas Dupind5107302018-03-19 15:30:29 -0700933 }
934
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700935 public void setHasBackdrop(boolean hasBackdrop) {
Lucas Dupin193677c2018-06-11 19:16:03 -0700936 for (ScrimState state : ScrimState.values()) {
937 state.setHasBackdrop(hasBackdrop);
938 }
Lucas Dupin9bee5822018-07-09 14:32:53 -0700939
940 // Backdrop event may arrive after state was already applied,
941 // in this case, back-scrim needs to be re-evaluated
942 if (mState == ScrimState.AOD || mState == ScrimState.PULSING) {
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800943 float newBehindAlpha = mState.getBehindAlpha();
Lucas Dupin9bee5822018-07-09 14:32:53 -0700944 if (mCurrentBehindAlpha != newBehindAlpha) {
945 mCurrentBehindAlpha = newBehindAlpha;
946 updateScrims();
947 }
948 }
Lucas Dupin193677c2018-06-11 19:16:03 -0700949 }
950
Selim Cinek84b2acc2019-07-07 00:40:38 -0700951 public void setUnlockIsFading(boolean unlockFading) {
952 for (ScrimState state : ScrimState.values()) {
953 state.setUnlockIsFading(unlockFading);
954 }
955 }
956
Lucas Dupin193677c2018-06-11 19:16:03 -0700957 public void setLaunchingAffordanceWithPreview(boolean launchingAffordanceWithPreview) {
958 for (ScrimState state : ScrimState.values()) {
959 state.setLaunchingAffordanceWithPreview(launchingAffordanceWithPreview);
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700960 }
961 }
962
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800963 public interface Callback {
964 default void onStart() {
965 }
966 default void onDisplayBlanked() {
967 }
968 default void onFinished() {
969 }
970 default void onCancelled() {
971 }
Jerry Changbde4c0c2019-06-13 16:58:41 +0800972 /** Returns whether to timeout wallpaper or not. */
973 default boolean shouldTimeoutWallpaper() {
974 return false;
975 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200976 }
Lucas Dupin690c6f52018-07-10 15:28:57 -0700977
978 /**
979 * Simple keyguard callback that updates scrims when keyguard visibility changes.
980 */
981 private class KeyguardVisibilityCallback extends KeyguardUpdateMonitorCallback {
982
983 @Override
984 public void onKeyguardVisibilityChanged(boolean showing) {
985 mNeedsDrawableColorUpdate = true;
986 scheduleUpdate();
987 }
988 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200989}