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