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; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 21 | import android.animation.LayoutTransition; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.TimeInterpolator; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 24 | import android.annotation.SuppressLint; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 27 | import android.graphics.Matrix; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
| 32 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 34 | import android.view.InputDevice; |
| 35 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
| 37 | import android.view.VelocityTracker; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 40 | import android.view.ViewDebug; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 41 | import android.view.ViewGroup; |
| 42 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 44 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 45 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 46 | import android.view.animation.Interpolator; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 48 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 49 | import com.android.launcher3.anim.PropertyListBuilder; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 50 | import com.android.launcher3.pageindicators.PageIndicator; |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 51 | import com.android.launcher3.touch.OverScroll; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 52 | import com.android.launcher3.util.Thunk; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 53 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
| 55 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 56 | /** |
| 57 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 58 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 59 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 60 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 62 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 63 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 64 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 65 | // 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] | 66 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 67 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 68 | public static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 69 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 70 | |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 71 | // OverScroll constants |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 72 | private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 73 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 74 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 75 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 76 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 77 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 78 | private static final float MAX_SCROLL_PROGRESS = 1.0f; |
| 79 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 80 | // The following constants need to be scaled based on density. The scaled versions will be |
| 81 | // assigned to the corresponding member variables below. |
| 82 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 83 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 84 | private static final int MIN_FLING_VELOCITY = 250; |
| 85 | |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 86 | public static final int INVALID_RESTORE_PAGE = -1001; |
| 87 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 88 | private boolean mFreeScroll = false; |
Tony Wickham | 1c9bf6a | 2017-11-17 10:25:27 -0800 | [diff] [blame^] | 89 | private boolean mSettleOnPageInFreeScroll = false; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 90 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 91 | protected int mFlingThresholdVelocity; |
| 92 | protected int mMinFlingVelocity; |
| 93 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 94 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 95 | protected boolean mFirstLayout = true; |
| 96 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 97 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | protected int mCurrentPage; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 99 | private int mChildCountOnLastLayout; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 100 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 101 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 102 | protected int mNextPage = INVALID_PAGE; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 103 | protected int mMaxScrollX; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 104 | protected LauncherScroller mScroller; |
| 105 | private Interpolator mDefaultInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 106 | private VelocityTracker mVelocityTracker; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 107 | @Thunk int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 108 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 109 | private float mParentDownMotionX; |
| 110 | private float mParentDownMotionY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 111 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 112 | private float mDownMotionY; |
| 113 | private float mDownScrollX; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 114 | private float mDragViewBaselineLeft; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 115 | private float mLastMotionX; |
| 116 | private float mLastMotionXRemainder; |
| 117 | private float mLastMotionY; |
| 118 | private float mTotalMotionX; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 119 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 120 | private boolean mCancelTap; |
| 121 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 122 | private int[] mPageScrolls; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 123 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 124 | protected final static int TOUCH_STATE_REST = 0; |
| 125 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 126 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 127 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 128 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 129 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 130 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 131 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 132 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 134 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 135 | private int mMaximumVelocity; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 136 | protected boolean mAllowOverScroll = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 137 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 138 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 139 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 140 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 141 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 142 | protected boolean mIsPageInTransition = false; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 143 | |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 144 | protected boolean mWasInOverscroll = false; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 145 | |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 146 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise |
| 147 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 148 | // the screens from continuing to translate beyond the normal bounds. |
| 149 | protected int mOverScrollX; |
| 150 | |
| 151 | protected int mUnboundedScrollX; |
| 152 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 153 | // Page Indicator |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 154 | @Thunk int mPageIndicatorViewId; |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 155 | protected PageIndicator mPageIndicator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 156 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 157 | // viewport) |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 158 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 159 | private Rect mViewport = new Rect(); |
| 160 | |
| 161 | // Reordering |
| 162 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 163 | // dimensions such that when we are zoomed out, the view is not clipped |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 164 | private static int REORDERING_DROP_REPOSITION_DURATION = 200; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 165 | @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 166 | private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80; |
| 167 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 168 | @Thunk View mDragView; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 169 | private Runnable mSidePageHoverRunnable; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 170 | @Thunk int mSidePageHoverIndex = -1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 171 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 172 | private boolean mReorderingStarted = false; |
| 173 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 174 | // animation after endReordering() |
| 175 | private boolean mIsReordering; |
| 176 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 177 | private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 178 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 179 | private Runnable mPostReorderingPreZoomInRunnable; |
| 180 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 181 | // Convenience/caching |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 182 | private static final Matrix sTmpInvMatrix = new Matrix(); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 183 | private static final float[] sTmpPoint = new float[2]; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 184 | private static final Rect sTmpRect = new Rect(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 185 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 186 | protected final Rect mInsets = new Rect(); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 187 | protected final boolean mIsRtl; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 188 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 189 | public PagedView(Context context) { |
| 190 | this(context, null); |
| 191 | } |
| 192 | |
| 193 | public PagedView(Context context, AttributeSet attrs) { |
| 194 | this(context, attrs, 0); |
| 195 | } |
| 196 | |
| 197 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 198 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 199 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 200 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 201 | R.styleable.PagedView, defStyle, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 202 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 203 | a.recycle(); |
| 204 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 205 | setHapticFeedbackEnabled(false); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 206 | mIsRtl = Utilities.isRtl(getResources()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 207 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Initializes various states for this workspace. |
| 212 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 213 | protected void init() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 214 | mScroller = new LauncherScroller(getContext()); |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 215 | setDefaultInterpolator(Interpolators.SCROLL); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 216 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 217 | |
| 218 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 219 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 220 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 221 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 222 | float density = getResources().getDisplayMetrics().density; |
| 223 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density); |
| 224 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density); |
| 225 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 226 | setOnHierarchyChangeListener(this); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 227 | setWillNotDraw(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 230 | protected void setDefaultInterpolator(Interpolator interpolator) { |
| 231 | mDefaultInterpolator = interpolator; |
| 232 | mScroller.setInterpolator(mDefaultInterpolator); |
| 233 | } |
| 234 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 235 | public void initParentViews(View parent) { |
| 236 | if (mPageIndicatorViewId > -1) { |
| 237 | mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId); |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 238 | mPageIndicator.setMarkersCount(getChildCount()); |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 239 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 243 | // Convenience methods to map points from self to parent and vice versa |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 244 | private float[] mapPointFromViewToParent(View v, float x, float y) { |
| 245 | sTmpPoint[0] = x; |
| 246 | sTmpPoint[1] = y; |
| 247 | v.getMatrix().mapPoints(sTmpPoint); |
| 248 | sTmpPoint[0] += v.getLeft(); |
| 249 | sTmpPoint[1] += v.getTop(); |
| 250 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 251 | } |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 252 | private float[] mapPointFromParentToView(View v, float x, float y) { |
| 253 | sTmpPoint[0] = x - v.getLeft(); |
| 254 | sTmpPoint[1] = y - v.getTop(); |
| 255 | v.getMatrix().invert(sTmpInvMatrix); |
| 256 | sTmpInvMatrix.mapPoints(sTmpPoint); |
| 257 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 260 | private void updateDragViewTranslationDuringDrag() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 261 | if (mDragView != null) { |
| 262 | float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) + |
| 263 | (mDragViewBaselineLeft - mDragView.getLeft()); |
| 264 | float y = mLastMotionY - mDownMotionY; |
| 265 | mDragView.setTranslationX(x); |
| 266 | mDragView.setTranslationY(y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 267 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 268 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " |
| 269 | + x + ", " + y); |
| 270 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 273 | @Override |
| 274 | public void setScaleX(float scaleX) { |
| 275 | super.setScaleX(scaleX); |
| 276 | if (isReordering(true)) { |
| 277 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 278 | mLastMotionX = p[0]; |
| 279 | mLastMotionY = p[1]; |
| 280 | updateDragViewTranslationDuringDrag(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
| 285 | // to be larger to account for the minimum possible scale) |
| 286 | int getViewportWidth() { |
| 287 | return mViewport.width(); |
| 288 | } |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 289 | public int getViewportHeight() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 290 | return mViewport.height(); |
| 291 | } |
| 292 | |
| 293 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
| 294 | // PagedView both horizontally and vertically |
| 295 | int getViewportOffsetX() { |
| 296 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
| 297 | } |
| 298 | |
| 299 | int getViewportOffsetY() { |
| 300 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
| 301 | } |
| 302 | |
Hyunyoung Song | 7744169 | 2016-06-27 22:00:48 -0700 | [diff] [blame] | 303 | public PageIndicator getPageIndicator() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 304 | return mPageIndicator; |
| 305 | } |
| 306 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 307 | /** |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 308 | * Returns the index of the currently displayed page. When in free scroll mode, this is the page |
| 309 | * that the user was on before entering free scroll mode (e.g. the home screen page they |
| 310 | * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()} |
| 311 | * to get the page the user is currently scrolling over. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 312 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 313 | public int getCurrentPage() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 314 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 315 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 316 | |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Returns the index of page to be shown immediately afterwards. |
| 319 | */ |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 320 | public int getNextPage() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 321 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 322 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 323 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 324 | public int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 325 | return getChildCount(); |
| 326 | } |
| 327 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 328 | public View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 329 | return getChildAt(index); |
| 330 | } |
| 331 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 332 | protected int indexToPage(int index) { |
| 333 | return index; |
| 334 | } |
| 335 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 336 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 337 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 338 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 339 | * the previous tab page. |
| 340 | */ |
| 341 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 342 | // If the current page is invalid, just reset the scroll position to zero |
| 343 | int newX = 0; |
| 344 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 345 | newX = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 346 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 347 | scrollTo(newX, 0); |
| 348 | mScroller.setFinalX(newX); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 349 | forceFinishScroller(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 352 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 353 | mScroller.abortAnimation(); |
| 354 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 355 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 356 | if (resetNextPage) { |
| 357 | mNextPage = INVALID_PAGE; |
| 358 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 359 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 360 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 361 | private void forceFinishScroller(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 362 | mScroller.forceFinished(true); |
| 363 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 364 | // updating current page on the pass. |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 365 | if (resetNextPage) { |
| 366 | mNextPage = INVALID_PAGE; |
| 367 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 370 | private int validateNewPage(int newPage) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 371 | // Ensure that it is clamped by the actual set of children in all cases |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 372 | return Utilities.boundToRange(newPage, 0, getPageCount() - 1); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 375 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 376 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 377 | */ |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 378 | public void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 379 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 380 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 381 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 382 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 383 | // the default |
| 384 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 385 | return; |
| 386 | } |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 387 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 388 | mCurrentPage = validateNewPage(currentPage); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 389 | updateCurrentPageScroll(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 390 | notifyPageSwitchListener(prevPage); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 391 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 394 | /** |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 395 | * Should be called whenever the page changes. In the case of a scroll, we wait until the page |
| 396 | * has settled. |
| 397 | */ |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 398 | protected void notifyPageSwitchListener(int prevPage) { |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 399 | updatePageIndicator(); |
| 400 | } |
| 401 | |
| 402 | private void updatePageIndicator() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 403 | // Update the page indicator (when we aren't reordering) |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 404 | if (mPageIndicator != null) { |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 405 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 406 | if (!isReordering(false)) { |
| 407 | mPageIndicator.setActiveMarker(getNextPage()); |
| 408 | } |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 409 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 410 | } |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 411 | protected void pageBeginTransition() { |
| 412 | if (!mIsPageInTransition) { |
| 413 | mIsPageInTransition = true; |
| 414 | onPageBeginTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 415 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 418 | protected void pageEndTransition() { |
| 419 | if (mIsPageInTransition) { |
| 420 | mIsPageInTransition = false; |
| 421 | onPageEndTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 422 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 425 | protected boolean isPageInTransition() { |
| 426 | return mIsPageInTransition; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 429 | /** |
| 430 | * Called when the page starts moving as part of the scroll. Subclasses can override this |
| 431 | * to provide custom behavior during animation. |
| 432 | */ |
| 433 | protected void onPageBeginTransition() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Called when the page ends moving as part of the scroll. Subclasses can override this |
| 438 | * to provide custom behavior during animation. |
| 439 | */ |
| 440 | protected void onPageEndTransition() { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 441 | mWasInOverscroll = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 444 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 445 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 446 | * |
| 447 | * @param l The listener used to respond to long clicks. |
| 448 | */ |
| 449 | @Override |
| 450 | public void setOnLongClickListener(OnLongClickListener l) { |
| 451 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 452 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 453 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 454 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 455 | } |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 456 | super.setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 459 | protected int getUnboundedScrollX() { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 460 | return mUnboundedScrollX; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 463 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 464 | public void scrollBy(int x, int y) { |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 465 | scrollTo(getUnboundedScrollX() + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 469 | public void scrollTo(int x, int y) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 470 | // In free scroll mode, we clamp the scrollX |
| 471 | if (mFreeScroll) { |
Adam Cohen | b2b02b9 | 2015-06-10 18:40:05 -0700 | [diff] [blame] | 472 | // If the scroller is trying to move to a location beyond the maximum allowed |
| 473 | // in the free scroll mode, we make sure to end the scroll operation. |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 474 | if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 475 | forceFinishScroller(false); |
Adam Cohen | b2b02b9 | 2015-06-10 18:40:05 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 478 | x = Utilities.boundToRange(x, 0, mMaxScrollX); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 481 | mUnboundedScrollX = x; |
| 482 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 483 | boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0); |
| 484 | boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 485 | if (isXBeforeFirstPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 486 | super.scrollTo(mIsRtl ? mMaxScrollX : 0, y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 487 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 488 | mWasInOverscroll = true; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 489 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 490 | overScroll(x - mMaxScrollX); |
| 491 | } else { |
| 492 | overScroll(x); |
| 493 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 494 | } |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 495 | } else if (isXAfterLastPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 496 | super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 497 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 498 | mWasInOverscroll = true; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 499 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 500 | overScroll(x); |
| 501 | } else { |
| 502 | overScroll(x - mMaxScrollX); |
| 503 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 504 | } |
| 505 | } else { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 506 | if (mWasInOverscroll) { |
| 507 | overScroll(0); |
| 508 | mWasInOverscroll = false; |
| 509 | } |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 510 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 511 | super.scrollTo(x, y); |
| 512 | } |
| 513 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 514 | // Update the last motion events when scrolling |
| 515 | if (isReordering(true)) { |
| 516 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 517 | mLastMotionX = p[0]; |
| 518 | mLastMotionY = p[1]; |
| 519 | updateDragViewTranslationDuringDrag(); |
| 520 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 523 | private void sendScrollAccessibilityEvent() { |
| 524 | AccessibilityManager am = |
| 525 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 526 | if (am.isEnabled()) { |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 527 | if (mCurrentPage != getNextPage()) { |
| 528 | AccessibilityEvent ev = |
| 529 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 530 | ev.setScrollable(true); |
| 531 | ev.setScrollX(getScrollX()); |
| 532 | ev.setScrollY(getScrollY()); |
| 533 | ev.setMaxScrollX(mMaxScrollX); |
| 534 | ev.setMaxScrollY(0); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 535 | |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 536 | sendAccessibilityEventUnchecked(ev); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 537 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 541 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 542 | protected boolean computeScrollHelper() { |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 543 | return computeScrollHelper(true); |
| 544 | } |
| 545 | |
| 546 | protected boolean computeScrollHelper(boolean shouldInvalidate) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 547 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 548 | // Don't bother scrolling if the page does not need to be moved |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 549 | if (getUnboundedScrollX() != mScroller.getCurrX() |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 550 | || getScrollY() != mScroller.getCurrY() |
| 551 | || mOverScrollX != mScroller.getCurrX()) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 552 | float scaleX = mFreeScroll ? getScaleX() : 1f; |
| 553 | int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX)); |
| 554 | scrollTo(scrollX, mScroller.getCurrY()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 555 | } |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 556 | if (shouldInvalidate) { |
| 557 | invalidate(); |
| 558 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 559 | return true; |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 560 | } else if (mNextPage != INVALID_PAGE && shouldInvalidate) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 561 | sendScrollAccessibilityEvent(); |
| 562 | |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 563 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 564 | mCurrentPage = validateNewPage(mNextPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 565 | mNextPage = INVALID_PAGE; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 566 | notifyPageSwitchListener(prevPage); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 567 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 568 | // We don't want to trigger a page end moving unless the page has settled |
| 569 | // and the user has stopped scrolling |
| 570 | if (mTouchState == TOUCH_STATE_REST) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 571 | pageEndTransition(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 572 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 573 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 574 | onPostReorderingAnimationCompleted(); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 575 | AccessibilityManager am = (AccessibilityManager) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 576 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 577 | if (am.isEnabled()) { |
| 578 | // Notify the user when the page changes |
| 579 | announceForAccessibility(getCurrentPageDescription()); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 580 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 581 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 582 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public void computeScroll() { |
| 588 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 591 | public int getNormalChildHeight() { |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 592 | return getViewportHeight() - getPaddingTop() - getPaddingBottom() |
| 593 | - mInsets.top - mInsets.bottom; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 596 | @Override |
| 597 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 598 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 599 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 600 | return; |
| 601 | } |
| 602 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 603 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 604 | // 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] | 605 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 606 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 607 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 608 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 609 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 610 | mViewport.set(0, 0, widthSize, heightSize); |
| 611 | |
| 612 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 613 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 614 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 617 | // Return early if we aren't given a proper dimension |
| 618 | if (widthSize <= 0 || heightSize <= 0) { |
| 619 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 620 | return; |
| 621 | } |
| 622 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 623 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 624 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 625 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 626 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 627 | int myWidthSpec = MeasureSpec.makeMeasureSpec( |
| 628 | getViewportWidth() - mInsets.left - mInsets.right, MeasureSpec.EXACTLY); |
| 629 | int myHeightSpec = MeasureSpec.makeMeasureSpec( |
| 630 | getViewportHeight() - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 631 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 632 | // measureChildren takes accounts for content padding, we only need to care about extra |
| 633 | // space due to insets. |
| 634 | measureChildren(myWidthSpec, myHeightSpec); |
| 635 | setMeasuredDimension(widthSize, heightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 638 | @SuppressLint("DrawAllocation") |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 639 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 640 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 641 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 642 | return; |
| 643 | } |
| 644 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 645 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 646 | final int childCount = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 647 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 648 | int offsetX = getViewportOffsetX(); |
| 649 | int offsetY = getViewportOffsetY(); |
| 650 | |
| 651 | // Update the viewport offsets |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 652 | mViewport.offset(offsetX, offsetY); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 653 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 654 | final int startIndex = mIsRtl ? childCount - 1 : 0; |
| 655 | final int endIndex = mIsRtl ? -1 : childCount; |
| 656 | final int delta = mIsRtl ? -1 : 1; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 657 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 658 | int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 659 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 660 | int childLeft = offsetX + getPaddingLeft(); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 661 | if (mPageScrolls == null || childCount != mChildCountOnLastLayout) { |
| 662 | mPageScrolls = new int[childCount]; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 665 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 666 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 667 | if (child.getVisibility() != View.GONE) { |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 668 | int childTop = offsetY + getPaddingTop() + mInsets.top; |
| 669 | childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding |
| 670 | - child.getMeasuredHeight()) / 2; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 671 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 672 | final int childWidth = child.getMeasuredWidth(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 673 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 674 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 675 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 676 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 677 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 678 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 679 | int scrollOffsetLeft = getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 680 | mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX; |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 681 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 682 | childLeft += childWidth + mPageSpacing + getChildGap(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 685 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 686 | final LayoutTransition transition = getLayoutTransition(); |
| 687 | // If the transition is running defer updating max scroll, as some empty pages could |
| 688 | // still be present, and a max scroll change could cause sudden jumps in scroll. |
| 689 | if (transition != null && transition.isRunning()) { |
| 690 | transition.addTransitionListener(new LayoutTransition.TransitionListener() { |
| 691 | |
| 692 | @Override |
| 693 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 694 | View view, int transitionType) { } |
| 695 | |
| 696 | @Override |
| 697 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 698 | View view, int transitionType) { |
| 699 | // Wait until all transitions are complete. |
| 700 | if (!transition.isRunning()) { |
| 701 | transition.removeTransitionListener(this); |
| 702 | updateMaxScrollX(); |
| 703 | } |
| 704 | } |
| 705 | }); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 706 | } else { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 707 | updateMaxScrollX(); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 710 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) { |
| 711 | updateCurrentPageScroll(); |
| 712 | mFirstLayout = false; |
| 713 | } |
| 714 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 715 | if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) { |
Sunny Goyal | fe770c9 | 2016-09-27 14:38:58 -0700 | [diff] [blame] | 716 | setCurrentPage(getNextPage()); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 717 | } |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 718 | mChildCountOnLastLayout = childCount; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 719 | |
| 720 | if (isReordering(true)) { |
| 721 | updateDragViewTranslationDuringDrag(); |
| 722 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 725 | protected int getChildGap() { |
| 726 | return 0; |
| 727 | } |
| 728 | |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 729 | private void updateMaxScrollX() { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 730 | mMaxScrollX = computeMaxScrollX(); |
| 731 | } |
| 732 | |
| 733 | protected int computeMaxScrollX() { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 734 | int childCount = getChildCount(); |
| 735 | if (childCount > 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 736 | final int index = mIsRtl ? 0 : childCount - 1; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 737 | return getScrollForPage(index); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 738 | } else { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 739 | return 0; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 743 | public void setPageSpacing(int pageSpacing) { |
| 744 | mPageSpacing = pageSpacing; |
| 745 | requestLayout(); |
| 746 | } |
| 747 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 748 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 749 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 750 | // Update the page indicator, we don't update the page indicator as we |
| 751 | // add/remove pages |
| 752 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 753 | mPageIndicator.addMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 756 | // This ensures that when children are added, they get the correct transforms / alphas |
| 757 | // in accordance with any scroll effects. |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 758 | invalidate(); |
| 759 | } |
| 760 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 761 | @Override |
| 762 | public void onChildViewRemoved(View parent, View child) { |
Tony | a361c72 | 2017-07-04 09:43:06 -0700 | [diff] [blame] | 763 | mCurrentPage = validateNewPage(mCurrentPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 764 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 767 | private void removeMarkerForView() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 768 | // Update the page indicator, we don't update the page indicator as we |
| 769 | // add/remove pages |
| 770 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 771 | mPageIndicator.removeMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
| 775 | @Override |
| 776 | public void removeView(View v) { |
| 777 | // XXX: We should find a better way to hook into this before the view |
| 778 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 779 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 780 | super.removeView(v); |
| 781 | } |
| 782 | @Override |
| 783 | public void removeViewInLayout(View v) { |
| 784 | // XXX: We should find a better way to hook into this before the view |
| 785 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 786 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 787 | super.removeViewInLayout(v); |
| 788 | } |
| 789 | @Override |
| 790 | public void removeViewAt(int index) { |
| 791 | // XXX: We should find a better way to hook into this before the view |
| 792 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 793 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 794 | super.removeViewAt(index); |
| 795 | } |
| 796 | @Override |
| 797 | public void removeAllViewsInLayout() { |
| 798 | // Update the page indicator, we don't update the page indicator as we |
| 799 | // add/remove pages |
| 800 | if (mPageIndicator != null) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 801 | mPageIndicator.setMarkersCount(0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | super.removeAllViewsInLayout(); |
| 805 | } |
| 806 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 807 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 808 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 809 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 810 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 811 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 812 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 815 | @Override |
| 816 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 817 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 818 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 819 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 820 | return true; |
| 821 | } |
| 822 | return false; |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 827 | int focusablePage; |
| 828 | if (mNextPage != INVALID_PAGE) { |
| 829 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 830 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 831 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 832 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 833 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 834 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 835 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 836 | } |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | @Override |
| 841 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 842 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | if (mIsRtl) { |
| 847 | if (direction == View.FOCUS_LEFT) { |
| 848 | direction = View.FOCUS_RIGHT; |
| 849 | } else if (direction == View.FOCUS_RIGHT) { |
| 850 | direction = View.FOCUS_LEFT; |
| 851 | } |
| 852 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 853 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 854 | if (getCurrentPage() > 0) { |
| 855 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 856 | return true; |
| 857 | } |
| 858 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 859 | if (getCurrentPage() < getPageCount() - 1) { |
| 860 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 861 | return true; |
| 862 | } |
| 863 | } |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 864 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | @Override |
| 868 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Jon Miranda | 6a85817 | 2016-10-25 16:19:17 -0700 | [diff] [blame] | 869 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 870 | return; |
| 871 | } |
| 872 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 873 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 874 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 875 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 876 | } |
| 877 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 878 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 879 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 880 | } |
| 881 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 882 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 883 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * 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] | 890 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 891 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 892 | * This happens when live folders requery, and if they're off page, they |
| 893 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 894 | */ |
| 895 | @Override |
| 896 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 897 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 898 | View v = focused; |
| 899 | while (true) { |
| 900 | if (v == current) { |
| 901 | super.focusableViewAvailable(focused); |
| 902 | return; |
| 903 | } |
| 904 | if (v == this) { |
| 905 | return; |
| 906 | } |
| 907 | ViewParent parent = v.getParent(); |
| 908 | if (parent instanceof View) { |
| 909 | v = (View)v.getParent(); |
| 910 | } else { |
| 911 | return; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * {@inheritDoc} |
| 918 | */ |
| 919 | @Override |
| 920 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 921 | if (disallowIntercept) { |
| 922 | // We need to make sure to cancel our long press if |
| 923 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 924 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 925 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 926 | } |
| 927 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 928 | } |
| 929 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 930 | /** |
| 931 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 932 | */ |
| 933 | protected boolean hitsPreviousPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 934 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 935 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 936 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 937 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 938 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 943 | */ |
| 944 | protected boolean hitsNextPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 945 | if (mIsRtl) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 946 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 947 | } |
| 948 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 949 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 950 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 951 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 952 | /** Returns whether x and y originated within the buffered viewport */ |
| 953 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 954 | sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 955 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 956 | return sTmpRect.contains(x, y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 959 | @Override |
| 960 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 961 | /* |
| 962 | * This method JUST determines whether we want to intercept the motion. |
| 963 | * If we return true, onTouchEvent will be called and we do the actual |
| 964 | * scrolling there. |
| 965 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 966 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 967 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 968 | // Skip touch handling if there are no pages to swipe |
| 969 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 970 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 971 | /* |
| 972 | * Shortcut the most recurring case: the user is in the dragging |
| 973 | * state and he is moving his finger. We want to intercept this |
| 974 | * motion. |
| 975 | */ |
| 976 | final int action = ev.getAction(); |
| 977 | if ((action == MotionEvent.ACTION_MOVE) && |
| 978 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 979 | return true; |
| 980 | } |
| 981 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 982 | switch (action & MotionEvent.ACTION_MASK) { |
| 983 | case MotionEvent.ACTION_MOVE: { |
| 984 | /* |
| 985 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 986 | * whether the user has moved far enough from his original down touch. |
| 987 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 988 | if (mActivePointerId != INVALID_POINTER) { |
| 989 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 990 | } |
| 991 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 992 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 993 | // i.e. fall through to the next case (don't break) |
| 994 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 995 | // 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] | 996 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | case MotionEvent.ACTION_DOWN: { |
| 1000 | final float x = ev.getX(); |
| 1001 | final float y = ev.getY(); |
| 1002 | // Remember location of down touch |
| 1003 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1004 | mDownMotionY = y; |
| 1005 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1006 | mLastMotionX = x; |
| 1007 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1008 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1009 | mParentDownMotionX = p[0]; |
| 1010 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1011 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1012 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1013 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1014 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1015 | /* |
| 1016 | * If being flinged and user touches the screen, initiate drag; |
| 1017 | * otherwise don't. mScroller.isFinished should be false when |
| 1018 | * being flinged. |
| 1019 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1020 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1021 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1022 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1023 | if (finishedScrolling) { |
| 1024 | mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 59b5c79 | 2013-12-04 16:09:07 -0800 | [diff] [blame] | 1025 | if (!mScroller.isFinished() && !mFreeScroll) { |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1026 | setCurrentPage(getNextPage()); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1027 | pageEndTransition(); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1028 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1029 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1030 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1031 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1032 | } else { |
| 1033 | mTouchState = TOUCH_STATE_REST; |
| 1034 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1035 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1036 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1037 | break; |
| 1038 | } |
| 1039 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1040 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1041 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1042 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1043 | break; |
| 1044 | |
| 1045 | case MotionEvent.ACTION_POINTER_UP: |
| 1046 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1047 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * The only time we want to intercept motion events is if we are in the |
| 1053 | * drag mode. |
| 1054 | */ |
| 1055 | return mTouchState != TOUCH_STATE_REST; |
| 1056 | } |
| 1057 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1058 | protected void determineScrollingStart(MotionEvent ev) { |
| 1059 | determineScrollingStart(ev, 1.0f); |
| 1060 | } |
| 1061 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1062 | /* |
| 1063 | * Determines if we should change the touch state to start scrolling after the |
| 1064 | * user moves their touch point too far. |
| 1065 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1066 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1067 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1068 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1069 | if (pointerIndex == -1) return; |
| 1070 | |
| 1071 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1072 | final float x = ev.getX(pointerIndex); |
| 1073 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1074 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1075 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1076 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1077 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1078 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1079 | boolean xMoved = xDiff > touchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1080 | |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1081 | if (xMoved) { |
| 1082 | // Scroll if the user moved far enough along the X axis |
| 1083 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1084 | mTotalMotionX += Math.abs(mLastMotionX - x); |
| 1085 | mLastMotionX = x; |
| 1086 | mLastMotionXRemainder = 0; |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1087 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1088 | pageBeginTransition(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1089 | // Stop listening for things like pinches. |
| 1090 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1095 | // Try canceling the long press. It could also have been scheduled |
| 1096 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1097 | // everything |
| 1098 | final View currentPage = getPageAt(mCurrentPage); |
| 1099 | if (currentPage != null) { |
| 1100 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1104 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1105 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1106 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1107 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1108 | int count = getChildCount(); |
| 1109 | |
| 1110 | final int totalDistance; |
| 1111 | |
| 1112 | int adjacentPage = page + 1; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1113 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1114 | adjacentPage = page - 1; |
| 1115 | } |
| 1116 | |
| 1117 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1118 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1119 | } else { |
| 1120 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1121 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1122 | |
| 1123 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1124 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
| 1125 | scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1126 | return scrollProgress; |
| 1127 | } |
| 1128 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1129 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1130 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1131 | return 0; |
| 1132 | } else { |
| 1133 | return mPageScrolls[index]; |
| 1134 | } |
| 1135 | } |
| 1136 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1137 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1138 | // position. This method returns the magnitude of this stray at any given time. |
| 1139 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1140 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1141 | return 0; |
| 1142 | } else { |
| 1143 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1144 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 1145 | int scrollOffset = scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1146 | int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX(); |
| 1147 | return (int) (child.getX() - baselineX); |
| 1148 | } |
| 1149 | } |
| 1150 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1151 | protected void dampedOverScroll(float amount) { |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 1152 | if (Float.compare(amount, 0f) == 0) return; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1153 | |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 1154 | int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth()); |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1155 | if (amount < 0) { |
| 1156 | mOverScrollX = overScrollAmount; |
| 1157 | super.scrollTo(mOverScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1158 | } else { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1159 | mOverScrollX = mMaxScrollX + overScrollAmount; |
| 1160 | super.scrollTo(mOverScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1161 | } |
| 1162 | invalidate(); |
| 1163 | } |
| 1164 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1165 | protected void overScroll(float amount) { |
| 1166 | dampedOverScroll(amount); |
| 1167 | } |
| 1168 | |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 1169 | /** |
| 1170 | * return true if freescroll has been enabled, false otherwise |
| 1171 | */ |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1172 | protected void enableFreeScroll() { |
Tony Wickham | 1c9bf6a | 2017-11-17 10:25:27 -0800 | [diff] [blame^] | 1173 | enableFreeScroll(false); |
| 1174 | } |
| 1175 | |
| 1176 | protected void enableFreeScroll(boolean settleOnPageInFreeScroll) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1177 | setEnableFreeScroll(true); |
Tony Wickham | 1c9bf6a | 2017-11-17 10:25:27 -0800 | [diff] [blame^] | 1178 | mSettleOnPageInFreeScroll = settleOnPageInFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1181 | protected void disableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1182 | setEnableFreeScroll(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1185 | private void setEnableFreeScroll(boolean freeScroll) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1186 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1187 | mFreeScroll = freeScroll; |
| 1188 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1189 | if (mFreeScroll) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1190 | setCurrentPage(getNextPage()); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1191 | } else if (wasFreeScroll) { |
| 1192 | snapToPage(getNextPage()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | setEnableOverscroll(!freeScroll); |
| 1196 | } |
| 1197 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1198 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1199 | mAllowOverScroll = enable; |
| 1200 | } |
| 1201 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1202 | private int getNearestHoverOverPageIndex() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1203 | if (mDragView != null) { |
| 1204 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1205 | + mDragView.getTranslationX()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1206 | int minDistance = Integer.MAX_VALUE; |
| 1207 | int minIndex = indexOfChild(mDragView); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1208 | int maxPageNo = getChildCount() - 1; |
| 1209 | for (int i = 0; i <= maxPageNo; i++) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1210 | View page = getPageAt(i); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1211 | int pageX = (page.getLeft() + page.getMeasuredWidth() / 2); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1212 | int d = Math.abs(dragX - pageX); |
| 1213 | if (d < minDistance) { |
| 1214 | minIndex = i; |
| 1215 | minDistance = d; |
| 1216 | } |
| 1217 | } |
| 1218 | return minIndex; |
| 1219 | } |
| 1220 | return -1; |
| 1221 | } |
| 1222 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1223 | @Override |
| 1224 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1225 | super.onTouchEvent(ev); |
| 1226 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1227 | // Skip touch handling if there are no pages to swipe |
| 1228 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1229 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1230 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1231 | |
| 1232 | final int action = ev.getAction(); |
| 1233 | |
| 1234 | switch (action & MotionEvent.ACTION_MASK) { |
| 1235 | case MotionEvent.ACTION_DOWN: |
| 1236 | /* |
| 1237 | * If being flinged and user touches, stop the fling. isFinished |
| 1238 | * will be false if being flinged. |
| 1239 | */ |
| 1240 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1241 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | // Remember where the motion event started |
| 1245 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1246 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1247 | mDownScrollX = getScrollX(); |
| 1248 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1249 | mParentDownMotionX = p[0]; |
| 1250 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1251 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1252 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1253 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1254 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1255 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1256 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1257 | pageBeginTransition(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1258 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1259 | break; |
| 1260 | |
| 1261 | case MotionEvent.ACTION_MOVE: |
| 1262 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1263 | // Scroll to follow the motion event |
| 1264 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1265 | |
| 1266 | if (pointerIndex == -1) return true; |
| 1267 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1268 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1269 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1270 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1271 | mTotalMotionX += Math.abs(deltaX); |
| 1272 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1273 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1274 | // keep the remainder because we are actually testing if we've moved from the last |
| 1275 | // scrolled position (which is discrete). |
| 1276 | if (Math.abs(deltaX) >= 1.0f) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1277 | scrollBy((int) deltaX, 0); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1278 | mLastMotionX = x; |
| 1279 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1280 | } else { |
| 1281 | awakenScrollBars(); |
| 1282 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1283 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1284 | // Update the last motion position |
| 1285 | mLastMotionX = ev.getX(); |
| 1286 | mLastMotionY = ev.getY(); |
| 1287 | |
| 1288 | // Update the parent down so that our zoom animations take this new movement into |
| 1289 | // account |
| 1290 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1291 | mParentDownMotionX = pt[0]; |
| 1292 | mParentDownMotionY = pt[1]; |
| 1293 | updateDragViewTranslationDuringDrag(); |
| 1294 | |
| 1295 | // Find the closest page to the touch point |
| 1296 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1297 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1298 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1299 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1300 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1301 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1302 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1303 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1304 | // Do not allow any page to be moved to 0th position. |
| 1305 | if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1306 | if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 && |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1307 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1308 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1309 | mSidePageHoverRunnable = new Runnable() { |
| 1310 | @Override |
| 1311 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1312 | // Setup the scroll to the correct page before we swap the views |
| 1313 | snapToPage(pageUnderPointIndex); |
| 1314 | |
| 1315 | // For each of the pages between the paged view and the drag view, |
| 1316 | // animate them from the previous position to the new position in |
| 1317 | // the layout (as a result of the drag view moving in the layout) |
| 1318 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1319 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1320 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1321 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1322 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1323 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1324 | View v = getChildAt(i); |
| 1325 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1326 | // drag view all subsequent views to pageUnderPointIndex will |
| 1327 | // shift down. |
| 1328 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1329 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1330 | |
| 1331 | // Animate the view translation from its old position to its new |
| 1332 | // position |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1333 | ObjectAnimator anim = (ObjectAnimator) v.getTag(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1334 | if (anim != null) { |
| 1335 | anim.cancel(); |
| 1336 | } |
| 1337 | |
| 1338 | v.setTranslationX(oldX - newX); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1339 | anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1340 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1341 | anim.start(); |
| 1342 | v.setTag(anim); |
| 1343 | } |
| 1344 | |
| 1345 | removeView(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1346 | addView(mDragView, pageUnderPointIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1347 | mSidePageHoverIndex = -1; |
Winson Chung | 876a619 | 2013-11-06 14:49:50 -0800 | [diff] [blame] | 1348 | if (mPageIndicator != null) { |
| 1349 | mPageIndicator.setActiveMarker(getNextPage()); |
| 1350 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1351 | } |
| 1352 | }; |
| 1353 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1354 | } |
| 1355 | } else { |
| 1356 | removeCallbacks(mSidePageHoverRunnable); |
| 1357 | mSidePageHoverIndex = -1; |
| 1358 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1359 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1360 | determineScrollingStart(ev); |
| 1361 | } |
| 1362 | break; |
| 1363 | |
| 1364 | case MotionEvent.ACTION_UP: |
| 1365 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1366 | final int activePointerId = mActivePointerId; |
| 1367 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1368 | final float x = ev.getX(pointerIndex); |
| 1369 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1370 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1371 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1372 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1373 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1374 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1375 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1376 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1377 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1378 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1379 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1380 | shouldFlingForVelocity(velocityX); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1381 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1382 | if (!mFreeScroll) { |
| 1383 | // In the case that the page is moved far to one direction and then is flung |
| 1384 | // in the opposite direction, we use a threshold to determine whether we should |
| 1385 | // just return to the starting page, or if we should skip one further. |
| 1386 | boolean returnToOriginalPage = false; |
| 1387 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1388 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1389 | returnToOriginalPage = true; |
| 1390 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1391 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1392 | int finalPage; |
| 1393 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1394 | // test for a large move if a fling has been registered. That is, a large |
| 1395 | // move to the left and fling to the right will register as a fling to the right. |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1396 | boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0; |
| 1397 | boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1398 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1399 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1400 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1401 | snapToPageWithVelocity(finalPage, velocityX); |
| 1402 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1403 | (isFling && isVelocityXLeft)) && |
| 1404 | mCurrentPage < getChildCount() - 1) { |
| 1405 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1406 | snapToPageWithVelocity(finalPage, velocityX); |
| 1407 | } else { |
| 1408 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1409 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1410 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1411 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1412 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | float scaleX = getScaleX(); |
| 1416 | int vX = (int) (-velocityX * scaleX); |
| 1417 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1418 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1419 | mScroller.setInterpolator(mDefaultInterpolator); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1420 | mScroller.fling(initialScrollX, |
| 1421 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
Tony Wickham | 1c9bf6a | 2017-11-17 10:25:27 -0800 | [diff] [blame^] | 1422 | int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX); |
| 1423 | mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX); |
| 1424 | int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1); |
| 1425 | int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0); |
| 1426 | if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll |
| 1427 | && unscaledScrollX < lastPageScroll) { |
| 1428 | // Make sure we land directly on a page. If flinging past one of the ends, |
| 1429 | // don't change the velocity as it will get stopped at the end anyway. |
| 1430 | mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX())); |
| 1431 | // Ensure the scroll/snap doesn't happen too fast; |
| 1432 | int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION |
| 1433 | - mScroller.getDuration(); |
| 1434 | if (extraScrollDuration > 0) { |
| 1435 | mScroller.extendDuration(extraScrollDuration); |
| 1436 | } |
| 1437 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1438 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1439 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1440 | onScrollInteractionEnd(); |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 1441 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1442 | // at this point we have not moved beyond the touch slop |
| 1443 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1444 | // we can just page |
| 1445 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1446 | if (nextPage != mCurrentPage) { |
| 1447 | snapToPage(nextPage); |
| 1448 | } else { |
| 1449 | snapToDestination(); |
| 1450 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1451 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1452 | // at this point we have not moved beyond the touch slop |
| 1453 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1454 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1455 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1456 | if (nextPage != mCurrentPage) { |
| 1457 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1458 | } else { |
| 1459 | snapToDestination(); |
| 1460 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1461 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1462 | // Update the last motion position |
| 1463 | mLastMotionX = ev.getX(); |
| 1464 | mLastMotionY = ev.getY(); |
| 1465 | |
| 1466 | // Update the parent down so that our zoom animations take this new movement into |
| 1467 | // account |
| 1468 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1469 | mParentDownMotionX = pt[0]; |
| 1470 | mParentDownMotionY = pt[1]; |
| 1471 | updateDragViewTranslationDuringDrag(); |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1472 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1473 | if (!mCancelTap) { |
| 1474 | onUnhandledTap(ev); |
| 1475 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1476 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1477 | |
| 1478 | // Remove the callback to wait for the side page hover timeout |
| 1479 | removeCallbacks(mSidePageHoverRunnable); |
| 1480 | // End any intermediate reordering states |
| 1481 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | |
| 1484 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1485 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1486 | snapToDestination(); |
Sunny Goyal | 4ff424a | 2016-08-12 12:46:01 -0700 | [diff] [blame] | 1487 | onScrollInteractionEnd(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1488 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1489 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1490 | break; |
| 1491 | |
| 1492 | case MotionEvent.ACTION_POINTER_UP: |
| 1493 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1494 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | return true; |
| 1499 | } |
| 1500 | |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1501 | protected boolean shouldFlingForVelocity(int velocityX) { |
| 1502 | return Math.abs(velocityX) > mFlingThresholdVelocity; |
| 1503 | } |
| 1504 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1505 | private void resetTouchState() { |
| 1506 | releaseVelocityTracker(); |
| 1507 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1508 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1509 | mTouchState = TOUCH_STATE_REST; |
| 1510 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1513 | /** |
| 1514 | * Triggered by scrolling via touch |
| 1515 | */ |
| 1516 | protected void onScrollInteractionBegin() { |
| 1517 | } |
| 1518 | |
| 1519 | protected void onScrollInteractionEnd() { |
| 1520 | } |
| 1521 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1522 | protected void onUnhandledTap(MotionEvent ev) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 1523 | Launcher.getLauncher(getContext()).onClick(this); |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1524 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1525 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1526 | @Override |
| 1527 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1528 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1529 | switch (event.getAction()) { |
| 1530 | case MotionEvent.ACTION_SCROLL: { |
| 1531 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1532 | final float vscroll; |
| 1533 | final float hscroll; |
| 1534 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1535 | vscroll = 0; |
| 1536 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1537 | } else { |
| 1538 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1539 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1540 | } |
| 1541 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1542 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1543 | : (hscroll > 0 || vscroll > 0); |
| 1544 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1545 | scrollRight(); |
| 1546 | } else { |
| 1547 | scrollLeft(); |
| 1548 | } |
| 1549 | return true; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | return super.onGenericMotionEvent(event); |
| 1555 | } |
| 1556 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1557 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1558 | if (mVelocityTracker == null) { |
| 1559 | mVelocityTracker = VelocityTracker.obtain(); |
| 1560 | } |
| 1561 | mVelocityTracker.addMovement(ev); |
| 1562 | } |
| 1563 | |
| 1564 | private void releaseVelocityTracker() { |
| 1565 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1566 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1567 | mVelocityTracker.recycle(); |
| 1568 | mVelocityTracker = null; |
| 1569 | } |
| 1570 | } |
| 1571 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1572 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1573 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1574 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1575 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1576 | if (pointerId == mActivePointerId) { |
| 1577 | // This was our active pointer going up. Choose a new |
| 1578 | // active pointer and adjust accordingly. |
| 1579 | // TODO: Make this decision more intelligent. |
| 1580 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1581 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1582 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1583 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1584 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1585 | if (mVelocityTracker != null) { |
| 1586 | mVelocityTracker.clear(); |
| 1587 | } |
| 1588 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1591 | @Override |
| 1592 | public void requestChildFocus(View child, View focused) { |
| 1593 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1594 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1595 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1596 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1600 | int getPageNearestToCenterOfScreen() { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1601 | return getPageNearestToCenterOfScreen(getScrollX()); |
| 1602 | } |
| 1603 | |
| 1604 | private int getPageNearestToCenterOfScreen(int scaledScrollX) { |
| 1605 | int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1606 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1607 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1608 | final int childCount = getChildCount(); |
| 1609 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1610 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1611 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1612 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1613 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1614 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1615 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1616 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1617 | minDistanceFromScreenCenterIndex = i; |
| 1618 | } |
| 1619 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1620 | return minDistanceFromScreenCenterIndex; |
| 1621 | } |
| 1622 | |
| 1623 | protected void snapToDestination() { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1624 | snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration()); |
| 1625 | } |
| 1626 | |
| 1627 | protected boolean isInOverScroll() { |
| 1628 | return (mOverScrollX > mMaxScrollX || mOverScrollX < 0); |
| 1629 | } |
| 1630 | |
| 1631 | protected int getPageSnapDuration() { |
| 1632 | if (isInOverScroll()) { |
| 1633 | return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION; |
| 1634 | } |
| 1635 | return PAGE_SNAP_ANIMATION_DURATION; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1638 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1639 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1640 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1641 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1642 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1643 | f -= 0.5f; // center the values about 0. |
| 1644 | f *= 0.3f * Math.PI / 2.0f; |
| 1645 | return (float) Math.sin(f); |
| 1646 | } |
| 1647 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1648 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1649 | whichPage = validateNewPage(whichPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1650 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1651 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1652 | final int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1653 | int delta = newX - getUnboundedScrollX(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1654 | int duration = 0; |
| 1655 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1656 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1657 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1658 | // as opposed to an apparent physical response to flinging |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1659 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1664 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1665 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1666 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1667 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1668 | float distance = halfScreenSize + halfScreenSize * |
| 1669 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1670 | |
| 1671 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1672 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1673 | |
| 1674 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1675 | // 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] | 1676 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1677 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1678 | |
| 1679 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Sunny Goyal | 1740d90 | 2015-05-27 11:14:01 -0700 | [diff] [blame] | 1682 | public void snapToPage(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1683 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 1686 | public void snapToPageImmediately(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1687 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1690 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1691 | snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1694 | protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
| 1695 | snapToPage(whichPage, duration, false, interpolator); |
| 1696 | } |
| 1697 | |
| 1698 | protected void snapToPage(int whichPage, int duration, boolean immediate, |
| 1699 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1700 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1701 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1702 | int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1703 | final int delta = newX - getUnboundedScrollX(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1704 | snapToPage(whichPage, delta, duration, immediate, interpolator); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1708 | snapToPage(whichPage, delta, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1709 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1710 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1711 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate, |
| 1712 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1713 | whichPage = validateNewPage(whichPage); |
| 1714 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1715 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1716 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1717 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1718 | if (immediate) { |
| 1719 | duration = 0; |
| 1720 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1721 | duration = Math.abs(delta); |
| 1722 | } |
| 1723 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1724 | if (duration != 0) { |
| 1725 | pageBeginTransition(); |
| 1726 | } |
| 1727 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1728 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1729 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1730 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1731 | |
| 1732 | if (interpolator != null) { |
| 1733 | mScroller.setInterpolator(interpolator); |
| 1734 | } else { |
| 1735 | mScroller.setInterpolator(mDefaultInterpolator); |
| 1736 | } |
| 1737 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1738 | mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1739 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 1740 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1741 | |
| 1742 | // Trigger a compute() to finish switching pages if necessary |
| 1743 | if (immediate) { |
| 1744 | computeScroll(); |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 1745 | pageEndTransition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1748 | invalidate(); |
| 1749 | } |
| 1750 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1751 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 1752 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 1756 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1759 | @Override |
| 1760 | public boolean performLongClick() { |
| 1761 | mCancelTap = true; |
| 1762 | return super.performLongClick(); |
| 1763 | } |
| 1764 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1765 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1766 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1767 | |
| 1768 | SavedState(Parcelable superState) { |
| 1769 | super(superState); |
| 1770 | } |
| 1771 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1772 | @Thunk SavedState(Parcel in) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1773 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1774 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | @Override |
| 1778 | public void writeToParcel(Parcel out, int flags) { |
| 1779 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1780 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1784 | new Parcelable.Creator<SavedState>() { |
| 1785 | public SavedState createFromParcel(Parcel in) { |
| 1786 | return new SavedState(in); |
| 1787 | } |
| 1788 | |
| 1789 | public SavedState[] newArray(int size) { |
| 1790 | return new SavedState[size]; |
| 1791 | } |
| 1792 | }; |
| 1793 | } |
| 1794 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1795 | // Animate the drag view back to the original position |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1796 | private void animateDragViewToOriginalPosition() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1797 | if (mDragView != null) { |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 1798 | Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView, |
| 1799 | new PropertyListBuilder() |
| 1800 | .scale(1) |
| 1801 | .translationX(0) |
| 1802 | .translationY(0) |
| 1803 | .build()) |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1804 | .setDuration(REORDERING_DROP_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1805 | anim.addListener(new AnimatorListenerAdapter() { |
| 1806 | @Override |
| 1807 | public void onAnimationEnd(Animator animation) { |
| 1808 | onPostReorderingAnimationCompleted(); |
| 1809 | } |
| 1810 | }); |
| 1811 | anim.start(); |
| 1812 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1815 | public void onStartReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1816 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 1817 | mTouchState = TOUCH_STATE_REORDERING; |
| 1818 | mIsReordering = true; |
| 1819 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1820 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 1821 | // is always drawn on top |
| 1822 | invalidate(); |
| 1823 | } |
| 1824 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1825 | @Thunk void onPostReorderingAnimationCompleted() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1826 | // Trigger the callback when reordering has settled |
| 1827 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 1828 | if (mPostReorderingPreZoomInRunnable != null && |
| 1829 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 1830 | mPostReorderingPreZoomInRunnable.run(); |
| 1831 | mPostReorderingPreZoomInRunnable = null; |
| 1832 | } |
| 1833 | } |
| 1834 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1835 | public void onEndReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1836 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1839 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1840 | int dragViewIndex = indexOfChild(v); |
| 1841 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1842 | // Do not allow the first page to be moved around |
| 1843 | if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false; |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1844 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1845 | // Check if we are within the reordering range |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1846 | if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1847 | // Find the drag view under the pointer |
| 1848 | mDragView = getChildAt(dragViewIndex); |
| 1849 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 1850 | mDragViewBaselineLeft = mDragView.getLeft(); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1851 | mReorderingStarted = true; |
| 1852 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1853 | snapToPage(getPageNearestToCenterOfScreen()); |
| 1854 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1855 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1856 | return true; |
| 1857 | } |
| 1858 | return false; |
| 1859 | } |
| 1860 | |
| 1861 | boolean isReordering(boolean testTouchState) { |
| 1862 | boolean state = mIsReordering; |
| 1863 | if (testTouchState) { |
| 1864 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 1865 | } |
| 1866 | return state; |
| 1867 | } |
| 1868 | void endReordering() { |
| 1869 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 1870 | // In that case, we don't want to do anything. |
| 1871 | if (!mReorderingStarted) return; |
| 1872 | mReorderingStarted = false; |
| 1873 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1874 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 1875 | public void run() { |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 1876 | // If we haven't flung-to-delete the current child, |
| 1877 | // then we just animate the drag view back into position |
| 1878 | onEndReordering(); |
| 1879 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1880 | enableFreeScroll(); |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 1881 | } |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1882 | }; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1883 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1884 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 1885 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 1886 | // Snap to the current page |
| 1887 | snapToPage(indexOfChild(mDragView), 0); |
| 1888 | // Animate the drag view back to the front position |
| 1889 | animateDragViewToOriginalPosition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1892 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1893 | @SuppressWarnings("deprecation") |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1894 | @Override |
| 1895 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1896 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1897 | info.setScrollable(getPageCount() > 1); |
| 1898 | if (getCurrentPage() < getPageCount() - 1) { |
| 1899 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 1900 | } |
| 1901 | if (getCurrentPage() > 0) { |
| 1902 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 1903 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 1904 | info.setClassName(getClass().getName()); |
| 1905 | |
| 1906 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 1907 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 1908 | // accessibility focus. |
| 1909 | info.setLongClickable(false); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 1910 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 1914 | public void sendAccessibilityEvent(int eventType) { |
| 1915 | // Don't let the view send real scroll events. |
| 1916 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1917 | super.sendAccessibilityEvent(eventType); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1922 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1923 | super.onInitializeAccessibilityEvent(event); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 1924 | event.setScrollable(getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1927 | @Override |
| 1928 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1929 | if (super.performAccessibilityAction(action, arguments)) { |
| 1930 | return true; |
| 1931 | } |
| 1932 | switch (action) { |
| 1933 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 1934 | if (getCurrentPage() < getPageCount() - 1) { |
| 1935 | scrollRight(); |
| 1936 | return true; |
| 1937 | } |
| 1938 | } break; |
| 1939 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 1940 | if (getCurrentPage() > 0) { |
| 1941 | scrollLeft(); |
| 1942 | return true; |
| 1943 | } |
| 1944 | } break; |
| 1945 | } |
| 1946 | return false; |
| 1947 | } |
| 1948 | |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 1949 | protected String getPageIndicatorDescription() { |
| 1950 | return getCurrentPageDescription(); |
| 1951 | } |
| 1952 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1953 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 1954 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1955 | getNextPage() + 1, getChildCount()); |
| 1956 | } |
| 1957 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 1958 | @Override |
| 1959 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 1960 | return true; |
| 1961 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 1962 | } |