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