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; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 22 | import android.graphics.Color; |
| 23 | import android.graphics.drawable.ColorDrawable; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 24 | import android.util.Log; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.ViewTreeObserver; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 27 | import android.view.animation.DecelerateInterpolator; |
| 28 | import android.view.animation.Interpolator; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 29 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 30 | import com.android.systemui.R; |
| 31 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 32 | /** |
| 33 | * Controls both the scrim behind the notifications and in front of the notifications (when a |
| 34 | * security method gets shown). |
| 35 | */ |
| 36 | public class ScrimController implements ViewTreeObserver.OnPreDrawListener { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 37 | private static final String TAG = "ScrimController"; |
| 38 | private static final boolean DEBUG = false; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 39 | |
| 40 | private static final float SCRIM_BEHIND_ALPHA = 0.62f; |
| 41 | private static final float SCRIM_BEHIND_ALPHA_KEYGUARD = 0.5f; |
| 42 | private static final float SCRIM_IN_FRONT_ALPHA = 0.75f; |
| 43 | private static final long ANIMATION_DURATION = 220; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 44 | private static final int TAG_KEY_ANIM = R.id.scrim; |
| 45 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 46 | private static final long PULSE_IN_ANIMATION_DURATION = 1000; |
| 47 | private static final long PULSE_VISIBLE_DURATION = 2000; |
| 48 | private static final long PULSE_OUT_ANIMATION_DURATION = 1000; |
| 49 | private static final long PULSE_INVISIBLE_DURATION = 1000; |
| 50 | private static final long PULSE_DURATION = PULSE_IN_ANIMATION_DURATION |
| 51 | + PULSE_VISIBLE_DURATION + PULSE_OUT_ANIMATION_DURATION + PULSE_INVISIBLE_DURATION; |
| 52 | private static final long PRE_PULSE_DELAY = 1000; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 53 | |
| 54 | private final View mScrimBehind; |
| 55 | private final View mScrimInFront; |
| 56 | private final UnlockMethodCache mUnlockMethodCache; |
| 57 | |
| 58 | private boolean mKeyguardShowing; |
| 59 | private float mFraction; |
| 60 | |
| 61 | private boolean mDarkenWhileDragging; |
| 62 | private boolean mBouncerShowing; |
| 63 | private boolean mAnimateChange; |
| 64 | private boolean mUpdatePending; |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 65 | private boolean mExpanding; |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 66 | private boolean mAnimateKeyguardFadingOut; |
| 67 | private long mDurationOverride = -1; |
| 68 | private long mAnimationDelay; |
| 69 | private Runnable mOnAnimationFinished; |
| 70 | private boolean mAnimationStarted; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 71 | private boolean mDozing; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 72 | private int mPulsesRemaining; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 73 | private final Interpolator mInterpolator = new DecelerateInterpolator(); |
| 74 | |
| 75 | public ScrimController(View scrimBehind, View scrimInFront) { |
| 76 | mScrimBehind = scrimBehind; |
| 77 | mScrimInFront = scrimInFront; |
| 78 | mUnlockMethodCache = UnlockMethodCache.getInstance(scrimBehind.getContext()); |
| 79 | } |
| 80 | |
| 81 | public void setKeyguardShowing(boolean showing) { |
| 82 | mKeyguardShowing = showing; |
| 83 | scheduleUpdate(); |
| 84 | } |
| 85 | |
| 86 | public void onTrackingStarted() { |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 87 | mExpanding = true; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 88 | mDarkenWhileDragging = !mUnlockMethodCache.isMethodInsecure(); |
| 89 | } |
| 90 | |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 91 | public void onExpandingFinished() { |
| 92 | mExpanding = false; |
| 93 | } |
| 94 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 95 | public void setPanelExpansion(float fraction) { |
Jorim Jaggi | 93439da | 2014-06-30 23:53:39 +0200 | [diff] [blame] | 96 | if (mFraction != fraction) { |
| 97 | mFraction = fraction; |
| 98 | scheduleUpdate(); |
| 99 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public void setBouncerShowing(boolean showing) { |
| 103 | mBouncerShowing = showing; |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 104 | mAnimateChange = !mExpanding; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 105 | scheduleUpdate(); |
| 106 | } |
| 107 | |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 108 | public void animateKeyguardFadingOut(long delay, long duration, Runnable onAnimationFinished) { |
| 109 | mAnimateKeyguardFadingOut = true; |
| 110 | mDurationOverride = duration; |
| 111 | mAnimationDelay = delay; |
| 112 | mAnimateChange = true; |
| 113 | mOnAnimationFinished = onAnimationFinished; |
| 114 | scheduleUpdate(); |
| 115 | } |
| 116 | |
Jorim Jaggi | dbc3dce | 2014-08-01 01:16:36 +0200 | [diff] [blame] | 117 | public void animateGoingToFullShade(long delay, long duration) { |
| 118 | mDurationOverride = duration; |
| 119 | mAnimationDelay = delay; |
| 120 | mAnimateChange = true; |
| 121 | scheduleUpdate(); |
| 122 | } |
| 123 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 124 | public void setDozing(boolean dozing) { |
| 125 | if (mDozing == dozing) return; |
| 126 | mDozing = dozing; |
| 127 | if (!mDozing) { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 128 | cancelPulsing(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 129 | } |
| 130 | scheduleUpdate(); |
| 131 | } |
| 132 | |
| 133 | /** When dozing, fade screen contents in and out a few times using the front scrim. */ |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 134 | public long pulse(int pulses) { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 135 | if (!mDozing) return 0; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 136 | mPulsesRemaining = Math.max(pulses, mPulsesRemaining); |
| 137 | mScrimInFront.postDelayed(mPulseIn, PRE_PULSE_DELAY); |
| 138 | return PRE_PULSE_DELAY + mPulsesRemaining * PULSE_DURATION; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 139 | } |
| 140 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 141 | private void cancelPulsing() { |
| 142 | mPulsesRemaining = 0; |
| 143 | mScrimInFront.removeCallbacks(mPulseIn); |
| 144 | mScrimInFront.removeCallbacks(mPulseOut); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 147 | private void scheduleUpdate() { |
| 148 | if (mUpdatePending) return; |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 149 | |
| 150 | // Make sure that a frame gets scheduled. |
| 151 | mScrimBehind.invalidate(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 152 | mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this); |
| 153 | mUpdatePending = true; |
| 154 | } |
| 155 | |
| 156 | private void updateScrims() { |
Selim Cinek | baa2327 | 2014-07-08 18:01:07 +0200 | [diff] [blame] | 157 | if (mAnimateKeyguardFadingOut) { |
| 158 | setScrimInFrontColor(0f); |
| 159 | setScrimBehindColor(0f); |
| 160 | }else if (!mKeyguardShowing && !mBouncerShowing) { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 161 | updateScrimNormal(); |
| 162 | setScrimInFrontColor(0); |
| 163 | } else { |
| 164 | updateScrimKeyguard(); |
| 165 | } |
| 166 | mAnimateChange = false; |
| 167 | } |
| 168 | |
| 169 | private void updateScrimKeyguard() { |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 170 | if (mExpanding && mDarkenWhileDragging) { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 171 | float behindFraction = Math.max(0, Math.min(mFraction, 1)); |
| 172 | float fraction = 1 - behindFraction; |
| 173 | setScrimInFrontColor(fraction * SCRIM_IN_FRONT_ALPHA); |
| 174 | setScrimBehindColor(behindFraction * SCRIM_BEHIND_ALPHA_KEYGUARD); |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 175 | } else if (mBouncerShowing) { |
| 176 | setScrimInFrontColor(SCRIM_IN_FRONT_ALPHA); |
| 177 | setScrimBehindColor(0f); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 178 | } else if (mDozing) { |
| 179 | setScrimInFrontColor(1); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 180 | } else { |
| 181 | setScrimInFrontColor(0f); |
| 182 | setScrimBehindColor(SCRIM_BEHIND_ALPHA_KEYGUARD); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void updateScrimNormal() { |
| 187 | float frac = mFraction; |
| 188 | // let's start this 20% of the way down the screen |
| 189 | frac = frac * 1.2f - 0.2f; |
| 190 | if (frac <= 0) { |
| 191 | setScrimBehindColor(0); |
| 192 | } else { |
| 193 | // woo, special effects |
| 194 | final float k = (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f)))); |
| 195 | setScrimBehindColor(k * SCRIM_BEHIND_ALPHA); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private void setScrimBehindColor(float alpha) { |
| 200 | setScrimColor(mScrimBehind, alpha); |
| 201 | } |
| 202 | |
| 203 | private void setScrimInFrontColor(float alpha) { |
| 204 | setScrimColor(mScrimInFront, alpha); |
| 205 | if (alpha == 0f) { |
| 206 | mScrimInFront.setClickable(false); |
| 207 | } else { |
| 208 | |
| 209 | // Eat touch events. |
| 210 | mScrimInFront.setClickable(true); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void setScrimColor(View scrim, float alpha) { |
| 215 | int color = Color.argb((int) (alpha * 255), 0, 0, 0); |
| 216 | if (mAnimateChange) { |
| 217 | startScrimAnimation(scrim, color); |
| 218 | } else { |
| 219 | scrim.setBackgroundColor(color); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private void startScrimAnimation(final View scrim, int targetColor) { |
| 224 | int current = getBackgroundAlpha(scrim); |
| 225 | int target = Color.alpha(targetColor); |
| 226 | if (current == targetColor) { |
| 227 | return; |
| 228 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 229 | Object runningAnim = scrim.getTag(TAG_KEY_ANIM); |
| 230 | if (runningAnim instanceof ValueAnimator) { |
| 231 | ((ValueAnimator) runningAnim).cancel(); |
| 232 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 233 | ValueAnimator anim = ValueAnimator.ofInt(current, target); |
| 234 | anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 235 | @Override |
| 236 | public void onAnimationUpdate(ValueAnimator animation) { |
| 237 | int value = (int) animation.getAnimatedValue(); |
| 238 | scrim.setBackgroundColor(Color.argb(value, 0, 0, 0)); |
| 239 | } |
| 240 | }); |
| 241 | anim.setInterpolator(mInterpolator); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 242 | anim.setStartDelay(mAnimationDelay); |
| 243 | anim.setDuration(mDurationOverride != -1 ? mDurationOverride : ANIMATION_DURATION); |
| 244 | anim.addListener(new AnimatorListenerAdapter() { |
| 245 | |
| 246 | @Override |
| 247 | public void onAnimationEnd(Animator animation) { |
| 248 | if (mOnAnimationFinished != null) { |
| 249 | mOnAnimationFinished.run(); |
| 250 | mOnAnimationFinished = null; |
| 251 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 252 | scrim.setTag(TAG_KEY_ANIM, null); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 253 | } |
| 254 | }); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 255 | anim.start(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 256 | scrim.setTag(TAG_KEY_ANIM, anim); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 257 | mAnimationStarted = true; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | private int getBackgroundAlpha(View scrim) { |
| 261 | if (scrim.getBackground() instanceof ColorDrawable) { |
| 262 | ColorDrawable drawable = (ColorDrawable) scrim.getBackground(); |
| 263 | return Color.alpha(drawable.getColor()); |
| 264 | } else { |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public boolean onPreDraw() { |
| 271 | mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this); |
| 272 | mUpdatePending = false; |
| 273 | updateScrims(); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 274 | mAnimateKeyguardFadingOut = false; |
| 275 | mDurationOverride = -1; |
| 276 | mAnimationDelay = 0; |
| 277 | |
| 278 | // Make sure that we always call the listener even if we didn't start an animation. |
| 279 | if (!mAnimationStarted && mOnAnimationFinished != null) { |
| 280 | mOnAnimationFinished.run(); |
| 281 | mOnAnimationFinished = null; |
| 282 | } |
| 283 | mAnimationStarted = false; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 284 | return true; |
| 285 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 286 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 287 | private final Runnable mPulseIn = new Runnable() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 288 | @Override |
| 289 | public void run() { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 290 | if (DEBUG) Log.d(TAG, "Pulse in, mDozing=" + mDozing |
| 291 | + " mPulsesRemaining=" + mPulsesRemaining); |
| 292 | if (!mDozing || mPulsesRemaining == 0) return; |
| 293 | mPulsesRemaining--; |
| 294 | mDurationOverride = PULSE_IN_ANIMATION_DURATION; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 295 | mAnimationDelay = 0; |
| 296 | mAnimateChange = true; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 297 | mOnAnimationFinished = mPulseInFinished; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 298 | setScrimColor(mScrimInFront, 0); |
| 299 | } |
| 300 | }; |
| 301 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 302 | private final Runnable mPulseInFinished = new Runnable() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 303 | @Override |
| 304 | public void run() { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 305 | if (DEBUG) Log.d(TAG, "Pulse in finished, mDozing=" + mDozing); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 306 | if (!mDozing) return; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 307 | mScrimInFront.postDelayed(mPulseOut, PULSE_VISIBLE_DURATION); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 308 | } |
| 309 | }; |
| 310 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 311 | private final Runnable mPulseOut = new Runnable() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 312 | @Override |
| 313 | public void run() { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 314 | if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 315 | if (!mDozing) return; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 316 | mDurationOverride = PULSE_OUT_ANIMATION_DURATION; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 317 | mAnimationDelay = 0; |
| 318 | mAnimateChange = true; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 319 | mOnAnimationFinished = mPulseOutFinished; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 320 | setScrimColor(mScrimInFront, 1); |
| 321 | } |
| 322 | }; |
| 323 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 324 | private final Runnable mPulseOutFinished = new Runnable() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 325 | @Override |
| 326 | public void run() { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame^] | 327 | if (DEBUG) Log.d(TAG, "Pulse out finished, mPulsesRemaining=" + mPulsesRemaining); |
| 328 | if (mPulsesRemaining > 0) { |
| 329 | mScrimInFront.postDelayed(mPulseIn, PULSE_INVISIBLE_DURATION); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | }; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 333 | } |