ScrimController.java revision 559d959b99f4daafc8befb2d9a8f2d85d771bf49
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 long PULSE_IN_ANIMATION_DURATION = 1000;
47    private static final long PULSE_VISIBLE_DURATION = 3000;
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;
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 mPulsesRemaining;
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() {
87        mExpanding = true;
88        mDarkenWhileDragging = !mUnlockMethodCache.isMethodInsecure();
89    }
90
91    public void onExpandingFinished() {
92        mExpanding = false;
93    }
94
95    public void setPanelExpansion(float fraction) {
96        if (mFraction != fraction) {
97            mFraction = fraction;
98            scheduleUpdate();
99        }
100    }
101
102    public void setBouncerShowing(boolean showing) {
103        mBouncerShowing = showing;
104        mAnimateChange = !mExpanding;
105        scheduleUpdate();
106    }
107
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
117    public void animateGoingToFullShade(long delay, long duration) {
118        mDurationOverride = duration;
119        mAnimationDelay = delay;
120        mAnimateChange = true;
121        scheduleUpdate();
122    }
123
124    public void setDozing(boolean dozing) {
125        if (mDozing == dozing) return;
126        mDozing = dozing;
127        if (!mDozing) {
128            cancelPulsing();
129        }
130        scheduleUpdate();
131    }
132
133    /** When dozing, fade screen contents in and out a few times using the front scrim. */
134    public long pulse(int pulses, boolean delayed) {
135        if (!mDozing) return 0;
136        mPulsesRemaining = Math.max(pulses, mPulsesRemaining);
137        final long delay = delayed ? PRE_PULSE_DELAY : 0;
138        mScrimInFront.postDelayed(mPulseIn, delay);
139        return delay + mPulsesRemaining * PULSE_DURATION;
140    }
141
142    private void cancelPulsing() {
143        mPulsesRemaining = 0;
144        mScrimInFront.removeCallbacks(mPulseIn);
145        mScrimInFront.removeCallbacks(mPulseOut);
146    }
147
148    private void scheduleUpdate() {
149        if (mUpdatePending) return;
150
151        // Make sure that a frame gets scheduled.
152        mScrimBehind.invalidate();
153        mScrimBehind.getViewTreeObserver().addOnPreDrawListener(this);
154        mUpdatePending = true;
155    }
156
157    private void updateScrims() {
158        if (mAnimateKeyguardFadingOut) {
159            setScrimInFrontColor(0f);
160            setScrimBehindColor(0f);
161        }else if (!mKeyguardShowing && !mBouncerShowing) {
162            updateScrimNormal();
163            setScrimInFrontColor(0);
164        } else {
165            updateScrimKeyguard();
166        }
167        mAnimateChange = false;
168    }
169
170    private void updateScrimKeyguard() {
171        if (mExpanding && mDarkenWhileDragging) {
172            float behindFraction = Math.max(0, Math.min(mFraction, 1));
173            float fraction = 1 - behindFraction;
174            setScrimInFrontColor(fraction * SCRIM_IN_FRONT_ALPHA);
175            setScrimBehindColor(behindFraction * SCRIM_BEHIND_ALPHA_KEYGUARD);
176        } else if (mBouncerShowing) {
177            setScrimInFrontColor(SCRIM_IN_FRONT_ALPHA);
178            setScrimBehindColor(0f);
179        } else if (mDozing) {
180            setScrimInFrontColor(1);
181        } else {
182            setScrimInFrontColor(0f);
183            setScrimBehindColor(SCRIM_BEHIND_ALPHA_KEYGUARD);
184        }
185    }
186
187    private void updateScrimNormal() {
188        float frac = mFraction;
189        // let's start this 20% of the way down the screen
190        frac = frac * 1.2f - 0.2f;
191        if (frac <= 0) {
192            setScrimBehindColor(0);
193        } else {
194            // woo, special effects
195            final float k = (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f))));
196            setScrimBehindColor(k * SCRIM_BEHIND_ALPHA);
197        }
198    }
199
200    private void setScrimBehindColor(float alpha) {
201        setScrimColor(mScrimBehind, alpha);
202    }
203
204    private void setScrimInFrontColor(float alpha) {
205        setScrimColor(mScrimInFront, alpha);
206        if (alpha == 0f) {
207            mScrimInFront.setClickable(false);
208        } else {
209
210            // Eat touch events.
211            mScrimInFront.setClickable(true);
212        }
213    }
214
215    private void setScrimColor(View scrim, float alpha) {
216        int color = Color.argb((int) (alpha * 255), 0, 0, 0);
217        if (mAnimateChange) {
218            startScrimAnimation(scrim, color);
219        } else {
220            scrim.setBackgroundColor(color);
221        }
222    }
223
224    private void startScrimAnimation(final View scrim, int targetColor) {
225        int current = getBackgroundAlpha(scrim);
226        int target = Color.alpha(targetColor);
227        if (current == targetColor) {
228            return;
229        }
230        Object runningAnim = scrim.getTag(TAG_KEY_ANIM);
231        if (runningAnim instanceof ValueAnimator) {
232            ((ValueAnimator) runningAnim).cancel();
233        }
234        ValueAnimator anim = ValueAnimator.ofInt(current, target);
235        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
236            @Override
237            public void onAnimationUpdate(ValueAnimator animation) {
238                int value = (int) animation.getAnimatedValue();
239                scrim.setBackgroundColor(Color.argb(value, 0, 0, 0));
240            }
241        });
242        anim.setInterpolator(mInterpolator);
243        anim.setStartDelay(mAnimationDelay);
244        anim.setDuration(mDurationOverride != -1 ? mDurationOverride : ANIMATION_DURATION);
245        anim.addListener(new AnimatorListenerAdapter() {
246
247            @Override
248            public void onAnimationEnd(Animator animation) {
249                if (mOnAnimationFinished != null) {
250                    mOnAnimationFinished.run();
251                    mOnAnimationFinished = null;
252                }
253                scrim.setTag(TAG_KEY_ANIM, null);
254            }
255        });
256        anim.start();
257        scrim.setTag(TAG_KEY_ANIM, anim);
258        mAnimationStarted = true;
259    }
260
261    private int getBackgroundAlpha(View scrim) {
262        if (scrim.getBackground() instanceof ColorDrawable) {
263            ColorDrawable drawable = (ColorDrawable) scrim.getBackground();
264            return Color.alpha(drawable.getColor());
265        } else {
266            return 0;
267        }
268    }
269
270    @Override
271    public boolean onPreDraw() {
272        mScrimBehind.getViewTreeObserver().removeOnPreDrawListener(this);
273        mUpdatePending = false;
274        updateScrims();
275        mAnimateKeyguardFadingOut = false;
276        mDurationOverride = -1;
277        mAnimationDelay = 0;
278
279        // Make sure that we always call the listener even if we didn't start an animation.
280        if (!mAnimationStarted && mOnAnimationFinished != null) {
281            mOnAnimationFinished.run();
282            mOnAnimationFinished = null;
283        }
284        mAnimationStarted = false;
285        return true;
286    }
287
288    private final Runnable mPulseIn = new Runnable() {
289        @Override
290        public void run() {
291            if (DEBUG) Log.d(TAG, "Pulse in, mDozing=" + mDozing
292                    + " mPulsesRemaining=" + mPulsesRemaining);
293            if (!mDozing || mPulsesRemaining == 0) return;
294            mPulsesRemaining--;
295            mDurationOverride = PULSE_IN_ANIMATION_DURATION;
296            mAnimationDelay = 0;
297            mAnimateChange = true;
298            mOnAnimationFinished = mPulseInFinished;
299            setScrimColor(mScrimInFront, 0);
300        }
301    };
302
303    private final Runnable mPulseInFinished = new Runnable() {
304        @Override
305        public void run() {
306            if (DEBUG) Log.d(TAG, "Pulse in finished, mDozing=" + mDozing);
307            if (!mDozing) return;
308            mScrimInFront.postDelayed(mPulseOut, PULSE_VISIBLE_DURATION);
309        }
310    };
311
312    private final Runnable mPulseOut = new Runnable() {
313        @Override
314        public void run() {
315            if (DEBUG) Log.d(TAG, "Pulse out, mDozing=" + mDozing);
316            if (!mDozing) return;
317            mDurationOverride = PULSE_OUT_ANIMATION_DURATION;
318            mAnimationDelay = 0;
319            mAnimateChange = true;
320            mOnAnimationFinished = mPulseOutFinished;
321            setScrimColor(mScrimInFront, 1);
322        }
323    };
324
325    private final Runnable mPulseOutFinished = new Runnable() {
326        @Override
327        public void run() {
328            if (DEBUG) Log.d(TAG, "Pulse out finished, mPulsesRemaining=" + mPulsesRemaining);
329            if (mPulsesRemaining > 0) {
330                mScrimInFront.postDelayed(mPulseIn, PULSE_INVISIBLE_DURATION);
331            }
332        }
333    };
334}
335