blob: 5dc39309da9940f2720243c866043c395ea88bf5 [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 int getCurrentPage() {
480 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700482
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700483 /**
484 * Returns the index of page to be shown immediately afterwards.
485 */
Winson Chung360e63f2012-04-27 13:48:05 -0700486 int getNextPage() {
487 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
488 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700489
Winson Chung86f77532010-08-24 11:08:22 -0700490 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 return getChildCount();
492 }
493
Winson Chung86f77532010-08-24 11:08:22 -0700494 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 return getChildAt(index);
496 }
497
Adam Cohenae4f1552011-10-20 00:15:42 -0700498 protected int indexToPage(int index) {
499 return index;
500 }
501
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800503 * Updates the scroll of the current page immediately to its final scroll position. We use this
504 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
505 * the previous tab page.
506 */
507 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700508 // If the current page is invalid, just reset the scroll position to zero
509 int newX = 0;
510 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700511 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700512 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800513 scrollTo(newX, 0);
514 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700515 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800516 }
517
518 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700519 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
521 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700522 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700523 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700524 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800525 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700526 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700527 }
528
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700529 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700530 mScroller.abortAnimation();
531 // We need to clean up the next page here to avoid computeScrollHelper from
532 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700533 if (resetNextPage) {
534 mNextPage = INVALID_PAGE;
535 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700536 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700537
538 private void forceFinishScroller() {
539 mScroller.forceFinished(true);
540 // We need to clean up the next page here to avoid computeScrollHelper from
541 // updating current page on the pass.
542 mNextPage = INVALID_PAGE;
543 }
544
Adam Cohen6f127a62014-06-12 14:54:41 -0700545 private int validateNewPage(int newPage) {
546 int validatedPage = newPage;
547 // When in free scroll mode, we need to clamp to the free scroll page range.
548 if (mFreeScroll) {
549 getFreeScrollPageRange(mTempVisiblePagesRange);
550 validatedPage = Math.max(mTempVisiblePagesRange[0],
551 Math.min(newPage, mTempVisiblePagesRange[1]));
552 }
553 // Ensure that it is clamped by the actual set of children in all cases
554 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
555 return validatedPage;
556 }
557
Chet Haasebc2f0822012-10-26 17:59:53 -0700558 /**
Winson Chung86f77532010-08-24 11:08:22 -0700559 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 */
Winson Chung86f77532010-08-24 11:08:22 -0700561 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800562 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700563 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800564 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
566 // the default
567 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800568 return;
569 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700570 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700571 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700572 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700573 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800574 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 }
576
Winson Chung8c87cd82013-07-23 16:20:10 -0700577 /**
578 * The restore page will be set in place of the current page at the next (likely first)
579 * layout.
580 */
581 void setRestorePage(int restorePage) {
582 mRestorePage = restorePage;
583 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800584 int getRestorePage() {
585 return mRestorePage;
586 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700587
Adam Cohen674531f2013-12-13 15:07:14 -0800588 /**
589 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
590 * has settled.
591 */
Michael Jurka0142d492010-08-25 17:46:15 -0700592 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700593 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800594 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
Winson Chungd2be3812013-07-16 11:11:32 -0700596
Adam Cohen674531f2013-12-13 15:07:14 -0800597 updatePageIndicator();
598 }
599
600 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700601 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700602 if (mPageIndicator != null) {
603 mPageIndicator.setContentDescription(getPageIndicatorDescription());
604 if (!isReordering(false)) {
605 mPageIndicator.setActiveMarker(getNextPage());
606 }
Winson Chungd2be3812013-07-16 11:11:32 -0700607 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800609 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700610 if (!mIsPageMoving) {
611 mIsPageMoving = true;
612 onPageBeginMoving();
613 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700614 }
615
Michael Jurkace7e05f2011-02-01 22:02:35 -0800616 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700617 if (mIsPageMoving) {
618 mIsPageMoving = false;
619 onPageEndMoving();
620 }
Michael Jurka0142d492010-08-25 17:46:15 -0700621 }
622
Adam Cohen26976d92011-03-22 15:33:33 -0700623 protected boolean isPageMoving() {
624 return mIsPageMoving;
625 }
626
Michael Jurka0142d492010-08-25 17:46:15 -0700627 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700628 protected void onPageBeginMoving() {
629 }
630
631 // a method that subclasses can override to add behavior
632 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700633 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700634 }
635
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 /**
Winson Chung86f77532010-08-24 11:08:22 -0700637 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 *
639 * @param l The listener used to respond to long clicks.
640 */
641 @Override
642 public void setOnLongClickListener(OnLongClickListener l) {
643 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700644 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700646 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 }
Adam Cohen1697b792013-09-17 19:08:21 -0700648 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
650
651 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800652 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700653 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800654 }
655
656 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700657 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700658 // In free scroll mode, we clamp the scrollX
659 if (mFreeScroll) {
660 x = Math.min(x, mFreeScrollMaxScrollX);
661 x = Math.max(x, mFreeScrollMinScrollX);
662 }
663
Adam Cohen0ffac432013-07-10 11:19:26 -0700664 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800665 mUnboundedScrollX = x;
666
Adam Cohen0ffac432013-07-10 11:19:26 -0700667 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
668 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
669 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800670 super.scrollTo(0, y);
671 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700672 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700673 if (isRtl) {
674 overScroll(x - mMaxScrollX);
675 } else {
676 overScroll(x);
677 }
Adam Cohen68d73932010-11-15 10:50:58 -0800678 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700679 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800680 super.scrollTo(mMaxScrollX, y);
681 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700682 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700683 if (isRtl) {
684 overScroll(x);
685 } else {
686 overScroll(x - mMaxScrollX);
687 }
Adam Cohen68d73932010-11-15 10:50:58 -0800688 }
689 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700690 if (mWasInOverscroll) {
691 overScroll(0);
692 mWasInOverscroll = false;
693 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800694 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800695 super.scrollTo(x, y);
696 }
697
Michael Jurka0142d492010-08-25 17:46:15 -0700698 mTouchX = x;
699 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700700
701 // Update the last motion events when scrolling
702 if (isReordering(true)) {
703 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
704 mLastMotionX = p[0];
705 mLastMotionY = p[1];
706 updateDragViewTranslationDuringDrag();
707 }
Michael Jurka0142d492010-08-25 17:46:15 -0700708 }
709
Adam Cohen53805212013-10-01 10:39:23 -0700710 private void sendScrollAccessibilityEvent() {
711 AccessibilityManager am =
712 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
713 if (am.isEnabled()) {
714 AccessibilityEvent ev =
715 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700716 ev.setItemCount(getChildCount());
717 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700718 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700719
Alan Viverette254139a2013-10-08 13:13:46 -0700720 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700721 if (getNextPage() >= mCurrentPage) {
722 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
723 } else {
724 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
725 }
726
727 ev.setAction(action);
728 sendAccessibilityEventUnchecked(ev);
729 }
730 }
731
Michael Jurka0142d492010-08-25 17:46:15 -0700732 // we moved this functionality to a helper function so SmoothPagedView can reuse it
733 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700735 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 if (getScrollX() != mScroller.getCurrX()
737 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700738 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700739 float scaleX = mFreeScroll ? getScaleX() : 1f;
740 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
741 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700742 }
Michael Jurka0142d492010-08-25 17:46:15 -0700743 invalidate();
744 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700745 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700746 sendScrollAccessibilityEvent();
747
Adam Cohen6f127a62014-06-12 14:54:41 -0700748 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700749 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700750 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700751
Winson Chung9c0565f2013-07-19 13:49:06 -0700752 // Load the associated pages if necessary
753 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
754 loadAssociatedPages(mCurrentPage);
755 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
756 }
757
Adam Cohen73aa9752010-11-24 16:26:58 -0800758 // We don't want to trigger a page end moving unless the page has settled
759 // and the user has stopped scrolling
760 if (mTouchState == TOUCH_STATE_REST) {
761 pageEndMoving();
762 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700763
Adam Cohen7d30a372013-07-01 17:03:59 -0700764 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700765 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700766 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700767 if (am.isEnabled()) {
768 // Notify the user when the page changes
769 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700770 }
Michael Jurka0142d492010-08-25 17:46:15 -0700771 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 }
Michael Jurka0142d492010-08-25 17:46:15 -0700773 return false;
774 }
775
776 @Override
777 public void computeScroll() {
778 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 }
780
Adam Cohen7d30a372013-07-01 17:03:59 -0700781 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
782 return mTopAlignPageWhenShrinkingForBouncer;
783 }
784
Adam Cohen96d30a12013-07-16 18:13:21 -0700785 public static class LayoutParams extends ViewGroup.LayoutParams {
786 public boolean isFullScreenPage = false;
787
788 /**
789 * {@inheritDoc}
790 */
791 public LayoutParams(int width, int height) {
792 super(width, height);
793 }
794
795 public LayoutParams(ViewGroup.LayoutParams source) {
796 super(source);
797 }
798 }
799
800 protected LayoutParams generateDefaultLayoutParams() {
801 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
802 }
803
Adam Cohenedb40762013-07-18 16:45:45 -0700804 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700805 LayoutParams lp = generateDefaultLayoutParams();
806 lp.isFullScreenPage = true;
807 super.addView(page, 0, lp);
808 }
809
Adam Cohen410f3cd2013-09-22 12:09:32 -0700810 public int getNormalChildHeight() {
811 return mNormalChildHeight;
812 }
813
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 @Override
815 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700817 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
818 return;
819 }
820
Adam Cohen7d30a372013-07-01 17:03:59 -0700821 // We measure the dimensions of the PagedView to be larger than the pages so that when we
822 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
824 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
825 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700826 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800827 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700828 // viewport, we can be at most one and a half screens offset once we scale down
829 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700830 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
831 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700832
Winson Chungc82d2622013-11-06 13:23:29 -0800833 int parentWidthSize = (int) (2f * maxSize);
834 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700835 int scaledWidthSize, scaledHeightSize;
836 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700837 scaledWidthSize = (int) (parentWidthSize / mMinScale);
838 scaledHeightSize = (int) (parentHeightSize / mMinScale);
839 } else {
840 scaledWidthSize = widthSize;
841 scaledHeightSize = heightSize;
842 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700843 mViewport.set(0, 0, widthSize, heightSize);
844
845 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
846 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
847 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849
Winson Chung8aad6102012-05-11 16:27:49 -0700850 // Return early if we aren't given a proper dimension
851 if (widthSize <= 0 || heightSize <= 0) {
852 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
853 return;
854 }
855
Adam Lesinski6b879f02010-11-04 16:15:23 -0700856 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
857 * of the All apps view on XLarge displays to not take up more space then it needs. Width
858 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
859 * each page to have the same width.
860 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700861 final int verticalPadding = getPaddingTop() + getPaddingBottom();
862 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700863
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700864 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700866 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700867 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700868 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
869 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
870 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
871 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 final int childCount = getChildCount();
873 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700874 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700875 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100876 if (child.getVisibility() != GONE) {
877 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878
Vladimir Marko2824b072013-10-04 16:42:17 +0100879 int childWidthMode;
880 int childHeightMode;
881 int childWidth;
882 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700883
Vladimir Marko2824b072013-10-04 16:42:17 +0100884 if (!lp.isFullScreenPage) {
885 if (lp.width == LayoutParams.WRAP_CONTENT) {
886 childWidthMode = MeasureSpec.AT_MOST;
887 } else {
888 childWidthMode = MeasureSpec.EXACTLY;
889 }
890
891 if (lp.height == LayoutParams.WRAP_CONTENT) {
892 childHeightMode = MeasureSpec.AT_MOST;
893 } else {
894 childHeightMode = MeasureSpec.EXACTLY;
895 }
896
Adam Cohen3b185e22013-10-29 14:45:58 -0700897 childWidth = getViewportWidth() - horizontalPadding
898 - mInsets.left - mInsets.right;
899 childHeight = getViewportHeight() - verticalPadding
900 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100901 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700902 } else {
903 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700904 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100905
Adam Cohen3b185e22013-10-29 14:45:58 -0700906 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
907 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700908 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700909 if (referenceChildWidth == 0) {
910 referenceChildWidth = childWidth;
911 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700912
Vladimir Marko2824b072013-10-04 16:42:17 +0100913 final int childWidthMeasureSpec =
914 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
915 final int childHeightMeasureSpec =
916 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
917 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700919 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700920 if (mSpacePagesAutomatically) {
921 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
922 - referenceChildWidth) / 2;
923 if (spacing >= 0) {
924 setPageSpacing(spacing);
925 }
926 mSpacePagesAutomatically = false;
927 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700928 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800929 }
930
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700931 /**
932 * This method should be called once before first layout / measure pass.
933 */
934 protected void setSinglePageInViewport() {
935 mSpacePagesAutomatically = true;
936 }
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700939 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700941 return;
942 }
943
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700946
Adam Cohen7d30a372013-07-01 17:03:59 -0700947 int offsetX = getViewportOffsetX();
948 int offsetY = getViewportOffsetY();
949
950 // Update the viewport offsets
951 mViewport.offset(offsetX, offsetY);
952
Adam Cohen0ffac432013-07-10 11:19:26 -0700953 final boolean isRtl = isLayoutRtl();
954
955 final int startIndex = isRtl ? childCount - 1 : 0;
956 final int endIndex = isRtl ? -1 : childCount;
957 final int delta = isRtl ? -1 : 1;
958
Adam Cohen7d30a372013-07-01 17:03:59 -0700959 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700960
Adam Cohen3b185e22013-10-29 14:45:58 -0700961 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000962 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700963
964 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700965 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
966 mPageScrolls = new int[getChildCount()];
967 }
968
Adam Cohen0ffac432013-07-10 11:19:26 -0700969 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700970 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700972 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100973 int childTop;
974 if (lp.isFullScreenPage) {
975 childTop = offsetY;
976 } else {
977 childTop = offsetY + getPaddingTop() + mInsets.top;
978 if (mCenterPagesVertically) {
979 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
980 }
981 }
982
Adam Cohen96d30a12013-07-16 18:13:21 -0700983 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700984 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800985
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700987 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800988 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700989
Adam Cohen3b185e22013-10-29 14:45:58 -0700990 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
991 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000992
993 int pageGap = mPageSpacing;
994 int next = i + delta;
995 if (next != endIndex) {
996 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
997 } else {
998 nextLp = null;
999 }
1000
1001 // Prevent full screen pages from showing in the viewport
1002 // when they are not the current page.
1003 if (lp.isFullScreenPage) {
1004 pageGap = getPaddingLeft();
1005 } else if (nextLp != null && nextLp.isFullScreenPage) {
1006 pageGap = getPaddingRight();
1007 }
1008
1009 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 }
1011 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001012
1013 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001014 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001015 mFirstLayout = false;
1016 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001017
1018 if (childCount > 0) {
1019 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -07001020 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -07001021 } else {
1022 mMaxScrollX = 0;
1023 }
1024
1025 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1026 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001027 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001028 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001029 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001030 } else {
1031 setCurrentPage(getNextPage());
1032 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001033 }
1034 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001035
1036 if (isReordering(true)) {
1037 updateDragViewTranslationDuringDrag();
1038 }
Winson Chunge3193b92010-09-10 11:44:42 -07001039 }
1040
Adam Cohen3b185e22013-10-29 14:45:58 -07001041 public void setPageSpacing(int pageSpacing) {
1042 mPageSpacing = pageSpacing;
1043 requestLayout();
1044 }
1045
Adam Cohenf34bab52010-09-30 14:11:56 -07001046 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001047 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1048
1049 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001050 for (int i = 0; i < getChildCount(); i++) {
1051 View child = getChildAt(i);
1052 if (child != null) {
1053 float scrollProgress = getScrollProgress(screenCenter, child, i);
1054 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001055 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001056 }
1057 }
1058 invalidate();
1059 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001060 }
1061
Winson Chungc58497e2013-09-03 17:48:37 -07001062 protected void enablePagedViewAnimations() {
1063 mAllowPagedViewAnimations = true;
1064
1065 }
1066 protected void disablePagedViewAnimations() {
1067 mAllowPagedViewAnimations = false;
1068 }
1069
Winson Chunge3193b92010-09-10 11:44:42 -07001070 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001071 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001072 // Update the page indicator, we don't update the page indicator as we
1073 // add/remove pages
1074 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001075 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001076 mPageIndicator.addMarker(pageIndex,
1077 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001078 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001079 }
1080
Adam Cohen2591f6a2011-10-25 14:36:40 -07001081 // This ensures that when children are added, they get the correct transforms / alphas
1082 // in accordance with any scroll effects.
1083 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001084 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001085 invalidate();
1086 }
1087
Michael Jurka8b805b12012-04-18 14:23:14 -07001088 @Override
1089 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001090 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001091 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001092 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001093 }
1094
Winson Chungd2be3812013-07-16 11:11:32 -07001095 private void removeMarkerForView(int index) {
1096 // Update the page indicator, we don't update the page indicator as we
1097 // add/remove pages
1098 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001099 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001100 }
1101 }
1102
1103 @Override
1104 public void removeView(View v) {
1105 // XXX: We should find a better way to hook into this before the view
1106 // gets removed form its parent...
1107 removeMarkerForView(indexOfChild(v));
1108 super.removeView(v);
1109 }
1110 @Override
1111 public void removeViewInLayout(View v) {
1112 // XXX: We should find a better way to hook into this before the view
1113 // gets removed form its parent...
1114 removeMarkerForView(indexOfChild(v));
1115 super.removeViewInLayout(v);
1116 }
1117 @Override
1118 public void removeViewAt(int index) {
1119 // XXX: We should find a better way to hook into this before the view
1120 // gets removed form its parent...
1121 removeViewAt(index);
1122 super.removeViewAt(index);
1123 }
1124 @Override
1125 public void removeAllViewsInLayout() {
1126 // Update the page indicator, we don't update the page indicator as we
1127 // add/remove pages
1128 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001129 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001130 }
1131
1132 super.removeAllViewsInLayout();
1133 }
1134
Adam Cohen73894962011-10-31 13:17:17 -07001135 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001136 if (index < 0 || index > getChildCount() - 1) return 0;
1137
Adam Cohenf698c6e2013-07-17 18:44:25 -07001138 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001139
Adam Cohenf698c6e2013-07-17 18:44:25 -07001140 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001141 }
1142
Adam Cohen6f127a62014-06-12 14:54:41 -07001143 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001144 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001145 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001146 }
1147
Michael Jurkadde558b2011-11-09 22:09:06 -08001148 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001149 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001150 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001151
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001152 range[0] = -1;
1153 range[1] = -1;
1154
Michael Jurkac4fb9172010-09-02 17:19:20 -07001155 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001156 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001157 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001158
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001159 int count = getChildCount();
1160 for (int i = 0; i < count; i++) {
1161 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001162
Winson Chungc9ca2982013-07-19 12:07:38 -07001163 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001164 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001165 if (mTmpIntPoint[0] > viewportWidth) {
1166 if (range[0] == -1) {
1167 continue;
1168 } else {
1169 break;
1170 }
1171 }
1172
Adam Cohen6a678da2013-09-24 10:58:01 -07001173 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001174 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1175 if (mTmpIntPoint[0] < 0) {
1176 if (range[0] == -1) {
1177 continue;
1178 } else {
1179 break;
1180 }
1181 }
1182 curScreen = i;
1183 if (range[0] < 0) {
1184 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001185 }
1186 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001187
1188 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001189 } else {
1190 range[0] = -1;
1191 range[1] = -1;
1192 }
1193 }
1194
Michael Jurka920d7f42012-05-14 16:29:55 -07001195 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001196 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001197 }
1198
Michael Jurkadde558b2011-11-09 22:09:06 -08001199 @Override
1200 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001201 // Find out which screens are visible; as an optimization we only call draw on them
1202 final int pageCount = getChildCount();
1203 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001204 int halfScreenSize = getViewportWidth() / 2;
1205 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1206 // Otherwise it is equal to the scaled overscroll position.
1207 int screenCenter = mOverScrollX + halfScreenSize;
1208
1209 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1210 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1211 // set it for the next frame
1212 mForceScreenScrolled = false;
1213 screenScrolled(screenCenter);
1214 mLastScreenCenter = screenCenter;
1215 }
1216
Michael Jurkadde558b2011-11-09 22:09:06 -08001217 getVisiblePages(mTempVisiblePagesRange);
1218 final int leftScreen = mTempVisiblePagesRange[0];
1219 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001220 if (leftScreen != -1 && rightScreen != -1) {
1221 final long drawingTime = getDrawingTime();
1222 // Clip to the bounds
1223 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001224 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1225 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001226
Adam Cohen7d30a372013-07-01 17:03:59 -07001227 // Draw all the children, leaving the drag view for last
1228 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001229 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001230 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001231 if (mForceDrawAllChildrenNextFrame ||
1232 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001233 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001234 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001235 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001236 // Draw the drag view on top (if there is one)
1237 if (mDragView != null) {
1238 drawChild(canvas, mDragView, drawingTime);
1239 }
1240
Michael Jurka5e368ff2012-05-14 23:13:15 -07001241 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001242 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001243 }
Michael Jurka0142d492010-08-25 17:46:15 -07001244 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246
1247 @Override
1248 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001249 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001250 if (page != mCurrentPage || !mScroller.isFinished()) {
1251 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 return true;
1253 }
1254 return false;
1255 }
1256
1257 @Override
1258 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001259 int focusablePage;
1260 if (mNextPage != INVALID_PAGE) {
1261 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001263 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 }
Winson Chung86f77532010-08-24 11:08:22 -07001265 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001267 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 }
1269 return false;
1270 }
1271
1272 @Override
1273 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001274 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001276 if (getCurrentPage() > 0) {
1277 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 return true;
1279 }
1280 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001281 if (getCurrentPage() < getPageCount() - 1) {
1282 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 return true;
1284 }
1285 }
1286 return super.dispatchUnhandledMove(focused, direction);
1287 }
1288
1289 @Override
1290 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001291 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001292 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001293 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 }
1295 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001296 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001297 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 }
1299 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001300 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001301 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 }
1303 }
1304 }
1305
1306 /**
1307 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001308 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 *
Winson Chung86f77532010-08-24 11:08:22 -07001310 * This happens when live folders requery, and if they're off page, they
1311 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 */
1313 @Override
1314 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001315 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 View v = focused;
1317 while (true) {
1318 if (v == current) {
1319 super.focusableViewAvailable(focused);
1320 return;
1321 }
1322 if (v == this) {
1323 return;
1324 }
1325 ViewParent parent = v.getParent();
1326 if (parent instanceof View) {
1327 v = (View)v.getParent();
1328 } else {
1329 return;
1330 }
1331 }
1332 }
1333
1334 /**
1335 * {@inheritDoc}
1336 */
1337 @Override
1338 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1339 if (disallowIntercept) {
1340 // We need to make sure to cancel our long press if
1341 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001342 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001343 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 }
1345 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1346 }
1347
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001348 /**
1349 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1350 */
1351 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001352 if (isLayoutRtl()) {
1353 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001354 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001355 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001356 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001357 }
1358
1359 /**
1360 * Return true if a tap at (x, y) should trigger a flip to the next page.
1361 */
1362 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001363 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001364 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001365 }
1366 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001367 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001368 }
Winson Chung52aee602013-01-30 12:01:02 -08001369
Adam Cohen7d30a372013-07-01 17:03:59 -07001370 /** Returns whether x and y originated within the buffered viewport */
1371 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1372 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1373 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1374 return mTmpRect.contains(x, y);
1375 }
1376
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 @Override
1378 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001379 if (DISABLE_TOUCH_INTERACTION) {
1380 return false;
1381 }
1382
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 /*
1384 * This method JUST determines whether we want to intercept the motion.
1385 * If we return true, onTouchEvent will be called and we do the actual
1386 * scrolling there.
1387 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001388 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001389
Winson Chung45e1d6e2010-11-09 17:19:49 -08001390 // Skip touch handling if there are no pages to swipe
1391 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1392
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 /*
1394 * Shortcut the most recurring case: the user is in the dragging
1395 * state and he is moving his finger. We want to intercept this
1396 * motion.
1397 */
1398 final int action = ev.getAction();
1399 if ((action == MotionEvent.ACTION_MOVE) &&
1400 (mTouchState == TOUCH_STATE_SCROLLING)) {
1401 return true;
1402 }
1403
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 switch (action & MotionEvent.ACTION_MASK) {
1405 case MotionEvent.ACTION_MOVE: {
1406 /*
1407 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1408 * whether the user has moved far enough from his original down touch.
1409 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001410 if (mActivePointerId != INVALID_POINTER) {
1411 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001412 }
1413 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1414 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1415 // i.e. fall through to the next case (don't break)
1416 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1417 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001418 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 }
1420
1421 case MotionEvent.ACTION_DOWN: {
1422 final float x = ev.getX();
1423 final float y = ev.getY();
1424 // Remember location of down touch
1425 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001426 mDownMotionY = y;
1427 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 mLastMotionX = x;
1429 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001430 float[] p = mapPointFromViewToParent(this, x, y);
1431 mParentDownMotionX = p[0];
1432 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001433 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001434 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001436
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 /*
1438 * If being flinged and user touches the screen, initiate drag;
1439 * otherwise don't. mScroller.isFinished should be false when
1440 * being flinged.
1441 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001442 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001443 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001444
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001445 if (finishedScrolling) {
1446 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001447 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001448 setCurrentPage(getNextPage());
1449 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001450 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001451 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001452 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1453 mTouchState = TOUCH_STATE_SCROLLING;
1454 } else {
1455 mTouchState = TOUCH_STATE_REST;
1456 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001457 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001458
Winson Chung86f77532010-08-24 11:08:22 -07001459 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001461 if (!DISABLE_TOUCH_SIDE_PAGES) {
1462 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1463 if (getChildCount() > 0) {
1464 if (hitsPreviousPage(x, y)) {
1465 mTouchState = TOUCH_STATE_PREV_PAGE;
1466 } else if (hitsNextPage(x, y)) {
1467 mTouchState = TOUCH_STATE_NEXT_PAGE;
1468 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 }
1470 }
1471 }
1472 break;
1473 }
1474
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001476 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001477 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 break;
1479
1480 case MotionEvent.ACTION_POINTER_UP:
1481 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001482 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 break;
1484 }
1485
1486 /*
1487 * The only time we want to intercept motion events is if we are in the
1488 * drag mode.
1489 */
1490 return mTouchState != TOUCH_STATE_REST;
1491 }
1492
Adam Cohenf8d28232011-02-01 21:47:00 -08001493 protected void determineScrollingStart(MotionEvent ev) {
1494 determineScrollingStart(ev, 1.0f);
1495 }
1496
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 /*
1498 * Determines if we should change the touch state to start scrolling after the
1499 * user moves their touch point too far.
1500 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001501 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001502 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001504 if (pointerIndex == -1) return;
1505
1506 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 final float x = ev.getX(pointerIndex);
1508 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001509 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1510
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 final int xDiff = (int) Math.abs(x - mLastMotionX);
1512 final int yDiff = (int) Math.abs(y - mLastMotionY);
1513
Adam Cohenf8d28232011-02-01 21:47:00 -08001514 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 boolean xPaged = xDiff > mPagingTouchSlop;
1516 boolean xMoved = xDiff > touchSlop;
1517 boolean yMoved = yDiff > touchSlop;
1518
Adam Cohenf8d28232011-02-01 21:47:00 -08001519 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001520 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 // Scroll if the user moved far enough along the X axis
1522 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001523 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001525 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001527 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001528 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001529 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001531 }
1532 }
1533
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 protected float getMaxScrollProgress() {
1535 return 1.0f;
1536 }
1537
Adam Cohenf8d28232011-02-01 21:47:00 -08001538 protected void cancelCurrentPageLongPress() {
1539 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001540 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001541 // Try canceling the long press. It could also have been scheduled
1542 // by a distant descendant, so use the mAllowLongPress flag to block
1543 // everything
1544 final View currentPage = getPageAt(mCurrentPage);
1545 if (currentPage != null) {
1546 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 }
1548 }
1549 }
1550
Adam Cohen7d30a372013-07-01 17:03:59 -07001551 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1552 final int halfScreenSize = getViewportWidth() / 2;
1553
1554 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1555 screenCenter = Math.max(halfScreenSize, screenCenter);
1556
1557 return getScrollProgress(screenCenter, v, page);
1558 }
1559
Adam Cohenb5ba0972011-09-07 18:02:31 -07001560 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001561 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001562
Adam Cohenedb40762013-07-18 16:45:45 -07001563 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001564 int count = getChildCount();
1565
1566 final int totalDistance;
1567
1568 int adjacentPage = page + 1;
1569 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1570 adjacentPage = page - 1;
1571 }
1572
1573 if (adjacentPage < 0 || adjacentPage > count - 1) {
1574 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1575 } else {
1576 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1577 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001578
1579 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001580 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1581 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001582 return scrollProgress;
1583 }
1584
Adam Cohenedb40762013-07-18 16:45:45 -07001585 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001586 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001587 return 0;
1588 } else {
1589 return mPageScrolls[index];
1590 }
1591 }
1592
Adam Cohen564a2e72013-10-09 14:47:32 -07001593 // While layout transitions are occurring, a child's position may stray from its baseline
1594 // position. This method returns the magnitude of this stray at any given time.
1595 public int getLayoutTransitionOffsetForPage(int index) {
1596 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1597 return 0;
1598 } else {
1599 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001600
1601 int scrollOffset = 0;
1602 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1603 if (!lp.isFullScreenPage) {
1604 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1605 }
1606
Adam Cohen564a2e72013-10-09 14:47:32 -07001607 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1608 return (int) (child.getX() - baselineX);
1609 }
1610 }
1611
Adam Cohene0f66b52010-11-23 15:06:07 -08001612 // This curve determines how the effect of scrolling over the limits of the page dimishes
1613 // as the user pulls further and further from the bounds
1614 private float overScrollInfluenceCurve(float f) {
1615 f -= 1.0f;
1616 return f * f * f + 1.0f;
1617 }
1618
Adam Cohen1e4359c2014-08-18 13:12:16 -07001619 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001620 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001621
1622 // We want to reach the max over scroll effect when the user has
1623 // over scrolled half the size of the screen
1624 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1625
Adam Cohen1e4359c2014-08-18 13:12:16 -07001626 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001627
1628 // Clamp this factor, f, to -1 < f < 1
1629 if (Math.abs(f) >= 1) {
1630 f /= Math.abs(f);
1631 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001632 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001633 }
1634
1635 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001636 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001637
1638 float f = (amount / screenSize);
1639
1640 if (f == 0) return;
1641 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1642
Adam Cohen7bfc9792011-01-28 13:52:37 -08001643 // Clamp this factor, f, to -1 < f < 1
1644 if (Math.abs(f) >= 1) {
1645 f /= Math.abs(f);
1646 }
1647
Adam Cohene0f66b52010-11-23 15:06:07 -08001648 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001649 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001650 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001651 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001652 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001653 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001654 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001655 }
1656 invalidate();
1657 }
1658
Adam Cohenb5ba0972011-09-07 18:02:31 -07001659 protected void overScroll(float amount) {
1660 dampedOverScroll(amount);
1661 }
1662
Michael Jurkac5b262c2011-01-12 20:24:50 -08001663 protected float maxOverScroll() {
1664 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001665 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001666 float f = 1.0f;
1667 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1668 return OVERSCROLL_DAMP_FACTOR * f;
1669 }
1670
Adam Cohenf358a4b2013-07-23 16:47:31 -07001671 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001672 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001673 }
1674
Adam Cohenf9618852013-11-08 06:45:03 -08001675 protected void disableFreeScroll() {
1676 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001677 }
1678
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001679 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001680 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001681 if (isLayoutRtl()) {
1682 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1683 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1684 } else {
1685 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1686 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1687 }
1688 }
1689
Adam Cohenf9618852013-11-08 06:45:03 -08001690 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001691 mFreeScroll = freeScroll;
1692
Adam Cohenf9618852013-11-08 06:45:03 -08001693 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001694 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001695 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001696 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1697 setCurrentPage(mTempVisiblePagesRange[0]);
1698 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1699 setCurrentPage(mTempVisiblePagesRange[1]);
1700 }
1701 }
1702
1703 setEnableOverscroll(!freeScroll);
1704 }
1705
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001706 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001707 mAllowOverScroll = enable;
1708 }
1709
1710 int getNearestHoverOverPageIndex() {
1711 if (mDragView != null) {
1712 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1713 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001714 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001715 int minDistance = Integer.MAX_VALUE;
1716 int minIndex = indexOfChild(mDragView);
1717 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1718 View page = getPageAt(i);
1719 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1720 int d = Math.abs(dragX - pageX);
1721 if (d < minDistance) {
1722 minIndex = i;
1723 minDistance = d;
1724 }
1725 }
1726 return minIndex;
1727 }
1728 return -1;
1729 }
1730
Winson Chung321e9ee2010-08-09 13:37:56 -07001731 @Override
1732 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001733 if (DISABLE_TOUCH_INTERACTION) {
1734 return false;
1735 }
1736
Adam Cohen1697b792013-09-17 19:08:21 -07001737 super.onTouchEvent(ev);
1738
Winson Chung45e1d6e2010-11-09 17:19:49 -08001739 // Skip touch handling if there are no pages to swipe
1740 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1741
Michael Jurkab8f06722010-10-10 15:58:46 -07001742 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001743
1744 final int action = ev.getAction();
1745
1746 switch (action & MotionEvent.ACTION_MASK) {
1747 case MotionEvent.ACTION_DOWN:
1748 /*
1749 * If being flinged and user touches, stop the fling. isFinished
1750 * will be false if being flinged.
1751 */
1752 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001753 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001754 }
1755
1756 // Remember where the motion event started
1757 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001758 mDownMotionY = mLastMotionY = ev.getY();
1759 mDownScrollX = getScrollX();
1760 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1761 mParentDownMotionX = p[0];
1762 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001763 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001764 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001765 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001766
Michael Jurka0142d492010-08-25 17:46:15 -07001767 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001768 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001769 pageBeginMoving();
1770 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001771 break;
1772
1773 case MotionEvent.ACTION_MOVE:
1774 if (mTouchState == TOUCH_STATE_SCROLLING) {
1775 // Scroll to follow the motion event
1776 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001777
1778 if (pointerIndex == -1) return true;
1779
Winson Chung321e9ee2010-08-09 13:37:56 -07001780 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001781 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001782
Adam Cohenaefd4e12011-02-14 16:39:38 -08001783 mTotalMotionX += Math.abs(deltaX);
1784
Winson Chungc0844aa2011-02-02 15:25:58 -08001785 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1786 // keep the remainder because we are actually testing if we've moved from the last
1787 // scrolled position (which is discrete).
1788 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001789 mTouchX += deltaX;
1790 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1791 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001792 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001793 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001794 } else {
1795 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001796 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001797 mLastMotionX = x;
1798 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001799 } else {
1800 awakenScrollBars();
1801 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001802 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1803 // Update the last motion position
1804 mLastMotionX = ev.getX();
1805 mLastMotionY = ev.getY();
1806
1807 // Update the parent down so that our zoom animations take this new movement into
1808 // account
1809 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1810 mParentDownMotionX = pt[0];
1811 mParentDownMotionY = pt[1];
1812 updateDragViewTranslationDuringDrag();
1813
1814 // Find the closest page to the touch point
1815 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001816
1817 // Change the drag view if we are hovering over the drop target
1818 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1819 (int) mParentDownMotionX, (int) mParentDownMotionY);
1820 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1821
Adam Cohen7d30a372013-07-01 17:03:59 -07001822 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1823 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1824 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1825 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1826
Adam Cohenf358a4b2013-07-23 16:47:31 -07001827 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1828 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1829 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001830 mTempVisiblePagesRange[0] = 0;
1831 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001832 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1834 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1835 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1836 mSidePageHoverIndex = pageUnderPointIndex;
1837 mSidePageHoverRunnable = new Runnable() {
1838 @Override
1839 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001840 // Setup the scroll to the correct page before we swap the views
1841 snapToPage(pageUnderPointIndex);
1842
1843 // For each of the pages between the paged view and the drag view,
1844 // animate them from the previous position to the new position in
1845 // the layout (as a result of the drag view moving in the layout)
1846 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1847 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1848 dragViewIndex + 1 : pageUnderPointIndex;
1849 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1850 dragViewIndex - 1 : pageUnderPointIndex;
1851 for (int i = lowerIndex; i <= upperIndex; ++i) {
1852 View v = getChildAt(i);
1853 // dragViewIndex < pageUnderPointIndex, so after we remove the
1854 // drag view all subsequent views to pageUnderPointIndex will
1855 // shift down.
1856 int oldX = getViewportOffsetX() + getChildOffset(i);
1857 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1858
1859 // Animate the view translation from its old position to its new
1860 // position
1861 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1862 if (anim != null) {
1863 anim.cancel();
1864 }
1865
1866 v.setTranslationX(oldX - newX);
1867 anim = new AnimatorSet();
1868 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1869 anim.playTogether(
1870 ObjectAnimator.ofFloat(v, "translationX", 0f));
1871 anim.start();
1872 v.setTag(anim);
1873 }
1874
1875 removeView(mDragView);
1876 onRemoveView(mDragView, false);
1877 addView(mDragView, pageUnderPointIndex);
1878 onAddView(mDragView, pageUnderPointIndex);
1879 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001880 if (mPageIndicator != null) {
1881 mPageIndicator.setActiveMarker(getNextPage());
1882 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001883 }
1884 };
1885 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1886 }
1887 } else {
1888 removeCallbacks(mSidePageHoverRunnable);
1889 mSidePageHoverIndex = -1;
1890 }
Adam Cohen564976a2010-10-13 18:52:07 -07001891 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001892 determineScrollingStart(ev);
1893 }
1894 break;
1895
1896 case MotionEvent.ACTION_UP:
1897 if (mTouchState == TOUCH_STATE_SCROLLING) {
1898 final int activePointerId = mActivePointerId;
1899 final int pointerIndex = ev.findPointerIndex(activePointerId);
1900 final float x = ev.getX(pointerIndex);
1901 final VelocityTracker velocityTracker = mVelocityTracker;
1902 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1903 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001904 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001905 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001906 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1907 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001908
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001909 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1910
Adam Cohen00481b32011-11-18 12:03:48 -08001911 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001912 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001913
Adam Cohenf358a4b2013-07-23 16:47:31 -07001914 if (!mFreeScroll) {
1915 // In the case that the page is moved far to one direction and then is flung
1916 // in the opposite direction, we use a threshold to determine whether we should
1917 // just return to the starting page, or if we should skip one further.
1918 boolean returnToOriginalPage = false;
1919 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1920 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1921 returnToOriginalPage = true;
1922 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001923
Adam Cohenf358a4b2013-07-23 16:47:31 -07001924 int finalPage;
1925 // We give flings precedence over large moves, which is why we short-circuit our
1926 // test for a large move if a fling has been registered. That is, a large
1927 // move to the left and fling to the right will register as a fling to the right.
1928 final boolean isRtl = isLayoutRtl();
1929 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1930 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1931 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1932 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1933 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1934 snapToPageWithVelocity(finalPage, velocityX);
1935 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1936 (isFling && isVelocityXLeft)) &&
1937 mCurrentPage < getChildCount() - 1) {
1938 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1939 snapToPageWithVelocity(finalPage, velocityX);
1940 } else {
1941 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001942 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001943 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001944 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001945 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001946 }
1947
1948 float scaleX = getScaleX();
1949 int vX = (int) (-velocityX * scaleX);
1950 int initialScrollX = (int) (getScrollX() * scaleX);
1951
Adam Cohenf9618852013-11-08 06:45:03 -08001952 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001953 mScroller.fling(initialScrollX,
1954 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1955 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001957 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001958 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1959 // at this point we have not moved beyond the touch slop
1960 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1961 // we can just page
1962 int nextPage = Math.max(0, mCurrentPage - 1);
1963 if (nextPage != mCurrentPage) {
1964 snapToPage(nextPage);
1965 } else {
1966 snapToDestination();
1967 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001968 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 // at this point we have not moved beyond the touch slop
1970 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1971 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001972 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1973 if (nextPage != mCurrentPage) {
1974 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001975 } else {
1976 snapToDestination();
1977 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001978 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1979 // Update the last motion position
1980 mLastMotionX = ev.getX();
1981 mLastMotionY = ev.getY();
1982
1983 // Update the parent down so that our zoom animations take this new movement into
1984 // account
1985 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1986 mParentDownMotionX = pt[0];
1987 mParentDownMotionY = pt[1];
1988 updateDragViewTranslationDuringDrag();
1989 boolean handledFling = false;
1990 if (!DISABLE_FLING_TO_DELETE) {
1991 // Check the velocity and see if we are flinging-to-delete
1992 PointF flingToDeleteVector = isFlingingToDelete();
1993 if (flingToDeleteVector != null) {
1994 onFlingToDelete(flingToDeleteVector);
1995 handledFling = true;
1996 }
1997 }
1998 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1999 (int) mParentDownMotionY)) {
2000 onDropToDelete();
2001 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002002 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002003 if (!mCancelTap) {
2004 onUnhandledTap(ev);
2005 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002006 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002007
2008 // Remove the callback to wait for the side page hover timeout
2009 removeCallbacks(mSidePageHoverRunnable);
2010 // End any intermediate reordering states
2011 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002012 break;
2013
2014 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002015 if (mTouchState == TOUCH_STATE_SCROLLING) {
2016 snapToDestination();
2017 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002018 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002019 break;
2020
2021 case MotionEvent.ACTION_POINTER_UP:
2022 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002023 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002024 break;
2025 }
2026
2027 return true;
2028 }
2029
Adam Cohen7d30a372013-07-01 17:03:59 -07002030 public void onFlingToDelete(View v) {}
2031 public void onRemoveView(View v, boolean deletePermanently) {}
2032 public void onRemoveViewAnimationCompleted() {}
2033 public void onAddView(View v, int index) {}
2034
2035 private void resetTouchState() {
2036 releaseVelocityTracker();
2037 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002038 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002039 mTouchState = TOUCH_STATE_REST;
2040 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002041 }
2042
Adam Cohenc2d6e892014-10-16 09:49:24 -07002043 /**
2044 * Triggered by scrolling via touch
2045 */
2046 protected void onScrollInteractionBegin() {
2047 }
2048
2049 protected void onScrollInteractionEnd() {
2050 }
2051
Adam Cohen1697b792013-09-17 19:08:21 -07002052 protected void onUnhandledTap(MotionEvent ev) {
2053 ((Launcher) getContext()).onClick(this);
2054 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002055
Winson Chung185d7162011-02-28 13:47:29 -08002056 @Override
2057 public boolean onGenericMotionEvent(MotionEvent event) {
2058 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2059 switch (event.getAction()) {
2060 case MotionEvent.ACTION_SCROLL: {
2061 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2062 final float vscroll;
2063 final float hscroll;
2064 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2065 vscroll = 0;
2066 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2067 } else {
2068 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2069 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2070 }
2071 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002072 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2073 : (hscroll > 0 || vscroll > 0);
2074 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002075 scrollRight();
2076 } else {
2077 scrollLeft();
2078 }
2079 return true;
2080 }
2081 }
2082 }
2083 }
2084 return super.onGenericMotionEvent(event);
2085 }
2086
Michael Jurkab8f06722010-10-10 15:58:46 -07002087 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2088 if (mVelocityTracker == null) {
2089 mVelocityTracker = VelocityTracker.obtain();
2090 }
2091 mVelocityTracker.addMovement(ev);
2092 }
2093
2094 private void releaseVelocityTracker() {
2095 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002096 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002097 mVelocityTracker.recycle();
2098 mVelocityTracker = null;
2099 }
2100 }
2101
Winson Chung321e9ee2010-08-09 13:37:56 -07002102 private void onSecondaryPointerUp(MotionEvent ev) {
2103 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2104 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2105 final int pointerId = ev.getPointerId(pointerIndex);
2106 if (pointerId == mActivePointerId) {
2107 // This was our active pointer going up. Choose a new
2108 // active pointer and adjust accordingly.
2109 // TODO: Make this decision more intelligent.
2110 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2111 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2112 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002113 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002114 mActivePointerId = ev.getPointerId(newPointerIndex);
2115 if (mVelocityTracker != null) {
2116 mVelocityTracker.clear();
2117 }
2118 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002119 }
2120
Winson Chung321e9ee2010-08-09 13:37:56 -07002121 @Override
2122 public void requestChildFocus(View child, View focused) {
2123 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002124 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002125 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002126 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002127 }
2128 }
2129
Winson Chung1908d072011-02-24 18:09:44 -08002130 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002131 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002132 }
2133
Adam Cohen7d30a372013-07-01 17:03:59 -07002134 int getPageNearestToPoint(float x) {
2135 int index = 0;
2136 for (int i = 0; i < getChildCount(); ++i) {
2137 if (x < getChildAt(i).getRight() - getScrollX()) {
2138 return index;
2139 } else {
2140 index++;
2141 }
2142 }
2143 return Math.min(index, getChildCount() - 1);
2144 }
2145
Adam Cohend19d3ca2010-09-15 14:43:42 -07002146 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002147 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002148 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002149 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002150 final int childCount = getChildCount();
2151 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002152 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002153 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002154 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002155 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002156 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2157 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2158 minDistanceFromScreenCenter = distanceFromScreenCenter;
2159 minDistanceFromScreenCenterIndex = i;
2160 }
2161 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002162 return minDistanceFromScreenCenterIndex;
2163 }
2164
Adam Cohen1e4359c2014-08-18 13:12:16 -07002165 protected boolean isInOverScroll() {
2166 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2167 }
2168
2169 protected int getPageSnapDuration() {
2170 if (isInOverScroll()) {
2171 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2172 }
2173 return PAGE_SNAP_ANIMATION_DURATION;
2174
2175 }
2176
Adam Cohend19d3ca2010-09-15 14:43:42 -07002177 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002178 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002179 }
2180
Adam Cohene0f66b52010-11-23 15:06:07 -08002181 private static class ScrollInterpolator implements Interpolator {
2182 public ScrollInterpolator() {
2183 }
2184
2185 public float getInterpolation(float t) {
2186 t -= 1.0f;
2187 return t*t*t*t*t + 1;
2188 }
2189 }
2190
2191 // We want the duration of the page snap animation to be influenced by the distance that
2192 // the screen has to travel, however, we don't want this duration to be effected in a
2193 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2194 // of travel has on the overall snap duration.
2195 float distanceInfluenceForSnapDuration(float f) {
2196 f -= 0.5f; // center the values about 0.
2197 f *= 0.3f * Math.PI / 2.0f;
2198 return (float) Math.sin(f);
2199 }
2200
Michael Jurka0142d492010-08-25 17:46:15 -07002201 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002202 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002203 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002204
Adam Cohenedb40762013-07-18 16:45:45 -07002205 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002206 int delta = newX - mUnboundedScrollX;
2207 int duration = 0;
2208
Adam Cohen1e4359c2014-08-18 13:12:16 -07002209 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002210 // If the velocity is low enough, then treat this more as an automatic page advance
2211 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002212 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002213 return;
2214 }
2215
2216 // Here we compute a "distance" that will be used in the computation of the overall
2217 // snap duration. This is a function of the actual distance that needs to be traveled;
2218 // we keep this value close to half screen size in order to reduce the variance in snap
2219 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002220 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002221 float distance = halfScreenSize + halfScreenSize *
2222 distanceInfluenceForSnapDuration(distanceRatio);
2223
2224 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002225 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002226
2227 // we want the page's snap velocity to approximately match the velocity at which the
2228 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002229 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2230 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002231
2232 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002233 }
2234
2235 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002236 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002237 }
2238
Adam Cohen7d30a372013-07-01 17:03:59 -07002239 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002240 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002241 }
2242
Michael Jurka0142d492010-08-25 17:46:15 -07002243 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002244 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002245 }
2246
Adam Cohenf9618852013-11-08 06:45:03 -08002247 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2248 snapToPage(whichPage, duration, false, interpolator);
2249 }
2250
2251 protected void snapToPage(int whichPage, int duration, boolean immediate,
2252 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002253 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002254
Adam Cohenedb40762013-07-18 16:45:45 -07002255 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002256 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002257 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002258 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002259 }
2260
2261 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002262 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002263 }
Michael Jurka0142d492010-08-25 17:46:15 -07002264
Adam Cohenf9618852013-11-08 06:45:03 -08002265 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2266 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002267 whichPage = validateNewPage(whichPage);
2268
Adam Cohen7d30a372013-07-01 17:03:59 -07002269 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002270 View focusedChild = getFocusedChild();
2271 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002272 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002273 focusedChild.clearFocus();
2274 }
2275
Adam Cohen53805212013-10-01 10:39:23 -07002276 sendScrollAccessibilityEvent();
2277
Michael Jurka0142d492010-08-25 17:46:15 -07002278 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002279 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002280 if (immediate) {
2281 duration = 0;
2282 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002283 duration = Math.abs(delta);
2284 }
2285
Adam Cohenf358a4b2013-07-23 16:47:31 -07002286 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002287 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002288 }
Adam Cohenf9618852013-11-08 06:45:03 -08002289
2290 if (interpolator != null) {
2291 mScroller.setInterpolator(interpolator);
2292 } else {
2293 mScroller.setInterpolator(mDefaultInterpolator);
2294 }
2295
Adam Cohen68d73932010-11-15 10:50:58 -08002296 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002297
Adam Cohen674531f2013-12-13 15:07:14 -08002298 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002299
2300 // Trigger a compute() to finish switching pages if necessary
2301 if (immediate) {
2302 computeScroll();
2303 }
2304
Winson Chung9c0565f2013-07-19 13:49:06 -07002305 // Defer loading associated pages until the scroll settles
2306 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2307
Adam Cohen7d30a372013-07-01 17:03:59 -07002308 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002309 invalidate();
2310 }
2311
Winson Chung321e9ee2010-08-09 13:37:56 -07002312 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002313 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002314 }
2315
2316 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002317 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002318 }
2319
Winson Chung86f77532010-08-24 11:08:22 -07002320 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002321 int result = -1;
2322 if (v != null) {
2323 ViewParent vp = v.getParent();
2324 int count = getChildCount();
2325 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002326 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002327 return i;
2328 }
2329 }
2330 }
2331 return result;
2332 }
2333
2334 /**
2335 * @return True is long presses are still allowed for the current touch
2336 */
2337 public boolean allowLongPress() {
2338 return mAllowLongPress;
2339 }
2340
Adam Cohendbdff6b2013-09-18 19:09:15 -07002341 @Override
2342 public boolean performLongClick() {
2343 mCancelTap = true;
2344 return super.performLongClick();
2345 }
2346
Michael Jurka0142d492010-08-25 17:46:15 -07002347 /**
2348 * Set true to allow long-press events to be triggered, usually checked by
2349 * {@link Launcher} to accept or block dpad-initiated long-presses.
2350 */
2351 public void setAllowLongPress(boolean allowLongPress) {
2352 mAllowLongPress = allowLongPress;
2353 }
2354
Winson Chung321e9ee2010-08-09 13:37:56 -07002355 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002356 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002357
2358 SavedState(Parcelable superState) {
2359 super(superState);
2360 }
2361
Adam Cohen091440a2015-03-18 14:16:05 -07002362 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002363 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002364 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002365 }
2366
2367 @Override
2368 public void writeToParcel(Parcel out, int flags) {
2369 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002370 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002371 }
2372
2373 public static final Parcelable.Creator<SavedState> CREATOR =
2374 new Parcelable.Creator<SavedState>() {
2375 public SavedState createFromParcel(Parcel in) {
2376 return new SavedState(in);
2377 }
2378
2379 public SavedState[] newArray(int size) {
2380 return new SavedState[size];
2381 }
2382 };
2383 }
2384
Winson Chungf314b0e2011-08-16 11:54:27 -07002385 protected void loadAssociatedPages(int page) {
2386 loadAssociatedPages(page, false);
2387 }
2388 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002389 if (mContentIsRefreshable) {
2390 final int count = getChildCount();
2391 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002392 int lowerPageBound = getAssociatedLowerPageBound(page);
2393 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002394 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2395 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002396 // First, clear any pages that should no longer be loaded
2397 for (int i = 0; i < count; ++i) {
2398 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002399 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002400 if (layout.getPageChildCount() > 0) {
2401 layout.removeAllViewsOnPage();
2402 }
2403 mDirtyPageContent.set(i, true);
2404 }
2405 }
2406 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002407 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002408 if ((i != page) && immediateAndOnly) {
2409 continue;
2410 }
Michael Jurka0142d492010-08-25 17:46:15 -07002411 if (lowerPageBound <= i && i <= upperPageBound) {
2412 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002413 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002414 mDirtyPageContent.set(i, false);
2415 }
Winson Chung86f77532010-08-24 11:08:22 -07002416 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002417 }
2418 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002419 }
2420 }
2421
Winson Chunge3193b92010-09-10 11:44:42 -07002422 protected int getAssociatedLowerPageBound(int page) {
2423 return Math.max(0, page - 1);
2424 }
2425 protected int getAssociatedUpperPageBound(int page) {
2426 final int count = getChildCount();
2427 return Math.min(page + 1, count - 1);
2428 }
2429
Winson Chung86f77532010-08-24 11:08:22 -07002430 /**
2431 * This method is called ONLY to synchronize the number of pages that the paged view has.
2432 * To actually fill the pages with information, implement syncPageItems() below. It is
2433 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2434 * and therefore, individual page items do not need to be updated in this method.
2435 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002436 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002437
2438 /**
2439 * This method is called to synchronize the items that are on a particular page. If views on
2440 * the page can be reused, then they should be updated within this method.
2441 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002442 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002443
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002444 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002445 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002446 }
2447 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002448 invalidatePageData(currentPage, false);
2449 }
2450 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002451 if (!mIsDataReady) {
2452 return;
2453 }
2454
Michael Jurka0142d492010-08-25 17:46:15 -07002455 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002456 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002457 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002458
Michael Jurka0142d492010-08-25 17:46:15 -07002459 // Update all the pages
2460 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002461
Winson Chung5a808352011-06-27 19:08:49 -07002462 // We must force a measure after we've loaded the pages to update the content width and
2463 // to determine the full scroll width
2464 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2465 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2466
2467 // Set a new page as the current page if necessary
2468 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002469 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002470 }
2471
Michael Jurka0142d492010-08-25 17:46:15 -07002472 // Mark each of the pages as dirty
2473 final int count = getChildCount();
2474 mDirtyPageContent.clear();
2475 for (int i = 0; i < count; ++i) {
2476 mDirtyPageContent.add(true);
2477 }
2478
2479 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002480 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002481 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002482 }
Adam Cohen06559042013-09-29 18:30:56 -07002483 if (isPageMoving()) {
2484 // If the page is moving, then snap it to the final position to ensure we don't get
2485 // stuck between pages
2486 snapToDestination();
2487 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002488 }
Winson Chung007c6982011-06-14 13:27:53 -07002489
Adam Cohen7d30a372013-07-01 17:03:59 -07002490 // Animate the drag view back to the original position
2491 void animateDragViewToOriginalPosition() {
2492 if (mDragView != null) {
2493 AnimatorSet anim = new AnimatorSet();
2494 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2495 anim.playTogether(
2496 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2498 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2499 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002500 anim.addListener(new AnimatorListenerAdapter() {
2501 @Override
2502 public void onAnimationEnd(Animator animation) {
2503 onPostReorderingAnimationCompleted();
2504 }
2505 });
2506 anim.start();
2507 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002508 }
2509
Adam Cohen7d30a372013-07-01 17:03:59 -07002510 protected void onStartReordering() {
2511 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2512 mTouchState = TOUCH_STATE_REORDERING;
2513 mIsReordering = true;
2514
Adam Cohen7d30a372013-07-01 17:03:59 -07002515 // We must invalidate to trigger a redraw to update the layers such that the drag view
2516 // is always drawn on top
2517 invalidate();
2518 }
2519
Adam Cohen091440a2015-03-18 14:16:05 -07002520 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002521 // Trigger the callback when reordering has settled
2522 --mPostReorderingPreZoomInRemainingAnimationCount;
2523 if (mPostReorderingPreZoomInRunnable != null &&
2524 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2525 mPostReorderingPreZoomInRunnable.run();
2526 mPostReorderingPreZoomInRunnable = null;
2527 }
2528 }
2529
2530 protected void onEndReordering() {
2531 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002532 }
2533
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002535 int dragViewIndex = indexOfChild(v);
2536
Adam Cohen327acfe2014-06-06 11:52:52 -07002537 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002538
Adam Cohen7d30a372013-07-01 17:03:59 -07002539 mTempVisiblePagesRange[0] = 0;
2540 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002541 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002542 mReorderingStarted = true;
2543
2544 // Check if we are within the reordering range
2545 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002546 dragViewIndex <= mTempVisiblePagesRange[1]) {
2547 // Find the drag view under the pointer
2548 mDragView = getChildAt(dragViewIndex);
2549 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2550 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002551 snapToPage(getPageNearestToCenterOfScreen());
2552 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002553 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002554 return true;
2555 }
2556 return false;
2557 }
2558
2559 boolean isReordering(boolean testTouchState) {
2560 boolean state = mIsReordering;
2561 if (testTouchState) {
2562 state &= (mTouchState == TOUCH_STATE_REORDERING);
2563 }
2564 return state;
2565 }
2566 void endReordering() {
2567 // For simplicity, we call endReordering sometimes even if reordering was never started.
2568 // In that case, we don't want to do anything.
2569 if (!mReorderingStarted) return;
2570 mReorderingStarted = false;
2571
2572 // If we haven't flung-to-delete the current child, then we just animate the drag view
2573 // back into position
2574 final Runnable onCompleteRunnable = new Runnable() {
2575 @Override
2576 public void run() {
2577 onEndReordering();
2578 }
2579 };
2580 if (!mDeferringForDelete) {
2581 mPostReorderingPreZoomInRunnable = new Runnable() {
2582 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002583 onCompleteRunnable.run();
2584 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002585 };
2586 };
2587
2588 mPostReorderingPreZoomInRemainingAnimationCount =
2589 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2590 // Snap to the current page
2591 snapToPage(indexOfChild(mDragView), 0);
2592 // Animate the drag view back to the front position
2593 animateDragViewToOriginalPosition();
2594 } else {
2595 // Handled in post-delete-animation-callbacks
2596 }
2597 }
2598
Adam Cohen7d30a372013-07-01 17:03:59 -07002599 /*
2600 * Flinging to delete - IN PROGRESS
2601 */
2602 private PointF isFlingingToDelete() {
2603 ViewConfiguration config = ViewConfiguration.get(getContext());
2604 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2605
2606 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2607 // Do a quick dot product test to ensure that we are flinging upwards
2608 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2609 mVelocityTracker.getYVelocity());
2610 PointF upVec = new PointF(0f, -1f);
2611 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2612 (vel.length() * upVec.length()));
2613 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2614 return vel;
2615 }
2616 }
2617 return null;
2618 }
2619
2620 /**
2621 * Creates an animation from the current drag view along its current velocity vector.
2622 * For this animation, the alpha runs for a fixed duration and we update the position
2623 * progressively.
2624 */
2625 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2626 private View mDragView;
2627 private PointF mVelocity;
2628 private Rect mFrom;
2629 private long mPrevTime;
2630 private float mFriction;
2631
2632 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2633
2634 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2635 long startTime, float friction) {
2636 mDragView = dragView;
2637 mVelocity = vel;
2638 mFrom = from;
2639 mPrevTime = startTime;
2640 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2641 }
2642
2643 @Override
2644 public void onAnimationUpdate(ValueAnimator animation) {
2645 float t = ((Float) animation.getAnimatedValue()).floatValue();
2646 long curTime = AnimationUtils.currentAnimationTimeMillis();
2647
2648 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2649 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2650
2651 mDragView.setTranslationX(mFrom.left);
2652 mDragView.setTranslationY(mFrom.top);
2653 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2654
2655 mVelocity.x *= mFriction;
2656 mVelocity.y *= mFriction;
2657 mPrevTime = curTime;
2658 }
2659 };
2660
2661 private static final int ANIM_TAG_KEY = 100;
2662
2663 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2664 return new Runnable() {
2665 @Override
2666 public void run() {
2667 int dragViewIndex = indexOfChild(dragView);
2668
2669 // For each of the pages around the drag view, animate them from the previous
2670 // position to the new position in the layout (as a result of the drag view moving
2671 // in the layout)
2672 // NOTE: We can make an assumption here because we have side-bound pages that we
2673 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002674 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002675 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2676 boolean slideFromLeft = (isLastWidgetPage ||
2677 dragViewIndex > mTempVisiblePagesRange[0]);
2678
2679 // Setup the scroll to the correct page before we swap the views
2680 if (slideFromLeft) {
2681 snapToPageImmediately(dragViewIndex - 1);
2682 }
2683
2684 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2685 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2686 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2687 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2688 ArrayList<Animator> animations = new ArrayList<Animator>();
2689 for (int i = lowerIndex; i <= upperIndex; ++i) {
2690 View v = getChildAt(i);
2691 // dragViewIndex < pageUnderPointIndex, so after we remove the
2692 // drag view all subsequent views to pageUnderPointIndex will
2693 // shift down.
2694 int oldX = 0;
2695 int newX = 0;
2696 if (slideFromLeft) {
2697 if (i == 0) {
2698 // Simulate the page being offscreen with the page spacing
2699 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002700 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002701 } else {
2702 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2703 }
2704 newX = getViewportOffsetX() + getChildOffset(i);
2705 } else {
2706 oldX = getChildOffset(i) - getChildOffset(i - 1);
2707 newX = 0;
2708 }
2709
2710 // Animate the view translation from its old position to its new
2711 // position
2712 AnimatorSet anim = (AnimatorSet) v.getTag();
2713 if (anim != null) {
2714 anim.cancel();
2715 }
2716
2717 // Note: Hacky, but we want to skip any optimizations to not draw completely
2718 // hidden views
2719 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2720 v.setTranslationX(oldX - newX);
2721 anim = new AnimatorSet();
2722 anim.playTogether(
2723 ObjectAnimator.ofFloat(v, "translationX", 0f),
2724 ObjectAnimator.ofFloat(v, "alpha", 1f));
2725 animations.add(anim);
2726 v.setTag(ANIM_TAG_KEY, anim);
2727 }
2728
2729 AnimatorSet slideAnimations = new AnimatorSet();
2730 slideAnimations.playTogether(animations);
2731 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2732 slideAnimations.addListener(new AnimatorListenerAdapter() {
2733 @Override
2734 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002735 mDeferringForDelete = false;
2736 onEndReordering();
2737 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002738 }
2739 });
2740 slideAnimations.start();
2741
2742 removeView(dragView);
2743 onRemoveView(dragView, true);
2744 }
2745 };
2746 }
2747
2748 public void onFlingToDelete(PointF vel) {
2749 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2750
2751 // NOTE: Because it takes time for the first frame of animation to actually be
2752 // called and we expect the animation to be a continuation of the fling, we have
2753 // to account for the time that has elapsed since the fling finished. And since
2754 // we don't have a startDelay, we will always get call to update when we call
2755 // start() (which we want to ignore).
2756 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2757 private int mCount = -1;
2758 private long mStartTime;
2759 private float mOffset;
2760 /* Anonymous inner class ctor */ {
2761 mStartTime = startTime;
2762 }
2763
2764 @Override
2765 public float getInterpolation(float t) {
2766 if (mCount < 0) {
2767 mCount++;
2768 } else if (mCount == 0) {
2769 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2770 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2771 mCount++;
2772 }
2773 return Math.min(1f, mOffset + t);
2774 }
2775 };
2776
2777 final Rect from = new Rect();
2778 final View dragView = mDragView;
2779 from.left = (int) dragView.getTranslationX();
2780 from.top = (int) dragView.getTranslationY();
2781 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2782 from, startTime, FLING_TO_DELETE_FRICTION);
2783
2784 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2785
2786 // Create and start the animation
2787 ValueAnimator mDropAnim = new ValueAnimator();
2788 mDropAnim.setInterpolator(tInterpolator);
2789 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2790 mDropAnim.setFloatValues(0f, 1f);
2791 mDropAnim.addUpdateListener(updateCb);
2792 mDropAnim.addListener(new AnimatorListenerAdapter() {
2793 public void onAnimationEnd(Animator animation) {
2794 onAnimationEndRunnable.run();
2795 }
2796 });
2797 mDropAnim.start();
2798 mDeferringForDelete = true;
2799 }
2800
2801 /* Drag to delete */
2802 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2803 if (mDeleteDropTarget != null) {
2804 mAltTmpRect.set(0, 0, 0, 0);
2805 View parent = (View) mDeleteDropTarget.getParent();
2806 if (parent != null) {
2807 parent.getGlobalVisibleRect(mAltTmpRect);
2808 }
2809 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2810 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2811 return mTmpRect.contains(x, y);
2812 }
2813 return false;
2814 }
2815
2816 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2817
2818 private void onDropToDelete() {
2819 final View dragView = mDragView;
2820
2821 final float toScale = 0f;
2822 final float toAlpha = 0f;
2823
2824 // Create and start the complex animation
2825 ArrayList<Animator> animations = new ArrayList<Animator>();
2826 AnimatorSet motionAnim = new AnimatorSet();
2827 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2828 motionAnim.playTogether(
2829 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2830 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2831 animations.add(motionAnim);
2832
2833 AnimatorSet alphaAnim = new AnimatorSet();
2834 alphaAnim.setInterpolator(new LinearInterpolator());
2835 alphaAnim.playTogether(
2836 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2837 animations.add(alphaAnim);
2838
2839 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2840
2841 AnimatorSet anim = new AnimatorSet();
2842 anim.playTogether(animations);
2843 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2844 anim.addListener(new AnimatorListenerAdapter() {
2845 public void onAnimationEnd(Animator animation) {
2846 onAnimationEndRunnable.run();
2847 }
2848 });
2849 anim.start();
2850
2851 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002852 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002853
2854 /* Accessibility */
2855 @Override
2856 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2857 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002858 info.setScrollable(getPageCount() > 1);
2859 if (getCurrentPage() < getPageCount() - 1) {
2860 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2861 }
2862 if (getCurrentPage() > 0) {
2863 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2864 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002865 }
2866
2867 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002868 public void sendAccessibilityEvent(int eventType) {
2869 // Don't let the view send real scroll events.
2870 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2871 super.sendAccessibilityEvent(eventType);
2872 }
2873 }
2874
2875 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002876 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2877 super.onInitializeAccessibilityEvent(event);
2878 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002879 }
2880
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002881 @Override
2882 public boolean performAccessibilityAction(int action, Bundle arguments) {
2883 if (super.performAccessibilityAction(action, arguments)) {
2884 return true;
2885 }
2886 switch (action) {
2887 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2888 if (getCurrentPage() < getPageCount() - 1) {
2889 scrollRight();
2890 return true;
2891 }
2892 } break;
2893 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2894 if (getCurrentPage() > 0) {
2895 scrollLeft();
2896 return true;
2897 }
2898 } break;
2899 }
2900 return false;
2901 }
2902
Adam Cohen0ffac432013-07-10 11:19:26 -07002903 protected String getCurrentPageDescription() {
2904 return String.format(getContext().getString(R.string.default_scroll_format),
2905 getNextPage() + 1, getChildCount());
2906 }
2907
Winson Chungd11265e2011-08-30 13:37:23 -07002908 @Override
2909 public boolean onHoverEvent(android.view.MotionEvent event) {
2910 return true;
2911 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002912}