blob: c9bbe0a06b61ebc40313d8bf64876a57f3e0cb65 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070025import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070026import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070028import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070030import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070032import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
45import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080050import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070051import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070053import java.util.ArrayList;
54
Winson Chung321e9ee2010-08-09 13:37:56 -070055/**
56 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070057 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070058 */
Michael Jurka8b805b12012-04-18 14:23:14 -070059public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070060 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070061 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070062 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070063
Winson Chung86f77532010-08-24 11:08:22 -070064 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070065 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Adam Cohen7d30a372013-07-01 17:03:59 -070067 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070068 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb64cb5a2011-02-15 13:53:42 -080070 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080071 // The page is moved more than halfway, automatically move to the next page on touch up.
72 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080073
Sunny Goyal8e2133b2015-05-06 13:39:07 -070074 private static final float MAX_SCROLL_PROGRESS = 1.0f;
75
Adam Cohen265b9a62011-12-07 14:37:18 -080076 // The following constants need to be scaled based on density. The scaled versions will be
77 // assigned to the corresponding member variables below.
78 private static final int FLING_THRESHOLD_VELOCITY = 500;
79 private static final int MIN_SNAP_VELOCITY = 1500;
80 private static final int MIN_FLING_VELOCITY = 250;
81
Adam Cohen21cd0022013-10-09 18:57:02 -070082 public static final int INVALID_RESTORE_PAGE = -1001;
83
Adam Cohenf358a4b2013-07-23 16:47:31 -070084 private boolean mFreeScroll = false;
85 private int mFreeScrollMinScrollX = -1;
86 private int mFreeScrollMaxScrollX = -1;
87
Adam Cohen265b9a62011-12-07 14:37:18 -080088 protected int mFlingThresholdVelocity;
89 protected int mMinFlingVelocity;
90 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070093 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070094
95 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070096 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070097 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -070098
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected int mNextPage = INVALID_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700100 private int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800101 protected LauncherScroller mScroller;
102 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Adam Cohen7d30a372013-07-01 17:03:59 -0700106 private float mParentDownMotionX;
107 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mDownMotionY;
110 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700111 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700112 private float mLastMotionX;
113 private float mLastMotionXRemainder;
114 private float mLastMotionY;
115 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700116 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700117
Adam Cohendbdff6b2013-09-18 19:09:15 -0700118 private boolean mCancelTap;
119
Adam Cohenedb40762013-07-18 16:45:45 -0700120 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 protected final static int TOUCH_STATE_REST = 0;
123 protected final static int TOUCH_STATE_SCROLLING = 1;
124 protected final static int TOUCH_STATE_PREV_PAGE = 2;
125 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700126 protected final static int TOUCH_STATE_REORDERING = 4;
127
Michael Jurka0142d492010-08-25 17:46:15 -0700128 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700129 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka7426c422010-11-11 15:23:47 -0800133 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private int mMaximumVelocity;
Michael Jurka87b14902011-05-25 22:13:09 -0700135 protected int mCellCountX = 0;
136 protected int mCellCountY = 0;
Adam Cohen68d73932010-11-15 10:50:58 -0800137 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800138 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Winson Chung86f77532010-08-24 11:08:22 -0700144 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700147 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700148
Patrick Dubroy1262e362010-10-06 15:49:50 -0700149 protected boolean mIsPageMoving = false;
150
Adam Cohenc2d6e892014-10-16 09:49:24 -0700151 private boolean mWasInOverscroll = false;
152
Winson Chungd2be3812013-07-16 11:11:32 -0700153 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mPageIndicatorViewId;
155 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700156 // The viewport whether the pages are to be contained (the actual view may be larger than the
157 // viewport)
158 private Rect mViewport = new Rect();
159
160 // Reordering
161 // We use the min scale to determine how much to expand the actually PagedView measured
162 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700163 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700164 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
166
Adam Cohen7d30a372013-07-01 17:03:59 -0700167 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700168 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700169 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 // This variable's scope is only for the duration of startReordering() and endReordering()
173 private boolean mReorderingStarted = false;
174 // This variable's scope is for the duration of startReordering() and after the zoomIn()
175 // animation after endReordering()
176 private boolean mIsReordering;
177 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700178 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700179 private int mPostReorderingPreZoomInRemainingAnimationCount;
180 private Runnable mPostReorderingPreZoomInRunnable;
181
Adam Cohen7d30a372013-07-01 17:03:59 -0700182 // Convenience/caching
Sunny Goyalcf25b522015-07-09 00:01:18 -0700183 protected static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700184 private static final float[] sTmpPoint = new float[2];
185 private static final int[] sTmpIntPoint = new int[2];
186 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700187
John Spurlock77e1f472013-09-11 10:09:51 -0400188 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700189 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400190
Sunny Goyal4d113a52015-05-27 10:05:28 -0700191 // Edge effect
192 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
193 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
194
Winson Chung86f77532010-08-24 11:08:22 -0700195 public interface PageSwitchListener {
196 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 public PagedView(Context context) {
200 this(context, null);
201 }
202
203 public PagedView(Context context, AttributeSet attrs) {
204 this(context, attrs, 0);
205 }
206
207 public PagedView(Context context, AttributeSet attrs, int defStyle) {
208 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700209
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 TypedArray a = context.obtainStyledAttributes(attrs,
211 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700212 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 a.recycle();
214
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700216 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700217 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
219
220 /**
221 * Initializes various states for this workspace.
222 */
Michael Jurka0142d492010-08-25 17:46:15 -0700223 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800224 mScroller = new LauncherScroller(getContext());
225 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700226 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227
228 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800231
Sunny Goyalcf25b522015-07-09 00:01:18 -0700232 float density = getResources().getDisplayMetrics().density;
233 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
234 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
235 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700236 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700237 setWillNotDraw(false);
238 }
239
240 protected void setEdgeGlowColor(int color) {
241 mEdgeGlowLeft.setColor(color);
242 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 }
244
Adam Cohenf9618852013-11-08 06:45:03 -0800245 protected void setDefaultInterpolator(Interpolator interpolator) {
246 mDefaultInterpolator = interpolator;
247 mScroller.setInterpolator(mDefaultInterpolator);
248 }
249
Winson Chungd2be3812013-07-16 11:11:32 -0700250 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700251 super.onAttachedToWindow();
252
Winson Chungd2be3812013-07-16 11:11:32 -0700253 // Hook up the page indicator
254 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700255 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700256 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700257 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700258 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700259
Winson Chung7819a562013-09-19 15:55:45 -0700260 ArrayList<PageIndicator.PageMarkerResources> markers =
261 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700262 for (int i = 0; i < getChildCount(); ++i) {
263 markers.add(getPageIndicatorMarker(i));
264 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700266 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700267
268 OnClickListener listener = getPageIndicatorClickListener();
269 if (listener != null) {
270 mPageIndicator.setOnClickListener(listener);
271 }
Adam Cohen53805212013-10-01 10:39:23 -0700272 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700273 }
274 }
275
Adam Cohen53805212013-10-01 10:39:23 -0700276 protected String getPageIndicatorDescription() {
277 return getCurrentPageDescription();
278 }
279
280 protected OnClickListener getPageIndicatorClickListener() {
281 return null;
282 }
283
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700284 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700285 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700286 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700287 // Unhook the page indicator
288 mPageIndicator = null;
289 }
290
Adam Cohen7d30a372013-07-01 17:03:59 -0700291 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700292 private float[] mapPointFromViewToParent(View v, float x, float y) {
293 sTmpPoint[0] = x;
294 sTmpPoint[1] = y;
295 v.getMatrix().mapPoints(sTmpPoint);
296 sTmpPoint[0] += v.getLeft();
297 sTmpPoint[1] += v.getTop();
298 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700299 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700300 private float[] mapPointFromParentToView(View v, float x, float y) {
301 sTmpPoint[0] = x - v.getLeft();
302 sTmpPoint[1] = y - v.getTop();
303 v.getMatrix().invert(sTmpInvMatrix);
304 sTmpInvMatrix.mapPoints(sTmpPoint);
305 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700306 }
307
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700308 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700309 if (mDragView != null) {
310 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
311 (mDragViewBaselineLeft - mDragView.getLeft());
312 float y = mLastMotionY - mDownMotionY;
313 mDragView.setTranslationX(x);
314 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700315
Adam Cohenf358a4b2013-07-23 16:47:31 -0700316 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
317 + x + ", " + y);
318 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700319 }
320
321 public void setMinScale(float f) {
322 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700323 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700324 requestLayout();
325 }
326
327 @Override
328 public void setScaleX(float scaleX) {
329 super.setScaleX(scaleX);
330 if (isReordering(true)) {
331 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
332 mLastMotionX = p[0];
333 mLastMotionY = p[1];
334 updateDragViewTranslationDuringDrag();
335 }
336 }
337
338 // Convenience methods to get the actual width/height of the PagedView (since it is measured
339 // to be larger to account for the minimum possible scale)
340 int getViewportWidth() {
341 return mViewport.width();
342 }
343 int getViewportHeight() {
344 return mViewport.height();
345 }
346
347 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
348 // PagedView both horizontally and vertically
349 int getViewportOffsetX() {
350 return (getMeasuredWidth() - getViewportWidth()) / 2;
351 }
352
353 int getViewportOffsetY() {
354 return (getMeasuredHeight() - getViewportHeight()) / 2;
355 }
356
Adam Cohenedb40762013-07-18 16:45:45 -0700357 PageIndicator getPageIndicator() {
358 return mPageIndicator;
359 }
Winson Chung7819a562013-09-19 15:55:45 -0700360 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
361 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700362 }
Adam Cohenedb40762013-07-18 16:45:45 -0700363
Adam Cohen674531f2013-12-13 15:07:14 -0800364 /**
365 * Add a page change listener which will be called when a page is _finished_ listening.
366 *
367 */
Winson Chung86f77532010-08-24 11:08:22 -0700368 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
369 mPageSwitchListener = pageSwitchListener;
370 if (mPageSwitchListener != null) {
371 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373 }
374
375 /**
Winson Chung86f77532010-08-24 11:08:22 -0700376 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700378 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700379 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700381
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700382 /**
383 * Returns the index of page to be shown immediately afterwards.
384 */
Winson Chung360e63f2012-04-27 13:48:05 -0700385 int getNextPage() {
386 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
387 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700388
Winson Chung86f77532010-08-24 11:08:22 -0700389 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 return getChildCount();
391 }
392
Sunny Goyal83a8f042015-05-19 12:52:12 -0700393 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 return getChildAt(index);
395 }
396
Adam Cohenae4f1552011-10-20 00:15:42 -0700397 protected int indexToPage(int index) {
398 return index;
399 }
400
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800402 * Updates the scroll of the current page immediately to its final scroll position. We use this
403 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
404 * the previous tab page.
405 */
406 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700407 // If the current page is invalid, just reset the scroll position to zero
408 int newX = 0;
409 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700410 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700411 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800412 scrollTo(newX, 0);
413 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700414 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800415 }
416
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700417 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700418 mScroller.abortAnimation();
419 // We need to clean up the next page here to avoid computeScrollHelper from
420 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700421 if (resetNextPage) {
422 mNextPage = INVALID_PAGE;
423 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700424 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700425
426 private void forceFinishScroller() {
427 mScroller.forceFinished(true);
428 // We need to clean up the next page here to avoid computeScrollHelper from
429 // updating current page on the pass.
430 mNextPage = INVALID_PAGE;
431 }
432
Adam Cohen6f127a62014-06-12 14:54:41 -0700433 private int validateNewPage(int newPage) {
434 int validatedPage = newPage;
435 // When in free scroll mode, we need to clamp to the free scroll page range.
436 if (mFreeScroll) {
437 getFreeScrollPageRange(mTempVisiblePagesRange);
438 validatedPage = Math.max(mTempVisiblePagesRange[0],
439 Math.min(newPage, mTempVisiblePagesRange[1]));
440 }
441 // Ensure that it is clamped by the actual set of children in all cases
442 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
443 return validatedPage;
444 }
445
Chet Haasebc2f0822012-10-26 17:59:53 -0700446 /**
Winson Chung86f77532010-08-24 11:08:22 -0700447 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700449 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800450 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700451 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800452 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800453 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
454 // the default
455 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800456 return;
457 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700458 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700459 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700460 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700461 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800462 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 }
464
Winson Chung8c87cd82013-07-23 16:20:10 -0700465 /**
466 * The restore page will be set in place of the current page at the next (likely first)
467 * layout.
468 */
469 void setRestorePage(int restorePage) {
470 mRestorePage = restorePage;
471 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800472 int getRestorePage() {
473 return mRestorePage;
474 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700475
Adam Cohen674531f2013-12-13 15:07:14 -0800476 /**
477 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
478 * has settled.
479 */
Michael Jurka0142d492010-08-25 17:46:15 -0700480 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700481 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800482 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
Winson Chungd2be3812013-07-16 11:11:32 -0700484
Adam Cohen674531f2013-12-13 15:07:14 -0800485 updatePageIndicator();
486 }
487
488 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700489 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700490 if (mPageIndicator != null) {
491 mPageIndicator.setContentDescription(getPageIndicatorDescription());
492 if (!isReordering(false)) {
493 mPageIndicator.setActiveMarker(getNextPage());
494 }
Winson Chungd2be3812013-07-16 11:11:32 -0700495 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800497 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700498 if (!mIsPageMoving) {
499 mIsPageMoving = true;
500 onPageBeginMoving();
501 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700502 }
503
Michael Jurkace7e05f2011-02-01 22:02:35 -0800504 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700505 if (mIsPageMoving) {
506 mIsPageMoving = false;
507 onPageEndMoving();
508 }
Michael Jurka0142d492010-08-25 17:46:15 -0700509 }
510
Adam Cohen26976d92011-03-22 15:33:33 -0700511 protected boolean isPageMoving() {
512 return mIsPageMoving;
513 }
514
Michael Jurka0142d492010-08-25 17:46:15 -0700515 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700516 protected void onPageBeginMoving() {
517 }
518
519 // a method that subclasses can override to add behavior
520 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700521 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700522 }
523
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 /**
Winson Chung86f77532010-08-24 11:08:22 -0700525 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700526 *
527 * @param l The listener used to respond to long clicks.
528 */
529 @Override
530 public void setOnLongClickListener(OnLongClickListener l) {
531 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700532 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700534 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 }
Adam Cohen1697b792013-09-17 19:08:21 -0700536 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 }
538
539 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800540 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700541 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800542 }
543
544 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700545 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700546 // In free scroll mode, we clamp the scrollX
547 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700548 // If the scroller is trying to move to a location beyond the maximum allowed
549 // in the free scroll mode, we make sure to end the scroll operation.
550 if (!mScroller.isFinished() &&
551 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
552 forceFinishScroller();
553 }
554
Adam Cohenf358a4b2013-07-23 16:47:31 -0700555 x = Math.min(x, mFreeScrollMaxScrollX);
556 x = Math.max(x, mFreeScrollMinScrollX);
557 }
558
Sunny Goyal70660032015-05-14 00:07:08 -0700559 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
560 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700561 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700562 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800563 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700564 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700565 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700566 overScroll(x - mMaxScrollX);
567 } else {
568 overScroll(x);
569 }
Adam Cohen68d73932010-11-15 10:50:58 -0800570 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700571 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700572 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800573 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700574 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700575 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700576 overScroll(x);
577 } else {
578 overScroll(x - mMaxScrollX);
579 }
Adam Cohen68d73932010-11-15 10:50:58 -0800580 }
581 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700582 if (mWasInOverscroll) {
583 overScroll(0);
584 mWasInOverscroll = false;
585 }
Adam Cohen68d73932010-11-15 10:50:58 -0800586 super.scrollTo(x, y);
587 }
588
Adam Cohen7d30a372013-07-01 17:03:59 -0700589 // Update the last motion events when scrolling
590 if (isReordering(true)) {
591 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
592 mLastMotionX = p[0];
593 mLastMotionY = p[1];
594 updateDragViewTranslationDuringDrag();
595 }
Michael Jurka0142d492010-08-25 17:46:15 -0700596 }
597
Adam Cohen53805212013-10-01 10:39:23 -0700598 private void sendScrollAccessibilityEvent() {
599 AccessibilityManager am =
600 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
601 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700602 if (mCurrentPage != getNextPage()) {
603 AccessibilityEvent ev =
604 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700605 ev.setScrollable(true);
606 ev.setScrollX(getScrollX());
607 ev.setScrollY(getScrollY());
608 ev.setMaxScrollX(mMaxScrollX);
609 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700610
Vadim Tryshevf4715972015-05-08 17:21:03 -0700611 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700612 }
Adam Cohen53805212013-10-01 10:39:23 -0700613 }
614 }
615
Michael Jurka0142d492010-08-25 17:46:15 -0700616 // we moved this functionality to a helper function so SmoothPagedView can reuse it
617 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700619 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700621 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700622 float scaleX = mFreeScroll ? getScaleX() : 1f;
623 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
624 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700625 }
Michael Jurka0142d492010-08-25 17:46:15 -0700626 invalidate();
627 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700628 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700629 sendScrollAccessibilityEvent();
630
Adam Cohen6f127a62014-06-12 14:54:41 -0700631 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700632 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700633 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700634
Adam Cohen73aa9752010-11-24 16:26:58 -0800635 // We don't want to trigger a page end moving unless the page has settled
636 // and the user has stopped scrolling
637 if (mTouchState == TOUCH_STATE_REST) {
638 pageEndMoving();
639 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700640
Adam Cohen7d30a372013-07-01 17:03:59 -0700641 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700642 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700643 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700644 if (am.isEnabled()) {
645 // Notify the user when the page changes
646 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700647 }
Michael Jurka0142d492010-08-25 17:46:15 -0700648 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
Michael Jurka0142d492010-08-25 17:46:15 -0700650 return false;
651 }
652
653 @Override
654 public void computeScroll() {
655 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 }
657
Adam Cohen96d30a12013-07-16 18:13:21 -0700658 public static class LayoutParams extends ViewGroup.LayoutParams {
659 public boolean isFullScreenPage = false;
660
661 /**
662 * {@inheritDoc}
663 */
664 public LayoutParams(int width, int height) {
665 super(width, height);
666 }
667
Sunny Goyal41b22c02015-05-14 15:20:48 -0700668 public LayoutParams(Context context, AttributeSet attrs) {
669 super(context, attrs);
670 }
671
Adam Cohen96d30a12013-07-16 18:13:21 -0700672 public LayoutParams(ViewGroup.LayoutParams source) {
673 super(source);
674 }
675 }
676
Sunny Goyal41b22c02015-05-14 15:20:48 -0700677 @Override
678 public LayoutParams generateLayoutParams(AttributeSet attrs) {
679 return new LayoutParams(getContext(), attrs);
680 }
681
682 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700683 protected LayoutParams generateDefaultLayoutParams() {
684 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
685 }
686
Sunny Goyal41b22c02015-05-14 15:20:48 -0700687 @Override
688 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
689 return new LayoutParams(p);
690 }
691
692 @Override
693 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
694 return p instanceof LayoutParams;
695 }
696
Adam Cohenedb40762013-07-18 16:45:45 -0700697 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700698 LayoutParams lp = generateDefaultLayoutParams();
699 lp.isFullScreenPage = true;
700 super.addView(page, 0, lp);
701 }
702
Adam Cohen410f3cd2013-09-22 12:09:32 -0700703 public int getNormalChildHeight() {
704 return mNormalChildHeight;
705 }
706
Winson Chung321e9ee2010-08-09 13:37:56 -0700707 @Override
708 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700709 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700710 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
711 return;
712 }
713
Adam Cohen7d30a372013-07-01 17:03:59 -0700714 // We measure the dimensions of the PagedView to be larger than the pages so that when we
715 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700716 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
717 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
718 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700719 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800720 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700721 // viewport, we can be at most one and a half screens offset once we scale down
722 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700723 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
724 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700725
Winson Chungc82d2622013-11-06 13:23:29 -0800726 int parentWidthSize = (int) (2f * maxSize);
727 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700728 int scaledWidthSize, scaledHeightSize;
729 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700730 scaledWidthSize = (int) (parentWidthSize / mMinScale);
731 scaledHeightSize = (int) (parentHeightSize / mMinScale);
732 } else {
733 scaledWidthSize = widthSize;
734 scaledHeightSize = heightSize;
735 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700736 mViewport.set(0, 0, widthSize, heightSize);
737
738 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
739 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
740 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 }
742
Winson Chung8aad6102012-05-11 16:27:49 -0700743 // Return early if we aren't given a proper dimension
744 if (widthSize <= 0 || heightSize <= 0) {
745 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
746 return;
747 }
748
Adam Lesinski6b879f02010-11-04 16:15:23 -0700749 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
750 * of the All apps view on XLarge displays to not take up more space then it needs. Width
751 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
752 * each page to have the same width.
753 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 final int verticalPadding = getPaddingTop() + getPaddingBottom();
755 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700756
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700757 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700759 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700760 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700761 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
762 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
763 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
764 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 final int childCount = getChildCount();
766 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700767 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700768 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100769 if (child.getVisibility() != GONE) {
770 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700771
Vladimir Marko2824b072013-10-04 16:42:17 +0100772 int childWidthMode;
773 int childHeightMode;
774 int childWidth;
775 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700776
Vladimir Marko2824b072013-10-04 16:42:17 +0100777 if (!lp.isFullScreenPage) {
778 if (lp.width == LayoutParams.WRAP_CONTENT) {
779 childWidthMode = MeasureSpec.AT_MOST;
780 } else {
781 childWidthMode = MeasureSpec.EXACTLY;
782 }
783
784 if (lp.height == LayoutParams.WRAP_CONTENT) {
785 childHeightMode = MeasureSpec.AT_MOST;
786 } else {
787 childHeightMode = MeasureSpec.EXACTLY;
788 }
789
Adam Cohen3b185e22013-10-29 14:45:58 -0700790 childWidth = getViewportWidth() - horizontalPadding
791 - mInsets.left - mInsets.right;
792 childHeight = getViewportHeight() - verticalPadding
793 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100794 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700795 } else {
796 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700797 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100798
Adam Cohen3b185e22013-10-29 14:45:58 -0700799 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
800 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700801 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700802 if (referenceChildWidth == 0) {
803 referenceChildWidth = childWidth;
804 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700805
Vladimir Marko2824b072013-10-04 16:42:17 +0100806 final int childWidthMeasureSpec =
807 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
808 final int childHeightMeasureSpec =
809 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
810 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700811 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700812 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700813 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800814 }
815
Sunny Goyalcf25b522015-07-09 00:01:18 -0700816 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700818 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700819 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700820 return;
821 }
822
Winson Chung785d2eb2011-04-14 16:08:02 -0700823 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700825
Adam Cohen7d30a372013-07-01 17:03:59 -0700826 int offsetX = getViewportOffsetX();
827 int offsetY = getViewportOffsetY();
828
829 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700830 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700831
Sunny Goyal70660032015-05-14 00:07:08 -0700832 final int startIndex = mIsRtl ? childCount - 1 : 0;
833 final int endIndex = mIsRtl ? -1 : childCount;
834 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700835
Adam Cohen7d30a372013-07-01 17:03:59 -0700836 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700837
Adam Cohen3b185e22013-10-29 14:45:58 -0700838 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000839 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700840
841 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700842 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
843 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700844 }
845
Adam Cohen0ffac432013-07-10 11:19:26 -0700846 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700847 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700849 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100850 int childTop;
851 if (lp.isFullScreenPage) {
852 childTop = offsetY;
853 } else {
854 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700855 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100856 }
857
Adam Cohen96d30a12013-07-16 18:13:21 -0700858 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700859 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800860
Winson Chung785d2eb2011-04-14 16:08:02 -0700861 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700862 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800863 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700864
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
866 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000867
868 int pageGap = mPageSpacing;
869 int next = i + delta;
870 if (next != endIndex) {
871 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
872 } else {
873 nextLp = null;
874 }
875
876 // Prevent full screen pages from showing in the viewport
877 // when they are not the current page.
878 if (lp.isFullScreenPage) {
879 pageGap = getPaddingLeft();
880 } else if (nextLp != null && nextLp.isFullScreenPage) {
881 pageGap = getPaddingRight();
882 }
883
Sunny Goyala1fbd842015-05-20 13:40:27 -0700884 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 }
886 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700887
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700888 final LayoutTransition transition = getLayoutTransition();
889 // If the transition is running defer updating max scroll, as some empty pages could
890 // still be present, and a max scroll change could cause sudden jumps in scroll.
891 if (transition != null && transition.isRunning()) {
892 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
893
894 @Override
895 public void startTransition(LayoutTransition transition, ViewGroup container,
896 View view, int transitionType) { }
897
898 @Override
899 public void endTransition(LayoutTransition transition, ViewGroup container,
900 View view, int transitionType) {
901 // Wait until all transitions are complete.
902 if (!transition.isRunning()) {
903 transition.removeTransitionListener(this);
904 updateMaxScrollX();
905 }
906 }
907 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700908 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700909 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700910 }
911
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700912 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
913 updateCurrentPageScroll();
914 mFirstLayout = false;
915 }
916
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700917 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700918 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700919 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700920 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700921 } else {
922 setCurrentPage(getNextPage());
923 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700924 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700925 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700926
927 if (isReordering(true)) {
928 updateDragViewTranslationDuringDrag();
929 }
Winson Chunge3193b92010-09-10 11:44:42 -0700930 }
931
Sunny Goyala1fbd842015-05-20 13:40:27 -0700932 protected int getChildGap() {
933 return 0;
934 }
935
Sunny Goyal316490e2015-06-02 09:38:28 -0700936 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700937 int childCount = getChildCount();
938 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700939 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700940 mMaxScrollX = getScrollForPage(index);
941 } else {
942 mMaxScrollX = 0;
943 }
944 }
945
Adam Cohen3b185e22013-10-29 14:45:58 -0700946 public void setPageSpacing(int pageSpacing) {
947 mPageSpacing = pageSpacing;
948 requestLayout();
949 }
950
Sunny Goyal4d113a52015-05-27 10:05:28 -0700951 /**
952 * Called when the center screen changes during scrolling.
953 */
954 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700955
Winson Chunge3193b92010-09-10 11:44:42 -0700956 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700957 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700958 // Update the page indicator, we don't update the page indicator as we
959 // add/remove pages
960 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700961 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700962 mPageIndicator.addMarker(pageIndex,
963 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700964 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700965 }
966
Adam Cohen2591f6a2011-10-25 14:36:40 -0700967 // This ensures that when children are added, they get the correct transforms / alphas
968 // in accordance with any scroll effects.
969 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700970 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700971 invalidate();
972 }
973
Michael Jurka8b805b12012-04-18 14:23:14 -0700974 @Override
975 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700976 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700977 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700978 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700979 }
980
Winson Chungd2be3812013-07-16 11:11:32 -0700981 private void removeMarkerForView(int index) {
982 // Update the page indicator, we don't update the page indicator as we
983 // add/remove pages
984 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700985 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700986 }
987 }
988
989 @Override
990 public void removeView(View v) {
991 // XXX: We should find a better way to hook into this before the view
992 // gets removed form its parent...
993 removeMarkerForView(indexOfChild(v));
994 super.removeView(v);
995 }
996 @Override
997 public void removeViewInLayout(View v) {
998 // XXX: We should find a better way to hook into this before the view
999 // gets removed form its parent...
1000 removeMarkerForView(indexOfChild(v));
1001 super.removeViewInLayout(v);
1002 }
1003 @Override
1004 public void removeViewAt(int index) {
1005 // XXX: We should find a better way to hook into this before the view
1006 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001007 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001008 super.removeViewAt(index);
1009 }
1010 @Override
1011 public void removeAllViewsInLayout() {
1012 // Update the page indicator, we don't update the page indicator as we
1013 // add/remove pages
1014 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001015 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001016 }
1017
1018 super.removeAllViewsInLayout();
1019 }
1020
Adam Cohen73894962011-10-31 13:17:17 -07001021 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001022 if (index < 0 || index > getChildCount() - 1) return 0;
1023
Adam Cohenf698c6e2013-07-17 18:44:25 -07001024 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001025
Adam Cohenf698c6e2013-07-17 18:44:25 -07001026 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001027 }
1028
Adam Cohen6f127a62014-06-12 14:54:41 -07001029 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001030 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001031 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001032 }
1033
Michael Jurkadde558b2011-11-09 22:09:06 -08001034 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001035 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001036 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001037
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001038 range[0] = -1;
1039 range[1] = -1;
1040
Michael Jurkac4fb9172010-09-02 17:19:20 -07001041 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001042 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001043 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001044
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001045 int count = getChildCount();
1046 for (int i = 0; i < count; i++) {
1047 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001048
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001049 sTmpIntPoint[0] = 0;
1050 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1051 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001052 if (range[0] == -1) {
1053 continue;
1054 } else {
1055 break;
1056 }
1057 }
1058
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001059 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1060 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1061 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001062 if (range[0] == -1) {
1063 continue;
1064 } else {
1065 break;
1066 }
1067 }
1068 curScreen = i;
1069 if (range[0] < 0) {
1070 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001071 }
1072 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001073
1074 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001075 } else {
1076 range[0] = -1;
1077 range[1] = -1;
1078 }
1079 }
1080
Michael Jurka920d7f42012-05-14 16:29:55 -07001081 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001082 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001083 }
1084
Michael Jurkadde558b2011-11-09 22:09:06 -08001085 @Override
1086 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001087 // Find out which screens are visible; as an optimization we only call draw on them
1088 final int pageCount = getChildCount();
1089 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001090 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001091 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001092
1093 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1094 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1095 // set it for the next frame
1096 mForceScreenScrolled = false;
1097 screenScrolled(screenCenter);
1098 mLastScreenCenter = screenCenter;
1099 }
1100
Michael Jurkadde558b2011-11-09 22:09:06 -08001101 getVisiblePages(mTempVisiblePagesRange);
1102 final int leftScreen = mTempVisiblePagesRange[0];
1103 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001104 if (leftScreen != -1 && rightScreen != -1) {
1105 final long drawingTime = getDrawingTime();
1106 // Clip to the bounds
1107 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001108 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1109 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001110
Adam Cohen7d30a372013-07-01 17:03:59 -07001111 // Draw all the children, leaving the drag view for last
1112 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001113 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001114 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001115 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001116 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001117 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001118 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001119 // Draw the drag view on top (if there is one)
1120 if (mDragView != null) {
1121 drawChild(canvas, mDragView, drawingTime);
1122 }
1123
Winson Chungc6f10b92011-11-14 11:39:07 -08001124 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001125 }
Michael Jurka0142d492010-08-25 17:46:15 -07001126 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 }
1128
1129 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001130 public void draw(Canvas canvas) {
1131 super.draw(canvas);
1132 if (getPageCount() > 0) {
1133 if (!mEdgeGlowLeft.isFinished()) {
1134 final int restoreCount = canvas.save();
1135 Rect display = mViewport;
1136 canvas.translate(display.left, display.top);
1137 canvas.rotate(270);
1138
1139 getEdgeVerticalPostion(sTmpIntPoint);
1140 canvas.translate(display.top - sTmpIntPoint[1], 0);
1141 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1142 if (mEdgeGlowLeft.draw(canvas)) {
1143 postInvalidateOnAnimation();
1144 }
1145 canvas.restoreToCount(restoreCount);
1146 }
1147 if (!mEdgeGlowRight.isFinished()) {
1148 final int restoreCount = canvas.save();
1149 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001150 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001151 canvas.rotate(90);
1152
1153 getEdgeVerticalPostion(sTmpIntPoint);
1154 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1155 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1156 if (mEdgeGlowRight.draw(canvas)) {
1157 postInvalidateOnAnimation();
1158 }
1159 canvas.restoreToCount(restoreCount);
1160 }
1161 }
1162 }
1163
1164 /**
1165 * Returns the top and bottom position for the edge effect.
1166 */
1167 protected abstract void getEdgeVerticalPostion(int[] pos);
1168
1169 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001171 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001172 if (page != mCurrentPage || !mScroller.isFinished()) {
1173 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 return true;
1175 }
1176 return false;
1177 }
1178
1179 @Override
1180 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001181 int focusablePage;
1182 if (mNextPage != INVALID_PAGE) {
1183 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001185 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 }
Winson Chung86f77532010-08-24 11:08:22 -07001187 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001189 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
1191 return false;
1192 }
1193
1194 @Override
1195 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001196 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001198 if (getCurrentPage() > 0) {
1199 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 return true;
1201 }
1202 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001203 if (getCurrentPage() < getPageCount() - 1) {
1204 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 return true;
1206 }
1207 }
1208 return super.dispatchUnhandledMove(focused, direction);
1209 }
1210
1211 @Override
1212 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001213 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001214 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001215 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
1217 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001218 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001219 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001222 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001223 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 }
1225 }
1226 }
1227
1228 /**
1229 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001230 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 *
Winson Chung86f77532010-08-24 11:08:22 -07001232 * This happens when live folders requery, and if they're off page, they
1233 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 */
1235 @Override
1236 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001237 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 View v = focused;
1239 while (true) {
1240 if (v == current) {
1241 super.focusableViewAvailable(focused);
1242 return;
1243 }
1244 if (v == this) {
1245 return;
1246 }
1247 ViewParent parent = v.getParent();
1248 if (parent instanceof View) {
1249 v = (View)v.getParent();
1250 } else {
1251 return;
1252 }
1253 }
1254 }
1255
1256 /**
1257 * {@inheritDoc}
1258 */
1259 @Override
1260 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1261 if (disallowIntercept) {
1262 // We need to make sure to cancel our long press if
1263 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001264 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001265 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1268 }
1269
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001270 /**
1271 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1272 */
1273 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001274 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001275 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001276 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001277 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001278 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001279 }
1280
1281 /**
1282 * Return true if a tap at (x, y) should trigger a flip to the next page.
1283 */
1284 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001285 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001286 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001287 }
1288 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001289 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001290 }
Winson Chung52aee602013-01-30 12:01:02 -08001291
Adam Cohen7d30a372013-07-01 17:03:59 -07001292 /** Returns whether x and y originated within the buffered viewport */
1293 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001294 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001295 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001296 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 }
1298
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 @Override
1300 public boolean onInterceptTouchEvent(MotionEvent ev) {
1301 /*
1302 * This method JUST determines whether we want to intercept the motion.
1303 * If we return true, onTouchEvent will be called and we do the actual
1304 * scrolling there.
1305 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001306 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001307
Winson Chung45e1d6e2010-11-09 17:19:49 -08001308 // Skip touch handling if there are no pages to swipe
1309 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1310
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 /*
1312 * Shortcut the most recurring case: the user is in the dragging
1313 * state and he is moving his finger. We want to intercept this
1314 * motion.
1315 */
1316 final int action = ev.getAction();
1317 if ((action == MotionEvent.ACTION_MOVE) &&
1318 (mTouchState == TOUCH_STATE_SCROLLING)) {
1319 return true;
1320 }
1321
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 switch (action & MotionEvent.ACTION_MASK) {
1323 case MotionEvent.ACTION_MOVE: {
1324 /*
1325 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1326 * whether the user has moved far enough from his original down touch.
1327 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001328 if (mActivePointerId != INVALID_POINTER) {
1329 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001330 }
1331 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1332 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1333 // i.e. fall through to the next case (don't break)
1334 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1335 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001336 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 }
1338
1339 case MotionEvent.ACTION_DOWN: {
1340 final float x = ev.getX();
1341 final float y = ev.getY();
1342 // Remember location of down touch
1343 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 mDownMotionY = y;
1345 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 mLastMotionX = x;
1347 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001348 float[] p = mapPointFromViewToParent(this, x, y);
1349 mParentDownMotionX = p[0];
1350 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001351 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001352 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001354
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 /*
1356 * If being flinged and user touches the screen, initiate drag;
1357 * otherwise don't. mScroller.isFinished should be false when
1358 * being flinged.
1359 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001360 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001361 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001362
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001363 if (finishedScrolling) {
1364 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001365 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001366 setCurrentPage(getNextPage());
1367 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001368 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001369 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001370 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1371 mTouchState = TOUCH_STATE_SCROLLING;
1372 } else {
1373 mTouchState = TOUCH_STATE_REST;
1374 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001376
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 break;
1378 }
1379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001381 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001382 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 break;
1384
1385 case MotionEvent.ACTION_POINTER_UP:
1386 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001387 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 break;
1389 }
1390
1391 /*
1392 * The only time we want to intercept motion events is if we are in the
1393 * drag mode.
1394 */
1395 return mTouchState != TOUCH_STATE_REST;
1396 }
1397
Adam Cohenf8d28232011-02-01 21:47:00 -08001398 protected void determineScrollingStart(MotionEvent ev) {
1399 determineScrollingStart(ev, 1.0f);
1400 }
1401
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 /*
1403 * Determines if we should change the touch state to start scrolling after the
1404 * user moves their touch point too far.
1405 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001406 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001407 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001409 if (pointerIndex == -1) return;
1410
1411 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 final float x = ev.getX(pointerIndex);
1413 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001414 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1415
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001417
Adam Cohenf8d28232011-02-01 21:47:00 -08001418 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001420
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001421 if (xMoved) {
1422 // Scroll if the user moved far enough along the X axis
1423 mTouchState = TOUCH_STATE_SCROLLING;
1424 mTotalMotionX += Math.abs(mLastMotionX - x);
1425 mLastMotionX = x;
1426 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001427 onScrollInteractionBegin();
1428 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001429 }
1430 }
1431
1432 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001433 // Try canceling the long press. It could also have been scheduled
1434 // by a distant descendant, so use the mAllowLongPress flag to block
1435 // everything
1436 final View currentPage = getPageAt(mCurrentPage);
1437 if (currentPage != null) {
1438 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 }
1440 }
1441
Adam Cohenb5ba0972011-09-07 18:02:31 -07001442 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001443 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001444
Adam Cohenedb40762013-07-18 16:45:45 -07001445 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001446 int count = getChildCount();
1447
1448 final int totalDistance;
1449
1450 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001451 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001452 adjacentPage = page - 1;
1453 }
1454
1455 if (adjacentPage < 0 || adjacentPage > count - 1) {
1456 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1457 } else {
1458 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1459 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001460
1461 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001462 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1463 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001464 return scrollProgress;
1465 }
1466
Adam Cohenedb40762013-07-18 16:45:45 -07001467 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001468 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001469 return 0;
1470 } else {
1471 return mPageScrolls[index];
1472 }
1473 }
1474
Adam Cohen564a2e72013-10-09 14:47:32 -07001475 // While layout transitions are occurring, a child's position may stray from its baseline
1476 // position. This method returns the magnitude of this stray at any given time.
1477 public int getLayoutTransitionOffsetForPage(int index) {
1478 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1479 return 0;
1480 } else {
1481 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001482
1483 int scrollOffset = 0;
1484 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1485 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001486 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001487 }
1488
Adam Cohen564a2e72013-10-09 14:47:32 -07001489 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1490 return (int) (child.getX() - baselineX);
1491 }
1492 }
1493
Adam Cohenb5ba0972011-09-07 18:02:31 -07001494 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001495 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001496 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001497 if (f < 0) {
1498 mEdgeGlowLeft.onPull(-f);
1499 } else if (f > 0) {
1500 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001501 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001502 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001503 }
1504 invalidate();
1505 }
1506
Adam Cohenb5ba0972011-09-07 18:02:31 -07001507 protected void overScroll(float amount) {
1508 dampedOverScroll(amount);
1509 }
1510
Winson Chungdc61c4d2015-04-20 18:26:57 -07001511 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001512 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001513 }
1514
Winson Chungdc61c4d2015-04-20 18:26:57 -07001515 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001516 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001517 }
1518
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001519 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001520 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001521 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001522 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1523 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1524 } else {
1525 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1526 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1527 }
1528 }
1529
Adam Cohenf9618852013-11-08 06:45:03 -08001530 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001531 mFreeScroll = freeScroll;
1532
Adam Cohenf9618852013-11-08 06:45:03 -08001533 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001534 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001535 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001536 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1537 setCurrentPage(mTempVisiblePagesRange[0]);
1538 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1539 setCurrentPage(mTempVisiblePagesRange[1]);
1540 }
1541 }
1542
1543 setEnableOverscroll(!freeScroll);
1544 }
1545
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001546 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001547 mAllowOverScroll = enable;
1548 }
1549
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001550 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001551 if (mDragView != null) {
1552 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1553 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001554 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001555 int minDistance = Integer.MAX_VALUE;
1556 int minIndex = indexOfChild(mDragView);
1557 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1558 View page = getPageAt(i);
1559 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1560 int d = Math.abs(dragX - pageX);
1561 if (d < minDistance) {
1562 minIndex = i;
1563 minDistance = d;
1564 }
1565 }
1566 return minIndex;
1567 }
1568 return -1;
1569 }
1570
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 @Override
1572 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001573 super.onTouchEvent(ev);
1574
Winson Chung45e1d6e2010-11-09 17:19:49 -08001575 // Skip touch handling if there are no pages to swipe
1576 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1577
Michael Jurkab8f06722010-10-10 15:58:46 -07001578 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001579
1580 final int action = ev.getAction();
1581
1582 switch (action & MotionEvent.ACTION_MASK) {
1583 case MotionEvent.ACTION_DOWN:
1584 /*
1585 * If being flinged and user touches, stop the fling. isFinished
1586 * will be false if being flinged.
1587 */
1588 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001589 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 }
1591
1592 // Remember where the motion event started
1593 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001594 mDownMotionY = mLastMotionY = ev.getY();
1595 mDownScrollX = getScrollX();
1596 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1597 mParentDownMotionX = p[0];
1598 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001599 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001600 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001602
Michael Jurka0142d492010-08-25 17:46:15 -07001603 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001604 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001605 pageBeginMoving();
1606 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 break;
1608
1609 case MotionEvent.ACTION_MOVE:
1610 if (mTouchState == TOUCH_STATE_SCROLLING) {
1611 // Scroll to follow the motion event
1612 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001613
1614 if (pointerIndex == -1) return true;
1615
Winson Chung321e9ee2010-08-09 13:37:56 -07001616 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001617 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001618
Adam Cohenaefd4e12011-02-14 16:39:38 -08001619 mTotalMotionX += Math.abs(deltaX);
1620
Winson Chungc0844aa2011-02-02 15:25:58 -08001621 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1622 // keep the remainder because we are actually testing if we've moved from the last
1623 // scrolled position (which is discrete).
1624 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001625 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001626 mLastMotionX = x;
1627 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001628 } else {
1629 awakenScrollBars();
1630 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001631 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1632 // Update the last motion position
1633 mLastMotionX = ev.getX();
1634 mLastMotionY = ev.getY();
1635
1636 // Update the parent down so that our zoom animations take this new movement into
1637 // account
1638 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1639 mParentDownMotionX = pt[0];
1640 mParentDownMotionY = pt[1];
1641 updateDragViewTranslationDuringDrag();
1642
1643 // Find the closest page to the touch point
1644 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001645
Adam Cohen7d30a372013-07-01 17:03:59 -07001646 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1647 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1648 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1649 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1650
Adam Cohenf358a4b2013-07-23 16:47:31 -07001651 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001652 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001653 mTempVisiblePagesRange[0] = 0;
1654 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001655 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001656 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1657 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1658 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1659 mSidePageHoverIndex = pageUnderPointIndex;
1660 mSidePageHoverRunnable = new Runnable() {
1661 @Override
1662 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 // Setup the scroll to the correct page before we swap the views
1664 snapToPage(pageUnderPointIndex);
1665
1666 // For each of the pages between the paged view and the drag view,
1667 // animate them from the previous position to the new position in
1668 // the layout (as a result of the drag view moving in the layout)
1669 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1670 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1671 dragViewIndex + 1 : pageUnderPointIndex;
1672 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1673 dragViewIndex - 1 : pageUnderPointIndex;
1674 for (int i = lowerIndex; i <= upperIndex; ++i) {
1675 View v = getChildAt(i);
1676 // dragViewIndex < pageUnderPointIndex, so after we remove the
1677 // drag view all subsequent views to pageUnderPointIndex will
1678 // shift down.
1679 int oldX = getViewportOffsetX() + getChildOffset(i);
1680 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1681
1682 // Animate the view translation from its old position to its new
1683 // position
1684 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1685 if (anim != null) {
1686 anim.cancel();
1687 }
1688
1689 v.setTranslationX(oldX - newX);
1690 anim = new AnimatorSet();
1691 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1692 anim.playTogether(
1693 ObjectAnimator.ofFloat(v, "translationX", 0f));
1694 anim.start();
1695 v.setTag(anim);
1696 }
1697
1698 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001700 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001701 if (mPageIndicator != null) {
1702 mPageIndicator.setActiveMarker(getNextPage());
1703 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001704 }
1705 };
1706 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1707 }
1708 } else {
1709 removeCallbacks(mSidePageHoverRunnable);
1710 mSidePageHoverIndex = -1;
1711 }
Adam Cohen564976a2010-10-13 18:52:07 -07001712 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001713 determineScrollingStart(ev);
1714 }
1715 break;
1716
1717 case MotionEvent.ACTION_UP:
1718 if (mTouchState == TOUCH_STATE_SCROLLING) {
1719 final int activePointerId = mActivePointerId;
1720 final int pointerIndex = ev.findPointerIndex(activePointerId);
1721 final float x = ev.getX(pointerIndex);
1722 final VelocityTracker velocityTracker = mVelocityTracker;
1723 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1724 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001725 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001726 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001727 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1728 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001729
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001730 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1731
Adam Cohen00481b32011-11-18 12:03:48 -08001732 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001733 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001734
Adam Cohenf358a4b2013-07-23 16:47:31 -07001735 if (!mFreeScroll) {
1736 // In the case that the page is moved far to one direction and then is flung
1737 // in the opposite direction, we use a threshold to determine whether we should
1738 // just return to the starting page, or if we should skip one further.
1739 boolean returnToOriginalPage = false;
1740 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1741 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1742 returnToOriginalPage = true;
1743 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001744
Adam Cohenf358a4b2013-07-23 16:47:31 -07001745 int finalPage;
1746 // We give flings precedence over large moves, which is why we short-circuit our
1747 // test for a large move if a fling has been registered. That is, a large
1748 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001749 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1750 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001751 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1752 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1753 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1754 snapToPageWithVelocity(finalPage, velocityX);
1755 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1756 (isFling && isVelocityXLeft)) &&
1757 mCurrentPage < getChildCount() - 1) {
1758 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1759 snapToPageWithVelocity(finalPage, velocityX);
1760 } else {
1761 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001762 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001763 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001764 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001765 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001766 }
1767
1768 float scaleX = getScaleX();
1769 int vX = (int) (-velocityX * scaleX);
1770 int initialScrollX = (int) (getScrollX() * scaleX);
1771
Adam Cohenf9618852013-11-08 06:45:03 -08001772 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001773 mScroller.fling(initialScrollX,
1774 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1775 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001776 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001777 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001778 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1779 // at this point we have not moved beyond the touch slop
1780 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1781 // we can just page
1782 int nextPage = Math.max(0, mCurrentPage - 1);
1783 if (nextPage != mCurrentPage) {
1784 snapToPage(nextPage);
1785 } else {
1786 snapToDestination();
1787 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001788 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001789 // at this point we have not moved beyond the touch slop
1790 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1791 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001792 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1793 if (nextPage != mCurrentPage) {
1794 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001795 } else {
1796 snapToDestination();
1797 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001798 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1799 // Update the last motion position
1800 mLastMotionX = ev.getX();
1801 mLastMotionY = ev.getY();
1802
1803 // Update the parent down so that our zoom animations take this new movement into
1804 // account
1805 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1806 mParentDownMotionX = pt[0];
1807 mParentDownMotionY = pt[1];
1808 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001809 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001810 if (!mCancelTap) {
1811 onUnhandledTap(ev);
1812 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001813 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001814
1815 // Remove the callback to wait for the side page hover timeout
1816 removeCallbacks(mSidePageHoverRunnable);
1817 // End any intermediate reordering states
1818 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001819 break;
1820
1821 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001822 if (mTouchState == TOUCH_STATE_SCROLLING) {
1823 snapToDestination();
1824 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001826 break;
1827
1828 case MotionEvent.ACTION_POINTER_UP:
1829 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001830 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001831 break;
1832 }
1833
1834 return true;
1835 }
1836
Adam Cohen7d30a372013-07-01 17:03:59 -07001837 private void resetTouchState() {
1838 releaseVelocityTracker();
1839 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001840 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001841 mTouchState = TOUCH_STATE_REST;
1842 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001843 mEdgeGlowLeft.onRelease();
1844 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001845 }
1846
Adam Cohenc2d6e892014-10-16 09:49:24 -07001847 /**
1848 * Triggered by scrolling via touch
1849 */
1850 protected void onScrollInteractionBegin() {
1851 }
1852
1853 protected void onScrollInteractionEnd() {
1854 }
1855
Adam Cohen1697b792013-09-17 19:08:21 -07001856 protected void onUnhandledTap(MotionEvent ev) {
1857 ((Launcher) getContext()).onClick(this);
1858 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001859
Winson Chung185d7162011-02-28 13:47:29 -08001860 @Override
1861 public boolean onGenericMotionEvent(MotionEvent event) {
1862 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1863 switch (event.getAction()) {
1864 case MotionEvent.ACTION_SCROLL: {
1865 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1866 final float vscroll;
1867 final float hscroll;
1868 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1869 vscroll = 0;
1870 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1871 } else {
1872 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1873 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1874 }
1875 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001876 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001877 : (hscroll > 0 || vscroll > 0);
1878 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001879 scrollRight();
1880 } else {
1881 scrollLeft();
1882 }
1883 return true;
1884 }
1885 }
1886 }
1887 }
1888 return super.onGenericMotionEvent(event);
1889 }
1890
Michael Jurkab8f06722010-10-10 15:58:46 -07001891 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1892 if (mVelocityTracker == null) {
1893 mVelocityTracker = VelocityTracker.obtain();
1894 }
1895 mVelocityTracker.addMovement(ev);
1896 }
1897
1898 private void releaseVelocityTracker() {
1899 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001900 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001901 mVelocityTracker.recycle();
1902 mVelocityTracker = null;
1903 }
1904 }
1905
Winson Chung321e9ee2010-08-09 13:37:56 -07001906 private void onSecondaryPointerUp(MotionEvent ev) {
1907 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1908 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1909 final int pointerId = ev.getPointerId(pointerIndex);
1910 if (pointerId == mActivePointerId) {
1911 // This was our active pointer going up. Choose a new
1912 // active pointer and adjust accordingly.
1913 // TODO: Make this decision more intelligent.
1914 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1915 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1916 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001917 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001918 mActivePointerId = ev.getPointerId(newPointerIndex);
1919 if (mVelocityTracker != null) {
1920 mVelocityTracker.clear();
1921 }
1922 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001923 }
1924
Winson Chung321e9ee2010-08-09 13:37:56 -07001925 @Override
1926 public void requestChildFocus(View child, View focused) {
1927 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001928 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001929 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001930 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001931 }
1932 }
1933
Adam Cohend19d3ca2010-09-15 14:43:42 -07001934 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001935 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001936 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001937 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001938 final int childCount = getChildCount();
1939 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001940 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001941 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001943 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1945 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1946 minDistanceFromScreenCenter = distanceFromScreenCenter;
1947 minDistanceFromScreenCenterIndex = i;
1948 }
1949 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001950 return minDistanceFromScreenCenterIndex;
1951 }
1952
1953 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001954 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001955 }
1956
Adam Cohene0f66b52010-11-23 15:06:07 -08001957 private static class ScrollInterpolator implements Interpolator {
1958 public ScrollInterpolator() {
1959 }
1960
1961 public float getInterpolation(float t) {
1962 t -= 1.0f;
1963 return t*t*t*t*t + 1;
1964 }
1965 }
1966
1967 // We want the duration of the page snap animation to be influenced by the distance that
1968 // the screen has to travel, however, we don't want this duration to be effected in a
1969 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1970 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001971 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001972 f -= 0.5f; // center the values about 0.
1973 f *= 0.3f * Math.PI / 2.0f;
1974 return (float) Math.sin(f);
1975 }
1976
Michael Jurka0142d492010-08-25 17:46:15 -07001977 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001978 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001979 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001980
Adam Cohenedb40762013-07-18 16:45:45 -07001981 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001982 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001983 int duration = 0;
1984
Sunny Goyal4d113a52015-05-27 10:05:28 -07001985 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001986 // If the velocity is low enough, then treat this more as an automatic page advance
1987 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001988 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001989 return;
1990 }
1991
1992 // Here we compute a "distance" that will be used in the computation of the overall
1993 // snap duration. This is a function of the actual distance that needs to be traveled;
1994 // we keep this value close to half screen size in order to reduce the variance in snap
1995 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001996 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001997 float distance = halfScreenSize + halfScreenSize *
1998 distanceInfluenceForSnapDuration(distanceRatio);
1999
2000 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002001 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002002
2003 // we want the page's snap velocity to approximately match the velocity at which the
2004 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002005 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2006 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002007
2008 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002009 }
2010
Sunny Goyal1740d902015-05-27 11:14:01 -07002011 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002012 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002013 }
2014
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002016 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002017 }
2018
Michael Jurka0142d492010-08-25 17:46:15 -07002019 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002020 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002021 }
2022
Adam Cohenf9618852013-11-08 06:45:03 -08002023 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2024 snapToPage(whichPage, duration, false, interpolator);
2025 }
2026
2027 protected void snapToPage(int whichPage, int duration, boolean immediate,
2028 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002029 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002030
Adam Cohenedb40762013-07-18 16:45:45 -07002031 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002032 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002033 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002034 }
2035
2036 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002037 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 }
Michael Jurka0142d492010-08-25 17:46:15 -07002039
Adam Cohenf9618852013-11-08 06:45:03 -08002040 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2041 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002042 whichPage = validateNewPage(whichPage);
2043
Adam Cohen7d30a372013-07-01 17:03:59 -07002044 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002045 View focusedChild = getFocusedChild();
2046 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002047 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002048 focusedChild.clearFocus();
2049 }
2050
2051 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002052 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002053 if (immediate) {
2054 duration = 0;
2055 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002056 duration = Math.abs(delta);
2057 }
2058
Adam Cohenf358a4b2013-07-23 16:47:31 -07002059 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002060 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002061 }
Adam Cohenf9618852013-11-08 06:45:03 -08002062
2063 if (interpolator != null) {
2064 mScroller.setInterpolator(interpolator);
2065 } else {
2066 mScroller.setInterpolator(mDefaultInterpolator);
2067 }
2068
Sunny Goyal4d113a52015-05-27 10:05:28 -07002069 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002070
Adam Cohen674531f2013-12-13 15:07:14 -08002071 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002072
2073 // Trigger a compute() to finish switching pages if necessary
2074 if (immediate) {
2075 computeScroll();
2076 }
2077
2078 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002079 invalidate();
2080 }
2081
Winson Chung321e9ee2010-08-09 13:37:56 -07002082 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002083 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002084 }
2085
2086 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002087 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002088 }
2089
Winson Chung86f77532010-08-24 11:08:22 -07002090 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 int result = -1;
2092 if (v != null) {
2093 ViewParent vp = v.getParent();
2094 int count = getChildCount();
2095 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002096 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002097 return i;
2098 }
2099 }
2100 }
2101 return result;
2102 }
2103
Adam Cohendbdff6b2013-09-18 19:09:15 -07002104 @Override
2105 public boolean performLongClick() {
2106 mCancelTap = true;
2107 return super.performLongClick();
2108 }
2109
Winson Chung321e9ee2010-08-09 13:37:56 -07002110 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002111 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002112
2113 SavedState(Parcelable superState) {
2114 super(superState);
2115 }
2116
Adam Cohen091440a2015-03-18 14:16:05 -07002117 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002119 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 }
2121
2122 @Override
2123 public void writeToParcel(Parcel out, int flags) {
2124 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002125 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002126 }
2127
2128 public static final Parcelable.Creator<SavedState> CREATOR =
2129 new Parcelable.Creator<SavedState>() {
2130 public SavedState createFromParcel(Parcel in) {
2131 return new SavedState(in);
2132 }
2133
2134 public SavedState[] newArray(int size) {
2135 return new SavedState[size];
2136 }
2137 };
2138 }
2139
Adam Cohen7d30a372013-07-01 17:03:59 -07002140 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002141 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002142 if (mDragView != null) {
2143 AnimatorSet anim = new AnimatorSet();
2144 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2145 anim.playTogether(
2146 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002147 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2148 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2149 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002150 anim.addListener(new AnimatorListenerAdapter() {
2151 @Override
2152 public void onAnimationEnd(Animator animation) {
2153 onPostReorderingAnimationCompleted();
2154 }
2155 });
2156 anim.start();
2157 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002158 }
2159
Sunny Goyal1d08f702015-05-04 15:50:25 -07002160 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002161 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2162 mTouchState = TOUCH_STATE_REORDERING;
2163 mIsReordering = true;
2164
Adam Cohen7d30a372013-07-01 17:03:59 -07002165 // We must invalidate to trigger a redraw to update the layers such that the drag view
2166 // is always drawn on top
2167 invalidate();
2168 }
2169
Adam Cohen091440a2015-03-18 14:16:05 -07002170 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002171 // Trigger the callback when reordering has settled
2172 --mPostReorderingPreZoomInRemainingAnimationCount;
2173 if (mPostReorderingPreZoomInRunnable != null &&
2174 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2175 mPostReorderingPreZoomInRunnable.run();
2176 mPostReorderingPreZoomInRunnable = null;
2177 }
2178 }
2179
Sunny Goyal1d08f702015-05-04 15:50:25 -07002180 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002181 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002182 }
2183
Adam Cohenf358a4b2013-07-23 16:47:31 -07002184 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002185 int dragViewIndex = indexOfChild(v);
2186
Adam Cohen327acfe2014-06-06 11:52:52 -07002187 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002188
Adam Cohen7d30a372013-07-01 17:03:59 -07002189 mTempVisiblePagesRange[0] = 0;
2190 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002191 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002192 mReorderingStarted = true;
2193
2194 // Check if we are within the reordering range
2195 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002196 dragViewIndex <= mTempVisiblePagesRange[1]) {
2197 // Find the drag view under the pointer
2198 mDragView = getChildAt(dragViewIndex);
2199 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2200 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002201 snapToPage(getPageNearestToCenterOfScreen());
2202 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002203 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002204 return true;
2205 }
2206 return false;
2207 }
2208
2209 boolean isReordering(boolean testTouchState) {
2210 boolean state = mIsReordering;
2211 if (testTouchState) {
2212 state &= (mTouchState == TOUCH_STATE_REORDERING);
2213 }
2214 return state;
2215 }
2216 void endReordering() {
2217 // For simplicity, we call endReordering sometimes even if reordering was never started.
2218 // In that case, we don't want to do anything.
2219 if (!mReorderingStarted) return;
2220 mReorderingStarted = false;
2221
2222 // If we haven't flung-to-delete the current child, then we just animate the drag view
2223 // back into position
2224 final Runnable onCompleteRunnable = new Runnable() {
2225 @Override
2226 public void run() {
2227 onEndReordering();
2228 }
2229 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002230
2231 mPostReorderingPreZoomInRunnable = new Runnable() {
2232 public void run() {
2233 onCompleteRunnable.run();
2234 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002235 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002236 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002237
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002238 mPostReorderingPreZoomInRemainingAnimationCount =
2239 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2240 // Snap to the current page
2241 snapToPage(indexOfChild(mDragView), 0);
2242 // Animate the drag view back to the front position
2243 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002244 }
2245
Adam Cohen7d30a372013-07-01 17:03:59 -07002246 private static final int ANIM_TAG_KEY = 100;
2247
Winson Chung6a0f57d2011-06-29 20:10:49 -07002248 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002249 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002250 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002251 @Override
2252 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2253 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002254 info.setScrollable(getPageCount() > 1);
2255 if (getCurrentPage() < getPageCount() - 1) {
2256 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2257 }
2258 if (getCurrentPage() > 0) {
2259 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2260 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002261 info.setClassName(getClass().getName());
2262
2263 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2264 // Besides disabling the accessibility long-click, this also prevents this view from getting
2265 // accessibility focus.
2266 info.setLongClickable(false);
2267 if (Utilities.isLmpOrAbove()) {
2268 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2269 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002270 }
2271
2272 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002273 public void sendAccessibilityEvent(int eventType) {
2274 // Don't let the view send real scroll events.
2275 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2276 super.sendAccessibilityEvent(eventType);
2277 }
2278 }
2279
2280 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002281 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2282 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002283 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002284 }
2285
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002286 @Override
2287 public boolean performAccessibilityAction(int action, Bundle arguments) {
2288 if (super.performAccessibilityAction(action, arguments)) {
2289 return true;
2290 }
2291 switch (action) {
2292 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2293 if (getCurrentPage() < getPageCount() - 1) {
2294 scrollRight();
2295 return true;
2296 }
2297 } break;
2298 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2299 if (getCurrentPage() > 0) {
2300 scrollLeft();
2301 return true;
2302 }
2303 } break;
2304 }
2305 return false;
2306 }
2307
Adam Cohen0ffac432013-07-10 11:19:26 -07002308 protected String getCurrentPageDescription() {
2309 return String.format(getContext().getString(R.string.default_scroll_format),
2310 getNextPage() + 1, getChildCount());
2311 }
2312
Winson Chungd11265e2011-08-30 13:37:23 -07002313 @Override
2314 public boolean onHoverEvent(android.view.MotionEvent event) {
2315 return true;
2316 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002317}