blob: 2dcff35b5eb840cbc2c521d25ffa885e19cc4cbc [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;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
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;
45import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070051
52import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Sunny Goyal4d113a52015-05-27 10:05:28 -070054
Winson Chung6a0f57d2011-06-29 20:10:49 -070055import java.util.ArrayList;
56
Winson Chung321e9ee2010-08-09 13:37:56 -070057/**
58 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070059 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070060 */
Michael Jurka8b805b12012-04-18 14:23:14 -070061public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070062 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070063 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Winson Chung86f77532010-08-24 11:08:22 -070066 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070067 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Adam Cohen7d30a372013-07-01 17:03:59 -070069 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070070 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070071 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen21cd0022013-10-09 18:57:02 -070085 public static final int INVALID_RESTORE_PAGE = -1001;
86
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private boolean mFreeScroll = false;
88 private int mFreeScrollMinScrollX = -1;
89 private int mFreeScrollMaxScrollX = -1;
90
Adam Cohen265b9a62011-12-07 14:37:18 -080091 protected int mFlingThresholdVelocity;
92 protected int mMinFlingVelocity;
93 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070094
Adam Cohenb5ba0972011-09-07 18:02:31 -070095 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected float mSmoothingTime;
97 protected float mTouchX;
98
99 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700100 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700101
102 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700103 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700104 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700105
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800107 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800108 protected LauncherScroller mScroller;
109 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Adam Cohen7d30a372013-07-01 17:03:59 -0700113 private float mParentDownMotionX;
114 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700116 private float mDownMotionY;
117 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700118 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800119 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800120 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800121 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800122 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700123 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700124
Adam Cohendbdff6b2013-09-18 19:09:15 -0700125 private boolean mCancelTap;
126
Adam Cohenedb40762013-07-18 16:45:45 -0700127 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Michael Jurka0142d492010-08-25 17:46:15 -0700129 protected final static int TOUCH_STATE_REST = 0;
130 protected final static int TOUCH_STATE_SCROLLING = 1;
131 protected final static int TOUCH_STATE_PREV_PAGE = 2;
132 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700133 protected final static int TOUCH_STATE_REORDERING = 4;
134
Michael Jurka0142d492010-08-25 17:46:15 -0700135 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700136 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800137
Michael Jurka0142d492010-08-25 17:46:15 -0700138 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka7426c422010-11-11 15:23:47 -0800140 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700142 protected int mPageLayoutWidthGap;
143 protected int mPageLayoutHeightGap;
Winson Chung7da10252010-10-28 16:07:04 -0700144 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800145 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800146 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka5f1c5092010-09-03 14:15:02 -0700148 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149
Michael Jurka5f1c5092010-09-03 14:15:02 -0700150 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Winson Chung86f77532010-08-24 11:08:22 -0700152 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700155 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700156
Patrick Dubroy1262e362010-10-06 15:49:50 -0700157 protected boolean mIsPageMoving = false;
158
Adam Cohenc2d6e892014-10-16 09:49:24 -0700159 private boolean mWasInOverscroll = false;
160
Winson Chungd2be3812013-07-16 11:11:32 -0700161 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700162 @Thunk int mPageIndicatorViewId;
163 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700164 // The viewport whether the pages are to be contained (the actual view may be larger than the
165 // viewport)
166 private Rect mViewport = new Rect();
167
168 // Reordering
169 // We use the min scale to determine how much to expand the actually PagedView measured
170 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700171 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700172 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700173 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
174
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700176 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700177 protected View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700180 // This variable's scope is only for the duration of startReordering() and endReordering()
181 private boolean mReorderingStarted = false;
182 // This variable's scope is for the duration of startReordering() and after the zoomIn()
183 // animation after endReordering()
184 private boolean mIsReordering;
185 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
186 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
187 private int mPostReorderingPreZoomInRemainingAnimationCount;
188 private Runnable mPostReorderingPreZoomInRunnable;
189
Adam Cohen7d30a372013-07-01 17:03:59 -0700190 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700191 private static final Matrix sTmpInvMatrix = new Matrix();
192 private static final float[] sTmpPoint = new float[2];
193 private static final int[] sTmpIntPoint = new int[2];
194 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800195 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700196
John Spurlock77e1f472013-09-11 10:09:51 -0400197 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700198 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400199
Sunny Goyal4d113a52015-05-27 10:05:28 -0700200 // Edge effect
201 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
202 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
203
Winson Chung86f77532010-08-24 11:08:22 -0700204 public interface PageSwitchListener {
205 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 public PagedView(Context context) {
209 this(context, null);
210 }
211
212 public PagedView(Context context, AttributeSet attrs) {
213 this(context, attrs, 0);
214 }
215
216 public PagedView(Context context, AttributeSet attrs, int defStyle) {
217 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700218
Adam Cohen9c4949e2010-10-05 12:27:22 -0700219 TypedArray a = context.obtainStyledAttributes(attrs,
220 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700221
Winson Chungdf4b83d2010-10-20 17:49:27 -0700222 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700223 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700224 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700225 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700226 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700227 a.recycle();
228
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700230 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700231 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 }
233
234 /**
235 * Initializes various states for this workspace.
236 */
Michael Jurka0142d492010-08-25 17:46:15 -0700237 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800238 mScroller = new LauncherScroller(getContext());
239 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700240 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700241 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700242
243 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700244 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700246 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800247
248 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
249 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
250 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700251 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700252 setWillNotDraw(false);
253 }
254
255 protected void setEdgeGlowColor(int color) {
256 mEdgeGlowLeft.setColor(color);
257 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 }
259
Adam Cohenf9618852013-11-08 06:45:03 -0800260 protected void setDefaultInterpolator(Interpolator interpolator) {
261 mDefaultInterpolator = interpolator;
262 mScroller.setInterpolator(mDefaultInterpolator);
263 }
264
Winson Chungd2be3812013-07-16 11:11:32 -0700265 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700266 super.onAttachedToWindow();
267
Winson Chungd2be3812013-07-16 11:11:32 -0700268 // Hook up the page indicator
269 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700270 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700271 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700272 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700273 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700274
Winson Chung7819a562013-09-19 15:55:45 -0700275 ArrayList<PageIndicator.PageMarkerResources> markers =
276 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700277 for (int i = 0; i < getChildCount(); ++i) {
278 markers.add(getPageIndicatorMarker(i));
279 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700280
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700281 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700282
283 OnClickListener listener = getPageIndicatorClickListener();
284 if (listener != null) {
285 mPageIndicator.setOnClickListener(listener);
286 }
Adam Cohen53805212013-10-01 10:39:23 -0700287 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700288 }
289 }
290
Adam Cohen53805212013-10-01 10:39:23 -0700291 protected String getPageIndicatorDescription() {
292 return getCurrentPageDescription();
293 }
294
295 protected OnClickListener getPageIndicatorClickListener() {
296 return null;
297 }
298
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700299 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700300 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700301 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700302 // Unhook the page indicator
303 mPageIndicator = null;
304 }
305
Adam Cohen7d30a372013-07-01 17:03:59 -0700306 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700307 private float[] mapPointFromViewToParent(View v, float x, float y) {
308 sTmpPoint[0] = x;
309 sTmpPoint[1] = y;
310 v.getMatrix().mapPoints(sTmpPoint);
311 sTmpPoint[0] += v.getLeft();
312 sTmpPoint[1] += v.getTop();
313 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700314 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700315 private float[] mapPointFromParentToView(View v, float x, float y) {
316 sTmpPoint[0] = x - v.getLeft();
317 sTmpPoint[1] = y - v.getTop();
318 v.getMatrix().invert(sTmpInvMatrix);
319 sTmpInvMatrix.mapPoints(sTmpPoint);
320 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700321 }
322
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700323 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700324 if (mDragView != null) {
325 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
326 (mDragViewBaselineLeft - mDragView.getLeft());
327 float y = mLastMotionY - mDownMotionY;
328 mDragView.setTranslationX(x);
329 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700330
Adam Cohenf358a4b2013-07-23 16:47:31 -0700331 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
332 + x + ", " + y);
333 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700334 }
335
336 public void setMinScale(float f) {
337 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700338 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700339 requestLayout();
340 }
341
342 @Override
343 public void setScaleX(float scaleX) {
344 super.setScaleX(scaleX);
345 if (isReordering(true)) {
346 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
347 mLastMotionX = p[0];
348 mLastMotionY = p[1];
349 updateDragViewTranslationDuringDrag();
350 }
351 }
352
353 // Convenience methods to get the actual width/height of the PagedView (since it is measured
354 // to be larger to account for the minimum possible scale)
355 int getViewportWidth() {
356 return mViewport.width();
357 }
358 int getViewportHeight() {
359 return mViewport.height();
360 }
361
362 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
363 // PagedView both horizontally and vertically
364 int getViewportOffsetX() {
365 return (getMeasuredWidth() - getViewportWidth()) / 2;
366 }
367
368 int getViewportOffsetY() {
369 return (getMeasuredHeight() - getViewportHeight()) / 2;
370 }
371
Adam Cohenedb40762013-07-18 16:45:45 -0700372 PageIndicator getPageIndicator() {
373 return mPageIndicator;
374 }
Winson Chung7819a562013-09-19 15:55:45 -0700375 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
376 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700377 }
Adam Cohenedb40762013-07-18 16:45:45 -0700378
Adam Cohen674531f2013-12-13 15:07:14 -0800379 /**
380 * Add a page change listener which will be called when a page is _finished_ listening.
381 *
382 */
Winson Chung86f77532010-08-24 11:08:22 -0700383 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
384 mPageSwitchListener = pageSwitchListener;
385 if (mPageSwitchListener != null) {
386 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 }
388 }
389
390 /**
Winson Chung86f77532010-08-24 11:08:22 -0700391 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700393 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700394 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700396
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700397 /**
398 * Returns the index of page to be shown immediately afterwards.
399 */
Winson Chung360e63f2012-04-27 13:48:05 -0700400 int getNextPage() {
401 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
402 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700403
Winson Chung86f77532010-08-24 11:08:22 -0700404 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700405 return getChildCount();
406 }
407
Sunny Goyal83a8f042015-05-19 12:52:12 -0700408 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700409 return getChildAt(index);
410 }
411
Adam Cohenae4f1552011-10-20 00:15:42 -0700412 protected int indexToPage(int index) {
413 return index;
414 }
415
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800417 * Updates the scroll of the current page immediately to its final scroll position. We use this
418 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
419 * the previous tab page.
420 */
421 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700422 // If the current page is invalid, just reset the scroll position to zero
423 int newX = 0;
424 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700425 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700426 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800427 scrollTo(newX, 0);
428 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700429 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800430 }
431
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700432 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700433 mScroller.abortAnimation();
434 // We need to clean up the next page here to avoid computeScrollHelper from
435 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700436 if (resetNextPage) {
437 mNextPage = INVALID_PAGE;
438 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700439 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700440
441 private void forceFinishScroller() {
442 mScroller.forceFinished(true);
443 // We need to clean up the next page here to avoid computeScrollHelper from
444 // updating current page on the pass.
445 mNextPage = INVALID_PAGE;
446 }
447
Adam Cohen6f127a62014-06-12 14:54:41 -0700448 private int validateNewPage(int newPage) {
449 int validatedPage = newPage;
450 // When in free scroll mode, we need to clamp to the free scroll page range.
451 if (mFreeScroll) {
452 getFreeScrollPageRange(mTempVisiblePagesRange);
453 validatedPage = Math.max(mTempVisiblePagesRange[0],
454 Math.min(newPage, mTempVisiblePagesRange[1]));
455 }
456 // Ensure that it is clamped by the actual set of children in all cases
457 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
458 return validatedPage;
459 }
460
Chet Haasebc2f0822012-10-26 17:59:53 -0700461 /**
Winson Chung86f77532010-08-24 11:08:22 -0700462 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700464 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800465 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700466 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800467 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800468 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
469 // the default
470 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800471 return;
472 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700473 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700474 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700475 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700476 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800477 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479
Winson Chung8c87cd82013-07-23 16:20:10 -0700480 /**
481 * The restore page will be set in place of the current page at the next (likely first)
482 * layout.
483 */
484 void setRestorePage(int restorePage) {
485 mRestorePage = restorePage;
486 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800487 int getRestorePage() {
488 return mRestorePage;
489 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700490
Adam Cohen674531f2013-12-13 15:07:14 -0800491 /**
492 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
493 * has settled.
494 */
Michael Jurka0142d492010-08-25 17:46:15 -0700495 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700496 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800497 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
Winson Chungd2be3812013-07-16 11:11:32 -0700499
Adam Cohen674531f2013-12-13 15:07:14 -0800500 updatePageIndicator();
501 }
502
503 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700504 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700505 if (mPageIndicator != null) {
506 mPageIndicator.setContentDescription(getPageIndicatorDescription());
507 if (!isReordering(false)) {
508 mPageIndicator.setActiveMarker(getNextPage());
509 }
Winson Chungd2be3812013-07-16 11:11:32 -0700510 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800512 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700513 if (!mIsPageMoving) {
514 mIsPageMoving = true;
515 onPageBeginMoving();
516 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700517 }
518
Michael Jurkace7e05f2011-02-01 22:02:35 -0800519 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700520 if (mIsPageMoving) {
521 mIsPageMoving = false;
522 onPageEndMoving();
523 }
Michael Jurka0142d492010-08-25 17:46:15 -0700524 }
525
Adam Cohen26976d92011-03-22 15:33:33 -0700526 protected boolean isPageMoving() {
527 return mIsPageMoving;
528 }
529
Michael Jurka0142d492010-08-25 17:46:15 -0700530 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700531 protected void onPageBeginMoving() {
532 }
533
534 // a method that subclasses can override to add behavior
535 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700536 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700537 }
538
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 /**
Winson Chung86f77532010-08-24 11:08:22 -0700540 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 *
542 * @param l The listener used to respond to long clicks.
543 */
544 @Override
545 public void setOnLongClickListener(OnLongClickListener l) {
546 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700547 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700549 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 }
Adam Cohen1697b792013-09-17 19:08:21 -0700551 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
553
554 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800555 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700556 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800557 }
558
559 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700560 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700561 // In free scroll mode, we clamp the scrollX
562 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700563 // If the scroller is trying to move to a location beyond the maximum allowed
564 // in the free scroll mode, we make sure to end the scroll operation.
565 if (!mScroller.isFinished() &&
566 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
567 forceFinishScroller();
568 }
569
Adam Cohenf358a4b2013-07-23 16:47:31 -0700570 x = Math.min(x, mFreeScrollMaxScrollX);
571 x = Math.max(x, mFreeScrollMinScrollX);
572 }
573
Sunny Goyal70660032015-05-14 00:07:08 -0700574 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
575 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700576 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700577 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800578 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700579 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700580 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700581 overScroll(x - mMaxScrollX);
582 } else {
583 overScroll(x);
584 }
Adam Cohen68d73932010-11-15 10:50:58 -0800585 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700586 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700587 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800588 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700589 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700590 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700591 overScroll(x);
592 } else {
593 overScroll(x - mMaxScrollX);
594 }
Adam Cohen68d73932010-11-15 10:50:58 -0800595 }
596 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700597 if (mWasInOverscroll) {
598 overScroll(0);
599 mWasInOverscroll = false;
600 }
Adam Cohen68d73932010-11-15 10:50:58 -0800601 super.scrollTo(x, y);
602 }
603
Michael Jurka0142d492010-08-25 17:46:15 -0700604 mTouchX = x;
605 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700606
607 // Update the last motion events when scrolling
608 if (isReordering(true)) {
609 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
610 mLastMotionX = p[0];
611 mLastMotionY = p[1];
612 updateDragViewTranslationDuringDrag();
613 }
Michael Jurka0142d492010-08-25 17:46:15 -0700614 }
615
Adam Cohen53805212013-10-01 10:39:23 -0700616 private void sendScrollAccessibilityEvent() {
617 AccessibilityManager am =
618 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
619 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700620 if (mCurrentPage != getNextPage()) {
621 AccessibilityEvent ev =
622 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700623 ev.setScrollable(true);
624 ev.setScrollX(getScrollX());
625 ev.setScrollY(getScrollY());
626 ev.setMaxScrollX(mMaxScrollX);
627 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700628
Vadim Tryshevf4715972015-05-08 17:21:03 -0700629 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700630 }
Adam Cohen53805212013-10-01 10:39:23 -0700631 }
632 }
633
Michael Jurka0142d492010-08-25 17:46:15 -0700634 // we moved this functionality to a helper function so SmoothPagedView can reuse it
635 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700637 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700639 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700640 float scaleX = mFreeScroll ? getScaleX() : 1f;
641 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
642 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700643 }
Michael Jurka0142d492010-08-25 17:46:15 -0700644 invalidate();
645 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700646 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700647 sendScrollAccessibilityEvent();
648
Adam Cohen6f127a62014-06-12 14:54:41 -0700649 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700650 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700651 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700652
Adam Cohen73aa9752010-11-24 16:26:58 -0800653 // We don't want to trigger a page end moving unless the page has settled
654 // and the user has stopped scrolling
655 if (mTouchState == TOUCH_STATE_REST) {
656 pageEndMoving();
657 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700658
Adam Cohen7d30a372013-07-01 17:03:59 -0700659 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700660 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700661 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700662 if (am.isEnabled()) {
663 // Notify the user when the page changes
664 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700665 }
Michael Jurka0142d492010-08-25 17:46:15 -0700666 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
Michael Jurka0142d492010-08-25 17:46:15 -0700668 return false;
669 }
670
671 @Override
672 public void computeScroll() {
673 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 }
675
Adam Cohen96d30a12013-07-16 18:13:21 -0700676 public static class LayoutParams extends ViewGroup.LayoutParams {
677 public boolean isFullScreenPage = false;
678
679 /**
680 * {@inheritDoc}
681 */
682 public LayoutParams(int width, int height) {
683 super(width, height);
684 }
685
Sunny Goyal41b22c02015-05-14 15:20:48 -0700686 public LayoutParams(Context context, AttributeSet attrs) {
687 super(context, attrs);
688 }
689
Adam Cohen96d30a12013-07-16 18:13:21 -0700690 public LayoutParams(ViewGroup.LayoutParams source) {
691 super(source);
692 }
693 }
694
Sunny Goyal41b22c02015-05-14 15:20:48 -0700695 @Override
696 public LayoutParams generateLayoutParams(AttributeSet attrs) {
697 return new LayoutParams(getContext(), attrs);
698 }
699
700 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700701 protected LayoutParams generateDefaultLayoutParams() {
702 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
703 }
704
Sunny Goyal41b22c02015-05-14 15:20:48 -0700705 @Override
706 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
707 return new LayoutParams(p);
708 }
709
710 @Override
711 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
712 return p instanceof LayoutParams;
713 }
714
Adam Cohenedb40762013-07-18 16:45:45 -0700715 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700716 LayoutParams lp = generateDefaultLayoutParams();
717 lp.isFullScreenPage = true;
718 super.addView(page, 0, lp);
719 }
720
Adam Cohen410f3cd2013-09-22 12:09:32 -0700721 public int getNormalChildHeight() {
722 return mNormalChildHeight;
723 }
724
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 @Override
726 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700727 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700728 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
729 return;
730 }
731
Adam Cohen7d30a372013-07-01 17:03:59 -0700732 // We measure the dimensions of the PagedView to be larger than the pages so that when we
733 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700734 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
735 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
736 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700737 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800738 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700739 // viewport, we can be at most one and a half screens offset once we scale down
740 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700741 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
742 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700743
Winson Chungc82d2622013-11-06 13:23:29 -0800744 int parentWidthSize = (int) (2f * maxSize);
745 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700746 int scaledWidthSize, scaledHeightSize;
747 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700748 scaledWidthSize = (int) (parentWidthSize / mMinScale);
749 scaledHeightSize = (int) (parentHeightSize / mMinScale);
750 } else {
751 scaledWidthSize = widthSize;
752 scaledHeightSize = heightSize;
753 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700754 mViewport.set(0, 0, widthSize, heightSize);
755
756 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
757 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
758 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760
Winson Chung8aad6102012-05-11 16:27:49 -0700761 // Return early if we aren't given a proper dimension
762 if (widthSize <= 0 || heightSize <= 0) {
763 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
764 return;
765 }
766
Adam Lesinski6b879f02010-11-04 16:15:23 -0700767 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
768 * of the All apps view on XLarge displays to not take up more space then it needs. Width
769 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
770 * each page to have the same width.
771 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 final int verticalPadding = getPaddingTop() + getPaddingBottom();
773 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700774
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700775 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700777 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700778 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700779 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
780 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
781 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
782 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 final int childCount = getChildCount();
784 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700785 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700786 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100787 if (child.getVisibility() != GONE) {
788 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700789
Vladimir Marko2824b072013-10-04 16:42:17 +0100790 int childWidthMode;
791 int childHeightMode;
792 int childWidth;
793 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700794
Vladimir Marko2824b072013-10-04 16:42:17 +0100795 if (!lp.isFullScreenPage) {
796 if (lp.width == LayoutParams.WRAP_CONTENT) {
797 childWidthMode = MeasureSpec.AT_MOST;
798 } else {
799 childWidthMode = MeasureSpec.EXACTLY;
800 }
801
802 if (lp.height == LayoutParams.WRAP_CONTENT) {
803 childHeightMode = MeasureSpec.AT_MOST;
804 } else {
805 childHeightMode = MeasureSpec.EXACTLY;
806 }
807
Adam Cohen3b185e22013-10-29 14:45:58 -0700808 childWidth = getViewportWidth() - horizontalPadding
809 - mInsets.left - mInsets.right;
810 childHeight = getViewportHeight() - verticalPadding
811 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100812 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700813 } else {
814 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700815 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100816
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800817 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700818 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700819 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700820 if (referenceChildWidth == 0) {
821 referenceChildWidth = childWidth;
822 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700823
Vladimir Marko2824b072013-10-04 16:42:17 +0100824 final int childWidthMeasureSpec =
825 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
826 final int childHeightMeasureSpec =
827 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
828 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700829 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700830 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700831 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800832 }
833
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700835 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700836 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700837 return;
838 }
839
Winson Chung785d2eb2011-04-14 16:08:02 -0700840 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700842
Adam Cohen7d30a372013-07-01 17:03:59 -0700843 int offsetX = getViewportOffsetX();
844 int offsetY = getViewportOffsetY();
845
846 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700847 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700848
Sunny Goyal70660032015-05-14 00:07:08 -0700849 final int startIndex = mIsRtl ? childCount - 1 : 0;
850 final int endIndex = mIsRtl ? -1 : childCount;
851 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700852
Adam Cohen7d30a372013-07-01 17:03:59 -0700853 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700854
Adam Cohen3b185e22013-10-29 14:45:58 -0700855 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000856 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700857
858 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700859 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
860 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700861 }
862
Adam Cohen0ffac432013-07-10 11:19:26 -0700863 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700864 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700866 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100867 int childTop;
868 if (lp.isFullScreenPage) {
869 childTop = offsetY;
870 } else {
871 childTop = offsetY + getPaddingTop() + mInsets.top;
872 if (mCenterPagesVertically) {
873 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
874 }
875 }
876
Adam Cohen96d30a12013-07-16 18:13:21 -0700877 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700878 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800879
Winson Chung785d2eb2011-04-14 16:08:02 -0700880 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700881 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800882 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700883
Adam Cohen3b185e22013-10-29 14:45:58 -0700884 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
885 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000886
887 int pageGap = mPageSpacing;
888 int next = i + delta;
889 if (next != endIndex) {
890 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
891 } else {
892 nextLp = null;
893 }
894
895 // Prevent full screen pages from showing in the viewport
896 // when they are not the current page.
897 if (lp.isFullScreenPage) {
898 pageGap = getPaddingLeft();
899 } else if (nextLp != null && nextLp.isFullScreenPage) {
900 pageGap = getPaddingRight();
901 }
902
Sunny Goyala1fbd842015-05-20 13:40:27 -0700903 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 }
905 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700906
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700907 final LayoutTransition transition = getLayoutTransition();
908 // If the transition is running defer updating max scroll, as some empty pages could
909 // still be present, and a max scroll change could cause sudden jumps in scroll.
910 if (transition != null && transition.isRunning()) {
911 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
912
913 @Override
914 public void startTransition(LayoutTransition transition, ViewGroup container,
915 View view, int transitionType) { }
916
917 @Override
918 public void endTransition(LayoutTransition transition, ViewGroup container,
919 View view, int transitionType) {
920 // Wait until all transitions are complete.
921 if (!transition.isRunning()) {
922 transition.removeTransitionListener(this);
923 updateMaxScrollX();
924 }
925 }
926 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700927 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700928 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700929 }
930
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700931 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
932 updateCurrentPageScroll();
933 mFirstLayout = false;
934 }
935
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700936 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700937 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700938 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700939 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700940 } else {
941 setCurrentPage(getNextPage());
942 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700943 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700944 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700945
946 if (isReordering(true)) {
947 updateDragViewTranslationDuringDrag();
948 }
Winson Chunge3193b92010-09-10 11:44:42 -0700949 }
950
Sunny Goyala1fbd842015-05-20 13:40:27 -0700951 protected int getChildGap() {
952 return 0;
953 }
954
Sunny Goyal316490e2015-06-02 09:38:28 -0700955 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700956 int childCount = getChildCount();
957 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700958 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700959 mMaxScrollX = getScrollForPage(index);
960 } else {
961 mMaxScrollX = 0;
962 }
963 }
964
Adam Cohen3b185e22013-10-29 14:45:58 -0700965 public void setPageSpacing(int pageSpacing) {
966 mPageSpacing = pageSpacing;
967 requestLayout();
968 }
969
Sunny Goyal4d113a52015-05-27 10:05:28 -0700970 /**
971 * Called when the center screen changes during scrolling.
972 */
973 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700974
Winson Chunge3193b92010-09-10 11:44:42 -0700975 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700976 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700977 // Update the page indicator, we don't update the page indicator as we
978 // add/remove pages
979 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700980 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700981 mPageIndicator.addMarker(pageIndex,
982 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700983 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700984 }
985
Adam Cohen2591f6a2011-10-25 14:36:40 -0700986 // This ensures that when children are added, they get the correct transforms / alphas
987 // in accordance with any scroll effects.
988 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700989 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700990 invalidate();
991 }
992
Michael Jurka8b805b12012-04-18 14:23:14 -0700993 @Override
994 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700995 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700996 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700997 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 }
999
Winson Chungd2be3812013-07-16 11:11:32 -07001000 private void removeMarkerForView(int index) {
1001 // Update the page indicator, we don't update the page indicator as we
1002 // add/remove pages
1003 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001004 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001005 }
1006 }
1007
1008 @Override
1009 public void removeView(View v) {
1010 // XXX: We should find a better way to hook into this before the view
1011 // gets removed form its parent...
1012 removeMarkerForView(indexOfChild(v));
1013 super.removeView(v);
1014 }
1015 @Override
1016 public void removeViewInLayout(View v) {
1017 // XXX: We should find a better way to hook into this before the view
1018 // gets removed form its parent...
1019 removeMarkerForView(indexOfChild(v));
1020 super.removeViewInLayout(v);
1021 }
1022 @Override
1023 public void removeViewAt(int index) {
1024 // XXX: We should find a better way to hook into this before the view
1025 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001026 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001027 super.removeViewAt(index);
1028 }
1029 @Override
1030 public void removeAllViewsInLayout() {
1031 // Update the page indicator, we don't update the page indicator as we
1032 // add/remove pages
1033 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001034 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001035 }
1036
1037 super.removeAllViewsInLayout();
1038 }
1039
Adam Cohen73894962011-10-31 13:17:17 -07001040 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001041 if (index < 0 || index > getChildCount() - 1) return 0;
1042
Adam Cohenf698c6e2013-07-17 18:44:25 -07001043 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001044
Adam Cohenf698c6e2013-07-17 18:44:25 -07001045 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001046 }
1047
Adam Cohen6f127a62014-06-12 14:54:41 -07001048 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001049 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001050 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001051 }
1052
Michael Jurkadde558b2011-11-09 22:09:06 -08001053 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001054 final int pageCount = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001055 range[0] = -1;
1056 range[1] = -1;
1057
Michael Jurkac4fb9172010-09-02 17:19:20 -07001058 if (pageCount > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001059 final int visibleLeft = -getLeft();
1060 final int visibleRight = visibleLeft + getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001061 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001062
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001063 int count = getChildCount();
1064 for (int i = 0; i < count; i++) {
1065 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001066
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001067 // Verify if the page bounds are within the visible range.
1068 sTmpRectF.left = 0;
1069 sTmpRectF.right = currPage.getMeasuredWidth();
1070 currPage.getMatrix().mapRect(sTmpRectF);
1071 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
1072 getMatrix().mapRect(sTmpRectF);
1073
1074 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001075 if (range[0] == -1) {
1076 continue;
1077 } else {
1078 break;
1079 }
1080 }
1081
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001082 curScreen = i;
1083 if (range[0] < 0) {
1084 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001085 }
1086 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001087
1088 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001089 } else {
1090 range[0] = -1;
1091 range[1] = -1;
1092 }
1093 }
1094
Michael Jurka920d7f42012-05-14 16:29:55 -07001095 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001096 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001097 }
1098
Michael Jurkadde558b2011-11-09 22:09:06 -08001099 @Override
1100 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001101 // Find out which screens are visible; as an optimization we only call draw on them
1102 final int pageCount = getChildCount();
1103 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001104 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001105 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001106
1107 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1108 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1109 // set it for the next frame
1110 mForceScreenScrolled = false;
1111 screenScrolled(screenCenter);
1112 mLastScreenCenter = screenCenter;
1113 }
1114
Michael Jurkadde558b2011-11-09 22:09:06 -08001115 getVisiblePages(mTempVisiblePagesRange);
1116 final int leftScreen = mTempVisiblePagesRange[0];
1117 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001118 if (leftScreen != -1 && rightScreen != -1) {
1119 final long drawingTime = getDrawingTime();
1120 // Clip to the bounds
1121 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001122 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1123 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001124
Adam Cohen7d30a372013-07-01 17:03:59 -07001125 // Draw all the children, leaving the drag view for last
1126 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001127 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001128 if (v == mDragView) continue;
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001129 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001130 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001131 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001132 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001133 // Draw the drag view on top (if there is one)
1134 if (mDragView != null) {
1135 drawChild(canvas, mDragView, drawingTime);
1136 }
1137
Winson Chungc6f10b92011-11-14 11:39:07 -08001138 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001139 }
Michael Jurka0142d492010-08-25 17:46:15 -07001140 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001141 }
1142
1143 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001144 public void draw(Canvas canvas) {
1145 super.draw(canvas);
1146 if (getPageCount() > 0) {
1147 if (!mEdgeGlowLeft.isFinished()) {
1148 final int restoreCount = canvas.save();
1149 Rect display = mViewport;
1150 canvas.translate(display.left, display.top);
1151 canvas.rotate(270);
1152
1153 getEdgeVerticalPostion(sTmpIntPoint);
1154 canvas.translate(display.top - sTmpIntPoint[1], 0);
1155 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1156 if (mEdgeGlowLeft.draw(canvas)) {
1157 postInvalidateOnAnimation();
1158 }
1159 canvas.restoreToCount(restoreCount);
1160 }
1161 if (!mEdgeGlowRight.isFinished()) {
1162 final int restoreCount = canvas.save();
1163 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001164 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001165 canvas.rotate(90);
1166
1167 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001168
Sunny Goyal93264612015-11-23 11:47:50 -08001169 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1170 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001171 if (mEdgeGlowRight.draw(canvas)) {
1172 postInvalidateOnAnimation();
1173 }
1174 canvas.restoreToCount(restoreCount);
1175 }
1176 }
1177 }
1178
1179 /**
1180 * Returns the top and bottom position for the edge effect.
1181 */
1182 protected abstract void getEdgeVerticalPostion(int[] pos);
1183
1184 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001186 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001187 if (page != mCurrentPage || !mScroller.isFinished()) {
1188 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 return true;
1190 }
1191 return false;
1192 }
1193
1194 @Override
1195 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001196 int focusablePage;
1197 if (mNextPage != INVALID_PAGE) {
1198 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001200 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 }
Winson Chung86f77532010-08-24 11:08:22 -07001202 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001204 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 }
1206 return false;
1207 }
1208
1209 @Override
1210 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001211 if (super.dispatchUnhandledMove(focused, direction)) {
1212 return true;
1213 }
1214
1215 if (mIsRtl) {
1216 if (direction == View.FOCUS_LEFT) {
1217 direction = View.FOCUS_RIGHT;
1218 } else if (direction == View.FOCUS_RIGHT) {
1219 direction = View.FOCUS_LEFT;
1220 }
1221 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001223 if (getCurrentPage() > 0) {
1224 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 return true;
1226 }
1227 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (getCurrentPage() < getPageCount() - 1) {
1229 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 return true;
1231 }
1232 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001233 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235
1236 @Override
1237 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001238 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001239 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001240 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 }
1242 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001243 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001244 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001247 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001248 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 }
1250 }
1251 }
1252
1253 /**
1254 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001255 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 *
Winson Chung86f77532010-08-24 11:08:22 -07001257 * This happens when live folders requery, and if they're off page, they
1258 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 */
1260 @Override
1261 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001262 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 View v = focused;
1264 while (true) {
1265 if (v == current) {
1266 super.focusableViewAvailable(focused);
1267 return;
1268 }
1269 if (v == this) {
1270 return;
1271 }
1272 ViewParent parent = v.getParent();
1273 if (parent instanceof View) {
1274 v = (View)v.getParent();
1275 } else {
1276 return;
1277 }
1278 }
1279 }
1280
1281 /**
1282 * {@inheritDoc}
1283 */
1284 @Override
1285 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1286 if (disallowIntercept) {
1287 // We need to make sure to cancel our long press if
1288 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001289 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001290 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
1292 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1293 }
1294
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001295 /**
1296 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1297 */
1298 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001299 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001300 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001301 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001302 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001303 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001304 }
1305
1306 /**
1307 * Return true if a tap at (x, y) should trigger a flip to the next page.
1308 */
1309 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001310 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001311 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001312 }
1313 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001314 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001315 }
Winson Chung52aee602013-01-30 12:01:02 -08001316
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 /** Returns whether x and y originated within the buffered viewport */
1318 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001319 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001320 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001321 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 }
1323
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 @Override
1325 public boolean onInterceptTouchEvent(MotionEvent ev) {
1326 /*
1327 * This method JUST determines whether we want to intercept the motion.
1328 * If we return true, onTouchEvent will be called and we do the actual
1329 * scrolling there.
1330 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001331 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001332
Winson Chung45e1d6e2010-11-09 17:19:49 -08001333 // Skip touch handling if there are no pages to swipe
1334 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1335
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 /*
1337 * Shortcut the most recurring case: the user is in the dragging
1338 * state and he is moving his finger. We want to intercept this
1339 * motion.
1340 */
1341 final int action = ev.getAction();
1342 if ((action == MotionEvent.ACTION_MOVE) &&
1343 (mTouchState == TOUCH_STATE_SCROLLING)) {
1344 return true;
1345 }
1346
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 switch (action & MotionEvent.ACTION_MASK) {
1348 case MotionEvent.ACTION_MOVE: {
1349 /*
1350 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1351 * whether the user has moved far enough from his original down touch.
1352 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001353 if (mActivePointerId != INVALID_POINTER) {
1354 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001355 }
1356 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1357 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1358 // i.e. fall through to the next case (don't break)
1359 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1360 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001361 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 }
1363
1364 case MotionEvent.ACTION_DOWN: {
1365 final float x = ev.getX();
1366 final float y = ev.getY();
1367 // Remember location of down touch
1368 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001369 mDownMotionY = y;
1370 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 mLastMotionX = x;
1372 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001373 float[] p = mapPointFromViewToParent(this, x, y);
1374 mParentDownMotionX = p[0];
1375 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001376 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001377 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 /*
1381 * If being flinged and user touches the screen, initiate drag;
1382 * otherwise don't. mScroller.isFinished should be false when
1383 * being flinged.
1384 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001385 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001386 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001387
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 if (finishedScrolling) {
1389 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001390 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001391 setCurrentPage(getNextPage());
1392 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001393 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001394 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1396 mTouchState = TOUCH_STATE_SCROLLING;
1397 } else {
1398 mTouchState = TOUCH_STATE_REST;
1399 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001400 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001401
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 break;
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001406 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001407 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 break;
1409
1410 case MotionEvent.ACTION_POINTER_UP:
1411 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001412 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 break;
1414 }
1415
1416 /*
1417 * The only time we want to intercept motion events is if we are in the
1418 * drag mode.
1419 */
1420 return mTouchState != TOUCH_STATE_REST;
1421 }
1422
Adam Cohenf8d28232011-02-01 21:47:00 -08001423 protected void determineScrollingStart(MotionEvent ev) {
1424 determineScrollingStart(ev, 1.0f);
1425 }
1426
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 /*
1428 * Determines if we should change the touch state to start scrolling after the
1429 * user moves their touch point too far.
1430 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001431 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001432 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001434 if (pointerIndex == -1) return;
1435
1436 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 final float x = ev.getX(pointerIndex);
1438 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1440
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001442
Adam Cohenf8d28232011-02-01 21:47:00 -08001443 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001445
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001446 if (xMoved) {
1447 // Scroll if the user moved far enough along the X axis
1448 mTouchState = TOUCH_STATE_SCROLLING;
1449 mTotalMotionX += Math.abs(mLastMotionX - x);
1450 mLastMotionX = x;
1451 mLastMotionXRemainder = 0;
1452 mTouchX = getViewportOffsetX() + getScrollX();
1453 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1454 onScrollInteractionBegin();
1455 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001456 }
1457 }
1458
1459 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001460 // Try canceling the long press. It could also have been scheduled
1461 // by a distant descendant, so use the mAllowLongPress flag to block
1462 // everything
1463 final View currentPage = getPageAt(mCurrentPage);
1464 if (currentPage != null) {
1465 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 }
1467 }
1468
Adam Cohenb5ba0972011-09-07 18:02:31 -07001469 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001470 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001471
Adam Cohenedb40762013-07-18 16:45:45 -07001472 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001473 int count = getChildCount();
1474
1475 final int totalDistance;
1476
1477 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001478 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001479 adjacentPage = page - 1;
1480 }
1481
1482 if (adjacentPage < 0 || adjacentPage > count - 1) {
1483 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1484 } else {
1485 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1486 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001487
1488 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001489 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1490 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001491 return scrollProgress;
1492 }
1493
Adam Cohenedb40762013-07-18 16:45:45 -07001494 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001495 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001496 return 0;
1497 } else {
1498 return mPageScrolls[index];
1499 }
1500 }
1501
Adam Cohen564a2e72013-10-09 14:47:32 -07001502 // While layout transitions are occurring, a child's position may stray from its baseline
1503 // position. This method returns the magnitude of this stray at any given time.
1504 public int getLayoutTransitionOffsetForPage(int index) {
1505 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1506 return 0;
1507 } else {
1508 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001509
1510 int scrollOffset = 0;
1511 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1512 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001513 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001514 }
1515
Adam Cohen564a2e72013-10-09 14:47:32 -07001516 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1517 return (int) (child.getX() - baselineX);
1518 }
1519 }
1520
Adam Cohenb5ba0972011-09-07 18:02:31 -07001521 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001522 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001523 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001524 if (f < 0) {
1525 mEdgeGlowLeft.onPull(-f);
1526 } else if (f > 0) {
1527 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001528 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001529 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001530 }
1531 invalidate();
1532 }
1533
Adam Cohenb5ba0972011-09-07 18:02:31 -07001534 protected void overScroll(float amount) {
1535 dampedOverScroll(amount);
1536 }
1537
Winson Chungdc61c4d2015-04-20 18:26:57 -07001538 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001539 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001540 }
1541
Winson Chungdc61c4d2015-04-20 18:26:57 -07001542 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001543 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001544 }
1545
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001546 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001547 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001548 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001549 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1550 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1551 } else {
1552 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1553 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1554 }
1555 }
1556
Adam Cohenf9618852013-11-08 06:45:03 -08001557 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001558 mFreeScroll = freeScroll;
1559
Adam Cohenf9618852013-11-08 06:45:03 -08001560 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001561 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001562 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001563 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1564 setCurrentPage(mTempVisiblePagesRange[0]);
1565 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1566 setCurrentPage(mTempVisiblePagesRange[1]);
1567 }
1568 }
1569
1570 setEnableOverscroll(!freeScroll);
1571 }
1572
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001573 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001574 mAllowOverScroll = enable;
1575 }
1576
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001577 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001578 if (mDragView != null) {
1579 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1580 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001581 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001582 int minDistance = Integer.MAX_VALUE;
1583 int minIndex = indexOfChild(mDragView);
1584 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1585 View page = getPageAt(i);
1586 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1587 int d = Math.abs(dragX - pageX);
1588 if (d < minDistance) {
1589 minIndex = i;
1590 minDistance = d;
1591 }
1592 }
1593 return minIndex;
1594 }
1595 return -1;
1596 }
1597
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 @Override
1599 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001600 super.onTouchEvent(ev);
1601
Winson Chung45e1d6e2010-11-09 17:19:49 -08001602 // Skip touch handling if there are no pages to swipe
1603 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1604
Michael Jurkab8f06722010-10-10 15:58:46 -07001605 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001606
1607 final int action = ev.getAction();
1608
1609 switch (action & MotionEvent.ACTION_MASK) {
1610 case MotionEvent.ACTION_DOWN:
1611 /*
1612 * If being flinged and user touches, stop the fling. isFinished
1613 * will be false if being flinged.
1614 */
1615 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001616 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 }
1618
1619 // Remember where the motion event started
1620 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001621 mDownMotionY = mLastMotionY = ev.getY();
1622 mDownScrollX = getScrollX();
1623 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1624 mParentDownMotionX = p[0];
1625 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001626 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001627 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001628 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001629
Michael Jurka0142d492010-08-25 17:46:15 -07001630 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001631 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001632 pageBeginMoving();
1633 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 break;
1635
1636 case MotionEvent.ACTION_MOVE:
1637 if (mTouchState == TOUCH_STATE_SCROLLING) {
1638 // Scroll to follow the motion event
1639 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001640
1641 if (pointerIndex == -1) return true;
1642
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001644 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001645
Adam Cohenaefd4e12011-02-14 16:39:38 -08001646 mTotalMotionX += Math.abs(deltaX);
1647
Winson Chungc0844aa2011-02-02 15:25:58 -08001648 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1649 // keep the remainder because we are actually testing if we've moved from the last
1650 // scrolled position (which is discrete).
1651 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001652 mTouchX += deltaX;
1653 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001654 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001655 mLastMotionX = x;
1656 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001657 } else {
1658 awakenScrollBars();
1659 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1661 // Update the last motion position
1662 mLastMotionX = ev.getX();
1663 mLastMotionY = ev.getY();
1664
1665 // Update the parent down so that our zoom animations take this new movement into
1666 // account
1667 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1668 mParentDownMotionX = pt[0];
1669 mParentDownMotionY = pt[1];
1670 updateDragViewTranslationDuringDrag();
1671
1672 // Find the closest page to the touch point
1673 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001674
Adam Cohen7d30a372013-07-01 17:03:59 -07001675 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1676 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1677 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1678 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1679
Adam Cohenf358a4b2013-07-23 16:47:31 -07001680 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001681 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001682 mTempVisiblePagesRange[0] = 0;
1683 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001684 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001685 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1686 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1687 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1688 mSidePageHoverIndex = pageUnderPointIndex;
1689 mSidePageHoverRunnable = new Runnable() {
1690 @Override
1691 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001692 // Setup the scroll to the correct page before we swap the views
1693 snapToPage(pageUnderPointIndex);
1694
1695 // For each of the pages between the paged view and the drag view,
1696 // animate them from the previous position to the new position in
1697 // the layout (as a result of the drag view moving in the layout)
1698 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1699 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1700 dragViewIndex + 1 : pageUnderPointIndex;
1701 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1702 dragViewIndex - 1 : pageUnderPointIndex;
1703 for (int i = lowerIndex; i <= upperIndex; ++i) {
1704 View v = getChildAt(i);
1705 // dragViewIndex < pageUnderPointIndex, so after we remove the
1706 // drag view all subsequent views to pageUnderPointIndex will
1707 // shift down.
1708 int oldX = getViewportOffsetX() + getChildOffset(i);
1709 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1710
1711 // Animate the view translation from its old position to its new
1712 // position
1713 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1714 if (anim != null) {
1715 anim.cancel();
1716 }
1717
1718 v.setTranslationX(oldX - newX);
1719 anim = new AnimatorSet();
1720 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1721 anim.playTogether(
1722 ObjectAnimator.ofFloat(v, "translationX", 0f));
1723 anim.start();
1724 v.setTag(anim);
1725 }
1726
1727 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001728 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001729 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001730 if (mPageIndicator != null) {
1731 mPageIndicator.setActiveMarker(getNextPage());
1732 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001733 }
1734 };
1735 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1736 }
1737 } else {
1738 removeCallbacks(mSidePageHoverRunnable);
1739 mSidePageHoverIndex = -1;
1740 }
Adam Cohen564976a2010-10-13 18:52:07 -07001741 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001742 determineScrollingStart(ev);
1743 }
1744 break;
1745
1746 case MotionEvent.ACTION_UP:
1747 if (mTouchState == TOUCH_STATE_SCROLLING) {
1748 final int activePointerId = mActivePointerId;
1749 final int pointerIndex = ev.findPointerIndex(activePointerId);
1750 final float x = ev.getX(pointerIndex);
1751 final VelocityTracker velocityTracker = mVelocityTracker;
1752 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1753 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001754 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001755 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001756 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1757 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001758
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001759 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1760
Adam Cohen00481b32011-11-18 12:03:48 -08001761 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001762 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001763
Adam Cohenf358a4b2013-07-23 16:47:31 -07001764 if (!mFreeScroll) {
1765 // In the case that the page is moved far to one direction and then is flung
1766 // in the opposite direction, we use a threshold to determine whether we should
1767 // just return to the starting page, or if we should skip one further.
1768 boolean returnToOriginalPage = false;
1769 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1770 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1771 returnToOriginalPage = true;
1772 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001773
Adam Cohenf358a4b2013-07-23 16:47:31 -07001774 int finalPage;
1775 // We give flings precedence over large moves, which is why we short-circuit our
1776 // test for a large move if a fling has been registered. That is, a large
1777 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001778 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1779 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001780 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1781 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1782 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1783 snapToPageWithVelocity(finalPage, velocityX);
1784 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1785 (isFling && isVelocityXLeft)) &&
1786 mCurrentPage < getChildCount() - 1) {
1787 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1788 snapToPageWithVelocity(finalPage, velocityX);
1789 } else {
1790 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001791 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001793 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001794 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001795 }
1796
1797 float scaleX = getScaleX();
1798 int vX = (int) (-velocityX * scaleX);
1799 int initialScrollX = (int) (getScrollX() * scaleX);
1800
Adam Cohenf9618852013-11-08 06:45:03 -08001801 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001802 mScroller.fling(initialScrollX,
1803 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1804 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001805 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001806 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001807 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1808 // at this point we have not moved beyond the touch slop
1809 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1810 // we can just page
1811 int nextPage = Math.max(0, mCurrentPage - 1);
1812 if (nextPage != mCurrentPage) {
1813 snapToPage(nextPage);
1814 } else {
1815 snapToDestination();
1816 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001817 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001818 // at this point we have not moved beyond the touch slop
1819 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1820 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001821 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1822 if (nextPage != mCurrentPage) {
1823 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001824 } else {
1825 snapToDestination();
1826 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001827 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1828 // Update the last motion position
1829 mLastMotionX = ev.getX();
1830 mLastMotionY = ev.getY();
1831
1832 // Update the parent down so that our zoom animations take this new movement into
1833 // account
1834 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1835 mParentDownMotionX = pt[0];
1836 mParentDownMotionY = pt[1];
1837 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001838 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001839 if (!mCancelTap) {
1840 onUnhandledTap(ev);
1841 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001842 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001843
1844 // Remove the callback to wait for the side page hover timeout
1845 removeCallbacks(mSidePageHoverRunnable);
1846 // End any intermediate reordering states
1847 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001848 break;
1849
1850 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001851 if (mTouchState == TOUCH_STATE_SCROLLING) {
1852 snapToDestination();
1853 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001854 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001855 break;
1856
1857 case MotionEvent.ACTION_POINTER_UP:
1858 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001859 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001860 break;
1861 }
1862
1863 return true;
1864 }
1865
Adam Cohen7d30a372013-07-01 17:03:59 -07001866 private void resetTouchState() {
1867 releaseVelocityTracker();
1868 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001869 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001870 mTouchState = TOUCH_STATE_REST;
1871 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001872 mEdgeGlowLeft.onRelease();
1873 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001874 }
1875
Adam Cohenc2d6e892014-10-16 09:49:24 -07001876 /**
1877 * Triggered by scrolling via touch
1878 */
1879 protected void onScrollInteractionBegin() {
1880 }
1881
1882 protected void onScrollInteractionEnd() {
1883 }
1884
Adam Cohen1697b792013-09-17 19:08:21 -07001885 protected void onUnhandledTap(MotionEvent ev) {
1886 ((Launcher) getContext()).onClick(this);
1887 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001888
Winson Chung185d7162011-02-28 13:47:29 -08001889 @Override
1890 public boolean onGenericMotionEvent(MotionEvent event) {
1891 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1892 switch (event.getAction()) {
1893 case MotionEvent.ACTION_SCROLL: {
1894 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1895 final float vscroll;
1896 final float hscroll;
1897 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1898 vscroll = 0;
1899 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1900 } else {
1901 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1902 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1903 }
1904 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001905 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001906 : (hscroll > 0 || vscroll > 0);
1907 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001908 scrollRight();
1909 } else {
1910 scrollLeft();
1911 }
1912 return true;
1913 }
1914 }
1915 }
1916 }
1917 return super.onGenericMotionEvent(event);
1918 }
1919
Michael Jurkab8f06722010-10-10 15:58:46 -07001920 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1921 if (mVelocityTracker == null) {
1922 mVelocityTracker = VelocityTracker.obtain();
1923 }
1924 mVelocityTracker.addMovement(ev);
1925 }
1926
1927 private void releaseVelocityTracker() {
1928 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001929 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001930 mVelocityTracker.recycle();
1931 mVelocityTracker = null;
1932 }
1933 }
1934
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 private void onSecondaryPointerUp(MotionEvent ev) {
1936 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1937 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1938 final int pointerId = ev.getPointerId(pointerIndex);
1939 if (pointerId == mActivePointerId) {
1940 // This was our active pointer going up. Choose a new
1941 // active pointer and adjust accordingly.
1942 // TODO: Make this decision more intelligent.
1943 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1944 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1945 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001946 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001947 mActivePointerId = ev.getPointerId(newPointerIndex);
1948 if (mVelocityTracker != null) {
1949 mVelocityTracker.clear();
1950 }
1951 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001952 }
1953
Winson Chung321e9ee2010-08-09 13:37:56 -07001954 @Override
1955 public void requestChildFocus(View child, View focused) {
1956 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001957 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001958 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001959 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001960 }
1961 }
1962
Adam Cohend19d3ca2010-09-15 14:43:42 -07001963 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001964 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001965 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001966 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001967 final int childCount = getChildCount();
1968 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001969 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001970 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001971 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001972 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001973 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1974 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1975 minDistanceFromScreenCenter = distanceFromScreenCenter;
1976 minDistanceFromScreenCenterIndex = i;
1977 }
1978 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001979 return minDistanceFromScreenCenterIndex;
1980 }
1981
1982 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001983 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 }
1985
Adam Cohene0f66b52010-11-23 15:06:07 -08001986 private static class ScrollInterpolator implements Interpolator {
1987 public ScrollInterpolator() {
1988 }
1989
1990 public float getInterpolation(float t) {
1991 t -= 1.0f;
1992 return t*t*t*t*t + 1;
1993 }
1994 }
1995
1996 // We want the duration of the page snap animation to be influenced by the distance that
1997 // the screen has to travel, however, we don't want this duration to be effected in a
1998 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1999 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07002000 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002001 f -= 0.5f; // center the values about 0.
2002 f *= 0.3f * Math.PI / 2.0f;
2003 return (float) Math.sin(f);
2004 }
2005
Michael Jurka0142d492010-08-25 17:46:15 -07002006 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002007 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002008 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002009
Adam Cohenedb40762013-07-18 16:45:45 -07002010 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002011 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002012 int duration = 0;
2013
Sunny Goyal4d113a52015-05-27 10:05:28 -07002014 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002015 // If the velocity is low enough, then treat this more as an automatic page advance
2016 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002017 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002018 return;
2019 }
2020
2021 // Here we compute a "distance" that will be used in the computation of the overall
2022 // snap duration. This is a function of the actual distance that needs to be traveled;
2023 // we keep this value close to half screen size in order to reduce the variance in snap
2024 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002025 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002026 float distance = halfScreenSize + halfScreenSize *
2027 distanceInfluenceForSnapDuration(distanceRatio);
2028
2029 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002030 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002031
2032 // we want the page's snap velocity to approximately match the velocity at which the
2033 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002034 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2035 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002036
2037 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002038 }
2039
Sunny Goyal1740d902015-05-27 11:14:01 -07002040 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002041 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002042 }
2043
Adam Cohen7d30a372013-07-01 17:03:59 -07002044 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002045 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 }
2047
Michael Jurka0142d492010-08-25 17:46:15 -07002048 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002049 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 }
2051
Adam Cohenf9618852013-11-08 06:45:03 -08002052 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2053 snapToPage(whichPage, duration, false, interpolator);
2054 }
2055
2056 protected void snapToPage(int whichPage, int duration, boolean immediate,
2057 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002058 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002059
Adam Cohenedb40762013-07-18 16:45:45 -07002060 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002061 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002062 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002063 }
2064
2065 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002066 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002067 }
Michael Jurka0142d492010-08-25 17:46:15 -07002068
Adam Cohenf9618852013-11-08 06:45:03 -08002069 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2070 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002071 whichPage = validateNewPage(whichPage);
2072
Adam Cohen7d30a372013-07-01 17:03:59 -07002073 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002074
2075 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002076 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002077 if (immediate) {
2078 duration = 0;
2079 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002080 duration = Math.abs(delta);
2081 }
2082
Adam Cohenf358a4b2013-07-23 16:47:31 -07002083 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002084 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002085 }
Adam Cohenf9618852013-11-08 06:45:03 -08002086
2087 if (interpolator != null) {
2088 mScroller.setInterpolator(interpolator);
2089 } else {
2090 mScroller.setInterpolator(mDefaultInterpolator);
2091 }
2092
Sunny Goyal4d113a52015-05-27 10:05:28 -07002093 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002094
Adam Cohen674531f2013-12-13 15:07:14 -08002095 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002096
2097 // Trigger a compute() to finish switching pages if necessary
2098 if (immediate) {
2099 computeScroll();
2100 }
2101
2102 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002103 invalidate();
2104 }
2105
Winson Chung321e9ee2010-08-09 13:37:56 -07002106 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002107 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002108 }
2109
2110 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002111 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002112 }
2113
Winson Chung86f77532010-08-24 11:08:22 -07002114 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 int result = -1;
2116 if (v != null) {
2117 ViewParent vp = v.getParent();
2118 int count = getChildCount();
2119 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002120 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002121 return i;
2122 }
2123 }
2124 }
2125 return result;
2126 }
2127
Adam Cohendbdff6b2013-09-18 19:09:15 -07002128 @Override
2129 public boolean performLongClick() {
2130 mCancelTap = true;
2131 return super.performLongClick();
2132 }
2133
Winson Chung321e9ee2010-08-09 13:37:56 -07002134 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002135 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002136
2137 SavedState(Parcelable superState) {
2138 super(superState);
2139 }
2140
Adam Cohen091440a2015-03-18 14:16:05 -07002141 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002142 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002143 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002144 }
2145
2146 @Override
2147 public void writeToParcel(Parcel out, int flags) {
2148 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002149 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002150 }
2151
2152 public static final Parcelable.Creator<SavedState> CREATOR =
2153 new Parcelable.Creator<SavedState>() {
2154 public SavedState createFromParcel(Parcel in) {
2155 return new SavedState(in);
2156 }
2157
2158 public SavedState[] newArray(int size) {
2159 return new SavedState[size];
2160 }
2161 };
2162 }
2163
Adam Cohen7d30a372013-07-01 17:03:59 -07002164 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002165 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002166 if (mDragView != null) {
2167 AnimatorSet anim = new AnimatorSet();
2168 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2169 anim.playTogether(
2170 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002171 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2172 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2173 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002174 anim.addListener(new AnimatorListenerAdapter() {
2175 @Override
2176 public void onAnimationEnd(Animator animation) {
2177 onPostReorderingAnimationCompleted();
2178 }
2179 });
2180 anim.start();
2181 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002182 }
2183
Sunny Goyal1d08f702015-05-04 15:50:25 -07002184 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002185 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2186 mTouchState = TOUCH_STATE_REORDERING;
2187 mIsReordering = true;
2188
Adam Cohen7d30a372013-07-01 17:03:59 -07002189 // We must invalidate to trigger a redraw to update the layers such that the drag view
2190 // is always drawn on top
2191 invalidate();
2192 }
2193
Adam Cohen091440a2015-03-18 14:16:05 -07002194 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002195 // Trigger the callback when reordering has settled
2196 --mPostReorderingPreZoomInRemainingAnimationCount;
2197 if (mPostReorderingPreZoomInRunnable != null &&
2198 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2199 mPostReorderingPreZoomInRunnable.run();
2200 mPostReorderingPreZoomInRunnable = null;
2201 }
2202 }
2203
Sunny Goyal1d08f702015-05-04 15:50:25 -07002204 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002205 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002206 }
2207
Adam Cohenf358a4b2013-07-23 16:47:31 -07002208 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002209 int dragViewIndex = indexOfChild(v);
2210
Adam Cohen327acfe2014-06-06 11:52:52 -07002211 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002212
Adam Cohen7d30a372013-07-01 17:03:59 -07002213 mTempVisiblePagesRange[0] = 0;
2214 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002215 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002216 mReorderingStarted = true;
2217
2218 // Check if we are within the reordering range
2219 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002220 dragViewIndex <= mTempVisiblePagesRange[1]) {
2221 // Find the drag view under the pointer
2222 mDragView = getChildAt(dragViewIndex);
2223 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2224 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002225 snapToPage(getPageNearestToCenterOfScreen());
2226 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002227 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002228 return true;
2229 }
2230 return false;
2231 }
2232
2233 boolean isReordering(boolean testTouchState) {
2234 boolean state = mIsReordering;
2235 if (testTouchState) {
2236 state &= (mTouchState == TOUCH_STATE_REORDERING);
2237 }
2238 return state;
2239 }
2240 void endReordering() {
2241 // For simplicity, we call endReordering sometimes even if reordering was never started.
2242 // In that case, we don't want to do anything.
2243 if (!mReorderingStarted) return;
2244 mReorderingStarted = false;
2245
2246 // If we haven't flung-to-delete the current child, then we just animate the drag view
2247 // back into position
2248 final Runnable onCompleteRunnable = new Runnable() {
2249 @Override
2250 public void run() {
2251 onEndReordering();
2252 }
2253 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002254
2255 mPostReorderingPreZoomInRunnable = new Runnable() {
2256 public void run() {
2257 onCompleteRunnable.run();
2258 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002259 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002260 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002261
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002262 mPostReorderingPreZoomInRemainingAnimationCount =
2263 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2264 // Snap to the current page
2265 snapToPage(indexOfChild(mDragView), 0);
2266 // Animate the drag view back to the front position
2267 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002268 }
2269
Adam Cohen7d30a372013-07-01 17:03:59 -07002270 private static final int ANIM_TAG_KEY = 100;
2271
Winson Chung6a0f57d2011-06-29 20:10:49 -07002272 /* Accessibility */
Sunny Goyal316490e2015-06-02 09:38:28 -07002273 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002274 @Override
2275 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2276 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002277 info.setScrollable(getPageCount() > 1);
2278 if (getCurrentPage() < getPageCount() - 1) {
2279 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2280 }
2281 if (getCurrentPage() > 0) {
2282 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2283 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002284 info.setClassName(getClass().getName());
2285
2286 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2287 // Besides disabling the accessibility long-click, this also prevents this view from getting
2288 // accessibility focus.
2289 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002290 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002291 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2292 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002293 }
2294
2295 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002296 public void sendAccessibilityEvent(int eventType) {
2297 // Don't let the view send real scroll events.
2298 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2299 super.sendAccessibilityEvent(eventType);
2300 }
2301 }
2302
2303 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002304 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2305 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002306 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002307 }
2308
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002309 @Override
2310 public boolean performAccessibilityAction(int action, Bundle arguments) {
2311 if (super.performAccessibilityAction(action, arguments)) {
2312 return true;
2313 }
2314 switch (action) {
2315 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2316 if (getCurrentPage() < getPageCount() - 1) {
2317 scrollRight();
2318 return true;
2319 }
2320 } break;
2321 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2322 if (getCurrentPage() > 0) {
2323 scrollLeft();
2324 return true;
2325 }
2326 } break;
2327 }
2328 return false;
2329 }
2330
Adam Cohen0ffac432013-07-10 11:19:26 -07002331 protected String getCurrentPageDescription() {
2332 return String.format(getContext().getString(R.string.default_scroll_format),
2333 getNextPage() + 1, getChildCount());
2334 }
2335
Winson Chungd11265e2011-08-30 13:37:23 -07002336 @Override
2337 public boolean onHoverEvent(android.view.MotionEvent event) {
2338 return true;
2339 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002340}