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