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