blob: a9a26f1a6764ed6a059ecca272508cad1ce1985d [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 */
Lyn Hanbde48202019-05-29 19:18:29 -070081 public static final int TRANSPARENT = 0;
Lucas Dupin82aa1632017-12-13 00:13:57 -080082 /**
83 * When scrims aren't transparent (alpha 0) but also not opaque (alpha 1.)
84 */
Lyn Hanbde48202019-05-29 19:18:29 -070085 public static final int SEMI_TRANSPARENT = 1;
Lucas Dupin82aa1632017-12-13 00:13:57 -080086 /**
87 * When at least 1 scrim is fully opaque (alpha set to 1.)
88 */
Lyn Hanbde48202019-05-29 19:18:29 -070089 public static final int OPAQUE = 2;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070090
Lyn Hanbde48202019-05-29 19:18:29 -070091 @IntDef(prefix = {"VISIBILITY_"}, value = {
92 TRANSPARENT,
93 SEMI_TRANSPARENT,
94 OPAQUE
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070095 })
96 @Retention(RetentionPolicy.SOURCE)
Lyn Hanbde48202019-05-29 19:18:29 -070097 public @interface ScrimVisibility {
98 }
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070099
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800100 /**
101 * Default alpha value for most scrims.
102 */
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800103 public static final float GRADIENT_SCRIM_ALPHA = 0.2f;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800104 /**
Lucas Dupin3113db62019-03-08 18:21:27 -0800105 * Scrim opacity when the phone is about to wake-up.
106 */
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700107 public static final float WAKE_SENSOR_SCRIM_ALPHA = 0.6f;
Lucas Dupin3113db62019-03-08 18:21:27 -0800108 /**
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800109 * A scrim varies its opacity based on a busyness factor, for example
110 * how many notifications are currently visible.
111 */
Lucas Dupinde64ee02018-12-21 14:45:12 -0800112 public static final float GRADIENT_SCRIM_ALPHA_BUSY = 0.7f;
Lucas Dupinde64ee02018-12-21 14:45:12 -0800113
Lucas Dupin55c6e802018-09-27 18:07:36 -0700114 /**
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800115 * The most common scrim, the one under the keyguard.
116 */
Lucas Dupina0bf8512017-05-24 17:04:47 -0700117 protected static final float SCRIM_BEHIND_ALPHA_KEYGUARD = GRADIENT_SCRIM_ALPHA;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800118
119 static final int TAG_KEY_ANIM = R.id.scrim;
Selim Cinek5104a6d2015-12-18 18:38:31 -0800120 private static final int TAG_START_ALPHA = R.id.scrim_alpha_start;
121 private static final int TAG_END_ALPHA = R.id.scrim_alpha_end;
Selim Cinek511f2702017-04-10 19:53:01 -0700122 private static final float NOT_INITIALIZED = -1;
John Spurlockbf370992014-06-17 13:58:31 -0400123
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800124 private ScrimState mState = ScrimState.UNINITIALIZED;
125 private final Context mContext;
Lyn Hanbde48202019-05-29 19:18:29 -0700126
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700127 protected final ScrimView mScrimInFront;
Lyn Hanbde48202019-05-29 19:18:29 -0700128 protected final ScrimView mScrimBehind;
129 protected final ScrimView mScrimForBubble;
130
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800131 private final UnlockMethodCache mUnlockMethodCache;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700132 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800133 private final DozeParameters mDozeParameters;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800134 private final AlarmTimeout mTimeTicker;
Lucas Dupin690c6f52018-07-10 15:28:57 -0700135 private final KeyguardVisibilityCallback mKeyguardVisibilityCallback;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800136 private final Handler mHandler;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200137
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700138 private final SysuiColorExtractor mColorExtractor;
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700139 private GradientColors mColors;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700140 private boolean mNeedsDrawableColorUpdate;
141
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800142 protected float mScrimBehindAlpha;
Lucas Dupin9324aa92017-07-26 20:29:38 -0700143 protected float mScrimBehindAlphaResValue;
Evan Rosky9a895012016-04-21 11:26:15 -0700144 protected float mScrimBehindAlphaKeyguard = SCRIM_BEHIND_ALPHA_KEYGUARD;
Xiaohui Chen4dab4b52016-03-07 12:31:14 -0800145
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800146 // Assuming the shade is expanded during initialization
147 private float mExpansionFraction = 1f;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200148
149 private boolean mDarkenWhileDragging;
Lucas Dupin67f02632018-03-12 11:08:31 -0700150 private boolean mExpansionAffectsAlpha = true;
Evan Rosky9a895012016-04-21 11:26:15 -0700151 protected boolean mAnimateChange;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200152 private boolean mUpdatePending;
Jorim Jaggie93698b2016-11-11 18:24:38 -0800153 private boolean mTracking;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800154 protected long mAnimationDuration = -1;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200155 private long mAnimationDelay;
156 private Runnable mOnAnimationFinished;
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200157 private boolean mDeferFinishedListener;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200158 private final Interpolator mInterpolator = new DecelerateInterpolator();
Lyn Hanbde48202019-05-29 19:18:29 -0700159
160 private float mInFrontAlpha = NOT_INITIALIZED;
161 private float mBehindAlpha = NOT_INITIALIZED;
162 private float mBubbleAlpha = NOT_INITIALIZED;
163
164 private int mInFrontTint;
165 private int mBehindTint;
166 private int mBubbleTint;
167
Lucas Dupin82aa1632017-12-13 00:13:57 -0800168 private boolean mWallpaperVisibilityTimedOut;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800169 private int mScrimsVisibility;
Yohei Yukawa795f0102018-04-13 14:55:30 -0700170 private final TriConsumer<ScrimState, Float, GradientColors> mScrimStateListener;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800171 private final Consumer<Integer> mScrimVisibleListener;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800172 private boolean mBlankScreen;
173 private boolean mScreenBlankingCallbackCalled;
174 private Callback mCallback;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800175 private boolean mWallpaperSupportsAmbientMode;
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800176 private boolean mScreenOn;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800177
178 // Scrim blanking callbacks
Lucas Dupin0791d972018-03-26 13:32:16 -0700179 private Runnable mPendingFrameCallback;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800180 private Runnable mBlankingTransitionRunnable;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800181
182 private final WakeLock mWakeLock;
183 private boolean mWakeLockHeld;
Lucas Dupind5107302018-03-19 15:30:29 -0700184 private boolean mKeyguardOccluded;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200185
Lyn Hanbde48202019-05-29 19:18:29 -0700186 public ScrimController(ScrimView scrimBehind, ScrimView scrimInFront, ScrimView scrimForBubble,
Yohei Yukawa795f0102018-04-13 14:55:30 -0700187 TriConsumer<ScrimState, Float, GradientColors> scrimStateListener,
shawnlin317db372018-04-09 19:49:48 +0800188 Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters,
189 AlarmManager alarmManager) {
Lyn Hanbde48202019-05-29 19:18:29 -0700190
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200191 mScrimBehind = scrimBehind;
192 mScrimInFront = scrimInFront;
Lyn Hanbde48202019-05-29 19:18:29 -0700193 mScrimForBubble = scrimForBubble;
194
Yohei Yukawa795f0102018-04-13 14:55:30 -0700195 mScrimStateListener = scrimStateListener;
Adrian Roosa5c63222017-07-27 16:33:39 +0200196 mScrimVisibleListener = scrimVisibleListener;
Lyn Hanbde48202019-05-29 19:18:29 -0700197
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800198 mContext = scrimBehind.getContext();
199 mUnlockMethodCache = UnlockMethodCache.getInstance(mContext);
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800200 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800201 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Lucas Dupin690c6f52018-07-10 15:28:57 -0700202 mKeyguardVisibilityCallback = new KeyguardVisibilityCallback();
203 mKeyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800204 mScrimBehindAlphaResValue = mContext.getResources().getFloat(R.dimen.scrim_behind_alpha);
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800205 mHandler = getHandler();
Lucas Dupin82aa1632017-12-13 00:13:57 -0800206 mTimeTicker = new AlarmTimeout(alarmManager, this::onHideWallpaperTimeout,
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800207 "hide_aod_wallpaper", mHandler);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800208 mWakeLock = createWakeLock();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700209 // Scrim alpha is initially set to the value on the resource but might be changed
210 // to make sure that text on top of it is legible.
211 mScrimBehindAlpha = mScrimBehindAlphaResValue;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800212 mDozeParameters = dozeParameters;
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800213
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700214 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupin314d41f2017-05-08 15:52:58 -0700215 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700216 mColors = mColorExtractor.getNeutralColors();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700217 mNeedsDrawableColorUpdate = true;
218
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800219 final ScrimState[] states = ScrimState.values();
220 for (int i = 0; i < states.length; i++) {
Lyn Hanbde48202019-05-29 19:18:29 -0700221 states[i].init(mScrimInFront, mScrimBehind, mScrimForBubble, mDozeParameters);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800222 states[i].setScrimBehindAlphaKeyguard(mScrimBehindAlphaKeyguard);
223 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800224
Lucas Dupin373356b2018-04-07 10:50:25 -0700225 mScrimBehind.setDefaultFocusHighlightEnabled(false);
226 mScrimInFront.setDefaultFocusHighlightEnabled(false);
Lyn Hanbde48202019-05-29 19:18:29 -0700227 mScrimForBubble.setDefaultFocusHighlightEnabled(false);
Lucas Dupin373356b2018-04-07 10:50:25 -0700228
Selim Cinek511f2702017-04-10 19:53:01 -0700229 updateScrims();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200230 }
231
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800232 public void transitionTo(ScrimState state) {
233 transitionTo(state, null);
234 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700235
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800236 public void transitionTo(ScrimState state, Callback callback) {
237 if (state == mState) {
Lucas Dupin19aba8e2017-12-11 12:42:26 -0800238 // Call the callback anyway, unless it's already enqueued
239 if (callback != null && mCallback != callback) {
240 callback.onFinished();
241 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800242 return;
243 } else if (DEBUG) {
244 Log.d(TAG, "State changed to: " + state);
245 }
246
247 if (state == ScrimState.UNINITIALIZED) {
248 throw new IllegalArgumentException("Cannot change to UNINITIALIZED.");
249 }
250
Lucas Dupin8635c442017-12-08 10:36:28 -0800251 final ScrimState oldState = mState;
252 mState = state;
Lucas Dupin5866aaf2018-02-02 14:45:31 -0800253 Trace.traceCounter(Trace.TRACE_TAG_APP, "scrim_state", mState.getIndex());
Lucas Dupin8635c442017-12-08 10:36:28 -0800254
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800255 if (mCallback != null) {
256 mCallback.onCancelled();
257 }
258 mCallback = callback;
259
Lucas Dupin8635c442017-12-08 10:36:28 -0800260 state.prepare(oldState);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800261 mScreenBlankingCallbackCalled = false;
262 mAnimationDelay = 0;
263 mBlankScreen = state.getBlanksScreen();
264 mAnimateChange = state.getAnimateChange();
265 mAnimationDuration = state.getAnimationDuration();
Lyn Hanbde48202019-05-29 19:18:29 -0700266
267 mInFrontTint = state.getFrontTint();
268 mBehindTint = state.getBehindTint();
269 mBubbleTint = state.getBubbleTint();
270
271 mInFrontAlpha = state.getFrontAlpha();
272 mBehindAlpha = state.getBehindAlpha();
273 mBubbleAlpha = state.getBubbleAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800274 applyExpansionToAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800275
Lucas Dupin38962d72018-03-14 12:41:39 -0700276 // Scrim might acquire focus when user is navigating with a D-pad or a keyboard.
277 // We need to disable focus otherwise AOD would end up with a gray overlay.
278 mScrimInFront.setFocusable(!state.isLowPowerState());
279 mScrimBehind.setFocusable(!state.isLowPowerState());
280
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800281 // Cancel blanking transitions that were pending before we requested a new state
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800282 if (mPendingFrameCallback != null) {
Lucas Dupin0791d972018-03-26 13:32:16 -0700283 mScrimBehind.removeCallbacks(mPendingFrameCallback);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800284 mPendingFrameCallback = null;
285 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800286 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
287 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800288 mBlankingTransitionRunnable = null;
289 }
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800290
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800291 // Showing/hiding the keyguard means that scrim colors have to be switched, not necessary
292 // to do the same when you're just showing the brightness mirror.
293 mNeedsDrawableColorUpdate = state != ScrimState.BRIGHTNESS_MIRROR;
294
Lucas Dupineea53b32017-12-18 13:47:14 -0800295 // The device might sleep if it's entering AOD, we need to make sure that
296 // the animation plays properly until the last frame.
297 // It's important to avoid holding the wakelock unless necessary because
298 // WakeLock#aqcuire will trigger an IPC and will cause jank.
Lucas Dupin38962d72018-03-14 12:41:39 -0700299 if (mState.isLowPowerState()) {
Lucas Dupineea53b32017-12-18 13:47:14 -0800300 holdWakeLock();
301 }
Lucas Dupin82aa1632017-12-13 00:13:57 -0800302
TYM Tsaia71c8922019-01-07 15:57:53 +0800303 // AOD wallpapers should fade away after a while.
304 // Docking pulses may take a long time, wallpapers should also fade away after a while.
Jerry Changbde4c0c2019-06-13 16:58:41 +0800305 mWallpaperVisibilityTimedOut = false;
306 if (shouldFadeAwayWallpaper()) {
307 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
308 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
Lucas Dupinc7804042018-12-21 12:26:33 -0800309 } else {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800310 mTimeTicker.cancel();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800311 }
312
Lucas Dupinff307d52018-02-05 21:16:39 -0800313 if (mKeyguardUpdateMonitor.needsSlowUnlockTransition() && mState == ScrimState.UNLOCKED) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800314 // In case the user isn't unlocked, make sure to delay a bit because the system is hosed
315 // with too many things at this case, in order to not skip the initial frames.
316 mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16);
317 mAnimationDelay = StatusBar.FADE_KEYGUARD_START_DELAY;
Lucas Dupin0791d972018-03-26 13:32:16 -0700318 } else if ((!mDozeParameters.getAlwaysOn() && oldState == ScrimState.AOD)
Lucas Dupin16cfe452018-02-08 13:14:50 -0800319 || (mState == ScrimState.AOD && !mDozeParameters.getDisplayNeedsBlanking())) {
320 // Scheduling a frame isn't enough when:
321 // • Leaving doze and we need to modify scrim color immediately
322 // • ColorFade will not kick-in and scrim cannot wait for pre-draw.
Lucas Dupinff307d52018-02-05 21:16:39 -0800323 onPreDraw();
324 } else {
325 scheduleUpdate();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800326 }
Yohei Yukawa795f0102018-04-13 14:55:30 -0700327
328 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800329 }
330
Jerry Changbde4c0c2019-06-13 16:58:41 +0800331 private boolean shouldFadeAwayWallpaper() {
332 if (!mWallpaperSupportsAmbientMode) {
333 return false;
334 }
335
336 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()) {
337 return true;
338 }
339
340 if (mState == ScrimState.PULSING
341 && mCallback != null && mCallback.shouldTimeoutWallpaper()) {
342 return true;
343 }
344
345 return false;
346 }
347
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800348 public ScrimState getState() {
349 return mState;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200350 }
351
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800352 protected void setScrimBehindValues(float scrimBehindAlphaKeyguard) {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700353 mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800354 ScrimState[] states = ScrimState.values();
355 for (int i = 0; i < states.length; i++) {
356 states[i].setScrimBehindAlphaKeyguard(scrimBehindAlphaKeyguard);
357 }
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700358 scheduleUpdate();
359 }
360
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200361 public void onTrackingStarted() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800362 mTracking = true;
Selim Cineke8bae622015-07-15 13:24:06 -0700363 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200364 }
365
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200366 public void onExpandingFinished() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800367 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200368 }
369
Lucas Dupin82aa1632017-12-13 00:13:57 -0800370 @VisibleForTesting
371 protected void onHideWallpaperTimeout() {
TYM Tsaia71c8922019-01-07 15:57:53 +0800372 if (mState != ScrimState.AOD && mState != ScrimState.PULSING) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800373 return;
374 }
375
376 holdWakeLock();
377 mWallpaperVisibilityTimedOut = true;
378 mAnimateChange = true;
379 mAnimationDuration = mDozeParameters.getWallpaperFadeOutDuration();
380 scheduleUpdate();
381 }
382
383 private void holdWakeLock() {
384 if (!mWakeLockHeld) {
385 if (mWakeLock != null) {
386 mWakeLockHeld = true;
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800387 mWakeLock.acquire(TAG);
Lucas Dupin82aa1632017-12-13 00:13:57 -0800388 } else {
389 Log.w(TAG, "Cannot hold wake lock, it has not been set yet");
390 }
391 }
392 }
393
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800394 /**
395 * Current state of the shade expansion when pulling it from the top.
396 * This value is 1 when on top of the keyguard and goes to 0 as the user drags up.
397 *
398 * The expansion fraction is tied to the scrim opacity.
399 *
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800400 * @param fraction From 0 to 1 where 0 means collapsed and 1 expanded.
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800401 */
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200402 public void setPanelExpansion(float fraction) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800403 if (mExpansionFraction != fraction) {
404 mExpansionFraction = fraction;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800405
Lyn Hanbde48202019-05-29 19:18:29 -0700406 boolean relevantState = (mState == ScrimState.UNLOCKED
407 || mState == ScrimState.KEYGUARD
408 || mState == ScrimState.PULSING
409 || mState == ScrimState.BUBBLE_EXPANDED);
410 if (!(relevantState && mExpansionAffectsAlpha)) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800411 return;
412 }
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800413 applyExpansionToAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800414 if (mUpdatePending) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800415 return;
416 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800417 setOrAdaptCurrentAnimation(mScrimBehind);
418 setOrAdaptCurrentAnimation(mScrimInFront);
Lyn Hanbde48202019-05-29 19:18:29 -0700419 setOrAdaptCurrentAnimation(mScrimForBubble);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700420 dispatchScrimState(mScrimBehind.getViewAlpha());
Jerry Changbde4c0c2019-06-13 16:58:41 +0800421
422 // Reset wallpaper timeout if it's already timeout like expanding panel while PULSING
423 // and docking.
424 if (mWallpaperVisibilityTimedOut) {
425 mWallpaperVisibilityTimedOut = false;
426 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
427 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
428 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800429 }
430 }
431
432 private void setOrAdaptCurrentAnimation(View scrim) {
Lyn Hanbde48202019-05-29 19:18:29 -0700433 float alpha = getCurrentScrimAlpha(scrim);
434 if (isAnimating(scrim)) {
435 // Adapt current animation.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800436 ValueAnimator previousAnimator = (ValueAnimator) scrim.getTag(TAG_KEY_ANIM);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800437 float previousEndValue = (Float) scrim.getTag(TAG_END_ALPHA);
438 float previousStartValue = (Float) scrim.getTag(TAG_START_ALPHA);
439 float relativeDiff = alpha - previousEndValue;
440 float newStartValue = previousStartValue + relativeDiff;
441 scrim.setTag(TAG_START_ALPHA, newStartValue);
442 scrim.setTag(TAG_END_ALPHA, alpha);
443 previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
Lyn Hanbde48202019-05-29 19:18:29 -0700444 } else {
445 // Set animation.
446 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
Jorim Jaggi93439da2014-06-30 23:53:39 +0200447 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200448 }
449
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800450 private void applyExpansionToAlpha() {
Lucas Dupin67f02632018-03-12 11:08:31 -0700451 if (!mExpansionAffectsAlpha) {
452 return;
453 }
454
Lyn Hanbde48202019-05-29 19:18:29 -0700455 if (mState == ScrimState.UNLOCKED || mState == ScrimState.BUBBLE_EXPANDED) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800456 // Darken scrim as you pull down the shade when unlocked
457 float behindFraction = getInterpolatedFraction();
458 behindFraction = (float) Math.pow(behindFraction, 0.8f);
Lyn Hanbde48202019-05-29 19:18:29 -0700459 mBehindAlpha = behindFraction * GRADIENT_SCRIM_ALPHA_BUSY;
460 mInFrontAlpha = 0;
Lucas Dupin1c327432019-01-03 13:37:53 -0800461 } else if (mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800462 // Either darken of make the scrim transparent when you
463 // pull down the shade
464 float interpolatedFract = getInterpolatedFraction();
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800465 float alphaBehind = mState.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800466 if (mDarkenWhileDragging) {
Lyn Hanbde48202019-05-29 19:18:29 -0700467 mBehindAlpha = MathUtils.lerp(GRADIENT_SCRIM_ALPHA_BUSY, alphaBehind,
Lucas Dupinb380c882018-02-25 21:57:17 -0800468 interpolatedFract);
Lyn Hanbde48202019-05-29 19:18:29 -0700469 mInFrontAlpha = 0;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800470 } else {
Lyn Hanbde48202019-05-29 19:18:29 -0700471 mBehindAlpha = MathUtils.lerp(0 /* start */, alphaBehind,
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800472 interpolatedFract);
Lyn Hanbde48202019-05-29 19:18:29 -0700473 mInFrontAlpha = 0;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800474 }
Lyn Hanbde48202019-05-29 19:18:29 -0700475 mBehindTint = ColorUtils.blendARGB(ScrimState.BOUNCER.getBehindTint(),
Lucas Dupin4cf01062019-03-08 17:07:49 -0800476 mState.getBehindTint(), interpolatedFract);
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800477 }
478 }
479
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800480 /**
Anthony Chene658cc22017-04-27 11:17:35 -0700481 * Sets the given drawable as the background of the scrim that shows up behind the
482 * notifications.
483 */
484 public void setScrimBehindDrawable(Drawable drawable) {
485 mScrimBehind.setDrawable(drawable);
486 }
487
Michael Wrightcae88752018-04-16 23:13:54 +0100488 /**
Lucas Dupin69bda602018-05-18 17:24:52 -0700489 * Sets the front scrim opacity in AOD so it's not as bright.
490 * <p>
491 * Displays usually don't support multiple dimming settings when in low power mode.
492 * The workaround is to modify the front scrim opacity when in AOD, so it's not as
493 * bright when you're at the movies or lying down on bed.
494 * <p>
495 * This value will be lost during transitions and only updated again after the the
496 * device is dozing when the light sensor is on.
Michael Wrightcae88752018-04-16 23:13:54 +0100497 */
498 public void setAodFrontScrimAlpha(float alpha) {
Lucas Dupin69bda602018-05-18 17:24:52 -0700499 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()
Lyn Hanbde48202019-05-29 19:18:29 -0700500 && mInFrontAlpha != alpha) {
501 mInFrontAlpha = alpha;
Lucas Dupin8523d132018-05-31 17:34:32 -0700502 updateScrims();
Michael Wrightcae88752018-04-16 23:13:54 +0100503 }
504
505 mState.AOD.setAodFrontScrimAlpha(alpha);
506 }
507
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700508 /**
Lucas Dupin34306c32019-07-16 11:56:53 -0700509 * Set front scrim to black, cancelling animations, in order to prepare to fade them
510 * away once the display turns on.
511 */
512 public void prepareForGentleWakeUp() {
513 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()) {
Mady Mellorfa8c70d2019-07-17 13:44:01 -0700514 mInFrontAlpha = 1f;
Lucas Dupin34306c32019-07-16 11:56:53 -0700515 mAnimateChange = false;
516 updateScrims();
517 mAnimateChange = true;
518 mAnimationDuration = ANIMATION_DURATION_LONG;
519 }
520 }
521
522 /**
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700523 * If the lock screen sensor is active.
524 */
525 public void setWakeLockScreenSensorActive(boolean active) {
526 for (ScrimState state : ScrimState.values()) {
527 state.setWakeLockScreenSensorActive(active);
528 }
529
530 if (mState == ScrimState.PULSING) {
531 float newBehindAlpha = mState.getBehindAlpha();
Lyn Hanbde48202019-05-29 19:18:29 -0700532 if (mBehindAlpha != newBehindAlpha) {
533 mBehindAlpha = newBehindAlpha;
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700534 updateScrims();
535 }
536 }
537 }
538
Evan Rosky9a895012016-04-21 11:26:15 -0700539 protected void scheduleUpdate() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200540 if (mUpdatePending) return;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200541
542 // Make sure that a frame gets scheduled.
543 mScrimBehind.invalidate();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200544 mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this);
545 mUpdatePending = true;
546 }
547
Xiaohui Chen5da71352016-02-22 10:04:41 -0800548 protected void updateScrims() {
Lucas Dupin9324aa92017-07-26 20:29:38 -0700549 // Make sure we have the right gradients and their opacities will satisfy GAR.
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700550 if (mNeedsDrawableColorUpdate) {
551 mNeedsDrawableColorUpdate = false;
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700552 // Only animate scrim color if the scrim view is actually visible
553 boolean animateScrimInFront = mScrimInFront.getViewAlpha() != 0 && !mBlankScreen;
554 boolean animateScrimBehind = mScrimBehind.getViewAlpha() != 0 && !mBlankScreen;
Lyn Hanbde48202019-05-29 19:18:29 -0700555 boolean animateScrimForBubble = mScrimForBubble.getViewAlpha() != 0 && !mBlankScreen;
556
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700557 mScrimInFront.setColors(mColors, animateScrimInFront);
558 mScrimBehind.setColors(mColors, animateScrimBehind);
Lyn Hanbde48202019-05-29 19:18:29 -0700559 mScrimForBubble.setColors(mColors, animateScrimForBubble);
Lucas Dupin9324aa92017-07-26 20:29:38 -0700560
561 // Calculate minimum scrim opacity for white or black text.
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700562 int textColor = mColors.supportsDarkText() ? Color.BLACK : Color.WHITE;
563 int mainColor = mColors.getMainColor();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700564 float minOpacity = ColorUtils.calculateMinimumBackgroundAlpha(textColor, mainColor,
565 4.5f /* minimumContrast */) / 255f;
566 mScrimBehindAlpha = Math.max(mScrimBehindAlphaResValue, minOpacity);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700567 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700568 }
569
Lucas Dupind5107302018-03-19 15:30:29 -0700570 // We want to override the back scrim opacity for the AOD state
Lucas Dupin82aa1632017-12-13 00:13:57 -0800571 // when it's time to fade the wallpaper away.
TYM Tsaia71c8922019-01-07 15:57:53 +0800572 boolean aodWallpaperTimeout = (mState == ScrimState.AOD || mState == ScrimState.PULSING)
573 && mWallpaperVisibilityTimedOut;
Lucas Dupind5107302018-03-19 15:30:29 -0700574 // We also want to hide FLAG_SHOW_WHEN_LOCKED activities under the scrim.
575 boolean occludedKeyguard = (mState == ScrimState.PULSING || mState == ScrimState.AOD)
576 && mKeyguardOccluded;
577 if (aodWallpaperTimeout || occludedKeyguard) {
Lyn Hanbde48202019-05-29 19:18:29 -0700578 mBehindAlpha = 1;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800579 }
Lyn Hanbde48202019-05-29 19:18:29 -0700580 setScrimAlpha(mScrimInFront, mInFrontAlpha);
581 setScrimAlpha(mScrimBehind, mBehindAlpha);
582 setScrimAlpha(mScrimForBubble, mBubbleAlpha);
Adrian Roosa5c63222017-07-27 16:33:39 +0200583 dispatchScrimsVisible();
584 }
585
Yohei Yukawa795f0102018-04-13 14:55:30 -0700586 private void dispatchScrimState(float alpha) {
587 mScrimStateListener.accept(mState, alpha, mScrimInFront.getColors());
588 }
589
Adrian Roosa5c63222017-07-27 16:33:39 +0200590 private void dispatchScrimsVisible() {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800591 final int currentScrimVisibility;
592 if (mScrimInFront.getViewAlpha() == 1 || mScrimBehind.getViewAlpha() == 1) {
Lyn Hanbde48202019-05-29 19:18:29 -0700593 currentScrimVisibility = OPAQUE;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800594 } else if (mScrimInFront.getViewAlpha() == 0 && mScrimBehind.getViewAlpha() == 0) {
Lyn Hanbde48202019-05-29 19:18:29 -0700595 currentScrimVisibility = TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800596 } else {
Lyn Hanbde48202019-05-29 19:18:29 -0700597 currentScrimVisibility = SEMI_TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800598 }
Adrian Roosa5c63222017-07-27 16:33:39 +0200599
Lucas Dupin82aa1632017-12-13 00:13:57 -0800600 if (mScrimsVisibility != currentScrimVisibility) {
601 mScrimsVisibility = currentScrimVisibility;
602 mScrimVisibleListener.accept(currentScrimVisibility);
Adrian Roosa5c63222017-07-27 16:33:39 +0200603 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200604 }
605
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800606 private float getInterpolatedFraction() {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800607 float frac = mExpansionFraction;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200608 // let's start this 20% of the way down the screen
609 frac = frac * 1.2f - 0.2f;
610 if (frac <= 0) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800611 return 0;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200612 } else {
613 // woo, special effects
Lyn Hanbde48202019-05-29 19:18:29 -0700614 return (float) (1f - 0.5f * (1f - Math.cos(3.14159f * Math.pow(1f - frac, 2f))));
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200615 }
616 }
617
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800618 private void setScrimAlpha(ScrimView scrim, float alpha) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800619 if (alpha == 0f) {
620 scrim.setClickable(false);
Selim Cinekaac93252015-04-14 20:04:12 -0700621 } else {
Lucas Dupin1c327432019-01-03 13:37:53 -0800622 // Eat touch events (unless dozing).
623 scrim.setClickable(mState != ScrimState.AOD);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200624 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800625 updateScrim(scrim, alpha);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100626 }
627
Lyn Hanbde48202019-05-29 19:18:29 -0700628 private String getScrimName(ScrimView scrim) {
629 if (scrim == mScrimInFront) {
630 return "front_scrim";
631 } else if (scrim == mScrimBehind) {
632 return "back_scrim";
633 } else if (scrim == mScrimForBubble) {
634 return "bubble_scrim";
635 }
636 return "unknown_scrim";
637 }
638
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800639 private void updateScrimColor(View scrim, float alpha, int tint) {
640 alpha = Math.max(0, Math.min(1.0f, alpha));
Selim Cinekaac93252015-04-14 20:04:12 -0700641 if (scrim instanceof ScrimView) {
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700642 ScrimView scrimView = (ScrimView) scrim;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700643
Lyn Hanbde48202019-05-29 19:18:29 -0700644 Trace.traceCounter(Trace.TRACE_TAG_APP, getScrimName(scrimView) + "_alpha",
Adrian Roosa5c63222017-07-27 16:33:39 +0200645 (int) (alpha * 255));
646
Lyn Hanbde48202019-05-29 19:18:29 -0700647 Trace.traceCounter(Trace.TRACE_TAG_APP, getScrimName(scrimView) + "_tint",
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800648 Color.alpha(tint));
Adrian Roosa5c63222017-07-27 16:33:39 +0200649
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800650 scrimView.setTint(tint);
651 scrimView.setViewAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700652 } else {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800653 scrim.setAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700654 }
Selim Cinek8cd80402017-08-01 10:44:29 -0700655 dispatchScrimsVisible();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100656 }
657
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800658 private void startScrimAnimation(final View scrim, float current) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800659 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
660 final int initialScrimTint = scrim instanceof ScrimView ? ((ScrimView) scrim).getTint() :
661 Color.TRANSPARENT;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700662 anim.addUpdateListener(animation -> {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800663 final float startAlpha = (Float) scrim.getTag(TAG_START_ALPHA);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800664 final float animAmount = (float) animation.getAnimatedValue();
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800665 final int finalScrimTint = getCurrentScrimTint(scrim);
666 final float finalScrimAlpha = getCurrentScrimAlpha(scrim);
667 float alpha = MathUtils.lerp(startAlpha, finalScrimAlpha, animAmount);
668 alpha = MathUtils.constrain(alpha, 0f, 1f);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800669 int tint = ColorUtils.blendARGB(initialScrimTint, finalScrimTint, animAmount);
670 updateScrimColor(scrim, alpha, tint);
Adrian Roosa5c63222017-07-27 16:33:39 +0200671 dispatchScrimsVisible();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200672 });
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800673 anim.setInterpolator(mInterpolator);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200674 anim.setStartDelay(mAnimationDelay);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800675 anim.setDuration(mAnimationDuration);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200676 anim.addListener(new AnimatorListenerAdapter() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800677 private Callback lastCallback = mCallback;
678
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200679 @Override
680 public void onAnimationEnd(Animator animation) {
Joanne Chunga393ad62019-01-16 21:41:31 +0800681 onFinished(lastCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800682
683 scrim.setTag(TAG_KEY_ANIM, null);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800684 dispatchScrimsVisible();
685
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200686 if (!mDeferFinishedListener && mOnAnimationFinished != null) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200687 mOnAnimationFinished.run();
688 mOnAnimationFinished = null;
689 }
690 }
691 });
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800692
693 // Cache alpha values because we might want to update this animator in the future if
694 // the user expands the panel while the animation is still running.
695 scrim.setTag(TAG_START_ALPHA, current);
696 scrim.setTag(TAG_END_ALPHA, getCurrentScrimAlpha(scrim));
697
John Spurlockbf370992014-06-17 13:58:31 -0400698 scrim.setTag(TAG_KEY_ANIM, anim);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800699 anim.start();
700 }
701
702 private float getCurrentScrimAlpha(View scrim) {
703 if (scrim == mScrimInFront) {
Lyn Hanbde48202019-05-29 19:18:29 -0700704 return mInFrontAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800705 } else if (scrim == mScrimBehind) {
Lyn Hanbde48202019-05-29 19:18:29 -0700706 return mBehindAlpha;
707 } else if (scrim == mScrimForBubble) {
708 return mBubbleAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800709 } else {
710 throw new IllegalArgumentException("Unknown scrim view");
711 }
712 }
713
714 private int getCurrentScrimTint(View scrim) {
715 if (scrim == mScrimInFront) {
Lyn Hanbde48202019-05-29 19:18:29 -0700716 return mInFrontTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800717 } else if (scrim == mScrimBehind) {
Lyn Hanbde48202019-05-29 19:18:29 -0700718 return mBehindTint;
719 } else if (scrim == mScrimForBubble) {
720 return mBubbleTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800721 } else {
722 throw new IllegalArgumentException("Unknown scrim view");
723 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200724 }
725
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200726 @Override
727 public boolean onPreDraw() {
728 mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this);
729 mUpdatePending = false;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800730 if (mCallback != null) {
731 mCallback.onStart();
Selim Cinek372d1bd2015-08-14 13:19:37 -0700732 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200733 updateScrims();
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800734 if (mOnAnimationFinished != null && !isAnimating(mScrimInFront)
735 && !isAnimating(mScrimBehind)) {
736 mOnAnimationFinished.run();
737 mOnAnimationFinished = null;
738 }
Selim Cinekedd32b82015-06-23 22:05:58 -0400739 return true;
740 }
741
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800742 private void onFinished() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800743 onFinished(mCallback);
744 }
745
746 private void onFinished(Callback callback) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800747 if (mWakeLockHeld) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800748 mWakeLock.release(TAG);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800749 mWakeLockHeld = false;
750 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800751
752 if (callback != null) {
753 callback.onFinished();
754
755 if (callback == mCallback) {
756 mCallback = null;
757 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800758 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800759
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800760 // When unlocking with fingerprint, we'll fade the scrims from black to transparent.
761 // At the end of the animation we need to remove the tint.
762 if (mState == ScrimState.UNLOCKED) {
Lyn Hanbde48202019-05-29 19:18:29 -0700763 mInFrontTint = Color.TRANSPARENT;
764 mBehindTint = Color.TRANSPARENT;
765 mBubbleTint = Color.TRANSPARENT;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800766 }
767 }
768
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700769 private boolean isAnimating(View scrim) {
770 return scrim.getTag(TAG_KEY_ANIM) != null;
771 }
772
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800773 @VisibleForTesting
774 void setOnAnimationFinished(Runnable onAnimationFinished) {
775 mOnAnimationFinished = onAnimationFinished;
776 }
Adrian Roos5f72c922016-03-08 15:54:26 -0800777
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800778 private void updateScrim(ScrimView scrim, float alpha) {
779 final float currentAlpha = scrim.getViewAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800780
781 ValueAnimator previousAnimator = ViewState.getChildTag(scrim, TAG_KEY_ANIM);
Selim Cinekaac93252015-04-14 20:04:12 -0700782 if (previousAnimator != null) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800783 if (mAnimateChange) {
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200784 // We are not done yet! Defer calling the finished listener.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800785 mDeferFinishedListener = true;
Selim Cinekaac93252015-04-14 20:04:12 -0700786 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800787 // Previous animators should always be cancelled. Not doing so would cause
788 // overlap, especially on states that don't animate, leading to flickering,
789 // and in the worst case, an internal state that doesn't represent what
790 // transitionTo requested.
791 cancelAnimator(previousAnimator);
792 mDeferFinishedListener = false;
Selim Cinekaac93252015-04-14 20:04:12 -0700793 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800794
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800795 if (mPendingFrameCallback != null) {
796 // Display is off and we're waiting.
797 return;
798 } else if (mBlankScreen) {
799 // Need to blank the display before continuing.
800 blankDisplay();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800801 return;
802 } else if (!mScreenBlankingCallbackCalled) {
803 // Not blanking the screen. Letting the callback know that we're ready
804 // to replace what was on the screen before.
805 if (mCallback != null) {
806 mCallback.onDisplayBlanked();
807 mScreenBlankingCallbackCalled = true;
808 }
809 }
810
Lucas Dupin6afae372017-11-30 13:09:22 -0500811 if (scrim == mScrimBehind) {
Yohei Yukawa795f0102018-04-13 14:55:30 -0700812 dispatchScrimState(alpha);
Lucas Dupin6afae372017-11-30 13:09:22 -0500813 }
814
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800815 final boolean wantsAlphaUpdate = alpha != currentAlpha;
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800816 final boolean wantsTintUpdate = scrim.getTint() != getCurrentScrimTint(scrim);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800817
818 if (wantsAlphaUpdate || wantsTintUpdate) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800819 if (mAnimateChange) {
820 startScrimAnimation(scrim, currentAlpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700821 } else {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800822 // update the alpha directly
823 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
824 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700825 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800826 } else {
827 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700828 }
829 }
830
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800831 @VisibleForTesting
832 protected void cancelAnimator(ValueAnimator previousAnimator) {
833 if (previousAnimator != null) {
834 previousAnimator.cancel();
835 }
836 }
837
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800838 private void blankDisplay() {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800839 updateScrimColor(mScrimInFront, 1, Color.BLACK);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800840
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800841 // Notify callback that the screen is completely black and we're
842 // ready to change the display power mode
Lucas Dupin0791d972018-03-26 13:32:16 -0700843 mPendingFrameCallback = () -> {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800844 if (mCallback != null) {
845 mCallback.onDisplayBlanked();
846 mScreenBlankingCallbackCalled = true;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800847 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800848
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800849 mBlankingTransitionRunnable = () -> {
850 mBlankingTransitionRunnable = null;
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800851 mPendingFrameCallback = null;
852 mBlankScreen = false;
853 // Try again.
854 updateScrims();
855 };
856
857 // Setting power states can happen after we push out the frame. Make sure we
858 // stay fully opaque until the power state request reaches the lower levels.
Lucas Dupin0791d972018-03-26 13:32:16 -0700859 final int delay = mScreenOn ? 32 : 500;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800860 if (DEBUG) {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800861 Log.d(TAG, "Fading out scrims with delay: " + delay);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800862 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800863 mHandler.postDelayed(mBlankingTransitionRunnable, delay);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800864 };
865 doOnTheNextFrame(mPendingFrameCallback);
866 }
867
Lucas Dupin0791d972018-03-26 13:32:16 -0700868 /**
869 * Executes a callback after the frame has hit the display.
Lyn Hanbde48202019-05-29 19:18:29 -0700870 *
Lucas Dupin0791d972018-03-26 13:32:16 -0700871 * @param callback What to run.
872 */
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800873 @VisibleForTesting
Lucas Dupin0791d972018-03-26 13:32:16 -0700874 protected void doOnTheNextFrame(Runnable callback) {
875 // Just calling View#postOnAnimation isn't enough because the frame might not have reached
876 // the display yet. A timeout is the safest solution.
877 mScrimBehind.postOnAnimationDelayed(callback, 32 /* delayMillis */);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800878 }
879
880 @VisibleForTesting
881 protected Handler getHandler() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800882 return new Handler();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800883 }
884
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700885 public int getBackgroundColor() {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700886 int color = mColors.getMainColor();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800887 return Color.argb((int) (mScrimBehind.getViewAlpha() * Color.alpha(color)),
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700888 Color.red(color), Color.green(color), Color.blue(color));
Selim Cinekd35c2792016-01-21 13:20:57 -0800889 }
890
891 public void setScrimBehindChangeRunnable(Runnable changeRunnable) {
892 mScrimBehind.setChangeRunnable(changeRunnable);
893 }
Selim Cineke803491c2016-04-09 21:24:45 -0700894
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700895 public void setCurrentUser(int currentUser) {
896 // Don't care in the base class.
897 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700898
899 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700900 public void onColorsChanged(ColorExtractor colorExtractor, int which) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700901 mColors = mColorExtractor.getNeutralColors();
902 mNeedsDrawableColorUpdate = true;
903 scheduleUpdate();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700904 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200905
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800906 @VisibleForTesting
907 protected WakeLock createWakeLock() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800908 return new DelayedWakeLock(mHandler, WakeLock.createPartial(mContext, "Scrims"));
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800909 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200910
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800911 @Override
912 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800913 pw.println(" ScrimController: ");
Lyn Hanbde48202019-05-29 19:18:29 -0700914 pw.print(" state: ");
915 pw.println(mState);
Adrian Roosba7ca592017-08-15 17:48:05 +0200916
Lyn Hanbde48202019-05-29 19:18:29 -0700917 pw.print(" frontScrim:");
918 pw.print(" viewAlpha=");
919 pw.print(mScrimInFront.getViewAlpha());
920 pw.print(" alpha=");
921 pw.print(mInFrontAlpha);
922 pw.print(" tint=0x");
923 pw.println(Integer.toHexString(mScrimInFront.getTint()));
Adrian Roosba7ca592017-08-15 17:48:05 +0200924
Lyn Hanbde48202019-05-29 19:18:29 -0700925 pw.print(" backScrim:");
926 pw.print(" viewAlpha=");
927 pw.print(mScrimBehind.getViewAlpha());
928 pw.print(" alpha=");
929 pw.print(mBehindAlpha);
930 pw.print(" tint=0x");
931 pw.println(Integer.toHexString(mScrimBehind.getTint()));
932
933 pw.print(" bubbleScrim:");
934 pw.print(" viewAlpha=");
935 pw.print(mScrimForBubble.getViewAlpha());
936 pw.print(" alpha=");
937 pw.print(mBubbleAlpha);
938 pw.print(" tint=0x");
939 pw.println(Integer.toHexString(mScrimForBubble.getTint()));
940
941 pw.print(" mTracking=");
942 pw.println(mTracking);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800943 }
944
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700945 public void setWallpaperSupportsAmbientMode(boolean wallpaperSupportsAmbientMode) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800946 mWallpaperSupportsAmbientMode = wallpaperSupportsAmbientMode;
947 ScrimState[] states = ScrimState.values();
948 for (int i = 0; i < states.length; i++) {
949 states[i].setWallpaperSupportsAmbientMode(wallpaperSupportsAmbientMode);
950 }
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700951 }
952
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800953 /**
954 * Interrupts blanking transitions once the display notifies that it's already on.
955 */
956 public void onScreenTurnedOn() {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800957 mScreenOn = true;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800958 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800959 if (DEBUG) {
960 Log.d(TAG, "Shorter blanking because screen turned on. All good.");
961 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800962 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800963 mBlankingTransitionRunnable.run();
964 }
965 }
966
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800967 public void onScreenTurnedOff() {
968 mScreenOn = false;
969 }
970
Lucas Dupin67f02632018-03-12 11:08:31 -0700971 public void setExpansionAffectsAlpha(boolean expansionAffectsAlpha) {
972 mExpansionAffectsAlpha = expansionAffectsAlpha;
973 }
974
Lucas Dupind5107302018-03-19 15:30:29 -0700975 public void setKeyguardOccluded(boolean keyguardOccluded) {
976 mKeyguardOccluded = keyguardOccluded;
Lucas Dupin63d72172018-06-06 11:42:55 -0700977 updateScrims();
Lucas Dupind5107302018-03-19 15:30:29 -0700978 }
979
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700980 public void setHasBackdrop(boolean hasBackdrop) {
Lucas Dupin193677c2018-06-11 19:16:03 -0700981 for (ScrimState state : ScrimState.values()) {
982 state.setHasBackdrop(hasBackdrop);
983 }
Lucas Dupin9bee5822018-07-09 14:32:53 -0700984
985 // Backdrop event may arrive after state was already applied,
986 // in this case, back-scrim needs to be re-evaluated
987 if (mState == ScrimState.AOD || mState == ScrimState.PULSING) {
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800988 float newBehindAlpha = mState.getBehindAlpha();
Lyn Hanbde48202019-05-29 19:18:29 -0700989 if (mBehindAlpha != newBehindAlpha) {
990 mBehindAlpha = newBehindAlpha;
Lucas Dupin9bee5822018-07-09 14:32:53 -0700991 updateScrims();
992 }
993 }
Lucas Dupin193677c2018-06-11 19:16:03 -0700994 }
995
Selim Cinek84b2acc2019-07-07 00:40:38 -0700996 public void setUnlockIsFading(boolean unlockFading) {
997 for (ScrimState state : ScrimState.values()) {
998 state.setUnlockIsFading(unlockFading);
999 }
1000 }
1001
Lucas Dupin193677c2018-06-11 19:16:03 -07001002 public void setLaunchingAffordanceWithPreview(boolean launchingAffordanceWithPreview) {
1003 for (ScrimState state : ScrimState.values()) {
1004 state.setLaunchingAffordanceWithPreview(launchingAffordanceWithPreview);
Lucas Dupinf8463ee2018-06-11 16:18:15 -07001005 }
1006 }
1007
Lucas Dupin9e3fa102017-11-08 17:16:55 -08001008 public interface Callback {
1009 default void onStart() {
1010 }
Lyn Hanbde48202019-05-29 19:18:29 -07001011
Lucas Dupin9e3fa102017-11-08 17:16:55 -08001012 default void onDisplayBlanked() {
1013 }
Lyn Hanbde48202019-05-29 19:18:29 -07001014
Lucas Dupin9e3fa102017-11-08 17:16:55 -08001015 default void onFinished() {
1016 }
Lyn Hanbde48202019-05-29 19:18:29 -07001017
Lucas Dupin9e3fa102017-11-08 17:16:55 -08001018 default void onCancelled() {
1019 }
Jerry Changbde4c0c2019-06-13 16:58:41 +08001020 /** Returns whether to timeout wallpaper or not. */
1021 default boolean shouldTimeoutWallpaper() {
1022 return false;
1023 }
Adrian Roosba7ca592017-08-15 17:48:05 +02001024 }
Lucas Dupin690c6f52018-07-10 15:28:57 -07001025
1026 /**
1027 * Simple keyguard callback that updates scrims when keyguard visibility changes.
1028 */
1029 private class KeyguardVisibilityCallback extends KeyguardUpdateMonitorCallback {
1030
1031 @Override
1032 public void onKeyguardVisibilityChanged(boolean showing) {
1033 mNeedsDrawableColorUpdate = true;
1034 scheduleUpdate();
1035 }
1036 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +02001037}