blob: b3a714b81fc11406949d26f46694f9da93517a46 [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) {
Michael Jurka0142d492010-08-25 17:46:15 -07001045 final int pageCount = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001046 range[0] = -1;
1047 range[1] = -1;
1048
Michael Jurkac4fb9172010-09-02 17:19:20 -07001049 if (pageCount > 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001050 int lastVisiblePageIndex = 0;
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001051 final int visibleLeft = -getLeft();
1052 final int visibleRight = visibleLeft + getViewportWidth();
Adam Cohen7d30a372013-07-01 17:03:59 -07001053
Tony Wickham29d853c2015-09-08 10:35:56 -07001054 for (int currPageIndex = 0; currPageIndex < pageCount; currPageIndex++) {
1055 View currPage = getPageAt(currPageIndex);
Winson Chungc9ca2982013-07-19 12:07:38 -07001056
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001057 // Verify if the page bounds are within the visible range.
1058 sTmpRectF.left = 0;
1059 sTmpRectF.right = currPage.getMeasuredWidth();
1060 currPage.getMatrix().mapRect(sTmpRectF);
1061 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
1062 getMatrix().mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001063
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001064 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001065 if (range[0] == -1) {
1066 continue;
1067 } else {
1068 break;
1069 }
1070 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001071 if (range[0] < 0) {
Tony Wickham29d853c2015-09-08 10:35:56 -07001072 range[0] = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001073 }
Tony Wickham29d853c2015-09-08 10:35:56 -07001074 lastVisiblePageIndex = currPageIndex;
Winson Chungc9ca2982013-07-19 12:07:38 -07001075 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001076
Tony Wickham29d853c2015-09-08 10:35:56 -07001077 range[1] = lastVisiblePageIndex;
Michael Jurkadde558b2011-11-09 22:09:06 -08001078 } else {
1079 range[0] = -1;
1080 range[1] = -1;
1081 }
1082 }
1083
Michael Jurka920d7f42012-05-14 16:29:55 -07001084 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001085 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001086 }
1087
Michael Jurkadde558b2011-11-09 22:09:06 -08001088 @Override
1089 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001090 // Find out which screens are visible; as an optimization we only call draw on them
1091 final int pageCount = getChildCount();
1092 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001093 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001094 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001095
1096 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1097 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1098 // set it for the next frame
1099 mForceScreenScrolled = false;
1100 screenScrolled(screenCenter);
1101 mLastScreenCenter = screenCenter;
1102 }
1103
Michael Jurkadde558b2011-11-09 22:09:06 -08001104 getVisiblePages(mTempVisiblePagesRange);
1105 final int leftScreen = mTempVisiblePagesRange[0];
1106 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001107 if (leftScreen != -1 && rightScreen != -1) {
1108 final long drawingTime = getDrawingTime();
1109 // Clip to the bounds
1110 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001111 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1112 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001113
Adam Cohen7d30a372013-07-01 17:03:59 -07001114 // Draw all the children, leaving the drag view for last
1115 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001116 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001117 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001118 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001119 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001120 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001121 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001122 // Draw the drag view on top (if there is one)
1123 if (mDragView != null) {
1124 drawChild(canvas, mDragView, drawingTime);
1125 }
1126
Winson Chungc6f10b92011-11-14 11:39:07 -08001127 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001128 }
Michael Jurka0142d492010-08-25 17:46:15 -07001129 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001130 }
1131
1132 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001133 public void draw(Canvas canvas) {
1134 super.draw(canvas);
1135 if (getPageCount() > 0) {
1136 if (!mEdgeGlowLeft.isFinished()) {
1137 final int restoreCount = canvas.save();
1138 Rect display = mViewport;
1139 canvas.translate(display.left, display.top);
1140 canvas.rotate(270);
1141
1142 getEdgeVerticalPostion(sTmpIntPoint);
1143 canvas.translate(display.top - sTmpIntPoint[1], 0);
1144 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1145 if (mEdgeGlowLeft.draw(canvas)) {
1146 postInvalidateOnAnimation();
1147 }
1148 canvas.restoreToCount(restoreCount);
1149 }
1150 if (!mEdgeGlowRight.isFinished()) {
1151 final int restoreCount = canvas.save();
1152 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001153 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001154 canvas.rotate(90);
1155
1156 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001157
Sunny Goyal93264612015-11-23 11:47:50 -08001158 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1159 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001160 if (mEdgeGlowRight.draw(canvas)) {
1161 postInvalidateOnAnimation();
1162 }
1163 canvas.restoreToCount(restoreCount);
1164 }
1165 }
1166 }
1167
1168 /**
1169 * Returns the top and bottom position for the edge effect.
1170 */
1171 protected abstract void getEdgeVerticalPostion(int[] pos);
1172
1173 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001175 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001176 if (page != mCurrentPage || !mScroller.isFinished()) {
1177 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 return true;
1179 }
1180 return false;
1181 }
1182
1183 @Override
1184 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001185 int focusablePage;
1186 if (mNextPage != INVALID_PAGE) {
1187 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001189 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
Winson Chung86f77532010-08-24 11:08:22 -07001191 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001193 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 }
1195 return false;
1196 }
1197
1198 @Override
1199 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001200 if (super.dispatchUnhandledMove(focused, direction)) {
1201 return true;
1202 }
1203
1204 if (mIsRtl) {
1205 if (direction == View.FOCUS_LEFT) {
1206 direction = View.FOCUS_RIGHT;
1207 } else if (direction == View.FOCUS_RIGHT) {
1208 direction = View.FOCUS_LEFT;
1209 }
1210 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001212 if (getCurrentPage() > 0) {
1213 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 return true;
1215 }
1216 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (getCurrentPage() < getPageCount() - 1) {
1218 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 return true;
1220 }
1221 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001222 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224
1225 @Override
1226 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001227 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001229 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 }
1231 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001232 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001233 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001236 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001237 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 }
1239 }
1240 }
1241
1242 /**
1243 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001244 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 *
Winson Chung86f77532010-08-24 11:08:22 -07001246 * This happens when live folders requery, and if they're off page, they
1247 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 */
1249 @Override
1250 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001251 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 View v = focused;
1253 while (true) {
1254 if (v == current) {
1255 super.focusableViewAvailable(focused);
1256 return;
1257 }
1258 if (v == this) {
1259 return;
1260 }
1261 ViewParent parent = v.getParent();
1262 if (parent instanceof View) {
1263 v = (View)v.getParent();
1264 } else {
1265 return;
1266 }
1267 }
1268 }
1269
1270 /**
1271 * {@inheritDoc}
1272 */
1273 @Override
1274 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1275 if (disallowIntercept) {
1276 // We need to make sure to cancel our long press if
1277 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001278 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001279 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 }
1281 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1282 }
1283
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001284 /**
1285 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1286 */
1287 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001288 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001289 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001290 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001291 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001292 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001293 }
1294
1295 /**
1296 * Return true if a tap at (x, y) should trigger a flip to the next page.
1297 */
1298 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001299 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001300 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001301 }
1302 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001303 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001304 }
Winson Chung52aee602013-01-30 12:01:02 -08001305
Adam Cohen7d30a372013-07-01 17:03:59 -07001306 /** Returns whether x and y originated within the buffered viewport */
1307 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001308 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001309 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001310 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001311 }
1312
Winson Chung321e9ee2010-08-09 13:37:56 -07001313 @Override
1314 public boolean onInterceptTouchEvent(MotionEvent ev) {
1315 /*
1316 * This method JUST determines whether we want to intercept the motion.
1317 * If we return true, onTouchEvent will be called and we do the actual
1318 * scrolling there.
1319 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001320 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001321
Winson Chung45e1d6e2010-11-09 17:19:49 -08001322 // Skip touch handling if there are no pages to swipe
1323 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 /*
1326 * Shortcut the most recurring case: the user is in the dragging
1327 * state and he is moving his finger. We want to intercept this
1328 * motion.
1329 */
1330 final int action = ev.getAction();
1331 if ((action == MotionEvent.ACTION_MOVE) &&
1332 (mTouchState == TOUCH_STATE_SCROLLING)) {
1333 return true;
1334 }
1335
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 switch (action & MotionEvent.ACTION_MASK) {
1337 case MotionEvent.ACTION_MOVE: {
1338 /*
1339 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1340 * whether the user has moved far enough from his original down touch.
1341 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001342 if (mActivePointerId != INVALID_POINTER) {
1343 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001344 }
1345 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1346 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1347 // i.e. fall through to the next case (don't break)
1348 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1349 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001350 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 }
1352
1353 case MotionEvent.ACTION_DOWN: {
1354 final float x = ev.getX();
1355 final float y = ev.getY();
1356 // Remember location of down touch
1357 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001358 mDownMotionY = y;
1359 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 mLastMotionX = x;
1361 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001362 float[] p = mapPointFromViewToParent(this, x, y);
1363 mParentDownMotionX = p[0];
1364 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001365 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001366 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001368
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 /*
1370 * If being flinged and user touches the screen, initiate drag;
1371 * otherwise don't. mScroller.isFinished should be false when
1372 * being flinged.
1373 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001374 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001375 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001376
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001377 if (finishedScrolling) {
1378 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001379 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001380 setCurrentPage(getNextPage());
1381 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001382 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001383 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001384 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1385 mTouchState = TOUCH_STATE_SCROLLING;
1386 } else {
1387 mTouchState = TOUCH_STATE_REST;
1388 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001389 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001390
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 break;
1392 }
1393
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001395 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001396 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 break;
1398
1399 case MotionEvent.ACTION_POINTER_UP:
1400 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001401 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 break;
1403 }
1404
1405 /*
1406 * The only time we want to intercept motion events is if we are in the
1407 * drag mode.
1408 */
1409 return mTouchState != TOUCH_STATE_REST;
1410 }
1411
Adam Cohenf8d28232011-02-01 21:47:00 -08001412 protected void determineScrollingStart(MotionEvent ev) {
1413 determineScrollingStart(ev, 1.0f);
1414 }
1415
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 /*
1417 * Determines if we should change the touch state to start scrolling after the
1418 * user moves their touch point too far.
1419 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001420 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001421 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001423 if (pointerIndex == -1) return;
1424
1425 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 final float x = ev.getX(pointerIndex);
1427 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001428 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1429
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001431
Adam Cohenf8d28232011-02-01 21:47:00 -08001432 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001434
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001435 if (xMoved) {
1436 // Scroll if the user moved far enough along the X axis
1437 mTouchState = TOUCH_STATE_SCROLLING;
1438 mTotalMotionX += Math.abs(mLastMotionX - x);
1439 mLastMotionX = x;
1440 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001441 onScrollInteractionBegin();
1442 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001443 }
1444 }
1445
1446 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001447 // Try canceling the long press. It could also have been scheduled
1448 // by a distant descendant, so use the mAllowLongPress flag to block
1449 // everything
1450 final View currentPage = getPageAt(mCurrentPage);
1451 if (currentPage != null) {
1452 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 }
1454 }
1455
Adam Cohenb5ba0972011-09-07 18:02:31 -07001456 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001457 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001458
Adam Cohenedb40762013-07-18 16:45:45 -07001459 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001460 int count = getChildCount();
1461
1462 final int totalDistance;
1463
1464 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001465 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001466 adjacentPage = page - 1;
1467 }
1468
1469 if (adjacentPage < 0 || adjacentPage > count - 1) {
1470 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1471 } else {
1472 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1473 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001474
1475 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001476 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1477 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001478 return scrollProgress;
1479 }
1480
Adam Cohenedb40762013-07-18 16:45:45 -07001481 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001482 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001483 return 0;
1484 } else {
1485 return mPageScrolls[index];
1486 }
1487 }
1488
Adam Cohen564a2e72013-10-09 14:47:32 -07001489 // While layout transitions are occurring, a child's position may stray from its baseline
1490 // position. This method returns the magnitude of this stray at any given time.
1491 public int getLayoutTransitionOffsetForPage(int index) {
1492 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1493 return 0;
1494 } else {
1495 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001496
1497 int scrollOffset = 0;
1498 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1499 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001500 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001501 }
1502
Adam Cohen564a2e72013-10-09 14:47:32 -07001503 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1504 return (int) (child.getX() - baselineX);
1505 }
1506 }
1507
Adam Cohenb5ba0972011-09-07 18:02:31 -07001508 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001509 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001510 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001511 if (f < 0) {
1512 mEdgeGlowLeft.onPull(-f);
1513 } else if (f > 0) {
1514 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001515 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001516 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001517 }
1518 invalidate();
1519 }
1520
Adam Cohenb5ba0972011-09-07 18:02:31 -07001521 protected void overScroll(float amount) {
1522 dampedOverScroll(amount);
1523 }
1524
Winson Chungdc61c4d2015-04-20 18:26:57 -07001525 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001526 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001527 }
1528
Winson Chungdc61c4d2015-04-20 18:26:57 -07001529 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001530 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001531 }
1532
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001533 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001534 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001535 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001536 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1537 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1538 } else {
1539 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1540 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1541 }
1542 }
1543
Adam Cohenf9618852013-11-08 06:45:03 -08001544 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001545 mFreeScroll = freeScroll;
1546
Adam Cohenf9618852013-11-08 06:45:03 -08001547 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001548 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001549 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001550 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1551 setCurrentPage(mTempVisiblePagesRange[0]);
1552 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1553 setCurrentPage(mTempVisiblePagesRange[1]);
1554 }
1555 }
1556
1557 setEnableOverscroll(!freeScroll);
1558 }
1559
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001560 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001561 mAllowOverScroll = enable;
1562 }
1563
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001564 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001565 if (mDragView != null) {
1566 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1567 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001568 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001569 int minDistance = Integer.MAX_VALUE;
1570 int minIndex = indexOfChild(mDragView);
1571 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1572 View page = getPageAt(i);
1573 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1574 int d = Math.abs(dragX - pageX);
1575 if (d < minDistance) {
1576 minIndex = i;
1577 minDistance = d;
1578 }
1579 }
1580 return minIndex;
1581 }
1582 return -1;
1583 }
1584
Winson Chung321e9ee2010-08-09 13:37:56 -07001585 @Override
1586 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001587 super.onTouchEvent(ev);
1588
Winson Chung45e1d6e2010-11-09 17:19:49 -08001589 // Skip touch handling if there are no pages to swipe
1590 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1591
Michael Jurkab8f06722010-10-10 15:58:46 -07001592 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001593
1594 final int action = ev.getAction();
1595
1596 switch (action & MotionEvent.ACTION_MASK) {
1597 case MotionEvent.ACTION_DOWN:
1598 /*
1599 * If being flinged and user touches, stop the fling. isFinished
1600 * will be false if being flinged.
1601 */
1602 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001603 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001604 }
1605
1606 // Remember where the motion event started
1607 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001608 mDownMotionY = mLastMotionY = ev.getY();
1609 mDownScrollX = getScrollX();
1610 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1611 mParentDownMotionX = p[0];
1612 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001613 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001614 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001616
Michael Jurka0142d492010-08-25 17:46:15 -07001617 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001618 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001619 pageBeginMoving();
1620 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001621 break;
1622
1623 case MotionEvent.ACTION_MOVE:
1624 if (mTouchState == TOUCH_STATE_SCROLLING) {
1625 // Scroll to follow the motion event
1626 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001627
1628 if (pointerIndex == -1) return true;
1629
Winson Chung321e9ee2010-08-09 13:37:56 -07001630 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001631 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001632
Adam Cohenaefd4e12011-02-14 16:39:38 -08001633 mTotalMotionX += Math.abs(deltaX);
1634
Winson Chungc0844aa2011-02-02 15:25:58 -08001635 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1636 // keep the remainder because we are actually testing if we've moved from the last
1637 // scrolled position (which is discrete).
1638 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001639 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001640 mLastMotionX = x;
1641 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001642 } else {
1643 awakenScrollBars();
1644 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001645 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1646 // Update the last motion position
1647 mLastMotionX = ev.getX();
1648 mLastMotionY = ev.getY();
1649
1650 // Update the parent down so that our zoom animations take this new movement into
1651 // account
1652 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1653 mParentDownMotionX = pt[0];
1654 mParentDownMotionY = pt[1];
1655 updateDragViewTranslationDuringDrag();
1656
1657 // Find the closest page to the touch point
1658 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001659
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1661 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1662 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1663 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1664
Adam Cohenf358a4b2013-07-23 16:47:31 -07001665 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001666 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001667 mTempVisiblePagesRange[0] = 0;
1668 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001669 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001670 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1671 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1672 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1673 mSidePageHoverIndex = pageUnderPointIndex;
1674 mSidePageHoverRunnable = new Runnable() {
1675 @Override
1676 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 // Setup the scroll to the correct page before we swap the views
1678 snapToPage(pageUnderPointIndex);
1679
1680 // For each of the pages between the paged view and the drag view,
1681 // animate them from the previous position to the new position in
1682 // the layout (as a result of the drag view moving in the layout)
1683 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1684 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1685 dragViewIndex + 1 : pageUnderPointIndex;
1686 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1687 dragViewIndex - 1 : pageUnderPointIndex;
1688 for (int i = lowerIndex; i <= upperIndex; ++i) {
1689 View v = getChildAt(i);
1690 // dragViewIndex < pageUnderPointIndex, so after we remove the
1691 // drag view all subsequent views to pageUnderPointIndex will
1692 // shift down.
1693 int oldX = getViewportOffsetX() + getChildOffset(i);
1694 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1695
1696 // Animate the view translation from its old position to its new
1697 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001698 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 if (anim != null) {
1700 anim.cancel();
1701 }
1702
1703 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001704 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001705 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001706 anim.start();
1707 v.setTag(anim);
1708 }
1709
1710 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001711 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001713 if (mPageIndicator != null) {
1714 mPageIndicator.setActiveMarker(getNextPage());
1715 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001716 }
1717 };
1718 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1719 }
1720 } else {
1721 removeCallbacks(mSidePageHoverRunnable);
1722 mSidePageHoverIndex = -1;
1723 }
Adam Cohen564976a2010-10-13 18:52:07 -07001724 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001725 determineScrollingStart(ev);
1726 }
1727 break;
1728
1729 case MotionEvent.ACTION_UP:
1730 if (mTouchState == TOUCH_STATE_SCROLLING) {
1731 final int activePointerId = mActivePointerId;
1732 final int pointerIndex = ev.findPointerIndex(activePointerId);
1733 final float x = ev.getX(pointerIndex);
1734 final VelocityTracker velocityTracker = mVelocityTracker;
1735 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1736 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001737 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001738 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001739 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1740 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001741
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001742 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1743
Adam Cohen00481b32011-11-18 12:03:48 -08001744 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001745 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001746
Adam Cohenf358a4b2013-07-23 16:47:31 -07001747 if (!mFreeScroll) {
1748 // In the case that the page is moved far to one direction and then is flung
1749 // in the opposite direction, we use a threshold to determine whether we should
1750 // just return to the starting page, or if we should skip one further.
1751 boolean returnToOriginalPage = false;
1752 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1753 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1754 returnToOriginalPage = true;
1755 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001756
Adam Cohenf358a4b2013-07-23 16:47:31 -07001757 int finalPage;
1758 // We give flings precedence over large moves, which is why we short-circuit our
1759 // test for a large move if a fling has been registered. That is, a large
1760 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001761 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1762 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001763 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1764 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1765 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1766 snapToPageWithVelocity(finalPage, velocityX);
1767 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1768 (isFling && isVelocityXLeft)) &&
1769 mCurrentPage < getChildCount() - 1) {
1770 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1771 snapToPageWithVelocity(finalPage, velocityX);
1772 } else {
1773 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001774 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001775 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001776 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001777 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001778 }
1779
1780 float scaleX = getScaleX();
1781 int vX = (int) (-velocityX * scaleX);
1782 int initialScrollX = (int) (getScrollX() * scaleX);
1783
Adam Cohenf9618852013-11-08 06:45:03 -08001784 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001785 mScroller.fling(initialScrollX,
1786 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1787 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001788 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001789 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001790 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1791 // at this point we have not moved beyond the touch slop
1792 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1793 // we can just page
1794 int nextPage = Math.max(0, mCurrentPage - 1);
1795 if (nextPage != mCurrentPage) {
1796 snapToPage(nextPage);
1797 } else {
1798 snapToDestination();
1799 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001800 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001801 // at this point we have not moved beyond the touch slop
1802 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1803 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001804 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1805 if (nextPage != mCurrentPage) {
1806 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001807 } else {
1808 snapToDestination();
1809 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001810 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1811 // Update the last motion position
1812 mLastMotionX = ev.getX();
1813 mLastMotionY = ev.getY();
1814
1815 // Update the parent down so that our zoom animations take this new movement into
1816 // account
1817 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1818 mParentDownMotionX = pt[0];
1819 mParentDownMotionY = pt[1];
1820 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001821 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001822 if (!mCancelTap) {
1823 onUnhandledTap(ev);
1824 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001825 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001826
1827 // Remove the callback to wait for the side page hover timeout
1828 removeCallbacks(mSidePageHoverRunnable);
1829 // End any intermediate reordering states
1830 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001831 break;
1832
1833 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001834 if (mTouchState == TOUCH_STATE_SCROLLING) {
1835 snapToDestination();
1836 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001837 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001838 break;
1839
1840 case MotionEvent.ACTION_POINTER_UP:
1841 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001842 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001843 break;
1844 }
1845
1846 return true;
1847 }
1848
Adam Cohen7d30a372013-07-01 17:03:59 -07001849 private void resetTouchState() {
1850 releaseVelocityTracker();
1851 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001852 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001853 mTouchState = TOUCH_STATE_REST;
1854 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001855 mEdgeGlowLeft.onRelease();
1856 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001857 }
1858
Adam Cohenc2d6e892014-10-16 09:49:24 -07001859 /**
1860 * Triggered by scrolling via touch
1861 */
1862 protected void onScrollInteractionBegin() {
1863 }
1864
1865 protected void onScrollInteractionEnd() {
1866 }
1867
Adam Cohen1697b792013-09-17 19:08:21 -07001868 protected void onUnhandledTap(MotionEvent ev) {
1869 ((Launcher) getContext()).onClick(this);
1870 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001871
Winson Chung185d7162011-02-28 13:47:29 -08001872 @Override
1873 public boolean onGenericMotionEvent(MotionEvent event) {
1874 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1875 switch (event.getAction()) {
1876 case MotionEvent.ACTION_SCROLL: {
1877 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1878 final float vscroll;
1879 final float hscroll;
1880 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1881 vscroll = 0;
1882 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1883 } else {
1884 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1885 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1886 }
1887 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001888 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001889 : (hscroll > 0 || vscroll > 0);
1890 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001891 scrollRight();
1892 } else {
1893 scrollLeft();
1894 }
1895 return true;
1896 }
1897 }
1898 }
1899 }
1900 return super.onGenericMotionEvent(event);
1901 }
1902
Michael Jurkab8f06722010-10-10 15:58:46 -07001903 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1904 if (mVelocityTracker == null) {
1905 mVelocityTracker = VelocityTracker.obtain();
1906 }
1907 mVelocityTracker.addMovement(ev);
1908 }
1909
1910 private void releaseVelocityTracker() {
1911 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001912 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001913 mVelocityTracker.recycle();
1914 mVelocityTracker = null;
1915 }
1916 }
1917
Winson Chung321e9ee2010-08-09 13:37:56 -07001918 private void onSecondaryPointerUp(MotionEvent ev) {
1919 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1920 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1921 final int pointerId = ev.getPointerId(pointerIndex);
1922 if (pointerId == mActivePointerId) {
1923 // This was our active pointer going up. Choose a new
1924 // active pointer and adjust accordingly.
1925 // TODO: Make this decision more intelligent.
1926 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1927 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1928 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001929 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001930 mActivePointerId = ev.getPointerId(newPointerIndex);
1931 if (mVelocityTracker != null) {
1932 mVelocityTracker.clear();
1933 }
1934 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001935 }
1936
Winson Chung321e9ee2010-08-09 13:37:56 -07001937 @Override
1938 public void requestChildFocus(View child, View focused) {
1939 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001940 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001941 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001942 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001943 }
1944 }
1945
Adam Cohend19d3ca2010-09-15 14:43:42 -07001946 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001947 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001949 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001950 final int childCount = getChildCount();
1951 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001952 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001953 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001954 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001955 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1957 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1958 minDistanceFromScreenCenter = distanceFromScreenCenter;
1959 minDistanceFromScreenCenterIndex = i;
1960 }
1961 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001962 return minDistanceFromScreenCenterIndex;
1963 }
1964
1965 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001966 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001967 }
1968
Adam Cohene0f66b52010-11-23 15:06:07 -08001969 private static class ScrollInterpolator implements Interpolator {
1970 public ScrollInterpolator() {
1971 }
1972
1973 public float getInterpolation(float t) {
1974 t -= 1.0f;
1975 return t*t*t*t*t + 1;
1976 }
1977 }
1978
1979 // We want the duration of the page snap animation to be influenced by the distance that
1980 // the screen has to travel, however, we don't want this duration to be effected in a
1981 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1982 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001983 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001984 f -= 0.5f; // center the values about 0.
1985 f *= 0.3f * Math.PI / 2.0f;
1986 return (float) Math.sin(f);
1987 }
1988
Michael Jurka0142d492010-08-25 17:46:15 -07001989 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07001990 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07001991 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08001992
Adam Cohenedb40762013-07-18 16:45:45 -07001993 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08001994 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08001995 int duration = 0;
1996
Sunny Goyal4d113a52015-05-27 10:05:28 -07001997 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001998 // If the velocity is low enough, then treat this more as an automatic page advance
1999 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002000 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002001 return;
2002 }
2003
2004 // Here we compute a "distance" that will be used in the computation of the overall
2005 // snap duration. This is a function of the actual distance that needs to be traveled;
2006 // we keep this value close to half screen size in order to reduce the variance in snap
2007 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002008 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002009 float distance = halfScreenSize + halfScreenSize *
2010 distanceInfluenceForSnapDuration(distanceRatio);
2011
2012 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002013 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002014
2015 // we want the page's snap velocity to approximately match the velocity at which the
2016 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002017 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2018 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002019
2020 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002021 }
2022
Sunny Goyal1740d902015-05-27 11:14:01 -07002023 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002024 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002025 }
2026
Adam Cohenf9c184a2016-01-15 16:47:43 -08002027 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002028 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002029 }
2030
Michael Jurka0142d492010-08-25 17:46:15 -07002031 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002032 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002033 }
2034
Adam Cohenf9618852013-11-08 06:45:03 -08002035 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2036 snapToPage(whichPage, duration, false, interpolator);
2037 }
2038
2039 protected void snapToPage(int whichPage, int duration, boolean immediate,
2040 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002041 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002042
Adam Cohenedb40762013-07-18 16:45:45 -07002043 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002044 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002045 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002046 }
2047
2048 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002049 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 }
Michael Jurka0142d492010-08-25 17:46:15 -07002051
Adam Cohenf9618852013-11-08 06:45:03 -08002052 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2053 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002054 whichPage = validateNewPage(whichPage);
2055
Adam Cohen7d30a372013-07-01 17:03:59 -07002056 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002057
2058 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002059 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002060 if (immediate) {
2061 duration = 0;
2062 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002063 duration = Math.abs(delta);
2064 }
2065
Adam Cohenf358a4b2013-07-23 16:47:31 -07002066 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002067 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002068 }
Adam Cohenf9618852013-11-08 06:45:03 -08002069
2070 if (interpolator != null) {
2071 mScroller.setInterpolator(interpolator);
2072 } else {
2073 mScroller.setInterpolator(mDefaultInterpolator);
2074 }
2075
Sunny Goyalc86df472016-02-25 09:19:38 -08002076 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002077
Adam Cohen674531f2013-12-13 15:07:14 -08002078 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002079
2080 // Trigger a compute() to finish switching pages if necessary
2081 if (immediate) {
2082 computeScroll();
2083 }
2084
2085 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002086 invalidate();
2087 }
2088
Winson Chung321e9ee2010-08-09 13:37:56 -07002089 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002090 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 }
2092
2093 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002094 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002095 }
2096
Adam Cohendbdff6b2013-09-18 19:09:15 -07002097 @Override
2098 public boolean performLongClick() {
2099 mCancelTap = true;
2100 return super.performLongClick();
2101 }
2102
Winson Chung321e9ee2010-08-09 13:37:56 -07002103 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002104 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002105
2106 SavedState(Parcelable superState) {
2107 super(superState);
2108 }
2109
Adam Cohen091440a2015-03-18 14:16:05 -07002110 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002111 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002112 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002113 }
2114
2115 @Override
2116 public void writeToParcel(Parcel out, int flags) {
2117 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002118 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002119 }
2120
2121 public static final Parcelable.Creator<SavedState> CREATOR =
2122 new Parcelable.Creator<SavedState>() {
2123 public SavedState createFromParcel(Parcel in) {
2124 return new SavedState(in);
2125 }
2126
2127 public SavedState[] newArray(int size) {
2128 return new SavedState[size];
2129 }
2130 };
2131 }
2132
Adam Cohen7d30a372013-07-01 17:03:59 -07002133 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002134 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002135 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002136 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2137 .translationX(0)
2138 .translationY(0)
2139 .scaleX(1)
2140 .scaleY(1)
2141 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002142 anim.addListener(new AnimatorListenerAdapter() {
2143 @Override
2144 public void onAnimationEnd(Animator animation) {
2145 onPostReorderingAnimationCompleted();
2146 }
2147 });
2148 anim.start();
2149 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002150 }
2151
Sunny Goyal1d08f702015-05-04 15:50:25 -07002152 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002153 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2154 mTouchState = TOUCH_STATE_REORDERING;
2155 mIsReordering = true;
2156
Adam Cohen7d30a372013-07-01 17:03:59 -07002157 // We must invalidate to trigger a redraw to update the layers such that the drag view
2158 // is always drawn on top
2159 invalidate();
2160 }
2161
Adam Cohen091440a2015-03-18 14:16:05 -07002162 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002163 // Trigger the callback when reordering has settled
2164 --mPostReorderingPreZoomInRemainingAnimationCount;
2165 if (mPostReorderingPreZoomInRunnable != null &&
2166 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2167 mPostReorderingPreZoomInRunnable.run();
2168 mPostReorderingPreZoomInRunnable = null;
2169 }
2170 }
2171
Sunny Goyal1d08f702015-05-04 15:50:25 -07002172 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002173 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002174 }
2175
Adam Cohenf358a4b2013-07-23 16:47:31 -07002176 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002177 int dragViewIndex = indexOfChild(v);
2178
Adam Cohen327acfe2014-06-06 11:52:52 -07002179 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002180
Adam Cohen7d30a372013-07-01 17:03:59 -07002181 mTempVisiblePagesRange[0] = 0;
2182 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002183 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002184 mReorderingStarted = true;
2185
2186 // Check if we are within the reordering range
2187 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002188 dragViewIndex <= mTempVisiblePagesRange[1]) {
2189 // Find the drag view under the pointer
2190 mDragView = getChildAt(dragViewIndex);
2191 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2192 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002193 snapToPage(getPageNearestToCenterOfScreen());
2194 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002195 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002196 return true;
2197 }
2198 return false;
2199 }
2200
2201 boolean isReordering(boolean testTouchState) {
2202 boolean state = mIsReordering;
2203 if (testTouchState) {
2204 state &= (mTouchState == TOUCH_STATE_REORDERING);
2205 }
2206 return state;
2207 }
2208 void endReordering() {
2209 // For simplicity, we call endReordering sometimes even if reordering was never started.
2210 // In that case, we don't want to do anything.
2211 if (!mReorderingStarted) return;
2212 mReorderingStarted = false;
2213
2214 // If we haven't flung-to-delete the current child, then we just animate the drag view
2215 // back into position
2216 final Runnable onCompleteRunnable = new Runnable() {
2217 @Override
2218 public void run() {
2219 onEndReordering();
2220 }
2221 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002222
2223 mPostReorderingPreZoomInRunnable = new Runnable() {
2224 public void run() {
2225 onCompleteRunnable.run();
2226 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002227 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002228 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002229
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002230 mPostReorderingPreZoomInRemainingAnimationCount =
2231 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2232 // Snap to the current page
2233 snapToPage(indexOfChild(mDragView), 0);
2234 // Animate the drag view back to the front position
2235 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002236 }
2237
Winson Chung6a0f57d2011-06-29 20:10:49 -07002238 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002239 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002240 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002241 @Override
2242 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2243 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002244 info.setScrollable(getPageCount() > 1);
2245 if (getCurrentPage() < getPageCount() - 1) {
2246 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2247 }
2248 if (getCurrentPage() > 0) {
2249 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2250 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002251 info.setClassName(getClass().getName());
2252
2253 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2254 // Besides disabling the accessibility long-click, this also prevents this view from getting
2255 // accessibility focus.
2256 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002257 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002258 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2259 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002260 }
2261
2262 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002263 public void sendAccessibilityEvent(int eventType) {
2264 // Don't let the view send real scroll events.
2265 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2266 super.sendAccessibilityEvent(eventType);
2267 }
2268 }
2269
2270 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002271 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2272 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002273 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002274 }
2275
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002276 @Override
2277 public boolean performAccessibilityAction(int action, Bundle arguments) {
2278 if (super.performAccessibilityAction(action, arguments)) {
2279 return true;
2280 }
2281 switch (action) {
2282 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2283 if (getCurrentPage() < getPageCount() - 1) {
2284 scrollRight();
2285 return true;
2286 }
2287 } break;
2288 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2289 if (getCurrentPage() > 0) {
2290 scrollLeft();
2291 return true;
2292 }
2293 } break;
2294 }
2295 return false;
2296 }
2297
Adam Cohen0ffac432013-07-10 11:19:26 -07002298 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002299 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002300 getNextPage() + 1, getChildCount());
2301 }
2302
Winson Chungd11265e2011-08-30 13:37:23 -07002303 @Override
2304 public boolean onHoverEvent(android.view.MotionEvent event) {
2305 return true;
2306 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002307}