blob: 7109895fd4f8c7c2cb8ae98c8b97f3ed46ab373f [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal8bf6f312016-01-23 14:40:35 -080031import android.graphics.RectF;
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;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070049import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070052import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080072 // The page is moved more than halfway, automatically move to the next page on touch up.
73 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Sunny Goyal8e2133b2015-05-06 13:39:07 -070075 private static final float MAX_SCROLL_PROGRESS = 1.0f;
76
Adam Cohen265b9a62011-12-07 14:37:18 -080077 // The following constants need to be scaled based on density. The scaled versions will be
78 // assigned to the corresponding member variables below.
79 private static final int FLING_THRESHOLD_VELOCITY = 500;
80 private static final int MIN_SNAP_VELOCITY = 1500;
81 private static final int MIN_FLING_VELOCITY = 250;
82
Adam Cohen21cd0022013-10-09 18:57:02 -070083 public static final int INVALID_RESTORE_PAGE = -1001;
84
Adam Cohenf358a4b2013-07-23 16:47:31 -070085 private boolean mFreeScroll = false;
86 private int mFreeScrollMinScrollX = -1;
87 private int mFreeScrollMaxScrollX = -1;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070094 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070098 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mNextPage = INVALID_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700103 private int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 protected LauncherScroller mScroller;
105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mParentDownMotionX;
110 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mDownMotionY;
113 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700114 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700115 private float mLastMotionX;
116 private float mLastMotionXRemainder;
117 private float mLastMotionY;
118 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700119 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700132 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800139 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Winson Chung86f77532010-08-24 11:08:22 -0700145 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Michael Jurka0142d492010-08-25 17:46:15 -0700147 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700149
Patrick Dubroy1262e362010-10-06 15:49:50 -0700150 protected boolean mIsPageMoving = false;
151
Adam Cohenc2d6e892014-10-16 09:49:24 -0700152 private boolean mWasInOverscroll = false;
153
Winson Chungd2be3812013-07-16 11:11:32 -0700154 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk int mPageIndicatorViewId;
156 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700157 // The viewport whether the pages are to be contained (the actual view may be larger than the
158 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800159 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700160 private Rect mViewport = new Rect();
161
162 // Reordering
163 // We use the min scale to determine how much to expand the actually PagedView measured
164 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700166 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700167 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
168
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700170 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700171 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700174 // This variable's scope is only for the duration of startReordering() and endReordering()
175 private boolean mReorderingStarted = false;
176 // This variable's scope is for the duration of startReordering() and after the zoomIn()
177 // animation after endReordering()
178 private boolean mIsReordering;
179 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700180 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 private int mPostReorderingPreZoomInRemainingAnimationCount;
182 private Runnable mPostReorderingPreZoomInRunnable;
183
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700185 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700186 private static final float[] sTmpPoint = new float[2];
187 private static final int[] sTmpIntPoint = new int[2];
188 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800189 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700190
John Spurlock77e1f472013-09-11 10:09:51 -0400191 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700192 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400193
Sunny Goyal4d113a52015-05-27 10:05:28 -0700194 // Edge effect
195 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
196 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
197
Winson Chung86f77532010-08-24 11:08:22 -0700198 public interface PageSwitchListener {
199 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
Winson Chung321e9ee2010-08-09 13:37:56 -0700202 public PagedView(Context context) {
203 this(context, null);
204 }
205
206 public PagedView(Context context, AttributeSet attrs) {
207 this(context, attrs, 0);
208 }
209
210 public PagedView(Context context, AttributeSet attrs, int defStyle) {
211 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700212
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 TypedArray a = context.obtainStyledAttributes(attrs,
214 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700215 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700216 a.recycle();
217
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700219 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700220 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222
223 /**
224 * Initializes various states for this workspace.
225 */
Michael Jurka0142d492010-08-25 17:46:15 -0700226 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800227 mScroller = new LauncherScroller(getContext());
228 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700229 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700230
231 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700232 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800234
Sunny Goyalcf25b522015-07-09 00:01:18 -0700235 float density = getResources().getDisplayMetrics().density;
236 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
237 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
238 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700239 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700240 setWillNotDraw(false);
241 }
242
243 protected void setEdgeGlowColor(int color) {
244 mEdgeGlowLeft.setColor(color);
245 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 }
247
Adam Cohenf9618852013-11-08 06:45:03 -0800248 protected void setDefaultInterpolator(Interpolator interpolator) {
249 mDefaultInterpolator = interpolator;
250 mScroller.setInterpolator(mDefaultInterpolator);
251 }
252
Winson Chungd2be3812013-07-16 11:11:32 -0700253 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700254 super.onAttachedToWindow();
255
Winson Chungd2be3812013-07-16 11:11:32 -0700256 // Hook up the page indicator
257 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700258 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700259 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700260 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700261 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700262
Winson Chung7819a562013-09-19 15:55:45 -0700263 ArrayList<PageIndicator.PageMarkerResources> markers =
264 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700265 for (int i = 0; i < getChildCount(); ++i) {
266 markers.add(getPageIndicatorMarker(i));
267 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700269 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700270
271 OnClickListener listener = getPageIndicatorClickListener();
272 if (listener != null) {
273 mPageIndicator.setOnClickListener(listener);
274 }
Adam Cohen53805212013-10-01 10:39:23 -0700275 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700276 }
277 }
278
Adam Cohen53805212013-10-01 10:39:23 -0700279 protected String getPageIndicatorDescription() {
280 return getCurrentPageDescription();
281 }
282
283 protected OnClickListener getPageIndicatorClickListener() {
284 return null;
285 }
286
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700287 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700288 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700289 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700290 // Unhook the page indicator
291 mPageIndicator = null;
292 }
293
Adam Cohen7d30a372013-07-01 17:03:59 -0700294 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700295 private float[] mapPointFromViewToParent(View v, float x, float y) {
296 sTmpPoint[0] = x;
297 sTmpPoint[1] = y;
298 v.getMatrix().mapPoints(sTmpPoint);
299 sTmpPoint[0] += v.getLeft();
300 sTmpPoint[1] += v.getTop();
301 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700302 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700303 private float[] mapPointFromParentToView(View v, float x, float y) {
304 sTmpPoint[0] = x - v.getLeft();
305 sTmpPoint[1] = y - v.getTop();
306 v.getMatrix().invert(sTmpInvMatrix);
307 sTmpInvMatrix.mapPoints(sTmpPoint);
308 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700309 }
310
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700311 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700312 if (mDragView != null) {
313 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
314 (mDragViewBaselineLeft - mDragView.getLeft());
315 float y = mLastMotionY - mDownMotionY;
316 mDragView.setTranslationX(x);
317 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700318
Adam Cohenf358a4b2013-07-23 16:47:31 -0700319 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
320 + x + ", " + y);
321 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 }
323
324 public void setMinScale(float f) {
325 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700326 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700327 requestLayout();
328 }
329
330 @Override
331 public void setScaleX(float scaleX) {
332 super.setScaleX(scaleX);
333 if (isReordering(true)) {
334 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
335 mLastMotionX = p[0];
336 mLastMotionY = p[1];
337 updateDragViewTranslationDuringDrag();
338 }
339 }
340
341 // Convenience methods to get the actual width/height of the PagedView (since it is measured
342 // to be larger to account for the minimum possible scale)
343 int getViewportWidth() {
344 return mViewport.width();
345 }
346 int getViewportHeight() {
347 return mViewport.height();
348 }
349
350 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
351 // PagedView both horizontally and vertically
352 int getViewportOffsetX() {
353 return (getMeasuredWidth() - getViewportWidth()) / 2;
354 }
355
356 int getViewportOffsetY() {
357 return (getMeasuredHeight() - getViewportHeight()) / 2;
358 }
359
Adam Cohenedb40762013-07-18 16:45:45 -0700360 PageIndicator getPageIndicator() {
361 return mPageIndicator;
362 }
Winson Chung7819a562013-09-19 15:55:45 -0700363 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
364 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700365 }
Adam Cohenedb40762013-07-18 16:45:45 -0700366
Adam Cohen674531f2013-12-13 15:07:14 -0800367 /**
368 * Add a page change listener which will be called when a page is _finished_ listening.
369 *
370 */
Winson Chung86f77532010-08-24 11:08:22 -0700371 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
372 mPageSwitchListener = pageSwitchListener;
373 if (mPageSwitchListener != null) {
374 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 }
376 }
377
378 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700379 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
380 * that the user was on before entering free scroll mode (e.g. the home screen page they
381 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
382 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700384 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700385 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700387
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700388 /**
389 * Returns the index of page to be shown immediately afterwards.
390 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700391 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700392 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
393 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700394
Winson Chung86f77532010-08-24 11:08:22 -0700395 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 return getChildCount();
397 }
398
Sunny Goyal83a8f042015-05-19 12:52:12 -0700399 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 return getChildAt(index);
401 }
402
Adam Cohenae4f1552011-10-20 00:15:42 -0700403 protected int indexToPage(int index) {
404 return index;
405 }
406
Winson Chung321e9ee2010-08-09 13:37:56 -0700407 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800408 * Updates the scroll of the current page immediately to its final scroll position. We use this
409 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
410 * the previous tab page.
411 */
412 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700413 // If the current page is invalid, just reset the scroll position to zero
414 int newX = 0;
415 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700416 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700417 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800418 scrollTo(newX, 0);
419 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700420 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800421 }
422
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700423 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700424 mScroller.abortAnimation();
425 // We need to clean up the next page here to avoid computeScrollHelper from
426 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700427 if (resetNextPage) {
428 mNextPage = INVALID_PAGE;
429 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700430 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700431
432 private void forceFinishScroller() {
433 mScroller.forceFinished(true);
434 // We need to clean up the next page here to avoid computeScrollHelper from
435 // updating current page on the pass.
436 mNextPage = INVALID_PAGE;
437 }
438
Adam Cohen6f127a62014-06-12 14:54:41 -0700439 private int validateNewPage(int newPage) {
440 int validatedPage = newPage;
441 // When in free scroll mode, we need to clamp to the free scroll page range.
442 if (mFreeScroll) {
443 getFreeScrollPageRange(mTempVisiblePagesRange);
444 validatedPage = Math.max(mTempVisiblePagesRange[0],
445 Math.min(newPage, mTempVisiblePagesRange[1]));
446 }
447 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickham86930612015-09-09 13:50:40 -0700448 validatedPage = Utilities.boundInRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700449 return validatedPage;
450 }
451
Chet Haasebc2f0822012-10-26 17:59:53 -0700452 /**
Winson Chung86f77532010-08-24 11:08:22 -0700453 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700455 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800456 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700457 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800458 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800459 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
460 // the default
461 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800462 return;
463 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700464 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700465 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700466 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700467 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800468 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700469 }
470
Winson Chung8c87cd82013-07-23 16:20:10 -0700471 /**
472 * The restore page will be set in place of the current page at the next (likely first)
473 * layout.
474 */
475 void setRestorePage(int restorePage) {
476 mRestorePage = restorePage;
477 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800478 int getRestorePage() {
479 return mRestorePage;
480 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700481
Adam Cohen674531f2013-12-13 15:07:14 -0800482 /**
483 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
484 * has settled.
485 */
Michael Jurka0142d492010-08-25 17:46:15 -0700486 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700487 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800488 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
Winson Chungd2be3812013-07-16 11:11:32 -0700490
Adam Cohen674531f2013-12-13 15:07:14 -0800491 updatePageIndicator();
492 }
493
494 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700495 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700496 if (mPageIndicator != null) {
497 mPageIndicator.setContentDescription(getPageIndicatorDescription());
498 if (!isReordering(false)) {
499 mPageIndicator.setActiveMarker(getNextPage());
500 }
Winson Chungd2be3812013-07-16 11:11:32 -0700501 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800503 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700504 if (!mIsPageMoving) {
505 mIsPageMoving = true;
506 onPageBeginMoving();
507 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700508 }
509
Michael Jurkace7e05f2011-02-01 22:02:35 -0800510 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700511 if (mIsPageMoving) {
512 mIsPageMoving = false;
513 onPageEndMoving();
514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 }
516
Adam Cohen26976d92011-03-22 15:33:33 -0700517 protected boolean isPageMoving() {
518 return mIsPageMoving;
519 }
520
Michael Jurka0142d492010-08-25 17:46:15 -0700521 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700522 protected void onPageBeginMoving() {
523 }
524
525 // a method that subclasses can override to add behavior
526 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700527 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700528 }
529
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 /**
Winson Chung86f77532010-08-24 11:08:22 -0700531 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 *
533 * @param l The listener used to respond to long clicks.
534 */
535 @Override
536 public void setOnLongClickListener(OnLongClickListener l) {
537 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700538 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700540 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 }
Adam Cohen1697b792013-09-17 19:08:21 -0700542 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 }
544
545 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800546 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700547 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800548 }
549
550 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700551 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700552 // In free scroll mode, we clamp the scrollX
553 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700554 // If the scroller is trying to move to a location beyond the maximum allowed
555 // in the free scroll mode, we make sure to end the scroll operation.
556 if (!mScroller.isFinished() &&
557 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
558 forceFinishScroller();
559 }
560
Adam Cohenf358a4b2013-07-23 16:47:31 -0700561 x = Math.min(x, mFreeScrollMaxScrollX);
562 x = Math.max(x, mFreeScrollMinScrollX);
563 }
564
Sunny Goyal70660032015-05-14 00:07:08 -0700565 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
566 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700567 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700568 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800569 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700570 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700571 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700572 overScroll(x - mMaxScrollX);
573 } else {
574 overScroll(x);
575 }
Adam Cohen68d73932010-11-15 10:50:58 -0800576 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700577 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700578 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800579 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700580 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700581 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700582 overScroll(x);
583 } else {
584 overScroll(x - mMaxScrollX);
585 }
Adam Cohen68d73932010-11-15 10:50:58 -0800586 }
587 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700588 if (mWasInOverscroll) {
589 overScroll(0);
590 mWasInOverscroll = false;
591 }
Adam Cohen68d73932010-11-15 10:50:58 -0800592 super.scrollTo(x, y);
593 }
594
Adam Cohen7d30a372013-07-01 17:03:59 -0700595 // Update the last motion events when scrolling
596 if (isReordering(true)) {
597 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
598 mLastMotionX = p[0];
599 mLastMotionY = p[1];
600 updateDragViewTranslationDuringDrag();
601 }
Michael Jurka0142d492010-08-25 17:46:15 -0700602 }
603
Adam Cohen53805212013-10-01 10:39:23 -0700604 private void sendScrollAccessibilityEvent() {
605 AccessibilityManager am =
606 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
607 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700608 if (mCurrentPage != getNextPage()) {
609 AccessibilityEvent ev =
610 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700611 ev.setScrollable(true);
612 ev.setScrollX(getScrollX());
613 ev.setScrollY(getScrollY());
614 ev.setMaxScrollX(mMaxScrollX);
615 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700616
Vadim Tryshevf4715972015-05-08 17:21:03 -0700617 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700618 }
Adam Cohen53805212013-10-01 10:39:23 -0700619 }
620 }
621
Michael Jurka0142d492010-08-25 17:46:15 -0700622 // we moved this functionality to a helper function so SmoothPagedView can reuse it
623 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700625 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700626 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700627 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700628 float scaleX = mFreeScroll ? getScaleX() : 1f;
629 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
630 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700631 }
Michael Jurka0142d492010-08-25 17:46:15 -0700632 invalidate();
633 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700634 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700635 sendScrollAccessibilityEvent();
636
Adam Cohen6f127a62014-06-12 14:54:41 -0700637 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700638 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700639 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700640
Adam Cohen73aa9752010-11-24 16:26:58 -0800641 // We don't want to trigger a page end moving unless the page has settled
642 // and the user has stopped scrolling
643 if (mTouchState == TOUCH_STATE_REST) {
644 pageEndMoving();
645 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700646
Adam Cohen7d30a372013-07-01 17:03:59 -0700647 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700648 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700649 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700650 if (am.isEnabled()) {
651 // Notify the user when the page changes
652 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700653 }
Michael Jurka0142d492010-08-25 17:46:15 -0700654 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700655 }
Michael Jurka0142d492010-08-25 17:46:15 -0700656 return false;
657 }
658
659 @Override
660 public void computeScroll() {
661 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 }
663
Adam Cohen96d30a12013-07-16 18:13:21 -0700664 public static class LayoutParams extends ViewGroup.LayoutParams {
665 public boolean isFullScreenPage = false;
666
667 /**
668 * {@inheritDoc}
669 */
670 public LayoutParams(int width, int height) {
671 super(width, height);
672 }
673
Sunny Goyal41b22c02015-05-14 15:20:48 -0700674 public LayoutParams(Context context, AttributeSet attrs) {
675 super(context, attrs);
676 }
677
Adam Cohen96d30a12013-07-16 18:13:21 -0700678 public LayoutParams(ViewGroup.LayoutParams source) {
679 super(source);
680 }
681 }
682
Sunny Goyal41b22c02015-05-14 15:20:48 -0700683 @Override
684 public LayoutParams generateLayoutParams(AttributeSet attrs) {
685 return new LayoutParams(getContext(), attrs);
686 }
687
688 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700689 protected LayoutParams generateDefaultLayoutParams() {
690 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
691 }
692
Sunny Goyal41b22c02015-05-14 15:20:48 -0700693 @Override
694 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
695 return new LayoutParams(p);
696 }
697
698 @Override
699 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
700 return p instanceof LayoutParams;
701 }
702
Adam Cohenedb40762013-07-18 16:45:45 -0700703 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700704 LayoutParams lp = generateDefaultLayoutParams();
705 lp.isFullScreenPage = true;
706 super.addView(page, 0, lp);
707 }
708
Adam Cohen410f3cd2013-09-22 12:09:32 -0700709 public int getNormalChildHeight() {
710 return mNormalChildHeight;
711 }
712
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 @Override
714 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700715 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700716 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
717 return;
718 }
719
Adam Cohen7d30a372013-07-01 17:03:59 -0700720 // We measure the dimensions of the PagedView to be larger than the pages so that when we
721 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700722 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
723 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
724 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700725 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800726 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700727 // viewport, we can be at most one and a half screens offset once we scale down
728 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700729 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
730 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700731
Winson Chungc82d2622013-11-06 13:23:29 -0800732 int parentWidthSize = (int) (2f * maxSize);
733 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700734 int scaledWidthSize, scaledHeightSize;
735 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700736 scaledWidthSize = (int) (parentWidthSize / mMinScale);
737 scaledHeightSize = (int) (parentHeightSize / mMinScale);
738 } else {
739 scaledWidthSize = widthSize;
740 scaledHeightSize = heightSize;
741 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700742 mViewport.set(0, 0, widthSize, heightSize);
743
744 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
745 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
746 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 }
748
Winson Chung8aad6102012-05-11 16:27:49 -0700749 // Return early if we aren't given a proper dimension
750 if (widthSize <= 0 || heightSize <= 0) {
751 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
752 return;
753 }
754
Adam Lesinski6b879f02010-11-04 16:15:23 -0700755 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
756 * of the All apps view on XLarge displays to not take up more space then it needs. Width
757 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
758 * each page to have the same width.
759 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 final int verticalPadding = getPaddingTop() + getPaddingBottom();
761 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700762
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700763 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700765 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700766 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700767 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
768 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
769 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
770 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 final int childCount = getChildCount();
772 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700773 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700774 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100775 if (child.getVisibility() != GONE) {
776 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700777
Vladimir Marko2824b072013-10-04 16:42:17 +0100778 int childWidthMode;
779 int childHeightMode;
780 int childWidth;
781 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700782
Vladimir Marko2824b072013-10-04 16:42:17 +0100783 if (!lp.isFullScreenPage) {
784 if (lp.width == LayoutParams.WRAP_CONTENT) {
785 childWidthMode = MeasureSpec.AT_MOST;
786 } else {
787 childWidthMode = MeasureSpec.EXACTLY;
788 }
789
790 if (lp.height == LayoutParams.WRAP_CONTENT) {
791 childHeightMode = MeasureSpec.AT_MOST;
792 } else {
793 childHeightMode = MeasureSpec.EXACTLY;
794 }
795
Adam Cohen3b185e22013-10-29 14:45:58 -0700796 childWidth = getViewportWidth() - horizontalPadding
797 - mInsets.left - mInsets.right;
798 childHeight = getViewportHeight() - verticalPadding
799 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100800 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700801 } else {
802 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700803 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100804
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800805 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700806 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700807 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700808 if (referenceChildWidth == 0) {
809 referenceChildWidth = childWidth;
810 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700811
Vladimir Marko2824b072013-10-04 16:42:17 +0100812 final int childWidthMeasureSpec =
813 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
814 final int childHeightMeasureSpec =
815 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
816 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700817 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700818 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700819 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800820 }
821
Sunny Goyalcf25b522015-07-09 00:01:18 -0700822 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700824 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700825 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700826 return;
827 }
828
Winson Chung785d2eb2011-04-14 16:08:02 -0700829 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700831
Adam Cohen7d30a372013-07-01 17:03:59 -0700832 int offsetX = getViewportOffsetX();
833 int offsetY = getViewportOffsetY();
834
835 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700836 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700837
Sunny Goyal70660032015-05-14 00:07:08 -0700838 final int startIndex = mIsRtl ? childCount - 1 : 0;
839 final int endIndex = mIsRtl ? -1 : childCount;
840 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700841
Adam Cohen7d30a372013-07-01 17:03:59 -0700842 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700843
Adam Cohen3b185e22013-10-29 14:45:58 -0700844 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000845 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700846
847 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700848 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
849 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700850 }
851
Adam Cohen0ffac432013-07-10 11:19:26 -0700852 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700853 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700855 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100856 int childTop;
857 if (lp.isFullScreenPage) {
858 childTop = offsetY;
859 } else {
860 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700861 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100862 }
863
Adam Cohen96d30a12013-07-16 18:13:21 -0700864 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700865 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800866
Winson Chung785d2eb2011-04-14 16:08:02 -0700867 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700868 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800869 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700870
Adam Cohen3b185e22013-10-29 14:45:58 -0700871 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
872 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000873
874 int pageGap = mPageSpacing;
875 int next = i + delta;
876 if (next != endIndex) {
877 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
878 } else {
879 nextLp = null;
880 }
881
882 // Prevent full screen pages from showing in the viewport
883 // when they are not the current page.
884 if (lp.isFullScreenPage) {
885 pageGap = getPaddingLeft();
886 } else if (nextLp != null && nextLp.isFullScreenPage) {
887 pageGap = getPaddingRight();
888 }
889
Sunny Goyala1fbd842015-05-20 13:40:27 -0700890 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 }
892 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700893
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700894 final LayoutTransition transition = getLayoutTransition();
895 // If the transition is running defer updating max scroll, as some empty pages could
896 // still be present, and a max scroll change could cause sudden jumps in scroll.
897 if (transition != null && transition.isRunning()) {
898 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
899
900 @Override
901 public void startTransition(LayoutTransition transition, ViewGroup container,
902 View view, int transitionType) { }
903
904 @Override
905 public void endTransition(LayoutTransition transition, ViewGroup container,
906 View view, int transitionType) {
907 // Wait until all transitions are complete.
908 if (!transition.isRunning()) {
909 transition.removeTransitionListener(this);
910 updateMaxScrollX();
911 }
912 }
913 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700914 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700915 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700916 }
917
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700918 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
919 updateCurrentPageScroll();
920 mFirstLayout = false;
921 }
922
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700923 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700924 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700925 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700926 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700927 } else {
928 setCurrentPage(getNextPage());
929 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700930 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700931 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700932
933 if (isReordering(true)) {
934 updateDragViewTranslationDuringDrag();
935 }
Winson Chunge3193b92010-09-10 11:44:42 -0700936 }
937
Sunny Goyala1fbd842015-05-20 13:40:27 -0700938 protected int getChildGap() {
939 return 0;
940 }
941
Sunny Goyal316490e2015-06-02 09:38:28 -0700942 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700943 int childCount = getChildCount();
944 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700945 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700946 mMaxScrollX = getScrollForPage(index);
947 } else {
948 mMaxScrollX = 0;
949 }
950 }
951
Adam Cohen3b185e22013-10-29 14:45:58 -0700952 public void setPageSpacing(int pageSpacing) {
953 mPageSpacing = pageSpacing;
954 requestLayout();
955 }
956
Sunny Goyal4d113a52015-05-27 10:05:28 -0700957 /**
958 * Called when the center screen changes during scrolling.
959 */
960 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700961
Winson Chunge3193b92010-09-10 11:44:42 -0700962 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700963 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700964 // Update the page indicator, we don't update the page indicator as we
965 // add/remove pages
966 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700967 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700968 mPageIndicator.addMarker(pageIndex,
969 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700970 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700971 }
972
Adam Cohen2591f6a2011-10-25 14:36:40 -0700973 // This ensures that when children are added, they get the correct transforms / alphas
974 // in accordance with any scroll effects.
975 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700976 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700977 invalidate();
978 }
979
Michael Jurka8b805b12012-04-18 14:23:14 -0700980 @Override
981 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700982 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700983 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700984 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700985 }
986
Winson Chungd2be3812013-07-16 11:11:32 -0700987 private void removeMarkerForView(int index) {
988 // Update the page indicator, we don't update the page indicator as we
989 // add/remove pages
990 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700991 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700992 }
993 }
994
995 @Override
996 public void removeView(View v) {
997 // XXX: We should find a better way to hook into this before the view
998 // gets removed form its parent...
999 removeMarkerForView(indexOfChild(v));
1000 super.removeView(v);
1001 }
1002 @Override
1003 public void removeViewInLayout(View v) {
1004 // XXX: We should find a better way to hook into this before the view
1005 // gets removed form its parent...
1006 removeMarkerForView(indexOfChild(v));
1007 super.removeViewInLayout(v);
1008 }
1009 @Override
1010 public void removeViewAt(int index) {
1011 // XXX: We should find a better way to hook into this before the view
1012 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001013 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001014 super.removeViewAt(index);
1015 }
1016 @Override
1017 public void removeAllViewsInLayout() {
1018 // Update the page indicator, we don't update the page indicator as we
1019 // add/remove pages
1020 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001021 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001022 }
1023
1024 super.removeAllViewsInLayout();
1025 }
1026
Adam Cohen73894962011-10-31 13:17:17 -07001027 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001028 if (index < 0 || index > getChildCount() - 1) return 0;
1029
Adam Cohenf698c6e2013-07-17 18:44:25 -07001030 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001031
Adam Cohenf698c6e2013-07-17 18:44:25 -07001032 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001033 }
1034
Adam Cohen6f127a62014-06-12 14:54:41 -07001035 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001036 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001037 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001038 }
1039
Michael Jurkadde558b2011-11-09 22:09:06 -08001040 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001041 final int pageCount = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001042 range[0] = -1;
1043 range[1] = -1;
1044
Michael Jurkac4fb9172010-09-02 17:19:20 -07001045 if (pageCount > 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001046 int lastVisiblePageIndex = 0;
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001047 final int visibleLeft = -getLeft();
1048 final int visibleRight = visibleLeft + getViewportWidth();
Adam Cohen7d30a372013-07-01 17:03:59 -07001049
Tony Wickham29d853c2015-09-08 10:35:56 -07001050 for (int currPageIndex = 0; currPageIndex < pageCount; currPageIndex++) {
1051 View currPage = getPageAt(currPageIndex);
Winson Chungc9ca2982013-07-19 12:07:38 -07001052
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001053 // Verify if the page bounds are within the visible range.
1054 sTmpRectF.left = 0;
1055 sTmpRectF.right = currPage.getMeasuredWidth();
1056 currPage.getMatrix().mapRect(sTmpRectF);
1057 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
1058 getMatrix().mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001059
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001060 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001061 if (range[0] == -1) {
1062 continue;
1063 } else {
1064 break;
1065 }
1066 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001067 if (range[0] < 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001068 range[0] = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001069 }
Tony Wickham29d853c2015-09-08 10:35:56 -07001070 lastVisiblePageIndex = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001071 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001072
Tony Wickham29d853c2015-09-08 10:35:56 -07001073 range[1] = lastVisiblePageIndex;
Michael Jurkadde558b2011-11-09 22:09:06 -08001074 } else {
1075 range[0] = -1;
1076 range[1] = -1;
1077 }
1078 }
1079
Michael Jurka920d7f42012-05-14 16:29:55 -07001080 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001081 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001082 }
1083
Michael Jurkadde558b2011-11-09 22:09:06 -08001084 @Override
1085 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001086 // Find out which screens are visible; as an optimization we only call draw on them
1087 final int pageCount = getChildCount();
1088 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001089 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001090 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001091
1092 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1093 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1094 // set it for the next frame
1095 mForceScreenScrolled = false;
1096 screenScrolled(screenCenter);
1097 mLastScreenCenter = screenCenter;
1098 }
1099
Michael Jurkadde558b2011-11-09 22:09:06 -08001100 getVisiblePages(mTempVisiblePagesRange);
1101 final int leftScreen = mTempVisiblePagesRange[0];
1102 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001103 if (leftScreen != -1 && rightScreen != -1) {
1104 final long drawingTime = getDrawingTime();
1105 // Clip to the bounds
1106 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001107 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1108 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001109
Adam Cohen7d30a372013-07-01 17:03:59 -07001110 // Draw all the children, leaving the drag view for last
1111 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001112 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001113 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001114 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001115 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001116 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001117 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001118 // Draw the drag view on top (if there is one)
1119 if (mDragView != null) {
1120 drawChild(canvas, mDragView, drawingTime);
1121 }
1122
Winson Chungc6f10b92011-11-14 11:39:07 -08001123 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001124 }
Michael Jurka0142d492010-08-25 17:46:15 -07001125 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 }
1127
1128 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001129 public void draw(Canvas canvas) {
1130 super.draw(canvas);
1131 if (getPageCount() > 0) {
1132 if (!mEdgeGlowLeft.isFinished()) {
1133 final int restoreCount = canvas.save();
1134 Rect display = mViewport;
1135 canvas.translate(display.left, display.top);
1136 canvas.rotate(270);
1137
1138 getEdgeVerticalPostion(sTmpIntPoint);
1139 canvas.translate(display.top - sTmpIntPoint[1], 0);
1140 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1141 if (mEdgeGlowLeft.draw(canvas)) {
1142 postInvalidateOnAnimation();
1143 }
1144 canvas.restoreToCount(restoreCount);
1145 }
1146 if (!mEdgeGlowRight.isFinished()) {
1147 final int restoreCount = canvas.save();
1148 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001149 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001150 canvas.rotate(90);
1151
1152 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001153
Sunny Goyal93264612015-11-23 11:47:50 -08001154 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1155 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001156 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) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001196 if (super.dispatchUnhandledMove(focused, direction)) {
1197 return true;
1198 }
1199
1200 if (mIsRtl) {
1201 if (direction == View.FOCUS_LEFT) {
1202 direction = View.FOCUS_RIGHT;
1203 } else if (direction == View.FOCUS_RIGHT) {
1204 direction = View.FOCUS_LEFT;
1205 }
1206 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001208 if (getCurrentPage() > 0) {
1209 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 return true;
1211 }
1212 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001213 if (getCurrentPage() < getPageCount() - 1) {
1214 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 return true;
1216 }
1217 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001218 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 }
1220
1221 @Override
1222 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001223 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001224 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001225 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 }
1227 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001229 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 }
1231 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001232 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001233 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235 }
1236 }
1237
1238 /**
1239 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001240 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 *
Winson Chung86f77532010-08-24 11:08:22 -07001242 * This happens when live folders requery, and if they're off page, they
1243 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 */
1245 @Override
1246 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001247 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 View v = focused;
1249 while (true) {
1250 if (v == current) {
1251 super.focusableViewAvailable(focused);
1252 return;
1253 }
1254 if (v == this) {
1255 return;
1256 }
1257 ViewParent parent = v.getParent();
1258 if (parent instanceof View) {
1259 v = (View)v.getParent();
1260 } else {
1261 return;
1262 }
1263 }
1264 }
1265
1266 /**
1267 * {@inheritDoc}
1268 */
1269 @Override
1270 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1271 if (disallowIntercept) {
1272 // We need to make sure to cancel our long press if
1273 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001274 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001275 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 }
1277 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1278 }
1279
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001280 /**
1281 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1282 */
1283 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001284 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001285 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001286 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001287 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001288 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001289 }
1290
1291 /**
1292 * Return true if a tap at (x, y) should trigger a flip to the next page.
1293 */
1294 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001295 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001296 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001297 }
1298 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001299 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001300 }
Winson Chung52aee602013-01-30 12:01:02 -08001301
Adam Cohen7d30a372013-07-01 17:03:59 -07001302 /** Returns whether x and y originated within the buffered viewport */
1303 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001304 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001305 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001306 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001307 }
1308
Winson Chung321e9ee2010-08-09 13:37:56 -07001309 @Override
1310 public boolean onInterceptTouchEvent(MotionEvent ev) {
1311 /*
1312 * This method JUST determines whether we want to intercept the motion.
1313 * If we return true, onTouchEvent will be called and we do the actual
1314 * scrolling there.
1315 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001316 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001317
Winson Chung45e1d6e2010-11-09 17:19:49 -08001318 // Skip touch handling if there are no pages to swipe
1319 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1320
Winson Chung321e9ee2010-08-09 13:37:56 -07001321 /*
1322 * Shortcut the most recurring case: the user is in the dragging
1323 * state and he is moving his finger. We want to intercept this
1324 * motion.
1325 */
1326 final int action = ev.getAction();
1327 if ((action == MotionEvent.ACTION_MOVE) &&
1328 (mTouchState == TOUCH_STATE_SCROLLING)) {
1329 return true;
1330 }
1331
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 switch (action & MotionEvent.ACTION_MASK) {
1333 case MotionEvent.ACTION_MOVE: {
1334 /*
1335 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1336 * whether the user has moved far enough from his original down touch.
1337 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001338 if (mActivePointerId != INVALID_POINTER) {
1339 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001340 }
1341 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1342 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1343 // i.e. fall through to the next case (don't break)
1344 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1345 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001346 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 }
1348
1349 case MotionEvent.ACTION_DOWN: {
1350 final float x = ev.getX();
1351 final float y = ev.getY();
1352 // Remember location of down touch
1353 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001354 mDownMotionY = y;
1355 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 mLastMotionX = x;
1357 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001358 float[] p = mapPointFromViewToParent(this, x, y);
1359 mParentDownMotionX = p[0];
1360 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001361 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001362 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001364
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 /*
1366 * If being flinged and user touches the screen, initiate drag;
1367 * otherwise don't. mScroller.isFinished should be false when
1368 * being flinged.
1369 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001370 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001371 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001372
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001373 if (finishedScrolling) {
1374 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001375 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001376 setCurrentPage(getNextPage());
1377 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001378 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001379 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001380 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1381 mTouchState = TOUCH_STATE_SCROLLING;
1382 } else {
1383 mTouchState = TOUCH_STATE_REST;
1384 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001385 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001386
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 break;
1388 }
1389
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001391 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 break;
1394
1395 case MotionEvent.ACTION_POINTER_UP:
1396 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001397 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 break;
1399 }
1400
1401 /*
1402 * The only time we want to intercept motion events is if we are in the
1403 * drag mode.
1404 */
1405 return mTouchState != TOUCH_STATE_REST;
1406 }
1407
Adam Cohenf8d28232011-02-01 21:47:00 -08001408 protected void determineScrollingStart(MotionEvent ev) {
1409 determineScrollingStart(ev, 1.0f);
1410 }
1411
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 /*
1413 * Determines if we should change the touch state to start scrolling after the
1414 * user moves their touch point too far.
1415 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001416 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001417 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001419 if (pointerIndex == -1) return;
1420
1421 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 final float x = ev.getX(pointerIndex);
1423 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001424 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1425
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427
Adam Cohenf8d28232011-02-01 21:47:00 -08001428 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001430
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001431 if (xMoved) {
1432 // Scroll if the user moved far enough along the X axis
1433 mTouchState = TOUCH_STATE_SCROLLING;
1434 mTotalMotionX += Math.abs(mLastMotionX - x);
1435 mLastMotionX = x;
1436 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001437 onScrollInteractionBegin();
1438 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001439 }
1440 }
1441
1442 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001443 // Try canceling the long press. It could also have been scheduled
1444 // by a distant descendant, so use the mAllowLongPress flag to block
1445 // everything
1446 final View currentPage = getPageAt(mCurrentPage);
1447 if (currentPage != null) {
1448 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001449 }
1450 }
1451
Adam Cohenb5ba0972011-09-07 18:02:31 -07001452 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001453 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001454
Adam Cohenedb40762013-07-18 16:45:45 -07001455 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001456 int count = getChildCount();
1457
1458 final int totalDistance;
1459
1460 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001461 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001462 adjacentPage = page - 1;
1463 }
1464
1465 if (adjacentPage < 0 || adjacentPage > count - 1) {
1466 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1467 } else {
1468 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1469 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001470
1471 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001472 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1473 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001474 return scrollProgress;
1475 }
1476
Adam Cohenedb40762013-07-18 16:45:45 -07001477 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001478 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001479 return 0;
1480 } else {
1481 return mPageScrolls[index];
1482 }
1483 }
1484
Adam Cohen564a2e72013-10-09 14:47:32 -07001485 // While layout transitions are occurring, a child's position may stray from its baseline
1486 // position. This method returns the magnitude of this stray at any given time.
1487 public int getLayoutTransitionOffsetForPage(int index) {
1488 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1489 return 0;
1490 } else {
1491 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001492
1493 int scrollOffset = 0;
1494 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1495 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001496 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001497 }
1498
Adam Cohen564a2e72013-10-09 14:47:32 -07001499 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1500 return (int) (child.getX() - baselineX);
1501 }
1502 }
1503
Adam Cohenb5ba0972011-09-07 18:02:31 -07001504 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001506 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001507 if (f < 0) {
1508 mEdgeGlowLeft.onPull(-f);
1509 } else if (f > 0) {
1510 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001511 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001512 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001513 }
1514 invalidate();
1515 }
1516
Adam Cohenb5ba0972011-09-07 18:02:31 -07001517 protected void overScroll(float amount) {
1518 dampedOverScroll(amount);
1519 }
1520
Winson Chungdc61c4d2015-04-20 18:26:57 -07001521 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001522 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001523 }
1524
Winson Chungdc61c4d2015-04-20 18:26:57 -07001525 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001526 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001527 }
1528
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001529 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001530 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001531 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001532 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1533 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1534 } else {
1535 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1536 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1537 }
1538 }
1539
Adam Cohenf9618852013-11-08 06:45:03 -08001540 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001541 mFreeScroll = freeScroll;
1542
Adam Cohenf9618852013-11-08 06:45:03 -08001543 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001544 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001545 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001546 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1547 setCurrentPage(mTempVisiblePagesRange[0]);
1548 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1549 setCurrentPage(mTempVisiblePagesRange[1]);
1550 }
1551 }
1552
1553 setEnableOverscroll(!freeScroll);
1554 }
1555
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001556 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001557 mAllowOverScroll = enable;
1558 }
1559
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001560 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001561 if (mDragView != null) {
1562 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1563 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001564 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001565 int minDistance = Integer.MAX_VALUE;
1566 int minIndex = indexOfChild(mDragView);
1567 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1568 View page = getPageAt(i);
1569 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1570 int d = Math.abs(dragX - pageX);
1571 if (d < minDistance) {
1572 minIndex = i;
1573 minDistance = d;
1574 }
1575 }
1576 return minIndex;
1577 }
1578 return -1;
1579 }
1580
Winson Chung321e9ee2010-08-09 13:37:56 -07001581 @Override
1582 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001583 super.onTouchEvent(ev);
1584
Winson Chung45e1d6e2010-11-09 17:19:49 -08001585 // Skip touch handling if there are no pages to swipe
1586 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1587
Michael Jurkab8f06722010-10-10 15:58:46 -07001588 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001589
1590 final int action = ev.getAction();
1591
1592 switch (action & MotionEvent.ACTION_MASK) {
1593 case MotionEvent.ACTION_DOWN:
1594 /*
1595 * If being flinged and user touches, stop the fling. isFinished
1596 * will be false if being flinged.
1597 */
1598 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001599 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 }
1601
1602 // Remember where the motion event started
1603 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001604 mDownMotionY = mLastMotionY = ev.getY();
1605 mDownScrollX = getScrollX();
1606 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1607 mParentDownMotionX = p[0];
1608 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001609 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001610 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001611 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001612
Michael Jurka0142d492010-08-25 17:46:15 -07001613 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001614 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001615 pageBeginMoving();
1616 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 break;
1618
1619 case MotionEvent.ACTION_MOVE:
1620 if (mTouchState == TOUCH_STATE_SCROLLING) {
1621 // Scroll to follow the motion event
1622 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001623
1624 if (pointerIndex == -1) return true;
1625
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001627 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001628
Adam Cohenaefd4e12011-02-14 16:39:38 -08001629 mTotalMotionX += Math.abs(deltaX);
1630
Winson Chungc0844aa2011-02-02 15:25:58 -08001631 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1632 // keep the remainder because we are actually testing if we've moved from the last
1633 // scrolled position (which is discrete).
1634 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001635 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001636 mLastMotionX = x;
1637 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001638 } else {
1639 awakenScrollBars();
1640 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001641 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1642 // Update the last motion position
1643 mLastMotionX = ev.getX();
1644 mLastMotionY = ev.getY();
1645
1646 // Update the parent down so that our zoom animations take this new movement into
1647 // account
1648 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1649 mParentDownMotionX = pt[0];
1650 mParentDownMotionY = pt[1];
1651 updateDragViewTranslationDuringDrag();
1652
1653 // Find the closest page to the touch point
1654 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001655
Adam Cohen7d30a372013-07-01 17:03:59 -07001656 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1657 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1658 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1659 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1660
Adam Cohenf358a4b2013-07-23 16:47:31 -07001661 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001662 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 mTempVisiblePagesRange[0] = 0;
1664 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001665 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001666 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1667 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1668 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1669 mSidePageHoverIndex = pageUnderPointIndex;
1670 mSidePageHoverRunnable = new Runnable() {
1671 @Override
1672 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001673 // Setup the scroll to the correct page before we swap the views
1674 snapToPage(pageUnderPointIndex);
1675
1676 // For each of the pages between the paged view and the drag view,
1677 // animate them from the previous position to the new position in
1678 // the layout (as a result of the drag view moving in the layout)
1679 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1680 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1681 dragViewIndex + 1 : pageUnderPointIndex;
1682 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1683 dragViewIndex - 1 : pageUnderPointIndex;
1684 for (int i = lowerIndex; i <= upperIndex; ++i) {
1685 View v = getChildAt(i);
1686 // dragViewIndex < pageUnderPointIndex, so after we remove the
1687 // drag view all subsequent views to pageUnderPointIndex will
1688 // shift down.
1689 int oldX = getViewportOffsetX() + getChildOffset(i);
1690 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1691
1692 // Animate the view translation from its old position to its new
1693 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001694 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 if (anim != null) {
1696 anim.cancel();
1697 }
1698
1699 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001700 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001701 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001702 anim.start();
1703 v.setTag(anim);
1704 }
1705
1706 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001707 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001709 if (mPageIndicator != null) {
1710 mPageIndicator.setActiveMarker(getNextPage());
1711 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 }
1713 };
1714 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1715 }
1716 } else {
1717 removeCallbacks(mSidePageHoverRunnable);
1718 mSidePageHoverIndex = -1;
1719 }
Adam Cohen564976a2010-10-13 18:52:07 -07001720 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001721 determineScrollingStart(ev);
1722 }
1723 break;
1724
1725 case MotionEvent.ACTION_UP:
1726 if (mTouchState == TOUCH_STATE_SCROLLING) {
1727 final int activePointerId = mActivePointerId;
1728 final int pointerIndex = ev.findPointerIndex(activePointerId);
1729 final float x = ev.getX(pointerIndex);
1730 final VelocityTracker velocityTracker = mVelocityTracker;
1731 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1732 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001733 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001734 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001735 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1736 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001737
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001738 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1739
Adam Cohen00481b32011-11-18 12:03:48 -08001740 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001741 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001742
Adam Cohenf358a4b2013-07-23 16:47:31 -07001743 if (!mFreeScroll) {
1744 // In the case that the page is moved far to one direction and then is flung
1745 // in the opposite direction, we use a threshold to determine whether we should
1746 // just return to the starting page, or if we should skip one further.
1747 boolean returnToOriginalPage = false;
1748 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1749 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1750 returnToOriginalPage = true;
1751 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001752
Adam Cohenf358a4b2013-07-23 16:47:31 -07001753 int finalPage;
1754 // We give flings precedence over large moves, which is why we short-circuit our
1755 // test for a large move if a fling has been registered. That is, a large
1756 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001757 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1758 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001759 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1760 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1761 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1762 snapToPageWithVelocity(finalPage, velocityX);
1763 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1764 (isFling && isVelocityXLeft)) &&
1765 mCurrentPage < getChildCount() - 1) {
1766 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1767 snapToPageWithVelocity(finalPage, velocityX);
1768 } else {
1769 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001770 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001771 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001772 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001773 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001774 }
1775
1776 float scaleX = getScaleX();
1777 int vX = (int) (-velocityX * scaleX);
1778 int initialScrollX = (int) (getScrollX() * scaleX);
1779
Adam Cohenf9618852013-11-08 06:45:03 -08001780 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001781 mScroller.fling(initialScrollX,
1782 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1783 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001784 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001785 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001786 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1787 // at this point we have not moved beyond the touch slop
1788 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1789 // we can just page
1790 int nextPage = Math.max(0, mCurrentPage - 1);
1791 if (nextPage != mCurrentPage) {
1792 snapToPage(nextPage);
1793 } else {
1794 snapToDestination();
1795 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001796 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001797 // at this point we have not moved beyond the touch slop
1798 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1799 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001800 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1801 if (nextPage != mCurrentPage) {
1802 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001803 } else {
1804 snapToDestination();
1805 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001806 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1807 // Update the last motion position
1808 mLastMotionX = ev.getX();
1809 mLastMotionY = ev.getY();
1810
1811 // Update the parent down so that our zoom animations take this new movement into
1812 // account
1813 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1814 mParentDownMotionX = pt[0];
1815 mParentDownMotionY = pt[1];
1816 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001817 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001818 if (!mCancelTap) {
1819 onUnhandledTap(ev);
1820 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001821 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001822
1823 // Remove the callback to wait for the side page hover timeout
1824 removeCallbacks(mSidePageHoverRunnable);
1825 // End any intermediate reordering states
1826 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001827 break;
1828
1829 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001830 if (mTouchState == TOUCH_STATE_SCROLLING) {
1831 snapToDestination();
1832 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001833 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001834 break;
1835
1836 case MotionEvent.ACTION_POINTER_UP:
1837 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001838 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001839 break;
1840 }
1841
1842 return true;
1843 }
1844
Adam Cohen7d30a372013-07-01 17:03:59 -07001845 private void resetTouchState() {
1846 releaseVelocityTracker();
1847 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001848 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001849 mTouchState = TOUCH_STATE_REST;
1850 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001851 mEdgeGlowLeft.onRelease();
1852 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001853 }
1854
Adam Cohenc2d6e892014-10-16 09:49:24 -07001855 /**
1856 * Triggered by scrolling via touch
1857 */
1858 protected void onScrollInteractionBegin() {
1859 }
1860
1861 protected void onScrollInteractionEnd() {
1862 }
1863
Adam Cohen1697b792013-09-17 19:08:21 -07001864 protected void onUnhandledTap(MotionEvent ev) {
1865 ((Launcher) getContext()).onClick(this);
1866 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001867
Winson Chung185d7162011-02-28 13:47:29 -08001868 @Override
1869 public boolean onGenericMotionEvent(MotionEvent event) {
1870 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1871 switch (event.getAction()) {
1872 case MotionEvent.ACTION_SCROLL: {
1873 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1874 final float vscroll;
1875 final float hscroll;
1876 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1877 vscroll = 0;
1878 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1879 } else {
1880 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1881 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1882 }
1883 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001884 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001885 : (hscroll > 0 || vscroll > 0);
1886 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001887 scrollRight();
1888 } else {
1889 scrollLeft();
1890 }
1891 return true;
1892 }
1893 }
1894 }
1895 }
1896 return super.onGenericMotionEvent(event);
1897 }
1898
Michael Jurkab8f06722010-10-10 15:58:46 -07001899 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1900 if (mVelocityTracker == null) {
1901 mVelocityTracker = VelocityTracker.obtain();
1902 }
1903 mVelocityTracker.addMovement(ev);
1904 }
1905
1906 private void releaseVelocityTracker() {
1907 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001908 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001909 mVelocityTracker.recycle();
1910 mVelocityTracker = null;
1911 }
1912 }
1913
Winson Chung321e9ee2010-08-09 13:37:56 -07001914 private void onSecondaryPointerUp(MotionEvent ev) {
1915 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1916 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1917 final int pointerId = ev.getPointerId(pointerIndex);
1918 if (pointerId == mActivePointerId) {
1919 // This was our active pointer going up. Choose a new
1920 // active pointer and adjust accordingly.
1921 // TODO: Make this decision more intelligent.
1922 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1923 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1924 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001925 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001926 mActivePointerId = ev.getPointerId(newPointerIndex);
1927 if (mVelocityTracker != null) {
1928 mVelocityTracker.clear();
1929 }
1930 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001931 }
1932
Winson Chung321e9ee2010-08-09 13:37:56 -07001933 @Override
1934 public void requestChildFocus(View child, View focused) {
1935 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001936 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001937 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001938 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001939 }
1940 }
1941
Adam Cohend19d3ca2010-09-15 14:43:42 -07001942 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001943 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001944 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001945 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001946 final int childCount = getChildCount();
1947 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001948 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001949 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001950 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001951 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001952 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1953 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1954 minDistanceFromScreenCenter = distanceFromScreenCenter;
1955 minDistanceFromScreenCenterIndex = i;
1956 }
1957 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001958 return minDistanceFromScreenCenterIndex;
1959 }
1960
1961 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001962 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001963 }
1964
Adam Cohene0f66b52010-11-23 15:06:07 -08001965 private static class ScrollInterpolator implements Interpolator {
1966 public ScrollInterpolator() {
1967 }
1968
1969 public float getInterpolation(float t) {
1970 t -= 1.0f;
1971 return t*t*t*t*t + 1;
1972 }
1973 }
1974
1975 // We want the duration of the page snap animation to be influenced by the distance that
1976 // the screen has to travel, however, we don't want this duration to be effected in a
1977 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1978 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001979 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001980 f -= 0.5f; // center the values about 0.
1981 f *= 0.3f * Math.PI / 2.0f;
1982 return (float) Math.sin(f);
1983 }
1984
Michael Jurka0142d492010-08-25 17:46:15 -07001985 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001986 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001987 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001988
Adam Cohenedb40762013-07-18 16:45:45 -07001989 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001990 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001991 int duration = 0;
1992
Sunny Goyal4d113a52015-05-27 10:05:28 -07001993 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001994 // If the velocity is low enough, then treat this more as an automatic page advance
1995 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07001996 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001997 return;
1998 }
1999
2000 // Here we compute a "distance" that will be used in the computation of the overall
2001 // snap duration. This is a function of the actual distance that needs to be traveled;
2002 // we keep this value close to half screen size in order to reduce the variance in snap
2003 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002004 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002005 float distance = halfScreenSize + halfScreenSize *
2006 distanceInfluenceForSnapDuration(distanceRatio);
2007
2008 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002009 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002010
2011 // we want the page's snap velocity to approximately match the velocity at which the
2012 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002013 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2014 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002015
2016 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002017 }
2018
Sunny Goyal1740d902015-05-27 11:14:01 -07002019 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002020 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002021 }
2022
Adam Cohen7d30a372013-07-01 17:03:59 -07002023 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002024 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 }
2026
Michael Jurka0142d492010-08-25 17:46:15 -07002027 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002028 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002029 }
2030
Adam Cohenf9618852013-11-08 06:45:03 -08002031 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2032 snapToPage(whichPage, duration, false, interpolator);
2033 }
2034
2035 protected void snapToPage(int whichPage, int duration, boolean immediate,
2036 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002037 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002038
Adam Cohenedb40762013-07-18 16:45:45 -07002039 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002040 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002041 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002042 }
2043
2044 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002045 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 }
Michael Jurka0142d492010-08-25 17:46:15 -07002047
Adam Cohenf9618852013-11-08 06:45:03 -08002048 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2049 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002050 whichPage = validateNewPage(whichPage);
2051
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002053
2054 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002055 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002056 if (immediate) {
2057 duration = 0;
2058 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002059 duration = Math.abs(delta);
2060 }
2061
Adam Cohenf358a4b2013-07-23 16:47:31 -07002062 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002063 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002064 }
Adam Cohenf9618852013-11-08 06:45:03 -08002065
2066 if (interpolator != null) {
2067 mScroller.setInterpolator(interpolator);
2068 } else {
2069 mScroller.setInterpolator(mDefaultInterpolator);
2070 }
2071
Sunny Goyal4d113a52015-05-27 10:05:28 -07002072 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002073
Adam Cohen674531f2013-12-13 15:07:14 -08002074 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002075
2076 // Trigger a compute() to finish switching pages if necessary
2077 if (immediate) {
2078 computeScroll();
2079 }
2080
2081 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002082 invalidate();
2083 }
2084
Winson Chung321e9ee2010-08-09 13:37:56 -07002085 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002086 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002087 }
2088
2089 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002090 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 }
2092
Adam Cohendbdff6b2013-09-18 19:09:15 -07002093 @Override
2094 public boolean performLongClick() {
2095 mCancelTap = true;
2096 return super.performLongClick();
2097 }
2098
Winson Chung321e9ee2010-08-09 13:37:56 -07002099 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002100 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002101
2102 SavedState(Parcelable superState) {
2103 super(superState);
2104 }
2105
Adam Cohen091440a2015-03-18 14:16:05 -07002106 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002107 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002108 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002109 }
2110
2111 @Override
2112 public void writeToParcel(Parcel out, int flags) {
2113 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002114 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 }
2116
2117 public static final Parcelable.Creator<SavedState> CREATOR =
2118 new Parcelable.Creator<SavedState>() {
2119 public SavedState createFromParcel(Parcel in) {
2120 return new SavedState(in);
2121 }
2122
2123 public SavedState[] newArray(int size) {
2124 return new SavedState[size];
2125 }
2126 };
2127 }
2128
Adam Cohen7d30a372013-07-01 17:03:59 -07002129 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002130 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002131 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002132 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2133 .translationX(0)
2134 .translationY(0)
2135 .scaleX(1)
2136 .scaleY(1)
2137 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002138 anim.addListener(new AnimatorListenerAdapter() {
2139 @Override
2140 public void onAnimationEnd(Animator animation) {
2141 onPostReorderingAnimationCompleted();
2142 }
2143 });
2144 anim.start();
2145 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002146 }
2147
Sunny Goyal1d08f702015-05-04 15:50:25 -07002148 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002149 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2150 mTouchState = TOUCH_STATE_REORDERING;
2151 mIsReordering = true;
2152
Adam Cohen7d30a372013-07-01 17:03:59 -07002153 // We must invalidate to trigger a redraw to update the layers such that the drag view
2154 // is always drawn on top
2155 invalidate();
2156 }
2157
Adam Cohen091440a2015-03-18 14:16:05 -07002158 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002159 // Trigger the callback when reordering has settled
2160 --mPostReorderingPreZoomInRemainingAnimationCount;
2161 if (mPostReorderingPreZoomInRunnable != null &&
2162 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2163 mPostReorderingPreZoomInRunnable.run();
2164 mPostReorderingPreZoomInRunnable = null;
2165 }
2166 }
2167
Sunny Goyal1d08f702015-05-04 15:50:25 -07002168 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002169 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002170 }
2171
Adam Cohenf358a4b2013-07-23 16:47:31 -07002172 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002173 int dragViewIndex = indexOfChild(v);
2174
Adam Cohen327acfe2014-06-06 11:52:52 -07002175 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002176
Adam Cohen7d30a372013-07-01 17:03:59 -07002177 mTempVisiblePagesRange[0] = 0;
2178 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002179 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002180 mReorderingStarted = true;
2181
2182 // Check if we are within the reordering range
2183 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002184 dragViewIndex <= mTempVisiblePagesRange[1]) {
2185 // Find the drag view under the pointer
2186 mDragView = getChildAt(dragViewIndex);
2187 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2188 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002189 snapToPage(getPageNearestToCenterOfScreen());
2190 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002191 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002192 return true;
2193 }
2194 return false;
2195 }
2196
2197 boolean isReordering(boolean testTouchState) {
2198 boolean state = mIsReordering;
2199 if (testTouchState) {
2200 state &= (mTouchState == TOUCH_STATE_REORDERING);
2201 }
2202 return state;
2203 }
2204 void endReordering() {
2205 // For simplicity, we call endReordering sometimes even if reordering was never started.
2206 // In that case, we don't want to do anything.
2207 if (!mReorderingStarted) return;
2208 mReorderingStarted = false;
2209
2210 // If we haven't flung-to-delete the current child, then we just animate the drag view
2211 // back into position
2212 final Runnable onCompleteRunnable = new Runnable() {
2213 @Override
2214 public void run() {
2215 onEndReordering();
2216 }
2217 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002218
2219 mPostReorderingPreZoomInRunnable = new Runnable() {
2220 public void run() {
2221 onCompleteRunnable.run();
2222 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002223 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002224 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002225
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002226 mPostReorderingPreZoomInRemainingAnimationCount =
2227 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2228 // Snap to the current page
2229 snapToPage(indexOfChild(mDragView), 0);
2230 // Animate the drag view back to the front position
2231 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002232 }
2233
Winson Chung6a0f57d2011-06-29 20:10:49 -07002234 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002235 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002236 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002237 @Override
2238 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2239 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002240 info.setScrollable(getPageCount() > 1);
2241 if (getCurrentPage() < getPageCount() - 1) {
2242 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2243 }
2244 if (getCurrentPage() > 0) {
2245 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2246 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002247 info.setClassName(getClass().getName());
2248
2249 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2250 // Besides disabling the accessibility long-click, this also prevents this view from getting
2251 // accessibility focus.
2252 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002253 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002254 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2255 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002256 }
2257
2258 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002259 public void sendAccessibilityEvent(int eventType) {
2260 // Don't let the view send real scroll events.
2261 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2262 super.sendAccessibilityEvent(eventType);
2263 }
2264 }
2265
2266 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002267 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2268 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002269 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002270 }
2271
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002272 @Override
2273 public boolean performAccessibilityAction(int action, Bundle arguments) {
2274 if (super.performAccessibilityAction(action, arguments)) {
2275 return true;
2276 }
2277 switch (action) {
2278 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2279 if (getCurrentPage() < getPageCount() - 1) {
2280 scrollRight();
2281 return true;
2282 }
2283 } break;
2284 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2285 if (getCurrentPage() > 0) {
2286 scrollLeft();
2287 return true;
2288 }
2289 } break;
2290 }
2291 return false;
2292 }
2293
Adam Cohen0ffac432013-07-10 11:19:26 -07002294 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002295 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002296 getNextPage() + 1, getChildCount());
2297 }
2298
Winson Chungd11265e2011-08-30 13:37:23 -07002299 @Override
2300 public boolean onHoverEvent(android.view.MotionEvent event) {
2301 return true;
2302 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002303}