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