blob: 3702cdaf80d1b69c0730cc861e15673fb5f37287 [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;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
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;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070031import android.os.Build;
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 Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070050import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070051import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070052import java.util.ArrayList;
53
Winson Chung321e9ee2010-08-09 13:37:56 -070054/**
55 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070056 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070057 */
Michael Jurka8b805b12012-04-18 14:23:14 -070058public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070059 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070060 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Winson Chung86f77532010-08-24 11:08:22 -070063 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070064 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Adam Cohen7d30a372013-07-01 17:03:59 -070066 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070067 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080070 // The page is moved more than halfway, automatically move to the next page on touch up.
71 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Sunny Goyal8e2133b2015-05-06 13:39:07 -070073 private static final float MAX_SCROLL_PROGRESS = 1.0f;
74
Adam Cohen265b9a62011-12-07 14:37:18 -080075 // The following constants need to be scaled based on density. The scaled versions will be
76 // assigned to the corresponding member variables below.
77 private static final int FLING_THRESHOLD_VELOCITY = 500;
78 private static final int MIN_SNAP_VELOCITY = 1500;
79 private static final int MIN_FLING_VELOCITY = 250;
80
Adam Cohen21cd0022013-10-09 18:57:02 -070081 public static final int INVALID_RESTORE_PAGE = -1001;
82
Adam Cohenf358a4b2013-07-23 16:47:31 -070083 private boolean mFreeScroll = false;
84 private int mFreeScrollMinScrollX = -1;
85 private int mFreeScrollMaxScrollX = -1;
86
Adam Cohen265b9a62011-12-07 14:37:18 -080087 protected int mFlingThresholdVelocity;
88 protected int mMinFlingVelocity;
89 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070090
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070092 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070093
94 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070095 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070096 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mNextPage = INVALID_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800100 protected LauncherScroller mScroller;
101 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104
Adam Cohen7d30a372013-07-01 17:03:59 -0700105 private float mParentDownMotionX;
106 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700108 private float mDownMotionY;
109 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700110 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700111 private float mLastMotionX;
112 private float mLastMotionXRemainder;
113 private float mLastMotionY;
114 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700115 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700116
Adam Cohendbdff6b2013-09-18 19:09:15 -0700117 private boolean mCancelTap;
118
Adam Cohenedb40762013-07-18 16:45:45 -0700119 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka0142d492010-08-25 17:46:15 -0700121 protected final static int TOUCH_STATE_REST = 0;
122 protected final static int TOUCH_STATE_SCROLLING = 1;
123 protected final static int TOUCH_STATE_PREV_PAGE = 2;
124 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700125 protected final static int TOUCH_STATE_REORDERING = 4;
126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700128 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800129
Michael Jurka0142d492010-08-25 17:46:15 -0700130 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka7426c422010-11-11 15:23:47 -0800132 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133 private int mMaximumVelocity;
Michael Jurka87b14902011-05-25 22:13:09 -0700134 protected int mCellCountX = 0;
135 protected int mCellCountY = 0;
Adam Cohen68d73932010-11-15 10:50:58 -0800136 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800137 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka5f1c5092010-09-03 14:15:02 -0700139 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Winson Chung86f77532010-08-24 11:08:22 -0700143 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700146 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700147
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148 protected boolean mIsPageMoving = false;
149
Adam Cohenc2d6e892014-10-16 09:49:24 -0700150 private boolean mWasInOverscroll = false;
151
Winson Chungd2be3812013-07-16 11:11:32 -0700152 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mPageIndicatorViewId;
154 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700155 // The viewport whether the pages are to be contained (the actual view may be larger than the
156 // viewport)
157 private Rect mViewport = new Rect();
158
159 // Reordering
160 // We use the min scale to determine how much to expand the actually PagedView measured
161 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700162 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700163 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700164 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
165
Adam Cohen7d30a372013-07-01 17:03:59 -0700166 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700167 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700168 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700171 // This variable's scope is only for the duration of startReordering() and endReordering()
172 private boolean mReorderingStarted = false;
173 // This variable's scope is for the duration of startReordering() and after the zoomIn()
174 // animation after endReordering()
175 private boolean mIsReordering;
176 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700177 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700178 private int mPostReorderingPreZoomInRemainingAnimationCount;
179 private Runnable mPostReorderingPreZoomInRunnable;
180
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700182 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700183 private static final float[] sTmpPoint = new float[2];
184 private static final int[] sTmpIntPoint = new int[2];
185 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700186
John Spurlock77e1f472013-09-11 10:09:51 -0400187 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700188 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400189
Sunny Goyal4d113a52015-05-27 10:05:28 -0700190 // Edge effect
191 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
192 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
193
Winson Chung86f77532010-08-24 11:08:22 -0700194 public interface PageSwitchListener {
195 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 }
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 public PagedView(Context context) {
199 this(context, null);
200 }
201
202 public PagedView(Context context, AttributeSet attrs) {
203 this(context, attrs, 0);
204 }
205
206 public PagedView(Context context, AttributeSet attrs, int defStyle) {
207 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700208
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 TypedArray a = context.obtainStyledAttributes(attrs,
210 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700211 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 a.recycle();
213
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700215 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700216 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 }
218
219 /**
220 * Initializes various states for this workspace.
221 */
Michael Jurka0142d492010-08-25 17:46:15 -0700222 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800223 mScroller = new LauncherScroller(getContext());
224 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700225 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226
227 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800230
Sunny Goyalcf25b522015-07-09 00:01:18 -0700231 float density = getResources().getDisplayMetrics().density;
232 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
233 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
234 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700235 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700236 setWillNotDraw(false);
237 }
238
239 protected void setEdgeGlowColor(int color) {
240 mEdgeGlowLeft.setColor(color);
241 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 }
243
Adam Cohenf9618852013-11-08 06:45:03 -0800244 protected void setDefaultInterpolator(Interpolator interpolator) {
245 mDefaultInterpolator = interpolator;
246 mScroller.setInterpolator(mDefaultInterpolator);
247 }
248
Winson Chungd2be3812013-07-16 11:11:32 -0700249 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700250 super.onAttachedToWindow();
251
Winson Chungd2be3812013-07-16 11:11:32 -0700252 // Hook up the page indicator
253 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700254 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700255 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700256 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700257 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700258
Winson Chung7819a562013-09-19 15:55:45 -0700259 ArrayList<PageIndicator.PageMarkerResources> markers =
260 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700261 for (int i = 0; i < getChildCount(); ++i) {
262 markers.add(getPageIndicatorMarker(i));
263 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700265 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700266
267 OnClickListener listener = getPageIndicatorClickListener();
268 if (listener != null) {
269 mPageIndicator.setOnClickListener(listener);
270 }
Adam Cohen53805212013-10-01 10:39:23 -0700271 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700272 }
273 }
274
Adam Cohen53805212013-10-01 10:39:23 -0700275 protected String getPageIndicatorDescription() {
276 return getCurrentPageDescription();
277 }
278
279 protected OnClickListener getPageIndicatorClickListener() {
280 return null;
281 }
282
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700283 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700284 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700285 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700286 // Unhook the page indicator
287 mPageIndicator = null;
288 }
289
Adam Cohen7d30a372013-07-01 17:03:59 -0700290 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700291 private float[] mapPointFromViewToParent(View v, float x, float y) {
292 sTmpPoint[0] = x;
293 sTmpPoint[1] = y;
294 v.getMatrix().mapPoints(sTmpPoint);
295 sTmpPoint[0] += v.getLeft();
296 sTmpPoint[1] += v.getTop();
297 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700298 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700299 private float[] mapPointFromParentToView(View v, float x, float y) {
300 sTmpPoint[0] = x - v.getLeft();
301 sTmpPoint[1] = y - v.getTop();
302 v.getMatrix().invert(sTmpInvMatrix);
303 sTmpInvMatrix.mapPoints(sTmpPoint);
304 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700305 }
306
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700307 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700308 if (mDragView != null) {
309 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
310 (mDragViewBaselineLeft - mDragView.getLeft());
311 float y = mLastMotionY - mDownMotionY;
312 mDragView.setTranslationX(x);
313 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700314
Adam Cohenf358a4b2013-07-23 16:47:31 -0700315 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
316 + x + ", " + y);
317 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700318 }
319
320 public void setMinScale(float f) {
321 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700322 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700323 requestLayout();
324 }
325
326 @Override
327 public void setScaleX(float scaleX) {
328 super.setScaleX(scaleX);
329 if (isReordering(true)) {
330 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
331 mLastMotionX = p[0];
332 mLastMotionY = p[1];
333 updateDragViewTranslationDuringDrag();
334 }
335 }
336
337 // Convenience methods to get the actual width/height of the PagedView (since it is measured
338 // to be larger to account for the minimum possible scale)
339 int getViewportWidth() {
340 return mViewport.width();
341 }
342 int getViewportHeight() {
343 return mViewport.height();
344 }
345
346 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
347 // PagedView both horizontally and vertically
348 int getViewportOffsetX() {
349 return (getMeasuredWidth() - getViewportWidth()) / 2;
350 }
351
352 int getViewportOffsetY() {
353 return (getMeasuredHeight() - getViewportHeight()) / 2;
354 }
355
Adam Cohenedb40762013-07-18 16:45:45 -0700356 PageIndicator getPageIndicator() {
357 return mPageIndicator;
358 }
Winson Chung7819a562013-09-19 15:55:45 -0700359 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
360 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700361 }
Adam Cohenedb40762013-07-18 16:45:45 -0700362
Adam Cohen674531f2013-12-13 15:07:14 -0800363 /**
364 * Add a page change listener which will be called when a page is _finished_ listening.
365 *
366 */
Winson Chung86f77532010-08-24 11:08:22 -0700367 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
368 mPageSwitchListener = pageSwitchListener;
369 if (mPageSwitchListener != null) {
370 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372 }
373
374 /**
Winson Chung86f77532010-08-24 11:08:22 -0700375 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700377 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700378 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700380
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700381 /**
382 * Returns the index of page to be shown immediately afterwards.
383 */
Winson Chung360e63f2012-04-27 13:48:05 -0700384 int getNextPage() {
385 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
386 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700387
Winson Chung86f77532010-08-24 11:08:22 -0700388 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 return getChildCount();
390 }
391
Sunny Goyal83a8f042015-05-19 12:52:12 -0700392 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 return getChildAt(index);
394 }
395
Adam Cohenae4f1552011-10-20 00:15:42 -0700396 protected int indexToPage(int index) {
397 return index;
398 }
399
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800401 * Updates the scroll of the current page immediately to its final scroll position. We use this
402 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
403 * the previous tab page.
404 */
405 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700406 // If the current page is invalid, just reset the scroll position to zero
407 int newX = 0;
408 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700409 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700410 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800411 scrollTo(newX, 0);
412 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700413 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800414 }
415
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700416 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700417 mScroller.abortAnimation();
418 // We need to clean up the next page here to avoid computeScrollHelper from
419 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700420 if (resetNextPage) {
421 mNextPage = INVALID_PAGE;
422 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700423 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700424
425 private void forceFinishScroller() {
426 mScroller.forceFinished(true);
427 // We need to clean up the next page here to avoid computeScrollHelper from
428 // updating current page on the pass.
429 mNextPage = INVALID_PAGE;
430 }
431
Adam Cohen6f127a62014-06-12 14:54:41 -0700432 private int validateNewPage(int newPage) {
433 int validatedPage = newPage;
434 // When in free scroll mode, we need to clamp to the free scroll page range.
435 if (mFreeScroll) {
436 getFreeScrollPageRange(mTempVisiblePagesRange);
437 validatedPage = Math.max(mTempVisiblePagesRange[0],
438 Math.min(newPage, mTempVisiblePagesRange[1]));
439 }
440 // Ensure that it is clamped by the actual set of children in all cases
441 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
442 return validatedPage;
443 }
444
Chet Haasebc2f0822012-10-26 17:59:53 -0700445 /**
Winson Chung86f77532010-08-24 11:08:22 -0700446 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700448 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800449 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700450 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800451 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800452 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
453 // the default
454 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800455 return;
456 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700457 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700458 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700459 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700460 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800461 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 }
463
Winson Chung8c87cd82013-07-23 16:20:10 -0700464 /**
465 * The restore page will be set in place of the current page at the next (likely first)
466 * layout.
467 */
468 void setRestorePage(int restorePage) {
469 mRestorePage = restorePage;
470 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800471 int getRestorePage() {
472 return mRestorePage;
473 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700474
Adam Cohen674531f2013-12-13 15:07:14 -0800475 /**
476 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
477 * has settled.
478 */
Michael Jurka0142d492010-08-25 17:46:15 -0700479 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700480 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800481 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 }
Winson Chungd2be3812013-07-16 11:11:32 -0700483
Adam Cohen674531f2013-12-13 15:07:14 -0800484 updatePageIndicator();
485 }
486
487 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700488 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700489 if (mPageIndicator != null) {
490 mPageIndicator.setContentDescription(getPageIndicatorDescription());
491 if (!isReordering(false)) {
492 mPageIndicator.setActiveMarker(getNextPage());
493 }
Winson Chungd2be3812013-07-16 11:11:32 -0700494 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800496 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700497 if (!mIsPageMoving) {
498 mIsPageMoving = true;
499 onPageBeginMoving();
500 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700501 }
502
Michael Jurkace7e05f2011-02-01 22:02:35 -0800503 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700504 if (mIsPageMoving) {
505 mIsPageMoving = false;
506 onPageEndMoving();
507 }
Michael Jurka0142d492010-08-25 17:46:15 -0700508 }
509
Adam Cohen26976d92011-03-22 15:33:33 -0700510 protected boolean isPageMoving() {
511 return mIsPageMoving;
512 }
513
Michael Jurka0142d492010-08-25 17:46:15 -0700514 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700515 protected void onPageBeginMoving() {
516 }
517
518 // a method that subclasses can override to add behavior
519 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700520 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700521 }
522
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 /**
Winson Chung86f77532010-08-24 11:08:22 -0700524 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 *
526 * @param l The listener used to respond to long clicks.
527 */
528 @Override
529 public void setOnLongClickListener(OnLongClickListener l) {
530 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700531 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700533 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700534 }
Adam Cohen1697b792013-09-17 19:08:21 -0700535 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
537
538 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800539 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700540 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800541 }
542
543 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700544 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700545 // In free scroll mode, we clamp the scrollX
546 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700547 // If the scroller is trying to move to a location beyond the maximum allowed
548 // in the free scroll mode, we make sure to end the scroll operation.
549 if (!mScroller.isFinished() &&
550 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
551 forceFinishScroller();
552 }
553
Adam Cohenf358a4b2013-07-23 16:47:31 -0700554 x = Math.min(x, mFreeScrollMaxScrollX);
555 x = Math.max(x, mFreeScrollMinScrollX);
556 }
557
Sunny Goyal70660032015-05-14 00:07:08 -0700558 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
559 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700560 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700561 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800562 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700563 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700564 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700565 overScroll(x - mMaxScrollX);
566 } else {
567 overScroll(x);
568 }
Adam Cohen68d73932010-11-15 10:50:58 -0800569 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700570 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700571 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800572 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700573 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700574 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700575 overScroll(x);
576 } else {
577 overScroll(x - mMaxScrollX);
578 }
Adam Cohen68d73932010-11-15 10:50:58 -0800579 }
580 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700581 if (mWasInOverscroll) {
582 overScroll(0);
583 mWasInOverscroll = false;
584 }
Adam Cohen68d73932010-11-15 10:50:58 -0800585 super.scrollTo(x, y);
586 }
587
Adam Cohen7d30a372013-07-01 17:03:59 -0700588 // Update the last motion events when scrolling
589 if (isReordering(true)) {
590 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
591 mLastMotionX = p[0];
592 mLastMotionY = p[1];
593 updateDragViewTranslationDuringDrag();
594 }
Michael Jurka0142d492010-08-25 17:46:15 -0700595 }
596
Adam Cohen53805212013-10-01 10:39:23 -0700597 private void sendScrollAccessibilityEvent() {
598 AccessibilityManager am =
599 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
600 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700601 if (mCurrentPage != getNextPage()) {
602 AccessibilityEvent ev =
603 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700604 ev.setScrollable(true);
605 ev.setScrollX(getScrollX());
606 ev.setScrollY(getScrollY());
607 ev.setMaxScrollX(mMaxScrollX);
608 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700609
Vadim Tryshevf4715972015-05-08 17:21:03 -0700610 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700611 }
Adam Cohen53805212013-10-01 10:39:23 -0700612 }
613 }
614
Michael Jurka0142d492010-08-25 17:46:15 -0700615 // we moved this functionality to a helper function so SmoothPagedView can reuse it
616 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700618 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700620 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700621 float scaleX = mFreeScroll ? getScaleX() : 1f;
622 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
623 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700624 }
Michael Jurka0142d492010-08-25 17:46:15 -0700625 invalidate();
626 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700627 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700628 sendScrollAccessibilityEvent();
629
Adam Cohen6f127a62014-06-12 14:54:41 -0700630 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700631 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700632 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700633
Adam Cohen73aa9752010-11-24 16:26:58 -0800634 // We don't want to trigger a page end moving unless the page has settled
635 // and the user has stopped scrolling
636 if (mTouchState == TOUCH_STATE_REST) {
637 pageEndMoving();
638 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700639
Adam Cohen7d30a372013-07-01 17:03:59 -0700640 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700641 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700642 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700643 if (am.isEnabled()) {
644 // Notify the user when the page changes
645 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700646 }
Michael Jurka0142d492010-08-25 17:46:15 -0700647 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 }
Michael Jurka0142d492010-08-25 17:46:15 -0700649 return false;
650 }
651
652 @Override
653 public void computeScroll() {
654 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700655 }
656
Adam Cohen96d30a12013-07-16 18:13:21 -0700657 public static class LayoutParams extends ViewGroup.LayoutParams {
658 public boolean isFullScreenPage = false;
659
660 /**
661 * {@inheritDoc}
662 */
663 public LayoutParams(int width, int height) {
664 super(width, height);
665 }
666
Sunny Goyal41b22c02015-05-14 15:20:48 -0700667 public LayoutParams(Context context, AttributeSet attrs) {
668 super(context, attrs);
669 }
670
Adam Cohen96d30a12013-07-16 18:13:21 -0700671 public LayoutParams(ViewGroup.LayoutParams source) {
672 super(source);
673 }
674 }
675
Sunny Goyal41b22c02015-05-14 15:20:48 -0700676 @Override
677 public LayoutParams generateLayoutParams(AttributeSet attrs) {
678 return new LayoutParams(getContext(), attrs);
679 }
680
681 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700682 protected LayoutParams generateDefaultLayoutParams() {
683 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
684 }
685
Sunny Goyal41b22c02015-05-14 15:20:48 -0700686 @Override
687 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
688 return new LayoutParams(p);
689 }
690
691 @Override
692 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
693 return p instanceof LayoutParams;
694 }
695
Adam Cohenedb40762013-07-18 16:45:45 -0700696 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700697 LayoutParams lp = generateDefaultLayoutParams();
698 lp.isFullScreenPage = true;
699 super.addView(page, 0, lp);
700 }
701
Adam Cohen410f3cd2013-09-22 12:09:32 -0700702 public int getNormalChildHeight() {
703 return mNormalChildHeight;
704 }
705
Winson Chung321e9ee2010-08-09 13:37:56 -0700706 @Override
707 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700708 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700709 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
710 return;
711 }
712
Adam Cohen7d30a372013-07-01 17:03:59 -0700713 // We measure the dimensions of the PagedView to be larger than the pages so that when we
714 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700715 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
716 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
717 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700718 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800719 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700720 // viewport, we can be at most one and a half screens offset once we scale down
721 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700722 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
723 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700724
Winson Chungc82d2622013-11-06 13:23:29 -0800725 int parentWidthSize = (int) (2f * maxSize);
726 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700727 int scaledWidthSize, scaledHeightSize;
728 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700729 scaledWidthSize = (int) (parentWidthSize / mMinScale);
730 scaledHeightSize = (int) (parentHeightSize / mMinScale);
731 } else {
732 scaledWidthSize = widthSize;
733 scaledHeightSize = heightSize;
734 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700735 mViewport.set(0, 0, widthSize, heightSize);
736
737 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
738 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
739 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741
Winson Chung8aad6102012-05-11 16:27:49 -0700742 // Return early if we aren't given a proper dimension
743 if (widthSize <= 0 || heightSize <= 0) {
744 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
745 return;
746 }
747
Adam Lesinski6b879f02010-11-04 16:15:23 -0700748 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
749 * of the All apps view on XLarge displays to not take up more space then it needs. Width
750 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
751 * each page to have the same width.
752 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 final int verticalPadding = getPaddingTop() + getPaddingBottom();
754 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700755
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700756 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700758 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700759 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700760 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
761 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
762 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
763 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 final int childCount = getChildCount();
765 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700766 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700767 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100768 if (child.getVisibility() != GONE) {
769 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700770
Vladimir Marko2824b072013-10-04 16:42:17 +0100771 int childWidthMode;
772 int childHeightMode;
773 int childWidth;
774 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700775
Vladimir Marko2824b072013-10-04 16:42:17 +0100776 if (!lp.isFullScreenPage) {
777 if (lp.width == LayoutParams.WRAP_CONTENT) {
778 childWidthMode = MeasureSpec.AT_MOST;
779 } else {
780 childWidthMode = MeasureSpec.EXACTLY;
781 }
782
783 if (lp.height == LayoutParams.WRAP_CONTENT) {
784 childHeightMode = MeasureSpec.AT_MOST;
785 } else {
786 childHeightMode = MeasureSpec.EXACTLY;
787 }
788
Adam Cohen3b185e22013-10-29 14:45:58 -0700789 childWidth = getViewportWidth() - horizontalPadding
790 - mInsets.left - mInsets.right;
791 childHeight = getViewportHeight() - verticalPadding
792 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100793 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700794 } else {
795 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700796 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100797
Adam Cohen3b185e22013-10-29 14:45:58 -0700798 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
799 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700800 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700801 if (referenceChildWidth == 0) {
802 referenceChildWidth = childWidth;
803 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700804
Vladimir Marko2824b072013-10-04 16:42:17 +0100805 final int childWidthMeasureSpec =
806 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
807 final int childHeightMeasureSpec =
808 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
809 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700810 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700811 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700812 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800813 }
814
Sunny Goyalcf25b522015-07-09 00:01:18 -0700815 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700817 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700818 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700819 return;
820 }
821
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700824
Adam Cohen7d30a372013-07-01 17:03:59 -0700825 int offsetX = getViewportOffsetX();
826 int offsetY = getViewportOffsetY();
827
828 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700829 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700830
Sunny Goyal70660032015-05-14 00:07:08 -0700831 final int startIndex = mIsRtl ? childCount - 1 : 0;
832 final int endIndex = mIsRtl ? -1 : childCount;
833 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700834
Adam Cohen7d30a372013-07-01 17:03:59 -0700835 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700836
Adam Cohen3b185e22013-10-29 14:45:58 -0700837 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000838 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700839
840 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700841 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
842 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700843 }
844
Adam Cohen0ffac432013-07-10 11:19:26 -0700845 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700846 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700848 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100849 int childTop;
850 if (lp.isFullScreenPage) {
851 childTop = offsetY;
852 } else {
853 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700854 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100855 }
856
Adam Cohen96d30a12013-07-16 18:13:21 -0700857 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700858 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800859
Winson Chung785d2eb2011-04-14 16:08:02 -0700860 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700861 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800862 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700863
Adam Cohen3b185e22013-10-29 14:45:58 -0700864 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
865 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000866
867 int pageGap = mPageSpacing;
868 int next = i + delta;
869 if (next != endIndex) {
870 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
871 } else {
872 nextLp = null;
873 }
874
875 // Prevent full screen pages from showing in the viewport
876 // when they are not the current page.
877 if (lp.isFullScreenPage) {
878 pageGap = getPaddingLeft();
879 } else if (nextLp != null && nextLp.isFullScreenPage) {
880 pageGap = getPaddingRight();
881 }
882
Sunny Goyala1fbd842015-05-20 13:40:27 -0700883 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700886
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700887 final LayoutTransition transition = getLayoutTransition();
888 // If the transition is running defer updating max scroll, as some empty pages could
889 // still be present, and a max scroll change could cause sudden jumps in scroll.
890 if (transition != null && transition.isRunning()) {
891 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
892
893 @Override
894 public void startTransition(LayoutTransition transition, ViewGroup container,
895 View view, int transitionType) { }
896
897 @Override
898 public void endTransition(LayoutTransition transition, ViewGroup container,
899 View view, int transitionType) {
900 // Wait until all transitions are complete.
901 if (!transition.isRunning()) {
902 transition.removeTransitionListener(this);
903 updateMaxScrollX();
904 }
905 }
906 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700907 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700908 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700909 }
910
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700911 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
912 updateCurrentPageScroll();
913 mFirstLayout = false;
914 }
915
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700916 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700917 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700918 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700919 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700920 } else {
921 setCurrentPage(getNextPage());
922 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700923 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700924 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700925
926 if (isReordering(true)) {
927 updateDragViewTranslationDuringDrag();
928 }
Winson Chunge3193b92010-09-10 11:44:42 -0700929 }
930
Sunny Goyala1fbd842015-05-20 13:40:27 -0700931 protected int getChildGap() {
932 return 0;
933 }
934
Sunny Goyal316490e2015-06-02 09:38:28 -0700935 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700936 int childCount = getChildCount();
937 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700938 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700939 mMaxScrollX = getScrollForPage(index);
940 } else {
941 mMaxScrollX = 0;
942 }
943 }
944
Adam Cohen3b185e22013-10-29 14:45:58 -0700945 public void setPageSpacing(int pageSpacing) {
946 mPageSpacing = pageSpacing;
947 requestLayout();
948 }
949
Sunny Goyal4d113a52015-05-27 10:05:28 -0700950 /**
951 * Called when the center screen changes during scrolling.
952 */
953 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700954
Winson Chunge3193b92010-09-10 11:44:42 -0700955 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700956 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700957 // Update the page indicator, we don't update the page indicator as we
958 // add/remove pages
959 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700960 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700961 mPageIndicator.addMarker(pageIndex,
962 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700963 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700964 }
965
Adam Cohen2591f6a2011-10-25 14:36:40 -0700966 // This ensures that when children are added, they get the correct transforms / alphas
967 // in accordance with any scroll effects.
968 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700969 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700970 invalidate();
971 }
972
Michael Jurka8b805b12012-04-18 14:23:14 -0700973 @Override
974 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700975 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700976 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700977 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700978 }
979
Winson Chungd2be3812013-07-16 11:11:32 -0700980 private void removeMarkerForView(int index) {
981 // Update the page indicator, we don't update the page indicator as we
982 // add/remove pages
983 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700984 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700985 }
986 }
987
988 @Override
989 public void removeView(View v) {
990 // XXX: We should find a better way to hook into this before the view
991 // gets removed form its parent...
992 removeMarkerForView(indexOfChild(v));
993 super.removeView(v);
994 }
995 @Override
996 public void removeViewInLayout(View v) {
997 // XXX: We should find a better way to hook into this before the view
998 // gets removed form its parent...
999 removeMarkerForView(indexOfChild(v));
1000 super.removeViewInLayout(v);
1001 }
1002 @Override
1003 public void removeViewAt(int index) {
1004 // XXX: We should find a better way to hook into this before the view
1005 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001006 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001007 super.removeViewAt(index);
1008 }
1009 @Override
1010 public void removeAllViewsInLayout() {
1011 // Update the page indicator, we don't update the page indicator as we
1012 // add/remove pages
1013 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001014 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001015 }
1016
1017 super.removeAllViewsInLayout();
1018 }
1019
Adam Cohen73894962011-10-31 13:17:17 -07001020 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001021 if (index < 0 || index > getChildCount() - 1) return 0;
1022
Adam Cohenf698c6e2013-07-17 18:44:25 -07001023 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001024
Adam Cohenf698c6e2013-07-17 18:44:25 -07001025 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001026 }
1027
Adam Cohen6f127a62014-06-12 14:54:41 -07001028 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001029 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001030 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001031 }
1032
Michael Jurkadde558b2011-11-09 22:09:06 -08001033 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001034 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001035 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001036
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001037 range[0] = -1;
1038 range[1] = -1;
1039
Michael Jurkac4fb9172010-09-02 17:19:20 -07001040 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001041 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001042 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001043
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001044 int count = getChildCount();
1045 for (int i = 0; i < count; i++) {
1046 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001047
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001048 sTmpIntPoint[0] = 0;
1049 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1050 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001051 if (range[0] == -1) {
1052 continue;
1053 } else {
1054 break;
1055 }
1056 }
1057
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001058 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1059 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1060 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001061 if (range[0] == -1) {
1062 continue;
1063 } else {
1064 break;
1065 }
1066 }
1067 curScreen = i;
1068 if (range[0] < 0) {
1069 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001070 }
1071 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001072
1073 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001074 } else {
1075 range[0] = -1;
1076 range[1] = -1;
1077 }
1078 }
1079
Michael Jurka920d7f42012-05-14 16:29:55 -07001080 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001081 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001082 }
1083
Michael Jurkadde558b2011-11-09 22:09:06 -08001084 @Override
1085 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001086 // Find out which screens are visible; as an optimization we only call draw on them
1087 final int pageCount = getChildCount();
1088 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001089 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001090 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001091
1092 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1093 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1094 // set it for the next frame
1095 mForceScreenScrolled = false;
1096 screenScrolled(screenCenter);
1097 mLastScreenCenter = screenCenter;
1098 }
1099
Michael Jurkadde558b2011-11-09 22:09:06 -08001100 getVisiblePages(mTempVisiblePagesRange);
1101 final int leftScreen = mTempVisiblePagesRange[0];
1102 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001103 if (leftScreen != -1 && rightScreen != -1) {
1104 final long drawingTime = getDrawingTime();
1105 // Clip to the bounds
1106 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001107 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1108 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001109
Adam Cohen7d30a372013-07-01 17:03:59 -07001110 // Draw all the children, leaving the drag view for last
1111 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001112 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001113 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001114 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001115 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001116 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001117 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001118 // Draw the drag view on top (if there is one)
1119 if (mDragView != null) {
1120 drawChild(canvas, mDragView, drawingTime);
1121 }
1122
Winson Chungc6f10b92011-11-14 11:39:07 -08001123 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001124 }
Michael Jurka0142d492010-08-25 17:46:15 -07001125 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 }
1127
1128 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001129 public void draw(Canvas canvas) {
1130 super.draw(canvas);
1131 if (getPageCount() > 0) {
1132 if (!mEdgeGlowLeft.isFinished()) {
1133 final int restoreCount = canvas.save();
1134 Rect display = mViewport;
1135 canvas.translate(display.left, display.top);
1136 canvas.rotate(270);
1137
1138 getEdgeVerticalPostion(sTmpIntPoint);
1139 canvas.translate(display.top - sTmpIntPoint[1], 0);
1140 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1141 if (mEdgeGlowLeft.draw(canvas)) {
1142 postInvalidateOnAnimation();
1143 }
1144 canvas.restoreToCount(restoreCount);
1145 }
1146 if (!mEdgeGlowRight.isFinished()) {
1147 final int restoreCount = canvas.save();
1148 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001149 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001150 canvas.rotate(90);
1151
1152 getEdgeVerticalPostion(sTmpIntPoint);
1153 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1154 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1155 if (mEdgeGlowRight.draw(canvas)) {
1156 postInvalidateOnAnimation();
1157 }
1158 canvas.restoreToCount(restoreCount);
1159 }
1160 }
1161 }
1162
1163 /**
1164 * Returns the top and bottom position for the edge effect.
1165 */
1166 protected abstract void getEdgeVerticalPostion(int[] pos);
1167
1168 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001170 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001171 if (page != mCurrentPage || !mScroller.isFinished()) {
1172 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 return true;
1174 }
1175 return false;
1176 }
1177
1178 @Override
1179 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001180 int focusablePage;
1181 if (mNextPage != INVALID_PAGE) {
1182 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001184 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 }
Winson Chung86f77532010-08-24 11:08:22 -07001186 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001188 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 }
1190 return false;
1191 }
1192
1193 @Override
1194 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001195 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001197 if (getCurrentPage() > 0) {
1198 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 return true;
1200 }
1201 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001202 if (getCurrentPage() < getPageCount() - 1) {
1203 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 return true;
1205 }
1206 }
1207 return super.dispatchUnhandledMove(focused, direction);
1208 }
1209
1210 @Override
1211 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001212 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001213 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001214 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001218 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 }
1220 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001221 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001222 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224 }
1225 }
1226
1227 /**
1228 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001229 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 *
Winson Chung86f77532010-08-24 11:08:22 -07001231 * This happens when live folders requery, and if they're off page, they
1232 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 */
1234 @Override
1235 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001236 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 View v = focused;
1238 while (true) {
1239 if (v == current) {
1240 super.focusableViewAvailable(focused);
1241 return;
1242 }
1243 if (v == this) {
1244 return;
1245 }
1246 ViewParent parent = v.getParent();
1247 if (parent instanceof View) {
1248 v = (View)v.getParent();
1249 } else {
1250 return;
1251 }
1252 }
1253 }
1254
1255 /**
1256 * {@inheritDoc}
1257 */
1258 @Override
1259 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1260 if (disallowIntercept) {
1261 // We need to make sure to cancel our long press if
1262 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001263 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001264 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1267 }
1268
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001269 /**
1270 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1271 */
1272 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001273 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001274 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001275 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001276 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001277 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001278 }
1279
1280 /**
1281 * Return true if a tap at (x, y) should trigger a flip to the next page.
1282 */
1283 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001284 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001285 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001286 }
1287 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001288 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001289 }
Winson Chung52aee602013-01-30 12:01:02 -08001290
Adam Cohen7d30a372013-07-01 17:03:59 -07001291 /** Returns whether x and y originated within the buffered viewport */
1292 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001293 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001294 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001295 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001296 }
1297
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 @Override
1299 public boolean onInterceptTouchEvent(MotionEvent ev) {
1300 /*
1301 * This method JUST determines whether we want to intercept the motion.
1302 * If we return true, onTouchEvent will be called and we do the actual
1303 * scrolling there.
1304 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001305 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001306
Winson Chung45e1d6e2010-11-09 17:19:49 -08001307 // Skip touch handling if there are no pages to swipe
1308 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1309
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 /*
1311 * Shortcut the most recurring case: the user is in the dragging
1312 * state and he is moving his finger. We want to intercept this
1313 * motion.
1314 */
1315 final int action = ev.getAction();
1316 if ((action == MotionEvent.ACTION_MOVE) &&
1317 (mTouchState == TOUCH_STATE_SCROLLING)) {
1318 return true;
1319 }
1320
Winson Chung321e9ee2010-08-09 13:37:56 -07001321 switch (action & MotionEvent.ACTION_MASK) {
1322 case MotionEvent.ACTION_MOVE: {
1323 /*
1324 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1325 * whether the user has moved far enough from his original down touch.
1326 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001327 if (mActivePointerId != INVALID_POINTER) {
1328 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001329 }
1330 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1331 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1332 // i.e. fall through to the next case (don't break)
1333 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1334 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001335 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 }
1337
1338 case MotionEvent.ACTION_DOWN: {
1339 final float x = ev.getX();
1340 final float y = ev.getY();
1341 // Remember location of down touch
1342 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 mDownMotionY = y;
1344 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 mLastMotionX = x;
1346 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001347 float[] p = mapPointFromViewToParent(this, x, y);
1348 mParentDownMotionX = p[0];
1349 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001350 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001351 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001353
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 /*
1355 * If being flinged and user touches the screen, initiate drag;
1356 * otherwise don't. mScroller.isFinished should be false when
1357 * being flinged.
1358 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001359 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001360 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001361
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001362 if (finishedScrolling) {
1363 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001364 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001365 setCurrentPage(getNextPage());
1366 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001367 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001368 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001369 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1370 mTouchState = TOUCH_STATE_SCROLLING;
1371 } else {
1372 mTouchState = TOUCH_STATE_REST;
1373 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001374 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001375
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 break;
1377 }
1378
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001380 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001381 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 break;
1383
1384 case MotionEvent.ACTION_POINTER_UP:
1385 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001386 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 break;
1388 }
1389
1390 /*
1391 * The only time we want to intercept motion events is if we are in the
1392 * drag mode.
1393 */
1394 return mTouchState != TOUCH_STATE_REST;
1395 }
1396
Adam Cohenf8d28232011-02-01 21:47:00 -08001397 protected void determineScrollingStart(MotionEvent ev) {
1398 determineScrollingStart(ev, 1.0f);
1399 }
1400
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 /*
1402 * Determines if we should change the touch state to start scrolling after the
1403 * user moves their touch point too far.
1404 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001405 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001406 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001408 if (pointerIndex == -1) return;
1409
1410 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 final float x = ev.getX(pointerIndex);
1412 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001416
Adam Cohenf8d28232011-02-01 21:47:00 -08001417 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001419
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001420 if (xMoved) {
1421 // Scroll if the user moved far enough along the X axis
1422 mTouchState = TOUCH_STATE_SCROLLING;
1423 mTotalMotionX += Math.abs(mLastMotionX - x);
1424 mLastMotionX = x;
1425 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001426 onScrollInteractionBegin();
1427 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001428 }
1429 }
1430
1431 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001432 // Try canceling the long press. It could also have been scheduled
1433 // by a distant descendant, so use the mAllowLongPress flag to block
1434 // everything
1435 final View currentPage = getPageAt(mCurrentPage);
1436 if (currentPage != null) {
1437 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 }
1439 }
1440
Adam Cohenb5ba0972011-09-07 18:02:31 -07001441 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001443
Adam Cohenedb40762013-07-18 16:45:45 -07001444 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001445 int count = getChildCount();
1446
1447 final int totalDistance;
1448
1449 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001450 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001451 adjacentPage = page - 1;
1452 }
1453
1454 if (adjacentPage < 0 || adjacentPage > count - 1) {
1455 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1456 } else {
1457 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1458 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001459
1460 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001461 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1462 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001463 return scrollProgress;
1464 }
1465
Adam Cohenedb40762013-07-18 16:45:45 -07001466 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001467 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001468 return 0;
1469 } else {
1470 return mPageScrolls[index];
1471 }
1472 }
1473
Adam Cohen564a2e72013-10-09 14:47:32 -07001474 // While layout transitions are occurring, a child's position may stray from its baseline
1475 // position. This method returns the magnitude of this stray at any given time.
1476 public int getLayoutTransitionOffsetForPage(int index) {
1477 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1478 return 0;
1479 } else {
1480 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001481
1482 int scrollOffset = 0;
1483 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1484 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001485 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001486 }
1487
Adam Cohen564a2e72013-10-09 14:47:32 -07001488 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1489 return (int) (child.getX() - baselineX);
1490 }
1491 }
1492
Adam Cohenb5ba0972011-09-07 18:02:31 -07001493 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001495 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001496 if (f < 0) {
1497 mEdgeGlowLeft.onPull(-f);
1498 } else if (f > 0) {
1499 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001500 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001501 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001502 }
1503 invalidate();
1504 }
1505
Adam Cohenb5ba0972011-09-07 18:02:31 -07001506 protected void overScroll(float amount) {
1507 dampedOverScroll(amount);
1508 }
1509
Winson Chungdc61c4d2015-04-20 18:26:57 -07001510 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001511 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001512 }
1513
Winson Chungdc61c4d2015-04-20 18:26:57 -07001514 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001515 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001516 }
1517
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001518 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001519 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001520 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001521 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1522 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1523 } else {
1524 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1525 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1526 }
1527 }
1528
Adam Cohenf9618852013-11-08 06:45:03 -08001529 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001530 mFreeScroll = freeScroll;
1531
Adam Cohenf9618852013-11-08 06:45:03 -08001532 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001533 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001534 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001535 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1536 setCurrentPage(mTempVisiblePagesRange[0]);
1537 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1538 setCurrentPage(mTempVisiblePagesRange[1]);
1539 }
1540 }
1541
1542 setEnableOverscroll(!freeScroll);
1543 }
1544
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001545 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001546 mAllowOverScroll = enable;
1547 }
1548
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001549 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001550 if (mDragView != null) {
1551 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1552 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001553 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001554 int minDistance = Integer.MAX_VALUE;
1555 int minIndex = indexOfChild(mDragView);
1556 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1557 View page = getPageAt(i);
1558 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1559 int d = Math.abs(dragX - pageX);
1560 if (d < minDistance) {
1561 minIndex = i;
1562 minDistance = d;
1563 }
1564 }
1565 return minIndex;
1566 }
1567 return -1;
1568 }
1569
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 @Override
1571 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001572 super.onTouchEvent(ev);
1573
Winson Chung45e1d6e2010-11-09 17:19:49 -08001574 // Skip touch handling if there are no pages to swipe
1575 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1576
Michael Jurkab8f06722010-10-10 15:58:46 -07001577 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001578
1579 final int action = ev.getAction();
1580
1581 switch (action & MotionEvent.ACTION_MASK) {
1582 case MotionEvent.ACTION_DOWN:
1583 /*
1584 * If being flinged and user touches, stop the fling. isFinished
1585 * will be false if being flinged.
1586 */
1587 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001588 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 }
1590
1591 // Remember where the motion event started
1592 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001593 mDownMotionY = mLastMotionY = ev.getY();
1594 mDownScrollX = getScrollX();
1595 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1596 mParentDownMotionX = p[0];
1597 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001598 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001599 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001601
Michael Jurka0142d492010-08-25 17:46:15 -07001602 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001603 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001604 pageBeginMoving();
1605 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001606 break;
1607
1608 case MotionEvent.ACTION_MOVE:
1609 if (mTouchState == TOUCH_STATE_SCROLLING) {
1610 // Scroll to follow the motion event
1611 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001612
1613 if (pointerIndex == -1) return true;
1614
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001616 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001617
Adam Cohenaefd4e12011-02-14 16:39:38 -08001618 mTotalMotionX += Math.abs(deltaX);
1619
Winson Chungc0844aa2011-02-02 15:25:58 -08001620 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1621 // keep the remainder because we are actually testing if we've moved from the last
1622 // scrolled position (which is discrete).
1623 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001624 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001625 mLastMotionX = x;
1626 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001627 } else {
1628 awakenScrollBars();
1629 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001630 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1631 // Update the last motion position
1632 mLastMotionX = ev.getX();
1633 mLastMotionY = ev.getY();
1634
1635 // Update the parent down so that our zoom animations take this new movement into
1636 // account
1637 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1638 mParentDownMotionX = pt[0];
1639 mParentDownMotionY = pt[1];
1640 updateDragViewTranslationDuringDrag();
1641
1642 // Find the closest page to the touch point
1643 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001644
Adam Cohen7d30a372013-07-01 17:03:59 -07001645 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1646 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1647 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1648 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1649
Adam Cohenf358a4b2013-07-23 16:47:31 -07001650 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001651 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001652 mTempVisiblePagesRange[0] = 0;
1653 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001654 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001655 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1656 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1657 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1658 mSidePageHoverIndex = pageUnderPointIndex;
1659 mSidePageHoverRunnable = new Runnable() {
1660 @Override
1661 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001662 // Setup the scroll to the correct page before we swap the views
1663 snapToPage(pageUnderPointIndex);
1664
1665 // For each of the pages between the paged view and the drag view,
1666 // animate them from the previous position to the new position in
1667 // the layout (as a result of the drag view moving in the layout)
1668 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1669 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1670 dragViewIndex + 1 : pageUnderPointIndex;
1671 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1672 dragViewIndex - 1 : pageUnderPointIndex;
1673 for (int i = lowerIndex; i <= upperIndex; ++i) {
1674 View v = getChildAt(i);
1675 // dragViewIndex < pageUnderPointIndex, so after we remove the
1676 // drag view all subsequent views to pageUnderPointIndex will
1677 // shift down.
1678 int oldX = getViewportOffsetX() + getChildOffset(i);
1679 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1680
1681 // Animate the view translation from its old position to its new
1682 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001683 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001684 if (anim != null) {
1685 anim.cancel();
1686 }
1687
1688 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001689 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001690 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001691 anim.start();
1692 v.setTag(anim);
1693 }
1694
1695 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001696 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001697 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001698 if (mPageIndicator != null) {
1699 mPageIndicator.setActiveMarker(getNextPage());
1700 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001701 }
1702 };
1703 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1704 }
1705 } else {
1706 removeCallbacks(mSidePageHoverRunnable);
1707 mSidePageHoverIndex = -1;
1708 }
Adam Cohen564976a2010-10-13 18:52:07 -07001709 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001710 determineScrollingStart(ev);
1711 }
1712 break;
1713
1714 case MotionEvent.ACTION_UP:
1715 if (mTouchState == TOUCH_STATE_SCROLLING) {
1716 final int activePointerId = mActivePointerId;
1717 final int pointerIndex = ev.findPointerIndex(activePointerId);
1718 final float x = ev.getX(pointerIndex);
1719 final VelocityTracker velocityTracker = mVelocityTracker;
1720 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1721 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001722 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001723 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001724 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1725 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001726
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001727 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1728
Adam Cohen00481b32011-11-18 12:03:48 -08001729 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001730 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001731
Adam Cohenf358a4b2013-07-23 16:47:31 -07001732 if (!mFreeScroll) {
1733 // In the case that the page is moved far to one direction and then is flung
1734 // in the opposite direction, we use a threshold to determine whether we should
1735 // just return to the starting page, or if we should skip one further.
1736 boolean returnToOriginalPage = false;
1737 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1738 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1739 returnToOriginalPage = true;
1740 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001741
Adam Cohenf358a4b2013-07-23 16:47:31 -07001742 int finalPage;
1743 // We give flings precedence over large moves, which is why we short-circuit our
1744 // test for a large move if a fling has been registered. That is, a large
1745 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001746 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1747 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001748 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1749 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1750 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1751 snapToPageWithVelocity(finalPage, velocityX);
1752 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1753 (isFling && isVelocityXLeft)) &&
1754 mCurrentPage < getChildCount() - 1) {
1755 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1756 snapToPageWithVelocity(finalPage, velocityX);
1757 } else {
1758 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001759 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001760 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001761 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001762 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001763 }
1764
1765 float scaleX = getScaleX();
1766 int vX = (int) (-velocityX * scaleX);
1767 int initialScrollX = (int) (getScrollX() * scaleX);
1768
Adam Cohenf9618852013-11-08 06:45:03 -08001769 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001770 mScroller.fling(initialScrollX,
1771 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1772 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001773 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001774 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001775 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1776 // at this point we have not moved beyond the touch slop
1777 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1778 // we can just page
1779 int nextPage = Math.max(0, mCurrentPage - 1);
1780 if (nextPage != mCurrentPage) {
1781 snapToPage(nextPage);
1782 } else {
1783 snapToDestination();
1784 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001785 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 // at this point we have not moved beyond the touch slop
1787 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1788 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001789 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1790 if (nextPage != mCurrentPage) {
1791 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 } else {
1793 snapToDestination();
1794 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001795 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1796 // Update the last motion position
1797 mLastMotionX = ev.getX();
1798 mLastMotionY = ev.getY();
1799
1800 // Update the parent down so that our zoom animations take this new movement into
1801 // account
1802 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1803 mParentDownMotionX = pt[0];
1804 mParentDownMotionY = pt[1];
1805 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001806 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001807 if (!mCancelTap) {
1808 onUnhandledTap(ev);
1809 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001810 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001811
1812 // Remove the callback to wait for the side page hover timeout
1813 removeCallbacks(mSidePageHoverRunnable);
1814 // End any intermediate reordering states
1815 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001816 break;
1817
1818 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001819 if (mTouchState == TOUCH_STATE_SCROLLING) {
1820 snapToDestination();
1821 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001822 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001823 break;
1824
1825 case MotionEvent.ACTION_POINTER_UP:
1826 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001827 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001828 break;
1829 }
1830
1831 return true;
1832 }
1833
Adam Cohen7d30a372013-07-01 17:03:59 -07001834 private void resetTouchState() {
1835 releaseVelocityTracker();
1836 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001837 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001838 mTouchState = TOUCH_STATE_REST;
1839 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001840 mEdgeGlowLeft.onRelease();
1841 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001842 }
1843
Adam Cohenc2d6e892014-10-16 09:49:24 -07001844 /**
1845 * Triggered by scrolling via touch
1846 */
1847 protected void onScrollInteractionBegin() {
1848 }
1849
1850 protected void onScrollInteractionEnd() {
1851 }
1852
Adam Cohen1697b792013-09-17 19:08:21 -07001853 protected void onUnhandledTap(MotionEvent ev) {
1854 ((Launcher) getContext()).onClick(this);
1855 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001856
Winson Chung185d7162011-02-28 13:47:29 -08001857 @Override
1858 public boolean onGenericMotionEvent(MotionEvent event) {
1859 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1860 switch (event.getAction()) {
1861 case MotionEvent.ACTION_SCROLL: {
1862 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1863 final float vscroll;
1864 final float hscroll;
1865 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1866 vscroll = 0;
1867 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1868 } else {
1869 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1870 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1871 }
1872 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001873 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001874 : (hscroll > 0 || vscroll > 0);
1875 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001876 scrollRight();
1877 } else {
1878 scrollLeft();
1879 }
1880 return true;
1881 }
1882 }
1883 }
1884 }
1885 return super.onGenericMotionEvent(event);
1886 }
1887
Michael Jurkab8f06722010-10-10 15:58:46 -07001888 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1889 if (mVelocityTracker == null) {
1890 mVelocityTracker = VelocityTracker.obtain();
1891 }
1892 mVelocityTracker.addMovement(ev);
1893 }
1894
1895 private void releaseVelocityTracker() {
1896 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001897 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001898 mVelocityTracker.recycle();
1899 mVelocityTracker = null;
1900 }
1901 }
1902
Winson Chung321e9ee2010-08-09 13:37:56 -07001903 private void onSecondaryPointerUp(MotionEvent ev) {
1904 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1905 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1906 final int pointerId = ev.getPointerId(pointerIndex);
1907 if (pointerId == mActivePointerId) {
1908 // This was our active pointer going up. Choose a new
1909 // active pointer and adjust accordingly.
1910 // TODO: Make this decision more intelligent.
1911 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1912 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1913 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001914 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001915 mActivePointerId = ev.getPointerId(newPointerIndex);
1916 if (mVelocityTracker != null) {
1917 mVelocityTracker.clear();
1918 }
1919 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001920 }
1921
Winson Chung321e9ee2010-08-09 13:37:56 -07001922 @Override
1923 public void requestChildFocus(View child, View focused) {
1924 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001925 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001926 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001927 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 }
1929 }
1930
Adam Cohend19d3ca2010-09-15 14:43:42 -07001931 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001932 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001933 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001934 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 final int childCount = getChildCount();
1936 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001937 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001938 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001939 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001940 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1942 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1943 minDistanceFromScreenCenter = distanceFromScreenCenter;
1944 minDistanceFromScreenCenterIndex = i;
1945 }
1946 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001947 return minDistanceFromScreenCenterIndex;
1948 }
1949
1950 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001951 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001952 }
1953
Adam Cohene0f66b52010-11-23 15:06:07 -08001954 private static class ScrollInterpolator implements Interpolator {
1955 public ScrollInterpolator() {
1956 }
1957
1958 public float getInterpolation(float t) {
1959 t -= 1.0f;
1960 return t*t*t*t*t + 1;
1961 }
1962 }
1963
1964 // We want the duration of the page snap animation to be influenced by the distance that
1965 // the screen has to travel, however, we don't want this duration to be effected in a
1966 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1967 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001968 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001969 f -= 0.5f; // center the values about 0.
1970 f *= 0.3f * Math.PI / 2.0f;
1971 return (float) Math.sin(f);
1972 }
1973
Michael Jurka0142d492010-08-25 17:46:15 -07001974 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001975 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001976 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001977
Adam Cohenedb40762013-07-18 16:45:45 -07001978 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001979 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001980 int duration = 0;
1981
Sunny Goyal4d113a52015-05-27 10:05:28 -07001982 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001983 // If the velocity is low enough, then treat this more as an automatic page advance
1984 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001985 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001986 return;
1987 }
1988
1989 // Here we compute a "distance" that will be used in the computation of the overall
1990 // snap duration. This is a function of the actual distance that needs to be traveled;
1991 // we keep this value close to half screen size in order to reduce the variance in snap
1992 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001993 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001994 float distance = halfScreenSize + halfScreenSize *
1995 distanceInfluenceForSnapDuration(distanceRatio);
1996
1997 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001998 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001999
2000 // we want the page's snap velocity to approximately match the velocity at which the
2001 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002002 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2003 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002004
2005 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002006 }
2007
Sunny Goyal1740d902015-05-27 11:14:01 -07002008 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002009 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002010 }
2011
Adam Cohen7d30a372013-07-01 17:03:59 -07002012 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002013 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002014 }
2015
Michael Jurka0142d492010-08-25 17:46:15 -07002016 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002017 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002018 }
2019
Adam Cohenf9618852013-11-08 06:45:03 -08002020 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2021 snapToPage(whichPage, duration, false, interpolator);
2022 }
2023
2024 protected void snapToPage(int whichPage, int duration, boolean immediate,
2025 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002026 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002027
Adam Cohenedb40762013-07-18 16:45:45 -07002028 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002029 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002030 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002031 }
2032
2033 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002034 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002035 }
Michael Jurka0142d492010-08-25 17:46:15 -07002036
Adam Cohenf9618852013-11-08 06:45:03 -08002037 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2038 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002039 whichPage = validateNewPage(whichPage);
2040
Adam Cohen7d30a372013-07-01 17:03:59 -07002041 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002042 View focusedChild = getFocusedChild();
2043 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002044 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002045 focusedChild.clearFocus();
2046 }
2047
2048 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002049 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 if (immediate) {
2051 duration = 0;
2052 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002053 duration = Math.abs(delta);
2054 }
2055
Adam Cohenf358a4b2013-07-23 16:47:31 -07002056 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002057 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002058 }
Adam Cohenf9618852013-11-08 06:45:03 -08002059
2060 if (interpolator != null) {
2061 mScroller.setInterpolator(interpolator);
2062 } else {
2063 mScroller.setInterpolator(mDefaultInterpolator);
2064 }
2065
Sunny Goyal4d113a52015-05-27 10:05:28 -07002066 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002067
Adam Cohen674531f2013-12-13 15:07:14 -08002068 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002069
2070 // Trigger a compute() to finish switching pages if necessary
2071 if (immediate) {
2072 computeScroll();
2073 }
2074
2075 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002076 invalidate();
2077 }
2078
Winson Chung321e9ee2010-08-09 13:37:56 -07002079 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002080 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002081 }
2082
2083 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002084 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002085 }
2086
Winson Chung86f77532010-08-24 11:08:22 -07002087 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002088 int result = -1;
2089 if (v != null) {
2090 ViewParent vp = v.getParent();
2091 int count = getChildCount();
2092 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002093 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002094 return i;
2095 }
2096 }
2097 }
2098 return result;
2099 }
2100
Adam Cohendbdff6b2013-09-18 19:09:15 -07002101 @Override
2102 public boolean performLongClick() {
2103 mCancelTap = true;
2104 return super.performLongClick();
2105 }
2106
Winson Chung321e9ee2010-08-09 13:37:56 -07002107 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002108 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002109
2110 SavedState(Parcelable superState) {
2111 super(superState);
2112 }
2113
Adam Cohen091440a2015-03-18 14:16:05 -07002114 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002116 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002117 }
2118
2119 @Override
2120 public void writeToParcel(Parcel out, int flags) {
2121 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002122 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002123 }
2124
2125 public static final Parcelable.Creator<SavedState> CREATOR =
2126 new Parcelable.Creator<SavedState>() {
2127 public SavedState createFromParcel(Parcel in) {
2128 return new SavedState(in);
2129 }
2130
2131 public SavedState[] newArray(int size) {
2132 return new SavedState[size];
2133 }
2134 };
2135 }
2136
Adam Cohen7d30a372013-07-01 17:03:59 -07002137 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002138 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002139 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002140 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2141 .translationX(0)
2142 .translationY(0)
2143 .scaleX(1)
2144 .scaleY(1)
2145 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002146 anim.addListener(new AnimatorListenerAdapter() {
2147 @Override
2148 public void onAnimationEnd(Animator animation) {
2149 onPostReorderingAnimationCompleted();
2150 }
2151 });
2152 anim.start();
2153 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002154 }
2155
Sunny Goyal1d08f702015-05-04 15:50:25 -07002156 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002157 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2158 mTouchState = TOUCH_STATE_REORDERING;
2159 mIsReordering = true;
2160
Adam Cohen7d30a372013-07-01 17:03:59 -07002161 // We must invalidate to trigger a redraw to update the layers such that the drag view
2162 // is always drawn on top
2163 invalidate();
2164 }
2165
Adam Cohen091440a2015-03-18 14:16:05 -07002166 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002167 // Trigger the callback when reordering has settled
2168 --mPostReorderingPreZoomInRemainingAnimationCount;
2169 if (mPostReorderingPreZoomInRunnable != null &&
2170 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2171 mPostReorderingPreZoomInRunnable.run();
2172 mPostReorderingPreZoomInRunnable = null;
2173 }
2174 }
2175
Sunny Goyal1d08f702015-05-04 15:50:25 -07002176 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002177 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002178 }
2179
Adam Cohenf358a4b2013-07-23 16:47:31 -07002180 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002181 int dragViewIndex = indexOfChild(v);
2182
Adam Cohen327acfe2014-06-06 11:52:52 -07002183 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002184
Adam Cohen7d30a372013-07-01 17:03:59 -07002185 mTempVisiblePagesRange[0] = 0;
2186 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002187 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002188 mReorderingStarted = true;
2189
2190 // Check if we are within the reordering range
2191 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002192 dragViewIndex <= mTempVisiblePagesRange[1]) {
2193 // Find the drag view under the pointer
2194 mDragView = getChildAt(dragViewIndex);
2195 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2196 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002197 snapToPage(getPageNearestToCenterOfScreen());
2198 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002199 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002200 return true;
2201 }
2202 return false;
2203 }
2204
2205 boolean isReordering(boolean testTouchState) {
2206 boolean state = mIsReordering;
2207 if (testTouchState) {
2208 state &= (mTouchState == TOUCH_STATE_REORDERING);
2209 }
2210 return state;
2211 }
2212 void endReordering() {
2213 // For simplicity, we call endReordering sometimes even if reordering was never started.
2214 // In that case, we don't want to do anything.
2215 if (!mReorderingStarted) return;
2216 mReorderingStarted = false;
2217
2218 // If we haven't flung-to-delete the current child, then we just animate the drag view
2219 // back into position
2220 final Runnable onCompleteRunnable = new Runnable() {
2221 @Override
2222 public void run() {
2223 onEndReordering();
2224 }
2225 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002226
2227 mPostReorderingPreZoomInRunnable = new Runnable() {
2228 public void run() {
2229 onCompleteRunnable.run();
2230 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002231 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002232 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002233
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002234 mPostReorderingPreZoomInRemainingAnimationCount =
2235 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2236 // Snap to the current page
2237 snapToPage(indexOfChild(mDragView), 0);
2238 // Animate the drag view back to the front position
2239 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002240 }
2241
Winson Chung6a0f57d2011-06-29 20:10:49 -07002242 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002243 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002244 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002245 @Override
2246 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2247 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002248 info.setScrollable(getPageCount() > 1);
2249 if (getCurrentPage() < getPageCount() - 1) {
2250 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2251 }
2252 if (getCurrentPage() > 0) {
2253 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2254 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002255 info.setClassName(getClass().getName());
2256
2257 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2258 // Besides disabling the accessibility long-click, this also prevents this view from getting
2259 // accessibility focus.
2260 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002261 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002262 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2263 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002264 }
2265
2266 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002267 public void sendAccessibilityEvent(int eventType) {
2268 // Don't let the view send real scroll events.
2269 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2270 super.sendAccessibilityEvent(eventType);
2271 }
2272 }
2273
2274 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002275 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2276 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002277 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002278 }
2279
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002280 @Override
2281 public boolean performAccessibilityAction(int action, Bundle arguments) {
2282 if (super.performAccessibilityAction(action, arguments)) {
2283 return true;
2284 }
2285 switch (action) {
2286 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2287 if (getCurrentPage() < getPageCount() - 1) {
2288 scrollRight();
2289 return true;
2290 }
2291 } break;
2292 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2293 if (getCurrentPage() > 0) {
2294 scrollLeft();
2295 return true;
2296 }
2297 } break;
2298 }
2299 return false;
2300 }
2301
Adam Cohen0ffac432013-07-10 11:19:26 -07002302 protected String getCurrentPageDescription() {
2303 return String.format(getContext().getString(R.string.default_scroll_format),
2304 getNextPage() + 1, getChildCount());
2305 }
2306
Winson Chungd11265e2011-08-30 13:37:23 -07002307 @Override
2308 public boolean onHoverEvent(android.view.MotionEvent event) {
2309 return true;
2310 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002311}