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