blob: 394b950c43c9dec29fbfa90e9d8e867a63a7cae5 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyald0030b02017-12-08 15:07:24 -080019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyalb7b01352018-08-09 12:31:20 -070021import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyald0030b02017-12-08 15:07:24 -080022
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070023import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070024import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070025import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070029import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070030import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.ViewGroup;
41import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070045import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070046
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070047import com.android.launcher3.anim.Interpolators;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070048import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070050import com.android.launcher3.touch.OverScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -070051import com.android.launcher3.util.OverScroller;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080060public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070063
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070065 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Vadim Tryshevfedca432015-08-19 17:55:02 -070067 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Sunny Goyalb72d8b22017-07-14 00:02:27 -070069 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070070 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Sunny Goyal8e2133b2015-05-06 13:39:07 -070076 private static final float MAX_SCROLL_PROGRESS = 1.0f;
77
Adam Cohen265b9a62011-12-07 14:37:18 -080078 // The following constants need to be scaled based on density. The scaled versions will be
79 // assigned to the corresponding member variables below.
80 private static final int FLING_THRESHOLD_VELOCITY = 500;
81 private static final int MIN_SNAP_VELOCITY = 1500;
82 private static final int MIN_FLING_VELOCITY = 250;
83
Adam Cohen21cd0022013-10-09 18:57:02 -070084 public static final int INVALID_RESTORE_PAGE = -1001;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070087
Adam Cohen265b9a62011-12-07 14:37:18 -080088 protected int mFlingThresholdVelocity;
89 protected int mMinFlingVelocity;
90 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected boolean mFirstLayout = true;
93
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected int mCurrentPage;
Adam Cohene61a9a22013-06-11 15:45:31 -070096
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -080099 protected int mMaxScrollX;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700100 protected OverScroller mScroller;
Adam Cohenf9618852013-11-08 06:45:03 -0800101 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800103 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104
105 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700106 private float mDownMotionY;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700107 private float mLastMotionX;
108 private float mLastMotionXRemainder;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700109 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700110
Vadim Trysheve628c472018-05-16 14:28:18 -0700111 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700112 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800113
Michael Jurka7426c422010-11-11 15:23:47 -0800114 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800116 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka5f1c5092010-09-03 14:15:02 -0700118 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka5f1c5092010-09-03 14:15:02 -0700120 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700122 protected boolean mIsPageInTransition = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700123
Sunny Goyal061380a2016-02-29 15:15:03 -0800124 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700125
Adam Cohen8d769d62017-06-23 17:27:38 -0700126 protected int mUnboundedScrollX;
127
Winson Chungd2be3812013-07-16 11:11:32 -0700128 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800130 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700133 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700134
John Spurlock77e1f472013-09-11 10:09:51 -0400135 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800136 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400137
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800138 // Similar to the platform implementation of isLayoutValid();
139 protected boolean mIsLayoutValid;
140
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700141 private int[] mTmpIntPair = new int[2];
142
Winson Chung321e9ee2010-08-09 13:37:56 -0700143 public PagedView(Context context) {
144 this(context, null);
145 }
146
147 public PagedView(Context context, AttributeSet attrs) {
148 this(context, attrs, 0);
149 }
150
151 public PagedView(Context context, AttributeSet attrs, int defStyle) {
152 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700153
Adam Cohen9c4949e2010-10-05 12:27:22 -0700154 TypedArray a = context.obtainStyledAttributes(attrs,
155 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700156 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700157 a.recycle();
158
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700160 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700161 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 }
163
164 /**
165 * Initializes various states for this workspace.
166 */
Michael Jurka0142d492010-08-25 17:46:15 -0700167 protected void init() {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700168 mScroller = new OverScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700169 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700170 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
172 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800175
Sunny Goyalcf25b522015-07-09 00:01:18 -0700176 float density = getResources().getDisplayMetrics().density;
177 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
178 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
179 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700180
181 if (Utilities.ATLEAST_OREO) {
182 setDefaultFocusHighlightEnabled(false);
183 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 }
185
Adam Cohenf9618852013-11-08 06:45:03 -0800186 protected void setDefaultInterpolator(Interpolator interpolator) {
187 mDefaultInterpolator = interpolator;
188 mScroller.setInterpolator(mDefaultInterpolator);
189 }
190
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700191 public void initParentViews(View parent) {
192 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800193 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700194 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700195 }
196 }
197
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800198 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700199 return mPageIndicator;
200 }
201
Adam Cohen674531f2013-12-13 15:07:14 -0800202 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700203 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
204 * that the user was on before entering free scroll mode (e.g. the home screen page they
205 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
206 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700208 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700209 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700211
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700212 /**
213 * Returns the index of page to be shown immediately afterwards.
214 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700215 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700216 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
217 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700218
Adam Cohenf9c184a2016-01-15 16:47:43 -0800219 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 return getChildCount();
221 }
222
Sunny Goyal83a8f042015-05-19 12:52:12 -0700223 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 return getChildAt(index);
225 }
226
Adam Cohenae4f1552011-10-20 00:15:42 -0700227 protected int indexToPage(int index) {
228 return index;
229 }
230
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800232 * Updates the scroll of the current page immediately to its final scroll position. We use this
233 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
234 * the previous tab page.
235 */
236 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700237 // If the current page is invalid, just reset the scroll position to zero
238 int newX = 0;
239 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700240 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700241 }
Sunny Goyalc82c6392018-06-06 15:39:13 -0700242 scrollTo(newX, 0);
Sunny Goyalb7b01352018-08-09 12:31:20 -0700243 mScroller.startScroll(mScroller.getCurrPos(), newX - mScroller.getCurrPos());
Sunny Goyalc82c6392018-06-06 15:39:13 -0700244 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 }
246
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700247 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700248 mScroller.abortAnimation();
249 // We need to clean up the next page here to avoid computeScrollHelper from
250 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700251 if (resetNextPage) {
252 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800253 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700254 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700255 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700256
Tony Wickham8f7ead32016-04-07 18:46:44 -0700257 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700258 mScroller.forceFinished(true);
259 // We need to clean up the next page here to avoid computeScrollHelper from
260 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700261 if (resetNextPage) {
262 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800263 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700264 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700265 }
266
Adam Cohen6f127a62014-06-12 14:54:41 -0700267 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700268 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700269 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700270 }
271
Chet Haasebc2f0822012-10-26 17:59:53 -0700272 /**
Winson Chung86f77532010-08-24 11:08:22 -0700273 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700275 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800276 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700277 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800278 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800279 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
280 // the default
281 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800282 return;
283 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700284 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700285 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700286 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700287 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800288 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 }
290
Winson Chung8c87cd82013-07-23 16:20:10 -0700291 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800292 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
293 * has settled.
294 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700295 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800296 updatePageIndicator();
297 }
298
299 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700300 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000301 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700302 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700304 protected void pageBeginTransition() {
305 if (!mIsPageInTransition) {
306 mIsPageInTransition = true;
307 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700308 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700309 }
310
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700311 protected void pageEndTransition() {
312 if (mIsPageInTransition) {
313 mIsPageInTransition = false;
314 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700315 }
Michael Jurka0142d492010-08-25 17:46:15 -0700316 }
317
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700318 protected boolean isPageInTransition() {
319 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700320 }
321
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700322 /**
323 * Called when the page starts moving as part of the scroll. Subclasses can override this
324 * to provide custom behavior during animation.
325 */
326 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700327 }
328
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700329 /**
330 * Called when the page ends moving as part of the scroll. Subclasses can override this
331 * to provide custom behavior during animation.
332 */
333 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700334 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700335 }
336
Sunny Goyalc86df472016-02-25 09:19:38 -0800337 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700338 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800339 }
340
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800342 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800343 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800344 }
345
346 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700347 public void scrollTo(int x, int y) {
Adam Cohen8d769d62017-06-23 17:27:38 -0700348 mUnboundedScrollX = x;
349
Sunny Goyal70660032015-05-14 00:07:08 -0700350 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
351 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700352 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700353 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800354 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700355 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700356 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700357 overScroll(x - mMaxScrollX);
358 } else {
359 overScroll(x);
360 }
Adam Cohen68d73932010-11-15 10:50:58 -0800361 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700362 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700363 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800364 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700365 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700366 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700367 overScroll(x);
368 } else {
369 overScroll(x - mMaxScrollX);
370 }
Adam Cohen68d73932010-11-15 10:50:58 -0800371 }
372 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700373 if (mWasInOverscroll) {
374 overScroll(0);
375 mWasInOverscroll = false;
376 }
Adam Cohen68d73932010-11-15 10:50:58 -0800377 super.scrollTo(x, y);
378 }
Sunny Goyalb7b01352018-08-09 12:31:20 -0700379
Michael Jurka0142d492010-08-25 17:46:15 -0700380 }
381
Adam Cohen53805212013-10-01 10:39:23 -0700382 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800383 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700384 if (mCurrentPage != getNextPage()) {
385 AccessibilityEvent ev =
386 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700387 ev.setScrollable(true);
388 ev.setScrollX(getScrollX());
389 ev.setScrollY(getScrollY());
390 ev.setMaxScrollX(mMaxScrollX);
391 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700392
Vadim Tryshevf4715972015-05-08 17:21:03 -0700393 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700394 }
Adam Cohen53805212013-10-01 10:39:23 -0700395 }
396 }
397
Michael Jurka0142d492010-08-25 17:46:15 -0700398 // we moved this functionality to a helper function so SmoothPagedView can reuse it
399 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800400 return computeScrollHelper(true);
401 }
402
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700403 protected void announcePageForAccessibility() {
404 if (isAccessibilityEnabled(getContext())) {
405 // Notify the user when the page changes
406 announceForAccessibility(getCurrentPageDescription());
407 }
408 }
409
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800410 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700412 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalb7b01352018-08-09 12:31:20 -0700413 if (getUnboundedScrollX() != mScroller.getCurrPos()
414 || getScrollX() != mScroller.getCurrPos()) {
415 scrollTo(mScroller.getCurrPos(), 0);
Winson Chung557d6ed2011-07-08 15:34:52 -0700416 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800417 if (shouldInvalidate) {
418 invalidate();
419 }
Michael Jurka0142d492010-08-25 17:46:15 -0700420 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800421 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700422 sendScrollAccessibilityEvent();
423
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700424 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700425 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700426 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700427 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700428
Adam Cohen73aa9752010-11-24 16:26:58 -0800429 // We don't want to trigger a page end moving unless the page has settled
430 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700431 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700432 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800433 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700434
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700435 if (canAnnouncePageDescription()) {
436 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700437 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
Michael Jurka0142d492010-08-25 17:46:15 -0700439 return false;
440 }
441
442 @Override
443 public void computeScroll() {
444 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 }
446
Sunny Goyalce8809a2018-01-18 14:02:47 -0800447 public int getExpectedHeight() {
448 return getMeasuredHeight();
449 }
450
Adam Cohen410f3cd2013-09-22 12:09:32 -0700451 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800452 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800453 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700454 }
455
Sunny Goyalce8809a2018-01-18 14:02:47 -0800456 public int getExpectedWidth() {
457 return getMeasuredWidth();
458 }
459
Sunny Goyalbc683e92017-12-06 10:25:07 -0800460 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800461 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800462 - mInsets.left - mInsets.right;
463 }
464
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800466 public void requestLayout() {
467 mIsLayoutValid = false;
468 super.requestLayout();
469 }
470
471 @Override
472 public void forceLayout() {
473 mIsLayoutValid = false;
474 super.forceLayout();
475 }
476
477 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700479 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700480 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
481 return;
482 }
483
Adam Cohen7d30a372013-07-01 17:03:59 -0700484 // We measure the dimensions of the PagedView to be larger than the pages so that when we
485 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700486 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
487 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
488 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700489 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700490
Adam Cohen7d30a372013-07-01 17:03:59 -0700491 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
492 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
493 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 }
495
Winson Chung8aad6102012-05-11 16:27:49 -0700496 // Return early if we aren't given a proper dimension
497 if (widthSize <= 0 || heightSize <= 0) {
498 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
499 return;
500 }
501
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700503 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700504 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700505
Sunny Goyalac00cba2017-11-13 15:58:01 -0800506 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800507 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800508 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800509 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700510
Sunny Goyalac00cba2017-11-13 15:58:01 -0800511 // measureChildren takes accounts for content padding, we only need to care about extra
512 // space due to insets.
513 measureChildren(myWidthSpec, myHeightSpec);
514 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800515 }
516
Sunny Goyalcf25b522015-07-09 00:01:18 -0700517 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700518 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700519 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800520 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700521 final int childCount = getChildCount();
522 boolean pageScrollChanged = false;
523 if (mPageScrolls == null || childCount != mPageScrolls.length) {
524 mPageScrolls = new int[childCount];
525 pageScrollChanged = true;
526 }
527
528 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 return;
530 }
531
Winson Chung785d2eb2011-04-14 16:08:02 -0700532 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700533
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700534 if (getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC)) {
535 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700537
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700538 final LayoutTransition transition = getLayoutTransition();
539 // If the transition is running defer updating max scroll, as some empty pages could
540 // still be present, and a max scroll change could cause sudden jumps in scroll.
541 if (transition != null && transition.isRunning()) {
542 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
543
544 @Override
545 public void startTransition(LayoutTransition transition, ViewGroup container,
546 View view, int transitionType) { }
547
548 @Override
549 public void endTransition(LayoutTransition transition, ViewGroup container,
550 View view, int transitionType) {
551 // Wait until all transitions are complete.
552 if (!transition.isRunning()) {
553 transition.removeTransitionListener(this);
554 updateMaxScrollX();
555 }
556 }
557 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700558 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700559 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700560 }
561
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700562 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
563 updateCurrentPageScroll();
564 mFirstLayout = false;
565 }
566
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800567 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700568 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700569 }
Winson Chunge3193b92010-09-10 11:44:42 -0700570 }
571
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700572 /**
573 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
574 * of {@code outPageScrolls} should be same as the the childCount
575 *
576 */
577 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
578 ComputePageScrollsLogic scrollLogic) {
579 final int childCount = getChildCount();
580
581 final int startIndex = mIsRtl ? childCount - 1 : 0;
582 final int endIndex = mIsRtl ? -1 : childCount;
583 final int delta = mIsRtl ? -1 : 1;
584
Vadim Tryshev84b46f02018-04-27 17:45:10 -0700585 final int verticalCenter = (getPaddingTop() + getMeasuredHeight() + mInsets.top
586 - mInsets.bottom - getPaddingBottom()) / 2;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700587
Vadim Tryshevcc0c98a2018-04-26 10:47:05 -0700588 final int scrollOffsetLeft = mInsets.left + getPaddingLeft();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700589 final int scrollOffsetRight = getWidth() - getPaddingRight() - mInsets.right;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700590 boolean pageScrollChanged = false;
591
Sunny Goyalc82c6392018-06-06 15:39:13 -0700592 for (int i = startIndex, childLeft = scrollOffsetLeft; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700593 final View child = getPageAt(i);
594 if (scrollLogic.shouldIncludeView(child)) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700595 final int childWidth = child.getMeasuredWidth();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700596 final int childRight = childLeft + childWidth;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700597
598 if (layoutChildren) {
599 final int childHeight = child.getMeasuredHeight();
Sunny Goyalc82c6392018-06-06 15:39:13 -0700600 final int childTop = verticalCenter - childHeight / 2;
601 child.layout(childLeft, childTop, childRight, childTop + childHeight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700602 }
603
Sunny Goyalc82c6392018-06-06 15:39:13 -0700604 // In case the pages are of different width, align the page to left or right edge
605 // based on the orientation.
606 final int pageScroll = mIsRtl
607 ? (childLeft - scrollOffsetLeft)
608 : Math.max(0, childRight - scrollOffsetRight);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700609 if (outPageScrolls[i] != pageScroll) {
610 pageScrollChanged = true;
611 outPageScrolls[i] = pageScroll;
612 }
613
614 childLeft += childWidth + mPageSpacing + getChildGap();
615 }
616 }
617 return pageScrollChanged;
618 }
619
Sunny Goyala1fbd842015-05-20 13:40:27 -0700620 protected int getChildGap() {
621 return 0;
622 }
623
Sunny Goyal4d519f22017-10-24 10:32:40 -0700624 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700625 mMaxScrollX = computeMaxScrollX();
626 }
627
628 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700629 int childCount = getChildCount();
630 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700631 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700632 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700633 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700634 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700635 }
636 }
637
Adam Cohen3b185e22013-10-29 14:45:58 -0700638 public void setPageSpacing(int pageSpacing) {
639 mPageSpacing = pageSpacing;
640 requestLayout();
641 }
642
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800643 private void dispatchPageCountChanged() {
644 if (mPageIndicator != null) {
645 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700646 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700647 // This ensures that when children are added, they get the correct transforms / alphas
648 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700649 invalidate();
650 }
651
Michael Jurka8b805b12012-04-18 14:23:14 -0700652 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800653 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800654 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800655 dispatchPageCountChanged();
656 }
657
658 @Override
659 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800660 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700661 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800662 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700663 }
664
Adam Cohen73894962011-10-31 13:17:17 -0700665 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700666 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800667 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700668 }
669
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 @Override
671 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700672 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700673 if (page != mCurrentPage || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700674 if (immediate) {
675 setCurrentPage(page);
676 } else {
677 snapToPage(page);
678 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 return true;
680 }
681 return false;
682 }
683
684 @Override
685 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700686 int focusablePage;
687 if (mNextPage != INVALID_PAGE) {
688 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700690 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 }
Winson Chung86f77532010-08-24 11:08:22 -0700692 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700693 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700694 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 }
696 return false;
697 }
698
699 @Override
700 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800701 if (super.dispatchUnhandledMove(focused, direction)) {
702 return true;
703 }
704
705 if (mIsRtl) {
706 if (direction == View.FOCUS_LEFT) {
707 direction = View.FOCUS_RIGHT;
708 } else if (direction == View.FOCUS_RIGHT) {
709 direction = View.FOCUS_LEFT;
710 }
711 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700713 if (getCurrentPage() > 0) {
714 snapToPage(getCurrentPage() - 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700715 getChildAt(getCurrentPage() - 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 return true;
717 }
718 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700719 if (getCurrentPage() < getPageCount() - 1) {
720 snapToPage(getCurrentPage() + 1);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700721 getChildAt(getCurrentPage() + 1).requestFocus(direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 return true;
723 }
724 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800725 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 }
727
728 @Override
729 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700730 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
731 return;
732 }
733
Adam Cohen0ffac432013-07-10 11:19:26 -0700734 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700735 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700736 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700737 }
738 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700739 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700740 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 }
742 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700743 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700744 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 }
746 }
747 }
748
749 /**
750 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700751 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700752 *
Winson Chung86f77532010-08-24 11:08:22 -0700753 * This happens when live folders requery, and if they're off page, they
754 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 */
756 @Override
757 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700758 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 View v = focused;
760 while (true) {
761 if (v == current) {
762 super.focusableViewAvailable(focused);
763 return;
764 }
765 if (v == this) {
766 return;
767 }
768 ViewParent parent = v.getParent();
769 if (parent instanceof View) {
770 v = (View)v.getParent();
771 } else {
772 return;
773 }
774 }
775 }
776
777 /**
778 * {@inheritDoc}
779 */
780 @Override
781 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
782 if (disallowIntercept) {
783 // We need to make sure to cancel our long press if
784 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700785 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700786 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 }
788 super.requestDisallowInterceptTouchEvent(disallowIntercept);
789 }
790
Adam Cohen7d30a372013-07-01 17:03:59 -0700791 /** Returns whether x and y originated within the buffered viewport */
Sunny Goyalaad33592018-08-07 17:20:35 -0700792 private boolean isTouchPointInViewportWithBuffer(float x, float y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800793 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyalaad33592018-08-07 17:20:35 -0700794 return sTmpRect.contains((int) x, (int) y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700795 }
796
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 @Override
798 public boolean onInterceptTouchEvent(MotionEvent ev) {
799 /*
800 * This method JUST determines whether we want to intercept the motion.
801 * If we return true, onTouchEvent will be called and we do the actual
802 * scrolling there.
803 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800804 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805
Winson Chung45e1d6e2010-11-09 17:19:49 -0800806 // Skip touch handling if there are no pages to swipe
807 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
808
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 /*
810 * Shortcut the most recurring case: the user is in the dragging
811 * state and he is moving his finger. We want to intercept this
812 * motion.
813 */
814 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700815 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 return true;
817 }
818
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 switch (action & MotionEvent.ACTION_MASK) {
820 case MotionEvent.ACTION_MOVE: {
821 /*
822 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
823 * whether the user has moved far enough from his original down touch.
824 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700825 if (mActivePointerId != INVALID_POINTER) {
826 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700827 }
828 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
829 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
830 // i.e. fall through to the next case (don't break)
831 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
832 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700833 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 }
835
836 case MotionEvent.ACTION_DOWN: {
837 final float x = ev.getX();
838 final float y = ev.getY();
839 // Remember location of down touch
840 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700841 mDownMotionY = y;
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800843 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800844 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700846
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 /*
848 * If being flinged and user touches the screen, initiate drag;
849 * otherwise don't. mScroller.isFinished should be false when
850 * being flinged.
851 */
Sunny Goyalb7b01352018-08-09 12:31:20 -0700852 final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
Adam Cohena7652152013-11-18 20:06:55 +0000853 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800854
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700855 if (finishedScrolling) {
Sunny Goyalaad33592018-08-07 17:20:35 -0700856 mIsBeingDragged = false;
Adam Cohen59b5c792013-12-04 16:09:07 -0800857 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000858 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700859 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800860 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700861 } else {
Sunny Goyalaad33592018-08-07 17:20:35 -0700862 mIsBeingDragged = isTouchPointInViewportWithBuffer(mDownMotionX, mDownMotionY);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700863 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700864
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 break;
866 }
867
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800869 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700870 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 break;
872
873 case MotionEvent.ACTION_POINTER_UP:
874 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800875 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 break;
877 }
878
879 /*
880 * The only time we want to intercept motion events is if we are in the
881 * drag mode.
882 */
Sunny Goyalaad33592018-08-07 17:20:35 -0700883 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800886 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -0700887 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -0800888 }
889
Adam Cohenf8d28232011-02-01 21:47:00 -0800890 protected void determineScrollingStart(MotionEvent ev) {
891 determineScrollingStart(ev, 1.0f);
892 }
893
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 /*
895 * Determines if we should change the touch state to start scrolling after the
896 * user moves their touch point too far.
897 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800898 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700899 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -0700901 if (pointerIndex == -1) return;
902
903 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 final float x = ev.getX(pointerIndex);
905 final float y = ev.getY(pointerIndex);
Sunny Goyalaad33592018-08-07 17:20:35 -0700906 if (!isTouchPointInViewportWithBuffer(x, y)) return;
Adam Cohen7d30a372013-07-01 17:03:59 -0700907
Winson Chung321e9ee2010-08-09 13:37:56 -0700908 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700909
Adam Cohenf8d28232011-02-01 21:47:00 -0800910 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700912
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700913 if (xMoved) {
914 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -0700915 mIsBeingDragged = true;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700916 mTotalMotionX += Math.abs(mLastMotionX - x);
917 mLastMotionX = x;
918 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700919 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700920 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -0800921 // Stop listening for things like pinches.
922 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -0800923 }
924 }
925
926 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700927 // Try canceling the long press. It could also have been scheduled
928 // by a distant descendant, so use the mAllowLongPress flag to block
929 // everything
930 final View currentPage = getPageAt(mCurrentPage);
931 if (currentPage != null) {
932 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 }
934 }
935
Adam Cohenb5ba0972011-09-07 18:02:31 -0700936 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800937 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700938
Adam Cohenedb40762013-07-18 16:45:45 -0700939 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -0700940 int count = getChildCount();
941
942 final int totalDistance;
943
944 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -0700945 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700946 adjacentPage = page - 1;
947 }
948
949 if (adjacentPage < 0 || adjacentPage > count - 1) {
950 totalDistance = v.getMeasuredWidth() + mPageSpacing;
951 } else {
952 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
953 }
Adam Cohenb5ba0972011-09-07 18:02:31 -0700954
955 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700956 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
957 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700958 return scrollProgress;
959 }
960
Adam Cohenedb40762013-07-18 16:45:45 -0700961 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -0700962 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -0700963 return 0;
964 } else {
965 return mPageScrolls[index];
966 }
967 }
968
Adam Cohen564a2e72013-10-09 14:47:32 -0700969 // While layout transitions are occurring, a child's position may stray from its baseline
970 // position. This method returns the magnitude of this stray at any given time.
971 public int getLayoutTransitionOffsetForPage(int index) {
972 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
973 return 0;
974 } else {
975 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -0700976
Sunny Goyal228153d2018-01-04 15:35:22 -0800977 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
978 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -0700979 return (int) (child.getX() - baselineX);
980 }
981 }
982
Sunny Goyalb7b01352018-08-09 12:31:20 -0700983 protected void dampedOverScroll(int amount) {
984 if (amount == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -0700985
Sunny Goyal228153d2018-01-04 15:35:22 -0800986 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -0700987 if (amount < 0) {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700988 super.scrollTo(overScrollAmount, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -0800989 } else {
Sunny Goyalb7b01352018-08-09 12:31:20 -0700990 super.scrollTo(mMaxScrollX + overScrollAmount, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -0800991 }
992 invalidate();
993 }
994
Sunny Goyalb7b01352018-08-09 12:31:20 -0700995 protected void overScroll(int amount) {
996 if (amount == 0) return;
997
998 if (mFreeScroll && !mScroller.isFinished()) {
999 if (amount < 0) {
1000 super.scrollTo(amount, getScrollY());
1001 } else {
1002 super.scrollTo(mMaxScrollX + amount, getScrollY());
1003 }
1004 } else {
1005 dampedOverScroll(amount);
1006 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001007 }
1008
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001009
Sunny Goyalb7b01352018-08-09 12:31:20 -07001010 protected void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001011 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001012 mFreeScroll = freeScroll;
1013
Adam Cohenf9618852013-11-08 06:45:03 -08001014 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001015 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001016 } else if (wasFreeScroll) {
1017 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001018 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001019 }
1020
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001021 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001022 mAllowOverScroll = enable;
1023 }
1024
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 @Override
1026 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001027 super.onTouchEvent(ev);
1028
Winson Chung45e1d6e2010-11-09 17:19:49 -08001029 // Skip touch handling if there are no pages to swipe
1030 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1031
Michael Jurkab8f06722010-10-10 15:58:46 -07001032 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001033
1034 final int action = ev.getAction();
1035
1036 switch (action & MotionEvent.ACTION_MASK) {
1037 case MotionEvent.ACTION_DOWN:
1038 /*
1039 * If being flinged and user touches, stop the fling. isFinished
1040 * will be false if being flinged.
1041 */
1042 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001043 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 }
1045
1046 // Remember where the motion event started
1047 mDownMotionX = mLastMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001048 mDownMotionY = ev.getY();
Winson Chungc0844aa2011-02-02 15:25:58 -08001049 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001050 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001052
Sunny Goyalaad33592018-08-07 17:20:35 -07001053 if (mIsBeingDragged) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001054 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001055 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001056 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 break;
1058
1059 case MotionEvent.ACTION_MOVE:
Sunny Goyalaad33592018-08-07 17:20:35 -07001060 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 // Scroll to follow the motion event
1062 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001063
1064 if (pointerIndex == -1) return true;
1065
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001067 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001068
Adam Cohenaefd4e12011-02-14 16:39:38 -08001069 mTotalMotionX += Math.abs(deltaX);
1070
Winson Chungc0844aa2011-02-02 15:25:58 -08001071 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1072 // keep the remainder because we are actually testing if we've moved from the last
1073 // scrolled position (which is discrete).
1074 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001075 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001076 mLastMotionX = x;
1077 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 } else {
1079 awakenScrollBars();
1080 }
Adam Cohen564976a2010-10-13 18:52:07 -07001081 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 determineScrollingStart(ev);
1083 }
1084 break;
1085
1086 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001087 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 final int activePointerId = mActivePointerId;
1089 final int pointerIndex = ev.findPointerIndex(activePointerId);
1090 final float x = ev.getX(pointerIndex);
1091 final VelocityTracker velocityTracker = mVelocityTracker;
1092 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1093 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001094 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001095 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001096 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1097 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001098
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001099 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
Tony Makc25afd02018-02-27 14:43:47 +00001100 boolean isFling = mTotalMotionX > mTouchSlop && shouldFlingForVelocity(velocityX);
Sunny Goyalb7b01352018-08-09 12:31:20 -07001101 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1102 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohen00481b32011-11-18 12:03:48 -08001103
Adam Cohenf358a4b2013-07-23 16:47:31 -07001104 if (!mFreeScroll) {
1105 // In the case that the page is moved far to one direction and then is flung
1106 // in the opposite direction, we use a threshold to determine whether we should
1107 // just return to the starting page, or if we should skip one further.
1108 boolean returnToOriginalPage = false;
1109 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1110 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1111 returnToOriginalPage = true;
1112 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001113
Adam Cohenf358a4b2013-07-23 16:47:31 -07001114 int finalPage;
1115 // We give flings precedence over large moves, which is why we short-circuit our
1116 // test for a large move if a fling has been registered. That is, a large
1117 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001118
Adam Cohenf358a4b2013-07-23 16:47:31 -07001119 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1120 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1121 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1122 snapToPageWithVelocity(finalPage, velocityX);
1123 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1124 (isFling && isVelocityXLeft)) &&
1125 mCurrentPage < getChildCount() - 1) {
1126 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1127 snapToPageWithVelocity(finalPage, velocityX);
1128 } else {
1129 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001130 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001132 if (!mScroller.isFinished()) {
1133 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001134 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001135
Sunny Goyalb7b01352018-08-09 12:31:20 -07001136 int initialScrollX = getScrollX();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001137
Sunny Goyalb7b01352018-08-09 12:31:20 -07001138 if (((initialScrollX >= mMaxScrollX) && (isVelocityXLeft || !isFling)) ||
1139 ((initialScrollX <= 0) && (!isVelocityXLeft || !isFling))) {
1140 mScroller.springBack(getScrollX(), 0, mMaxScrollX);
1141 } else {
1142 mScroller.setInterpolator(mDefaultInterpolator);
1143 mScroller.fling(initialScrollX, -velocityX,
1144 0, mMaxScrollX,
1145 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR));
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001146
Sunny Goyalb7b01352018-08-09 12:31:20 -07001147 int finalX = mScroller.getFinalPos();
1148 mNextPage = getPageNearestToCenterOfScreen(finalX);
1149
1150 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1151 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1152 if (finalX > 0 && finalX < mMaxScrollX) {
1153 // If scrolling ends in the half of the added space that is closer to
1154 // the end, settle to the end. Otherwise snap to the nearest page.
1155 // If flinging past one of the ends, don't change the velocity as it
1156 // will get stopped at the end anyway.
1157 int pageSnappedX = finalX < firstPageScroll / 2 ? 0
1158 : finalX > (lastPageScroll + mMaxScrollX) / 2
1159 ? mMaxScrollX
1160 : getScrollForPage(mNextPage);
1161
1162 mScroller.setFinalPos(pageSnappedX);
1163 // Ensure the scroll/snap doesn't happen too fast;
1164 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1165 - mScroller.getDuration();
1166 if (extraScrollDuration > 0) {
1167 mScroller.extendDuration(extraScrollDuration);
1168 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001169 }
1170 }
1171 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001173 onScrollInteractionEnd();
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001175
Adam Cohen7d30a372013-07-01 17:03:59 -07001176 // End any intermediate reordering states
1177 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 break;
1179
1180 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001181 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001182 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001183 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001184 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001185 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 break;
1187
1188 case MotionEvent.ACTION_POINTER_UP:
1189 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001190 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 break;
1192 }
1193
1194 return true;
1195 }
1196
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001197 protected boolean shouldFlingForVelocity(int velocityX) {
1198 return Math.abs(velocityX) > mFlingThresholdVelocity;
1199 }
1200
Adam Cohen7d30a372013-07-01 17:03:59 -07001201 private void resetTouchState() {
1202 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001203 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001204 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001205 }
1206
Adam Cohenc2d6e892014-10-16 09:49:24 -07001207 /**
1208 * Triggered by scrolling via touch
1209 */
1210 protected void onScrollInteractionBegin() {
1211 }
1212
1213 protected void onScrollInteractionEnd() {
1214 }
1215
Winson Chung185d7162011-02-28 13:47:29 -08001216 @Override
1217 public boolean onGenericMotionEvent(MotionEvent event) {
1218 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1219 switch (event.getAction()) {
1220 case MotionEvent.ACTION_SCROLL: {
1221 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1222 final float vscroll;
1223 final float hscroll;
1224 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1225 vscroll = 0;
1226 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1227 } else {
1228 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1229 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1230 }
1231 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001232 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001233 : (hscroll > 0 || vscroll > 0);
1234 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001235 scrollRight();
1236 } else {
1237 scrollLeft();
1238 }
1239 return true;
1240 }
1241 }
1242 }
1243 }
1244 return super.onGenericMotionEvent(event);
1245 }
1246
Michael Jurkab8f06722010-10-10 15:58:46 -07001247 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1248 if (mVelocityTracker == null) {
1249 mVelocityTracker = VelocityTracker.obtain();
1250 }
1251 mVelocityTracker.addMovement(ev);
1252 }
1253
1254 private void releaseVelocityTracker() {
1255 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001257 mVelocityTracker.recycle();
1258 mVelocityTracker = null;
1259 }
1260 }
1261
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 private void onSecondaryPointerUp(MotionEvent ev) {
1263 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1264 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1265 final int pointerId = ev.getPointerId(pointerIndex);
1266 if (pointerId == mActivePointerId) {
1267 // This was our active pointer going up. Choose a new
1268 // active pointer and adjust accordingly.
1269 // TODO: Make this decision more intelligent.
1270 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1271 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001272 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 mActivePointerId = ev.getPointerId(newPointerIndex);
1274 if (mVelocityTracker != null) {
1275 mVelocityTracker.clear();
1276 }
1277 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001278 }
1279
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 @Override
1281 public void requestChildFocus(View child, View focused) {
1282 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001283 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001284 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001285 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 }
1287 }
1288
Sunny Goyal228153d2018-01-04 15:35:22 -08001289 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001290 return getPageNearestToCenterOfScreen(getScrollX());
1291 }
1292
1293 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001294 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001295 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 final int childCount = getChildCount();
1298 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001299 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001300 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001302 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1304 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1305 minDistanceFromScreenCenter = distanceFromScreenCenter;
1306 minDistanceFromScreenCenterIndex = i;
1307 }
1308 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001309 return minDistanceFromScreenCenterIndex;
1310 }
1311
1312 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001313 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1314 }
1315
1316 protected boolean isInOverScroll() {
Sunny Goyalb7b01352018-08-09 12:31:20 -07001317 return (getScrollX() > mMaxScrollX || getScrollX() < 0);
Adam Cohen8d769d62017-06-23 17:27:38 -07001318 }
1319
1320 protected int getPageSnapDuration() {
1321 if (isInOverScroll()) {
1322 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1323 }
1324 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
1326
Adam Cohene0f66b52010-11-23 15:06:07 -08001327 // We want the duration of the page snap animation to be influenced by the distance that
1328 // the screen has to travel, however, we don't want this duration to be effected in a
1329 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1330 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001331 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001332 f -= 0.5f; // center the values about 0.
1333 f *= 0.3f * Math.PI / 2.0f;
1334 return (float) Math.sin(f);
1335 }
1336
Winson Chung05a31ed2018-02-08 17:08:43 -08001337 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001338 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001339 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001340
Adam Cohenedb40762013-07-18 16:45:45 -07001341 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001342 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001343 int duration = 0;
1344
Sunny Goyal4d113a52015-05-27 10:05:28 -07001345 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001346 // If the velocity is low enough, then treat this more as an automatic page advance
1347 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001348 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001349 }
1350
1351 // Here we compute a "distance" that will be used in the computation of the overall
1352 // snap duration. This is a function of the actual distance that needs to be traveled;
1353 // we keep this value close to half screen size in order to reduce the variance in snap
1354 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001355 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001356 float distance = halfScreenSize + halfScreenSize *
1357 distanceInfluenceForSnapDuration(distanceRatio);
1358
1359 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001360 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001361
1362 // we want the page's snap velocity to approximately match the velocity at which the
1363 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001364 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1365 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001366
Winson Chung05a31ed2018-02-08 17:08:43 -08001367 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001368 }
1369
Winson Chung05a31ed2018-02-08 17:08:43 -08001370 public boolean snapToPage(int whichPage) {
1371 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 }
1373
Winson Chung05a31ed2018-02-08 17:08:43 -08001374 public boolean snapToPageImmediately(int whichPage) {
1375 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001376 }
1377
Winson Chung05a31ed2018-02-08 17:08:43 -08001378 public boolean snapToPage(int whichPage, int duration) {
1379 return snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001380 }
1381
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001382 public boolean snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
Winson Chung05a31ed2018-02-08 17:08:43 -08001383 return snapToPage(whichPage, duration, false, interpolator);
Adam Cohenf9618852013-11-08 06:45:03 -08001384 }
1385
Winson Chung05a31ed2018-02-08 17:08:43 -08001386 protected boolean snapToPage(int whichPage, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001387 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001388 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001389
Adam Cohenedb40762013-07-18 16:45:45 -07001390 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001391 final int delta = newX - getUnboundedScrollX();
Winson Chung05a31ed2018-02-08 17:08:43 -08001392 return snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001393 }
1394
Winson Chung05a31ed2018-02-08 17:08:43 -08001395 protected boolean snapToPage(int whichPage, int delta, int duration) {
1396 return snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 }
Michael Jurka0142d492010-08-25 17:46:15 -07001398
Winson Chung05a31ed2018-02-08 17:08:43 -08001399 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate,
Adam Cohenf9618852013-11-08 06:45:03 -08001400 TimeInterpolator interpolator) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001401 if (mFirstLayout) {
1402 setCurrentPage(whichPage);
1403 return false;
1404 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001405
1406 if (FeatureFlags.IS_DOGFOOD_BUILD) {
1407 duration *= Settings.System.getFloat(getContext().getContentResolver(),
1408 Settings.System.WINDOW_ANIMATION_SCALE, 1);
1409 }
1410
Adam Cohen6f127a62014-06-12 14:54:41 -07001411 whichPage = validateNewPage(whichPage);
1412
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001416 if (immediate) {
1417 duration = 0;
1418 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 duration = Math.abs(delta);
1420 }
1421
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001422 if (duration != 0) {
1423 pageBeginTransition();
1424 }
1425
Adam Cohenf358a4b2013-07-23 16:47:31 -07001426 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001427 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001428 }
Adam Cohenf9618852013-11-08 06:45:03 -08001429
1430 if (interpolator != null) {
1431 mScroller.setInterpolator(interpolator);
1432 } else {
1433 mScroller.setInterpolator(mDefaultInterpolator);
1434 }
1435
Sunny Goyalb7b01352018-08-09 12:31:20 -07001436 mScroller.startScroll(getUnboundedScrollX(), delta, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001437
Adam Cohen674531f2013-12-13 15:07:14 -08001438 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001439
1440 // Trigger a compute() to finish switching pages if necessary
1441 if (immediate) {
1442 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001443 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 }
1445
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001447 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 }
1449
Vadim Tryshev98913d02018-05-18 18:41:34 -07001450 public boolean scrollLeft() {
1451 if (getNextPage() > 0) {
1452 snapToPage(getNextPage() - 1);
1453 return true;
1454 }
1455 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 }
1457
Vadim Tryshev98913d02018-05-18 18:41:34 -07001458 public boolean scrollRight() {
1459 if (getNextPage() < getChildCount() - 1) {
1460 snapToPage(getNextPage() + 1);
1461 return true;
1462 }
1463 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 }
1465
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001466 @Override
1467 public CharSequence getAccessibilityClassName() {
1468 // Some accessibility services have special logic for ScrollView. Since we provide same
1469 // accessibility info as ScrollView, inform the service to handle use the same way.
1470 return ScrollView.class.getName();
1471 }
1472
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001473 protected boolean isPageOrderFlipped() {
1474 return false;
1475 }
1476
Winson Chung6a0f57d2011-06-29 20:10:49 -07001477 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001478 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001479 @Override
1480 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1481 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001482 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001483 info.setScrollable(getPageCount() > 1);
1484 if (getCurrentPage() < getPageCount() - 1) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001485 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
1486 : AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001487 }
1488 if (getCurrentPage() > 0) {
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001489 info.addAction(pagesFlipped ? AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
1490 : AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001491 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001492
1493 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1494 // Besides disabling the accessibility long-click, this also prevents this view from getting
1495 // accessibility focus.
1496 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001497 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001498 }
1499
1500 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001501 public void sendAccessibilityEvent(int eventType) {
1502 // Don't let the view send real scroll events.
1503 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1504 super.sendAccessibilityEvent(eventType);
1505 }
1506 }
1507
1508 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001509 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1510 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001511 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001512 }
1513
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001514 @Override
1515 public boolean performAccessibilityAction(int action, Bundle arguments) {
1516 if (super.performAccessibilityAction(action, arguments)) {
1517 return true;
1518 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001519 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001520 switch (action) {
1521 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001522 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001523 return true;
1524 }
1525 } break;
1526 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001527 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001528 return true;
1529 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001530 }
1531 break;
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001532 }
1533 return false;
1534 }
1535
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001536 protected boolean canAnnouncePageDescription() {
1537 return true;
1538 }
1539
Adam Cohen0ffac432013-07-10 11:19:26 -07001540 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001541 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001542 getNextPage() + 1, getChildCount());
1543 }
1544
Tony Mak2e564402018-02-27 17:19:34 +00001545 protected float getDownMotionX() {
1546 return mDownMotionX;
1547 }
1548
1549 protected float getDownMotionY() {
1550 return mDownMotionY;
1551 }
1552
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001553 protected interface ComputePageScrollsLogic {
1554
1555 boolean shouldIncludeView(View view);
1556 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001557
1558 public int[] getVisibleChildrenRange() {
1559 float visibleLeft = 0;
1560 float visibleRight = visibleLeft + getMeasuredWidth();
1561 float scaleX = getScaleX();
1562 if (scaleX < 1 && scaleX > 0) {
1563 float mid = getMeasuredWidth() / 2;
1564 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1565 visibleRight = mid + ((visibleRight - mid) / scaleX);
1566 }
1567
1568 int leftChild = -1;
1569 int rightChild = -1;
1570 final int childCount = getChildCount();
1571 for (int i = 0; i < childCount; i++) {
1572 final View child = getPageAt(i);
1573
1574 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1575 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1576 if (leftChild == -1) {
1577 leftChild = i;
1578 }
1579 rightChild = i;
1580 }
1581 }
1582 mTmpIntPair[0] = leftChild;
1583 mTmpIntPair[1] = rightChild;
1584 return mTmpIntPair;
1585 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001586}