blob: 54aa24ef6d3fe144d922dff517f00bcd103f36df [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;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070024import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ValueAnimator.AnimatorUpdateListener;
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;
30import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070049import android.view.animation.AnimationUtils;
50import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070052import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chungc58497e2013-09-03 17:48:37 -070056interface Page {
57 public int getPageChildCount();
58 public View getChildOnPageAt(int i);
59 public void removeAllViewsOnPage();
60 public void removeViewOnPageAt(int i);
61 public int indexOfChildOnPage(View v);
62}
63
Winson Chung321e9ee2010-08-09 13:37:56 -070064/**
65 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070066 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070067 */
Michael Jurka8b805b12012-04-18 14:23:14 -070068public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070070 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070071 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Winson Chung86f77532010-08-24 11:08:22 -070073 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070074 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070075
Adam Cohen7d30a372013-07-01 17:03:59 -070076 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070077 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070078 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070079 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Adam Cohenb5ba0972011-09-07 18:02:31 -070081 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070082 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080083
Adam Cohenb64cb5a2011-02-15 13:53:42 -080084 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080085 // The page is moved more than halfway, automatically move to the next page on touch up.
86 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080087
Adam Cohen265b9a62011-12-07 14:37:18 -080088 // The following constants need to be scaled based on density. The scaled versions will be
89 // assigned to the corresponding member variables below.
90 private static final int FLING_THRESHOLD_VELOCITY = 500;
91 private static final int MIN_SNAP_VELOCITY = 1500;
92 private static final int MIN_FLING_VELOCITY = 250;
93
Adam Cohen7d30a372013-07-01 17:03:59 -070094 // We are disabling touch interaction of the widget region for factory ROM.
95 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070096 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070097 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070098
Adam Cohen21cd0022013-10-09 18:57:02 -070099 public static final int INVALID_RESTORE_PAGE = -1001;
100
Adam Cohenf358a4b2013-07-23 16:47:31 -0700101 private boolean mFreeScroll = false;
102 private int mFreeScrollMinScrollX = -1;
103 private int mFreeScrollMaxScrollX = -1;
104
Winson Chung8aad6102012-05-11 16:27:49 -0700105 static final int AUTOMATIC_PAGE_SPACING = -1;
106
Adam Cohen265b9a62011-12-07 14:37:18 -0800107 protected int mFlingThresholdVelocity;
108 protected int mMinFlingVelocity;
109 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700110
Adam Cohenb5ba0972011-09-07 18:02:31 -0700111 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected float mSmoothingTime;
113 protected float mTouchX;
114
115 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700116 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700117
118 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700119 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700120 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800123 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800124 protected LauncherScroller mScroller;
125 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700127 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 private float mParentDownMotionX;
130 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 private float mDownMotionY;
133 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700134 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800136 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800137 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800138 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700139 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700140
Adam Cohendbdff6b2013-09-18 19:09:15 -0700141 private boolean mCancelTap;
142
Adam Cohenedb40762013-07-18 16:45:45 -0700143 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 protected final static int TOUCH_STATE_REST = 0;
146 protected final static int TOUCH_STATE_SCROLLING = 1;
147 protected final static int TOUCH_STATE_PREV_PAGE = 2;
148 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 protected final static int TOUCH_STATE_REORDERING = 4;
150
Adam Cohene45440e2010-10-14 18:33:38 -0700151 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurka0142d492010-08-25 17:46:15 -0700153 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700154 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka7426c422010-11-11 15:23:47 -0800158 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 private int mPagingTouchSlop;
160 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700161 protected int mPageLayoutWidthGap;
162 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700163 protected int mCellCountX = 0;
164 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700165 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800166 protected boolean mAllowOverScroll = true;
167 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800168 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700169 protected boolean mForceDrawAllChildrenNextFrame;
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700170 private boolean mSpacePagesAutomatically = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800173 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
174 // the screens from continuing to translate beyond the normal bounds.
175 protected int mOverScrollX;
176
Michael Jurka5f1c5092010-09-03 14:15:02 -0700177 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700178
Michael Jurka5f1c5092010-09-03 14:15:02 -0700179 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Winson Chung86f77532010-08-24 11:08:22 -0700181 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Michael Jurkae326f182011-11-21 14:05:46 -0800183 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Michael Jurka0142d492010-08-25 17:46:15 -0700185 // If true, syncPages and syncPageItems will be called to refresh pages
186 protected boolean mContentIsRefreshable = true;
187
188 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700189 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700190
191 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
192 // to switch to a new page
193 protected boolean mUsePagingTouchSlop = true;
194
Michael Jurka8b805b12012-04-18 14:23:14 -0700195 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700196 // (SmoothPagedView does this)
197 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700198 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700199
Patrick Dubroy1262e362010-10-06 15:49:50 -0700200 protected boolean mIsPageMoving = false;
201
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 // All syncs and layout passes are deferred until data is ready.
203 protected boolean mIsDataReady = false;
204
Adam Cohen7d30a372013-07-01 17:03:59 -0700205 protected boolean mAllowLongPress = true;
206
Winson Chungd2be3812013-07-16 11:11:32 -0700207 // Page Indicator
208 private int mPageIndicatorViewId;
209 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700210 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700211
Adam Cohen7d30a372013-07-01 17:03:59 -0700212 // The viewport whether the pages are to be contained (the actual view may be larger than the
213 // viewport)
214 private Rect mViewport = new Rect();
215
216 // Reordering
217 // We use the min scale to determine how much to expand the actually PagedView measured
218 // dimensions such that when we are zoomed out, the view is not clipped
219 private int REORDERING_DROP_REPOSITION_DURATION = 200;
220 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
221 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700222 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700223 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700224 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700225 protected View mDragView;
226 protected AnimatorSet mZoomInOutAnim;
227 private Runnable mSidePageHoverRunnable;
228 private int mSidePageHoverIndex = -1;
229 // This variable's scope is only for the duration of startReordering() and endReordering()
230 private boolean mReorderingStarted = false;
231 // This variable's scope is for the duration of startReordering() and after the zoomIn()
232 // animation after endReordering()
233 private boolean mIsReordering;
234 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
235 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
236 private int mPostReorderingPreZoomInRemainingAnimationCount;
237 private Runnable mPostReorderingPreZoomInRunnable;
238
Adam Cohen7d30a372013-07-01 17:03:59 -0700239 // Convenience/caching
240 private Matrix mTmpInvMatrix = new Matrix();
241 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700242 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700243 private Rect mTmpRect = new Rect();
244 private Rect mAltTmpRect = new Rect();
245
246 // Fling to delete
247 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
248 private float FLING_TO_DELETE_FRICTION = 0.035f;
249 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
250 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
251 protected int mFlingToDeleteThresholdVelocity = -1400;
252 // Drag to delete
253 private boolean mDeferringForDelete = false;
254 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
255 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
256
257 // Drop to delete
258 private View mDeleteDropTarget;
259
Adam Cohen7d30a372013-07-01 17:03:59 -0700260 // Bouncer
261 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700262
John Spurlock77e1f472013-09-11 10:09:51 -0400263 protected final Rect mInsets = new Rect();
264
Winson Chung86f77532010-08-24 11:08:22 -0700265 public interface PageSwitchListener {
266 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 }
268
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 public PagedView(Context context) {
270 this(context, null);
271 }
272
273 public PagedView(Context context, AttributeSet attrs) {
274 this(context, attrs, 0);
275 }
276
277 public PagedView(Context context, AttributeSet attrs, int defStyle) {
278 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700279
Adam Cohen9c4949e2010-10-05 12:27:22 -0700280 TypedArray a = context.obtainStyledAttributes(attrs,
281 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700282
Winson Chungdf4b83d2010-10-20 17:49:27 -0700283 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700284 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700285 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700286 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700287 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700288 a.recycle();
289
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700291 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 }
293
294 /**
295 * Initializes various states for this workspace.
296 */
Michael Jurka0142d492010-08-25 17:46:15 -0700297 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700298 mDirtyPageContent = new ArrayList<Boolean>();
299 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800300 mScroller = new LauncherScroller(getContext());
301 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700302 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700303 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700304
305 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700306 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
308 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700309 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800310
Adam Cohen7d30a372013-07-01 17:03:59 -0700311 // Scale the fling-to-delete threshold by the density
312 mFlingToDeleteThresholdVelocity =
313 (int) (mFlingToDeleteThresholdVelocity * mDensity);
314
Adam Cohen265b9a62011-12-07 14:37:18 -0800315 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
316 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
317 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700318 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700319 }
320
Adam Cohenf9618852013-11-08 06:45:03 -0800321 protected void setDefaultInterpolator(Interpolator interpolator) {
322 mDefaultInterpolator = interpolator;
323 mScroller.setInterpolator(mDefaultInterpolator);
324 }
325
Winson Chungd2be3812013-07-16 11:11:32 -0700326 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700327 super.onAttachedToWindow();
328
Winson Chungd2be3812013-07-16 11:11:32 -0700329 // Hook up the page indicator
330 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700331 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700332 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700333 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700334 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700335
Winson Chung7819a562013-09-19 15:55:45 -0700336 ArrayList<PageIndicator.PageMarkerResources> markers =
337 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700338 for (int i = 0; i < getChildCount(); ++i) {
339 markers.add(getPageIndicatorMarker(i));
340 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700341
Winson Chungc58497e2013-09-03 17:48:37 -0700342 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700343
344 OnClickListener listener = getPageIndicatorClickListener();
345 if (listener != null) {
346 mPageIndicator.setOnClickListener(listener);
347 }
Adam Cohen53805212013-10-01 10:39:23 -0700348 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700349 }
350 }
351
Adam Cohen53805212013-10-01 10:39:23 -0700352 protected String getPageIndicatorDescription() {
353 return getCurrentPageDescription();
354 }
355
356 protected OnClickListener getPageIndicatorClickListener() {
357 return null;
358 }
359
Winson Chungd2be3812013-07-16 11:11:32 -0700360 protected void onDetachedFromWindow() {
361 // Unhook the page indicator
362 mPageIndicator = null;
363 }
364
Adam Cohen7d30a372013-07-01 17:03:59 -0700365 void setDeleteDropTarget(View v) {
366 mDeleteDropTarget = v;
367 }
368
369 // Convenience methods to map points from self to parent and vice versa
370 float[] mapPointFromViewToParent(View v, float x, float y) {
371 mTmpPoint[0] = x;
372 mTmpPoint[1] = y;
373 v.getMatrix().mapPoints(mTmpPoint);
374 mTmpPoint[0] += v.getLeft();
375 mTmpPoint[1] += v.getTop();
376 return mTmpPoint;
377 }
378 float[] mapPointFromParentToView(View v, float x, float y) {
379 mTmpPoint[0] = x - v.getLeft();
380 mTmpPoint[1] = y - v.getTop();
381 v.getMatrix().invert(mTmpInvMatrix);
382 mTmpInvMatrix.mapPoints(mTmpPoint);
383 return mTmpPoint;
384 }
385
386 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700387 if (mDragView != null) {
388 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
389 (mDragViewBaselineLeft - mDragView.getLeft());
390 float y = mLastMotionY - mDownMotionY;
391 mDragView.setTranslationX(x);
392 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700393
Adam Cohenf358a4b2013-07-23 16:47:31 -0700394 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
395 + x + ", " + y);
396 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700397 }
398
399 public void setMinScale(float f) {
400 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700401 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700402 requestLayout();
403 }
404
405 @Override
406 public void setScaleX(float scaleX) {
407 super.setScaleX(scaleX);
408 if (isReordering(true)) {
409 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
410 mLastMotionX = p[0];
411 mLastMotionY = p[1];
412 updateDragViewTranslationDuringDrag();
413 }
414 }
415
416 // Convenience methods to get the actual width/height of the PagedView (since it is measured
417 // to be larger to account for the minimum possible scale)
418 int getViewportWidth() {
419 return mViewport.width();
420 }
421 int getViewportHeight() {
422 return mViewport.height();
423 }
424
425 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
426 // PagedView both horizontally and vertically
427 int getViewportOffsetX() {
428 return (getMeasuredWidth() - getViewportWidth()) / 2;
429 }
430
431 int getViewportOffsetY() {
432 return (getMeasuredHeight() - getViewportHeight()) / 2;
433 }
434
Adam Cohenedb40762013-07-18 16:45:45 -0700435 PageIndicator getPageIndicator() {
436 return mPageIndicator;
437 }
Winson Chung7819a562013-09-19 15:55:45 -0700438 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
439 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700440 }
Adam Cohenedb40762013-07-18 16:45:45 -0700441
Adam Cohen674531f2013-12-13 15:07:14 -0800442 /**
443 * Add a page change listener which will be called when a page is _finished_ listening.
444 *
445 */
Winson Chung86f77532010-08-24 11:08:22 -0700446 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
447 mPageSwitchListener = pageSwitchListener;
448 if (mPageSwitchListener != null) {
449 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 }
451 }
452
453 /**
Winson Chung52aee602013-01-30 12:01:02 -0800454 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
455 */
456 public boolean isLayoutRtl() {
457 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
458 }
459
460 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
462 * out pages.
463 */
464 protected void setDataIsReady() {
465 mIsDataReady = true;
466 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700467
Winson Chungf0ea4d32011-06-06 14:27:16 -0700468 protected boolean isDataReady() {
469 return mIsDataReady;
470 }
471
472 /**
Winson Chung86f77532010-08-24 11:08:22 -0700473 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 *
Winson Chung86f77532010-08-24 11:08:22 -0700475 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 */
Winson Chung86f77532010-08-24 11:08:22 -0700477 int getCurrentPage() {
478 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700480
Winson Chung360e63f2012-04-27 13:48:05 -0700481 int getNextPage() {
482 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
483 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700484
Winson Chung86f77532010-08-24 11:08:22 -0700485 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 return getChildCount();
487 }
488
Winson Chung86f77532010-08-24 11:08:22 -0700489 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 return getChildAt(index);
491 }
492
Adam Cohenae4f1552011-10-20 00:15:42 -0700493 protected int indexToPage(int index) {
494 return index;
495 }
496
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800498 * Updates the scroll of the current page immediately to its final scroll position. We use this
499 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
500 * the previous tab page.
501 */
502 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700503 // If the current page is invalid, just reset the scroll position to zero
504 int newX = 0;
505 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700506 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700507 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800508 scrollTo(newX, 0);
509 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700510 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800511 }
512
513 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700514 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700515 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
516 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700517 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700518 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700519 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800520 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700521 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700522 }
523
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700524 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700525 mScroller.abortAnimation();
526 // We need to clean up the next page here to avoid computeScrollHelper from
527 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700528 if (resetNextPage) {
529 mNextPage = INVALID_PAGE;
530 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700531 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700532
533 private void forceFinishScroller() {
534 mScroller.forceFinished(true);
535 // We need to clean up the next page here to avoid computeScrollHelper from
536 // updating current page on the pass.
537 mNextPage = INVALID_PAGE;
538 }
539
Adam Cohen6f127a62014-06-12 14:54:41 -0700540 private int validateNewPage(int newPage) {
541 int validatedPage = newPage;
542 // When in free scroll mode, we need to clamp to the free scroll page range.
543 if (mFreeScroll) {
544 getFreeScrollPageRange(mTempVisiblePagesRange);
545 validatedPage = Math.max(mTempVisiblePagesRange[0],
546 Math.min(newPage, mTempVisiblePagesRange[1]));
547 }
548 // Ensure that it is clamped by the actual set of children in all cases
549 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
550 return validatedPage;
551 }
552
Chet Haasebc2f0822012-10-26 17:59:53 -0700553 /**
Winson Chung86f77532010-08-24 11:08:22 -0700554 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 */
Winson Chung86f77532010-08-24 11:08:22 -0700556 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800557 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700558 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800559 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800560 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
561 // the default
562 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800563 return;
564 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700565 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700566 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700567 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700568 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800569 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571
Winson Chung8c87cd82013-07-23 16:20:10 -0700572 /**
573 * The restore page will be set in place of the current page at the next (likely first)
574 * layout.
575 */
576 void setRestorePage(int restorePage) {
577 mRestorePage = restorePage;
578 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800579 int getRestorePage() {
580 return mRestorePage;
581 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700582
Adam Cohen674531f2013-12-13 15:07:14 -0800583 /**
584 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
585 * has settled.
586 */
Michael Jurka0142d492010-08-25 17:46:15 -0700587 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700588 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800589 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700590 }
Winson Chungd2be3812013-07-16 11:11:32 -0700591
Adam Cohen674531f2013-12-13 15:07:14 -0800592 updatePageIndicator();
593 }
594
595 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700596 // Update the page indicator (when we aren't reordering)
597 if (mPageIndicator != null && !isReordering(false)) {
598 mPageIndicator.setActiveMarker(getNextPage());
599 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800601 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700602 if (!mIsPageMoving) {
603 mIsPageMoving = true;
604 onPageBeginMoving();
605 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700606 }
607
Michael Jurkace7e05f2011-02-01 22:02:35 -0800608 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700609 if (mIsPageMoving) {
610 mIsPageMoving = false;
611 onPageEndMoving();
612 }
Michael Jurka0142d492010-08-25 17:46:15 -0700613 }
614
Adam Cohen26976d92011-03-22 15:33:33 -0700615 protected boolean isPageMoving() {
616 return mIsPageMoving;
617 }
618
Michael Jurka0142d492010-08-25 17:46:15 -0700619 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700620 protected void onPageBeginMoving() {
621 }
622
623 // a method that subclasses can override to add behavior
624 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700625 }
626
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 /**
Winson Chung86f77532010-08-24 11:08:22 -0700628 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700629 *
630 * @param l The listener used to respond to long clicks.
631 */
632 @Override
633 public void setOnLongClickListener(OnLongClickListener l) {
634 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700635 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700637 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
Adam Cohen1697b792013-09-17 19:08:21 -0700639 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 }
641
642 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800643 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800645 }
646
647 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700648 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700649 // In free scroll mode, we clamp the scrollX
650 if (mFreeScroll) {
651 x = Math.min(x, mFreeScrollMaxScrollX);
652 x = Math.max(x, mFreeScrollMinScrollX);
653 }
654
Adam Cohen0ffac432013-07-10 11:19:26 -0700655 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800656 mUnboundedScrollX = x;
657
Adam Cohen0ffac432013-07-10 11:19:26 -0700658 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
659 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
660 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800661 super.scrollTo(0, y);
662 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700663 if (isRtl) {
664 overScroll(x - mMaxScrollX);
665 } else {
666 overScroll(x);
667 }
Adam Cohen68d73932010-11-15 10:50:58 -0800668 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700669 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800670 super.scrollTo(mMaxScrollX, y);
671 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700672 if (isRtl) {
673 overScroll(x);
674 } else {
675 overScroll(x - mMaxScrollX);
676 }
Adam Cohen68d73932010-11-15 10:50:58 -0800677 }
678 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800679 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800680 super.scrollTo(x, y);
681 }
682
Michael Jurka0142d492010-08-25 17:46:15 -0700683 mTouchX = x;
684 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700685
686 // Update the last motion events when scrolling
687 if (isReordering(true)) {
688 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
689 mLastMotionX = p[0];
690 mLastMotionY = p[1];
691 updateDragViewTranslationDuringDrag();
692 }
Michael Jurka0142d492010-08-25 17:46:15 -0700693 }
694
Adam Cohen53805212013-10-01 10:39:23 -0700695 private void sendScrollAccessibilityEvent() {
696 AccessibilityManager am =
697 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
698 if (am.isEnabled()) {
699 AccessibilityEvent ev =
700 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700701 ev.setItemCount(getChildCount());
702 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700703 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700704
Alan Viverette254139a2013-10-08 13:13:46 -0700705 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700706 if (getNextPage() >= mCurrentPage) {
707 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
708 } else {
709 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
710 }
711
712 ev.setAction(action);
713 sendAccessibilityEventUnchecked(ev);
714 }
715 }
716
Michael Jurka0142d492010-08-25 17:46:15 -0700717 // we moved this functionality to a helper function so SmoothPagedView can reuse it
718 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700720 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 if (getScrollX() != mScroller.getCurrX()
722 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700723 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700724 float scaleX = mFreeScroll ? getScaleX() : 1f;
725 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
726 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 }
Michael Jurka0142d492010-08-25 17:46:15 -0700728 invalidate();
729 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700730 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700731 sendScrollAccessibilityEvent();
732
Adam Cohen6f127a62014-06-12 14:54:41 -0700733 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700734 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700735 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700736
Winson Chung9c0565f2013-07-19 13:49:06 -0700737 // Load the associated pages if necessary
738 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
739 loadAssociatedPages(mCurrentPage);
740 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
741 }
742
Adam Cohen73aa9752010-11-24 16:26:58 -0800743 // We don't want to trigger a page end moving unless the page has settled
744 // and the user has stopped scrolling
745 if (mTouchState == TOUCH_STATE_REST) {
746 pageEndMoving();
747 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700748
Adam Cohen7d30a372013-07-01 17:03:59 -0700749 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700750 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700751 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700752 if (am.isEnabled()) {
753 // Notify the user when the page changes
754 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700755 }
Michael Jurka0142d492010-08-25 17:46:15 -0700756 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 }
Michael Jurka0142d492010-08-25 17:46:15 -0700758 return false;
759 }
760
761 @Override
762 public void computeScroll() {
763 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765
Adam Cohen7d30a372013-07-01 17:03:59 -0700766 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
767 return mTopAlignPageWhenShrinkingForBouncer;
768 }
769
Adam Cohen96d30a12013-07-16 18:13:21 -0700770 public static class LayoutParams extends ViewGroup.LayoutParams {
771 public boolean isFullScreenPage = false;
772
773 /**
774 * {@inheritDoc}
775 */
776 public LayoutParams(int width, int height) {
777 super(width, height);
778 }
779
780 public LayoutParams(ViewGroup.LayoutParams source) {
781 super(source);
782 }
783 }
784
785 protected LayoutParams generateDefaultLayoutParams() {
786 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
787 }
788
Adam Cohenedb40762013-07-18 16:45:45 -0700789 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700790 LayoutParams lp = generateDefaultLayoutParams();
791 lp.isFullScreenPage = true;
792 super.addView(page, 0, lp);
793 }
794
Adam Cohen410f3cd2013-09-22 12:09:32 -0700795 public int getNormalChildHeight() {
796 return mNormalChildHeight;
797 }
798
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 @Override
800 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700802 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
803 return;
804 }
805
Adam Cohen7d30a372013-07-01 17:03:59 -0700806 // We measure the dimensions of the PagedView to be larger than the pages so that when we
807 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700808 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
809 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
810 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700811 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800812 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700813 // viewport, we can be at most one and a half screens offset once we scale down
814 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700815 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
816 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700817
Winson Chungc82d2622013-11-06 13:23:29 -0800818 int parentWidthSize = (int) (2f * maxSize);
819 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700820 int scaledWidthSize, scaledHeightSize;
821 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700822 scaledWidthSize = (int) (parentWidthSize / mMinScale);
823 scaledHeightSize = (int) (parentHeightSize / mMinScale);
824 } else {
825 scaledWidthSize = widthSize;
826 scaledHeightSize = heightSize;
827 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700828 mViewport.set(0, 0, widthSize, heightSize);
829
830 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
831 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
832 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 }
834
Winson Chung8aad6102012-05-11 16:27:49 -0700835 // Return early if we aren't given a proper dimension
836 if (widthSize <= 0 || heightSize <= 0) {
837 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
838 return;
839 }
840
Adam Lesinski6b879f02010-11-04 16:15:23 -0700841 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
842 * of the All apps view on XLarge displays to not take up more space then it needs. Width
843 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
844 * each page to have the same width.
845 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700846 final int verticalPadding = getPaddingTop() + getPaddingBottom();
847 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700848
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700849 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700851 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700852 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700853 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
854 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
855 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
856 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 final int childCount = getChildCount();
858 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700859 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700860 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100861 if (child.getVisibility() != GONE) {
862 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700863
Vladimir Marko2824b072013-10-04 16:42:17 +0100864 int childWidthMode;
865 int childHeightMode;
866 int childWidth;
867 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700868
Vladimir Marko2824b072013-10-04 16:42:17 +0100869 if (!lp.isFullScreenPage) {
870 if (lp.width == LayoutParams.WRAP_CONTENT) {
871 childWidthMode = MeasureSpec.AT_MOST;
872 } else {
873 childWidthMode = MeasureSpec.EXACTLY;
874 }
875
876 if (lp.height == LayoutParams.WRAP_CONTENT) {
877 childHeightMode = MeasureSpec.AT_MOST;
878 } else {
879 childHeightMode = MeasureSpec.EXACTLY;
880 }
881
Adam Cohen3b185e22013-10-29 14:45:58 -0700882 childWidth = getViewportWidth() - horizontalPadding
883 - mInsets.left - mInsets.right;
884 childHeight = getViewportHeight() - verticalPadding
885 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100886 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700887 } else {
888 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700889 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100890
Adam Cohen3b185e22013-10-29 14:45:58 -0700891 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
892 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700893 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700894 if (referenceChildWidth == 0) {
895 referenceChildWidth = childWidth;
896 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700897
Vladimir Marko2824b072013-10-04 16:42:17 +0100898 final int childWidthMeasureSpec =
899 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
900 final int childHeightMeasureSpec =
901 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
902 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700903 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700904 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700905 if (mSpacePagesAutomatically) {
906 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
907 - referenceChildWidth) / 2;
908 if (spacing >= 0) {
909 setPageSpacing(spacing);
910 }
911 mSpacePagesAutomatically = false;
912 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700913 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800914 }
915
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700916 /**
917 * This method should be called once before first layout / measure pass.
918 */
919 protected void setSinglePageInViewport() {
920 mSpacePagesAutomatically = true;
921 }
922
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700924 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700926 return;
927 }
928
Winson Chung785d2eb2011-04-14 16:08:02 -0700929 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700931
Adam Cohen7d30a372013-07-01 17:03:59 -0700932 int offsetX = getViewportOffsetX();
933 int offsetY = getViewportOffsetY();
934
935 // Update the viewport offsets
936 mViewport.offset(offsetX, offsetY);
937
Adam Cohen0ffac432013-07-10 11:19:26 -0700938 final boolean isRtl = isLayoutRtl();
939
940 final int startIndex = isRtl ? childCount - 1 : 0;
941 final int endIndex = isRtl ? -1 : childCount;
942 final int delta = isRtl ? -1 : 1;
943
Adam Cohen7d30a372013-07-01 17:03:59 -0700944 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700945
Adam Cohen3b185e22013-10-29 14:45:58 -0700946 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000947 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700948
949 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700950 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
951 mPageScrolls = new int[getChildCount()];
952 }
953
Adam Cohen0ffac432013-07-10 11:19:26 -0700954 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700955 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700957 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100958 int childTop;
959 if (lp.isFullScreenPage) {
960 childTop = offsetY;
961 } else {
962 childTop = offsetY + getPaddingTop() + mInsets.top;
963 if (mCenterPagesVertically) {
964 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
965 }
966 }
967
Adam Cohen96d30a12013-07-16 18:13:21 -0700968 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700969 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800970
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700972 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800973 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700974
Adam Cohen3b185e22013-10-29 14:45:58 -0700975 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
976 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000977
978 int pageGap = mPageSpacing;
979 int next = i + delta;
980 if (next != endIndex) {
981 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
982 } else {
983 nextLp = null;
984 }
985
986 // Prevent full screen pages from showing in the viewport
987 // when they are not the current page.
988 if (lp.isFullScreenPage) {
989 pageGap = getPaddingLeft();
990 } else if (nextLp != null && nextLp.isFullScreenPage) {
991 pageGap = getPaddingRight();
992 }
993
994 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 }
996 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700997
998 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
999 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -08001000 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001001 setHorizontalScrollBarEnabled(true);
1002 mFirstLayout = false;
1003 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001004
1005 if (childCount > 0) {
1006 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -07001007 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -07001008 } else {
1009 mMaxScrollX = 0;
1010 }
1011
1012 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1013 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001014 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001015 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001016 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001017 } else {
1018 setCurrentPage(getNextPage());
1019 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001020 }
1021 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001022
1023 if (isReordering(true)) {
1024 updateDragViewTranslationDuringDrag();
1025 }
Winson Chunge3193b92010-09-10 11:44:42 -07001026 }
1027
Adam Cohen3b185e22013-10-29 14:45:58 -07001028 public void setPageSpacing(int pageSpacing) {
1029 mPageSpacing = pageSpacing;
1030 requestLayout();
1031 }
1032
Adam Cohenf34bab52010-09-30 14:11:56 -07001033 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001034 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1035
1036 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001037 for (int i = 0; i < getChildCount(); i++) {
1038 View child = getChildAt(i);
1039 if (child != null) {
1040 float scrollProgress = getScrollProgress(screenCenter, child, i);
1041 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001042 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001043 }
1044 }
1045 invalidate();
1046 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001047 }
1048
Winson Chungc58497e2013-09-03 17:48:37 -07001049 protected void enablePagedViewAnimations() {
1050 mAllowPagedViewAnimations = true;
1051
1052 }
1053 protected void disablePagedViewAnimations() {
1054 mAllowPagedViewAnimations = false;
1055 }
1056
Winson Chunge3193b92010-09-10 11:44:42 -07001057 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001058 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001059 // Update the page indicator, we don't update the page indicator as we
1060 // add/remove pages
1061 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001062 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001063 mPageIndicator.addMarker(pageIndex,
1064 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001065 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001066 }
1067
Adam Cohen2591f6a2011-10-25 14:36:40 -07001068 // This ensures that when children are added, they get the correct transforms / alphas
1069 // in accordance with any scroll effects.
1070 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001071 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001072 invalidate();
1073 }
1074
Michael Jurka8b805b12012-04-18 14:23:14 -07001075 @Override
1076 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001077 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001078 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001079 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001080 }
1081
Winson Chungd2be3812013-07-16 11:11:32 -07001082 private void removeMarkerForView(int index) {
1083 // Update the page indicator, we don't update the page indicator as we
1084 // add/remove pages
1085 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001086 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001087 }
1088 }
1089
1090 @Override
1091 public void removeView(View v) {
1092 // XXX: We should find a better way to hook into this before the view
1093 // gets removed form its parent...
1094 removeMarkerForView(indexOfChild(v));
1095 super.removeView(v);
1096 }
1097 @Override
1098 public void removeViewInLayout(View v) {
1099 // XXX: We should find a better way to hook into this before the view
1100 // gets removed form its parent...
1101 removeMarkerForView(indexOfChild(v));
1102 super.removeViewInLayout(v);
1103 }
1104 @Override
1105 public void removeViewAt(int index) {
1106 // XXX: We should find a better way to hook into this before the view
1107 // gets removed form its parent...
1108 removeViewAt(index);
1109 super.removeViewAt(index);
1110 }
1111 @Override
1112 public void removeAllViewsInLayout() {
1113 // Update the page indicator, we don't update the page indicator as we
1114 // add/remove pages
1115 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001116 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001117 }
1118
1119 super.removeAllViewsInLayout();
1120 }
1121
Adam Cohen73894962011-10-31 13:17:17 -07001122 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001123 if (index < 0 || index > getChildCount() - 1) return 0;
1124
Adam Cohenf698c6e2013-07-17 18:44:25 -07001125 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001126
Adam Cohenf698c6e2013-07-17 18:44:25 -07001127 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001128 }
1129
Adam Cohen6f127a62014-06-12 14:54:41 -07001130 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001131 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001132 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001133 }
1134
Michael Jurkadde558b2011-11-09 22:09:06 -08001135 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001136 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001137 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001138
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001139 range[0] = -1;
1140 range[1] = -1;
1141
Michael Jurkac4fb9172010-09-02 17:19:20 -07001142 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001143 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001144 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001145
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001146 int count = getChildCount();
1147 for (int i = 0; i < count; i++) {
1148 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001149
Winson Chungc9ca2982013-07-19 12:07:38 -07001150 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001151 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001152 if (mTmpIntPoint[0] > viewportWidth) {
1153 if (range[0] == -1) {
1154 continue;
1155 } else {
1156 break;
1157 }
1158 }
1159
Adam Cohen6a678da2013-09-24 10:58:01 -07001160 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001161 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1162 if (mTmpIntPoint[0] < 0) {
1163 if (range[0] == -1) {
1164 continue;
1165 } else {
1166 break;
1167 }
1168 }
1169 curScreen = i;
1170 if (range[0] < 0) {
1171 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001172 }
1173 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001174
1175 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001176 } else {
1177 range[0] = -1;
1178 range[1] = -1;
1179 }
1180 }
1181
Michael Jurka920d7f42012-05-14 16:29:55 -07001182 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001183 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001184 }
1185
Michael Jurkadde558b2011-11-09 22:09:06 -08001186 @Override
1187 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001188 // Find out which screens are visible; as an optimization we only call draw on them
1189 final int pageCount = getChildCount();
1190 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001191 int halfScreenSize = getViewportWidth() / 2;
1192 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1193 // Otherwise it is equal to the scaled overscroll position.
1194 int screenCenter = mOverScrollX + halfScreenSize;
1195
1196 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1197 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1198 // set it for the next frame
1199 mForceScreenScrolled = false;
1200 screenScrolled(screenCenter);
1201 mLastScreenCenter = screenCenter;
1202 }
1203
Michael Jurkadde558b2011-11-09 22:09:06 -08001204 getVisiblePages(mTempVisiblePagesRange);
1205 final int leftScreen = mTempVisiblePagesRange[0];
1206 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001207 if (leftScreen != -1 && rightScreen != -1) {
1208 final long drawingTime = getDrawingTime();
1209 // Clip to the bounds
1210 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001211 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1212 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001213
Adam Cohen7d30a372013-07-01 17:03:59 -07001214 // Draw all the children, leaving the drag view for last
1215 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001216 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001217 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001218 if (mForceDrawAllChildrenNextFrame ||
1219 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001220 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001221 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001222 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001223 // Draw the drag view on top (if there is one)
1224 if (mDragView != null) {
1225 drawChild(canvas, mDragView, drawingTime);
1226 }
1227
Michael Jurka5e368ff2012-05-14 23:13:15 -07001228 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001229 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001230 }
Michael Jurka0142d492010-08-25 17:46:15 -07001231 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 }
1233
1234 @Override
1235 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001236 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001237 if (page != mCurrentPage || !mScroller.isFinished()) {
1238 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 return true;
1240 }
1241 return false;
1242 }
1243
1244 @Override
1245 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001246 int focusablePage;
1247 if (mNextPage != INVALID_PAGE) {
1248 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001250 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
Winson Chung86f77532010-08-24 11:08:22 -07001252 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001254 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 }
1256 return false;
1257 }
1258
1259 @Override
1260 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001261 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001263 if (getCurrentPage() > 0) {
1264 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 return true;
1266 }
1267 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001268 if (getCurrentPage() < getPageCount() - 1) {
1269 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 return true;
1271 }
1272 }
1273 return super.dispatchUnhandledMove(focused, direction);
1274 }
1275
1276 @Override
1277 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001278 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001279 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001280 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 }
1282 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001283 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001284 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 }
1286 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001287 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001288 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001289 }
1290 }
1291 }
1292
1293 /**
1294 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001295 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 *
Winson Chung86f77532010-08-24 11:08:22 -07001297 * This happens when live folders requery, and if they're off page, they
1298 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 */
1300 @Override
1301 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001302 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 View v = focused;
1304 while (true) {
1305 if (v == current) {
1306 super.focusableViewAvailable(focused);
1307 return;
1308 }
1309 if (v == this) {
1310 return;
1311 }
1312 ViewParent parent = v.getParent();
1313 if (parent instanceof View) {
1314 v = (View)v.getParent();
1315 } else {
1316 return;
1317 }
1318 }
1319 }
1320
1321 /**
1322 * {@inheritDoc}
1323 */
1324 @Override
1325 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1326 if (disallowIntercept) {
1327 // We need to make sure to cancel our long press if
1328 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001329 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001330 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 }
1332 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1333 }
1334
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001335 /**
1336 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1337 */
1338 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001339 if (isLayoutRtl()) {
1340 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001341 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001342 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001343 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001344 }
1345
1346 /**
1347 * Return true if a tap at (x, y) should trigger a flip to the next page.
1348 */
1349 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001350 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001351 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001352 }
1353 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001354 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001355 }
Winson Chung52aee602013-01-30 12:01:02 -08001356
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 /** Returns whether x and y originated within the buffered viewport */
1358 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1359 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1360 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1361 return mTmpRect.contains(x, y);
1362 }
1363
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 @Override
1365 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001366 if (DISABLE_TOUCH_INTERACTION) {
1367 return false;
1368 }
1369
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 /*
1371 * This method JUST determines whether we want to intercept the motion.
1372 * If we return true, onTouchEvent will be called and we do the actual
1373 * scrolling there.
1374 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001375 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001376
Winson Chung45e1d6e2010-11-09 17:19:49 -08001377 // Skip touch handling if there are no pages to swipe
1378 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 /*
1381 * Shortcut the most recurring case: the user is in the dragging
1382 * state and he is moving his finger. We want to intercept this
1383 * motion.
1384 */
1385 final int action = ev.getAction();
1386 if ((action == MotionEvent.ACTION_MOVE) &&
1387 (mTouchState == TOUCH_STATE_SCROLLING)) {
1388 return true;
1389 }
1390
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 switch (action & MotionEvent.ACTION_MASK) {
1392 case MotionEvent.ACTION_MOVE: {
1393 /*
1394 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1395 * whether the user has moved far enough from his original down touch.
1396 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001397 if (mActivePointerId != INVALID_POINTER) {
1398 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001399 }
1400 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1401 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1402 // i.e. fall through to the next case (don't break)
1403 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1404 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001405 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 }
1407
1408 case MotionEvent.ACTION_DOWN: {
1409 final float x = ev.getX();
1410 final float y = ev.getY();
1411 // Remember location of down touch
1412 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 mDownMotionY = y;
1414 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 mLastMotionX = x;
1416 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001417 float[] p = mapPointFromViewToParent(this, x, y);
1418 mParentDownMotionX = p[0];
1419 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001420 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001421 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 /*
1425 * If being flinged and user touches the screen, initiate drag;
1426 * otherwise don't. mScroller.isFinished should be false when
1427 * being flinged.
1428 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001429 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001430 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001431
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001432 if (finishedScrolling) {
1433 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001434 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001435 setCurrentPage(getNextPage());
1436 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001437 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001438 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1440 mTouchState = TOUCH_STATE_SCROLLING;
1441 } else {
1442 mTouchState = TOUCH_STATE_REST;
1443 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001444 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001445
Winson Chung86f77532010-08-24 11:08:22 -07001446 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001448 if (!DISABLE_TOUCH_SIDE_PAGES) {
1449 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1450 if (getChildCount() > 0) {
1451 if (hitsPreviousPage(x, y)) {
1452 mTouchState = TOUCH_STATE_PREV_PAGE;
1453 } else if (hitsNextPage(x, y)) {
1454 mTouchState = TOUCH_STATE_NEXT_PAGE;
1455 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 }
1457 }
1458 }
1459 break;
1460 }
1461
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001463 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 break;
1466
1467 case MotionEvent.ACTION_POINTER_UP:
1468 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001469 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 break;
1471 }
1472
1473 /*
1474 * The only time we want to intercept motion events is if we are in the
1475 * drag mode.
1476 */
1477 return mTouchState != TOUCH_STATE_REST;
1478 }
1479
Adam Cohenf8d28232011-02-01 21:47:00 -08001480 protected void determineScrollingStart(MotionEvent ev) {
1481 determineScrollingStart(ev, 1.0f);
1482 }
1483
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 /*
1485 * Determines if we should change the touch state to start scrolling after the
1486 * user moves their touch point too far.
1487 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001488 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001489 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001491 if (pointerIndex == -1) return;
1492
1493 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 final float x = ev.getX(pointerIndex);
1495 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001496 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1497
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 final int xDiff = (int) Math.abs(x - mLastMotionX);
1499 final int yDiff = (int) Math.abs(y - mLastMotionY);
1500
Adam Cohenf8d28232011-02-01 21:47:00 -08001501 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 boolean xPaged = xDiff > mPagingTouchSlop;
1503 boolean xMoved = xDiff > touchSlop;
1504 boolean yMoved = yDiff > touchSlop;
1505
Adam Cohenf8d28232011-02-01 21:47:00 -08001506 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001507 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 // Scroll if the user moved far enough along the X axis
1509 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001510 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001512 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001513 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001514 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1515 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001517 }
1518 }
1519
Adam Cohen7d30a372013-07-01 17:03:59 -07001520 protected float getMaxScrollProgress() {
1521 return 1.0f;
1522 }
1523
Adam Cohenf8d28232011-02-01 21:47:00 -08001524 protected void cancelCurrentPageLongPress() {
1525 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001527 // Try canceling the long press. It could also have been scheduled
1528 // by a distant descendant, so use the mAllowLongPress flag to block
1529 // everything
1530 final View currentPage = getPageAt(mCurrentPage);
1531 if (currentPage != null) {
1532 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001533 }
1534 }
1535 }
1536
Adam Cohen7d30a372013-07-01 17:03:59 -07001537 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1538 final int halfScreenSize = getViewportWidth() / 2;
1539
1540 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1541 screenCenter = Math.max(halfScreenSize, screenCenter);
1542
1543 return getScrollProgress(screenCenter, v, page);
1544 }
1545
Adam Cohenb5ba0972011-09-07 18:02:31 -07001546 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001547 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001548
Adam Cohenedb40762013-07-18 16:45:45 -07001549 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001550 int count = getChildCount();
1551
1552 final int totalDistance;
1553
1554 int adjacentPage = page + 1;
1555 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1556 adjacentPage = page - 1;
1557 }
1558
1559 if (adjacentPage < 0 || adjacentPage > count - 1) {
1560 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1561 } else {
1562 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1563 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001564
1565 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001566 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1567 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001568 return scrollProgress;
1569 }
1570
Adam Cohenedb40762013-07-18 16:45:45 -07001571 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001572 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001573 return 0;
1574 } else {
1575 return mPageScrolls[index];
1576 }
1577 }
1578
Adam Cohen564a2e72013-10-09 14:47:32 -07001579 // While layout transitions are occurring, a child's position may stray from its baseline
1580 // position. This method returns the magnitude of this stray at any given time.
1581 public int getLayoutTransitionOffsetForPage(int index) {
1582 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1583 return 0;
1584 } else {
1585 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001586
1587 int scrollOffset = 0;
1588 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1589 if (!lp.isFullScreenPage) {
1590 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1591 }
1592
Adam Cohen564a2e72013-10-09 14:47:32 -07001593 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1594 return (int) (child.getX() - baselineX);
1595 }
1596 }
1597
Adam Cohene0f66b52010-11-23 15:06:07 -08001598 // This curve determines how the effect of scrolling over the limits of the page dimishes
1599 // as the user pulls further and further from the bounds
1600 private float overScrollInfluenceCurve(float f) {
1601 f -= 1.0f;
1602 return f * f * f + 1.0f;
1603 }
1604
Adam Cohen1e4359c2014-08-18 13:12:16 -07001605 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001606 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001607
1608 // We want to reach the max over scroll effect when the user has
1609 // over scrolled half the size of the screen
1610 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1611
Adam Cohen1e4359c2014-08-18 13:12:16 -07001612 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001613
1614 // Clamp this factor, f, to -1 < f < 1
1615 if (Math.abs(f) >= 1) {
1616 f /= Math.abs(f);
1617 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001618 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001619 }
1620
1621 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001622 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001623
1624 float f = (amount / screenSize);
1625
1626 if (f == 0) return;
1627 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1628
Adam Cohen7bfc9792011-01-28 13:52:37 -08001629 // Clamp this factor, f, to -1 < f < 1
1630 if (Math.abs(f) >= 1) {
1631 f /= Math.abs(f);
1632 }
1633
Adam Cohene0f66b52010-11-23 15:06:07 -08001634 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001635 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001636 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001637 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001638 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001639 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001640 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001641 }
1642 invalidate();
1643 }
1644
Adam Cohenb5ba0972011-09-07 18:02:31 -07001645 protected void overScroll(float amount) {
1646 dampedOverScroll(amount);
1647 }
1648
Michael Jurkac5b262c2011-01-12 20:24:50 -08001649 protected float maxOverScroll() {
1650 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001651 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001652 float f = 1.0f;
1653 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1654 return OVERSCROLL_DAMP_FACTOR * f;
1655 }
1656
Adam Cohenf358a4b2013-07-23 16:47:31 -07001657 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001658 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001659 }
1660
Adam Cohenf9618852013-11-08 06:45:03 -08001661 protected void disableFreeScroll() {
1662 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001663 }
1664
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001665 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001666 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001667 if (isLayoutRtl()) {
1668 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1669 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1670 } else {
1671 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1672 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1673 }
1674 }
1675
Adam Cohenf9618852013-11-08 06:45:03 -08001676 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001677 mFreeScroll = freeScroll;
1678
Adam Cohenf9618852013-11-08 06:45:03 -08001679 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001680 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001681 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001682 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1683 setCurrentPage(mTempVisiblePagesRange[0]);
1684 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1685 setCurrentPage(mTempVisiblePagesRange[1]);
1686 }
1687 }
1688
1689 setEnableOverscroll(!freeScroll);
1690 }
1691
1692 private void setEnableOverscroll(boolean enable) {
1693 mAllowOverScroll = enable;
1694 }
1695
1696 int getNearestHoverOverPageIndex() {
1697 if (mDragView != null) {
1698 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1699 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001700 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001701 int minDistance = Integer.MAX_VALUE;
1702 int minIndex = indexOfChild(mDragView);
1703 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1704 View page = getPageAt(i);
1705 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1706 int d = Math.abs(dragX - pageX);
1707 if (d < minDistance) {
1708 minIndex = i;
1709 minDistance = d;
1710 }
1711 }
1712 return minIndex;
1713 }
1714 return -1;
1715 }
1716
Winson Chung321e9ee2010-08-09 13:37:56 -07001717 @Override
1718 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001719 if (DISABLE_TOUCH_INTERACTION) {
1720 return false;
1721 }
1722
Adam Cohen1697b792013-09-17 19:08:21 -07001723 super.onTouchEvent(ev);
1724
Winson Chung45e1d6e2010-11-09 17:19:49 -08001725 // Skip touch handling if there are no pages to swipe
1726 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1727
Michael Jurkab8f06722010-10-10 15:58:46 -07001728 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001729
1730 final int action = ev.getAction();
1731
1732 switch (action & MotionEvent.ACTION_MASK) {
1733 case MotionEvent.ACTION_DOWN:
1734 /*
1735 * If being flinged and user touches, stop the fling. isFinished
1736 * will be false if being flinged.
1737 */
1738 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001739 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001740 }
1741
1742 // Remember where the motion event started
1743 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001744 mDownMotionY = mLastMotionY = ev.getY();
1745 mDownScrollX = getScrollX();
1746 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1747 mParentDownMotionX = p[0];
1748 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001749 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001750 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001752
Michael Jurka0142d492010-08-25 17:46:15 -07001753 if (mTouchState == TOUCH_STATE_SCROLLING) {
1754 pageBeginMoving();
1755 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001756 break;
1757
1758 case MotionEvent.ACTION_MOVE:
1759 if (mTouchState == TOUCH_STATE_SCROLLING) {
1760 // Scroll to follow the motion event
1761 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001762
1763 if (pointerIndex == -1) return true;
1764
Winson Chung321e9ee2010-08-09 13:37:56 -07001765 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001766 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001767
Adam Cohenaefd4e12011-02-14 16:39:38 -08001768 mTotalMotionX += Math.abs(deltaX);
1769
Winson Chungc0844aa2011-02-02 15:25:58 -08001770 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1771 // keep the remainder because we are actually testing if we've moved from the last
1772 // scrolled position (which is discrete).
1773 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001774 mTouchX += deltaX;
1775 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1776 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001777 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001778 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001779 } else {
1780 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001782 mLastMotionX = x;
1783 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001784 } else {
1785 awakenScrollBars();
1786 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001787 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1788 // Update the last motion position
1789 mLastMotionX = ev.getX();
1790 mLastMotionY = ev.getY();
1791
1792 // Update the parent down so that our zoom animations take this new movement into
1793 // account
1794 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1795 mParentDownMotionX = pt[0];
1796 mParentDownMotionY = pt[1];
1797 updateDragViewTranslationDuringDrag();
1798
1799 // Find the closest page to the touch point
1800 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001801
1802 // Change the drag view if we are hovering over the drop target
1803 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1804 (int) mParentDownMotionX, (int) mParentDownMotionY);
1805 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1806
Adam Cohen7d30a372013-07-01 17:03:59 -07001807 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1808 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1809 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1810 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1811
Adam Cohenf358a4b2013-07-23 16:47:31 -07001812 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1813 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1814 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 mTempVisiblePagesRange[0] = 0;
1816 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001817 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001818 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1819 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1820 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1821 mSidePageHoverIndex = pageUnderPointIndex;
1822 mSidePageHoverRunnable = new Runnable() {
1823 @Override
1824 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 // Setup the scroll to the correct page before we swap the views
1826 snapToPage(pageUnderPointIndex);
1827
1828 // For each of the pages between the paged view and the drag view,
1829 // animate them from the previous position to the new position in
1830 // the layout (as a result of the drag view moving in the layout)
1831 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1832 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1833 dragViewIndex + 1 : pageUnderPointIndex;
1834 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1835 dragViewIndex - 1 : pageUnderPointIndex;
1836 for (int i = lowerIndex; i <= upperIndex; ++i) {
1837 View v = getChildAt(i);
1838 // dragViewIndex < pageUnderPointIndex, so after we remove the
1839 // drag view all subsequent views to pageUnderPointIndex will
1840 // shift down.
1841 int oldX = getViewportOffsetX() + getChildOffset(i);
1842 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1843
1844 // Animate the view translation from its old position to its new
1845 // position
1846 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1847 if (anim != null) {
1848 anim.cancel();
1849 }
1850
1851 v.setTranslationX(oldX - newX);
1852 anim = new AnimatorSet();
1853 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1854 anim.playTogether(
1855 ObjectAnimator.ofFloat(v, "translationX", 0f));
1856 anim.start();
1857 v.setTag(anim);
1858 }
1859
1860 removeView(mDragView);
1861 onRemoveView(mDragView, false);
1862 addView(mDragView, pageUnderPointIndex);
1863 onAddView(mDragView, pageUnderPointIndex);
1864 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001865 if (mPageIndicator != null) {
1866 mPageIndicator.setActiveMarker(getNextPage());
1867 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001868 }
1869 };
1870 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1871 }
1872 } else {
1873 removeCallbacks(mSidePageHoverRunnable);
1874 mSidePageHoverIndex = -1;
1875 }
Adam Cohen564976a2010-10-13 18:52:07 -07001876 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001877 determineScrollingStart(ev);
1878 }
1879 break;
1880
1881 case MotionEvent.ACTION_UP:
1882 if (mTouchState == TOUCH_STATE_SCROLLING) {
1883 final int activePointerId = mActivePointerId;
1884 final int pointerIndex = ev.findPointerIndex(activePointerId);
1885 final float x = ev.getX(pointerIndex);
1886 final VelocityTracker velocityTracker = mVelocityTracker;
1887 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1888 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001889 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001890 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001891 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1892 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001893
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001894 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1895
Adam Cohen00481b32011-11-18 12:03:48 -08001896 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001897 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001898
Adam Cohenf358a4b2013-07-23 16:47:31 -07001899 if (!mFreeScroll) {
1900 // In the case that the page is moved far to one direction and then is flung
1901 // in the opposite direction, we use a threshold to determine whether we should
1902 // just return to the starting page, or if we should skip one further.
1903 boolean returnToOriginalPage = false;
1904 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1905 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1906 returnToOriginalPage = true;
1907 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001908
Adam Cohenf358a4b2013-07-23 16:47:31 -07001909 int finalPage;
1910 // We give flings precedence over large moves, which is why we short-circuit our
1911 // test for a large move if a fling has been registered. That is, a large
1912 // move to the left and fling to the right will register as a fling to the right.
1913 final boolean isRtl = isLayoutRtl();
1914 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1915 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1916 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1917 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1918 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1919 snapToPageWithVelocity(finalPage, velocityX);
1920 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1921 (isFling && isVelocityXLeft)) &&
1922 mCurrentPage < getChildCount() - 1) {
1923 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1924 snapToPageWithVelocity(finalPage, velocityX);
1925 } else {
1926 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001927 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001929 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001930 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 }
1932
1933 float scaleX = getScaleX();
1934 int vX = (int) (-velocityX * scaleX);
1935 int initialScrollX = (int) (getScrollX() * scaleX);
1936
Adam Cohenf9618852013-11-08 06:45:03 -08001937 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001938 mScroller.fling(initialScrollX,
1939 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1940 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 }
Adam Cohencae7f572013-11-04 14:42:52 -08001942 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1943 // at this point we have not moved beyond the touch slop
1944 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1945 // we can just page
1946 int nextPage = Math.max(0, mCurrentPage - 1);
1947 if (nextPage != mCurrentPage) {
1948 snapToPage(nextPage);
1949 } else {
1950 snapToDestination();
1951 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001952 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 // at this point we have not moved beyond the touch slop
1954 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1955 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001956 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1957 if (nextPage != mCurrentPage) {
1958 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 } else {
1960 snapToDestination();
1961 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001962 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1963 // Update the last motion position
1964 mLastMotionX = ev.getX();
1965 mLastMotionY = ev.getY();
1966
1967 // Update the parent down so that our zoom animations take this new movement into
1968 // account
1969 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1970 mParentDownMotionX = pt[0];
1971 mParentDownMotionY = pt[1];
1972 updateDragViewTranslationDuringDrag();
1973 boolean handledFling = false;
1974 if (!DISABLE_FLING_TO_DELETE) {
1975 // Check the velocity and see if we are flinging-to-delete
1976 PointF flingToDeleteVector = isFlingingToDelete();
1977 if (flingToDeleteVector != null) {
1978 onFlingToDelete(flingToDeleteVector);
1979 handledFling = true;
1980 }
1981 }
1982 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1983 (int) mParentDownMotionY)) {
1984 onDropToDelete();
1985 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001986 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001987 if (!mCancelTap) {
1988 onUnhandledTap(ev);
1989 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001990 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001991
1992 // Remove the callback to wait for the side page hover timeout
1993 removeCallbacks(mSidePageHoverRunnable);
1994 // End any intermediate reordering states
1995 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001996 break;
1997
1998 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001999 if (mTouchState == TOUCH_STATE_SCROLLING) {
2000 snapToDestination();
2001 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002002 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 break;
2004
2005 case MotionEvent.ACTION_POINTER_UP:
2006 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002007 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002008 break;
2009 }
2010
2011 return true;
2012 }
2013
Adam Cohen7d30a372013-07-01 17:03:59 -07002014 public void onFlingToDelete(View v) {}
2015 public void onRemoveView(View v, boolean deletePermanently) {}
2016 public void onRemoveViewAnimationCompleted() {}
2017 public void onAddView(View v, int index) {}
2018
2019 private void resetTouchState() {
2020 releaseVelocityTracker();
2021 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002022 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002023 mTouchState = TOUCH_STATE_REST;
2024 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 }
2026
Adam Cohen1697b792013-09-17 19:08:21 -07002027 protected void onUnhandledTap(MotionEvent ev) {
2028 ((Launcher) getContext()).onClick(this);
2029 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002030
Winson Chung185d7162011-02-28 13:47:29 -08002031 @Override
2032 public boolean onGenericMotionEvent(MotionEvent event) {
2033 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2034 switch (event.getAction()) {
2035 case MotionEvent.ACTION_SCROLL: {
2036 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2037 final float vscroll;
2038 final float hscroll;
2039 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2040 vscroll = 0;
2041 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2042 } else {
2043 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2044 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2045 }
2046 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002047 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2048 : (hscroll > 0 || vscroll > 0);
2049 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002050 scrollRight();
2051 } else {
2052 scrollLeft();
2053 }
2054 return true;
2055 }
2056 }
2057 }
2058 }
2059 return super.onGenericMotionEvent(event);
2060 }
2061
Michael Jurkab8f06722010-10-10 15:58:46 -07002062 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2063 if (mVelocityTracker == null) {
2064 mVelocityTracker = VelocityTracker.obtain();
2065 }
2066 mVelocityTracker.addMovement(ev);
2067 }
2068
2069 private void releaseVelocityTracker() {
2070 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002071 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002072 mVelocityTracker.recycle();
2073 mVelocityTracker = null;
2074 }
2075 }
2076
Winson Chung321e9ee2010-08-09 13:37:56 -07002077 private void onSecondaryPointerUp(MotionEvent ev) {
2078 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2079 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2080 final int pointerId = ev.getPointerId(pointerIndex);
2081 if (pointerId == mActivePointerId) {
2082 // This was our active pointer going up. Choose a new
2083 // active pointer and adjust accordingly.
2084 // TODO: Make this decision more intelligent.
2085 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2086 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2087 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002088 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002089 mActivePointerId = ev.getPointerId(newPointerIndex);
2090 if (mVelocityTracker != null) {
2091 mVelocityTracker.clear();
2092 }
2093 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002094 }
2095
Winson Chung321e9ee2010-08-09 13:37:56 -07002096 @Override
2097 public void requestChildFocus(View child, View focused) {
2098 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002099 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002100 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002101 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002102 }
2103 }
2104
Winson Chung1908d072011-02-24 18:09:44 -08002105 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002106 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002107 }
2108
Adam Cohen7d30a372013-07-01 17:03:59 -07002109 int getPageNearestToPoint(float x) {
2110 int index = 0;
2111 for (int i = 0; i < getChildCount(); ++i) {
2112 if (x < getChildAt(i).getRight() - getScrollX()) {
2113 return index;
2114 } else {
2115 index++;
2116 }
2117 }
2118 return Math.min(index, getChildCount() - 1);
2119 }
2120
Adam Cohend19d3ca2010-09-15 14:43:42 -07002121 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002122 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002123 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002124 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002125 final int childCount = getChildCount();
2126 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002127 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002128 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002129 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002130 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002131 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2132 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2133 minDistanceFromScreenCenter = distanceFromScreenCenter;
2134 minDistanceFromScreenCenterIndex = i;
2135 }
2136 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002137 return minDistanceFromScreenCenterIndex;
2138 }
2139
Adam Cohen1e4359c2014-08-18 13:12:16 -07002140 protected boolean isInOverScroll() {
2141 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2142 }
2143
2144 protected int getPageSnapDuration() {
2145 if (isInOverScroll()) {
2146 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2147 }
2148 return PAGE_SNAP_ANIMATION_DURATION;
2149
2150 }
2151
Adam Cohend19d3ca2010-09-15 14:43:42 -07002152 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002153 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002154 }
2155
Adam Cohene0f66b52010-11-23 15:06:07 -08002156 private static class ScrollInterpolator implements Interpolator {
2157 public ScrollInterpolator() {
2158 }
2159
2160 public float getInterpolation(float t) {
2161 t -= 1.0f;
2162 return t*t*t*t*t + 1;
2163 }
2164 }
2165
2166 // We want the duration of the page snap animation to be influenced by the distance that
2167 // the screen has to travel, however, we don't want this duration to be effected in a
2168 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2169 // of travel has on the overall snap duration.
2170 float distanceInfluenceForSnapDuration(float f) {
2171 f -= 0.5f; // center the values about 0.
2172 f *= 0.3f * Math.PI / 2.0f;
2173 return (float) Math.sin(f);
2174 }
2175
Michael Jurka0142d492010-08-25 17:46:15 -07002176 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002177 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002178 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002179
Adam Cohenedb40762013-07-18 16:45:45 -07002180 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002181 int delta = newX - mUnboundedScrollX;
2182 int duration = 0;
2183
Adam Cohen1e4359c2014-08-18 13:12:16 -07002184 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002185 // If the velocity is low enough, then treat this more as an automatic page advance
2186 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002187 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002188 return;
2189 }
2190
2191 // Here we compute a "distance" that will be used in the computation of the overall
2192 // snap duration. This is a function of the actual distance that needs to be traveled;
2193 // we keep this value close to half screen size in order to reduce the variance in snap
2194 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002195 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002196 float distance = halfScreenSize + halfScreenSize *
2197 distanceInfluenceForSnapDuration(distanceRatio);
2198
2199 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002200 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002201
2202 // we want the page's snap velocity to approximately match the velocity at which the
2203 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002204 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2205 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002206
2207 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002208 }
2209
2210 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002211 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002212 }
2213
Adam Cohen7d30a372013-07-01 17:03:59 -07002214 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002215 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002216 }
2217
Michael Jurka0142d492010-08-25 17:46:15 -07002218 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002219 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002220 }
2221
Adam Cohenf9618852013-11-08 06:45:03 -08002222 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2223 snapToPage(whichPage, duration, false, interpolator);
2224 }
2225
2226 protected void snapToPage(int whichPage, int duration, boolean immediate,
2227 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002228 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002229
Adam Cohenedb40762013-07-18 16:45:45 -07002230 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002231 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002232 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002233 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002234 }
2235
2236 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002237 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002238 }
Michael Jurka0142d492010-08-25 17:46:15 -07002239
Adam Cohenf9618852013-11-08 06:45:03 -08002240 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2241 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002242 whichPage = validateNewPage(whichPage);
2243
Adam Cohen7d30a372013-07-01 17:03:59 -07002244 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002245 View focusedChild = getFocusedChild();
2246 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002247 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002248 focusedChild.clearFocus();
2249 }
2250
Adam Cohen53805212013-10-01 10:39:23 -07002251 sendScrollAccessibilityEvent();
2252
Michael Jurka0142d492010-08-25 17:46:15 -07002253 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002254 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002255 if (immediate) {
2256 duration = 0;
2257 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002258 duration = Math.abs(delta);
2259 }
2260
Adam Cohenf358a4b2013-07-23 16:47:31 -07002261 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002262 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002263 }
Adam Cohenf9618852013-11-08 06:45:03 -08002264
2265 if (interpolator != null) {
2266 mScroller.setInterpolator(interpolator);
2267 } else {
2268 mScroller.setInterpolator(mDefaultInterpolator);
2269 }
2270
Adam Cohen68d73932010-11-15 10:50:58 -08002271 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002272
Adam Cohen674531f2013-12-13 15:07:14 -08002273 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002274
2275 // Trigger a compute() to finish switching pages if necessary
2276 if (immediate) {
2277 computeScroll();
2278 }
2279
Winson Chung9c0565f2013-07-19 13:49:06 -07002280 // Defer loading associated pages until the scroll settles
2281 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2282
Adam Cohen7d30a372013-07-01 17:03:59 -07002283 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002284 invalidate();
2285 }
2286
Winson Chung321e9ee2010-08-09 13:37:56 -07002287 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002288 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002289 }
2290
2291 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002292 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002293 }
2294
Winson Chung86f77532010-08-24 11:08:22 -07002295 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002296 int result = -1;
2297 if (v != null) {
2298 ViewParent vp = v.getParent();
2299 int count = getChildCount();
2300 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002301 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002302 return i;
2303 }
2304 }
2305 }
2306 return result;
2307 }
2308
2309 /**
2310 * @return True is long presses are still allowed for the current touch
2311 */
2312 public boolean allowLongPress() {
2313 return mAllowLongPress;
2314 }
2315
Adam Cohendbdff6b2013-09-18 19:09:15 -07002316 @Override
2317 public boolean performLongClick() {
2318 mCancelTap = true;
2319 return super.performLongClick();
2320 }
2321
Michael Jurka0142d492010-08-25 17:46:15 -07002322 /**
2323 * Set true to allow long-press events to be triggered, usually checked by
2324 * {@link Launcher} to accept or block dpad-initiated long-presses.
2325 */
2326 public void setAllowLongPress(boolean allowLongPress) {
2327 mAllowLongPress = allowLongPress;
2328 }
2329
Winson Chung321e9ee2010-08-09 13:37:56 -07002330 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002331 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002332
2333 SavedState(Parcelable superState) {
2334 super(superState);
2335 }
2336
2337 private SavedState(Parcel in) {
2338 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002339 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002340 }
2341
2342 @Override
2343 public void writeToParcel(Parcel out, int flags) {
2344 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002345 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002346 }
2347
2348 public static final Parcelable.Creator<SavedState> CREATOR =
2349 new Parcelable.Creator<SavedState>() {
2350 public SavedState createFromParcel(Parcel in) {
2351 return new SavedState(in);
2352 }
2353
2354 public SavedState[] newArray(int size) {
2355 return new SavedState[size];
2356 }
2357 };
2358 }
2359
Winson Chungf314b0e2011-08-16 11:54:27 -07002360 protected void loadAssociatedPages(int page) {
2361 loadAssociatedPages(page, false);
2362 }
2363 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002364 if (mContentIsRefreshable) {
2365 final int count = getChildCount();
2366 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002367 int lowerPageBound = getAssociatedLowerPageBound(page);
2368 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002369 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2370 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002371 // First, clear any pages that should no longer be loaded
2372 for (int i = 0; i < count; ++i) {
2373 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002374 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002375 if (layout.getPageChildCount() > 0) {
2376 layout.removeAllViewsOnPage();
2377 }
2378 mDirtyPageContent.set(i, true);
2379 }
2380 }
2381 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002382 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002383 if ((i != page) && immediateAndOnly) {
2384 continue;
2385 }
Michael Jurka0142d492010-08-25 17:46:15 -07002386 if (lowerPageBound <= i && i <= upperPageBound) {
2387 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002388 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002389 mDirtyPageContent.set(i, false);
2390 }
Winson Chung86f77532010-08-24 11:08:22 -07002391 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002392 }
2393 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002394 }
2395 }
2396
Winson Chunge3193b92010-09-10 11:44:42 -07002397 protected int getAssociatedLowerPageBound(int page) {
2398 return Math.max(0, page - 1);
2399 }
2400 protected int getAssociatedUpperPageBound(int page) {
2401 final int count = getChildCount();
2402 return Math.min(page + 1, count - 1);
2403 }
2404
Winson Chung86f77532010-08-24 11:08:22 -07002405 /**
2406 * This method is called ONLY to synchronize the number of pages that the paged view has.
2407 * To actually fill the pages with information, implement syncPageItems() below. It is
2408 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2409 * and therefore, individual page items do not need to be updated in this method.
2410 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002411 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002412
2413 /**
2414 * This method is called to synchronize the items that are on a particular page. If views on
2415 * the page can be reused, then they should be updated within this method.
2416 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002417 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002418
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002419 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002420 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002421 }
2422 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002423 invalidatePageData(currentPage, false);
2424 }
2425 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 if (!mIsDataReady) {
2427 return;
2428 }
2429
Michael Jurka0142d492010-08-25 17:46:15 -07002430 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002431 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002432 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002433
Michael Jurka0142d492010-08-25 17:46:15 -07002434 // Update all the pages
2435 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002436
Winson Chung5a808352011-06-27 19:08:49 -07002437 // We must force a measure after we've loaded the pages to update the content width and
2438 // to determine the full scroll width
2439 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2440 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2441
2442 // Set a new page as the current page if necessary
2443 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002444 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002445 }
2446
Michael Jurka0142d492010-08-25 17:46:15 -07002447 // Mark each of the pages as dirty
2448 final int count = getChildCount();
2449 mDirtyPageContent.clear();
2450 for (int i = 0; i < count; ++i) {
2451 mDirtyPageContent.add(true);
2452 }
2453
2454 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002455 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002456 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002457 }
Adam Cohen06559042013-09-29 18:30:56 -07002458 if (isPageMoving()) {
2459 // If the page is moving, then snap it to the final position to ensure we don't get
2460 // stuck between pages
2461 snapToDestination();
2462 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002463 }
Winson Chung007c6982011-06-14 13:27:53 -07002464
Adam Cohen7d30a372013-07-01 17:03:59 -07002465 // Animate the drag view back to the original position
2466 void animateDragViewToOriginalPosition() {
2467 if (mDragView != null) {
2468 AnimatorSet anim = new AnimatorSet();
2469 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2470 anim.playTogether(
2471 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2473 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2474 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002475 anim.addListener(new AnimatorListenerAdapter() {
2476 @Override
2477 public void onAnimationEnd(Animator animation) {
2478 onPostReorderingAnimationCompleted();
2479 }
2480 });
2481 anim.start();
2482 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002483 }
2484
Adam Cohen7d30a372013-07-01 17:03:59 -07002485 protected void onStartReordering() {
2486 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2487 mTouchState = TOUCH_STATE_REORDERING;
2488 mIsReordering = true;
2489
Adam Cohen7d30a372013-07-01 17:03:59 -07002490 // We must invalidate to trigger a redraw to update the layers such that the drag view
2491 // is always drawn on top
2492 invalidate();
2493 }
2494
2495 private void onPostReorderingAnimationCompleted() {
2496 // Trigger the callback when reordering has settled
2497 --mPostReorderingPreZoomInRemainingAnimationCount;
2498 if (mPostReorderingPreZoomInRunnable != null &&
2499 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2500 mPostReorderingPreZoomInRunnable.run();
2501 mPostReorderingPreZoomInRunnable = null;
2502 }
2503 }
2504
2505 protected void onEndReordering() {
2506 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002507 }
2508
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002510 int dragViewIndex = indexOfChild(v);
2511
Adam Cohen327acfe2014-06-06 11:52:52 -07002512 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002513
Adam Cohen7d30a372013-07-01 17:03:59 -07002514 mTempVisiblePagesRange[0] = 0;
2515 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002516 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002517 mReorderingStarted = true;
2518
2519 // Check if we are within the reordering range
2520 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002521 dragViewIndex <= mTempVisiblePagesRange[1]) {
2522 // Find the drag view under the pointer
2523 mDragView = getChildAt(dragViewIndex);
2524 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2525 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002526 snapToPage(getPageNearestToCenterOfScreen());
2527 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002528 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002529 return true;
2530 }
2531 return false;
2532 }
2533
2534 boolean isReordering(boolean testTouchState) {
2535 boolean state = mIsReordering;
2536 if (testTouchState) {
2537 state &= (mTouchState == TOUCH_STATE_REORDERING);
2538 }
2539 return state;
2540 }
2541 void endReordering() {
2542 // For simplicity, we call endReordering sometimes even if reordering was never started.
2543 // In that case, we don't want to do anything.
2544 if (!mReorderingStarted) return;
2545 mReorderingStarted = false;
2546
2547 // If we haven't flung-to-delete the current child, then we just animate the drag view
2548 // back into position
2549 final Runnable onCompleteRunnable = new Runnable() {
2550 @Override
2551 public void run() {
2552 onEndReordering();
2553 }
2554 };
2555 if (!mDeferringForDelete) {
2556 mPostReorderingPreZoomInRunnable = new Runnable() {
2557 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002558 onCompleteRunnable.run();
2559 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002560 };
2561 };
2562
2563 mPostReorderingPreZoomInRemainingAnimationCount =
2564 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2565 // Snap to the current page
2566 snapToPage(indexOfChild(mDragView), 0);
2567 // Animate the drag view back to the front position
2568 animateDragViewToOriginalPosition();
2569 } else {
2570 // Handled in post-delete-animation-callbacks
2571 }
2572 }
2573
Adam Cohen7d30a372013-07-01 17:03:59 -07002574 /*
2575 * Flinging to delete - IN PROGRESS
2576 */
2577 private PointF isFlingingToDelete() {
2578 ViewConfiguration config = ViewConfiguration.get(getContext());
2579 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2580
2581 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2582 // Do a quick dot product test to ensure that we are flinging upwards
2583 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2584 mVelocityTracker.getYVelocity());
2585 PointF upVec = new PointF(0f, -1f);
2586 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2587 (vel.length() * upVec.length()));
2588 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2589 return vel;
2590 }
2591 }
2592 return null;
2593 }
2594
2595 /**
2596 * Creates an animation from the current drag view along its current velocity vector.
2597 * For this animation, the alpha runs for a fixed duration and we update the position
2598 * progressively.
2599 */
2600 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2601 private View mDragView;
2602 private PointF mVelocity;
2603 private Rect mFrom;
2604 private long mPrevTime;
2605 private float mFriction;
2606
2607 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2608
2609 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2610 long startTime, float friction) {
2611 mDragView = dragView;
2612 mVelocity = vel;
2613 mFrom = from;
2614 mPrevTime = startTime;
2615 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2616 }
2617
2618 @Override
2619 public void onAnimationUpdate(ValueAnimator animation) {
2620 float t = ((Float) animation.getAnimatedValue()).floatValue();
2621 long curTime = AnimationUtils.currentAnimationTimeMillis();
2622
2623 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2624 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2625
2626 mDragView.setTranslationX(mFrom.left);
2627 mDragView.setTranslationY(mFrom.top);
2628 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2629
2630 mVelocity.x *= mFriction;
2631 mVelocity.y *= mFriction;
2632 mPrevTime = curTime;
2633 }
2634 };
2635
2636 private static final int ANIM_TAG_KEY = 100;
2637
2638 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2639 return new Runnable() {
2640 @Override
2641 public void run() {
2642 int dragViewIndex = indexOfChild(dragView);
2643
2644 // For each of the pages around the drag view, animate them from the previous
2645 // position to the new position in the layout (as a result of the drag view moving
2646 // in the layout)
2647 // NOTE: We can make an assumption here because we have side-bound pages that we
2648 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002649 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002650 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2651 boolean slideFromLeft = (isLastWidgetPage ||
2652 dragViewIndex > mTempVisiblePagesRange[0]);
2653
2654 // Setup the scroll to the correct page before we swap the views
2655 if (slideFromLeft) {
2656 snapToPageImmediately(dragViewIndex - 1);
2657 }
2658
2659 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2660 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2661 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2662 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2663 ArrayList<Animator> animations = new ArrayList<Animator>();
2664 for (int i = lowerIndex; i <= upperIndex; ++i) {
2665 View v = getChildAt(i);
2666 // dragViewIndex < pageUnderPointIndex, so after we remove the
2667 // drag view all subsequent views to pageUnderPointIndex will
2668 // shift down.
2669 int oldX = 0;
2670 int newX = 0;
2671 if (slideFromLeft) {
2672 if (i == 0) {
2673 // Simulate the page being offscreen with the page spacing
2674 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002675 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002676 } else {
2677 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2678 }
2679 newX = getViewportOffsetX() + getChildOffset(i);
2680 } else {
2681 oldX = getChildOffset(i) - getChildOffset(i - 1);
2682 newX = 0;
2683 }
2684
2685 // Animate the view translation from its old position to its new
2686 // position
2687 AnimatorSet anim = (AnimatorSet) v.getTag();
2688 if (anim != null) {
2689 anim.cancel();
2690 }
2691
2692 // Note: Hacky, but we want to skip any optimizations to not draw completely
2693 // hidden views
2694 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2695 v.setTranslationX(oldX - newX);
2696 anim = new AnimatorSet();
2697 anim.playTogether(
2698 ObjectAnimator.ofFloat(v, "translationX", 0f),
2699 ObjectAnimator.ofFloat(v, "alpha", 1f));
2700 animations.add(anim);
2701 v.setTag(ANIM_TAG_KEY, anim);
2702 }
2703
2704 AnimatorSet slideAnimations = new AnimatorSet();
2705 slideAnimations.playTogether(animations);
2706 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2707 slideAnimations.addListener(new AnimatorListenerAdapter() {
2708 @Override
2709 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002710 mDeferringForDelete = false;
2711 onEndReordering();
2712 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002713 }
2714 });
2715 slideAnimations.start();
2716
2717 removeView(dragView);
2718 onRemoveView(dragView, true);
2719 }
2720 };
2721 }
2722
2723 public void onFlingToDelete(PointF vel) {
2724 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2725
2726 // NOTE: Because it takes time for the first frame of animation to actually be
2727 // called and we expect the animation to be a continuation of the fling, we have
2728 // to account for the time that has elapsed since the fling finished. And since
2729 // we don't have a startDelay, we will always get call to update when we call
2730 // start() (which we want to ignore).
2731 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2732 private int mCount = -1;
2733 private long mStartTime;
2734 private float mOffset;
2735 /* Anonymous inner class ctor */ {
2736 mStartTime = startTime;
2737 }
2738
2739 @Override
2740 public float getInterpolation(float t) {
2741 if (mCount < 0) {
2742 mCount++;
2743 } else if (mCount == 0) {
2744 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2745 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2746 mCount++;
2747 }
2748 return Math.min(1f, mOffset + t);
2749 }
2750 };
2751
2752 final Rect from = new Rect();
2753 final View dragView = mDragView;
2754 from.left = (int) dragView.getTranslationX();
2755 from.top = (int) dragView.getTranslationY();
2756 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2757 from, startTime, FLING_TO_DELETE_FRICTION);
2758
2759 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2760
2761 // Create and start the animation
2762 ValueAnimator mDropAnim = new ValueAnimator();
2763 mDropAnim.setInterpolator(tInterpolator);
2764 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2765 mDropAnim.setFloatValues(0f, 1f);
2766 mDropAnim.addUpdateListener(updateCb);
2767 mDropAnim.addListener(new AnimatorListenerAdapter() {
2768 public void onAnimationEnd(Animator animation) {
2769 onAnimationEndRunnable.run();
2770 }
2771 });
2772 mDropAnim.start();
2773 mDeferringForDelete = true;
2774 }
2775
2776 /* Drag to delete */
2777 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2778 if (mDeleteDropTarget != null) {
2779 mAltTmpRect.set(0, 0, 0, 0);
2780 View parent = (View) mDeleteDropTarget.getParent();
2781 if (parent != null) {
2782 parent.getGlobalVisibleRect(mAltTmpRect);
2783 }
2784 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2785 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2786 return mTmpRect.contains(x, y);
2787 }
2788 return false;
2789 }
2790
2791 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2792
2793 private void onDropToDelete() {
2794 final View dragView = mDragView;
2795
2796 final float toScale = 0f;
2797 final float toAlpha = 0f;
2798
2799 // Create and start the complex animation
2800 ArrayList<Animator> animations = new ArrayList<Animator>();
2801 AnimatorSet motionAnim = new AnimatorSet();
2802 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2803 motionAnim.playTogether(
2804 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2805 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2806 animations.add(motionAnim);
2807
2808 AnimatorSet alphaAnim = new AnimatorSet();
2809 alphaAnim.setInterpolator(new LinearInterpolator());
2810 alphaAnim.playTogether(
2811 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2812 animations.add(alphaAnim);
2813
2814 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2815
2816 AnimatorSet anim = new AnimatorSet();
2817 anim.playTogether(animations);
2818 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2819 anim.addListener(new AnimatorListenerAdapter() {
2820 public void onAnimationEnd(Animator animation) {
2821 onAnimationEndRunnable.run();
2822 }
2823 });
2824 anim.start();
2825
2826 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002827 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002828
2829 /* Accessibility */
2830 @Override
2831 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2832 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002833 info.setScrollable(getPageCount() > 1);
2834 if (getCurrentPage() < getPageCount() - 1) {
2835 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2836 }
2837 if (getCurrentPage() > 0) {
2838 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2839 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002840 }
2841
2842 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002843 public void sendAccessibilityEvent(int eventType) {
2844 // Don't let the view send real scroll events.
2845 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2846 super.sendAccessibilityEvent(eventType);
2847 }
2848 }
2849
2850 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002851 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2852 super.onInitializeAccessibilityEvent(event);
2853 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002854 }
2855
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002856 @Override
2857 public boolean performAccessibilityAction(int action, Bundle arguments) {
2858 if (super.performAccessibilityAction(action, arguments)) {
2859 return true;
2860 }
2861 switch (action) {
2862 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2863 if (getCurrentPage() < getPageCount() - 1) {
2864 scrollRight();
2865 return true;
2866 }
2867 } break;
2868 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2869 if (getCurrentPage() > 0) {
2870 scrollLeft();
2871 return true;
2872 }
2873 } break;
2874 }
2875 return false;
2876 }
2877
Adam Cohen0ffac432013-07-10 11:19:26 -07002878 protected String getCurrentPageDescription() {
2879 return String.format(getContext().getString(R.string.default_scroll_format),
2880 getNextPage() + 1, getChildCount());
2881 }
2882
Winson Chungd11265e2011-08-30 13:37:23 -07002883 @Override
2884 public boolean onHoverEvent(android.view.MotionEvent event) {
2885 return true;
2886 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002887}