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