blob: 9971c90ac82e5d8345b8e64b4480f38a987af01c [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.
Lucas Dupin023d7272019-01-28 14:31:40 -0800305 if (mWallpaperSupportsAmbientMode && mDozeParameters.getAlwaysOn()
306 && mState == ScrimState.AOD) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800307 if (!mWallpaperVisibilityTimedOut) {
308 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
309 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800310 }
Lucas Dupinc7804042018-12-21 12:26:33 -0800311 } else {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800312 mTimeTicker.cancel();
313 mWallpaperVisibilityTimedOut = false;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800314 }
315
Lucas Dupinff307d52018-02-05 21:16:39 -0800316 if (mKeyguardUpdateMonitor.needsSlowUnlockTransition() && mState == ScrimState.UNLOCKED) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800317 // In case the user isn't unlocked, make sure to delay a bit because the system is hosed
318 // with too many things at this case, in order to not skip the initial frames.
319 mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16);
320 mAnimationDelay = StatusBar.FADE_KEYGUARD_START_DELAY;
Lucas Dupin0791d972018-03-26 13:32:16 -0700321 } else if ((!mDozeParameters.getAlwaysOn() && oldState == ScrimState.AOD)
Lucas Dupin16cfe452018-02-08 13:14:50 -0800322 || (mState == ScrimState.AOD && !mDozeParameters.getDisplayNeedsBlanking())) {
323 // Scheduling a frame isn't enough when:
324 // • Leaving doze and we need to modify scrim color immediately
325 // • ColorFade will not kick-in and scrim cannot wait for pre-draw.
Lucas Dupinff307d52018-02-05 21:16:39 -0800326 onPreDraw();
327 } else {
328 scheduleUpdate();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800329 }
Yohei Yukawa795f0102018-04-13 14:55:30 -0700330
331 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800332 }
333
334 public ScrimState getState() {
335 return mState;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200336 }
337
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800338 protected void setScrimBehindValues(float scrimBehindAlphaKeyguard) {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700339 mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800340 ScrimState[] states = ScrimState.values();
341 for (int i = 0; i < states.length; i++) {
342 states[i].setScrimBehindAlphaKeyguard(scrimBehindAlphaKeyguard);
343 }
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700344 scheduleUpdate();
345 }
346
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200347 public void onTrackingStarted() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800348 mTracking = true;
Selim Cineke8bae622015-07-15 13:24:06 -0700349 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200350 }
351
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200352 public void onExpandingFinished() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800353 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200354 }
355
Lucas Dupin82aa1632017-12-13 00:13:57 -0800356 @VisibleForTesting
357 protected void onHideWallpaperTimeout() {
TYM Tsaia71c8922019-01-07 15:57:53 +0800358 if (mState != ScrimState.AOD && mState != ScrimState.PULSING) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800359 return;
360 }
361
362 holdWakeLock();
363 mWallpaperVisibilityTimedOut = true;
364 mAnimateChange = true;
365 mAnimationDuration = mDozeParameters.getWallpaperFadeOutDuration();
366 scheduleUpdate();
367 }
368
369 private void holdWakeLock() {
370 if (!mWakeLockHeld) {
371 if (mWakeLock != null) {
372 mWakeLockHeld = true;
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800373 mWakeLock.acquire(TAG);
Lucas Dupin82aa1632017-12-13 00:13:57 -0800374 } else {
375 Log.w(TAG, "Cannot hold wake lock, it has not been set yet");
376 }
377 }
378 }
379
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800380 /**
381 * Current state of the shade expansion when pulling it from the top.
382 * This value is 1 when on top of the keyguard and goes to 0 as the user drags up.
383 *
384 * The expansion fraction is tied to the scrim opacity.
385 *
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800386 * @param fraction From 0 to 1 where 0 means collapsed and 1 expanded.
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800387 */
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200388 public void setPanelExpansion(float fraction) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800389 if (mExpansionFraction != fraction) {
390 mExpansionFraction = fraction;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800391
Lyn Hanbde48202019-05-29 19:18:29 -0700392 boolean relevantState = (mState == ScrimState.UNLOCKED
393 || mState == ScrimState.KEYGUARD
394 || mState == ScrimState.PULSING
395 || mState == ScrimState.BUBBLE_EXPANDED);
396 if (!(relevantState && mExpansionAffectsAlpha)) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800397 return;
398 }
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800399 applyExpansionToAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800400 if (mUpdatePending) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800401 return;
402 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800403 setOrAdaptCurrentAnimation(mScrimBehind);
404 setOrAdaptCurrentAnimation(mScrimInFront);
Lyn Hanbde48202019-05-29 19:18:29 -0700405 setOrAdaptCurrentAnimation(mScrimForBubble);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700406 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800407 }
408 }
409
410 private void setOrAdaptCurrentAnimation(View scrim) {
Lyn Hanbde48202019-05-29 19:18:29 -0700411 float alpha = getCurrentScrimAlpha(scrim);
412 if (isAnimating(scrim)) {
413 // Adapt current animation.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800414 ValueAnimator previousAnimator = (ValueAnimator) scrim.getTag(TAG_KEY_ANIM);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800415 float previousEndValue = (Float) scrim.getTag(TAG_END_ALPHA);
416 float previousStartValue = (Float) scrim.getTag(TAG_START_ALPHA);
417 float relativeDiff = alpha - previousEndValue;
418 float newStartValue = previousStartValue + relativeDiff;
419 scrim.setTag(TAG_START_ALPHA, newStartValue);
420 scrim.setTag(TAG_END_ALPHA, alpha);
421 previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
Lyn Hanbde48202019-05-29 19:18:29 -0700422 } else {
423 // Set animation.
424 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
Jorim Jaggi93439da2014-06-30 23:53:39 +0200425 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200426 }
427
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800428 private void applyExpansionToAlpha() {
Lucas Dupin67f02632018-03-12 11:08:31 -0700429 if (!mExpansionAffectsAlpha) {
430 return;
431 }
432
Lyn Hanbde48202019-05-29 19:18:29 -0700433 if (mState == ScrimState.UNLOCKED || mState == ScrimState.BUBBLE_EXPANDED) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800434 // Darken scrim as you pull down the shade when unlocked
435 float behindFraction = getInterpolatedFraction();
436 behindFraction = (float) Math.pow(behindFraction, 0.8f);
Lyn Hanbde48202019-05-29 19:18:29 -0700437 mBehindAlpha = behindFraction * GRADIENT_SCRIM_ALPHA_BUSY;
438 mInFrontAlpha = 0;
Lucas Dupin1c327432019-01-03 13:37:53 -0800439 } else if (mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800440 // Either darken of make the scrim transparent when you
441 // pull down the shade
442 float interpolatedFract = getInterpolatedFraction();
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800443 float alphaBehind = mState.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800444 if (mDarkenWhileDragging) {
Lyn Hanbde48202019-05-29 19:18:29 -0700445 mBehindAlpha = MathUtils.lerp(GRADIENT_SCRIM_ALPHA_BUSY, alphaBehind,
Lucas Dupinb380c882018-02-25 21:57:17 -0800446 interpolatedFract);
Lyn Hanbde48202019-05-29 19:18:29 -0700447 mInFrontAlpha = 0;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800448 } else {
Lyn Hanbde48202019-05-29 19:18:29 -0700449 mBehindAlpha = MathUtils.lerp(0 /* start */, alphaBehind,
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800450 interpolatedFract);
Lyn Hanbde48202019-05-29 19:18:29 -0700451 mInFrontAlpha = 0;
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800452 }
Lyn Hanbde48202019-05-29 19:18:29 -0700453 mBehindTint = ColorUtils.blendARGB(ScrimState.BOUNCER.getBehindTint(),
Lucas Dupin4cf01062019-03-08 17:07:49 -0800454 mState.getBehindTint(), interpolatedFract);
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800455 }
456 }
457
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800458 /**
Anthony Chene658cc22017-04-27 11:17:35 -0700459 * Sets the given drawable as the background of the scrim that shows up behind the
460 * notifications.
461 */
462 public void setScrimBehindDrawable(Drawable drawable) {
463 mScrimBehind.setDrawable(drawable);
464 }
465
Michael Wrightcae88752018-04-16 23:13:54 +0100466 /**
Lucas Dupin69bda602018-05-18 17:24:52 -0700467 * Sets the front scrim opacity in AOD so it's not as bright.
468 * <p>
469 * Displays usually don't support multiple dimming settings when in low power mode.
470 * The workaround is to modify the front scrim opacity when in AOD, so it's not as
471 * bright when you're at the movies or lying down on bed.
472 * <p>
473 * This value will be lost during transitions and only updated again after the the
474 * device is dozing when the light sensor is on.
Michael Wrightcae88752018-04-16 23:13:54 +0100475 */
476 public void setAodFrontScrimAlpha(float alpha) {
Lucas Dupin69bda602018-05-18 17:24:52 -0700477 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()
Lyn Hanbde48202019-05-29 19:18:29 -0700478 && mInFrontAlpha != alpha) {
479 mInFrontAlpha = alpha;
Lucas Dupin8523d132018-05-31 17:34:32 -0700480 updateScrims();
Michael Wrightcae88752018-04-16 23:13:54 +0100481 }
482
483 mState.AOD.setAodFrontScrimAlpha(alpha);
484 }
485
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700486 /**
487 * If the lock screen sensor is active.
488 */
489 public void setWakeLockScreenSensorActive(boolean active) {
490 for (ScrimState state : ScrimState.values()) {
491 state.setWakeLockScreenSensorActive(active);
492 }
493
494 if (mState == ScrimState.PULSING) {
495 float newBehindAlpha = mState.getBehindAlpha();
Lyn Hanbde48202019-05-29 19:18:29 -0700496 if (mBehindAlpha != newBehindAlpha) {
497 mBehindAlpha = newBehindAlpha;
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700498 updateScrims();
499 }
500 }
501 }
502
Evan Rosky9a895012016-04-21 11:26:15 -0700503 protected void scheduleUpdate() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200504 if (mUpdatePending) return;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200505
506 // Make sure that a frame gets scheduled.
507 mScrimBehind.invalidate();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200508 mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this);
509 mUpdatePending = true;
510 }
511
Xiaohui Chen5da71352016-02-22 10:04:41 -0800512 protected void updateScrims() {
Lucas Dupin9324aa92017-07-26 20:29:38 -0700513 // Make sure we have the right gradients and their opacities will satisfy GAR.
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700514 if (mNeedsDrawableColorUpdate) {
515 mNeedsDrawableColorUpdate = false;
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700516 // Only animate scrim color if the scrim view is actually visible
517 boolean animateScrimInFront = mScrimInFront.getViewAlpha() != 0 && !mBlankScreen;
518 boolean animateScrimBehind = mScrimBehind.getViewAlpha() != 0 && !mBlankScreen;
Lyn Hanbde48202019-05-29 19:18:29 -0700519 boolean animateScrimForBubble = mScrimForBubble.getViewAlpha() != 0 && !mBlankScreen;
520
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700521 mScrimInFront.setColors(mColors, animateScrimInFront);
522 mScrimBehind.setColors(mColors, animateScrimBehind);
Lyn Hanbde48202019-05-29 19:18:29 -0700523 mScrimForBubble.setColors(mColors, animateScrimForBubble);
Lucas Dupin9324aa92017-07-26 20:29:38 -0700524
525 // Calculate minimum scrim opacity for white or black text.
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700526 int textColor = mColors.supportsDarkText() ? Color.BLACK : Color.WHITE;
527 int mainColor = mColors.getMainColor();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700528 float minOpacity = ColorUtils.calculateMinimumBackgroundAlpha(textColor, mainColor,
529 4.5f /* minimumContrast */) / 255f;
530 mScrimBehindAlpha = Math.max(mScrimBehindAlphaResValue, minOpacity);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700531 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700532 }
533
Lucas Dupind5107302018-03-19 15:30:29 -0700534 // We want to override the back scrim opacity for the AOD state
Lucas Dupin82aa1632017-12-13 00:13:57 -0800535 // when it's time to fade the wallpaper away.
TYM Tsaia71c8922019-01-07 15:57:53 +0800536 boolean aodWallpaperTimeout = (mState == ScrimState.AOD || mState == ScrimState.PULSING)
537 && mWallpaperVisibilityTimedOut;
Lucas Dupind5107302018-03-19 15:30:29 -0700538 // We also want to hide FLAG_SHOW_WHEN_LOCKED activities under the scrim.
539 boolean occludedKeyguard = (mState == ScrimState.PULSING || mState == ScrimState.AOD)
540 && mKeyguardOccluded;
541 if (aodWallpaperTimeout || occludedKeyguard) {
Lyn Hanbde48202019-05-29 19:18:29 -0700542 mBehindAlpha = 1;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800543 }
Lyn Hanbde48202019-05-29 19:18:29 -0700544 setScrimAlpha(mScrimInFront, mInFrontAlpha);
545 setScrimAlpha(mScrimBehind, mBehindAlpha);
546 setScrimAlpha(mScrimForBubble, mBubbleAlpha);
Adrian Roosa5c63222017-07-27 16:33:39 +0200547 dispatchScrimsVisible();
548 }
549
Yohei Yukawa795f0102018-04-13 14:55:30 -0700550 private void dispatchScrimState(float alpha) {
551 mScrimStateListener.accept(mState, alpha, mScrimInFront.getColors());
552 }
553
Adrian Roosa5c63222017-07-27 16:33:39 +0200554 private void dispatchScrimsVisible() {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800555 final int currentScrimVisibility;
556 if (mScrimInFront.getViewAlpha() == 1 || mScrimBehind.getViewAlpha() == 1) {
Lyn Hanbde48202019-05-29 19:18:29 -0700557 currentScrimVisibility = OPAQUE;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800558 } else if (mScrimInFront.getViewAlpha() == 0 && mScrimBehind.getViewAlpha() == 0) {
Lyn Hanbde48202019-05-29 19:18:29 -0700559 currentScrimVisibility = TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800560 } else {
Lyn Hanbde48202019-05-29 19:18:29 -0700561 currentScrimVisibility = SEMI_TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800562 }
Adrian Roosa5c63222017-07-27 16:33:39 +0200563
Lucas Dupin82aa1632017-12-13 00:13:57 -0800564 if (mScrimsVisibility != currentScrimVisibility) {
565 mScrimsVisibility = currentScrimVisibility;
566 mScrimVisibleListener.accept(currentScrimVisibility);
Adrian Roosa5c63222017-07-27 16:33:39 +0200567 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200568 }
569
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800570 private float getInterpolatedFraction() {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800571 float frac = mExpansionFraction;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200572 // let's start this 20% of the way down the screen
573 frac = frac * 1.2f - 0.2f;
574 if (frac <= 0) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800575 return 0;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200576 } else {
577 // woo, special effects
Lyn Hanbde48202019-05-29 19:18:29 -0700578 return (float) (1f - 0.5f * (1f - Math.cos(3.14159f * Math.pow(1f - frac, 2f))));
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200579 }
580 }
581
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800582 private void setScrimAlpha(ScrimView scrim, float alpha) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800583 if (alpha == 0f) {
584 scrim.setClickable(false);
Selim Cinekaac93252015-04-14 20:04:12 -0700585 } else {
Lucas Dupin1c327432019-01-03 13:37:53 -0800586 // Eat touch events (unless dozing).
587 scrim.setClickable(mState != ScrimState.AOD);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200588 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800589 updateScrim(scrim, alpha);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100590 }
591
Lyn Hanbde48202019-05-29 19:18:29 -0700592 private String getScrimName(ScrimView scrim) {
593 if (scrim == mScrimInFront) {
594 return "front_scrim";
595 } else if (scrim == mScrimBehind) {
596 return "back_scrim";
597 } else if (scrim == mScrimForBubble) {
598 return "bubble_scrim";
599 }
600 return "unknown_scrim";
601 }
602
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800603 private void updateScrimColor(View scrim, float alpha, int tint) {
604 alpha = Math.max(0, Math.min(1.0f, alpha));
Selim Cinekaac93252015-04-14 20:04:12 -0700605 if (scrim instanceof ScrimView) {
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700606 ScrimView scrimView = (ScrimView) scrim;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700607
Lyn Hanbde48202019-05-29 19:18:29 -0700608 Trace.traceCounter(Trace.TRACE_TAG_APP, getScrimName(scrimView) + "_alpha",
Adrian Roosa5c63222017-07-27 16:33:39 +0200609 (int) (alpha * 255));
610
Lyn Hanbde48202019-05-29 19:18:29 -0700611 Trace.traceCounter(Trace.TRACE_TAG_APP, getScrimName(scrimView) + "_tint",
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800612 Color.alpha(tint));
Adrian Roosa5c63222017-07-27 16:33:39 +0200613
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800614 scrimView.setTint(tint);
615 scrimView.setViewAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700616 } else {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800617 scrim.setAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700618 }
Selim Cinek8cd80402017-08-01 10:44:29 -0700619 dispatchScrimsVisible();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100620 }
621
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800622 private void startScrimAnimation(final View scrim, float current) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800623 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
624 final int initialScrimTint = scrim instanceof ScrimView ? ((ScrimView) scrim).getTint() :
625 Color.TRANSPARENT;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700626 anim.addUpdateListener(animation -> {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800627 final float startAlpha = (Float) scrim.getTag(TAG_START_ALPHA);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800628 final float animAmount = (float) animation.getAnimatedValue();
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800629 final int finalScrimTint = getCurrentScrimTint(scrim);
630 final float finalScrimAlpha = getCurrentScrimAlpha(scrim);
631 float alpha = MathUtils.lerp(startAlpha, finalScrimAlpha, animAmount);
632 alpha = MathUtils.constrain(alpha, 0f, 1f);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800633 int tint = ColorUtils.blendARGB(initialScrimTint, finalScrimTint, animAmount);
634 updateScrimColor(scrim, alpha, tint);
Adrian Roosa5c63222017-07-27 16:33:39 +0200635 dispatchScrimsVisible();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200636 });
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800637 anim.setInterpolator(mInterpolator);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200638 anim.setStartDelay(mAnimationDelay);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800639 anim.setDuration(mAnimationDuration);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200640 anim.addListener(new AnimatorListenerAdapter() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800641 private Callback lastCallback = mCallback;
642
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200643 @Override
644 public void onAnimationEnd(Animator animation) {
Joanne Chunga393ad62019-01-16 21:41:31 +0800645 onFinished(lastCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800646
647 scrim.setTag(TAG_KEY_ANIM, null);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800648 dispatchScrimsVisible();
649
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200650 if (!mDeferFinishedListener && mOnAnimationFinished != null) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200651 mOnAnimationFinished.run();
652 mOnAnimationFinished = null;
653 }
654 }
655 });
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800656
657 // Cache alpha values because we might want to update this animator in the future if
658 // the user expands the panel while the animation is still running.
659 scrim.setTag(TAG_START_ALPHA, current);
660 scrim.setTag(TAG_END_ALPHA, getCurrentScrimAlpha(scrim));
661
John Spurlockbf370992014-06-17 13:58:31 -0400662 scrim.setTag(TAG_KEY_ANIM, anim);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800663 anim.start();
664 }
665
666 private float getCurrentScrimAlpha(View scrim) {
667 if (scrim == mScrimInFront) {
Lyn Hanbde48202019-05-29 19:18:29 -0700668 return mInFrontAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800669 } else if (scrim == mScrimBehind) {
Lyn Hanbde48202019-05-29 19:18:29 -0700670 return mBehindAlpha;
671 } else if (scrim == mScrimForBubble) {
672 return mBubbleAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800673 } else {
674 throw new IllegalArgumentException("Unknown scrim view");
675 }
676 }
677
678 private int getCurrentScrimTint(View scrim) {
679 if (scrim == mScrimInFront) {
Lyn Hanbde48202019-05-29 19:18:29 -0700680 return mInFrontTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800681 } else if (scrim == mScrimBehind) {
Lyn Hanbde48202019-05-29 19:18:29 -0700682 return mBehindTint;
683 } else if (scrim == mScrimForBubble) {
684 return mBubbleTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800685 } else {
686 throw new IllegalArgumentException("Unknown scrim view");
687 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200688 }
689
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200690 @Override
691 public boolean onPreDraw() {
692 mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this);
693 mUpdatePending = false;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800694 if (mCallback != null) {
695 mCallback.onStart();
Selim Cinek372d1bd2015-08-14 13:19:37 -0700696 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200697 updateScrims();
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800698 if (mOnAnimationFinished != null && !isAnimating(mScrimInFront)
699 && !isAnimating(mScrimBehind)) {
700 mOnAnimationFinished.run();
701 mOnAnimationFinished = null;
702 }
Selim Cinekedd32b82015-06-23 22:05:58 -0400703 return true;
704 }
705
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800706 private void onFinished() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800707 onFinished(mCallback);
708 }
709
710 private void onFinished(Callback callback) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800711 if (mWakeLockHeld) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800712 mWakeLock.release(TAG);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800713 mWakeLockHeld = false;
714 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800715
716 if (callback != null) {
717 callback.onFinished();
718
719 if (callback == mCallback) {
720 mCallback = null;
721 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800722 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800723
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800724 // When unlocking with fingerprint, we'll fade the scrims from black to transparent.
725 // At the end of the animation we need to remove the tint.
726 if (mState == ScrimState.UNLOCKED) {
Lyn Hanbde48202019-05-29 19:18:29 -0700727 mInFrontTint = Color.TRANSPARENT;
728 mBehindTint = Color.TRANSPARENT;
729 mBubbleTint = Color.TRANSPARENT;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800730 }
731 }
732
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700733 private boolean isAnimating(View scrim) {
734 return scrim.getTag(TAG_KEY_ANIM) != null;
735 }
736
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800737 @VisibleForTesting
738 void setOnAnimationFinished(Runnable onAnimationFinished) {
739 mOnAnimationFinished = onAnimationFinished;
740 }
Adrian Roos5f72c922016-03-08 15:54:26 -0800741
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800742 private void updateScrim(ScrimView scrim, float alpha) {
743 final float currentAlpha = scrim.getViewAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800744
745 ValueAnimator previousAnimator = ViewState.getChildTag(scrim, TAG_KEY_ANIM);
Selim Cinekaac93252015-04-14 20:04:12 -0700746 if (previousAnimator != null) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800747 if (mAnimateChange) {
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200748 // We are not done yet! Defer calling the finished listener.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800749 mDeferFinishedListener = true;
Selim Cinekaac93252015-04-14 20:04:12 -0700750 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800751 // Previous animators should always be cancelled. Not doing so would cause
752 // overlap, especially on states that don't animate, leading to flickering,
753 // and in the worst case, an internal state that doesn't represent what
754 // transitionTo requested.
755 cancelAnimator(previousAnimator);
756 mDeferFinishedListener = false;
Selim Cinekaac93252015-04-14 20:04:12 -0700757 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800758
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800759 if (mPendingFrameCallback != null) {
760 // Display is off and we're waiting.
761 return;
762 } else if (mBlankScreen) {
763 // Need to blank the display before continuing.
764 blankDisplay();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800765 return;
766 } else if (!mScreenBlankingCallbackCalled) {
767 // Not blanking the screen. Letting the callback know that we're ready
768 // to replace what was on the screen before.
769 if (mCallback != null) {
770 mCallback.onDisplayBlanked();
771 mScreenBlankingCallbackCalled = true;
772 }
773 }
774
Lucas Dupin6afae372017-11-30 13:09:22 -0500775 if (scrim == mScrimBehind) {
Yohei Yukawa795f0102018-04-13 14:55:30 -0700776 dispatchScrimState(alpha);
Lucas Dupin6afae372017-11-30 13:09:22 -0500777 }
778
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800779 final boolean wantsAlphaUpdate = alpha != currentAlpha;
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800780 final boolean wantsTintUpdate = scrim.getTint() != getCurrentScrimTint(scrim);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800781
782 if (wantsAlphaUpdate || wantsTintUpdate) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800783 if (mAnimateChange) {
784 startScrimAnimation(scrim, currentAlpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700785 } else {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800786 // update the alpha directly
787 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
788 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700789 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800790 } else {
791 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700792 }
793 }
794
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800795 @VisibleForTesting
796 protected void cancelAnimator(ValueAnimator previousAnimator) {
797 if (previousAnimator != null) {
798 previousAnimator.cancel();
799 }
800 }
801
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800802 private void blankDisplay() {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800803 updateScrimColor(mScrimInFront, 1, Color.BLACK);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800804
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800805 // Notify callback that the screen is completely black and we're
806 // ready to change the display power mode
Lucas Dupin0791d972018-03-26 13:32:16 -0700807 mPendingFrameCallback = () -> {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800808 if (mCallback != null) {
809 mCallback.onDisplayBlanked();
810 mScreenBlankingCallbackCalled = true;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800811 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800812
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800813 mBlankingTransitionRunnable = () -> {
814 mBlankingTransitionRunnable = null;
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800815 mPendingFrameCallback = null;
816 mBlankScreen = false;
817 // Try again.
818 updateScrims();
819 };
820
821 // Setting power states can happen after we push out the frame. Make sure we
822 // stay fully opaque until the power state request reaches the lower levels.
Lucas Dupin0791d972018-03-26 13:32:16 -0700823 final int delay = mScreenOn ? 32 : 500;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800824 if (DEBUG) {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800825 Log.d(TAG, "Fading out scrims with delay: " + delay);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800826 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800827 mHandler.postDelayed(mBlankingTransitionRunnable, delay);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800828 };
829 doOnTheNextFrame(mPendingFrameCallback);
830 }
831
Lucas Dupin0791d972018-03-26 13:32:16 -0700832 /**
833 * Executes a callback after the frame has hit the display.
Lyn Hanbde48202019-05-29 19:18:29 -0700834 *
Lucas Dupin0791d972018-03-26 13:32:16 -0700835 * @param callback What to run.
836 */
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800837 @VisibleForTesting
Lucas Dupin0791d972018-03-26 13:32:16 -0700838 protected void doOnTheNextFrame(Runnable callback) {
839 // Just calling View#postOnAnimation isn't enough because the frame might not have reached
840 // the display yet. A timeout is the safest solution.
841 mScrimBehind.postOnAnimationDelayed(callback, 32 /* delayMillis */);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800842 }
843
844 @VisibleForTesting
845 protected Handler getHandler() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800846 return new Handler();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800847 }
848
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700849 public int getBackgroundColor() {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700850 int color = mColors.getMainColor();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800851 return Color.argb((int) (mScrimBehind.getViewAlpha() * Color.alpha(color)),
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700852 Color.red(color), Color.green(color), Color.blue(color));
Selim Cinekd35c2792016-01-21 13:20:57 -0800853 }
854
855 public void setScrimBehindChangeRunnable(Runnable changeRunnable) {
856 mScrimBehind.setChangeRunnable(changeRunnable);
857 }
Selim Cineke803491c2016-04-09 21:24:45 -0700858
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700859 public void setCurrentUser(int currentUser) {
860 // Don't care in the base class.
861 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700862
863 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700864 public void onColorsChanged(ColorExtractor colorExtractor, int which) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700865 mColors = mColorExtractor.getNeutralColors();
866 mNeedsDrawableColorUpdate = true;
867 scheduleUpdate();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700868 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200869
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800870 @VisibleForTesting
871 protected WakeLock createWakeLock() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800872 return new DelayedWakeLock(mHandler, WakeLock.createPartial(mContext, "Scrims"));
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800873 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200874
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800875 @Override
876 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800877 pw.println(" ScrimController: ");
Lyn Hanbde48202019-05-29 19:18:29 -0700878 pw.print(" state: ");
879 pw.println(mState);
Adrian Roosba7ca592017-08-15 17:48:05 +0200880
Lyn Hanbde48202019-05-29 19:18:29 -0700881 pw.print(" frontScrim:");
882 pw.print(" viewAlpha=");
883 pw.print(mScrimInFront.getViewAlpha());
884 pw.print(" alpha=");
885 pw.print(mInFrontAlpha);
886 pw.print(" tint=0x");
887 pw.println(Integer.toHexString(mScrimInFront.getTint()));
Adrian Roosba7ca592017-08-15 17:48:05 +0200888
Lyn Hanbde48202019-05-29 19:18:29 -0700889 pw.print(" backScrim:");
890 pw.print(" viewAlpha=");
891 pw.print(mScrimBehind.getViewAlpha());
892 pw.print(" alpha=");
893 pw.print(mBehindAlpha);
894 pw.print(" tint=0x");
895 pw.println(Integer.toHexString(mScrimBehind.getTint()));
896
897 pw.print(" bubbleScrim:");
898 pw.print(" viewAlpha=");
899 pw.print(mScrimForBubble.getViewAlpha());
900 pw.print(" alpha=");
901 pw.print(mBubbleAlpha);
902 pw.print(" tint=0x");
903 pw.println(Integer.toHexString(mScrimForBubble.getTint()));
904
905 pw.print(" mTracking=");
906 pw.println(mTracking);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800907 }
908
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700909 public void setWallpaperSupportsAmbientMode(boolean wallpaperSupportsAmbientMode) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800910 mWallpaperSupportsAmbientMode = wallpaperSupportsAmbientMode;
911 ScrimState[] states = ScrimState.values();
912 for (int i = 0; i < states.length; i++) {
913 states[i].setWallpaperSupportsAmbientMode(wallpaperSupportsAmbientMode);
914 }
Lucas Dupin7517b5d2017-08-22 12:51:25 -0700915 }
916
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800917 /**
918 * Interrupts blanking transitions once the display notifies that it's already on.
919 */
920 public void onScreenTurnedOn() {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800921 mScreenOn = true;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800922 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800923 if (DEBUG) {
924 Log.d(TAG, "Shorter blanking because screen turned on. All good.");
925 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800926 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800927 mBlankingTransitionRunnable.run();
928 }
929 }
930
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800931 public void onScreenTurnedOff() {
932 mScreenOn = false;
933 }
934
Lucas Dupin67f02632018-03-12 11:08:31 -0700935 public void setExpansionAffectsAlpha(boolean expansionAffectsAlpha) {
936 mExpansionAffectsAlpha = expansionAffectsAlpha;
937 }
938
Lucas Dupind5107302018-03-19 15:30:29 -0700939 public void setKeyguardOccluded(boolean keyguardOccluded) {
940 mKeyguardOccluded = keyguardOccluded;
Lucas Dupin63d72172018-06-06 11:42:55 -0700941 updateScrims();
Lucas Dupind5107302018-03-19 15:30:29 -0700942 }
943
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700944 public void setHasBackdrop(boolean hasBackdrop) {
Lucas Dupin193677c2018-06-11 19:16:03 -0700945 for (ScrimState state : ScrimState.values()) {
946 state.setHasBackdrop(hasBackdrop);
947 }
Lucas Dupin9bee5822018-07-09 14:32:53 -0700948
949 // Backdrop event may arrive after state was already applied,
950 // in this case, back-scrim needs to be re-evaluated
951 if (mState == ScrimState.AOD || mState == ScrimState.PULSING) {
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800952 float newBehindAlpha = mState.getBehindAlpha();
Lyn Hanbde48202019-05-29 19:18:29 -0700953 if (mBehindAlpha != newBehindAlpha) {
954 mBehindAlpha = newBehindAlpha;
Lucas Dupin9bee5822018-07-09 14:32:53 -0700955 updateScrims();
956 }
957 }
Lucas Dupin193677c2018-06-11 19:16:03 -0700958 }
959
960 public void setLaunchingAffordanceWithPreview(boolean launchingAffordanceWithPreview) {
961 for (ScrimState state : ScrimState.values()) {
962 state.setLaunchingAffordanceWithPreview(launchingAffordanceWithPreview);
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700963 }
964 }
965
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800966 public interface Callback {
967 default void onStart() {
968 }
Lyn Hanbde48202019-05-29 19:18:29 -0700969
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800970 default void onDisplayBlanked() {
971 }
Lyn Hanbde48202019-05-29 19:18:29 -0700972
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800973 default void onFinished() {
974 }
Lyn Hanbde48202019-05-29 19:18:29 -0700975
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800976 default void onCancelled() {
977 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200978 }
Lucas Dupin690c6f52018-07-10 15:28:57 -0700979
980 /**
981 * Simple keyguard callback that updates scrims when keyguard visibility changes.
982 */
983 private class KeyguardVisibilityCallback extends KeyguardUpdateMonitorCallback {
984
985 @Override
986 public void onKeyguardVisibilityChanged(boolean showing) {
987 mNeedsDrawableColorUpdate = true;
988 scheduleUpdate();
989 }
990 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200991}