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 | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled; |
| 20 | import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType; |
| 21 | import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS; |
| 22 | import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR; |
| 23 | import static com.android.launcher3.touch.PagedOrientationHandler.CANVAS_TRANSLATE; |
| 24 | import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY; |
| 25 | import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO; |
| 26 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 27 | import android.animation.LayoutTransition; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 28 | import android.animation.TimeInterpolator; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 29 | import android.annotation.SuppressLint; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 31 | import android.content.res.TypedArray; |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 32 | import android.graphics.Canvas; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 35 | import android.provider.Settings; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 38 | import android.view.InputDevice; |
| 39 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 40 | import android.view.MotionEvent; |
| 41 | import android.view.VelocityTracker; |
| 42 | import android.view.View; |
| 43 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 44 | import android.view.ViewDebug; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 45 | import android.view.ViewGroup; |
| 46 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
| 48 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 49 | import android.view.animation.Interpolator; |
Vadim Tryshev | e6bbefb | 2018-04-03 13:38:06 -0700 | [diff] [blame] | 50 | import android.widget.ScrollView; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 51 | |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 52 | import androidx.annotation.Nullable; |
| 53 | |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 54 | import com.android.launcher3.anim.Interpolators; |
| 55 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
| 56 | import com.android.launcher3.config.FeatureFlags; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 57 | import com.android.launcher3.pageindicators.PageIndicator; |
| 58 | import com.android.launcher3.touch.OverScroll; |
| 59 | import com.android.launcher3.touch.PagedOrientationHandler; |
| 60 | import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 61 | import com.android.launcher3.util.OverScroller; |
| 62 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 63 | import com.android.launcher3.views.ActivityContext; |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 64 | |
| 65 | import java.util.ArrayList; |
| 66 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 67 | /** |
| 68 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 69 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 70 | */ |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 71 | public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 72 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 73 | private static final boolean DEBUG = false; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 74 | public static final boolean DEBUG_FAILED_QUICKSWITCH = false; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 75 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 76 | public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1; |
Sunny Goyal | a7a5bf3 | 2020-01-05 15:35:29 +0530 | [diff] [blame] | 77 | public static final int INVALID_PAGE = -1; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 78 | protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 79 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 80 | public static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 81 | |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 82 | // OverScroll constants |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 83 | private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 84 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 85 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 86 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 87 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 88 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 89 | private static final float MAX_SCROLL_PROGRESS = 1.0f; |
| 90 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 91 | // The following constants need to be scaled based on density. The scaled versions will be |
| 92 | // assigned to the corresponding member variables below. |
| 93 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 94 | private static final int EASY_FLING_THRESHOLD_VELOCITY = 400; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 95 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 96 | private static final int MIN_FLING_VELOCITY = 250; |
| 97 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 98 | private boolean mFreeScroll = false; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame^] | 99 | /** If {@code false}, disable swipe gesture to switch between pages. */ |
| 100 | private boolean mSwipeGestureEnabled = true; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 101 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 102 | protected final int mFlingThresholdVelocity; |
| 103 | protected final int mEasyFlingThresholdVelocity; |
| 104 | protected final int mMinFlingVelocity; |
| 105 | protected final int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 106 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 107 | protected boolean mFirstLayout = true; |
| 108 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 109 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 110 | protected int mCurrentPage; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 111 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 112 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 113 | protected int mNextPage = INVALID_PAGE; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 114 | protected int mMaxScroll; |
| 115 | protected int mMinScroll; |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 116 | protected OverScroller mScroller; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 117 | private Interpolator mDefaultInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 118 | private VelocityTracker mVelocityTracker; |
Sunny Goyal | e15e2a8 | 2017-12-15 13:05:42 -0800 | [diff] [blame] | 119 | protected int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 120 | |
| 121 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 122 | private float mDownMotionY; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 123 | private float mDownMotionPrimary; |
| 124 | private float mLastMotion; |
| 125 | private float mLastMotionRemainder; |
| 126 | private float mTotalMotion; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 127 | // Used in special cases where the fling checks can be relaxed for an intentional gesture |
| 128 | private boolean mAllowEasyFling; |
Sunny Goyal | acd17df | 2020-04-03 15:58:45 -0700 | [diff] [blame] | 129 | protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 130 | |
Vadim Tryshev | e628c47 | 2018-05-16 14:28:18 -0700 | [diff] [blame] | 131 | protected int[] mPageScrolls; |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 132 | private boolean mIsBeingDragged; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 133 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 134 | // The amount of movement to begin scrolling |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 135 | protected int mTouchSlop; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 136 | // The amount of movement to begin paging |
| 137 | protected int mPageSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 138 | private int mMaximumVelocity; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 139 | protected boolean mAllowOverScroll = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 140 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 141 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 142 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 143 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 144 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 145 | protected boolean mIsPageInTransition = false; |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 146 | private Runnable mOnPageTransitionEndCallback; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 147 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 148 | protected float mSpringOverScroll; |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 149 | |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 150 | protected boolean mWasInOverscroll = false; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 151 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 152 | protected int mUnboundedScroll; |
| 153 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 154 | // Page Indicator |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 155 | @Thunk int mPageIndicatorViewId; |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 156 | protected T mPageIndicator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 157 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 158 | protected final Rect mInsets = new Rect(); |
Tony Wickham | dfb5cc9 | 2018-02-26 16:41:57 -0800 | [diff] [blame] | 159 | protected boolean mIsRtl; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 160 | |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 161 | // Similar to the platform implementation of isLayoutValid(); |
| 162 | protected boolean mIsLayoutValid; |
| 163 | |
Vadim Tryshev | 528b9e0 | 2018-05-24 18:22:03 -0700 | [diff] [blame] | 164 | private int[] mTmpIntPair = new int[2]; |
| 165 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 166 | public PagedView(Context context) { |
| 167 | this(context, null); |
| 168 | } |
| 169 | |
| 170 | public PagedView(Context context, AttributeSet attrs) { |
| 171 | this(context, attrs, 0); |
| 172 | } |
| 173 | |
| 174 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 175 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 176 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 177 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 178 | R.styleable.PagedView, defStyle, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 179 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 180 | a.recycle(); |
| 181 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 182 | setHapticFeedbackEnabled(false); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 183 | mIsRtl = Utilities.isRtl(getResources()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 184 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 185 | mScroller = new OverScroller(context); |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 186 | setDefaultInterpolator(Interpolators.SCROLL); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 187 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 188 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 189 | final ViewConfiguration configuration = ViewConfiguration.get(context); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 190 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 191 | mPageSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 192 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 193 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 194 | float density = getResources().getDisplayMetrics().density; |
| 195 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 196 | mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density); |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 197 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density); |
| 198 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density); |
Sunny Goyal | a9f5546 | 2018-03-25 18:15:15 -0700 | [diff] [blame] | 199 | |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 200 | setDefaultFocusHighlightEnabled(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 203 | protected void setDefaultInterpolator(Interpolator interpolator) { |
| 204 | mDefaultInterpolator = interpolator; |
| 205 | mScroller.setInterpolator(mDefaultInterpolator); |
| 206 | } |
| 207 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 208 | public void initParentViews(View parent) { |
| 209 | if (mPageIndicatorViewId > -1) { |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 210 | mPageIndicator = parent.findViewById(mPageIndicatorViewId); |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 211 | mPageIndicator.setMarkersCount(getChildCount()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 215 | public T getPageIndicator() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 216 | return mPageIndicator; |
| 217 | } |
| 218 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 219 | /** |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 220 | * Returns the index of the currently displayed page. When in free scroll mode, this is the page |
| 221 | * that the user was on before entering free scroll mode (e.g. the home screen page they |
| 222 | * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()} |
| 223 | * to get the page the user is currently scrolling over. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 224 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 225 | public int getCurrentPage() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 226 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 227 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 228 | |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Returns the index of page to be shown immediately afterwards. |
| 231 | */ |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 232 | public int getNextPage() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 233 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 234 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 235 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 236 | public int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 237 | return getChildCount(); |
| 238 | } |
| 239 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 240 | public View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 241 | return getChildAt(index); |
| 242 | } |
| 243 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 244 | protected int indexToPage(int index) { |
| 245 | return index; |
| 246 | } |
| 247 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 248 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 249 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 250 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 251 | * the previous tab page. |
| 252 | */ |
| 253 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 254 | // If the current page is invalid, just reset the scroll position to zero |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 255 | int newPosition = 0; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 256 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 257 | newPosition = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 258 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 259 | mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition); |
| 260 | mOrientationHandler.scrollerStartScroll(mScroller, newPosition); |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 261 | forceFinishScroller(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 264 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 265 | mScroller.abortAnimation(); |
| 266 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 267 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 268 | if (resetNextPage) { |
| 269 | mNextPage = INVALID_PAGE; |
Sunny Goyal | 857b1b9 | 2018-03-05 10:49:05 -0800 | [diff] [blame] | 270 | pageEndTransition(); |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 271 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 272 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 273 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 274 | private void forceFinishScroller(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 275 | mScroller.forceFinished(true); |
| 276 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 277 | // updating current page on the pass. |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 278 | if (resetNextPage) { |
| 279 | mNextPage = INVALID_PAGE; |
Sunny Goyal | 857b1b9 | 2018-03-05 10:49:05 -0800 | [diff] [blame] | 280 | pageEndTransition(); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 281 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 284 | private int validateNewPage(int newPage) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 285 | newPage = ensureWithinScrollBounds(newPage); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 286 | // 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] | 287 | return Utilities.boundToRange(newPage, 0, getPageCount() - 1); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 290 | /** |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 291 | * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX. |
| 292 | */ |
| 293 | private int ensureWithinScrollBounds(int page) { |
| 294 | int dir = !mIsRtl ? 1 : - 1; |
| 295 | int currScroll = getScrollForPage(page); |
| 296 | int prevScroll; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 297 | while (currScroll < mMinScroll) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 298 | page += dir; |
| 299 | prevScroll = currScroll; |
| 300 | currScroll = getScrollForPage(page); |
| 301 | if (currScroll <= prevScroll) { |
| 302 | Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX"); |
| 303 | break; |
| 304 | } |
| 305 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 306 | while (currScroll > mMaxScroll) { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 307 | page -= dir; |
| 308 | prevScroll = currScroll; |
| 309 | currScroll = getScrollForPage(page); |
| 310 | if (currScroll >= prevScroll) { |
| 311 | Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX"); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | return page; |
| 316 | } |
| 317 | |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 318 | public void setCurrentPage(int currentPage) { |
| 319 | setCurrentPage(currentPage, INVALID_PAGE); |
| 320 | } |
| 321 | |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 322 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 323 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 324 | */ |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 325 | public void setCurrentPage(int currentPage, int overridePrevPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 326 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 327 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 328 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 329 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 330 | // the default |
| 331 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 332 | return; |
| 333 | } |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 334 | int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 335 | mCurrentPage = validateNewPage(currentPage); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 336 | updateCurrentPageScroll(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 337 | notifyPageSwitchListener(prevPage); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 338 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 341 | /** |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 342 | * Should be called whenever the page changes. In the case of a scroll, we wait until the page |
| 343 | * has settled. |
| 344 | */ |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 345 | protected void notifyPageSwitchListener(int prevPage) { |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 346 | updatePageIndicator(); |
| 347 | } |
| 348 | |
| 349 | private void updatePageIndicator() { |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 350 | if (mPageIndicator != null) { |
Tony Mak | 4f521af | 2018-03-15 16:37:34 +0000 | [diff] [blame] | 351 | mPageIndicator.setActiveMarker(getNextPage()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 352 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 353 | } |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 354 | protected void pageBeginTransition() { |
| 355 | if (!mIsPageInTransition) { |
| 356 | mIsPageInTransition = true; |
| 357 | onPageBeginTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 358 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 361 | protected void pageEndTransition() { |
| 362 | if (mIsPageInTransition) { |
| 363 | mIsPageInTransition = false; |
| 364 | onPageEndTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 365 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 368 | protected boolean isPageInTransition() { |
| 369 | return mIsPageInTransition; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Called when the page starts moving as part of the scroll. Subclasses can override this |
| 374 | * to provide custom behavior during animation. |
| 375 | */ |
| 376 | protected void onPageBeginTransition() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 379 | /** |
| 380 | * Called when the page ends moving as part of the scroll. Subclasses can override this |
| 381 | * to provide custom behavior during animation. |
| 382 | */ |
| 383 | protected void onPageEndTransition() { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 384 | mWasInOverscroll = false; |
vadimt | 4d93df5 | 2019-07-10 17:32:58 -0700 | [diff] [blame] | 385 | AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext()); |
Tony Wickham | 619daaf | 2020-02-07 12:29:06 -0800 | [diff] [blame] | 386 | AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage), |
| 387 | AccessibilityEvent.TYPE_VIEW_FOCUSED, null); |
Tony Wickham | 62117d7 | 2020-03-26 16:56:26 -0700 | [diff] [blame] | 388 | if (mOnPageTransitionEndCallback != null) { |
| 389 | mOnPageTransitionEndCallback.run(); |
| 390 | mOnPageTransitionEndCallback = null; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Sets a callback to run once when the scrolling finishes. If there is currently |
| 396 | * no page in transition, then the callback is called immediately. |
| 397 | */ |
| 398 | public void setOnPageTransitionEndCallback(@Nullable Runnable callback) { |
| 399 | if (mIsPageInTransition || callback == null) { |
| 400 | mOnPageTransitionEndCallback = callback; |
| 401 | } else { |
| 402 | callback.run(); |
| 403 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 406 | protected int getUnboundedScroll() { |
| 407 | return mUnboundedScroll; |
| 408 | } |
| 409 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 410 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 411 | public void scrollBy(int x, int y) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 412 | mOrientationHandler.delegateScrollBy(this, getUnboundedScroll(), x, y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 416 | public void scrollTo(int x, int y) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 417 | int primaryScroll = mOrientationHandler.getPrimaryValue(x, y); |
| 418 | int secondaryScroll = mOrientationHandler.getSecondaryValue(x, y); |
| 419 | mUnboundedScroll = primaryScroll; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 420 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 421 | boolean isBeforeFirstPage = mIsRtl ? |
| 422 | (primaryScroll > mMaxScroll) : (primaryScroll < mMinScroll); |
| 423 | boolean isAfterLastPage = mIsRtl ? |
| 424 | (primaryScroll < mMinScroll) : (primaryScroll > mMaxScroll); |
| 425 | if (!isBeforeFirstPage && !isAfterLastPage) { |
| 426 | mSpringOverScroll = 0; |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 429 | if (isBeforeFirstPage) { |
| 430 | mOrientationHandler.delegateScrollTo(this, |
| 431 | secondaryScroll, mIsRtl ? mMaxScroll : mMinScroll); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 432 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 433 | mWasInOverscroll = true; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 434 | overScroll(primaryScroll - (mIsRtl ? mMaxScroll : mMinScroll)); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 435 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 436 | } else if (isAfterLastPage) { |
| 437 | mOrientationHandler.delegateScrollTo(this, |
| 438 | secondaryScroll, mIsRtl ? mMinScroll : mMaxScroll); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 439 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 440 | mWasInOverscroll = true; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 441 | overScroll(primaryScroll - (mIsRtl ? mMinScroll : mMaxScroll)); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 442 | } |
| 443 | } else { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 444 | if (mWasInOverscroll) { |
| 445 | overScroll(0); |
| 446 | mWasInOverscroll = false; |
| 447 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 448 | super.scrollTo(x, y); |
| 449 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 450 | } |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 451 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 452 | /** |
| 453 | * Helper for {@link PagedOrientationHandler} to be able to call parent's scrollTo method |
| 454 | */ |
| 455 | public void superScrollTo(int x, int y) { |
| 456 | super.scrollTo(x, y); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 459 | private void sendScrollAccessibilityEvent() { |
Sunny Goyal | d0030b0 | 2017-12-08 15:07:24 -0800 | [diff] [blame] | 460 | if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) { |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 461 | if (mCurrentPage != getNextPage()) { |
| 462 | AccessibilityEvent ev = |
| 463 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 464 | ev.setScrollable(true); |
| 465 | ev.setScrollX(getScrollX()); |
| 466 | ev.setScrollY(getScrollY()); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 467 | mOrientationHandler.setMaxScroll(ev, mMaxScroll); |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 468 | sendAccessibilityEventUnchecked(ev); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 469 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 473 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 474 | protected boolean computeScrollHelper() { |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 475 | return computeScrollHelper(true); |
| 476 | } |
| 477 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 478 | protected void announcePageForAccessibility() { |
| 479 | if (isAccessibilityEnabled(getContext())) { |
| 480 | // Notify the user when the page changes |
| 481 | announceForAccessibility(getCurrentPageDescription()); |
| 482 | } |
| 483 | } |
| 484 | |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 485 | protected boolean computeScrollHelper(boolean shouldInvalidate) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 486 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 487 | // Don't bother scrolling if the page does not need to be moved |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 488 | int currentScroll = mOrientationHandler.getPrimaryScroll(this); |
| 489 | if (mUnboundedScroll != mScroller.getCurrPos() |
| 490 | || currentScroll != mScroller.getCurrPos()) { |
| 491 | mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrPos()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 492 | } |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 493 | if (shouldInvalidate) { |
| 494 | invalidate(); |
| 495 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 496 | return true; |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 497 | } else if (mNextPage != INVALID_PAGE && shouldInvalidate) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 498 | sendScrollAccessibilityEvent(); |
| 499 | |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 500 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 501 | mCurrentPage = validateNewPage(mNextPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 502 | mNextPage = INVALID_PAGE; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 503 | notifyPageSwitchListener(prevPage); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 504 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 505 | // We don't want to trigger a page end moving unless the page has settled |
| 506 | // and the user has stopped scrolling |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 507 | if (!mIsBeingDragged) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 508 | pageEndTransition(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 509 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 510 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 511 | if (canAnnouncePageDescription()) { |
| 512 | announcePageForAccessibility(); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 513 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 514 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void computeScroll() { |
| 520 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 523 | public int getExpectedHeight() { |
| 524 | return getMeasuredHeight(); |
| 525 | } |
| 526 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 527 | public int getNormalChildHeight() { |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 528 | return getExpectedHeight() - getPaddingTop() - getPaddingBottom() |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 529 | - mInsets.top - mInsets.bottom; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 532 | public int getExpectedWidth() { |
| 533 | return getMeasuredWidth(); |
| 534 | } |
| 535 | |
Sunny Goyal | bc683e9 | 2017-12-06 10:25:07 -0800 | [diff] [blame] | 536 | public int getNormalChildWidth() { |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 537 | return getExpectedWidth() - getPaddingLeft() - getPaddingRight() |
Sunny Goyal | bc683e9 | 2017-12-06 10:25:07 -0800 | [diff] [blame] | 538 | - mInsets.left - mInsets.right; |
| 539 | } |
| 540 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 541 | @Override |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 542 | public void requestLayout() { |
| 543 | mIsLayoutValid = false; |
| 544 | super.requestLayout(); |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void forceLayout() { |
| 549 | mIsLayoutValid = false; |
| 550 | super.forceLayout(); |
| 551 | } |
| 552 | |
| 553 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 554 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 555 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 556 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 557 | return; |
| 558 | } |
| 559 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 560 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 561 | // 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] | 562 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 563 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 564 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 565 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 566 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 567 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 568 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 569 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 572 | // Return early if we aren't given a proper dimension |
| 573 | if (widthSize <= 0 || heightSize <= 0) { |
| 574 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 575 | return; |
| 576 | } |
| 577 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 578 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 579 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 580 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 581 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 582 | int myWidthSpec = MeasureSpec.makeMeasureSpec( |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 583 | widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY); |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 584 | int myHeightSpec = MeasureSpec.makeMeasureSpec( |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 585 | heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 586 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 587 | // measureChildren takes accounts for content padding, we only need to care about extra |
| 588 | // space due to insets. |
| 589 | measureChildren(myWidthSpec, myHeightSpec); |
| 590 | setMeasuredDimension(widthSize, heightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 593 | @SuppressLint("DrawAllocation") |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 594 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 595 | 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] | 596 | mIsLayoutValid = true; |
Vadim Tryshev | eb701c6 | 2018-05-14 16:00:15 -0700 | [diff] [blame] | 597 | final int childCount = getChildCount(); |
| 598 | boolean pageScrollChanged = false; |
| 599 | if (mPageScrolls == null || childCount != mPageScrolls.length) { |
| 600 | mPageScrolls = new int[childCount]; |
| 601 | pageScrollChanged = true; |
| 602 | } |
| 603 | |
| 604 | if (childCount == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 608 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 609 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 610 | boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC); |
| 611 | if (isScrollChanged) { |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 612 | pageScrollChanged = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 613 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 614 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 615 | final LayoutTransition transition = getLayoutTransition(); |
| 616 | // If the transition is running defer updating max scroll, as some empty pages could |
| 617 | // still be present, and a max scroll change could cause sudden jumps in scroll. |
| 618 | if (transition != null && transition.isRunning()) { |
| 619 | transition.addTransitionListener(new LayoutTransition.TransitionListener() { |
| 620 | |
| 621 | @Override |
| 622 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 623 | View view, int transitionType) { } |
| 624 | |
| 625 | @Override |
| 626 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 627 | View view, int transitionType) { |
| 628 | // Wait until all transitions are complete. |
| 629 | if (!transition.isRunning()) { |
| 630 | transition.removeTransitionListener(this); |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 631 | updateMinAndMaxScrollX(); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | }); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 635 | } else { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 636 | updateMinAndMaxScrollX(); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 639 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) { |
| 640 | updateCurrentPageScroll(); |
| 641 | mFirstLayout = false; |
| 642 | } |
| 643 | |
Sunny Goyal | 85a7fe0 | 2018-02-02 10:09:32 -0800 | [diff] [blame] | 644 | if (mScroller.isFinished() && pageScrollChanged) { |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 645 | setCurrentPage(getNextPage()); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 646 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 649 | /** |
| 650 | * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length |
| 651 | * of {@code outPageScrolls} should be same as the the childCount |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 652 | */ |
| 653 | protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren, |
| 654 | ComputePageScrollsLogic scrollLogic) { |
| 655 | final int childCount = getChildCount(); |
| 656 | |
| 657 | final int startIndex = mIsRtl ? childCount - 1 : 0; |
| 658 | final int endIndex = mIsRtl ? -1 : childCount; |
| 659 | final int delta = mIsRtl ? -1 : 1; |
| 660 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 661 | final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 662 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 663 | final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets); |
| 664 | final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 665 | boolean pageScrollChanged = false; |
| 666 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 667 | for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) { |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 668 | final View child = getPageAt(i); |
| 669 | if (scrollLogic.shouldIncludeView(child)) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 670 | ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart, |
| 671 | pageCenter, layoutChildren); |
| 672 | final int primaryDimension = bounds.primaryDimension; |
| 673 | final int childPrimaryEnd = bounds.childPrimaryEnd; |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 674 | |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 675 | // In case the pages are of different width, align the page to left or right edge |
| 676 | // based on the orientation. |
| 677 | final int pageScroll = mIsRtl |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 678 | ? (childStart - scrollOffsetStart) |
| 679 | : Math.max(0, childPrimaryEnd - scrollOffsetEnd); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 680 | if (outPageScrolls[i] != pageScroll) { |
| 681 | pageScrollChanged = true; |
| 682 | outPageScrolls[i] = pageScroll; |
| 683 | } |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 684 | childStart += primaryDimension + mPageSpacing + getChildGap(); |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | return pageScrollChanged; |
| 688 | } |
| 689 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 690 | protected int getChildGap() { |
| 691 | return 0; |
| 692 | } |
| 693 | |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 694 | protected void updateMinAndMaxScrollX() { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 695 | mMinScroll = computeMinScroll(); |
| 696 | mMaxScroll = computeMaxScroll(); |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 699 | protected int computeMinScroll() { |
Tony Wickham | 66d1c2f | 2019-04-24 11:32:59 -0700 | [diff] [blame] | 700 | return 0; |
Tony | 50876bf | 2018-09-19 23:09:14 -0400 | [diff] [blame] | 701 | } |
| 702 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 703 | protected int computeMaxScroll() { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 704 | int childCount = getChildCount(); |
| 705 | if (childCount > 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 706 | final int index = mIsRtl ? 0 : childCount - 1; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 707 | return getScrollForPage(index); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 708 | } else { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 709 | return 0; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 713 | public void setPageSpacing(int pageSpacing) { |
| 714 | mPageSpacing = pageSpacing; |
| 715 | requestLayout(); |
| 716 | } |
| 717 | |
Tony | 50876bf | 2018-09-19 23:09:14 -0400 | [diff] [blame] | 718 | public int getPageSpacing() { |
| 719 | return mPageSpacing; |
| 720 | } |
| 721 | |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 722 | private void dispatchPageCountChanged() { |
| 723 | if (mPageIndicator != null) { |
| 724 | mPageIndicator.setMarkersCount(getChildCount()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 725 | } |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 726 | // This ensures that when children are added, they get the correct transforms / alphas |
| 727 | // in accordance with any scroll effects. |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 728 | invalidate(); |
| 729 | } |
| 730 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 731 | @Override |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 732 | public void onViewAdded(View child) { |
Winson Chung | c7c5158 | 2018-02-23 17:58:36 -0800 | [diff] [blame] | 733 | super.onViewAdded(child); |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 734 | dispatchPageCountChanged(); |
| 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public void onViewRemoved(View child) { |
Winson Chung | c7c5158 | 2018-02-23 17:58:36 -0800 | [diff] [blame] | 739 | super.onViewRemoved(child); |
Tony | a361c72 | 2017-07-04 09:43:06 -0700 | [diff] [blame] | 740 | mCurrentPage = validateNewPage(mCurrentPage); |
Sunny Goyal | 0bd7f4f | 2018-02-01 09:53:35 -0800 | [diff] [blame] | 741 | dispatchPageCountChanged(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 744 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 745 | if (index < 0 || index > getChildCount() - 1) return 0; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 746 | View pageAtIndex = getPageAt(index); |
| 747 | return mOrientationHandler.getChildStart(pageAtIndex); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Alex Chau | 7944ee5 | 2021-01-08 19:07:25 +0000 | [diff] [blame] | 750 | protected int getChildVisibleSize(int index) { |
| 751 | View layout = getPageAt(index); |
| 752 | return mOrientationHandler.getMeasuredSize(layout); |
| 753 | } |
| 754 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 755 | @Override |
| 756 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 757 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 758 | if (page != mCurrentPage || !mScroller.isFinished()) { |
Vadim Tryshev | eeaa8ee | 2018-04-11 12:23:42 -0700 | [diff] [blame] | 759 | if (immediate) { |
| 760 | setCurrentPage(page); |
| 761 | } else { |
| 762 | snapToPage(page); |
| 763 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 764 | return true; |
| 765 | } |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | @Override |
| 770 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 771 | int focusablePage; |
| 772 | if (mNextPage != INVALID_PAGE) { |
| 773 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 774 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 775 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 776 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 777 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 778 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 779 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 780 | } |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 786 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | if (mIsRtl) { |
| 791 | if (direction == View.FOCUS_LEFT) { |
| 792 | direction = View.FOCUS_RIGHT; |
| 793 | } else if (direction == View.FOCUS_RIGHT) { |
| 794 | direction = View.FOCUS_LEFT; |
| 795 | } |
| 796 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 797 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 798 | if (getCurrentPage() > 0) { |
| 799 | snapToPage(getCurrentPage() - 1); |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 800 | getChildAt(getCurrentPage() - 1).requestFocus(direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 801 | return true; |
| 802 | } |
| 803 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 804 | if (getCurrentPage() < getPageCount() - 1) { |
| 805 | snapToPage(getCurrentPage() + 1); |
Sunny Goyal | c82c639 | 2018-06-06 15:39:13 -0700 | [diff] [blame] | 806 | getChildAt(getCurrentPage() + 1).requestFocus(direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 807 | return true; |
| 808 | } |
| 809 | } |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 810 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | @Override |
| 814 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Jon Miranda | 6a85817 | 2016-10-25 16:19:17 -0700 | [diff] [blame] | 815 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 816 | return; |
| 817 | } |
| 818 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 819 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 820 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 821 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 822 | } |
| 823 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 824 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 825 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 826 | } |
| 827 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 828 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 829 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * 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] | 836 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 837 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 838 | * This happens when live folders requery, and if they're off page, they |
| 839 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 840 | */ |
| 841 | @Override |
| 842 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 843 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 844 | View v = focused; |
| 845 | while (true) { |
| 846 | if (v == current) { |
| 847 | super.focusableViewAvailable(focused); |
| 848 | return; |
| 849 | } |
| 850 | if (v == this) { |
| 851 | return; |
| 852 | } |
| 853 | ViewParent parent = v.getParent(); |
| 854 | if (parent instanceof View) { |
| 855 | v = (View)v.getParent(); |
| 856 | } else { |
| 857 | return; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /** |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame^] | 863 | * If {@code enableSwipeGesture} is {@code true}, enables swipe gesture to navigate between |
| 864 | * pages. Otherwise, disables the navigation gesture. |
| 865 | */ |
| 866 | public void setSwipeGestureEnabled(boolean swipeGestureEnabled) { |
| 867 | mSwipeGestureEnabled = swipeGestureEnabled; |
| 868 | } |
| 869 | |
| 870 | /** |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 871 | * {@inheritDoc} |
| 872 | */ |
| 873 | @Override |
| 874 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 875 | if (disallowIntercept) { |
| 876 | // We need to make sure to cancel our long press if |
| 877 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 878 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 879 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 880 | } |
| 881 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 882 | } |
| 883 | |
| 884 | @Override |
| 885 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 886 | /* |
| 887 | * This method JUST determines whether we want to intercept the motion. |
| 888 | * If we return true, onTouchEvent will be called and we do the actual |
| 889 | * scrolling there. |
| 890 | */ |
| 891 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame^] | 892 | if (!mSwipeGestureEnabled) return false; |
| 893 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 894 | // Skip touch handling if there are no pages to swipe |
Tony Wickham | b6841ac | 2019-07-22 12:39:59 -0700 | [diff] [blame] | 895 | if (getChildCount() <= 0) return false; |
Sunny Goyal | c4bb373 | 2019-06-20 11:34:14 -0700 | [diff] [blame] | 896 | |
| 897 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 898 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 899 | /* |
| 900 | * Shortcut the most recurring case: the user is in the dragging |
| 901 | * state and he is moving his finger. We want to intercept this |
| 902 | * motion. |
| 903 | */ |
| 904 | final int action = ev.getAction(); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 905 | if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 906 | return true; |
| 907 | } |
| 908 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 909 | switch (action & MotionEvent.ACTION_MASK) { |
| 910 | case MotionEvent.ACTION_MOVE: { |
| 911 | /* |
| 912 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 913 | * whether the user has moved far enough from their original down touch. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 914 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 915 | if (mActivePointerId != INVALID_POINTER) { |
| 916 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 917 | } |
| 918 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 919 | // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 920 | // i.e. fall through to the next case (don't break) |
| 921 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 922 | // 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] | 923 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | case MotionEvent.ACTION_DOWN: { |
| 927 | final float x = ev.getX(); |
| 928 | final float y = ev.getY(); |
| 929 | // Remember location of down touch |
| 930 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 931 | mDownMotionY = y; |
Jon Miranda | f52af43 | 2020-04-10 17:25:57 -0700 | [diff] [blame] | 932 | mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 933 | mLastMotionRemainder = 0; |
| 934 | mTotalMotion = 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 935 | mAllowEasyFling = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 936 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 937 | |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 938 | updateIsBeingDraggedOnTouchDown(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 939 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 943 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 944 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 945 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 946 | break; |
| 947 | |
| 948 | case MotionEvent.ACTION_POINTER_UP: |
| 949 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 950 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 951 | break; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * The only time we want to intercept motion events is if we are in the |
| 956 | * drag mode. |
| 957 | */ |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 958 | return mIsBeingDragged; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 961 | /** |
| 962 | * If being flinged and user touches the screen, initiate drag; otherwise don't. |
| 963 | */ |
| 964 | private void updateIsBeingDraggedOnTouchDown() { |
| 965 | // mScroller.isFinished should be false when being flinged. |
| 966 | final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos()); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 967 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3); |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 968 | |
| 969 | if (finishedScrolling) { |
| 970 | mIsBeingDragged = false; |
| 971 | if (!mScroller.isFinished() && !mFreeScroll) { |
| 972 | setCurrentPage(getNextPage()); |
| 973 | pageEndTransition(); |
| 974 | } |
| 975 | } else { |
| 976 | mIsBeingDragged = true; |
| 977 | } |
| 978 | } |
| 979 | |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 980 | public boolean isHandlingTouch() { |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 981 | return mIsBeingDragged; |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 984 | protected void determineScrollingStart(MotionEvent ev) { |
| 985 | determineScrollingStart(ev, 1.0f); |
| 986 | } |
| 987 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 988 | /* |
| 989 | * Determines if we should change the touch state to start scrolling after the |
| 990 | * user moves their touch point too far. |
| 991 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 992 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 993 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 994 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 995 | if (pointerIndex == -1) return; |
| 996 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 997 | final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex); |
| 998 | final int diff = (int) Math.abs(primaryDirection - mLastMotion); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 999 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1000 | boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1001 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1002 | if (moved) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1003 | // Scroll if the user moved far enough along the X axis |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1004 | mIsBeingDragged = true; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1005 | mTotalMotion += Math.abs(mLastMotion - primaryDirection); |
| 1006 | mLastMotion = primaryDirection; |
| 1007 | mLastMotionRemainder = 0; |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1008 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1009 | pageBeginTransition(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1010 | // Stop listening for things like pinches. |
| 1011 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1016 | // Try canceling the long press. It could also have been scheduled |
| 1017 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1018 | // everything |
| 1019 | final View currentPage = getPageAt(mCurrentPage); |
| 1020 | if (currentPage != null) { |
| 1021 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1025 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1026 | final int halfScreenSize = getMeasuredWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1027 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1028 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1029 | int count = getChildCount(); |
| 1030 | |
| 1031 | final int totalDistance; |
| 1032 | |
| 1033 | int adjacentPage = page + 1; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1034 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1035 | adjacentPage = page - 1; |
| 1036 | } |
| 1037 | |
| 1038 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1039 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1040 | } else { |
| 1041 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1042 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1043 | |
| 1044 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1045 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
| 1046 | scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1047 | return scrollProgress; |
| 1048 | } |
| 1049 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1050 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1051 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | } else { |
| 1054 | return mPageScrolls[index]; |
| 1055 | } |
| 1056 | } |
| 1057 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1058 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1059 | // position. This method returns the magnitude of this stray at any given time. |
| 1060 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1061 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1062 | return 0; |
| 1063 | } else { |
| 1064 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1065 | |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1066 | int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
| 1067 | int baselineX = mPageScrolls[index] + scrollOffset; |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1068 | return (int) (child.getX() - baselineX); |
| 1069 | } |
| 1070 | } |
| 1071 | |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1072 | @Override |
| 1073 | protected void dispatchDraw(Canvas canvas) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1074 | if (mScroller.isSpringing() && mSpringOverScroll != 0) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1075 | int saveCount = canvas.save(); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1076 | mOrientationHandler.set(canvas, CANVAS_TRANSLATE, -mSpringOverScroll); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1077 | super.dispatchDraw(canvas); |
| 1078 | |
| 1079 | canvas.restoreToCount(saveCount); |
| 1080 | } else { |
| 1081 | super.dispatchDraw(canvas); |
| 1082 | } |
| 1083 | } |
| 1084 | |
Jon Miranda | 57dab6f | 2020-04-10 13:38:14 -0700 | [diff] [blame] | 1085 | /** |
| 1086 | * Returns the amount of overscroll caused by the spring in {@link OverScroller}. |
| 1087 | */ |
| 1088 | private int getSpringOverScroll(int amount) { |
| 1089 | if (mScroller.isSpringing()) { |
| 1090 | return amount < 0 |
Tony Wickham | eb88502 | 2020-04-30 23:27:39 -0500 | [diff] [blame] | 1091 | ? mScroller.getCurrPos() - mMinScroll |
Jon Miranda | 57dab6f | 2020-04-10 13:38:14 -0700 | [diff] [blame] | 1092 | : Math.max(0, mScroller.getCurrPos() - mMaxScroll); |
| 1093 | } else { |
| 1094 | return 0; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1098 | protected void dampedOverScroll(int amount) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1099 | if (amount == 0) { |
| 1100 | return; |
| 1101 | } |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1102 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1103 | int size = mOrientationHandler.getMeasuredSize(this); |
| 1104 | int overScrollAmount = OverScroll.dampedScroll(amount, size); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1105 | if (mScroller.isSpringing()) { |
Jon Miranda | 57dab6f | 2020-04-10 13:38:14 -0700 | [diff] [blame] | 1106 | mSpringOverScroll = getSpringOverScroll(amount); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1107 | invalidate(); |
| 1108 | return; |
| 1109 | } |
| 1110 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1111 | int primaryScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1112 | int boundedScroll = Utilities.boundToRange(primaryScroll, mMinScroll, mMaxScroll); |
| 1113 | mOrientationHandler.delegateScrollTo(this, boundedScroll + overScrollAmount); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1114 | invalidate(); |
| 1115 | } |
| 1116 | |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1117 | protected void overScroll(int amount) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1118 | if (mScroller.isSpringing()) { |
Jon Miranda | 57dab6f | 2020-04-10 13:38:14 -0700 | [diff] [blame] | 1119 | mSpringOverScroll = getSpringOverScroll(amount); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1120 | invalidate(); |
| 1121 | return; |
| 1122 | } |
| 1123 | |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1124 | if (amount == 0) return; |
| 1125 | |
| 1126 | if (mFreeScroll && !mScroller.isFinished()) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1127 | int scrollAmount = amount < 0 ? mMinScroll + amount : mMaxScroll + amount; |
| 1128 | mOrientationHandler.delegateScrollTo(this, scrollAmount); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1129 | } else { |
| 1130 | dampedOverScroll(amount); |
| 1131 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1134 | |
Tony Wickham | 023f404 | 2019-01-29 10:52:31 -0800 | [diff] [blame] | 1135 | public void setEnableFreeScroll(boolean freeScroll) { |
Winson Chung | 0e44900 | 2019-04-26 11:09:58 -0700 | [diff] [blame] | 1136 | if (mFreeScroll == freeScroll) { |
| 1137 | return; |
| 1138 | } |
| 1139 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1140 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1141 | mFreeScroll = freeScroll; |
| 1142 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1143 | if (mFreeScroll) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame] | 1144 | setCurrentPage(getNextPage()); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1145 | } else if (wasFreeScroll) { |
Tony Wickham | af33f2c | 2019-10-04 12:55:22 -0700 | [diff] [blame] | 1146 | if (getScrollForPage(getNextPage()) != getScrollX()) { |
| 1147 | snapToPage(getNextPage()); |
| 1148 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1149 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1152 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1153 | mAllowOverScroll = enable; |
| 1154 | } |
| 1155 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1156 | @Override |
| 1157 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1158 | // Skip touch handling if there are no pages to swipe |
Tony Wickham | b6841ac | 2019-07-22 12:39:59 -0700 | [diff] [blame] | 1159 | if (getChildCount() <= 0) return false; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1160 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1161 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1162 | |
| 1163 | final int action = ev.getAction(); |
| 1164 | |
| 1165 | switch (action & MotionEvent.ACTION_MASK) { |
| 1166 | case MotionEvent.ACTION_DOWN: |
Tony Wickham | a0aee21 | 2019-09-18 09:24:03 -0700 | [diff] [blame] | 1167 | updateIsBeingDraggedOnTouchDown(); |
| 1168 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1169 | /* |
| 1170 | * If being flinged and user touches, stop the fling. isFinished |
| 1171 | * will be false if being flinged. |
| 1172 | */ |
| 1173 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1174 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // Remember where the motion event started |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1178 | mDownMotionX = ev.getX(); |
Sunny Goyal | 7c7be8c | 2018-03-07 19:58:07 -0800 | [diff] [blame] | 1179 | mDownMotionY = ev.getY(); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1180 | mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0); |
| 1181 | mLastMotionRemainder = 0; |
| 1182 | mTotalMotion = 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1183 | mAllowEasyFling = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1184 | mActivePointerId = ev.getPointerId(0); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1185 | if (mIsBeingDragged) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1186 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1187 | pageBeginTransition(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1188 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1189 | break; |
| 1190 | |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1191 | case ACTION_MOVE_ALLOW_EASY_FLING: |
| 1192 | // Start scrolling immediately |
| 1193 | determineScrollingStart(ev); |
| 1194 | mAllowEasyFling = true; |
| 1195 | break; |
| 1196 | |
| 1197 | case MotionEvent.ACTION_MOVE: |
| 1198 | if (mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1199 | // Scroll to follow the motion event |
| 1200 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1201 | |
| 1202 | if (pointerIndex == -1) return true; |
| 1203 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1204 | float direction = mOrientationHandler.getPrimaryDirection(ev, pointerIndex); |
| 1205 | float delta = mLastMotion + mLastMotionRemainder - direction; |
| 1206 | mTotalMotion += Math.abs(delta); |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1207 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1208 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1209 | // keep the remainder because we are actually testing if we've moved from the last |
| 1210 | // scrolled position (which is discrete). |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1211 | if (Math.abs(delta) >= 1.0f) { |
| 1212 | mLastMotion = direction; |
| 1213 | mLastMotionRemainder = delta - (int) delta; |
| 1214 | |
| 1215 | mOrientationHandler.set(this, VIEW_SCROLL_BY, (int) delta); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1216 | } else { |
| 1217 | awakenScrollBars(); |
| 1218 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1219 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1220 | determineScrollingStart(ev); |
| 1221 | } |
| 1222 | break; |
| 1223 | |
| 1224 | case MotionEvent.ACTION_UP: |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1225 | if (mIsBeingDragged) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1226 | final int activePointerId = mActivePointerId; |
| 1227 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
Vinit Nayak | 93bf1fe | 2020-05-26 17:22:13 -0700 | [diff] [blame] | 1228 | if (pointerIndex == -1) return true; |
| 1229 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1230 | final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, |
| 1231 | pointerIndex); |
Sunny Goyal | 4bb8b06 | 2019-02-19 16:37:38 -0800 | [diff] [blame] | 1232 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1233 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1234 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1235 | int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker, |
| 1236 | mActivePointerId); |
| 1237 | int delta = (int) (primaryDirection - mDownMotionPrimary); |
| 1238 | int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage)); |
| 1239 | |
| 1240 | boolean isSignificantMove = Math.abs(delta) > pageOrientedSize * |
| 1241 | SIGNIFICANT_MOVE_THRESHOLD; |
| 1242 | |
| 1243 | mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection); |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1244 | boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop; |
| 1245 | boolean isFling = passedSlop && shouldFlingForVelocity(velocity); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1246 | boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0; |
| 1247 | boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0; |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1248 | if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) { |
| 1249 | Log.d("Quickswitch", "isFling=false vel=" + velocity |
| 1250 | + " threshold=" + mEasyFlingThresholdVelocity); |
| 1251 | } |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1252 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1253 | if (!mFreeScroll) { |
| 1254 | // In the case that the page is moved far to one direction and then is flung |
| 1255 | // in the opposite direction, we use a threshold to determine whether we should |
| 1256 | // just return to the starting page, or if we should skip one further. |
| 1257 | boolean returnToOriginalPage = false; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1258 | if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1259 | Math.signum(velocity) != Math.signum(delta) && isFling) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1260 | returnToOriginalPage = true; |
| 1261 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1262 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1263 | int finalPage; |
| 1264 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1265 | // test for a large move if a fling has been registered. That is, a large |
| 1266 | // move to the left and fling to the right will register as a fling to the right. |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1267 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1268 | if (((isSignificantMove && !isDeltaLeft && !isFling) || |
| 1269 | (isFling && !isVelocityLeft)) && mCurrentPage > 0) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1270 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1271 | snapToPageWithVelocity(finalPage, velocity); |
| 1272 | } else if (((isSignificantMove && isDeltaLeft && !isFling) || |
| 1273 | (isFling && isVelocityLeft)) && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1274 | mCurrentPage < getChildCount() - 1) { |
| 1275 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1276 | snapToPageWithVelocity(finalPage, velocity); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1277 | } else { |
| 1278 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1279 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1280 | } else { |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1281 | if (!mScroller.isFinished()) { |
| 1282 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1283 | } |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1284 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1285 | int initialScroll = mOrientationHandler.getPrimaryScroll(this); |
| 1286 | int maxScroll = mMaxScroll; |
| 1287 | int minScroll = mMinScroll; |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1288 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1289 | if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) || |
| 1290 | ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) { |
| 1291 | mScroller.springBack(initialScroll, minScroll, maxScroll); |
Tony Wickham | 312209b | 2019-06-18 15:54:29 -0700 | [diff] [blame] | 1292 | mNextPage = getPageNearestToCenterOfScreen(); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1293 | } else { |
| 1294 | mScroller.setInterpolator(mDefaultInterpolator); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1295 | mScroller.fling(initialScroll, -velocity, |
| 1296 | minScroll, maxScroll, |
| 1297 | Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR)); |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1298 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1299 | int finalPos = mScroller.getFinalPos(); |
| 1300 | mNextPage = getPageNearestToCenterOfScreen(finalPos); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1301 | |
| 1302 | int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1); |
| 1303 | int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1304 | if (finalPos > minScroll && finalPos < maxScroll) { |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1305 | // If scrolling ends in the half of the added space that is closer to |
| 1306 | // the end, settle to the end. Otherwise snap to the nearest page. |
| 1307 | // If flinging past one of the ends, don't change the velocity as it |
| 1308 | // will get stopped at the end anyway. |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1309 | int pageSnapped = finalPos < (firstPageScroll + minScroll) / 2 |
| 1310 | ? minScroll |
| 1311 | : finalPos > (lastPageScroll + maxScroll) / 2 |
| 1312 | ? maxScroll |
| 1313 | : getScrollForPage(mNextPage); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1314 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1315 | mScroller.setFinalPos(pageSnapped); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1316 | // Ensure the scroll/snap doesn't happen too fast; |
| 1317 | int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1318 | - mScroller.getDuration(); |
Sunny Goyal | b7b0135 | 2018-08-09 12:31:20 -0700 | [diff] [blame] | 1319 | if (extraScrollDuration > 0) { |
| 1320 | mScroller.extendDuration(extraScrollDuration); |
| 1321 | } |
Santiago Etchebehere | 260b4cb | 2018-06-15 16:42:19 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1325 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1326 | onScrollInteractionEnd(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1327 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1328 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1329 | // End any intermediate reordering states |
| 1330 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1331 | break; |
| 1332 | |
| 1333 | case MotionEvent.ACTION_CANCEL: |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1334 | if (mIsBeingDragged) { |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1335 | snapToDestination(); |
Sunny Goyal | 4ff424a | 2016-08-12 12:46:01 -0700 | [diff] [blame] | 1336 | onScrollInteractionEnd(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1337 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1338 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1339 | break; |
| 1340 | |
| 1341 | case MotionEvent.ACTION_POINTER_UP: |
| 1342 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1343 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1344 | break; |
| 1345 | } |
| 1346 | |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1350 | protected boolean shouldFlingForVelocity(int velocity) { |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1351 | float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity; |
| 1352 | return Math.abs(velocity) > threshold; |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1355 | private void resetTouchState() { |
| 1356 | releaseVelocityTracker(); |
Sunny Goyal | aad3359 | 2018-08-07 17:20:35 -0700 | [diff] [blame] | 1357 | mIsBeingDragged = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1358 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1361 | /** |
| 1362 | * Triggered by scrolling via touch |
| 1363 | */ |
| 1364 | protected void onScrollInteractionBegin() { |
| 1365 | } |
| 1366 | |
| 1367 | protected void onScrollInteractionEnd() { |
| 1368 | } |
| 1369 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1370 | @Override |
| 1371 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1372 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1373 | switch (event.getAction()) { |
| 1374 | case MotionEvent.ACTION_SCROLL: { |
| 1375 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1376 | final float vscroll; |
| 1377 | final float hscroll; |
| 1378 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1379 | vscroll = 0; |
| 1380 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1381 | } else { |
| 1382 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1383 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1384 | } |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 1385 | if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) { |
| 1386 | return false; |
Samuel Fufa | 59cba19 | 2019-08-22 18:31:34 -0700 | [diff] [blame] | 1387 | } |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1388 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1389 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1390 | : (hscroll > 0 || vscroll > 0); |
| 1391 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1392 | scrollRight(); |
| 1393 | } else { |
| 1394 | scrollLeft(); |
| 1395 | } |
| 1396 | return true; |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | return super.onGenericMotionEvent(event); |
| 1402 | } |
| 1403 | |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 1404 | /** |
| 1405 | * Returns true if the paged view can scroll for the provided vertical and horizontal |
| 1406 | * scroll values |
| 1407 | */ |
| 1408 | protected boolean canScroll(float absVScroll, float absHScroll) { |
| 1409 | ActivityContext ac = ActivityContext.lookupContext(getContext()); |
| 1410 | return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null); |
Samuel Fufa | 59cba19 | 2019-08-22 18:31:34 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1413 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1414 | if (mVelocityTracker == null) { |
| 1415 | mVelocityTracker = VelocityTracker.obtain(); |
| 1416 | } |
| 1417 | mVelocityTracker.addMovement(ev); |
| 1418 | } |
| 1419 | |
| 1420 | private void releaseVelocityTracker() { |
| 1421 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1422 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1423 | mVelocityTracker.recycle(); |
| 1424 | mVelocityTracker = null; |
| 1425 | } |
| 1426 | } |
| 1427 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1428 | private void onSecondaryPointerUp(MotionEvent ev) { |
Winson Chung | 4941f65 | 2020-06-18 15:11:53 -0700 | [diff] [blame] | 1429 | final int pointerIndex = ev.getActionIndex(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1430 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1431 | if (pointerId == mActivePointerId) { |
| 1432 | // This was our active pointer going up. Choose a new |
| 1433 | // active pointer and adjust accordingly. |
| 1434 | // TODO: Make this decision more intelligent. |
| 1435 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1436 | mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev, |
| 1437 | newPointerIndex); |
| 1438 | mLastMotionRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1439 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1440 | if (mVelocityTracker != null) { |
| 1441 | mVelocityTracker.clear(); |
| 1442 | } |
| 1443 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1446 | @Override |
| 1447 | public void requestChildFocus(View child, View focused) { |
| 1448 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1449 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1450 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1451 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
Sunny Goyal | 228153d | 2018-01-04 15:35:22 -0800 | [diff] [blame] | 1455 | public int getPageNearestToCenterOfScreen() { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1456 | return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this)); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1459 | private int getPageNearestToCenterOfScreen(int scaledScroll) { |
| 1460 | int pageOrientationSize = mOrientationHandler.getMeasuredSize(this); |
| 1461 | int screenCenter = scaledScroll + (pageOrientationSize / 2); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1462 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1463 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1464 | final int childCount = getChildCount(); |
| 1465 | for (int i = 0; i < childCount; ++i) { |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1466 | int distanceFromScreenCenter = Math.abs( |
| 1467 | getDisplacementFromScreenCenter(i, screenCenter)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1468 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1469 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1470 | minDistanceFromScreenCenterIndex = i; |
| 1471 | } |
| 1472 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1473 | return minDistanceFromScreenCenterIndex; |
| 1474 | } |
| 1475 | |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1476 | private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) { |
Alex Chau | 7944ee5 | 2021-01-08 19:07:25 +0000 | [diff] [blame] | 1477 | int childSize = getChildVisibleSize(childIndex); |
Tony Wickham | 7383d4e | 2020-07-07 19:25:25 -0700 | [diff] [blame] | 1478 | int halfChildSize = (childSize / 2); |
| 1479 | int childCenter = getChildOffset(childIndex) + halfChildSize; |
| 1480 | return childCenter - screenCenter; |
| 1481 | } |
| 1482 | |
| 1483 | protected int getDisplacementFromScreenCenter(int childIndex) { |
| 1484 | int pageOrientationSize = mOrientationHandler.getMeasuredSize(this); |
| 1485 | int screenCenter = mOrientationHandler.getPrimaryScroll(this) + (pageOrientationSize / 2); |
| 1486 | return getDisplacementFromScreenCenter(childIndex, screenCenter); |
| 1487 | } |
| 1488 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1489 | protected void snapToDestination() { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1490 | snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration()); |
| 1491 | } |
| 1492 | |
| 1493 | protected boolean isInOverScroll() { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1494 | int scroll = mOrientationHandler.getPrimaryScroll(this); |
| 1495 | return scroll > mMaxScroll || scroll < mMinScroll; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | protected int getPageSnapDuration() { |
| 1499 | if (isInOverScroll()) { |
| 1500 | return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION; |
| 1501 | } |
| 1502 | return PAGE_SNAP_ANIMATION_DURATION; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1505 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1506 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1507 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1508 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1509 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1510 | f -= 0.5f; // center the values about 0. |
| 1511 | f *= 0.3f * Math.PI / 2.0f; |
| 1512 | return (float) Math.sin(f); |
| 1513 | } |
| 1514 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1515 | protected boolean snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1516 | whichPage = validateNewPage(whichPage); |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1517 | int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1518 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1519 | final int newLoc = getScrollForPage(whichPage); |
| 1520 | int delta = newLoc - getUnboundedScroll(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1521 | int duration = 0; |
| 1522 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1523 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1524 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1525 | // as opposed to an apparent physical response to flinging |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1526 | return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1530 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1531 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1532 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1533 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1534 | float distance = halfScreenSize + halfScreenSize * |
| 1535 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1536 | |
| 1537 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1538 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1539 | |
| 1540 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1541 | // 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] | 1542 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1543 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1544 | |
Jon Miranda | 9e19866 | 2020-03-11 14:42:02 -0700 | [diff] [blame] | 1545 | if (QUICKSTEP_SPRINGS.get() && mCurrentPage != whichPage) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1546 | return snapToPage(whichPage, delta, duration, false, null, |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1547 | velocity * Math.signum(delta), true); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1548 | } else { |
| 1549 | return snapToPage(whichPage, delta, duration); |
| 1550 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1553 | public boolean snapToPage(int whichPage) { |
| 1554 | return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1557 | public boolean snapToPageImmediately(int whichPage) { |
| 1558 | return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1561 | public boolean snapToPage(int whichPage, int duration) { |
| 1562 | return snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 1565 | public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1566 | return snapToPage(whichPage, duration, false, interpolator); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1569 | protected boolean snapToPage(int whichPage, int duration, boolean immediate, |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1570 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1571 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1572 | |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1573 | int newLoc = getScrollForPage(whichPage); |
| 1574 | final int delta = newLoc - getUnboundedScroll(); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1575 | return snapToPage(whichPage, delta, duration, immediate, interpolator, 0, false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1578 | protected boolean snapToPage(int whichPage, int delta, int duration) { |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1579 | return snapToPage(whichPage, delta, duration, false, null, 0, false); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1580 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1581 | |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1582 | protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate, |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1583 | TimeInterpolator interpolator, float velocity, boolean spring) { |
Sunny Goyal | 9bb8ffb | 2018-05-24 17:11:16 -0700 | [diff] [blame] | 1584 | if (mFirstLayout) { |
| 1585 | setCurrentPage(whichPage); |
| 1586 | return false; |
| 1587 | } |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 1588 | |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 1589 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Tony Wickham | cb3f870 | 2018-08-27 17:36:03 -0700 | [diff] [blame] | 1590 | duration *= Settings.Global.getFloat(getContext().getContentResolver(), |
| 1591 | Settings.Global.WINDOW_ANIMATION_SCALE, 1); |
Tony Wickham | d58c2d5 | 2018-05-04 12:10:55 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1594 | whichPage = validateNewPage(whichPage); |
| 1595 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1596 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1597 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1598 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1599 | if (immediate) { |
| 1600 | duration = 0; |
| 1601 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1602 | duration = Math.abs(delta); |
| 1603 | } |
| 1604 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1605 | if (duration != 0) { |
| 1606 | pageBeginTransition(); |
| 1607 | } |
| 1608 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1609 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1610 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1611 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1612 | |
| 1613 | if (interpolator != null) { |
| 1614 | mScroller.setInterpolator(interpolator); |
| 1615 | } else { |
| 1616 | mScroller.setInterpolator(mDefaultInterpolator); |
| 1617 | } |
| 1618 | |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1619 | if (spring && QUICKSTEP_SPRINGS.get()) { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1620 | mScroller.startScrollSpring(getUnboundedScroll(), delta, duration, velocity); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1621 | } else { |
Vinit Nayak | a406f72 | 2019-12-19 11:50:55 -0800 | [diff] [blame] | 1622 | mScroller.startScroll(getUnboundedScroll(), delta, duration); |
Jon Miranda | 71cb80c | 2019-01-24 21:25:13 -0800 | [diff] [blame] | 1623 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1624 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 1625 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1626 | |
| 1627 | // Trigger a compute() to finish switching pages if necessary |
| 1628 | if (immediate) { |
| 1629 | computeScroll(); |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 1630 | pageEndTransition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1633 | invalidate(); |
Winson Chung | 05a31ed | 2018-02-08 17:08:43 -0800 | [diff] [blame] | 1634 | return Math.abs(delta) > 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1637 | public boolean scrollLeft() { |
| 1638 | if (getNextPage() > 0) { |
| 1639 | snapToPage(getNextPage() - 1); |
| 1640 | return true; |
| 1641 | } |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1642 | return onOverscroll(-getMeasuredWidth()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1645 | public boolean scrollRight() { |
| 1646 | if (getNextPage() < getChildCount() - 1) { |
| 1647 | snapToPage(getNextPage() + 1); |
| 1648 | return true; |
| 1649 | } |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1650 | return onOverscroll(getMeasuredWidth()); |
| 1651 | } |
| 1652 | |
| 1653 | protected boolean onOverscroll(int amount) { |
| 1654 | if (!mAllowOverScroll) return false; |
| 1655 | onScrollInteractionBegin(); |
| 1656 | overScroll(amount); |
| 1657 | onScrollInteractionEnd(); |
| 1658 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Vadim Tryshev | e6bbefb | 2018-04-03 13:38:06 -0700 | [diff] [blame] | 1661 | @Override |
| 1662 | public CharSequence getAccessibilityClassName() { |
| 1663 | // Some accessibility services have special logic for ScrollView. Since we provide same |
| 1664 | // accessibility info as ScrollView, inform the service to handle use the same way. |
| 1665 | return ScrollView.class.getName(); |
| 1666 | } |
| 1667 | |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1668 | protected boolean isPageOrderFlipped() { |
| 1669 | return false; |
| 1670 | } |
| 1671 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1672 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1673 | @SuppressWarnings("deprecation") |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1674 | @Override |
| 1675 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1676 | super.onInitializeAccessibilityNodeInfo(info); |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1677 | final boolean pagesFlipped = isPageOrderFlipped(); |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1678 | int offset = (mAllowOverScroll ? 0 : 1); |
| 1679 | info.setScrollable(getPageCount() > offset); |
| 1680 | if (getCurrentPage() < getPageCount() - offset) { |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1681 | info.addAction(pagesFlipped ? |
| 1682 | AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD |
| 1683 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD); |
| 1684 | info.addAction(mIsRtl ? |
| 1685 | AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT |
| 1686 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1687 | } |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1688 | if (getCurrentPage() >= offset) { |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1689 | info.addAction(pagesFlipped ? |
| 1690 | AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD |
| 1691 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD); |
| 1692 | info.addAction(mIsRtl ? |
| 1693 | AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT |
| 1694 | : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1695 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 1696 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 1697 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 1698 | // accessibility focus. |
| 1699 | info.setLongClickable(false); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 1700 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 1704 | public void sendAccessibilityEvent(int eventType) { |
| 1705 | // Don't let the view send real scroll events. |
| 1706 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1707 | super.sendAccessibilityEvent(eventType); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1712 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1713 | super.onInitializeAccessibilityEvent(event); |
Pinyao Ting | af6daa2 | 2019-09-18 22:18:03 +0000 | [diff] [blame] | 1714 | event.setScrollable(mAllowOverScroll || getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1717 | @Override |
| 1718 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1719 | if (super.performAccessibilityAction(action, arguments)) { |
| 1720 | return true; |
| 1721 | } |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1722 | final boolean pagesFlipped = isPageOrderFlipped(); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1723 | switch (action) { |
| 1724 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1725 | if (pagesFlipped ? scrollLeft() : scrollRight()) { |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1726 | return true; |
| 1727 | } |
| 1728 | } break; |
| 1729 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
Vadim Tryshev | 98913d0 | 2018-05-18 18:41:34 -0700 | [diff] [blame] | 1730 | if (pagesFlipped ? scrollRight() : scrollLeft()) { |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1731 | return true; |
| 1732 | } |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1733 | } break; |
| 1734 | case android.R.id.accessibilityActionPageRight: { |
| 1735 | if (!mIsRtl) { |
| 1736 | return scrollRight(); |
| 1737 | } else { |
| 1738 | return scrollLeft(); |
| 1739 | } |
Vadim Tryshev | 0fc0713 | 2018-05-04 12:08:54 -0700 | [diff] [blame] | 1740 | } |
yingleiw | 02cc848 | 2019-08-02 16:14:27 -0700 | [diff] [blame] | 1741 | case android.R.id.accessibilityActionPageLeft: { |
| 1742 | if (!mIsRtl) { |
| 1743 | return scrollLeft(); |
| 1744 | } else { |
| 1745 | return scrollRight(); |
| 1746 | } |
| 1747 | } |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1748 | } |
| 1749 | return false; |
| 1750 | } |
| 1751 | |
Vadim Tryshev | 2c430b3 | 2018-04-04 14:45:21 -0700 | [diff] [blame] | 1752 | protected boolean canAnnouncePageDescription() { |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1756 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 1757 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1758 | getNextPage() + 1, getChildCount()); |
| 1759 | } |
| 1760 | |
Tony Mak | 2e56440 | 2018-02-27 17:19:34 +0000 | [diff] [blame] | 1761 | protected float getDownMotionX() { |
| 1762 | return mDownMotionX; |
| 1763 | } |
| 1764 | |
| 1765 | protected float getDownMotionY() { |
| 1766 | return mDownMotionY; |
| 1767 | } |
| 1768 | |
Sunny Goyal | 20a13ff | 2018-03-13 16:44:00 -0700 | [diff] [blame] | 1769 | protected interface ComputePageScrollsLogic { |
| 1770 | |
| 1771 | boolean shouldIncludeView(View view); |
| 1772 | } |
Vadim Tryshev | 528b9e0 | 2018-05-24 18:22:03 -0700 | [diff] [blame] | 1773 | |
| 1774 | public int[] getVisibleChildrenRange() { |
| 1775 | float visibleLeft = 0; |
| 1776 | float visibleRight = visibleLeft + getMeasuredWidth(); |
| 1777 | float scaleX = getScaleX(); |
| 1778 | if (scaleX < 1 && scaleX > 0) { |
| 1779 | float mid = getMeasuredWidth() / 2; |
| 1780 | visibleLeft = mid - ((mid - visibleLeft) / scaleX); |
| 1781 | visibleRight = mid + ((visibleRight - mid) / scaleX); |
| 1782 | } |
| 1783 | |
| 1784 | int leftChild = -1; |
| 1785 | int rightChild = -1; |
| 1786 | final int childCount = getChildCount(); |
| 1787 | for (int i = 0; i < childCount; i++) { |
| 1788 | final View child = getPageAt(i); |
| 1789 | |
| 1790 | float left = child.getLeft() + child.getTranslationX() - getScrollX(); |
| 1791 | if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) { |
| 1792 | if (leftChild == -1) { |
| 1793 | leftChild = i; |
| 1794 | } |
| 1795 | rightChild = i; |
| 1796 | } |
| 1797 | } |
| 1798 | mTmpIntPair[0] = leftChild; |
| 1799 | mTmpIntPair[1] = rightChild; |
| 1800 | return mTmpIntPair; |
| 1801 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 1802 | } |