PagedView.java revision 869306140d3bb8782040ed65d3ae0ba498c2de6c
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.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.LayoutTransition;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
24import android.annotation.SuppressLint;
25import android.annotation.TargetApi;
26import android.content.Context;
27import android.content.res.TypedArray;
28import android.graphics.Canvas;
29import android.graphics.Matrix;
30import android.graphics.Rect;
31import android.os.Build;
32import android.os.Bundle;
33import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
36import android.util.DisplayMetrics;
37import android.util.Log;
38import android.view.InputDevice;
39import android.view.KeyEvent;
40import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
46import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityManager;
48import android.view.accessibility.AccessibilityNodeInfo;
49import android.view.animation.Interpolator;
50import com.android.launcher3.util.LauncherEdgeEffect;
51import com.android.launcher3.util.Thunk;
52import java.util.ArrayList;
53
54/**
55 * An abstraction of the original Workspace which supports browsing through a
56 * sequential list of "pages"
57 */
58public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
59    private static final String TAG = "PagedView";
60    private static final boolean DEBUG = false;
61    protected static final int INVALID_PAGE = -1;
62
63    // the min drag distance for a fling to register, to prevent random page shifts
64    private static final int MIN_LENGTH_FOR_FLING = 25;
65
66    public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
67    protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
68
69    private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
70    // The page is moved more than halfway, automatically move to the next page on touch up.
71    private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
72
73    private static final float MAX_SCROLL_PROGRESS = 1.0f;
74
75    // The following constants need to be scaled based on density. The scaled versions will be
76    // assigned to the corresponding member variables below.
77    private static final int FLING_THRESHOLD_VELOCITY = 500;
78    private static final int MIN_SNAP_VELOCITY = 1500;
79    private static final int MIN_FLING_VELOCITY = 250;
80
81    public static final int INVALID_RESTORE_PAGE = -1001;
82
83    private boolean mFreeScroll = false;
84    private int mFreeScrollMinScrollX = -1;
85    private int mFreeScrollMaxScrollX = -1;
86
87    protected int mFlingThresholdVelocity;
88    protected int mMinFlingVelocity;
89    protected int mMinSnapVelocity;
90
91    protected boolean mFirstLayout = true;
92    private int mNormalChildHeight;
93
94    protected int mCurrentPage;
95    protected int mRestorePage = INVALID_RESTORE_PAGE;
96    private int mChildCountOnLastLayout;
97
98    protected int mNextPage = INVALID_PAGE;
99    private int mMaxScrollX;
100    protected LauncherScroller mScroller;
101    private Interpolator mDefaultInterpolator;
102    private VelocityTracker mVelocityTracker;
103    @Thunk int mPageSpacing = 0;
104
105    private float mParentDownMotionX;
106    private float mParentDownMotionY;
107    private float mDownMotionX;
108    private float mDownMotionY;
109    private float mDownScrollX;
110    private float mDragViewBaselineLeft;
111    private float mLastMotionX;
112    private float mLastMotionXRemainder;
113    private float mLastMotionY;
114    private float mTotalMotionX;
115    private int mLastScreenCenter = -1;
116
117    private boolean mCancelTap;
118
119    private int[] mPageScrolls;
120
121    protected final static int TOUCH_STATE_REST = 0;
122    protected final static int TOUCH_STATE_SCROLLING = 1;
123    protected final static int TOUCH_STATE_PREV_PAGE = 2;
124    protected final static int TOUCH_STATE_NEXT_PAGE = 3;
125    protected final static int TOUCH_STATE_REORDERING = 4;
126
127    protected int mTouchState = TOUCH_STATE_REST;
128    private boolean mForceScreenScrolled = false;
129
130    protected OnLongClickListener mLongClickListener;
131
132    protected int mTouchSlop;
133    private int mMaximumVelocity;
134    protected int mCellCountX = 0;
135    protected int mCellCountY = 0;
136    protected boolean mAllowOverScroll = true;
137    protected int[] mTempVisiblePagesRange = new int[2];
138
139    protected static final int INVALID_POINTER = -1;
140
141    protected int mActivePointerId = INVALID_POINTER;
142
143    private PageSwitchListener mPageSwitchListener;
144
145    // If true, modify alpha of neighboring pages as user scrolls left/right
146    protected boolean mFadeInAdjacentScreens = false;
147
148    protected boolean mIsPageMoving = false;
149
150    private boolean mWasInOverscroll = false;
151
152    // Page Indicator
153    @Thunk int mPageIndicatorViewId;
154    @Thunk PageIndicator mPageIndicator;
155    // The viewport whether the pages are to be contained (the actual view may be larger than the
156    // viewport)
157    private Rect mViewport = new Rect();
158
159    // Reordering
160    // We use the min scale to determine how much to expand the actually PagedView measured
161    // dimensions such that when we are zoomed out, the view is not clipped
162    private static int REORDERING_DROP_REPOSITION_DURATION = 200;
163    @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
164    private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
165
166    private float mMinScale = 1f;
167    private boolean mUseMinScale = false;
168    @Thunk View mDragView;
169    private Runnable mSidePageHoverRunnable;
170    @Thunk int mSidePageHoverIndex = -1;
171    // This variable's scope is only for the duration of startReordering() and endReordering()
172    private boolean mReorderingStarted = false;
173    // This variable's scope is for the duration of startReordering() and after the zoomIn()
174    // animation after endReordering()
175    private boolean mIsReordering;
176    // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
177    private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
178    private int mPostReorderingPreZoomInRemainingAnimationCount;
179    private Runnable mPostReorderingPreZoomInRunnable;
180
181    // Convenience/caching
182    private static final Matrix sTmpInvMatrix = new Matrix();
183    private static final float[] sTmpPoint = new float[2];
184    private static final int[] sTmpIntPoint = new int[2];
185    private static final Rect sTmpRect = new Rect();
186
187    protected final Rect mInsets = new Rect();
188    protected final boolean mIsRtl;
189
190    // Edge effect
191    private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
192    private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
193
194    public interface PageSwitchListener {
195        void onPageSwitch(View newPage, int newPageIndex);
196    }
197
198    public PagedView(Context context) {
199        this(context, null);
200    }
201
202    public PagedView(Context context, AttributeSet attrs) {
203        this(context, attrs, 0);
204    }
205
206    public PagedView(Context context, AttributeSet attrs, int defStyle) {
207        super(context, attrs, defStyle);
208
209        TypedArray a = context.obtainStyledAttributes(attrs,
210                R.styleable.PagedView, defStyle, 0);
211        mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
212        a.recycle();
213
214        setHapticFeedbackEnabled(false);
215        mIsRtl = Utilities.isRtl(getResources());
216        init();
217    }
218
219    /**
220     * Initializes various states for this workspace.
221     */
222    protected void init() {
223        mScroller = new LauncherScroller(getContext());
224        setDefaultInterpolator(new ScrollInterpolator());
225        mCurrentPage = 0;
226
227        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
228        mTouchSlop = configuration.getScaledPagingTouchSlop();
229        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
230
231        float density = getResources().getDisplayMetrics().density;
232        mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
233        mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
234        mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
235        setOnHierarchyChangeListener(this);
236        setWillNotDraw(false);
237    }
238
239    protected void setEdgeGlowColor(int color) {
240        mEdgeGlowLeft.setColor(color);
241        mEdgeGlowRight.setColor(color);
242    }
243
244    protected void setDefaultInterpolator(Interpolator interpolator) {
245        mDefaultInterpolator = interpolator;
246        mScroller.setInterpolator(mDefaultInterpolator);
247    }
248
249    protected void onAttachedToWindow() {
250        super.onAttachedToWindow();
251
252        // Hook up the page indicator
253        ViewGroup parent = (ViewGroup) getParent();
254        ViewGroup grandParent = (ViewGroup) parent.getParent();
255        if (mPageIndicator == null && mPageIndicatorViewId > -1) {
256            mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
257            mPageIndicator.removeAllMarkers(true);
258
259            ArrayList<PageIndicator.PageMarkerResources> markers =
260                    new ArrayList<PageIndicator.PageMarkerResources>();
261            for (int i = 0; i < getChildCount(); ++i) {
262                markers.add(getPageIndicatorMarker(i));
263            }
264
265            mPageIndicator.addMarkers(markers, true);
266
267            OnClickListener listener = getPageIndicatorClickListener();
268            if (listener != null) {
269                mPageIndicator.setOnClickListener(listener);
270            }
271            mPageIndicator.setContentDescription(getPageIndicatorDescription());
272        }
273    }
274
275    protected String getPageIndicatorDescription() {
276        return getCurrentPageDescription();
277    }
278
279    protected OnClickListener getPageIndicatorClickListener() {
280        return null;
281    }
282
283    @Override
284    protected void onDetachedFromWindow() {
285        super.onDetachedFromWindow();
286        // Unhook the page indicator
287        mPageIndicator = null;
288    }
289
290    // Convenience methods to map points from self to parent and vice versa
291    private float[] mapPointFromViewToParent(View v, float x, float y) {
292        sTmpPoint[0] = x;
293        sTmpPoint[1] = y;
294        v.getMatrix().mapPoints(sTmpPoint);
295        sTmpPoint[0] += v.getLeft();
296        sTmpPoint[1] += v.getTop();
297        return sTmpPoint;
298    }
299    private float[] mapPointFromParentToView(View v, float x, float y) {
300        sTmpPoint[0] = x - v.getLeft();
301        sTmpPoint[1] = y - v.getTop();
302        v.getMatrix().invert(sTmpInvMatrix);
303        sTmpInvMatrix.mapPoints(sTmpPoint);
304        return sTmpPoint;
305    }
306
307    private void updateDragViewTranslationDuringDrag() {
308        if (mDragView != null) {
309            float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
310                    (mDragViewBaselineLeft - mDragView.getLeft());
311            float y = mLastMotionY - mDownMotionY;
312            mDragView.setTranslationX(x);
313            mDragView.setTranslationY(y);
314
315            if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
316                    + x + ", " + y);
317        }
318    }
319
320    public void setMinScale(float f) {
321        mMinScale = f;
322        mUseMinScale = true;
323        requestLayout();
324    }
325
326    @Override
327    public void setScaleX(float scaleX) {
328        super.setScaleX(scaleX);
329        if (isReordering(true)) {
330            float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
331            mLastMotionX = p[0];
332            mLastMotionY = p[1];
333            updateDragViewTranslationDuringDrag();
334        }
335    }
336
337    // Convenience methods to get the actual width/height of the PagedView (since it is measured
338    // to be larger to account for the minimum possible scale)
339    int getViewportWidth() {
340        return mViewport.width();
341    }
342    int getViewportHeight() {
343        return mViewport.height();
344    }
345
346    // Convenience methods to get the offset ASSUMING that we are centering the pages in the
347    // PagedView both horizontally and vertically
348    int getViewportOffsetX() {
349        return (getMeasuredWidth() - getViewportWidth()) / 2;
350    }
351
352    int getViewportOffsetY() {
353        return (getMeasuredHeight() - getViewportHeight()) / 2;
354    }
355
356    PageIndicator getPageIndicator() {
357        return mPageIndicator;
358    }
359    protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
360        return new PageIndicator.PageMarkerResources();
361    }
362
363    /**
364     * Add a page change listener which will be called when a page is _finished_ listening.
365     *
366     */
367    public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
368        mPageSwitchListener = pageSwitchListener;
369        if (mPageSwitchListener != null) {
370            mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
371        }
372    }
373
374    /**
375     * Returns the index of the currently displayed page. When in free scroll mode, this is the page
376     * that the user was on before entering free scroll mode (e.g. the home screen page they
377     * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
378     * to get the page the user is currently scrolling over.
379     */
380    public int getCurrentPage() {
381        return mCurrentPage;
382    }
383
384    /**
385     * Returns the index of page to be shown immediately afterwards.
386     */
387    public int getNextPage() {
388        return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
389    }
390
391    int getPageCount() {
392        return getChildCount();
393    }
394
395    public View getPageAt(int index) {
396        return getChildAt(index);
397    }
398
399    protected int indexToPage(int index) {
400        return index;
401    }
402
403    /**
404     * Updates the scroll of the current page immediately to its final scroll position.  We use this
405     * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
406     * the previous tab page.
407     */
408    protected void updateCurrentPageScroll() {
409        // If the current page is invalid, just reset the scroll position to zero
410        int newX = 0;
411        if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
412            newX = getScrollForPage(mCurrentPage);
413        }
414        scrollTo(newX, 0);
415        mScroller.setFinalX(newX);
416        forceFinishScroller();
417    }
418
419    private void abortScrollerAnimation(boolean resetNextPage) {
420        mScroller.abortAnimation();
421        // We need to clean up the next page here to avoid computeScrollHelper from
422        // updating current page on the pass.
423        if (resetNextPage) {
424            mNextPage = INVALID_PAGE;
425        }
426    }
427
428    private void forceFinishScroller() {
429        mScroller.forceFinished(true);
430        // We need to clean up the next page here to avoid computeScrollHelper from
431        // updating current page on the pass.
432        mNextPage = INVALID_PAGE;
433    }
434
435    private int validateNewPage(int newPage) {
436        int validatedPage = newPage;
437        // When in free scroll mode, we need to clamp to the free scroll page range.
438        if (mFreeScroll) {
439            getFreeScrollPageRange(mTempVisiblePagesRange);
440            validatedPage = Math.max(mTempVisiblePagesRange[0],
441                    Math.min(newPage, mTempVisiblePagesRange[1]));
442        }
443        // Ensure that it is clamped by the actual set of children in all cases
444        validatedPage = Utilities.boundInRange(validatedPage, 0, getPageCount() - 1);
445        return validatedPage;
446    }
447
448    /**
449     * Sets the current page.
450     */
451    public void setCurrentPage(int currentPage) {
452        if (!mScroller.isFinished()) {
453            abortScrollerAnimation(true);
454        }
455        // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
456        // the default
457        if (getChildCount() == 0) {
458            return;
459        }
460        mForceScreenScrolled = true;
461        mCurrentPage = validateNewPage(currentPage);
462        updateCurrentPageScroll();
463        notifyPageSwitchListener();
464        invalidate();
465    }
466
467    /**
468     * The restore page will be set in place of the current page at the next (likely first)
469     * layout.
470     */
471    void setRestorePage(int restorePage) {
472        mRestorePage = restorePage;
473    }
474    int getRestorePage() {
475        return mRestorePage;
476    }
477
478    /**
479     * Should be called whenever the page changes. In the case of a scroll, we wait until the page
480     * has settled.
481     */
482    protected void notifyPageSwitchListener() {
483        if (mPageSwitchListener != null) {
484            mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
485        }
486
487        updatePageIndicator();
488    }
489
490    private void updatePageIndicator() {
491        // Update the page indicator (when we aren't reordering)
492        if (mPageIndicator != null) {
493            mPageIndicator.setContentDescription(getPageIndicatorDescription());
494            if (!isReordering(false)) {
495                mPageIndicator.setActiveMarker(getNextPage());
496            }
497        }
498    }
499    protected void pageBeginMoving() {
500        if (!mIsPageMoving) {
501            mIsPageMoving = true;
502            onPageBeginMoving();
503        }
504    }
505
506    protected void pageEndMoving() {
507        if (mIsPageMoving) {
508            mIsPageMoving = false;
509            onPageEndMoving();
510        }
511    }
512
513    protected boolean isPageMoving() {
514        return mIsPageMoving;
515    }
516
517    // a method that subclasses can override to add behavior
518    protected void onPageBeginMoving() {
519    }
520
521    // a method that subclasses can override to add behavior
522    protected void onPageEndMoving() {
523        mWasInOverscroll = false;
524    }
525
526    /**
527     * Registers the specified listener on each page contained in this workspace.
528     *
529     * @param l The listener used to respond to long clicks.
530     */
531    @Override
532    public void setOnLongClickListener(OnLongClickListener l) {
533        mLongClickListener = l;
534        final int count = getPageCount();
535        for (int i = 0; i < count; i++) {
536            getPageAt(i).setOnLongClickListener(l);
537        }
538        super.setOnLongClickListener(l);
539    }
540
541    @Override
542    public void scrollBy(int x, int y) {
543        scrollTo(getScrollX() + x, getScrollY() + y);
544    }
545
546    @Override
547    public void scrollTo(int x, int y) {
548        // In free scroll mode, we clamp the scrollX
549        if (mFreeScroll) {
550            // If the scroller is trying to move to a location beyond the maximum allowed
551            // in the free scroll mode, we make sure to end the scroll operation.
552            if (!mScroller.isFinished() &&
553                    (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
554                forceFinishScroller();
555            }
556
557            x = Math.min(x, mFreeScrollMaxScrollX);
558            x = Math.max(x, mFreeScrollMinScrollX);
559        }
560
561        boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
562        boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
563        if (isXBeforeFirstPage) {
564            super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
565            if (mAllowOverScroll) {
566                mWasInOverscroll = true;
567                if (mIsRtl) {
568                    overScroll(x - mMaxScrollX);
569                } else {
570                    overScroll(x);
571                }
572            }
573        } else if (isXAfterLastPage) {
574            super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
575            if (mAllowOverScroll) {
576                mWasInOverscroll = true;
577                if (mIsRtl) {
578                    overScroll(x);
579                } else {
580                    overScroll(x - mMaxScrollX);
581                }
582            }
583        } else {
584            if (mWasInOverscroll) {
585                overScroll(0);
586                mWasInOverscroll = false;
587            }
588            super.scrollTo(x, y);
589        }
590
591        // Update the last motion events when scrolling
592        if (isReordering(true)) {
593            float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
594            mLastMotionX = p[0];
595            mLastMotionY = p[1];
596            updateDragViewTranslationDuringDrag();
597        }
598    }
599
600    private void sendScrollAccessibilityEvent() {
601        AccessibilityManager am =
602                (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
603        if (am.isEnabled()) {
604            if (mCurrentPage != getNextPage()) {
605                AccessibilityEvent ev =
606                        AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
607                ev.setScrollable(true);
608                ev.setScrollX(getScrollX());
609                ev.setScrollY(getScrollY());
610                ev.setMaxScrollX(mMaxScrollX);
611                ev.setMaxScrollY(0);
612
613                sendAccessibilityEventUnchecked(ev);
614            }
615        }
616    }
617
618    // we moved this functionality to a helper function so SmoothPagedView can reuse it
619    protected boolean computeScrollHelper() {
620        if (mScroller.computeScrollOffset()) {
621            // Don't bother scrolling if the page does not need to be moved
622            if (getScrollX() != mScroller.getCurrX()
623                || getScrollY() != mScroller.getCurrY()) {
624                float scaleX = mFreeScroll ? getScaleX() : 1f;
625                int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
626                scrollTo(scrollX, mScroller.getCurrY());
627            }
628            invalidate();
629            return true;
630        } else if (mNextPage != INVALID_PAGE) {
631            sendScrollAccessibilityEvent();
632
633            mCurrentPage = validateNewPage(mNextPage);
634            mNextPage = INVALID_PAGE;
635            notifyPageSwitchListener();
636
637            // We don't want to trigger a page end moving unless the page has settled
638            // and the user has stopped scrolling
639            if (mTouchState == TOUCH_STATE_REST) {
640                pageEndMoving();
641            }
642
643            onPostReorderingAnimationCompleted();
644            AccessibilityManager am = (AccessibilityManager)
645                    getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
646            if (am.isEnabled()) {
647                // Notify the user when the page changes
648                announceForAccessibility(getCurrentPageDescription());
649            }
650            return true;
651        }
652        return false;
653    }
654
655    @Override
656    public void computeScroll() {
657        computeScrollHelper();
658    }
659
660    public static class LayoutParams extends ViewGroup.LayoutParams {
661        public boolean isFullScreenPage = false;
662
663        /**
664         * {@inheritDoc}
665         */
666        public LayoutParams(int width, int height) {
667            super(width, height);
668        }
669
670        public LayoutParams(Context context, AttributeSet attrs) {
671            super(context, attrs);
672        }
673
674        public LayoutParams(ViewGroup.LayoutParams source) {
675            super(source);
676        }
677    }
678
679    @Override
680    public LayoutParams generateLayoutParams(AttributeSet attrs) {
681        return new LayoutParams(getContext(), attrs);
682    }
683
684    @Override
685    protected LayoutParams generateDefaultLayoutParams() {
686        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
687    }
688
689    @Override
690    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
691        return new LayoutParams(p);
692    }
693
694    @Override
695    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
696        return p instanceof LayoutParams;
697    }
698
699    public void addFullScreenPage(View page) {
700        LayoutParams lp = generateDefaultLayoutParams();
701        lp.isFullScreenPage = true;
702        super.addView(page, 0, lp);
703    }
704
705    public int getNormalChildHeight() {
706        return mNormalChildHeight;
707    }
708
709    @Override
710    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
711        if (getChildCount() == 0) {
712            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
713            return;
714        }
715
716        // We measure the dimensions of the PagedView to be larger than the pages so that when we
717        // zoom out (and scale down), the view is still contained in the parent
718        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
719        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
720        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
721        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
722        // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
723        // viewport, we can be at most one and a half screens offset once we scale down
724        DisplayMetrics dm = getResources().getDisplayMetrics();
725        int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
726                dm.heightPixels + mInsets.top + mInsets.bottom);
727
728        int parentWidthSize = (int) (2f * maxSize);
729        int parentHeightSize = (int) (2f * maxSize);
730        int scaledWidthSize, scaledHeightSize;
731        if (mUseMinScale) {
732            scaledWidthSize = (int) (parentWidthSize / mMinScale);
733            scaledHeightSize = (int) (parentHeightSize / mMinScale);
734        } else {
735            scaledWidthSize = widthSize;
736            scaledHeightSize = heightSize;
737        }
738        mViewport.set(0, 0, widthSize, heightSize);
739
740        if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
741            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
742            return;
743        }
744
745        // Return early if we aren't given a proper dimension
746        if (widthSize <= 0 || heightSize <= 0) {
747            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
748            return;
749        }
750
751        /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
752         * of the All apps view on XLarge displays to not take up more space then it needs. Width
753         * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
754         * each page to have the same width.
755         */
756        final int verticalPadding = getPaddingTop() + getPaddingBottom();
757        final int horizontalPadding = getPaddingLeft() + getPaddingRight();
758
759        int referenceChildWidth = 0;
760        // The children are given the same width and height as the workspace
761        // unless they were set to WRAP_CONTENT
762        if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
763        if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
764        if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
765        if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
766        if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
767        final int childCount = getChildCount();
768        for (int i = 0; i < childCount; i++) {
769            // disallowing padding in paged view (just pass 0)
770            final View child = getPageAt(i);
771            if (child.getVisibility() != GONE) {
772                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
773
774                int childWidthMode;
775                int childHeightMode;
776                int childWidth;
777                int childHeight;
778
779                if (!lp.isFullScreenPage) {
780                    if (lp.width == LayoutParams.WRAP_CONTENT) {
781                        childWidthMode = MeasureSpec.AT_MOST;
782                    } else {
783                        childWidthMode = MeasureSpec.EXACTLY;
784                    }
785
786                    if (lp.height == LayoutParams.WRAP_CONTENT) {
787                        childHeightMode = MeasureSpec.AT_MOST;
788                    } else {
789                        childHeightMode = MeasureSpec.EXACTLY;
790                    }
791
792                    childWidth = getViewportWidth() - horizontalPadding
793                            - mInsets.left - mInsets.right;
794                    childHeight = getViewportHeight() - verticalPadding
795                            - mInsets.top - mInsets.bottom;
796                    mNormalChildHeight = childHeight;
797                } else {
798                    childWidthMode = MeasureSpec.EXACTLY;
799                    childHeightMode = MeasureSpec.EXACTLY;
800
801                    childWidth = getViewportWidth() - mInsets.left - mInsets.right;
802                    childHeight = getViewportHeight();
803                }
804                if (referenceChildWidth == 0) {
805                    referenceChildWidth = childWidth;
806                }
807
808                final int childWidthMeasureSpec =
809                        MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
810                    final int childHeightMeasureSpec =
811                        MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
812                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
813            }
814        }
815        setMeasuredDimension(scaledWidthSize, scaledHeightSize);
816    }
817
818    @SuppressLint("DrawAllocation")
819    @Override
820    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
821        if (getChildCount() == 0) {
822            return;
823        }
824
825        if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
826        final int childCount = getChildCount();
827
828        int offsetX = getViewportOffsetX();
829        int offsetY = getViewportOffsetY();
830
831        // Update the viewport offsets
832        mViewport.offset(offsetX, offsetY);
833
834        final int startIndex = mIsRtl ? childCount - 1 : 0;
835        final int endIndex = mIsRtl ? -1 : childCount;
836        final int delta = mIsRtl ? -1 : 1;
837
838        int verticalPadding = getPaddingTop() + getPaddingBottom();
839
840        LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
841        LayoutParams nextLp;
842
843        int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
844        if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
845            mPageScrolls = new int[childCount];
846        }
847
848        for (int i = startIndex; i != endIndex; i += delta) {
849            final View child = getPageAt(i);
850            if (child.getVisibility() != View.GONE) {
851                lp = (LayoutParams) child.getLayoutParams();
852                int childTop;
853                if (lp.isFullScreenPage) {
854                    childTop = offsetY;
855                } else {
856                    childTop = offsetY + getPaddingTop() + mInsets.top;
857                    childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
858                }
859
860                final int childWidth = child.getMeasuredWidth();
861                final int childHeight = child.getMeasuredHeight();
862
863                if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
864                child.layout(childLeft, childTop,
865                        childLeft + child.getMeasuredWidth(), childTop + childHeight);
866
867                int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
868                mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
869
870                int pageGap = mPageSpacing;
871                int next = i + delta;
872                if (next != endIndex) {
873                    nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
874                } else {
875                    nextLp = null;
876                }
877
878                // Prevent full screen pages from showing in the viewport
879                // when they are not the current page.
880                if (lp.isFullScreenPage) {
881                    pageGap = getPaddingLeft();
882                } else if (nextLp != null && nextLp.isFullScreenPage) {
883                    pageGap = getPaddingRight();
884                }
885
886                childLeft += childWidth + pageGap + getChildGap();
887            }
888        }
889
890        final LayoutTransition transition = getLayoutTransition();
891        // If the transition is running defer updating max scroll, as some empty pages could
892        // still be present, and a max scroll change could cause sudden jumps in scroll.
893        if (transition != null && transition.isRunning()) {
894            transition.addTransitionListener(new LayoutTransition.TransitionListener() {
895
896                @Override
897                public void startTransition(LayoutTransition transition, ViewGroup container,
898                        View view, int transitionType) { }
899
900                @Override
901                public void endTransition(LayoutTransition transition, ViewGroup container,
902                        View view, int transitionType) {
903                    // Wait until all transitions are complete.
904                    if (!transition.isRunning()) {
905                        transition.removeTransitionListener(this);
906                        updateMaxScrollX();
907                    }
908                }
909            });
910        } else {
911            updateMaxScrollX();
912        }
913
914        if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
915            updateCurrentPageScroll();
916            mFirstLayout = false;
917        }
918
919        if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
920            if (mRestorePage != INVALID_RESTORE_PAGE) {
921                setCurrentPage(mRestorePage);
922                mRestorePage = INVALID_RESTORE_PAGE;
923            } else {
924                setCurrentPage(getNextPage());
925            }
926        }
927        mChildCountOnLastLayout = childCount;
928
929        if (isReordering(true)) {
930            updateDragViewTranslationDuringDrag();
931        }
932    }
933
934    protected int getChildGap() {
935        return 0;
936    }
937
938    @Thunk void updateMaxScrollX() {
939        int childCount = getChildCount();
940        if (childCount > 0) {
941            final int index = mIsRtl ? 0 : childCount - 1;
942            mMaxScrollX = getScrollForPage(index);
943        } else {
944            mMaxScrollX = 0;
945        }
946    }
947
948    public void setPageSpacing(int pageSpacing) {
949        mPageSpacing = pageSpacing;
950        requestLayout();
951    }
952
953    /**
954     * Called when the center screen changes during scrolling.
955     */
956    protected void screenScrolled(int screenCenter) { }
957
958    @Override
959    public void onChildViewAdded(View parent, View child) {
960        // Update the page indicator, we don't update the page indicator as we
961        // add/remove pages
962        if (mPageIndicator != null && !isReordering(false)) {
963            int pageIndex = indexOfChild(child);
964            mPageIndicator.addMarker(pageIndex,
965                    getPageIndicatorMarker(pageIndex),
966                    true);
967        }
968
969        // This ensures that when children are added, they get the correct transforms / alphas
970        // in accordance with any scroll effects.
971        mForceScreenScrolled = true;
972        updateFreescrollBounds();
973        invalidate();
974    }
975
976    @Override
977    public void onChildViewRemoved(View parent, View child) {
978        mForceScreenScrolled = true;
979        updateFreescrollBounds();
980        invalidate();
981    }
982
983    private void removeMarkerForView(int index) {
984        // Update the page indicator, we don't update the page indicator as we
985        // add/remove pages
986        if (mPageIndicator != null && !isReordering(false)) {
987            mPageIndicator.removeMarker(index, true);
988        }
989    }
990
991    @Override
992    public void removeView(View v) {
993        // XXX: We should find a better way to hook into this before the view
994        // gets removed form its parent...
995        removeMarkerForView(indexOfChild(v));
996        super.removeView(v);
997    }
998    @Override
999    public void removeViewInLayout(View v) {
1000        // XXX: We should find a better way to hook into this before the view
1001        // gets removed form its parent...
1002        removeMarkerForView(indexOfChild(v));
1003        super.removeViewInLayout(v);
1004    }
1005    @Override
1006    public void removeViewAt(int index) {
1007        // XXX: We should find a better way to hook into this before the view
1008        // gets removed form its parent...
1009        removeMarkerForView(index);
1010        super.removeViewAt(index);
1011    }
1012    @Override
1013    public void removeAllViewsInLayout() {
1014        // Update the page indicator, we don't update the page indicator as we
1015        // add/remove pages
1016        if (mPageIndicator != null) {
1017            mPageIndicator.removeAllMarkers(true);
1018        }
1019
1020        super.removeAllViewsInLayout();
1021    }
1022
1023    protected int getChildOffset(int index) {
1024        if (index < 0 || index > getChildCount() - 1) return 0;
1025
1026        int offset = getPageAt(index).getLeft() - getViewportOffsetX();
1027
1028        return offset;
1029    }
1030
1031    protected void getFreeScrollPageRange(int[] range) {
1032        range[0] = 0;
1033        range[1] = Math.max(0, getChildCount() - 1);
1034    }
1035
1036    protected void getVisiblePages(int[] range) {
1037        final int pageCount = getChildCount();
1038        sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
1039
1040        range[0] = -1;
1041        range[1] = -1;
1042
1043        if (pageCount > 0) {
1044            int viewportWidth = getViewportWidth();
1045            int lastVisiblePageIndex = 0;
1046
1047            for (int currPageIndex = 0; currPageIndex < pageCount; currPageIndex++) {
1048                View currPage = getPageAt(currPageIndex);
1049
1050                sTmpIntPoint[0] = 0;
1051                Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1052                if (sTmpIntPoint[0] > viewportWidth) {
1053                    if (range[0] == -1) {
1054                        continue;
1055                    } else {
1056                        break;
1057                    }
1058                }
1059
1060                sTmpIntPoint[0] = currPage.getMeasuredWidth();
1061                Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1062                if (sTmpIntPoint[0] < 0) {
1063                    if (range[0] == -1) {
1064                        continue;
1065                    } else {
1066                        break;
1067                    }
1068                }
1069                if (range[0] < 0) {
1070                    range[0] = currPageIndex;
1071                }
1072                lastVisiblePageIndex = currPageIndex;
1073            }
1074
1075            range[1] = lastVisiblePageIndex;
1076        } else {
1077            range[0] = -1;
1078            range[1] = -1;
1079        }
1080    }
1081
1082    protected boolean shouldDrawChild(View child) {
1083        return child.getVisibility() == VISIBLE;
1084    }
1085
1086    @Override
1087    protected void dispatchDraw(Canvas canvas) {
1088        // Find out which screens are visible; as an optimization we only call draw on them
1089        final int pageCount = getChildCount();
1090        if (pageCount > 0) {
1091            int halfScreenSize = getViewportWidth() / 2;
1092            int screenCenter = getScrollX() + halfScreenSize;
1093
1094            if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1095                // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1096                // set it for the next frame
1097                mForceScreenScrolled = false;
1098                screenScrolled(screenCenter);
1099                mLastScreenCenter = screenCenter;
1100            }
1101
1102            getVisiblePages(mTempVisiblePagesRange);
1103            final int leftScreen = mTempVisiblePagesRange[0];
1104            final int rightScreen = mTempVisiblePagesRange[1];
1105            if (leftScreen != -1 && rightScreen != -1) {
1106                final long drawingTime = getDrawingTime();
1107                // Clip to the bounds
1108                canvas.save();
1109                canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1110                        getScrollY() + getBottom() - getTop());
1111
1112                // Draw all the children, leaving the drag view for last
1113                for (int i = pageCount - 1; i >= 0; i--) {
1114                    final View v = getPageAt(i);
1115                    if (v == mDragView) continue;
1116                    if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
1117                        drawChild(canvas, v, drawingTime);
1118                    }
1119                }
1120                // Draw the drag view on top (if there is one)
1121                if (mDragView != null) {
1122                    drawChild(canvas, mDragView, drawingTime);
1123                }
1124
1125                canvas.restore();
1126            }
1127        }
1128    }
1129
1130    @Override
1131    public void draw(Canvas canvas) {
1132        super.draw(canvas);
1133        if (getPageCount() > 0) {
1134            if (!mEdgeGlowLeft.isFinished()) {
1135                final int restoreCount = canvas.save();
1136                Rect display = mViewport;
1137                canvas.translate(display.left, display.top);
1138                canvas.rotate(270);
1139
1140                getEdgeVerticalPostion(sTmpIntPoint);
1141                canvas.translate(display.top - sTmpIntPoint[1], 0);
1142                mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1143                if (mEdgeGlowLeft.draw(canvas)) {
1144                    postInvalidateOnAnimation();
1145                }
1146                canvas.restoreToCount(restoreCount);
1147            }
1148            if (!mEdgeGlowRight.isFinished()) {
1149                final int restoreCount = canvas.save();
1150                Rect display = mViewport;
1151                canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
1152                canvas.rotate(90);
1153
1154                getEdgeVerticalPostion(sTmpIntPoint);
1155                canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1156                mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1157                if (mEdgeGlowRight.draw(canvas)) {
1158                    postInvalidateOnAnimation();
1159                }
1160                canvas.restoreToCount(restoreCount);
1161            }
1162        }
1163    }
1164
1165    /**
1166     * Returns the top and bottom position for the edge effect.
1167     */
1168    protected abstract void getEdgeVerticalPostion(int[] pos);
1169
1170    @Override
1171    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
1172        int page = indexToPage(indexOfChild(child));
1173        if (page != mCurrentPage || !mScroller.isFinished()) {
1174            snapToPage(page);
1175            return true;
1176        }
1177        return false;
1178    }
1179
1180    @Override
1181    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
1182        int focusablePage;
1183        if (mNextPage != INVALID_PAGE) {
1184            focusablePage = mNextPage;
1185        } else {
1186            focusablePage = mCurrentPage;
1187        }
1188        View v = getPageAt(focusablePage);
1189        if (v != null) {
1190            return v.requestFocus(direction, previouslyFocusedRect);
1191        }
1192        return false;
1193    }
1194
1195    @Override
1196    public boolean dispatchUnhandledMove(View focused, int direction) {
1197        // XXX-RTL: This will be fixed in a future CL
1198        if (direction == View.FOCUS_LEFT) {
1199            if (getCurrentPage() > 0) {
1200                snapToPage(getCurrentPage() - 1);
1201                return true;
1202            }
1203        } else if (direction == View.FOCUS_RIGHT) {
1204            if (getCurrentPage() < getPageCount() - 1) {
1205                snapToPage(getCurrentPage() + 1);
1206                return true;
1207            }
1208        }
1209        return super.dispatchUnhandledMove(focused, direction);
1210    }
1211
1212    @Override
1213    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
1214        // XXX-RTL: This will be fixed in a future CL
1215        if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
1216            getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
1217        }
1218        if (direction == View.FOCUS_LEFT) {
1219            if (mCurrentPage > 0) {
1220                getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
1221            }
1222        } else if (direction == View.FOCUS_RIGHT){
1223            if (mCurrentPage < getPageCount() - 1) {
1224                getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
1225            }
1226        }
1227    }
1228
1229    /**
1230     * If one of our descendant views decides that it could be focused now, only
1231     * pass that along if it's on the current page.
1232     *
1233     * This happens when live folders requery, and if they're off page, they
1234     * end up calling requestFocus, which pulls it on page.
1235     */
1236    @Override
1237    public void focusableViewAvailable(View focused) {
1238        View current = getPageAt(mCurrentPage);
1239        View v = focused;
1240        while (true) {
1241            if (v == current) {
1242                super.focusableViewAvailable(focused);
1243                return;
1244            }
1245            if (v == this) {
1246                return;
1247            }
1248            ViewParent parent = v.getParent();
1249            if (parent instanceof View) {
1250                v = (View)v.getParent();
1251            } else {
1252                return;
1253            }
1254        }
1255    }
1256
1257    /**
1258     * {@inheritDoc}
1259     */
1260    @Override
1261    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1262        if (disallowIntercept) {
1263            // We need to make sure to cancel our long press if
1264            // a scrollable widget takes over touch events
1265            final View currentPage = getPageAt(mCurrentPage);
1266            currentPage.cancelLongPress();
1267        }
1268        super.requestDisallowInterceptTouchEvent(disallowIntercept);
1269    }
1270
1271    /**
1272     * Return true if a tap at (x, y) should trigger a flip to the previous page.
1273     */
1274    protected boolean hitsPreviousPage(float x, float y) {
1275        if (mIsRtl) {
1276            return (x > (getViewportOffsetX() + getViewportWidth() -
1277                    getPaddingRight() - mPageSpacing));
1278        }
1279        return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
1280    }
1281
1282    /**
1283     * Return true if a tap at (x, y) should trigger a flip to the next page.
1284     */
1285    protected boolean hitsNextPage(float x, float y) {
1286        if (mIsRtl) {
1287            return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
1288        }
1289        return  (x > (getViewportOffsetX() + getViewportWidth() -
1290                getPaddingRight() - mPageSpacing));
1291    }
1292
1293    /** Returns whether x and y originated within the buffered viewport */
1294    private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1295        sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1296                mViewport.right + mViewport.width() / 2, mViewport.bottom);
1297        return sTmpRect.contains(x, y);
1298    }
1299
1300    @Override
1301    public boolean onInterceptTouchEvent(MotionEvent ev) {
1302        /*
1303         * This method JUST determines whether we want to intercept the motion.
1304         * If we return true, onTouchEvent will be called and we do the actual
1305         * scrolling there.
1306         */
1307        acquireVelocityTrackerAndAddMovement(ev);
1308
1309        // Skip touch handling if there are no pages to swipe
1310        if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1311
1312        /*
1313         * Shortcut the most recurring case: the user is in the dragging
1314         * state and he is moving his finger.  We want to intercept this
1315         * motion.
1316         */
1317        final int action = ev.getAction();
1318        if ((action == MotionEvent.ACTION_MOVE) &&
1319                (mTouchState == TOUCH_STATE_SCROLLING)) {
1320            return true;
1321        }
1322
1323        switch (action & MotionEvent.ACTION_MASK) {
1324            case MotionEvent.ACTION_MOVE: {
1325                /*
1326                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1327                 * whether the user has moved far enough from his original down touch.
1328                 */
1329                if (mActivePointerId != INVALID_POINTER) {
1330                    determineScrollingStart(ev);
1331                }
1332                // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1333                // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1334                // i.e. fall through to the next case (don't break)
1335                // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1336                // while it's small- this was causing a crash before we checked for INVALID_POINTER)
1337                break;
1338            }
1339
1340            case MotionEvent.ACTION_DOWN: {
1341                final float x = ev.getX();
1342                final float y = ev.getY();
1343                // Remember location of down touch
1344                mDownMotionX = x;
1345                mDownMotionY = y;
1346                mDownScrollX = getScrollX();
1347                mLastMotionX = x;
1348                mLastMotionY = y;
1349                float[] p = mapPointFromViewToParent(this, x, y);
1350                mParentDownMotionX = p[0];
1351                mParentDownMotionY = p[1];
1352                mLastMotionXRemainder = 0;
1353                mTotalMotionX = 0;
1354                mActivePointerId = ev.getPointerId(0);
1355
1356                /*
1357                 * If being flinged and user touches the screen, initiate drag;
1358                 * otherwise don't.  mScroller.isFinished should be false when
1359                 * being flinged.
1360                 */
1361                final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
1362                final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
1363
1364                if (finishedScrolling) {
1365                    mTouchState = TOUCH_STATE_REST;
1366                    if (!mScroller.isFinished() && !mFreeScroll) {
1367                        setCurrentPage(getNextPage());
1368                        pageEndMoving();
1369                    }
1370                } else {
1371                    if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1372                        mTouchState = TOUCH_STATE_SCROLLING;
1373                    } else {
1374                        mTouchState = TOUCH_STATE_REST;
1375                    }
1376                }
1377
1378                break;
1379            }
1380
1381            case MotionEvent.ACTION_UP:
1382            case MotionEvent.ACTION_CANCEL:
1383                resetTouchState();
1384                break;
1385
1386            case MotionEvent.ACTION_POINTER_UP:
1387                onSecondaryPointerUp(ev);
1388                releaseVelocityTracker();
1389                break;
1390        }
1391
1392        /*
1393         * The only time we want to intercept motion events is if we are in the
1394         * drag mode.
1395         */
1396        return mTouchState != TOUCH_STATE_REST;
1397    }
1398
1399    protected void determineScrollingStart(MotionEvent ev) {
1400        determineScrollingStart(ev, 1.0f);
1401    }
1402
1403    /*
1404     * Determines if we should change the touch state to start scrolling after the
1405     * user moves their touch point too far.
1406     */
1407    protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
1408        // Disallow scrolling if we don't have a valid pointer index
1409        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1410        if (pointerIndex == -1) return;
1411
1412        // Disallow scrolling if we started the gesture from outside the viewport
1413        final float x = ev.getX(pointerIndex);
1414        final float y = ev.getY(pointerIndex);
1415        if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1416
1417        final int xDiff = (int) Math.abs(x - mLastMotionX);
1418
1419        final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
1420        boolean xMoved = xDiff > touchSlop;
1421
1422        if (xMoved) {
1423            // Scroll if the user moved far enough along the X axis
1424            mTouchState = TOUCH_STATE_SCROLLING;
1425            mTotalMotionX += Math.abs(mLastMotionX - x);
1426            mLastMotionX = x;
1427            mLastMotionXRemainder = 0;
1428            onScrollInteractionBegin();
1429            pageBeginMoving();
1430        }
1431    }
1432
1433    protected void cancelCurrentPageLongPress() {
1434        // Try canceling the long press. It could also have been scheduled
1435        // by a distant descendant, so use the mAllowLongPress flag to block
1436        // everything
1437        final View currentPage = getPageAt(mCurrentPage);
1438        if (currentPage != null) {
1439            currentPage.cancelLongPress();
1440        }
1441    }
1442
1443    protected float getScrollProgress(int screenCenter, View v, int page) {
1444        final int halfScreenSize = getViewportWidth() / 2;
1445
1446        int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
1447        int count = getChildCount();
1448
1449        final int totalDistance;
1450
1451        int adjacentPage = page + 1;
1452        if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
1453            adjacentPage = page - 1;
1454        }
1455
1456        if (adjacentPage < 0 || adjacentPage > count - 1) {
1457            totalDistance = v.getMeasuredWidth() + mPageSpacing;
1458        } else {
1459            totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1460        }
1461
1462        float scrollProgress = delta / (totalDistance * 1.0f);
1463        scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1464        scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
1465        return scrollProgress;
1466    }
1467
1468    public int getScrollForPage(int index) {
1469        if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1470            return 0;
1471        } else {
1472            return mPageScrolls[index];
1473        }
1474    }
1475
1476    // While layout transitions are occurring, a child's position may stray from its baseline
1477    // position. This method returns the magnitude of this stray at any given time.
1478    public int getLayoutTransitionOffsetForPage(int index) {
1479        if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1480            return 0;
1481        } else {
1482            View child = getChildAt(index);
1483
1484            int scrollOffset = 0;
1485            LayoutParams lp = (LayoutParams) child.getLayoutParams();
1486            if (!lp.isFullScreenPage) {
1487                scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1488            }
1489
1490            int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1491            return (int) (child.getX() - baselineX);
1492        }
1493    }
1494
1495    protected void dampedOverScroll(float amount) {
1496        int screenSize = getViewportWidth();
1497        float f = (amount / screenSize);
1498        if (f < 0) {
1499            mEdgeGlowLeft.onPull(-f);
1500        } else if (f > 0) {
1501            mEdgeGlowRight.onPull(f);
1502        } else {
1503            return;
1504        }
1505        invalidate();
1506    }
1507
1508    protected void overScroll(float amount) {
1509        dampedOverScroll(amount);
1510    }
1511
1512    public void enableFreeScroll() {
1513        setEnableFreeScroll(true);
1514    }
1515
1516    public void disableFreeScroll() {
1517        setEnableFreeScroll(false);
1518    }
1519
1520    void updateFreescrollBounds() {
1521        getFreeScrollPageRange(mTempVisiblePagesRange);
1522        if (mIsRtl) {
1523            mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1524            mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1525        } else {
1526            mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1527            mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1528        }
1529    }
1530
1531    private void setEnableFreeScroll(boolean freeScroll) {
1532        mFreeScroll = freeScroll;
1533
1534        if (mFreeScroll) {
1535            updateFreescrollBounds();
1536            getFreeScrollPageRange(mTempVisiblePagesRange);
1537            if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1538                setCurrentPage(mTempVisiblePagesRange[0]);
1539            } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1540                setCurrentPage(mTempVisiblePagesRange[1]);
1541            }
1542        }
1543
1544        setEnableOverscroll(!freeScroll);
1545    }
1546
1547    protected void setEnableOverscroll(boolean enable) {
1548        mAllowOverScroll = enable;
1549    }
1550
1551    private int getNearestHoverOverPageIndex() {
1552        if (mDragView != null) {
1553            int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1554                    + mDragView.getTranslationX());
1555            getFreeScrollPageRange(mTempVisiblePagesRange);
1556            int minDistance = Integer.MAX_VALUE;
1557            int minIndex = indexOfChild(mDragView);
1558            for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1559                View page = getPageAt(i);
1560                int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1561                int d = Math.abs(dragX - pageX);
1562                if (d < minDistance) {
1563                    minIndex = i;
1564                    minDistance = d;
1565                }
1566            }
1567            return minIndex;
1568        }
1569        return -1;
1570    }
1571
1572    @Override
1573    public boolean onTouchEvent(MotionEvent ev) {
1574        super.onTouchEvent(ev);
1575
1576        // Skip touch handling if there are no pages to swipe
1577        if (getChildCount() <= 0) return super.onTouchEvent(ev);
1578
1579        acquireVelocityTrackerAndAddMovement(ev);
1580
1581        final int action = ev.getAction();
1582
1583        switch (action & MotionEvent.ACTION_MASK) {
1584        case MotionEvent.ACTION_DOWN:
1585            /*
1586             * If being flinged and user touches, stop the fling. isFinished
1587             * will be false if being flinged.
1588             */
1589            if (!mScroller.isFinished()) {
1590                abortScrollerAnimation(false);
1591            }
1592
1593            // Remember where the motion event started
1594            mDownMotionX = mLastMotionX = ev.getX();
1595            mDownMotionY = mLastMotionY = ev.getY();
1596            mDownScrollX = getScrollX();
1597            float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1598            mParentDownMotionX = p[0];
1599            mParentDownMotionY = p[1];
1600            mLastMotionXRemainder = 0;
1601            mTotalMotionX = 0;
1602            mActivePointerId = ev.getPointerId(0);
1603
1604            if (mTouchState == TOUCH_STATE_SCROLLING) {
1605                onScrollInteractionBegin();
1606                pageBeginMoving();
1607            }
1608            break;
1609
1610        case MotionEvent.ACTION_MOVE:
1611            if (mTouchState == TOUCH_STATE_SCROLLING) {
1612                // Scroll to follow the motion event
1613                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1614
1615                if (pointerIndex == -1) return true;
1616
1617                final float x = ev.getX(pointerIndex);
1618                final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
1619
1620                mTotalMotionX += Math.abs(deltaX);
1621
1622                // Only scroll and update mLastMotionX if we have moved some discrete amount.  We
1623                // keep the remainder because we are actually testing if we've moved from the last
1624                // scrolled position (which is discrete).
1625                if (Math.abs(deltaX) >= 1.0f) {
1626                    scrollBy((int) deltaX, 0);
1627                    mLastMotionX = x;
1628                    mLastMotionXRemainder = deltaX - (int) deltaX;
1629                } else {
1630                    awakenScrollBars();
1631                }
1632            } else if (mTouchState == TOUCH_STATE_REORDERING) {
1633                // Update the last motion position
1634                mLastMotionX = ev.getX();
1635                mLastMotionY = ev.getY();
1636
1637                // Update the parent down so that our zoom animations take this new movement into
1638                // account
1639                float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1640                mParentDownMotionX = pt[0];
1641                mParentDownMotionY = pt[1];
1642                updateDragViewTranslationDuringDrag();
1643
1644                // Find the closest page to the touch point
1645                final int dragViewIndex = indexOfChild(mDragView);
1646
1647                if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1648                if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1649                if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1650                if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1651
1652                final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1653                if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
1654                    mTempVisiblePagesRange[0] = 0;
1655                    mTempVisiblePagesRange[1] = getPageCount() - 1;
1656                    getFreeScrollPageRange(mTempVisiblePagesRange);
1657                    if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1658                            pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1659                            pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1660                        mSidePageHoverIndex = pageUnderPointIndex;
1661                        mSidePageHoverRunnable = new Runnable() {
1662                            @Override
1663                            public void run() {
1664                                // Setup the scroll to the correct page before we swap the views
1665                                snapToPage(pageUnderPointIndex);
1666
1667                                // For each of the pages between the paged view and the drag view,
1668                                // animate them from the previous position to the new position in
1669                                // the layout (as a result of the drag view moving in the layout)
1670                                int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1671                                int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1672                                        dragViewIndex + 1 : pageUnderPointIndex;
1673                                int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1674                                        dragViewIndex - 1 : pageUnderPointIndex;
1675                                for (int i = lowerIndex; i <= upperIndex; ++i) {
1676                                    View v = getChildAt(i);
1677                                    // dragViewIndex < pageUnderPointIndex, so after we remove the
1678                                    // drag view all subsequent views to pageUnderPointIndex will
1679                                    // shift down.
1680                                    int oldX = getViewportOffsetX() + getChildOffset(i);
1681                                    int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1682
1683                                    // Animate the view translation from its old position to its new
1684                                    // position
1685                                    ObjectAnimator anim = (ObjectAnimator) v.getTag();
1686                                    if (anim != null) {
1687                                        anim.cancel();
1688                                    }
1689
1690                                    v.setTranslationX(oldX - newX);
1691                                    anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
1692                                    anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1693                                    anim.start();
1694                                    v.setTag(anim);
1695                                }
1696
1697                                removeView(mDragView);
1698                                addView(mDragView, pageUnderPointIndex);
1699                                mSidePageHoverIndex = -1;
1700                                if (mPageIndicator != null) {
1701                                    mPageIndicator.setActiveMarker(getNextPage());
1702                                }
1703                            }
1704                        };
1705                        postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1706                    }
1707                } else {
1708                    removeCallbacks(mSidePageHoverRunnable);
1709                    mSidePageHoverIndex = -1;
1710                }
1711            } else {
1712                determineScrollingStart(ev);
1713            }
1714            break;
1715
1716        case MotionEvent.ACTION_UP:
1717            if (mTouchState == TOUCH_STATE_SCROLLING) {
1718                final int activePointerId = mActivePointerId;
1719                final int pointerIndex = ev.findPointerIndex(activePointerId);
1720                final float x = ev.getX(pointerIndex);
1721                final VelocityTracker velocityTracker = mVelocityTracker;
1722                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1723                int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
1724                final int deltaX = (int) (x - mDownMotionX);
1725                final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
1726                boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1727                        SIGNIFICANT_MOVE_THRESHOLD;
1728
1729                mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1730
1731                boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
1732                        Math.abs(velocityX) > mFlingThresholdVelocity;
1733
1734                if (!mFreeScroll) {
1735                    // In the case that the page is moved far to one direction and then is flung
1736                    // in the opposite direction, we use a threshold to determine whether we should
1737                    // just return to the starting page, or if we should skip one further.
1738                    boolean returnToOriginalPage = false;
1739                    if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1740                            Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1741                        returnToOriginalPage = true;
1742                    }
1743
1744                    int finalPage;
1745                    // We give flings precedence over large moves, which is why we short-circuit our
1746                    // test for a large move if a fling has been registered. That is, a large
1747                    // move to the left and fling to the right will register as a fling to the right.
1748                    boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1749                    boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
1750                    if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1751                            (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1752                        finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1753                        snapToPageWithVelocity(finalPage, velocityX);
1754                    } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1755                            (isFling && isVelocityXLeft)) &&
1756                            mCurrentPage < getChildCount() - 1) {
1757                        finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1758                        snapToPageWithVelocity(finalPage, velocityX);
1759                    } else {
1760                        snapToDestination();
1761                    }
1762                } else {
1763                    if (!mScroller.isFinished()) {
1764                        abortScrollerAnimation(true);
1765                    }
1766
1767                    float scaleX = getScaleX();
1768                    int vX = (int) (-velocityX * scaleX);
1769                    int initialScrollX = (int) (getScrollX() * scaleX);
1770
1771                    mScroller.setInterpolator(mDefaultInterpolator);
1772                    mScroller.fling(initialScrollX,
1773                            getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1774                    invalidate();
1775                }
1776                onScrollInteractionEnd();
1777            } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1778                // at this point we have not moved beyond the touch slop
1779                // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1780                // we can just page
1781                int nextPage = Math.max(0, mCurrentPage - 1);
1782                if (nextPage != mCurrentPage) {
1783                    snapToPage(nextPage);
1784                } else {
1785                    snapToDestination();
1786                }
1787            } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
1788                // at this point we have not moved beyond the touch slop
1789                // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1790                // we can just page
1791                int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1792                if (nextPage != mCurrentPage) {
1793                    snapToPage(nextPage);
1794                } else {
1795                    snapToDestination();
1796                }
1797            } else if (mTouchState == TOUCH_STATE_REORDERING) {
1798                // Update the last motion position
1799                mLastMotionX = ev.getX();
1800                mLastMotionY = ev.getY();
1801
1802                // Update the parent down so that our zoom animations take this new movement into
1803                // account
1804                float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1805                mParentDownMotionX = pt[0];
1806                mParentDownMotionY = pt[1];
1807                updateDragViewTranslationDuringDrag();
1808            } else {
1809                if (!mCancelTap) {
1810                    onUnhandledTap(ev);
1811                }
1812            }
1813
1814            // Remove the callback to wait for the side page hover timeout
1815            removeCallbacks(mSidePageHoverRunnable);
1816            // End any intermediate reordering states
1817            resetTouchState();
1818            break;
1819
1820        case MotionEvent.ACTION_CANCEL:
1821            if (mTouchState == TOUCH_STATE_SCROLLING) {
1822                snapToDestination();
1823            }
1824            resetTouchState();
1825            break;
1826
1827        case MotionEvent.ACTION_POINTER_UP:
1828            onSecondaryPointerUp(ev);
1829            releaseVelocityTracker();
1830            break;
1831        }
1832
1833        return true;
1834    }
1835
1836    private void resetTouchState() {
1837        releaseVelocityTracker();
1838        endReordering();
1839        mCancelTap = false;
1840        mTouchState = TOUCH_STATE_REST;
1841        mActivePointerId = INVALID_POINTER;
1842        mEdgeGlowLeft.onRelease();
1843        mEdgeGlowRight.onRelease();
1844    }
1845
1846    /**
1847     * Triggered by scrolling via touch
1848     */
1849    protected void onScrollInteractionBegin() {
1850    }
1851
1852    protected void onScrollInteractionEnd() {
1853    }
1854
1855    protected void onUnhandledTap(MotionEvent ev) {
1856        ((Launcher) getContext()).onClick(this);
1857    }
1858
1859    @Override
1860    public boolean onGenericMotionEvent(MotionEvent event) {
1861        if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1862            switch (event.getAction()) {
1863                case MotionEvent.ACTION_SCROLL: {
1864                    // Handle mouse (or ext. device) by shifting the page depending on the scroll
1865                    final float vscroll;
1866                    final float hscroll;
1867                    if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1868                        vscroll = 0;
1869                        hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1870                    } else {
1871                        vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1872                        hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1873                    }
1874                    if (hscroll != 0 || vscroll != 0) {
1875                        boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
1876                                                         : (hscroll > 0 || vscroll > 0);
1877                        if (isForwardScroll) {
1878                            scrollRight();
1879                        } else {
1880                            scrollLeft();
1881                        }
1882                        return true;
1883                    }
1884                }
1885            }
1886        }
1887        return super.onGenericMotionEvent(event);
1888    }
1889
1890    private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1891        if (mVelocityTracker == null) {
1892            mVelocityTracker = VelocityTracker.obtain();
1893        }
1894        mVelocityTracker.addMovement(ev);
1895    }
1896
1897    private void releaseVelocityTracker() {
1898        if (mVelocityTracker != null) {
1899            mVelocityTracker.clear();
1900            mVelocityTracker.recycle();
1901            mVelocityTracker = null;
1902        }
1903    }
1904
1905    private void onSecondaryPointerUp(MotionEvent ev) {
1906        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1907                MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1908        final int pointerId = ev.getPointerId(pointerIndex);
1909        if (pointerId == mActivePointerId) {
1910            // This was our active pointer going up. Choose a new
1911            // active pointer and adjust accordingly.
1912            // TODO: Make this decision more intelligent.
1913            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1914            mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1915            mLastMotionY = ev.getY(newPointerIndex);
1916            mLastMotionXRemainder = 0;
1917            mActivePointerId = ev.getPointerId(newPointerIndex);
1918            if (mVelocityTracker != null) {
1919                mVelocityTracker.clear();
1920            }
1921        }
1922    }
1923
1924    @Override
1925    public void requestChildFocus(View child, View focused) {
1926        super.requestChildFocus(child, focused);
1927        int page = indexToPage(indexOfChild(child));
1928        if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
1929            snapToPage(page);
1930        }
1931    }
1932
1933    int getPageNearestToCenterOfScreen() {
1934        int minDistanceFromScreenCenter = Integer.MAX_VALUE;
1935        int minDistanceFromScreenCenterIndex = -1;
1936        int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
1937        final int childCount = getChildCount();
1938        for (int i = 0; i < childCount; ++i) {
1939            View layout = (View) getPageAt(i);
1940            int childWidth = layout.getMeasuredWidth();
1941            int halfChildWidth = (childWidth / 2);
1942            int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
1943            int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1944            if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1945                minDistanceFromScreenCenter = distanceFromScreenCenter;
1946                minDistanceFromScreenCenterIndex = i;
1947            }
1948        }
1949        return minDistanceFromScreenCenterIndex;
1950    }
1951
1952    protected void snapToDestination() {
1953        snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
1954    }
1955
1956    private static class ScrollInterpolator implements Interpolator {
1957        public ScrollInterpolator() {
1958        }
1959
1960        public float getInterpolation(float t) {
1961            t -= 1.0f;
1962            return t*t*t*t*t + 1;
1963        }
1964    }
1965
1966    // We want the duration of the page snap animation to be influenced by the distance that
1967    // the screen has to travel, however, we don't want this duration to be effected in a
1968    // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1969    // of travel has on the overall snap duration.
1970    private float distanceInfluenceForSnapDuration(float f) {
1971        f -= 0.5f; // center the values about 0.
1972        f *= 0.3f * Math.PI / 2.0f;
1973        return (float) Math.sin(f);
1974    }
1975
1976    protected void snapToPageWithVelocity(int whichPage, int velocity) {
1977        whichPage = validateNewPage(whichPage);
1978        int halfScreenSize = getViewportWidth() / 2;
1979
1980        final int newX = getScrollForPage(whichPage);
1981        int delta = newX - getScrollX();
1982        int duration = 0;
1983
1984        if (Math.abs(velocity) < mMinFlingVelocity) {
1985            // If the velocity is low enough, then treat this more as an automatic page advance
1986            // as opposed to an apparent physical response to flinging
1987            snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1988            return;
1989        }
1990
1991        // Here we compute a "distance" that will be used in the computation of the overall
1992        // snap duration. This is a function of the actual distance that needs to be traveled;
1993        // we keep this value close to half screen size in order to reduce the variance in snap
1994        // duration as a function of the distance the page needs to travel.
1995        float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
1996        float distance = halfScreenSize + halfScreenSize *
1997                distanceInfluenceForSnapDuration(distanceRatio);
1998
1999        velocity = Math.abs(velocity);
2000        velocity = Math.max(mMinSnapVelocity, velocity);
2001
2002        // we want the page's snap velocity to approximately match the velocity at which the
2003        // user flings, so we scale the duration by a value near to the derivative of the scroll
2004        // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2005        duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
2006
2007        snapToPage(whichPage, delta, duration);
2008    }
2009
2010    public void snapToPage(int whichPage) {
2011        snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2012    }
2013
2014    protected void snapToPageImmediately(int whichPage) {
2015        snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
2016    }
2017
2018    protected void snapToPage(int whichPage, int duration) {
2019        snapToPage(whichPage, duration, false, null);
2020    }
2021
2022    protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2023        snapToPage(whichPage, duration, false, interpolator);
2024    }
2025
2026    protected void snapToPage(int whichPage, int duration, boolean immediate,
2027            TimeInterpolator interpolator) {
2028        whichPage = validateNewPage(whichPage);
2029
2030        int newX = getScrollForPage(whichPage);
2031        final int delta = newX - getScrollX();
2032        snapToPage(whichPage, delta, duration, immediate, interpolator);
2033    }
2034
2035    protected void snapToPage(int whichPage, int delta, int duration) {
2036        snapToPage(whichPage, delta, duration, false, null);
2037    }
2038
2039    protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2040            TimeInterpolator interpolator) {
2041        whichPage = validateNewPage(whichPage);
2042
2043        mNextPage = whichPage;
2044        View focusedChild = getFocusedChild();
2045        if (focusedChild != null && whichPage != mCurrentPage &&
2046                focusedChild == getPageAt(mCurrentPage)) {
2047            focusedChild.clearFocus();
2048        }
2049
2050        pageBeginMoving();
2051        awakenScrollBars(duration);
2052        if (immediate) {
2053            duration = 0;
2054        } else if (duration == 0) {
2055            duration = Math.abs(delta);
2056        }
2057
2058        if (!mScroller.isFinished()) {
2059            abortScrollerAnimation(false);
2060        }
2061
2062        if (interpolator != null) {
2063            mScroller.setInterpolator(interpolator);
2064        } else {
2065            mScroller.setInterpolator(mDefaultInterpolator);
2066        }
2067
2068        mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
2069
2070        updatePageIndicator();
2071
2072        // Trigger a compute() to finish switching pages if necessary
2073        if (immediate) {
2074            computeScroll();
2075        }
2076
2077        mForceScreenScrolled = true;
2078        invalidate();
2079    }
2080
2081    public void scrollLeft() {
2082        if (getNextPage() > 0) snapToPage(getNextPage() - 1);
2083    }
2084
2085    public void scrollRight() {
2086        if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
2087    }
2088
2089    public int getPageForView(View v) {
2090        int result = -1;
2091        if (v != null) {
2092            ViewParent vp = v.getParent();
2093            int count = getChildCount();
2094            for (int i = 0; i < count; i++) {
2095                if (vp == getPageAt(i)) {
2096                    return i;
2097                }
2098            }
2099        }
2100        return result;
2101    }
2102
2103    @Override
2104    public boolean performLongClick() {
2105        mCancelTap = true;
2106        return super.performLongClick();
2107    }
2108
2109    public static class SavedState extends BaseSavedState {
2110        int currentPage = -1;
2111
2112        SavedState(Parcelable superState) {
2113            super(superState);
2114        }
2115
2116        @Thunk SavedState(Parcel in) {
2117            super(in);
2118            currentPage = in.readInt();
2119        }
2120
2121        @Override
2122        public void writeToParcel(Parcel out, int flags) {
2123            super.writeToParcel(out, flags);
2124            out.writeInt(currentPage);
2125        }
2126
2127        public static final Parcelable.Creator<SavedState> CREATOR =
2128                new Parcelable.Creator<SavedState>() {
2129            public SavedState createFromParcel(Parcel in) {
2130                return new SavedState(in);
2131            }
2132
2133            public SavedState[] newArray(int size) {
2134                return new SavedState[size];
2135            }
2136        };
2137    }
2138
2139    // Animate the drag view back to the original position
2140    private void animateDragViewToOriginalPosition() {
2141        if (mDragView != null) {
2142            Animator anim = new LauncherViewPropertyAnimator(mDragView)
2143                    .translationX(0)
2144                    .translationY(0)
2145                    .scaleX(1)
2146                    .scaleY(1)
2147                    .setDuration(REORDERING_DROP_REPOSITION_DURATION);
2148            anim.addListener(new AnimatorListenerAdapter() {
2149                @Override
2150                public void onAnimationEnd(Animator animation) {
2151                    onPostReorderingAnimationCompleted();
2152                }
2153            });
2154            anim.start();
2155        }
2156    }
2157
2158    public void onStartReordering() {
2159        // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2160        mTouchState = TOUCH_STATE_REORDERING;
2161        mIsReordering = true;
2162
2163        // We must invalidate to trigger a redraw to update the layers such that the drag view
2164        // is always drawn on top
2165        invalidate();
2166    }
2167
2168    @Thunk void onPostReorderingAnimationCompleted() {
2169        // Trigger the callback when reordering has settled
2170        --mPostReorderingPreZoomInRemainingAnimationCount;
2171        if (mPostReorderingPreZoomInRunnable != null &&
2172                mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2173            mPostReorderingPreZoomInRunnable.run();
2174            mPostReorderingPreZoomInRunnable = null;
2175        }
2176    }
2177
2178    public void onEndReordering() {
2179        mIsReordering = false;
2180    }
2181
2182    public boolean startReordering(View v) {
2183        int dragViewIndex = indexOfChild(v);
2184
2185        if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
2186
2187        mTempVisiblePagesRange[0] = 0;
2188        mTempVisiblePagesRange[1] = getPageCount() - 1;
2189        getFreeScrollPageRange(mTempVisiblePagesRange);
2190        mReorderingStarted = true;
2191
2192        // Check if we are within the reordering range
2193        if (mTempVisiblePagesRange[0] <= dragViewIndex &&
2194            dragViewIndex <= mTempVisiblePagesRange[1]) {
2195            // Find the drag view under the pointer
2196            mDragView = getChildAt(dragViewIndex);
2197            mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2198            mDragViewBaselineLeft = mDragView.getLeft();
2199            snapToPage(getPageNearestToCenterOfScreen());
2200            disableFreeScroll();
2201            onStartReordering();
2202            return true;
2203        }
2204        return false;
2205    }
2206
2207    boolean isReordering(boolean testTouchState) {
2208        boolean state = mIsReordering;
2209        if (testTouchState) {
2210            state &= (mTouchState == TOUCH_STATE_REORDERING);
2211        }
2212        return state;
2213    }
2214    void endReordering() {
2215        // For simplicity, we call endReordering sometimes even if reordering was never started.
2216        // In that case, we don't want to do anything.
2217        if (!mReorderingStarted) return;
2218        mReorderingStarted = false;
2219
2220        // If we haven't flung-to-delete the current child, then we just animate the drag view
2221        // back into position
2222        final Runnable onCompleteRunnable = new Runnable() {
2223            @Override
2224            public void run() {
2225                onEndReordering();
2226            }
2227        };
2228
2229        mPostReorderingPreZoomInRunnable = new Runnable() {
2230            public void run() {
2231                onCompleteRunnable.run();
2232                enableFreeScroll();
2233            };
2234        };
2235
2236        mPostReorderingPreZoomInRemainingAnimationCount =
2237                NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2238        // Snap to the current page
2239        snapToPage(indexOfChild(mDragView), 0);
2240        // Animate the drag view back to the front position
2241        animateDragViewToOriginalPosition();
2242    }
2243
2244    /* Accessibility */
2245    @SuppressWarnings("deprecation")
2246    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
2247    @Override
2248    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2249        super.onInitializeAccessibilityNodeInfo(info);
2250        info.setScrollable(getPageCount() > 1);
2251        if (getCurrentPage() < getPageCount() - 1) {
2252            info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2253        }
2254        if (getCurrentPage() > 0) {
2255            info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2256        }
2257        info.setClassName(getClass().getName());
2258
2259        // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2260        // Besides disabling the accessibility long-click, this also prevents this view from getting
2261        // accessibility focus.
2262        info.setLongClickable(false);
2263        if (Utilities.ATLEAST_LOLLIPOP) {
2264            info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2265        }
2266    }
2267
2268    @Override
2269    public void sendAccessibilityEvent(int eventType) {
2270        // Don't let the view send real scroll events.
2271        if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2272            super.sendAccessibilityEvent(eventType);
2273        }
2274    }
2275
2276    @Override
2277    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2278        super.onInitializeAccessibilityEvent(event);
2279        event.setScrollable(getPageCount() > 1);
2280    }
2281
2282    @Override
2283    public boolean performAccessibilityAction(int action, Bundle arguments) {
2284        if (super.performAccessibilityAction(action, arguments)) {
2285            return true;
2286        }
2287        switch (action) {
2288            case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2289                if (getCurrentPage() < getPageCount() - 1) {
2290                    scrollRight();
2291                    return true;
2292                }
2293            } break;
2294            case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2295                if (getCurrentPage() > 0) {
2296                    scrollLeft();
2297                    return true;
2298                }
2299            } break;
2300        }
2301        return false;
2302    }
2303
2304    protected String getCurrentPageDescription() {
2305        return getContext().getString(R.string.default_scroll_format,
2306                getNextPage() + 1, getChildCount());
2307    }
2308
2309    @Override
2310    public boolean onHoverEvent(android.view.MotionEvent event) {
2311        return true;
2312    }
2313}
2314