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