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