blob: 02e894b7ff26e24cf59c3075244049a379f32246 [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
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal8bf6f312016-01-23 14:40:35 -080031import android.graphics.RectF;
Sunny Goyal316490e2015-06-02 09:38:28 -070032import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070049import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Tony Wickhamf549dab2016-05-16 09:54:06 -070052
53import com.android.launcher3.pageindicators.PageIndicator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070054import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
Tony Wickhamf549dab2016-05-16 09:54:06 -070056
Winson Chung6a0f57d2011-06-29 20:10:49 -070057import java.util.ArrayList;
58
Winson Chung321e9ee2010-08-09 13:37:56 -070059/**
60 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070061 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070062 */
Michael Jurka8b805b12012-04-18 14:23:14 -070063public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070064 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070065 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Winson Chung86f77532010-08-24 11:08:22 -070068 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070069 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Vadim Tryshevfedca432015-08-19 17:55:02 -070071 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070072 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070073
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080075 // The page is moved more than halfway, automatically move to the next page on touch up.
76 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080077
Sunny Goyal8e2133b2015-05-06 13:39:07 -070078 private static final float MAX_SCROLL_PROGRESS = 1.0f;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 // The following constants need to be scaled based on density. The scaled versions will be
81 // assigned to the corresponding member variables below.
82 private static final int FLING_THRESHOLD_VELOCITY = 500;
83 private static final int MIN_SNAP_VELOCITY = 1500;
84 private static final int MIN_FLING_VELOCITY = 250;
85
Adam Cohen21cd0022013-10-09 18:57:02 -070086 public static final int INVALID_RESTORE_PAGE = -1001;
87
Adam Cohenf358a4b2013-07-23 16:47:31 -070088 private boolean mFreeScroll = false;
89 private int mFreeScrollMinScrollX = -1;
90 private int mFreeScrollMaxScrollX = -1;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 protected int mFlingThresholdVelocity;
93 protected int mMinFlingVelocity;
94 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070097 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070098
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700101 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700102 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700103
Sunny Goyal4ffec482016-02-09 11:28:52 -0800104 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800106 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800107 protected LauncherScroller mScroller;
108 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mParentDownMotionX;
113 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700115 private float mDownMotionY;
116 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700117 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700118 private float mLastMotionX;
119 private float mLastMotionXRemainder;
120 private float mLastMotionY;
121 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700122 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700123
Adam Cohendbdff6b2013-09-18 19:09:15 -0700124 private boolean mCancelTap;
125
Adam Cohenedb40762013-07-18 16:45:45 -0700126 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected final static int TOUCH_STATE_REST = 0;
129 protected final static int TOUCH_STATE_SCROLLING = 1;
130 protected final static int TOUCH_STATE_PREV_PAGE = 2;
131 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 protected final static int TOUCH_STATE_REORDERING = 4;
133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700135 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800136
Michael Jurka0142d492010-08-25 17:46:15 -0700137 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka7426c422010-11-11 15:23:47 -0800139 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800141 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800142 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka5f1c5092010-09-03 14:15:02 -0700144 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700150
Patrick Dubroy1262e362010-10-06 15:49:50 -0700151 protected boolean mIsPageMoving = false;
152
Sunny Goyal061380a2016-02-29 15:15:03 -0800153 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700154
Winson Chungd2be3812013-07-16 11:11:32 -0700155 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mPageIndicatorViewId;
157 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700158 // The viewport whether the pages are to be contained (the actual view may be larger than the
159 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800160 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 private Rect mViewport = new Rect();
162
163 // Reordering
164 // We use the min scale to determine how much to expand the actually PagedView measured
165 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700166 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700167 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700168 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
169
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700171 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700172 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 // This variable's scope is only for the duration of startReordering() and endReordering()
176 private boolean mReorderingStarted = false;
177 // This variable's scope is for the duration of startReordering() and after the zoomIn()
178 // animation after endReordering()
179 private boolean mIsReordering;
180 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700181 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700182 private int mPostReorderingPreZoomInRemainingAnimationCount;
183 private Runnable mPostReorderingPreZoomInRunnable;
184
Adam Cohen7d30a372013-07-01 17:03:59 -0700185 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700186 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700187 private static final float[] sTmpPoint = new float[2];
188 private static final int[] sTmpIntPoint = new int[2];
189 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800190 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700191
John Spurlock77e1f472013-09-11 10:09:51 -0400192 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700193 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400194
Sunny Goyal4d113a52015-05-27 10:05:28 -0700195 // Edge effect
196 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
197 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 public PagedView(Context context) {
200 this(context, null);
201 }
202
203 public PagedView(Context context, AttributeSet attrs) {
204 this(context, attrs, 0);
205 }
206
207 public PagedView(Context context, AttributeSet attrs, int defStyle) {
208 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700209
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 TypedArray a = context.obtainStyledAttributes(attrs,
211 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700212 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 a.recycle();
214
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700216 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700217 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
219
220 /**
221 * Initializes various states for this workspace.
222 */
Michael Jurka0142d492010-08-25 17:46:15 -0700223 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800224 mScroller = new LauncherScroller(getContext());
225 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700226 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227
228 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800231
Sunny Goyalcf25b522015-07-09 00:01:18 -0700232 float density = getResources().getDisplayMetrics().density;
233 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
234 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
235 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700236 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700237 setWillNotDraw(false);
238 }
239
240 protected void setEdgeGlowColor(int color) {
241 mEdgeGlowLeft.setColor(color);
242 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 }
244
Adam Cohenf9618852013-11-08 06:45:03 -0800245 protected void setDefaultInterpolator(Interpolator interpolator) {
246 mDefaultInterpolator = interpolator;
247 mScroller.setInterpolator(mDefaultInterpolator);
248 }
249
Winson Chungd2be3812013-07-16 11:11:32 -0700250 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700251 super.onAttachedToWindow();
252
Winson Chungd2be3812013-07-16 11:11:32 -0700253 // Hook up the page indicator
254 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700255 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700256 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700257 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700258 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700259
Tony Wickhamf549dab2016-05-16 09:54:06 -0700260 ArrayList<PageIndicator.PageMarkerResources> markers = new ArrayList<>();
Winson Chung82dfe582013-07-26 15:07:49 -0700261 for (int i = 0; i < getChildCount(); ++i) {
262 markers.add(getPageIndicatorMarker(i));
263 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700265 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700266
267 OnClickListener listener = getPageIndicatorClickListener();
268 if (listener != null) {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700269 mPageIndicator.getView().setOnClickListener(listener);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700270 }
Tony Wickhamf549dab2016-05-16 09:54:06 -0700271 mPageIndicator.getView().setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700272 }
273 }
274
Adam Cohen53805212013-10-01 10:39:23 -0700275 protected String getPageIndicatorDescription() {
276 return getCurrentPageDescription();
277 }
278
279 protected OnClickListener getPageIndicatorClickListener() {
280 return null;
281 }
282
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700283 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700284 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700285 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700286 // Unhook the page indicator
287 mPageIndicator = null;
288 }
289
Adam Cohen7d30a372013-07-01 17:03:59 -0700290 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700291 private float[] mapPointFromViewToParent(View v, float x, float y) {
292 sTmpPoint[0] = x;
293 sTmpPoint[1] = y;
294 v.getMatrix().mapPoints(sTmpPoint);
295 sTmpPoint[0] += v.getLeft();
296 sTmpPoint[1] += v.getTop();
297 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700298 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700299 private float[] mapPointFromParentToView(View v, float x, float y) {
300 sTmpPoint[0] = x - v.getLeft();
301 sTmpPoint[1] = y - v.getTop();
302 v.getMatrix().invert(sTmpInvMatrix);
303 sTmpInvMatrix.mapPoints(sTmpPoint);
304 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700305 }
306
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700307 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700308 if (mDragView != null) {
309 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
310 (mDragViewBaselineLeft - mDragView.getLeft());
311 float y = mLastMotionY - mDownMotionY;
312 mDragView.setTranslationX(x);
313 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700314
Adam Cohenf358a4b2013-07-23 16:47:31 -0700315 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
316 + x + ", " + y);
317 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700318 }
319
320 public void setMinScale(float f) {
321 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700322 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700323 requestLayout();
324 }
325
326 @Override
327 public void setScaleX(float scaleX) {
328 super.setScaleX(scaleX);
329 if (isReordering(true)) {
330 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
331 mLastMotionX = p[0];
332 mLastMotionY = p[1];
333 updateDragViewTranslationDuringDrag();
334 }
335 }
336
337 // Convenience methods to get the actual width/height of the PagedView (since it is measured
338 // to be larger to account for the minimum possible scale)
339 int getViewportWidth() {
340 return mViewport.width();
341 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800342 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700343 return mViewport.height();
344 }
345
346 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
347 // PagedView both horizontally and vertically
348 int getViewportOffsetX() {
349 return (getMeasuredWidth() - getViewportWidth()) / 2;
350 }
351
352 int getViewportOffsetY() {
353 return (getMeasuredHeight() - getViewportHeight()) / 2;
354 }
355
Adam Cohenedb40762013-07-18 16:45:45 -0700356 PageIndicator getPageIndicator() {
357 return mPageIndicator;
358 }
Winson Chung7819a562013-09-19 15:55:45 -0700359 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700360 return new PageIndicator.PageMarkerResources(R.drawable.ic_pageindicator_current,
361 R.drawable.ic_pageindicator_default);
Winson Chung82dfe582013-07-26 15:07:49 -0700362 }
Adam Cohenedb40762013-07-18 16:45:45 -0700363
Adam Cohen674531f2013-12-13 15:07:14 -0800364 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700365 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
366 * that the user was on before entering free scroll mode (e.g. the home screen page they
367 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
368 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700370 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700371 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700373
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700374 /**
375 * Returns the index of page to be shown immediately afterwards.
376 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700377 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700378 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
379 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700380
Adam Cohenf9c184a2016-01-15 16:47:43 -0800381 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700382 return getChildCount();
383 }
384
Sunny Goyal83a8f042015-05-19 12:52:12 -0700385 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 return getChildAt(index);
387 }
388
Adam Cohenae4f1552011-10-20 00:15:42 -0700389 protected int indexToPage(int index) {
390 return index;
391 }
392
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800394 * Updates the scroll of the current page immediately to its final scroll position. We use this
395 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
396 * the previous tab page.
397 */
398 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700399 // If the current page is invalid, just reset the scroll position to zero
400 int newX = 0;
401 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700402 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700403 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800404 scrollTo(newX, 0);
405 mScroller.setFinalX(newX);
Tony Wickham8f7ead32016-04-07 18:46:44 -0700406 forceFinishScroller(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800407 }
408
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700409 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700410 mScroller.abortAnimation();
411 // We need to clean up the next page here to avoid computeScrollHelper from
412 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700413 if (resetNextPage) {
414 mNextPage = INVALID_PAGE;
415 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700416 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700417
Tony Wickham8f7ead32016-04-07 18:46:44 -0700418 private void forceFinishScroller(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700419 mScroller.forceFinished(true);
420 // We need to clean up the next page here to avoid computeScrollHelper from
421 // updating current page on the pass.
Tony Wickham8f7ead32016-04-07 18:46:44 -0700422 if (resetNextPage) {
423 mNextPage = INVALID_PAGE;
424 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700425 }
426
Adam Cohen6f127a62014-06-12 14:54:41 -0700427 private int validateNewPage(int newPage) {
428 int validatedPage = newPage;
429 // When in free scroll mode, we need to clamp to the free scroll page range.
430 if (mFreeScroll) {
431 getFreeScrollPageRange(mTempVisiblePagesRange);
432 validatedPage = Math.max(mTempVisiblePagesRange[0],
433 Math.min(newPage, mTempVisiblePagesRange[1]));
434 }
435 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickhamf549dab2016-05-16 09:54:06 -0700436 validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700437 return validatedPage;
438 }
439
Chet Haasebc2f0822012-10-26 17:59:53 -0700440 /**
Winson Chung86f77532010-08-24 11:08:22 -0700441 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700443 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800444 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700445 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800446 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800447 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
448 // the default
449 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800450 return;
451 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700452 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700453 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700454 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700455 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800456 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 }
458
Winson Chung8c87cd82013-07-23 16:20:10 -0700459 /**
460 * The restore page will be set in place of the current page at the next (likely first)
461 * layout.
462 */
463 void setRestorePage(int restorePage) {
464 mRestorePage = restorePage;
465 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800466 int getRestorePage() {
467 return mRestorePage;
468 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700469
Adam Cohen674531f2013-12-13 15:07:14 -0800470 /**
471 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
472 * has settled.
473 */
Michael Jurka0142d492010-08-25 17:46:15 -0700474 protected void notifyPageSwitchListener() {
Adam Cohen674531f2013-12-13 15:07:14 -0800475 updatePageIndicator();
476 }
477
478 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700479 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700480 if (mPageIndicator != null) {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700481 mPageIndicator.getView().setContentDescription(getPageIndicatorDescription());
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700482 if (!isReordering(false)) {
483 mPageIndicator.setActiveMarker(getNextPage());
484 }
Winson Chungd2be3812013-07-16 11:11:32 -0700485 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800487 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700488 if (!mIsPageMoving) {
489 mIsPageMoving = true;
490 onPageBeginMoving();
491 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700492 }
493
Michael Jurkace7e05f2011-02-01 22:02:35 -0800494 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700495 if (mIsPageMoving) {
496 mIsPageMoving = false;
497 onPageEndMoving();
498 }
Michael Jurka0142d492010-08-25 17:46:15 -0700499 }
500
Adam Cohen26976d92011-03-22 15:33:33 -0700501 protected boolean isPageMoving() {
502 return mIsPageMoving;
503 }
504
Michael Jurka0142d492010-08-25 17:46:15 -0700505 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700506 protected void onPageBeginMoving() {
507 }
508
509 // a method that subclasses can override to add behavior
510 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700511 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700512 }
513
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 /**
Winson Chung86f77532010-08-24 11:08:22 -0700515 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700516 *
517 * @param l The listener used to respond to long clicks.
518 */
519 @Override
520 public void setOnLongClickListener(OnLongClickListener l) {
521 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700522 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700524 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 }
Adam Cohen1697b792013-09-17 19:08:21 -0700526 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 }
528
Sunny Goyalc86df472016-02-25 09:19:38 -0800529 protected int getUnboundedScrollX() {
530 return getScrollX();
531 }
532
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800534 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800535 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800536 }
537
538 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700539 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700540 // In free scroll mode, we clamp the scrollX
541 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700542 // If the scroller is trying to move to a location beyond the maximum allowed
543 // in the free scroll mode, we make sure to end the scroll operation.
544 if (!mScroller.isFinished() &&
545 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
Tony Wickham8f7ead32016-04-07 18:46:44 -0700546 forceFinishScroller(false);
Adam Cohenb2b02b92015-06-10 18:40:05 -0700547 }
548
Adam Cohenf358a4b2013-07-23 16:47:31 -0700549 x = Math.min(x, mFreeScrollMaxScrollX);
550 x = Math.max(x, mFreeScrollMinScrollX);
551 }
552
Sunny Goyal70660032015-05-14 00:07:08 -0700553 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
554 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700555 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700556 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800557 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700558 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700559 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700560 overScroll(x - mMaxScrollX);
561 } else {
562 overScroll(x);
563 }
Adam Cohen68d73932010-11-15 10:50:58 -0800564 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700565 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700566 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800567 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700568 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700569 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700570 overScroll(x);
571 } else {
572 overScroll(x - mMaxScrollX);
573 }
Adam Cohen68d73932010-11-15 10:50:58 -0800574 }
575 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700576 if (mWasInOverscroll) {
577 overScroll(0);
578 mWasInOverscroll = false;
579 }
Adam Cohen68d73932010-11-15 10:50:58 -0800580 super.scrollTo(x, y);
581 }
582
Adam Cohen7d30a372013-07-01 17:03:59 -0700583 // Update the last motion events when scrolling
584 if (isReordering(true)) {
585 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
586 mLastMotionX = p[0];
587 mLastMotionY = p[1];
588 updateDragViewTranslationDuringDrag();
589 }
Michael Jurka0142d492010-08-25 17:46:15 -0700590 }
591
Adam Cohen53805212013-10-01 10:39:23 -0700592 private void sendScrollAccessibilityEvent() {
593 AccessibilityManager am =
594 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
595 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700596 if (mCurrentPage != getNextPage()) {
597 AccessibilityEvent ev =
598 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700599 ev.setScrollable(true);
600 ev.setScrollX(getScrollX());
601 ev.setScrollY(getScrollY());
602 ev.setMaxScrollX(mMaxScrollX);
603 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700604
Vadim Tryshevf4715972015-05-08 17:21:03 -0700605 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700606 }
Adam Cohen53805212013-10-01 10:39:23 -0700607 }
608 }
609
Michael Jurka0142d492010-08-25 17:46:15 -0700610 // we moved this functionality to a helper function so SmoothPagedView can reuse it
611 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800612 return computeScrollHelper(true);
613 }
614
615 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700617 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800619 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700620 float scaleX = mFreeScroll ? getScaleX() : 1f;
621 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
622 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700623 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800624 if (shouldInvalidate) {
625 invalidate();
626 }
Michael Jurka0142d492010-08-25 17:46:15 -0700627 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800628 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700629 sendScrollAccessibilityEvent();
630
Adam Cohen6f127a62014-06-12 14:54:41 -0700631 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700632 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700633 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700634
Adam Cohen73aa9752010-11-24 16:26:58 -0800635 // We don't want to trigger a page end moving unless the page has settled
636 // and the user has stopped scrolling
637 if (mTouchState == TOUCH_STATE_REST) {
638 pageEndMoving();
639 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700640
Adam Cohen7d30a372013-07-01 17:03:59 -0700641 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700642 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700643 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700644 if (am.isEnabled()) {
645 // Notify the user when the page changes
646 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700647 }
Michael Jurka0142d492010-08-25 17:46:15 -0700648 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
Michael Jurka0142d492010-08-25 17:46:15 -0700650 return false;
651 }
652
653 @Override
654 public void computeScroll() {
655 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 }
657
Adam Cohen96d30a12013-07-16 18:13:21 -0700658 public static class LayoutParams extends ViewGroup.LayoutParams {
659 public boolean isFullScreenPage = false;
660
661 /**
662 * {@inheritDoc}
663 */
664 public LayoutParams(int width, int height) {
665 super(width, height);
666 }
667
Sunny Goyal41b22c02015-05-14 15:20:48 -0700668 public LayoutParams(Context context, AttributeSet attrs) {
669 super(context, attrs);
670 }
671
Adam Cohen96d30a12013-07-16 18:13:21 -0700672 public LayoutParams(ViewGroup.LayoutParams source) {
673 super(source);
674 }
675 }
676
Sunny Goyal41b22c02015-05-14 15:20:48 -0700677 @Override
678 public LayoutParams generateLayoutParams(AttributeSet attrs) {
679 return new LayoutParams(getContext(), attrs);
680 }
681
682 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700683 protected LayoutParams generateDefaultLayoutParams() {
684 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
685 }
686
Sunny Goyal41b22c02015-05-14 15:20:48 -0700687 @Override
688 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
689 return new LayoutParams(p);
690 }
691
692 @Override
693 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
694 return p instanceof LayoutParams;
695 }
696
Adam Cohenedb40762013-07-18 16:45:45 -0700697 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700698 LayoutParams lp = generateDefaultLayoutParams();
699 lp.isFullScreenPage = true;
700 super.addView(page, 0, lp);
701 }
702
Adam Cohen410f3cd2013-09-22 12:09:32 -0700703 public int getNormalChildHeight() {
704 return mNormalChildHeight;
705 }
706
Winson Chung321e9ee2010-08-09 13:37:56 -0700707 @Override
708 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700709 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700710 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
711 return;
712 }
713
Adam Cohen7d30a372013-07-01 17:03:59 -0700714 // We measure the dimensions of the PagedView to be larger than the pages so that when we
715 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700716 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
717 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
718 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700719 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800720 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700721 // viewport, we can be at most one and a half screens offset once we scale down
722 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700723 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
724 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700725
Winson Chungc82d2622013-11-06 13:23:29 -0800726 int parentWidthSize = (int) (2f * maxSize);
727 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700728 int scaledWidthSize, scaledHeightSize;
729 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700730 scaledWidthSize = (int) (parentWidthSize / mMinScale);
731 scaledHeightSize = (int) (parentHeightSize / mMinScale);
732 } else {
733 scaledWidthSize = widthSize;
734 scaledHeightSize = heightSize;
735 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700736 mViewport.set(0, 0, widthSize, heightSize);
737
738 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
739 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
740 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 }
742
Winson Chung8aad6102012-05-11 16:27:49 -0700743 // Return early if we aren't given a proper dimension
744 if (widthSize <= 0 || heightSize <= 0) {
745 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
746 return;
747 }
748
Adam Lesinski6b879f02010-11-04 16:15:23 -0700749 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
750 * of the All apps view on XLarge displays to not take up more space then it needs. Width
751 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
752 * each page to have the same width.
753 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 final int verticalPadding = getPaddingTop() + getPaddingBottom();
755 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700756
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700757 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700759 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700760 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700761 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
762 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
763 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
764 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 final int childCount = getChildCount();
766 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700767 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700768 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100769 if (child.getVisibility() != GONE) {
770 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700771
Vladimir Marko2824b072013-10-04 16:42:17 +0100772 int childWidthMode;
773 int childHeightMode;
774 int childWidth;
775 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700776
Vladimir Marko2824b072013-10-04 16:42:17 +0100777 if (!lp.isFullScreenPage) {
778 if (lp.width == LayoutParams.WRAP_CONTENT) {
779 childWidthMode = MeasureSpec.AT_MOST;
780 } else {
781 childWidthMode = MeasureSpec.EXACTLY;
782 }
783
784 if (lp.height == LayoutParams.WRAP_CONTENT) {
785 childHeightMode = MeasureSpec.AT_MOST;
786 } else {
787 childHeightMode = MeasureSpec.EXACTLY;
788 }
789
Adam Cohen3b185e22013-10-29 14:45:58 -0700790 childWidth = getViewportWidth() - horizontalPadding
791 - mInsets.left - mInsets.right;
792 childHeight = getViewportHeight() - verticalPadding
793 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100794 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700795 } else {
796 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700797 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100798
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800799 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700800 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700801 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700802 if (referenceChildWidth == 0) {
803 referenceChildWidth = childWidth;
804 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700805
Vladimir Marko2824b072013-10-04 16:42:17 +0100806 final int childWidthMeasureSpec =
807 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
808 final int childHeightMeasureSpec =
809 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
810 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700811 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700812 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700813 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800814 }
815
Sunny Goyalcf25b522015-07-09 00:01:18 -0700816 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700818 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700819 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700820 return;
821 }
822
Winson Chung785d2eb2011-04-14 16:08:02 -0700823 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700825
Adam Cohen7d30a372013-07-01 17:03:59 -0700826 int offsetX = getViewportOffsetX();
827 int offsetY = getViewportOffsetY();
828
829 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700830 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700831
Sunny Goyal70660032015-05-14 00:07:08 -0700832 final int startIndex = mIsRtl ? childCount - 1 : 0;
833 final int endIndex = mIsRtl ? -1 : childCount;
834 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700835
Adam Cohen7d30a372013-07-01 17:03:59 -0700836 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700837
Adam Cohen3b185e22013-10-29 14:45:58 -0700838 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000839 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700840
841 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700842 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
843 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700844 }
845
Adam Cohen0ffac432013-07-10 11:19:26 -0700846 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700847 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700849 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100850 int childTop;
851 if (lp.isFullScreenPage) {
852 childTop = offsetY;
853 } else {
854 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700855 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100856 }
857
Adam Cohen96d30a12013-07-16 18:13:21 -0700858 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700859 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800860
Winson Chung785d2eb2011-04-14 16:08:02 -0700861 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700862 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800863 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700864
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
866 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000867
868 int pageGap = mPageSpacing;
869 int next = i + delta;
870 if (next != endIndex) {
871 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
872 } else {
873 nextLp = null;
874 }
875
876 // Prevent full screen pages from showing in the viewport
877 // when they are not the current page.
878 if (lp.isFullScreenPage) {
879 pageGap = getPaddingLeft();
880 } else if (nextLp != null && nextLp.isFullScreenPage) {
881 pageGap = getPaddingRight();
882 }
883
Sunny Goyala1fbd842015-05-20 13:40:27 -0700884 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 }
886 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700887
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700888 final LayoutTransition transition = getLayoutTransition();
889 // If the transition is running defer updating max scroll, as some empty pages could
890 // still be present, and a max scroll change could cause sudden jumps in scroll.
891 if (transition != null && transition.isRunning()) {
892 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
893
894 @Override
895 public void startTransition(LayoutTransition transition, ViewGroup container,
896 View view, int transitionType) { }
897
898 @Override
899 public void endTransition(LayoutTransition transition, ViewGroup container,
900 View view, int transitionType) {
901 // Wait until all transitions are complete.
902 if (!transition.isRunning()) {
903 transition.removeTransitionListener(this);
904 updateMaxScrollX();
905 }
906 }
907 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700908 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700909 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700910 }
911
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700912 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
913 updateCurrentPageScroll();
914 mFirstLayout = false;
915 }
916
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700917 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700918 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700919 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700920 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700921 } else {
922 setCurrentPage(getNextPage());
923 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700924 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700925 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700926
927 if (isReordering(true)) {
928 updateDragViewTranslationDuringDrag();
929 }
Winson Chunge3193b92010-09-10 11:44:42 -0700930 }
931
Sunny Goyala1fbd842015-05-20 13:40:27 -0700932 protected int getChildGap() {
933 return 0;
934 }
935
Sunny Goyal316490e2015-06-02 09:38:28 -0700936 @Thunk void updateMaxScrollX() {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700937 mMaxScrollX = computeMaxScrollX();
938 }
939
940 protected int computeMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700941 int childCount = getChildCount();
942 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700943 final int index = mIsRtl ? 0 : childCount - 1;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700944 return getScrollForPage(index);
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700945 } else {
Tony Wickhamf549dab2016-05-16 09:54:06 -0700946 return 0;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700947 }
948 }
949
Adam Cohen3b185e22013-10-29 14:45:58 -0700950 public void setPageSpacing(int pageSpacing) {
951 mPageSpacing = pageSpacing;
952 requestLayout();
953 }
954
Sunny Goyal4d113a52015-05-27 10:05:28 -0700955 /**
956 * Called when the center screen changes during scrolling.
957 */
958 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700959
Winson Chunge3193b92010-09-10 11:44:42 -0700960 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700961 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700962 // Update the page indicator, we don't update the page indicator as we
963 // add/remove pages
964 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700965 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700966 mPageIndicator.addMarker(pageIndex,
967 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700968 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700969 }
970
Adam Cohen2591f6a2011-10-25 14:36:40 -0700971 // This ensures that when children are added, they get the correct transforms / alphas
972 // in accordance with any scroll effects.
973 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700974 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700975 invalidate();
976 }
977
Michael Jurka8b805b12012-04-18 14:23:14 -0700978 @Override
979 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700980 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700981 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700982 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700983 }
984
Winson Chungd2be3812013-07-16 11:11:32 -0700985 private void removeMarkerForView(int index) {
986 // Update the page indicator, we don't update the page indicator as we
987 // add/remove pages
988 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700989 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700990 }
991 }
992
993 @Override
994 public void removeView(View v) {
995 // XXX: We should find a better way to hook into this before the view
996 // gets removed form its parent...
997 removeMarkerForView(indexOfChild(v));
998 super.removeView(v);
999 }
1000 @Override
1001 public void removeViewInLayout(View v) {
1002 // XXX: We should find a better way to hook into this before the view
1003 // gets removed form its parent...
1004 removeMarkerForView(indexOfChild(v));
1005 super.removeViewInLayout(v);
1006 }
1007 @Override
1008 public void removeViewAt(int index) {
1009 // XXX: We should find a better way to hook into this before the view
1010 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001011 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001012 super.removeViewAt(index);
1013 }
1014 @Override
1015 public void removeAllViewsInLayout() {
1016 // Update the page indicator, we don't update the page indicator as we
1017 // add/remove pages
1018 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001019 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001020 }
1021
1022 super.removeAllViewsInLayout();
1023 }
1024
Adam Cohen73894962011-10-31 13:17:17 -07001025 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001026 if (index < 0 || index > getChildCount() - 1) return 0;
1027
Adam Cohenf698c6e2013-07-17 18:44:25 -07001028 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001029
Adam Cohenf698c6e2013-07-17 18:44:25 -07001030 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001031 }
1032
Adam Cohen6f127a62014-06-12 14:54:41 -07001033 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001034 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001035 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001036 }
1037
Michael Jurkadde558b2011-11-09 22:09:06 -08001038 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001039 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001040 range[0] = -1;
1041 range[1] = -1;
1042
Sunny Goyalee688162016-02-12 14:24:21 -08001043 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001044 final int visibleLeft = -getLeft();
1045 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001046 final Matrix pageShiftMatrix = getPageShiftMatrix();
1047 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001048
Sunny Goyalee688162016-02-12 14:24:21 -08001049 for (int i = 0; i < count; i++) {
1050 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001051
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001052 // Verify if the page bounds are within the visible range.
1053 sTmpRectF.left = 0;
1054 sTmpRectF.right = currPage.getMeasuredWidth();
1055 currPage.getMatrix().mapRect(sTmpRectF);
1056 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001057 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001058
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001059 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001060 if (range[0] == -1) {
1061 continue;
1062 } else {
1063 break;
1064 }
1065 }
Sunny Goyalee688162016-02-12 14:24:21 -08001066 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001067 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001068 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001069 }
1070 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001071
Sunny Goyalee688162016-02-12 14:24:21 -08001072 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001073 } else {
1074 range[0] = -1;
1075 range[1] = -1;
1076 }
1077 }
1078
Sunny Goyalee688162016-02-12 14:24:21 -08001079 protected Matrix getPageShiftMatrix() {
1080 return getMatrix();
1081 }
1082
Michael Jurka920d7f42012-05-14 16:29:55 -07001083 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001084 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001085 }
1086
Michael Jurkadde558b2011-11-09 22:09:06 -08001087 @Override
1088 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001089 // Find out which screens are visible; as an optimization we only call draw on them
1090 final int pageCount = getChildCount();
1091 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001092 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001093 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001094
1095 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1096 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1097 // set it for the next frame
1098 mForceScreenScrolled = false;
1099 screenScrolled(screenCenter);
1100 mLastScreenCenter = screenCenter;
1101 }
1102
Michael Jurkadde558b2011-11-09 22:09:06 -08001103 getVisiblePages(mTempVisiblePagesRange);
1104 final int leftScreen = mTempVisiblePagesRange[0];
1105 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001106 if (leftScreen != -1 && rightScreen != -1) {
1107 final long drawingTime = getDrawingTime();
1108 // Clip to the bounds
1109 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001110 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1111 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001112
Adam Cohen7d30a372013-07-01 17:03:59 -07001113 // Draw all the children, leaving the drag view for last
1114 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001115 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001116 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001117 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001118 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001119 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001120 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001121 // Draw the drag view on top (if there is one)
1122 if (mDragView != null) {
1123 drawChild(canvas, mDragView, drawingTime);
1124 }
1125
Winson Chungc6f10b92011-11-14 11:39:07 -08001126 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001127 }
Michael Jurka0142d492010-08-25 17:46:15 -07001128 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001129 }
1130
1131 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001132 public void draw(Canvas canvas) {
1133 super.draw(canvas);
1134 if (getPageCount() > 0) {
1135 if (!mEdgeGlowLeft.isFinished()) {
1136 final int restoreCount = canvas.save();
1137 Rect display = mViewport;
1138 canvas.translate(display.left, display.top);
1139 canvas.rotate(270);
1140
1141 getEdgeVerticalPostion(sTmpIntPoint);
1142 canvas.translate(display.top - sTmpIntPoint[1], 0);
1143 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1144 if (mEdgeGlowLeft.draw(canvas)) {
1145 postInvalidateOnAnimation();
1146 }
1147 canvas.restoreToCount(restoreCount);
1148 }
1149 if (!mEdgeGlowRight.isFinished()) {
1150 final int restoreCount = canvas.save();
1151 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001152 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001153 canvas.rotate(90);
1154
1155 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001156
Sunny Goyal93264612015-11-23 11:47:50 -08001157 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1158 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001159 if (mEdgeGlowRight.draw(canvas)) {
1160 postInvalidateOnAnimation();
1161 }
1162 canvas.restoreToCount(restoreCount);
1163 }
1164 }
1165 }
1166
1167 /**
1168 * Returns the top and bottom position for the edge effect.
1169 */
1170 protected abstract void getEdgeVerticalPostion(int[] pos);
1171
1172 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001174 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001175 if (page != mCurrentPage || !mScroller.isFinished()) {
1176 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 return true;
1178 }
1179 return false;
1180 }
1181
1182 @Override
1183 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001184 int focusablePage;
1185 if (mNextPage != INVALID_PAGE) {
1186 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001188 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 }
Winson Chung86f77532010-08-24 11:08:22 -07001190 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001192 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 }
1194 return false;
1195 }
1196
1197 @Override
1198 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001199 if (super.dispatchUnhandledMove(focused, direction)) {
1200 return true;
1201 }
1202
1203 if (mIsRtl) {
1204 if (direction == View.FOCUS_LEFT) {
1205 direction = View.FOCUS_RIGHT;
1206 } else if (direction == View.FOCUS_RIGHT) {
1207 direction = View.FOCUS_LEFT;
1208 }
1209 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001211 if (getCurrentPage() > 0) {
1212 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 return true;
1214 }
1215 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001216 if (getCurrentPage() < getPageCount() - 1) {
1217 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 return true;
1219 }
1220 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001221 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223
1224 @Override
1225 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001226 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001227 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001228 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 }
1230 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001231 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001232 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 }
1234 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001235 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001236 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 }
1238 }
1239 }
1240
1241 /**
1242 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001243 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 *
Winson Chung86f77532010-08-24 11:08:22 -07001245 * This happens when live folders requery, and if they're off page, they
1246 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 */
1248 @Override
1249 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001250 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 View v = focused;
1252 while (true) {
1253 if (v == current) {
1254 super.focusableViewAvailable(focused);
1255 return;
1256 }
1257 if (v == this) {
1258 return;
1259 }
1260 ViewParent parent = v.getParent();
1261 if (parent instanceof View) {
1262 v = (View)v.getParent();
1263 } else {
1264 return;
1265 }
1266 }
1267 }
1268
1269 /**
1270 * {@inheritDoc}
1271 */
1272 @Override
1273 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1274 if (disallowIntercept) {
1275 // We need to make sure to cancel our long press if
1276 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001277 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001278 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 }
1280 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1281 }
1282
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001283 /**
1284 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1285 */
1286 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001287 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001288 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001289 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001290 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001291 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001292 }
1293
1294 /**
1295 * Return true if a tap at (x, y) should trigger a flip to the next page.
1296 */
1297 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001298 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001299 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001300 }
1301 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001302 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 }
Winson Chung52aee602013-01-30 12:01:02 -08001304
Adam Cohen7d30a372013-07-01 17:03:59 -07001305 /** Returns whether x and y originated within the buffered viewport */
1306 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001307 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001308 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001309 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001310 }
1311
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 @Override
1313 public boolean onInterceptTouchEvent(MotionEvent ev) {
1314 /*
1315 * This method JUST determines whether we want to intercept the motion.
1316 * If we return true, onTouchEvent will be called and we do the actual
1317 * scrolling there.
1318 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001319 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001320
Winson Chung45e1d6e2010-11-09 17:19:49 -08001321 // Skip touch handling if there are no pages to swipe
1322 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1323
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 /*
1325 * Shortcut the most recurring case: the user is in the dragging
1326 * state and he is moving his finger. We want to intercept this
1327 * motion.
1328 */
1329 final int action = ev.getAction();
1330 if ((action == MotionEvent.ACTION_MOVE) &&
1331 (mTouchState == TOUCH_STATE_SCROLLING)) {
1332 return true;
1333 }
1334
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 switch (action & MotionEvent.ACTION_MASK) {
1336 case MotionEvent.ACTION_MOVE: {
1337 /*
1338 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1339 * whether the user has moved far enough from his original down touch.
1340 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001341 if (mActivePointerId != INVALID_POINTER) {
1342 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001343 }
1344 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1345 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1346 // i.e. fall through to the next case (don't break)
1347 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1348 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001349 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 }
1351
1352 case MotionEvent.ACTION_DOWN: {
1353 final float x = ev.getX();
1354 final float y = ev.getY();
1355 // Remember location of down touch
1356 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 mDownMotionY = y;
1358 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 mLastMotionX = x;
1360 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001361 float[] p = mapPointFromViewToParent(this, x, y);
1362 mParentDownMotionX = p[0];
1363 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001364 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001365 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001367
Winson Chung321e9ee2010-08-09 13:37:56 -07001368 /*
1369 * If being flinged and user touches the screen, initiate drag;
1370 * otherwise don't. mScroller.isFinished should be false when
1371 * being flinged.
1372 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001373 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001374 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001375
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001376 if (finishedScrolling) {
1377 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001378 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001379 setCurrentPage(getNextPage());
1380 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001381 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001382 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001383 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1384 mTouchState = TOUCH_STATE_SCROLLING;
1385 } else {
1386 mTouchState = TOUCH_STATE_REST;
1387 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001389
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 break;
1391 }
1392
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001394 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397
1398 case MotionEvent.ACTION_POINTER_UP:
1399 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001400 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 break;
1402 }
1403
1404 /*
1405 * The only time we want to intercept motion events is if we are in the
1406 * drag mode.
1407 */
1408 return mTouchState != TOUCH_STATE_REST;
1409 }
1410
Adam Cohenf8d28232011-02-01 21:47:00 -08001411 protected void determineScrollingStart(MotionEvent ev) {
1412 determineScrollingStart(ev, 1.0f);
1413 }
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 /*
1416 * Determines if we should change the touch state to start scrolling after the
1417 * user moves their touch point too far.
1418 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001419 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001420 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 if (pointerIndex == -1) return;
1423
1424 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 final float x = ev.getX(pointerIndex);
1426 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001427 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001430
Adam Cohenf8d28232011-02-01 21:47:00 -08001431 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001433
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001434 if (xMoved) {
1435 // Scroll if the user moved far enough along the X axis
1436 mTouchState = TOUCH_STATE_SCROLLING;
1437 mTotalMotionX += Math.abs(mLastMotionX - x);
1438 mLastMotionX = x;
1439 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001440 onScrollInteractionBegin();
1441 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001442 // Stop listening for things like pinches.
1443 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001444 }
1445 }
1446
1447 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001448 // Try canceling the long press. It could also have been scheduled
1449 // by a distant descendant, so use the mAllowLongPress flag to block
1450 // everything
1451 final View currentPage = getPageAt(mCurrentPage);
1452 if (currentPage != null) {
1453 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 }
1455 }
1456
Adam Cohenb5ba0972011-09-07 18:02:31 -07001457 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001459
Adam Cohenedb40762013-07-18 16:45:45 -07001460 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001461 int count = getChildCount();
1462
1463 final int totalDistance;
1464
1465 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001466 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001467 adjacentPage = page - 1;
1468 }
1469
1470 if (adjacentPage < 0 || adjacentPage > count - 1) {
1471 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1472 } else {
1473 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1474 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001475
1476 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001477 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1478 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001479 return scrollProgress;
1480 }
1481
Adam Cohenedb40762013-07-18 16:45:45 -07001482 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001483 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001484 return 0;
1485 } else {
1486 return mPageScrolls[index];
1487 }
1488 }
1489
Adam Cohen564a2e72013-10-09 14:47:32 -07001490 // While layout transitions are occurring, a child's position may stray from its baseline
1491 // position. This method returns the magnitude of this stray at any given time.
1492 public int getLayoutTransitionOffsetForPage(int index) {
1493 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1494 return 0;
1495 } else {
1496 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001497
1498 int scrollOffset = 0;
1499 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1500 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001501 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001502 }
1503
Adam Cohen564a2e72013-10-09 14:47:32 -07001504 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1505 return (int) (child.getX() - baselineX);
1506 }
1507 }
1508
Adam Cohenb5ba0972011-09-07 18:02:31 -07001509 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001510 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001511 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001512 if (f < 0) {
1513 mEdgeGlowLeft.onPull(-f);
1514 } else if (f > 0) {
1515 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001516 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001517 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001518 }
1519 invalidate();
1520 }
1521
Adam Cohenb5ba0972011-09-07 18:02:31 -07001522 protected void overScroll(float amount) {
1523 dampedOverScroll(amount);
1524 }
1525
Winson Chungdc61c4d2015-04-20 18:26:57 -07001526 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001527 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001528 }
1529
Winson Chungdc61c4d2015-04-20 18:26:57 -07001530 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001531 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001532 }
1533
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001534 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001535 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001536 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001537 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1538 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1539 } else {
1540 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1541 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1542 }
1543 }
1544
Adam Cohenf9618852013-11-08 06:45:03 -08001545 private void setEnableFreeScroll(boolean freeScroll) {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001546 boolean wasFreeScroll = mFreeScroll;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001547 mFreeScroll = freeScroll;
1548
Adam Cohenf9618852013-11-08 06:45:03 -08001549 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001550 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001551 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001552 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1553 setCurrentPage(mTempVisiblePagesRange[0]);
1554 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1555 setCurrentPage(mTempVisiblePagesRange[1]);
1556 }
Tony Wickham8f7ead32016-04-07 18:46:44 -07001557 } else if (wasFreeScroll) {
1558 snapToPage(getNextPage());
Adam Cohenf358a4b2013-07-23 16:47:31 -07001559 }
1560
1561 setEnableOverscroll(!freeScroll);
1562 }
1563
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001564 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001565 mAllowOverScroll = enable;
1566 }
1567
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001568 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001569 if (mDragView != null) {
1570 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1571 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001572 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001573 int minDistance = Integer.MAX_VALUE;
1574 int minIndex = indexOfChild(mDragView);
1575 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1576 View page = getPageAt(i);
1577 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1578 int d = Math.abs(dragX - pageX);
1579 if (d < minDistance) {
1580 minIndex = i;
1581 minDistance = d;
1582 }
1583 }
1584 return minIndex;
1585 }
1586 return -1;
1587 }
1588
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 @Override
1590 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001591 super.onTouchEvent(ev);
1592
Winson Chung45e1d6e2010-11-09 17:19:49 -08001593 // Skip touch handling if there are no pages to swipe
1594 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1595
Michael Jurkab8f06722010-10-10 15:58:46 -07001596 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001597
1598 final int action = ev.getAction();
1599
1600 switch (action & MotionEvent.ACTION_MASK) {
1601 case MotionEvent.ACTION_DOWN:
1602 /*
1603 * If being flinged and user touches, stop the fling. isFinished
1604 * will be false if being flinged.
1605 */
1606 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001607 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001608 }
1609
1610 // Remember where the motion event started
1611 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001612 mDownMotionY = mLastMotionY = ev.getY();
1613 mDownScrollX = getScrollX();
1614 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1615 mParentDownMotionX = p[0];
1616 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001617 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001618 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001619 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001620
Michael Jurka0142d492010-08-25 17:46:15 -07001621 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001622 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001623 pageBeginMoving();
1624 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001625 break;
1626
1627 case MotionEvent.ACTION_MOVE:
1628 if (mTouchState == TOUCH_STATE_SCROLLING) {
1629 // Scroll to follow the motion event
1630 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001631
1632 if (pointerIndex == -1) return true;
1633
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001635 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001636
Adam Cohenaefd4e12011-02-14 16:39:38 -08001637 mTotalMotionX += Math.abs(deltaX);
1638
Winson Chungc0844aa2011-02-02 15:25:58 -08001639 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1640 // keep the remainder because we are actually testing if we've moved from the last
1641 // scrolled position (which is discrete).
1642 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001643 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001644 mLastMotionX = x;
1645 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001646 } else {
1647 awakenScrollBars();
1648 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001649 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1650 // Update the last motion position
1651 mLastMotionX = ev.getX();
1652 mLastMotionY = ev.getY();
1653
1654 // Update the parent down so that our zoom animations take this new movement into
1655 // account
1656 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1657 mParentDownMotionX = pt[0];
1658 mParentDownMotionY = pt[1];
1659 updateDragViewTranslationDuringDrag();
1660
1661 // Find the closest page to the touch point
1662 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001663
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1665 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1666 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1667 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1668
Adam Cohenf358a4b2013-07-23 16:47:31 -07001669 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001670 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001671 mTempVisiblePagesRange[0] = 0;
1672 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001673 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001674 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1675 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1676 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1677 mSidePageHoverIndex = pageUnderPointIndex;
1678 mSidePageHoverRunnable = new Runnable() {
1679 @Override
1680 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001681 // Setup the scroll to the correct page before we swap the views
1682 snapToPage(pageUnderPointIndex);
1683
1684 // For each of the pages between the paged view and the drag view,
1685 // animate them from the previous position to the new position in
1686 // the layout (as a result of the drag view moving in the layout)
1687 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1688 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1689 dragViewIndex + 1 : pageUnderPointIndex;
1690 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1691 dragViewIndex - 1 : pageUnderPointIndex;
1692 for (int i = lowerIndex; i <= upperIndex; ++i) {
1693 View v = getChildAt(i);
1694 // dragViewIndex < pageUnderPointIndex, so after we remove the
1695 // drag view all subsequent views to pageUnderPointIndex will
1696 // shift down.
1697 int oldX = getViewportOffsetX() + getChildOffset(i);
1698 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1699
1700 // Animate the view translation from its old position to its new
1701 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001702 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001703 if (anim != null) {
1704 anim.cancel();
1705 }
1706
1707 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001708 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001710 anim.start();
1711 v.setTag(anim);
1712 }
1713
1714 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001715 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001717 if (mPageIndicator != null) {
1718 mPageIndicator.setActiveMarker(getNextPage());
1719 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001720 }
1721 };
1722 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1723 }
1724 } else {
1725 removeCallbacks(mSidePageHoverRunnable);
1726 mSidePageHoverIndex = -1;
1727 }
Adam Cohen564976a2010-10-13 18:52:07 -07001728 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001729 determineScrollingStart(ev);
1730 }
1731 break;
1732
1733 case MotionEvent.ACTION_UP:
1734 if (mTouchState == TOUCH_STATE_SCROLLING) {
1735 final int activePointerId = mActivePointerId;
1736 final int pointerIndex = ev.findPointerIndex(activePointerId);
1737 final float x = ev.getX(pointerIndex);
1738 final VelocityTracker velocityTracker = mVelocityTracker;
1739 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1740 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001741 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001742 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001743 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1744 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001745
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001746 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1747
Adam Cohen00481b32011-11-18 12:03:48 -08001748 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001749 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001750
Adam Cohenf358a4b2013-07-23 16:47:31 -07001751 if (!mFreeScroll) {
1752 // In the case that the page is moved far to one direction and then is flung
1753 // in the opposite direction, we use a threshold to determine whether we should
1754 // just return to the starting page, or if we should skip one further.
1755 boolean returnToOriginalPage = false;
1756 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1757 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1758 returnToOriginalPage = true;
1759 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001760
Adam Cohenf358a4b2013-07-23 16:47:31 -07001761 int finalPage;
1762 // We give flings precedence over large moves, which is why we short-circuit our
1763 // test for a large move if a fling has been registered. That is, a large
1764 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001765 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1766 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001767 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1768 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1769 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1770 snapToPageWithVelocity(finalPage, velocityX);
1771 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1772 (isFling && isVelocityXLeft)) &&
1773 mCurrentPage < getChildCount() - 1) {
1774 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1775 snapToPageWithVelocity(finalPage, velocityX);
1776 } else {
1777 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001778 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001779 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001780 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001781 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001782 }
1783
1784 float scaleX = getScaleX();
1785 int vX = (int) (-velocityX * scaleX);
1786 int initialScrollX = (int) (getScrollX() * scaleX);
1787
Adam Cohenf9618852013-11-08 06:45:03 -08001788 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001789 mScroller.fling(initialScrollX,
1790 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
Tony Wickham8f7ead32016-04-07 18:46:44 -07001791 mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX));
Adam Cohenf358a4b2013-07-23 16:47:31 -07001792 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001794 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001795 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1796 // at this point we have not moved beyond the touch slop
1797 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1798 // we can just page
1799 int nextPage = Math.max(0, mCurrentPage - 1);
1800 if (nextPage != mCurrentPage) {
1801 snapToPage(nextPage);
1802 } else {
1803 snapToDestination();
1804 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001805 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001806 // at this point we have not moved beyond the touch slop
1807 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1808 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001809 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1810 if (nextPage != mCurrentPage) {
1811 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001812 } else {
1813 snapToDestination();
1814 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1816 // Update the last motion position
1817 mLastMotionX = ev.getX();
1818 mLastMotionY = ev.getY();
1819
1820 // Update the parent down so that our zoom animations take this new movement into
1821 // account
1822 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1823 mParentDownMotionX = pt[0];
1824 mParentDownMotionY = pt[1];
1825 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001826 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001827 if (!mCancelTap) {
1828 onUnhandledTap(ev);
1829 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001830 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001831
1832 // Remove the callback to wait for the side page hover timeout
1833 removeCallbacks(mSidePageHoverRunnable);
1834 // End any intermediate reordering states
1835 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001836 break;
1837
1838 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001839 if (mTouchState == TOUCH_STATE_SCROLLING) {
1840 snapToDestination();
1841 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001842 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001843 break;
1844
1845 case MotionEvent.ACTION_POINTER_UP:
1846 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001847 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001848 break;
1849 }
1850
1851 return true;
1852 }
1853
Adam Cohen7d30a372013-07-01 17:03:59 -07001854 private void resetTouchState() {
1855 releaseVelocityTracker();
1856 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001857 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001858 mTouchState = TOUCH_STATE_REST;
1859 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001860 mEdgeGlowLeft.onRelease();
1861 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001862 }
1863
Adam Cohenc2d6e892014-10-16 09:49:24 -07001864 /**
1865 * Triggered by scrolling via touch
1866 */
1867 protected void onScrollInteractionBegin() {
1868 }
1869
1870 protected void onScrollInteractionEnd() {
1871 }
1872
Adam Cohen1697b792013-09-17 19:08:21 -07001873 protected void onUnhandledTap(MotionEvent ev) {
1874 ((Launcher) getContext()).onClick(this);
1875 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001876
Winson Chung185d7162011-02-28 13:47:29 -08001877 @Override
1878 public boolean onGenericMotionEvent(MotionEvent event) {
1879 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1880 switch (event.getAction()) {
1881 case MotionEvent.ACTION_SCROLL: {
1882 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1883 final float vscroll;
1884 final float hscroll;
1885 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1886 vscroll = 0;
1887 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1888 } else {
1889 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1890 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1891 }
1892 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001893 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001894 : (hscroll > 0 || vscroll > 0);
1895 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001896 scrollRight();
1897 } else {
1898 scrollLeft();
1899 }
1900 return true;
1901 }
1902 }
1903 }
1904 }
1905 return super.onGenericMotionEvent(event);
1906 }
1907
Michael Jurkab8f06722010-10-10 15:58:46 -07001908 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1909 if (mVelocityTracker == null) {
1910 mVelocityTracker = VelocityTracker.obtain();
1911 }
1912 mVelocityTracker.addMovement(ev);
1913 }
1914
1915 private void releaseVelocityTracker() {
1916 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001917 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001918 mVelocityTracker.recycle();
1919 mVelocityTracker = null;
1920 }
1921 }
1922
Winson Chung321e9ee2010-08-09 13:37:56 -07001923 private void onSecondaryPointerUp(MotionEvent ev) {
1924 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1925 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1926 final int pointerId = ev.getPointerId(pointerIndex);
1927 if (pointerId == mActivePointerId) {
1928 // This was our active pointer going up. Choose a new
1929 // active pointer and adjust accordingly.
1930 // TODO: Make this decision more intelligent.
1931 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1932 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1933 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001934 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 mActivePointerId = ev.getPointerId(newPointerIndex);
1936 if (mVelocityTracker != null) {
1937 mVelocityTracker.clear();
1938 }
1939 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001940 }
1941
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 @Override
1943 public void requestChildFocus(View child, View focused) {
1944 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001945 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001946 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001947 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 }
1949 }
1950
Adam Cohend19d3ca2010-09-15 14:43:42 -07001951 int getPageNearestToCenterOfScreen() {
Tony Wickham8f7ead32016-04-07 18:46:44 -07001952 return getPageNearestToCenterOfScreen(getScrollX());
1953 }
1954
1955 private int getPageNearestToCenterOfScreen(int scaledScrollX) {
1956 int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2);
Adam Cohen22f823d2011-09-01 17:22:18 -07001957 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001958 int minDistanceFromScreenCenterIndex = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 final int childCount = getChildCount();
1960 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001961 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001962 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001963 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001964 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001965 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1966 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1967 minDistanceFromScreenCenter = distanceFromScreenCenter;
1968 minDistanceFromScreenCenterIndex = i;
1969 }
1970 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001971 return minDistanceFromScreenCenterIndex;
1972 }
1973
1974 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001975 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001976 }
1977
Adam Cohene0f66b52010-11-23 15:06:07 -08001978 private static class ScrollInterpolator implements Interpolator {
1979 public ScrollInterpolator() {
1980 }
1981
1982 public float getInterpolation(float t) {
1983 t -= 1.0f;
1984 return t*t*t*t*t + 1;
1985 }
1986 }
1987
1988 // We want the duration of the page snap animation to be influenced by the distance that
1989 // the screen has to travel, however, we don't want this duration to be effected in a
1990 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1991 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001992 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001993 f -= 0.5f; // center the values about 0.
1994 f *= 0.3f * Math.PI / 2.0f;
1995 return (float) Math.sin(f);
1996 }
1997
Michael Jurka0142d492010-08-25 17:46:15 -07001998 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001999 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002000 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002001
Adam Cohenedb40762013-07-18 16:45:45 -07002002 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002003 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002004 int duration = 0;
2005
Sunny Goyal4d113a52015-05-27 10:05:28 -07002006 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002007 // If the velocity is low enough, then treat this more as an automatic page advance
2008 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002009 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002010 return;
2011 }
2012
2013 // Here we compute a "distance" that will be used in the computation of the overall
2014 // snap duration. This is a function of the actual distance that needs to be traveled;
2015 // we keep this value close to half screen size in order to reduce the variance in snap
2016 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002017 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002018 float distance = halfScreenSize + halfScreenSize *
2019 distanceInfluenceForSnapDuration(distanceRatio);
2020
2021 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002022 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002023
2024 // we want the page's snap velocity to approximately match the velocity at which the
2025 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002026 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2027 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002028
2029 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002030 }
2031
Sunny Goyal1740d902015-05-27 11:14:01 -07002032 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002033 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002034 }
2035
Adam Cohenf9c184a2016-01-15 16:47:43 -08002036 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002037 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 }
2039
Michael Jurka0142d492010-08-25 17:46:15 -07002040 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002041 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002042 }
2043
Adam Cohenf9618852013-11-08 06:45:03 -08002044 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2045 snapToPage(whichPage, duration, false, interpolator);
2046 }
2047
2048 protected void snapToPage(int whichPage, int duration, boolean immediate,
2049 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002050 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002051
Adam Cohenedb40762013-07-18 16:45:45 -07002052 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002053 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002054 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002055 }
2056
2057 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002058 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002059 }
Michael Jurka0142d492010-08-25 17:46:15 -07002060
Adam Cohenf9618852013-11-08 06:45:03 -08002061 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2062 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002063 whichPage = validateNewPage(whichPage);
2064
Adam Cohen7d30a372013-07-01 17:03:59 -07002065 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002066
2067 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002068 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002069 if (immediate) {
2070 duration = 0;
2071 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002072 duration = Math.abs(delta);
2073 }
2074
Adam Cohenf358a4b2013-07-23 16:47:31 -07002075 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002076 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002077 }
Adam Cohenf9618852013-11-08 06:45:03 -08002078
2079 if (interpolator != null) {
2080 mScroller.setInterpolator(interpolator);
2081 } else {
2082 mScroller.setInterpolator(mDefaultInterpolator);
2083 }
2084
Sunny Goyalc86df472016-02-25 09:19:38 -08002085 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002086
Adam Cohen674531f2013-12-13 15:07:14 -08002087 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002088
2089 // Trigger a compute() to finish switching pages if necessary
2090 if (immediate) {
2091 computeScroll();
2092 }
2093
2094 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002095 invalidate();
2096 }
2097
Winson Chung321e9ee2010-08-09 13:37:56 -07002098 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002099 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002100 }
2101
2102 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002103 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002104 }
2105
Adam Cohendbdff6b2013-09-18 19:09:15 -07002106 @Override
2107 public boolean performLongClick() {
2108 mCancelTap = true;
2109 return super.performLongClick();
2110 }
2111
Winson Chung321e9ee2010-08-09 13:37:56 -07002112 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002113 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002114
2115 SavedState(Parcelable superState) {
2116 super(superState);
2117 }
2118
Adam Cohen091440a2015-03-18 14:16:05 -07002119 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002121 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002122 }
2123
2124 @Override
2125 public void writeToParcel(Parcel out, int flags) {
2126 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002127 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002128 }
2129
2130 public static final Parcelable.Creator<SavedState> CREATOR =
2131 new Parcelable.Creator<SavedState>() {
2132 public SavedState createFromParcel(Parcel in) {
2133 return new SavedState(in);
2134 }
2135
2136 public SavedState[] newArray(int size) {
2137 return new SavedState[size];
2138 }
2139 };
2140 }
2141
Adam Cohen7d30a372013-07-01 17:03:59 -07002142 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002143 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002144 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002145 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2146 .translationX(0)
2147 .translationY(0)
2148 .scaleX(1)
2149 .scaleY(1)
2150 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002151 anim.addListener(new AnimatorListenerAdapter() {
2152 @Override
2153 public void onAnimationEnd(Animator animation) {
2154 onPostReorderingAnimationCompleted();
2155 }
2156 });
2157 anim.start();
2158 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002159 }
2160
Sunny Goyal1d08f702015-05-04 15:50:25 -07002161 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002162 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2163 mTouchState = TOUCH_STATE_REORDERING;
2164 mIsReordering = true;
2165
Adam Cohen7d30a372013-07-01 17:03:59 -07002166 // We must invalidate to trigger a redraw to update the layers such that the drag view
2167 // is always drawn on top
2168 invalidate();
2169 }
2170
Adam Cohen091440a2015-03-18 14:16:05 -07002171 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002172 // Trigger the callback when reordering has settled
2173 --mPostReorderingPreZoomInRemainingAnimationCount;
2174 if (mPostReorderingPreZoomInRunnable != null &&
2175 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2176 mPostReorderingPreZoomInRunnable.run();
2177 mPostReorderingPreZoomInRunnable = null;
2178 }
2179 }
2180
Sunny Goyal1d08f702015-05-04 15:50:25 -07002181 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002182 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002183 }
2184
Adam Cohenf358a4b2013-07-23 16:47:31 -07002185 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002186 int dragViewIndex = indexOfChild(v);
2187
Adam Cohen327acfe2014-06-06 11:52:52 -07002188 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002189
Adam Cohen7d30a372013-07-01 17:03:59 -07002190 mTempVisiblePagesRange[0] = 0;
2191 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002192 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002193 mReorderingStarted = true;
2194
2195 // Check if we are within the reordering range
2196 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002197 dragViewIndex <= mTempVisiblePagesRange[1]) {
2198 // Find the drag view under the pointer
2199 mDragView = getChildAt(dragViewIndex);
2200 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2201 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002202 snapToPage(getPageNearestToCenterOfScreen());
2203 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002204 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002205 return true;
2206 }
2207 return false;
2208 }
2209
2210 boolean isReordering(boolean testTouchState) {
2211 boolean state = mIsReordering;
2212 if (testTouchState) {
2213 state &= (mTouchState == TOUCH_STATE_REORDERING);
2214 }
2215 return state;
2216 }
2217 void endReordering() {
2218 // For simplicity, we call endReordering sometimes even if reordering was never started.
2219 // In that case, we don't want to do anything.
2220 if (!mReorderingStarted) return;
2221 mReorderingStarted = false;
2222
2223 // If we haven't flung-to-delete the current child, then we just animate the drag view
2224 // back into position
2225 final Runnable onCompleteRunnable = new Runnable() {
2226 @Override
2227 public void run() {
2228 onEndReordering();
2229 }
2230 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002231
2232 mPostReorderingPreZoomInRunnable = new Runnable() {
2233 public void run() {
2234 onCompleteRunnable.run();
2235 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002236 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002237 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002238
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002239 mPostReorderingPreZoomInRemainingAnimationCount =
2240 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2241 // Snap to the current page
2242 snapToPage(indexOfChild(mDragView), 0);
2243 // Animate the drag view back to the front position
2244 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002245 }
2246
Winson Chung6a0f57d2011-06-29 20:10:49 -07002247 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002248 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002249 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002250 @Override
2251 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2252 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002253 info.setScrollable(getPageCount() > 1);
2254 if (getCurrentPage() < getPageCount() - 1) {
2255 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2256 }
2257 if (getCurrentPage() > 0) {
2258 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2259 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002260 info.setClassName(getClass().getName());
2261
2262 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2263 // Besides disabling the accessibility long-click, this also prevents this view from getting
2264 // accessibility focus.
2265 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002266 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002267 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2268 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002269 }
2270
2271 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002272 public void sendAccessibilityEvent(int eventType) {
2273 // Don't let the view send real scroll events.
2274 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2275 super.sendAccessibilityEvent(eventType);
2276 }
2277 }
2278
2279 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002280 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2281 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002282 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002283 }
2284
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002285 @Override
2286 public boolean performAccessibilityAction(int action, Bundle arguments) {
2287 if (super.performAccessibilityAction(action, arguments)) {
2288 return true;
2289 }
2290 switch (action) {
2291 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2292 if (getCurrentPage() < getPageCount() - 1) {
2293 scrollRight();
2294 return true;
2295 }
2296 } break;
2297 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2298 if (getCurrentPage() > 0) {
2299 scrollLeft();
2300 return true;
2301 }
2302 } break;
2303 }
2304 return false;
2305 }
2306
Adam Cohen0ffac432013-07-10 11:19:26 -07002307 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002308 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002309 getNextPage() + 1, getChildCount());
2310 }
2311
Winson Chungd11265e2011-08-30 13:37:23 -07002312 @Override
2313 public boolean onHoverEvent(android.view.MotionEvent event) {
2314 return true;
2315 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002316}