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