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