blob: 90a6b1598838c2584bd1b307458b71531ea9524f [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;
Winson Chungf0c6ae02012-03-21 16:10:31 -070077 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Adam Cohenb5ba0972011-09-07 18:02:31 -070080 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070081 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Adam Cohen265b9a62011-12-07 14:37:18 -080087 // The following constants need to be scaled based on density. The scaled versions will be
88 // assigned to the corresponding member variables below.
89 private static final int FLING_THRESHOLD_VELOCITY = 500;
90 private static final int MIN_SNAP_VELOCITY = 1500;
91 private static final int MIN_FLING_VELOCITY = 250;
92
Adam Cohen7d30a372013-07-01 17:03:59 -070093 // We are disabling touch interaction of the widget region for factory ROM.
94 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070096 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070097
Adam Cohen21cd0022013-10-09 18:57:02 -070098 public static final int INVALID_RESTORE_PAGE = -1001;
99
Adam Cohenf358a4b2013-07-23 16:47:31 -0700100 private boolean mFreeScroll = false;
101 private int mFreeScrollMinScrollX = -1;
102 private int mFreeScrollMaxScrollX = -1;
103
Winson Chung8aad6102012-05-11 16:27:49 -0700104 static final int AUTOMATIC_PAGE_SPACING = -1;
105
Adam Cohen265b9a62011-12-07 14:37:18 -0800106 protected int mFlingThresholdVelocity;
107 protected int mMinFlingVelocity;
108 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700109
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700111 protected float mSmoothingTime;
112 protected float mTouchX;
113
114 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700115 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700116
117 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700118 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700119 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700120
Michael Jurka0142d492010-08-25 17:46:15 -0700121 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800122 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800123 protected LauncherScroller mScroller;
124 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700126 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 private float mParentDownMotionX;
129 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 private float mDownMotionY;
132 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700133 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800135 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800137 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700138 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700139
Adam Cohendbdff6b2013-09-18 19:09:15 -0700140 private boolean mCancelTap;
141
Adam Cohenedb40762013-07-18 16:45:45 -0700142 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 protected final static int TOUCH_STATE_REST = 0;
145 protected final static int TOUCH_STATE_SCROLLING = 1;
146 protected final static int TOUCH_STATE_PREV_PAGE = 2;
147 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected final static int TOUCH_STATE_REORDERING = 4;
149
Adam Cohene45440e2010-10-14 18:33:38 -0700150 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700153 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700156
Michael Jurka7426c422010-11-11 15:23:47 -0800157 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700158 private int mPagingTouchSlop;
159 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700160 protected int mPageLayoutWidthGap;
161 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700162 protected int mCellCountX = 0;
163 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700164 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800165 protected boolean mAllowOverScroll = true;
166 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800167 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700168 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700169
Michael Jurka8b805b12012-04-18 14:23:14 -0700170 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800171 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
172 // the screens from continuing to translate beyond the normal bounds.
173 protected int mOverScrollX;
174
Michael Jurka5f1c5092010-09-03 14:15:02 -0700175 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700176
Michael Jurka5f1c5092010-09-03 14:15:02 -0700177 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700178
Winson Chung86f77532010-08-24 11:08:22 -0700179 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Michael Jurkae326f182011-11-21 14:05:46 -0800181 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Michael Jurka0142d492010-08-25 17:46:15 -0700183 // If true, syncPages and syncPageItems will be called to refresh pages
184 protected boolean mContentIsRefreshable = true;
185
186 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700187 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700188
189 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
190 // to switch to a new page
191 protected boolean mUsePagingTouchSlop = true;
192
Michael Jurka8b805b12012-04-18 14:23:14 -0700193 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700194 // (SmoothPagedView does this)
195 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700196 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700197
Patrick Dubroy1262e362010-10-06 15:49:50 -0700198 protected boolean mIsPageMoving = false;
199
Winson Chungf0ea4d32011-06-06 14:27:16 -0700200 // All syncs and layout passes are deferred until data is ready.
201 protected boolean mIsDataReady = false;
202
Adam Cohen7d30a372013-07-01 17:03:59 -0700203 protected boolean mAllowLongPress = true;
204
Winson Chungd2be3812013-07-16 11:11:32 -0700205 // Page Indicator
206 private int mPageIndicatorViewId;
207 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700208 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700209
Adam Cohen7d30a372013-07-01 17:03:59 -0700210 // The viewport whether the pages are to be contained (the actual view may be larger than the
211 // viewport)
212 private Rect mViewport = new Rect();
213
214 // Reordering
215 // We use the min scale to determine how much to expand the actually PagedView measured
216 // dimensions such that when we are zoomed out, the view is not clipped
217 private int REORDERING_DROP_REPOSITION_DURATION = 200;
218 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
219 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700220 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700221 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700222 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700223 protected View mDragView;
224 protected AnimatorSet mZoomInOutAnim;
225 private Runnable mSidePageHoverRunnable;
226 private int mSidePageHoverIndex = -1;
227 // This variable's scope is only for the duration of startReordering() and endReordering()
228 private boolean mReorderingStarted = false;
229 // This variable's scope is for the duration of startReordering() and after the zoomIn()
230 // animation after endReordering()
231 private boolean mIsReordering;
232 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
233 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
234 private int mPostReorderingPreZoomInRemainingAnimationCount;
235 private Runnable mPostReorderingPreZoomInRunnable;
236
Adam Cohen7d30a372013-07-01 17:03:59 -0700237 // Convenience/caching
238 private Matrix mTmpInvMatrix = new Matrix();
239 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700240 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700241 private Rect mTmpRect = new Rect();
242 private Rect mAltTmpRect = new Rect();
243
244 // Fling to delete
245 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
246 private float FLING_TO_DELETE_FRICTION = 0.035f;
247 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
248 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
249 protected int mFlingToDeleteThresholdVelocity = -1400;
250 // Drag to delete
251 private boolean mDeferringForDelete = false;
252 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
253 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
254
255 // Drop to delete
256 private View mDeleteDropTarget;
257
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 // Bouncer
259 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700260
John Spurlock77e1f472013-09-11 10:09:51 -0400261 protected final Rect mInsets = new Rect();
262
Winson Chung86f77532010-08-24 11:08:22 -0700263 public interface PageSwitchListener {
264 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 }
266
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 public PagedView(Context context) {
268 this(context, null);
269 }
270
271 public PagedView(Context context, AttributeSet attrs) {
272 this(context, attrs, 0);
273 }
274
275 public PagedView(Context context, AttributeSet attrs, int defStyle) {
276 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700277
Adam Cohen9c4949e2010-10-05 12:27:22 -0700278 TypedArray a = context.obtainStyledAttributes(attrs,
279 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700280
Winson Chungdf4b83d2010-10-20 17:49:27 -0700281 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700282 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700283 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700284 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700285 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700286 a.recycle();
287
Winson Chung321e9ee2010-08-09 13:37:56 -0700288 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700289 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 }
291
292 /**
293 * Initializes various states for this workspace.
294 */
Michael Jurka0142d492010-08-25 17:46:15 -0700295 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700296 mDirtyPageContent = new ArrayList<Boolean>();
297 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800298 mScroller = new LauncherScroller(getContext());
299 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700300 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700301 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700302
303 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700304 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
306 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700307 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800308
Adam Cohen7d30a372013-07-01 17:03:59 -0700309 // Scale the fling-to-delete threshold by the density
310 mFlingToDeleteThresholdVelocity =
311 (int) (mFlingToDeleteThresholdVelocity * mDensity);
312
Adam Cohen265b9a62011-12-07 14:37:18 -0800313 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
314 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
315 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700316 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 }
318
Adam Cohenf9618852013-11-08 06:45:03 -0800319 protected void setDefaultInterpolator(Interpolator interpolator) {
320 mDefaultInterpolator = interpolator;
321 mScroller.setInterpolator(mDefaultInterpolator);
322 }
323
Winson Chungd2be3812013-07-16 11:11:32 -0700324 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700325 super.onAttachedToWindow();
326
Winson Chungd2be3812013-07-16 11:11:32 -0700327 // Hook up the page indicator
328 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700329 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700330 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700331 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700332 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700333
Winson Chung7819a562013-09-19 15:55:45 -0700334 ArrayList<PageIndicator.PageMarkerResources> markers =
335 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700336 for (int i = 0; i < getChildCount(); ++i) {
337 markers.add(getPageIndicatorMarker(i));
338 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700339
Winson Chungc58497e2013-09-03 17:48:37 -0700340 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700341
342 OnClickListener listener = getPageIndicatorClickListener();
343 if (listener != null) {
344 mPageIndicator.setOnClickListener(listener);
345 }
Adam Cohen53805212013-10-01 10:39:23 -0700346 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700347 }
348 }
349
Adam Cohen53805212013-10-01 10:39:23 -0700350 protected String getPageIndicatorDescription() {
351 return getCurrentPageDescription();
352 }
353
354 protected OnClickListener getPageIndicatorClickListener() {
355 return null;
356 }
357
Winson Chungd2be3812013-07-16 11:11:32 -0700358 protected void onDetachedFromWindow() {
359 // Unhook the page indicator
360 mPageIndicator = null;
361 }
362
Adam Cohen7d30a372013-07-01 17:03:59 -0700363 void setDeleteDropTarget(View v) {
364 mDeleteDropTarget = v;
365 }
366
367 // Convenience methods to map points from self to parent and vice versa
368 float[] mapPointFromViewToParent(View v, float x, float y) {
369 mTmpPoint[0] = x;
370 mTmpPoint[1] = y;
371 v.getMatrix().mapPoints(mTmpPoint);
372 mTmpPoint[0] += v.getLeft();
373 mTmpPoint[1] += v.getTop();
374 return mTmpPoint;
375 }
376 float[] mapPointFromParentToView(View v, float x, float y) {
377 mTmpPoint[0] = x - v.getLeft();
378 mTmpPoint[1] = y - v.getTop();
379 v.getMatrix().invert(mTmpInvMatrix);
380 mTmpInvMatrix.mapPoints(mTmpPoint);
381 return mTmpPoint;
382 }
383
384 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700385 if (mDragView != null) {
386 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
387 (mDragViewBaselineLeft - mDragView.getLeft());
388 float y = mLastMotionY - mDownMotionY;
389 mDragView.setTranslationX(x);
390 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700391
Adam Cohenf358a4b2013-07-23 16:47:31 -0700392 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
393 + x + ", " + y);
394 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700395 }
396
397 public void setMinScale(float f) {
398 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700399 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700400 requestLayout();
401 }
402
403 @Override
404 public void setScaleX(float scaleX) {
405 super.setScaleX(scaleX);
406 if (isReordering(true)) {
407 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
408 mLastMotionX = p[0];
409 mLastMotionY = p[1];
410 updateDragViewTranslationDuringDrag();
411 }
412 }
413
414 // Convenience methods to get the actual width/height of the PagedView (since it is measured
415 // to be larger to account for the minimum possible scale)
416 int getViewportWidth() {
417 return mViewport.width();
418 }
419 int getViewportHeight() {
420 return mViewport.height();
421 }
422
423 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
424 // PagedView both horizontally and vertically
425 int getViewportOffsetX() {
426 return (getMeasuredWidth() - getViewportWidth()) / 2;
427 }
428
429 int getViewportOffsetY() {
430 return (getMeasuredHeight() - getViewportHeight()) / 2;
431 }
432
Adam Cohenedb40762013-07-18 16:45:45 -0700433 PageIndicator getPageIndicator() {
434 return mPageIndicator;
435 }
Winson Chung7819a562013-09-19 15:55:45 -0700436 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
437 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700438 }
Adam Cohenedb40762013-07-18 16:45:45 -0700439
Adam Cohen674531f2013-12-13 15:07:14 -0800440 /**
441 * Add a page change listener which will be called when a page is _finished_ listening.
442 *
443 */
Winson Chung86f77532010-08-24 11:08:22 -0700444 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
445 mPageSwitchListener = pageSwitchListener;
446 if (mPageSwitchListener != null) {
447 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 }
449 }
450
451 /**
Winson Chung52aee602013-01-30 12:01:02 -0800452 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
453 */
454 public boolean isLayoutRtl() {
455 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
456 }
457
458 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
460 * out pages.
461 */
462 protected void setDataIsReady() {
463 mIsDataReady = true;
464 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700465
Winson Chungf0ea4d32011-06-06 14:27:16 -0700466 protected boolean isDataReady() {
467 return mIsDataReady;
468 }
469
470 /**
Winson Chung86f77532010-08-24 11:08:22 -0700471 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 *
Winson Chung86f77532010-08-24 11:08:22 -0700473 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 */
Winson Chung86f77532010-08-24 11:08:22 -0700475 int getCurrentPage() {
476 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700478
Winson Chung360e63f2012-04-27 13:48:05 -0700479 int getNextPage() {
480 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
481 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700482
Winson Chung86f77532010-08-24 11:08:22 -0700483 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 return getChildCount();
485 }
486
Winson Chung86f77532010-08-24 11:08:22 -0700487 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 return getChildAt(index);
489 }
490
Adam Cohenae4f1552011-10-20 00:15:42 -0700491 protected int indexToPage(int index) {
492 return index;
493 }
494
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800496 * Updates the scroll of the current page immediately to its final scroll position. We use this
497 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
498 * the previous tab page.
499 */
500 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700501 // If the current page is invalid, just reset the scroll position to zero
502 int newX = 0;
503 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700504 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700505 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800506 scrollTo(newX, 0);
507 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700508 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800509 }
510
511 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700512 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700513 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
514 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700515 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700516 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700517 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800518 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700519 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700520 }
521
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700522 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700523 mScroller.abortAnimation();
524 // We need to clean up the next page here to avoid computeScrollHelper from
525 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700526 if (resetNextPage) {
527 mNextPage = INVALID_PAGE;
528 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700529 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700530
531 private void forceFinishScroller() {
532 mScroller.forceFinished(true);
533 // We need to clean up the next page here to avoid computeScrollHelper from
534 // updating current page on the pass.
535 mNextPage = INVALID_PAGE;
536 }
537
Adam Cohen6f127a62014-06-12 14:54:41 -0700538 private int validateNewPage(int newPage) {
539 int validatedPage = newPage;
540 // When in free scroll mode, we need to clamp to the free scroll page range.
541 if (mFreeScroll) {
542 getFreeScrollPageRange(mTempVisiblePagesRange);
543 validatedPage = Math.max(mTempVisiblePagesRange[0],
544 Math.min(newPage, mTempVisiblePagesRange[1]));
545 }
546 // Ensure that it is clamped by the actual set of children in all cases
547 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
548 return validatedPage;
549 }
550
Chet Haasebc2f0822012-10-26 17:59:53 -0700551 /**
Winson Chung86f77532010-08-24 11:08:22 -0700552 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 */
Winson Chung86f77532010-08-24 11:08:22 -0700554 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800555 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700556 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800557 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
559 // the default
560 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800561 return;
562 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700563 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700564 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700565 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700566 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800567 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569
Winson Chung8c87cd82013-07-23 16:20:10 -0700570 /**
571 * The restore page will be set in place of the current page at the next (likely first)
572 * layout.
573 */
574 void setRestorePage(int restorePage) {
575 mRestorePage = restorePage;
576 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800577 int getRestorePage() {
578 return mRestorePage;
579 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700580
Adam Cohen674531f2013-12-13 15:07:14 -0800581 /**
582 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
583 * has settled.
584 */
Michael Jurka0142d492010-08-25 17:46:15 -0700585 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700586 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800587 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700588 }
Winson Chungd2be3812013-07-16 11:11:32 -0700589
Adam Cohen674531f2013-12-13 15:07:14 -0800590 updatePageIndicator();
591 }
592
593 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700594 // Update the page indicator (when we aren't reordering)
595 if (mPageIndicator != null && !isReordering(false)) {
596 mPageIndicator.setActiveMarker(getNextPage());
597 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800599 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700600 if (!mIsPageMoving) {
601 mIsPageMoving = true;
602 onPageBeginMoving();
603 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700604 }
605
Michael Jurkace7e05f2011-02-01 22:02:35 -0800606 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700607 if (mIsPageMoving) {
608 mIsPageMoving = false;
609 onPageEndMoving();
610 }
Michael Jurka0142d492010-08-25 17:46:15 -0700611 }
612
Adam Cohen26976d92011-03-22 15:33:33 -0700613 protected boolean isPageMoving() {
614 return mIsPageMoving;
615 }
616
Michael Jurka0142d492010-08-25 17:46:15 -0700617 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700618 protected void onPageBeginMoving() {
619 }
620
621 // a method that subclasses can override to add behavior
622 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700623 }
624
Winson Chung321e9ee2010-08-09 13:37:56 -0700625 /**
Winson Chung86f77532010-08-24 11:08:22 -0700626 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 *
628 * @param l The listener used to respond to long clicks.
629 */
630 @Override
631 public void setOnLongClickListener(OnLongClickListener l) {
632 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700633 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700635 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 }
Adam Cohen1697b792013-09-17 19:08:21 -0700637 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
639
640 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800641 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800643 }
644
645 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700646 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700647 // In free scroll mode, we clamp the scrollX
648 if (mFreeScroll) {
649 x = Math.min(x, mFreeScrollMaxScrollX);
650 x = Math.max(x, mFreeScrollMinScrollX);
651 }
652
Adam Cohen0ffac432013-07-10 11:19:26 -0700653 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800654 mUnboundedScrollX = x;
655
Adam Cohen0ffac432013-07-10 11:19:26 -0700656 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
657 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
658 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800659 super.scrollTo(0, y);
660 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700661 if (isRtl) {
662 overScroll(x - mMaxScrollX);
663 } else {
664 overScroll(x);
665 }
Adam Cohen68d73932010-11-15 10:50:58 -0800666 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700667 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800668 super.scrollTo(mMaxScrollX, y);
669 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700670 if (isRtl) {
671 overScroll(x);
672 } else {
673 overScroll(x - mMaxScrollX);
674 }
Adam Cohen68d73932010-11-15 10:50:58 -0800675 }
676 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800677 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800678 super.scrollTo(x, y);
679 }
680
Michael Jurka0142d492010-08-25 17:46:15 -0700681 mTouchX = x;
682 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700683
684 // Update the last motion events when scrolling
685 if (isReordering(true)) {
686 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
687 mLastMotionX = p[0];
688 mLastMotionY = p[1];
689 updateDragViewTranslationDuringDrag();
690 }
Michael Jurka0142d492010-08-25 17:46:15 -0700691 }
692
Adam Cohen53805212013-10-01 10:39:23 -0700693 private void sendScrollAccessibilityEvent() {
694 AccessibilityManager am =
695 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
696 if (am.isEnabled()) {
697 AccessibilityEvent ev =
698 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700699 ev.setItemCount(getChildCount());
700 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700701 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700702
Alan Viverette254139a2013-10-08 13:13:46 -0700703 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700704 if (getNextPage() >= mCurrentPage) {
705 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
706 } else {
707 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
708 }
709
710 ev.setAction(action);
711 sendAccessibilityEventUnchecked(ev);
712 }
713 }
714
Michael Jurka0142d492010-08-25 17:46:15 -0700715 // we moved this functionality to a helper function so SmoothPagedView can reuse it
716 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700718 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 if (getScrollX() != mScroller.getCurrX()
720 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700721 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700722 float scaleX = mFreeScroll ? getScaleX() : 1f;
723 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
724 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700725 }
Michael Jurka0142d492010-08-25 17:46:15 -0700726 invalidate();
727 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700728 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700729 sendScrollAccessibilityEvent();
730
Adam Cohen6f127a62014-06-12 14:54:41 -0700731 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700732 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700733 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700734
Winson Chung9c0565f2013-07-19 13:49:06 -0700735 // Load the associated pages if necessary
736 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
737 loadAssociatedPages(mCurrentPage);
738 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
739 }
740
Adam Cohen73aa9752010-11-24 16:26:58 -0800741 // We don't want to trigger a page end moving unless the page has settled
742 // and the user has stopped scrolling
743 if (mTouchState == TOUCH_STATE_REST) {
744 pageEndMoving();
745 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700746
Adam Cohen7d30a372013-07-01 17:03:59 -0700747 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700748 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700749 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700750 if (am.isEnabled()) {
751 // Notify the user when the page changes
752 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700753 }
Michael Jurka0142d492010-08-25 17:46:15 -0700754 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 }
Michael Jurka0142d492010-08-25 17:46:15 -0700756 return false;
757 }
758
759 @Override
760 public void computeScroll() {
761 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 }
763
Adam Cohen7d30a372013-07-01 17:03:59 -0700764 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
765 return mTopAlignPageWhenShrinkingForBouncer;
766 }
767
Adam Cohen96d30a12013-07-16 18:13:21 -0700768 public static class LayoutParams extends ViewGroup.LayoutParams {
769 public boolean isFullScreenPage = false;
770
771 /**
772 * {@inheritDoc}
773 */
774 public LayoutParams(int width, int height) {
775 super(width, height);
776 }
777
778 public LayoutParams(ViewGroup.LayoutParams source) {
779 super(source);
780 }
781 }
782
783 protected LayoutParams generateDefaultLayoutParams() {
784 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
785 }
786
Adam Cohenedb40762013-07-18 16:45:45 -0700787 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700788 LayoutParams lp = generateDefaultLayoutParams();
789 lp.isFullScreenPage = true;
790 super.addView(page, 0, lp);
791 }
792
Adam Cohen410f3cd2013-09-22 12:09:32 -0700793 public int getNormalChildHeight() {
794 return mNormalChildHeight;
795 }
796
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 @Override
798 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700800 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
801 return;
802 }
803
Adam Cohen7d30a372013-07-01 17:03:59 -0700804 // We measure the dimensions of the PagedView to be larger than the pages so that when we
805 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700806 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
807 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
808 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700809 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800810 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700811 // viewport, we can be at most one and a half screens offset once we scale down
812 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700813 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
814 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700815
Winson Chungc82d2622013-11-06 13:23:29 -0800816 int parentWidthSize = (int) (2f * maxSize);
817 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700818 int scaledWidthSize, scaledHeightSize;
819 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700820 scaledWidthSize = (int) (parentWidthSize / mMinScale);
821 scaledHeightSize = (int) (parentHeightSize / mMinScale);
822 } else {
823 scaledWidthSize = widthSize;
824 scaledHeightSize = heightSize;
825 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700826 mViewport.set(0, 0, widthSize, heightSize);
827
828 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
829 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
830 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
832
Winson Chung8aad6102012-05-11 16:27:49 -0700833 // Return early if we aren't given a proper dimension
834 if (widthSize <= 0 || heightSize <= 0) {
835 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
836 return;
837 }
838
Adam Lesinski6b879f02010-11-04 16:15:23 -0700839 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
840 * of the All apps view on XLarge displays to not take up more space then it needs. Width
841 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
842 * each page to have the same width.
843 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700844 final int verticalPadding = getPaddingTop() + getPaddingBottom();
845 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700846
847 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700848 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700849 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700850 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
851 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
852 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
853 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 final int childCount = getChildCount();
855 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700856 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700857 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100858 if (child.getVisibility() != GONE) {
859 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700860
Vladimir Marko2824b072013-10-04 16:42:17 +0100861 int childWidthMode;
862 int childHeightMode;
863 int childWidth;
864 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700865
Vladimir Marko2824b072013-10-04 16:42:17 +0100866 if (!lp.isFullScreenPage) {
867 if (lp.width == LayoutParams.WRAP_CONTENT) {
868 childWidthMode = MeasureSpec.AT_MOST;
869 } else {
870 childWidthMode = MeasureSpec.EXACTLY;
871 }
872
873 if (lp.height == LayoutParams.WRAP_CONTENT) {
874 childHeightMode = MeasureSpec.AT_MOST;
875 } else {
876 childHeightMode = MeasureSpec.EXACTLY;
877 }
878
Adam Cohen3b185e22013-10-29 14:45:58 -0700879 childWidth = getViewportWidth() - horizontalPadding
880 - mInsets.left - mInsets.right;
881 childHeight = getViewportHeight() - verticalPadding
882 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100883 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700884 } else {
885 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700886 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100887
Adam Cohen3b185e22013-10-29 14:45:58 -0700888 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
889 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700890 }
891
Vladimir Marko2824b072013-10-04 16:42:17 +0100892 final int childWidthMeasureSpec =
893 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
894 final int childHeightMeasureSpec =
895 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
896 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700897 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700898 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700899 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800900 }
901
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700903 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700905 return;
906 }
907
Winson Chung785d2eb2011-04-14 16:08:02 -0700908 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700910
Adam Cohen7d30a372013-07-01 17:03:59 -0700911 int offsetX = getViewportOffsetX();
912 int offsetY = getViewportOffsetY();
913
914 // Update the viewport offsets
915 mViewport.offset(offsetX, offsetY);
916
Adam Cohen0ffac432013-07-10 11:19:26 -0700917 final boolean isRtl = isLayoutRtl();
918
919 final int startIndex = isRtl ? childCount - 1 : 0;
920 final int endIndex = isRtl ? -1 : childCount;
921 final int delta = isRtl ? -1 : 1;
922
Adam Cohen7d30a372013-07-01 17:03:59 -0700923 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700924
Adam Cohen3b185e22013-10-29 14:45:58 -0700925 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000926 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700927
928 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700929 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
930 mPageScrolls = new int[getChildCount()];
931 }
932
Adam Cohen0ffac432013-07-10 11:19:26 -0700933 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700934 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700936 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100937 int childTop;
938 if (lp.isFullScreenPage) {
939 childTop = offsetY;
940 } else {
941 childTop = offsetY + getPaddingTop() + mInsets.top;
942 if (mCenterPagesVertically) {
943 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
944 }
945 }
946
Adam Cohen96d30a12013-07-16 18:13:21 -0700947 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700948 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800949
Winson Chung785d2eb2011-04-14 16:08:02 -0700950 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700951 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800952 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700953
Adam Cohen3b185e22013-10-29 14:45:58 -0700954 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
955 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000956
957 int pageGap = mPageSpacing;
958 int next = i + delta;
959 if (next != endIndex) {
960 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
961 } else {
962 nextLp = null;
963 }
964
965 // Prevent full screen pages from showing in the viewport
966 // when they are not the current page.
967 if (lp.isFullScreenPage) {
968 pageGap = getPaddingLeft();
969 } else if (nextLp != null && nextLp.isFullScreenPage) {
970 pageGap = getPaddingRight();
971 }
972
973 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 }
975 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700976
977 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
978 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800979 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700980 setHorizontalScrollBarEnabled(true);
981 mFirstLayout = false;
982 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700983
984 if (childCount > 0) {
985 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700986 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700987 } else {
988 mMaxScrollX = 0;
989 }
990
991 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
992 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700993 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700994 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700995 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700996 } else {
997 setCurrentPage(getNextPage());
998 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700999 }
1000 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001001
1002 if (isReordering(true)) {
1003 updateDragViewTranslationDuringDrag();
1004 }
Winson Chunge3193b92010-09-10 11:44:42 -07001005 }
1006
Adam Cohen3b185e22013-10-29 14:45:58 -07001007 public void setPageSpacing(int pageSpacing) {
1008 mPageSpacing = pageSpacing;
1009 requestLayout();
1010 }
1011
Adam Cohenf34bab52010-09-30 14:11:56 -07001012 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001013 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1014
1015 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001016 for (int i = 0; i < getChildCount(); i++) {
1017 View child = getChildAt(i);
1018 if (child != null) {
1019 float scrollProgress = getScrollProgress(screenCenter, child, i);
1020 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001021 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001022 }
1023 }
1024 invalidate();
1025 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001026 }
1027
Winson Chungc58497e2013-09-03 17:48:37 -07001028 protected void enablePagedViewAnimations() {
1029 mAllowPagedViewAnimations = true;
1030
1031 }
1032 protected void disablePagedViewAnimations() {
1033 mAllowPagedViewAnimations = false;
1034 }
1035
Winson Chunge3193b92010-09-10 11:44:42 -07001036 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001037 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001038 // Update the page indicator, we don't update the page indicator as we
1039 // add/remove pages
1040 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001041 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001042 mPageIndicator.addMarker(pageIndex,
1043 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001044 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001045 }
1046
Adam Cohen2591f6a2011-10-25 14:36:40 -07001047 // This ensures that when children are added, they get the correct transforms / alphas
1048 // in accordance with any scroll effects.
1049 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001050 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001051 invalidate();
1052 }
1053
Michael Jurka8b805b12012-04-18 14:23:14 -07001054 @Override
1055 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001056 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001057 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001058 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001059 }
1060
Winson Chungd2be3812013-07-16 11:11:32 -07001061 private void removeMarkerForView(int index) {
1062 // Update the page indicator, we don't update the page indicator as we
1063 // add/remove pages
1064 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001065 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001066 }
1067 }
1068
1069 @Override
1070 public void removeView(View v) {
1071 // XXX: We should find a better way to hook into this before the view
1072 // gets removed form its parent...
1073 removeMarkerForView(indexOfChild(v));
1074 super.removeView(v);
1075 }
1076 @Override
1077 public void removeViewInLayout(View v) {
1078 // XXX: We should find a better way to hook into this before the view
1079 // gets removed form its parent...
1080 removeMarkerForView(indexOfChild(v));
1081 super.removeViewInLayout(v);
1082 }
1083 @Override
1084 public void removeViewAt(int index) {
1085 // XXX: We should find a better way to hook into this before the view
1086 // gets removed form its parent...
1087 removeViewAt(index);
1088 super.removeViewAt(index);
1089 }
1090 @Override
1091 public void removeAllViewsInLayout() {
1092 // Update the page indicator, we don't update the page indicator as we
1093 // add/remove pages
1094 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001095 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001096 }
1097
1098 super.removeAllViewsInLayout();
1099 }
1100
Adam Cohen73894962011-10-31 13:17:17 -07001101 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001102 if (index < 0 || index > getChildCount() - 1) return 0;
1103
Adam Cohenf698c6e2013-07-17 18:44:25 -07001104 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001105
Adam Cohenf698c6e2013-07-17 18:44:25 -07001106 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001107 }
1108
Adam Cohen6f127a62014-06-12 14:54:41 -07001109 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001110 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001111 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001112 }
1113
Michael Jurkadde558b2011-11-09 22:09:06 -08001114 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001115 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001116 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001117
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001118 range[0] = -1;
1119 range[1] = -1;
1120
Michael Jurkac4fb9172010-09-02 17:19:20 -07001121 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001122 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001123 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001124
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001125 int count = getChildCount();
1126 for (int i = 0; i < count; i++) {
1127 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001128
Winson Chungc9ca2982013-07-19 12:07:38 -07001129 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001130 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001131 if (mTmpIntPoint[0] > viewportWidth) {
1132 if (range[0] == -1) {
1133 continue;
1134 } else {
1135 break;
1136 }
1137 }
1138
Adam Cohen6a678da2013-09-24 10:58:01 -07001139 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001140 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1141 if (mTmpIntPoint[0] < 0) {
1142 if (range[0] == -1) {
1143 continue;
1144 } else {
1145 break;
1146 }
1147 }
1148 curScreen = i;
1149 if (range[0] < 0) {
1150 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001151 }
1152 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001153
1154 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001155 } else {
1156 range[0] = -1;
1157 range[1] = -1;
1158 }
1159 }
1160
Michael Jurka920d7f42012-05-14 16:29:55 -07001161 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001162 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001163 }
1164
Michael Jurkadde558b2011-11-09 22:09:06 -08001165 @Override
1166 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001167 // Find out which screens are visible; as an optimization we only call draw on them
1168 final int pageCount = getChildCount();
1169 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001170 int halfScreenSize = getViewportWidth() / 2;
1171 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1172 // Otherwise it is equal to the scaled overscroll position.
1173 int screenCenter = mOverScrollX + halfScreenSize;
1174
1175 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1176 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1177 // set it for the next frame
1178 mForceScreenScrolled = false;
1179 screenScrolled(screenCenter);
1180 mLastScreenCenter = screenCenter;
1181 }
1182
Michael Jurkadde558b2011-11-09 22:09:06 -08001183 getVisiblePages(mTempVisiblePagesRange);
1184 final int leftScreen = mTempVisiblePagesRange[0];
1185 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001186 if (leftScreen != -1 && rightScreen != -1) {
1187 final long drawingTime = getDrawingTime();
1188 // Clip to the bounds
1189 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001190 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1191 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001192
Adam Cohen7d30a372013-07-01 17:03:59 -07001193 // Draw all the children, leaving the drag view for last
1194 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001195 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001196 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001197 if (mForceDrawAllChildrenNextFrame ||
1198 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001199 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001200 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001201 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001202 // Draw the drag view on top (if there is one)
1203 if (mDragView != null) {
1204 drawChild(canvas, mDragView, drawingTime);
1205 }
1206
Michael Jurka5e368ff2012-05-14 23:13:15 -07001207 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001208 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001209 }
Michael Jurka0142d492010-08-25 17:46:15 -07001210 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 }
1212
1213 @Override
1214 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001215 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001216 if (page != mCurrentPage || !mScroller.isFinished()) {
1217 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 return true;
1219 }
1220 return false;
1221 }
1222
1223 @Override
1224 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001225 int focusablePage;
1226 if (mNextPage != INVALID_PAGE) {
1227 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001229 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 }
Winson Chung86f77532010-08-24 11:08:22 -07001231 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001233 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235 return false;
1236 }
1237
1238 @Override
1239 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001240 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001242 if (getCurrentPage() > 0) {
1243 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 return true;
1245 }
1246 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001247 if (getCurrentPage() < getPageCount() - 1) {
1248 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 return true;
1250 }
1251 }
1252 return super.dispatchUnhandledMove(focused, direction);
1253 }
1254
1255 @Override
1256 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001257 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001258 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001259 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 }
1261 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001262 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001263 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 }
1265 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001266 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001267 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 }
1269 }
1270 }
1271
1272 /**
1273 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001274 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 *
Winson Chung86f77532010-08-24 11:08:22 -07001276 * This happens when live folders requery, and if they're off page, they
1277 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 */
1279 @Override
1280 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001281 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 View v = focused;
1283 while (true) {
1284 if (v == current) {
1285 super.focusableViewAvailable(focused);
1286 return;
1287 }
1288 if (v == this) {
1289 return;
1290 }
1291 ViewParent parent = v.getParent();
1292 if (parent instanceof View) {
1293 v = (View)v.getParent();
1294 } else {
1295 return;
1296 }
1297 }
1298 }
1299
1300 /**
1301 * {@inheritDoc}
1302 */
1303 @Override
1304 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1305 if (disallowIntercept) {
1306 // We need to make sure to cancel our long press if
1307 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001308 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001309 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 }
1311 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1312 }
1313
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001314 /**
1315 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1316 */
1317 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001318 if (isLayoutRtl()) {
1319 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001320 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001321 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001322 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001323 }
1324
1325 /**
1326 * Return true if a tap at (x, y) should trigger a flip to the next page.
1327 */
1328 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001329 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001330 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001331 }
1332 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001333 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001334 }
Winson Chung52aee602013-01-30 12:01:02 -08001335
Adam Cohen7d30a372013-07-01 17:03:59 -07001336 /** Returns whether x and y originated within the buffered viewport */
1337 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1338 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1339 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1340 return mTmpRect.contains(x, y);
1341 }
1342
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 @Override
1344 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001345 if (DISABLE_TOUCH_INTERACTION) {
1346 return false;
1347 }
1348
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 /*
1350 * This method JUST determines whether we want to intercept the motion.
1351 * If we return true, onTouchEvent will be called and we do the actual
1352 * scrolling there.
1353 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001354 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001355
Winson Chung45e1d6e2010-11-09 17:19:49 -08001356 // Skip touch handling if there are no pages to swipe
1357 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1358
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 /*
1360 * Shortcut the most recurring case: the user is in the dragging
1361 * state and he is moving his finger. We want to intercept this
1362 * motion.
1363 */
1364 final int action = ev.getAction();
1365 if ((action == MotionEvent.ACTION_MOVE) &&
1366 (mTouchState == TOUCH_STATE_SCROLLING)) {
1367 return true;
1368 }
1369
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 switch (action & MotionEvent.ACTION_MASK) {
1371 case MotionEvent.ACTION_MOVE: {
1372 /*
1373 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1374 * whether the user has moved far enough from his original down touch.
1375 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001376 if (mActivePointerId != INVALID_POINTER) {
1377 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001378 }
1379 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1380 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1381 // i.e. fall through to the next case (don't break)
1382 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1383 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001384 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 }
1386
1387 case MotionEvent.ACTION_DOWN: {
1388 final float x = ev.getX();
1389 final float y = ev.getY();
1390 // Remember location of down touch
1391 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 mDownMotionY = y;
1393 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 mLastMotionX = x;
1395 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001396 float[] p = mapPointFromViewToParent(this, x, y);
1397 mParentDownMotionX = p[0];
1398 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001399 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001400 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001402
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 /*
1404 * If being flinged and user touches the screen, initiate drag;
1405 * otherwise don't. mScroller.isFinished should be false when
1406 * being flinged.
1407 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001408 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001409 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001410
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 if (finishedScrolling) {
1412 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001413 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001414 setCurrentPage(getNextPage());
1415 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001416 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001417 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001418 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1419 mTouchState = TOUCH_STATE_SCROLLING;
1420 } else {
1421 mTouchState = TOUCH_STATE_REST;
1422 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001423 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001424
Winson Chung86f77532010-08-24 11:08:22 -07001425 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001427 if (!DISABLE_TOUCH_SIDE_PAGES) {
1428 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1429 if (getChildCount() > 0) {
1430 if (hitsPreviousPage(x, y)) {
1431 mTouchState = TOUCH_STATE_PREV_PAGE;
1432 } else if (hitsNextPage(x, y)) {
1433 mTouchState = TOUCH_STATE_NEXT_PAGE;
1434 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 }
1436 }
1437 }
1438 break;
1439 }
1440
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001442 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001443 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 break;
1445
1446 case MotionEvent.ACTION_POINTER_UP:
1447 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001448 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 break;
1450 }
1451
1452 /*
1453 * The only time we want to intercept motion events is if we are in the
1454 * drag mode.
1455 */
1456 return mTouchState != TOUCH_STATE_REST;
1457 }
1458
Adam Cohenf8d28232011-02-01 21:47:00 -08001459 protected void determineScrollingStart(MotionEvent ev) {
1460 determineScrollingStart(ev, 1.0f);
1461 }
1462
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 /*
1464 * Determines if we should change the touch state to start scrolling after the
1465 * user moves their touch point too far.
1466 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001467 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001468 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001470 if (pointerIndex == -1) return;
1471
1472 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 final float x = ev.getX(pointerIndex);
1474 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001475 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1476
Winson Chung321e9ee2010-08-09 13:37:56 -07001477 final int xDiff = (int) Math.abs(x - mLastMotionX);
1478 final int yDiff = (int) Math.abs(y - mLastMotionY);
1479
Adam Cohenf8d28232011-02-01 21:47:00 -08001480 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 boolean xPaged = xDiff > mPagingTouchSlop;
1482 boolean xMoved = xDiff > touchSlop;
1483 boolean yMoved = yDiff > touchSlop;
1484
Adam Cohenf8d28232011-02-01 21:47:00 -08001485 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001486 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 // Scroll if the user moved far enough along the X axis
1488 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001489 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001491 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001492 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001493 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1494 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001496 }
1497 }
1498
Adam Cohen7d30a372013-07-01 17:03:59 -07001499 protected float getMaxScrollProgress() {
1500 return 1.0f;
1501 }
1502
Adam Cohenf8d28232011-02-01 21:47:00 -08001503 protected void cancelCurrentPageLongPress() {
1504 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001506 // Try canceling the long press. It could also have been scheduled
1507 // by a distant descendant, so use the mAllowLongPress flag to block
1508 // everything
1509 final View currentPage = getPageAt(mCurrentPage);
1510 if (currentPage != null) {
1511 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 }
1513 }
1514 }
1515
Adam Cohen7d30a372013-07-01 17:03:59 -07001516 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1517 final int halfScreenSize = getViewportWidth() / 2;
1518
1519 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1520 screenCenter = Math.max(halfScreenSize, screenCenter);
1521
1522 return getScrollProgress(screenCenter, v, page);
1523 }
1524
Adam Cohenb5ba0972011-09-07 18:02:31 -07001525 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001527
Adam Cohenedb40762013-07-18 16:45:45 -07001528 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001529 int count = getChildCount();
1530
1531 final int totalDistance;
1532
1533 int adjacentPage = page + 1;
1534 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1535 adjacentPage = page - 1;
1536 }
1537
1538 if (adjacentPage < 0 || adjacentPage > count - 1) {
1539 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1540 } else {
1541 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1542 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001543
1544 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001545 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1546 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001547 return scrollProgress;
1548 }
1549
Adam Cohenedb40762013-07-18 16:45:45 -07001550 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001551 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001552 return 0;
1553 } else {
1554 return mPageScrolls[index];
1555 }
1556 }
1557
Adam Cohen564a2e72013-10-09 14:47:32 -07001558 // While layout transitions are occurring, a child's position may stray from its baseline
1559 // position. This method returns the magnitude of this stray at any given time.
1560 public int getLayoutTransitionOffsetForPage(int index) {
1561 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1562 return 0;
1563 } else {
1564 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001565
1566 int scrollOffset = 0;
1567 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1568 if (!lp.isFullScreenPage) {
1569 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1570 }
1571
Adam Cohen564a2e72013-10-09 14:47:32 -07001572 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1573 return (int) (child.getX() - baselineX);
1574 }
1575 }
1576
Adam Cohene0f66b52010-11-23 15:06:07 -08001577 // This curve determines how the effect of scrolling over the limits of the page dimishes
1578 // as the user pulls further and further from the bounds
1579 private float overScrollInfluenceCurve(float f) {
1580 f -= 1.0f;
1581 return f * f * f + 1.0f;
1582 }
1583
Adam Cohenb5ba0972011-09-07 18:02:31 -07001584 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001585 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001586
1587 // We want to reach the max over scroll effect when the user has
1588 // over scrolled half the size of the screen
1589 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1590
1591 if (f == 0) return;
1592
1593 // Clamp this factor, f, to -1 < f < 1
1594 if (Math.abs(f) >= 1) {
1595 f /= Math.abs(f);
1596 }
1597
1598 int overScrollAmount = (int) Math.round(f * screenSize);
1599 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001600 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001601 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001602 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001603 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001604 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001605 }
1606 invalidate();
1607 }
1608
1609 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001610 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001611
1612 float f = (amount / screenSize);
1613
1614 if (f == 0) return;
1615 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1616
Adam Cohen7bfc9792011-01-28 13:52:37 -08001617 // Clamp this factor, f, to -1 < f < 1
1618 if (Math.abs(f) >= 1) {
1619 f /= Math.abs(f);
1620 }
1621
Adam Cohene0f66b52010-11-23 15:06:07 -08001622 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001623 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001624 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001625 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001626 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001627 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001628 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001629 }
1630 invalidate();
1631 }
1632
Adam Cohenb5ba0972011-09-07 18:02:31 -07001633 protected void overScroll(float amount) {
1634 dampedOverScroll(amount);
1635 }
1636
Michael Jurkac5b262c2011-01-12 20:24:50 -08001637 protected float maxOverScroll() {
1638 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001639 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001640 float f = 1.0f;
1641 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1642 return OVERSCROLL_DAMP_FACTOR * f;
1643 }
1644
Adam Cohenf358a4b2013-07-23 16:47:31 -07001645 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001646 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001647 }
1648
Adam Cohenf9618852013-11-08 06:45:03 -08001649 protected void disableFreeScroll() {
1650 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001651 }
1652
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001653 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001654 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001655 if (isLayoutRtl()) {
1656 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1657 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1658 } else {
1659 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1660 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1661 }
1662 }
1663
Adam Cohenf9618852013-11-08 06:45:03 -08001664 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001665 mFreeScroll = freeScroll;
1666
Adam Cohenf9618852013-11-08 06:45:03 -08001667 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001668 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001669 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001670 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1671 setCurrentPage(mTempVisiblePagesRange[0]);
1672 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1673 setCurrentPage(mTempVisiblePagesRange[1]);
1674 }
1675 }
1676
1677 setEnableOverscroll(!freeScroll);
1678 }
1679
1680 private void setEnableOverscroll(boolean enable) {
1681 mAllowOverScroll = enable;
1682 }
1683
1684 int getNearestHoverOverPageIndex() {
1685 if (mDragView != null) {
1686 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1687 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001688 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001689 int minDistance = Integer.MAX_VALUE;
1690 int minIndex = indexOfChild(mDragView);
1691 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1692 View page = getPageAt(i);
1693 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1694 int d = Math.abs(dragX - pageX);
1695 if (d < minDistance) {
1696 minIndex = i;
1697 minDistance = d;
1698 }
1699 }
1700 return minIndex;
1701 }
1702 return -1;
1703 }
1704
Winson Chung321e9ee2010-08-09 13:37:56 -07001705 @Override
1706 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001707 if (DISABLE_TOUCH_INTERACTION) {
1708 return false;
1709 }
1710
Adam Cohen1697b792013-09-17 19:08:21 -07001711 super.onTouchEvent(ev);
1712
Winson Chung45e1d6e2010-11-09 17:19:49 -08001713 // Skip touch handling if there are no pages to swipe
1714 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1715
Michael Jurkab8f06722010-10-10 15:58:46 -07001716 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001717
1718 final int action = ev.getAction();
1719
1720 switch (action & MotionEvent.ACTION_MASK) {
1721 case MotionEvent.ACTION_DOWN:
1722 /*
1723 * If being flinged and user touches, stop the fling. isFinished
1724 * will be false if being flinged.
1725 */
1726 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001727 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 }
1729
1730 // Remember where the motion event started
1731 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001732 mDownMotionY = mLastMotionY = ev.getY();
1733 mDownScrollX = getScrollX();
1734 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1735 mParentDownMotionX = p[0];
1736 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001737 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001738 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001739 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001740
Michael Jurka0142d492010-08-25 17:46:15 -07001741 if (mTouchState == TOUCH_STATE_SCROLLING) {
1742 pageBeginMoving();
1743 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001744 break;
1745
1746 case MotionEvent.ACTION_MOVE:
1747 if (mTouchState == TOUCH_STATE_SCROLLING) {
1748 // Scroll to follow the motion event
1749 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001750
1751 if (pointerIndex == -1) return true;
1752
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001754 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001755
Adam Cohenaefd4e12011-02-14 16:39:38 -08001756 mTotalMotionX += Math.abs(deltaX);
1757
Winson Chungc0844aa2011-02-02 15:25:58 -08001758 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1759 // keep the remainder because we are actually testing if we've moved from the last
1760 // scrolled position (which is discrete).
1761 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001762 mTouchX += deltaX;
1763 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1764 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001765 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001766 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001767 } else {
1768 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001769 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001770 mLastMotionX = x;
1771 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001772 } else {
1773 awakenScrollBars();
1774 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001775 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1776 // Update the last motion position
1777 mLastMotionX = ev.getX();
1778 mLastMotionY = ev.getY();
1779
1780 // Update the parent down so that our zoom animations take this new movement into
1781 // account
1782 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1783 mParentDownMotionX = pt[0];
1784 mParentDownMotionY = pt[1];
1785 updateDragViewTranslationDuringDrag();
1786
1787 // Find the closest page to the touch point
1788 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001789
1790 // Change the drag view if we are hovering over the drop target
1791 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1792 (int) mParentDownMotionX, (int) mParentDownMotionY);
1793 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1794
Adam Cohen7d30a372013-07-01 17:03:59 -07001795 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1796 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1797 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1798 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1799
Adam Cohenf358a4b2013-07-23 16:47:31 -07001800 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1801 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1802 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001803 mTempVisiblePagesRange[0] = 0;
1804 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001805 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001806 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1807 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1808 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1809 mSidePageHoverIndex = pageUnderPointIndex;
1810 mSidePageHoverRunnable = new Runnable() {
1811 @Override
1812 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001813 // Setup the scroll to the correct page before we swap the views
1814 snapToPage(pageUnderPointIndex);
1815
1816 // For each of the pages between the paged view and the drag view,
1817 // animate them from the previous position to the new position in
1818 // the layout (as a result of the drag view moving in the layout)
1819 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1820 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1821 dragViewIndex + 1 : pageUnderPointIndex;
1822 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1823 dragViewIndex - 1 : pageUnderPointIndex;
1824 for (int i = lowerIndex; i <= upperIndex; ++i) {
1825 View v = getChildAt(i);
1826 // dragViewIndex < pageUnderPointIndex, so after we remove the
1827 // drag view all subsequent views to pageUnderPointIndex will
1828 // shift down.
1829 int oldX = getViewportOffsetX() + getChildOffset(i);
1830 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1831
1832 // Animate the view translation from its old position to its new
1833 // position
1834 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1835 if (anim != null) {
1836 anim.cancel();
1837 }
1838
1839 v.setTranslationX(oldX - newX);
1840 anim = new AnimatorSet();
1841 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1842 anim.playTogether(
1843 ObjectAnimator.ofFloat(v, "translationX", 0f));
1844 anim.start();
1845 v.setTag(anim);
1846 }
1847
1848 removeView(mDragView);
1849 onRemoveView(mDragView, false);
1850 addView(mDragView, pageUnderPointIndex);
1851 onAddView(mDragView, pageUnderPointIndex);
1852 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001853 if (mPageIndicator != null) {
1854 mPageIndicator.setActiveMarker(getNextPage());
1855 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001856 }
1857 };
1858 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1859 }
1860 } else {
1861 removeCallbacks(mSidePageHoverRunnable);
1862 mSidePageHoverIndex = -1;
1863 }
Adam Cohen564976a2010-10-13 18:52:07 -07001864 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001865 determineScrollingStart(ev);
1866 }
1867 break;
1868
1869 case MotionEvent.ACTION_UP:
1870 if (mTouchState == TOUCH_STATE_SCROLLING) {
1871 final int activePointerId = mActivePointerId;
1872 final int pointerIndex = ev.findPointerIndex(activePointerId);
1873 final float x = ev.getX(pointerIndex);
1874 final VelocityTracker velocityTracker = mVelocityTracker;
1875 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1876 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001877 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001878 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001879 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1880 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001881
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001882 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1883
Adam Cohen00481b32011-11-18 12:03:48 -08001884 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001885 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001886
Adam Cohenf358a4b2013-07-23 16:47:31 -07001887 if (!mFreeScroll) {
1888 // In the case that the page is moved far to one direction and then is flung
1889 // in the opposite direction, we use a threshold to determine whether we should
1890 // just return to the starting page, or if we should skip one further.
1891 boolean returnToOriginalPage = false;
1892 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1893 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1894 returnToOriginalPage = true;
1895 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001896
Adam Cohenf358a4b2013-07-23 16:47:31 -07001897 int finalPage;
1898 // We give flings precedence over large moves, which is why we short-circuit our
1899 // test for a large move if a fling has been registered. That is, a large
1900 // move to the left and fling to the right will register as a fling to the right.
1901 final boolean isRtl = isLayoutRtl();
1902 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1903 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1904 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1905 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1906 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1907 snapToPageWithVelocity(finalPage, velocityX);
1908 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1909 (isFling && isVelocityXLeft)) &&
1910 mCurrentPage < getChildCount() - 1) {
1911 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1912 snapToPageWithVelocity(finalPage, velocityX);
1913 } else {
1914 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001915 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001916 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001917 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001918 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001919 }
1920
1921 float scaleX = getScaleX();
1922 int vX = (int) (-velocityX * scaleX);
1923 int initialScrollX = (int) (getScrollX() * scaleX);
1924
Adam Cohenf9618852013-11-08 06:45:03 -08001925 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001926 mScroller.fling(initialScrollX,
1927 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1928 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001929 }
Adam Cohencae7f572013-11-04 14:42:52 -08001930 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1931 // at this point we have not moved beyond the touch slop
1932 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1933 // we can just page
1934 int nextPage = Math.max(0, mCurrentPage - 1);
1935 if (nextPage != mCurrentPage) {
1936 snapToPage(nextPage);
1937 } else {
1938 snapToDestination();
1939 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001940 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 // at this point we have not moved beyond the touch slop
1942 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1943 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001944 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1945 if (nextPage != mCurrentPage) {
1946 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001947 } else {
1948 snapToDestination();
1949 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001950 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1951 // Update the last motion position
1952 mLastMotionX = ev.getX();
1953 mLastMotionY = ev.getY();
1954
1955 // Update the parent down so that our zoom animations take this new movement into
1956 // account
1957 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1958 mParentDownMotionX = pt[0];
1959 mParentDownMotionY = pt[1];
1960 updateDragViewTranslationDuringDrag();
1961 boolean handledFling = false;
1962 if (!DISABLE_FLING_TO_DELETE) {
1963 // Check the velocity and see if we are flinging-to-delete
1964 PointF flingToDeleteVector = isFlingingToDelete();
1965 if (flingToDeleteVector != null) {
1966 onFlingToDelete(flingToDeleteVector);
1967 handledFling = true;
1968 }
1969 }
1970 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1971 (int) mParentDownMotionY)) {
1972 onDropToDelete();
1973 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001974 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001975 if (!mCancelTap) {
1976 onUnhandledTap(ev);
1977 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001979
1980 // Remove the callback to wait for the side page hover timeout
1981 removeCallbacks(mSidePageHoverRunnable);
1982 // End any intermediate reordering states
1983 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 break;
1985
1986 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001987 if (mTouchState == TOUCH_STATE_SCROLLING) {
1988 snapToDestination();
1989 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001990 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001991 break;
1992
1993 case MotionEvent.ACTION_POINTER_UP:
1994 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001995 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001996 break;
1997 }
1998
1999 return true;
2000 }
2001
Adam Cohen7d30a372013-07-01 17:03:59 -07002002 public void onFlingToDelete(View v) {}
2003 public void onRemoveView(View v, boolean deletePermanently) {}
2004 public void onRemoveViewAnimationCompleted() {}
2005 public void onAddView(View v, int index) {}
2006
2007 private void resetTouchState() {
2008 releaseVelocityTracker();
2009 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002010 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002011 mTouchState = TOUCH_STATE_REST;
2012 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002013 }
2014
Adam Cohen1697b792013-09-17 19:08:21 -07002015 protected void onUnhandledTap(MotionEvent ev) {
2016 ((Launcher) getContext()).onClick(this);
2017 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002018
Winson Chung185d7162011-02-28 13:47:29 -08002019 @Override
2020 public boolean onGenericMotionEvent(MotionEvent event) {
2021 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2022 switch (event.getAction()) {
2023 case MotionEvent.ACTION_SCROLL: {
2024 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2025 final float vscroll;
2026 final float hscroll;
2027 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2028 vscroll = 0;
2029 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2030 } else {
2031 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2032 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2033 }
2034 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002035 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2036 : (hscroll > 0 || vscroll > 0);
2037 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002038 scrollRight();
2039 } else {
2040 scrollLeft();
2041 }
2042 return true;
2043 }
2044 }
2045 }
2046 }
2047 return super.onGenericMotionEvent(event);
2048 }
2049
Michael Jurkab8f06722010-10-10 15:58:46 -07002050 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2051 if (mVelocityTracker == null) {
2052 mVelocityTracker = VelocityTracker.obtain();
2053 }
2054 mVelocityTracker.addMovement(ev);
2055 }
2056
2057 private void releaseVelocityTracker() {
2058 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002059 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002060 mVelocityTracker.recycle();
2061 mVelocityTracker = null;
2062 }
2063 }
2064
Winson Chung321e9ee2010-08-09 13:37:56 -07002065 private void onSecondaryPointerUp(MotionEvent ev) {
2066 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2067 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2068 final int pointerId = ev.getPointerId(pointerIndex);
2069 if (pointerId == mActivePointerId) {
2070 // This was our active pointer going up. Choose a new
2071 // active pointer and adjust accordingly.
2072 // TODO: Make this decision more intelligent.
2073 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2074 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2075 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002076 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002077 mActivePointerId = ev.getPointerId(newPointerIndex);
2078 if (mVelocityTracker != null) {
2079 mVelocityTracker.clear();
2080 }
2081 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002082 }
2083
Winson Chung321e9ee2010-08-09 13:37:56 -07002084 @Override
2085 public void requestChildFocus(View child, View focused) {
2086 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002087 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002088 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002089 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002090 }
2091 }
2092
Winson Chung1908d072011-02-24 18:09:44 -08002093 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002094 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002095 }
2096
Adam Cohen7d30a372013-07-01 17:03:59 -07002097 int getPageNearestToPoint(float x) {
2098 int index = 0;
2099 for (int i = 0; i < getChildCount(); ++i) {
2100 if (x < getChildAt(i).getRight() - getScrollX()) {
2101 return index;
2102 } else {
2103 index++;
2104 }
2105 }
2106 return Math.min(index, getChildCount() - 1);
2107 }
2108
Adam Cohend19d3ca2010-09-15 14:43:42 -07002109 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002110 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002111 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002112 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002113 final int childCount = getChildCount();
2114 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002115 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002116 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002117 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002118 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002119 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2120 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2121 minDistanceFromScreenCenter = distanceFromScreenCenter;
2122 minDistanceFromScreenCenterIndex = i;
2123 }
2124 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002125 return minDistanceFromScreenCenterIndex;
2126 }
2127
2128 protected void snapToDestination() {
2129 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002130 }
2131
Adam Cohene0f66b52010-11-23 15:06:07 -08002132 private static class ScrollInterpolator implements Interpolator {
2133 public ScrollInterpolator() {
2134 }
2135
2136 public float getInterpolation(float t) {
2137 t -= 1.0f;
2138 return t*t*t*t*t + 1;
2139 }
2140 }
2141
2142 // We want the duration of the page snap animation to be influenced by the distance that
2143 // the screen has to travel, however, we don't want this duration to be effected in a
2144 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2145 // of travel has on the overall snap duration.
2146 float distanceInfluenceForSnapDuration(float f) {
2147 f -= 0.5f; // center the values about 0.
2148 f *= 0.3f * Math.PI / 2.0f;
2149 return (float) Math.sin(f);
2150 }
2151
Michael Jurka0142d492010-08-25 17:46:15 -07002152 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002153 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002154 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002155
Adam Cohenedb40762013-07-18 16:45:45 -07002156 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002157 int delta = newX - mUnboundedScrollX;
2158 int duration = 0;
2159
Adam Cohen265b9a62011-12-07 14:37:18 -08002160 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002161 // If the velocity is low enough, then treat this more as an automatic page advance
2162 // as opposed to an apparent physical response to flinging
2163 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2164 return;
2165 }
2166
2167 // Here we compute a "distance" that will be used in the computation of the overall
2168 // snap duration. This is a function of the actual distance that needs to be traveled;
2169 // we keep this value close to half screen size in order to reduce the variance in snap
2170 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002171 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002172 float distance = halfScreenSize + halfScreenSize *
2173 distanceInfluenceForSnapDuration(distanceRatio);
2174
2175 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002176 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002177
2178 // we want the page's snap velocity to approximately match the velocity at which the
2179 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002180 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2181 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002182
2183 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002184 }
2185
2186 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002187 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002188 }
2189
Adam Cohen7d30a372013-07-01 17:03:59 -07002190 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002191 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002192 }
2193
Michael Jurka0142d492010-08-25 17:46:15 -07002194 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002195 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002196 }
2197
Adam Cohenf9618852013-11-08 06:45:03 -08002198 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2199 snapToPage(whichPage, duration, false, interpolator);
2200 }
2201
2202 protected void snapToPage(int whichPage, int duration, boolean immediate,
2203 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002204 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002205
Adam Cohenedb40762013-07-18 16:45:45 -07002206 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002207 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002208 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002209 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002210 }
2211
2212 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002213 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002214 }
Michael Jurka0142d492010-08-25 17:46:15 -07002215
Adam Cohenf9618852013-11-08 06:45:03 -08002216 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2217 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002218 whichPage = validateNewPage(whichPage);
2219
Adam Cohen7d30a372013-07-01 17:03:59 -07002220 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002221 View focusedChild = getFocusedChild();
2222 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002223 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002224 focusedChild.clearFocus();
2225 }
2226
Adam Cohen53805212013-10-01 10:39:23 -07002227 sendScrollAccessibilityEvent();
2228
Michael Jurka0142d492010-08-25 17:46:15 -07002229 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002230 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002231 if (immediate) {
2232 duration = 0;
2233 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002234 duration = Math.abs(delta);
2235 }
2236
Adam Cohenf358a4b2013-07-23 16:47:31 -07002237 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002238 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002239 }
Adam Cohenf9618852013-11-08 06:45:03 -08002240
2241 if (interpolator != null) {
2242 mScroller.setInterpolator(interpolator);
2243 } else {
2244 mScroller.setInterpolator(mDefaultInterpolator);
2245 }
2246
Adam Cohen68d73932010-11-15 10:50:58 -08002247 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002248
Adam Cohen674531f2013-12-13 15:07:14 -08002249 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002250
2251 // Trigger a compute() to finish switching pages if necessary
2252 if (immediate) {
2253 computeScroll();
2254 }
2255
Winson Chung9c0565f2013-07-19 13:49:06 -07002256 // Defer loading associated pages until the scroll settles
2257 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2258
Adam Cohen7d30a372013-07-01 17:03:59 -07002259 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002260 invalidate();
2261 }
2262
Winson Chung321e9ee2010-08-09 13:37:56 -07002263 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002264 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002265 }
2266
2267 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002268 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002269 }
2270
Winson Chung86f77532010-08-24 11:08:22 -07002271 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002272 int result = -1;
2273 if (v != null) {
2274 ViewParent vp = v.getParent();
2275 int count = getChildCount();
2276 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002277 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002278 return i;
2279 }
2280 }
2281 }
2282 return result;
2283 }
2284
2285 /**
2286 * @return True is long presses are still allowed for the current touch
2287 */
2288 public boolean allowLongPress() {
2289 return mAllowLongPress;
2290 }
2291
Adam Cohendbdff6b2013-09-18 19:09:15 -07002292 @Override
2293 public boolean performLongClick() {
2294 mCancelTap = true;
2295 return super.performLongClick();
2296 }
2297
Michael Jurka0142d492010-08-25 17:46:15 -07002298 /**
2299 * Set true to allow long-press events to be triggered, usually checked by
2300 * {@link Launcher} to accept or block dpad-initiated long-presses.
2301 */
2302 public void setAllowLongPress(boolean allowLongPress) {
2303 mAllowLongPress = allowLongPress;
2304 }
2305
Winson Chung321e9ee2010-08-09 13:37:56 -07002306 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002307 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002308
2309 SavedState(Parcelable superState) {
2310 super(superState);
2311 }
2312
2313 private SavedState(Parcel in) {
2314 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002315 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002316 }
2317
2318 @Override
2319 public void writeToParcel(Parcel out, int flags) {
2320 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002321 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002322 }
2323
2324 public static final Parcelable.Creator<SavedState> CREATOR =
2325 new Parcelable.Creator<SavedState>() {
2326 public SavedState createFromParcel(Parcel in) {
2327 return new SavedState(in);
2328 }
2329
2330 public SavedState[] newArray(int size) {
2331 return new SavedState[size];
2332 }
2333 };
2334 }
2335
Winson Chungf314b0e2011-08-16 11:54:27 -07002336 protected void loadAssociatedPages(int page) {
2337 loadAssociatedPages(page, false);
2338 }
2339 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002340 if (mContentIsRefreshable) {
2341 final int count = getChildCount();
2342 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002343 int lowerPageBound = getAssociatedLowerPageBound(page);
2344 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002345 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2346 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002347 // First, clear any pages that should no longer be loaded
2348 for (int i = 0; i < count; ++i) {
2349 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002350 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002351 if (layout.getPageChildCount() > 0) {
2352 layout.removeAllViewsOnPage();
2353 }
2354 mDirtyPageContent.set(i, true);
2355 }
2356 }
2357 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002358 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002359 if ((i != page) && immediateAndOnly) {
2360 continue;
2361 }
Michael Jurka0142d492010-08-25 17:46:15 -07002362 if (lowerPageBound <= i && i <= upperPageBound) {
2363 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002364 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002365 mDirtyPageContent.set(i, false);
2366 }
Winson Chung86f77532010-08-24 11:08:22 -07002367 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002368 }
2369 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002370 }
2371 }
2372
Winson Chunge3193b92010-09-10 11:44:42 -07002373 protected int getAssociatedLowerPageBound(int page) {
2374 return Math.max(0, page - 1);
2375 }
2376 protected int getAssociatedUpperPageBound(int page) {
2377 final int count = getChildCount();
2378 return Math.min(page + 1, count - 1);
2379 }
2380
Winson Chung86f77532010-08-24 11:08:22 -07002381 /**
2382 * This method is called ONLY to synchronize the number of pages that the paged view has.
2383 * To actually fill the pages with information, implement syncPageItems() below. It is
2384 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2385 * and therefore, individual page items do not need to be updated in this method.
2386 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002387 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002388
2389 /**
2390 * This method is called to synchronize the items that are on a particular page. If views on
2391 * the page can be reused, then they should be updated within this method.
2392 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002393 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002394
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002395 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002396 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002397 }
2398 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002399 invalidatePageData(currentPage, false);
2400 }
2401 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002402 if (!mIsDataReady) {
2403 return;
2404 }
2405
Michael Jurka0142d492010-08-25 17:46:15 -07002406 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002407 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002408 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002409
Michael Jurka0142d492010-08-25 17:46:15 -07002410 // Update all the pages
2411 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002412
Winson Chung5a808352011-06-27 19:08:49 -07002413 // We must force a measure after we've loaded the pages to update the content width and
2414 // to determine the full scroll width
2415 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2416 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2417
2418 // Set a new page as the current page if necessary
2419 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002420 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002421 }
2422
Michael Jurka0142d492010-08-25 17:46:15 -07002423 // Mark each of the pages as dirty
2424 final int count = getChildCount();
2425 mDirtyPageContent.clear();
2426 for (int i = 0; i < count; ++i) {
2427 mDirtyPageContent.add(true);
2428 }
2429
2430 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002431 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002432 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002433 }
Adam Cohen06559042013-09-29 18:30:56 -07002434 if (isPageMoving()) {
2435 // If the page is moving, then snap it to the final position to ensure we don't get
2436 // stuck between pages
2437 snapToDestination();
2438 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002439 }
Winson Chung007c6982011-06-14 13:27:53 -07002440
Adam Cohen7d30a372013-07-01 17:03:59 -07002441 // Animate the drag view back to the original position
2442 void animateDragViewToOriginalPosition() {
2443 if (mDragView != null) {
2444 AnimatorSet anim = new AnimatorSet();
2445 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2446 anim.playTogether(
2447 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002448 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2449 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2450 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002451 anim.addListener(new AnimatorListenerAdapter() {
2452 @Override
2453 public void onAnimationEnd(Animator animation) {
2454 onPostReorderingAnimationCompleted();
2455 }
2456 });
2457 anim.start();
2458 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002459 }
2460
Adam Cohen7d30a372013-07-01 17:03:59 -07002461 protected void onStartReordering() {
2462 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2463 mTouchState = TOUCH_STATE_REORDERING;
2464 mIsReordering = true;
2465
Adam Cohen7d30a372013-07-01 17:03:59 -07002466 // We must invalidate to trigger a redraw to update the layers such that the drag view
2467 // is always drawn on top
2468 invalidate();
2469 }
2470
2471 private void onPostReorderingAnimationCompleted() {
2472 // Trigger the callback when reordering has settled
2473 --mPostReorderingPreZoomInRemainingAnimationCount;
2474 if (mPostReorderingPreZoomInRunnable != null &&
2475 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2476 mPostReorderingPreZoomInRunnable.run();
2477 mPostReorderingPreZoomInRunnable = null;
2478 }
2479 }
2480
2481 protected void onEndReordering() {
2482 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002483 }
2484
Adam Cohenf358a4b2013-07-23 16:47:31 -07002485 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002486 int dragViewIndex = indexOfChild(v);
2487
Adam Cohen327acfe2014-06-06 11:52:52 -07002488 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002489
Adam Cohen7d30a372013-07-01 17:03:59 -07002490 mTempVisiblePagesRange[0] = 0;
2491 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002492 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002493 mReorderingStarted = true;
2494
2495 // Check if we are within the reordering range
2496 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 dragViewIndex <= mTempVisiblePagesRange[1]) {
2498 // Find the drag view under the pointer
2499 mDragView = getChildAt(dragViewIndex);
2500 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2501 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002502 snapToPage(getPageNearestToCenterOfScreen());
2503 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002504 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002505 return true;
2506 }
2507 return false;
2508 }
2509
2510 boolean isReordering(boolean testTouchState) {
2511 boolean state = mIsReordering;
2512 if (testTouchState) {
2513 state &= (mTouchState == TOUCH_STATE_REORDERING);
2514 }
2515 return state;
2516 }
2517 void endReordering() {
2518 // For simplicity, we call endReordering sometimes even if reordering was never started.
2519 // In that case, we don't want to do anything.
2520 if (!mReorderingStarted) return;
2521 mReorderingStarted = false;
2522
2523 // If we haven't flung-to-delete the current child, then we just animate the drag view
2524 // back into position
2525 final Runnable onCompleteRunnable = new Runnable() {
2526 @Override
2527 public void run() {
2528 onEndReordering();
2529 }
2530 };
2531 if (!mDeferringForDelete) {
2532 mPostReorderingPreZoomInRunnable = new Runnable() {
2533 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 onCompleteRunnable.run();
2535 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002536 };
2537 };
2538
2539 mPostReorderingPreZoomInRemainingAnimationCount =
2540 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2541 // Snap to the current page
2542 snapToPage(indexOfChild(mDragView), 0);
2543 // Animate the drag view back to the front position
2544 animateDragViewToOriginalPosition();
2545 } else {
2546 // Handled in post-delete-animation-callbacks
2547 }
2548 }
2549
Adam Cohen7d30a372013-07-01 17:03:59 -07002550 /*
2551 * Flinging to delete - IN PROGRESS
2552 */
2553 private PointF isFlingingToDelete() {
2554 ViewConfiguration config = ViewConfiguration.get(getContext());
2555 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2556
2557 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2558 // Do a quick dot product test to ensure that we are flinging upwards
2559 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2560 mVelocityTracker.getYVelocity());
2561 PointF upVec = new PointF(0f, -1f);
2562 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2563 (vel.length() * upVec.length()));
2564 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2565 return vel;
2566 }
2567 }
2568 return null;
2569 }
2570
2571 /**
2572 * Creates an animation from the current drag view along its current velocity vector.
2573 * For this animation, the alpha runs for a fixed duration and we update the position
2574 * progressively.
2575 */
2576 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2577 private View mDragView;
2578 private PointF mVelocity;
2579 private Rect mFrom;
2580 private long mPrevTime;
2581 private float mFriction;
2582
2583 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2584
2585 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2586 long startTime, float friction) {
2587 mDragView = dragView;
2588 mVelocity = vel;
2589 mFrom = from;
2590 mPrevTime = startTime;
2591 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2592 }
2593
2594 @Override
2595 public void onAnimationUpdate(ValueAnimator animation) {
2596 float t = ((Float) animation.getAnimatedValue()).floatValue();
2597 long curTime = AnimationUtils.currentAnimationTimeMillis();
2598
2599 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2600 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2601
2602 mDragView.setTranslationX(mFrom.left);
2603 mDragView.setTranslationY(mFrom.top);
2604 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2605
2606 mVelocity.x *= mFriction;
2607 mVelocity.y *= mFriction;
2608 mPrevTime = curTime;
2609 }
2610 };
2611
2612 private static final int ANIM_TAG_KEY = 100;
2613
2614 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2615 return new Runnable() {
2616 @Override
2617 public void run() {
2618 int dragViewIndex = indexOfChild(dragView);
2619
2620 // For each of the pages around the drag view, animate them from the previous
2621 // position to the new position in the layout (as a result of the drag view moving
2622 // in the layout)
2623 // NOTE: We can make an assumption here because we have side-bound pages that we
2624 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002625 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002626 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2627 boolean slideFromLeft = (isLastWidgetPage ||
2628 dragViewIndex > mTempVisiblePagesRange[0]);
2629
2630 // Setup the scroll to the correct page before we swap the views
2631 if (slideFromLeft) {
2632 snapToPageImmediately(dragViewIndex - 1);
2633 }
2634
2635 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2636 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2637 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2638 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2639 ArrayList<Animator> animations = new ArrayList<Animator>();
2640 for (int i = lowerIndex; i <= upperIndex; ++i) {
2641 View v = getChildAt(i);
2642 // dragViewIndex < pageUnderPointIndex, so after we remove the
2643 // drag view all subsequent views to pageUnderPointIndex will
2644 // shift down.
2645 int oldX = 0;
2646 int newX = 0;
2647 if (slideFromLeft) {
2648 if (i == 0) {
2649 // Simulate the page being offscreen with the page spacing
2650 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002651 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002652 } else {
2653 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2654 }
2655 newX = getViewportOffsetX() + getChildOffset(i);
2656 } else {
2657 oldX = getChildOffset(i) - getChildOffset(i - 1);
2658 newX = 0;
2659 }
2660
2661 // Animate the view translation from its old position to its new
2662 // position
2663 AnimatorSet anim = (AnimatorSet) v.getTag();
2664 if (anim != null) {
2665 anim.cancel();
2666 }
2667
2668 // Note: Hacky, but we want to skip any optimizations to not draw completely
2669 // hidden views
2670 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2671 v.setTranslationX(oldX - newX);
2672 anim = new AnimatorSet();
2673 anim.playTogether(
2674 ObjectAnimator.ofFloat(v, "translationX", 0f),
2675 ObjectAnimator.ofFloat(v, "alpha", 1f));
2676 animations.add(anim);
2677 v.setTag(ANIM_TAG_KEY, anim);
2678 }
2679
2680 AnimatorSet slideAnimations = new AnimatorSet();
2681 slideAnimations.playTogether(animations);
2682 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2683 slideAnimations.addListener(new AnimatorListenerAdapter() {
2684 @Override
2685 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002686 mDeferringForDelete = false;
2687 onEndReordering();
2688 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002689 }
2690 });
2691 slideAnimations.start();
2692
2693 removeView(dragView);
2694 onRemoveView(dragView, true);
2695 }
2696 };
2697 }
2698
2699 public void onFlingToDelete(PointF vel) {
2700 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2701
2702 // NOTE: Because it takes time for the first frame of animation to actually be
2703 // called and we expect the animation to be a continuation of the fling, we have
2704 // to account for the time that has elapsed since the fling finished. And since
2705 // we don't have a startDelay, we will always get call to update when we call
2706 // start() (which we want to ignore).
2707 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2708 private int mCount = -1;
2709 private long mStartTime;
2710 private float mOffset;
2711 /* Anonymous inner class ctor */ {
2712 mStartTime = startTime;
2713 }
2714
2715 @Override
2716 public float getInterpolation(float t) {
2717 if (mCount < 0) {
2718 mCount++;
2719 } else if (mCount == 0) {
2720 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2721 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2722 mCount++;
2723 }
2724 return Math.min(1f, mOffset + t);
2725 }
2726 };
2727
2728 final Rect from = new Rect();
2729 final View dragView = mDragView;
2730 from.left = (int) dragView.getTranslationX();
2731 from.top = (int) dragView.getTranslationY();
2732 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2733 from, startTime, FLING_TO_DELETE_FRICTION);
2734
2735 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2736
2737 // Create and start the animation
2738 ValueAnimator mDropAnim = new ValueAnimator();
2739 mDropAnim.setInterpolator(tInterpolator);
2740 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2741 mDropAnim.setFloatValues(0f, 1f);
2742 mDropAnim.addUpdateListener(updateCb);
2743 mDropAnim.addListener(new AnimatorListenerAdapter() {
2744 public void onAnimationEnd(Animator animation) {
2745 onAnimationEndRunnable.run();
2746 }
2747 });
2748 mDropAnim.start();
2749 mDeferringForDelete = true;
2750 }
2751
2752 /* Drag to delete */
2753 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2754 if (mDeleteDropTarget != null) {
2755 mAltTmpRect.set(0, 0, 0, 0);
2756 View parent = (View) mDeleteDropTarget.getParent();
2757 if (parent != null) {
2758 parent.getGlobalVisibleRect(mAltTmpRect);
2759 }
2760 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2761 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2762 return mTmpRect.contains(x, y);
2763 }
2764 return false;
2765 }
2766
2767 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2768
2769 private void onDropToDelete() {
2770 final View dragView = mDragView;
2771
2772 final float toScale = 0f;
2773 final float toAlpha = 0f;
2774
2775 // Create and start the complex animation
2776 ArrayList<Animator> animations = new ArrayList<Animator>();
2777 AnimatorSet motionAnim = new AnimatorSet();
2778 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2779 motionAnim.playTogether(
2780 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2781 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2782 animations.add(motionAnim);
2783
2784 AnimatorSet alphaAnim = new AnimatorSet();
2785 alphaAnim.setInterpolator(new LinearInterpolator());
2786 alphaAnim.playTogether(
2787 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2788 animations.add(alphaAnim);
2789
2790 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2791
2792 AnimatorSet anim = new AnimatorSet();
2793 anim.playTogether(animations);
2794 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2795 anim.addListener(new AnimatorListenerAdapter() {
2796 public void onAnimationEnd(Animator animation) {
2797 onAnimationEndRunnable.run();
2798 }
2799 });
2800 anim.start();
2801
2802 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002803 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002804
2805 /* Accessibility */
2806 @Override
2807 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2808 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002809 info.setScrollable(getPageCount() > 1);
2810 if (getCurrentPage() < getPageCount() - 1) {
2811 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2812 }
2813 if (getCurrentPage() > 0) {
2814 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2815 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002816 }
2817
2818 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002819 public void sendAccessibilityEvent(int eventType) {
2820 // Don't let the view send real scroll events.
2821 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2822 super.sendAccessibilityEvent(eventType);
2823 }
2824 }
2825
2826 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002827 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2828 super.onInitializeAccessibilityEvent(event);
2829 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002830 }
2831
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002832 @Override
2833 public boolean performAccessibilityAction(int action, Bundle arguments) {
2834 if (super.performAccessibilityAction(action, arguments)) {
2835 return true;
2836 }
2837 switch (action) {
2838 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2839 if (getCurrentPage() < getPageCount() - 1) {
2840 scrollRight();
2841 return true;
2842 }
2843 } break;
2844 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2845 if (getCurrentPage() > 0) {
2846 scrollLeft();
2847 return true;
2848 }
2849 } break;
2850 }
2851 return false;
2852 }
2853
Adam Cohen0ffac432013-07-10 11:19:26 -07002854 protected String getCurrentPageDescription() {
2855 return String.format(getContext().getString(R.string.default_scroll_format),
2856 getNextPage() + 1, getChildCount());
2857 }
2858
Winson Chungd11265e2011-08-30 13:37:23 -07002859 @Override
2860 public boolean onHoverEvent(android.view.MotionEvent event) {
2861 return true;
2862 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002863}