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