blob: 7d65f46861650b9c976d8360bad628d0978c7af7 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070024import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
30import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070049import android.view.animation.AnimationUtils;
50import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070052import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chungc58497e2013-09-03 17:48:37 -070056interface Page {
57 public int getPageChildCount();
58 public View getChildOnPageAt(int i);
59 public void removeAllViewsOnPage();
60 public void removeViewOnPageAt(int i);
61 public int indexOfChildOnPage(View v);
62}
63
Winson Chung321e9ee2010-08-09 13:37:56 -070064/**
65 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070066 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070067 */
Michael Jurka8b805b12012-04-18 14:23:14 -070068public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070070 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070071 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Winson Chung86f77532010-08-24 11:08:22 -070073 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070074 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070075
Adam Cohen7d30a372013-07-01 17:03:59 -070076 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070077 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070078 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070079 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Adam Cohenb5ba0972011-09-07 18:02:31 -070081 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070082 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080083
Adam Cohenb64cb5a2011-02-15 13:53:42 -080084 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080085 // The page is moved more than halfway, automatically move to the next page on touch up.
86 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080087
Adam Cohen265b9a62011-12-07 14:37:18 -080088 // The following constants need to be scaled based on density. The scaled versions will be
89 // assigned to the corresponding member variables below.
90 private static final int FLING_THRESHOLD_VELOCITY = 500;
91 private static final int MIN_SNAP_VELOCITY = 1500;
92 private static final int MIN_FLING_VELOCITY = 250;
93
Adam Cohen7d30a372013-07-01 17:03:59 -070094 // We are disabling touch interaction of the widget region for factory ROM.
95 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070096 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070097 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070098
Adam Cohen21cd0022013-10-09 18:57:02 -070099 public static final int INVALID_RESTORE_PAGE = -1001;
100
Adam Cohenf358a4b2013-07-23 16:47:31 -0700101 private boolean mFreeScroll = false;
102 private int mFreeScrollMinScrollX = -1;
103 private int mFreeScrollMaxScrollX = -1;
104
Winson Chung8aad6102012-05-11 16:27:49 -0700105 static final int AUTOMATIC_PAGE_SPACING = -1;
106
Adam Cohen265b9a62011-12-07 14:37:18 -0800107 protected int mFlingThresholdVelocity;
108 protected int mMinFlingVelocity;
109 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700110
Adam Cohenb5ba0972011-09-07 18:02:31 -0700111 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected float mSmoothingTime;
113 protected float mTouchX;
114
115 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700116 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700117
118 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700119 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700120 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800123 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800124 protected LauncherScroller mScroller;
125 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700127 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 private float mParentDownMotionX;
130 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 private float mDownMotionY;
133 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700134 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800136 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800137 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800138 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700139 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700140
Adam Cohendbdff6b2013-09-18 19:09:15 -0700141 private boolean mCancelTap;
142
Adam Cohenedb40762013-07-18 16:45:45 -0700143 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 protected final static int TOUCH_STATE_REST = 0;
146 protected final static int TOUCH_STATE_SCROLLING = 1;
147 protected final static int TOUCH_STATE_PREV_PAGE = 2;
148 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 protected final static int TOUCH_STATE_REORDERING = 4;
150
Adam Cohene45440e2010-10-14 18:33:38 -0700151 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurka0142d492010-08-25 17:46:15 -0700153 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700154 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka7426c422010-11-11 15:23:47 -0800158 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 private int mPagingTouchSlop;
160 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700161 protected int mPageLayoutWidthGap;
162 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700163 protected int mCellCountX = 0;
164 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700165 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800166 protected boolean mAllowOverScroll = true;
167 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800168 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700169 protected boolean mForceDrawAllChildrenNextFrame;
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700170 private boolean mSpacePagesAutomatically = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800173 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
174 // the screens from continuing to translate beyond the normal bounds.
175 protected int mOverScrollX;
176
Michael Jurka5f1c5092010-09-03 14:15:02 -0700177 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700178
Michael Jurka5f1c5092010-09-03 14:15:02 -0700179 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Winson Chung86f77532010-08-24 11:08:22 -0700181 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Michael Jurkae326f182011-11-21 14:05:46 -0800183 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Michael Jurka0142d492010-08-25 17:46:15 -0700185 // If true, syncPages and syncPageItems will be called to refresh pages
186 protected boolean mContentIsRefreshable = true;
187
188 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700189 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700190
191 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
192 // to switch to a new page
193 protected boolean mUsePagingTouchSlop = true;
194
Michael Jurka8b805b12012-04-18 14:23:14 -0700195 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700196 // (SmoothPagedView does this)
197 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700198 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700199
Patrick Dubroy1262e362010-10-06 15:49:50 -0700200 protected boolean mIsPageMoving = false;
201
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 // All syncs and layout passes are deferred until data is ready.
203 protected boolean mIsDataReady = false;
204
Adam Cohen7d30a372013-07-01 17:03:59 -0700205 protected boolean mAllowLongPress = true;
206
Adam Cohenc2d6e892014-10-16 09:49:24 -0700207 private boolean mWasInOverscroll = false;
208
Winson Chungd2be3812013-07-16 11:11:32 -0700209 // Page Indicator
210 private int mPageIndicatorViewId;
211 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700212 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700213
Adam Cohen7d30a372013-07-01 17:03:59 -0700214 // The viewport whether the pages are to be contained (the actual view may be larger than the
215 // viewport)
216 private Rect mViewport = new Rect();
217
218 // Reordering
219 // We use the min scale to determine how much to expand the actually PagedView measured
220 // dimensions such that when we are zoomed out, the view is not clipped
221 private int REORDERING_DROP_REPOSITION_DURATION = 200;
222 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
223 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700224 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700225 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700226 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700227 protected View mDragView;
228 protected AnimatorSet mZoomInOutAnim;
229 private Runnable mSidePageHoverRunnable;
230 private int mSidePageHoverIndex = -1;
231 // This variable's scope is only for the duration of startReordering() and endReordering()
232 private boolean mReorderingStarted = false;
233 // This variable's scope is for the duration of startReordering() and after the zoomIn()
234 // animation after endReordering()
235 private boolean mIsReordering;
236 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
237 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
238 private int mPostReorderingPreZoomInRemainingAnimationCount;
239 private Runnable mPostReorderingPreZoomInRunnable;
240
Adam Cohen7d30a372013-07-01 17:03:59 -0700241 // Convenience/caching
242 private Matrix mTmpInvMatrix = new Matrix();
243 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700244 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700245 private Rect mTmpRect = new Rect();
246 private Rect mAltTmpRect = new Rect();
247
248 // Fling to delete
249 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
250 private float FLING_TO_DELETE_FRICTION = 0.035f;
251 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
252 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
253 protected int mFlingToDeleteThresholdVelocity = -1400;
254 // Drag to delete
255 private boolean mDeferringForDelete = false;
256 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
257 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
258
259 // Drop to delete
260 private View mDeleteDropTarget;
261
Adam Cohen7d30a372013-07-01 17:03:59 -0700262 // Bouncer
263 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700264
John Spurlock77e1f472013-09-11 10:09:51 -0400265 protected final Rect mInsets = new Rect();
266
Winson Chung86f77532010-08-24 11:08:22 -0700267 public interface PageSwitchListener {
268 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 }
270
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 public PagedView(Context context) {
272 this(context, null);
273 }
274
275 public PagedView(Context context, AttributeSet attrs) {
276 this(context, attrs, 0);
277 }
278
279 public PagedView(Context context, AttributeSet attrs, int defStyle) {
280 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700281
Adam Cohen9c4949e2010-10-05 12:27:22 -0700282 TypedArray a = context.obtainStyledAttributes(attrs,
283 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700284
Winson Chungdf4b83d2010-10-20 17:49:27 -0700285 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700286 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700287 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700288 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700289 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700290 a.recycle();
291
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700293 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 }
295
296 /**
297 * Initializes various states for this workspace.
298 */
Michael Jurka0142d492010-08-25 17:46:15 -0700299 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700300 mDirtyPageContent = new ArrayList<Boolean>();
301 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800302 mScroller = new LauncherScroller(getContext());
303 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700304 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700305 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700306
307 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700308 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
310 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700311 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800312
Adam Cohen7d30a372013-07-01 17:03:59 -0700313 // Scale the fling-to-delete threshold by the density
314 mFlingToDeleteThresholdVelocity =
315 (int) (mFlingToDeleteThresholdVelocity * mDensity);
316
Adam Cohen265b9a62011-12-07 14:37:18 -0800317 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
318 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
319 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700320 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 }
322
Adam Cohenf9618852013-11-08 06:45:03 -0800323 protected void setDefaultInterpolator(Interpolator interpolator) {
324 mDefaultInterpolator = interpolator;
325 mScroller.setInterpolator(mDefaultInterpolator);
326 }
327
Winson Chungd2be3812013-07-16 11:11:32 -0700328 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700329 super.onAttachedToWindow();
330
Winson Chungd2be3812013-07-16 11:11:32 -0700331 // Hook up the page indicator
332 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700333 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700334 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700335 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700336 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700337
Winson Chung7819a562013-09-19 15:55:45 -0700338 ArrayList<PageIndicator.PageMarkerResources> markers =
339 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700340 for (int i = 0; i < getChildCount(); ++i) {
341 markers.add(getPageIndicatorMarker(i));
342 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700343
Winson Chungc58497e2013-09-03 17:48:37 -0700344 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700345
346 OnClickListener listener = getPageIndicatorClickListener();
347 if (listener != null) {
348 mPageIndicator.setOnClickListener(listener);
349 }
Adam Cohen53805212013-10-01 10:39:23 -0700350 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700351 }
352 }
353
Adam Cohen53805212013-10-01 10:39:23 -0700354 protected String getPageIndicatorDescription() {
355 return getCurrentPageDescription();
356 }
357
358 protected OnClickListener getPageIndicatorClickListener() {
359 return null;
360 }
361
Winson Chungd2be3812013-07-16 11:11:32 -0700362 protected void onDetachedFromWindow() {
363 // Unhook the page indicator
364 mPageIndicator = null;
365 }
366
Adam Cohen7d30a372013-07-01 17:03:59 -0700367 void setDeleteDropTarget(View v) {
368 mDeleteDropTarget = v;
369 }
370
371 // Convenience methods to map points from self to parent and vice versa
372 float[] mapPointFromViewToParent(View v, float x, float y) {
373 mTmpPoint[0] = x;
374 mTmpPoint[1] = y;
375 v.getMatrix().mapPoints(mTmpPoint);
376 mTmpPoint[0] += v.getLeft();
377 mTmpPoint[1] += v.getTop();
378 return mTmpPoint;
379 }
380 float[] mapPointFromParentToView(View v, float x, float y) {
381 mTmpPoint[0] = x - v.getLeft();
382 mTmpPoint[1] = y - v.getTop();
383 v.getMatrix().invert(mTmpInvMatrix);
384 mTmpInvMatrix.mapPoints(mTmpPoint);
385 return mTmpPoint;
386 }
387
388 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700389 if (mDragView != null) {
390 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
391 (mDragViewBaselineLeft - mDragView.getLeft());
392 float y = mLastMotionY - mDownMotionY;
393 mDragView.setTranslationX(x);
394 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700395
Adam Cohenf358a4b2013-07-23 16:47:31 -0700396 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
397 + x + ", " + y);
398 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700399 }
400
401 public void setMinScale(float f) {
402 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700403 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700404 requestLayout();
405 }
406
407 @Override
408 public void setScaleX(float scaleX) {
409 super.setScaleX(scaleX);
410 if (isReordering(true)) {
411 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
412 mLastMotionX = p[0];
413 mLastMotionY = p[1];
414 updateDragViewTranslationDuringDrag();
415 }
416 }
417
418 // Convenience methods to get the actual width/height of the PagedView (since it is measured
419 // to be larger to account for the minimum possible scale)
420 int getViewportWidth() {
421 return mViewport.width();
422 }
423 int getViewportHeight() {
424 return mViewport.height();
425 }
426
427 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
428 // PagedView both horizontally and vertically
429 int getViewportOffsetX() {
430 return (getMeasuredWidth() - getViewportWidth()) / 2;
431 }
432
433 int getViewportOffsetY() {
434 return (getMeasuredHeight() - getViewportHeight()) / 2;
435 }
436
Adam Cohenedb40762013-07-18 16:45:45 -0700437 PageIndicator getPageIndicator() {
438 return mPageIndicator;
439 }
Winson Chung7819a562013-09-19 15:55:45 -0700440 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
441 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700442 }
Adam Cohenedb40762013-07-18 16:45:45 -0700443
Adam Cohen674531f2013-12-13 15:07:14 -0800444 /**
445 * Add a page change listener which will be called when a page is _finished_ listening.
446 *
447 */
Winson Chung86f77532010-08-24 11:08:22 -0700448 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
449 mPageSwitchListener = pageSwitchListener;
450 if (mPageSwitchListener != null) {
451 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
453 }
454
455 /**
Winson Chung52aee602013-01-30 12:01:02 -0800456 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
457 */
458 public boolean isLayoutRtl() {
459 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
460 }
461
462 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700463 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
464 * out pages.
465 */
466 protected void setDataIsReady() {
467 mIsDataReady = true;
468 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700469
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 protected boolean isDataReady() {
471 return mIsDataReady;
472 }
473
474 /**
Winson Chung86f77532010-08-24 11:08:22 -0700475 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 *
Winson Chung86f77532010-08-24 11:08:22 -0700477 * @return 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
Winson Chung360e63f2012-04-27 13:48:05 -0700483 int getNextPage() {
484 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
485 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700486
Winson Chung86f77532010-08-24 11:08:22 -0700487 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 return getChildCount();
489 }
490
Winson Chung86f77532010-08-24 11:08:22 -0700491 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 return getChildAt(index);
493 }
494
Adam Cohenae4f1552011-10-20 00:15:42 -0700495 protected int indexToPage(int index) {
496 return index;
497 }
498
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800500 * Updates the scroll of the current page immediately to its final scroll position. We use this
501 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
502 * the previous tab page.
503 */
504 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700505 // If the current page is invalid, just reset the scroll position to zero
506 int newX = 0;
507 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700508 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800510 scrollTo(newX, 0);
511 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700512 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800513 }
514
515 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700516 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700517 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
518 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700519 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700521 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800522 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700523 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700524 }
525
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700526 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700527 mScroller.abortAnimation();
528 // We need to clean up the next page here to avoid computeScrollHelper from
529 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700530 if (resetNextPage) {
531 mNextPage = INVALID_PAGE;
532 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700533 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700534
535 private void forceFinishScroller() {
536 mScroller.forceFinished(true);
537 // We need to clean up the next page here to avoid computeScrollHelper from
538 // updating current page on the pass.
539 mNextPage = INVALID_PAGE;
540 }
541
Adam Cohen6f127a62014-06-12 14:54:41 -0700542 private int validateNewPage(int newPage) {
543 int validatedPage = newPage;
544 // When in free scroll mode, we need to clamp to the free scroll page range.
545 if (mFreeScroll) {
546 getFreeScrollPageRange(mTempVisiblePagesRange);
547 validatedPage = Math.max(mTempVisiblePagesRange[0],
548 Math.min(newPage, mTempVisiblePagesRange[1]));
549 }
550 // Ensure that it is clamped by the actual set of children in all cases
551 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
552 return validatedPage;
553 }
554
Chet Haasebc2f0822012-10-26 17:59:53 -0700555 /**
Winson Chung86f77532010-08-24 11:08:22 -0700556 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 */
Winson Chung86f77532010-08-24 11:08:22 -0700558 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800559 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700560 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800561 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800562 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
563 // the default
564 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800565 return;
566 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700567 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700568 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700569 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700570 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800571 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
573
Winson Chung8c87cd82013-07-23 16:20:10 -0700574 /**
575 * The restore page will be set in place of the current page at the next (likely first)
576 * layout.
577 */
578 void setRestorePage(int restorePage) {
579 mRestorePage = restorePage;
580 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800581 int getRestorePage() {
582 return mRestorePage;
583 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700584
Adam Cohen674531f2013-12-13 15:07:14 -0800585 /**
586 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
587 * has settled.
588 */
Michael Jurka0142d492010-08-25 17:46:15 -0700589 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700590 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800591 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700592 }
Winson Chungd2be3812013-07-16 11:11:32 -0700593
Adam Cohen674531f2013-12-13 15:07:14 -0800594 updatePageIndicator();
595 }
596
597 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700598 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700599 if (mPageIndicator != null) {
600 mPageIndicator.setContentDescription(getPageIndicatorDescription());
601 if (!isReordering(false)) {
602 mPageIndicator.setActiveMarker(getNextPage());
603 }
Winson Chungd2be3812013-07-16 11:11:32 -0700604 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800606 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700607 if (!mIsPageMoving) {
608 mIsPageMoving = true;
609 onPageBeginMoving();
610 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700611 }
612
Michael Jurkace7e05f2011-02-01 22:02:35 -0800613 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700614 if (mIsPageMoving) {
615 mIsPageMoving = false;
616 onPageEndMoving();
617 }
Michael Jurka0142d492010-08-25 17:46:15 -0700618 }
619
Adam Cohen26976d92011-03-22 15:33:33 -0700620 protected boolean isPageMoving() {
621 return mIsPageMoving;
622 }
623
Michael Jurka0142d492010-08-25 17:46:15 -0700624 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700625 protected void onPageBeginMoving() {
626 }
627
628 // a method that subclasses can override to add behavior
629 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700630 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700631 }
632
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 /**
Winson Chung86f77532010-08-24 11:08:22 -0700634 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 *
636 * @param l The listener used to respond to long clicks.
637 */
638 @Override
639 public void setOnLongClickListener(OnLongClickListener l) {
640 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700641 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700643 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 }
Adam Cohen1697b792013-09-17 19:08:21 -0700645 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647
648 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800649 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700650 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800651 }
652
653 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700654 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700655 // In free scroll mode, we clamp the scrollX
656 if (mFreeScroll) {
657 x = Math.min(x, mFreeScrollMaxScrollX);
658 x = Math.max(x, mFreeScrollMinScrollX);
659 }
660
Adam Cohen0ffac432013-07-10 11:19:26 -0700661 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800662 mUnboundedScrollX = x;
663
Adam Cohen0ffac432013-07-10 11:19:26 -0700664 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
665 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
666 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800667 super.scrollTo(0, y);
668 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700669 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700670 if (isRtl) {
671 overScroll(x - mMaxScrollX);
672 } else {
673 overScroll(x);
674 }
Adam Cohen68d73932010-11-15 10:50:58 -0800675 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700676 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800677 super.scrollTo(mMaxScrollX, y);
678 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700679 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700680 if (isRtl) {
681 overScroll(x);
682 } else {
683 overScroll(x - mMaxScrollX);
684 }
Adam Cohen68d73932010-11-15 10:50:58 -0800685 }
686 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700687 if (mWasInOverscroll) {
688 overScroll(0);
689 mWasInOverscroll = false;
690 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800691 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800692 super.scrollTo(x, y);
693 }
694
Michael Jurka0142d492010-08-25 17:46:15 -0700695 mTouchX = x;
696 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700697
698 // Update the last motion events when scrolling
699 if (isReordering(true)) {
700 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
701 mLastMotionX = p[0];
702 mLastMotionY = p[1];
703 updateDragViewTranslationDuringDrag();
704 }
Michael Jurka0142d492010-08-25 17:46:15 -0700705 }
706
Adam Cohen53805212013-10-01 10:39:23 -0700707 private void sendScrollAccessibilityEvent() {
708 AccessibilityManager am =
709 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
710 if (am.isEnabled()) {
711 AccessibilityEvent ev =
712 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700713 ev.setItemCount(getChildCount());
714 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700715 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700716
Alan Viverette254139a2013-10-08 13:13:46 -0700717 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700718 if (getNextPage() >= mCurrentPage) {
719 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
720 } else {
721 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
722 }
723
724 ev.setAction(action);
725 sendAccessibilityEventUnchecked(ev);
726 }
727 }
728
Michael Jurka0142d492010-08-25 17:46:15 -0700729 // we moved this functionality to a helper function so SmoothPagedView can reuse it
730 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700731 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700732 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 if (getScrollX() != mScroller.getCurrX()
734 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700735 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700736 float scaleX = mFreeScroll ? getScaleX() : 1f;
737 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
738 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700739 }
Michael Jurka0142d492010-08-25 17:46:15 -0700740 invalidate();
741 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700742 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700743 sendScrollAccessibilityEvent();
744
Adam Cohen6f127a62014-06-12 14:54:41 -0700745 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700746 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700747 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700748
Winson Chung9c0565f2013-07-19 13:49:06 -0700749 // Load the associated pages if necessary
750 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
751 loadAssociatedPages(mCurrentPage);
752 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
753 }
754
Adam Cohen73aa9752010-11-24 16:26:58 -0800755 // We don't want to trigger a page end moving unless the page has settled
756 // and the user has stopped scrolling
757 if (mTouchState == TOUCH_STATE_REST) {
758 pageEndMoving();
759 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700760
Adam Cohen7d30a372013-07-01 17:03:59 -0700761 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700762 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700763 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700764 if (am.isEnabled()) {
765 // Notify the user when the page changes
766 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700767 }
Michael Jurka0142d492010-08-25 17:46:15 -0700768 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 }
Michael Jurka0142d492010-08-25 17:46:15 -0700770 return false;
771 }
772
773 @Override
774 public void computeScroll() {
775 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 }
777
Adam Cohen7d30a372013-07-01 17:03:59 -0700778 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
779 return mTopAlignPageWhenShrinkingForBouncer;
780 }
781
Adam Cohen96d30a12013-07-16 18:13:21 -0700782 public static class LayoutParams extends ViewGroup.LayoutParams {
783 public boolean isFullScreenPage = false;
784
785 /**
786 * {@inheritDoc}
787 */
788 public LayoutParams(int width, int height) {
789 super(width, height);
790 }
791
792 public LayoutParams(ViewGroup.LayoutParams source) {
793 super(source);
794 }
795 }
796
797 protected LayoutParams generateDefaultLayoutParams() {
798 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
799 }
800
Adam Cohenedb40762013-07-18 16:45:45 -0700801 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700802 LayoutParams lp = generateDefaultLayoutParams();
803 lp.isFullScreenPage = true;
804 super.addView(page, 0, lp);
805 }
806
Adam Cohen410f3cd2013-09-22 12:09:32 -0700807 public int getNormalChildHeight() {
808 return mNormalChildHeight;
809 }
810
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 @Override
812 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700813 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700814 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
815 return;
816 }
817
Adam Cohen7d30a372013-07-01 17:03:59 -0700818 // We measure the dimensions of the PagedView to be larger than the pages so that when we
819 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700820 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
821 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
822 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700823 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800824 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700825 // viewport, we can be at most one and a half screens offset once we scale down
826 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700827 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
828 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700829
Winson Chungc82d2622013-11-06 13:23:29 -0800830 int parentWidthSize = (int) (2f * maxSize);
831 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700832 int scaledWidthSize, scaledHeightSize;
833 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700834 scaledWidthSize = (int) (parentWidthSize / mMinScale);
835 scaledHeightSize = (int) (parentHeightSize / mMinScale);
836 } else {
837 scaledWidthSize = widthSize;
838 scaledHeightSize = heightSize;
839 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700840 mViewport.set(0, 0, widthSize, heightSize);
841
842 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
843 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
844 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 }
846
Winson Chung8aad6102012-05-11 16:27:49 -0700847 // Return early if we aren't given a proper dimension
848 if (widthSize <= 0 || heightSize <= 0) {
849 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
850 return;
851 }
852
Adam Lesinski6b879f02010-11-04 16:15:23 -0700853 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
854 * of the All apps view on XLarge displays to not take up more space then it needs. Width
855 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
856 * each page to have the same width.
857 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700858 final int verticalPadding = getPaddingTop() + getPaddingBottom();
859 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700860
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700861 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700863 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700864 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700865 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
866 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
867 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
868 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 final int childCount = getChildCount();
870 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700871 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700872 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100873 if (child.getVisibility() != GONE) {
874 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700875
Vladimir Marko2824b072013-10-04 16:42:17 +0100876 int childWidthMode;
877 int childHeightMode;
878 int childWidth;
879 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700880
Vladimir Marko2824b072013-10-04 16:42:17 +0100881 if (!lp.isFullScreenPage) {
882 if (lp.width == LayoutParams.WRAP_CONTENT) {
883 childWidthMode = MeasureSpec.AT_MOST;
884 } else {
885 childWidthMode = MeasureSpec.EXACTLY;
886 }
887
888 if (lp.height == LayoutParams.WRAP_CONTENT) {
889 childHeightMode = MeasureSpec.AT_MOST;
890 } else {
891 childHeightMode = MeasureSpec.EXACTLY;
892 }
893
Adam Cohen3b185e22013-10-29 14:45:58 -0700894 childWidth = getViewportWidth() - horizontalPadding
895 - mInsets.left - mInsets.right;
896 childHeight = getViewportHeight() - verticalPadding
897 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100898 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700899 } else {
900 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700901 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100902
Adam Cohen3b185e22013-10-29 14:45:58 -0700903 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
904 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700905 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700906 if (referenceChildWidth == 0) {
907 referenceChildWidth = childWidth;
908 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700909
Vladimir Marko2824b072013-10-04 16:42:17 +0100910 final int childWidthMeasureSpec =
911 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
912 final int childHeightMeasureSpec =
913 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
914 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700915 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700916 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700917 if (mSpacePagesAutomatically) {
918 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
919 - referenceChildWidth) / 2;
920 if (spacing >= 0) {
921 setPageSpacing(spacing);
922 }
923 mSpacePagesAutomatically = false;
924 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700925 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800926 }
927
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700928 /**
929 * This method should be called once before first layout / measure pass.
930 */
931 protected void setSinglePageInViewport() {
932 mSpacePagesAutomatically = true;
933 }
934
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700936 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700938 return;
939 }
940
Winson Chung785d2eb2011-04-14 16:08:02 -0700941 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700943
Adam Cohen7d30a372013-07-01 17:03:59 -0700944 int offsetX = getViewportOffsetX();
945 int offsetY = getViewportOffsetY();
946
947 // Update the viewport offsets
948 mViewport.offset(offsetX, offsetY);
949
Adam Cohen0ffac432013-07-10 11:19:26 -0700950 final boolean isRtl = isLayoutRtl();
951
952 final int startIndex = isRtl ? childCount - 1 : 0;
953 final int endIndex = isRtl ? -1 : childCount;
954 final int delta = isRtl ? -1 : 1;
955
Adam Cohen7d30a372013-07-01 17:03:59 -0700956 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700957
Adam Cohen3b185e22013-10-29 14:45:58 -0700958 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000959 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700960
961 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700962 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
963 mPageScrolls = new int[getChildCount()];
964 }
965
Adam Cohen0ffac432013-07-10 11:19:26 -0700966 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700967 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700969 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100970 int childTop;
971 if (lp.isFullScreenPage) {
972 childTop = offsetY;
973 } else {
974 childTop = offsetY + getPaddingTop() + mInsets.top;
975 if (mCenterPagesVertically) {
976 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
977 }
978 }
979
Adam Cohen96d30a12013-07-16 18:13:21 -0700980 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700981 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800982
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700984 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800985 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700986
Adam Cohen3b185e22013-10-29 14:45:58 -0700987 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
988 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000989
990 int pageGap = mPageSpacing;
991 int next = i + delta;
992 if (next != endIndex) {
993 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
994 } else {
995 nextLp = null;
996 }
997
998 // Prevent full screen pages from showing in the viewport
999 // when they are not the current page.
1000 if (lp.isFullScreenPage) {
1001 pageGap = getPaddingLeft();
1002 } else if (nextLp != null && nextLp.isFullScreenPage) {
1003 pageGap = getPaddingRight();
1004 }
1005
1006 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 }
1008 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001009
1010 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001011 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001012 mFirstLayout = false;
1013 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001014
1015 if (childCount > 0) {
1016 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -07001017 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -07001018 } else {
1019 mMaxScrollX = 0;
1020 }
1021
1022 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1023 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001024 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001025 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001026 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001027 } else {
1028 setCurrentPage(getNextPage());
1029 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001030 }
1031 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001032
1033 if (isReordering(true)) {
1034 updateDragViewTranslationDuringDrag();
1035 }
Winson Chunge3193b92010-09-10 11:44:42 -07001036 }
1037
Adam Cohen3b185e22013-10-29 14:45:58 -07001038 public void setPageSpacing(int pageSpacing) {
1039 mPageSpacing = pageSpacing;
1040 requestLayout();
1041 }
1042
Adam Cohenf34bab52010-09-30 14:11:56 -07001043 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001044 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1045
1046 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001047 for (int i = 0; i < getChildCount(); i++) {
1048 View child = getChildAt(i);
1049 if (child != null) {
1050 float scrollProgress = getScrollProgress(screenCenter, child, i);
1051 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001052 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001053 }
1054 }
1055 invalidate();
1056 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001057 }
1058
Winson Chungc58497e2013-09-03 17:48:37 -07001059 protected void enablePagedViewAnimations() {
1060 mAllowPagedViewAnimations = true;
1061
1062 }
1063 protected void disablePagedViewAnimations() {
1064 mAllowPagedViewAnimations = false;
1065 }
1066
Winson Chunge3193b92010-09-10 11:44:42 -07001067 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001068 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001069 // Update the page indicator, we don't update the page indicator as we
1070 // add/remove pages
1071 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001072 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001073 mPageIndicator.addMarker(pageIndex,
1074 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001075 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001076 }
1077
Adam Cohen2591f6a2011-10-25 14:36:40 -07001078 // This ensures that when children are added, they get the correct transforms / alphas
1079 // in accordance with any scroll effects.
1080 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001081 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001082 invalidate();
1083 }
1084
Michael Jurka8b805b12012-04-18 14:23:14 -07001085 @Override
1086 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001087 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001088 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001089 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001090 }
1091
Winson Chungd2be3812013-07-16 11:11:32 -07001092 private void removeMarkerForView(int index) {
1093 // Update the page indicator, we don't update the page indicator as we
1094 // add/remove pages
1095 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001096 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001097 }
1098 }
1099
1100 @Override
1101 public void removeView(View v) {
1102 // XXX: We should find a better way to hook into this before the view
1103 // gets removed form its parent...
1104 removeMarkerForView(indexOfChild(v));
1105 super.removeView(v);
1106 }
1107 @Override
1108 public void removeViewInLayout(View v) {
1109 // XXX: We should find a better way to hook into this before the view
1110 // gets removed form its parent...
1111 removeMarkerForView(indexOfChild(v));
1112 super.removeViewInLayout(v);
1113 }
1114 @Override
1115 public void removeViewAt(int index) {
1116 // XXX: We should find a better way to hook into this before the view
1117 // gets removed form its parent...
1118 removeViewAt(index);
1119 super.removeViewAt(index);
1120 }
1121 @Override
1122 public void removeAllViewsInLayout() {
1123 // Update the page indicator, we don't update the page indicator as we
1124 // add/remove pages
1125 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001126 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001127 }
1128
1129 super.removeAllViewsInLayout();
1130 }
1131
Adam Cohen73894962011-10-31 13:17:17 -07001132 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001133 if (index < 0 || index > getChildCount() - 1) return 0;
1134
Adam Cohenf698c6e2013-07-17 18:44:25 -07001135 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001136
Adam Cohenf698c6e2013-07-17 18:44:25 -07001137 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001138 }
1139
Adam Cohen6f127a62014-06-12 14:54:41 -07001140 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001141 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001142 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001143 }
1144
Michael Jurkadde558b2011-11-09 22:09:06 -08001145 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001146 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001147 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001148
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001149 range[0] = -1;
1150 range[1] = -1;
1151
Michael Jurkac4fb9172010-09-02 17:19:20 -07001152 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001153 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001154 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001155
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001156 int count = getChildCount();
1157 for (int i = 0; i < count; i++) {
1158 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001159
Winson Chungc9ca2982013-07-19 12:07:38 -07001160 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001161 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001162 if (mTmpIntPoint[0] > viewportWidth) {
1163 if (range[0] == -1) {
1164 continue;
1165 } else {
1166 break;
1167 }
1168 }
1169
Adam Cohen6a678da2013-09-24 10:58:01 -07001170 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001171 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1172 if (mTmpIntPoint[0] < 0) {
1173 if (range[0] == -1) {
1174 continue;
1175 } else {
1176 break;
1177 }
1178 }
1179 curScreen = i;
1180 if (range[0] < 0) {
1181 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001182 }
1183 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001184
1185 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001186 } else {
1187 range[0] = -1;
1188 range[1] = -1;
1189 }
1190 }
1191
Michael Jurka920d7f42012-05-14 16:29:55 -07001192 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001193 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001194 }
1195
Michael Jurkadde558b2011-11-09 22:09:06 -08001196 @Override
1197 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001198 // Find out which screens are visible; as an optimization we only call draw on them
1199 final int pageCount = getChildCount();
1200 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001201 int halfScreenSize = getViewportWidth() / 2;
1202 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1203 // Otherwise it is equal to the scaled overscroll position.
1204 int screenCenter = mOverScrollX + halfScreenSize;
1205
1206 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1207 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1208 // set it for the next frame
1209 mForceScreenScrolled = false;
1210 screenScrolled(screenCenter);
1211 mLastScreenCenter = screenCenter;
1212 }
1213
Michael Jurkadde558b2011-11-09 22:09:06 -08001214 getVisiblePages(mTempVisiblePagesRange);
1215 final int leftScreen = mTempVisiblePagesRange[0];
1216 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001217 if (leftScreen != -1 && rightScreen != -1) {
1218 final long drawingTime = getDrawingTime();
1219 // Clip to the bounds
1220 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001221 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1222 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001223
Adam Cohen7d30a372013-07-01 17:03:59 -07001224 // Draw all the children, leaving the drag view for last
1225 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001226 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001227 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001228 if (mForceDrawAllChildrenNextFrame ||
1229 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001230 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001231 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001232 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001233 // Draw the drag view on top (if there is one)
1234 if (mDragView != null) {
1235 drawChild(canvas, mDragView, drawingTime);
1236 }
1237
Michael Jurka5e368ff2012-05-14 23:13:15 -07001238 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001239 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001240 }
Michael Jurka0142d492010-08-25 17:46:15 -07001241 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 }
1243
1244 @Override
1245 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001246 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001247 if (page != mCurrentPage || !mScroller.isFinished()) {
1248 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 return true;
1250 }
1251 return false;
1252 }
1253
1254 @Override
1255 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001256 int focusablePage;
1257 if (mNextPage != INVALID_PAGE) {
1258 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001260 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 }
Winson Chung86f77532010-08-24 11:08:22 -07001262 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001264 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266 return false;
1267 }
1268
1269 @Override
1270 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001271 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001273 if (getCurrentPage() > 0) {
1274 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 return true;
1276 }
1277 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001278 if (getCurrentPage() < getPageCount() - 1) {
1279 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 return true;
1281 }
1282 }
1283 return super.dispatchUnhandledMove(focused, direction);
1284 }
1285
1286 @Override
1287 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001288 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001289 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001290 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
1292 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001293 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001294 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 }
1296 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001297 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001298 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 }
1300 }
1301 }
1302
1303 /**
1304 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001305 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 *
Winson Chung86f77532010-08-24 11:08:22 -07001307 * This happens when live folders requery, and if they're off page, they
1308 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 */
1310 @Override
1311 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001312 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001313 View v = focused;
1314 while (true) {
1315 if (v == current) {
1316 super.focusableViewAvailable(focused);
1317 return;
1318 }
1319 if (v == this) {
1320 return;
1321 }
1322 ViewParent parent = v.getParent();
1323 if (parent instanceof View) {
1324 v = (View)v.getParent();
1325 } else {
1326 return;
1327 }
1328 }
1329 }
1330
1331 /**
1332 * {@inheritDoc}
1333 */
1334 @Override
1335 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1336 if (disallowIntercept) {
1337 // We need to make sure to cancel our long press if
1338 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001339 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001340 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 }
1342 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1343 }
1344
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001345 /**
1346 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1347 */
1348 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001349 if (isLayoutRtl()) {
1350 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001351 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001352 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001353 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001354 }
1355
1356 /**
1357 * Return true if a tap at (x, y) should trigger a flip to the next page.
1358 */
1359 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001360 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001361 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001362 }
1363 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001364 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001365 }
Winson Chung52aee602013-01-30 12:01:02 -08001366
Adam Cohen7d30a372013-07-01 17:03:59 -07001367 /** Returns whether x and y originated within the buffered viewport */
1368 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1369 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1370 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1371 return mTmpRect.contains(x, y);
1372 }
1373
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 @Override
1375 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001376 if (DISABLE_TOUCH_INTERACTION) {
1377 return false;
1378 }
1379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 /*
1381 * This method JUST determines whether we want to intercept the motion.
1382 * If we return true, onTouchEvent will be called and we do the actual
1383 * scrolling there.
1384 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001385 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001386
Winson Chung45e1d6e2010-11-09 17:19:49 -08001387 // Skip touch handling if there are no pages to swipe
1388 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1389
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 /*
1391 * Shortcut the most recurring case: the user is in the dragging
1392 * state and he is moving his finger. We want to intercept this
1393 * motion.
1394 */
1395 final int action = ev.getAction();
1396 if ((action == MotionEvent.ACTION_MOVE) &&
1397 (mTouchState == TOUCH_STATE_SCROLLING)) {
1398 return true;
1399 }
1400
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 switch (action & MotionEvent.ACTION_MASK) {
1402 case MotionEvent.ACTION_MOVE: {
1403 /*
1404 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1405 * whether the user has moved far enough from his original down touch.
1406 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001407 if (mActivePointerId != INVALID_POINTER) {
1408 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001409 }
1410 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1411 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1412 // i.e. fall through to the next case (don't break)
1413 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1414 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001415 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 }
1417
1418 case MotionEvent.ACTION_DOWN: {
1419 final float x = ev.getX();
1420 final float y = ev.getY();
1421 // Remember location of down touch
1422 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001423 mDownMotionY = y;
1424 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 mLastMotionX = x;
1426 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001427 float[] p = mapPointFromViewToParent(this, x, y);
1428 mParentDownMotionX = p[0];
1429 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001430 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001431 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001433
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 /*
1435 * If being flinged and user touches the screen, initiate drag;
1436 * otherwise don't. mScroller.isFinished should be false when
1437 * being flinged.
1438 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001439 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001440 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001441
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001442 if (finishedScrolling) {
1443 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001444 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001445 setCurrentPage(getNextPage());
1446 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001447 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001448 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001449 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1450 mTouchState = TOUCH_STATE_SCROLLING;
1451 } else {
1452 mTouchState = TOUCH_STATE_REST;
1453 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001454 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001455
Winson Chung86f77532010-08-24 11:08:22 -07001456 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 if (!DISABLE_TOUCH_SIDE_PAGES) {
1459 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1460 if (getChildCount() > 0) {
1461 if (hitsPreviousPage(x, y)) {
1462 mTouchState = TOUCH_STATE_PREV_PAGE;
1463 } else if (hitsNextPage(x, y)) {
1464 mTouchState = TOUCH_STATE_NEXT_PAGE;
1465 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 }
1467 }
1468 }
1469 break;
1470 }
1471
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001473 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001474 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 break;
1476
1477 case MotionEvent.ACTION_POINTER_UP:
1478 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001479 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 break;
1481 }
1482
1483 /*
1484 * The only time we want to intercept motion events is if we are in the
1485 * drag mode.
1486 */
1487 return mTouchState != TOUCH_STATE_REST;
1488 }
1489
Adam Cohenf8d28232011-02-01 21:47:00 -08001490 protected void determineScrollingStart(MotionEvent ev) {
1491 determineScrollingStart(ev, 1.0f);
1492 }
1493
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 /*
1495 * Determines if we should change the touch state to start scrolling after the
1496 * user moves their touch point too far.
1497 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001498 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001499 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001501 if (pointerIndex == -1) return;
1502
1503 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 final float x = ev.getX(pointerIndex);
1505 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001506 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1507
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 final int xDiff = (int) Math.abs(x - mLastMotionX);
1509 final int yDiff = (int) Math.abs(y - mLastMotionY);
1510
Adam Cohenf8d28232011-02-01 21:47:00 -08001511 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 boolean xPaged = xDiff > mPagingTouchSlop;
1513 boolean xMoved = xDiff > touchSlop;
1514 boolean yMoved = yDiff > touchSlop;
1515
Adam Cohenf8d28232011-02-01 21:47:00 -08001516 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001517 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 // Scroll if the user moved far enough along the X axis
1519 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001520 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001522 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001523 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001524 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001525 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001526 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001528 }
1529 }
1530
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 protected float getMaxScrollProgress() {
1532 return 1.0f;
1533 }
1534
Adam Cohenf8d28232011-02-01 21:47:00 -08001535 protected void cancelCurrentPageLongPress() {
1536 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001537 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001538 // Try canceling the long press. It could also have been scheduled
1539 // by a distant descendant, so use the mAllowLongPress flag to block
1540 // everything
1541 final View currentPage = getPageAt(mCurrentPage);
1542 if (currentPage != null) {
1543 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 }
1545 }
1546 }
1547
Adam Cohen7d30a372013-07-01 17:03:59 -07001548 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1549 final int halfScreenSize = getViewportWidth() / 2;
1550
1551 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1552 screenCenter = Math.max(halfScreenSize, screenCenter);
1553
1554 return getScrollProgress(screenCenter, v, page);
1555 }
1556
Adam Cohenb5ba0972011-09-07 18:02:31 -07001557 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001558 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001559
Adam Cohenedb40762013-07-18 16:45:45 -07001560 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001561 int count = getChildCount();
1562
1563 final int totalDistance;
1564
1565 int adjacentPage = page + 1;
1566 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1567 adjacentPage = page - 1;
1568 }
1569
1570 if (adjacentPage < 0 || adjacentPage > count - 1) {
1571 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1572 } else {
1573 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1574 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001575
1576 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001577 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1578 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001579 return scrollProgress;
1580 }
1581
Adam Cohenedb40762013-07-18 16:45:45 -07001582 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001583 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001584 return 0;
1585 } else {
1586 return mPageScrolls[index];
1587 }
1588 }
1589
Adam Cohen564a2e72013-10-09 14:47:32 -07001590 // While layout transitions are occurring, a child's position may stray from its baseline
1591 // position. This method returns the magnitude of this stray at any given time.
1592 public int getLayoutTransitionOffsetForPage(int index) {
1593 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1594 return 0;
1595 } else {
1596 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001597
1598 int scrollOffset = 0;
1599 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1600 if (!lp.isFullScreenPage) {
1601 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1602 }
1603
Adam Cohen564a2e72013-10-09 14:47:32 -07001604 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1605 return (int) (child.getX() - baselineX);
1606 }
1607 }
1608
Adam Cohene0f66b52010-11-23 15:06:07 -08001609 // This curve determines how the effect of scrolling over the limits of the page dimishes
1610 // as the user pulls further and further from the bounds
1611 private float overScrollInfluenceCurve(float f) {
1612 f -= 1.0f;
1613 return f * f * f + 1.0f;
1614 }
1615
Adam Cohen1e4359c2014-08-18 13:12:16 -07001616 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001617 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001618
1619 // We want to reach the max over scroll effect when the user has
1620 // over scrolled half the size of the screen
1621 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1622
Adam Cohen1e4359c2014-08-18 13:12:16 -07001623 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001624
1625 // Clamp this factor, f, to -1 < f < 1
1626 if (Math.abs(f) >= 1) {
1627 f /= Math.abs(f);
1628 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001629 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001630 }
1631
1632 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001633 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001634
1635 float f = (amount / screenSize);
1636
1637 if (f == 0) return;
1638 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1639
Adam Cohen7bfc9792011-01-28 13:52:37 -08001640 // Clamp this factor, f, to -1 < f < 1
1641 if (Math.abs(f) >= 1) {
1642 f /= Math.abs(f);
1643 }
1644
Adam Cohene0f66b52010-11-23 15:06:07 -08001645 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001646 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001647 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001648 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001649 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001650 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001651 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001652 }
1653 invalidate();
1654 }
1655
Adam Cohenb5ba0972011-09-07 18:02:31 -07001656 protected void overScroll(float amount) {
1657 dampedOverScroll(amount);
1658 }
1659
Michael Jurkac5b262c2011-01-12 20:24:50 -08001660 protected float maxOverScroll() {
1661 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001662 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001663 float f = 1.0f;
1664 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1665 return OVERSCROLL_DAMP_FACTOR * f;
1666 }
1667
Adam Cohenf358a4b2013-07-23 16:47:31 -07001668 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001669 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001670 }
1671
Adam Cohenf9618852013-11-08 06:45:03 -08001672 protected void disableFreeScroll() {
1673 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001674 }
1675
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001676 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001677 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001678 if (isLayoutRtl()) {
1679 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1680 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1681 } else {
1682 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1683 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1684 }
1685 }
1686
Adam Cohenf9618852013-11-08 06:45:03 -08001687 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001688 mFreeScroll = freeScroll;
1689
Adam Cohenf9618852013-11-08 06:45:03 -08001690 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001691 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001692 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001693 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1694 setCurrentPage(mTempVisiblePagesRange[0]);
1695 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1696 setCurrentPage(mTempVisiblePagesRange[1]);
1697 }
1698 }
1699
1700 setEnableOverscroll(!freeScroll);
1701 }
1702
1703 private void setEnableOverscroll(boolean enable) {
1704 mAllowOverScroll = enable;
1705 }
1706
1707 int getNearestHoverOverPageIndex() {
1708 if (mDragView != null) {
1709 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1710 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001711 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001712 int minDistance = Integer.MAX_VALUE;
1713 int minIndex = indexOfChild(mDragView);
1714 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1715 View page = getPageAt(i);
1716 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1717 int d = Math.abs(dragX - pageX);
1718 if (d < minDistance) {
1719 minIndex = i;
1720 minDistance = d;
1721 }
1722 }
1723 return minIndex;
1724 }
1725 return -1;
1726 }
1727
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 @Override
1729 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001730 if (DISABLE_TOUCH_INTERACTION) {
1731 return false;
1732 }
1733
Adam Cohen1697b792013-09-17 19:08:21 -07001734 super.onTouchEvent(ev);
1735
Winson Chung45e1d6e2010-11-09 17:19:49 -08001736 // Skip touch handling if there are no pages to swipe
1737 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1738
Michael Jurkab8f06722010-10-10 15:58:46 -07001739 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001740
1741 final int action = ev.getAction();
1742
1743 switch (action & MotionEvent.ACTION_MASK) {
1744 case MotionEvent.ACTION_DOWN:
1745 /*
1746 * If being flinged and user touches, stop the fling. isFinished
1747 * will be false if being flinged.
1748 */
1749 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001750 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 }
1752
1753 // Remember where the motion event started
1754 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001755 mDownMotionY = mLastMotionY = ev.getY();
1756 mDownScrollX = getScrollX();
1757 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1758 mParentDownMotionX = p[0];
1759 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001760 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001761 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001762 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001763
Michael Jurka0142d492010-08-25 17:46:15 -07001764 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001765 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001766 pageBeginMoving();
1767 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001768 break;
1769
1770 case MotionEvent.ACTION_MOVE:
1771 if (mTouchState == TOUCH_STATE_SCROLLING) {
1772 // Scroll to follow the motion event
1773 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001774
1775 if (pointerIndex == -1) return true;
1776
Winson Chung321e9ee2010-08-09 13:37:56 -07001777 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001778 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001779
Adam Cohenaefd4e12011-02-14 16:39:38 -08001780 mTotalMotionX += Math.abs(deltaX);
1781
Winson Chungc0844aa2011-02-02 15:25:58 -08001782 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1783 // keep the remainder because we are actually testing if we've moved from the last
1784 // scrolled position (which is discrete).
1785 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001786 mTouchX += deltaX;
1787 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1788 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001789 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001790 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001791 } else {
1792 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001794 mLastMotionX = x;
1795 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001796 } else {
1797 awakenScrollBars();
1798 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001799 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1800 // Update the last motion position
1801 mLastMotionX = ev.getX();
1802 mLastMotionY = ev.getY();
1803
1804 // Update the parent down so that our zoom animations take this new movement into
1805 // account
1806 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1807 mParentDownMotionX = pt[0];
1808 mParentDownMotionY = pt[1];
1809 updateDragViewTranslationDuringDrag();
1810
1811 // Find the closest page to the touch point
1812 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001813
1814 // Change the drag view if we are hovering over the drop target
1815 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1816 (int) mParentDownMotionX, (int) mParentDownMotionY);
1817 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1818
Adam Cohen7d30a372013-07-01 17:03:59 -07001819 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1820 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1821 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1822 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1823
Adam Cohenf358a4b2013-07-23 16:47:31 -07001824 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1825 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1826 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001827 mTempVisiblePagesRange[0] = 0;
1828 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001829 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001830 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1831 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1832 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1833 mSidePageHoverIndex = pageUnderPointIndex;
1834 mSidePageHoverRunnable = new Runnable() {
1835 @Override
1836 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001837 // Setup the scroll to the correct page before we swap the views
1838 snapToPage(pageUnderPointIndex);
1839
1840 // For each of the pages between the paged view and the drag view,
1841 // animate them from the previous position to the new position in
1842 // the layout (as a result of the drag view moving in the layout)
1843 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1844 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1845 dragViewIndex + 1 : pageUnderPointIndex;
1846 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1847 dragViewIndex - 1 : pageUnderPointIndex;
1848 for (int i = lowerIndex; i <= upperIndex; ++i) {
1849 View v = getChildAt(i);
1850 // dragViewIndex < pageUnderPointIndex, so after we remove the
1851 // drag view all subsequent views to pageUnderPointIndex will
1852 // shift down.
1853 int oldX = getViewportOffsetX() + getChildOffset(i);
1854 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1855
1856 // Animate the view translation from its old position to its new
1857 // position
1858 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1859 if (anim != null) {
1860 anim.cancel();
1861 }
1862
1863 v.setTranslationX(oldX - newX);
1864 anim = new AnimatorSet();
1865 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1866 anim.playTogether(
1867 ObjectAnimator.ofFloat(v, "translationX", 0f));
1868 anim.start();
1869 v.setTag(anim);
1870 }
1871
1872 removeView(mDragView);
1873 onRemoveView(mDragView, false);
1874 addView(mDragView, pageUnderPointIndex);
1875 onAddView(mDragView, pageUnderPointIndex);
1876 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001877 if (mPageIndicator != null) {
1878 mPageIndicator.setActiveMarker(getNextPage());
1879 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001880 }
1881 };
1882 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1883 }
1884 } else {
1885 removeCallbacks(mSidePageHoverRunnable);
1886 mSidePageHoverIndex = -1;
1887 }
Adam Cohen564976a2010-10-13 18:52:07 -07001888 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001889 determineScrollingStart(ev);
1890 }
1891 break;
1892
1893 case MotionEvent.ACTION_UP:
1894 if (mTouchState == TOUCH_STATE_SCROLLING) {
1895 final int activePointerId = mActivePointerId;
1896 final int pointerIndex = ev.findPointerIndex(activePointerId);
1897 final float x = ev.getX(pointerIndex);
1898 final VelocityTracker velocityTracker = mVelocityTracker;
1899 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1900 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001901 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001902 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001903 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1904 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001905
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001906 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1907
Adam Cohen00481b32011-11-18 12:03:48 -08001908 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001909 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001910
Adam Cohenf358a4b2013-07-23 16:47:31 -07001911 if (!mFreeScroll) {
1912 // In the case that the page is moved far to one direction and then is flung
1913 // in the opposite direction, we use a threshold to determine whether we should
1914 // just return to the starting page, or if we should skip one further.
1915 boolean returnToOriginalPage = false;
1916 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1917 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1918 returnToOriginalPage = true;
1919 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001920
Adam Cohenf358a4b2013-07-23 16:47:31 -07001921 int finalPage;
1922 // We give flings precedence over large moves, which is why we short-circuit our
1923 // test for a large move if a fling has been registered. That is, a large
1924 // move to the left and fling to the right will register as a fling to the right.
1925 final boolean isRtl = isLayoutRtl();
1926 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1927 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1928 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1929 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1930 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1931 snapToPageWithVelocity(finalPage, velocityX);
1932 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1933 (isFling && isVelocityXLeft)) &&
1934 mCurrentPage < getChildCount() - 1) {
1935 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1936 snapToPageWithVelocity(finalPage, velocityX);
1937 } else {
1938 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001939 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001940 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001941 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001942 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001943 }
1944
1945 float scaleX = getScaleX();
1946 int vX = (int) (-velocityX * scaleX);
1947 int initialScrollX = (int) (getScrollX() * scaleX);
1948
Adam Cohenf9618852013-11-08 06:45:03 -08001949 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001950 mScroller.fling(initialScrollX,
1951 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1952 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001954 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001955 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1956 // at this point we have not moved beyond the touch slop
1957 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1958 // we can just page
1959 int nextPage = Math.max(0, mCurrentPage - 1);
1960 if (nextPage != mCurrentPage) {
1961 snapToPage(nextPage);
1962 } else {
1963 snapToDestination();
1964 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001965 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001966 // at this point we have not moved beyond the touch slop
1967 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1968 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001969 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1970 if (nextPage != mCurrentPage) {
1971 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001972 } else {
1973 snapToDestination();
1974 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001975 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1976 // Update the last motion position
1977 mLastMotionX = ev.getX();
1978 mLastMotionY = ev.getY();
1979
1980 // Update the parent down so that our zoom animations take this new movement into
1981 // account
1982 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1983 mParentDownMotionX = pt[0];
1984 mParentDownMotionY = pt[1];
1985 updateDragViewTranslationDuringDrag();
1986 boolean handledFling = false;
1987 if (!DISABLE_FLING_TO_DELETE) {
1988 // Check the velocity and see if we are flinging-to-delete
1989 PointF flingToDeleteVector = isFlingingToDelete();
1990 if (flingToDeleteVector != null) {
1991 onFlingToDelete(flingToDeleteVector);
1992 handledFling = true;
1993 }
1994 }
1995 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1996 (int) mParentDownMotionY)) {
1997 onDropToDelete();
1998 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001999 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002000 if (!mCancelTap) {
2001 onUnhandledTap(ev);
2002 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002004
2005 // Remove the callback to wait for the side page hover timeout
2006 removeCallbacks(mSidePageHoverRunnable);
2007 // End any intermediate reordering states
2008 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002009 break;
2010
2011 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002012 if (mTouchState == TOUCH_STATE_SCROLLING) {
2013 snapToDestination();
2014 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002016 break;
2017
2018 case MotionEvent.ACTION_POINTER_UP:
2019 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002020 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002021 break;
2022 }
2023
2024 return true;
2025 }
2026
Adam Cohen7d30a372013-07-01 17:03:59 -07002027 public void onFlingToDelete(View v) {}
2028 public void onRemoveView(View v, boolean deletePermanently) {}
2029 public void onRemoveViewAnimationCompleted() {}
2030 public void onAddView(View v, int index) {}
2031
2032 private void resetTouchState() {
2033 releaseVelocityTracker();
2034 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002035 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002036 mTouchState = TOUCH_STATE_REST;
2037 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 }
2039
Adam Cohenc2d6e892014-10-16 09:49:24 -07002040 /**
2041 * Triggered by scrolling via touch
2042 */
2043 protected void onScrollInteractionBegin() {
2044 }
2045
2046 protected void onScrollInteractionEnd() {
2047 }
2048
Adam Cohen1697b792013-09-17 19:08:21 -07002049 protected void onUnhandledTap(MotionEvent ev) {
2050 ((Launcher) getContext()).onClick(this);
2051 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002052
Winson Chung185d7162011-02-28 13:47:29 -08002053 @Override
2054 public boolean onGenericMotionEvent(MotionEvent event) {
2055 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2056 switch (event.getAction()) {
2057 case MotionEvent.ACTION_SCROLL: {
2058 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2059 final float vscroll;
2060 final float hscroll;
2061 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2062 vscroll = 0;
2063 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2064 } else {
2065 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2066 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2067 }
2068 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002069 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2070 : (hscroll > 0 || vscroll > 0);
2071 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002072 scrollRight();
2073 } else {
2074 scrollLeft();
2075 }
2076 return true;
2077 }
2078 }
2079 }
2080 }
2081 return super.onGenericMotionEvent(event);
2082 }
2083
Michael Jurkab8f06722010-10-10 15:58:46 -07002084 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2085 if (mVelocityTracker == null) {
2086 mVelocityTracker = VelocityTracker.obtain();
2087 }
2088 mVelocityTracker.addMovement(ev);
2089 }
2090
2091 private void releaseVelocityTracker() {
2092 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002093 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002094 mVelocityTracker.recycle();
2095 mVelocityTracker = null;
2096 }
2097 }
2098
Winson Chung321e9ee2010-08-09 13:37:56 -07002099 private void onSecondaryPointerUp(MotionEvent ev) {
2100 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2101 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2102 final int pointerId = ev.getPointerId(pointerIndex);
2103 if (pointerId == mActivePointerId) {
2104 // This was our active pointer going up. Choose a new
2105 // active pointer and adjust accordingly.
2106 // TODO: Make this decision more intelligent.
2107 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2108 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2109 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002110 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002111 mActivePointerId = ev.getPointerId(newPointerIndex);
2112 if (mVelocityTracker != null) {
2113 mVelocityTracker.clear();
2114 }
2115 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002116 }
2117
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 @Override
2119 public void requestChildFocus(View child, View focused) {
2120 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002121 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002122 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002123 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 }
2125 }
2126
Winson Chung1908d072011-02-24 18:09:44 -08002127 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002128 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002129 }
2130
Adam Cohen7d30a372013-07-01 17:03:59 -07002131 int getPageNearestToPoint(float x) {
2132 int index = 0;
2133 for (int i = 0; i < getChildCount(); ++i) {
2134 if (x < getChildAt(i).getRight() - getScrollX()) {
2135 return index;
2136 } else {
2137 index++;
2138 }
2139 }
2140 return Math.min(index, getChildCount() - 1);
2141 }
2142
Adam Cohend19d3ca2010-09-15 14:43:42 -07002143 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002144 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002145 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002146 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002147 final int childCount = getChildCount();
2148 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002149 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002150 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002151 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002152 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002153 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2154 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2155 minDistanceFromScreenCenter = distanceFromScreenCenter;
2156 minDistanceFromScreenCenterIndex = i;
2157 }
2158 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002159 return minDistanceFromScreenCenterIndex;
2160 }
2161
Adam Cohen1e4359c2014-08-18 13:12:16 -07002162 protected boolean isInOverScroll() {
2163 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2164 }
2165
2166 protected int getPageSnapDuration() {
2167 if (isInOverScroll()) {
2168 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2169 }
2170 return PAGE_SNAP_ANIMATION_DURATION;
2171
2172 }
2173
Adam Cohend19d3ca2010-09-15 14:43:42 -07002174 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002175 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002176 }
2177
Adam Cohene0f66b52010-11-23 15:06:07 -08002178 private static class ScrollInterpolator implements Interpolator {
2179 public ScrollInterpolator() {
2180 }
2181
2182 public float getInterpolation(float t) {
2183 t -= 1.0f;
2184 return t*t*t*t*t + 1;
2185 }
2186 }
2187
2188 // We want the duration of the page snap animation to be influenced by the distance that
2189 // the screen has to travel, however, we don't want this duration to be effected in a
2190 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2191 // of travel has on the overall snap duration.
2192 float distanceInfluenceForSnapDuration(float f) {
2193 f -= 0.5f; // center the values about 0.
2194 f *= 0.3f * Math.PI / 2.0f;
2195 return (float) Math.sin(f);
2196 }
2197
Michael Jurka0142d492010-08-25 17:46:15 -07002198 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002199 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002200 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002201
Adam Cohenedb40762013-07-18 16:45:45 -07002202 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002203 int delta = newX - mUnboundedScrollX;
2204 int duration = 0;
2205
Adam Cohen1e4359c2014-08-18 13:12:16 -07002206 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002207 // If the velocity is low enough, then treat this more as an automatic page advance
2208 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002209 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002210 return;
2211 }
2212
2213 // Here we compute a "distance" that will be used in the computation of the overall
2214 // snap duration. This is a function of the actual distance that needs to be traveled;
2215 // we keep this value close to half screen size in order to reduce the variance in snap
2216 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002217 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002218 float distance = halfScreenSize + halfScreenSize *
2219 distanceInfluenceForSnapDuration(distanceRatio);
2220
2221 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002222 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002223
2224 // we want the page's snap velocity to approximately match the velocity at which the
2225 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002226 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2227 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002228
2229 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002230 }
2231
2232 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002233 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002234 }
2235
Adam Cohen7d30a372013-07-01 17:03:59 -07002236 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002237 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002238 }
2239
Michael Jurka0142d492010-08-25 17:46:15 -07002240 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002241 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002242 }
2243
Adam Cohenf9618852013-11-08 06:45:03 -08002244 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2245 snapToPage(whichPage, duration, false, interpolator);
2246 }
2247
2248 protected void snapToPage(int whichPage, int duration, boolean immediate,
2249 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002250 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002251
Adam Cohenedb40762013-07-18 16:45:45 -07002252 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002253 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002254 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002255 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002256 }
2257
2258 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002259 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002260 }
Michael Jurka0142d492010-08-25 17:46:15 -07002261
Adam Cohenf9618852013-11-08 06:45:03 -08002262 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2263 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002264 whichPage = validateNewPage(whichPage);
2265
Adam Cohen7d30a372013-07-01 17:03:59 -07002266 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002267 View focusedChild = getFocusedChild();
2268 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002269 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002270 focusedChild.clearFocus();
2271 }
2272
Adam Cohen53805212013-10-01 10:39:23 -07002273 sendScrollAccessibilityEvent();
2274
Michael Jurka0142d492010-08-25 17:46:15 -07002275 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002276 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002277 if (immediate) {
2278 duration = 0;
2279 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002280 duration = Math.abs(delta);
2281 }
2282
Adam Cohenf358a4b2013-07-23 16:47:31 -07002283 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002284 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002285 }
Adam Cohenf9618852013-11-08 06:45:03 -08002286
2287 if (interpolator != null) {
2288 mScroller.setInterpolator(interpolator);
2289 } else {
2290 mScroller.setInterpolator(mDefaultInterpolator);
2291 }
2292
Adam Cohen68d73932010-11-15 10:50:58 -08002293 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002294
Adam Cohen674531f2013-12-13 15:07:14 -08002295 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002296
2297 // Trigger a compute() to finish switching pages if necessary
2298 if (immediate) {
2299 computeScroll();
2300 }
2301
Winson Chung9c0565f2013-07-19 13:49:06 -07002302 // Defer loading associated pages until the scroll settles
2303 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2304
Adam Cohen7d30a372013-07-01 17:03:59 -07002305 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002306 invalidate();
2307 }
2308
Winson Chung321e9ee2010-08-09 13:37:56 -07002309 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002310 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002311 }
2312
2313 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002314 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002315 }
2316
Winson Chung86f77532010-08-24 11:08:22 -07002317 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002318 int result = -1;
2319 if (v != null) {
2320 ViewParent vp = v.getParent();
2321 int count = getChildCount();
2322 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002323 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002324 return i;
2325 }
2326 }
2327 }
2328 return result;
2329 }
2330
2331 /**
2332 * @return True is long presses are still allowed for the current touch
2333 */
2334 public boolean allowLongPress() {
2335 return mAllowLongPress;
2336 }
2337
Adam Cohendbdff6b2013-09-18 19:09:15 -07002338 @Override
2339 public boolean performLongClick() {
2340 mCancelTap = true;
2341 return super.performLongClick();
2342 }
2343
Michael Jurka0142d492010-08-25 17:46:15 -07002344 /**
2345 * Set true to allow long-press events to be triggered, usually checked by
2346 * {@link Launcher} to accept or block dpad-initiated long-presses.
2347 */
2348 public void setAllowLongPress(boolean allowLongPress) {
2349 mAllowLongPress = allowLongPress;
2350 }
2351
Winson Chung321e9ee2010-08-09 13:37:56 -07002352 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002353 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002354
2355 SavedState(Parcelable superState) {
2356 super(superState);
2357 }
2358
2359 private SavedState(Parcel in) {
2360 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002361 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002362 }
2363
2364 @Override
2365 public void writeToParcel(Parcel out, int flags) {
2366 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002367 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002368 }
2369
2370 public static final Parcelable.Creator<SavedState> CREATOR =
2371 new Parcelable.Creator<SavedState>() {
2372 public SavedState createFromParcel(Parcel in) {
2373 return new SavedState(in);
2374 }
2375
2376 public SavedState[] newArray(int size) {
2377 return new SavedState[size];
2378 }
2379 };
2380 }
2381
Winson Chungf314b0e2011-08-16 11:54:27 -07002382 protected void loadAssociatedPages(int page) {
2383 loadAssociatedPages(page, false);
2384 }
2385 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002386 if (mContentIsRefreshable) {
2387 final int count = getChildCount();
2388 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002389 int lowerPageBound = getAssociatedLowerPageBound(page);
2390 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002391 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2392 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002393 // First, clear any pages that should no longer be loaded
2394 for (int i = 0; i < count; ++i) {
2395 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002396 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002397 if (layout.getPageChildCount() > 0) {
2398 layout.removeAllViewsOnPage();
2399 }
2400 mDirtyPageContent.set(i, true);
2401 }
2402 }
2403 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002404 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002405 if ((i != page) && immediateAndOnly) {
2406 continue;
2407 }
Michael Jurka0142d492010-08-25 17:46:15 -07002408 if (lowerPageBound <= i && i <= upperPageBound) {
2409 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002410 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002411 mDirtyPageContent.set(i, false);
2412 }
Winson Chung86f77532010-08-24 11:08:22 -07002413 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002414 }
2415 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002416 }
2417 }
2418
Winson Chunge3193b92010-09-10 11:44:42 -07002419 protected int getAssociatedLowerPageBound(int page) {
2420 return Math.max(0, page - 1);
2421 }
2422 protected int getAssociatedUpperPageBound(int page) {
2423 final int count = getChildCount();
2424 return Math.min(page + 1, count - 1);
2425 }
2426
Winson Chung86f77532010-08-24 11:08:22 -07002427 /**
2428 * This method is called ONLY to synchronize the number of pages that the paged view has.
2429 * To actually fill the pages with information, implement syncPageItems() below. It is
2430 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2431 * and therefore, individual page items do not need to be updated in this method.
2432 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002433 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002434
2435 /**
2436 * This method is called to synchronize the items that are on a particular page. If views on
2437 * the page can be reused, then they should be updated within this method.
2438 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002439 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002440
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002441 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002442 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002443 }
2444 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002445 invalidatePageData(currentPage, false);
2446 }
2447 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002448 if (!mIsDataReady) {
2449 return;
2450 }
2451
Michael Jurka0142d492010-08-25 17:46:15 -07002452 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002453 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002454 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002455
Michael Jurka0142d492010-08-25 17:46:15 -07002456 // Update all the pages
2457 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002458
Winson Chung5a808352011-06-27 19:08:49 -07002459 // We must force a measure after we've loaded the pages to update the content width and
2460 // to determine the full scroll width
2461 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2462 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2463
2464 // Set a new page as the current page if necessary
2465 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002466 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002467 }
2468
Michael Jurka0142d492010-08-25 17:46:15 -07002469 // Mark each of the pages as dirty
2470 final int count = getChildCount();
2471 mDirtyPageContent.clear();
2472 for (int i = 0; i < count; ++i) {
2473 mDirtyPageContent.add(true);
2474 }
2475
2476 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002477 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002478 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002479 }
Adam Cohen06559042013-09-29 18:30:56 -07002480 if (isPageMoving()) {
2481 // If the page is moving, then snap it to the final position to ensure we don't get
2482 // stuck between pages
2483 snapToDestination();
2484 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002485 }
Winson Chung007c6982011-06-14 13:27:53 -07002486
Adam Cohen7d30a372013-07-01 17:03:59 -07002487 // Animate the drag view back to the original position
2488 void animateDragViewToOriginalPosition() {
2489 if (mDragView != null) {
2490 AnimatorSet anim = new AnimatorSet();
2491 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2492 anim.playTogether(
2493 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2495 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2496 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002497 anim.addListener(new AnimatorListenerAdapter() {
2498 @Override
2499 public void onAnimationEnd(Animator animation) {
2500 onPostReorderingAnimationCompleted();
2501 }
2502 });
2503 anim.start();
2504 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002505 }
2506
Adam Cohen7d30a372013-07-01 17:03:59 -07002507 protected void onStartReordering() {
2508 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2509 mTouchState = TOUCH_STATE_REORDERING;
2510 mIsReordering = true;
2511
Adam Cohen7d30a372013-07-01 17:03:59 -07002512 // We must invalidate to trigger a redraw to update the layers such that the drag view
2513 // is always drawn on top
2514 invalidate();
2515 }
2516
2517 private void onPostReorderingAnimationCompleted() {
2518 // Trigger the callback when reordering has settled
2519 --mPostReorderingPreZoomInRemainingAnimationCount;
2520 if (mPostReorderingPreZoomInRunnable != null &&
2521 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2522 mPostReorderingPreZoomInRunnable.run();
2523 mPostReorderingPreZoomInRunnable = null;
2524 }
2525 }
2526
2527 protected void onEndReordering() {
2528 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002529 }
2530
Adam Cohenf358a4b2013-07-23 16:47:31 -07002531 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002532 int dragViewIndex = indexOfChild(v);
2533
Adam Cohen327acfe2014-06-06 11:52:52 -07002534 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002535
Adam Cohen7d30a372013-07-01 17:03:59 -07002536 mTempVisiblePagesRange[0] = 0;
2537 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002538 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002539 mReorderingStarted = true;
2540
2541 // Check if we are within the reordering range
2542 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002543 dragViewIndex <= mTempVisiblePagesRange[1]) {
2544 // Find the drag view under the pointer
2545 mDragView = getChildAt(dragViewIndex);
2546 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2547 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002548 snapToPage(getPageNearestToCenterOfScreen());
2549 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002550 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002551 return true;
2552 }
2553 return false;
2554 }
2555
2556 boolean isReordering(boolean testTouchState) {
2557 boolean state = mIsReordering;
2558 if (testTouchState) {
2559 state &= (mTouchState == TOUCH_STATE_REORDERING);
2560 }
2561 return state;
2562 }
2563 void endReordering() {
2564 // For simplicity, we call endReordering sometimes even if reordering was never started.
2565 // In that case, we don't want to do anything.
2566 if (!mReorderingStarted) return;
2567 mReorderingStarted = false;
2568
2569 // If we haven't flung-to-delete the current child, then we just animate the drag view
2570 // back into position
2571 final Runnable onCompleteRunnable = new Runnable() {
2572 @Override
2573 public void run() {
2574 onEndReordering();
2575 }
2576 };
2577 if (!mDeferringForDelete) {
2578 mPostReorderingPreZoomInRunnable = new Runnable() {
2579 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002580 onCompleteRunnable.run();
2581 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002582 };
2583 };
2584
2585 mPostReorderingPreZoomInRemainingAnimationCount =
2586 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2587 // Snap to the current page
2588 snapToPage(indexOfChild(mDragView), 0);
2589 // Animate the drag view back to the front position
2590 animateDragViewToOriginalPosition();
2591 } else {
2592 // Handled in post-delete-animation-callbacks
2593 }
2594 }
2595
Adam Cohen7d30a372013-07-01 17:03:59 -07002596 /*
2597 * Flinging to delete - IN PROGRESS
2598 */
2599 private PointF isFlingingToDelete() {
2600 ViewConfiguration config = ViewConfiguration.get(getContext());
2601 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2602
2603 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2604 // Do a quick dot product test to ensure that we are flinging upwards
2605 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2606 mVelocityTracker.getYVelocity());
2607 PointF upVec = new PointF(0f, -1f);
2608 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2609 (vel.length() * upVec.length()));
2610 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2611 return vel;
2612 }
2613 }
2614 return null;
2615 }
2616
2617 /**
2618 * Creates an animation from the current drag view along its current velocity vector.
2619 * For this animation, the alpha runs for a fixed duration and we update the position
2620 * progressively.
2621 */
2622 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2623 private View mDragView;
2624 private PointF mVelocity;
2625 private Rect mFrom;
2626 private long mPrevTime;
2627 private float mFriction;
2628
2629 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2630
2631 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2632 long startTime, float friction) {
2633 mDragView = dragView;
2634 mVelocity = vel;
2635 mFrom = from;
2636 mPrevTime = startTime;
2637 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2638 }
2639
2640 @Override
2641 public void onAnimationUpdate(ValueAnimator animation) {
2642 float t = ((Float) animation.getAnimatedValue()).floatValue();
2643 long curTime = AnimationUtils.currentAnimationTimeMillis();
2644
2645 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2646 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2647
2648 mDragView.setTranslationX(mFrom.left);
2649 mDragView.setTranslationY(mFrom.top);
2650 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2651
2652 mVelocity.x *= mFriction;
2653 mVelocity.y *= mFriction;
2654 mPrevTime = curTime;
2655 }
2656 };
2657
2658 private static final int ANIM_TAG_KEY = 100;
2659
2660 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2661 return new Runnable() {
2662 @Override
2663 public void run() {
2664 int dragViewIndex = indexOfChild(dragView);
2665
2666 // For each of the pages around the drag view, animate them from the previous
2667 // position to the new position in the layout (as a result of the drag view moving
2668 // in the layout)
2669 // NOTE: We can make an assumption here because we have side-bound pages that we
2670 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002671 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002672 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2673 boolean slideFromLeft = (isLastWidgetPage ||
2674 dragViewIndex > mTempVisiblePagesRange[0]);
2675
2676 // Setup the scroll to the correct page before we swap the views
2677 if (slideFromLeft) {
2678 snapToPageImmediately(dragViewIndex - 1);
2679 }
2680
2681 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2682 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2683 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2684 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2685 ArrayList<Animator> animations = new ArrayList<Animator>();
2686 for (int i = lowerIndex; i <= upperIndex; ++i) {
2687 View v = getChildAt(i);
2688 // dragViewIndex < pageUnderPointIndex, so after we remove the
2689 // drag view all subsequent views to pageUnderPointIndex will
2690 // shift down.
2691 int oldX = 0;
2692 int newX = 0;
2693 if (slideFromLeft) {
2694 if (i == 0) {
2695 // Simulate the page being offscreen with the page spacing
2696 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002697 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002698 } else {
2699 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2700 }
2701 newX = getViewportOffsetX() + getChildOffset(i);
2702 } else {
2703 oldX = getChildOffset(i) - getChildOffset(i - 1);
2704 newX = 0;
2705 }
2706
2707 // Animate the view translation from its old position to its new
2708 // position
2709 AnimatorSet anim = (AnimatorSet) v.getTag();
2710 if (anim != null) {
2711 anim.cancel();
2712 }
2713
2714 // Note: Hacky, but we want to skip any optimizations to not draw completely
2715 // hidden views
2716 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2717 v.setTranslationX(oldX - newX);
2718 anim = new AnimatorSet();
2719 anim.playTogether(
2720 ObjectAnimator.ofFloat(v, "translationX", 0f),
2721 ObjectAnimator.ofFloat(v, "alpha", 1f));
2722 animations.add(anim);
2723 v.setTag(ANIM_TAG_KEY, anim);
2724 }
2725
2726 AnimatorSet slideAnimations = new AnimatorSet();
2727 slideAnimations.playTogether(animations);
2728 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2729 slideAnimations.addListener(new AnimatorListenerAdapter() {
2730 @Override
2731 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002732 mDeferringForDelete = false;
2733 onEndReordering();
2734 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002735 }
2736 });
2737 slideAnimations.start();
2738
2739 removeView(dragView);
2740 onRemoveView(dragView, true);
2741 }
2742 };
2743 }
2744
2745 public void onFlingToDelete(PointF vel) {
2746 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2747
2748 // NOTE: Because it takes time for the first frame of animation to actually be
2749 // called and we expect the animation to be a continuation of the fling, we have
2750 // to account for the time that has elapsed since the fling finished. And since
2751 // we don't have a startDelay, we will always get call to update when we call
2752 // start() (which we want to ignore).
2753 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2754 private int mCount = -1;
2755 private long mStartTime;
2756 private float mOffset;
2757 /* Anonymous inner class ctor */ {
2758 mStartTime = startTime;
2759 }
2760
2761 @Override
2762 public float getInterpolation(float t) {
2763 if (mCount < 0) {
2764 mCount++;
2765 } else if (mCount == 0) {
2766 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2767 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2768 mCount++;
2769 }
2770 return Math.min(1f, mOffset + t);
2771 }
2772 };
2773
2774 final Rect from = new Rect();
2775 final View dragView = mDragView;
2776 from.left = (int) dragView.getTranslationX();
2777 from.top = (int) dragView.getTranslationY();
2778 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2779 from, startTime, FLING_TO_DELETE_FRICTION);
2780
2781 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2782
2783 // Create and start the animation
2784 ValueAnimator mDropAnim = new ValueAnimator();
2785 mDropAnim.setInterpolator(tInterpolator);
2786 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2787 mDropAnim.setFloatValues(0f, 1f);
2788 mDropAnim.addUpdateListener(updateCb);
2789 mDropAnim.addListener(new AnimatorListenerAdapter() {
2790 public void onAnimationEnd(Animator animation) {
2791 onAnimationEndRunnable.run();
2792 }
2793 });
2794 mDropAnim.start();
2795 mDeferringForDelete = true;
2796 }
2797
2798 /* Drag to delete */
2799 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2800 if (mDeleteDropTarget != null) {
2801 mAltTmpRect.set(0, 0, 0, 0);
2802 View parent = (View) mDeleteDropTarget.getParent();
2803 if (parent != null) {
2804 parent.getGlobalVisibleRect(mAltTmpRect);
2805 }
2806 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2807 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2808 return mTmpRect.contains(x, y);
2809 }
2810 return false;
2811 }
2812
2813 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2814
2815 private void onDropToDelete() {
2816 final View dragView = mDragView;
2817
2818 final float toScale = 0f;
2819 final float toAlpha = 0f;
2820
2821 // Create and start the complex animation
2822 ArrayList<Animator> animations = new ArrayList<Animator>();
2823 AnimatorSet motionAnim = new AnimatorSet();
2824 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2825 motionAnim.playTogether(
2826 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2827 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2828 animations.add(motionAnim);
2829
2830 AnimatorSet alphaAnim = new AnimatorSet();
2831 alphaAnim.setInterpolator(new LinearInterpolator());
2832 alphaAnim.playTogether(
2833 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2834 animations.add(alphaAnim);
2835
2836 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2837
2838 AnimatorSet anim = new AnimatorSet();
2839 anim.playTogether(animations);
2840 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2841 anim.addListener(new AnimatorListenerAdapter() {
2842 public void onAnimationEnd(Animator animation) {
2843 onAnimationEndRunnable.run();
2844 }
2845 });
2846 anim.start();
2847
2848 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002849 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002850
2851 /* Accessibility */
2852 @Override
2853 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2854 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002855 info.setScrollable(getPageCount() > 1);
2856 if (getCurrentPage() < getPageCount() - 1) {
2857 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2858 }
2859 if (getCurrentPage() > 0) {
2860 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2861 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002862 }
2863
2864 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002865 public void sendAccessibilityEvent(int eventType) {
2866 // Don't let the view send real scroll events.
2867 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2868 super.sendAccessibilityEvent(eventType);
2869 }
2870 }
2871
2872 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002873 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2874 super.onInitializeAccessibilityEvent(event);
2875 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002876 }
2877
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002878 @Override
2879 public boolean performAccessibilityAction(int action, Bundle arguments) {
2880 if (super.performAccessibilityAction(action, arguments)) {
2881 return true;
2882 }
2883 switch (action) {
2884 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2885 if (getCurrentPage() < getPageCount() - 1) {
2886 scrollRight();
2887 return true;
2888 }
2889 } break;
2890 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2891 if (getCurrentPage() > 0) {
2892 scrollLeft();
2893 return true;
2894 }
2895 } break;
2896 }
2897 return false;
2898 }
2899
Adam Cohen0ffac432013-07-10 11:19:26 -07002900 protected String getCurrentPageDescription() {
2901 return String.format(getContext().getString(R.string.default_scroll_format),
2902 getNextPage() + 1, getChildCount());
2903 }
2904
Winson Chungd11265e2011-08-30 13:37:23 -07002905 @Override
2906 public boolean onHoverEvent(android.view.MotionEvent event) {
2907 return true;
2908 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002909}