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