blob: ef89192ddbcb3190fd3eb86976b9acdb07f498c4 [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 Goyalc86df472016-02-25 09:19:38 -0800103 protected 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 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800346 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700347 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
Adam Cohenf9c184a2016-01-15 16:47:43 -0800395 public 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
Sunny Goyalc86df472016-02-25 09:19:38 -0800545 protected int getUnboundedScrollX() {
546 return getScrollX();
547 }
548
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800550 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800551 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800552 }
553
554 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700555 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700556 // In free scroll mode, we clamp the scrollX
557 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700558 // If the scroller is trying to move to a location beyond the maximum allowed
559 // in the free scroll mode, we make sure to end the scroll operation.
560 if (!mScroller.isFinished() &&
561 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
562 forceFinishScroller();
563 }
564
Adam Cohenf358a4b2013-07-23 16:47:31 -0700565 x = Math.min(x, mFreeScrollMaxScrollX);
566 x = Math.max(x, mFreeScrollMinScrollX);
567 }
568
Sunny Goyal70660032015-05-14 00:07:08 -0700569 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
570 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700571 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700572 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800573 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700574 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700575 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700576 overScroll(x - mMaxScrollX);
577 } else {
578 overScroll(x);
579 }
Adam Cohen68d73932010-11-15 10:50:58 -0800580 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700581 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700582 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800583 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700584 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700585 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700586 overScroll(x);
587 } else {
588 overScroll(x - mMaxScrollX);
589 }
Adam Cohen68d73932010-11-15 10:50:58 -0800590 }
591 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700592 if (mWasInOverscroll) {
593 overScroll(0);
594 mWasInOverscroll = false;
595 }
Adam Cohen68d73932010-11-15 10:50:58 -0800596 super.scrollTo(x, y);
597 }
598
Adam Cohen7d30a372013-07-01 17:03:59 -0700599 // Update the last motion events when scrolling
600 if (isReordering(true)) {
601 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
602 mLastMotionX = p[0];
603 mLastMotionY = p[1];
604 updateDragViewTranslationDuringDrag();
605 }
Michael Jurka0142d492010-08-25 17:46:15 -0700606 }
607
Adam Cohen53805212013-10-01 10:39:23 -0700608 private void sendScrollAccessibilityEvent() {
609 AccessibilityManager am =
610 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
611 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700612 if (mCurrentPage != getNextPage()) {
613 AccessibilityEvent ev =
614 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700615 ev.setScrollable(true);
616 ev.setScrollX(getScrollX());
617 ev.setScrollY(getScrollY());
618 ev.setMaxScrollX(mMaxScrollX);
619 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700620
Vadim Tryshevf4715972015-05-08 17:21:03 -0700621 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700622 }
Adam Cohen53805212013-10-01 10:39:23 -0700623 }
624 }
625
Michael Jurka0142d492010-08-25 17:46:15 -0700626 // we moved this functionality to a helper function so SmoothPagedView can reuse it
627 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700629 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700631 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700632 float scaleX = mFreeScroll ? getScaleX() : 1f;
633 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
634 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700635 }
Michael Jurka0142d492010-08-25 17:46:15 -0700636 invalidate();
637 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700638 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700639 sendScrollAccessibilityEvent();
640
Adam Cohen6f127a62014-06-12 14:54:41 -0700641 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700642 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700643 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700644
Adam Cohen73aa9752010-11-24 16:26:58 -0800645 // We don't want to trigger a page end moving unless the page has settled
646 // and the user has stopped scrolling
647 if (mTouchState == TOUCH_STATE_REST) {
648 pageEndMoving();
649 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700650
Adam Cohen7d30a372013-07-01 17:03:59 -0700651 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700652 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700653 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700654 if (am.isEnabled()) {
655 // Notify the user when the page changes
656 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700657 }
Michael Jurka0142d492010-08-25 17:46:15 -0700658 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 }
Michael Jurka0142d492010-08-25 17:46:15 -0700660 return false;
661 }
662
663 @Override
664 public void computeScroll() {
665 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 }
667
Adam Cohen96d30a12013-07-16 18:13:21 -0700668 public static class LayoutParams extends ViewGroup.LayoutParams {
669 public boolean isFullScreenPage = false;
670
671 /**
672 * {@inheritDoc}
673 */
674 public LayoutParams(int width, int height) {
675 super(width, height);
676 }
677
Sunny Goyal41b22c02015-05-14 15:20:48 -0700678 public LayoutParams(Context context, AttributeSet attrs) {
679 super(context, attrs);
680 }
681
Adam Cohen96d30a12013-07-16 18:13:21 -0700682 public LayoutParams(ViewGroup.LayoutParams source) {
683 super(source);
684 }
685 }
686
Sunny Goyal41b22c02015-05-14 15:20:48 -0700687 @Override
688 public LayoutParams generateLayoutParams(AttributeSet attrs) {
689 return new LayoutParams(getContext(), attrs);
690 }
691
692 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700693 protected LayoutParams generateDefaultLayoutParams() {
694 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
695 }
696
Sunny Goyal41b22c02015-05-14 15:20:48 -0700697 @Override
698 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
699 return new LayoutParams(p);
700 }
701
702 @Override
703 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
704 return p instanceof LayoutParams;
705 }
706
Adam Cohenedb40762013-07-18 16:45:45 -0700707 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700708 LayoutParams lp = generateDefaultLayoutParams();
709 lp.isFullScreenPage = true;
710 super.addView(page, 0, lp);
711 }
712
Adam Cohen410f3cd2013-09-22 12:09:32 -0700713 public int getNormalChildHeight() {
714 return mNormalChildHeight;
715 }
716
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 @Override
718 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700719 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700720 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
721 return;
722 }
723
Adam Cohen7d30a372013-07-01 17:03:59 -0700724 // We measure the dimensions of the PagedView to be larger than the pages so that when we
725 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700726 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
727 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
728 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700729 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800730 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700731 // viewport, we can be at most one and a half screens offset once we scale down
732 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700733 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
734 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700735
Winson Chungc82d2622013-11-06 13:23:29 -0800736 int parentWidthSize = (int) (2f * maxSize);
737 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700738 int scaledWidthSize, scaledHeightSize;
739 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700740 scaledWidthSize = (int) (parentWidthSize / mMinScale);
741 scaledHeightSize = (int) (parentHeightSize / mMinScale);
742 } else {
743 scaledWidthSize = widthSize;
744 scaledHeightSize = heightSize;
745 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700746 mViewport.set(0, 0, widthSize, heightSize);
747
748 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
749 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
750 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752
Winson Chung8aad6102012-05-11 16:27:49 -0700753 // Return early if we aren't given a proper dimension
754 if (widthSize <= 0 || heightSize <= 0) {
755 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
756 return;
757 }
758
Adam Lesinski6b879f02010-11-04 16:15:23 -0700759 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
760 * of the All apps view on XLarge displays to not take up more space then it needs. Width
761 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
762 * each page to have the same width.
763 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 final int verticalPadding = getPaddingTop() + getPaddingBottom();
765 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700766
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700767 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700769 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700770 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700771 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
772 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
773 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
774 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 final int childCount = getChildCount();
776 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700777 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700778 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100779 if (child.getVisibility() != GONE) {
780 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700781
Vladimir Marko2824b072013-10-04 16:42:17 +0100782 int childWidthMode;
783 int childHeightMode;
784 int childWidth;
785 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700786
Vladimir Marko2824b072013-10-04 16:42:17 +0100787 if (!lp.isFullScreenPage) {
788 if (lp.width == LayoutParams.WRAP_CONTENT) {
789 childWidthMode = MeasureSpec.AT_MOST;
790 } else {
791 childWidthMode = MeasureSpec.EXACTLY;
792 }
793
794 if (lp.height == LayoutParams.WRAP_CONTENT) {
795 childHeightMode = MeasureSpec.AT_MOST;
796 } else {
797 childHeightMode = MeasureSpec.EXACTLY;
798 }
799
Adam Cohen3b185e22013-10-29 14:45:58 -0700800 childWidth = getViewportWidth() - horizontalPadding
801 - mInsets.left - mInsets.right;
802 childHeight = getViewportHeight() - verticalPadding
803 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100804 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700805 } else {
806 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700807 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100808
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800809 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700810 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700811 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700812 if (referenceChildWidth == 0) {
813 referenceChildWidth = childWidth;
814 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700815
Vladimir Marko2824b072013-10-04 16:42:17 +0100816 final int childWidthMeasureSpec =
817 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
818 final int childHeightMeasureSpec =
819 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
820 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700821 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700822 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800824 }
825
Sunny Goyalcf25b522015-07-09 00:01:18 -0700826 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700828 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700829 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700830 return;
831 }
832
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700835
Adam Cohen7d30a372013-07-01 17:03:59 -0700836 int offsetX = getViewportOffsetX();
837 int offsetY = getViewportOffsetY();
838
839 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700840 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700841
Sunny Goyal70660032015-05-14 00:07:08 -0700842 final int startIndex = mIsRtl ? childCount - 1 : 0;
843 final int endIndex = mIsRtl ? -1 : childCount;
844 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700845
Adam Cohen7d30a372013-07-01 17:03:59 -0700846 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700847
Adam Cohen3b185e22013-10-29 14:45:58 -0700848 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000849 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700850
851 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700852 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
853 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700854 }
855
Adam Cohen0ffac432013-07-10 11:19:26 -0700856 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700857 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700859 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100860 int childTop;
861 if (lp.isFullScreenPage) {
862 childTop = offsetY;
863 } else {
864 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700865 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100866 }
867
Adam Cohen96d30a12013-07-16 18:13:21 -0700868 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700869 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800870
Winson Chung785d2eb2011-04-14 16:08:02 -0700871 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700872 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800873 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700874
Adam Cohen3b185e22013-10-29 14:45:58 -0700875 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
876 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000877
878 int pageGap = mPageSpacing;
879 int next = i + delta;
880 if (next != endIndex) {
881 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
882 } else {
883 nextLp = null;
884 }
885
886 // Prevent full screen pages from showing in the viewport
887 // when they are not the current page.
888 if (lp.isFullScreenPage) {
889 pageGap = getPaddingLeft();
890 } else if (nextLp != null && nextLp.isFullScreenPage) {
891 pageGap = getPaddingRight();
892 }
893
Sunny Goyala1fbd842015-05-20 13:40:27 -0700894 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 }
896 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700897
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700898 final LayoutTransition transition = getLayoutTransition();
899 // If the transition is running defer updating max scroll, as some empty pages could
900 // still be present, and a max scroll change could cause sudden jumps in scroll.
901 if (transition != null && transition.isRunning()) {
902 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
903
904 @Override
905 public void startTransition(LayoutTransition transition, ViewGroup container,
906 View view, int transitionType) { }
907
908 @Override
909 public void endTransition(LayoutTransition transition, ViewGroup container,
910 View view, int transitionType) {
911 // Wait until all transitions are complete.
912 if (!transition.isRunning()) {
913 transition.removeTransitionListener(this);
914 updateMaxScrollX();
915 }
916 }
917 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700918 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700919 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700920 }
921
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700922 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
923 updateCurrentPageScroll();
924 mFirstLayout = false;
925 }
926
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700927 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700928 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700929 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700930 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700931 } else {
932 setCurrentPage(getNextPage());
933 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700934 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700935 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700936
937 if (isReordering(true)) {
938 updateDragViewTranslationDuringDrag();
939 }
Winson Chunge3193b92010-09-10 11:44:42 -0700940 }
941
Sunny Goyala1fbd842015-05-20 13:40:27 -0700942 protected int getChildGap() {
943 return 0;
944 }
945
Sunny Goyal316490e2015-06-02 09:38:28 -0700946 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700947 int childCount = getChildCount();
948 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700949 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700950 mMaxScrollX = getScrollForPage(index);
951 } else {
952 mMaxScrollX = 0;
953 }
954 }
955
Adam Cohen3b185e22013-10-29 14:45:58 -0700956 public void setPageSpacing(int pageSpacing) {
957 mPageSpacing = pageSpacing;
958 requestLayout();
959 }
960
Sunny Goyal4d113a52015-05-27 10:05:28 -0700961 /**
962 * Called when the center screen changes during scrolling.
963 */
964 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700965
Winson Chunge3193b92010-09-10 11:44:42 -0700966 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700967 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700968 // Update the page indicator, we don't update the page indicator as we
969 // add/remove pages
970 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700971 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700972 mPageIndicator.addMarker(pageIndex,
973 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700974 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700975 }
976
Adam Cohen2591f6a2011-10-25 14:36:40 -0700977 // This ensures that when children are added, they get the correct transforms / alphas
978 // in accordance with any scroll effects.
979 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700980 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700981 invalidate();
982 }
983
Michael Jurka8b805b12012-04-18 14:23:14 -0700984 @Override
985 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700986 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700987 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700988 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700989 }
990
Winson Chungd2be3812013-07-16 11:11:32 -0700991 private void removeMarkerForView(int index) {
992 // Update the page indicator, we don't update the page indicator as we
993 // add/remove pages
994 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700995 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -0700996 }
997 }
998
999 @Override
1000 public void removeView(View v) {
1001 // XXX: We should find a better way to hook into this before the view
1002 // gets removed form its parent...
1003 removeMarkerForView(indexOfChild(v));
1004 super.removeView(v);
1005 }
1006 @Override
1007 public void removeViewInLayout(View v) {
1008 // XXX: We should find a better way to hook into this before the view
1009 // gets removed form its parent...
1010 removeMarkerForView(indexOfChild(v));
1011 super.removeViewInLayout(v);
1012 }
1013 @Override
1014 public void removeViewAt(int index) {
1015 // XXX: We should find a better way to hook into this before the view
1016 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001017 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001018 super.removeViewAt(index);
1019 }
1020 @Override
1021 public void removeAllViewsInLayout() {
1022 // Update the page indicator, we don't update the page indicator as we
1023 // add/remove pages
1024 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001025 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001026 }
1027
1028 super.removeAllViewsInLayout();
1029 }
1030
Adam Cohen73894962011-10-31 13:17:17 -07001031 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001032 if (index < 0 || index > getChildCount() - 1) return 0;
1033
Adam Cohenf698c6e2013-07-17 18:44:25 -07001034 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001035
Adam Cohenf698c6e2013-07-17 18:44:25 -07001036 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001037 }
1038
Adam Cohen6f127a62014-06-12 14:54:41 -07001039 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001040 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001041 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001042 }
1043
Michael Jurkadde558b2011-11-09 22:09:06 -08001044 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001045 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001046 range[0] = -1;
1047 range[1] = -1;
1048
Sunny Goyalee688162016-02-12 14:24:21 -08001049 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001050 final int visibleLeft = -getLeft();
1051 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001052 final Matrix pageShiftMatrix = getPageShiftMatrix();
1053 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001054
Sunny Goyalee688162016-02-12 14:24:21 -08001055 for (int i = 0; i < count; i++) {
1056 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001057
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001058 // Verify if the page bounds are within the visible range.
1059 sTmpRectF.left = 0;
1060 sTmpRectF.right = currPage.getMeasuredWidth();
1061 currPage.getMatrix().mapRect(sTmpRectF);
1062 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001063 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001064
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001065 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001066 if (range[0] == -1) {
1067 continue;
1068 } else {
1069 break;
1070 }
1071 }
Sunny Goyalee688162016-02-12 14:24:21 -08001072 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001073 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001074 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001075 }
1076 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001077
Sunny Goyalee688162016-02-12 14:24:21 -08001078 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001079 } else {
1080 range[0] = -1;
1081 range[1] = -1;
1082 }
1083 }
1084
Sunny Goyalee688162016-02-12 14:24:21 -08001085 protected Matrix getPageShiftMatrix() {
1086 return getMatrix();
1087 }
1088
Michael Jurka920d7f42012-05-14 16:29:55 -07001089 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001090 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001091 }
1092
Michael Jurkadde558b2011-11-09 22:09:06 -08001093 @Override
1094 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001095 // Find out which screens are visible; as an optimization we only call draw on them
1096 final int pageCount = getChildCount();
1097 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001098 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001099 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001100
1101 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1102 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1103 // set it for the next frame
1104 mForceScreenScrolled = false;
1105 screenScrolled(screenCenter);
1106 mLastScreenCenter = screenCenter;
1107 }
1108
Michael Jurkadde558b2011-11-09 22:09:06 -08001109 getVisiblePages(mTempVisiblePagesRange);
1110 final int leftScreen = mTempVisiblePagesRange[0];
1111 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001112 if (leftScreen != -1 && rightScreen != -1) {
1113 final long drawingTime = getDrawingTime();
1114 // Clip to the bounds
1115 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001116 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1117 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001118
Adam Cohen7d30a372013-07-01 17:03:59 -07001119 // Draw all the children, leaving the drag view for last
1120 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001121 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001122 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001123 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001124 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001125 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001126 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001127 // Draw the drag view on top (if there is one)
1128 if (mDragView != null) {
1129 drawChild(canvas, mDragView, drawingTime);
1130 }
1131
Winson Chungc6f10b92011-11-14 11:39:07 -08001132 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001133 }
Michael Jurka0142d492010-08-25 17:46:15 -07001134 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 }
1136
1137 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001138 public void draw(Canvas canvas) {
1139 super.draw(canvas);
1140 if (getPageCount() > 0) {
1141 if (!mEdgeGlowLeft.isFinished()) {
1142 final int restoreCount = canvas.save();
1143 Rect display = mViewport;
1144 canvas.translate(display.left, display.top);
1145 canvas.rotate(270);
1146
1147 getEdgeVerticalPostion(sTmpIntPoint);
1148 canvas.translate(display.top - sTmpIntPoint[1], 0);
1149 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1150 if (mEdgeGlowLeft.draw(canvas)) {
1151 postInvalidateOnAnimation();
1152 }
1153 canvas.restoreToCount(restoreCount);
1154 }
1155 if (!mEdgeGlowRight.isFinished()) {
1156 final int restoreCount = canvas.save();
1157 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001158 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001159 canvas.rotate(90);
1160
1161 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001162
Sunny Goyal93264612015-11-23 11:47:50 -08001163 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1164 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001165 if (mEdgeGlowRight.draw(canvas)) {
1166 postInvalidateOnAnimation();
1167 }
1168 canvas.restoreToCount(restoreCount);
1169 }
1170 }
1171 }
1172
1173 /**
1174 * Returns the top and bottom position for the edge effect.
1175 */
1176 protected abstract void getEdgeVerticalPostion(int[] pos);
1177
1178 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001180 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001181 if (page != mCurrentPage || !mScroller.isFinished()) {
1182 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 return true;
1184 }
1185 return false;
1186 }
1187
1188 @Override
1189 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001190 int focusablePage;
1191 if (mNextPage != INVALID_PAGE) {
1192 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001194 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 }
Winson Chung86f77532010-08-24 11:08:22 -07001196 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001198 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 }
1200 return false;
1201 }
1202
1203 @Override
1204 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001205 if (super.dispatchUnhandledMove(focused, direction)) {
1206 return true;
1207 }
1208
1209 if (mIsRtl) {
1210 if (direction == View.FOCUS_LEFT) {
1211 direction = View.FOCUS_RIGHT;
1212 } else if (direction == View.FOCUS_RIGHT) {
1213 direction = View.FOCUS_LEFT;
1214 }
1215 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (getCurrentPage() > 0) {
1218 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 return true;
1220 }
1221 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001222 if (getCurrentPage() < getPageCount() - 1) {
1223 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 return true;
1225 }
1226 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001227 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 }
1229
1230 @Override
1231 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001232 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001233 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001234 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 }
1236 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001237 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001238 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 }
1240 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001241 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001242 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
1244 }
1245 }
1246
1247 /**
1248 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001249 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 *
Winson Chung86f77532010-08-24 11:08:22 -07001251 * This happens when live folders requery, and if they're off page, they
1252 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 */
1254 @Override
1255 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001256 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 View v = focused;
1258 while (true) {
1259 if (v == current) {
1260 super.focusableViewAvailable(focused);
1261 return;
1262 }
1263 if (v == this) {
1264 return;
1265 }
1266 ViewParent parent = v.getParent();
1267 if (parent instanceof View) {
1268 v = (View)v.getParent();
1269 } else {
1270 return;
1271 }
1272 }
1273 }
1274
1275 /**
1276 * {@inheritDoc}
1277 */
1278 @Override
1279 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1280 if (disallowIntercept) {
1281 // We need to make sure to cancel our long press if
1282 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001283 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001284 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 }
1286 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1287 }
1288
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001289 /**
1290 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1291 */
1292 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001293 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001294 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001295 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001296 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001297 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001298 }
1299
1300 /**
1301 * Return true if a tap at (x, y) should trigger a flip to the next page.
1302 */
1303 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001304 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001305 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001306 }
1307 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001308 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001309 }
Winson Chung52aee602013-01-30 12:01:02 -08001310
Adam Cohen7d30a372013-07-01 17:03:59 -07001311 /** Returns whether x and y originated within the buffered viewport */
1312 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001313 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001314 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001315 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001316 }
1317
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 @Override
1319 public boolean onInterceptTouchEvent(MotionEvent ev) {
1320 /*
1321 * This method JUST determines whether we want to intercept the motion.
1322 * If we return true, onTouchEvent will be called and we do the actual
1323 * scrolling there.
1324 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001325 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001326
Winson Chung45e1d6e2010-11-09 17:19:49 -08001327 // Skip touch handling if there are no pages to swipe
1328 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1329
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 /*
1331 * Shortcut the most recurring case: the user is in the dragging
1332 * state and he is moving his finger. We want to intercept this
1333 * motion.
1334 */
1335 final int action = ev.getAction();
1336 if ((action == MotionEvent.ACTION_MOVE) &&
1337 (mTouchState == TOUCH_STATE_SCROLLING)) {
1338 return true;
1339 }
1340
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 switch (action & MotionEvent.ACTION_MASK) {
1342 case MotionEvent.ACTION_MOVE: {
1343 /*
1344 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1345 * whether the user has moved far enough from his original down touch.
1346 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001347 if (mActivePointerId != INVALID_POINTER) {
1348 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001349 }
1350 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1351 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1352 // i.e. fall through to the next case (don't break)
1353 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1354 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001355 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 }
1357
1358 case MotionEvent.ACTION_DOWN: {
1359 final float x = ev.getX();
1360 final float y = ev.getY();
1361 // Remember location of down touch
1362 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001363 mDownMotionY = y;
1364 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 mLastMotionX = x;
1366 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001367 float[] p = mapPointFromViewToParent(this, x, y);
1368 mParentDownMotionX = p[0];
1369 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001370 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001371 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001373
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 /*
1375 * If being flinged and user touches the screen, initiate drag;
1376 * otherwise don't. mScroller.isFinished should be false when
1377 * being flinged.
1378 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001379 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001380 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001381
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001382 if (finishedScrolling) {
1383 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001384 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001385 setCurrentPage(getNextPage());
1386 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001387 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001389 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1390 mTouchState = TOUCH_STATE_SCROLLING;
1391 } else {
1392 mTouchState = TOUCH_STATE_REST;
1393 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001394 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001395
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397 }
1398
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001400 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001401 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 break;
1403
1404 case MotionEvent.ACTION_POINTER_UP:
1405 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001406 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 break;
1408 }
1409
1410 /*
1411 * The only time we want to intercept motion events is if we are in the
1412 * drag mode.
1413 */
1414 return mTouchState != TOUCH_STATE_REST;
1415 }
1416
Adam Cohenf8d28232011-02-01 21:47:00 -08001417 protected void determineScrollingStart(MotionEvent ev) {
1418 determineScrollingStart(ev, 1.0f);
1419 }
1420
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 /*
1422 * Determines if we should change the touch state to start scrolling after the
1423 * user moves their touch point too far.
1424 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001425 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001426 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001428 if (pointerIndex == -1) return;
1429
1430 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 final float x = ev.getX(pointerIndex);
1432 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001433 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1434
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001436
Adam Cohenf8d28232011-02-01 21:47:00 -08001437 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001439
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001440 if (xMoved) {
1441 // Scroll if the user moved far enough along the X axis
1442 mTouchState = TOUCH_STATE_SCROLLING;
1443 mTotalMotionX += Math.abs(mLastMotionX - x);
1444 mLastMotionX = x;
1445 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001446 onScrollInteractionBegin();
1447 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001448 }
1449 }
1450
1451 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001452 // Try canceling the long press. It could also have been scheduled
1453 // by a distant descendant, so use the mAllowLongPress flag to block
1454 // everything
1455 final View currentPage = getPageAt(mCurrentPage);
1456 if (currentPage != null) {
1457 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 }
1459 }
1460
Adam Cohenb5ba0972011-09-07 18:02:31 -07001461 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001462 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001463
Adam Cohenedb40762013-07-18 16:45:45 -07001464 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001465 int count = getChildCount();
1466
1467 final int totalDistance;
1468
1469 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001470 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001471 adjacentPage = page - 1;
1472 }
1473
1474 if (adjacentPage < 0 || adjacentPage > count - 1) {
1475 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1476 } else {
1477 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1478 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001479
1480 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001481 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1482 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001483 return scrollProgress;
1484 }
1485
Adam Cohenedb40762013-07-18 16:45:45 -07001486 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001487 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001488 return 0;
1489 } else {
1490 return mPageScrolls[index];
1491 }
1492 }
1493
Adam Cohen564a2e72013-10-09 14:47:32 -07001494 // While layout transitions are occurring, a child's position may stray from its baseline
1495 // position. This method returns the magnitude of this stray at any given time.
1496 public int getLayoutTransitionOffsetForPage(int index) {
1497 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1498 return 0;
1499 } else {
1500 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001501
1502 int scrollOffset = 0;
1503 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1504 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001505 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001506 }
1507
Adam Cohen564a2e72013-10-09 14:47:32 -07001508 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1509 return (int) (child.getX() - baselineX);
1510 }
1511 }
1512
Adam Cohenb5ba0972011-09-07 18:02:31 -07001513 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001515 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001516 if (f < 0) {
1517 mEdgeGlowLeft.onPull(-f);
1518 } else if (f > 0) {
1519 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001520 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001521 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001522 }
1523 invalidate();
1524 }
1525
Adam Cohenb5ba0972011-09-07 18:02:31 -07001526 protected void overScroll(float amount) {
1527 dampedOverScroll(amount);
1528 }
1529
Winson Chungdc61c4d2015-04-20 18:26:57 -07001530 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001531 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001532 }
1533
Winson Chungdc61c4d2015-04-20 18:26:57 -07001534 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001535 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001536 }
1537
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001538 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001539 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001540 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001541 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1542 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1543 } else {
1544 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1545 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1546 }
1547 }
1548
Adam Cohenf9618852013-11-08 06:45:03 -08001549 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001550 mFreeScroll = freeScroll;
1551
Adam Cohenf9618852013-11-08 06:45:03 -08001552 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001553 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001554 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001555 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1556 setCurrentPage(mTempVisiblePagesRange[0]);
1557 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1558 setCurrentPage(mTempVisiblePagesRange[1]);
1559 }
1560 }
1561
1562 setEnableOverscroll(!freeScroll);
1563 }
1564
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001565 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001566 mAllowOverScroll = enable;
1567 }
1568
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001569 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001570 if (mDragView != null) {
1571 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1572 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001573 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001574 int minDistance = Integer.MAX_VALUE;
1575 int minIndex = indexOfChild(mDragView);
1576 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1577 View page = getPageAt(i);
1578 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1579 int d = Math.abs(dragX - pageX);
1580 if (d < minDistance) {
1581 minIndex = i;
1582 minDistance = d;
1583 }
1584 }
1585 return minIndex;
1586 }
1587 return -1;
1588 }
1589
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 @Override
1591 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001592 super.onTouchEvent(ev);
1593
Winson Chung45e1d6e2010-11-09 17:19:49 -08001594 // Skip touch handling if there are no pages to swipe
1595 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1596
Michael Jurkab8f06722010-10-10 15:58:46 -07001597 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001598
1599 final int action = ev.getAction();
1600
1601 switch (action & MotionEvent.ACTION_MASK) {
1602 case MotionEvent.ACTION_DOWN:
1603 /*
1604 * If being flinged and user touches, stop the fling. isFinished
1605 * will be false if being flinged.
1606 */
1607 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001608 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001609 }
1610
1611 // Remember where the motion event started
1612 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001613 mDownMotionY = mLastMotionY = ev.getY();
1614 mDownScrollX = getScrollX();
1615 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1616 mParentDownMotionX = p[0];
1617 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001618 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001619 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001620 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001621
Michael Jurka0142d492010-08-25 17:46:15 -07001622 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001623 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001624 pageBeginMoving();
1625 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001626 break;
1627
1628 case MotionEvent.ACTION_MOVE:
1629 if (mTouchState == TOUCH_STATE_SCROLLING) {
1630 // Scroll to follow the motion event
1631 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001632
1633 if (pointerIndex == -1) return true;
1634
Winson Chung321e9ee2010-08-09 13:37:56 -07001635 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001636 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001637
Adam Cohenaefd4e12011-02-14 16:39:38 -08001638 mTotalMotionX += Math.abs(deltaX);
1639
Winson Chungc0844aa2011-02-02 15:25:58 -08001640 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1641 // keep the remainder because we are actually testing if we've moved from the last
1642 // scrolled position (which is discrete).
1643 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001644 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001645 mLastMotionX = x;
1646 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001647 } else {
1648 awakenScrollBars();
1649 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001650 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1651 // Update the last motion position
1652 mLastMotionX = ev.getX();
1653 mLastMotionY = ev.getY();
1654
1655 // Update the parent down so that our zoom animations take this new movement into
1656 // account
1657 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1658 mParentDownMotionX = pt[0];
1659 mParentDownMotionY = pt[1];
1660 updateDragViewTranslationDuringDrag();
1661
1662 // Find the closest page to the touch point
1663 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001664
Adam Cohen7d30a372013-07-01 17:03:59 -07001665 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1666 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1667 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1668 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1669
Adam Cohenf358a4b2013-07-23 16:47:31 -07001670 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001671 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001672 mTempVisiblePagesRange[0] = 0;
1673 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001674 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001675 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1676 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1677 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1678 mSidePageHoverIndex = pageUnderPointIndex;
1679 mSidePageHoverRunnable = new Runnable() {
1680 @Override
1681 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001682 // Setup the scroll to the correct page before we swap the views
1683 snapToPage(pageUnderPointIndex);
1684
1685 // For each of the pages between the paged view and the drag view,
1686 // animate them from the previous position to the new position in
1687 // the layout (as a result of the drag view moving in the layout)
1688 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1689 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1690 dragViewIndex + 1 : pageUnderPointIndex;
1691 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1692 dragViewIndex - 1 : pageUnderPointIndex;
1693 for (int i = lowerIndex; i <= upperIndex; ++i) {
1694 View v = getChildAt(i);
1695 // dragViewIndex < pageUnderPointIndex, so after we remove the
1696 // drag view all subsequent views to pageUnderPointIndex will
1697 // shift down.
1698 int oldX = getViewportOffsetX() + getChildOffset(i);
1699 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1700
1701 // Animate the view translation from its old position to its new
1702 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001703 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001704 if (anim != null) {
1705 anim.cancel();
1706 }
1707
1708 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001709 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001710 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001711 anim.start();
1712 v.setTag(anim);
1713 }
1714
1715 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001717 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001718 if (mPageIndicator != null) {
1719 mPageIndicator.setActiveMarker(getNextPage());
1720 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001721 }
1722 };
1723 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1724 }
1725 } else {
1726 removeCallbacks(mSidePageHoverRunnable);
1727 mSidePageHoverIndex = -1;
1728 }
Adam Cohen564976a2010-10-13 18:52:07 -07001729 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 determineScrollingStart(ev);
1731 }
1732 break;
1733
1734 case MotionEvent.ACTION_UP:
1735 if (mTouchState == TOUCH_STATE_SCROLLING) {
1736 final int activePointerId = mActivePointerId;
1737 final int pointerIndex = ev.findPointerIndex(activePointerId);
1738 final float x = ev.getX(pointerIndex);
1739 final VelocityTracker velocityTracker = mVelocityTracker;
1740 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1741 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001742 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001743 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001744 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1745 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001746
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001747 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1748
Adam Cohen00481b32011-11-18 12:03:48 -08001749 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001750 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001751
Adam Cohenf358a4b2013-07-23 16:47:31 -07001752 if (!mFreeScroll) {
1753 // In the case that the page is moved far to one direction and then is flung
1754 // in the opposite direction, we use a threshold to determine whether we should
1755 // just return to the starting page, or if we should skip one further.
1756 boolean returnToOriginalPage = false;
1757 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1758 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1759 returnToOriginalPage = true;
1760 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001761
Adam Cohenf358a4b2013-07-23 16:47:31 -07001762 int finalPage;
1763 // We give flings precedence over large moves, which is why we short-circuit our
1764 // test for a large move if a fling has been registered. That is, a large
1765 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001766 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1767 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001768 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1769 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1770 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1771 snapToPageWithVelocity(finalPage, velocityX);
1772 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1773 (isFling && isVelocityXLeft)) &&
1774 mCurrentPage < getChildCount() - 1) {
1775 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1776 snapToPageWithVelocity(finalPage, velocityX);
1777 } else {
1778 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001779 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001780 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001781 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001782 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001783 }
1784
1785 float scaleX = getScaleX();
1786 int vX = (int) (-velocityX * scaleX);
1787 int initialScrollX = (int) (getScrollX() * scaleX);
1788
Adam Cohenf9618852013-11-08 06:45:03 -08001789 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001790 mScroller.fling(initialScrollX,
1791 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1792 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001794 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001795 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1796 // at this point we have not moved beyond the touch slop
1797 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1798 // we can just page
1799 int nextPage = Math.max(0, mCurrentPage - 1);
1800 if (nextPage != mCurrentPage) {
1801 snapToPage(nextPage);
1802 } else {
1803 snapToDestination();
1804 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001805 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001806 // at this point we have not moved beyond the touch slop
1807 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1808 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001809 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1810 if (nextPage != mCurrentPage) {
1811 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001812 } else {
1813 snapToDestination();
1814 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1816 // Update the last motion position
1817 mLastMotionX = ev.getX();
1818 mLastMotionY = ev.getY();
1819
1820 // Update the parent down so that our zoom animations take this new movement into
1821 // account
1822 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1823 mParentDownMotionX = pt[0];
1824 mParentDownMotionY = pt[1];
1825 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001826 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001827 if (!mCancelTap) {
1828 onUnhandledTap(ev);
1829 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001830 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001831
1832 // Remove the callback to wait for the side page hover timeout
1833 removeCallbacks(mSidePageHoverRunnable);
1834 // End any intermediate reordering states
1835 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001836 break;
1837
1838 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001839 if (mTouchState == TOUCH_STATE_SCROLLING) {
1840 snapToDestination();
1841 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001842 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001843 break;
1844
1845 case MotionEvent.ACTION_POINTER_UP:
1846 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001847 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001848 break;
1849 }
1850
1851 return true;
1852 }
1853
Adam Cohen7d30a372013-07-01 17:03:59 -07001854 private void resetTouchState() {
1855 releaseVelocityTracker();
1856 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001857 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001858 mTouchState = TOUCH_STATE_REST;
1859 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001860 mEdgeGlowLeft.onRelease();
1861 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001862 }
1863
Adam Cohenc2d6e892014-10-16 09:49:24 -07001864 /**
1865 * Triggered by scrolling via touch
1866 */
1867 protected void onScrollInteractionBegin() {
1868 }
1869
1870 protected void onScrollInteractionEnd() {
1871 }
1872
Adam Cohen1697b792013-09-17 19:08:21 -07001873 protected void onUnhandledTap(MotionEvent ev) {
1874 ((Launcher) getContext()).onClick(this);
1875 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001876
Winson Chung185d7162011-02-28 13:47:29 -08001877 @Override
1878 public boolean onGenericMotionEvent(MotionEvent event) {
1879 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1880 switch (event.getAction()) {
1881 case MotionEvent.ACTION_SCROLL: {
1882 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1883 final float vscroll;
1884 final float hscroll;
1885 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1886 vscroll = 0;
1887 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1888 } else {
1889 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1890 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1891 }
1892 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001893 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001894 : (hscroll > 0 || vscroll > 0);
1895 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001896 scrollRight();
1897 } else {
1898 scrollLeft();
1899 }
1900 return true;
1901 }
1902 }
1903 }
1904 }
1905 return super.onGenericMotionEvent(event);
1906 }
1907
Michael Jurkab8f06722010-10-10 15:58:46 -07001908 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1909 if (mVelocityTracker == null) {
1910 mVelocityTracker = VelocityTracker.obtain();
1911 }
1912 mVelocityTracker.addMovement(ev);
1913 }
1914
1915 private void releaseVelocityTracker() {
1916 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001917 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001918 mVelocityTracker.recycle();
1919 mVelocityTracker = null;
1920 }
1921 }
1922
Winson Chung321e9ee2010-08-09 13:37:56 -07001923 private void onSecondaryPointerUp(MotionEvent ev) {
1924 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1925 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1926 final int pointerId = ev.getPointerId(pointerIndex);
1927 if (pointerId == mActivePointerId) {
1928 // This was our active pointer going up. Choose a new
1929 // active pointer and adjust accordingly.
1930 // TODO: Make this decision more intelligent.
1931 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1932 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1933 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001934 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 mActivePointerId = ev.getPointerId(newPointerIndex);
1936 if (mVelocityTracker != null) {
1937 mVelocityTracker.clear();
1938 }
1939 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001940 }
1941
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 @Override
1943 public void requestChildFocus(View child, View focused) {
1944 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001945 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001946 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001947 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 }
1949 }
1950
Adam Cohend19d3ca2010-09-15 14:43:42 -07001951 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001952 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001954 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001955 final int childCount = getChildCount();
1956 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001957 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001958 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001960 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001961 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1962 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1963 minDistanceFromScreenCenter = distanceFromScreenCenter;
1964 minDistanceFromScreenCenterIndex = i;
1965 }
1966 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001967 return minDistanceFromScreenCenterIndex;
1968 }
1969
1970 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001971 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001972 }
1973
Adam Cohene0f66b52010-11-23 15:06:07 -08001974 private static class ScrollInterpolator implements Interpolator {
1975 public ScrollInterpolator() {
1976 }
1977
1978 public float getInterpolation(float t) {
1979 t -= 1.0f;
1980 return t*t*t*t*t + 1;
1981 }
1982 }
1983
1984 // We want the duration of the page snap animation to be influenced by the distance that
1985 // the screen has to travel, however, we don't want this duration to be effected in a
1986 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1987 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001988 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001989 f -= 0.5f; // center the values about 0.
1990 f *= 0.3f * Math.PI / 2.0f;
1991 return (float) Math.sin(f);
1992 }
1993
Michael Jurka0142d492010-08-25 17:46:15 -07001994 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001995 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001996 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001997
Adam Cohenedb40762013-07-18 16:45:45 -07001998 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001999 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002000 int duration = 0;
2001
Sunny Goyal4d113a52015-05-27 10:05:28 -07002002 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002003 // If the velocity is low enough, then treat this more as an automatic page advance
2004 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002005 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002006 return;
2007 }
2008
2009 // Here we compute a "distance" that will be used in the computation of the overall
2010 // snap duration. This is a function of the actual distance that needs to be traveled;
2011 // we keep this value close to half screen size in order to reduce the variance in snap
2012 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002013 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002014 float distance = halfScreenSize + halfScreenSize *
2015 distanceInfluenceForSnapDuration(distanceRatio);
2016
2017 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002018 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002019
2020 // we want the page's snap velocity to approximately match the velocity at which the
2021 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002022 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2023 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002024
2025 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002026 }
2027
Sunny Goyal1740d902015-05-27 11:14:01 -07002028 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002029 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002030 }
2031
Adam Cohenf9c184a2016-01-15 16:47:43 -08002032 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002033 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002034 }
2035
Michael Jurka0142d492010-08-25 17:46:15 -07002036 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002037 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002038 }
2039
Adam Cohenf9618852013-11-08 06:45:03 -08002040 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2041 snapToPage(whichPage, duration, false, interpolator);
2042 }
2043
2044 protected void snapToPage(int whichPage, int duration, boolean immediate,
2045 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002046 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002047
Adam Cohenedb40762013-07-18 16:45:45 -07002048 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002049 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002050 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002051 }
2052
2053 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002054 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002055 }
Michael Jurka0142d492010-08-25 17:46:15 -07002056
Adam Cohenf9618852013-11-08 06:45:03 -08002057 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2058 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002059 whichPage = validateNewPage(whichPage);
2060
Adam Cohen7d30a372013-07-01 17:03:59 -07002061 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002062
2063 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002064 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002065 if (immediate) {
2066 duration = 0;
2067 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002068 duration = Math.abs(delta);
2069 }
2070
Adam Cohenf358a4b2013-07-23 16:47:31 -07002071 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002072 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002073 }
Adam Cohenf9618852013-11-08 06:45:03 -08002074
2075 if (interpolator != null) {
2076 mScroller.setInterpolator(interpolator);
2077 } else {
2078 mScroller.setInterpolator(mDefaultInterpolator);
2079 }
2080
Sunny Goyalc86df472016-02-25 09:19:38 -08002081 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002082
Adam Cohen674531f2013-12-13 15:07:14 -08002083 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002084
2085 // Trigger a compute() to finish switching pages if necessary
2086 if (immediate) {
2087 computeScroll();
2088 }
2089
2090 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 invalidate();
2092 }
2093
Winson Chung321e9ee2010-08-09 13:37:56 -07002094 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002095 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002096 }
2097
2098 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002099 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002100 }
2101
Adam Cohendbdff6b2013-09-18 19:09:15 -07002102 @Override
2103 public boolean performLongClick() {
2104 mCancelTap = true;
2105 return super.performLongClick();
2106 }
2107
Winson Chung321e9ee2010-08-09 13:37:56 -07002108 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002109 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002110
2111 SavedState(Parcelable superState) {
2112 super(superState);
2113 }
2114
Adam Cohen091440a2015-03-18 14:16:05 -07002115 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002116 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002117 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 }
2119
2120 @Override
2121 public void writeToParcel(Parcel out, int flags) {
2122 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002123 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 }
2125
2126 public static final Parcelable.Creator<SavedState> CREATOR =
2127 new Parcelable.Creator<SavedState>() {
2128 public SavedState createFromParcel(Parcel in) {
2129 return new SavedState(in);
2130 }
2131
2132 public SavedState[] newArray(int size) {
2133 return new SavedState[size];
2134 }
2135 };
2136 }
2137
Adam Cohen7d30a372013-07-01 17:03:59 -07002138 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002139 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002140 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002141 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2142 .translationX(0)
2143 .translationY(0)
2144 .scaleX(1)
2145 .scaleY(1)
2146 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002147 anim.addListener(new AnimatorListenerAdapter() {
2148 @Override
2149 public void onAnimationEnd(Animator animation) {
2150 onPostReorderingAnimationCompleted();
2151 }
2152 });
2153 anim.start();
2154 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002155 }
2156
Sunny Goyal1d08f702015-05-04 15:50:25 -07002157 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002158 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2159 mTouchState = TOUCH_STATE_REORDERING;
2160 mIsReordering = true;
2161
Adam Cohen7d30a372013-07-01 17:03:59 -07002162 // We must invalidate to trigger a redraw to update the layers such that the drag view
2163 // is always drawn on top
2164 invalidate();
2165 }
2166
Adam Cohen091440a2015-03-18 14:16:05 -07002167 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002168 // Trigger the callback when reordering has settled
2169 --mPostReorderingPreZoomInRemainingAnimationCount;
2170 if (mPostReorderingPreZoomInRunnable != null &&
2171 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2172 mPostReorderingPreZoomInRunnable.run();
2173 mPostReorderingPreZoomInRunnable = null;
2174 }
2175 }
2176
Sunny Goyal1d08f702015-05-04 15:50:25 -07002177 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002178 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002179 }
2180
Adam Cohenf358a4b2013-07-23 16:47:31 -07002181 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002182 int dragViewIndex = indexOfChild(v);
2183
Adam Cohen327acfe2014-06-06 11:52:52 -07002184 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002185
Adam Cohen7d30a372013-07-01 17:03:59 -07002186 mTempVisiblePagesRange[0] = 0;
2187 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002188 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002189 mReorderingStarted = true;
2190
2191 // Check if we are within the reordering range
2192 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002193 dragViewIndex <= mTempVisiblePagesRange[1]) {
2194 // Find the drag view under the pointer
2195 mDragView = getChildAt(dragViewIndex);
2196 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2197 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002198 snapToPage(getPageNearestToCenterOfScreen());
2199 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002200 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002201 return true;
2202 }
2203 return false;
2204 }
2205
2206 boolean isReordering(boolean testTouchState) {
2207 boolean state = mIsReordering;
2208 if (testTouchState) {
2209 state &= (mTouchState == TOUCH_STATE_REORDERING);
2210 }
2211 return state;
2212 }
2213 void endReordering() {
2214 // For simplicity, we call endReordering sometimes even if reordering was never started.
2215 // In that case, we don't want to do anything.
2216 if (!mReorderingStarted) return;
2217 mReorderingStarted = false;
2218
2219 // If we haven't flung-to-delete the current child, then we just animate the drag view
2220 // back into position
2221 final Runnable onCompleteRunnable = new Runnable() {
2222 @Override
2223 public void run() {
2224 onEndReordering();
2225 }
2226 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002227
2228 mPostReorderingPreZoomInRunnable = new Runnable() {
2229 public void run() {
2230 onCompleteRunnable.run();
2231 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002232 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002233 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002234
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002235 mPostReorderingPreZoomInRemainingAnimationCount =
2236 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2237 // Snap to the current page
2238 snapToPage(indexOfChild(mDragView), 0);
2239 // Animate the drag view back to the front position
2240 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002241 }
2242
Winson Chung6a0f57d2011-06-29 20:10:49 -07002243 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002244 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002245 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002246 @Override
2247 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2248 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002249 info.setScrollable(getPageCount() > 1);
2250 if (getCurrentPage() < getPageCount() - 1) {
2251 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2252 }
2253 if (getCurrentPage() > 0) {
2254 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2255 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002256 info.setClassName(getClass().getName());
2257
2258 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2259 // Besides disabling the accessibility long-click, this also prevents this view from getting
2260 // accessibility focus.
2261 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002262 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002263 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2264 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002265 }
2266
2267 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002268 public void sendAccessibilityEvent(int eventType) {
2269 // Don't let the view send real scroll events.
2270 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2271 super.sendAccessibilityEvent(eventType);
2272 }
2273 }
2274
2275 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002276 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2277 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002278 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002279 }
2280
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002281 @Override
2282 public boolean performAccessibilityAction(int action, Bundle arguments) {
2283 if (super.performAccessibilityAction(action, arguments)) {
2284 return true;
2285 }
2286 switch (action) {
2287 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2288 if (getCurrentPage() < getPageCount() - 1) {
2289 scrollRight();
2290 return true;
2291 }
2292 } break;
2293 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2294 if (getCurrentPage() > 0) {
2295 scrollLeft();
2296 return true;
2297 }
2298 } break;
2299 }
2300 return false;
2301 }
2302
Adam Cohen0ffac432013-07-10 11:19:26 -07002303 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002304 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002305 getNextPage() + 1, getChildCount());
2306 }
2307
Winson Chungd11265e2011-08-30 13:37:23 -07002308 @Override
2309 public boolean onHoverEvent(android.view.MotionEvent event) {
2310 return true;
2311 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002312}