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