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