Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 18 | |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
| 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.TimeInterpolator; |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 29 | import android.graphics.Matrix; |
| 30 | import android.graphics.PointF; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
| 35 | import android.util.AttributeSet; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 36 | import android.util.DisplayMetrics; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 38 | import android.view.InputDevice; |
| 39 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 40 | import android.view.MotionEvent; |
| 41 | import android.view.VelocityTracker; |
| 42 | import android.view.View; |
| 43 | import android.view.ViewConfiguration; |
| 44 | import android.view.ViewGroup; |
| 45 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 46 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 49 | import android.view.animation.AnimationUtils; |
| 50 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 51 | import android.view.animation.Interpolator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 52 | import android.view.animation.LinearInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 53 | import android.widget.Scroller; |
| 54 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 57 | interface Page { |
| 58 | public int getPageChildCount(); |
| 59 | public View getChildOnPageAt(int i); |
| 60 | public void removeAllViewsOnPage(); |
| 61 | public void removeViewOnPageAt(int i); |
| 62 | public int indexOfChildOnPage(View v); |
| 63 | } |
| 64 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 65 | /** |
| 66 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 67 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 68 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 69 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 70 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 71 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 72 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 73 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 74 | // the min drag distance for a fling to register, to prevent random page shifts |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 75 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 76 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 77 | protected static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 78 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 79 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 80 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 81 | private static final float OVERSCROLL_ACCELERATE_FACTOR = 2; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 82 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 83 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 84 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 85 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 86 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 87 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 88 | // The following constants need to be scaled based on density. The scaled versions will be |
| 89 | // assigned to the corresponding member variables below. |
| 90 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 91 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 92 | private static final int MIN_FLING_VELOCITY = 250; |
| 93 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 94 | // We are disabling touch interaction of the widget region for factory ROM. |
| 95 | private static final boolean DISABLE_TOUCH_INTERACTION = false; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 96 | private static final boolean DISABLE_TOUCH_SIDE_PAGES = true; |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 97 | private static final boolean DISABLE_FLING_TO_DELETE = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 98 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 99 | private boolean mFreeScroll = false; |
| 100 | private int mFreeScrollMinScrollX = -1; |
| 101 | private int mFreeScrollMaxScrollX = -1; |
| 102 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 103 | static final int AUTOMATIC_PAGE_SPACING = -1; |
| 104 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 105 | protected int mFlingThresholdVelocity; |
| 106 | protected int mMinFlingVelocity; |
| 107 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 108 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 109 | protected float mDensity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 110 | protected float mSmoothingTime; |
| 111 | protected float mTouchX; |
| 112 | |
| 113 | protected boolean mFirstLayout = true; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 114 | private int mNormalChildHeight; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 115 | |
| 116 | protected int mCurrentPage; |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 117 | protected int mRestorePage = -1; |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 118 | protected int mChildCountOnLastLayout; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 119 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 120 | protected int mNextPage = INVALID_PAGE; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 121 | protected int mMaxScrollX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 122 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 123 | private VelocityTracker mVelocityTracker; |
| 124 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 125 | private float mParentDownMotionX; |
| 126 | private float mParentDownMotionY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 127 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 128 | private float mDownMotionY; |
| 129 | private float mDownScrollX; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 130 | private float mDragViewBaselineLeft; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 131 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 132 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 133 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 134 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 135 | private int mLastScreenCenter = -1; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 136 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 137 | private boolean mCancelTap; |
| 138 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 139 | private int[] mPageScrolls; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 140 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 141 | protected final static int TOUCH_STATE_REST = 0; |
| 142 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 143 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 144 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 145 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 146 | |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 147 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 148 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 149 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 150 | protected boolean mForceScreenScrolled = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 151 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 152 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 153 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 154 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 155 | private int mPagingTouchSlop; |
| 156 | private int mMaximumVelocity; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 157 | protected int mPageSpacing; |
| 158 | protected int mPageLayoutPaddingTop; |
| 159 | protected int mPageLayoutPaddingBottom; |
| 160 | protected int mPageLayoutPaddingLeft; |
| 161 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 162 | protected int mPageLayoutWidthGap; |
| 163 | protected int mPageLayoutHeightGap; |
Michael Jurka | 87b1490 | 2011-05-25 22:13:09 -0700 | [diff] [blame] | 164 | protected int mCellCountX = 0; |
| 165 | protected int mCellCountY = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 166 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 167 | protected boolean mAllowOverScroll = true; |
| 168 | protected int mUnboundedScrollX; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 169 | protected int[] mTempVisiblePagesRange = new int[2]; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 170 | protected boolean mForceDrawAllChildrenNextFrame; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 171 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 172 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 173 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 174 | // the screens from continuing to translate beyond the normal bounds. |
| 175 | protected int mOverScrollX; |
| 176 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 177 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 178 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 179 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 180 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 181 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 182 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 183 | protected ArrayList<Boolean> mDirtyPageContent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 184 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 185 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 186 | protected boolean mContentIsRefreshable = true; |
| 187 | |
| 188 | // If true, modify alpha of neighboring pages as user scrolls left/right |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 189 | protected boolean mFadeInAdjacentScreens = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 190 | |
| 191 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 192 | // to switch to a new page |
| 193 | protected boolean mUsePagingTouchSlop = true; |
| 194 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 195 | // If true, the subclass should directly update scrollX itself in its computeScroll method |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 196 | // (SmoothPagedView does this) |
| 197 | protected boolean mDeferScrollUpdate = false; |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 198 | protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 199 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 200 | protected boolean mIsPageMoving = false; |
| 201 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 202 | // All syncs and layout passes are deferred until data is ready. |
| 203 | protected boolean mIsDataReady = false; |
| 204 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 205 | protected boolean mAllowLongPress = true; |
| 206 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 207 | // Page Indicator |
| 208 | private int mPageIndicatorViewId; |
| 209 | private PageIndicator mPageIndicator; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 210 | private boolean mAllowPagedViewAnimations = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 211 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 212 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 213 | // viewport) |
| 214 | private Rect mViewport = new Rect(); |
| 215 | |
| 216 | // Reordering |
| 217 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 218 | // dimensions such that when we are zoomed out, the view is not clipped |
| 219 | private int REORDERING_DROP_REPOSITION_DURATION = 200; |
| 220 | protected int REORDERING_REORDER_REPOSITION_DURATION = 300; |
| 221 | protected int REORDERING_ZOOM_IN_OUT_DURATION = 250; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 222 | private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 223 | private float mMinScale = 1f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 224 | private boolean mUseMinScale = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 225 | protected View mDragView; |
| 226 | protected AnimatorSet mZoomInOutAnim; |
| 227 | private Runnable mSidePageHoverRunnable; |
| 228 | private int mSidePageHoverIndex = -1; |
| 229 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 230 | private boolean mReorderingStarted = false; |
| 231 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 232 | // animation after endReordering() |
| 233 | private boolean mIsReordering; |
| 234 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
| 235 | private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
| 236 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 237 | private Runnable mPostReorderingPreZoomInRunnable; |
| 238 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 239 | // Convenience/caching |
| 240 | private Matrix mTmpInvMatrix = new Matrix(); |
| 241 | private float[] mTmpPoint = new float[2]; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 242 | private int[] mTmpIntPoint = new int[2]; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 243 | private Rect mTmpRect = new Rect(); |
| 244 | private Rect mAltTmpRect = new Rect(); |
| 245 | |
| 246 | // Fling to delete |
| 247 | private int FLING_TO_DELETE_FADE_OUT_DURATION = 350; |
| 248 | private float FLING_TO_DELETE_FRICTION = 0.035f; |
| 249 | // The degrees specifies how much deviation from the up vector to still consider a fling "up" |
| 250 | private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f; |
| 251 | protected int mFlingToDeleteThresholdVelocity = -1400; |
| 252 | // Drag to delete |
| 253 | private boolean mDeferringForDelete = false; |
| 254 | private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250; |
| 255 | private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350; |
| 256 | |
| 257 | // Drop to delete |
| 258 | private View mDeleteDropTarget; |
| 259 | |
| 260 | private boolean mAutoComputePageSpacing = false; |
| 261 | private boolean mRecomputePageSpacing = false; |
| 262 | |
| 263 | // Bouncer |
| 264 | private boolean mTopAlignPageWhenShrinkingForBouncer = false; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 265 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 266 | protected final Rect mInsets = new Rect(); |
| 267 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 268 | public interface PageSwitchListener { |
| 269 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 272 | public PagedView(Context context) { |
| 273 | this(context, null); |
| 274 | } |
| 275 | |
| 276 | public PagedView(Context context, AttributeSet attrs) { |
| 277 | this(context, attrs, 0); |
| 278 | } |
| 279 | |
| 280 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 281 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 282 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 283 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 284 | R.styleable.PagedView, defStyle, 0); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 285 | setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 286 | if (mPageSpacing < 0) { |
| 287 | mAutoComputePageSpacing = mRecomputePageSpacing = true; |
| 288 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 289 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 290 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 291 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 292 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 293 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 294 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 295 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 296 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 297 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 298 | R.styleable.PagedView_pageLayoutWidthGap, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 299 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 300 | R.styleable.PagedView_pageLayoutHeightGap, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 301 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 302 | a.recycle(); |
| 303 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 304 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 305 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Initializes various states for this workspace. |
| 310 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 311 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 312 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 313 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 314 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 315 | mCurrentPage = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 316 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 317 | |
| 318 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 319 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 321 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 322 | mDensity = getResources().getDisplayMetrics().density; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 323 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 324 | // Scale the fling-to-delete threshold by the density |
| 325 | mFlingToDeleteThresholdVelocity = |
| 326 | (int) (mFlingToDeleteThresholdVelocity * mDensity); |
| 327 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 328 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
| 329 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity); |
| 330 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 331 | setOnHierarchyChangeListener(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 334 | protected void onAttachedToWindow() { |
| 335 | // Hook up the page indicator |
| 336 | ViewGroup parent = (ViewGroup) getParent(); |
| 337 | if (mPageIndicator == null && mPageIndicatorViewId > -1) { |
| 338 | mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 339 | mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 340 | |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 341 | ArrayList<PageIndicator.PageMarkerResources> markers = |
| 342 | new ArrayList<PageIndicator.PageMarkerResources>(); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 343 | for (int i = 0; i < getChildCount(); ++i) { |
| 344 | markers.add(getPageIndicatorMarker(i)); |
| 345 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 346 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 347 | mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | protected void onDetachedFromWindow() { |
| 352 | // Unhook the page indicator |
| 353 | mPageIndicator = null; |
| 354 | } |
| 355 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 356 | void setDeleteDropTarget(View v) { |
| 357 | mDeleteDropTarget = v; |
| 358 | } |
| 359 | |
| 360 | // Convenience methods to map points from self to parent and vice versa |
| 361 | float[] mapPointFromViewToParent(View v, float x, float y) { |
| 362 | mTmpPoint[0] = x; |
| 363 | mTmpPoint[1] = y; |
| 364 | v.getMatrix().mapPoints(mTmpPoint); |
| 365 | mTmpPoint[0] += v.getLeft(); |
| 366 | mTmpPoint[1] += v.getTop(); |
| 367 | return mTmpPoint; |
| 368 | } |
| 369 | float[] mapPointFromParentToView(View v, float x, float y) { |
| 370 | mTmpPoint[0] = x - v.getLeft(); |
| 371 | mTmpPoint[1] = y - v.getTop(); |
| 372 | v.getMatrix().invert(mTmpInvMatrix); |
| 373 | mTmpInvMatrix.mapPoints(mTmpPoint); |
| 374 | return mTmpPoint; |
| 375 | } |
| 376 | |
| 377 | void updateDragViewTranslationDuringDrag() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 378 | if (mDragView != null) { |
| 379 | float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) + |
| 380 | (mDragViewBaselineLeft - mDragView.getLeft()); |
| 381 | float y = mLastMotionY - mDownMotionY; |
| 382 | mDragView.setTranslationX(x); |
| 383 | mDragView.setTranslationY(y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 384 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 385 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " |
| 386 | + x + ", " + y); |
| 387 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | public void setMinScale(float f) { |
| 391 | mMinScale = f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 392 | mUseMinScale = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 393 | requestLayout(); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void setScaleX(float scaleX) { |
| 398 | super.setScaleX(scaleX); |
| 399 | if (isReordering(true)) { |
| 400 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 401 | mLastMotionX = p[0]; |
| 402 | mLastMotionY = p[1]; |
| 403 | updateDragViewTranslationDuringDrag(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
| 408 | // to be larger to account for the minimum possible scale) |
| 409 | int getViewportWidth() { |
| 410 | return mViewport.width(); |
| 411 | } |
| 412 | int getViewportHeight() { |
| 413 | return mViewport.height(); |
| 414 | } |
| 415 | |
| 416 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
| 417 | // PagedView both horizontally and vertically |
| 418 | int getViewportOffsetX() { |
| 419 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
| 420 | } |
| 421 | |
| 422 | int getViewportOffsetY() { |
| 423 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
| 424 | } |
| 425 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 426 | PageIndicator getPageIndicator() { |
| 427 | return mPageIndicator; |
| 428 | } |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 429 | protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) { |
| 430 | return new PageIndicator.PageMarkerResources(); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 431 | } |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 432 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 433 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 434 | mPageSwitchListener = pageSwitchListener; |
| 435 | if (mPageSwitchListener != null) { |
| 436 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 441 | * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api. |
| 442 | */ |
| 443 | public boolean isLayoutRtl() { |
| 444 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 445 | } |
| 446 | |
| 447 | /** |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 448 | * Called by subclasses to mark that data is ready, and that we can begin loading and laying |
| 449 | * out pages. |
| 450 | */ |
| 451 | protected void setDataIsReady() { |
| 452 | mIsDataReady = true; |
| 453 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 454 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 455 | protected boolean isDataReady() { |
| 456 | return mIsDataReady; |
| 457 | } |
| 458 | |
| 459 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 460 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 461 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 462 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 463 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 464 | int getCurrentPage() { |
| 465 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 466 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 467 | |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 468 | int getNextPage() { |
| 469 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 470 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 471 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 472 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 473 | return getChildCount(); |
| 474 | } |
| 475 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 476 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 477 | return getChildAt(index); |
| 478 | } |
| 479 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 480 | protected int indexToPage(int index) { |
| 481 | return index; |
| 482 | } |
| 483 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 484 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 485 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 486 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 487 | * the previous tab page. |
| 488 | */ |
| 489 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 490 | // If the current page is invalid, just reset the scroll position to zero |
| 491 | int newX = 0; |
| 492 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 493 | newX = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 494 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 495 | scrollTo(newX, 0); |
| 496 | mScroller.setFinalX(newX); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 497 | mScroller.forceFinished(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 501 | * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation |
| 502 | * ends, {@link #resumeScrolling()} should be called, along with |
| 503 | * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling. |
| 504 | */ |
| 505 | void pauseScrolling() { |
| 506 | mScroller.forceFinished(true); |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Enables scrolling again. |
| 511 | * @see #pauseScrolling() |
| 512 | */ |
| 513 | void resumeScrolling() { |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 514 | } |
| 515 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 516 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 517 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 518 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 519 | if (!mScroller.isFinished()) { |
| 520 | mScroller.abortAnimation(); |
| 521 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 522 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 523 | // the default |
| 524 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 525 | return; |
| 526 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 527 | |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 528 | mForceScreenScrolled = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 529 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 530 | updateCurrentPageScroll(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 531 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 532 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 535 | /** |
| 536 | * The restore page will be set in place of the current page at the next (likely first) |
| 537 | * layout. |
| 538 | */ |
| 539 | void setRestorePage(int restorePage) { |
| 540 | mRestorePage = restorePage; |
| 541 | } |
| 542 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 543 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 544 | if (mPageSwitchListener != null) { |
| 545 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 546 | } |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 547 | |
| 548 | // Update the page indicator (when we aren't reordering) |
| 549 | if (mPageIndicator != null && !isReordering(false)) { |
| 550 | mPageIndicator.setActiveMarker(getNextPage()); |
| 551 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 552 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 553 | protected void pageBeginMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 554 | if (!mIsPageMoving) { |
| 555 | mIsPageMoving = true; |
| 556 | onPageBeginMoving(); |
| 557 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 560 | protected void pageEndMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 561 | if (mIsPageMoving) { |
| 562 | mIsPageMoving = false; |
| 563 | onPageEndMoving(); |
| 564 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 567 | protected boolean isPageMoving() { |
| 568 | return mIsPageMoving; |
| 569 | } |
| 570 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 571 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 572 | protected void onPageBeginMoving() { |
| 573 | } |
| 574 | |
| 575 | // a method that subclasses can override to add behavior |
| 576 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 579 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 580 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 581 | * |
| 582 | * @param l The listener used to respond to long clicks. |
| 583 | */ |
| 584 | @Override |
| 585 | public void setOnLongClickListener(OnLongClickListener l) { |
| 586 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 587 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 588 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 589 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 590 | } |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 591 | super.setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 595 | public void scrollBy(int x, int y) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 596 | scrollTo(mUnboundedScrollX + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 600 | public void scrollTo(int x, int y) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 601 | // In free scroll mode, we clamp the scrollX |
| 602 | if (mFreeScroll) { |
| 603 | x = Math.min(x, mFreeScrollMaxScrollX); |
| 604 | x = Math.max(x, mFreeScrollMinScrollX); |
| 605 | } |
| 606 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 607 | final boolean isRtl = isLayoutRtl(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 608 | mUnboundedScrollX = x; |
| 609 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 610 | boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0); |
| 611 | boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX); |
| 612 | if (isXBeforeFirstPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 613 | super.scrollTo(0, y); |
| 614 | if (mAllowOverScroll) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 615 | if (isRtl) { |
| 616 | overScroll(x - mMaxScrollX); |
| 617 | } else { |
| 618 | overScroll(x); |
| 619 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 620 | } |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 621 | } else if (isXAfterLastPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 622 | super.scrollTo(mMaxScrollX, y); |
| 623 | if (mAllowOverScroll) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 624 | if (isRtl) { |
| 625 | overScroll(x); |
| 626 | } else { |
| 627 | overScroll(x - mMaxScrollX); |
| 628 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 629 | } |
| 630 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 631 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 632 | super.scrollTo(x, y); |
| 633 | } |
| 634 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 635 | mTouchX = x; |
| 636 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 637 | |
| 638 | // Update the last motion events when scrolling |
| 639 | if (isReordering(true)) { |
| 640 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 641 | mLastMotionX = p[0]; |
| 642 | mLastMotionY = p[1]; |
| 643 | updateDragViewTranslationDuringDrag(); |
| 644 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 648 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 649 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 650 | // Don't bother scrolling if the page does not need to be moved |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 651 | if (getScrollX() != mScroller.getCurrX() |
| 652 | || getScrollY() != mScroller.getCurrY() |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 653 | || mOverScrollX != mScroller.getCurrX()) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 654 | float scaleX = mFreeScroll ? getScaleX() : 1f; |
| 655 | int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX)); |
| 656 | scrollTo(scrollX, mScroller.getCurrY()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 657 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 658 | invalidate(); |
| 659 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 660 | } else if (mNextPage != INVALID_PAGE) { |
| 661 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 662 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 663 | notifyPageSwitchListener(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 664 | |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 665 | // Load the associated pages if necessary |
| 666 | if (mDeferLoadAssociatedPagesUntilScrollCompletes) { |
| 667 | loadAssociatedPages(mCurrentPage); |
| 668 | mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
| 669 | } |
| 670 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 671 | // We don't want to trigger a page end moving unless the page has settled |
| 672 | // and the user has stopped scrolling |
| 673 | if (mTouchState == TOUCH_STATE_REST) { |
| 674 | pageEndMoving(); |
| 675 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 676 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 677 | onPostReorderingAnimationCompleted(); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 678 | // Notify the user when the page changes |
| 679 | AccessibilityManager accessibilityManager = (AccessibilityManager) |
| 680 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 681 | if (accessibilityManager.isEnabled()) { |
| 682 | AccessibilityEvent ev = |
| 683 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
| 684 | ev.getText().add(getCurrentPageDescription()); |
| 685 | sendAccessibilityEventUnchecked(ev); |
| 686 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 687 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 688 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | |
| 692 | @Override |
| 693 | public void computeScroll() { |
| 694 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 697 | protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) { |
| 698 | return mTopAlignPageWhenShrinkingForBouncer; |
| 699 | } |
| 700 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 701 | public static class LayoutParams extends ViewGroup.LayoutParams { |
| 702 | public boolean isFullScreenPage = false; |
| 703 | |
| 704 | /** |
| 705 | * {@inheritDoc} |
| 706 | */ |
| 707 | public LayoutParams(int width, int height) { |
| 708 | super(width, height); |
| 709 | } |
| 710 | |
| 711 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 712 | super(source); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | protected LayoutParams generateDefaultLayoutParams() { |
| 717 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 718 | } |
| 719 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 720 | public void addFullScreenPage(View page) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 721 | LayoutParams lp = generateDefaultLayoutParams(); |
| 722 | lp.isFullScreenPage = true; |
| 723 | super.addView(page, 0, lp); |
| 724 | } |
| 725 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 726 | public int getNormalChildHeight() { |
| 727 | return mNormalChildHeight; |
| 728 | } |
| 729 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 730 | @Override |
| 731 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 732 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 733 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 734 | return; |
| 735 | } |
| 736 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 737 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 738 | // zoom out (and scale down), the view is still contained in the parent |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 739 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 740 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 741 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 742 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 743 | // NOTE: We multiply by 1.5f to account for the fact that depending on the offset of the |
| 744 | // viewport, we can be at most one and a half screens offset once we scale down |
| 745 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 746 | int maxSize = Math.max(dm.widthPixels, dm.heightPixels + mInsets.top + mInsets.bottom); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 747 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 748 | int parentWidthSize, parentHeightSize; |
| 749 | int scaledWidthSize, scaledHeightSize; |
| 750 | if (mUseMinScale) { |
| 751 | parentWidthSize = (int) (1.5f * maxSize); |
| 752 | parentHeightSize = maxSize; |
| 753 | scaledWidthSize = (int) (parentWidthSize / mMinScale); |
| 754 | scaledHeightSize = (int) (parentHeightSize / mMinScale); |
| 755 | } else { |
| 756 | scaledWidthSize = widthSize; |
| 757 | scaledHeightSize = heightSize; |
| 758 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 759 | mViewport.set(0, 0, widthSize, heightSize); |
| 760 | |
| 761 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 762 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 763 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 766 | // Return early if we aren't given a proper dimension |
| 767 | if (widthSize <= 0 || heightSize <= 0) { |
| 768 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 769 | return; |
| 770 | } |
| 771 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 772 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 773 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 774 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 775 | * each page to have the same width. |
| 776 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 777 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 778 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 779 | |
| 780 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 781 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 782 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 783 | if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize); |
| 784 | if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize); |
| 785 | if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding); |
| 786 | if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 787 | final int childCount = getChildCount(); |
| 788 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 789 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 790 | final View child = getPageAt(i); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 791 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 792 | |
| 793 | int childWidthMode; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 794 | int childHeightMode; |
| 795 | int childWidth; |
| 796 | int childHeight; |
| 797 | |
| 798 | if (!lp.isFullScreenPage) { |
| 799 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 800 | childWidthMode = MeasureSpec.AT_MOST; |
| 801 | } else { |
| 802 | childWidthMode = MeasureSpec.EXACTLY; |
| 803 | } |
| 804 | |
| 805 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 806 | childHeightMode = MeasureSpec.AT_MOST; |
| 807 | } else { |
| 808 | childHeightMode = MeasureSpec.EXACTLY; |
| 809 | } |
| 810 | |
| 811 | childWidth = widthSize - horizontalPadding; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 812 | childHeight = heightSize - verticalPadding - mInsets.top - mInsets.bottom; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 813 | mNormalChildHeight = childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 814 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 815 | } else { |
| 816 | childWidthMode = MeasureSpec.EXACTLY; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 817 | childHeightMode = MeasureSpec.EXACTLY; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 818 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 819 | if (mUseMinScale) { |
| 820 | childWidth = getViewportWidth(); |
| 821 | childHeight = getViewportHeight(); |
| 822 | } else { |
| 823 | childWidth = widthSize - getPaddingLeft() - getPaddingRight(); |
| 824 | childHeight = heightSize - getPaddingTop() - getPaddingBottom(); |
| 825 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | final int childWidthMeasureSpec = |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 829 | MeasureSpec.makeMeasureSpec(childWidth, childWidthMode); |
| 830 | final int childHeightMeasureSpec = |
| 831 | MeasureSpec.makeMeasureSpec(childHeight, childHeightMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 832 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 833 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 834 | setMeasuredDimension(scaledWidthSize, scaledHeightSize); |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 835 | |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 836 | if (childCount > 0) { |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 837 | // Calculate the variable page spacing if necessary |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 838 | if (mAutoComputePageSpacing && mRecomputePageSpacing) { |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 839 | // The gap between pages in the PagedView should be equal to the gap from the page |
| 840 | // to the edge of the screen (so it is not visible in the current screen). To |
| 841 | // account for unequal padding on each side of the paged view, we take the maximum |
| 842 | // of the left/right gap and use that as the gap between each page. |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 843 | int offset = (getViewportWidth() - getChildWidth(0)) / 2; |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 844 | int spacing = Math.max(offset, widthSize - offset - |
| 845 | getChildAt(0).getMeasuredWidth()); |
| 846 | setPageSpacing(spacing); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 847 | mRecomputePageSpacing = false; |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 852 | public void setPageSpacing(int pageSpacing) { |
| 853 | mPageSpacing = pageSpacing; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 854 | requestLayout(); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 857 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 858 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 859 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 863 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 864 | final int childCount = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 865 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 866 | int screenWidth = getViewportWidth(); |
| 867 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 868 | int offsetX = getViewportOffsetX(); |
| 869 | int offsetY = getViewportOffsetY(); |
| 870 | |
| 871 | // Update the viewport offsets |
| 872 | mViewport.offset(offsetX, offsetY); |
| 873 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 874 | final boolean isRtl = isLayoutRtl(); |
| 875 | |
| 876 | final int startIndex = isRtl ? childCount - 1 : 0; |
| 877 | final int endIndex = isRtl ? -1 : childCount; |
| 878 | final int delta = isRtl ? -1 : 1; |
| 879 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 880 | int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 881 | int childLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2; |
| 882 | |
| 883 | if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) { |
| 884 | mPageScrolls = new int[getChildCount()]; |
| 885 | } |
| 886 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 887 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 888 | final View child = getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 889 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 890 | int childTop; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 891 | if (lp.isFullScreenPage) { |
| 892 | childTop = offsetY; |
| 893 | } else { |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 894 | childTop = offsetY + getPaddingTop() + mInsets.top; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 895 | if (mCenterPagesVertically) { |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 896 | childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 897 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 898 | } |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 899 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 900 | if (child.getVisibility() != View.GONE) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 901 | final int childWidth = child.getMeasuredWidth(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 902 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 903 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 904 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 905 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 906 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 907 | |
| 908 | // We assume the left and right padding are equal, and hence center the pages |
| 909 | // horizontally |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 910 | int scrollOffset = (getViewportWidth() - childWidth) / 2; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 911 | mPageScrolls[i] = childLeft - scrollOffset - offsetX; |
| 912 | |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 913 | if (i != endIndex - delta) { |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 914 | childLeft += childWidth + scrollOffset; |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 915 | int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 916 | childLeft += nextScrollOffset; |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 917 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 920 | |
| 921 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 922 | setHorizontalScrollBarEnabled(false); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 923 | updateCurrentPageScroll(); |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 924 | setHorizontalScrollBarEnabled(true); |
| 925 | mFirstLayout = false; |
| 926 | } |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 927 | |
| 928 | if (childCount > 0) { |
| 929 | final int index = isLayoutRtl() ? 0 : childCount - 1; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 930 | mMaxScrollX = getScrollForPage(index); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 931 | } else { |
| 932 | mMaxScrollX = 0; |
| 933 | } |
| 934 | |
| 935 | if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && |
| 936 | !mDeferringForDelete) { |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 937 | if (mRestorePage > -1) { |
| 938 | setCurrentPage(mRestorePage); |
| 939 | mRestorePage = -1; |
| 940 | } else { |
| 941 | setCurrentPage(getNextPage()); |
| 942 | } |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 943 | } |
| 944 | mChildCountOnLastLayout = getChildCount(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 945 | |
| 946 | if (isReordering(true)) { |
| 947 | updateDragViewTranslationDuringDrag(); |
| 948 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 951 | protected void screenScrolled(int screenCenter) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 952 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 953 | |
| 954 | if (mFadeInAdjacentScreens && !isInOverscroll) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 955 | for (int i = 0; i < getChildCount(); i++) { |
| 956 | View child = getChildAt(i); |
| 957 | if (child != null) { |
| 958 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
| 959 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 960 | child.setAlpha(alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | invalidate(); |
| 964 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 967 | protected void enablePagedViewAnimations() { |
| 968 | mAllowPagedViewAnimations = true; |
| 969 | |
| 970 | } |
| 971 | protected void disablePagedViewAnimations() { |
| 972 | mAllowPagedViewAnimations = false; |
| 973 | } |
| 974 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 975 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 976 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 977 | // Update the page indicator, we don't update the page indicator as we |
| 978 | // add/remove pages |
| 979 | if (mPageIndicator != null && !isReordering(false)) { |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 980 | int pageIndex = indexOfChild(child); |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 981 | mPageIndicator.addMarker(pageIndex, |
| 982 | getPageIndicatorMarker(pageIndex), |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 983 | mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 986 | // This ensures that when children are added, they get the correct transforms / alphas |
| 987 | // in accordance with any scroll effects. |
| 988 | mForceScreenScrolled = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 989 | mRecomputePageSpacing = true; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 990 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 991 | invalidate(); |
| 992 | } |
| 993 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 994 | @Override |
| 995 | public void onChildViewRemoved(View parent, View child) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 996 | mForceScreenScrolled = true; |
| 997 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1000 | private void removeMarkerForView(int index) { |
| 1001 | // Update the page indicator, we don't update the page indicator as we |
| 1002 | // add/remove pages |
| 1003 | if (mPageIndicator != null && !isReordering(false)) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1004 | mPageIndicator.removeMarker(index, mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | @Override |
| 1009 | public void removeView(View v) { |
| 1010 | // XXX: We should find a better way to hook into this before the view |
| 1011 | // gets removed form its parent... |
| 1012 | removeMarkerForView(indexOfChild(v)); |
| 1013 | super.removeView(v); |
| 1014 | } |
| 1015 | @Override |
| 1016 | public void removeViewInLayout(View v) { |
| 1017 | // XXX: We should find a better way to hook into this before the view |
| 1018 | // gets removed form its parent... |
| 1019 | removeMarkerForView(indexOfChild(v)); |
| 1020 | super.removeViewInLayout(v); |
| 1021 | } |
| 1022 | @Override |
| 1023 | public void removeViewAt(int index) { |
| 1024 | // XXX: We should find a better way to hook into this before the view |
| 1025 | // gets removed form its parent... |
| 1026 | removeViewAt(index); |
| 1027 | super.removeViewAt(index); |
| 1028 | } |
| 1029 | @Override |
| 1030 | public void removeAllViewsInLayout() { |
| 1031 | // Update the page indicator, we don't update the page indicator as we |
| 1032 | // add/remove pages |
| 1033 | if (mPageIndicator != null) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1034 | mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | super.removeAllViewsInLayout(); |
| 1038 | } |
| 1039 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1040 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1041 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 1042 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1043 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1044 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1045 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1048 | protected void getOverviewModePages(int[] range) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1049 | range[0] = 0; |
| 1050 | range[1] = getChildCount() - 1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1053 | protected void getVisiblePages(int[] range) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1054 | final int pageCount = getChildCount(); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1055 | mTmpIntPoint[0] = mTmpIntPoint[1] = 0; |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 1056 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1057 | range[0] = -1; |
| 1058 | range[1] = -1; |
| 1059 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1060 | if (pageCount > 0) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1061 | int viewportWidth = getViewportWidth(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1062 | int curScreen = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1063 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1064 | int count = getChildCount(); |
| 1065 | for (int i = 0; i < count; i++) { |
| 1066 | View currPage = getPageAt(i); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1067 | |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1068 | mTmpIntPoint[0] = 0; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 1069 | Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1070 | if (mTmpIntPoint[0] > viewportWidth) { |
| 1071 | if (range[0] == -1) { |
| 1072 | continue; |
| 1073 | } else { |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Adam Cohen | 6a678da | 2013-09-24 10:58:01 -0700 | [diff] [blame] | 1078 | mTmpIntPoint[0] = currPage.getMeasuredWidth(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1079 | Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); |
| 1080 | if (mTmpIntPoint[0] < 0) { |
| 1081 | if (range[0] == -1) { |
| 1082 | continue; |
| 1083 | } else { |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | curScreen = i; |
| 1088 | if (range[0] < 0) { |
| 1089 | range[0] = curScreen; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1092 | |
| 1093 | range[1] = curScreen; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | range[0] = -1; |
| 1096 | range[1] = -1; |
| 1097 | } |
| 1098 | } |
| 1099 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1100 | protected boolean shouldDrawChild(View child) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 1101 | return child.getAlpha() > 0 && child.getVisibility() == VISIBLE; |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1104 | @Override |
| 1105 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1106 | int halfScreenSize = getViewportWidth() / 2; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1107 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. |
| 1108 | // Otherwise it is equal to the scaled overscroll position. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1109 | int screenCenter = mOverScrollX + halfScreenSize; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1110 | |
| 1111 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 1112 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 1113 | // set it for the next frame |
| 1114 | mForceScreenScrolled = false; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1115 | screenScrolled(screenCenter); |
| 1116 | mLastScreenCenter = screenCenter; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | // Find out which screens are visible; as an optimization we only call draw on them |
| 1120 | final int pageCount = getChildCount(); |
| 1121 | if (pageCount > 0) { |
| 1122 | getVisiblePages(mTempVisiblePagesRange); |
| 1123 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 1124 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1125 | if (leftScreen != -1 && rightScreen != -1) { |
| 1126 | final long drawingTime = getDrawingTime(); |
| 1127 | // Clip to the bounds |
| 1128 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1129 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 1130 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1131 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1132 | // Draw all the children, leaving the drag view for last |
| 1133 | for (int i = pageCount - 1; i >= 0; i--) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1134 | final View v = getPageAt(i); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1135 | if (v == mDragView) continue; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 1136 | if (mForceDrawAllChildrenNextFrame || |
| 1137 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1138 | drawChild(canvas, v, drawingTime); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1139 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1140 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1141 | // Draw the drag view on top (if there is one) |
| 1142 | if (mDragView != null) { |
| 1143 | drawChild(canvas, mDragView, drawingTime); |
| 1144 | } |
| 1145 | |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 1146 | mForceDrawAllChildrenNextFrame = false; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1147 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1148 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1149 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | @Override |
| 1153 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1154 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1155 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 1156 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | @Override |
| 1163 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1164 | int focusablePage; |
| 1165 | if (mNextPage != INVALID_PAGE) { |
| 1166 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1167 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1168 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1169 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1170 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1171 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1172 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1173 | } |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | @Override |
| 1178 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1179 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1180 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1181 | if (getCurrentPage() > 0) { |
| 1182 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1183 | return true; |
| 1184 | } |
| 1185 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1186 | if (getCurrentPage() < getPageCount() - 1) { |
| 1187 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1188 | return true; |
| 1189 | } |
| 1190 | } |
| 1191 | return super.dispatchUnhandledMove(focused, direction); |
| 1192 | } |
| 1193 | |
| 1194 | @Override |
| 1195 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1196 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1197 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1198 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1199 | } |
| 1200 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1201 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1202 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1203 | } |
| 1204 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1205 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1206 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * If one of our descendant views decides that it could be focused now, only |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1213 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1214 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1215 | * This happens when live folders requery, and if they're off page, they |
| 1216 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1217 | */ |
| 1218 | @Override |
| 1219 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1220 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1221 | View v = focused; |
| 1222 | while (true) { |
| 1223 | if (v == current) { |
| 1224 | super.focusableViewAvailable(focused); |
| 1225 | return; |
| 1226 | } |
| 1227 | if (v == this) { |
| 1228 | return; |
| 1229 | } |
| 1230 | ViewParent parent = v.getParent(); |
| 1231 | if (parent instanceof View) { |
| 1232 | v = (View)v.getParent(); |
| 1233 | } else { |
| 1234 | return; |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * {@inheritDoc} |
| 1241 | */ |
| 1242 | @Override |
| 1243 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1244 | if (disallowIntercept) { |
| 1245 | // We need to make sure to cancel our long press if |
| 1246 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1247 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1248 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1249 | } |
| 1250 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1251 | } |
| 1252 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1253 | /** |
| 1254 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1255 | */ |
| 1256 | protected boolean hitsPreviousPage(float x, float y) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1257 | int offset = (getViewportWidth() - getChildWidth(mCurrentPage)) / 2; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1258 | if (isLayoutRtl()) { |
| 1259 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1260 | offset + mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1261 | } |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1262 | return (x < getViewportOffsetX() + offset - mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1267 | */ |
| 1268 | protected boolean hitsNextPage(float x, float y) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1269 | int offset = (getViewportWidth() - getChildWidth(mCurrentPage)) / 2; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1270 | if (isLayoutRtl()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1271 | return (x < getViewportOffsetX() + offset - mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1272 | } |
| 1273 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1274 | offset + mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1275 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1276 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1277 | /** Returns whether x and y originated within the buffered viewport */ |
| 1278 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
| 1279 | mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
| 1280 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
| 1281 | return mTmpRect.contains(x, y); |
| 1282 | } |
| 1283 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1284 | @Override |
| 1285 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1286 | if (DISABLE_TOUCH_INTERACTION) { |
| 1287 | return false; |
| 1288 | } |
| 1289 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1290 | /* |
| 1291 | * This method JUST determines whether we want to intercept the motion. |
| 1292 | * If we return true, onTouchEvent will be called and we do the actual |
| 1293 | * scrolling there. |
| 1294 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1295 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1296 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1297 | // Skip touch handling if there are no pages to swipe |
| 1298 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1299 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1300 | /* |
| 1301 | * Shortcut the most recurring case: the user is in the dragging |
| 1302 | * state and he is moving his finger. We want to intercept this |
| 1303 | * motion. |
| 1304 | */ |
| 1305 | final int action = ev.getAction(); |
| 1306 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1307 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1311 | switch (action & MotionEvent.ACTION_MASK) { |
| 1312 | case MotionEvent.ACTION_MOVE: { |
| 1313 | /* |
| 1314 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1315 | * whether the user has moved far enough from his original down touch. |
| 1316 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1317 | if (mActivePointerId != INVALID_POINTER) { |
| 1318 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1319 | } |
| 1320 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1321 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1322 | // i.e. fall through to the next case (don't break) |
| 1323 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1324 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1325 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | case MotionEvent.ACTION_DOWN: { |
| 1329 | final float x = ev.getX(); |
| 1330 | final float y = ev.getY(); |
| 1331 | // Remember location of down touch |
| 1332 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1333 | mDownMotionY = y; |
| 1334 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1335 | mLastMotionX = x; |
| 1336 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1337 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1338 | mParentDownMotionX = p[0]; |
| 1339 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1340 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1341 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1342 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1343 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1344 | /* |
| 1345 | * If being flinged and user touches the screen, initiate drag; |
| 1346 | * otherwise don't. mScroller.isFinished should be false when |
| 1347 | * being flinged. |
| 1348 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1349 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1350 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 1351 | if (finishedScrolling) { |
| 1352 | mTouchState = TOUCH_STATE_REST; |
| 1353 | mScroller.abortAnimation(); |
| 1354 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1355 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1356 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1357 | } else { |
| 1358 | mTouchState = TOUCH_STATE_REST; |
| 1359 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1360 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1361 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1362 | // check if this can be the beginning of a tap on the side of the pages |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1363 | // to scroll the current page |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1364 | if (!DISABLE_TOUCH_SIDE_PAGES) { |
| 1365 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
| 1366 | if (getChildCount() > 0) { |
| 1367 | if (hitsPreviousPage(x, y)) { |
| 1368 | mTouchState = TOUCH_STATE_PREV_PAGE; |
| 1369 | } else if (hitsNextPage(x, y)) { |
| 1370 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1371 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | break; |
| 1376 | } |
| 1377 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1378 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1379 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1380 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1381 | break; |
| 1382 | |
| 1383 | case MotionEvent.ACTION_POINTER_UP: |
| 1384 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1385 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1386 | break; |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * The only time we want to intercept motion events is if we are in the |
| 1391 | * drag mode. |
| 1392 | */ |
| 1393 | return mTouchState != TOUCH_STATE_REST; |
| 1394 | } |
| 1395 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1396 | protected void determineScrollingStart(MotionEvent ev) { |
| 1397 | determineScrollingStart(ev, 1.0f); |
| 1398 | } |
| 1399 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1400 | /* |
| 1401 | * Determines if we should change the touch state to start scrolling after the |
| 1402 | * user moves their touch point too far. |
| 1403 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1404 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1405 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1406 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1407 | if (pointerIndex == -1) return; |
| 1408 | |
| 1409 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1410 | final float x = ev.getX(pointerIndex); |
| 1411 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1412 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1413 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1414 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1415 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1416 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1417 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1418 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1419 | boolean xMoved = xDiff > touchSlop; |
| 1420 | boolean yMoved = yDiff > touchSlop; |
| 1421 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1422 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1423 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1424 | // Scroll if the user moved far enough along the X axis |
| 1425 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1426 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1427 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1428 | mLastMotionXRemainder = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1429 | mTouchX = getViewportOffsetX() + getScrollX(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1430 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1431 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1432 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1436 | protected float getMaxScrollProgress() { |
| 1437 | return 1.0f; |
| 1438 | } |
| 1439 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1440 | protected void cancelCurrentPageLongPress() { |
| 1441 | if (mAllowLongPress) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1442 | //mAllowLongPress = false; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1443 | // Try canceling the long press. It could also have been scheduled |
| 1444 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1445 | // everything |
| 1446 | final View currentPage = getPageAt(mCurrentPage); |
| 1447 | if (currentPage != null) { |
| 1448 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1453 | protected float getBoundedScrollProgress(int screenCenter, View v, int page) { |
| 1454 | final int halfScreenSize = getViewportWidth() / 2; |
| 1455 | |
| 1456 | screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter); |
| 1457 | screenCenter = Math.max(halfScreenSize, screenCenter); |
| 1458 | |
| 1459 | return getScrollProgress(screenCenter, v, page); |
| 1460 | } |
| 1461 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1462 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1463 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1464 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1465 | int totalDistance = v.getMeasuredWidth() + mPageSpacing; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1466 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1467 | |
| 1468 | float scrollProgress = delta / (totalDistance * 1.0f); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1469 | scrollProgress = Math.min(scrollProgress, getMaxScrollProgress()); |
| 1470 | scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1471 | return scrollProgress; |
| 1472 | } |
| 1473 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1474 | public int getScrollForPage(int index) { |
| 1475 | if (mPageScrolls == null || index >= mPageScrolls.length) { |
| 1476 | return 0; |
| 1477 | } else { |
| 1478 | return mPageScrolls[index]; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1482 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1483 | // as the user pulls further and further from the bounds |
| 1484 | private float overScrollInfluenceCurve(float f) { |
| 1485 | f -= 1.0f; |
| 1486 | return f * f * f + 1.0f; |
| 1487 | } |
| 1488 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1489 | protected void acceleratedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1490 | int screenSize = getViewportWidth(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1491 | |
| 1492 | // We want to reach the max over scroll effect when the user has |
| 1493 | // over scrolled half the size of the screen |
| 1494 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1495 | |
| 1496 | if (f == 0) return; |
| 1497 | |
| 1498 | // Clamp this factor, f, to -1 < f < 1 |
| 1499 | if (Math.abs(f) >= 1) { |
| 1500 | f /= Math.abs(f); |
| 1501 | } |
| 1502 | |
| 1503 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1504 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1505 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1506 | super.scrollTo(0, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1507 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1508 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1509 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1510 | } |
| 1511 | invalidate(); |
| 1512 | } |
| 1513 | |
| 1514 | protected void dampedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1515 | int screenSize = getViewportWidth(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1516 | |
| 1517 | float f = (amount / screenSize); |
| 1518 | |
| 1519 | if (f == 0) return; |
| 1520 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1521 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 1522 | // Clamp this factor, f, to -1 < f < 1 |
| 1523 | if (Math.abs(f) >= 1) { |
| 1524 | f /= Math.abs(f); |
| 1525 | } |
| 1526 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1527 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1528 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1529 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1530 | super.scrollTo(0, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1531 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1532 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1533 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1534 | } |
| 1535 | invalidate(); |
| 1536 | } |
| 1537 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1538 | protected void overScroll(float amount) { |
| 1539 | dampedOverScroll(amount); |
| 1540 | } |
| 1541 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1542 | protected float maxOverScroll() { |
| 1543 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1544 | // exceed). Used to find out how much extra wallpaper we need for the over scroll effect |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1545 | float f = 1.0f; |
| 1546 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1547 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1548 | } |
| 1549 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1550 | protected void enableFreeScroll() { |
| 1551 | setEnableFreeScroll(true, -1); |
| 1552 | } |
| 1553 | |
| 1554 | protected void disableFreeScroll(int snapPage) { |
| 1555 | setEnableFreeScroll(false, snapPage); |
| 1556 | } |
| 1557 | |
| 1558 | private void setEnableFreeScroll(boolean freeScroll, int snapPage) { |
| 1559 | mFreeScroll = freeScroll; |
| 1560 | |
| 1561 | if (snapPage == -1) { |
| 1562 | snapPage = getPageNearestToCenterOfScreen(); |
| 1563 | } |
| 1564 | |
| 1565 | getOverviewModePages(mTempVisiblePagesRange); |
| 1566 | if (!mFreeScroll) { |
| 1567 | snapToPage(snapPage); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1568 | } else { |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1569 | if (isLayoutRtl()) { |
| 1570 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1571 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1572 | } else { |
| 1573 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1574 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1575 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1576 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1577 | if (getCurrentPage() < mTempVisiblePagesRange[0]) { |
| 1578 | setCurrentPage(mTempVisiblePagesRange[0]); |
| 1579 | } else if (getCurrentPage() > mTempVisiblePagesRange[1]) { |
| 1580 | setCurrentPage(mTempVisiblePagesRange[1]); |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | setEnableOverscroll(!freeScroll); |
| 1585 | } |
| 1586 | |
| 1587 | private void setEnableOverscroll(boolean enable) { |
| 1588 | mAllowOverScroll = enable; |
| 1589 | } |
| 1590 | |
| 1591 | int getNearestHoverOverPageIndex() { |
| 1592 | if (mDragView != null) { |
| 1593 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1594 | + mDragView.getTranslationX()); |
| 1595 | getOverviewModePages(mTempVisiblePagesRange); |
| 1596 | int minDistance = Integer.MAX_VALUE; |
| 1597 | int minIndex = indexOfChild(mDragView); |
| 1598 | for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) { |
| 1599 | View page = getPageAt(i); |
| 1600 | int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2); |
| 1601 | int d = Math.abs(dragX - pageX); |
| 1602 | if (d < minDistance) { |
| 1603 | minIndex = i; |
| 1604 | minDistance = d; |
| 1605 | } |
| 1606 | } |
| 1607 | return minIndex; |
| 1608 | } |
| 1609 | return -1; |
| 1610 | } |
| 1611 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1612 | @Override |
| 1613 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1614 | if (DISABLE_TOUCH_INTERACTION) { |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1618 | super.onTouchEvent(ev); |
| 1619 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1620 | // Skip touch handling if there are no pages to swipe |
| 1621 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1622 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1623 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1624 | |
| 1625 | final int action = ev.getAction(); |
| 1626 | |
| 1627 | switch (action & MotionEvent.ACTION_MASK) { |
| 1628 | case MotionEvent.ACTION_DOWN: |
| 1629 | /* |
| 1630 | * If being flinged and user touches, stop the fling. isFinished |
| 1631 | * will be false if being flinged. |
| 1632 | */ |
| 1633 | if (!mScroller.isFinished()) { |
| 1634 | mScroller.abortAnimation(); |
| 1635 | } |
| 1636 | |
| 1637 | // Remember where the motion event started |
| 1638 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1639 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1640 | mDownScrollX = getScrollX(); |
| 1641 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1642 | mParentDownMotionX = p[0]; |
| 1643 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1644 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1645 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1646 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1647 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1648 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1649 | pageBeginMoving(); |
| 1650 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1651 | break; |
| 1652 | |
| 1653 | case MotionEvent.ACTION_MOVE: |
| 1654 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1655 | // Scroll to follow the motion event |
| 1656 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1657 | |
| 1658 | if (pointerIndex == -1) return true; |
| 1659 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1660 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1661 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1662 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1663 | mTotalMotionX += Math.abs(deltaX); |
| 1664 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1665 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1666 | // keep the remainder because we are actually testing if we've moved from the last |
| 1667 | // scrolled position (which is discrete). |
| 1668 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1669 | mTouchX += deltaX; |
| 1670 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1671 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1672 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1673 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1674 | } else { |
| 1675 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1676 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1677 | mLastMotionX = x; |
| 1678 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1679 | } else { |
| 1680 | awakenScrollBars(); |
| 1681 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1682 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1683 | // Update the last motion position |
| 1684 | mLastMotionX = ev.getX(); |
| 1685 | mLastMotionY = ev.getY(); |
| 1686 | |
| 1687 | // Update the parent down so that our zoom animations take this new movement into |
| 1688 | // account |
| 1689 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1690 | mParentDownMotionX = pt[0]; |
| 1691 | mParentDownMotionY = pt[1]; |
| 1692 | updateDragViewTranslationDuringDrag(); |
| 1693 | |
| 1694 | // Find the closest page to the touch point |
| 1695 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1696 | |
| 1697 | // Change the drag view if we are hovering over the drop target |
| 1698 | boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget( |
| 1699 | (int) mParentDownMotionX, (int) mParentDownMotionY); |
| 1700 | setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete); |
| 1701 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1702 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1703 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1704 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1705 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1706 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1707 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
| 1708 | if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) && |
| 1709 | !isHoveringOverDelete) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1710 | mTempVisiblePagesRange[0] = 0; |
| 1711 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1712 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1713 | if (mTempVisiblePagesRange[0] <= pageUnderPointIndex && |
| 1714 | pageUnderPointIndex <= mTempVisiblePagesRange[1] && |
| 1715 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1716 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1717 | mSidePageHoverRunnable = new Runnable() { |
| 1718 | @Override |
| 1719 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1720 | // Setup the scroll to the correct page before we swap the views |
| 1721 | snapToPage(pageUnderPointIndex); |
| 1722 | |
| 1723 | // For each of the pages between the paged view and the drag view, |
| 1724 | // animate them from the previous position to the new position in |
| 1725 | // the layout (as a result of the drag view moving in the layout) |
| 1726 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1727 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1728 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1729 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1730 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1731 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1732 | View v = getChildAt(i); |
| 1733 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1734 | // drag view all subsequent views to pageUnderPointIndex will |
| 1735 | // shift down. |
| 1736 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1737 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1738 | |
| 1739 | // Animate the view translation from its old position to its new |
| 1740 | // position |
| 1741 | AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY); |
| 1742 | if (anim != null) { |
| 1743 | anim.cancel(); |
| 1744 | } |
| 1745 | |
| 1746 | v.setTranslationX(oldX - newX); |
| 1747 | anim = new AnimatorSet(); |
| 1748 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
| 1749 | anim.playTogether( |
| 1750 | ObjectAnimator.ofFloat(v, "translationX", 0f)); |
| 1751 | anim.start(); |
| 1752 | v.setTag(anim); |
| 1753 | } |
| 1754 | |
| 1755 | removeView(mDragView); |
| 1756 | onRemoveView(mDragView, false); |
| 1757 | addView(mDragView, pageUnderPointIndex); |
| 1758 | onAddView(mDragView, pageUnderPointIndex); |
| 1759 | mSidePageHoverIndex = -1; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1760 | mPageIndicator.setActiveMarker(getNextPage()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1761 | } |
| 1762 | }; |
| 1763 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1764 | } |
| 1765 | } else { |
| 1766 | removeCallbacks(mSidePageHoverRunnable); |
| 1767 | mSidePageHoverIndex = -1; |
| 1768 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1769 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1770 | determineScrollingStart(ev); |
| 1771 | } |
| 1772 | break; |
| 1773 | |
| 1774 | case MotionEvent.ACTION_UP: |
| 1775 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1776 | final int activePointerId = mActivePointerId; |
| 1777 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1778 | final float x = ev.getX(pointerIndex); |
| 1779 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1780 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1781 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1782 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1783 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1784 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1785 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1786 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1787 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1788 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1789 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1790 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1791 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1792 | if (!mFreeScroll) { |
| 1793 | // In the case that the page is moved far to one direction and then is flung |
| 1794 | // in the opposite direction, we use a threshold to determine whether we should |
| 1795 | // just return to the starting page, or if we should skip one further. |
| 1796 | boolean returnToOriginalPage = false; |
| 1797 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1798 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1799 | returnToOriginalPage = true; |
| 1800 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1801 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1802 | int finalPage; |
| 1803 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1804 | // test for a large move if a fling has been registered. That is, a large |
| 1805 | // move to the left and fling to the right will register as a fling to the right. |
| 1806 | final boolean isRtl = isLayoutRtl(); |
| 1807 | boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0; |
| 1808 | boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0; |
| 1809 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1810 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1811 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1812 | snapToPageWithVelocity(finalPage, velocityX); |
| 1813 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1814 | (isFling && isVelocityXLeft)) && |
| 1815 | mCurrentPage < getChildCount() - 1) { |
| 1816 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1817 | snapToPageWithVelocity(finalPage, velocityX); |
| 1818 | } else { |
| 1819 | snapToDestination(); |
| 1820 | } } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1821 | // at this point we have not moved beyond the touch slop |
| 1822 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1823 | // we can just page |
| 1824 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1825 | if (nextPage != mCurrentPage) { |
| 1826 | snapToPage(nextPage); |
| 1827 | } else { |
| 1828 | snapToDestination(); |
| 1829 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1830 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1831 | if (!mScroller.isFinished()) { |
| 1832 | mScroller.abortAnimation(); |
| 1833 | } |
| 1834 | |
| 1835 | float scaleX = getScaleX(); |
| 1836 | int vX = (int) (-velocityX * scaleX); |
| 1837 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1838 | |
| 1839 | mScroller.fling(initialScrollX, |
| 1840 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
| 1841 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1842 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1843 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1844 | // at this point we have not moved beyond the touch slop |
| 1845 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1846 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1847 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1848 | if (nextPage != mCurrentPage) { |
| 1849 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1850 | } else { |
| 1851 | snapToDestination(); |
| 1852 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1853 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1854 | // Update the last motion position |
| 1855 | mLastMotionX = ev.getX(); |
| 1856 | mLastMotionY = ev.getY(); |
| 1857 | |
| 1858 | // Update the parent down so that our zoom animations take this new movement into |
| 1859 | // account |
| 1860 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1861 | mParentDownMotionX = pt[0]; |
| 1862 | mParentDownMotionY = pt[1]; |
| 1863 | updateDragViewTranslationDuringDrag(); |
| 1864 | boolean handledFling = false; |
| 1865 | if (!DISABLE_FLING_TO_DELETE) { |
| 1866 | // Check the velocity and see if we are flinging-to-delete |
| 1867 | PointF flingToDeleteVector = isFlingingToDelete(); |
| 1868 | if (flingToDeleteVector != null) { |
| 1869 | onFlingToDelete(flingToDeleteVector); |
| 1870 | handledFling = true; |
| 1871 | } |
| 1872 | } |
| 1873 | if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX, |
| 1874 | (int) mParentDownMotionY)) { |
| 1875 | onDropToDelete(); |
| 1876 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1877 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1878 | if (!mCancelTap) { |
| 1879 | onUnhandledTap(ev); |
| 1880 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1881 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1882 | |
| 1883 | // Remove the callback to wait for the side page hover timeout |
| 1884 | removeCallbacks(mSidePageHoverRunnable); |
| 1885 | // End any intermediate reordering states |
| 1886 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1887 | break; |
| 1888 | |
| 1889 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1890 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1891 | snapToDestination(); |
| 1892 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1893 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1894 | break; |
| 1895 | |
| 1896 | case MotionEvent.ACTION_POINTER_UP: |
| 1897 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1898 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1899 | break; |
| 1900 | } |
| 1901 | |
| 1902 | return true; |
| 1903 | } |
| 1904 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1905 | public void onFlingToDelete(View v) {} |
| 1906 | public void onRemoveView(View v, boolean deletePermanently) {} |
| 1907 | public void onRemoveViewAnimationCompleted() {} |
| 1908 | public void onAddView(View v, int index) {} |
| 1909 | |
| 1910 | private void resetTouchState() { |
| 1911 | releaseVelocityTracker(); |
| 1912 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1913 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1914 | mTouchState = TOUCH_STATE_REST; |
| 1915 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1918 | protected void onUnhandledTap(MotionEvent ev) { |
| 1919 | ((Launcher) getContext()).onClick(this); |
| 1920 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1921 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1922 | @Override |
| 1923 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1924 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1925 | switch (event.getAction()) { |
| 1926 | case MotionEvent.ACTION_SCROLL: { |
| 1927 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1928 | final float vscroll; |
| 1929 | final float hscroll; |
| 1930 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1931 | vscroll = 0; |
| 1932 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1933 | } else { |
| 1934 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1935 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1936 | } |
| 1937 | if (hscroll != 0 || vscroll != 0) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1938 | boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0) |
| 1939 | : (hscroll > 0 || vscroll > 0); |
| 1940 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1941 | scrollRight(); |
| 1942 | } else { |
| 1943 | scrollLeft(); |
| 1944 | } |
| 1945 | return true; |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | return super.onGenericMotionEvent(event); |
| 1951 | } |
| 1952 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1953 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1954 | if (mVelocityTracker == null) { |
| 1955 | mVelocityTracker = VelocityTracker.obtain(); |
| 1956 | } |
| 1957 | mVelocityTracker.addMovement(ev); |
| 1958 | } |
| 1959 | |
| 1960 | private void releaseVelocityTracker() { |
| 1961 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1962 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1963 | mVelocityTracker.recycle(); |
| 1964 | mVelocityTracker = null; |
| 1965 | } |
| 1966 | } |
| 1967 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1968 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1969 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1970 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1971 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1972 | if (pointerId == mActivePointerId) { |
| 1973 | // This was our active pointer going up. Choose a new |
| 1974 | // active pointer and adjust accordingly. |
| 1975 | // TODO: Make this decision more intelligent. |
| 1976 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1977 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1978 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1979 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1980 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1981 | if (mVelocityTracker != null) { |
| 1982 | mVelocityTracker.clear(); |
| 1983 | } |
| 1984 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1987 | @Override |
| 1988 | public void requestChildFocus(View child, View focused) { |
| 1989 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1990 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1991 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1992 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1996 | protected int getChildWidth(int index) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1997 | return getPageAt(index).getMeasuredWidth(); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2000 | int getPageNearestToPoint(float x) { |
| 2001 | int index = 0; |
| 2002 | for (int i = 0; i < getChildCount(); ++i) { |
| 2003 | if (x < getChildAt(i).getRight() - getScrollX()) { |
| 2004 | return index; |
| 2005 | } else { |
| 2006 | index++; |
| 2007 | } |
| 2008 | } |
| 2009 | return Math.min(index, getChildCount() - 1); |
| 2010 | } |
| 2011 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 2012 | int getPageNearestToCenterOfScreen() { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2013 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2014 | int minDistanceFromScreenCenterIndex = -1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2015 | int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2016 | final int childCount = getChildCount(); |
| 2017 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2018 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 2019 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2020 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2021 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2022 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 2023 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 2024 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 2025 | minDistanceFromScreenCenterIndex = i; |
| 2026 | } |
| 2027 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 2028 | return minDistanceFromScreenCenterIndex; |
| 2029 | } |
| 2030 | |
| 2031 | protected void snapToDestination() { |
| 2032 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2035 | private static class ScrollInterpolator implements Interpolator { |
| 2036 | public ScrollInterpolator() { |
| 2037 | } |
| 2038 | |
| 2039 | public float getInterpolation(float t) { |
| 2040 | t -= 1.0f; |
| 2041 | return t*t*t*t*t + 1; |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | // We want the duration of the page snap animation to be influenced by the distance that |
| 2046 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 2047 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 2048 | // of travel has on the overall snap duration. |
| 2049 | float distanceInfluenceForSnapDuration(float f) { |
| 2050 | f -= 0.5f; // center the values about 0. |
| 2051 | f *= 0.3f * Math.PI / 2.0f; |
| 2052 | return (float) Math.sin(f); |
| 2053 | } |
| 2054 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2055 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2056 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2057 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2058 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 2059 | final int newX = getScrollForPage(whichPage); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2060 | int delta = newX - mUnboundedScrollX; |
| 2061 | int duration = 0; |
| 2062 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 2063 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2064 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 2065 | // as opposed to an apparent physical response to flinging |
| 2066 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 2067 | return; |
| 2068 | } |
| 2069 | |
| 2070 | // Here we compute a "distance" that will be used in the computation of the overall |
| 2071 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 2072 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 2073 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 2074 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2075 | float distance = halfScreenSize + halfScreenSize * |
| 2076 | distanceInfluenceForSnapDuration(distanceRatio); |
| 2077 | |
| 2078 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 2079 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2080 | |
| 2081 | // we want the page's snap velocity to approximately match the velocity at which the |
| 2082 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 2083 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 2084 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2085 | |
| 2086 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 2090 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2093 | protected void snapToPageImmediately(int whichPage) { |
| 2094 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true); |
| 2095 | } |
| 2096 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2097 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2098 | snapToPage(whichPage, duration, false); |
| 2099 | } |
| 2100 | |
| 2101 | protected void snapToPage(int whichPage, int duration, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2102 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2103 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 2104 | int newX = getScrollForPage(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 2105 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2106 | final int delta = newX - sX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2107 | snapToPage(whichPage, delta, duration, immediate); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2111 | snapToPage(whichPage, delta, duration, false); |
| 2112 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2113 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2114 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate) { |
| 2115 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2116 | View focusedChild = getFocusedChild(); |
| 2117 | if (focusedChild != null && whichPage != mCurrentPage && |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2118 | focusedChild == getPageAt(mCurrentPage)) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2119 | focusedChild.clearFocus(); |
| 2120 | } |
| 2121 | |
| 2122 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2123 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2124 | if (immediate) { |
| 2125 | duration = 0; |
| 2126 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2127 | duration = Math.abs(delta); |
| 2128 | } |
| 2129 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2130 | if (!mScroller.isFinished()) { |
| 2131 | mScroller.abortAnimation(); |
| 2132 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 2133 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2134 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2135 | notifyPageSwitchListener(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2136 | |
| 2137 | // Trigger a compute() to finish switching pages if necessary |
| 2138 | if (immediate) { |
| 2139 | computeScroll(); |
| 2140 | } |
| 2141 | |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 2142 | // Defer loading associated pages until the scroll settles |
| 2143 | mDeferLoadAssociatedPagesUntilScrollCompletes = true; |
| 2144 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2145 | mForceScreenScrolled = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2146 | invalidate(); |
| 2147 | } |
| 2148 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2149 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2150 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2154 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2157 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2158 | int result = -1; |
| 2159 | if (v != null) { |
| 2160 | ViewParent vp = v.getParent(); |
| 2161 | int count = getChildCount(); |
| 2162 | for (int i = 0; i < count; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2163 | if (vp == getPageAt(i)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2164 | return i; |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | return result; |
| 2169 | } |
| 2170 | |
| 2171 | /** |
| 2172 | * @return True is long presses are still allowed for the current touch |
| 2173 | */ |
| 2174 | public boolean allowLongPress() { |
| 2175 | return mAllowLongPress; |
| 2176 | } |
| 2177 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2178 | @Override |
| 2179 | public boolean performLongClick() { |
| 2180 | mCancelTap = true; |
| 2181 | return super.performLongClick(); |
| 2182 | } |
| 2183 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2184 | /** |
| 2185 | * Set true to allow long-press events to be triggered, usually checked by |
| 2186 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 2187 | */ |
| 2188 | public void setAllowLongPress(boolean allowLongPress) { |
| 2189 | mAllowLongPress = allowLongPress; |
| 2190 | } |
| 2191 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2192 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2193 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2194 | |
| 2195 | SavedState(Parcelable superState) { |
| 2196 | super(superState); |
| 2197 | } |
| 2198 | |
| 2199 | private SavedState(Parcel in) { |
| 2200 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2201 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | @Override |
| 2205 | public void writeToParcel(Parcel out, int flags) { |
| 2206 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2207 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 2211 | new Parcelable.Creator<SavedState>() { |
| 2212 | public SavedState createFromParcel(Parcel in) { |
| 2213 | return new SavedState(in); |
| 2214 | } |
| 2215 | |
| 2216 | public SavedState[] newArray(int size) { |
| 2217 | return new SavedState[size]; |
| 2218 | } |
| 2219 | }; |
| 2220 | } |
| 2221 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2222 | protected void loadAssociatedPages(int page) { |
| 2223 | loadAssociatedPages(page, false); |
| 2224 | } |
| 2225 | protected void loadAssociatedPages(int page, boolean immediateAndOnly) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2226 | if (mContentIsRefreshable) { |
| 2227 | final int count = getChildCount(); |
| 2228 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 2229 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 2230 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2231 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 2232 | + upperPageBound); |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 2233 | // First, clear any pages that should no longer be loaded |
| 2234 | for (int i = 0; i < count; ++i) { |
| 2235 | Page layout = (Page) getPageAt(i); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2236 | if ((i < lowerPageBound) || (i > upperPageBound)) { |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 2237 | if (layout.getPageChildCount() > 0) { |
| 2238 | layout.removeAllViewsOnPage(); |
| 2239 | } |
| 2240 | mDirtyPageContent.set(i, true); |
| 2241 | } |
| 2242 | } |
| 2243 | // Next, load any new pages |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2244 | for (int i = 0; i < count; ++i) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2245 | if ((i != page) && immediateAndOnly) { |
| 2246 | continue; |
| 2247 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2248 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 2249 | if (mDirtyPageContent.get(i)) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2250 | syncPageItems(i, (i == page) && immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2251 | mDirtyPageContent.set(i, false); |
| 2252 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2253 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2254 | } |
| 2255 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 2259 | protected int getAssociatedLowerPageBound(int page) { |
| 2260 | return Math.max(0, page - 1); |
| 2261 | } |
| 2262 | protected int getAssociatedUpperPageBound(int page) { |
| 2263 | final int count = getChildCount(); |
| 2264 | return Math.min(page + 1, count - 1); |
| 2265 | } |
| 2266 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2267 | /** |
| 2268 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 2269 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 2270 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 2271 | * and therefore, individual page items do not need to be updated in this method. |
| 2272 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2273 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2274 | |
| 2275 | /** |
| 2276 | * This method is called to synchronize the items that are on a particular page. If views on |
| 2277 | * the page can be reused, then they should be updated within this method. |
| 2278 | */ |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2279 | public abstract void syncPageItems(int page, boolean immediate); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2280 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 2281 | protected void invalidatePageData() { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2282 | invalidatePageData(-1, false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2283 | } |
| 2284 | protected void invalidatePageData(int currentPage) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2285 | invalidatePageData(currentPage, false); |
| 2286 | } |
| 2287 | protected void invalidatePageData(int currentPage, boolean immediateAndOnly) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 2288 | if (!mIsDataReady) { |
| 2289 | return; |
| 2290 | } |
| 2291 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2292 | if (mContentIsRefreshable) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 2293 | // Force all scrolling-related behavior to end |
| 2294 | mScroller.forceFinished(true); |
| 2295 | mNextPage = INVALID_PAGE; |
| 2296 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2297 | // Update all the pages |
| 2298 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2299 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2300 | // We must force a measure after we've loaded the pages to update the content width and |
| 2301 | // to determine the full scroll width |
| 2302 | measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 2303 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); |
| 2304 | |
| 2305 | // Set a new page as the current page if necessary |
| 2306 | if (currentPage > -1) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 2307 | setCurrentPage(Math.min(getPageCount() - 1, currentPage)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2310 | // Mark each of the pages as dirty |
| 2311 | final int count = getChildCount(); |
| 2312 | mDirtyPageContent.clear(); |
| 2313 | for (int i = 0; i < count; ++i) { |
| 2314 | mDirtyPageContent.add(true); |
| 2315 | } |
| 2316 | |
| 2317 | // Load any pages that are necessary for the current window of views |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2318 | loadAssociatedPages(mCurrentPage, immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2319 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2320 | } |
Adam Cohen | 0655904 | 2013-09-29 18:30:56 -0700 | [diff] [blame] | 2321 | if (isPageMoving()) { |
| 2322 | // If the page is moving, then snap it to the final position to ensure we don't get |
| 2323 | // stuck between pages |
| 2324 | snapToDestination(); |
| 2325 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2326 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 2327 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2328 | // Animate the drag view back to the original position |
| 2329 | void animateDragViewToOriginalPosition() { |
| 2330 | if (mDragView != null) { |
| 2331 | AnimatorSet anim = new AnimatorSet(); |
| 2332 | anim.setDuration(REORDERING_DROP_REPOSITION_DURATION); |
| 2333 | anim.playTogether( |
| 2334 | ObjectAnimator.ofFloat(mDragView, "translationX", 0f), |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2335 | ObjectAnimator.ofFloat(mDragView, "translationY", 0f), |
| 2336 | ObjectAnimator.ofFloat(mDragView, "scaleX", 1f), |
| 2337 | ObjectAnimator.ofFloat(mDragView, "scaleY", 1f)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2338 | anim.addListener(new AnimatorListenerAdapter() { |
| 2339 | @Override |
| 2340 | public void onAnimationEnd(Animator animation) { |
| 2341 | onPostReorderingAnimationCompleted(); |
| 2342 | } |
| 2343 | }); |
| 2344 | anim.start(); |
| 2345 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2348 | protected void onStartReordering() { |
| 2349 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 2350 | mTouchState = TOUCH_STATE_REORDERING; |
| 2351 | mIsReordering = true; |
| 2352 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2353 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 2354 | // is always drawn on top |
| 2355 | invalidate(); |
| 2356 | } |
| 2357 | |
| 2358 | private void onPostReorderingAnimationCompleted() { |
| 2359 | // Trigger the callback when reordering has settled |
| 2360 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 2361 | if (mPostReorderingPreZoomInRunnable != null && |
| 2362 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 2363 | mPostReorderingPreZoomInRunnable.run(); |
| 2364 | mPostReorderingPreZoomInRunnable = null; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | protected void onEndReordering() { |
| 2369 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2372 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2373 | int dragViewIndex = indexOfChild(v); |
| 2374 | |
| 2375 | if (mTouchState != TOUCH_STATE_REST) return false; |
| 2376 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2377 | mTempVisiblePagesRange[0] = 0; |
| 2378 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2379 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2380 | mReorderingStarted = true; |
| 2381 | |
| 2382 | // Check if we are within the reordering range |
| 2383 | if (mTempVisiblePagesRange[0] <= dragViewIndex && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2384 | dragViewIndex <= mTempVisiblePagesRange[1]) { |
| 2385 | // Find the drag view under the pointer |
| 2386 | mDragView = getChildAt(dragViewIndex); |
| 2387 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 2388 | mDragViewBaselineLeft = mDragView.getLeft(); |
| 2389 | disableFreeScroll(-1); |
| 2390 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2391 | return true; |
| 2392 | } |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
| 2396 | boolean isReordering(boolean testTouchState) { |
| 2397 | boolean state = mIsReordering; |
| 2398 | if (testTouchState) { |
| 2399 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2400 | } |
| 2401 | return state; |
| 2402 | } |
| 2403 | void endReordering() { |
| 2404 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2405 | // In that case, we don't want to do anything. |
| 2406 | if (!mReorderingStarted) return; |
| 2407 | mReorderingStarted = false; |
| 2408 | |
| 2409 | // If we haven't flung-to-delete the current child, then we just animate the drag view |
| 2410 | // back into position |
| 2411 | final Runnable onCompleteRunnable = new Runnable() { |
| 2412 | @Override |
| 2413 | public void run() { |
| 2414 | onEndReordering(); |
| 2415 | } |
| 2416 | }; |
| 2417 | if (!mDeferringForDelete) { |
| 2418 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2419 | public void run() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2420 | onCompleteRunnable.run(); |
| 2421 | enableFreeScroll(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2422 | }; |
| 2423 | }; |
| 2424 | |
| 2425 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2426 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2427 | // Snap to the current page |
| 2428 | snapToPage(indexOfChild(mDragView), 0); |
| 2429 | // Animate the drag view back to the front position |
| 2430 | animateDragViewToOriginalPosition(); |
| 2431 | } else { |
| 2432 | // Handled in post-delete-animation-callbacks |
| 2433 | } |
| 2434 | } |
| 2435 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2436 | /* |
| 2437 | * Flinging to delete - IN PROGRESS |
| 2438 | */ |
| 2439 | private PointF isFlingingToDelete() { |
| 2440 | ViewConfiguration config = ViewConfiguration.get(getContext()); |
| 2441 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 2442 | |
| 2443 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 2444 | // Do a quick dot product test to ensure that we are flinging upwards |
| 2445 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 2446 | mVelocityTracker.getYVelocity()); |
| 2447 | PointF upVec = new PointF(0f, -1f); |
| 2448 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 2449 | (vel.length() * upVec.length())); |
| 2450 | if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) { |
| 2451 | return vel; |
| 2452 | } |
| 2453 | } |
| 2454 | return null; |
| 2455 | } |
| 2456 | |
| 2457 | /** |
| 2458 | * Creates an animation from the current drag view along its current velocity vector. |
| 2459 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 2460 | * progressively. |
| 2461 | */ |
| 2462 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
| 2463 | private View mDragView; |
| 2464 | private PointF mVelocity; |
| 2465 | private Rect mFrom; |
| 2466 | private long mPrevTime; |
| 2467 | private float mFriction; |
| 2468 | |
| 2469 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
| 2470 | |
| 2471 | public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from, |
| 2472 | long startTime, float friction) { |
| 2473 | mDragView = dragView; |
| 2474 | mVelocity = vel; |
| 2475 | mFrom = from; |
| 2476 | mPrevTime = startTime; |
| 2477 | mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction); |
| 2478 | } |
| 2479 | |
| 2480 | @Override |
| 2481 | public void onAnimationUpdate(ValueAnimator animation) { |
| 2482 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 2483 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2484 | |
| 2485 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 2486 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 2487 | |
| 2488 | mDragView.setTranslationX(mFrom.left); |
| 2489 | mDragView.setTranslationY(mFrom.top); |
| 2490 | mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 2491 | |
| 2492 | mVelocity.x *= mFriction; |
| 2493 | mVelocity.y *= mFriction; |
| 2494 | mPrevTime = curTime; |
| 2495 | } |
| 2496 | }; |
| 2497 | |
| 2498 | private static final int ANIM_TAG_KEY = 100; |
| 2499 | |
| 2500 | private Runnable createPostDeleteAnimationRunnable(final View dragView) { |
| 2501 | return new Runnable() { |
| 2502 | @Override |
| 2503 | public void run() { |
| 2504 | int dragViewIndex = indexOfChild(dragView); |
| 2505 | |
| 2506 | // For each of the pages around the drag view, animate them from the previous |
| 2507 | // position to the new position in the layout (as a result of the drag view moving |
| 2508 | // in the layout) |
| 2509 | // NOTE: We can make an assumption here because we have side-bound pages that we |
| 2510 | // will always have pages to animate in from the left |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2511 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2512 | boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]); |
| 2513 | boolean slideFromLeft = (isLastWidgetPage || |
| 2514 | dragViewIndex > mTempVisiblePagesRange[0]); |
| 2515 | |
| 2516 | // Setup the scroll to the correct page before we swap the views |
| 2517 | if (slideFromLeft) { |
| 2518 | snapToPageImmediately(dragViewIndex - 1); |
| 2519 | } |
| 2520 | |
| 2521 | int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]); |
| 2522 | int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1); |
| 2523 | int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 ); |
| 2524 | int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex); |
| 2525 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2526 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 2527 | View v = getChildAt(i); |
| 2528 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 2529 | // drag view all subsequent views to pageUnderPointIndex will |
| 2530 | // shift down. |
| 2531 | int oldX = 0; |
| 2532 | int newX = 0; |
| 2533 | if (slideFromLeft) { |
| 2534 | if (i == 0) { |
| 2535 | // Simulate the page being offscreen with the page spacing |
| 2536 | oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i) |
| 2537 | - mPageSpacing; |
| 2538 | } else { |
| 2539 | oldX = getViewportOffsetX() + getChildOffset(i - 1); |
| 2540 | } |
| 2541 | newX = getViewportOffsetX() + getChildOffset(i); |
| 2542 | } else { |
| 2543 | oldX = getChildOffset(i) - getChildOffset(i - 1); |
| 2544 | newX = 0; |
| 2545 | } |
| 2546 | |
| 2547 | // Animate the view translation from its old position to its new |
| 2548 | // position |
| 2549 | AnimatorSet anim = (AnimatorSet) v.getTag(); |
| 2550 | if (anim != null) { |
| 2551 | anim.cancel(); |
| 2552 | } |
| 2553 | |
| 2554 | // Note: Hacky, but we want to skip any optimizations to not draw completely |
| 2555 | // hidden views |
| 2556 | v.setAlpha(Math.max(v.getAlpha(), 0.01f)); |
| 2557 | v.setTranslationX(oldX - newX); |
| 2558 | anim = new AnimatorSet(); |
| 2559 | anim.playTogether( |
| 2560 | ObjectAnimator.ofFloat(v, "translationX", 0f), |
| 2561 | ObjectAnimator.ofFloat(v, "alpha", 1f)); |
| 2562 | animations.add(anim); |
| 2563 | v.setTag(ANIM_TAG_KEY, anim); |
| 2564 | } |
| 2565 | |
| 2566 | AnimatorSet slideAnimations = new AnimatorSet(); |
| 2567 | slideAnimations.playTogether(animations); |
| 2568 | slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION); |
| 2569 | slideAnimations.addListener(new AnimatorListenerAdapter() { |
| 2570 | @Override |
| 2571 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2572 | mDeferringForDelete = false; |
| 2573 | onEndReordering(); |
| 2574 | onRemoveViewAnimationCompleted(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2575 | } |
| 2576 | }); |
| 2577 | slideAnimations.start(); |
| 2578 | |
| 2579 | removeView(dragView); |
| 2580 | onRemoveView(dragView, true); |
| 2581 | } |
| 2582 | }; |
| 2583 | } |
| 2584 | |
| 2585 | public void onFlingToDelete(PointF vel) { |
| 2586 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2587 | |
| 2588 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 2589 | // called and we expect the animation to be a continuation of the fling, we have |
| 2590 | // to account for the time that has elapsed since the fling finished. And since |
| 2591 | // we don't have a startDelay, we will always get call to update when we call |
| 2592 | // start() (which we want to ignore). |
| 2593 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 2594 | private int mCount = -1; |
| 2595 | private long mStartTime; |
| 2596 | private float mOffset; |
| 2597 | /* Anonymous inner class ctor */ { |
| 2598 | mStartTime = startTime; |
| 2599 | } |
| 2600 | |
| 2601 | @Override |
| 2602 | public float getInterpolation(float t) { |
| 2603 | if (mCount < 0) { |
| 2604 | mCount++; |
| 2605 | } else if (mCount == 0) { |
| 2606 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 2607 | mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2608 | mCount++; |
| 2609 | } |
| 2610 | return Math.min(1f, mOffset + t); |
| 2611 | } |
| 2612 | }; |
| 2613 | |
| 2614 | final Rect from = new Rect(); |
| 2615 | final View dragView = mDragView; |
| 2616 | from.left = (int) dragView.getTranslationX(); |
| 2617 | from.top = (int) dragView.getTranslationY(); |
| 2618 | AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel, |
| 2619 | from, startTime, FLING_TO_DELETE_FRICTION); |
| 2620 | |
| 2621 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
| 2622 | |
| 2623 | // Create and start the animation |
| 2624 | ValueAnimator mDropAnim = new ValueAnimator(); |
| 2625 | mDropAnim.setInterpolator(tInterpolator); |
| 2626 | mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2627 | mDropAnim.setFloatValues(0f, 1f); |
| 2628 | mDropAnim.addUpdateListener(updateCb); |
| 2629 | mDropAnim.addListener(new AnimatorListenerAdapter() { |
| 2630 | public void onAnimationEnd(Animator animation) { |
| 2631 | onAnimationEndRunnable.run(); |
| 2632 | } |
| 2633 | }); |
| 2634 | mDropAnim.start(); |
| 2635 | mDeferringForDelete = true; |
| 2636 | } |
| 2637 | |
| 2638 | /* Drag to delete */ |
| 2639 | private boolean isHoveringOverDeleteDropTarget(int x, int y) { |
| 2640 | if (mDeleteDropTarget != null) { |
| 2641 | mAltTmpRect.set(0, 0, 0, 0); |
| 2642 | View parent = (View) mDeleteDropTarget.getParent(); |
| 2643 | if (parent != null) { |
| 2644 | parent.getGlobalVisibleRect(mAltTmpRect); |
| 2645 | } |
| 2646 | mDeleteDropTarget.getGlobalVisibleRect(mTmpRect); |
| 2647 | mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top); |
| 2648 | return mTmpRect.contains(x, y); |
| 2649 | } |
| 2650 | return false; |
| 2651 | } |
| 2652 | |
| 2653 | protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {} |
| 2654 | |
| 2655 | private void onDropToDelete() { |
| 2656 | final View dragView = mDragView; |
| 2657 | |
| 2658 | final float toScale = 0f; |
| 2659 | final float toAlpha = 0f; |
| 2660 | |
| 2661 | // Create and start the complex animation |
| 2662 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2663 | AnimatorSet motionAnim = new AnimatorSet(); |
| 2664 | motionAnim.setInterpolator(new DecelerateInterpolator(2)); |
| 2665 | motionAnim.playTogether( |
| 2666 | ObjectAnimator.ofFloat(dragView, "scaleX", toScale), |
| 2667 | ObjectAnimator.ofFloat(dragView, "scaleY", toScale)); |
| 2668 | animations.add(motionAnim); |
| 2669 | |
| 2670 | AnimatorSet alphaAnim = new AnimatorSet(); |
| 2671 | alphaAnim.setInterpolator(new LinearInterpolator()); |
| 2672 | alphaAnim.playTogether( |
| 2673 | ObjectAnimator.ofFloat(dragView, "alpha", toAlpha)); |
| 2674 | animations.add(alphaAnim); |
| 2675 | |
| 2676 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
| 2677 | |
| 2678 | AnimatorSet anim = new AnimatorSet(); |
| 2679 | anim.playTogether(animations); |
| 2680 | anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION); |
| 2681 | anim.addListener(new AnimatorListenerAdapter() { |
| 2682 | public void onAnimationEnd(Animator animation) { |
| 2683 | onAnimationEndRunnable.run(); |
| 2684 | } |
| 2685 | }); |
| 2686 | anim.start(); |
| 2687 | |
| 2688 | mDeferringForDelete = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 2689 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2690 | |
| 2691 | /* Accessibility */ |
| 2692 | @Override |
| 2693 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2694 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2695 | info.setScrollable(getPageCount() > 1); |
| 2696 | if (getCurrentPage() < getPageCount() - 1) { |
| 2697 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2698 | } |
| 2699 | if (getCurrentPage() > 0) { |
| 2700 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2701 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | @Override |
| 2705 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2706 | super.onInitializeAccessibilityEvent(event); |
| 2707 | event.setScrollable(true); |
| 2708 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2709 | event.setFromIndex(mCurrentPage); |
| 2710 | event.setToIndex(mCurrentPage); |
| 2711 | event.setItemCount(getChildCount()); |
| 2712 | } |
| 2713 | } |
| 2714 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2715 | @Override |
| 2716 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2717 | if (super.performAccessibilityAction(action, arguments)) { |
| 2718 | return true; |
| 2719 | } |
| 2720 | switch (action) { |
| 2721 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2722 | if (getCurrentPage() < getPageCount() - 1) { |
| 2723 | scrollRight(); |
| 2724 | return true; |
| 2725 | } |
| 2726 | } break; |
| 2727 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2728 | if (getCurrentPage() > 0) { |
| 2729 | scrollLeft(); |
| 2730 | return true; |
| 2731 | } |
| 2732 | } break; |
| 2733 | } |
| 2734 | return false; |
| 2735 | } |
| 2736 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2737 | protected String getCurrentPageDescription() { |
| 2738 | return String.format(getContext().getString(R.string.default_scroll_format), |
| 2739 | getNextPage() + 1, getChildCount()); |
| 2740 | } |
| 2741 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 2742 | @Override |
| 2743 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2744 | return true; |
| 2745 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2746 | } |