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