blob: 158a30c403647dccb5744008c3013cecc2aeacf3 [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
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
55
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chungc58497e2013-09-03 17:48:37 -070058interface Page {
59 public int getPageChildCount();
60 public View getChildOnPageAt(int i);
61 public void removeAllViewsOnPage();
62 public void removeViewOnPageAt(int i);
63 public int indexOfChildOnPage(View v);
64}
65
Winson Chung321e9ee2010-08-09 13:37:56 -070066/**
67 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070068 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070069 */
Michael Jurka8b805b12012-04-18 14:23:14 -070070public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070071 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070072 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070073 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070074
Winson Chung86f77532010-08-24 11:08:22 -070075 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070076 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Adam Cohen7d30a372013-07-01 17:03:59 -070078 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070079 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070080 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Adam Cohenb5ba0972011-09-07 18:02:31 -070083 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070084 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080085
Adam Cohenb64cb5a2011-02-15 13:53:42 -080086 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080087 // The page is moved more than halfway, automatically move to the next page on touch up.
88 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080089
Adam Cohen265b9a62011-12-07 14:37:18 -080090 // The following constants need to be scaled based on density. The scaled versions will be
91 // assigned to the corresponding member variables below.
92 private static final int FLING_THRESHOLD_VELOCITY = 500;
93 private static final int MIN_SNAP_VELOCITY = 1500;
94 private static final int MIN_FLING_VELOCITY = 250;
95
Adam Cohen7d30a372013-07-01 17:03:59 -070096 // We are disabling touch interaction of the widget region for factory ROM.
97 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070098 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070099 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700100
Adam Cohen21cd0022013-10-09 18:57:02 -0700101 public static final int INVALID_RESTORE_PAGE = -1001;
102
Adam Cohenf358a4b2013-07-23 16:47:31 -0700103 private boolean mFreeScroll = false;
104 private int mFreeScrollMinScrollX = -1;
105 private int mFreeScrollMaxScrollX = -1;
106
Winson Chung8aad6102012-05-11 16:27:49 -0700107 static final int AUTOMATIC_PAGE_SPACING = -1;
108
Adam Cohen265b9a62011-12-07 14:37:18 -0800109 protected int mFlingThresholdVelocity;
110 protected int mMinFlingVelocity;
111 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700112
Adam Cohenb5ba0972011-09-07 18:02:31 -0700113 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700114 protected float mSmoothingTime;
115 protected float mTouchX;
116
117 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700118 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700119
120 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700121 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700122 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700123
Michael Jurka0142d492010-08-25 17:46:15 -0700124 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800125 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800126 protected LauncherScroller mScroller;
127 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 private float mParentDownMotionX;
132 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700134 private float mDownMotionY;
135 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700136 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800137 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800138 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800139 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800140 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700141 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700142
Adam Cohendbdff6b2013-09-18 19:09:15 -0700143 private boolean mCancelTap;
144
Adam Cohenedb40762013-07-18 16:45:45 -0700145 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Michael Jurka0142d492010-08-25 17:46:15 -0700147 protected final static int TOUCH_STATE_REST = 0;
148 protected final static int TOUCH_STATE_SCROLLING = 1;
149 protected final static int TOUCH_STATE_PREV_PAGE = 2;
150 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700151 protected final static int TOUCH_STATE_REORDERING = 4;
152
Adam Cohene45440e2010-10-14 18:33:38 -0700153 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700156 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800157
Michael Jurka0142d492010-08-25 17:46:15 -0700158 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159
Michael Jurka7426c422010-11-11 15:23:47 -0800160 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 private int mPagingTouchSlop;
162 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700163 protected int mPageLayoutWidthGap;
164 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700165 protected int mCellCountX = 0;
166 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700167 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800168 protected boolean mAllowOverScroll = true;
169 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800170 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700171 protected boolean mForceDrawAllChildrenNextFrame;
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700172 private boolean mSpacePagesAutomatically = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700173
Michael Jurka8b805b12012-04-18 14:23:14 -0700174 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800175 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
176 // the screens from continuing to translate beyond the normal bounds.
177 protected int mOverScrollX;
178
Michael Jurka5f1c5092010-09-03 14:15:02 -0700179 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Michael Jurka5f1c5092010-09-03 14:15:02 -0700181 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Winson Chung86f77532010-08-24 11:08:22 -0700183 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Michael Jurkae326f182011-11-21 14:05:46 -0800185 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Michael Jurka0142d492010-08-25 17:46:15 -0700187 // If true, syncPages and syncPageItems will be called to refresh pages
188 protected boolean mContentIsRefreshable = true;
189
190 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700191 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700192
193 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
194 // to switch to a new page
195 protected boolean mUsePagingTouchSlop = true;
196
Michael Jurka8b805b12012-04-18 14:23:14 -0700197 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700198 // (SmoothPagedView does this)
199 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700200 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700201
Patrick Dubroy1262e362010-10-06 15:49:50 -0700202 protected boolean mIsPageMoving = false;
203
Winson Chungf0ea4d32011-06-06 14:27:16 -0700204 // All syncs and layout passes are deferred until data is ready.
205 protected boolean mIsDataReady = false;
206
Adam Cohen7d30a372013-07-01 17:03:59 -0700207 protected boolean mAllowLongPress = true;
208
Adam Cohenc2d6e892014-10-16 09:49:24 -0700209 private boolean mWasInOverscroll = false;
210
Winson Chungd2be3812013-07-16 11:11:32 -0700211 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk int mPageIndicatorViewId;
213 @Thunk PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700214 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700215
Adam Cohen7d30a372013-07-01 17:03:59 -0700216 // The viewport whether the pages are to be contained (the actual view may be larger than the
217 // viewport)
218 private Rect mViewport = new Rect();
219
220 // Reordering
221 // We use the min scale to determine how much to expand the actually PagedView measured
222 // dimensions such that when we are zoomed out, the view is not clipped
223 private int REORDERING_DROP_REPOSITION_DURATION = 200;
224 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
225 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700226 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700227 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700228 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 protected View mDragView;
230 protected AnimatorSet mZoomInOutAnim;
231 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700233 // This variable's scope is only for the duration of startReordering() and endReordering()
234 private boolean mReorderingStarted = false;
235 // This variable's scope is for the duration of startReordering() and after the zoomIn()
236 // animation after endReordering()
237 private boolean mIsReordering;
238 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
239 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
240 private int mPostReorderingPreZoomInRemainingAnimationCount;
241 private Runnable mPostReorderingPreZoomInRunnable;
242
Adam Cohen7d30a372013-07-01 17:03:59 -0700243 // Convenience/caching
244 private Matrix mTmpInvMatrix = new Matrix();
245 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700246 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700247 private Rect mTmpRect = new Rect();
248 private Rect mAltTmpRect = new Rect();
249
250 // Fling to delete
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
Adam Cohen7d30a372013-07-01 17:03:59 -0700252 private float FLING_TO_DELETE_FRICTION = 0.035f;
253 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
254 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
255 protected int mFlingToDeleteThresholdVelocity = -1400;
256 // Drag to delete
Adam Cohen091440a2015-03-18 14:16:05 -0700257 @Thunk boolean mDeferringForDelete = false;
258 @Thunk int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
Adam Cohen7d30a372013-07-01 17:03:59 -0700259 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
260
261 // Drop to delete
262 private View mDeleteDropTarget;
263
Adam Cohen7d30a372013-07-01 17:03:59 -0700264 // Bouncer
265 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700266
John Spurlock77e1f472013-09-11 10:09:51 -0400267 protected final Rect mInsets = new Rect();
268
Winson Chung86f77532010-08-24 11:08:22 -0700269 public interface PageSwitchListener {
270 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 }
272
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 public PagedView(Context context) {
274 this(context, null);
275 }
276
277 public PagedView(Context context, AttributeSet attrs) {
278 this(context, attrs, 0);
279 }
280
281 public PagedView(Context context, AttributeSet attrs, int defStyle) {
282 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700283
Adam Cohen9c4949e2010-10-05 12:27:22 -0700284 TypedArray a = context.obtainStyledAttributes(attrs,
285 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700286
Winson Chungdf4b83d2010-10-20 17:49:27 -0700287 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700288 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700289 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700290 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700291 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700292 a.recycle();
293
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700295 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 }
297
298 /**
299 * Initializes various states for this workspace.
300 */
Michael Jurka0142d492010-08-25 17:46:15 -0700301 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700302 mDirtyPageContent = new ArrayList<Boolean>();
303 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800304 mScroller = new LauncherScroller(getContext());
305 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700306 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700307 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700308
309 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700310 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
312 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700313 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800314
Adam Cohen7d30a372013-07-01 17:03:59 -0700315 // Scale the fling-to-delete threshold by the density
316 mFlingToDeleteThresholdVelocity =
317 (int) (mFlingToDeleteThresholdVelocity * mDensity);
318
Adam Cohen265b9a62011-12-07 14:37:18 -0800319 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
320 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
321 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700322 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 }
324
Adam Cohenf9618852013-11-08 06:45:03 -0800325 protected void setDefaultInterpolator(Interpolator interpolator) {
326 mDefaultInterpolator = interpolator;
327 mScroller.setInterpolator(mDefaultInterpolator);
328 }
329
Winson Chungd2be3812013-07-16 11:11:32 -0700330 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700331 super.onAttachedToWindow();
332
Winson Chungd2be3812013-07-16 11:11:32 -0700333 // Hook up the page indicator
334 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700335 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700336 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700337 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700338 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700339
Winson Chung7819a562013-09-19 15:55:45 -0700340 ArrayList<PageIndicator.PageMarkerResources> markers =
341 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700342 for (int i = 0; i < getChildCount(); ++i) {
343 markers.add(getPageIndicatorMarker(i));
344 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700345
Winson Chungc58497e2013-09-03 17:48:37 -0700346 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700347
348 OnClickListener listener = getPageIndicatorClickListener();
349 if (listener != null) {
350 mPageIndicator.setOnClickListener(listener);
351 }
Adam Cohen53805212013-10-01 10:39:23 -0700352 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700353 }
354 }
355
Adam Cohen53805212013-10-01 10:39:23 -0700356 protected String getPageIndicatorDescription() {
357 return getCurrentPageDescription();
358 }
359
360 protected OnClickListener getPageIndicatorClickListener() {
361 return null;
362 }
363
Winson Chungd2be3812013-07-16 11:11:32 -0700364 protected void onDetachedFromWindow() {
365 // Unhook the page indicator
366 mPageIndicator = null;
367 }
368
Adam Cohen7d30a372013-07-01 17:03:59 -0700369 void setDeleteDropTarget(View v) {
370 mDeleteDropTarget = v;
371 }
372
373 // Convenience methods to map points from self to parent and vice versa
374 float[] mapPointFromViewToParent(View v, float x, float y) {
375 mTmpPoint[0] = x;
376 mTmpPoint[1] = y;
377 v.getMatrix().mapPoints(mTmpPoint);
378 mTmpPoint[0] += v.getLeft();
379 mTmpPoint[1] += v.getTop();
380 return mTmpPoint;
381 }
382 float[] mapPointFromParentToView(View v, float x, float y) {
383 mTmpPoint[0] = x - v.getLeft();
384 mTmpPoint[1] = y - v.getTop();
385 v.getMatrix().invert(mTmpInvMatrix);
386 mTmpInvMatrix.mapPoints(mTmpPoint);
387 return mTmpPoint;
388 }
389
390 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700391 if (mDragView != null) {
392 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
393 (mDragViewBaselineLeft - mDragView.getLeft());
394 float y = mLastMotionY - mDownMotionY;
395 mDragView.setTranslationX(x);
396 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700397
Adam Cohenf358a4b2013-07-23 16:47:31 -0700398 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
399 + x + ", " + y);
400 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700401 }
402
403 public void setMinScale(float f) {
404 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700405 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700406 requestLayout();
407 }
408
409 @Override
410 public void setScaleX(float scaleX) {
411 super.setScaleX(scaleX);
412 if (isReordering(true)) {
413 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
414 mLastMotionX = p[0];
415 mLastMotionY = p[1];
416 updateDragViewTranslationDuringDrag();
417 }
418 }
419
420 // Convenience methods to get the actual width/height of the PagedView (since it is measured
421 // to be larger to account for the minimum possible scale)
422 int getViewportWidth() {
423 return mViewport.width();
424 }
425 int getViewportHeight() {
426 return mViewport.height();
427 }
428
429 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
430 // PagedView both horizontally and vertically
431 int getViewportOffsetX() {
432 return (getMeasuredWidth() - getViewportWidth()) / 2;
433 }
434
435 int getViewportOffsetY() {
436 return (getMeasuredHeight() - getViewportHeight()) / 2;
437 }
438
Adam Cohenedb40762013-07-18 16:45:45 -0700439 PageIndicator getPageIndicator() {
440 return mPageIndicator;
441 }
Winson Chung7819a562013-09-19 15:55:45 -0700442 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
443 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700444 }
Adam Cohenedb40762013-07-18 16:45:45 -0700445
Adam Cohen674531f2013-12-13 15:07:14 -0800446 /**
447 * Add a page change listener which will be called when a page is _finished_ listening.
448 *
449 */
Winson Chung86f77532010-08-24 11:08:22 -0700450 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
451 mPageSwitchListener = pageSwitchListener;
452 if (mPageSwitchListener != null) {
453 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
455 }
456
457 /**
Winson Chung52aee602013-01-30 12:01:02 -0800458 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
459 */
460 public boolean isLayoutRtl() {
461 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
462 }
463
464 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700465 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
466 * out pages.
467 */
468 protected void setDataIsReady() {
469 mIsDataReady = true;
470 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700471
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 protected boolean isDataReady() {
473 return mIsDataReady;
474 }
475
476 /**
Winson Chung86f77532010-08-24 11:08:22 -0700477 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 *
Winson Chung86f77532010-08-24 11:08:22 -0700479 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 */
Winson Chung86f77532010-08-24 11:08:22 -0700481 int getCurrentPage() {
482 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700484
Winson Chung360e63f2012-04-27 13:48:05 -0700485 int getNextPage() {
486 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
487 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700488
Winson Chung86f77532010-08-24 11:08:22 -0700489 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 return getChildCount();
491 }
492
Winson Chung86f77532010-08-24 11:08:22 -0700493 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 return getChildAt(index);
495 }
496
Adam Cohenae4f1552011-10-20 00:15:42 -0700497 protected int indexToPage(int index) {
498 return index;
499 }
500
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800502 * Updates the scroll of the current page immediately to its final scroll position. We use this
503 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
504 * the previous tab page.
505 */
506 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700507 // If the current page is invalid, just reset the scroll position to zero
508 int newX = 0;
509 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700510 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700511 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800512 scrollTo(newX, 0);
513 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700514 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800515 }
516
517 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700518 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700519 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
520 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700521 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700522 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700523 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800524 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700525 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700526 }
527
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700528 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700529 mScroller.abortAnimation();
530 // We need to clean up the next page here to avoid computeScrollHelper from
531 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700532 if (resetNextPage) {
533 mNextPage = INVALID_PAGE;
534 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700535 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700536
537 private void forceFinishScroller() {
538 mScroller.forceFinished(true);
539 // We need to clean up the next page here to avoid computeScrollHelper from
540 // updating current page on the pass.
541 mNextPage = INVALID_PAGE;
542 }
543
Adam Cohen6f127a62014-06-12 14:54:41 -0700544 private int validateNewPage(int newPage) {
545 int validatedPage = newPage;
546 // When in free scroll mode, we need to clamp to the free scroll page range.
547 if (mFreeScroll) {
548 getFreeScrollPageRange(mTempVisiblePagesRange);
549 validatedPage = Math.max(mTempVisiblePagesRange[0],
550 Math.min(newPage, mTempVisiblePagesRange[1]));
551 }
552 // Ensure that it is clamped by the actual set of children in all cases
553 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
554 return validatedPage;
555 }
556
Chet Haasebc2f0822012-10-26 17:59:53 -0700557 /**
Winson Chung86f77532010-08-24 11:08:22 -0700558 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 */
Winson Chung86f77532010-08-24 11:08:22 -0700560 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800561 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700562 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800563 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800564 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
565 // the default
566 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800567 return;
568 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700569 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700570 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700571 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700572 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800573 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 }
575
Winson Chung8c87cd82013-07-23 16:20:10 -0700576 /**
577 * The restore page will be set in place of the current page at the next (likely first)
578 * layout.
579 */
580 void setRestorePage(int restorePage) {
581 mRestorePage = restorePage;
582 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800583 int getRestorePage() {
584 return mRestorePage;
585 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700586
Adam Cohen674531f2013-12-13 15:07:14 -0800587 /**
588 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
589 * has settled.
590 */
Michael Jurka0142d492010-08-25 17:46:15 -0700591 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700592 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800593 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
Winson Chungd2be3812013-07-16 11:11:32 -0700595
Adam Cohen674531f2013-12-13 15:07:14 -0800596 updatePageIndicator();
597 }
598
599 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700600 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700601 if (mPageIndicator != null) {
602 mPageIndicator.setContentDescription(getPageIndicatorDescription());
603 if (!isReordering(false)) {
604 mPageIndicator.setActiveMarker(getNextPage());
605 }
Winson Chungd2be3812013-07-16 11:11:32 -0700606 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700607 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800608 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700609 if (!mIsPageMoving) {
610 mIsPageMoving = true;
611 onPageBeginMoving();
612 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700613 }
614
Michael Jurkace7e05f2011-02-01 22:02:35 -0800615 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700616 if (mIsPageMoving) {
617 mIsPageMoving = false;
618 onPageEndMoving();
619 }
Michael Jurka0142d492010-08-25 17:46:15 -0700620 }
621
Adam Cohen26976d92011-03-22 15:33:33 -0700622 protected boolean isPageMoving() {
623 return mIsPageMoving;
624 }
625
Michael Jurka0142d492010-08-25 17:46:15 -0700626 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700627 protected void onPageBeginMoving() {
628 }
629
630 // a method that subclasses can override to add behavior
631 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700632 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700633 }
634
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 /**
Winson Chung86f77532010-08-24 11:08:22 -0700636 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 *
638 * @param l The listener used to respond to long clicks.
639 */
640 @Override
641 public void setOnLongClickListener(OnLongClickListener l) {
642 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700643 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700645 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
Adam Cohen1697b792013-09-17 19:08:21 -0700647 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 }
649
650 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800651 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700652 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800653 }
654
655 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700656 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700657 // In free scroll mode, we clamp the scrollX
658 if (mFreeScroll) {
659 x = Math.min(x, mFreeScrollMaxScrollX);
660 x = Math.max(x, mFreeScrollMinScrollX);
661 }
662
Adam Cohen0ffac432013-07-10 11:19:26 -0700663 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800664 mUnboundedScrollX = x;
665
Adam Cohen0ffac432013-07-10 11:19:26 -0700666 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
667 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
668 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800669 super.scrollTo(0, y);
670 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700671 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700672 if (isRtl) {
673 overScroll(x - mMaxScrollX);
674 } else {
675 overScroll(x);
676 }
Adam Cohen68d73932010-11-15 10:50:58 -0800677 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700678 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800679 super.scrollTo(mMaxScrollX, y);
680 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700681 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700682 if (isRtl) {
683 overScroll(x);
684 } else {
685 overScroll(x - mMaxScrollX);
686 }
Adam Cohen68d73932010-11-15 10:50:58 -0800687 }
688 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700689 if (mWasInOverscroll) {
690 overScroll(0);
691 mWasInOverscroll = false;
692 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800693 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800694 super.scrollTo(x, y);
695 }
696
Michael Jurka0142d492010-08-25 17:46:15 -0700697 mTouchX = x;
698 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700699
700 // Update the last motion events when scrolling
701 if (isReordering(true)) {
702 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
703 mLastMotionX = p[0];
704 mLastMotionY = p[1];
705 updateDragViewTranslationDuringDrag();
706 }
Michael Jurka0142d492010-08-25 17:46:15 -0700707 }
708
Adam Cohen53805212013-10-01 10:39:23 -0700709 private void sendScrollAccessibilityEvent() {
710 AccessibilityManager am =
711 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
712 if (am.isEnabled()) {
713 AccessibilityEvent ev =
714 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700715 ev.setItemCount(getChildCount());
716 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700717 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700718
Alan Viverette254139a2013-10-08 13:13:46 -0700719 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700720 if (getNextPage() >= mCurrentPage) {
721 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
722 } else {
723 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
724 }
725
726 ev.setAction(action);
727 sendAccessibilityEventUnchecked(ev);
728 }
729 }
730
Michael Jurka0142d492010-08-25 17:46:15 -0700731 // we moved this functionality to a helper function so SmoothPagedView can reuse it
732 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 if (getScrollX() != mScroller.getCurrX()
736 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700737 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700738 float scaleX = mFreeScroll ? getScaleX() : 1f;
739 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
740 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700741 }
Michael Jurka0142d492010-08-25 17:46:15 -0700742 invalidate();
743 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700744 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700745 sendScrollAccessibilityEvent();
746
Adam Cohen6f127a62014-06-12 14:54:41 -0700747 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700748 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700749 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700750
Winson Chung9c0565f2013-07-19 13:49:06 -0700751 // Load the associated pages if necessary
752 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
753 loadAssociatedPages(mCurrentPage);
754 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
755 }
756
Adam Cohen73aa9752010-11-24 16:26:58 -0800757 // We don't want to trigger a page end moving unless the page has settled
758 // and the user has stopped scrolling
759 if (mTouchState == TOUCH_STATE_REST) {
760 pageEndMoving();
761 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700762
Adam Cohen7d30a372013-07-01 17:03:59 -0700763 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700764 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700765 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700766 if (am.isEnabled()) {
767 // Notify the user when the page changes
768 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700769 }
Michael Jurka0142d492010-08-25 17:46:15 -0700770 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
Michael Jurka0142d492010-08-25 17:46:15 -0700772 return false;
773 }
774
775 @Override
776 public void computeScroll() {
777 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 }
779
Adam Cohen7d30a372013-07-01 17:03:59 -0700780 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
781 return mTopAlignPageWhenShrinkingForBouncer;
782 }
783
Adam Cohen96d30a12013-07-16 18:13:21 -0700784 public static class LayoutParams extends ViewGroup.LayoutParams {
785 public boolean isFullScreenPage = false;
786
787 /**
788 * {@inheritDoc}
789 */
790 public LayoutParams(int width, int height) {
791 super(width, height);
792 }
793
794 public LayoutParams(ViewGroup.LayoutParams source) {
795 super(source);
796 }
797 }
798
799 protected LayoutParams generateDefaultLayoutParams() {
800 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
801 }
802
Adam Cohenedb40762013-07-18 16:45:45 -0700803 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700804 LayoutParams lp = generateDefaultLayoutParams();
805 lp.isFullScreenPage = true;
806 super.addView(page, 0, lp);
807 }
808
Adam Cohen410f3cd2013-09-22 12:09:32 -0700809 public int getNormalChildHeight() {
810 return mNormalChildHeight;
811 }
812
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 @Override
814 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700816 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
817 return;
818 }
819
Adam Cohen7d30a372013-07-01 17:03:59 -0700820 // We measure the dimensions of the PagedView to be larger than the pages so that when we
821 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700822 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
823 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
824 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700825 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800826 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700827 // viewport, we can be at most one and a half screens offset once we scale down
828 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700829 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
830 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700831
Winson Chungc82d2622013-11-06 13:23:29 -0800832 int parentWidthSize = (int) (2f * maxSize);
833 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700834 int scaledWidthSize, scaledHeightSize;
835 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700836 scaledWidthSize = (int) (parentWidthSize / mMinScale);
837 scaledHeightSize = (int) (parentHeightSize / mMinScale);
838 } else {
839 scaledWidthSize = widthSize;
840 scaledHeightSize = heightSize;
841 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700842 mViewport.set(0, 0, widthSize, heightSize);
843
844 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
845 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
846 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848
Winson Chung8aad6102012-05-11 16:27:49 -0700849 // Return early if we aren't given a proper dimension
850 if (widthSize <= 0 || heightSize <= 0) {
851 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
852 return;
853 }
854
Adam Lesinski6b879f02010-11-04 16:15:23 -0700855 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
856 * of the All apps view on XLarge displays to not take up more space then it needs. Width
857 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
858 * each page to have the same width.
859 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700860 final int verticalPadding = getPaddingTop() + getPaddingBottom();
861 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700862
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700863 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700865 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700866 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700867 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
868 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
869 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
870 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 final int childCount = getChildCount();
872 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700873 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700874 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100875 if (child.getVisibility() != GONE) {
876 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700877
Vladimir Marko2824b072013-10-04 16:42:17 +0100878 int childWidthMode;
879 int childHeightMode;
880 int childWidth;
881 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700882
Vladimir Marko2824b072013-10-04 16:42:17 +0100883 if (!lp.isFullScreenPage) {
884 if (lp.width == LayoutParams.WRAP_CONTENT) {
885 childWidthMode = MeasureSpec.AT_MOST;
886 } else {
887 childWidthMode = MeasureSpec.EXACTLY;
888 }
889
890 if (lp.height == LayoutParams.WRAP_CONTENT) {
891 childHeightMode = MeasureSpec.AT_MOST;
892 } else {
893 childHeightMode = MeasureSpec.EXACTLY;
894 }
895
Adam Cohen3b185e22013-10-29 14:45:58 -0700896 childWidth = getViewportWidth() - horizontalPadding
897 - mInsets.left - mInsets.right;
898 childHeight = getViewportHeight() - verticalPadding
899 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100900 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700901 } else {
902 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700903 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100904
Adam Cohen3b185e22013-10-29 14:45:58 -0700905 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
906 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700907 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700908 if (referenceChildWidth == 0) {
909 referenceChildWidth = childWidth;
910 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700911
Vladimir Marko2824b072013-10-04 16:42:17 +0100912 final int childWidthMeasureSpec =
913 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
914 final int childHeightMeasureSpec =
915 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
916 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700917 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700918 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700919 if (mSpacePagesAutomatically) {
920 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
921 - referenceChildWidth) / 2;
922 if (spacing >= 0) {
923 setPageSpacing(spacing);
924 }
925 mSpacePagesAutomatically = false;
926 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700927 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800928 }
929
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700930 /**
931 * This method should be called once before first layout / measure pass.
932 */
933 protected void setSinglePageInViewport() {
934 mSpacePagesAutomatically = true;
935 }
936
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700938 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700940 return;
941 }
942
Winson Chung785d2eb2011-04-14 16:08:02 -0700943 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700945
Adam Cohen7d30a372013-07-01 17:03:59 -0700946 int offsetX = getViewportOffsetX();
947 int offsetY = getViewportOffsetY();
948
949 // Update the viewport offsets
950 mViewport.offset(offsetX, offsetY);
951
Adam Cohen0ffac432013-07-10 11:19:26 -0700952 final boolean isRtl = isLayoutRtl();
953
954 final int startIndex = isRtl ? childCount - 1 : 0;
955 final int endIndex = isRtl ? -1 : childCount;
956 final int delta = isRtl ? -1 : 1;
957
Adam Cohen7d30a372013-07-01 17:03:59 -0700958 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700959
Adam Cohen3b185e22013-10-29 14:45:58 -0700960 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000961 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700962
963 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700964 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
965 mPageScrolls = new int[getChildCount()];
966 }
967
Adam Cohen0ffac432013-07-10 11:19:26 -0700968 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700969 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700971 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100972 int childTop;
973 if (lp.isFullScreenPage) {
974 childTop = offsetY;
975 } else {
976 childTop = offsetY + getPaddingTop() + mInsets.top;
977 if (mCenterPagesVertically) {
978 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
979 }
980 }
981
Adam Cohen96d30a12013-07-16 18:13:21 -0700982 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700983 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800984
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700986 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800987 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700988
Adam Cohen3b185e22013-10-29 14:45:58 -0700989 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
990 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000991
992 int pageGap = mPageSpacing;
993 int next = i + delta;
994 if (next != endIndex) {
995 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
996 } else {
997 nextLp = null;
998 }
999
1000 // Prevent full screen pages from showing in the viewport
1001 // when they are not the current page.
1002 if (lp.isFullScreenPage) {
1003 pageGap = getPaddingLeft();
1004 } else if (nextLp != null && nextLp.isFullScreenPage) {
1005 pageGap = getPaddingRight();
1006 }
1007
1008 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
1010 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001011
1012 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001013 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001014 mFirstLayout = false;
1015 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001016
1017 if (childCount > 0) {
1018 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -07001019 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -07001020 } else {
1021 mMaxScrollX = 0;
1022 }
1023
1024 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1025 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001026 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001027 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001028 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001029 } else {
1030 setCurrentPage(getNextPage());
1031 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001032 }
1033 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001034
1035 if (isReordering(true)) {
1036 updateDragViewTranslationDuringDrag();
1037 }
Winson Chunge3193b92010-09-10 11:44:42 -07001038 }
1039
Adam Cohen3b185e22013-10-29 14:45:58 -07001040 public void setPageSpacing(int pageSpacing) {
1041 mPageSpacing = pageSpacing;
1042 requestLayout();
1043 }
1044
Adam Cohenf34bab52010-09-30 14:11:56 -07001045 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001046 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1047
1048 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001049 for (int i = 0; i < getChildCount(); i++) {
1050 View child = getChildAt(i);
1051 if (child != null) {
1052 float scrollProgress = getScrollProgress(screenCenter, child, i);
1053 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001054 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001055 }
1056 }
1057 invalidate();
1058 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001059 }
1060
Winson Chungc58497e2013-09-03 17:48:37 -07001061 protected void enablePagedViewAnimations() {
1062 mAllowPagedViewAnimations = true;
1063
1064 }
1065 protected void disablePagedViewAnimations() {
1066 mAllowPagedViewAnimations = false;
1067 }
1068
Winson Chunge3193b92010-09-10 11:44:42 -07001069 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001070 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001071 // Update the page indicator, we don't update the page indicator as we
1072 // add/remove pages
1073 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001074 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001075 mPageIndicator.addMarker(pageIndex,
1076 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001077 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001078 }
1079
Adam Cohen2591f6a2011-10-25 14:36:40 -07001080 // This ensures that when children are added, they get the correct transforms / alphas
1081 // in accordance with any scroll effects.
1082 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001083 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001084 invalidate();
1085 }
1086
Michael Jurka8b805b12012-04-18 14:23:14 -07001087 @Override
1088 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001089 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001090 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001091 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001092 }
1093
Winson Chungd2be3812013-07-16 11:11:32 -07001094 private void removeMarkerForView(int index) {
1095 // Update the page indicator, we don't update the page indicator as we
1096 // add/remove pages
1097 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001098 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001099 }
1100 }
1101
1102 @Override
1103 public void removeView(View v) {
1104 // XXX: We should find a better way to hook into this before the view
1105 // gets removed form its parent...
1106 removeMarkerForView(indexOfChild(v));
1107 super.removeView(v);
1108 }
1109 @Override
1110 public void removeViewInLayout(View v) {
1111 // XXX: We should find a better way to hook into this before the view
1112 // gets removed form its parent...
1113 removeMarkerForView(indexOfChild(v));
1114 super.removeViewInLayout(v);
1115 }
1116 @Override
1117 public void removeViewAt(int index) {
1118 // XXX: We should find a better way to hook into this before the view
1119 // gets removed form its parent...
1120 removeViewAt(index);
1121 super.removeViewAt(index);
1122 }
1123 @Override
1124 public void removeAllViewsInLayout() {
1125 // Update the page indicator, we don't update the page indicator as we
1126 // add/remove pages
1127 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001128 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001129 }
1130
1131 super.removeAllViewsInLayout();
1132 }
1133
Adam Cohen73894962011-10-31 13:17:17 -07001134 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001135 if (index < 0 || index > getChildCount() - 1) return 0;
1136
Adam Cohenf698c6e2013-07-17 18:44:25 -07001137 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001138
Adam Cohenf698c6e2013-07-17 18:44:25 -07001139 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001140 }
1141
Adam Cohen6f127a62014-06-12 14:54:41 -07001142 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001143 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001144 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001145 }
1146
Michael Jurkadde558b2011-11-09 22:09:06 -08001147 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001148 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001149 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001150
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001151 range[0] = -1;
1152 range[1] = -1;
1153
Michael Jurkac4fb9172010-09-02 17:19:20 -07001154 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001155 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001156 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001157
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001158 int count = getChildCount();
1159 for (int i = 0; i < count; i++) {
1160 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001161
Winson Chungc9ca2982013-07-19 12:07:38 -07001162 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001163 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001164 if (mTmpIntPoint[0] > viewportWidth) {
1165 if (range[0] == -1) {
1166 continue;
1167 } else {
1168 break;
1169 }
1170 }
1171
Adam Cohen6a678da2013-09-24 10:58:01 -07001172 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001173 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1174 if (mTmpIntPoint[0] < 0) {
1175 if (range[0] == -1) {
1176 continue;
1177 } else {
1178 break;
1179 }
1180 }
1181 curScreen = i;
1182 if (range[0] < 0) {
1183 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001184 }
1185 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001186
1187 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001188 } else {
1189 range[0] = -1;
1190 range[1] = -1;
1191 }
1192 }
1193
Michael Jurka920d7f42012-05-14 16:29:55 -07001194 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001195 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001196 }
1197
Michael Jurkadde558b2011-11-09 22:09:06 -08001198 @Override
1199 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001200 // Find out which screens are visible; as an optimization we only call draw on them
1201 final int pageCount = getChildCount();
1202 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001203 int halfScreenSize = getViewportWidth() / 2;
1204 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1205 // Otherwise it is equal to the scaled overscroll position.
1206 int screenCenter = mOverScrollX + halfScreenSize;
1207
1208 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1209 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1210 // set it for the next frame
1211 mForceScreenScrolled = false;
1212 screenScrolled(screenCenter);
1213 mLastScreenCenter = screenCenter;
1214 }
1215
Michael Jurkadde558b2011-11-09 22:09:06 -08001216 getVisiblePages(mTempVisiblePagesRange);
1217 final int leftScreen = mTempVisiblePagesRange[0];
1218 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001219 if (leftScreen != -1 && rightScreen != -1) {
1220 final long drawingTime = getDrawingTime();
1221 // Clip to the bounds
1222 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001223 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1224 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001225
Adam Cohen7d30a372013-07-01 17:03:59 -07001226 // Draw all the children, leaving the drag view for last
1227 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001228 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001229 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001230 if (mForceDrawAllChildrenNextFrame ||
1231 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001232 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001233 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001234 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001235 // Draw the drag view on top (if there is one)
1236 if (mDragView != null) {
1237 drawChild(canvas, mDragView, drawingTime);
1238 }
1239
Michael Jurka5e368ff2012-05-14 23:13:15 -07001240 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001241 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001242 }
Michael Jurka0142d492010-08-25 17:46:15 -07001243 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 }
1245
1246 @Override
1247 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001248 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001249 if (page != mCurrentPage || !mScroller.isFinished()) {
1250 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 return true;
1252 }
1253 return false;
1254 }
1255
1256 @Override
1257 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001258 int focusablePage;
1259 if (mNextPage != INVALID_PAGE) {
1260 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001262 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 }
Winson Chung86f77532010-08-24 11:08:22 -07001264 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001266 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 }
1268 return false;
1269 }
1270
1271 @Override
1272 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001273 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001275 if (getCurrentPage() > 0) {
1276 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 return true;
1278 }
1279 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001280 if (getCurrentPage() < getPageCount() - 1) {
1281 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 return true;
1283 }
1284 }
1285 return super.dispatchUnhandledMove(focused, direction);
1286 }
1287
1288 @Override
1289 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001290 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001291 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001292 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
1294 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001295 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001296 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 }
1298 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001299 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001300 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 }
1302 }
1303 }
1304
1305 /**
1306 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001307 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 *
Winson Chung86f77532010-08-24 11:08:22 -07001309 * This happens when live folders requery, and if they're off page, they
1310 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 */
1312 @Override
1313 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001314 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 View v = focused;
1316 while (true) {
1317 if (v == current) {
1318 super.focusableViewAvailable(focused);
1319 return;
1320 }
1321 if (v == this) {
1322 return;
1323 }
1324 ViewParent parent = v.getParent();
1325 if (parent instanceof View) {
1326 v = (View)v.getParent();
1327 } else {
1328 return;
1329 }
1330 }
1331 }
1332
1333 /**
1334 * {@inheritDoc}
1335 */
1336 @Override
1337 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1338 if (disallowIntercept) {
1339 // We need to make sure to cancel our long press if
1340 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001341 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001342 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 }
1344 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1345 }
1346
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001347 /**
1348 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1349 */
1350 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001351 if (isLayoutRtl()) {
1352 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001353 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001354 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001355 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001356 }
1357
1358 /**
1359 * Return true if a tap at (x, y) should trigger a flip to the next page.
1360 */
1361 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001362 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001363 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001364 }
1365 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001366 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001367 }
Winson Chung52aee602013-01-30 12:01:02 -08001368
Adam Cohen7d30a372013-07-01 17:03:59 -07001369 /** Returns whether x and y originated within the buffered viewport */
1370 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1371 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1372 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1373 return mTmpRect.contains(x, y);
1374 }
1375
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 @Override
1377 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001378 if (DISABLE_TOUCH_INTERACTION) {
1379 return false;
1380 }
1381
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 /*
1383 * This method JUST determines whether we want to intercept the motion.
1384 * If we return true, onTouchEvent will be called and we do the actual
1385 * scrolling there.
1386 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001387 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001388
Winson Chung45e1d6e2010-11-09 17:19:49 -08001389 // Skip touch handling if there are no pages to swipe
1390 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1391
Winson Chung321e9ee2010-08-09 13:37:56 -07001392 /*
1393 * Shortcut the most recurring case: the user is in the dragging
1394 * state and he is moving his finger. We want to intercept this
1395 * motion.
1396 */
1397 final int action = ev.getAction();
1398 if ((action == MotionEvent.ACTION_MOVE) &&
1399 (mTouchState == TOUCH_STATE_SCROLLING)) {
1400 return true;
1401 }
1402
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 switch (action & MotionEvent.ACTION_MASK) {
1404 case MotionEvent.ACTION_MOVE: {
1405 /*
1406 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1407 * whether the user has moved far enough from his original down touch.
1408 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001409 if (mActivePointerId != INVALID_POINTER) {
1410 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001411 }
1412 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1413 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1414 // i.e. fall through to the next case (don't break)
1415 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1416 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001417 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 }
1419
1420 case MotionEvent.ACTION_DOWN: {
1421 final float x = ev.getX();
1422 final float y = ev.getY();
1423 // Remember location of down touch
1424 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001425 mDownMotionY = y;
1426 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 mLastMotionX = x;
1428 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001429 float[] p = mapPointFromViewToParent(this, x, y);
1430 mParentDownMotionX = p[0];
1431 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001432 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001433 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001435
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 /*
1437 * If being flinged and user touches the screen, initiate drag;
1438 * otherwise don't. mScroller.isFinished should be false when
1439 * being flinged.
1440 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001441 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001442 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001443
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001444 if (finishedScrolling) {
1445 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001446 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001447 setCurrentPage(getNextPage());
1448 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001449 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001450 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001451 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1452 mTouchState = TOUCH_STATE_SCROLLING;
1453 } else {
1454 mTouchState = TOUCH_STATE_REST;
1455 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001456 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001457
Winson Chung86f77532010-08-24 11:08:22 -07001458 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001460 if (!DISABLE_TOUCH_SIDE_PAGES) {
1461 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1462 if (getChildCount() > 0) {
1463 if (hitsPreviousPage(x, y)) {
1464 mTouchState = TOUCH_STATE_PREV_PAGE;
1465 } else if (hitsNextPage(x, y)) {
1466 mTouchState = TOUCH_STATE_NEXT_PAGE;
1467 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 }
1469 }
1470 }
1471 break;
1472 }
1473
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001475 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001476 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001477 break;
1478
1479 case MotionEvent.ACTION_POINTER_UP:
1480 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001481 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 break;
1483 }
1484
1485 /*
1486 * The only time we want to intercept motion events is if we are in the
1487 * drag mode.
1488 */
1489 return mTouchState != TOUCH_STATE_REST;
1490 }
1491
Adam Cohenf8d28232011-02-01 21:47:00 -08001492 protected void determineScrollingStart(MotionEvent ev) {
1493 determineScrollingStart(ev, 1.0f);
1494 }
1495
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 /*
1497 * Determines if we should change the touch state to start scrolling after the
1498 * user moves their touch point too far.
1499 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001500 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001501 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001503 if (pointerIndex == -1) return;
1504
1505 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 final float x = ev.getX(pointerIndex);
1507 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001508 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1509
Winson Chung321e9ee2010-08-09 13:37:56 -07001510 final int xDiff = (int) Math.abs(x - mLastMotionX);
1511 final int yDiff = (int) Math.abs(y - mLastMotionY);
1512
Adam Cohenf8d28232011-02-01 21:47:00 -08001513 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001514 boolean xPaged = xDiff > mPagingTouchSlop;
1515 boolean xMoved = xDiff > touchSlop;
1516 boolean yMoved = yDiff > touchSlop;
1517
Adam Cohenf8d28232011-02-01 21:47:00 -08001518 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001519 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001520 // Scroll if the user moved far enough along the X axis
1521 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001522 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001523 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001524 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001525 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001526 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001527 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001528 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001530 }
1531 }
1532
Adam Cohen7d30a372013-07-01 17:03:59 -07001533 protected float getMaxScrollProgress() {
1534 return 1.0f;
1535 }
1536
Adam Cohenf8d28232011-02-01 21:47:00 -08001537 protected void cancelCurrentPageLongPress() {
1538 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001539 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001540 // Try canceling the long press. It could also have been scheduled
1541 // by a distant descendant, so use the mAllowLongPress flag to block
1542 // everything
1543 final View currentPage = getPageAt(mCurrentPage);
1544 if (currentPage != null) {
1545 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 }
1547 }
1548 }
1549
Adam Cohen7d30a372013-07-01 17:03:59 -07001550 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1551 final int halfScreenSize = getViewportWidth() / 2;
1552
1553 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1554 screenCenter = Math.max(halfScreenSize, screenCenter);
1555
1556 return getScrollProgress(screenCenter, v, page);
1557 }
1558
Adam Cohenb5ba0972011-09-07 18:02:31 -07001559 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001560 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001561
Adam Cohenedb40762013-07-18 16:45:45 -07001562 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001563 int count = getChildCount();
1564
1565 final int totalDistance;
1566
1567 int adjacentPage = page + 1;
1568 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1569 adjacentPage = page - 1;
1570 }
1571
1572 if (adjacentPage < 0 || adjacentPage > count - 1) {
1573 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1574 } else {
1575 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1576 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001577
1578 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001579 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1580 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001581 return scrollProgress;
1582 }
1583
Adam Cohenedb40762013-07-18 16:45:45 -07001584 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001585 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001586 return 0;
1587 } else {
1588 return mPageScrolls[index];
1589 }
1590 }
1591
Adam Cohen564a2e72013-10-09 14:47:32 -07001592 // While layout transitions are occurring, a child's position may stray from its baseline
1593 // position. This method returns the magnitude of this stray at any given time.
1594 public int getLayoutTransitionOffsetForPage(int index) {
1595 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1596 return 0;
1597 } else {
1598 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001599
1600 int scrollOffset = 0;
1601 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1602 if (!lp.isFullScreenPage) {
1603 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1604 }
1605
Adam Cohen564a2e72013-10-09 14:47:32 -07001606 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1607 return (int) (child.getX() - baselineX);
1608 }
1609 }
1610
Adam Cohene0f66b52010-11-23 15:06:07 -08001611 // This curve determines how the effect of scrolling over the limits of the page dimishes
1612 // as the user pulls further and further from the bounds
1613 private float overScrollInfluenceCurve(float f) {
1614 f -= 1.0f;
1615 return f * f * f + 1.0f;
1616 }
1617
Adam Cohen1e4359c2014-08-18 13:12:16 -07001618 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001619 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001620
1621 // We want to reach the max over scroll effect when the user has
1622 // over scrolled half the size of the screen
1623 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1624
Adam Cohen1e4359c2014-08-18 13:12:16 -07001625 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001626
1627 // Clamp this factor, f, to -1 < f < 1
1628 if (Math.abs(f) >= 1) {
1629 f /= Math.abs(f);
1630 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001631 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001632 }
1633
1634 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001635 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001636
1637 float f = (amount / screenSize);
1638
1639 if (f == 0) return;
1640 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1641
Adam Cohen7bfc9792011-01-28 13:52:37 -08001642 // Clamp this factor, f, to -1 < f < 1
1643 if (Math.abs(f) >= 1) {
1644 f /= Math.abs(f);
1645 }
1646
Adam Cohene0f66b52010-11-23 15:06:07 -08001647 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001648 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001649 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001650 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001651 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001652 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001653 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001654 }
1655 invalidate();
1656 }
1657
Adam Cohenb5ba0972011-09-07 18:02:31 -07001658 protected void overScroll(float amount) {
1659 dampedOverScroll(amount);
1660 }
1661
Michael Jurkac5b262c2011-01-12 20:24:50 -08001662 protected float maxOverScroll() {
1663 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001664 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001665 float f = 1.0f;
1666 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1667 return OVERSCROLL_DAMP_FACTOR * f;
1668 }
1669
Adam Cohenf358a4b2013-07-23 16:47:31 -07001670 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001671 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001672 }
1673
Adam Cohenf9618852013-11-08 06:45:03 -08001674 protected void disableFreeScroll() {
1675 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001676 }
1677
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001678 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001679 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001680 if (isLayoutRtl()) {
1681 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1682 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1683 } else {
1684 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1685 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1686 }
1687 }
1688
Adam Cohenf9618852013-11-08 06:45:03 -08001689 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001690 mFreeScroll = freeScroll;
1691
Adam Cohenf9618852013-11-08 06:45:03 -08001692 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001693 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001694 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001695 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1696 setCurrentPage(mTempVisiblePagesRange[0]);
1697 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1698 setCurrentPage(mTempVisiblePagesRange[1]);
1699 }
1700 }
1701
1702 setEnableOverscroll(!freeScroll);
1703 }
1704
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001705 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001706 mAllowOverScroll = enable;
1707 }
1708
1709 int getNearestHoverOverPageIndex() {
1710 if (mDragView != null) {
1711 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1712 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001713 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001714 int minDistance = Integer.MAX_VALUE;
1715 int minIndex = indexOfChild(mDragView);
1716 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1717 View page = getPageAt(i);
1718 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1719 int d = Math.abs(dragX - pageX);
1720 if (d < minDistance) {
1721 minIndex = i;
1722 minDistance = d;
1723 }
1724 }
1725 return minIndex;
1726 }
1727 return -1;
1728 }
1729
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 @Override
1731 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001732 if (DISABLE_TOUCH_INTERACTION) {
1733 return false;
1734 }
1735
Adam Cohen1697b792013-09-17 19:08:21 -07001736 super.onTouchEvent(ev);
1737
Winson Chung45e1d6e2010-11-09 17:19:49 -08001738 // Skip touch handling if there are no pages to swipe
1739 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1740
Michael Jurkab8f06722010-10-10 15:58:46 -07001741 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001742
1743 final int action = ev.getAction();
1744
1745 switch (action & MotionEvent.ACTION_MASK) {
1746 case MotionEvent.ACTION_DOWN:
1747 /*
1748 * If being flinged and user touches, stop the fling. isFinished
1749 * will be false if being flinged.
1750 */
1751 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001752 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 }
1754
1755 // Remember where the motion event started
1756 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001757 mDownMotionY = mLastMotionY = ev.getY();
1758 mDownScrollX = getScrollX();
1759 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1760 mParentDownMotionX = p[0];
1761 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001762 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001763 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001764 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001765
Michael Jurka0142d492010-08-25 17:46:15 -07001766 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001767 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001768 pageBeginMoving();
1769 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001770 break;
1771
1772 case MotionEvent.ACTION_MOVE:
1773 if (mTouchState == TOUCH_STATE_SCROLLING) {
1774 // Scroll to follow the motion event
1775 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001776
1777 if (pointerIndex == -1) return true;
1778
Winson Chung321e9ee2010-08-09 13:37:56 -07001779 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001780 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001781
Adam Cohenaefd4e12011-02-14 16:39:38 -08001782 mTotalMotionX += Math.abs(deltaX);
1783
Winson Chungc0844aa2011-02-02 15:25:58 -08001784 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1785 // keep the remainder because we are actually testing if we've moved from the last
1786 // scrolled position (which is discrete).
1787 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001788 mTouchX += deltaX;
1789 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1790 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001791 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001792 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001793 } else {
1794 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001795 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001796 mLastMotionX = x;
1797 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001798 } else {
1799 awakenScrollBars();
1800 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001801 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1802 // Update the last motion position
1803 mLastMotionX = ev.getX();
1804 mLastMotionY = ev.getY();
1805
1806 // Update the parent down so that our zoom animations take this new movement into
1807 // account
1808 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1809 mParentDownMotionX = pt[0];
1810 mParentDownMotionY = pt[1];
1811 updateDragViewTranslationDuringDrag();
1812
1813 // Find the closest page to the touch point
1814 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001815
1816 // Change the drag view if we are hovering over the drop target
1817 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1818 (int) mParentDownMotionX, (int) mParentDownMotionY);
1819 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1820
Adam Cohen7d30a372013-07-01 17:03:59 -07001821 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1822 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1823 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1824 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1825
Adam Cohenf358a4b2013-07-23 16:47:31 -07001826 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1827 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1828 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 mTempVisiblePagesRange[0] = 0;
1830 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001831 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001832 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1833 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1834 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1835 mSidePageHoverIndex = pageUnderPointIndex;
1836 mSidePageHoverRunnable = new Runnable() {
1837 @Override
1838 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001839 // Setup the scroll to the correct page before we swap the views
1840 snapToPage(pageUnderPointIndex);
1841
1842 // For each of the pages between the paged view and the drag view,
1843 // animate them from the previous position to the new position in
1844 // the layout (as a result of the drag view moving in the layout)
1845 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1846 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1847 dragViewIndex + 1 : pageUnderPointIndex;
1848 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1849 dragViewIndex - 1 : pageUnderPointIndex;
1850 for (int i = lowerIndex; i <= upperIndex; ++i) {
1851 View v = getChildAt(i);
1852 // dragViewIndex < pageUnderPointIndex, so after we remove the
1853 // drag view all subsequent views to pageUnderPointIndex will
1854 // shift down.
1855 int oldX = getViewportOffsetX() + getChildOffset(i);
1856 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1857
1858 // Animate the view translation from its old position to its new
1859 // position
1860 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1861 if (anim != null) {
1862 anim.cancel();
1863 }
1864
1865 v.setTranslationX(oldX - newX);
1866 anim = new AnimatorSet();
1867 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1868 anim.playTogether(
1869 ObjectAnimator.ofFloat(v, "translationX", 0f));
1870 anim.start();
1871 v.setTag(anim);
1872 }
1873
1874 removeView(mDragView);
1875 onRemoveView(mDragView, false);
1876 addView(mDragView, pageUnderPointIndex);
1877 onAddView(mDragView, pageUnderPointIndex);
1878 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001879 if (mPageIndicator != null) {
1880 mPageIndicator.setActiveMarker(getNextPage());
1881 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001882 }
1883 };
1884 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1885 }
1886 } else {
1887 removeCallbacks(mSidePageHoverRunnable);
1888 mSidePageHoverIndex = -1;
1889 }
Adam Cohen564976a2010-10-13 18:52:07 -07001890 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001891 determineScrollingStart(ev);
1892 }
1893 break;
1894
1895 case MotionEvent.ACTION_UP:
1896 if (mTouchState == TOUCH_STATE_SCROLLING) {
1897 final int activePointerId = mActivePointerId;
1898 final int pointerIndex = ev.findPointerIndex(activePointerId);
1899 final float x = ev.getX(pointerIndex);
1900 final VelocityTracker velocityTracker = mVelocityTracker;
1901 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1902 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001903 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001904 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001905 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1906 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001907
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001908 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1909
Adam Cohen00481b32011-11-18 12:03:48 -08001910 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001911 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001912
Adam Cohenf358a4b2013-07-23 16:47:31 -07001913 if (!mFreeScroll) {
1914 // In the case that the page is moved far to one direction and then is flung
1915 // in the opposite direction, we use a threshold to determine whether we should
1916 // just return to the starting page, or if we should skip one further.
1917 boolean returnToOriginalPage = false;
1918 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1919 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1920 returnToOriginalPage = true;
1921 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001922
Adam Cohenf358a4b2013-07-23 16:47:31 -07001923 int finalPage;
1924 // We give flings precedence over large moves, which is why we short-circuit our
1925 // test for a large move if a fling has been registered. That is, a large
1926 // move to the left and fling to the right will register as a fling to the right.
1927 final boolean isRtl = isLayoutRtl();
1928 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1929 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1930 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1931 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1932 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1933 snapToPageWithVelocity(finalPage, velocityX);
1934 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1935 (isFling && isVelocityXLeft)) &&
1936 mCurrentPage < getChildCount() - 1) {
1937 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1938 snapToPageWithVelocity(finalPage, velocityX);
1939 } else {
1940 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001941 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001943 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001944 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001945 }
1946
1947 float scaleX = getScaleX();
1948 int vX = (int) (-velocityX * scaleX);
1949 int initialScrollX = (int) (getScrollX() * scaleX);
1950
Adam Cohenf9618852013-11-08 06:45:03 -08001951 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001952 mScroller.fling(initialScrollX,
1953 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1954 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001955 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001956 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001957 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1958 // at this point we have not moved beyond the touch slop
1959 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1960 // we can just page
1961 int nextPage = Math.max(0, mCurrentPage - 1);
1962 if (nextPage != mCurrentPage) {
1963 snapToPage(nextPage);
1964 } else {
1965 snapToDestination();
1966 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001967 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001968 // at this point we have not moved beyond the touch slop
1969 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1970 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001971 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1972 if (nextPage != mCurrentPage) {
1973 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001974 } else {
1975 snapToDestination();
1976 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001977 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1978 // Update the last motion position
1979 mLastMotionX = ev.getX();
1980 mLastMotionY = ev.getY();
1981
1982 // Update the parent down so that our zoom animations take this new movement into
1983 // account
1984 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1985 mParentDownMotionX = pt[0];
1986 mParentDownMotionY = pt[1];
1987 updateDragViewTranslationDuringDrag();
1988 boolean handledFling = false;
1989 if (!DISABLE_FLING_TO_DELETE) {
1990 // Check the velocity and see if we are flinging-to-delete
1991 PointF flingToDeleteVector = isFlingingToDelete();
1992 if (flingToDeleteVector != null) {
1993 onFlingToDelete(flingToDeleteVector);
1994 handledFling = true;
1995 }
1996 }
1997 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1998 (int) mParentDownMotionY)) {
1999 onDropToDelete();
2000 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002001 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002002 if (!mCancelTap) {
2003 onUnhandledTap(ev);
2004 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002005 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002006
2007 // Remove the callback to wait for the side page hover timeout
2008 removeCallbacks(mSidePageHoverRunnable);
2009 // End any intermediate reordering states
2010 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002011 break;
2012
2013 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002014 if (mTouchState == TOUCH_STATE_SCROLLING) {
2015 snapToDestination();
2016 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002017 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002018 break;
2019
2020 case MotionEvent.ACTION_POINTER_UP:
2021 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002022 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002023 break;
2024 }
2025
2026 return true;
2027 }
2028
Adam Cohen7d30a372013-07-01 17:03:59 -07002029 public void onFlingToDelete(View v) {}
2030 public void onRemoveView(View v, boolean deletePermanently) {}
2031 public void onRemoveViewAnimationCompleted() {}
2032 public void onAddView(View v, int index) {}
2033
2034 private void resetTouchState() {
2035 releaseVelocityTracker();
2036 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002037 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 mTouchState = TOUCH_STATE_REST;
2039 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002040 }
2041
Adam Cohenc2d6e892014-10-16 09:49:24 -07002042 /**
2043 * Triggered by scrolling via touch
2044 */
2045 protected void onScrollInteractionBegin() {
2046 }
2047
2048 protected void onScrollInteractionEnd() {
2049 }
2050
Adam Cohen1697b792013-09-17 19:08:21 -07002051 protected void onUnhandledTap(MotionEvent ev) {
2052 ((Launcher) getContext()).onClick(this);
2053 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002054
Winson Chung185d7162011-02-28 13:47:29 -08002055 @Override
2056 public boolean onGenericMotionEvent(MotionEvent event) {
2057 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2058 switch (event.getAction()) {
2059 case MotionEvent.ACTION_SCROLL: {
2060 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2061 final float vscroll;
2062 final float hscroll;
2063 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2064 vscroll = 0;
2065 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2066 } else {
2067 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2068 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2069 }
2070 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002071 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2072 : (hscroll > 0 || vscroll > 0);
2073 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002074 scrollRight();
2075 } else {
2076 scrollLeft();
2077 }
2078 return true;
2079 }
2080 }
2081 }
2082 }
2083 return super.onGenericMotionEvent(event);
2084 }
2085
Michael Jurkab8f06722010-10-10 15:58:46 -07002086 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2087 if (mVelocityTracker == null) {
2088 mVelocityTracker = VelocityTracker.obtain();
2089 }
2090 mVelocityTracker.addMovement(ev);
2091 }
2092
2093 private void releaseVelocityTracker() {
2094 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002095 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002096 mVelocityTracker.recycle();
2097 mVelocityTracker = null;
2098 }
2099 }
2100
Winson Chung321e9ee2010-08-09 13:37:56 -07002101 private void onSecondaryPointerUp(MotionEvent ev) {
2102 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2103 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2104 final int pointerId = ev.getPointerId(pointerIndex);
2105 if (pointerId == mActivePointerId) {
2106 // This was our active pointer going up. Choose a new
2107 // active pointer and adjust accordingly.
2108 // TODO: Make this decision more intelligent.
2109 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2110 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2111 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002112 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002113 mActivePointerId = ev.getPointerId(newPointerIndex);
2114 if (mVelocityTracker != null) {
2115 mVelocityTracker.clear();
2116 }
2117 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002118 }
2119
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 @Override
2121 public void requestChildFocus(View child, View focused) {
2122 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002123 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002124 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002125 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002126 }
2127 }
2128
Winson Chung1908d072011-02-24 18:09:44 -08002129 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002130 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002131 }
2132
Adam Cohen7d30a372013-07-01 17:03:59 -07002133 int getPageNearestToPoint(float x) {
2134 int index = 0;
2135 for (int i = 0; i < getChildCount(); ++i) {
2136 if (x < getChildAt(i).getRight() - getScrollX()) {
2137 return index;
2138 } else {
2139 index++;
2140 }
2141 }
2142 return Math.min(index, getChildCount() - 1);
2143 }
2144
Adam Cohend19d3ca2010-09-15 14:43:42 -07002145 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002146 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002147 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002148 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002149 final int childCount = getChildCount();
2150 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002151 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002152 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002153 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002154 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002155 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2156 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2157 minDistanceFromScreenCenter = distanceFromScreenCenter;
2158 minDistanceFromScreenCenterIndex = i;
2159 }
2160 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002161 return minDistanceFromScreenCenterIndex;
2162 }
2163
Adam Cohen1e4359c2014-08-18 13:12:16 -07002164 protected boolean isInOverScroll() {
2165 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2166 }
2167
2168 protected int getPageSnapDuration() {
2169 if (isInOverScroll()) {
2170 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2171 }
2172 return PAGE_SNAP_ANIMATION_DURATION;
2173
2174 }
2175
Adam Cohend19d3ca2010-09-15 14:43:42 -07002176 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002177 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002178 }
2179
Adam Cohene0f66b52010-11-23 15:06:07 -08002180 private static class ScrollInterpolator implements Interpolator {
2181 public ScrollInterpolator() {
2182 }
2183
2184 public float getInterpolation(float t) {
2185 t -= 1.0f;
2186 return t*t*t*t*t + 1;
2187 }
2188 }
2189
2190 // We want the duration of the page snap animation to be influenced by the distance that
2191 // the screen has to travel, however, we don't want this duration to be effected in a
2192 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2193 // of travel has on the overall snap duration.
2194 float distanceInfluenceForSnapDuration(float f) {
2195 f -= 0.5f; // center the values about 0.
2196 f *= 0.3f * Math.PI / 2.0f;
2197 return (float) Math.sin(f);
2198 }
2199
Michael Jurka0142d492010-08-25 17:46:15 -07002200 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002201 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002203
Adam Cohenedb40762013-07-18 16:45:45 -07002204 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002205 int delta = newX - mUnboundedScrollX;
2206 int duration = 0;
2207
Adam Cohen1e4359c2014-08-18 13:12:16 -07002208 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002209 // If the velocity is low enough, then treat this more as an automatic page advance
2210 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002211 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002212 return;
2213 }
2214
2215 // Here we compute a "distance" that will be used in the computation of the overall
2216 // snap duration. This is a function of the actual distance that needs to be traveled;
2217 // we keep this value close to half screen size in order to reduce the variance in snap
2218 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002219 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002220 float distance = halfScreenSize + halfScreenSize *
2221 distanceInfluenceForSnapDuration(distanceRatio);
2222
2223 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002224 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002225
2226 // we want the page's snap velocity to approximately match the velocity at which the
2227 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002228 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2229 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002230
2231 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002232 }
2233
2234 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002235 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002236 }
2237
Adam Cohen7d30a372013-07-01 17:03:59 -07002238 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002239 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002240 }
2241
Michael Jurka0142d492010-08-25 17:46:15 -07002242 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002243 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002244 }
2245
Adam Cohenf9618852013-11-08 06:45:03 -08002246 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2247 snapToPage(whichPage, duration, false, interpolator);
2248 }
2249
2250 protected void snapToPage(int whichPage, int duration, boolean immediate,
2251 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002252 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002253
Adam Cohenedb40762013-07-18 16:45:45 -07002254 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002255 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002256 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002257 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002258 }
2259
2260 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002261 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002262 }
Michael Jurka0142d492010-08-25 17:46:15 -07002263
Adam Cohenf9618852013-11-08 06:45:03 -08002264 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2265 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002266 whichPage = validateNewPage(whichPage);
2267
Adam Cohen7d30a372013-07-01 17:03:59 -07002268 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002269 View focusedChild = getFocusedChild();
2270 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002271 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002272 focusedChild.clearFocus();
2273 }
2274
Adam Cohen53805212013-10-01 10:39:23 -07002275 sendScrollAccessibilityEvent();
2276
Michael Jurka0142d492010-08-25 17:46:15 -07002277 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002278 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002279 if (immediate) {
2280 duration = 0;
2281 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002282 duration = Math.abs(delta);
2283 }
2284
Adam Cohenf358a4b2013-07-23 16:47:31 -07002285 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002286 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002287 }
Adam Cohenf9618852013-11-08 06:45:03 -08002288
2289 if (interpolator != null) {
2290 mScroller.setInterpolator(interpolator);
2291 } else {
2292 mScroller.setInterpolator(mDefaultInterpolator);
2293 }
2294
Adam Cohen68d73932010-11-15 10:50:58 -08002295 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002296
Adam Cohen674531f2013-12-13 15:07:14 -08002297 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002298
2299 // Trigger a compute() to finish switching pages if necessary
2300 if (immediate) {
2301 computeScroll();
2302 }
2303
Winson Chung9c0565f2013-07-19 13:49:06 -07002304 // Defer loading associated pages until the scroll settles
2305 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2306
Adam Cohen7d30a372013-07-01 17:03:59 -07002307 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002308 invalidate();
2309 }
2310
Winson Chung321e9ee2010-08-09 13:37:56 -07002311 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002312 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002313 }
2314
2315 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002316 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002317 }
2318
Winson Chung86f77532010-08-24 11:08:22 -07002319 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002320 int result = -1;
2321 if (v != null) {
2322 ViewParent vp = v.getParent();
2323 int count = getChildCount();
2324 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002325 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002326 return i;
2327 }
2328 }
2329 }
2330 return result;
2331 }
2332
2333 /**
2334 * @return True is long presses are still allowed for the current touch
2335 */
2336 public boolean allowLongPress() {
2337 return mAllowLongPress;
2338 }
2339
Adam Cohendbdff6b2013-09-18 19:09:15 -07002340 @Override
2341 public boolean performLongClick() {
2342 mCancelTap = true;
2343 return super.performLongClick();
2344 }
2345
Michael Jurka0142d492010-08-25 17:46:15 -07002346 /**
2347 * Set true to allow long-press events to be triggered, usually checked by
2348 * {@link Launcher} to accept or block dpad-initiated long-presses.
2349 */
2350 public void setAllowLongPress(boolean allowLongPress) {
2351 mAllowLongPress = allowLongPress;
2352 }
2353
Winson Chung321e9ee2010-08-09 13:37:56 -07002354 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002355 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002356
2357 SavedState(Parcelable superState) {
2358 super(superState);
2359 }
2360
Adam Cohen091440a2015-03-18 14:16:05 -07002361 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002362 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002363 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002364 }
2365
2366 @Override
2367 public void writeToParcel(Parcel out, int flags) {
2368 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002369 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002370 }
2371
2372 public static final Parcelable.Creator<SavedState> CREATOR =
2373 new Parcelable.Creator<SavedState>() {
2374 public SavedState createFromParcel(Parcel in) {
2375 return new SavedState(in);
2376 }
2377
2378 public SavedState[] newArray(int size) {
2379 return new SavedState[size];
2380 }
2381 };
2382 }
2383
Winson Chungf314b0e2011-08-16 11:54:27 -07002384 protected void loadAssociatedPages(int page) {
2385 loadAssociatedPages(page, false);
2386 }
2387 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002388 if (mContentIsRefreshable) {
2389 final int count = getChildCount();
2390 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002391 int lowerPageBound = getAssociatedLowerPageBound(page);
2392 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002393 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2394 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002395 // First, clear any pages that should no longer be loaded
2396 for (int i = 0; i < count; ++i) {
2397 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002399 if (layout.getPageChildCount() > 0) {
2400 layout.removeAllViewsOnPage();
2401 }
2402 mDirtyPageContent.set(i, true);
2403 }
2404 }
2405 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002406 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002407 if ((i != page) && immediateAndOnly) {
2408 continue;
2409 }
Michael Jurka0142d492010-08-25 17:46:15 -07002410 if (lowerPageBound <= i && i <= upperPageBound) {
2411 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002412 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002413 mDirtyPageContent.set(i, false);
2414 }
Winson Chung86f77532010-08-24 11:08:22 -07002415 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002416 }
2417 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002418 }
2419 }
2420
Winson Chunge3193b92010-09-10 11:44:42 -07002421 protected int getAssociatedLowerPageBound(int page) {
2422 return Math.max(0, page - 1);
2423 }
2424 protected int getAssociatedUpperPageBound(int page) {
2425 final int count = getChildCount();
2426 return Math.min(page + 1, count - 1);
2427 }
2428
Winson Chung86f77532010-08-24 11:08:22 -07002429 /**
2430 * This method is called ONLY to synchronize the number of pages that the paged view has.
2431 * To actually fill the pages with information, implement syncPageItems() below. It is
2432 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2433 * and therefore, individual page items do not need to be updated in this method.
2434 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002435 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002436
2437 /**
2438 * This method is called to synchronize the items that are on a particular page. If views on
2439 * the page can be reused, then they should be updated within this method.
2440 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002441 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002442
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002443 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002444 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002445 }
2446 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002447 invalidatePageData(currentPage, false);
2448 }
2449 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002450 if (!mIsDataReady) {
2451 return;
2452 }
2453
Michael Jurka0142d492010-08-25 17:46:15 -07002454 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002455 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002456 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002457
Michael Jurka0142d492010-08-25 17:46:15 -07002458 // Update all the pages
2459 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002460
Winson Chung5a808352011-06-27 19:08:49 -07002461 // We must force a measure after we've loaded the pages to update the content width and
2462 // to determine the full scroll width
2463 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2464 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2465
2466 // Set a new page as the current page if necessary
2467 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002468 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002469 }
2470
Michael Jurka0142d492010-08-25 17:46:15 -07002471 // Mark each of the pages as dirty
2472 final int count = getChildCount();
2473 mDirtyPageContent.clear();
2474 for (int i = 0; i < count; ++i) {
2475 mDirtyPageContent.add(true);
2476 }
2477
2478 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002479 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002480 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002481 }
Adam Cohen06559042013-09-29 18:30:56 -07002482 if (isPageMoving()) {
2483 // If the page is moving, then snap it to the final position to ensure we don't get
2484 // stuck between pages
2485 snapToDestination();
2486 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002487 }
Winson Chung007c6982011-06-14 13:27:53 -07002488
Adam Cohen7d30a372013-07-01 17:03:59 -07002489 // Animate the drag view back to the original position
2490 void animateDragViewToOriginalPosition() {
2491 if (mDragView != null) {
2492 AnimatorSet anim = new AnimatorSet();
2493 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2494 anim.playTogether(
2495 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2497 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2498 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002499 anim.addListener(new AnimatorListenerAdapter() {
2500 @Override
2501 public void onAnimationEnd(Animator animation) {
2502 onPostReorderingAnimationCompleted();
2503 }
2504 });
2505 anim.start();
2506 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002507 }
2508
Adam Cohen7d30a372013-07-01 17:03:59 -07002509 protected void onStartReordering() {
2510 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2511 mTouchState = TOUCH_STATE_REORDERING;
2512 mIsReordering = true;
2513
Adam Cohen7d30a372013-07-01 17:03:59 -07002514 // We must invalidate to trigger a redraw to update the layers such that the drag view
2515 // is always drawn on top
2516 invalidate();
2517 }
2518
Adam Cohen091440a2015-03-18 14:16:05 -07002519 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002520 // Trigger the callback when reordering has settled
2521 --mPostReorderingPreZoomInRemainingAnimationCount;
2522 if (mPostReorderingPreZoomInRunnable != null &&
2523 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2524 mPostReorderingPreZoomInRunnable.run();
2525 mPostReorderingPreZoomInRunnable = null;
2526 }
2527 }
2528
2529 protected void onEndReordering() {
2530 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002531 }
2532
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002534 int dragViewIndex = indexOfChild(v);
2535
Adam Cohen327acfe2014-06-06 11:52:52 -07002536 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002537
Adam Cohen7d30a372013-07-01 17:03:59 -07002538 mTempVisiblePagesRange[0] = 0;
2539 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002540 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002541 mReorderingStarted = true;
2542
2543 // Check if we are within the reordering range
2544 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002545 dragViewIndex <= mTempVisiblePagesRange[1]) {
2546 // Find the drag view under the pointer
2547 mDragView = getChildAt(dragViewIndex);
2548 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2549 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002550 snapToPage(getPageNearestToCenterOfScreen());
2551 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002552 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002553 return true;
2554 }
2555 return false;
2556 }
2557
2558 boolean isReordering(boolean testTouchState) {
2559 boolean state = mIsReordering;
2560 if (testTouchState) {
2561 state &= (mTouchState == TOUCH_STATE_REORDERING);
2562 }
2563 return state;
2564 }
2565 void endReordering() {
2566 // For simplicity, we call endReordering sometimes even if reordering was never started.
2567 // In that case, we don't want to do anything.
2568 if (!mReorderingStarted) return;
2569 mReorderingStarted = false;
2570
2571 // If we haven't flung-to-delete the current child, then we just animate the drag view
2572 // back into position
2573 final Runnable onCompleteRunnable = new Runnable() {
2574 @Override
2575 public void run() {
2576 onEndReordering();
2577 }
2578 };
2579 if (!mDeferringForDelete) {
2580 mPostReorderingPreZoomInRunnable = new Runnable() {
2581 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002582 onCompleteRunnable.run();
2583 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002584 };
2585 };
2586
2587 mPostReorderingPreZoomInRemainingAnimationCount =
2588 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2589 // Snap to the current page
2590 snapToPage(indexOfChild(mDragView), 0);
2591 // Animate the drag view back to the front position
2592 animateDragViewToOriginalPosition();
2593 } else {
2594 // Handled in post-delete-animation-callbacks
2595 }
2596 }
2597
Adam Cohen7d30a372013-07-01 17:03:59 -07002598 /*
2599 * Flinging to delete - IN PROGRESS
2600 */
2601 private PointF isFlingingToDelete() {
2602 ViewConfiguration config = ViewConfiguration.get(getContext());
2603 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2604
2605 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2606 // Do a quick dot product test to ensure that we are flinging upwards
2607 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2608 mVelocityTracker.getYVelocity());
2609 PointF upVec = new PointF(0f, -1f);
2610 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2611 (vel.length() * upVec.length()));
2612 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2613 return vel;
2614 }
2615 }
2616 return null;
2617 }
2618
2619 /**
2620 * Creates an animation from the current drag view along its current velocity vector.
2621 * For this animation, the alpha runs for a fixed duration and we update the position
2622 * progressively.
2623 */
2624 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2625 private View mDragView;
2626 private PointF mVelocity;
2627 private Rect mFrom;
2628 private long mPrevTime;
2629 private float mFriction;
2630
2631 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2632
2633 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2634 long startTime, float friction) {
2635 mDragView = dragView;
2636 mVelocity = vel;
2637 mFrom = from;
2638 mPrevTime = startTime;
2639 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2640 }
2641
2642 @Override
2643 public void onAnimationUpdate(ValueAnimator animation) {
2644 float t = ((Float) animation.getAnimatedValue()).floatValue();
2645 long curTime = AnimationUtils.currentAnimationTimeMillis();
2646
2647 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2648 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2649
2650 mDragView.setTranslationX(mFrom.left);
2651 mDragView.setTranslationY(mFrom.top);
2652 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2653
2654 mVelocity.x *= mFriction;
2655 mVelocity.y *= mFriction;
2656 mPrevTime = curTime;
2657 }
2658 };
2659
2660 private static final int ANIM_TAG_KEY = 100;
2661
2662 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2663 return new Runnable() {
2664 @Override
2665 public void run() {
2666 int dragViewIndex = indexOfChild(dragView);
2667
2668 // For each of the pages around the drag view, animate them from the previous
2669 // position to the new position in the layout (as a result of the drag view moving
2670 // in the layout)
2671 // NOTE: We can make an assumption here because we have side-bound pages that we
2672 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002673 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002674 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2675 boolean slideFromLeft = (isLastWidgetPage ||
2676 dragViewIndex > mTempVisiblePagesRange[0]);
2677
2678 // Setup the scroll to the correct page before we swap the views
2679 if (slideFromLeft) {
2680 snapToPageImmediately(dragViewIndex - 1);
2681 }
2682
2683 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2684 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2685 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2686 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2687 ArrayList<Animator> animations = new ArrayList<Animator>();
2688 for (int i = lowerIndex; i <= upperIndex; ++i) {
2689 View v = getChildAt(i);
2690 // dragViewIndex < pageUnderPointIndex, so after we remove the
2691 // drag view all subsequent views to pageUnderPointIndex will
2692 // shift down.
2693 int oldX = 0;
2694 int newX = 0;
2695 if (slideFromLeft) {
2696 if (i == 0) {
2697 // Simulate the page being offscreen with the page spacing
2698 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002699 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002700 } else {
2701 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2702 }
2703 newX = getViewportOffsetX() + getChildOffset(i);
2704 } else {
2705 oldX = getChildOffset(i) - getChildOffset(i - 1);
2706 newX = 0;
2707 }
2708
2709 // Animate the view translation from its old position to its new
2710 // position
2711 AnimatorSet anim = (AnimatorSet) v.getTag();
2712 if (anim != null) {
2713 anim.cancel();
2714 }
2715
2716 // Note: Hacky, but we want to skip any optimizations to not draw completely
2717 // hidden views
2718 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2719 v.setTranslationX(oldX - newX);
2720 anim = new AnimatorSet();
2721 anim.playTogether(
2722 ObjectAnimator.ofFloat(v, "translationX", 0f),
2723 ObjectAnimator.ofFloat(v, "alpha", 1f));
2724 animations.add(anim);
2725 v.setTag(ANIM_TAG_KEY, anim);
2726 }
2727
2728 AnimatorSet slideAnimations = new AnimatorSet();
2729 slideAnimations.playTogether(animations);
2730 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2731 slideAnimations.addListener(new AnimatorListenerAdapter() {
2732 @Override
2733 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002734 mDeferringForDelete = false;
2735 onEndReordering();
2736 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002737 }
2738 });
2739 slideAnimations.start();
2740
2741 removeView(dragView);
2742 onRemoveView(dragView, true);
2743 }
2744 };
2745 }
2746
2747 public void onFlingToDelete(PointF vel) {
2748 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2749
2750 // NOTE: Because it takes time for the first frame of animation to actually be
2751 // called and we expect the animation to be a continuation of the fling, we have
2752 // to account for the time that has elapsed since the fling finished. And since
2753 // we don't have a startDelay, we will always get call to update when we call
2754 // start() (which we want to ignore).
2755 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2756 private int mCount = -1;
2757 private long mStartTime;
2758 private float mOffset;
2759 /* Anonymous inner class ctor */ {
2760 mStartTime = startTime;
2761 }
2762
2763 @Override
2764 public float getInterpolation(float t) {
2765 if (mCount < 0) {
2766 mCount++;
2767 } else if (mCount == 0) {
2768 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2769 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2770 mCount++;
2771 }
2772 return Math.min(1f, mOffset + t);
2773 }
2774 };
2775
2776 final Rect from = new Rect();
2777 final View dragView = mDragView;
2778 from.left = (int) dragView.getTranslationX();
2779 from.top = (int) dragView.getTranslationY();
2780 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2781 from, startTime, FLING_TO_DELETE_FRICTION);
2782
2783 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2784
2785 // Create and start the animation
2786 ValueAnimator mDropAnim = new ValueAnimator();
2787 mDropAnim.setInterpolator(tInterpolator);
2788 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2789 mDropAnim.setFloatValues(0f, 1f);
2790 mDropAnim.addUpdateListener(updateCb);
2791 mDropAnim.addListener(new AnimatorListenerAdapter() {
2792 public void onAnimationEnd(Animator animation) {
2793 onAnimationEndRunnable.run();
2794 }
2795 });
2796 mDropAnim.start();
2797 mDeferringForDelete = true;
2798 }
2799
2800 /* Drag to delete */
2801 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2802 if (mDeleteDropTarget != null) {
2803 mAltTmpRect.set(0, 0, 0, 0);
2804 View parent = (View) mDeleteDropTarget.getParent();
2805 if (parent != null) {
2806 parent.getGlobalVisibleRect(mAltTmpRect);
2807 }
2808 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2809 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2810 return mTmpRect.contains(x, y);
2811 }
2812 return false;
2813 }
2814
2815 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2816
2817 private void onDropToDelete() {
2818 final View dragView = mDragView;
2819
2820 final float toScale = 0f;
2821 final float toAlpha = 0f;
2822
2823 // Create and start the complex animation
2824 ArrayList<Animator> animations = new ArrayList<Animator>();
2825 AnimatorSet motionAnim = new AnimatorSet();
2826 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2827 motionAnim.playTogether(
2828 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2829 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2830 animations.add(motionAnim);
2831
2832 AnimatorSet alphaAnim = new AnimatorSet();
2833 alphaAnim.setInterpolator(new LinearInterpolator());
2834 alphaAnim.playTogether(
2835 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2836 animations.add(alphaAnim);
2837
2838 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2839
2840 AnimatorSet anim = new AnimatorSet();
2841 anim.playTogether(animations);
2842 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2843 anim.addListener(new AnimatorListenerAdapter() {
2844 public void onAnimationEnd(Animator animation) {
2845 onAnimationEndRunnable.run();
2846 }
2847 });
2848 anim.start();
2849
2850 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002851 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002852
2853 /* Accessibility */
2854 @Override
2855 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2856 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002857 info.setScrollable(getPageCount() > 1);
2858 if (getCurrentPage() < getPageCount() - 1) {
2859 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2860 }
2861 if (getCurrentPage() > 0) {
2862 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2863 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002864 }
2865
2866 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002867 public void sendAccessibilityEvent(int eventType) {
2868 // Don't let the view send real scroll events.
2869 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2870 super.sendAccessibilityEvent(eventType);
2871 }
2872 }
2873
2874 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002875 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2876 super.onInitializeAccessibilityEvent(event);
2877 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002878 }
2879
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002880 @Override
2881 public boolean performAccessibilityAction(int action, Bundle arguments) {
2882 if (super.performAccessibilityAction(action, arguments)) {
2883 return true;
2884 }
2885 switch (action) {
2886 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2887 if (getCurrentPage() < getPageCount() - 1) {
2888 scrollRight();
2889 return true;
2890 }
2891 } break;
2892 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2893 if (getCurrentPage() > 0) {
2894 scrollLeft();
2895 return true;
2896 }
2897 } break;
2898 }
2899 return false;
2900 }
2901
Adam Cohen0ffac432013-07-10 11:19:26 -07002902 protected String getCurrentPageDescription() {
2903 return String.format(getContext().getString(R.string.default_scroll_format),
2904 getNextPage() + 1, getChildCount());
2905 }
2906
Winson Chungd11265e2011-08-30 13:37:23 -07002907 @Override
2908 public boolean onHoverEvent(android.view.MotionEvent event) {
2909 return true;
2910 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002911}