blob: 242e3e3743f5c1739780cf9432758da308239fc0 [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
Andras Kloczl55edfe52021-05-14 12:21:30 +020019import static androidx.annotation.VisibleForTesting.PACKAGE_PRIVATE;
Andras Kloczldbf577a2021-05-26 15:02:52 +020020
Sunny Goyalc39db732021-04-03 01:20:21 -070021import static com.android.launcher3.anim.Interpolators.SCROLL;
Sunny Goyal8b37c572020-03-31 12:12:14 -070022import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
23import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
Sunny Goyal8b37c572020-03-31 12:12:14 -070024import static com.android.launcher3.touch.OverScroll.OVERSCROLL_DAMP_FACTOR;
Sunny Goyal8b37c572020-03-31 12:12:14 -070025import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_BY;
26import static com.android.launcher3.touch.PagedOrientationHandler.VIEW_SCROLL_TO;
27
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070028import android.animation.LayoutTransition;
Sunny Goyalcf25b522015-07-09 00:01:18 -070029import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070031import android.content.res.TypedArray;
Jon Miranda71cb80c2019-01-24 21:25:13 -080032import android.graphics.Canvas;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070034import android.os.Bundle;
Tony Wickhamd58c2d52018-05-04 12:10:55 -070035import android.provider.Settings;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080044import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
48import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalc39db732021-04-03 01:20:21 -070049import android.widget.OverScroller;
Vadim Trysheve6bbefb2018-04-03 13:38:06 -070050import android.widget.ScrollView;
Tony Wickhamf549dab2016-05-16 09:54:06 -070051
Tony Wickham62117d72020-03-26 16:56:26 -070052import androidx.annotation.Nullable;
Andras Kloczl55edfe52021-05-14 12:21:30 +020053import androidx.annotation.VisibleForTesting;
Tony Wickham62117d72020-03-26 16:56:26 -070054
Jon Miranda9e198662020-03-11 14:42:02 -070055import com.android.launcher3.compat.AccessibilityManagerCompat;
56import com.android.launcher3.config.FeatureFlags;
Jon Miranda9e198662020-03-11 14:42:02 -070057import com.android.launcher3.pageindicators.PageIndicator;
Jon Miranda9e198662020-03-11 14:42:02 -070058import com.android.launcher3.touch.PagedOrientationHandler;
59import com.android.launcher3.touch.PagedOrientationHandler.ChildBounds;
Sunny Goyalc39db732021-04-03 01:20:21 -070060import com.android.launcher3.util.EdgeEffectCompat;
Andras Kloczl55edfe52021-05-14 12:21:30 +020061import com.android.launcher3.util.IntSet;
Jon Miranda9e198662020-03-11 14:42:02 -070062import com.android.launcher3.util.Thunk;
Sunny Goyal8b37c572020-03-31 12:12:14 -070063import com.android.launcher3.views.ActivityContext;
Jon Miranda9e198662020-03-11 14:42:02 -070064
65import java.util.ArrayList;
Sunny Goyal59969372021-05-06 12:11:44 -070066import java.util.function.Consumer;
Jon Miranda9e198662020-03-11 14:42:02 -070067
Winson Chung321e9ee2010-08-09 13:37:56 -070068/**
69 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070070 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070071 */
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -080072public abstract class PagedView<T extends View & PageIndicator> extends ViewGroup {
Winson Chung321e9ee2010-08-09 13:37:56 -070073 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070074 private static final boolean DEBUG = false;
Winson Chung4941f652020-06-18 15:11:53 -070075 public static final boolean DEBUG_FAILED_QUICKSWITCH = false;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070076
Winson Chung4941f652020-06-18 15:11:53 -070077 public static final int ACTION_MOVE_ALLOW_EASY_FLING = MotionEvent.ACTION_MASK - 1;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053078 public static final int INVALID_PAGE = -1;
Sunny Goyal20a13ff2018-03-13 16:44:00 -070079 protected static final ComputePageScrollsLogic SIMPLE_SCROLL_LOGIC = (v) -> v.getVisibility() != GONE;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Vadim Tryshevfedca432015-08-19 17:55:02 -070081 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Sunny Goyal8e2133b2015-05-06 13:39:07 -070087 private static final float MAX_SCROLL_PROGRESS = 1.0f;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
Winson Chung4941f652020-06-18 15:11:53 -070092 private static final int EASY_FLING_THRESHOLD_VELOCITY = 400;
Adam Cohen265b9a62011-12-07 14:37:18 -080093 private static final int MIN_SNAP_VELOCITY = 1500;
94 private static final int MIN_FLING_VELOCITY = 250;
95
Adam Cohenf358a4b2013-07-23 16:47:31 -070096 private boolean mFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070097
Winson Chung4941f652020-06-18 15:11:53 -070098 protected final int mFlingThresholdVelocity;
99 protected final int mEasyFlingThresholdVelocity;
100 protected final int mMinFlingVelocity;
101 protected final int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected boolean mFirstLayout = true;
104
Sunny Goyal4ffec482016-02-09 11:28:52 -0800105 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mCurrentPage;
Alex Chauba61b982021-06-15 16:29:19 +0100107 // Difference between current scroll position and mCurrentPage's page scroll. Used to maintain
108 // relative scroll position unchanged in updateCurrentPageScroll. Cleared when snapping to a
109 // page.
110 protected int mCurrentPageScrollDiff;
Adam Cohene61a9a22013-06-11 15:45:31 -0700111
Sunny Goyal4ffec482016-02-09 11:28:52 -0800112 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected int mNextPage = INVALID_PAGE;
Vinit Nayaka406f722019-12-19 11:50:55 -0800114 protected int mMaxScroll;
115 protected int mMinScroll;
Sunny Goyalb7b01352018-08-09 12:31:20 -0700116 protected OverScroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800118 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
120 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700121 private float mDownMotionY;
Vinit Nayaka406f722019-12-19 11:50:55 -0800122 private float mDownMotionPrimary;
123 private float mLastMotion;
124 private float mLastMotionRemainder;
125 private float mTotalMotion;
Winson Chung4941f652020-06-18 15:11:53 -0700126 // Used in special cases where the fling checks can be relaxed for an intentional gesture
127 private boolean mAllowEasyFling;
Sunny Goyalacd17df2020-04-03 15:58:45 -0700128 protected PagedOrientationHandler mOrientationHandler = PagedOrientationHandler.PORTRAIT;
Adam Cohenedb40762013-07-18 16:45:45 -0700129
Vadim Trysheve628c472018-05-16 14:28:18 -0700130 protected int[] mPageScrolls;
Sunny Goyalaad33592018-08-07 17:20:35 -0700131 private boolean mIsBeingDragged;
Adam Cohencae7f572013-11-04 14:42:52 -0800132
Winson Chung4941f652020-06-18 15:11:53 -0700133 // The amount of movement to begin scrolling
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected int mTouchSlop;
Winson Chung4941f652020-06-18 15:11:53 -0700135 // The amount of movement to begin paging
136 protected int mPageSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700144 protected boolean mIsPageInTransition = false;
Tony Wickham62117d72020-03-26 16:56:26 -0700145 private Runnable mOnPageTransitionEndCallback;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700146
Winson Chungd2be3812013-07-16 11:11:32 -0700147 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700148 @Thunk int mPageIndicatorViewId;
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800149 protected T mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700150
John Spurlock77e1f472013-09-11 10:09:51 -0400151 protected final Rect mInsets = new Rect();
Tony Wickhamdfb5cc92018-02-26 16:41:57 -0800152 protected boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400153
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800154 // Similar to the platform implementation of isLayoutValid();
155 protected boolean mIsLayoutValid;
156
Vadim Tryshev528b9e02018-05-24 18:22:03 -0700157 private int[] mTmpIntPair = new int[2];
158
Sunny Goyalc39db732021-04-03 01:20:21 -0700159 protected EdgeEffectCompat mEdgeGlowLeft;
160 protected EdgeEffectCompat mEdgeGlowRight;
161
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 public PagedView(Context context) {
163 this(context, null);
164 }
165
166 public PagedView(Context context, AttributeSet attrs) {
167 this(context, attrs, 0);
168 }
169
170 public PagedView(Context context, AttributeSet attrs, int defStyle) {
171 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700172
Adam Cohen9c4949e2010-10-05 12:27:22 -0700173 TypedArray a = context.obtainStyledAttributes(attrs,
174 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700175 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700176 a.recycle();
177
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700179 mIsRtl = Utilities.isRtl(getResources());
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Sunny Goyalc39db732021-04-03 01:20:21 -0700181 mScroller = new OverScroller(context, SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700182 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Vinit Nayaka406f722019-12-19 11:50:55 -0800184 final ViewConfiguration configuration = ViewConfiguration.get(context);
Winson Chung4941f652020-06-18 15:11:53 -0700185 mTouchSlop = configuration.getScaledTouchSlop();
186 mPageSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800188
Sunny Goyalcf25b522015-07-09 00:01:18 -0700189 float density = getResources().getDisplayMetrics().density;
190 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
Winson Chung4941f652020-06-18 15:11:53 -0700191 mEasyFlingThresholdVelocity = (int) (EASY_FLING_THRESHOLD_VELOCITY * density);
Sunny Goyalcf25b522015-07-09 00:01:18 -0700192 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
193 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Sunny Goyala9f55462018-03-25 18:15:15 -0700194
Sunny Goyalc39db732021-04-03 01:20:21 -0700195 initEdgeEffect();
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700196 setDefaultFocusHighlightEnabled(false);
Sunny Goyalc39db732021-04-03 01:20:21 -0700197 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 }
199
Sunny Goyalc39db732021-04-03 01:20:21 -0700200 protected void initEdgeEffect() {
201 mEdgeGlowLeft = new EdgeEffectCompat(getContext());
202 mEdgeGlowRight = new EdgeEffectCompat(getContext());
Adam Cohenf9618852013-11-08 06:45:03 -0800203 }
204
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700205 public void initParentViews(View parent) {
206 if (mPageIndicatorViewId > -1) {
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800207 mPageIndicator = parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700208 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700209 }
210 }
211
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800212 public T getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700213 return mPageIndicator;
214 }
215
Adam Cohen674531f2013-12-13 15:07:14 -0800216 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700217 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
218 * that the user was on before entering free scroll mode (e.g. the home screen page they
Alex Chaub794ea62021-02-03 18:00:30 +0000219 * long-pressed on to enter the overview). Try using {@link #getDestinationPage()}
Tony Wickham29d853c2015-09-08 10:35:56 -0700220 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700222 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700223 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700225
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700226 /**
227 * Returns the index of page to be shown immediately afterwards.
228 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700229 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700230 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
231 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700232
Adam Cohenf9c184a2016-01-15 16:47:43 -0800233 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildCount();
235 }
236
Sunny Goyal83a8f042015-05-19 12:52:12 -0700237 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 return getChildAt(index);
239 }
240
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800242 * Updates the scroll of the current page immediately to its final scroll position. We use this
243 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
244 * the previous tab page.
245 */
246 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700247 // If the current page is invalid, just reset the scroll position to zero
Vinit Nayaka406f722019-12-19 11:50:55 -0800248 int newPosition = 0;
Adam Cohen0ffac432013-07-10 11:19:26 -0700249 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Alex Chauba61b982021-06-15 16:29:19 +0100250 newPosition = getScrollForPage(mCurrentPage) + mCurrentPageScrollDiff;
Adam Cohen0ffac432013-07-10 11:19:26 -0700251 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800252 mOrientationHandler.set(this, VIEW_SCROLL_TO, newPosition);
Sunny Goyalc39db732021-04-03 01:20:21 -0700253 mScroller.startScroll(mScroller.getCurrX(), 0, newPosition - mScroller.getCurrX(), 0);
Sunny Goyalc82c6392018-06-06 15:39:13 -0700254 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800255 }
256
Tony Wickhamf8d731a2021-05-26 14:58:13 -0700257 /**
258 * Immediately finishes any overscroll effect and jumps to the end of the scroller animation.
259 */
260 public void abortScrollerAnimation() {
261 mEdgeGlowLeft.finish();
262 mEdgeGlowRight.finish();
263 abortScrollerAnimation(true);
264 }
265
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700266 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700267 mScroller.abortAnimation();
268 // We need to clean up the next page here to avoid computeScrollHelper from
269 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700270 if (resetNextPage) {
271 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800272 pageEndTransition();
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700273 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700274 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700275
Tony Wickham8f7ead32016-04-07 18:46:44 -0700276 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700277 mScroller.forceFinished(true);
278 // We need to clean up the next page here to avoid computeScrollHelper from
279 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700280 if (resetNextPage) {
281 mNextPage = INVALID_PAGE;
Sunny Goyal857b1b92018-03-05 10:49:05 -0800282 pageEndTransition();
Tony Wickham8f7ead32016-04-07 18:46:44 -0700283 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700284 }
285
Adam Cohen6f127a62014-06-12 14:54:41 -0700286 private int validateNewPage(int newPage) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700287 newPage = ensureWithinScrollBounds(newPage);
Adam Cohen6f127a62014-06-12 14:54:41 -0700288 // Ensure that it is clamped by the actual set of children in all cases
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100289 newPage = Utilities.boundToRange(newPage, 0, getPageCount() - 1);
290
291 if (getPanelCount() > 1) {
292 // Always return left panel as new page
293 newPage = getLeftmostVisiblePageForIndex(newPage);
294 }
295 return newPage;
296 }
297
Andras Kloczl55edfe52021-05-14 12:21:30 +0200298 /**
299 * In most cases where panelCount is 1, this method will just return the page index that was
300 * passed in.
301 * But for example when two panel home is enabled we might need the leftmost visible page index
302 * because that page is the current page.
303 */
304 public int getLeftmostVisiblePageForIndex(int pageIndex) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100305 int panelCount = getPanelCount();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200306 return pageIndex - pageIndex % panelCount;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100307 }
308
309 /**
310 * Returns the number of pages that are shown at the same time.
311 */
312 protected int getPanelCount() {
313 return 1;
314 }
315
316 /**
Andras Kloczl55edfe52021-05-14 12:21:30 +0200317 * Returns an IntSet with the indices of the currently visible pages
318 */
319 @VisibleForTesting(otherwise = PACKAGE_PRIVATE)
320 public IntSet getVisiblePageIndices() {
Andras Kloczl7e912442021-05-06 12:50:28 +0200321 return getPageIndices(mCurrentPage);
322 }
323
324 /**
325 * In case the panelCount is 1 this just returns the same page index in an IntSet.
326 * But in cases where the panelCount > 1 this will return all the page indices that belong
327 * together, i.e. on the Workspace they are next to each other and shown at the same time.
328 */
329 private IntSet getPageIndices(int pageIndex) {
330 // we want to make sure the pageIndex is the leftmost page
331 pageIndex = getLeftmostVisiblePageForIndex(pageIndex);
332
333 IntSet pageIndices = new IntSet();
Andras Kloczl55edfe52021-05-14 12:21:30 +0200334 int panelCount = getPanelCount();
335 int pageCount = getPageCount();
Andras Kloczl7e912442021-05-06 12:50:28 +0200336 for (int page = pageIndex; page < pageIndex + panelCount && page < pageCount; page++) {
337 pageIndices.add(page);
Andras Kloczl55edfe52021-05-14 12:21:30 +0200338 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200339 return pageIndices;
340 }
341
342 /**
343 * Returns an IntSet with the indices of the neighbour pages that are in the focus direction.
344 */
345 private IntSet getNeighbourPageIndices(int focus) {
346 int panelCount = getPanelCount();
347 // getNextPage is more reliable than getCurrentPage
348 int currentPage = getNextPage();
349
350 int nextPage;
351 if (focus == View.FOCUS_LEFT) {
352 nextPage = currentPage - panelCount;
353 } else if (focus == View.FOCUS_RIGHT) {
354 nextPage = currentPage + panelCount;
355 } else {
356 // no neighbours to those direction
357 return new IntSet();
358 }
359 nextPage = validateNewPage(nextPage);
360 if (nextPage == currentPage) {
361 // We reached the end of the pages
362 return new IntSet();
363 }
364
365 int pageCount = getPageCount();
366 IntSet neighbourIndices = new IntSet();
367 for (int page = nextPage; page < nextPage + panelCount && page < pageCount; page++) {
368 neighbourIndices.add(page);
369 }
370 return neighbourIndices;
Andras Kloczl55edfe52021-05-14 12:21:30 +0200371 }
372
373 /**
Sunny Goyal59969372021-05-06 12:11:44 -0700374 * Executes the callback against each visible page
Andras Kloczl2bb51342021-03-20 01:13:45 +0100375 */
Sunny Goyal59969372021-05-06 12:11:44 -0700376 public void forEachVisiblePage(Consumer<View> callback) {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200377 getVisiblePageIndices().forEach(pageIndex -> {
378 View page = getPageAt(pageIndex);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100379 if (page != null) {
Sunny Goyal59969372021-05-06 12:11:44 -0700380 callback.accept(page);
Andras Kloczl2bb51342021-03-20 01:13:45 +0100381 }
Andras Kloczl55edfe52021-05-14 12:21:30 +0200382 });
Andras Kloczl2bb51342021-03-20 01:13:45 +0100383 }
384
385 /**
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100386 * Returns true if the view is on one of the current pages, false otherwise.
387 */
388 public boolean isVisible(View child) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200389 return isVisible(indexOfChild(child));
390 }
391
392 /**
393 * Returns true if the page with the given index is currently visible, false otherwise.
394 */
395 private boolean isVisible(int pageIndex) {
396 return getLeftmostVisiblePageForIndex(pageIndex) == mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700397 }
398
Chet Haasebc2f0822012-10-26 17:59:53 -0700399 /**
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700400 * @return The closest page to the provided page that is within mMinScrollX and mMaxScrollX.
401 */
402 private int ensureWithinScrollBounds(int page) {
403 int dir = !mIsRtl ? 1 : - 1;
404 int currScroll = getScrollForPage(page);
405 int prevScroll;
Vinit Nayaka406f722019-12-19 11:50:55 -0800406 while (currScroll < mMinScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700407 page += dir;
408 prevScroll = currScroll;
409 currScroll = getScrollForPage(page);
410 if (currScroll <= prevScroll) {
411 Log.e(TAG, "validateNewPage: failed to find a page > mMinScrollX");
412 break;
413 }
414 }
Vinit Nayaka406f722019-12-19 11:50:55 -0800415 while (currScroll > mMaxScroll) {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700416 page -= dir;
417 prevScroll = currScroll;
418 currScroll = getScrollForPage(page);
419 if (currScroll >= prevScroll) {
420 Log.e(TAG, "validateNewPage: failed to find a page < mMaxScrollX");
421 break;
422 }
423 }
424 return page;
425 }
426
Tony Wickham03f27012019-04-25 14:22:54 -0700427 public void setCurrentPage(int currentPage) {
428 setCurrentPage(currentPage, INVALID_PAGE);
429 }
430
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700431 /**
Winson Chung86f77532010-08-24 11:08:22 -0700432 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 */
Tony Wickham03f27012019-04-25 14:22:54 -0700434 public void setCurrentPage(int currentPage, int overridePrevPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800435 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700436 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800437 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800438 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
439 // the default
440 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800441 return;
442 }
Tony Wickham03f27012019-04-25 14:22:54 -0700443 int prevPage = overridePrevPage != INVALID_PAGE ? overridePrevPage : mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700444 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700445 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700446 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800447 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 }
449
Winson Chung8c87cd82013-07-23 16:20:10 -0700450 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800451 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
452 * has settled.
453 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700454 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800455 updatePageIndicator();
456 }
457
458 private void updatePageIndicator() {
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700459 if (mPageIndicator != null) {
Tony Mak4f521af2018-03-15 16:37:34 +0000460 mPageIndicator.setActiveMarker(getNextPage());
Winson Chungd2be3812013-07-16 11:11:32 -0700461 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700463 protected void pageBeginTransition() {
464 if (!mIsPageInTransition) {
465 mIsPageInTransition = true;
466 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700467 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700468 }
469
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700470 protected void pageEndTransition() {
Sunny Goyal05b59c02021-04-29 13:55:40 -0700471 if (mIsPageInTransition && !mIsBeingDragged && mScroller.isFinished()
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700472 && (!isShown() || (mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()))) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700473 mIsPageInTransition = false;
474 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700475 }
Michael Jurka0142d492010-08-25 17:46:15 -0700476 }
477
Sunny Goyalb745a2a2021-04-30 13:38:22 -0700478 @Override
479 public void onVisibilityAggregated(boolean isVisible) {
480 pageEndTransition();
481 super.onVisibilityAggregated(isVisible);
482 }
483
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700484 protected boolean isPageInTransition() {
485 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700486 }
487
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700488 /**
489 * Called when the page starts moving as part of the scroll. Subclasses can override this
490 * to provide custom behavior during animation.
491 */
492 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700493 }
494
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700495 /**
496 * Called when the page ends moving as part of the scroll. Subclasses can override this
497 * to provide custom behavior during animation.
498 */
499 protected void onPageEndTransition() {
Alex Chauba61b982021-06-15 16:29:19 +0100500 mCurrentPageScrollDiff = 0;
vadimt4d93df52019-07-10 17:32:58 -0700501 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
Tony Wickham619daaf2020-02-07 12:29:06 -0800502 AccessibilityManagerCompat.sendCustomAccessibilityEvent(getPageAt(mCurrentPage),
503 AccessibilityEvent.TYPE_VIEW_FOCUSED, null);
Tony Wickham62117d72020-03-26 16:56:26 -0700504 if (mOnPageTransitionEndCallback != null) {
505 mOnPageTransitionEndCallback.run();
506 mOnPageTransitionEndCallback = null;
507 }
508 }
509
510 /**
511 * Sets a callback to run once when the scrolling finishes. If there is currently
512 * no page in transition, then the callback is called immediately.
513 */
514 public void setOnPageTransitionEndCallback(@Nullable Runnable callback) {
515 if (mIsPageInTransition || callback == null) {
516 mOnPageTransitionEndCallback = callback;
517 } else {
518 callback.run();
519 }
Michael Jurka0142d492010-08-25 17:46:15 -0700520 }
521
Adam Cohen68d73932010-11-15 10:50:58 -0800522 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700523 public void scrollTo(int x, int y) {
Sunny Goyalc39db732021-04-03 01:20:21 -0700524 x = Utilities.boundToRange(x,
525 mOrientationHandler.getPrimaryValue(mMinScroll, 0), mMaxScroll);
526 y = Utilities.boundToRange(y,
527 mOrientationHandler.getPrimaryValue(0, mMinScroll), mMaxScroll);
Vinit Nayaka406f722019-12-19 11:50:55 -0800528 super.scrollTo(x, y);
Michael Jurka0142d492010-08-25 17:46:15 -0700529 }
530
Adam Cohen53805212013-10-01 10:39:23 -0700531 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800532 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700533 if (mCurrentPage != getNextPage()) {
534 AccessibilityEvent ev =
535 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700536 ev.setScrollable(true);
537 ev.setScrollX(getScrollX());
538 ev.setScrollY(getScrollY());
Vinit Nayaka406f722019-12-19 11:50:55 -0800539 mOrientationHandler.setMaxScroll(ev, mMaxScroll);
Vadim Tryshevf4715972015-05-08 17:21:03 -0700540 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700541 }
Adam Cohen53805212013-10-01 10:39:23 -0700542 }
543 }
544
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700545 protected void announcePageForAccessibility() {
546 if (isAccessibilityEnabled(getContext())) {
547 // Notify the user when the page changes
548 announceForAccessibility(getCurrentPageDescription());
549 }
550 }
551
Winson Chung85425412021-05-10 16:24:14 -0700552 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700554 // Don't bother scrolling if the page does not need to be moved
Sunny Goyalc39db732021-04-03 01:20:21 -0700555 int oldPos = mOrientationHandler.getPrimaryScroll(this);
556 int newPos = mScroller.getCurrX();
557 if (oldPos != newPos) {
558 mOrientationHandler.set(this, VIEW_SCROLL_TO, mScroller.getCurrX());
Winson Chung557d6ed2011-07-08 15:34:52 -0700559 }
Winson Chung85425412021-05-10 16:24:14 -0700560
561 if (mAllowOverScroll) {
562 if (newPos < mMinScroll && oldPos >= mMinScroll) {
563 mEdgeGlowLeft.onAbsorb((int) mScroller.getCurrVelocity());
564 mScroller.abortAnimation();
565 } else if (newPos > mMaxScroll && oldPos <= mMaxScroll) {
566 mEdgeGlowRight.onAbsorb((int) mScroller.getCurrVelocity());
567 mScroller.abortAnimation();
Sunny Goyalc39db732021-04-03 01:20:21 -0700568 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800569 }
Adam Cohen53805212013-10-01 10:39:23 -0700570
Winson Chung85425412021-05-10 16:24:14 -0700571 // If the scroller has scrolled to the final position and there is no edge effect, then
572 // finish the scroller to skip waiting for additional settling
573 int finalPos = mOrientationHandler.getPrimaryValue(mScroller.getFinalX(),
574 mScroller.getFinalY());
575 if (newPos == finalPos && mEdgeGlowLeft.isFinished() && mEdgeGlowRight.isFinished()) {
576 mScroller.abortAnimation();
577 }
578
579 invalidate();
580 return true;
581 } else if (mNextPage != INVALID_PAGE) {
582 sendScrollAccessibilityEvent();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700583 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700584 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700585 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700586 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700587
Adam Cohen73aa9752010-11-24 16:26:58 -0800588 // We don't want to trigger a page end moving unless the page has settled
589 // and the user has stopped scrolling
Sunny Goyalaad33592018-08-07 17:20:35 -0700590 if (!mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700591 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800592 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700593
Vadim Tryshev2c430b32018-04-04 14:45:21 -0700594 if (canAnnouncePageDescription()) {
595 announcePageForAccessibility();
Adam Cohen0ffac432013-07-10 11:19:26 -0700596 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
Michael Jurka0142d492010-08-25 17:46:15 -0700598 return false;
599 }
600
601 @Override
602 public void computeScroll() {
603 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 }
605
Sunny Goyalce8809a2018-01-18 14:02:47 -0800606 public int getExpectedHeight() {
607 return getMeasuredHeight();
608 }
609
Adam Cohen410f3cd2013-09-22 12:09:32 -0700610 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800611 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800612 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700613 }
614
Sunny Goyalce8809a2018-01-18 14:02:47 -0800615 public int getExpectedWidth() {
616 return getMeasuredWidth();
617 }
618
Sunny Goyalbc683e92017-12-06 10:25:07 -0800619 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800620 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800621 - mInsets.left - mInsets.right;
622 }
623
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 @Override
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800625 public void requestLayout() {
626 mIsLayoutValid = false;
627 super.requestLayout();
628 }
629
630 @Override
631 public void forceLayout() {
632 mIsLayoutValid = false;
633 super.forceLayout();
634 }
635
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100636 private int getPageWidthSize(int widthSize) {
637 return (widthSize - mInsets.left - mInsets.right) / getPanelCount();
638 }
639
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800640 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700642 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700643 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
644 return;
645 }
646
Adam Cohen7d30a372013-07-01 17:03:59 -0700647 // We measure the dimensions of the PagedView to be larger than the pages so that when we
648 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700649 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
650 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
651 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700652 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700653
Adam Cohen7d30a372013-07-01 17:03:59 -0700654 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
655 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
656 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700657 }
658
Winson Chung8aad6102012-05-11 16:27:49 -0700659 // Return early if we aren't given a proper dimension
660 if (widthSize <= 0 || heightSize <= 0) {
661 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
662 return;
663 }
664
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700666 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700667 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700668
Sunny Goyalac00cba2017-11-13 15:58:01 -0800669 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100670 getPageWidthSize(widthSize), MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800671 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800672 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700673
Sunny Goyalac00cba2017-11-13 15:58:01 -0800674 // measureChildren takes accounts for content padding, we only need to care about extra
675 // space due to insets.
676 measureChildren(myWidthSpec, myHeightSpec);
677 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800678 }
679
Sunny Goyalcf25b522015-07-09 00:01:18 -0700680 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700682 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800683 mIsLayoutValid = true;
Vadim Trysheveb701c62018-05-14 16:00:15 -0700684 final int childCount = getChildCount();
685 boolean pageScrollChanged = false;
686 if (mPageScrolls == null || childCount != mPageScrolls.length) {
687 mPageScrolls = new int[childCount];
688 pageScrollChanged = true;
689 }
690
691 if (childCount == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700692 return;
693 }
694
Winson Chung785d2eb2011-04-14 16:08:02 -0700695 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700696
Vinit Nayaka406f722019-12-19 11:50:55 -0800697 boolean isScrollChanged = getPageScrolls(mPageScrolls, true, SIMPLE_SCROLL_LOGIC);
698 if (isScrollChanged) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700699 pageScrollChanged = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700700 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700701
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700702 final LayoutTransition transition = getLayoutTransition();
703 // If the transition is running defer updating max scroll, as some empty pages could
704 // still be present, and a max scroll change could cause sudden jumps in scroll.
705 if (transition != null && transition.isRunning()) {
706 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
707
708 @Override
709 public void startTransition(LayoutTransition transition, ViewGroup container,
710 View view, int transitionType) { }
711
712 @Override
713 public void endTransition(LayoutTransition transition, ViewGroup container,
714 View view, int transitionType) {
715 // Wait until all transitions are complete.
716 if (!transition.isRunning()) {
717 transition.removeTransitionListener(this);
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700718 updateMinAndMaxScrollX();
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700719 }
720 }
721 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700722 } else {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700723 updateMinAndMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700724 }
725
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700726 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
727 updateCurrentPageScroll();
728 mFirstLayout = false;
729 }
730
Sunny Goyal85a7fe02018-02-02 10:09:32 -0800731 if (mScroller.isFinished() && pageScrollChanged) {
Sunny Goyalc82c6392018-06-06 15:39:13 -0700732 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700733 }
Winson Chunge3193b92010-09-10 11:44:42 -0700734 }
735
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700736 /**
737 * Initializes {@code outPageScrolls} with scroll positions for view at that index. The length
738 * of {@code outPageScrolls} should be same as the the childCount
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700739 */
740 protected boolean getPageScrolls(int[] outPageScrolls, boolean layoutChildren,
741 ComputePageScrollsLogic scrollLogic) {
742 final int childCount = getChildCount();
743
744 final int startIndex = mIsRtl ? childCount - 1 : 0;
745 final int endIndex = mIsRtl ? -1 : childCount;
746 final int delta = mIsRtl ? -1 : 1;
747
Vinit Nayaka406f722019-12-19 11:50:55 -0800748 final int pageCenter = mOrientationHandler.getCenterForPage(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700749
Vinit Nayaka406f722019-12-19 11:50:55 -0800750 final int scrollOffsetStart = mOrientationHandler.getScrollOffsetStart(this, mInsets);
751 final int scrollOffsetEnd = mOrientationHandler.getScrollOffsetEnd(this, mInsets);
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700752 boolean pageScrollChanged = false;
Thales Limae4457742021-07-20 16:46:38 +0100753 int panelCount = getPanelCount();
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700754
Vinit Nayaka406f722019-12-19 11:50:55 -0800755 for (int i = startIndex, childStart = scrollOffsetStart; i != endIndex; i += delta) {
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700756 final View child = getPageAt(i);
757 if (scrollLogic.shouldIncludeView(child)) {
Vinit Nayaka406f722019-12-19 11:50:55 -0800758 ChildBounds bounds = mOrientationHandler.getChildBounds(child, childStart,
759 pageCenter, layoutChildren);
760 final int primaryDimension = bounds.primaryDimension;
761 final int childPrimaryEnd = bounds.childPrimaryEnd;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700762
Alex Chaub5c4d112021-03-29 17:37:53 +0100763 // In case the pages are of different width, align the page to left edge for non-RTL
764 // or right edge for RTL.
765 final int pageScroll =
766 mIsRtl ? childPrimaryEnd - scrollOffsetEnd : childStart - scrollOffsetStart;
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700767 if (outPageScrolls[i] != pageScroll) {
768 pageScrollChanged = true;
769 outPageScrolls[i] = pageScroll;
770 }
Thales Limae4457742021-07-20 16:46:38 +0100771 childStart += primaryDimension + getChildGap();
772
773 // This makes sure that the space is added after the page, not after each panel
774 if (i % panelCount == panelCount - 1) {
775 childStart += mPageSpacing;
776 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700777 }
778 }
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100779
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100780 if (panelCount > 1) {
781 for (int i = 0; i < childCount; i++) {
782 // In case we have multiple panels, always use left panel's page scroll for all
783 // panels on the screen.
784 int adjustedScroll = outPageScrolls[getLeftmostVisiblePageForIndex(i)];
785 if (outPageScrolls[i] != adjustedScroll) {
786 outPageScrolls[i] = adjustedScroll;
787 pageScrollChanged = true;
788 }
789 }
790 }
Sunny Goyal20a13ff2018-03-13 16:44:00 -0700791 return pageScrollChanged;
792 }
793
Sunny Goyala1fbd842015-05-20 13:40:27 -0700794 protected int getChildGap() {
795 return 0;
796 }
797
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700798 protected void updateMinAndMaxScrollX() {
Vinit Nayaka406f722019-12-19 11:50:55 -0800799 mMinScroll = computeMinScroll();
800 mMaxScroll = computeMaxScroll();
Tony Wickhamf549dab2016-05-16 09:54:06 -0700801 }
802
Vinit Nayaka406f722019-12-19 11:50:55 -0800803 protected int computeMinScroll() {
Tony Wickham66d1c2f2019-04-24 11:32:59 -0700804 return 0;
Tony50876bf2018-09-19 23:09:14 -0400805 }
806
Vinit Nayaka406f722019-12-19 11:50:55 -0800807 protected int computeMaxScroll() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700808 int childCount = getChildCount();
809 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700810 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700811 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700812 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700813 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700814 }
815 }
816
Adam Cohen3b185e22013-10-29 14:45:58 -0700817 public void setPageSpacing(int pageSpacing) {
818 mPageSpacing = pageSpacing;
819 requestLayout();
820 }
821
Tony50876bf2018-09-19 23:09:14 -0400822 public int getPageSpacing() {
823 return mPageSpacing;
824 }
825
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800826 private void dispatchPageCountChanged() {
827 if (mPageIndicator != null) {
828 mPageIndicator.setMarkersCount(getChildCount());
Winson Chungd2be3812013-07-16 11:11:32 -0700829 }
Adam Cohen2591f6a2011-10-25 14:36:40 -0700830 // This ensures that when children are added, they get the correct transforms / alphas
831 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700832 invalidate();
833 }
834
Michael Jurka8b805b12012-04-18 14:23:14 -0700835 @Override
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800836 public void onViewAdded(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800837 super.onViewAdded(child);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800838 dispatchPageCountChanged();
839 }
840
841 @Override
842 public void onViewRemoved(View child) {
Winson Chungc7c51582018-02-23 17:58:36 -0800843 super.onViewRemoved(child);
Tonya361c722017-07-04 09:43:06 -0700844 mCurrentPage = validateNewPage(mCurrentPage);
Sunny Goyal0bd7f4f2018-02-01 09:53:35 -0800845 dispatchPageCountChanged();
Winson Chungd2be3812013-07-16 11:11:32 -0700846 }
847
Adam Cohen73894962011-10-31 13:17:17 -0700848 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700849 if (index < 0 || index > getChildCount() - 1) return 0;
Vinit Nayaka406f722019-12-19 11:50:55 -0800850 View pageAtIndex = getPageAt(index);
851 return mOrientationHandler.getChildStart(pageAtIndex);
Adam Cohen73894962011-10-31 13:17:17 -0700852 }
853
Alex Chau7944ee52021-01-08 19:07:25 +0000854 protected int getChildVisibleSize(int index) {
855 View layout = getPageAt(index);
856 return mOrientationHandler.getMeasuredSize(layout);
857 }
858
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 @Override
860 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Andras Kloczl7e912442021-05-06 12:50:28 +0200861 int page = indexOfChild(child);
862 if (!isVisible(page) || !mScroller.isFinished()) {
Vadim Trysheveeaa8ee2018-04-11 12:23:42 -0700863 if (immediate) {
864 setCurrentPage(page);
865 } else {
866 snapToPage(page);
867 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 return true;
869 }
870 return false;
871 }
872
873 @Override
874 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700875 int focusablePage;
876 if (mNextPage != INVALID_PAGE) {
877 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700879 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
Winson Chung86f77532010-08-24 11:08:22 -0700881 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700883 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885 return false;
886 }
887
888 @Override
889 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800890 if (super.dispatchUnhandledMove(focused, direction)) {
891 return true;
892 }
893
894 if (mIsRtl) {
895 if (direction == View.FOCUS_LEFT) {
896 direction = View.FOCUS_RIGHT;
897 } else if (direction == View.FOCUS_RIGHT) {
898 direction = View.FOCUS_LEFT;
899 }
900 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200901
902 int currentPage = getNextPage();
903 int closestNeighbourIndex = -1;
904 int closestNeighbourDistance = Integer.MAX_VALUE;
905 // Find the closest neighbour page
906 for (int neighbourPageIndex : getNeighbourPageIndices(direction)) {
907 int distance = Math.abs(neighbourPageIndex - currentPage);
908 if (closestNeighbourDistance > distance) {
909 closestNeighbourDistance = distance;
910 closestNeighbourIndex = neighbourPageIndex;
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 }
912 }
Andras Kloczl7e912442021-05-06 12:50:28 +0200913 if (closestNeighbourIndex != -1) {
914 View page = getPageAt(closestNeighbourIndex);
915 snapToPage(closestNeighbourIndex);
916 page.requestFocus(direction);
917 return true;
918 }
919
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800920 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 }
922
923 @Override
924 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700925 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
926 return;
927 }
928
Andras Kloczl7e912442021-05-06 12:50:28 +0200929 // nextPage is more reliable when multiple control movements have been done in a short
930 // period of time
931 getPageIndices(getNextPage())
932 .addAll(getNeighbourPageIndices(direction))
933 .forEach(pageIndex ->
934 getPageAt(pageIndex).addFocusables(views, direction, focusableMode));
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 }
936
937 /**
938 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700939 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 *
Winson Chung86f77532010-08-24 11:08:22 -0700941 * This happens when live folders requery, and if they're off page, they
942 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 */
944 @Override
945 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700946 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 View v = focused;
948 while (true) {
949 if (v == current) {
950 super.focusableViewAvailable(focused);
951 return;
952 }
953 if (v == this) {
954 return;
955 }
956 ViewParent parent = v.getParent();
957 if (parent instanceof View) {
958 v = (View)v.getParent();
959 } else {
960 return;
961 }
962 }
963 }
964
965 /**
966 * {@inheritDoc}
967 */
968 @Override
969 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
970 if (disallowIntercept) {
971 // We need to make sure to cancel our long press if
972 // a scrollable widget takes over touch events
Andras Kloczl7a1ca0b2021-04-08 16:08:16 +0200973 cancelCurrentPageLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 }
975 super.requestDisallowInterceptTouchEvent(disallowIntercept);
976 }
977
978 @Override
979 public boolean onInterceptTouchEvent(MotionEvent ev) {
980 /*
981 * This method JUST determines whether we want to intercept the motion.
982 * If we return true, onTouchEvent will be called and we do the actual
983 * scrolling there.
984 */
985
Winson Chung45e1d6e2010-11-09 17:19:49 -0800986 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -0700987 if (getChildCount() <= 0) return false;
Sunny Goyalc4bb3732019-06-20 11:34:14 -0700988
989 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800990
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 /*
992 * Shortcut the most recurring case: the user is in the dragging
993 * state and he is moving his finger. We want to intercept this
994 * motion.
995 */
996 final int action = ev.getAction();
Sunny Goyalaad33592018-08-07 17:20:35 -0700997 if ((action == MotionEvent.ACTION_MOVE) && mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 return true;
999 }
1000
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 switch (action & MotionEvent.ACTION_MASK) {
1002 case MotionEvent.ACTION_MOVE: {
1003 /*
1004 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Vinit Nayaka406f722019-12-19 11:50:55 -08001005 * whether the user has moved far enough from their original down touch.
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001007 if (mActivePointerId != INVALID_POINTER) {
1008 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001009 }
1010 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
Vinit Nayaka406f722019-12-19 11:50:55 -08001011 // event. in that case, treat the first occurrence of a move event as a ACTION_DOWN
Michael Jurka1ff706b2010-09-14 17:35:20 -07001012 // i.e. fall through to the next case (don't break)
1013 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1014 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001015 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 }
1017
1018 case MotionEvent.ACTION_DOWN: {
1019 final float x = ev.getX();
1020 final float y = ev.getY();
1021 // Remember location of down touch
1022 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001023 mDownMotionY = y;
Jon Mirandaf52af432020-04-10 17:25:57 -07001024 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
Vinit Nayaka406f722019-12-19 11:50:55 -08001025 mLastMotionRemainder = 0;
1026 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001027 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 mActivePointerId = ev.getPointerId(0);
Sunny Goyalc39db732021-04-03 01:20:21 -07001029 updateIsBeingDraggedOnTouchDown(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 break;
1031 }
1032
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001034 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001035 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 break;
1037
1038 case MotionEvent.ACTION_POINTER_UP:
1039 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001040 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 break;
1042 }
1043
1044 /*
1045 * The only time we want to intercept motion events is if we are in the
1046 * drag mode.
1047 */
Sunny Goyalaad33592018-08-07 17:20:35 -07001048 return mIsBeingDragged;
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 }
1050
Tony Wickhama0aee212019-09-18 09:24:03 -07001051 /**
1052 * If being flinged and user touches the screen, initiate drag; otherwise don't.
1053 */
Sunny Goyalc39db732021-04-03 01:20:21 -07001054 private void updateIsBeingDraggedOnTouchDown(MotionEvent ev) {
Tony Wickhama0aee212019-09-18 09:24:03 -07001055 // mScroller.isFinished should be false when being flinged.
Sunny Goyalc39db732021-04-03 01:20:21 -07001056 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung4941f652020-06-18 15:11:53 -07001057 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mPageSlop / 3);
Tony Wickhama0aee212019-09-18 09:24:03 -07001058
1059 if (finishedScrolling) {
1060 mIsBeingDragged = false;
1061 if (!mScroller.isFinished() && !mFreeScroll) {
1062 setCurrentPage(getNextPage());
1063 pageEndTransition();
1064 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001065 mIsBeingDragged = !mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished();
Tony Wickhama0aee212019-09-18 09:24:03 -07001066 } else {
1067 mIsBeingDragged = true;
1068 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001069
1070 // Catch the edge effect if it is active.
1071 float displacement = mOrientationHandler.getSecondaryValue(ev.getX(), ev.getY())
1072 / mOrientationHandler.getSecondaryValue(getWidth(), getHeight());
1073 if (!mEdgeGlowLeft.isFinished()) {
1074 mEdgeGlowLeft.onPullDistance(0f, 1f - displacement);
1075 }
1076 if (!mEdgeGlowRight.isFinished()) {
1077 mEdgeGlowRight.onPullDistance(0f, displacement);
1078 }
Tony Wickhama0aee212019-09-18 09:24:03 -07001079 }
1080
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001081 public boolean isHandlingTouch() {
Sunny Goyalaad33592018-08-07 17:20:35 -07001082 return mIsBeingDragged;
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001083 }
1084
Adam Cohenf8d28232011-02-01 21:47:00 -08001085 protected void determineScrollingStart(MotionEvent ev) {
1086 determineScrollingStart(ev, 1.0f);
1087 }
1088
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 /*
1090 * Determines if we should change the touch state to start scrolling after the
1091 * user moves their touch point too far.
1092 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001093 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001094 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001096 if (pointerIndex == -1) return;
1097
Vinit Nayaka406f722019-12-19 11:50:55 -08001098 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev, pointerIndex);
1099 final int diff = (int) Math.abs(primaryDirection - mLastMotion);
Adam Cohenf8d28232011-02-01 21:47:00 -08001100 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung4941f652020-06-18 15:11:53 -07001101 boolean moved = diff > touchSlop || ev.getAction() == ACTION_MOVE_ALLOW_EASY_FLING;
Winson Chung321e9ee2010-08-09 13:37:56 -07001102
Vinit Nayaka406f722019-12-19 11:50:55 -08001103 if (moved) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001104 // Scroll if the user moved far enough along the X axis
Sunny Goyalaad33592018-08-07 17:20:35 -07001105 mIsBeingDragged = true;
Vinit Nayaka406f722019-12-19 11:50:55 -08001106 mTotalMotion += Math.abs(mLastMotion - primaryDirection);
1107 mLastMotion = primaryDirection;
1108 mLastMotionRemainder = 0;
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001109 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001110 // Stop listening for things like pinches.
1111 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001112 }
1113 }
1114
1115 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001116 // Try canceling the long press. It could also have been scheduled
1117 // by a distant descendant, so use the mAllowLongPress flag to block
1118 // everything
Sunny Goyal59969372021-05-06 12:11:44 -07001119 forEachVisiblePage(View::cancelLongPress);
Winson Chung321e9ee2010-08-09 13:37:56 -07001120 }
1121
Adam Cohenb5ba0972011-09-07 18:02:31 -07001122 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001123 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenedb40762013-07-18 16:45:45 -07001124 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001125 int panelCount = getPanelCount();
1126 int pageCount = getChildCount();
Adam Cohen3b185e22013-10-29 14:45:58 -07001127
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001128 int adjacentPage = page + panelCount;
Sunny Goyal70660032015-05-14 00:07:08 -07001129 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001130 adjacentPage = page - panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001131 }
1132
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001133 final int totalDistance;
1134 if (adjacentPage < 0 || adjacentPage > pageCount - 1) {
1135 totalDistance = (v.getMeasuredWidth() + mPageSpacing) * panelCount;
Adam Cohen3b185e22013-10-29 14:45:58 -07001136 } else {
1137 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1138 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001139
1140 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001141 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001142 scrollProgress = Math.max(scrollProgress, -MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001143 return scrollProgress;
1144 }
1145
Adam Cohenedb40762013-07-18 16:45:45 -07001146 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001147 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001148 return 0;
1149 } else {
1150 return mPageScrolls[index];
1151 }
1152 }
1153
Adam Cohen564a2e72013-10-09 14:47:32 -07001154 // While layout transitions are occurring, a child's position may stray from its baseline
1155 // position. This method returns the magnitude of this stray at any given time.
1156 public int getLayoutTransitionOffsetForPage(int index) {
1157 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1158 return 0;
1159 } else {
1160 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001161
Sunny Goyal228153d2018-01-04 15:35:22 -08001162 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1163 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001164 return (int) (child.getX() - baselineX);
1165 }
1166 }
1167
Tony Wickham023f4042019-01-29 10:52:31 -08001168 public void setEnableFreeScroll(boolean freeScroll) {
Winson Chung0e449002019-04-26 11:09:58 -07001169 if (mFreeScroll == freeScroll) {
1170 return;
1171 }
1172
Tony Wickham8f7ead32016-04-07 18:46:44 -07001173 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001174 mFreeScroll = freeScroll;
1175
Adam Cohenf9618852013-11-08 06:45:03 -08001176 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001177 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001178 } else if (wasFreeScroll) {
Tony Wickhamaf33f2c2019-10-04 12:55:22 -07001179 if (getScrollForPage(getNextPage()) != getScrollX()) {
1180 snapToPage(getNextPage());
1181 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001182 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001183 }
1184
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001185 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001186 mAllowOverScroll = enable;
1187 }
1188
Alex Chau822acf42021-06-03 11:35:05 +01001189 protected float getSignificantMoveThreshold() {
1190 return SIGNIFICANT_MOVE_THRESHOLD;
1191 }
1192
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 @Override
1194 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001195 // Skip touch handling if there are no pages to swipe
Tony Wickhamb6841ac2019-07-22 12:39:59 -07001196 if (getChildCount() <= 0) return false;
Winson Chung45e1d6e2010-11-09 17:19:49 -08001197
Michael Jurkab8f06722010-10-10 15:58:46 -07001198 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199
1200 final int action = ev.getAction();
1201
1202 switch (action & MotionEvent.ACTION_MASK) {
1203 case MotionEvent.ACTION_DOWN:
Sunny Goyalc39db732021-04-03 01:20:21 -07001204 updateIsBeingDraggedOnTouchDown(ev);
Tony Wickhama0aee212019-09-18 09:24:03 -07001205
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 /*
1207 * If being flinged and user touches, stop the fling. isFinished
1208 * will be false if being flinged.
1209 */
1210 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001211 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 }
1213
1214 // Remember where the motion event started
Vinit Nayaka406f722019-12-19 11:50:55 -08001215 mDownMotionX = ev.getX();
Sunny Goyal7c7be8c2018-03-07 19:58:07 -08001216 mDownMotionY = ev.getY();
Vinit Nayaka406f722019-12-19 11:50:55 -08001217 mDownMotionPrimary = mLastMotion = mOrientationHandler.getPrimaryDirection(ev, 0);
1218 mLastMotionRemainder = 0;
1219 mTotalMotion = 0;
Winson Chung4941f652020-06-18 15:11:53 -07001220 mAllowEasyFling = false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 mActivePointerId = ev.getPointerId(0);
Sunny Goyalaad33592018-08-07 17:20:35 -07001222 if (mIsBeingDragged) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001223 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001224 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 break;
1226
Winson Chung4941f652020-06-18 15:11:53 -07001227 case ACTION_MOVE_ALLOW_EASY_FLING:
1228 // Start scrolling immediately
1229 determineScrollingStart(ev);
1230 mAllowEasyFling = true;
1231 break;
1232
1233 case MotionEvent.ACTION_MOVE:
1234 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 // Scroll to follow the motion event
1236 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001237
1238 if (pointerIndex == -1) return true;
Sunny Goyalc39db732021-04-03 01:20:21 -07001239 float oldScroll = mOrientationHandler.getPrimaryScroll(this);
1240 float dx = ev.getX(pointerIndex);
1241 float dy = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001242
Sunny Goyalc39db732021-04-03 01:20:21 -07001243 float direction = mOrientationHandler.getPrimaryValue(dx, dy);
Vinit Nayaka406f722019-12-19 11:50:55 -08001244 float delta = mLastMotion + mLastMotionRemainder - direction;
Sunny Goyalc39db732021-04-03 01:20:21 -07001245
1246 int width = getWidth();
1247 int height = getHeight();
1248 int size = mOrientationHandler.getPrimaryValue(width, height);
1249
1250 final float displacement = mOrientationHandler.getSecondaryValue(dx, dy)
1251 / mOrientationHandler.getSecondaryValue(width, height);
Vinit Nayaka406f722019-12-19 11:50:55 -08001252 mTotalMotion += Math.abs(delta);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001253
Sunny Goyalc39db732021-04-03 01:20:21 -07001254 if (mAllowOverScroll) {
1255 float consumed = 0;
1256 if (delta < 0 && mEdgeGlowRight.getDistance() != 0f) {
1257 consumed = size * mEdgeGlowRight.onPullDistance(delta / size, displacement);
1258 } else if (delta > 0 && mEdgeGlowLeft.getDistance() != 0f) {
1259 consumed = -size * mEdgeGlowLeft.onPullDistance(
1260 -delta / size, 1 - displacement);
1261 }
1262 delta -= consumed;
1263 }
Alex Chau822acf42021-06-03 11:35:05 +01001264 delta /= mOrientationHandler.getPrimaryScale(this);
Sunny Goyalc39db732021-04-03 01:20:21 -07001265
Winson Chungc0844aa2011-02-02 15:25:58 -08001266 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1267 // keep the remainder because we are actually testing if we've moved from the last
1268 // scrolled position (which is discrete).
Sunny Goyalc39db732021-04-03 01:20:21 -07001269 mLastMotion = direction;
1270 int movedDelta = (int) delta;
1271 mLastMotionRemainder = delta - movedDelta;
Vinit Nayaka406f722019-12-19 11:50:55 -08001272
Sunny Goyalc39db732021-04-03 01:20:21 -07001273 if (delta != 0) {
1274 mOrientationHandler.set(this, VIEW_SCROLL_BY, movedDelta);
1275
1276 if (mAllowOverScroll) {
1277 final float pulledToX = oldScroll + delta;
1278
1279 if (pulledToX < mMinScroll) {
1280 mEdgeGlowLeft.onPullDistance(-delta / size, 1.f - displacement);
1281 if (!mEdgeGlowRight.isFinished()) {
1282 mEdgeGlowRight.onRelease();
1283 }
1284 } else if (pulledToX > mMaxScroll) {
1285 mEdgeGlowRight.onPullDistance(delta / size, displacement);
1286 if (!mEdgeGlowLeft.isFinished()) {
1287 mEdgeGlowLeft.onRelease();
1288 }
1289 }
1290
1291 if (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()) {
1292 postInvalidateOnAnimation();
1293 }
1294 }
1295
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 } else {
1297 awakenScrollBars();
1298 }
Adam Cohen564976a2010-10-13 18:52:07 -07001299 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 determineScrollingStart(ev);
1301 }
1302 break;
1303
1304 case MotionEvent.ACTION_UP:
Sunny Goyalaad33592018-08-07 17:20:35 -07001305 if (mIsBeingDragged) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 final int activePointerId = mActivePointerId;
1307 final int pointerIndex = ev.findPointerIndex(activePointerId);
Vinit Nayak93bf1fe2020-05-26 17:22:13 -07001308 if (pointerIndex == -1) return true;
1309
Vinit Nayaka406f722019-12-19 11:50:55 -08001310 final float primaryDirection = mOrientationHandler.getPrimaryDirection(ev,
1311 pointerIndex);
Sunny Goyal4bb8b062019-02-19 16:37:38 -08001312 final VelocityTracker velocityTracker = mVelocityTracker;
1313 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Adam Cohenaefd4e12011-02-14 16:39:38 -08001314
Vinit Nayaka406f722019-12-19 11:50:55 -08001315 int velocity = (int) mOrientationHandler.getPrimaryVelocity(velocityTracker,
1316 mActivePointerId);
Alex Chau822acf42021-06-03 11:35:05 +01001317 float delta = primaryDirection - mDownMotionPrimary;
1318 delta /= mOrientationHandler.getPrimaryScale(this);
Vinit Nayaka406f722019-12-19 11:50:55 -08001319 int pageOrientedSize = mOrientationHandler.getMeasuredSize(getPageAt(mCurrentPage));
1320
Alex Chau822acf42021-06-03 11:35:05 +01001321 boolean isSignificantMove = Math.abs(delta)
1322 > pageOrientedSize * getSignificantMoveThreshold();
Vinit Nayaka406f722019-12-19 11:50:55 -08001323
1324 mTotalMotion += Math.abs(mLastMotion + mLastMotionRemainder - primaryDirection);
Winson Chung4941f652020-06-18 15:11:53 -07001325 boolean passedSlop = mAllowEasyFling || mTotalMotion > mPageSlop;
1326 boolean isFling = passedSlop && shouldFlingForVelocity(velocity);
Vinit Nayaka406f722019-12-19 11:50:55 -08001327 boolean isDeltaLeft = mIsRtl ? delta > 0 : delta < 0;
1328 boolean isVelocityLeft = mIsRtl ? velocity > 0 : velocity < 0;
Winson Chung4941f652020-06-18 15:11:53 -07001329 if (DEBUG_FAILED_QUICKSWITCH && !isFling && mAllowEasyFling) {
1330 Log.d("Quickswitch", "isFling=false vel=" + velocity
1331 + " threshold=" + mEasyFlingThresholdVelocity);
1332 }
Adam Cohen00481b32011-11-18 12:03:48 -08001333
Adam Cohenf358a4b2013-07-23 16:47:31 -07001334 if (!mFreeScroll) {
1335 // In the case that the page is moved far to one direction and then is flung
1336 // in the opposite direction, we use a threshold to determine whether we should
1337 // just return to the starting page, or if we should skip one further.
1338 boolean returnToOriginalPage = false;
Vinit Nayaka406f722019-12-19 11:50:55 -08001339 if (Math.abs(delta) > pageOrientedSize * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1340 Math.signum(velocity) != Math.signum(delta) && isFling) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001341 returnToOriginalPage = true;
1342 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001343
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 int finalPage;
1345 // We give flings precedence over large moves, which is why we short-circuit our
1346 // test for a large move if a fling has been registered. That is, a large
1347 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyalb7b01352018-08-09 12:31:20 -07001348
Vinit Nayaka406f722019-12-19 11:50:55 -08001349 if (((isSignificantMove && !isDeltaLeft && !isFling) ||
1350 (isFling && !isVelocityLeft)) && mCurrentPage > 0) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001351 finalPage = returnToOriginalPage
1352 ? mCurrentPage : mCurrentPage - getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001353 snapToPageWithVelocity(finalPage, velocity);
1354 } else if (((isSignificantMove && isDeltaLeft && !isFling) ||
1355 (isFling && isVelocityLeft)) &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07001356 mCurrentPage < getChildCount() - 1) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001357 finalPage = returnToOriginalPage
1358 ? mCurrentPage : mCurrentPage + getPanelCount();
Vinit Nayaka406f722019-12-19 11:50:55 -08001359 snapToPageWithVelocity(finalPage, velocity);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001360 } else {
1361 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001362 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 } else {
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001364 if (!mScroller.isFinished()) {
1365 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366 }
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001367
Vinit Nayaka406f722019-12-19 11:50:55 -08001368 int initialScroll = mOrientationHandler.getPrimaryScroll(this);
1369 int maxScroll = mMaxScroll;
1370 int minScroll = mMinScroll;
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001371
Vinit Nayaka406f722019-12-19 11:50:55 -08001372 if (((initialScroll >= maxScroll) && (isVelocityLeft || !isFling)) ||
1373 ((initialScroll <= minScroll) && (!isVelocityLeft || !isFling))) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001374 mScroller.springBack(initialScroll, 0, minScroll, maxScroll, 0, 0);
Alex Chaub794ea62021-02-03 18:00:30 +00001375 mNextPage = getDestinationPage();
Sunny Goyalb7b01352018-08-09 12:31:20 -07001376 } else {
Sunny Goyalc39db732021-04-03 01:20:21 -07001377 int velocity1 = -velocity;
1378 // Continue a scroll or fling in progress
1379 mScroller.fling(initialScroll, 0, velocity1, 0, minScroll, maxScroll, 0, 0,
1380 Math.round(getWidth() * 0.5f * OVERSCROLL_DAMP_FACTOR), 0);
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001381
Sunny Goyalc39db732021-04-03 01:20:21 -07001382 int finalPos = mScroller.getFinalX();
Alex Chaub794ea62021-02-03 18:00:30 +00001383 mNextPage = getDestinationPage(finalPos);
Sunny Goyalc39db732021-04-03 01:20:21 -07001384 onNotSnappingToPageInFreeScroll();
Santiago Etchebehere260b4cb2018-06-15 16:42:19 -07001385 }
1386 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001389
Sunny Goyalc39db732021-04-03 01:20:21 -07001390 mEdgeGlowLeft.onRelease();
1391 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 // End any intermediate reordering states
1393 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 break;
1395
1396 case MotionEvent.ACTION_CANCEL:
Sunny Goyalaad33592018-08-07 17:20:35 -07001397 if (mIsBeingDragged) {
Michael Jurkab8f06722010-10-10 15:58:46 -07001398 snapToDestination();
1399 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001400 mEdgeGlowLeft.onRelease();
1401 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001402 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 break;
1404
1405 case MotionEvent.ACTION_POINTER_UP:
1406 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 break;
1409 }
1410
1411 return true;
1412 }
1413
Sunny Goyalc39db732021-04-03 01:20:21 -07001414 protected void onNotSnappingToPageInFreeScroll() { }
Alex Chaub794ea62021-02-03 18:00:30 +00001415
Vinit Nayaka406f722019-12-19 11:50:55 -08001416 protected boolean shouldFlingForVelocity(int velocity) {
Winson Chung4941f652020-06-18 15:11:53 -07001417 float threshold = mAllowEasyFling ? mEasyFlingThresholdVelocity : mFlingThresholdVelocity;
1418 return Math.abs(velocity) > threshold;
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001419 }
1420
Adam Cohen7d30a372013-07-01 17:03:59 -07001421 private void resetTouchState() {
1422 releaseVelocityTracker();
Sunny Goyalaad33592018-08-07 17:20:35 -07001423 mIsBeingDragged = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001424 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001425 }
1426
Winson Chung185d7162011-02-28 13:47:29 -08001427 @Override
1428 public boolean onGenericMotionEvent(MotionEvent event) {
1429 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1430 switch (event.getAction()) {
1431 case MotionEvent.ACTION_SCROLL: {
1432 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1433 final float vscroll;
1434 final float hscroll;
1435 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1436 vscroll = 0;
1437 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1438 } else {
1439 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1440 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1441 }
Sunny Goyal8b37c572020-03-31 12:12:14 -07001442 if (!canScroll(Math.abs(vscroll), Math.abs(hscroll))) {
1443 return false;
Samuel Fufa59cba192019-08-22 18:31:34 -07001444 }
Winson Chung185d7162011-02-28 13:47:29 -08001445 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001446 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001447 : (hscroll > 0 || vscroll > 0);
1448 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001449 scrollRight();
1450 } else {
1451 scrollLeft();
1452 }
1453 return true;
1454 }
1455 }
1456 }
1457 }
1458 return super.onGenericMotionEvent(event);
1459 }
1460
Sunny Goyal8b37c572020-03-31 12:12:14 -07001461 /**
1462 * Returns true if the paged view can scroll for the provided vertical and horizontal
1463 * scroll values
1464 */
1465 protected boolean canScroll(float absVScroll, float absHScroll) {
1466 ActivityContext ac = ActivityContext.lookupContext(getContext());
1467 return (ac == null || AbstractFloatingView.getTopOpenView(ac) == null);
Samuel Fufa59cba192019-08-22 18:31:34 -07001468 }
1469
Michael Jurkab8f06722010-10-10 15:58:46 -07001470 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1471 if (mVelocityTracker == null) {
1472 mVelocityTracker = VelocityTracker.obtain();
1473 }
1474 mVelocityTracker.addMovement(ev);
1475 }
1476
1477 private void releaseVelocityTracker() {
1478 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001479 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001480 mVelocityTracker.recycle();
1481 mVelocityTracker = null;
1482 }
1483 }
1484
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 private void onSecondaryPointerUp(MotionEvent ev) {
Winson Chung4941f652020-06-18 15:11:53 -07001486 final int pointerIndex = ev.getActionIndex();
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 final int pointerId = ev.getPointerId(pointerIndex);
1488 if (pointerId == mActivePointerId) {
1489 // This was our active pointer going up. Choose a new
1490 // active pointer and adjust accordingly.
1491 // TODO: Make this decision more intelligent.
1492 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
Vinit Nayaka406f722019-12-19 11:50:55 -08001493 mLastMotion = mDownMotionPrimary = mOrientationHandler.getPrimaryDirection(ev,
1494 newPointerIndex);
1495 mLastMotionRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 mActivePointerId = ev.getPointerId(newPointerIndex);
1497 if (mVelocityTracker != null) {
1498 mVelocityTracker.clear();
1499 }
1500 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001501 }
1502
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 @Override
1504 public void requestChildFocus(View child, View focused) {
1505 super.requestChildFocus(child, focused);
Andras Kloczl359d1ae2021-05-04 22:25:27 +02001506
1507 // In case the device is controlled by a controller, mCurrentPage isn't updated properly
1508 // which results in incorrect navigation
1509 int nextPage = getNextPage();
1510 if (nextPage != mCurrentPage) {
1511 setCurrentPage(nextPage);
1512 }
1513
Andras Kloczl7e912442021-05-06 12:50:28 +02001514 int page = indexOfChild(child);
1515 if (page >= 0 && !isVisible(page) && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001516 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001517 }
1518 }
1519
Alex Chaub794ea62021-02-03 18:00:30 +00001520 public int getDestinationPage() {
1521 return getDestinationPage(mOrientationHandler.getPrimaryScroll(this));
1522 }
1523
Alex Chaudedbc8a2021-03-17 16:53:26 +00001524 protected int getDestinationPage(int primaryScroll) {
1525 return getPageNearestToCenterOfScreen(primaryScroll);
Alex Chaub794ea62021-02-03 18:00:30 +00001526 }
1527
Sunny Goyal228153d2018-01-04 15:35:22 -08001528 public int getPageNearestToCenterOfScreen() {
Vinit Nayaka406f722019-12-19 11:50:55 -08001529 return getPageNearestToCenterOfScreen(mOrientationHandler.getPrimaryScroll(this));
Tony Wickham8f7ead32016-04-07 18:46:44 -07001530 }
1531
Alex Chaudedbc8a2021-03-17 16:53:26 +00001532 private int getPageNearestToCenterOfScreen(int primaryScroll) {
1533 int screenCenter = getScreenCenter(primaryScroll);
Adam Cohen22f823d2011-09-01 17:22:18 -07001534 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001535 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001536 final int childCount = getChildCount();
1537 for (int i = 0; i < childCount; ++i) {
Tony Wickham7383d4e2020-07-07 19:25:25 -07001538 int distanceFromScreenCenter = Math.abs(
1539 getDisplacementFromScreenCenter(i, screenCenter));
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1541 minDistanceFromScreenCenter = distanceFromScreenCenter;
1542 minDistanceFromScreenCenterIndex = i;
1543 }
1544 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001545 return minDistanceFromScreenCenterIndex;
1546 }
1547
Tony Wickham7383d4e2020-07-07 19:25:25 -07001548 private int getDisplacementFromScreenCenter(int childIndex, int screenCenter) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001549 int childSize = Math.round(getChildVisibleSize(childIndex));
Tony Wickham7383d4e2020-07-07 19:25:25 -07001550 int halfChildSize = (childSize / 2);
1551 int childCenter = getChildOffset(childIndex) + halfChildSize;
1552 return childCenter - screenCenter;
1553 }
1554
1555 protected int getDisplacementFromScreenCenter(int childIndex) {
Alex Chaudedbc8a2021-03-17 16:53:26 +00001556 int primaryScroll = mOrientationHandler.getPrimaryScroll(this);
1557 int screenCenter = getScreenCenter(primaryScroll);
Tony Wickham7383d4e2020-07-07 19:25:25 -07001558 return getDisplacementFromScreenCenter(childIndex, screenCenter);
1559 }
1560
Alex Chaudedbc8a2021-03-17 16:53:26 +00001561 private int getScreenCenter(int primaryScroll) {
1562 float primaryScale = mOrientationHandler.getPrimaryScale(this);
1563 float primaryPivot = mOrientationHandler.getPrimaryValue(getPivotX(), getPivotY());
1564 int pageOrientationSize = mOrientationHandler.getMeasuredSize(this);
1565 return Math.round(primaryScroll + (pageOrientationSize / 2f - primaryPivot) / primaryScale
1566 + primaryPivot);
1567 }
1568
Adam Cohend19d3ca2010-09-15 14:43:42 -07001569 protected void snapToDestination() {
Sunny Goyalc39db732021-04-03 01:20:21 -07001570 snapToPage(getDestinationPage(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 }
1572
Adam Cohene0f66b52010-11-23 15:06:07 -08001573 // We want the duration of the page snap animation to be influenced by the distance that
1574 // the screen has to travel, however, we don't want this duration to be effected in a
1575 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1576 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001577 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001578 f -= 0.5f; // center the values about 0.
1579 f *= 0.3f * Math.PI / 2.0f;
1580 return (float) Math.sin(f);
1581 }
1582
Winson Chung05a31ed2018-02-08 17:08:43 -08001583 protected boolean snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001584 whichPage = validateNewPage(whichPage);
Vinit Nayaka406f722019-12-19 11:50:55 -08001585 int halfScreenSize = mOrientationHandler.getMeasuredSize(this) / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001586
Vinit Nayaka406f722019-12-19 11:50:55 -08001587 final int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001588 int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
Adam Cohene0f66b52010-11-23 15:06:07 -08001589 int duration = 0;
1590
Sunny Goyal4d113a52015-05-27 10:05:28 -07001591 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001592 // If the velocity is low enough, then treat this more as an automatic page advance
1593 // as opposed to an apparent physical response to flinging
Winson Chung05a31ed2018-02-08 17:08:43 -08001594 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001595 }
1596
1597 // Here we compute a "distance" that will be used in the computation of the overall
1598 // snap duration. This is a function of the actual distance that needs to be traveled;
1599 // we keep this value close to half screen size in order to reduce the variance in snap
1600 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001601 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001602 float distance = halfScreenSize + halfScreenSize *
1603 distanceInfluenceForSnapDuration(distanceRatio);
1604
1605 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001606 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001607
1608 // we want the page's snap velocity to approximately match the velocity at which the
1609 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001610 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1611 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001612
Sunny Goyalc39db732021-04-03 01:20:21 -07001613 return snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001614 }
1615
Winson Chung05a31ed2018-02-08 17:08:43 -08001616 public boolean snapToPage(int whichPage) {
1617 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001618 }
1619
Winson Chung05a31ed2018-02-08 17:08:43 -08001620 public boolean snapToPageImmediately(int whichPage) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001621 return snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
Adam Cohen7d30a372013-07-01 17:03:59 -07001622 }
1623
Winson Chung05a31ed2018-02-08 17:08:43 -08001624 public boolean snapToPage(int whichPage, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001625 return snapToPage(whichPage, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001626 }
1627
Sunny Goyalc39db732021-04-03 01:20:21 -07001628 protected boolean snapToPage(int whichPage, int duration, boolean immediate) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001629 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001630
Vinit Nayaka406f722019-12-19 11:50:55 -08001631 int newLoc = getScrollForPage(whichPage);
Sunny Goyalc39db732021-04-03 01:20:21 -07001632 final int delta = newLoc - mOrientationHandler.getPrimaryScroll(this);
1633 return snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07001634 }
1635
Winson Chung05a31ed2018-02-08 17:08:43 -08001636 protected boolean snapToPage(int whichPage, int delta, int duration) {
Sunny Goyalc39db732021-04-03 01:20:21 -07001637 return snapToPage(whichPage, delta, duration, false);
Adam Cohen7d30a372013-07-01 17:03:59 -07001638 }
Michael Jurka0142d492010-08-25 17:46:15 -07001639
Sunny Goyalc39db732021-04-03 01:20:21 -07001640 protected boolean snapToPage(int whichPage, int delta, int duration, boolean immediate) {
Sunny Goyal9bb8ffb2018-05-24 17:11:16 -07001641 if (mFirstLayout) {
1642 setCurrentPage(whichPage);
1643 return false;
1644 }
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001645
Andy Wickhamda5a89b2021-07-23 12:38:14 -10001646 if (FeatureFlags.IS_STUDIO_BUILD && !Utilities.IS_RUNNING_IN_TEST_HARNESS) {
Tony Wickhamcb3f8702018-08-27 17:36:03 -07001647 duration *= Settings.Global.getFloat(getContext().getContentResolver(),
1648 Settings.Global.WINDOW_ANIMATION_SCALE, 1);
Tony Wickhamd58c2d52018-05-04 12:10:55 -07001649 }
1650
Adam Cohen6f127a62014-06-12 14:54:41 -07001651 whichPage = validateNewPage(whichPage);
1652
Adam Cohen7d30a372013-07-01 17:03:59 -07001653 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001654
Winson Chung321e9ee2010-08-09 13:37:56 -07001655 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001656 if (immediate) {
1657 duration = 0;
1658 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 duration = Math.abs(delta);
1660 }
1661
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001662 if (duration != 0) {
1663 pageBeginTransition();
1664 }
1665
Adam Cohenf358a4b2013-07-23 16:47:31 -07001666 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001667 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001668 }
Adam Cohenf9618852013-11-08 06:45:03 -08001669
Sunny Goyalc39db732021-04-03 01:20:21 -07001670 mScroller.startScroll(mOrientationHandler.getPrimaryScroll(this), 0, delta, 0, duration);
Adam Cohen674531f2013-12-13 15:07:14 -08001671 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001672
1673 // Trigger a compute() to finish switching pages if necessary
1674 if (immediate) {
1675 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001676 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 }
1678
Winson Chung321e9ee2010-08-09 13:37:56 -07001679 invalidate();
Winson Chung05a31ed2018-02-08 17:08:43 -08001680 return Math.abs(delta) > 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001681 }
1682
Vadim Tryshev98913d02018-05-18 18:41:34 -07001683 public boolean scrollLeft() {
1684 if (getNextPage() > 0) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001685 snapToPage(getNextPage() - getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001686 return true;
1687 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001688 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001689 }
1690
Vadim Tryshev98913d02018-05-18 18:41:34 -07001691 public boolean scrollRight() {
1692 if (getNextPage() < getChildCount() - 1) {
Andras Kloczl77b3eb02021-07-15 11:15:24 +02001693 snapToPage(getNextPage() + getPanelCount());
Vadim Tryshev98913d02018-05-18 18:41:34 -07001694 return true;
1695 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001696 return mAllowOverScroll;
Winson Chung321e9ee2010-08-09 13:37:56 -07001697 }
1698
Vadim Trysheve6bbefb2018-04-03 13:38:06 -07001699 @Override
1700 public CharSequence getAccessibilityClassName() {
1701 // Some accessibility services have special logic for ScrollView. Since we provide same
1702 // accessibility info as ScrollView, inform the service to handle use the same way.
1703 return ScrollView.class.getName();
1704 }
1705
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001706 protected boolean isPageOrderFlipped() {
1707 return false;
1708 }
1709
Winson Chung6a0f57d2011-06-29 20:10:49 -07001710 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001711 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001712 @Override
1713 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1714 super.onInitializeAccessibilityNodeInfo(info);
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001715 final boolean pagesFlipped = isPageOrderFlipped();
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001716 int offset = (mAllowOverScroll ? 0 : 1);
1717 info.setScrollable(getPageCount() > offset);
1718 if (getCurrentPage() < getPageCount() - offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001719 info.addAction(pagesFlipped ?
1720 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD
1721 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
1722 info.addAction(mIsRtl ?
1723 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT
1724 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001725 }
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001726 if (getCurrentPage() >= offset) {
yingleiw02cc8482019-08-02 16:14:27 -07001727 info.addAction(pagesFlipped ?
1728 AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD
1729 : AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
1730 info.addAction(mIsRtl ?
1731 AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_RIGHT
1732 : AccessibilityNodeInfo.AccessibilityAction.ACTION_PAGE_LEFT);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001733 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001734 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1735 // Besides disabling the accessibility long-click, this also prevents this view from getting
1736 // accessibility focus.
1737 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001738 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001739 }
1740
1741 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001742 public void sendAccessibilityEvent(int eventType) {
1743 // Don't let the view send real scroll events.
1744 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1745 super.sendAccessibilityEvent(eventType);
1746 }
1747 }
1748
1749 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001750 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1751 super.onInitializeAccessibilityEvent(event);
Pinyao Tingaf6daa22019-09-18 22:18:03 +00001752 event.setScrollable(mAllowOverScroll || getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001753 }
1754
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001755 @Override
1756 public boolean performAccessibilityAction(int action, Bundle arguments) {
1757 if (super.performAccessibilityAction(action, arguments)) {
1758 return true;
1759 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001760 final boolean pagesFlipped = isPageOrderFlipped();
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001761 switch (action) {
1762 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001763 if (pagesFlipped ? scrollLeft() : scrollRight()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001764 return true;
1765 }
1766 } break;
1767 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Vadim Tryshev98913d02018-05-18 18:41:34 -07001768 if (pagesFlipped ? scrollRight() : scrollLeft()) {
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001769 return true;
1770 }
yingleiw02cc8482019-08-02 16:14:27 -07001771 } break;
1772 case android.R.id.accessibilityActionPageRight: {
1773 if (!mIsRtl) {
1774 return scrollRight();
1775 } else {
1776 return scrollLeft();
1777 }
Vadim Tryshev0fc07132018-05-04 12:08:54 -07001778 }
yingleiw02cc8482019-08-02 16:14:27 -07001779 case android.R.id.accessibilityActionPageLeft: {
1780 if (!mIsRtl) {
1781 return scrollLeft();
1782 } else {
1783 return scrollRight();
1784 }
1785 }
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001786 }
1787 return false;
1788 }
1789
Vadim Tryshev2c430b32018-04-04 14:45:21 -07001790 protected boolean canAnnouncePageDescription() {
1791 return true;
1792 }
1793
Adam Cohen0ffac432013-07-10 11:19:26 -07001794 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001795 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001796 getNextPage() + 1, getChildCount());
1797 }
1798
Tony Mak2e564402018-02-27 17:19:34 +00001799 protected float getDownMotionX() {
1800 return mDownMotionX;
1801 }
1802
1803 protected float getDownMotionY() {
1804 return mDownMotionY;
1805 }
1806
Sunny Goyal20a13ff2018-03-13 16:44:00 -07001807 protected interface ComputePageScrollsLogic {
1808
1809 boolean shouldIncludeView(View view);
1810 }
Vadim Tryshev528b9e02018-05-24 18:22:03 -07001811
1812 public int[] getVisibleChildrenRange() {
1813 float visibleLeft = 0;
1814 float visibleRight = visibleLeft + getMeasuredWidth();
1815 float scaleX = getScaleX();
1816 if (scaleX < 1 && scaleX > 0) {
1817 float mid = getMeasuredWidth() / 2;
1818 visibleLeft = mid - ((mid - visibleLeft) / scaleX);
1819 visibleRight = mid + ((visibleRight - mid) / scaleX);
1820 }
1821
1822 int leftChild = -1;
1823 int rightChild = -1;
1824 final int childCount = getChildCount();
1825 for (int i = 0; i < childCount; i++) {
1826 final View child = getPageAt(i);
1827
1828 float left = child.getLeft() + child.getTranslationX() - getScrollX();
1829 if (left <= visibleRight && (left + child.getMeasuredWidth()) >= visibleLeft) {
1830 if (leftChild == -1) {
1831 leftChild = i;
1832 }
1833 rightChild = i;
1834 }
1835 }
1836 mTmpIntPair[0] = leftChild;
1837 mTmpIntPair[1] = rightChild;
1838 return mTmpIntPair;
1839 }
Sunny Goyalc39db732021-04-03 01:20:21 -07001840
1841 @Override
1842 public void draw(Canvas canvas) {
1843 super.draw(canvas);
1844 drawEdgeEffect(canvas);
Sunny Goyal05b59c02021-04-29 13:55:40 -07001845 pageEndTransition();
Sunny Goyalc39db732021-04-03 01:20:21 -07001846 }
1847
1848 protected void drawEdgeEffect(Canvas canvas) {
1849 if (mAllowOverScroll && (!mEdgeGlowRight.isFinished() || !mEdgeGlowLeft.isFinished())) {
1850 final int width = getWidth();
1851 final int height = getHeight();
1852 if (!mEdgeGlowLeft.isFinished()) {
1853 final int restoreCount = canvas.save();
1854 canvas.rotate(-90);
1855 canvas.translate(-height, Math.min(mMinScroll, getScrollX()));
1856 mEdgeGlowLeft.setSize(height, width);
1857 if (mEdgeGlowLeft.draw(canvas)) {
1858 postInvalidateOnAnimation();
1859 }
1860 canvas.restoreToCount(restoreCount);
1861 }
1862 if (!mEdgeGlowRight.isFinished()) {
1863 final int restoreCount = canvas.save();
1864 canvas.rotate(90, width, 0);
1865 canvas.translate(width, -(Math.max(mMaxScroll, getScrollX())));
1866
1867 mEdgeGlowRight.setSize(height, width);
1868 if (mEdgeGlowRight.draw(canvas)) {
1869 postInvalidateOnAnimation();
1870 }
1871 canvas.restoreToCount(restoreCount);
1872 }
1873 }
1874 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001875}