PanelView.java revision b690f0d5023fcf144f2701058d5a6f88d66cc97a
1/*
2 * Copyright (C) 2012 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.ObjectAnimator;
22import android.animation.ValueAnimator;
23import android.content.Context;
24import android.content.res.Configuration;
25import android.content.res.Resources;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.MotionEvent;
29import android.view.ViewConfiguration;
30import android.view.ViewTreeObserver;
31import android.view.animation.AnimationUtils;
32import android.view.animation.Interpolator;
33import android.widget.FrameLayout;
34
35import com.android.systemui.R;
36import com.android.systemui.statusbar.FlingAnimationUtils;
37import com.android.systemui.statusbar.StatusBarState;
38
39import java.io.FileDescriptor;
40import java.io.PrintWriter;
41
42public abstract class PanelView extends FrameLayout {
43    public static final boolean DEBUG = PanelBar.DEBUG;
44    public static final String TAG = PanelView.class.getSimpleName();
45
46    private final void logf(String fmt, Object... args) {
47        Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args));
48    }
49
50    protected PhoneStatusBar mStatusBar;
51    private float mPeekHeight;
52    private float mHintDistance;
53    private int mEdgeTapAreaWidth;
54    private float mInitialOffsetOnTouch;
55    private float mExpandedFraction = 0;
56    protected float mExpandedHeight = 0;
57    private boolean mJustPeeked;
58    private boolean mClosing;
59    protected boolean mTracking;
60    private boolean mTouchSlopExceeded;
61    private int mTrackingPointer;
62    protected int mTouchSlop;
63    protected boolean mHintAnimationRunning;
64    private boolean mOverExpandedBeforeFling;
65    private float mOriginalIndicationY;
66
67    private ValueAnimator mHeightAnimator;
68    private ObjectAnimator mPeekAnimator;
69    private VelocityTrackerInterface mVelocityTracker;
70    private FlingAnimationUtils mFlingAnimationUtils;
71
72    /**
73     * Whether an instant expand request is currently pending and we are just waiting for layout.
74     */
75    private boolean mInstantExpanding;
76
77    PanelBar mBar;
78
79    protected int mMaxPanelHeight = -1;
80    private String mViewName;
81    private float mInitialTouchY;
82    private float mInitialTouchX;
83
84    private Interpolator mLinearOutSlowInInterpolator;
85    private Interpolator mFastOutSlowInInterpolator;
86    private Interpolator mBounceInterpolator;
87    protected KeyguardBottomAreaView mKeyguardBottomArea;
88
89    private boolean mPeekPending;
90    private boolean mCollapseAfterPeek;
91    private boolean mExpanding;
92    private boolean mGestureWaitForTouchSlop;
93    private Runnable mPeekRunnable = new Runnable() {
94        @Override
95        public void run() {
96            mPeekPending = false;
97            runPeekAnimation();
98        }
99    };
100
101    protected void onExpandingFinished() {
102        mBar.onExpandingFinished();
103    }
104
105    protected void onExpandingStarted() {
106    }
107
108    private void notifyExpandingStarted() {
109        if (!mExpanding) {
110            mExpanding = true;
111            onExpandingStarted();
112        }
113    }
114
115    private void notifyExpandingFinished() {
116        if (mExpanding) {
117            mExpanding = false;
118            onExpandingFinished();
119        }
120    }
121
122    private void schedulePeek() {
123        mPeekPending = true;
124        long timeout = ViewConfiguration.getTapTimeout();
125        postOnAnimationDelayed(mPeekRunnable, timeout);
126        notifyBarPanelExpansionChanged();
127    }
128
129    private void runPeekAnimation() {
130        mPeekHeight = getPeekHeight();
131        if (DEBUG) logf("peek to height=%.1f", mPeekHeight);
132        if (mHeightAnimator != null) {
133            return;
134        }
135        mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight)
136                .setDuration(250);
137        mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator);
138        mPeekAnimator.addListener(new AnimatorListenerAdapter() {
139            private boolean mCancelled;
140
141            @Override
142            public void onAnimationCancel(Animator animation) {
143                mCancelled = true;
144            }
145
146            @Override
147            public void onAnimationEnd(Animator animation) {
148                mPeekAnimator = null;
149                if (mCollapseAfterPeek && !mCancelled) {
150                    postOnAnimation(new Runnable() {
151                        @Override
152                        public void run() {
153                            collapse();
154                        }
155                    });
156                }
157                mCollapseAfterPeek = false;
158            }
159        });
160        notifyExpandingStarted();
161        mPeekAnimator.start();
162        mJustPeeked = true;
163    }
164
165    public PanelView(Context context, AttributeSet attrs) {
166        super(context, attrs);
167        mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f);
168        mFastOutSlowInInterpolator =
169                AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
170        mLinearOutSlowInInterpolator =
171                AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
172        mBounceInterpolator = new BounceInterpolator();
173    }
174
175    protected void loadDimens() {
176        final Resources res = getContext().getResources();
177        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
178        mTouchSlop = configuration.getScaledTouchSlop();
179        mHintDistance = res.getDimension(R.dimen.hint_move_distance);
180        mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width);
181    }
182
183    private void trackMovement(MotionEvent event) {
184        // Add movement to velocity tracker using raw screen X and Y coordinates instead
185        // of window coordinates because the window frame may be moving at the same time.
186        float deltaX = event.getRawX() - event.getX();
187        float deltaY = event.getRawY() - event.getY();
188        event.offsetLocation(deltaX, deltaY);
189        if (mVelocityTracker != null) mVelocityTracker.addMovement(event);
190        event.offsetLocation(-deltaX, -deltaY);
191    }
192
193    @Override
194    public boolean onTouchEvent(MotionEvent event) {
195        if (mInstantExpanding) {
196            return false;
197        }
198
199        /*
200         * We capture touch events here and update the expand height here in case according to
201         * the users fingers. This also handles multi-touch.
202         *
203         * If the user just clicks shortly, we give him a quick peek of the shade.
204         *
205         * Flinging is also enabled in order to open or close the shade.
206         */
207
208        int pointerIndex = event.findPointerIndex(mTrackingPointer);
209        if (pointerIndex < 0) {
210            pointerIndex = 0;
211            mTrackingPointer = event.getPointerId(pointerIndex);
212        }
213        final float y = event.getY(pointerIndex);
214        final float x = event.getX(pointerIndex);
215
216        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
217            mGestureWaitForTouchSlop = mExpandedHeight == 0f;
218        }
219        boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop;
220
221        switch (event.getActionMasked()) {
222            case MotionEvent.ACTION_DOWN:
223                mInitialTouchY = y;
224                mInitialTouchX = x;
225                mInitialOffsetOnTouch = mExpandedHeight;
226                mTouchSlopExceeded = false;
227                mJustPeeked = false;
228                if (mVelocityTracker == null) {
229                    initVelocityTracker();
230                }
231                trackMovement(event);
232                if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) ||
233                        mPeekPending || mPeekAnimator != null) {
234                    if (mHeightAnimator != null) {
235                        mHeightAnimator.cancel(); // end any outstanding animations
236                    }
237                    cancelPeek();
238                    mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning)
239                            || mPeekPending || mPeekAnimator != null;
240                    onTrackingStarted();
241                }
242                if (mExpandedHeight == 0) {
243                    schedulePeek();
244                }
245                break;
246
247            case MotionEvent.ACTION_POINTER_UP:
248                final int upPointer = event.getPointerId(event.getActionIndex());
249                if (mTrackingPointer == upPointer) {
250                    // gesture is ongoing, find a new pointer to track
251                    final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
252                    final float newY = event.getY(newIndex);
253                    final float newX = event.getX(newIndex);
254                    mTrackingPointer = event.getPointerId(newIndex);
255                    mInitialOffsetOnTouch = mExpandedHeight;
256                    mInitialTouchY = newY;
257                    mInitialTouchX = newX;
258                }
259                break;
260
261            case MotionEvent.ACTION_MOVE:
262                float h = y - mInitialTouchY;
263
264                // If the panel was collapsed when touching, we only need to check for the
265                // y-component of the gesture, as we have no conflicting horizontal gesture.
266                if (Math.abs(h) > mTouchSlop
267                        && (Math.abs(h) > Math.abs(x - mInitialTouchX)
268                                || mInitialOffsetOnTouch == 0f)) {
269                    mTouchSlopExceeded = true;
270                    if (waitForTouchSlop && !mTracking) {
271                        if (!mJustPeeked) {
272                            mInitialOffsetOnTouch = mExpandedHeight;
273                            mInitialTouchX = x;
274                            mInitialTouchY = y;
275                            h = 0;
276                        }
277                        if (mHeightAnimator != null) {
278                            mHeightAnimator.cancel(); // end any outstanding animations
279                        }
280                        removeCallbacks(mPeekRunnable);
281                        mPeekPending = false;
282                        onTrackingStarted();
283                    }
284                }
285                final float newHeight = Math.max(0, h + mInitialOffsetOnTouch);
286                if (newHeight > mPeekHeight) {
287                    if (mPeekAnimator != null) {
288                        mPeekAnimator.cancel();
289                    }
290                    mJustPeeked = false;
291                }
292                if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) {
293                    setExpandedHeightInternal(newHeight);
294                }
295
296                trackMovement(event);
297                break;
298
299            case MotionEvent.ACTION_UP:
300            case MotionEvent.ACTION_CANCEL:
301                mTrackingPointer = -1;
302                trackMovement(event);
303                if ((mTracking && mTouchSlopExceeded)
304                        || Math.abs(x - mInitialTouchX) > mTouchSlop
305                        || Math.abs(y - mInitialTouchY) > mTouchSlop
306                        || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
307                    float vel = 0f;
308                    float vectorVel = 0f;
309                    if (mVelocityTracker != null) {
310                        mVelocityTracker.computeCurrentVelocity(1000);
311                        vel = mVelocityTracker.getYVelocity();
312                        vectorVel = (float) Math.hypot(
313                                mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
314                    }
315                    boolean expand = flingExpands(vel, vectorVel);
316                    onTrackingStopped(expand);
317                    fling(vel, expand);
318                } else {
319                    boolean expands = onEmptySpaceClick(mInitialTouchX);
320                    onTrackingStopped(expands);
321                }
322
323                if (mVelocityTracker != null) {
324                    mVelocityTracker.recycle();
325                    mVelocityTracker = null;
326                }
327                break;
328        }
329        return !waitForTouchSlop || mTracking;
330    }
331
332    protected abstract boolean hasConflictingGestures();
333
334    protected void onTrackingStopped(boolean expand) {
335        mTracking = false;
336        mBar.onTrackingStopped(PanelView.this, expand);
337    }
338
339    protected void onTrackingStarted() {
340        mTracking = true;
341        mCollapseAfterPeek = false;
342        mBar.onTrackingStarted(PanelView.this);
343        notifyExpandingStarted();
344    }
345
346    @Override
347    public boolean onInterceptTouchEvent(MotionEvent event) {
348        if (mInstantExpanding) {
349            return false;
350        }
351
352        /*
353         * If the user drags anywhere inside the panel we intercept it if he moves his finger
354         * upwards. This allows closing the shade from anywhere inside the panel.
355         *
356         * We only do this if the current content is scrolled to the bottom,
357         * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture
358         * possible.
359         */
360        int pointerIndex = event.findPointerIndex(mTrackingPointer);
361        if (pointerIndex < 0) {
362            pointerIndex = 0;
363            mTrackingPointer = event.getPointerId(pointerIndex);
364        }
365        final float x = event.getX(pointerIndex);
366        final float y = event.getY(pointerIndex);
367        boolean scrolledToBottom = isScrolledToBottom();
368
369        switch (event.getActionMasked()) {
370            case MotionEvent.ACTION_DOWN:
371                mStatusBar.userActivity();
372                if (mHeightAnimator != null && !mHintAnimationRunning ||
373                        mPeekPending || mPeekAnimator != null) {
374                    if (mHeightAnimator != null) {
375                        mHeightAnimator.cancel(); // end any outstanding animations
376                    }
377                    cancelPeek();
378                    mTouchSlopExceeded = true;
379                    return true;
380                }
381                mInitialTouchY = y;
382                mInitialTouchX = x;
383                mTouchSlopExceeded = false;
384                mJustPeeked = false;
385                initVelocityTracker();
386                trackMovement(event);
387                break;
388            case MotionEvent.ACTION_POINTER_UP:
389                final int upPointer = event.getPointerId(event.getActionIndex());
390                if (mTrackingPointer == upPointer) {
391                    // gesture is ongoing, find a new pointer to track
392                    final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
393                    mTrackingPointer = event.getPointerId(newIndex);
394                    mInitialTouchX = event.getX(newIndex);
395                    mInitialTouchY = event.getY(newIndex);
396                }
397                break;
398
399            case MotionEvent.ACTION_MOVE:
400                final float h = y - mInitialTouchY;
401                trackMovement(event);
402                if (scrolledToBottom) {
403                    if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) {
404                        if (mHeightAnimator != null) {
405                            mHeightAnimator.cancel();
406                        }
407                        mInitialOffsetOnTouch = mExpandedHeight;
408                        mInitialTouchY = y;
409                        mInitialTouchX = x;
410                        mTracking = true;
411                        mTouchSlopExceeded = true;
412                        onTrackingStarted();
413                        return true;
414                    }
415                }
416                break;
417        }
418        return false;
419    }
420
421    private void initVelocityTracker() {
422        if (mVelocityTracker != null) {
423            mVelocityTracker.recycle();
424        }
425        mVelocityTracker = VelocityTrackerFactory.obtain(getContext());
426    }
427
428    protected boolean isScrolledToBottom() {
429        return true;
430    }
431
432    protected float getContentHeight() {
433        return mExpandedHeight;
434    }
435
436    @Override
437    protected void onFinishInflate() {
438        super.onFinishInflate();
439        loadDimens();
440    }
441
442    @Override
443    protected void onConfigurationChanged(Configuration newConfig) {
444        super.onConfigurationChanged(newConfig);
445        loadDimens();
446        mMaxPanelHeight = -1;
447    }
448
449    /**
450     * @param vel the current vertical velocity of the motion
451     * @param vectorVel the length of the vectorial velocity
452     * @return whether a fling should expands the panel; contracts otherwise
453     */
454    protected boolean flingExpands(float vel, float vectorVel) {
455        if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
456            return getExpandedFraction() > 0.5f;
457        } else {
458            return vel > 0;
459        }
460    }
461
462    protected void fling(float vel, boolean expand) {
463        cancelPeek();
464        float target = expand ? getMaxPanelHeight() : 0.0f;
465        if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
466            notifyExpandingFinished();
467            return;
468        }
469        mOverExpandedBeforeFling = getOverExpansionAmount() > 0f;
470        ValueAnimator animator = createHeightAnimator(target);
471        if (expand) {
472            mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
473        } else {
474            mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel,
475                    getHeight());
476
477            // Make it shorter if we run a canned animation
478            if (vel == 0) {
479                animator.setDuration((long)
480                        (animator.getDuration() * getCannedFlingDurationFactor()));
481            }
482        }
483        animator.addListener(new AnimatorListenerAdapter() {
484            private boolean mCancelled;
485
486            @Override
487            public void onAnimationCancel(Animator animation) {
488                mCancelled = true;
489            }
490
491            @Override
492            public void onAnimationEnd(Animator animation) {
493                mHeightAnimator = null;
494                if (!mCancelled) {
495                    notifyExpandingFinished();
496                }
497            }
498        });
499        mHeightAnimator = animator;
500        animator.start();
501    }
502
503    @Override
504    protected void onAttachedToWindow() {
505        super.onAttachedToWindow();
506        mViewName = getResources().getResourceName(getId());
507    }
508
509    public String getName() {
510        return mViewName;
511    }
512
513    @Override
514    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
515        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
516
517        if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)",
518                widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight());
519
520        // Did one of our children change size?
521        int newHeight = getMeasuredHeight();
522        if (newHeight > mMaxPanelHeight) {
523            // we only adapt the max height if it's bigger
524            mMaxPanelHeight = newHeight;
525            // If the user isn't actively poking us, let's rubberband to the content
526            if (!mTracking && mHeightAnimator == null
527                    && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight
528                    && mMaxPanelHeight > 0 && mPeekAnimator == null) {
529                mExpandedHeight = mMaxPanelHeight;
530            }
531        }
532    }
533
534    public void setExpandedHeight(float height) {
535        if (DEBUG) logf("setExpandedHeight(%.1f)", height);
536        setExpandedHeightInternal(height + getOverExpansionPixels());
537    }
538
539    @Override
540    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
541        if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom,
542                (int)mExpandedHeight, mMaxPanelHeight);
543        super.onLayout(changed, left, top, right, bottom);
544        requestPanelHeightUpdate();
545    }
546
547    protected void requestPanelHeightUpdate() {
548        float currentMaxPanelHeight = getMaxPanelHeight();
549
550        // If the user isn't actively poking us, let's update the height
551        if ((!mTracking || isTrackingBlocked())
552                && mHeightAnimator == null
553                && mExpandedHeight > 0
554                && currentMaxPanelHeight != mExpandedHeight
555                && !mPeekPending
556                && mPeekAnimator == null) {
557            setExpandedHeight(currentMaxPanelHeight);
558        }
559    }
560
561    public void setExpandedHeightInternal(float h) {
562        float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount();
563        if (mHeightAnimator == null) {
564            float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion);
565            if (getOverExpansionPixels() != overExpansionPixels && mTracking) {
566                setOverExpansion(overExpansionPixels, true /* isPixels */);
567            }
568            mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount();
569        } else {
570            mExpandedHeight = h;
571            if (mOverExpandedBeforeFling) {
572                setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */);
573            }
574        }
575
576        mExpandedHeight = Math.max(0, mExpandedHeight);
577        onHeightUpdated(mExpandedHeight);
578        mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0
579                ? 0
580                : mExpandedHeight / fhWithoutOverExpansion);
581        notifyBarPanelExpansionChanged();
582    }
583
584    /**
585     * @return true if the panel tracking should be temporarily blocked; this is used when a
586     *         conflicting gesture (opening QS) is happening
587     */
588    protected abstract boolean isTrackingBlocked();
589
590    protected abstract void setOverExpansion(float overExpansion, boolean isPixels);
591
592    protected abstract void onHeightUpdated(float expandedHeight);
593
594    protected abstract float getOverExpansionAmount();
595
596    protected abstract float getOverExpansionPixels();
597
598    /**
599     * This returns the maximum height of the panel. Children should override this if their
600     * desired height is not the full height.
601     *
602     * @return the default implementation simply returns the maximum height.
603     */
604    protected int getMaxPanelHeight() {
605        mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight());
606        return mMaxPanelHeight;
607    }
608
609    public void setExpandedFraction(float frac) {
610        setExpandedHeight(getMaxPanelHeight() * frac);
611    }
612
613    public float getExpandedHeight() {
614        return mExpandedHeight;
615    }
616
617    public float getExpandedFraction() {
618        return mExpandedFraction;
619    }
620
621    public boolean isFullyExpanded() {
622        return mExpandedHeight >= getMaxPanelHeight();
623    }
624
625    public boolean isFullyCollapsed() {
626        return mExpandedHeight <= 0;
627    }
628
629    public boolean isCollapsing() {
630        return mClosing;
631    }
632
633    public boolean isTracking() {
634        return mTracking;
635    }
636
637    public void setBar(PanelBar panelBar) {
638        mBar = panelBar;
639    }
640
641    public void collapse() {
642        // TODO: abort animation or ongoing touch
643        if (DEBUG) logf("collapse: " + this);
644        if (mPeekPending || mPeekAnimator != null) {
645            mCollapseAfterPeek = true;
646            if (mPeekPending) {
647
648                // We know that the whole gesture is just a peek triggered by a simple click, so
649                // better start it now.
650                removeCallbacks(mPeekRunnable);
651                mPeekRunnable.run();
652            }
653        } else if (!isFullyCollapsed()) {
654            if (mHeightAnimator != null) {
655                mHeightAnimator.cancel();
656            }
657            mClosing = true;
658            notifyExpandingStarted();
659            fling(0, false /* expand */);
660        }
661    }
662
663    public void expand() {
664        if (DEBUG) logf("expand: " + this);
665        if (isFullyCollapsed()) {
666            mBar.startOpeningPanel(this);
667            notifyExpandingStarted();
668            fling(0, true /* expand */);
669        } else if (DEBUG) {
670            if (DEBUG) logf("skipping expansion: is expanded");
671        }
672    }
673
674    public void cancelPeek() {
675        if (mPeekAnimator != null) {
676            mPeekAnimator.cancel();
677        }
678        removeCallbacks(mPeekRunnable);
679        mPeekPending = false;
680
681        // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also
682        // notify mBar that we might have closed ourselves.
683        notifyBarPanelExpansionChanged();
684    }
685
686    public void instantExpand() {
687        mInstantExpanding = true;
688        abortAnimations();
689        if (mTracking) {
690            onTrackingStopped(true /* expands */); // The panel is expanded after this call.
691            notifyExpandingFinished();
692        }
693        setVisibility(VISIBLE);
694
695        // Wait for window manager to pickup the change, so we know the maximum height of the panel
696        // then.
697        getViewTreeObserver().addOnGlobalLayoutListener(
698                new ViewTreeObserver.OnGlobalLayoutListener() {
699                    @Override
700                    public void onGlobalLayout() {
701                        if (mStatusBar.getStatusBarWindow().getHeight()
702                                != mStatusBar.getStatusBarHeight()) {
703                            getViewTreeObserver().removeOnGlobalLayoutListener(this);
704                            setExpandedFraction(1f);
705                            mInstantExpanding = false;
706                        }
707                    }
708                });
709
710        // Make sure a layout really happens.
711        requestLayout();
712    }
713
714    private void abortAnimations() {
715        cancelPeek();
716        if (mHeightAnimator != null) {
717            mHeightAnimator.cancel();
718        }
719    }
720
721    protected void startUnlockHintAnimation() {
722
723        // We don't need to hint the user if an animation is already running or the user is changing
724        // the expansion.
725        if (mHeightAnimator != null || mTracking) {
726            return;
727        }
728        cancelPeek();
729        notifyExpandingStarted();
730        startUnlockHintAnimationPhase1(new Runnable() {
731            @Override
732            public void run() {
733                notifyExpandingFinished();
734                mStatusBar.onHintFinished();
735                mHintAnimationRunning = false;
736            }
737        });
738        mStatusBar.onUnlockHintStarted();
739        mHintAnimationRunning = true;
740    }
741
742    /**
743     * Phase 1: Move everything upwards.
744     */
745    private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) {
746        float target = Math.max(0, getMaxPanelHeight() - mHintDistance);
747        ValueAnimator animator = createHeightAnimator(target);
748        animator.setDuration(250);
749        animator.setInterpolator(mFastOutSlowInInterpolator);
750        animator.addListener(new AnimatorListenerAdapter() {
751            private boolean mCancelled;
752
753            @Override
754            public void onAnimationCancel(Animator animation) {
755                mCancelled = true;
756            }
757
758            @Override
759            public void onAnimationEnd(Animator animation) {
760                if (mCancelled) {
761                    mHeightAnimator = null;
762                    onAnimationFinished.run();
763                } else {
764                    startUnlockHintAnimationPhase2(onAnimationFinished);
765                }
766            }
767        });
768        animator.start();
769        mHeightAnimator = animator;
770        mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY();
771        mKeyguardBottomArea.getIndicationView().animate()
772                .y(mOriginalIndicationY - mHintDistance)
773                .setDuration(250)
774                .setInterpolator(mFastOutSlowInInterpolator)
775                .withEndAction(new Runnable() {
776                    @Override
777                    public void run() {
778                        mKeyguardBottomArea.getIndicationView().animate()
779                                .y(mOriginalIndicationY)
780                                .setDuration(450)
781                                .setInterpolator(mBounceInterpolator)
782                                .start();
783                    }
784                })
785                .start();
786    }
787
788    /**
789     * Phase 2: Bounce down.
790     */
791    private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) {
792        ValueAnimator animator = createHeightAnimator(getMaxPanelHeight());
793        animator.setDuration(450);
794        animator.setInterpolator(mBounceInterpolator);
795        animator.addListener(new AnimatorListenerAdapter() {
796            @Override
797            public void onAnimationEnd(Animator animation) {
798                mHeightAnimator = null;
799                onAnimationFinished.run();
800            }
801        });
802        animator.start();
803        mHeightAnimator = animator;
804    }
805
806    private ValueAnimator createHeightAnimator(float targetHeight) {
807        ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight);
808        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
809            @Override
810            public void onAnimationUpdate(ValueAnimator animation) {
811                setExpandedHeightInternal((Float) animation.getAnimatedValue());
812            }
813        });
814        return animator;
815    }
816
817    private void notifyBarPanelExpansionChanged() {
818        mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending
819                || mPeekAnimator != null);
820    }
821
822    /**
823     * Gets called when the user performs a click anywhere in the empty area of the panel.
824     *
825     * @return whether the panel will be expanded after the action performed by this method
826     */
827    private boolean onEmptySpaceClick(float x) {
828        if (mHintAnimationRunning) {
829            return true;
830        }
831        if (x < mEdgeTapAreaWidth
832                && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
833            onEdgeClicked(false /* right */);
834            return true;
835        } else if (x > getWidth() - mEdgeTapAreaWidth
836                && mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
837            onEdgeClicked(true /* right */);
838            return true;
839        } else {
840            return onMiddleClicked();
841        }
842    }
843
844    private boolean onMiddleClicked() {
845        switch (mStatusBar.getBarState()) {
846            case StatusBarState.KEYGUARD:
847                startUnlockHintAnimation();
848                return true;
849            case StatusBarState.SHADE_LOCKED:
850                mStatusBar.goToKeyguard();
851                return true;
852            case StatusBarState.SHADE:
853                collapse();
854                return false;
855            default:
856                return true;
857        }
858    }
859
860    protected abstract void onEdgeClicked(boolean right);
861
862    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
863        pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s"
864                + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s"
865                + "]",
866                this.getClass().getSimpleName(),
867                getExpandedHeight(),
868                getMaxPanelHeight(),
869                mClosing?"T":"f",
870                mTracking?"T":"f",
871                mJustPeeked?"T":"f",
872                mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""),
873                mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"")
874        ));
875    }
876
877    public abstract void resetViews();
878
879    protected abstract float getPeekHeight();
880
881    protected abstract float getCannedFlingDurationFactor();
882}
883