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