blob: ad94a6b5fa5017edf2704f359cd01000c97b74d3 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Sunny Goyald0030b02017-12-08 15:07:24 -080019import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20import static com.android.launcher3.compat.AccessibilityManagerCompat.isObservedEventType;
21
Winson Chung228a0fa2011-01-26 22:14:13 -080022import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080023import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070024import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ObjectAnimator;
26import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070027import android.annotation.SuppressLint;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070029import android.content.res.TypedArray;
Adam Cohen7d30a372013-07-01 17:03:59 -070030import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080037import android.view.InputDevice;
38import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070039import android.view.MotionEvent;
40import android.view.VelocityTracker;
41import android.view.View;
42import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080043import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080048import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070049
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070050import com.android.launcher3.anim.Interpolators;
Sunny Goyal9e76f682017-02-13 12:13:43 -080051import com.android.launcher3.anim.PropertyListBuilder;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyalb72d8b22017-07-14 00:02:27 -070053import com.android.launcher3.touch.OverScroll;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070055
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chung321e9ee2010-08-09 13:37:56 -070058/**
59 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070060 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070061 */
Michael Jurka8b805b12012-04-18 14:23:14 -070062public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070064 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070065 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Winson Chung86f77532010-08-24 11:08:22 -070067 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070068 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Vadim Tryshevfedca432015-08-19 17:55:02 -070070 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Tony Wickham07b1d672018-01-04 17:16:05 -080071 public static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Sunny Goyalb72d8b22017-07-14 00:02:27 -070073 // OverScroll constants
Adam Cohen8d769d62017-06-23 17:27:38 -070074 private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270;
Adam Cohen8d769d62017-06-23 17:27:38 -070075
Adam Cohenb64cb5a2011-02-15 13:53:42 -080076 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080077 // The page is moved more than halfway, automatically move to the next page on touch up.
78 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080079
Sunny Goyal8e2133b2015-05-06 13:39:07 -070080 private static final float MAX_SCROLL_PROGRESS = 1.0f;
81
Adam Cohen265b9a62011-12-07 14:37:18 -080082 // The following constants need to be scaled based on density. The scaled versions will be
83 // assigned to the corresponding member variables below.
84 private static final int FLING_THRESHOLD_VELOCITY = 500;
85 private static final int MIN_SNAP_VELOCITY = 1500;
86 private static final int MIN_FLING_VELOCITY = 250;
87
Adam Cohen21cd0022013-10-09 18:57:02 -070088 public static final int INVALID_RESTORE_PAGE = -1001;
89
Adam Cohenf358a4b2013-07-23 16:47:31 -070090 private boolean mFreeScroll = false;
Tony Wickham1c9bf6a2017-11-17 10:25:27 -080091 private boolean mSettleOnPageInFreeScroll = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070092
Adam Cohen265b9a62011-12-07 14:37:18 -080093 protected int mFlingThresholdVelocity;
94 protected int mMinFlingVelocity;
95 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected boolean mFirstLayout = true;
98
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700101 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700102
Sunny Goyal4ffec482016-02-09 11:28:52 -0800103 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800105 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800106 protected LauncherScroller mScroller;
107 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private VelocityTracker mVelocityTracker;
Sunny Goyale15e2a82017-12-15 13:05:42 -0800109 protected int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Adam Cohen7d30a372013-07-01 17:03:59 -0700111 private float mParentDownMotionX;
112 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700114 private float mDownMotionY;
115 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700116 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700117 private float mLastMotionX;
118 private float mLastMotionXRemainder;
119 private float mLastMotionY;
120 private float mTotalMotionX;
Adam Cohenedb40762013-07-18 16:45:45 -0700121
Adam Cohendbdff6b2013-09-18 19:09:15 -0700122 private boolean mCancelTap;
123
Adam Cohenedb40762013-07-18 16:45:45 -0700124 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka0142d492010-08-25 17:46:15 -0700126 protected final static int TOUCH_STATE_REST = 0;
127 protected final static int TOUCH_STATE_SCROLLING = 1;
128 protected final static int TOUCH_STATE_PREV_PAGE = 2;
129 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700130 protected final static int TOUCH_STATE_REORDERING = 4;
131
Michael Jurka0142d492010-08-25 17:46:15 -0700132 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
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;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700145
Sunny Goyal061380a2016-02-29 15:15:03 -0800146 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700147
Adam Cohen8d769d62017-06-23 17:27:38 -0700148 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
149 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
150 // the screens from continuing to translate beyond the normal bounds.
151 protected int mOverScrollX;
152
153 protected int mUnboundedScrollX;
154
Winson Chungd2be3812013-07-16 11:11:32 -0700155 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mPageIndicatorViewId;
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700157 protected PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700158
159 // Reordering
160 // We use the min scale to determine how much to expand the actually PagedView measured
161 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700162 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700163 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700164 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
165
Sunny Goyalcf25b522015-07-09 00:01:18 -0700166 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 // This variable's scope is only for the duration of startReordering() and endReordering()
170 private boolean mReorderingStarted = false;
171 // This variable's scope is for the duration of startReordering() and after the zoomIn()
172 // animation after endReordering()
173 private boolean mIsReordering;
174 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700175 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700176 private int mPostReorderingPreZoomInRemainingAnimationCount;
177 private Runnable mPostReorderingPreZoomInRunnable;
178
Adam Cohen7d30a372013-07-01 17:03:59 -0700179 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700180 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700181 private static final float[] sTmpPoint = new float[2];
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700182 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700183
John Spurlock77e1f472013-09-11 10:09:51 -0400184 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700185 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400186
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 public PagedView(Context context) {
188 this(context, null);
189 }
190
191 public PagedView(Context context, AttributeSet attrs) {
192 this(context, attrs, 0);
193 }
194
195 public PagedView(Context context, AttributeSet attrs, int defStyle) {
196 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700197
Adam Cohen9c4949e2010-10-05 12:27:22 -0700198 TypedArray a = context.obtainStyledAttributes(attrs,
199 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700200 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 a.recycle();
202
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700204 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700205 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
208 /**
209 * Initializes various states for this workspace.
210 */
Michael Jurka0142d492010-08-25 17:46:15 -0700211 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800212 mScroller = new LauncherScroller(getContext());
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700213 setDefaultInterpolator(Interpolators.SCROLL);
Winson Chung86f77532010-08-24 11:08:22 -0700214 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700215
216 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700217 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800219
Sunny Goyalcf25b522015-07-09 00:01:18 -0700220 float density = getResources().getDisplayMetrics().density;
221 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
222 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
223 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700224 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700225 setWillNotDraw(false);
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
Adam Cohenf9618852013-11-08 06:45:03 -0800228 protected void setDefaultInterpolator(Interpolator interpolator) {
229 mDefaultInterpolator = interpolator;
230 mScroller.setInterpolator(mDefaultInterpolator);
231 }
232
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700233 public void initParentViews(View parent) {
234 if (mPageIndicatorViewId > -1) {
235 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700236 mPageIndicator.setMarkersCount(getChildCount());
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700237 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700238 }
239 }
240
Adam Cohen7d30a372013-07-01 17:03:59 -0700241 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700242 private float[] mapPointFromViewToParent(View v, float x, float y) {
243 sTmpPoint[0] = x;
244 sTmpPoint[1] = y;
245 v.getMatrix().mapPoints(sTmpPoint);
246 sTmpPoint[0] += v.getLeft();
247 sTmpPoint[1] += v.getTop();
248 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700249 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700250 private float[] mapPointFromParentToView(View v, float x, float y) {
251 sTmpPoint[0] = x - v.getLeft();
252 sTmpPoint[1] = y - v.getTop();
253 v.getMatrix().invert(sTmpInvMatrix);
254 sTmpInvMatrix.mapPoints(sTmpPoint);
255 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700256 }
257
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700258 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700259 if (mDragView != null) {
260 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
261 (mDragViewBaselineLeft - mDragView.getLeft());
262 float y = mLastMotionY - mDownMotionY;
263 mDragView.setTranslationX(x);
264 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700265
Adam Cohenf358a4b2013-07-23 16:47:31 -0700266 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
267 + x + ", " + y);
268 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700269 }
270
Adam Cohen7d30a372013-07-01 17:03:59 -0700271 @Override
272 public void setScaleX(float scaleX) {
273 super.setScaleX(scaleX);
274 if (isReordering(true)) {
275 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
276 mLastMotionX = p[0];
277 mLastMotionY = p[1];
278 updateDragViewTranslationDuringDrag();
279 }
280 }
281
Hyunyoung Song77441692016-06-27 22:00:48 -0700282 public PageIndicator getPageIndicator() {
Adam Cohenedb40762013-07-18 16:45:45 -0700283 return mPageIndicator;
284 }
285
Adam Cohen674531f2013-12-13 15:07:14 -0800286 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700287 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
288 * that the user was on before entering free scroll mode (e.g. the home screen page they
289 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
290 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700292 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700293 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700295
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700296 /**
297 * Returns the index of page to be shown immediately afterwards.
298 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700299 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700300 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
301 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700302
Adam Cohenf9c184a2016-01-15 16:47:43 -0800303 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 return getChildCount();
305 }
306
Sunny Goyal83a8f042015-05-19 12:52:12 -0700307 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 return getChildAt(index);
309 }
310
Adam Cohenae4f1552011-10-20 00:15:42 -0700311 protected int indexToPage(int index) {
312 return index;
313 }
314
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800316 * Updates the scroll of the current page immediately to its final scroll position. We use this
317 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
318 * the previous tab page.
319 */
320 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700321 // If the current page is invalid, just reset the scroll position to zero
322 int newX = 0;
323 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700324 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700325 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800326 scrollTo(newX, 0);
327 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700328 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800329 }
330
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700331 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700332 mScroller.abortAnimation();
333 // We need to clean up the next page here to avoid computeScrollHelper from
334 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700335 if (resetNextPage) {
336 mNextPage = INVALID_PAGE;
337 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700338 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700339
Tony Wickham8f7ead32016-04-07 18:46:44 -0700340 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700341 mScroller.forceFinished(true);
342 // We need to clean up the next page here to avoid computeScrollHelper from
343 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700344 if (resetNextPage) {
345 mNextPage = INVALID_PAGE;
346 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700347 }
348
Adam Cohen6f127a62014-06-12 14:54:41 -0700349 private int validateNewPage(int newPage) {
Adam Cohen6f127a62014-06-12 14:54:41 -0700350 // Ensure that it is clamped by the actual set of children in all cases
Sunny Goyal4d519f22017-10-24 10:32:40 -0700351 return Utilities.boundToRange(newPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700352 }
353
Chet Haasebc2f0822012-10-26 17:59:53 -0700354 /**
Winson Chung86f77532010-08-24 11:08:22 -0700355 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700357 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800358 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700359 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800360 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800361 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
362 // the default
363 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800364 return;
365 }
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700366 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700367 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700368 updateCurrentPageScroll();
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700369 notifyPageSwitchListener(prevPage);
Winson Chunga12a2502010-12-20 14:41:35 -0800370 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372
Winson Chung8c87cd82013-07-23 16:20:10 -0700373 /**
Adam Cohen674531f2013-12-13 15:07:14 -0800374 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
375 * has settled.
376 */
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700377 protected void notifyPageSwitchListener(int prevPage) {
Adam Cohen674531f2013-12-13 15:07:14 -0800378 updatePageIndicator();
379 }
380
381 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700382 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700383 if (mPageIndicator != null) {
Sunny Goyal53fe1f22016-07-08 08:47:07 -0700384 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700385 if (!isReordering(false)) {
386 mPageIndicator.setActiveMarker(getNextPage());
387 }
Winson Chungd2be3812013-07-16 11:11:32 -0700388 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700390 protected void pageBeginTransition() {
391 if (!mIsPageInTransition) {
392 mIsPageInTransition = true;
393 onPageBeginTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700394 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700395 }
396
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700397 protected void pageEndTransition() {
398 if (mIsPageInTransition) {
399 mIsPageInTransition = false;
400 onPageEndTransition();
Michael Jurkad74c9842011-07-10 12:44:21 -0700401 }
Michael Jurka0142d492010-08-25 17:46:15 -0700402 }
403
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700404 protected boolean isPageInTransition() {
405 return mIsPageInTransition;
Adam Cohen26976d92011-03-22 15:33:33 -0700406 }
407
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700408 /**
409 * Called when the page starts moving as part of the scroll. Subclasses can override this
410 * to provide custom behavior during animation.
411 */
412 protected void onPageBeginTransition() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700413 }
414
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700415 /**
416 * Called when the page ends moving as part of the scroll. Subclasses can override this
417 * to provide custom behavior during animation.
418 */
419 protected void onPageEndTransition() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700420 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700421 }
422
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 /**
Winson Chung86f77532010-08-24 11:08:22 -0700424 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700425 *
426 * @param l The listener used to respond to long clicks.
427 */
428 @Override
429 public void setOnLongClickListener(OnLongClickListener l) {
430 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700431 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700432 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700433 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
Adam Cohen1697b792013-09-17 19:08:21 -0700435 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 }
437
Sunny Goyalc86df472016-02-25 09:19:38 -0800438 protected int getUnboundedScrollX() {
Adam Cohen8d769d62017-06-23 17:27:38 -0700439 return mUnboundedScrollX;
Sunny Goyalc86df472016-02-25 09:19:38 -0800440 }
441
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800443 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800444 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800445 }
446
447 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700448 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700449 // In free scroll mode, we clamp the scrollX
450 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700451 // If the scroller is trying to move to a location beyond the maximum allowed
452 // in the free scroll mode, we make sure to end the scroll operation.
Sunny Goyal4d519f22017-10-24 10:32:40 -0700453 if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700454 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700455 }
456
Sunny Goyal4d519f22017-10-24 10:32:40 -0700457 x = Utilities.boundToRange(x, 0, mMaxScrollX);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700458 }
459
Adam Cohen8d769d62017-06-23 17:27:38 -0700460 mUnboundedScrollX = x;
461
Sunny Goyal70660032015-05-14 00:07:08 -0700462 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
463 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700464 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700465 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800466 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700467 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700468 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700469 overScroll(x - mMaxScrollX);
470 } else {
471 overScroll(x);
472 }
Adam Cohen68d73932010-11-15 10:50:58 -0800473 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700474 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700475 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800476 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700477 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700478 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700479 overScroll(x);
480 } else {
481 overScroll(x - mMaxScrollX);
482 }
Adam Cohen68d73932010-11-15 10:50:58 -0800483 }
484 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700485 if (mWasInOverscroll) {
486 overScroll(0);
487 mWasInOverscroll = false;
488 }
Adam Cohen8d769d62017-06-23 17:27:38 -0700489 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800490 super.scrollTo(x, y);
491 }
492
Adam Cohen7d30a372013-07-01 17:03:59 -0700493 // Update the last motion events when scrolling
494 if (isReordering(true)) {
495 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
496 mLastMotionX = p[0];
497 mLastMotionY = p[1];
498 updateDragViewTranslationDuringDrag();
499 }
Michael Jurka0142d492010-08-25 17:46:15 -0700500 }
501
Adam Cohen53805212013-10-01 10:39:23 -0700502 private void sendScrollAccessibilityEvent() {
Sunny Goyald0030b02017-12-08 15:07:24 -0800503 if (isObservedEventType(getContext(), AccessibilityEvent.TYPE_VIEW_SCROLLED)) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700504 if (mCurrentPage != getNextPage()) {
505 AccessibilityEvent ev =
506 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700507 ev.setScrollable(true);
508 ev.setScrollX(getScrollX());
509 ev.setScrollY(getScrollY());
510 ev.setMaxScrollX(mMaxScrollX);
511 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700512
Vadim Tryshevf4715972015-05-08 17:21:03 -0700513 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700514 }
Adam Cohen53805212013-10-01 10:39:23 -0700515 }
516 }
517
Michael Jurka0142d492010-08-25 17:46:15 -0700518 // we moved this functionality to a helper function so SmoothPagedView can reuse it
519 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800520 return computeScrollHelper(true);
521 }
522
523 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700525 // Don't bother scrolling if the page does not need to be moved
Sunny Goyal76dbf6f2017-01-03 14:55:47 -0800526 if (getUnboundedScrollX() != mScroller.getCurrX()
Adam Cohen8d769d62017-06-23 17:27:38 -0700527 || getScrollY() != mScroller.getCurrY()
528 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700529 float scaleX = mFreeScroll ? getScaleX() : 1f;
530 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
531 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700532 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800533 if (shouldInvalidate) {
534 invalidate();
535 }
Michael Jurka0142d492010-08-25 17:46:15 -0700536 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800537 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700538 sendScrollAccessibilityEvent();
539
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700540 int prevPage = mCurrentPage;
Adam Cohen6f127a62014-06-12 14:54:41 -0700541 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700542 mNextPage = INVALID_PAGE;
Hyunyoung Song3f5a11f2017-06-29 16:42:04 -0700543 notifyPageSwitchListener(prevPage);
Winson Chungb44b5242011-06-13 11:32:14 -0700544
Adam Cohen73aa9752010-11-24 16:26:58 -0800545 // We don't want to trigger a page end moving unless the page has settled
546 // and the user has stopped scrolling
547 if (mTouchState == TOUCH_STATE_REST) {
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700548 pageEndTransition();
Adam Cohen73aa9752010-11-24 16:26:58 -0800549 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700550
Adam Cohen7d30a372013-07-01 17:03:59 -0700551 onPostReorderingAnimationCompleted();
Sunny Goyald0030b02017-12-08 15:07:24 -0800552 if (isAccessibilityEnabled(getContext())) {
Adam Cohen53805212013-10-01 10:39:23 -0700553 // Notify the user when the page changes
554 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700555 }
Michael Jurka0142d492010-08-25 17:46:15 -0700556 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
Michael Jurka0142d492010-08-25 17:46:15 -0700558 return false;
559 }
560
561 @Override
562 public void computeScroll() {
563 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
565
Sunny Goyalce8809a2018-01-18 14:02:47 -0800566 public int getExpectedHeight() {
567 return getMeasuredHeight();
568 }
569
Adam Cohen410f3cd2013-09-22 12:09:32 -0700570 public int getNormalChildHeight() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800571 return getExpectedHeight() - getPaddingTop() - getPaddingBottom()
Sunny Goyalac00cba2017-11-13 15:58:01 -0800572 - mInsets.top - mInsets.bottom;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700573 }
574
Sunny Goyalce8809a2018-01-18 14:02:47 -0800575 public int getExpectedWidth() {
576 return getMeasuredWidth();
577 }
578
Sunny Goyalbc683e92017-12-06 10:25:07 -0800579 public int getNormalChildWidth() {
Sunny Goyalce8809a2018-01-18 14:02:47 -0800580 return getExpectedWidth() - getPaddingLeft() - getPaddingRight()
Sunny Goyalbc683e92017-12-06 10:25:07 -0800581 - mInsets.left - mInsets.right;
582 }
583
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 @Override
585 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700586 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700587 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
588 return;
589 }
590
Adam Cohen7d30a372013-07-01 17:03:59 -0700591 // We measure the dimensions of the PagedView to be larger than the pages so that when we
592 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700593 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
594 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
595 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700596 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700597
Adam Cohen7d30a372013-07-01 17:03:59 -0700598 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
599 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
600 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 }
602
Winson Chung8aad6102012-05-11 16:27:49 -0700603 // Return early if we aren't given a proper dimension
604 if (widthSize <= 0 || heightSize <= 0) {
605 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
606 return;
607 }
608
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700610 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700611 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700612
Sunny Goyalac00cba2017-11-13 15:58:01 -0800613 int myWidthSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800614 widthSize - mInsets.left - mInsets.right, MeasureSpec.EXACTLY);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800615 int myHeightSpec = MeasureSpec.makeMeasureSpec(
Sunny Goyal228153d2018-01-04 15:35:22 -0800616 heightSize - mInsets.top - mInsets.bottom, MeasureSpec.EXACTLY);
Adam Cohen96d30a12013-07-16 18:13:21 -0700617
Sunny Goyalac00cba2017-11-13 15:58:01 -0800618 // measureChildren takes accounts for content padding, we only need to care about extra
619 // space due to insets.
620 measureChildren(myWidthSpec, myHeightSpec);
621 setMeasuredDimension(widthSize, heightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800622 }
623
Sunny Goyalcf25b522015-07-09 00:01:18 -0700624 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700625 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700626 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700627 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700628 return;
629 }
630
Winson Chung785d2eb2011-04-14 16:08:02 -0700631 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700632 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700633
Sunny Goyal70660032015-05-14 00:07:08 -0700634 final int startIndex = mIsRtl ? childCount - 1 : 0;
635 final int endIndex = mIsRtl ? -1 : childCount;
636 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700637
Adam Cohen7d30a372013-07-01 17:03:59 -0700638 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700639
Sunny Goyal228153d2018-01-04 15:35:22 -0800640 int scrollOffsetLeft = mInsets.left + getPaddingLeft();
641 int childLeft = scrollOffsetLeft;
642
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700643 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
644 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700645 }
646
Adam Cohen0ffac432013-07-10 11:19:26 -0700647 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700648 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 if (child.getVisibility() != View.GONE) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800650 int childTop = getPaddingTop() + mInsets.top;
651 childTop += (getMeasuredHeight() - mInsets.top - mInsets.bottom - verticalPadding
Sunny Goyalac00cba2017-11-13 15:58:01 -0800652 - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100653
Adam Cohen96d30a12013-07-16 18:13:21 -0700654 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700655 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800656
Winson Chung785d2eb2011-04-14 16:08:02 -0700657 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700658 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800659 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700660
Sunny Goyal228153d2018-01-04 15:35:22 -0800661 mPageScrolls[i] = childLeft - scrollOffsetLeft;
Adam Cohen84a465a2013-11-11 18:49:56 +0000662
Sunny Goyalac00cba2017-11-13 15:58:01 -0800663 childLeft += childWidth + mPageSpacing + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 }
665 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700666
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700667 final LayoutTransition transition = getLayoutTransition();
668 // If the transition is running defer updating max scroll, as some empty pages could
669 // still be present, and a max scroll change could cause sudden jumps in scroll.
670 if (transition != null && transition.isRunning()) {
671 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
672
673 @Override
674 public void startTransition(LayoutTransition transition, ViewGroup container,
675 View view, int transitionType) { }
676
677 @Override
678 public void endTransition(LayoutTransition transition, ViewGroup container,
679 View view, int transitionType) {
680 // Wait until all transitions are complete.
681 if (!transition.isRunning()) {
682 transition.removeTransitionListener(this);
683 updateMaxScrollX();
684 }
685 }
686 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700687 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700688 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700689 }
690
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700691 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
692 updateCurrentPageScroll();
693 mFirstLayout = false;
694 }
695
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700696 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700697 setCurrentPage(getNextPage());
Adam Cohenf698c6e2013-07-17 18:44:25 -0700698 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700699 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700700
701 if (isReordering(true)) {
702 updateDragViewTranslationDuringDrag();
703 }
Winson Chunge3193b92010-09-10 11:44:42 -0700704 }
705
Sunny Goyala1fbd842015-05-20 13:40:27 -0700706 protected int getChildGap() {
707 return 0;
708 }
709
Sunny Goyal4d519f22017-10-24 10:32:40 -0700710 private void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700711 mMaxScrollX = computeMaxScrollX();
712 }
713
714 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700715 int childCount = getChildCount();
716 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700717 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700718 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700719 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700720 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700721 }
722 }
723
Adam Cohen3b185e22013-10-29 14:45:58 -0700724 public void setPageSpacing(int pageSpacing) {
725 mPageSpacing = pageSpacing;
726 requestLayout();
727 }
728
Winson Chunge3193b92010-09-10 11:44:42 -0700729 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700731 // Update the page indicator, we don't update the page indicator as we
732 // add/remove pages
733 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700734 mPageIndicator.addMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700735 }
736
Adam Cohen2591f6a2011-10-25 14:36:40 -0700737 // This ensures that when children are added, they get the correct transforms / alphas
738 // in accordance with any scroll effects.
Adam Cohen2591f6a2011-10-25 14:36:40 -0700739 invalidate();
740 }
741
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 @Override
743 public void onChildViewRemoved(View parent, View child) {
Tonya361c722017-07-04 09:43:06 -0700744 mCurrentPage = validateNewPage(mCurrentPage);
Adam Cohen7d30a372013-07-01 17:03:59 -0700745 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 }
747
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700748 private void removeMarkerForView() {
Winson Chungd2be3812013-07-16 11:11:32 -0700749 // Update the page indicator, we don't update the page indicator as we
750 // add/remove pages
751 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700752 mPageIndicator.removeMarker();
Winson Chungd2be3812013-07-16 11:11:32 -0700753 }
754 }
755
756 @Override
757 public void removeView(View v) {
758 // XXX: We should find a better way to hook into this before the view
759 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700760 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700761 super.removeView(v);
762 }
763 @Override
764 public void removeViewInLayout(View v) {
765 // XXX: We should find a better way to hook into this before the view
766 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700767 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700768 super.removeViewInLayout(v);
769 }
770 @Override
771 public void removeViewAt(int index) {
772 // XXX: We should find a better way to hook into this before the view
773 // gets removed form its parent...
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700774 removeMarkerForView();
Winson Chungd2be3812013-07-16 11:11:32 -0700775 super.removeViewAt(index);
776 }
777 @Override
778 public void removeAllViewsInLayout() {
779 // Update the page indicator, we don't update the page indicator as we
780 // add/remove pages
781 if (mPageIndicator != null) {
Sunny Goyalc64cfdd2016-05-18 14:12:02 -0700782 mPageIndicator.setMarkersCount(0);
Winson Chungd2be3812013-07-16 11:11:32 -0700783 }
784
785 super.removeAllViewsInLayout();
786 }
787
Adam Cohen73894962011-10-31 13:17:17 -0700788 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700789 if (index < 0 || index > getChildCount() - 1) return 0;
Sunny Goyal228153d2018-01-04 15:35:22 -0800790 return getPageAt(index).getLeft();
Adam Cohen73894962011-10-31 13:17:17 -0700791 }
792
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 @Override
794 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700795 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700796 if (page != mCurrentPage || !mScroller.isFinished()) {
797 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 return true;
799 }
800 return false;
801 }
802
803 @Override
804 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700805 int focusablePage;
806 if (mNextPage != INVALID_PAGE) {
807 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700809 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
Winson Chung86f77532010-08-24 11:08:22 -0700811 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700813 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 }
815 return false;
816 }
817
818 @Override
819 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800820 if (super.dispatchUnhandledMove(focused, direction)) {
821 return true;
822 }
823
824 if (mIsRtl) {
825 if (direction == View.FOCUS_LEFT) {
826 direction = View.FOCUS_RIGHT;
827 } else if (direction == View.FOCUS_RIGHT) {
828 direction = View.FOCUS_LEFT;
829 }
830 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700832 if (getCurrentPage() > 0) {
833 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 return true;
835 }
836 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700837 if (getCurrentPage() < getPageCount() - 1) {
838 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 return true;
840 }
841 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -0800842 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844
845 @Override
846 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Jon Miranda6a858172016-10-25 16:19:17 -0700847 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
848 return;
849 }
850
Adam Cohen0ffac432013-07-10 11:19:26 -0700851 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700852 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700853 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 }
855 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700856 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700857 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 }
859 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700860 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700861 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 }
863 }
864 }
865
866 /**
867 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700868 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 *
Winson Chung86f77532010-08-24 11:08:22 -0700870 * This happens when live folders requery, and if they're off page, they
871 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 */
873 @Override
874 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700875 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 View v = focused;
877 while (true) {
878 if (v == current) {
879 super.focusableViewAvailable(focused);
880 return;
881 }
882 if (v == this) {
883 return;
884 }
885 ViewParent parent = v.getParent();
886 if (parent instanceof View) {
887 v = (View)v.getParent();
888 } else {
889 return;
890 }
891 }
892 }
893
894 /**
895 * {@inheritDoc}
896 */
897 @Override
898 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
899 if (disallowIntercept) {
900 // We need to make sure to cancel our long press if
901 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700902 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700903 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 }
905 super.requestDisallowInterceptTouchEvent(disallowIntercept);
906 }
907
Adam Cohen7d30a372013-07-01 17:03:59 -0700908 /** Returns whether x and y originated within the buffered viewport */
909 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800910 sTmpRect.set(-getMeasuredWidth() / 2, 0, 3 * getMeasuredWidth() / 2, getMeasuredHeight());
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700911 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700912 }
913
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 @Override
915 public boolean onInterceptTouchEvent(MotionEvent ev) {
916 /*
917 * This method JUST determines whether we want to intercept the motion.
918 * If we return true, onTouchEvent will be called and we do the actual
919 * scrolling there.
920 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800921 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922
Winson Chung45e1d6e2010-11-09 17:19:49 -0800923 // Skip touch handling if there are no pages to swipe
924 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
925
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 /*
927 * Shortcut the most recurring case: the user is in the dragging
928 * state and he is moving his finger. We want to intercept this
929 * motion.
930 */
931 final int action = ev.getAction();
932 if ((action == MotionEvent.ACTION_MOVE) &&
933 (mTouchState == TOUCH_STATE_SCROLLING)) {
934 return true;
935 }
936
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 switch (action & MotionEvent.ACTION_MASK) {
938 case MotionEvent.ACTION_MOVE: {
939 /*
940 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
941 * whether the user has moved far enough from his original down touch.
942 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700943 if (mActivePointerId != INVALID_POINTER) {
944 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -0700945 }
946 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
947 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
948 // i.e. fall through to the next case (don't break)
949 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
950 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -0700951 break;
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 }
953
954 case MotionEvent.ACTION_DOWN: {
955 final float x = ev.getX();
956 final float y = ev.getY();
957 // Remember location of down touch
958 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -0700959 mDownMotionY = y;
960 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 mLastMotionX = x;
962 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -0700963 float[] p = mapPointFromViewToParent(this, x, y);
964 mParentDownMotionX = p[0];
965 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -0800966 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800967 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -0700969
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 /*
971 * If being flinged and user touches the screen, initiate drag;
972 * otherwise don't. mScroller.isFinished should be false when
973 * being flinged.
974 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700975 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +0000976 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -0800977
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700978 if (finishedScrolling) {
979 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -0800980 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +0000981 setCurrentPage(getNextPage());
Sunny Goyal9ccafbf2016-10-26 13:06:08 -0700982 pageEndTransition();
Adam Cohen2da0a052013-11-08 06:28:17 -0800983 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700984 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -0700985 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
986 mTouchState = TOUCH_STATE_SCROLLING;
987 } else {
988 mTouchState = TOUCH_STATE_REST;
989 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700990 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 break;
993 }
994
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800996 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -0700997 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 break;
999
1000 case MotionEvent.ACTION_POINTER_UP:
1001 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001002 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 break;
1004 }
1005
1006 /*
1007 * The only time we want to intercept motion events is if we are in the
1008 * drag mode.
1009 */
1010 return mTouchState != TOUCH_STATE_REST;
1011 }
1012
Adam Cohenf8d28232011-02-01 21:47:00 -08001013 protected void determineScrollingStart(MotionEvent ev) {
1014 determineScrollingStart(ev, 1.0f);
1015 }
1016
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 /*
1018 * Determines if we should change the touch state to start scrolling after the
1019 * user moves their touch point too far.
1020 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001021 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001022 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001024 if (pointerIndex == -1) return;
1025
1026 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 final float x = ev.getX(pointerIndex);
1028 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001029 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1030
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001032
Adam Cohenf8d28232011-02-01 21:47:00 -08001033 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001035
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001036 if (xMoved) {
1037 // Scroll if the user moved far enough along the X axis
1038 mTouchState = TOUCH_STATE_SCROLLING;
1039 mTotalMotionX += Math.abs(mLastMotionX - x);
1040 mLastMotionX = x;
1041 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001042 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001043 pageBeginTransition();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001044 // Stop listening for things like pinches.
1045 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001046 }
1047 }
1048
1049 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001050 // Try canceling the long press. It could also have been scheduled
1051 // by a distant descendant, so use the mAllowLongPress flag to block
1052 // everything
1053 final View currentPage = getPageAt(mCurrentPage);
1054 if (currentPage != null) {
1055 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 }
1057 }
1058
Adam Cohenb5ba0972011-09-07 18:02:31 -07001059 protected float getScrollProgress(int screenCenter, View v, int page) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001060 final int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001061
Adam Cohenedb40762013-07-18 16:45:45 -07001062 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001063 int count = getChildCount();
1064
1065 final int totalDistance;
1066
1067 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001068 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001069 adjacentPage = page - 1;
1070 }
1071
1072 if (adjacentPage < 0 || adjacentPage > count - 1) {
1073 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1074 } else {
1075 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1076 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001077
1078 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001079 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1080 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001081 return scrollProgress;
1082 }
1083
Adam Cohenedb40762013-07-18 16:45:45 -07001084 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001085 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001086 return 0;
1087 } else {
1088 return mPageScrolls[index];
1089 }
1090 }
1091
Adam Cohen564a2e72013-10-09 14:47:32 -07001092 // While layout transitions are occurring, a child's position may stray from its baseline
1093 // position. This method returns the magnitude of this stray at any given time.
1094 public int getLayoutTransitionOffsetForPage(int index) {
1095 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1096 return 0;
1097 } else {
1098 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001099
Sunny Goyal228153d2018-01-04 15:35:22 -08001100 int scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
1101 int baselineX = mPageScrolls[index] + scrollOffset;
Adam Cohen564a2e72013-10-09 14:47:32 -07001102 return (int) (child.getX() - baselineX);
1103 }
1104 }
1105
Adam Cohenb5ba0972011-09-07 18:02:31 -07001106 protected void dampedOverScroll(float amount) {
Sunny Goyalb72d8b22017-07-14 00:02:27 -07001107 if (Float.compare(amount, 0f) == 0) return;
Adam Cohen8d769d62017-06-23 17:27:38 -07001108
Sunny Goyal228153d2018-01-04 15:35:22 -08001109 int overScrollAmount = OverScroll.dampedScroll(amount, getMeasuredWidth());
Adam Cohen8d769d62017-06-23 17:27:38 -07001110 if (amount < 0) {
1111 mOverScrollX = overScrollAmount;
1112 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001113 } else {
Adam Cohen8d769d62017-06-23 17:27:38 -07001114 mOverScrollX = mMaxScrollX + overScrollAmount;
1115 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001116 }
1117 invalidate();
1118 }
1119
Adam Cohenb5ba0972011-09-07 18:02:31 -07001120 protected void overScroll(float amount) {
1121 dampedOverScroll(amount);
1122 }
1123
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001124 /**
1125 * return true if freescroll has been enabled, false otherwise
1126 */
Sunny Goyalc99cb172017-10-19 16:15:09 -07001127 protected void enableFreeScroll() {
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001128 enableFreeScroll(false);
1129 }
1130
1131 protected void enableFreeScroll(boolean settleOnPageInFreeScroll) {
Adam Cohenf9618852013-11-08 06:45:03 -08001132 setEnableFreeScroll(true);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001133 mSettleOnPageInFreeScroll = settleOnPageInFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001134 }
1135
Sunny Goyalc99cb172017-10-19 16:15:09 -07001136 protected void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001137 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138 }
1139
Adam Cohenf9618852013-11-08 06:45:03 -08001140 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001141 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001142 mFreeScroll = freeScroll;
1143
Adam Cohenf9618852013-11-08 06:45:03 -08001144 if (mFreeScroll) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001145 setCurrentPage(getNextPage());
Tony Wickham8f7ead32016-04-07 18:46:44 -07001146 } else if (wasFreeScroll) {
1147 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001148 }
1149
1150 setEnableOverscroll(!freeScroll);
1151 }
1152
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001153 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001154 mAllowOverScroll = enable;
1155 }
1156
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001157 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001158 if (mDragView != null) {
1159 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1160 + mDragView.getTranslationX());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001161 int minDistance = Integer.MAX_VALUE;
1162 int minIndex = indexOfChild(mDragView);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001163 int maxPageNo = getChildCount() - 1;
1164 for (int i = 0; i <= maxPageNo; i++) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001165 View page = getPageAt(i);
Sunny Goyal4d519f22017-10-24 10:32:40 -07001166 int pageX = (page.getLeft() + page.getMeasuredWidth() / 2);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001167 int d = Math.abs(dragX - pageX);
1168 if (d < minDistance) {
1169 minIndex = i;
1170 minDistance = d;
1171 }
1172 }
1173 return minIndex;
1174 }
1175 return -1;
1176 }
1177
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 @Override
1179 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001180 super.onTouchEvent(ev);
1181
Winson Chung45e1d6e2010-11-09 17:19:49 -08001182 // Skip touch handling if there are no pages to swipe
1183 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1184
Michael Jurkab8f06722010-10-10 15:58:46 -07001185 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001186
1187 final int action = ev.getAction();
1188
1189 switch (action & MotionEvent.ACTION_MASK) {
1190 case MotionEvent.ACTION_DOWN:
1191 /*
1192 * If being flinged and user touches, stop the fling. isFinished
1193 * will be false if being flinged.
1194 */
1195 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001196 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
1198
1199 // Remember where the motion event started
1200 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001201 mDownMotionY = mLastMotionY = ev.getY();
1202 mDownScrollX = getScrollX();
1203 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1204 mParentDownMotionX = p[0];
1205 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001206 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001207 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001209
Michael Jurka0142d492010-08-25 17:46:15 -07001210 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001211 onScrollInteractionBegin();
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001212 pageBeginTransition();
Michael Jurka0142d492010-08-25 17:46:15 -07001213 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 break;
1215
1216 case MotionEvent.ACTION_MOVE:
1217 if (mTouchState == TOUCH_STATE_SCROLLING) {
1218 // Scroll to follow the motion event
1219 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001220
1221 if (pointerIndex == -1) return true;
1222
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001224 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001225
Adam Cohenaefd4e12011-02-14 16:39:38 -08001226 mTotalMotionX += Math.abs(deltaX);
1227
Winson Chungc0844aa2011-02-02 15:25:58 -08001228 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1229 // keep the remainder because we are actually testing if we've moved from the last
1230 // scrolled position (which is discrete).
1231 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001232 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001233 mLastMotionX = x;
1234 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 } else {
1236 awakenScrollBars();
1237 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001238 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1239 // Update the last motion position
1240 mLastMotionX = ev.getX();
1241 mLastMotionY = ev.getY();
1242
1243 // Update the parent down so that our zoom animations take this new movement into
1244 // account
1245 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1246 mParentDownMotionX = pt[0];
1247 mParentDownMotionY = pt[1];
1248 updateDragViewTranslationDuringDrag();
1249
1250 // Find the closest page to the touch point
1251 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001252
Adam Cohen7d30a372013-07-01 17:03:59 -07001253 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1254 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1255 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1256 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1257
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001259 // Do not allow any page to be moved to 0th position.
1260 if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) {
Sunny Goyal4d519f22017-10-24 10:32:40 -07001261 if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 &&
Adam Cohen7d30a372013-07-01 17:03:59 -07001262 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1263 mSidePageHoverIndex = pageUnderPointIndex;
1264 mSidePageHoverRunnable = new Runnable() {
1265 @Override
1266 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001267 // Setup the scroll to the correct page before we swap the views
1268 snapToPage(pageUnderPointIndex);
1269
1270 // For each of the pages between the paged view and the drag view,
1271 // animate them from the previous position to the new position in
1272 // the layout (as a result of the drag view moving in the layout)
1273 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1274 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1275 dragViewIndex + 1 : pageUnderPointIndex;
1276 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1277 dragViewIndex - 1 : pageUnderPointIndex;
1278 for (int i = lowerIndex; i <= upperIndex; ++i) {
1279 View v = getChildAt(i);
1280 // dragViewIndex < pageUnderPointIndex, so after we remove the
1281 // drag view all subsequent views to pageUnderPointIndex will
1282 // shift down.
Sunny Goyal228153d2018-01-04 15:35:22 -08001283 int oldX = getChildOffset(i);
1284 int newX = getChildOffset(i + shiftDelta);
Adam Cohen7d30a372013-07-01 17:03:59 -07001285
1286 // Animate the view translation from its old position to its new
1287 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001288 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001289 if (anim != null) {
1290 anim.cancel();
1291 }
1292
1293 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001294 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001295 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001296 anim.start();
1297 v.setTag(anim);
1298 }
1299
1300 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001302 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001303 if (mPageIndicator != null) {
1304 mPageIndicator.setActiveMarker(getNextPage());
1305 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001306 }
1307 };
1308 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1309 }
1310 } else {
1311 removeCallbacks(mSidePageHoverRunnable);
1312 mSidePageHoverIndex = -1;
1313 }
Adam Cohen564976a2010-10-13 18:52:07 -07001314 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 determineScrollingStart(ev);
1316 }
1317 break;
1318
1319 case MotionEvent.ACTION_UP:
1320 if (mTouchState == TOUCH_STATE_SCROLLING) {
1321 final int activePointerId = mActivePointerId;
1322 final int pointerIndex = ev.findPointerIndex(activePointerId);
1323 final float x = ev.getX(pointerIndex);
1324 final VelocityTracker velocityTracker = mVelocityTracker;
1325 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1326 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001327 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001328 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001329 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1330 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001331
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001332 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1333
Adam Cohen00481b32011-11-18 12:03:48 -08001334 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001335 shouldFlingForVelocity(velocityX);
Adam Cohen00481b32011-11-18 12:03:48 -08001336
Adam Cohenf358a4b2013-07-23 16:47:31 -07001337 if (!mFreeScroll) {
1338 // In the case that the page is moved far to one direction and then is flung
1339 // in the opposite direction, we use a threshold to determine whether we should
1340 // just return to the starting page, or if we should skip one further.
1341 boolean returnToOriginalPage = false;
1342 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1343 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1344 returnToOriginalPage = true;
1345 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001346
Adam Cohenf358a4b2013-07-23 16:47:31 -07001347 int finalPage;
1348 // We give flings precedence over large moves, which is why we short-circuit our
1349 // test for a large move if a fling has been registered. That is, a large
1350 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001351 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1352 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001353 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1354 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1355 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1356 snapToPageWithVelocity(finalPage, velocityX);
1357 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1358 (isFling && isVelocityXLeft)) &&
1359 mCurrentPage < getChildCount() - 1) {
1360 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1361 snapToPageWithVelocity(finalPage, velocityX);
1362 } else {
1363 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001364 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001367 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001368 }
1369
1370 float scaleX = getScaleX();
1371 int vX = (int) (-velocityX * scaleX);
1372 int initialScrollX = (int) (getScrollX() * scaleX);
1373
Adam Cohenf9618852013-11-08 06:45:03 -08001374 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375 mScroller.fling(initialScrollX,
1376 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham1c9bf6a2017-11-17 10:25:27 -08001377 int unscaledScrollX = (int) (mScroller.getFinalX() / scaleX);
1378 mNextPage = getPageNearestToCenterOfScreen(unscaledScrollX);
1379 int firstPageScroll = getScrollForPage(!mIsRtl ? 0 : getPageCount() - 1);
1380 int lastPageScroll = getScrollForPage(!mIsRtl ? getPageCount() - 1 : 0);
1381 if (mSettleOnPageInFreeScroll && unscaledScrollX > firstPageScroll
1382 && unscaledScrollX < lastPageScroll) {
1383 // Make sure we land directly on a page. If flinging past one of the ends,
1384 // don't change the velocity as it will get stopped at the end anyway.
1385 mScroller.setFinalX((int) (getScrollForPage(mNextPage) * getScaleX()));
1386 // Ensure the scroll/snap doesn't happen too fast;
1387 int extraScrollDuration = OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION
1388 - mScroller.getDuration();
1389 if (extraScrollDuration > 0) {
1390 mScroller.extendDuration(extraScrollDuration);
1391 }
1392 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001395 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001396 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1397 // at this point we have not moved beyond the touch slop
1398 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1399 // we can just page
1400 int nextPage = Math.max(0, mCurrentPage - 1);
1401 if (nextPage != mCurrentPage) {
1402 snapToPage(nextPage);
1403 } else {
1404 snapToDestination();
1405 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001406 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 // at this point we have not moved beyond the touch slop
1408 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1409 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001410 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1411 if (nextPage != mCurrentPage) {
1412 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 } else {
1414 snapToDestination();
1415 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001416 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1417 // Update the last motion position
1418 mLastMotionX = ev.getX();
1419 mLastMotionY = ev.getY();
1420
1421 // Update the parent down so that our zoom animations take this new movement into
1422 // account
1423 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1424 mParentDownMotionX = pt[0];
1425 mParentDownMotionY = pt[1];
1426 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001427 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001428 if (!mCancelTap) {
1429 onUnhandledTap(ev);
1430 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001432
1433 // Remove the callback to wait for the side page hover timeout
1434 removeCallbacks(mSidePageHoverRunnable);
1435 // End any intermediate reordering states
1436 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 break;
1438
1439 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001440 if (mTouchState == TOUCH_STATE_SCROLLING) {
1441 snapToDestination();
Sunny Goyal4ff424a2016-08-12 12:46:01 -07001442 onScrollInteractionEnd();
Michael Jurkab8f06722010-10-10 15:58:46 -07001443 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 break;
1446
1447 case MotionEvent.ACTION_POINTER_UP:
1448 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001449 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 break;
1451 }
1452
1453 return true;
1454 }
1455
Sunny Goyal65d9ceb2017-05-08 09:17:42 -07001456 protected boolean shouldFlingForVelocity(int velocityX) {
1457 return Math.abs(velocityX) > mFlingThresholdVelocity;
1458 }
1459
Adam Cohen7d30a372013-07-01 17:03:59 -07001460 private void resetTouchState() {
1461 releaseVelocityTracker();
1462 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001463 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 mTouchState = TOUCH_STATE_REST;
1465 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001466 }
1467
Adam Cohenc2d6e892014-10-16 09:49:24 -07001468 /**
1469 * Triggered by scrolling via touch
1470 */
1471 protected void onScrollInteractionBegin() {
1472 }
1473
1474 protected void onScrollInteractionEnd() {
1475 }
1476
Adam Cohen1697b792013-09-17 19:08:21 -07001477 protected void onUnhandledTap(MotionEvent ev) {
Tony2fd02082016-10-07 12:50:01 -07001478 Launcher.getLauncher(getContext()).onClick(this);
Adam Cohen1697b792013-09-17 19:08:21 -07001479 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001480
Winson Chung185d7162011-02-28 13:47:29 -08001481 @Override
1482 public boolean onGenericMotionEvent(MotionEvent event) {
1483 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1484 switch (event.getAction()) {
1485 case MotionEvent.ACTION_SCROLL: {
1486 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1487 final float vscroll;
1488 final float hscroll;
1489 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1490 vscroll = 0;
1491 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1492 } else {
1493 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1494 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1495 }
1496 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001497 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001498 : (hscroll > 0 || vscroll > 0);
1499 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001500 scrollRight();
1501 } else {
1502 scrollLeft();
1503 }
1504 return true;
1505 }
1506 }
1507 }
1508 }
1509 return super.onGenericMotionEvent(event);
1510 }
1511
Michael Jurkab8f06722010-10-10 15:58:46 -07001512 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1513 if (mVelocityTracker == null) {
1514 mVelocityTracker = VelocityTracker.obtain();
1515 }
1516 mVelocityTracker.addMovement(ev);
1517 }
1518
1519 private void releaseVelocityTracker() {
1520 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001521 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001522 mVelocityTracker.recycle();
1523 mVelocityTracker = null;
1524 }
1525 }
1526
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 private void onSecondaryPointerUp(MotionEvent ev) {
1528 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1529 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1530 final int pointerId = ev.getPointerId(pointerIndex);
1531 if (pointerId == mActivePointerId) {
1532 // This was our active pointer going up. Choose a new
1533 // active pointer and adjust accordingly.
1534 // TODO: Make this decision more intelligent.
1535 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1536 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1537 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001538 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 mActivePointerId = ev.getPointerId(newPointerIndex);
1540 if (mVelocityTracker != null) {
1541 mVelocityTracker.clear();
1542 }
1543 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001544 }
1545
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 @Override
1547 public void requestChildFocus(View child, View focused) {
1548 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001549 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001550 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001551 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 }
1553 }
1554
Sunny Goyal228153d2018-01-04 15:35:22 -08001555 public int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001556 return getPageNearestToCenterOfScreen(getScrollX());
1557 }
1558
1559 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001560 int screenCenter = scaledScrollX + (getMeasuredWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001561 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001562 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001563 final int childCount = getChildCount();
1564 for (int i = 0; i < childCount; ++i) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001565 View layout = getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001566 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001567 int halfChildWidth = (childWidth / 2);
Sunny Goyal228153d2018-01-04 15:35:22 -08001568 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1570 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1571 minDistanceFromScreenCenter = distanceFromScreenCenter;
1572 minDistanceFromScreenCenterIndex = i;
1573 }
1574 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001575 return minDistanceFromScreenCenterIndex;
1576 }
1577
1578 protected void snapToDestination() {
Adam Cohen8d769d62017-06-23 17:27:38 -07001579 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
1580 }
1581
1582 protected boolean isInOverScroll() {
1583 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
1584 }
1585
1586 protected int getPageSnapDuration() {
1587 if (isInOverScroll()) {
1588 return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION;
1589 }
1590 return PAGE_SNAP_ANIMATION_DURATION;
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 }
1592
Adam Cohene0f66b52010-11-23 15:06:07 -08001593 // We want the duration of the page snap animation to be influenced by the distance that
1594 // the screen has to travel, however, we don't want this duration to be effected in a
1595 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1596 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001597 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001598 f -= 0.5f; // center the values about 0.
1599 f *= 0.3f * Math.PI / 2.0f;
1600 return (float) Math.sin(f);
1601 }
1602
Michael Jurka0142d492010-08-25 17:46:15 -07001603 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001604 whichPage = validateNewPage(whichPage);
Sunny Goyal228153d2018-01-04 15:35:22 -08001605 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001606
Adam Cohenedb40762013-07-18 16:45:45 -07001607 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001608 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001609 int duration = 0;
1610
Sunny Goyal4d113a52015-05-27 10:05:28 -07001611 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001612 // If the velocity is low enough, then treat this more as an automatic page advance
1613 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001614 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001615 return;
1616 }
1617
1618 // Here we compute a "distance" that will be used in the computation of the overall
1619 // snap duration. This is a function of the actual distance that needs to be traveled;
1620 // we keep this value close to half screen size in order to reduce the variance in snap
1621 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001622 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001623 float distance = halfScreenSize + halfScreenSize *
1624 distanceInfluenceForSnapDuration(distanceRatio);
1625
1626 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001627 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001628
1629 // we want the page's snap velocity to approximately match the velocity at which the
1630 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001631 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1632 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001633
1634 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001635 }
1636
Sunny Goyal1740d902015-05-27 11:14:01 -07001637 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001638 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001639 }
1640
Adam Cohenf9c184a2016-01-15 16:47:43 -08001641 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001642 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001643 }
1644
Tony Wickham07b1d672018-01-04 17:16:05 -08001645 public void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001646 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001647 }
1648
Adam Cohenf9618852013-11-08 06:45:03 -08001649 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
1650 snapToPage(whichPage, duration, false, interpolator);
1651 }
1652
1653 protected void snapToPage(int whichPage, int duration, boolean immediate,
1654 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001655 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001656
Adam Cohenedb40762013-07-18 16:45:45 -07001657 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001658 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08001659 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07001660 }
1661
1662 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08001663 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 }
Michael Jurka0142d492010-08-25 17:46:15 -07001665
Adam Cohenf9618852013-11-08 06:45:03 -08001666 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
1667 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001668 whichPage = validateNewPage(whichPage);
1669
Adam Cohen7d30a372013-07-01 17:03:59 -07001670 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07001671
Winson Chung321e9ee2010-08-09 13:37:56 -07001672 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07001673 if (immediate) {
1674 duration = 0;
1675 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001676 duration = Math.abs(delta);
1677 }
1678
Sunny Goyal9ccafbf2016-10-26 13:06:08 -07001679 if (duration != 0) {
1680 pageBeginTransition();
1681 }
1682
Adam Cohenf358a4b2013-07-23 16:47:31 -07001683 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08001684 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001685 }
Adam Cohenf9618852013-11-08 06:45:03 -08001686
1687 if (interpolator != null) {
1688 mScroller.setInterpolator(interpolator);
1689 } else {
1690 mScroller.setInterpolator(mDefaultInterpolator);
1691 }
1692
Sunny Goyalc86df472016-02-25 09:19:38 -08001693 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001694
Adam Cohen674531f2013-12-13 15:07:14 -08001695 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07001696
1697 // Trigger a compute() to finish switching pages if necessary
1698 if (immediate) {
1699 computeScroll();
Sunny Goyal76dbf6f2017-01-03 14:55:47 -08001700 pageEndTransition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001701 }
1702
Winson Chung321e9ee2010-08-09 13:37:56 -07001703 invalidate();
1704 }
1705
Winson Chung321e9ee2010-08-09 13:37:56 -07001706 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001707 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001708 }
1709
1710 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07001711 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001712 }
1713
Adam Cohendbdff6b2013-09-18 19:09:15 -07001714 @Override
1715 public boolean performLongClick() {
1716 mCancelTap = true;
1717 return super.performLongClick();
1718 }
1719
Winson Chung321e9ee2010-08-09 13:37:56 -07001720 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001721 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001722
1723 SavedState(Parcelable superState) {
1724 super(superState);
1725 }
1726
Adam Cohen091440a2015-03-18 14:16:05 -07001727 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001729 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 }
1731
1732 @Override
1733 public void writeToParcel(Parcel out, int flags) {
1734 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001735 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001736 }
1737
1738 public static final Parcelable.Creator<SavedState> CREATOR =
1739 new Parcelable.Creator<SavedState>() {
1740 public SavedState createFromParcel(Parcel in) {
1741 return new SavedState(in);
1742 }
1743
1744 public SavedState[] newArray(int size) {
1745 return new SavedState[size];
1746 }
1747 };
1748 }
1749
Adam Cohen7d30a372013-07-01 17:03:59 -07001750 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07001751 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001752 if (mDragView != null) {
Sunny Goyal9e76f682017-02-13 12:13:43 -08001753 Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView,
1754 new PropertyListBuilder()
1755 .scale(1)
1756 .translationX(0)
1757 .translationY(0)
1758 .build())
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001759 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001760 anim.addListener(new AnimatorListenerAdapter() {
1761 @Override
1762 public void onAnimationEnd(Animator animation) {
1763 onPostReorderingAnimationCompleted();
1764 }
1765 });
1766 anim.start();
1767 }
Winson Chung3ac74c52011-06-30 17:39:37 -07001768 }
1769
Sunny Goyal1d08f702015-05-04 15:50:25 -07001770 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001771 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
1772 mTouchState = TOUCH_STATE_REORDERING;
1773 mIsReordering = true;
1774
Adam Cohen7d30a372013-07-01 17:03:59 -07001775 // We must invalidate to trigger a redraw to update the layers such that the drag view
1776 // is always drawn on top
1777 invalidate();
1778 }
1779
Adam Cohen091440a2015-03-18 14:16:05 -07001780 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001781 // Trigger the callback when reordering has settled
1782 --mPostReorderingPreZoomInRemainingAnimationCount;
1783 if (mPostReorderingPreZoomInRunnable != null &&
1784 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
1785 mPostReorderingPreZoomInRunnable.run();
1786 mPostReorderingPreZoomInRunnable = null;
1787 }
1788 }
1789
Sunny Goyal1d08f702015-05-04 15:50:25 -07001790 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001791 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001792 }
1793
Adam Cohenf358a4b2013-07-23 16:47:31 -07001794 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07001795 int dragViewIndex = indexOfChild(v);
1796
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001797 // Do not allow the first page to be moved around
1798 if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07001799
Adam Cohen7d30a372013-07-01 17:03:59 -07001800 // Check if we are within the reordering range
Sunny Goyal4d519f22017-10-24 10:32:40 -07001801 if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001802 // Find the drag view under the pointer
1803 mDragView = getChildAt(dragViewIndex);
1804 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
1805 mDragViewBaselineLeft = mDragView.getLeft();
Sunny Goyal4d519f22017-10-24 10:32:40 -07001806 mReorderingStarted = true;
1807
Adam Cohenf9618852013-11-08 06:45:03 -08001808 snapToPage(getPageNearestToCenterOfScreen());
1809 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001810 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07001811 return true;
1812 }
1813 return false;
1814 }
1815
1816 boolean isReordering(boolean testTouchState) {
1817 boolean state = mIsReordering;
1818 if (testTouchState) {
1819 state &= (mTouchState == TOUCH_STATE_REORDERING);
1820 }
1821 return state;
1822 }
1823 void endReordering() {
1824 // For simplicity, we call endReordering sometimes even if reordering was never started.
1825 // In that case, we don't want to do anything.
1826 if (!mReorderingStarted) return;
1827 mReorderingStarted = false;
1828
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001829 mPostReorderingPreZoomInRunnable = new Runnable() {
1830 public void run() {
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001831 // If we haven't flung-to-delete the current child,
1832 // then we just animate the drag view back into position
1833 onEndReordering();
1834
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001835 enableFreeScroll();
Mario Bertschlerdcc26fc2016-12-21 11:48:48 -08001836 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001837 };
Adam Cohen7d30a372013-07-01 17:03:59 -07001838
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001839 mPostReorderingPreZoomInRemainingAnimationCount =
1840 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
1841 // Snap to the current page
1842 snapToPage(indexOfChild(mDragView), 0);
1843 // Animate the drag view back to the front position
1844 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07001845 }
1846
Winson Chung6a0f57d2011-06-29 20:10:49 -07001847 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07001848 @SuppressWarnings("deprecation")
Winson Chung6a0f57d2011-06-29 20:10:49 -07001849 @Override
1850 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1851 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001852 info.setScrollable(getPageCount() > 1);
1853 if (getCurrentPage() < getPageCount() - 1) {
1854 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1855 }
1856 if (getCurrentPage() > 0) {
1857 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1858 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07001859 info.setClassName(getClass().getName());
1860
1861 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
1862 // Besides disabling the accessibility long-click, this also prevents this view from getting
1863 // accessibility focus.
1864 info.setLongClickable(false);
Sunny Goyala52ecb02016-12-16 15:04:51 -08001865 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001866 }
1867
1868 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07001869 public void sendAccessibilityEvent(int eventType) {
1870 // Don't let the view send real scroll events.
1871 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1872 super.sendAccessibilityEvent(eventType);
1873 }
1874 }
1875
1876 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07001877 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1878 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07001879 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001880 }
1881
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001882 @Override
1883 public boolean performAccessibilityAction(int action, Bundle arguments) {
1884 if (super.performAccessibilityAction(action, arguments)) {
1885 return true;
1886 }
1887 switch (action) {
1888 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1889 if (getCurrentPage() < getPageCount() - 1) {
1890 scrollRight();
1891 return true;
1892 }
1893 } break;
1894 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1895 if (getCurrentPage() > 0) {
1896 scrollLeft();
1897 return true;
1898 }
1899 } break;
1900 }
1901 return false;
1902 }
1903
Sunny Goyal53fe1f22016-07-08 08:47:07 -07001904 protected String getPageIndicatorDescription() {
1905 return getCurrentPageDescription();
1906 }
1907
Adam Cohen0ffac432013-07-10 11:19:26 -07001908 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07001909 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07001910 getNextPage() + 1, getChildCount());
1911 }
1912
Winson Chungd11265e2011-08-30 13:37:23 -07001913 @Override
1914 public boolean onHoverEvent(android.view.MotionEvent event) {
1915 return true;
1916 }
Adam Cohen5084cba2013-09-03 12:01:16 -07001917}