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