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