Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 23 | import android.app.WallpaperManager; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 24 | import android.content.Context; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 25 | import android.graphics.Color; |
Anthony Chen | e658cc2 | 2017-04-27 11:17:35 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 27 | import android.os.Handler; |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 28 | import android.os.Trace; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 29 | import android.util.Log; |
Lucas Dupin | a0bf851 | 2017-05-24 17:04:47 -0700 | [diff] [blame] | 30 | import android.util.MathUtils; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.ViewTreeObserver; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 33 | import android.view.animation.DecelerateInterpolator; |
| 34 | import android.view.animation.Interpolator; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 35 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 37 | import com.android.internal.colorextraction.ColorExtractor; |
| 38 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
| 39 | import com.android.internal.colorextraction.ColorExtractor.OnColorsChangedListener; |
Lucas Dupin | 9324aa9 | 2017-07-26 20:29:38 -0700 | [diff] [blame] | 40 | import com.android.internal.graphics.ColorUtils; |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 41 | import com.android.internal.util.function.TriConsumer; |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 42 | import com.android.keyguard.KeyguardUpdateMonitor; |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 43 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 44 | import com.android.systemui.Dependency; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 45 | import com.android.systemui.Dumpable; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 46 | import com.android.systemui.R; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 47 | import com.android.systemui.colorextraction.SysuiColorExtractor; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 48 | import com.android.systemui.statusbar.ScrimView; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.stack.ViewState; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 50 | import com.android.systemui.util.AlarmTimeout; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 51 | import com.android.systemui.util.wakelock.DelayedWakeLock; |
| 52 | import com.android.systemui.util.wakelock.WakeLock; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 53 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 54 | import java.io.FileDescriptor; |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 55 | import java.io.PrintWriter; |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 56 | import java.util.function.Consumer; |
| 57 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 58 | /** |
| 59 | * Controls both the scrim behind the notifications and in front of the notifications (when a |
| 60 | * security method gets shown). |
| 61 | */ |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 62 | public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnColorsChangedListener, |
| 63 | Dumpable { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 64 | |
Lucas Dupin | 55c6e80 | 2018-09-27 18:07:36 -0700 | [diff] [blame] | 65 | static final String TAG = "ScrimController"; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 66 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 67 | |
Lucas Dupin | 4749f1b | 2018-04-04 15:09:06 -0700 | [diff] [blame] | 68 | /** |
| 69 | * General scrim animation duration. |
| 70 | */ |
John Spurlock | 8b12f22 | 2014-09-09 11:54:11 -0400 | [diff] [blame] | 71 | public static final long ANIMATION_DURATION = 220; |
Lucas Dupin | 4749f1b | 2018-04-04 15:09:06 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Longer duration, currently only used when going to AOD. |
| 74 | */ |
| 75 | public static final long ANIMATION_DURATION_LONG = 1000; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 76 | /** |
| 77 | * When both scrims have 0 alpha. |
| 78 | */ |
| 79 | public static final int VISIBILITY_FULLY_TRANSPARENT = 0; |
| 80 | /** |
| 81 | * When scrims aren't transparent (alpha 0) but also not opaque (alpha 1.) |
| 82 | */ |
| 83 | public static final int VISIBILITY_SEMI_TRANSPARENT = 1; |
| 84 | /** |
| 85 | * When at least 1 scrim is fully opaque (alpha set to 1.) |
| 86 | */ |
| 87 | public static final int VISIBILITY_FULLY_OPAQUE = 2; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Default alpha value for most scrims. |
| 90 | */ |
Lucas Dupin | 3d36dd8 | 2019-01-02 14:38:35 -0800 | [diff] [blame] | 91 | public static final float GRADIENT_SCRIM_ALPHA = 0.2f; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 92 | /** |
| 93 | * A scrim varies its opacity based on a busyness factor, for example |
| 94 | * how many notifications are currently visible. |
| 95 | */ |
Lucas Dupin | de64ee0 | 2018-12-21 14:45:12 -0800 | [diff] [blame] | 96 | public static final float GRADIENT_SCRIM_ALPHA_BUSY = 0.7f; |
Lucas Dupin | de64ee0 | 2018-12-21 14:45:12 -0800 | [diff] [blame] | 97 | |
Lucas Dupin | 55c6e80 | 2018-09-27 18:07:36 -0700 | [diff] [blame] | 98 | /** |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 99 | * The most common scrim, the one under the keyguard. |
| 100 | */ |
Lucas Dupin | a0bf851 | 2017-05-24 17:04:47 -0700 | [diff] [blame] | 101 | protected static final float SCRIM_BEHIND_ALPHA_KEYGUARD = GRADIENT_SCRIM_ALPHA; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 102 | |
| 103 | static final int TAG_KEY_ANIM = R.id.scrim; |
Selim Cinek | 5104a6d | 2015-12-18 18:38:31 -0800 | [diff] [blame] | 104 | private static final int TAG_START_ALPHA = R.id.scrim_alpha_start; |
| 105 | private static final int TAG_END_ALPHA = R.id.scrim_alpha_end; |
Selim Cinek | 511f270 | 2017-04-10 19:53:01 -0700 | [diff] [blame] | 106 | private static final float NOT_INITIALIZED = -1; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 107 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 108 | private ScrimState mState = ScrimState.UNINITIALIZED; |
| 109 | private final Context mContext; |
Xiaohui Chen | 5da7135 | 2016-02-22 10:04:41 -0800 | [diff] [blame] | 110 | protected final ScrimView mScrimBehind; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 111 | protected final ScrimView mScrimInFront; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 112 | private final UnlockMethodCache mUnlockMethodCache; |
Jorim Jaggi | e8fde5d | 2016-06-30 23:41:37 -0700 | [diff] [blame] | 113 | private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 114 | private final DozeParameters mDozeParameters; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 115 | private final AlarmTimeout mTimeTicker; |
Lucas Dupin | 690c6f5 | 2018-07-10 15:28:57 -0700 | [diff] [blame] | 116 | private final KeyguardVisibilityCallback mKeyguardVisibilityCallback; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 117 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 118 | private final SysuiColorExtractor mColorExtractor; |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 119 | private GradientColors mLockColors; |
| 120 | private GradientColors mSystemColors; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 121 | private boolean mNeedsDrawableColorUpdate; |
| 122 | |
Anthony Chen | 3cb3ad9 | 2016-12-01 10:58:47 -0800 | [diff] [blame] | 123 | protected float mScrimBehindAlpha; |
Lucas Dupin | 9324aa9 | 2017-07-26 20:29:38 -0700 | [diff] [blame] | 124 | protected float mScrimBehindAlphaResValue; |
Evan Rosky | 9a89501 | 2016-04-21 11:26:15 -0700 | [diff] [blame] | 125 | protected float mScrimBehindAlphaKeyguard = SCRIM_BEHIND_ALPHA_KEYGUARD; |
Xiaohui Chen | 4dab4b5 | 2016-03-07 12:31:14 -0800 | [diff] [blame] | 126 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 127 | // Assuming the shade is expanded during initialization |
| 128 | private float mExpansionFraction = 1f; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 129 | |
| 130 | private boolean mDarkenWhileDragging; |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 131 | private boolean mExpansionAffectsAlpha = true; |
Evan Rosky | 9a89501 | 2016-04-21 11:26:15 -0700 | [diff] [blame] | 132 | protected boolean mAnimateChange; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 133 | private boolean mUpdatePending; |
Jorim Jaggi | e93698b | 2016-11-11 18:24:38 -0800 | [diff] [blame] | 134 | private boolean mTracking; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 135 | protected long mAnimationDuration = -1; |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 136 | private long mAnimationDelay; |
| 137 | private Runnable mOnAnimationFinished; |
Jorim Jaggi | f5304ad | 2017-07-17 18:31:13 +0200 | [diff] [blame] | 138 | private boolean mDeferFinishedListener; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 139 | private final Interpolator mInterpolator = new DecelerateInterpolator(); |
Selim Cinek | 511f270 | 2017-04-10 19:53:01 -0700 | [diff] [blame] | 140 | private float mCurrentInFrontAlpha = NOT_INITIALIZED; |
| 141 | private float mCurrentBehindAlpha = NOT_INITIALIZED; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 142 | private int mCurrentInFrontTint; |
| 143 | private int mCurrentBehindTint; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 144 | private boolean mWallpaperVisibilityTimedOut; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 145 | private int mScrimsVisibility; |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 146 | private final TriConsumer<ScrimState, Float, GradientColors> mScrimStateListener; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 147 | private final Consumer<Integer> mScrimVisibleListener; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 148 | private boolean mBlankScreen; |
| 149 | private boolean mScreenBlankingCallbackCalled; |
| 150 | private Callback mCallback; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 151 | private boolean mWallpaperSupportsAmbientMode; |
Lucas Dupin | 3a64ed7 | 2018-02-12 12:44:37 -0800 | [diff] [blame] | 152 | private boolean mScreenOn; |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 153 | |
| 154 | // Scrim blanking callbacks |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 155 | private Runnable mPendingFrameCallback; |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 156 | private Runnable mBlankingTransitionRunnable; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 157 | |
| 158 | private final WakeLock mWakeLock; |
| 159 | private boolean mWakeLockHeld; |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 160 | private boolean mKeyguardOccluded; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 161 | |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 162 | public ScrimController(ScrimView scrimBehind, ScrimView scrimInFront, |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 163 | TriConsumer<ScrimState, Float, GradientColors> scrimStateListener, |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 164 | Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters, |
| 165 | AlarmManager alarmManager) { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 166 | mScrimBehind = scrimBehind; |
| 167 | mScrimInFront = scrimInFront; |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 168 | mScrimStateListener = scrimStateListener; |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 169 | mScrimVisibleListener = scrimVisibleListener; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 170 | mContext = scrimBehind.getContext(); |
| 171 | mUnlockMethodCache = UnlockMethodCache.getInstance(mContext); |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 172 | mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 173 | mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
Lucas Dupin | 690c6f5 | 2018-07-10 15:28:57 -0700 | [diff] [blame] | 174 | mKeyguardVisibilityCallback = new KeyguardVisibilityCallback(); |
| 175 | mKeyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 176 | mScrimBehindAlphaResValue = mContext.getResources().getFloat(R.dimen.scrim_behind_alpha); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 177 | mTimeTicker = new AlarmTimeout(alarmManager, this::onHideWallpaperTimeout, |
| 178 | "hide_aod_wallpaper", new Handler()); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 179 | mWakeLock = createWakeLock(); |
Lucas Dupin | 9324aa9 | 2017-07-26 20:29:38 -0700 | [diff] [blame] | 180 | // Scrim alpha is initially set to the value on the resource but might be changed |
| 181 | // to make sure that text on top of it is legible. |
| 182 | mScrimBehindAlpha = mScrimBehindAlphaResValue; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 183 | mDozeParameters = dozeParameters; |
Anthony Chen | 3cb3ad9 | 2016-12-01 10:58:47 -0800 | [diff] [blame] | 184 | |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 185 | mColorExtractor = Dependency.get(SysuiColorExtractor.class); |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 186 | mColorExtractor.addOnColorsChangedListener(this); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 187 | mLockColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 188 | ColorExtractor.TYPE_DARK, true /* ignoreVisibility */); |
Lucas Dupin | 5266ad1 | 2017-06-17 20:57:28 -0700 | [diff] [blame] | 189 | mSystemColors = mColorExtractor.getColors(WallpaperManager.FLAG_SYSTEM, |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 190 | ColorExtractor.TYPE_DARK, true /* ignoreVisibility */); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 191 | mNeedsDrawableColorUpdate = true; |
| 192 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 193 | final ScrimState[] states = ScrimState.values(); |
| 194 | for (int i = 0; i < states.length; i++) { |
| 195 | states[i].init(mScrimInFront, mScrimBehind, mDozeParameters); |
| 196 | states[i].setScrimBehindAlphaKeyguard(mScrimBehindAlphaKeyguard); |
| 197 | } |
| 198 | mState = ScrimState.UNINITIALIZED; |
| 199 | |
Lucas Dupin | 373356b | 2018-04-07 10:50:25 -0700 | [diff] [blame] | 200 | mScrimBehind.setDefaultFocusHighlightEnabled(false); |
| 201 | mScrimInFront.setDefaultFocusHighlightEnabled(false); |
| 202 | |
Selim Cinek | 511f270 | 2017-04-10 19:53:01 -0700 | [diff] [blame] | 203 | updateScrims(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 206 | public void transitionTo(ScrimState state) { |
| 207 | transitionTo(state, null); |
| 208 | } |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 209 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 210 | public void transitionTo(ScrimState state, Callback callback) { |
| 211 | if (state == mState) { |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 212 | // Call the callback anyway, unless it's already enqueued |
| 213 | if (callback != null && mCallback != callback) { |
| 214 | callback.onFinished(); |
| 215 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 216 | return; |
| 217 | } else if (DEBUG) { |
| 218 | Log.d(TAG, "State changed to: " + state); |
| 219 | } |
| 220 | |
| 221 | if (state == ScrimState.UNINITIALIZED) { |
| 222 | throw new IllegalArgumentException("Cannot change to UNINITIALIZED."); |
| 223 | } |
| 224 | |
Lucas Dupin | 8635c44 | 2017-12-08 10:36:28 -0800 | [diff] [blame] | 225 | final ScrimState oldState = mState; |
| 226 | mState = state; |
Lucas Dupin | 5866aaf | 2018-02-02 14:45:31 -0800 | [diff] [blame] | 227 | Trace.traceCounter(Trace.TRACE_TAG_APP, "scrim_state", mState.getIndex()); |
Lucas Dupin | 8635c44 | 2017-12-08 10:36:28 -0800 | [diff] [blame] | 228 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 229 | if (mCallback != null) { |
| 230 | mCallback.onCancelled(); |
| 231 | } |
| 232 | mCallback = callback; |
| 233 | |
Lucas Dupin | 8635c44 | 2017-12-08 10:36:28 -0800 | [diff] [blame] | 234 | state.prepare(oldState); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 235 | mScreenBlankingCallbackCalled = false; |
| 236 | mAnimationDelay = 0; |
| 237 | mBlankScreen = state.getBlanksScreen(); |
| 238 | mAnimateChange = state.getAnimateChange(); |
| 239 | mAnimationDuration = state.getAnimationDuration(); |
| 240 | mCurrentInFrontTint = state.getFrontTint(); |
| 241 | mCurrentBehindTint = state.getBehindTint(); |
| 242 | mCurrentInFrontAlpha = state.getFrontAlpha(); |
Lucas Dupin | 3d36dd8 | 2019-01-02 14:38:35 -0800 | [diff] [blame] | 243 | mCurrentBehindAlpha = state.getBehindAlpha(); |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 244 | applyExpansionToAlpha(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 245 | |
Lucas Dupin | 38962d7 | 2018-03-14 12:41:39 -0700 | [diff] [blame] | 246 | // Scrim might acquire focus when user is navigating with a D-pad or a keyboard. |
| 247 | // We need to disable focus otherwise AOD would end up with a gray overlay. |
| 248 | mScrimInFront.setFocusable(!state.isLowPowerState()); |
| 249 | mScrimBehind.setFocusable(!state.isLowPowerState()); |
| 250 | |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 251 | // Cancel blanking transitions that were pending before we requested a new state |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 252 | if (mPendingFrameCallback != null) { |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 253 | mScrimBehind.removeCallbacks(mPendingFrameCallback); |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 254 | mPendingFrameCallback = null; |
| 255 | } |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 256 | if (getHandler().hasCallbacks(mBlankingTransitionRunnable)) { |
| 257 | getHandler().removeCallbacks(mBlankingTransitionRunnable); |
| 258 | mBlankingTransitionRunnable = null; |
| 259 | } |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 260 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 261 | // Showing/hiding the keyguard means that scrim colors have to be switched, not necessary |
| 262 | // to do the same when you're just showing the brightness mirror. |
| 263 | mNeedsDrawableColorUpdate = state != ScrimState.BRIGHTNESS_MIRROR; |
| 264 | |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 265 | // The device might sleep if it's entering AOD, we need to make sure that |
| 266 | // the animation plays properly until the last frame. |
| 267 | // It's important to avoid holding the wakelock unless necessary because |
| 268 | // WakeLock#aqcuire will trigger an IPC and will cause jank. |
Lucas Dupin | 38962d7 | 2018-03-14 12:41:39 -0700 | [diff] [blame] | 269 | if (mState.isLowPowerState()) { |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 270 | holdWakeLock(); |
| 271 | } |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 272 | |
TYM Tsai | a71c892 | 2019-01-07 15:57:53 +0800 | [diff] [blame] | 273 | // AOD wallpapers should fade away after a while. |
| 274 | // Docking pulses may take a long time, wallpapers should also fade away after a while. |
Lucas Dupin | 023d727 | 2019-01-28 14:31:40 -0800 | [diff] [blame] | 275 | if (mWallpaperSupportsAmbientMode && mDozeParameters.getAlwaysOn() |
| 276 | && mState == ScrimState.AOD) { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 277 | if (!mWallpaperVisibilityTimedOut) { |
| 278 | mTimeTicker.schedule(mDozeParameters.getWallpaperAodDuration(), |
| 279 | AlarmTimeout.MODE_IGNORE_IF_SCHEDULED); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 280 | } |
Lucas Dupin | c780404 | 2018-12-21 12:26:33 -0800 | [diff] [blame] | 281 | } else { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 282 | mTimeTicker.cancel(); |
| 283 | mWallpaperVisibilityTimedOut = false; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Lucas Dupin | ff307d5 | 2018-02-05 21:16:39 -0800 | [diff] [blame] | 286 | if (mKeyguardUpdateMonitor.needsSlowUnlockTransition() && mState == ScrimState.UNLOCKED) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 287 | // In case the user isn't unlocked, make sure to delay a bit because the system is hosed |
| 288 | // with too many things at this case, in order to not skip the initial frames. |
| 289 | mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16); |
| 290 | mAnimationDelay = StatusBar.FADE_KEYGUARD_START_DELAY; |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 291 | } else if ((!mDozeParameters.getAlwaysOn() && oldState == ScrimState.AOD) |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 292 | || (mState == ScrimState.AOD && !mDozeParameters.getDisplayNeedsBlanking())) { |
| 293 | // Scheduling a frame isn't enough when: |
| 294 | // • Leaving doze and we need to modify scrim color immediately |
| 295 | // • ColorFade will not kick-in and scrim cannot wait for pre-draw. |
Lucas Dupin | ff307d5 | 2018-02-05 21:16:39 -0800 | [diff] [blame] | 296 | onPreDraw(); |
| 297 | } else { |
| 298 | scheduleUpdate(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 299 | } |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 300 | |
| 301 | dispatchScrimState(mScrimBehind.getViewAlpha()); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | public ScrimState getState() { |
| 305 | return mState; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 308 | protected void setScrimBehindValues(float scrimBehindAlphaKeyguard) { |
Jorim Jaggi | e31f6b8 | 2016-07-01 16:15:09 -0700 | [diff] [blame] | 309 | mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 310 | ScrimState[] states = ScrimState.values(); |
| 311 | for (int i = 0; i < states.length; i++) { |
| 312 | states[i].setScrimBehindAlphaKeyguard(scrimBehindAlphaKeyguard); |
| 313 | } |
Jorim Jaggi | e31f6b8 | 2016-07-01 16:15:09 -0700 | [diff] [blame] | 314 | scheduleUpdate(); |
| 315 | } |
| 316 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 317 | public void onTrackingStarted() { |
Jorim Jaggi | e93698b | 2016-11-11 18:24:38 -0800 | [diff] [blame] | 318 | mTracking = true; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 319 | mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 322 | public void onExpandingFinished() { |
Jorim Jaggi | e93698b | 2016-11-11 18:24:38 -0800 | [diff] [blame] | 323 | mTracking = false; |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 326 | @VisibleForTesting |
| 327 | protected void onHideWallpaperTimeout() { |
TYM Tsai | a71c892 | 2019-01-07 15:57:53 +0800 | [diff] [blame] | 328 | if (mState != ScrimState.AOD && mState != ScrimState.PULSING) { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | holdWakeLock(); |
| 333 | mWallpaperVisibilityTimedOut = true; |
| 334 | mAnimateChange = true; |
| 335 | mAnimationDuration = mDozeParameters.getWallpaperFadeOutDuration(); |
| 336 | scheduleUpdate(); |
| 337 | } |
| 338 | |
| 339 | private void holdWakeLock() { |
| 340 | if (!mWakeLockHeld) { |
| 341 | if (mWakeLock != null) { |
| 342 | mWakeLockHeld = true; |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 343 | mWakeLock.acquire(TAG); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 344 | } else { |
| 345 | Log.w(TAG, "Cannot hold wake lock, it has not been set yet"); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 350 | /** |
| 351 | * Current state of the shade expansion when pulling it from the top. |
| 352 | * This value is 1 when on top of the keyguard and goes to 0 as the user drags up. |
| 353 | * |
| 354 | * The expansion fraction is tied to the scrim opacity. |
| 355 | * |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 356 | * @param fraction From 0 to 1 where 0 means collapsed and 1 expanded. |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 357 | */ |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 358 | public void setPanelExpansion(float fraction) { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 359 | if (mExpansionFraction != fraction) { |
| 360 | mExpansionFraction = fraction; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 361 | |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 362 | final boolean keyguardOrUnlocked = mState == ScrimState.UNLOCKED |
Lucas Dupin | 1c32743 | 2019-01-03 13:37:53 -0800 | [diff] [blame] | 363 | || mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING; |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 364 | if (!keyguardOrUnlocked || !mExpansionAffectsAlpha) { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 365 | return; |
| 366 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 367 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 368 | applyExpansionToAlpha(); |
| 369 | |
| 370 | if (mUpdatePending) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 374 | setOrAdaptCurrentAnimation(mScrimBehind); |
| 375 | setOrAdaptCurrentAnimation(mScrimInFront); |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 376 | |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 377 | dispatchScrimState(mScrimBehind.getViewAlpha()); |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | private void setOrAdaptCurrentAnimation(View scrim) { |
| 382 | if (!isAnimating(scrim)) { |
| 383 | updateScrimColor(scrim, getCurrentScrimAlpha(scrim), getCurrentScrimTint(scrim)); |
| 384 | } else { |
| 385 | ValueAnimator previousAnimator = (ValueAnimator) scrim.getTag(TAG_KEY_ANIM); |
| 386 | float alpha = getCurrentScrimAlpha(scrim); |
| 387 | float previousEndValue = (Float) scrim.getTag(TAG_END_ALPHA); |
| 388 | float previousStartValue = (Float) scrim.getTag(TAG_START_ALPHA); |
| 389 | float relativeDiff = alpha - previousEndValue; |
| 390 | float newStartValue = previousStartValue + relativeDiff; |
| 391 | scrim.setTag(TAG_START_ALPHA, newStartValue); |
| 392 | scrim.setTag(TAG_END_ALPHA, alpha); |
| 393 | previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime()); |
Jorim Jaggi | 93439da | 2014-06-30 23:53:39 +0200 | [diff] [blame] | 394 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 397 | private void applyExpansionToAlpha() { |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 398 | if (!mExpansionAffectsAlpha) { |
| 399 | return; |
| 400 | } |
| 401 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 402 | if (mState == ScrimState.UNLOCKED) { |
| 403 | // Darken scrim as you pull down the shade when unlocked |
| 404 | float behindFraction = getInterpolatedFraction(); |
| 405 | behindFraction = (float) Math.pow(behindFraction, 0.8f); |
Lucas Dupin | d7c44eb | 2018-04-05 10:00:31 -0700 | [diff] [blame] | 406 | mCurrentBehindAlpha = behindFraction * GRADIENT_SCRIM_ALPHA_BUSY; |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 407 | mCurrentInFrontAlpha = 0; |
Lucas Dupin | 1c32743 | 2019-01-03 13:37:53 -0800 | [diff] [blame] | 408 | } else if (mState == ScrimState.KEYGUARD || mState == ScrimState.PULSING) { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 409 | // Either darken of make the scrim transparent when you |
| 410 | // pull down the shade |
| 411 | float interpolatedFract = getInterpolatedFraction(); |
Lucas Dupin | 3d36dd8 | 2019-01-02 14:38:35 -0800 | [diff] [blame] | 412 | float alphaBehind = mState.getBehindAlpha(); |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 413 | if (mDarkenWhileDragging) { |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 414 | mCurrentBehindAlpha = MathUtils.lerp(GRADIENT_SCRIM_ALPHA_BUSY, alphaBehind, |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 415 | interpolatedFract); |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 416 | mCurrentInFrontAlpha = 0; |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 417 | } else { |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 418 | mCurrentBehindAlpha = MathUtils.lerp(0 /* start */, alphaBehind, |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 419 | interpolatedFract); |
| 420 | mCurrentInFrontAlpha = 0; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 425 | /** |
Anthony Chen | e658cc2 | 2017-04-27 11:17:35 -0700 | [diff] [blame] | 426 | * Sets the given drawable as the background of the scrim that shows up behind the |
| 427 | * notifications. |
| 428 | */ |
| 429 | public void setScrimBehindDrawable(Drawable drawable) { |
| 430 | mScrimBehind.setDrawable(drawable); |
| 431 | } |
| 432 | |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 433 | /** |
Lucas Dupin | 69bda60 | 2018-05-18 17:24:52 -0700 | [diff] [blame] | 434 | * Sets the front scrim opacity in AOD so it's not as bright. |
| 435 | * <p> |
| 436 | * Displays usually don't support multiple dimming settings when in low power mode. |
| 437 | * The workaround is to modify the front scrim opacity when in AOD, so it's not as |
| 438 | * bright when you're at the movies or lying down on bed. |
| 439 | * <p> |
| 440 | * This value will be lost during transitions and only updated again after the the |
| 441 | * device is dozing when the light sensor is on. |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 442 | */ |
| 443 | public void setAodFrontScrimAlpha(float alpha) { |
Lucas Dupin | 69bda60 | 2018-05-18 17:24:52 -0700 | [diff] [blame] | 444 | if (mState == ScrimState.AOD && mDozeParameters.getAlwaysOn() |
| 445 | && mCurrentInFrontAlpha != alpha) { |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 446 | mCurrentInFrontAlpha = alpha; |
Lucas Dupin | 8523d13 | 2018-05-31 17:34:32 -0700 | [diff] [blame] | 447 | updateScrims(); |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | mState.AOD.setAodFrontScrimAlpha(alpha); |
| 451 | } |
| 452 | |
Evan Rosky | 9a89501 | 2016-04-21 11:26:15 -0700 | [diff] [blame] | 453 | protected void scheduleUpdate() { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 454 | if (mUpdatePending) return; |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 455 | |
| 456 | // Make sure that a frame gets scheduled. |
| 457 | mScrimBehind.invalidate(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 458 | mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this); |
| 459 | mUpdatePending = true; |
| 460 | } |
| 461 | |
Xiaohui Chen | 5da7135 | 2016-02-22 10:04:41 -0800 | [diff] [blame] | 462 | protected void updateScrims() { |
Lucas Dupin | 9324aa9 | 2017-07-26 20:29:38 -0700 | [diff] [blame] | 463 | // Make sure we have the right gradients and their opacities will satisfy GAR. |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 464 | if (mNeedsDrawableColorUpdate) { |
| 465 | mNeedsDrawableColorUpdate = false; |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 466 | boolean isKeyguard = mKeyguardUpdateMonitor.isKeyguardVisible() && !mKeyguardOccluded; |
| 467 | GradientColors currentScrimColors = isKeyguard ? mLockColors : mSystemColors; |
| 468 | // Only animate scrim color if the scrim view is actually visible |
| 469 | boolean animateScrimInFront = mScrimInFront.getViewAlpha() != 0 && !mBlankScreen; |
| 470 | boolean animateScrimBehind = mScrimBehind.getViewAlpha() != 0 && !mBlankScreen; |
| 471 | mScrimInFront.setColors(currentScrimColors, animateScrimInFront); |
| 472 | mScrimBehind.setColors(currentScrimColors, animateScrimBehind); |
Lucas Dupin | 9324aa9 | 2017-07-26 20:29:38 -0700 | [diff] [blame] | 473 | |
| 474 | // Calculate minimum scrim opacity for white or black text. |
| 475 | int textColor = currentScrimColors.supportsDarkText() ? Color.BLACK : Color.WHITE; |
| 476 | int mainColor = currentScrimColors.getMainColor(); |
| 477 | float minOpacity = ColorUtils.calculateMinimumBackgroundAlpha(textColor, mainColor, |
| 478 | 4.5f /* minimumContrast */) / 255f; |
| 479 | mScrimBehindAlpha = Math.max(mScrimBehindAlphaResValue, minOpacity); |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 480 | dispatchScrimState(mScrimBehind.getViewAlpha()); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 483 | // We want to override the back scrim opacity for the AOD state |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 484 | // when it's time to fade the wallpaper away. |
TYM Tsai | a71c892 | 2019-01-07 15:57:53 +0800 | [diff] [blame] | 485 | boolean aodWallpaperTimeout = (mState == ScrimState.AOD || mState == ScrimState.PULSING) |
| 486 | && mWallpaperVisibilityTimedOut; |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 487 | // We also want to hide FLAG_SHOW_WHEN_LOCKED activities under the scrim. |
| 488 | boolean occludedKeyguard = (mState == ScrimState.PULSING || mState == ScrimState.AOD) |
| 489 | && mKeyguardOccluded; |
| 490 | if (aodWallpaperTimeout || occludedKeyguard) { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 491 | mCurrentBehindAlpha = 1; |
| 492 | } |
| 493 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 494 | setScrimInFrontAlpha(mCurrentInFrontAlpha); |
| 495 | setScrimBehindAlpha(mCurrentBehindAlpha); |
| 496 | |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 497 | dispatchScrimsVisible(); |
| 498 | } |
| 499 | |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 500 | private void dispatchScrimState(float alpha) { |
| 501 | mScrimStateListener.accept(mState, alpha, mScrimInFront.getColors()); |
| 502 | } |
| 503 | |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 504 | private void dispatchScrimsVisible() { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 505 | final int currentScrimVisibility; |
| 506 | if (mScrimInFront.getViewAlpha() == 1 || mScrimBehind.getViewAlpha() == 1) { |
| 507 | currentScrimVisibility = VISIBILITY_FULLY_OPAQUE; |
| 508 | } else if (mScrimInFront.getViewAlpha() == 0 && mScrimBehind.getViewAlpha() == 0) { |
| 509 | currentScrimVisibility = VISIBILITY_FULLY_TRANSPARENT; |
| 510 | } else { |
| 511 | currentScrimVisibility = VISIBILITY_SEMI_TRANSPARENT; |
| 512 | } |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 513 | |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 514 | if (mScrimsVisibility != currentScrimVisibility) { |
| 515 | mScrimsVisibility = currentScrimVisibility; |
| 516 | mScrimVisibleListener.accept(currentScrimVisibility); |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 517 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 520 | private float getInterpolatedFraction() { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 521 | float frac = mExpansionFraction; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 522 | // let's start this 20% of the way down the screen |
| 523 | frac = frac * 1.2f - 0.2f; |
| 524 | if (frac <= 0) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 525 | return 0; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 526 | } else { |
| 527 | // woo, special effects |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 528 | return (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f)))); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 532 | private void setScrimBehindAlpha(float alpha) { |
| 533 | setScrimAlpha(mScrimBehind, alpha); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 536 | private void setScrimInFrontAlpha(float alpha) { |
| 537 | setScrimAlpha(mScrimInFront, alpha); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 540 | private void setScrimAlpha(ScrimView scrim, float alpha) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 541 | if (alpha == 0f) { |
| 542 | scrim.setClickable(false); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 543 | } else { |
Lucas Dupin | 1c32743 | 2019-01-03 13:37:53 -0800 | [diff] [blame] | 544 | // Eat touch events (unless dozing). |
| 545 | scrim.setClickable(mState != ScrimState.AOD); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 546 | } |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 547 | updateScrim(scrim, alpha); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 550 | private void updateScrimColor(View scrim, float alpha, int tint) { |
| 551 | alpha = Math.max(0, Math.min(1.0f, alpha)); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 552 | if (scrim instanceof ScrimView) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 553 | ScrimView scrimView = (ScrimView) scrim; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 554 | |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 555 | Trace.traceCounter(Trace.TRACE_TAG_APP, |
| 556 | scrim == mScrimInFront ? "front_scrim_alpha" : "back_scrim_alpha", |
| 557 | (int) (alpha * 255)); |
| 558 | |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 559 | Trace.traceCounter(Trace.TRACE_TAG_APP, |
| 560 | scrim == mScrimInFront ? "front_scrim_tint" : "back_scrim_tint", |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 561 | Color.alpha(tint)); |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 562 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 563 | scrimView.setTint(tint); |
| 564 | scrimView.setViewAlpha(alpha); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 565 | } else { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 566 | scrim.setAlpha(alpha); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 567 | } |
Selim Cinek | 8cd8040 | 2017-08-01 10:44:29 -0700 | [diff] [blame] | 568 | dispatchScrimsVisible(); |
Jorim Jaggi | 048af1f | 2014-11-11 22:51:10 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 571 | private void startScrimAnimation(final View scrim, float current) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 572 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); |
| 573 | final int initialScrimTint = scrim instanceof ScrimView ? ((ScrimView) scrim).getTint() : |
| 574 | Color.TRANSPARENT; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 575 | anim.addUpdateListener(animation -> { |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 576 | final float startAlpha = (Float) scrim.getTag(TAG_START_ALPHA); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 577 | final float animAmount = (float) animation.getAnimatedValue(); |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 578 | final int finalScrimTint = getCurrentScrimTint(scrim); |
| 579 | final float finalScrimAlpha = getCurrentScrimAlpha(scrim); |
| 580 | float alpha = MathUtils.lerp(startAlpha, finalScrimAlpha, animAmount); |
| 581 | alpha = MathUtils.constrain(alpha, 0f, 1f); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 582 | int tint = ColorUtils.blendARGB(initialScrimTint, finalScrimTint, animAmount); |
| 583 | updateScrimColor(scrim, alpha, tint); |
Adrian Roos | a5c6322 | 2017-07-27 16:33:39 +0200 | [diff] [blame] | 584 | dispatchScrimsVisible(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 585 | }); |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 586 | anim.setInterpolator(mInterpolator); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 587 | anim.setStartDelay(mAnimationDelay); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 588 | anim.setDuration(mAnimationDuration); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 589 | anim.addListener(new AnimatorListenerAdapter() { |
Joanne Chung | a393ad6 | 2019-01-16 21:41:31 +0800 | [diff] [blame] | 590 | private Callback lastCallback = mCallback; |
| 591 | |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 592 | @Override |
| 593 | public void onAnimationEnd(Animator animation) { |
Joanne Chung | a393ad6 | 2019-01-16 21:41:31 +0800 | [diff] [blame] | 594 | onFinished(lastCallback); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 595 | |
| 596 | scrim.setTag(TAG_KEY_ANIM, null); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 597 | dispatchScrimsVisible(); |
| 598 | |
Jorim Jaggi | f5304ad | 2017-07-17 18:31:13 +0200 | [diff] [blame] | 599 | if (!mDeferFinishedListener && mOnAnimationFinished != null) { |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 600 | mOnAnimationFinished.run(); |
| 601 | mOnAnimationFinished = null; |
| 602 | } |
| 603 | } |
| 604 | }); |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 605 | |
| 606 | // Cache alpha values because we might want to update this animator in the future if |
| 607 | // the user expands the panel while the animation is still running. |
| 608 | scrim.setTag(TAG_START_ALPHA, current); |
| 609 | scrim.setTag(TAG_END_ALPHA, getCurrentScrimAlpha(scrim)); |
| 610 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 611 | scrim.setTag(TAG_KEY_ANIM, anim); |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 612 | anim.start(); |
| 613 | } |
| 614 | |
| 615 | private float getCurrentScrimAlpha(View scrim) { |
| 616 | if (scrim == mScrimInFront) { |
| 617 | return mCurrentInFrontAlpha; |
| 618 | } else if (scrim == mScrimBehind) { |
| 619 | return mCurrentBehindAlpha; |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 620 | } else { |
| 621 | throw new IllegalArgumentException("Unknown scrim view"); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | private int getCurrentScrimTint(View scrim) { |
| 626 | if (scrim == mScrimInFront) { |
| 627 | return mCurrentInFrontTint; |
| 628 | } else if (scrim == mScrimBehind) { |
| 629 | return mCurrentBehindTint; |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 630 | } else { |
| 631 | throw new IllegalArgumentException("Unknown scrim view"); |
| 632 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 633 | } |
| 634 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 635 | @Override |
| 636 | public boolean onPreDraw() { |
| 637 | mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this); |
| 638 | mUpdatePending = false; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 639 | if (mCallback != null) { |
| 640 | mCallback.onStart(); |
Selim Cinek | 372d1bd | 2015-08-14 13:19:37 -0700 | [diff] [blame] | 641 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 642 | updateScrims(); |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 643 | if (mOnAnimationFinished != null && !isAnimating(mScrimInFront) |
| 644 | && !isAnimating(mScrimBehind)) { |
| 645 | mOnAnimationFinished.run(); |
| 646 | mOnAnimationFinished = null; |
| 647 | } |
Selim Cinek | edd32b8 | 2015-06-23 22:05:58 -0400 | [diff] [blame] | 648 | return true; |
| 649 | } |
| 650 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 651 | private void onFinished() { |
Joanne Chung | a393ad6 | 2019-01-16 21:41:31 +0800 | [diff] [blame] | 652 | onFinished(mCallback); |
| 653 | } |
| 654 | |
| 655 | private void onFinished(Callback callback) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 656 | if (mWakeLockHeld) { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 657 | mWakeLock.release(TAG); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 658 | mWakeLockHeld = false; |
| 659 | } |
Joanne Chung | a393ad6 | 2019-01-16 21:41:31 +0800 | [diff] [blame] | 660 | |
| 661 | if (callback != null) { |
| 662 | callback.onFinished(); |
| 663 | |
| 664 | if (callback == mCallback) { |
| 665 | mCallback = null; |
| 666 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 667 | } |
Joanne Chung | a393ad6 | 2019-01-16 21:41:31 +0800 | [diff] [blame] | 668 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 669 | // When unlocking with fingerprint, we'll fade the scrims from black to transparent. |
| 670 | // At the end of the animation we need to remove the tint. |
| 671 | if (mState == ScrimState.UNLOCKED) { |
| 672 | mCurrentInFrontTint = Color.TRANSPARENT; |
| 673 | mCurrentBehindTint = Color.TRANSPARENT; |
| 674 | } |
| 675 | } |
| 676 | |
Jorim Jaggi | 83eb6bb | 2015-08-17 17:38:58 -0700 | [diff] [blame] | 677 | private boolean isAnimating(View scrim) { |
| 678 | return scrim.getTag(TAG_KEY_ANIM) != null; |
| 679 | } |
| 680 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 681 | @VisibleForTesting |
| 682 | void setOnAnimationFinished(Runnable onAnimationFinished) { |
| 683 | mOnAnimationFinished = onAnimationFinished; |
| 684 | } |
Adrian Roos | 5f72c92 | 2016-03-08 15:54:26 -0800 | [diff] [blame] | 685 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 686 | private void updateScrim(ScrimView scrim, float alpha) { |
| 687 | final float currentAlpha = scrim.getViewAlpha(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 688 | |
| 689 | ValueAnimator previousAnimator = ViewState.getChildTag(scrim, TAG_KEY_ANIM); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 690 | if (previousAnimator != null) { |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 691 | if (mAnimateChange) { |
Jorim Jaggi | f5304ad | 2017-07-17 18:31:13 +0200 | [diff] [blame] | 692 | // We are not done yet! Defer calling the finished listener. |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 693 | mDeferFinishedListener = true; |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 694 | } |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 695 | // Previous animators should always be cancelled. Not doing so would cause |
| 696 | // overlap, especially on states that don't animate, leading to flickering, |
| 697 | // and in the worst case, an internal state that doesn't represent what |
| 698 | // transitionTo requested. |
| 699 | cancelAnimator(previousAnimator); |
| 700 | mDeferFinishedListener = false; |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 701 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 702 | |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 703 | if (mPendingFrameCallback != null) { |
| 704 | // Display is off and we're waiting. |
| 705 | return; |
| 706 | } else if (mBlankScreen) { |
| 707 | // Need to blank the display before continuing. |
| 708 | blankDisplay(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 709 | return; |
| 710 | } else if (!mScreenBlankingCallbackCalled) { |
| 711 | // Not blanking the screen. Letting the callback know that we're ready |
| 712 | // to replace what was on the screen before. |
| 713 | if (mCallback != null) { |
| 714 | mCallback.onDisplayBlanked(); |
| 715 | mScreenBlankingCallbackCalled = true; |
| 716 | } |
| 717 | } |
| 718 | |
Lucas Dupin | 6afae37 | 2017-11-30 13:09:22 -0500 | [diff] [blame] | 719 | if (scrim == mScrimBehind) { |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 720 | dispatchScrimState(alpha); |
Lucas Dupin | 6afae37 | 2017-11-30 13:09:22 -0500 | [diff] [blame] | 721 | } |
| 722 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 723 | final boolean wantsAlphaUpdate = alpha != currentAlpha; |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 724 | final boolean wantsTintUpdate = scrim.getTint() != getCurrentScrimTint(scrim); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 725 | |
| 726 | if (wantsAlphaUpdate || wantsTintUpdate) { |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 727 | if (mAnimateChange) { |
| 728 | startScrimAnimation(scrim, currentAlpha); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 729 | } else { |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 730 | // update the alpha directly |
| 731 | updateScrimColor(scrim, alpha, getCurrentScrimTint(scrim)); |
| 732 | onFinished(); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 733 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 734 | } else { |
| 735 | onFinished(); |
Selim Cinek | aac9325 | 2015-04-14 20:04:12 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 739 | @VisibleForTesting |
| 740 | protected void cancelAnimator(ValueAnimator previousAnimator) { |
| 741 | if (previousAnimator != null) { |
| 742 | previousAnimator.cancel(); |
| 743 | } |
| 744 | } |
| 745 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 746 | private void blankDisplay() { |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 747 | updateScrimColor(mScrimInFront, 1, Color.BLACK); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 748 | |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 749 | // Notify callback that the screen is completely black and we're |
| 750 | // ready to change the display power mode |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 751 | mPendingFrameCallback = () -> { |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 752 | if (mCallback != null) { |
| 753 | mCallback.onDisplayBlanked(); |
| 754 | mScreenBlankingCallbackCalled = true; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 755 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 756 | |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 757 | mBlankingTransitionRunnable = () -> { |
| 758 | mBlankingTransitionRunnable = null; |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 759 | mPendingFrameCallback = null; |
| 760 | mBlankScreen = false; |
| 761 | // Try again. |
| 762 | updateScrims(); |
| 763 | }; |
| 764 | |
| 765 | // Setting power states can happen after we push out the frame. Make sure we |
| 766 | // stay fully opaque until the power state request reaches the lower levels. |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 767 | final int delay = mScreenOn ? 32 : 500; |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 768 | if (DEBUG) { |
Lucas Dupin | 3a64ed7 | 2018-02-12 12:44:37 -0800 | [diff] [blame] | 769 | Log.d(TAG, "Fading out scrims with delay: " + delay); |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 770 | } |
Lucas Dupin | 3a64ed7 | 2018-02-12 12:44:37 -0800 | [diff] [blame] | 771 | getHandler().postDelayed(mBlankingTransitionRunnable, delay); |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 772 | }; |
| 773 | doOnTheNextFrame(mPendingFrameCallback); |
| 774 | } |
| 775 | |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 776 | /** |
| 777 | * Executes a callback after the frame has hit the display. |
| 778 | * @param callback What to run. |
| 779 | */ |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 780 | @VisibleForTesting |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 781 | protected void doOnTheNextFrame(Runnable callback) { |
| 782 | // Just calling View#postOnAnimation isn't enough because the frame might not have reached |
| 783 | // the display yet. A timeout is the safest solution. |
| 784 | mScrimBehind.postOnAnimationDelayed(callback, 32 /* delayMillis */); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | @VisibleForTesting |
| 788 | protected Handler getHandler() { |
| 789 | return Handler.getMain(); |
| 790 | } |
| 791 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 792 | public int getBackgroundColor() { |
| 793 | int color = mLockColors.getMainColor(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 794 | return Color.argb((int) (mScrimBehind.getViewAlpha() * Color.alpha(color)), |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 795 | Color.red(color), Color.green(color), Color.blue(color)); |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | public void setScrimBehindChangeRunnable(Runnable changeRunnable) { |
| 799 | mScrimBehind.setChangeRunnable(changeRunnable); |
| 800 | } |
Selim Cinek | e803491c | 2016-04-09 21:24:45 -0700 | [diff] [blame] | 801 | |
Jorim Jaggi | e31f6b8 | 2016-07-01 16:15:09 -0700 | [diff] [blame] | 802 | public void setCurrentUser(int currentUser) { |
| 803 | // Don't care in the base class. |
| 804 | } |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 805 | |
| 806 | @Override |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 807 | public void onColorsChanged(ColorExtractor colorExtractor, int which) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 808 | if ((which & WallpaperManager.FLAG_LOCK) != 0) { |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 809 | mLockColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 810 | ColorExtractor.TYPE_DARK, true /* ignoreVisibility */); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 811 | mNeedsDrawableColorUpdate = true; |
| 812 | scheduleUpdate(); |
| 813 | } |
| 814 | if ((which & WallpaperManager.FLAG_SYSTEM) != 0) { |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 815 | mSystemColors = mColorExtractor.getColors(WallpaperManager.FLAG_SYSTEM, |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 816 | ColorExtractor.TYPE_DARK, mState != ScrimState.UNLOCKED); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 817 | mNeedsDrawableColorUpdate = true; |
| 818 | scheduleUpdate(); |
| 819 | } |
| 820 | } |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 821 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 822 | @VisibleForTesting |
| 823 | protected WakeLock createWakeLock() { |
| 824 | return new DelayedWakeLock(getHandler(), |
Lucas Dupin | e03081f | 2018-04-03 11:31:27 -0700 | [diff] [blame] | 825 | WakeLock.createPartial(mContext, "Scrims")); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 826 | } |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 827 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 828 | @Override |
| 829 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Lucas Dupin | 3fcdd47 | 2018-01-19 19:06:45 -0800 | [diff] [blame] | 830 | pw.println(" ScrimController: "); |
| 831 | pw.print(" state: "); pw.println(mState); |
| 832 | pw.print(" frontScrim:"); pw.print(" viewAlpha="); pw.print(mScrimInFront.getViewAlpha()); |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 833 | pw.print(" alpha="); pw.print(mCurrentInFrontAlpha); |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 834 | pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimInFront.getTint())); |
| 835 | |
Lucas Dupin | 3fcdd47 | 2018-01-19 19:06:45 -0800 | [diff] [blame] | 836 | pw.print(" backScrim:"); pw.print(" viewAlpha="); pw.print(mScrimBehind.getViewAlpha()); |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 837 | pw.print(" alpha="); pw.print(mCurrentBehindAlpha); |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 838 | pw.print(" tint=0x"); pw.println(Integer.toHexString(mScrimBehind.getTint())); |
| 839 | |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 840 | pw.print(" mTracking="); pw.println(mTracking); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 843 | public void setWallpaperSupportsAmbientMode(boolean wallpaperSupportsAmbientMode) { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 844 | mWallpaperSupportsAmbientMode = wallpaperSupportsAmbientMode; |
| 845 | ScrimState[] states = ScrimState.values(); |
| 846 | for (int i = 0; i < states.length; i++) { |
| 847 | states[i].setWallpaperSupportsAmbientMode(wallpaperSupportsAmbientMode); |
| 848 | } |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 851 | /** |
| 852 | * Interrupts blanking transitions once the display notifies that it's already on. |
| 853 | */ |
| 854 | public void onScreenTurnedOn() { |
Lucas Dupin | 3a64ed7 | 2018-02-12 12:44:37 -0800 | [diff] [blame] | 855 | mScreenOn = true; |
Lucas Dupin | 53cf12a | 2018-02-05 16:14:45 -0800 | [diff] [blame] | 856 | final Handler handler = getHandler(); |
| 857 | if (handler.hasCallbacks(mBlankingTransitionRunnable)) { |
| 858 | if (DEBUG) { |
| 859 | Log.d(TAG, "Shorter blanking because screen turned on. All good."); |
| 860 | } |
| 861 | handler.removeCallbacks(mBlankingTransitionRunnable); |
| 862 | mBlankingTransitionRunnable.run(); |
| 863 | } |
| 864 | } |
| 865 | |
Lucas Dupin | 3a64ed7 | 2018-02-12 12:44:37 -0800 | [diff] [blame] | 866 | public void onScreenTurnedOff() { |
| 867 | mScreenOn = false; |
| 868 | } |
| 869 | |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 870 | public void setExpansionAffectsAlpha(boolean expansionAffectsAlpha) { |
| 871 | mExpansionAffectsAlpha = expansionAffectsAlpha; |
| 872 | } |
| 873 | |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 874 | public void setKeyguardOccluded(boolean keyguardOccluded) { |
| 875 | mKeyguardOccluded = keyguardOccluded; |
Lucas Dupin | 63d7217 | 2018-06-06 11:42:55 -0700 | [diff] [blame] | 876 | updateScrims(); |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 879 | public void setHasBackdrop(boolean hasBackdrop) { |
Lucas Dupin | 193677c | 2018-06-11 19:16:03 -0700 | [diff] [blame] | 880 | for (ScrimState state : ScrimState.values()) { |
| 881 | state.setHasBackdrop(hasBackdrop); |
| 882 | } |
Lucas Dupin | 9bee582 | 2018-07-09 14:32:53 -0700 | [diff] [blame] | 883 | |
| 884 | // Backdrop event may arrive after state was already applied, |
| 885 | // in this case, back-scrim needs to be re-evaluated |
| 886 | if (mState == ScrimState.AOD || mState == ScrimState.PULSING) { |
Lucas Dupin | 3d36dd8 | 2019-01-02 14:38:35 -0800 | [diff] [blame] | 887 | float newBehindAlpha = mState.getBehindAlpha(); |
Lucas Dupin | 9bee582 | 2018-07-09 14:32:53 -0700 | [diff] [blame] | 888 | if (mCurrentBehindAlpha != newBehindAlpha) { |
| 889 | mCurrentBehindAlpha = newBehindAlpha; |
| 890 | updateScrims(); |
| 891 | } |
| 892 | } |
Lucas Dupin | 193677c | 2018-06-11 19:16:03 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | public void setLaunchingAffordanceWithPreview(boolean launchingAffordanceWithPreview) { |
| 896 | for (ScrimState state : ScrimState.values()) { |
| 897 | state.setLaunchingAffordanceWithPreview(launchingAffordanceWithPreview); |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 901 | public void setPulseReason(int pulseReason) { |
| 902 | ScrimState.PULSING.setPulseReason(pulseReason); |
| 903 | } |
| 904 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 905 | public interface Callback { |
| 906 | default void onStart() { |
| 907 | } |
| 908 | default void onDisplayBlanked() { |
| 909 | } |
| 910 | default void onFinished() { |
| 911 | } |
| 912 | default void onCancelled() { |
| 913 | } |
Adrian Roos | ba7ca59 | 2017-08-15 17:48:05 +0200 | [diff] [blame] | 914 | } |
Lucas Dupin | 690c6f5 | 2018-07-10 15:28:57 -0700 | [diff] [blame] | 915 | |
| 916 | /** |
| 917 | * Simple keyguard callback that updates scrims when keyguard visibility changes. |
| 918 | */ |
| 919 | private class KeyguardVisibilityCallback extends KeyguardUpdateMonitorCallback { |
| 920 | |
| 921 | @Override |
| 922 | public void onKeyguardVisibilityChanged(boolean showing) { |
| 923 | mNeedsDrawableColorUpdate = true; |
| 924 | scheduleUpdate(); |
| 925 | } |
| 926 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 927 | } |