blob: 9019e9a3f44b75eb94018ddd52b002e71e51e7de [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;
Selim Cinek72cd9a72019-08-09 17:19:57 -070050import com.android.systemui.statusbar.policy.KeyguardMonitor;
Lucas Dupin82aa1632017-12-13 00:13:57 -080051import com.android.systemui.util.AlarmTimeout;
Lucas Dupin9e3fa102017-11-08 17:16:55 -080052import com.android.systemui.util.wakelock.DelayedWakeLock;
53import com.android.systemui.util.wakelock.WakeLock;
John Spurlockbf370992014-06-17 13:58:31 -040054
Lucas Dupin9e3fa102017-11-08 17:16:55 -080055import java.io.FileDescriptor;
Adrian Roosba7ca592017-08-15 17:48:05 +020056import java.io.PrintWriter;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070057import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
Adrian Roosa5c63222017-07-27 16:33:39 +020059import java.util.function.Consumer;
60
Jorim Jaggiecc798e2014-05-26 18:14:37 +020061/**
62 * Controls both the scrim behind the notifications and in front of the notifications (when a
63 * security method gets shown).
64 */
Selim Cinek99e9adf2018-03-15 09:17:47 -070065public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnColorsChangedListener,
66 Dumpable {
Lucas Dupin9e3fa102017-11-08 17:16:55 -080067
Lucas Dupin55c6e802018-09-27 18:07:36 -070068 static final String TAG = "ScrimController";
Lucas Dupin9e3fa102017-11-08 17:16:55 -080069 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
70
Lucas Dupin4749f1b2018-04-04 15:09:06 -070071 /**
72 * General scrim animation duration.
73 */
John Spurlock8b12f222014-09-09 11:54:11 -040074 public static final long ANIMATION_DURATION = 220;
Lucas Dupin4749f1b2018-04-04 15:09:06 -070075 /**
76 * Longer duration, currently only used when going to AOD.
77 */
78 public static final long ANIMATION_DURATION_LONG = 1000;
Lucas Dupin82aa1632017-12-13 00:13:57 -080079 /**
80 * When both scrims have 0 alpha.
81 */
Selim Cinekf97d7f72019-10-25 17:37:00 -070082 public static final int VISIBILITY_FULLY_TRANSPARENT = 0;
Lucas Dupin82aa1632017-12-13 00:13:57 -080083 /**
84 * When scrims aren't transparent (alpha 0) but also not opaque (alpha 1.)
85 */
Selim Cinekf97d7f72019-10-25 17:37:00 -070086 public static final int VISIBILITY_SEMI_TRANSPARENT = 1;
Lucas Dupin82aa1632017-12-13 00:13:57 -080087 /**
88 * When at least 1 scrim is fully opaque (alpha set to 1.)
89 */
Selim Cinekf97d7f72019-10-25 17:37:00 -070090 public static final int VISIBILITY_FULLY_OPAQUE = 2;
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070091
Selim Cinekf97d7f72019-10-25 17:37:00 -070092 @IntDef(prefix = { "VISIBILITY_" }, value = {
93 VISIBILITY_FULLY_TRANSPARENT,
94 VISIBILITY_SEMI_TRANSPARENT,
95 VISIBILITY_FULLY_OPAQUE
Lucas Dupincdbb1cb2019-05-16 19:48:30 -070096 })
97 @Retention(RetentionPolicy.SOURCE)
Selim Cinekf97d7f72019-10-25 17:37:00 -070098 public @interface ScrimVisibility {}
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 protected final ScrimView mScrimBehind;
Selim Cinekf97d7f72019-10-25 17:37:00 -0700127 protected final ScrimView mScrimInFront;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800128 private final UnlockMethodCache mUnlockMethodCache;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700129 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800130 private final DozeParameters mDozeParameters;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800131 private final AlarmTimeout mTimeTicker;
Lucas Dupin690c6f52018-07-10 15:28:57 -0700132 private final KeyguardVisibilityCallback mKeyguardVisibilityCallback;
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800133 private final Handler mHandler;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200134
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700135 private final SysuiColorExtractor mColorExtractor;
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700136 private GradientColors mColors;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700137 private boolean mNeedsDrawableColorUpdate;
138
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800139 protected float mScrimBehindAlpha;
Lucas Dupin9324aa92017-07-26 20:29:38 -0700140 protected float mScrimBehindAlphaResValue;
Evan Rosky9a895012016-04-21 11:26:15 -0700141 protected float mScrimBehindAlphaKeyguard = SCRIM_BEHIND_ALPHA_KEYGUARD;
Xiaohui Chen4dab4b52016-03-07 12:31:14 -0800142
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800143 // Assuming the shade is expanded during initialization
144 private float mExpansionFraction = 1f;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200145
146 private boolean mDarkenWhileDragging;
Lucas Dupin67f02632018-03-12 11:08:31 -0700147 private boolean mExpansionAffectsAlpha = true;
Evan Rosky9a895012016-04-21 11:26:15 -0700148 protected boolean mAnimateChange;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200149 private boolean mUpdatePending;
Jorim Jaggie93698b2016-11-11 18:24:38 -0800150 private boolean mTracking;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800151 protected long mAnimationDuration = -1;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200152 private long mAnimationDelay;
153 private Runnable mOnAnimationFinished;
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200154 private boolean mDeferFinishedListener;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200155 private final Interpolator mInterpolator = new DecelerateInterpolator();
Selim Cinekf97d7f72019-10-25 17:37:00 -0700156 private float mCurrentInFrontAlpha = NOT_INITIALIZED;
157 private float mCurrentBehindAlpha = NOT_INITIALIZED;
158 private int mCurrentInFrontTint;
159 private int mCurrentBehindTint;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800160 private boolean mWallpaperVisibilityTimedOut;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800161 private int mScrimsVisibility;
Yohei Yukawa795f0102018-04-13 14:55:30 -0700162 private final TriConsumer<ScrimState, Float, GradientColors> mScrimStateListener;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800163 private final Consumer<Integer> mScrimVisibleListener;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800164 private boolean mBlankScreen;
165 private boolean mScreenBlankingCallbackCalled;
166 private Callback mCallback;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800167 private boolean mWallpaperSupportsAmbientMode;
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800168 private boolean mScreenOn;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800169
170 // Scrim blanking callbacks
Lucas Dupin0791d972018-03-26 13:32:16 -0700171 private Runnable mPendingFrameCallback;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800172 private Runnable mBlankingTransitionRunnable;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800173
174 private final WakeLock mWakeLock;
175 private boolean mWakeLockHeld;
Lucas Dupind5107302018-03-19 15:30:29 -0700176 private boolean mKeyguardOccluded;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200177
Selim Cinekf97d7f72019-10-25 17:37:00 -0700178 public ScrimController(ScrimView scrimBehind, ScrimView scrimInFront,
Yohei Yukawa795f0102018-04-13 14:55:30 -0700179 TriConsumer<ScrimState, Float, GradientColors> scrimStateListener,
shawnlin317db372018-04-09 19:49:48 +0800180 Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters,
Selim Cinek72cd9a72019-08-09 17:19:57 -0700181 AlarmManager alarmManager, KeyguardMonitor keyguardMonitor) {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200182 mScrimBehind = scrimBehind;
183 mScrimInFront = scrimInFront;
Yohei Yukawa795f0102018-04-13 14:55:30 -0700184 mScrimStateListener = scrimStateListener;
Adrian Roosa5c63222017-07-27 16:33:39 +0200185 mScrimVisibleListener = scrimVisibleListener;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800186 mContext = scrimBehind.getContext();
187 mUnlockMethodCache = UnlockMethodCache.getInstance(mContext);
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800188 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800189 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Lucas Dupin690c6f52018-07-10 15:28:57 -0700190 mKeyguardVisibilityCallback = new KeyguardVisibilityCallback();
191 mKeyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800192 mScrimBehindAlphaResValue = mContext.getResources().getFloat(R.dimen.scrim_behind_alpha);
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800193 mHandler = getHandler();
Lucas Dupin82aa1632017-12-13 00:13:57 -0800194 mTimeTicker = new AlarmTimeout(alarmManager, this::onHideWallpaperTimeout,
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800195 "hide_aod_wallpaper", mHandler);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800196 mWakeLock = createWakeLock();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700197 // Scrim alpha is initially set to the value on the resource but might be changed
198 // to make sure that text on top of it is legible.
199 mScrimBehindAlpha = mScrimBehindAlphaResValue;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800200 mDozeParameters = dozeParameters;
Selim Cinek72cd9a72019-08-09 17:19:57 -0700201 keyguardMonitor.addCallback(new KeyguardMonitor.Callback() {
202 @Override
203 public void onKeyguardFadingAwayChanged() {
204 setKeyguardFadingAway(keyguardMonitor.isKeyguardFadingAway(),
205 keyguardMonitor.getKeyguardFadingAwayDuration());
206 }
207 });
Anthony Chen3cb3ad92016-12-01 10:58:47 -0800208
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700209 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupin314d41f2017-05-08 15:52:58 -0700210 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700211 mColors = mColorExtractor.getNeutralColors();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700212 mNeedsDrawableColorUpdate = true;
213
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800214 final ScrimState[] states = ScrimState.values();
215 for (int i = 0; i < states.length; i++) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700216 states[i].init(mScrimInFront, mScrimBehind, mDozeParameters);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800217 states[i].setScrimBehindAlphaKeyguard(mScrimBehindAlphaKeyguard);
218 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800219
Lucas Dupin373356b2018-04-07 10:50:25 -0700220 mScrimBehind.setDefaultFocusHighlightEnabled(false);
221 mScrimInFront.setDefaultFocusHighlightEnabled(false);
222
Selim Cinek511f2702017-04-10 19:53:01 -0700223 updateScrims();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200224 }
225
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800226 public void transitionTo(ScrimState state) {
227 transitionTo(state, null);
228 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700229
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800230 public void transitionTo(ScrimState state, Callback callback) {
231 if (state == mState) {
Lucas Dupin19aba8e2017-12-11 12:42:26 -0800232 // Call the callback anyway, unless it's already enqueued
233 if (callback != null && mCallback != callback) {
234 callback.onFinished();
235 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800236 return;
237 } else if (DEBUG) {
238 Log.d(TAG, "State changed to: " + state);
239 }
240
241 if (state == ScrimState.UNINITIALIZED) {
242 throw new IllegalArgumentException("Cannot change to UNINITIALIZED.");
243 }
244
Lucas Dupin8635c442017-12-08 10:36:28 -0800245 final ScrimState oldState = mState;
246 mState = state;
Lucas Dupin5866aaf2018-02-02 14:45:31 -0800247 Trace.traceCounter(Trace.TRACE_TAG_APP, "scrim_state", mState.getIndex());
Lucas Dupin8635c442017-12-08 10:36:28 -0800248
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800249 if (mCallback != null) {
250 mCallback.onCancelled();
251 }
252 mCallback = callback;
253
Lucas Dupin8635c442017-12-08 10:36:28 -0800254 state.prepare(oldState);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800255 mScreenBlankingCallbackCalled = false;
256 mAnimationDelay = 0;
257 mBlankScreen = state.getBlanksScreen();
258 mAnimateChange = state.getAnimateChange();
259 mAnimationDuration = state.getAnimationDuration();
Selim Cinekf97d7f72019-10-25 17:37:00 -0700260 mCurrentInFrontTint = state.getFrontTint();
261 mCurrentBehindTint = state.getBehindTint();
262 mCurrentInFrontAlpha = state.getFrontAlpha();
263 mCurrentBehindAlpha = state.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800264 applyExpansionToAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800265
Lucas Dupin38962d72018-03-14 12:41:39 -0700266 // Scrim might acquire focus when user is navigating with a D-pad or a keyboard.
267 // We need to disable focus otherwise AOD would end up with a gray overlay.
268 mScrimInFront.setFocusable(!state.isLowPowerState());
269 mScrimBehind.setFocusable(!state.isLowPowerState());
270
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800271 // Cancel blanking transitions that were pending before we requested a new state
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800272 if (mPendingFrameCallback != null) {
Lucas Dupin0791d972018-03-26 13:32:16 -0700273 mScrimBehind.removeCallbacks(mPendingFrameCallback);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800274 mPendingFrameCallback = null;
275 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800276 if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
277 mHandler.removeCallbacks(mBlankingTransitionRunnable);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800278 mBlankingTransitionRunnable = null;
279 }
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800280
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800281 // Showing/hiding the keyguard means that scrim colors have to be switched, not necessary
282 // to do the same when you're just showing the brightness mirror.
283 mNeedsDrawableColorUpdate = state != ScrimState.BRIGHTNESS_MIRROR;
284
Lucas Dupineea53b32017-12-18 13:47:14 -0800285 // The device might sleep if it's entering AOD, we need to make sure that
286 // the animation plays properly until the last frame.
287 // It's important to avoid holding the wakelock unless necessary because
288 // WakeLock#aqcuire will trigger an IPC and will cause jank.
Lucas Dupin38962d72018-03-14 12:41:39 -0700289 if (mState.isLowPowerState()) {
Lucas Dupineea53b32017-12-18 13:47:14 -0800290 holdWakeLock();
291 }
Lucas Dupin82aa1632017-12-13 00:13:57 -0800292
TYM Tsaia71c8922019-01-07 15:57:53 +0800293 // AOD wallpapers should fade away after a while.
294 // Docking pulses may take a long time, wallpapers should also fade away after a while.
Jerry Changbde4c0c2019-06-13 16:58:41 +0800295 mWallpaperVisibilityTimedOut = false;
296 if (shouldFadeAwayWallpaper()) {
297 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
298 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
Lucas Dupinc7804042018-12-21 12:26:33 -0800299 } else {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800300 mTimeTicker.cancel();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800301 }
302
Lucas Dupinff307d52018-02-05 21:16:39 -0800303 if (mKeyguardUpdateMonitor.needsSlowUnlockTransition() && mState == ScrimState.UNLOCKED) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800304 // In case the user isn't unlocked, make sure to delay a bit because the system is hosed
305 // with too many things at this case, in order to not skip the initial frames.
306 mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16);
307 mAnimationDelay = StatusBar.FADE_KEYGUARD_START_DELAY;
Lucas Dupin0791d972018-03-26 13:32:16 -0700308 } else if ((!mDozeParameters.getAlwaysOn() && oldState == ScrimState.AOD)
Lucas Dupin16cfe452018-02-08 13:14:50 -0800309 || (mState == ScrimState.AOD && !mDozeParameters.getDisplayNeedsBlanking())) {
310 // Scheduling a frame isn't enough when:
311 // • Leaving doze and we need to modify scrim color immediately
312 // • ColorFade will not kick-in and scrim cannot wait for pre-draw.
Lucas Dupinff307d52018-02-05 21:16:39 -0800313 onPreDraw();
314 } else {
315 scheduleUpdate();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800316 }
Yohei Yukawa795f0102018-04-13 14:55:30 -0700317
318 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800319 }
320
Jerry Changbde4c0c2019-06-13 16:58:41 +0800321 private boolean shouldFadeAwayWallpaper() {
322 if (!mWallpaperSupportsAmbientMode) {
323 return false;
324 }
325
326 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()) {
327 return true;
328 }
329
330 if (mState == ScrimState.PULSING
331 && mCallback != null && mCallback.shouldTimeoutWallpaper()) {
332 return true;
333 }
334
335 return false;
336 }
337
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800338 public ScrimState getState() {
339 return mState;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200340 }
341
Lucas Dupinbc9aac12018-03-04 20:18:15 -0800342 protected void setScrimBehindValues(float scrimBehindAlphaKeyguard) {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700343 mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800344 ScrimState[] states = ScrimState.values();
345 for (int i = 0; i < states.length; i++) {
346 states[i].setScrimBehindAlphaKeyguard(scrimBehindAlphaKeyguard);
347 }
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700348 scheduleUpdate();
349 }
350
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200351 public void onTrackingStarted() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800352 mTracking = true;
Selim Cineke8bae622015-07-15 13:24:06 -0700353 mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200354 }
355
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200356 public void onExpandingFinished() {
Jorim Jaggie93698b2016-11-11 18:24:38 -0800357 mTracking = false;
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200358 }
359
Lucas Dupin82aa1632017-12-13 00:13:57 -0800360 @VisibleForTesting
361 protected void onHideWallpaperTimeout() {
TYM Tsaia71c8922019-01-07 15:57:53 +0800362 if (mState != ScrimState.AOD && mState != ScrimState.PULSING) {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800363 return;
364 }
365
366 holdWakeLock();
367 mWallpaperVisibilityTimedOut = true;
368 mAnimateChange = true;
369 mAnimationDuration = mDozeParameters.getWallpaperFadeOutDuration();
370 scheduleUpdate();
371 }
372
373 private void holdWakeLock() {
374 if (!mWakeLockHeld) {
375 if (mWakeLock != null) {
376 mWakeLockHeld = true;
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800377 mWakeLock.acquire(TAG);
Lucas Dupin82aa1632017-12-13 00:13:57 -0800378 } else {
379 Log.w(TAG, "Cannot hold wake lock, it has not been set yet");
380 }
381 }
382 }
383
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800384 /**
385 * Current state of the shade expansion when pulling it from the top.
386 * This value is 1 when on top of the keyguard and goes to 0 as the user drags up.
387 *
388 * The expansion fraction is tied to the scrim opacity.
389 *
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800390 * @param fraction From 0 to 1 where 0 means collapsed and 1 expanded.
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800391 */
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200392 public void setPanelExpansion(float fraction) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800393 if (mExpansionFraction != fraction) {
394 mExpansionFraction = fraction;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800395
Selim Cinekf97d7f72019-10-25 17:37:00 -0700396 final boolean keyguardOrUnlocked = mState == ScrimState.UNLOCKED
397 || mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING;
398 if (!keyguardOrUnlocked || !mExpansionAffectsAlpha) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800399 return;
400 }
Selim Cinekf97d7f72019-10-25 17:37:00 -0700401
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800402 applyExpansionToAlpha();
Selim Cinekf97d7f72019-10-25 17:37:00 -0700403
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800404 if (mUpdatePending) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800405 return;
406 }
Selim Cinekf97d7f72019-10-25 17:37:00 -0700407
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800408 setOrAdaptCurrentAnimation(mScrimBehind);
409 setOrAdaptCurrentAnimation(mScrimInFront);
Selim Cinekf97d7f72019-10-25 17:37:00 -0700410
Yohei Yukawa795f0102018-04-13 14:55:30 -0700411 dispatchScrimState(mScrimBehind.getViewAlpha());
Jerry Changbde4c0c2019-06-13 16:58:41 +0800412
413 // Reset wallpaper timeout if it's already timeout like expanding panel while PULSING
414 // and docking.
415 if (mWallpaperVisibilityTimedOut) {
416 mWallpaperVisibilityTimedOut = false;
417 mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(),
418 AlarmTimeout.MODE_IGNORE_IF_SCHEDULED);
419 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800420 }
421 }
422
423 private void setOrAdaptCurrentAnimation(View scrim) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700424 if (!isAnimating(scrim)) {
425 updateScrimColor(scrim, getCurrentScrimAlpha(scrim), getCurrentScrimTint(scrim));
426 } else {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800427 ValueAnimator previousAnimator = (ValueAnimator) scrim.getTag(TAG_KEY_ANIM);
Selim Cinekf97d7f72019-10-25 17:37:00 -0700428 float alpha = getCurrentScrimAlpha(scrim);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800429 float previousEndValue = (Float) scrim.getTag(TAG_END_ALPHA);
430 float previousStartValue = (Float) scrim.getTag(TAG_START_ALPHA);
431 float relativeDiff = alpha - previousEndValue;
432 float newStartValue = previousStartValue + relativeDiff;
433 scrim.setTag(TAG_START_ALPHA, newStartValue);
434 scrim.setTag(TAG_END_ALPHA, alpha);
435 previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
Jorim Jaggi93439da2014-06-30 23:53:39 +0200436 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200437 }
438
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800439 private void applyExpansionToAlpha() {
Lucas Dupin67f02632018-03-12 11:08:31 -0700440 if (!mExpansionAffectsAlpha) {
441 return;
442 }
443
Selim Cinekf97d7f72019-10-25 17:37:00 -0700444 if (mState == ScrimState.UNLOCKED) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800445 // Darken scrim as you pull down the shade when unlocked
446 float behindFraction = getInterpolatedFraction();
447 behindFraction = (float) Math.pow(behindFraction, 0.8f);
Selim Cinekf97d7f72019-10-25 17:37:00 -0700448 mCurrentBehindAlpha = behindFraction * GRADIENT_SCRIM_ALPHA_BUSY;
449 mCurrentInFrontAlpha = 0;
Lucas Dupin1c327432019-01-03 13:37:53 -0800450 } else if (mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING) {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800451 // Either darken of make the scrim transparent when you
452 // pull down the shade
453 float interpolatedFract = getInterpolatedFraction();
Lucas Dupin3d36dd82019-01-02 14:38:35 -0800454 float alphaBehind = mState.getBehindAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800455 if (mDarkenWhileDragging) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700456 mCurrentBehindAlpha = MathUtils.lerp(GRADIENT_SCRIM_ALPHA_BUSY, alphaBehind,
Lucas Dupinb380c882018-02-25 21:57:17 -0800457 interpolatedFract);
Selim Cinekf97d7f72019-10-25 17:37:00 -0700458 mCurrentInFrontAlpha = mState.getFrontAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800459 } else {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700460 mCurrentBehindAlpha = MathUtils.lerp(0 /* start */, alphaBehind,
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800461 interpolatedFract);
Selim Cinekf97d7f72019-10-25 17:37:00 -0700462 mCurrentInFrontAlpha = mState.getFrontAlpha();
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800463 }
Selim Cinekf97d7f72019-10-25 17:37:00 -0700464 mCurrentBehindTint = ColorUtils.blendARGB(ScrimState.BOUNCER.getBehindTint(),
Lucas Dupin4cf01062019-03-08 17:07:49 -0800465 mState.getBehindTint(), interpolatedFract);
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800466 }
467 }
468
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800469 /**
Anthony Chene658cc22017-04-27 11:17:35 -0700470 * Sets the given drawable as the background of the scrim that shows up behind the
471 * notifications.
472 */
473 public void setScrimBehindDrawable(Drawable drawable) {
474 mScrimBehind.setDrawable(drawable);
475 }
476
Michael Wrightcae88752018-04-16 23:13:54 +0100477 /**
Lucas Dupin69bda602018-05-18 17:24:52 -0700478 * Sets the front scrim opacity in AOD so it's not as bright.
479 * <p>
480 * Displays usually don't support multiple dimming settings when in low power mode.
481 * The workaround is to modify the front scrim opacity when in AOD, so it's not as
482 * bright when you're at the movies or lying down on bed.
483 * <p>
484 * This value will be lost during transitions and only updated again after the the
485 * device is dozing when the light sensor is on.
Michael Wrightcae88752018-04-16 23:13:54 +0100486 */
487 public void setAodFrontScrimAlpha(float alpha) {
Chris.CC Leed424e202019-09-16 12:17:19 +0800488 if (((mState == ScrimState.AOD && mDozeParameters.getAlwaysOn())
Selim Cinekf97d7f72019-10-25 17:37:00 -0700489 || mState == ScrimState.PULSING) && mCurrentInFrontAlpha != alpha) {
490 mCurrentInFrontAlpha = alpha;
Lucas Dupin8523d132018-05-31 17:34:32 -0700491 updateScrims();
Michael Wrightcae88752018-04-16 23:13:54 +0100492 }
493
494 mState.AOD.setAodFrontScrimAlpha(alpha);
Chris.CC Leed424e202019-09-16 12:17:19 +0800495 mState.PULSING.setAodFrontScrimAlpha(alpha);
Michael Wrightcae88752018-04-16 23:13:54 +0100496 }
497
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700498 /**
Lucas Dupin34306c32019-07-16 11:56:53 -0700499 * Set front scrim to black, cancelling animations, in order to prepare to fade them
500 * away once the display turns on.
501 */
502 public void prepareForGentleWakeUp() {
503 if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn()) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700504 mCurrentInFrontAlpha = 1f;
505 mCurrentInFrontTint = Color.BLACK;
506 mCurrentBehindTint = Color.BLACK;
Lucas Dupin34306c32019-07-16 11:56:53 -0700507 mAnimateChange = false;
508 updateScrims();
509 mAnimateChange = true;
510 mAnimationDuration = ANIMATION_DURATION_LONG;
511 }
512 }
513
514 /**
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700515 * If the lock screen sensor is active.
516 */
517 public void setWakeLockScreenSensorActive(boolean active) {
518 for (ScrimState state : ScrimState.values()) {
519 state.setWakeLockScreenSensorActive(active);
520 }
521
522 if (mState == ScrimState.PULSING) {
523 float newBehindAlpha = mState.getBehindAlpha();
Selim Cinekf97d7f72019-10-25 17:37:00 -0700524 if (mCurrentBehindAlpha != newBehindAlpha) {
525 mCurrentBehindAlpha = newBehindAlpha;
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700526 updateScrims();
527 }
528 }
529 }
530
Evan Rosky9a895012016-04-21 11:26:15 -0700531 protected void scheduleUpdate() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200532 if (mUpdatePending) return;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200533
534 // Make sure that a frame gets scheduled.
535 mScrimBehind.invalidate();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200536 mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this);
537 mUpdatePending = true;
538 }
539
Xiaohui Chen5da71352016-02-22 10:04:41 -0800540 protected void updateScrims() {
Lucas Dupin9324aa92017-07-26 20:29:38 -0700541 // Make sure we have the right gradients and their opacities will satisfy GAR.
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700542 if (mNeedsDrawableColorUpdate) {
543 mNeedsDrawableColorUpdate = false;
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700544 // Only animate scrim color if the scrim view is actually visible
545 boolean animateScrimInFront = mScrimInFront.getViewAlpha() != 0 && !mBlankScreen;
546 boolean animateScrimBehind = mScrimBehind.getViewAlpha() != 0 && !mBlankScreen;
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700547 mScrimInFront.setColors(mColors, animateScrimInFront);
548 mScrimBehind.setColors(mColors, animateScrimBehind);
Lucas Dupin9324aa92017-07-26 20:29:38 -0700549
550 // Calculate minimum scrim opacity for white or black text.
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700551 int textColor = mColors.supportsDarkText() ? Color.BLACK : Color.WHITE;
552 int mainColor = mColors.getMainColor();
Lucas Dupin9324aa92017-07-26 20:29:38 -0700553 float minOpacity = ColorUtils.calculateMinimumBackgroundAlpha(textColor, mainColor,
554 4.5f /* minimumContrast */) / 255f;
555 mScrimBehindAlpha = Math.max(mScrimBehindAlphaResValue, minOpacity);
Yohei Yukawa795f0102018-04-13 14:55:30 -0700556 dispatchScrimState(mScrimBehind.getViewAlpha());
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700557 }
558
Lucas Dupind5107302018-03-19 15:30:29 -0700559 // We want to override the back scrim opacity for the AOD state
Lucas Dupin82aa1632017-12-13 00:13:57 -0800560 // when it's time to fade the wallpaper away.
TYM Tsaia71c8922019-01-07 15:57:53 +0800561 boolean aodWallpaperTimeout = (mState == ScrimState.AOD || mState == ScrimState.PULSING)
562 && mWallpaperVisibilityTimedOut;
Lucas Dupind5107302018-03-19 15:30:29 -0700563 // We also want to hide FLAG_SHOW_WHEN_LOCKED activities under the scrim.
564 boolean occludedKeyguard = (mState == ScrimState.PULSING || mState == ScrimState.AOD)
565 && mKeyguardOccluded;
566 if (aodWallpaperTimeout || occludedKeyguard) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700567 mCurrentBehindAlpha = 1;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800568 }
Selim Cinekf97d7f72019-10-25 17:37:00 -0700569
570 setScrimInFrontAlpha(mCurrentInFrontAlpha);
571 setScrimBehindAlpha(mCurrentBehindAlpha);
572
Adrian Roosa5c63222017-07-27 16:33:39 +0200573 dispatchScrimsVisible();
574 }
575
Yohei Yukawa795f0102018-04-13 14:55:30 -0700576 private void dispatchScrimState(float alpha) {
577 mScrimStateListener.accept(mState, alpha, mScrimInFront.getColors());
578 }
579
Adrian Roosa5c63222017-07-27 16:33:39 +0200580 private void dispatchScrimsVisible() {
Lucas Dupin82aa1632017-12-13 00:13:57 -0800581 final int currentScrimVisibility;
582 if (mScrimInFront.getViewAlpha() == 1 || mScrimBehind.getViewAlpha() == 1) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700583 currentScrimVisibility = VISIBILITY_FULLY_OPAQUE;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800584 } else if (mScrimInFront.getViewAlpha() == 0 && mScrimBehind.getViewAlpha() == 0) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700585 currentScrimVisibility = VISIBILITY_FULLY_TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800586 } else {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700587 currentScrimVisibility = VISIBILITY_SEMI_TRANSPARENT;
Lucas Dupin82aa1632017-12-13 00:13:57 -0800588 }
Adrian Roosa5c63222017-07-27 16:33:39 +0200589
Lucas Dupin82aa1632017-12-13 00:13:57 -0800590 if (mScrimsVisibility != currentScrimVisibility) {
591 mScrimsVisibility = currentScrimVisibility;
592 mScrimVisibleListener.accept(currentScrimVisibility);
Adrian Roosa5c63222017-07-27 16:33:39 +0200593 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200594 }
595
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800596 private float getInterpolatedFraction() {
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800597 float frac = mExpansionFraction;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200598 // let's start this 20% of the way down the screen
599 frac = frac * 1.2f - 0.2f;
600 if (frac <= 0) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800601 return 0;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200602 } else {
603 // woo, special effects
Selim Cinekf97d7f72019-10-25 17:37:00 -0700604 return (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f))));
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200605 }
606 }
607
Selim Cinekf97d7f72019-10-25 17:37:00 -0700608 private void setScrimBehindAlpha(float alpha) {
609 setScrimAlpha(mScrimBehind, alpha);
610 }
611
612 private void setScrimInFrontAlpha(float alpha) {
613 setScrimAlpha(mScrimInFront, alpha);
614 }
615
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800616 private void setScrimAlpha(ScrimView scrim, float alpha) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800617 if (alpha == 0f) {
618 scrim.setClickable(false);
Selim Cinekaac93252015-04-14 20:04:12 -0700619 } else {
Lucas Dupin1c327432019-01-03 13:37:53 -0800620 // Eat touch events (unless dozing).
621 scrim.setClickable(mState != ScrimState.AOD);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200622 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800623 updateScrim(scrim, alpha);
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100624 }
625
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800626 private void updateScrimColor(View scrim, float alpha, int tint) {
627 alpha = Math.max(0, Math.min(1.0f, alpha));
Selim Cinekaac93252015-04-14 20:04:12 -0700628 if (scrim instanceof ScrimView) {
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700629 ScrimView scrimView = (ScrimView) scrim;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700630
Selim Cinekf97d7f72019-10-25 17:37:00 -0700631 Trace.traceCounter(Trace.TRACE_TAG_APP,
632 scrim == mScrimInFront ? "front_scrim_alpha" : "back_scrim_alpha",
Adrian Roosa5c63222017-07-27 16:33:39 +0200633 (int) (alpha * 255));
634
Selim Cinekf97d7f72019-10-25 17:37:00 -0700635 Trace.traceCounter(Trace.TRACE_TAG_APP,
636 scrim == mScrimInFront ? "front_scrim_tint" : "back_scrim_tint",
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800637 Color.alpha(tint));
Adrian Roosa5c63222017-07-27 16:33:39 +0200638
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800639 scrimView.setTint(tint);
640 scrimView.setViewAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700641 } else {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800642 scrim.setAlpha(alpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700643 }
Selim Cinek8cd80402017-08-01 10:44:29 -0700644 dispatchScrimsVisible();
Jorim Jaggi048af1f2014-11-11 22:51:10 +0100645 }
646
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800647 private void startScrimAnimation(final View scrim, float current) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800648 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
649 final int initialScrimTint = scrim instanceof ScrimView ? ((ScrimView) scrim).getTint() :
650 Color.TRANSPARENT;
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700651 anim.addUpdateListener(animation -> {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800652 final float startAlpha = (Float) scrim.getTag(TAG_START_ALPHA);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800653 final float animAmount = (float) animation.getAnimatedValue();
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800654 final int finalScrimTint = getCurrentScrimTint(scrim);
655 final float finalScrimAlpha = getCurrentScrimAlpha(scrim);
656 float alpha = MathUtils.lerp(startAlpha, finalScrimAlpha, animAmount);
657 alpha = MathUtils.constrain(alpha, 0f, 1f);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800658 int tint = ColorUtils.blendARGB(initialScrimTint, finalScrimTint, animAmount);
659 updateScrimColor(scrim, alpha, tint);
Adrian Roosa5c63222017-07-27 16:33:39 +0200660 dispatchScrimsVisible();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200661 });
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800662 anim.setInterpolator(mInterpolator);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200663 anim.setStartDelay(mAnimationDelay);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800664 anim.setDuration(mAnimationDuration);
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200665 anim.addListener(new AnimatorListenerAdapter() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800666 private Callback lastCallback = mCallback;
667
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200668 @Override
669 public void onAnimationEnd(Animator animation) {
Joanne Chunga393ad62019-01-16 21:41:31 +0800670 onFinished(lastCallback);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800671
Selim Cinekf97d7f72019-10-25 17:37:00 -0700672 scrim.setTag(TAG_KEY_ANIM, null);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800673 dispatchScrimsVisible();
674
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200675 if (!mDeferFinishedListener && mOnAnimationFinished != null) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200676 mOnAnimationFinished.run();
677 mOnAnimationFinished = null;
678 }
679 }
680 });
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800681
682 // Cache alpha values because we might want to update this animator in the future if
683 // the user expands the panel while the animation is still running.
684 scrim.setTag(TAG_START_ALPHA, current);
685 scrim.setTag(TAG_END_ALPHA, getCurrentScrimAlpha(scrim));
686
John Spurlockbf370992014-06-17 13:58:31 -0400687 scrim.setTag(TAG_KEY_ANIM, anim);
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800688 anim.start();
689 }
690
691 private float getCurrentScrimAlpha(View scrim) {
692 if (scrim == mScrimInFront) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700693 return mCurrentInFrontAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800694 } else if (scrim == mScrimBehind) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700695 return mCurrentBehindAlpha;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800696 } else {
697 throw new IllegalArgumentException("Unknown scrim view");
698 }
699 }
700
701 private int getCurrentScrimTint(View scrim) {
702 if (scrim == mScrimInFront) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700703 return mCurrentInFrontTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800704 } else if (scrim == mScrimBehind) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700705 return mCurrentBehindTint;
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800706 } else {
707 throw new IllegalArgumentException("Unknown scrim view");
708 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200709 }
710
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200711 @Override
712 public boolean onPreDraw() {
713 mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this);
714 mUpdatePending = false;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800715 if (mCallback != null) {
716 mCallback.onStart();
Selim Cinek372d1bd2015-08-14 13:19:37 -0700717 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200718 updateScrims();
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800719 if (mOnAnimationFinished != null && !isAnimating(mScrimInFront)
720 && !isAnimating(mScrimBehind)) {
721 mOnAnimationFinished.run();
722 mOnAnimationFinished = null;
723 }
Selim Cinekedd32b82015-06-23 22:05:58 -0400724 return true;
725 }
726
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800727 private void onFinished() {
Joanne Chunga393ad62019-01-16 21:41:31 +0800728 onFinished(mCallback);
729 }
730
731 private void onFinished(Callback callback) {
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800732 if (mWakeLockHeld) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800733 mWakeLock.release(TAG);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800734 mWakeLockHeld = false;
735 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800736
737 if (callback != null) {
738 callback.onFinished();
739
740 if (callback == mCallback) {
741 mCallback = null;
742 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800743 }
Joanne Chunga393ad62019-01-16 21:41:31 +0800744
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800745 // When unlocking with fingerprint, we'll fade the scrims from black to transparent.
746 // At the end of the animation we need to remove the tint.
747 if (mState == ScrimState.UNLOCKED) {
Selim Cinekf97d7f72019-10-25 17:37:00 -0700748 mCurrentInFrontTint = Color.TRANSPARENT;
749 mCurrentBehindTint = Color.TRANSPARENT;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800750 }
751 }
752
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700753 private boolean isAnimating(View scrim) {
754 return scrim.getTag(TAG_KEY_ANIM) != null;
755 }
756
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800757 @VisibleForTesting
758 void setOnAnimationFinished(Runnable onAnimationFinished) {
759 mOnAnimationFinished = onAnimationFinished;
760 }
Adrian Roos5f72c922016-03-08 15:54:26 -0800761
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800762 private void updateScrim(ScrimView scrim, float alpha) {
763 final float currentAlpha = scrim.getViewAlpha();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800764
765 ValueAnimator previousAnimator = ViewState.getChildTag(scrim, TAG_KEY_ANIM);
Selim Cinekaac93252015-04-14 20:04:12 -0700766 if (previousAnimator != null) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800767 if (mAnimateChange) {
Jorim Jaggif5304ad2017-07-17 18:31:13 +0200768 // We are not done yet! Defer calling the finished listener.
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800769 mDeferFinishedListener = true;
Selim Cinekaac93252015-04-14 20:04:12 -0700770 }
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800771 // Previous animators should always be cancelled. Not doing so would cause
772 // overlap, especially on states that don't animate, leading to flickering,
773 // and in the worst case, an internal state that doesn't represent what
774 // transitionTo requested.
775 cancelAnimator(previousAnimator);
776 mDeferFinishedListener = false;
Selim Cinekaac93252015-04-14 20:04:12 -0700777 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800778
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800779 if (mPendingFrameCallback != null) {
780 // Display is off and we're waiting.
781 return;
782 } else if (mBlankScreen) {
783 // Need to blank the display before continuing.
784 blankDisplay();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800785 return;
786 } else if (!mScreenBlankingCallbackCalled) {
787 // Not blanking the screen. Letting the callback know that we're ready
788 // to replace what was on the screen before.
789 if (mCallback != null) {
790 mCallback.onDisplayBlanked();
791 mScreenBlankingCallbackCalled = true;
792 }
793 }
794
Lucas Dupin6afae372017-11-30 13:09:22 -0500795 if (scrim == mScrimBehind) {
Yohei Yukawa795f0102018-04-13 14:55:30 -0700796 dispatchScrimState(alpha);
Lucas Dupin6afae372017-11-30 13:09:22 -0500797 }
798
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800799 final boolean wantsAlphaUpdate = alpha != currentAlpha;
Lucas Dupin3503c5f2018-03-02 19:04:00 -0800800 final boolean wantsTintUpdate = scrim.getTint() != getCurrentScrimTint(scrim);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800801
802 if (wantsAlphaUpdate || wantsTintUpdate) {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800803 if (mAnimateChange) {
804 startScrimAnimation(scrim, currentAlpha);
Selim Cinekaac93252015-04-14 20:04:12 -0700805 } else {
Lucas Dupin3daf3b02018-02-27 17:23:41 -0800806 // update the alpha directly
807 updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim));
Selim Cinekf97d7f72019-10-25 17:37:00 -0700808 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700809 }
Selim Cinekf97d7f72019-10-25 17:37:00 -0700810 } else {
811 onFinished();
Selim Cinekaac93252015-04-14 20:04:12 -0700812 }
813 }
814
Lucas Dupin80a3fcc2018-02-07 10:49:55 -0800815 @VisibleForTesting
816 protected void cancelAnimator(ValueAnimator previousAnimator) {
817 if (previousAnimator != null) {
818 previousAnimator.cancel();
819 }
820 }
821
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800822 private void blankDisplay() {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800823 updateScrimColor(mScrimInFront, 1, Color.BLACK);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800824
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800825 // Notify callback that the screen is completely black and we're
826 // ready to change the display power mode
Lucas Dupin0791d972018-03-26 13:32:16 -0700827 mPendingFrameCallback = () -> {
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800828 if (mCallback != null) {
829 mCallback.onDisplayBlanked();
830 mScreenBlankingCallbackCalled = true;
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800831 }
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800832
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800833 mBlankingTransitionRunnable = () -> {
834 mBlankingTransitionRunnable = null;
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800835 mPendingFrameCallback = null;
836 mBlankScreen = false;
837 // Try again.
838 updateScrims();
839 };
840
841 // Setting power states can happen after we push out the frame. Make sure we
842 // stay fully opaque until the power state request reaches the lower levels.
Lucas Dupin0791d972018-03-26 13:32:16 -0700843 final int delay = mScreenOn ? 32 : 500;
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800844 if (DEBUG) {
Lucas Dupin3a64ed72018-02-12 12:44:37 -0800845 Log.d(TAG, "Fading out scrims with delay: " + delay);
Lucas Dupin53cf12a2018-02-05 16:14:45 -0800846 }
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800847 mHandler.postDelayed(mBlankingTransitionRunnable, delay);
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800848 };
849 doOnTheNextFrame(mPendingFrameCallback);
850 }
851
Lucas Dupin0791d972018-03-26 13:32:16 -0700852 /**
853 * Executes a callback after the frame has hit the display.
854 * @param callback What to run.
855 */
Lucas Dupin8c7cb022018-02-05 10:49:03 -0800856 @VisibleForTesting
Lucas Dupin0791d972018-03-26 13:32:16 -0700857 protected void doOnTheNextFrame(Runnable callback) {
858 // Just calling View#postOnAnimation isn't enough because the frame might not have reached
859 // the display yet. A timeout is the safest solution.
860 mScrimBehind.postOnAnimationDelayed(callback, 32 /* delayMillis */);
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800861 }
862
863 @VisibleForTesting
864 protected Handler getHandler() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800865 return new Handler();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800866 }
867
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700868 public int getBackgroundColor() {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700869 int color = mColors.getMainColor();
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800870 return Color.argb((int) (mScrimBehind.getViewAlpha() * Color.alpha(color)),
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700871 Color.red(color), Color.green(color), Color.blue(color));
Selim Cinekd35c2792016-01-21 13:20:57 -0800872 }
873
874 public void setScrimBehindChangeRunnable(Runnable changeRunnable) {
875 mScrimBehind.setChangeRunnable(changeRunnable);
876 }
Selim Cineke803491c2016-04-09 21:24:45 -0700877
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700878 public void setCurrentUser(int currentUser) {
879 // Don't care in the base class.
880 }
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700881
882 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -0700883 public void onColorsChanged(ColorExtractor colorExtractor, int which) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -0700884 mColors = mColorExtractor.getNeutralColors();
885 mNeedsDrawableColorUpdate = true;
886 scheduleUpdate();
Lucas Dupin8da8f2e92017-04-21 14:02:16 -0700887 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200888
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800889 @VisibleForTesting
890 protected WakeLock createWakeLock() {
Lucas Dupin54fbfb32019-03-05 18:08:13 -0800891 return new DelayedWakeLock(mHandler, WakeLock.createPartial(mContext, "Scrims"));
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800892 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200893
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800894 @Override
895 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800896 pw.println(" ScrimController: ");
Selim Cinekf97d7f72019-10-25 17:37:00 -0700897 pw.print(" state: "); pw.println(mState);
898 pw.print(" frontScrim:"); pw.print(" viewAlpha="); pw.print(mScrimInFront.getViewAlpha());
899 pw.print(" alpha="); pw.print(mCurrentInFrontAlpha);
900 pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimInFront.getTint()));
Adrian Roosba7ca592017-08-15 17:48:05 +0200901
Selim Cinekf97d7f72019-10-25 17:37:00 -0700902 pw.print(" backScrim:"); pw.print(" viewAlpha="); pw.print(mScrimBehind.getViewAlpha());
903 pw.print(" alpha="); pw.print(mCurrentBehindAlpha);
904 pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimBehind.getTint()));
Adrian Roosba7ca592017-08-15 17:48:05 +0200905
Selim Cinekf97d7f72019-10-25 17:37:00 -0700906 pw.print(" mTracking="); 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();
Selim Cinekf97d7f72019-10-25 17:37:00 -0700953 if (mCurrentBehindAlpha != newBehindAlpha) {
954 mCurrentBehindAlpha = newBehindAlpha;
Lucas Dupin9bee5822018-07-09 14:32:53 -0700955 updateScrims();
956 }
957 }
Lucas Dupin193677c2018-06-11 19:16:03 -0700958 }
959
Selim Cinek72cd9a72019-08-09 17:19:57 -0700960 private void setKeyguardFadingAway(boolean fadingAway, long duration) {
Selim Cinek84b2acc2019-07-07 00:40:38 -0700961 for (ScrimState state : ScrimState.values()) {
Selim Cinek72cd9a72019-08-09 17:19:57 -0700962 state.setKeyguardFadingAway(fadingAway, duration);
Selim Cinek84b2acc2019-07-07 00:40:38 -0700963 }
964 }
965
Lucas Dupin193677c2018-06-11 19:16:03 -0700966 public void setLaunchingAffordanceWithPreview(boolean launchingAffordanceWithPreview) {
967 for (ScrimState state : ScrimState.values()) {
968 state.setLaunchingAffordanceWithPreview(launchingAffordanceWithPreview);
Lucas Dupinf8463ee2018-06-11 16:18:15 -0700969 }
970 }
971
Lucas Dupin9e3fa102017-11-08 17:16:55 -0800972 public interface Callback {
973 default void onStart() {
974 }
975 default void onDisplayBlanked() {
976 }
977 default void onFinished() {
978 }
979 default void onCancelled() {
980 }
Jerry Changbde4c0c2019-06-13 16:58:41 +0800981 /** Returns whether to timeout wallpaper or not. */
982 default boolean shouldTimeoutWallpaper() {
983 return false;
984 }
Adrian Roosba7ca592017-08-15 17:48:05 +0200985 }
Lucas Dupin690c6f52018-07-10 15:28:57 -0700986
987 /**
988 * Simple keyguard callback that updates scrims when keyguard visibility changes.
989 */
990 private class KeyguardVisibilityCallback extends KeyguardUpdateMonitorCallback {
991
992 @Override
993 public void onKeyguardVisibilityChanged(boolean showing) {
994 mNeedsDrawableColorUpdate = true;
995 scheduleUpdate();
996 }
997 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200998}