blob: 686dd2f582d99a2625ea02b97f71c9237d368d3d [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070030import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.os.Parcel;
32import android.os.Parcelable;
33import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070034import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080036import android.view.InputDevice;
37import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.MotionEvent;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
42import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070045import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080047import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070048
Adam Cohen091440a2015-03-18 14:16:05 -070049import com.android.launcher3.util.Thunk;
50
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
Michael Jurka8b805b12012-04-18 14:23:14 -070057public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070058 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohen7d30a372013-07-01 17:03:59 -070065 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070066 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070067 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070068 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb5ba0972011-09-07 18:02:31 -070070 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070071 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen7d30a372013-07-01 17:03:59 -070085 // We are disabling touch interaction of the widget region for factory ROM.
86 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070088
Adam Cohen21cd0022013-10-09 18:57:02 -070089 public static final int INVALID_RESTORE_PAGE = -1001;
90
Adam Cohenf358a4b2013-07-23 16:47:31 -070091 private boolean mFreeScroll = false;
92 private int mFreeScrollMinScrollX = -1;
93 private int mFreeScrollMaxScrollX = -1;
94
Winson Chung8aad6102012-05-11 16:27:49 -070095 static final int AUTOMATIC_PAGE_SPACING = -1;
96
Adam Cohen265b9a62011-12-07 14:37:18 -080097 protected int mFlingThresholdVelocity;
98 protected int mMinFlingVelocity;
99 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700100
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected float mSmoothingTime;
103 protected float mTouchX;
104
105 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700106 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700107
108 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700109 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700110 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800113 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800114 protected LauncherScroller mScroller;
115 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Adam Cohen7d30a372013-07-01 17:03:59 -0700119 private float mParentDownMotionX;
120 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700122 private float mDownMotionY;
123 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700124 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800125 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800126 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800127 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800128 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700129 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700130
Adam Cohendbdff6b2013-09-18 19:09:15 -0700131 private boolean mCancelTap;
132
Adam Cohenedb40762013-07-18 16:45:45 -0700133 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka0142d492010-08-25 17:46:15 -0700135 protected final static int TOUCH_STATE_REST = 0;
136 protected final static int TOUCH_STATE_SCROLLING = 1;
137 protected final static int TOUCH_STATE_PREV_PAGE = 2;
138 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700139 protected final static int TOUCH_STATE_REORDERING = 4;
140
Adam Cohene45440e2010-10-14 18:33:38 -0700141 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka0142d492010-08-25 17:46:15 -0700143 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700144 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka7426c422010-11-11 15:23:47 -0800148 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149 private int mPagingTouchSlop;
150 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700151 protected int mPageLayoutWidthGap;
152 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700153 protected int mCellCountX = 0;
154 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700155 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800156 protected boolean mAllowOverScroll = true;
157 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800158 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700159 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800162 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
163 // the screens from continuing to translate beyond the normal bounds.
164 protected int mOverScrollX;
165
Michael Jurka5f1c5092010-09-03 14:15:02 -0700166 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700167
Michael Jurka5f1c5092010-09-03 14:15:02 -0700168 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700169
Winson Chung86f77532010-08-24 11:08:22 -0700170 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
Michael Jurka0142d492010-08-25 17:46:15 -0700172 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700174
175 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
176 // to switch to a new page
177 protected boolean mUsePagingTouchSlop = true;
178
Michael Jurka8b805b12012-04-18 14:23:14 -0700179 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700180 // (SmoothPagedView does this)
181 protected boolean mDeferScrollUpdate = false;
182
Patrick Dubroy1262e362010-10-06 15:49:50 -0700183 protected boolean mIsPageMoving = false;
184
Adam Cohenc2d6e892014-10-16 09:49:24 -0700185 private boolean mWasInOverscroll = false;
186
Winson Chungd2be3812013-07-16 11:11:32 -0700187 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700188 @Thunk int mPageIndicatorViewId;
189 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700190 // The viewport whether the pages are to be contained (the actual view may be larger than the
191 // viewport)
192 private Rect mViewport = new Rect();
193
194 // Reordering
195 // We use the min scale to determine how much to expand the actually PagedView measured
196 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700197 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
198 private static int REORDERING_REORDER_REPOSITION_DURATION = 300;
199 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
200
Adam Cohen7d30a372013-07-01 17:03:59 -0700201 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700202 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700203 protected View mDragView;
204 protected AnimatorSet mZoomInOutAnim;
205 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700207 // This variable's scope is only for the duration of startReordering() and endReordering()
208 private boolean mReorderingStarted = false;
209 // This variable's scope is for the duration of startReordering() and after the zoomIn()
210 // animation after endReordering()
211 private boolean mIsReordering;
212 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
213 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
214 private int mPostReorderingPreZoomInRemainingAnimationCount;
215 private Runnable mPostReorderingPreZoomInRunnable;
216
Adam Cohen7d30a372013-07-01 17:03:59 -0700217 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700218 private static final Matrix sTmpInvMatrix = new Matrix();
219 private static final float[] sTmpPoint = new float[2];
220 private static final int[] sTmpIntPoint = new int[2];
221 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700222
John Spurlock77e1f472013-09-11 10:09:51 -0400223 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700224 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400225
Winson Chung86f77532010-08-24 11:08:22 -0700226 public interface PageSwitchListener {
227 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 public PagedView(Context context) {
231 this(context, null);
232 }
233
234 public PagedView(Context context, AttributeSet attrs) {
235 this(context, attrs, 0);
236 }
237
238 public PagedView(Context context, AttributeSet attrs, int defStyle) {
239 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700240
Adam Cohen9c4949e2010-10-05 12:27:22 -0700241 TypedArray a = context.obtainStyledAttributes(attrs,
242 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700243
Winson Chungdf4b83d2010-10-20 17:49:27 -0700244 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700245 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700246 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700247 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700248 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700249 a.recycle();
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700252 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700253 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 }
255
256 /**
257 * Initializes various states for this workspace.
258 */
Michael Jurka0142d492010-08-25 17:46:15 -0700259 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800260 mScroller = new LauncherScroller(getContext());
261 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700262 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700263 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700264
265 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700266 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
268 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700269 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800270
271 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
272 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
273 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700274 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 }
276
Adam Cohenf9618852013-11-08 06:45:03 -0800277 protected void setDefaultInterpolator(Interpolator interpolator) {
278 mDefaultInterpolator = interpolator;
279 mScroller.setInterpolator(mDefaultInterpolator);
280 }
281
Winson Chungd2be3812013-07-16 11:11:32 -0700282 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700283 super.onAttachedToWindow();
284
Winson Chungd2be3812013-07-16 11:11:32 -0700285 // Hook up the page indicator
286 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700287 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700288 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700289 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700290 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700291
Winson Chung7819a562013-09-19 15:55:45 -0700292 ArrayList<PageIndicator.PageMarkerResources> markers =
293 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700294 for (int i = 0; i < getChildCount(); ++i) {
295 markers.add(getPageIndicatorMarker(i));
296 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700297
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700298 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700299
300 OnClickListener listener = getPageIndicatorClickListener();
301 if (listener != null) {
302 mPageIndicator.setOnClickListener(listener);
303 }
Adam Cohen53805212013-10-01 10:39:23 -0700304 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700305 }
306 }
307
Adam Cohen53805212013-10-01 10:39:23 -0700308 protected String getPageIndicatorDescription() {
309 return getCurrentPageDescription();
310 }
311
312 protected OnClickListener getPageIndicatorClickListener() {
313 return null;
314 }
315
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700316 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700317 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700318 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700319 // Unhook the page indicator
320 mPageIndicator = null;
321 }
322
Adam Cohen7d30a372013-07-01 17:03:59 -0700323 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700324 private float[] mapPointFromViewToParent(View v, float x, float y) {
325 sTmpPoint[0] = x;
326 sTmpPoint[1] = y;
327 v.getMatrix().mapPoints(sTmpPoint);
328 sTmpPoint[0] += v.getLeft();
329 sTmpPoint[1] += v.getTop();
330 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700331 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700332 private float[] mapPointFromParentToView(View v, float x, float y) {
333 sTmpPoint[0] = x - v.getLeft();
334 sTmpPoint[1] = y - v.getTop();
335 v.getMatrix().invert(sTmpInvMatrix);
336 sTmpInvMatrix.mapPoints(sTmpPoint);
337 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700338 }
339
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700340 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700341 if (mDragView != null) {
342 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
343 (mDragViewBaselineLeft - mDragView.getLeft());
344 float y = mLastMotionY - mDownMotionY;
345 mDragView.setTranslationX(x);
346 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700347
Adam Cohenf358a4b2013-07-23 16:47:31 -0700348 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
349 + x + ", " + y);
350 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700351 }
352
353 public void setMinScale(float f) {
354 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700355 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700356 requestLayout();
357 }
358
359 @Override
360 public void setScaleX(float scaleX) {
361 super.setScaleX(scaleX);
362 if (isReordering(true)) {
363 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
364 mLastMotionX = p[0];
365 mLastMotionY = p[1];
366 updateDragViewTranslationDuringDrag();
367 }
368 }
369
370 // Convenience methods to get the actual width/height of the PagedView (since it is measured
371 // to be larger to account for the minimum possible scale)
372 int getViewportWidth() {
373 return mViewport.width();
374 }
375 int getViewportHeight() {
376 return mViewport.height();
377 }
378
379 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
380 // PagedView both horizontally and vertically
381 int getViewportOffsetX() {
382 return (getMeasuredWidth() - getViewportWidth()) / 2;
383 }
384
385 int getViewportOffsetY() {
386 return (getMeasuredHeight() - getViewportHeight()) / 2;
387 }
388
Adam Cohenedb40762013-07-18 16:45:45 -0700389 PageIndicator getPageIndicator() {
390 return mPageIndicator;
391 }
Winson Chung7819a562013-09-19 15:55:45 -0700392 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
393 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700394 }
Adam Cohenedb40762013-07-18 16:45:45 -0700395
Adam Cohen674531f2013-12-13 15:07:14 -0800396 /**
397 * Add a page change listener which will be called when a page is _finished_ listening.
398 *
399 */
Winson Chung86f77532010-08-24 11:08:22 -0700400 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
401 mPageSwitchListener = pageSwitchListener;
402 if (mPageSwitchListener != null) {
403 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 }
405 }
406
407 /**
Winson Chung86f77532010-08-24 11:08:22 -0700408 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700409 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700410 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700411 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700413
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700414 /**
415 * Returns the index of page to be shown immediately afterwards.
416 */
Winson Chung360e63f2012-04-27 13:48:05 -0700417 int getNextPage() {
418 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
419 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700420
Winson Chung86f77532010-08-24 11:08:22 -0700421 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 return getChildCount();
423 }
424
Sunny Goyal83a8f042015-05-19 12:52:12 -0700425 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 return getChildAt(index);
427 }
428
Adam Cohenae4f1552011-10-20 00:15:42 -0700429 protected int indexToPage(int index) {
430 return index;
431 }
432
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800434 * Updates the scroll of the current page immediately to its final scroll position. We use this
435 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
436 * the previous tab page.
437 */
438 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700439 // If the current page is invalid, just reset the scroll position to zero
440 int newX = 0;
441 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700442 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700443 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800444 scrollTo(newX, 0);
445 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700446 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800447 }
448
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700449 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700450 mScroller.abortAnimation();
451 // We need to clean up the next page here to avoid computeScrollHelper from
452 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700453 if (resetNextPage) {
454 mNextPage = INVALID_PAGE;
455 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700456 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700457
458 private void forceFinishScroller() {
459 mScroller.forceFinished(true);
460 // We need to clean up the next page here to avoid computeScrollHelper from
461 // updating current page on the pass.
462 mNextPage = INVALID_PAGE;
463 }
464
Adam Cohen6f127a62014-06-12 14:54:41 -0700465 private int validateNewPage(int newPage) {
466 int validatedPage = newPage;
467 // When in free scroll mode, we need to clamp to the free scroll page range.
468 if (mFreeScroll) {
469 getFreeScrollPageRange(mTempVisiblePagesRange);
470 validatedPage = Math.max(mTempVisiblePagesRange[0],
471 Math.min(newPage, mTempVisiblePagesRange[1]));
472 }
473 // Ensure that it is clamped by the actual set of children in all cases
474 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
475 return validatedPage;
476 }
477
Chet Haasebc2f0822012-10-26 17:59:53 -0700478 /**
Winson Chung86f77532010-08-24 11:08:22 -0700479 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700481 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800482 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700483 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800484 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800485 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
486 // the default
487 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800488 return;
489 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700490 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700491 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700492 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700493 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800494 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
496
Winson Chung8c87cd82013-07-23 16:20:10 -0700497 /**
498 * The restore page will be set in place of the current page at the next (likely first)
499 * layout.
500 */
501 void setRestorePage(int restorePage) {
502 mRestorePage = restorePage;
503 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800504 int getRestorePage() {
505 return mRestorePage;
506 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700507
Adam Cohen674531f2013-12-13 15:07:14 -0800508 /**
509 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
510 * has settled.
511 */
Michael Jurka0142d492010-08-25 17:46:15 -0700512 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700513 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800514 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
Winson Chungd2be3812013-07-16 11:11:32 -0700516
Adam Cohen674531f2013-12-13 15:07:14 -0800517 updatePageIndicator();
518 }
519
520 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700521 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700522 if (mPageIndicator != null) {
523 mPageIndicator.setContentDescription(getPageIndicatorDescription());
524 if (!isReordering(false)) {
525 mPageIndicator.setActiveMarker(getNextPage());
526 }
Winson Chungd2be3812013-07-16 11:11:32 -0700527 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700528 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800529 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700530 if (!mIsPageMoving) {
531 mIsPageMoving = true;
532 onPageBeginMoving();
533 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700534 }
535
Michael Jurkace7e05f2011-02-01 22:02:35 -0800536 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700537 if (mIsPageMoving) {
538 mIsPageMoving = false;
539 onPageEndMoving();
540 }
Michael Jurka0142d492010-08-25 17:46:15 -0700541 }
542
Adam Cohen26976d92011-03-22 15:33:33 -0700543 protected boolean isPageMoving() {
544 return mIsPageMoving;
545 }
546
Michael Jurka0142d492010-08-25 17:46:15 -0700547 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700548 protected void onPageBeginMoving() {
549 }
550
551 // a method that subclasses can override to add behavior
552 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700553 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700554 }
555
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 /**
Winson Chung86f77532010-08-24 11:08:22 -0700557 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 *
559 * @param l The listener used to respond to long clicks.
560 */
561 @Override
562 public void setOnLongClickListener(OnLongClickListener l) {
563 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700564 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700566 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 }
Adam Cohen1697b792013-09-17 19:08:21 -0700568 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 }
570
571 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800572 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800574 }
575
576 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700577 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700578 // In free scroll mode, we clamp the scrollX
579 if (mFreeScroll) {
580 x = Math.min(x, mFreeScrollMaxScrollX);
581 x = Math.max(x, mFreeScrollMinScrollX);
582 }
583
Adam Cohen68d73932010-11-15 10:50:58 -0800584 mUnboundedScrollX = x;
585
Sunny Goyal70660032015-05-14 00:07:08 -0700586 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
587 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700588 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800589 super.scrollTo(0, y);
590 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700591 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700592 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700593 overScroll(x - mMaxScrollX);
594 } else {
595 overScroll(x);
596 }
Adam Cohen68d73932010-11-15 10:50:58 -0800597 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700598 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800599 super.scrollTo(mMaxScrollX, y);
600 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700601 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700602 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700603 overScroll(x);
604 } else {
605 overScroll(x - mMaxScrollX);
606 }
Adam Cohen68d73932010-11-15 10:50:58 -0800607 }
608 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700609 if (mWasInOverscroll) {
610 overScroll(0);
611 mWasInOverscroll = false;
612 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800613 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800614 super.scrollTo(x, y);
615 }
616
Michael Jurka0142d492010-08-25 17:46:15 -0700617 mTouchX = x;
618 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700619
620 // Update the last motion events when scrolling
621 if (isReordering(true)) {
622 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
623 mLastMotionX = p[0];
624 mLastMotionY = p[1];
625 updateDragViewTranslationDuringDrag();
626 }
Michael Jurka0142d492010-08-25 17:46:15 -0700627 }
628
Adam Cohen53805212013-10-01 10:39:23 -0700629 private void sendScrollAccessibilityEvent() {
630 AccessibilityManager am =
631 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
632 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700633 if (mCurrentPage != getNextPage()) {
634 AccessibilityEvent ev =
635 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700636
Vadim Tryshevf4715972015-05-08 17:21:03 -0700637 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700638 }
Adam Cohen53805212013-10-01 10:39:23 -0700639 }
640 }
641
Michael Jurka0142d492010-08-25 17:46:15 -0700642 // we moved this functionality to a helper function so SmoothPagedView can reuse it
643 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 if (getScrollX() != mScroller.getCurrX()
647 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700648 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700649 float scaleX = mFreeScroll ? getScaleX() : 1f;
650 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
651 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700652 }
Michael Jurka0142d492010-08-25 17:46:15 -0700653 invalidate();
654 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700655 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700656 sendScrollAccessibilityEvent();
657
Adam Cohen6f127a62014-06-12 14:54:41 -0700658 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700659 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700660 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700661
Adam Cohen73aa9752010-11-24 16:26:58 -0800662 // We don't want to trigger a page end moving unless the page has settled
663 // and the user has stopped scrolling
664 if (mTouchState == TOUCH_STATE_REST) {
665 pageEndMoving();
666 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700667
Adam Cohen7d30a372013-07-01 17:03:59 -0700668 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700669 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700670 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700671 if (am.isEnabled()) {
672 // Notify the user when the page changes
673 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700674 }
Michael Jurka0142d492010-08-25 17:46:15 -0700675 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 }
Michael Jurka0142d492010-08-25 17:46:15 -0700677 return false;
678 }
679
680 @Override
681 public void computeScroll() {
682 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 }
684
Adam Cohen96d30a12013-07-16 18:13:21 -0700685 public static class LayoutParams extends ViewGroup.LayoutParams {
686 public boolean isFullScreenPage = false;
687
688 /**
689 * {@inheritDoc}
690 */
691 public LayoutParams(int width, int height) {
692 super(width, height);
693 }
694
Sunny Goyal41b22c02015-05-14 15:20:48 -0700695 public LayoutParams(Context context, AttributeSet attrs) {
696 super(context, attrs);
697 }
698
Adam Cohen96d30a12013-07-16 18:13:21 -0700699 public LayoutParams(ViewGroup.LayoutParams source) {
700 super(source);
701 }
702 }
703
Sunny Goyal41b22c02015-05-14 15:20:48 -0700704 @Override
705 public LayoutParams generateLayoutParams(AttributeSet attrs) {
706 return new LayoutParams(getContext(), attrs);
707 }
708
709 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700710 protected LayoutParams generateDefaultLayoutParams() {
711 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
712 }
713
Sunny Goyal41b22c02015-05-14 15:20:48 -0700714 @Override
715 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
716 return new LayoutParams(p);
717 }
718
719 @Override
720 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
721 return p instanceof LayoutParams;
722 }
723
Adam Cohenedb40762013-07-18 16:45:45 -0700724 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700725 LayoutParams lp = generateDefaultLayoutParams();
726 lp.isFullScreenPage = true;
727 super.addView(page, 0, lp);
728 }
729
Adam Cohen410f3cd2013-09-22 12:09:32 -0700730 public int getNormalChildHeight() {
731 return mNormalChildHeight;
732 }
733
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 @Override
735 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700736 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700737 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
738 return;
739 }
740
Adam Cohen7d30a372013-07-01 17:03:59 -0700741 // We measure the dimensions of the PagedView to be larger than the pages so that when we
742 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700743 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
744 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
745 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700746 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800747 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700748 // viewport, we can be at most one and a half screens offset once we scale down
749 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700750 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
751 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700752
Winson Chungc82d2622013-11-06 13:23:29 -0800753 int parentWidthSize = (int) (2f * maxSize);
754 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700755 int scaledWidthSize, scaledHeightSize;
756 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700757 scaledWidthSize = (int) (parentWidthSize / mMinScale);
758 scaledHeightSize = (int) (parentHeightSize / mMinScale);
759 } else {
760 scaledWidthSize = widthSize;
761 scaledHeightSize = heightSize;
762 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700763 mViewport.set(0, 0, widthSize, heightSize);
764
765 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
766 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
767 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769
Winson Chung8aad6102012-05-11 16:27:49 -0700770 // Return early if we aren't given a proper dimension
771 if (widthSize <= 0 || heightSize <= 0) {
772 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
773 return;
774 }
775
Adam Lesinski6b879f02010-11-04 16:15:23 -0700776 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
777 * of the All apps view on XLarge displays to not take up more space then it needs. Width
778 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
779 * each page to have the same width.
780 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 final int verticalPadding = getPaddingTop() + getPaddingBottom();
782 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700783
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700784 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700786 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700787 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700788 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
789 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
790 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
791 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 final int childCount = getChildCount();
793 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700794 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700795 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100796 if (child.getVisibility() != GONE) {
797 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700798
Vladimir Marko2824b072013-10-04 16:42:17 +0100799 int childWidthMode;
800 int childHeightMode;
801 int childWidth;
802 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700803
Vladimir Marko2824b072013-10-04 16:42:17 +0100804 if (!lp.isFullScreenPage) {
805 if (lp.width == LayoutParams.WRAP_CONTENT) {
806 childWidthMode = MeasureSpec.AT_MOST;
807 } else {
808 childWidthMode = MeasureSpec.EXACTLY;
809 }
810
811 if (lp.height == LayoutParams.WRAP_CONTENT) {
812 childHeightMode = MeasureSpec.AT_MOST;
813 } else {
814 childHeightMode = MeasureSpec.EXACTLY;
815 }
816
Adam Cohen3b185e22013-10-29 14:45:58 -0700817 childWidth = getViewportWidth() - horizontalPadding
818 - mInsets.left - mInsets.right;
819 childHeight = getViewportHeight() - verticalPadding
820 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100821 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700822 } else {
823 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700824 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100825
Adam Cohen3b185e22013-10-29 14:45:58 -0700826 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
827 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700828 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700829 if (referenceChildWidth == 0) {
830 referenceChildWidth = childWidth;
831 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700832
Vladimir Marko2824b072013-10-04 16:42:17 +0100833 final int childWidthMeasureSpec =
834 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
835 final int childHeightMeasureSpec =
836 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
837 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700838 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700839 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700840 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800841 }
842
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700844 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700845 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700846 return;
847 }
848
Winson Chung785d2eb2011-04-14 16:08:02 -0700849 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700851
Adam Cohen7d30a372013-07-01 17:03:59 -0700852 int offsetX = getViewportOffsetX();
853 int offsetY = getViewportOffsetY();
854
855 // Update the viewport offsets
856 mViewport.offset(offsetX, offsetY);
857
Sunny Goyal70660032015-05-14 00:07:08 -0700858 final int startIndex = mIsRtl ? childCount - 1 : 0;
859 final int endIndex = mIsRtl ? -1 : childCount;
860 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700861
Adam Cohen7d30a372013-07-01 17:03:59 -0700862 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700863
Adam Cohen3b185e22013-10-29 14:45:58 -0700864 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000865 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700866
867 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700868 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
869 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700870 }
871
Adam Cohen0ffac432013-07-10 11:19:26 -0700872 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700873 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700875 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100876 int childTop;
877 if (lp.isFullScreenPage) {
878 childTop = offsetY;
879 } else {
880 childTop = offsetY + getPaddingTop() + mInsets.top;
881 if (mCenterPagesVertically) {
882 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
883 }
884 }
885
Adam Cohen96d30a12013-07-16 18:13:21 -0700886 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700887 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800888
Winson Chung785d2eb2011-04-14 16:08:02 -0700889 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700890 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800891 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700892
Adam Cohen3b185e22013-10-29 14:45:58 -0700893 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
894 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000895
896 int pageGap = mPageSpacing;
897 int next = i + delta;
898 if (next != endIndex) {
899 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
900 } else {
901 nextLp = null;
902 }
903
904 // Prevent full screen pages from showing in the viewport
905 // when they are not the current page.
906 if (lp.isFullScreenPage) {
907 pageGap = getPaddingLeft();
908 } else if (nextLp != null && nextLp.isFullScreenPage) {
909 pageGap = getPaddingRight();
910 }
911
Sunny Goyal22fc63e2015-05-13 16:01:07 -0700912 childLeft += childWidth + pageGap
913 + (lp.isFullScreenPage ? 0 : (getPaddingLeft() + getPaddingRight()));
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 }
915 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700916
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700917 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -0800918 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700919 mFirstLayout = false;
920 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700921
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700922 final LayoutTransition transition = getLayoutTransition();
923 // If the transition is running defer updating max scroll, as some empty pages could
924 // still be present, and a max scroll change could cause sudden jumps in scroll.
925 if (transition != null && transition.isRunning()) {
926 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
927
928 @Override
929 public void startTransition(LayoutTransition transition, ViewGroup container,
930 View view, int transitionType) { }
931
932 @Override
933 public void endTransition(LayoutTransition transition, ViewGroup container,
934 View view, int transitionType) {
935 // Wait until all transitions are complete.
936 if (!transition.isRunning()) {
937 transition.removeTransitionListener(this);
938 updateMaxScrollX();
939 }
940 }
941 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700942 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700943 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700944 }
945
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700946 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700947 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700948 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700949 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700950 } else {
951 setCurrentPage(getNextPage());
952 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700953 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700954 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700955
956 if (isReordering(true)) {
957 updateDragViewTranslationDuringDrag();
958 }
Winson Chunge3193b92010-09-10 11:44:42 -0700959 }
960
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700961 private void updateMaxScrollX() {
962 int childCount = getChildCount();
963 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700964 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700965 mMaxScrollX = getScrollForPage(index);
966 } else {
967 mMaxScrollX = 0;
968 }
969 }
970
Adam Cohen3b185e22013-10-29 14:45:58 -0700971 public void setPageSpacing(int pageSpacing) {
972 mPageSpacing = pageSpacing;
973 requestLayout();
974 }
975
Adam Cohenf34bab52010-09-30 14:11:56 -0700976 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700977 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
978
979 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700980 for (int i = 0; i < getChildCount(); i++) {
981 View child = getChildAt(i);
982 if (child != null) {
983 float scrollProgress = getScrollProgress(screenCenter, child, i);
984 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800985 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700986 }
987 }
988 invalidate();
989 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700990 }
991
Winson Chunge3193b92010-09-10 11:44:42 -0700992 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700993 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700994 // Update the page indicator, we don't update the page indicator as we
995 // add/remove pages
996 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700997 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700998 mPageIndicator.addMarker(pageIndex,
999 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001000 true);
Winson Chungd2be3812013-07-16 11:11:32 -07001001 }
1002
Adam Cohen2591f6a2011-10-25 14:36:40 -07001003 // This ensures that when children are added, they get the correct transforms / alphas
1004 // in accordance with any scroll effects.
1005 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001006 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001007 invalidate();
1008 }
1009
Michael Jurka8b805b12012-04-18 14:23:14 -07001010 @Override
1011 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001012 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001013 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001014 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001015 }
1016
Winson Chungd2be3812013-07-16 11:11:32 -07001017 private void removeMarkerForView(int index) {
1018 // Update the page indicator, we don't update the page indicator as we
1019 // add/remove pages
1020 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001021 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001022 }
1023 }
1024
1025 @Override
1026 public void removeView(View v) {
1027 // XXX: We should find a better way to hook into this before the view
1028 // gets removed form its parent...
1029 removeMarkerForView(indexOfChild(v));
1030 super.removeView(v);
1031 }
1032 @Override
1033 public void removeViewInLayout(View v) {
1034 // XXX: We should find a better way to hook into this before the view
1035 // gets removed form its parent...
1036 removeMarkerForView(indexOfChild(v));
1037 super.removeViewInLayout(v);
1038 }
1039 @Override
1040 public void removeViewAt(int index) {
1041 // XXX: We should find a better way to hook into this before the view
1042 // gets removed form its parent...
1043 removeViewAt(index);
1044 super.removeViewAt(index);
1045 }
1046 @Override
1047 public void removeAllViewsInLayout() {
1048 // Update the page indicator, we don't update the page indicator as we
1049 // add/remove pages
1050 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001051 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001052 }
1053
1054 super.removeAllViewsInLayout();
1055 }
1056
Adam Cohen73894962011-10-31 13:17:17 -07001057 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001058 if (index < 0 || index > getChildCount() - 1) return 0;
1059
Adam Cohenf698c6e2013-07-17 18:44:25 -07001060 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001061
Adam Cohenf698c6e2013-07-17 18:44:25 -07001062 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001063 }
1064
Adam Cohen6f127a62014-06-12 14:54:41 -07001065 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001066 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001067 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001068 }
1069
Michael Jurkadde558b2011-11-09 22:09:06 -08001070 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001071 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001072 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001073
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001074 range[0] = -1;
1075 range[1] = -1;
1076
Michael Jurkac4fb9172010-09-02 17:19:20 -07001077 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001078 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001079 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001080
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001081 int count = getChildCount();
1082 for (int i = 0; i < count; i++) {
1083 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001084
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001085 sTmpIntPoint[0] = 0;
1086 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1087 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001088 if (range[0] == -1) {
1089 continue;
1090 } else {
1091 break;
1092 }
1093 }
1094
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001095 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1096 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1097 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001098 if (range[0] == -1) {
1099 continue;
1100 } else {
1101 break;
1102 }
1103 }
1104 curScreen = i;
1105 if (range[0] < 0) {
1106 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001107 }
1108 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001109
1110 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001111 } else {
1112 range[0] = -1;
1113 range[1] = -1;
1114 }
1115 }
1116
Michael Jurka920d7f42012-05-14 16:29:55 -07001117 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001118 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001119 }
1120
Michael Jurkadde558b2011-11-09 22:09:06 -08001121 @Override
1122 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001123 // Find out which screens are visible; as an optimization we only call draw on them
1124 final int pageCount = getChildCount();
1125 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001126 int halfScreenSize = getViewportWidth() / 2;
1127 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1128 // Otherwise it is equal to the scaled overscroll position.
1129 int screenCenter = mOverScrollX + halfScreenSize;
1130
1131 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1132 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1133 // set it for the next frame
1134 mForceScreenScrolled = false;
1135 screenScrolled(screenCenter);
1136 mLastScreenCenter = screenCenter;
1137 }
1138
Michael Jurkadde558b2011-11-09 22:09:06 -08001139 getVisiblePages(mTempVisiblePagesRange);
1140 final int leftScreen = mTempVisiblePagesRange[0];
1141 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001142 if (leftScreen != -1 && rightScreen != -1) {
1143 final long drawingTime = getDrawingTime();
1144 // Clip to the bounds
1145 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001146 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1147 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001148
Adam Cohen7d30a372013-07-01 17:03:59 -07001149 // Draw all the children, leaving the drag view for last
1150 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001151 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001152 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001153 if (mForceDrawAllChildrenNextFrame ||
1154 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001155 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001156 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001157 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001158 // Draw the drag view on top (if there is one)
1159 if (mDragView != null) {
1160 drawChild(canvas, mDragView, drawingTime);
1161 }
1162
Michael Jurka5e368ff2012-05-14 23:13:15 -07001163 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001164 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001165 }
Michael Jurka0142d492010-08-25 17:46:15 -07001166 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 }
1168
1169 @Override
1170 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001171 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001172 if (page != mCurrentPage || !mScroller.isFinished()) {
1173 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 return true;
1175 }
1176 return false;
1177 }
1178
1179 @Override
1180 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001181 int focusablePage;
1182 if (mNextPage != INVALID_PAGE) {
1183 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001185 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 }
Winson Chung86f77532010-08-24 11:08:22 -07001187 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001189 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
1191 return false;
1192 }
1193
1194 @Override
1195 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001196 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001198 if (getCurrentPage() > 0) {
1199 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 return true;
1201 }
1202 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001203 if (getCurrentPage() < getPageCount() - 1) {
1204 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 return true;
1206 }
1207 }
1208 return super.dispatchUnhandledMove(focused, direction);
1209 }
1210
1211 @Override
1212 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001213 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001214 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001215 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
1217 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001218 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001219 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001222 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001223 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 }
1225 }
1226 }
1227
1228 /**
1229 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001230 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 *
Winson Chung86f77532010-08-24 11:08:22 -07001232 * This happens when live folders requery, and if they're off page, they
1233 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 */
1235 @Override
1236 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001237 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 View v = focused;
1239 while (true) {
1240 if (v == current) {
1241 super.focusableViewAvailable(focused);
1242 return;
1243 }
1244 if (v == this) {
1245 return;
1246 }
1247 ViewParent parent = v.getParent();
1248 if (parent instanceof View) {
1249 v = (View)v.getParent();
1250 } else {
1251 return;
1252 }
1253 }
1254 }
1255
1256 /**
1257 * {@inheritDoc}
1258 */
1259 @Override
1260 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1261 if (disallowIntercept) {
1262 // We need to make sure to cancel our long press if
1263 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001264 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001265 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1268 }
1269
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001270 /**
1271 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1272 */
1273 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001274 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001275 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001276 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001277 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001278 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001279 }
1280
1281 /**
1282 * Return true if a tap at (x, y) should trigger a flip to the next page.
1283 */
1284 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001285 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001286 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001287 }
1288 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001289 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001290 }
Winson Chung52aee602013-01-30 12:01:02 -08001291
Adam Cohen7d30a372013-07-01 17:03:59 -07001292 /** Returns whether x and y originated within the buffered viewport */
1293 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001294 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001295 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001296 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 }
1298
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 @Override
1300 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 if (DISABLE_TOUCH_INTERACTION) {
1302 return false;
1303 }
1304
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 /*
1306 * This method JUST determines whether we want to intercept the motion.
1307 * If we return true, onTouchEvent will be called and we do the actual
1308 * scrolling there.
1309 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001310 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001311
Winson Chung45e1d6e2010-11-09 17:19:49 -08001312 // Skip touch handling if there are no pages to swipe
1313 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1314
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 /*
1316 * Shortcut the most recurring case: the user is in the dragging
1317 * state and he is moving his finger. We want to intercept this
1318 * motion.
1319 */
1320 final int action = ev.getAction();
1321 if ((action == MotionEvent.ACTION_MOVE) &&
1322 (mTouchState == TOUCH_STATE_SCROLLING)) {
1323 return true;
1324 }
1325
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 switch (action & MotionEvent.ACTION_MASK) {
1327 case MotionEvent.ACTION_MOVE: {
1328 /*
1329 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1330 * whether the user has moved far enough from his original down touch.
1331 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001332 if (mActivePointerId != INVALID_POINTER) {
1333 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001334 }
1335 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1336 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1337 // i.e. fall through to the next case (don't break)
1338 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1339 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001340 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 }
1342
1343 case MotionEvent.ACTION_DOWN: {
1344 final float x = ev.getX();
1345 final float y = ev.getY();
1346 // Remember location of down touch
1347 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001348 mDownMotionY = y;
1349 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 mLastMotionX = x;
1351 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001352 float[] p = mapPointFromViewToParent(this, x, y);
1353 mParentDownMotionX = p[0];
1354 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001355 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001356 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001358
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 /*
1360 * If being flinged and user touches the screen, initiate drag;
1361 * otherwise don't. mScroller.isFinished should be false when
1362 * being flinged.
1363 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001364 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001365 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001366
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001367 if (finishedScrolling) {
1368 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001369 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001370 setCurrentPage(getNextPage());
1371 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001372 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001373 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001374 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1375 mTouchState = TOUCH_STATE_SCROLLING;
1376 } else {
1377 mTouchState = TOUCH_STATE_REST;
1378 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001379 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001380
Winson Chung86f77532010-08-24 11:08:22 -07001381 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001383 if (!DISABLE_TOUCH_SIDE_PAGES) {
1384 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1385 if (getChildCount() > 0) {
1386 if (hitsPreviousPage(x, y)) {
1387 mTouchState = TOUCH_STATE_PREV_PAGE;
1388 } else if (hitsNextPage(x, y)) {
1389 mTouchState = TOUCH_STATE_NEXT_PAGE;
1390 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 }
1392 }
1393 }
1394 break;
1395 }
1396
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001398 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001399 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 break;
1401
1402 case MotionEvent.ACTION_POINTER_UP:
1403 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001404 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 break;
1406 }
1407
1408 /*
1409 * The only time we want to intercept motion events is if we are in the
1410 * drag mode.
1411 */
1412 return mTouchState != TOUCH_STATE_REST;
1413 }
1414
Adam Cohenf8d28232011-02-01 21:47:00 -08001415 protected void determineScrollingStart(MotionEvent ev) {
1416 determineScrollingStart(ev, 1.0f);
1417 }
1418
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 /*
1420 * Determines if we should change the touch state to start scrolling after the
1421 * user moves their touch point too far.
1422 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001423 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001424 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001426 if (pointerIndex == -1) return;
1427
1428 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 final float x = ev.getX(pointerIndex);
1430 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001431 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1432
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 final int xDiff = (int) Math.abs(x - mLastMotionX);
1434 final int yDiff = (int) Math.abs(y - mLastMotionY);
1435
Adam Cohenf8d28232011-02-01 21:47:00 -08001436 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 boolean xPaged = xDiff > mPagingTouchSlop;
1438 boolean xMoved = xDiff > touchSlop;
1439 boolean yMoved = yDiff > touchSlop;
1440
Adam Cohenf8d28232011-02-01 21:47:00 -08001441 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001442 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 // Scroll if the user moved far enough along the X axis
1444 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001445 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001447 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001448 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001449 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001450 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001451 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001453 }
1454 }
1455
1456 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001457 // Try canceling the long press. It could also have been scheduled
1458 // by a distant descendant, so use the mAllowLongPress flag to block
1459 // everything
1460 final View currentPage = getPageAt(mCurrentPage);
1461 if (currentPage != null) {
1462 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 }
1464 }
1465
Adam Cohenb5ba0972011-09-07 18:02:31 -07001466 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001467 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001468
Adam Cohenedb40762013-07-18 16:45:45 -07001469 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001470 int count = getChildCount();
1471
1472 final int totalDistance;
1473
1474 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001475 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001476 adjacentPage = page - 1;
1477 }
1478
1479 if (adjacentPage < 0 || adjacentPage > count - 1) {
1480 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1481 } else {
1482 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1483 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001484
1485 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001486 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1487 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001488 return scrollProgress;
1489 }
1490
Adam Cohenedb40762013-07-18 16:45:45 -07001491 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001492 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001493 return 0;
1494 } else {
1495 return mPageScrolls[index];
1496 }
1497 }
1498
Adam Cohen564a2e72013-10-09 14:47:32 -07001499 // While layout transitions are occurring, a child's position may stray from its baseline
1500 // position. This method returns the magnitude of this stray at any given time.
1501 public int getLayoutTransitionOffsetForPage(int index) {
1502 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1503 return 0;
1504 } else {
1505 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001506
1507 int scrollOffset = 0;
1508 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1509 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001510 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001511 }
1512
Adam Cohen564a2e72013-10-09 14:47:32 -07001513 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1514 return (int) (child.getX() - baselineX);
1515 }
1516 }
1517
Adam Cohene0f66b52010-11-23 15:06:07 -08001518 // This curve determines how the effect of scrolling over the limits of the page dimishes
1519 // as the user pulls further and further from the bounds
1520 private float overScrollInfluenceCurve(float f) {
1521 f -= 1.0f;
1522 return f * f * f + 1.0f;
1523 }
1524
Adam Cohen1e4359c2014-08-18 13:12:16 -07001525 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001527
1528 // We want to reach the max over scroll effect when the user has
1529 // over scrolled half the size of the screen
1530 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1531
Adam Cohen1e4359c2014-08-18 13:12:16 -07001532 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001533
1534 // Clamp this factor, f, to -1 < f < 1
1535 if (Math.abs(f) >= 1) {
1536 f /= Math.abs(f);
1537 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001538 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001539 }
1540
1541 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001542 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001543
1544 float f = (amount / screenSize);
1545
1546 if (f == 0) return;
1547 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1548
Adam Cohen7bfc9792011-01-28 13:52:37 -08001549 // Clamp this factor, f, to -1 < f < 1
1550 if (Math.abs(f) >= 1) {
1551 f /= Math.abs(f);
1552 }
1553
Adam Cohene0f66b52010-11-23 15:06:07 -08001554 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001555 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001556 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001557 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001558 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001559 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001560 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001561 }
1562 invalidate();
1563 }
1564
Adam Cohenb5ba0972011-09-07 18:02:31 -07001565 protected void overScroll(float amount) {
1566 dampedOverScroll(amount);
1567 }
1568
Michael Jurkac5b262c2011-01-12 20:24:50 -08001569 protected float maxOverScroll() {
1570 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001571 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001572 float f = 1.0f;
1573 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1574 return OVERSCROLL_DAMP_FACTOR * f;
1575 }
1576
Winson Chungdc61c4d2015-04-20 18:26:57 -07001577 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001578 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001579 }
1580
Winson Chungdc61c4d2015-04-20 18:26:57 -07001581 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001582 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001583 }
1584
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001585 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001586 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001587 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001588 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1589 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1590 } else {
1591 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1592 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1593 }
1594 }
1595
Adam Cohenf9618852013-11-08 06:45:03 -08001596 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001597 mFreeScroll = freeScroll;
1598
Adam Cohenf9618852013-11-08 06:45:03 -08001599 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001600 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001601 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001602 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1603 setCurrentPage(mTempVisiblePagesRange[0]);
1604 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1605 setCurrentPage(mTempVisiblePagesRange[1]);
1606 }
1607 }
1608
1609 setEnableOverscroll(!freeScroll);
1610 }
1611
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001612 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 mAllowOverScroll = enable;
1614 }
1615
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001616 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001617 if (mDragView != null) {
1618 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1619 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001620 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001621 int minDistance = Integer.MAX_VALUE;
1622 int minIndex = indexOfChild(mDragView);
1623 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1624 View page = getPageAt(i);
1625 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1626 int d = Math.abs(dragX - pageX);
1627 if (d < minDistance) {
1628 minIndex = i;
1629 minDistance = d;
1630 }
1631 }
1632 return minIndex;
1633 }
1634 return -1;
1635 }
1636
Winson Chung321e9ee2010-08-09 13:37:56 -07001637 @Override
1638 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001639 if (DISABLE_TOUCH_INTERACTION) {
1640 return false;
1641 }
1642
Adam Cohen1697b792013-09-17 19:08:21 -07001643 super.onTouchEvent(ev);
1644
Winson Chung45e1d6e2010-11-09 17:19:49 -08001645 // Skip touch handling if there are no pages to swipe
1646 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1647
Michael Jurkab8f06722010-10-10 15:58:46 -07001648 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001649
1650 final int action = ev.getAction();
1651
1652 switch (action & MotionEvent.ACTION_MASK) {
1653 case MotionEvent.ACTION_DOWN:
1654 /*
1655 * If being flinged and user touches, stop the fling. isFinished
1656 * will be false if being flinged.
1657 */
1658 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001659 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001660 }
1661
1662 // Remember where the motion event started
1663 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 mDownMotionY = mLastMotionY = ev.getY();
1665 mDownScrollX = getScrollX();
1666 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1667 mParentDownMotionX = p[0];
1668 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001669 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001670 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001671 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001672
Michael Jurka0142d492010-08-25 17:46:15 -07001673 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001674 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001675 pageBeginMoving();
1676 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001677 break;
1678
1679 case MotionEvent.ACTION_MOVE:
1680 if (mTouchState == TOUCH_STATE_SCROLLING) {
1681 // Scroll to follow the motion event
1682 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001683
1684 if (pointerIndex == -1) return true;
1685
Winson Chung321e9ee2010-08-09 13:37:56 -07001686 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001687 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001688
Adam Cohenaefd4e12011-02-14 16:39:38 -08001689 mTotalMotionX += Math.abs(deltaX);
1690
Winson Chungc0844aa2011-02-02 15:25:58 -08001691 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1692 // keep the remainder because we are actually testing if we've moved from the last
1693 // scrolled position (which is discrete).
1694 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001695 mTouchX += deltaX;
1696 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1697 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001698 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001699 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001700 } else {
1701 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001702 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001703 mLastMotionX = x;
1704 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001705 } else {
1706 awakenScrollBars();
1707 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1709 // Update the last motion position
1710 mLastMotionX = ev.getX();
1711 mLastMotionY = ev.getY();
1712
1713 // Update the parent down so that our zoom animations take this new movement into
1714 // account
1715 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1716 mParentDownMotionX = pt[0];
1717 mParentDownMotionY = pt[1];
1718 updateDragViewTranslationDuringDrag();
1719
1720 // Find the closest page to the touch point
1721 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001722
Adam Cohen7d30a372013-07-01 17:03:59 -07001723 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1724 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1725 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1726 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1727
Adam Cohenf358a4b2013-07-23 16:47:31 -07001728 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001729 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001730 mTempVisiblePagesRange[0] = 0;
1731 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001732 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001733 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1734 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1735 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1736 mSidePageHoverIndex = pageUnderPointIndex;
1737 mSidePageHoverRunnable = new Runnable() {
1738 @Override
1739 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001740 // Setup the scroll to the correct page before we swap the views
1741 snapToPage(pageUnderPointIndex);
1742
1743 // For each of the pages between the paged view and the drag view,
1744 // animate them from the previous position to the new position in
1745 // the layout (as a result of the drag view moving in the layout)
1746 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1747 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1748 dragViewIndex + 1 : pageUnderPointIndex;
1749 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1750 dragViewIndex - 1 : pageUnderPointIndex;
1751 for (int i = lowerIndex; i <= upperIndex; ++i) {
1752 View v = getChildAt(i);
1753 // dragViewIndex < pageUnderPointIndex, so after we remove the
1754 // drag view all subsequent views to pageUnderPointIndex will
1755 // shift down.
1756 int oldX = getViewportOffsetX() + getChildOffset(i);
1757 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1758
1759 // Animate the view translation from its old position to its new
1760 // position
1761 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1762 if (anim != null) {
1763 anim.cancel();
1764 }
1765
1766 v.setTranslationX(oldX - newX);
1767 anim = new AnimatorSet();
1768 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1769 anim.playTogether(
1770 ObjectAnimator.ofFloat(v, "translationX", 0f));
1771 anim.start();
1772 v.setTag(anim);
1773 }
1774
1775 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001776 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001777 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001778 if (mPageIndicator != null) {
1779 mPageIndicator.setActiveMarker(getNextPage());
1780 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001781 }
1782 };
1783 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1784 }
1785 } else {
1786 removeCallbacks(mSidePageHoverRunnable);
1787 mSidePageHoverIndex = -1;
1788 }
Adam Cohen564976a2010-10-13 18:52:07 -07001789 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001790 determineScrollingStart(ev);
1791 }
1792 break;
1793
1794 case MotionEvent.ACTION_UP:
1795 if (mTouchState == TOUCH_STATE_SCROLLING) {
1796 final int activePointerId = mActivePointerId;
1797 final int pointerIndex = ev.findPointerIndex(activePointerId);
1798 final float x = ev.getX(pointerIndex);
1799 final VelocityTracker velocityTracker = mVelocityTracker;
1800 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1801 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001802 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001803 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001804 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1805 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001806
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001807 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1808
Adam Cohen00481b32011-11-18 12:03:48 -08001809 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001810 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001811
Adam Cohenf358a4b2013-07-23 16:47:31 -07001812 if (!mFreeScroll) {
1813 // In the case that the page is moved far to one direction and then is flung
1814 // in the opposite direction, we use a threshold to determine whether we should
1815 // just return to the starting page, or if we should skip one further.
1816 boolean returnToOriginalPage = false;
1817 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1818 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1819 returnToOriginalPage = true;
1820 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001821
Adam Cohenf358a4b2013-07-23 16:47:31 -07001822 int finalPage;
1823 // We give flings precedence over large moves, which is why we short-circuit our
1824 // test for a large move if a fling has been registered. That is, a large
1825 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001826 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1827 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001828 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1829 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1830 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1831 snapToPageWithVelocity(finalPage, velocityX);
1832 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1833 (isFling && isVelocityXLeft)) &&
1834 mCurrentPage < getChildCount() - 1) {
1835 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1836 snapToPageWithVelocity(finalPage, velocityX);
1837 } else {
1838 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001839 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001840 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001841 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001842 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001843 }
1844
1845 float scaleX = getScaleX();
1846 int vX = (int) (-velocityX * scaleX);
1847 int initialScrollX = (int) (getScrollX() * scaleX);
1848
Adam Cohenf9618852013-11-08 06:45:03 -08001849 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001850 mScroller.fling(initialScrollX,
1851 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1852 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001853 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001854 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001855 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1856 // at this point we have not moved beyond the touch slop
1857 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1858 // we can just page
1859 int nextPage = Math.max(0, mCurrentPage - 1);
1860 if (nextPage != mCurrentPage) {
1861 snapToPage(nextPage);
1862 } else {
1863 snapToDestination();
1864 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001865 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001866 // at this point we have not moved beyond the touch slop
1867 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1868 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001869 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1870 if (nextPage != mCurrentPage) {
1871 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001872 } else {
1873 snapToDestination();
1874 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001875 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1876 // Update the last motion position
1877 mLastMotionX = ev.getX();
1878 mLastMotionY = ev.getY();
1879
1880 // Update the parent down so that our zoom animations take this new movement into
1881 // account
1882 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1883 mParentDownMotionX = pt[0];
1884 mParentDownMotionY = pt[1];
1885 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001886 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001887 if (!mCancelTap) {
1888 onUnhandledTap(ev);
1889 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001890 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001891
1892 // Remove the callback to wait for the side page hover timeout
1893 removeCallbacks(mSidePageHoverRunnable);
1894 // End any intermediate reordering states
1895 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001896 break;
1897
1898 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001899 if (mTouchState == TOUCH_STATE_SCROLLING) {
1900 snapToDestination();
1901 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001902 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001903 break;
1904
1905 case MotionEvent.ACTION_POINTER_UP:
1906 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001907 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001908 break;
1909 }
1910
1911 return true;
1912 }
1913
Adam Cohen7d30a372013-07-01 17:03:59 -07001914 private void resetTouchState() {
1915 releaseVelocityTracker();
1916 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001917 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001918 mTouchState = TOUCH_STATE_REST;
1919 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001920 }
1921
Adam Cohenc2d6e892014-10-16 09:49:24 -07001922 /**
1923 * Triggered by scrolling via touch
1924 */
1925 protected void onScrollInteractionBegin() {
1926 }
1927
1928 protected void onScrollInteractionEnd() {
1929 }
1930
Adam Cohen1697b792013-09-17 19:08:21 -07001931 protected void onUnhandledTap(MotionEvent ev) {
1932 ((Launcher) getContext()).onClick(this);
1933 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001934
Winson Chung185d7162011-02-28 13:47:29 -08001935 @Override
1936 public boolean onGenericMotionEvent(MotionEvent event) {
1937 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1938 switch (event.getAction()) {
1939 case MotionEvent.ACTION_SCROLL: {
1940 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1941 final float vscroll;
1942 final float hscroll;
1943 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1944 vscroll = 0;
1945 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1946 } else {
1947 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1948 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1949 }
1950 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001951 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001952 : (hscroll > 0 || vscroll > 0);
1953 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001954 scrollRight();
1955 } else {
1956 scrollLeft();
1957 }
1958 return true;
1959 }
1960 }
1961 }
1962 }
1963 return super.onGenericMotionEvent(event);
1964 }
1965
Michael Jurkab8f06722010-10-10 15:58:46 -07001966 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1967 if (mVelocityTracker == null) {
1968 mVelocityTracker = VelocityTracker.obtain();
1969 }
1970 mVelocityTracker.addMovement(ev);
1971 }
1972
1973 private void releaseVelocityTracker() {
1974 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001975 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001976 mVelocityTracker.recycle();
1977 mVelocityTracker = null;
1978 }
1979 }
1980
Winson Chung321e9ee2010-08-09 13:37:56 -07001981 private void onSecondaryPointerUp(MotionEvent ev) {
1982 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1983 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1984 final int pointerId = ev.getPointerId(pointerIndex);
1985 if (pointerId == mActivePointerId) {
1986 // This was our active pointer going up. Choose a new
1987 // active pointer and adjust accordingly.
1988 // TODO: Make this decision more intelligent.
1989 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1990 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1991 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001992 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001993 mActivePointerId = ev.getPointerId(newPointerIndex);
1994 if (mVelocityTracker != null) {
1995 mVelocityTracker.clear();
1996 }
1997 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001998 }
1999
Winson Chung321e9ee2010-08-09 13:37:56 -07002000 @Override
2001 public void requestChildFocus(View child, View focused) {
2002 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002003 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002004 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002005 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002006 }
2007 }
2008
Adam Cohend19d3ca2010-09-15 14:43:42 -07002009 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002010 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002011 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002012 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002013 final int childCount = getChildCount();
2014 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002015 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002016 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002017 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002018 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002019 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2020 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2021 minDistanceFromScreenCenter = distanceFromScreenCenter;
2022 minDistanceFromScreenCenterIndex = i;
2023 }
2024 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002025 return minDistanceFromScreenCenterIndex;
2026 }
2027
Adam Cohen1e4359c2014-08-18 13:12:16 -07002028 protected boolean isInOverScroll() {
2029 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2030 }
2031
2032 protected int getPageSnapDuration() {
2033 if (isInOverScroll()) {
2034 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2035 }
2036 return PAGE_SNAP_ANIMATION_DURATION;
2037
2038 }
2039
Adam Cohend19d3ca2010-09-15 14:43:42 -07002040 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002041 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002042 }
2043
Adam Cohene0f66b52010-11-23 15:06:07 -08002044 private static class ScrollInterpolator implements Interpolator {
2045 public ScrollInterpolator() {
2046 }
2047
2048 public float getInterpolation(float t) {
2049 t -= 1.0f;
2050 return t*t*t*t*t + 1;
2051 }
2052 }
2053
2054 // We want the duration of the page snap animation to be influenced by the distance that
2055 // the screen has to travel, however, we don't want this duration to be effected in a
2056 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2057 // of travel has on the overall snap duration.
2058 float distanceInfluenceForSnapDuration(float f) {
2059 f -= 0.5f; // center the values about 0.
2060 f *= 0.3f * Math.PI / 2.0f;
2061 return (float) Math.sin(f);
2062 }
2063
Michael Jurka0142d492010-08-25 17:46:15 -07002064 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002065 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002066 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002067
Adam Cohenedb40762013-07-18 16:45:45 -07002068 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002069 int delta = newX - mUnboundedScrollX;
2070 int duration = 0;
2071
Adam Cohen1e4359c2014-08-18 13:12:16 -07002072 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002073 // If the velocity is low enough, then treat this more as an automatic page advance
2074 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002075 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002076 return;
2077 }
2078
2079 // Here we compute a "distance" that will be used in the computation of the overall
2080 // snap duration. This is a function of the actual distance that needs to be traveled;
2081 // we keep this value close to half screen size in order to reduce the variance in snap
2082 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002083 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002084 float distance = halfScreenSize + halfScreenSize *
2085 distanceInfluenceForSnapDuration(distanceRatio);
2086
2087 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002088 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002089
2090 // we want the page's snap velocity to approximately match the velocity at which the
2091 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002092 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2093 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002094
2095 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002096 }
2097
2098 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002099 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002100 }
2101
Adam Cohen7d30a372013-07-01 17:03:59 -07002102 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002103 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002104 }
2105
Michael Jurka0142d492010-08-25 17:46:15 -07002106 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002107 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002108 }
2109
Adam Cohenf9618852013-11-08 06:45:03 -08002110 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2111 snapToPage(whichPage, duration, false, interpolator);
2112 }
2113
2114 protected void snapToPage(int whichPage, int duration, boolean immediate,
2115 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002116 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002117
Adam Cohenedb40762013-07-18 16:45:45 -07002118 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002119 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002121 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002122 }
2123
2124 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002125 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002126 }
Michael Jurka0142d492010-08-25 17:46:15 -07002127
Adam Cohenf9618852013-11-08 06:45:03 -08002128 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2129 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002130 whichPage = validateNewPage(whichPage);
2131
Adam Cohen7d30a372013-07-01 17:03:59 -07002132 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002133 View focusedChild = getFocusedChild();
2134 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002135 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002136 focusedChild.clearFocus();
2137 }
2138
2139 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002140 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002141 if (immediate) {
2142 duration = 0;
2143 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002144 duration = Math.abs(delta);
2145 }
2146
Adam Cohenf358a4b2013-07-23 16:47:31 -07002147 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002148 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002149 }
Adam Cohenf9618852013-11-08 06:45:03 -08002150
2151 if (interpolator != null) {
2152 mScroller.setInterpolator(interpolator);
2153 } else {
2154 mScroller.setInterpolator(mDefaultInterpolator);
2155 }
2156
Adam Cohen68d73932010-11-15 10:50:58 -08002157 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002158
Adam Cohen674531f2013-12-13 15:07:14 -08002159 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002160
2161 // Trigger a compute() to finish switching pages if necessary
2162 if (immediate) {
2163 computeScroll();
2164 }
2165
2166 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002167 invalidate();
2168 }
2169
Winson Chung321e9ee2010-08-09 13:37:56 -07002170 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002171 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002172 }
2173
2174 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002175 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002176 }
2177
Winson Chung86f77532010-08-24 11:08:22 -07002178 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002179 int result = -1;
2180 if (v != null) {
2181 ViewParent vp = v.getParent();
2182 int count = getChildCount();
2183 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002184 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002185 return i;
2186 }
2187 }
2188 }
2189 return result;
2190 }
2191
Adam Cohendbdff6b2013-09-18 19:09:15 -07002192 @Override
2193 public boolean performLongClick() {
2194 mCancelTap = true;
2195 return super.performLongClick();
2196 }
2197
Winson Chung321e9ee2010-08-09 13:37:56 -07002198 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002199 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002200
2201 SavedState(Parcelable superState) {
2202 super(superState);
2203 }
2204
Adam Cohen091440a2015-03-18 14:16:05 -07002205 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002206 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002207 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002208 }
2209
2210 @Override
2211 public void writeToParcel(Parcel out, int flags) {
2212 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002213 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002214 }
2215
2216 public static final Parcelable.Creator<SavedState> CREATOR =
2217 new Parcelable.Creator<SavedState>() {
2218 public SavedState createFromParcel(Parcel in) {
2219 return new SavedState(in);
2220 }
2221
2222 public SavedState[] newArray(int size) {
2223 return new SavedState[size];
2224 }
2225 };
2226 }
2227
Adam Cohen7d30a372013-07-01 17:03:59 -07002228 // Animate the drag view back to the original position
2229 void animateDragViewToOriginalPosition() {
2230 if (mDragView != null) {
2231 AnimatorSet anim = new AnimatorSet();
2232 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2233 anim.playTogether(
2234 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002235 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2236 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2237 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002238 anim.addListener(new AnimatorListenerAdapter() {
2239 @Override
2240 public void onAnimationEnd(Animator animation) {
2241 onPostReorderingAnimationCompleted();
2242 }
2243 });
2244 anim.start();
2245 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002246 }
2247
Sunny Goyal1d08f702015-05-04 15:50:25 -07002248 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002249 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2250 mTouchState = TOUCH_STATE_REORDERING;
2251 mIsReordering = true;
2252
Adam Cohen7d30a372013-07-01 17:03:59 -07002253 // We must invalidate to trigger a redraw to update the layers such that the drag view
2254 // is always drawn on top
2255 invalidate();
2256 }
2257
Adam Cohen091440a2015-03-18 14:16:05 -07002258 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002259 // Trigger the callback when reordering has settled
2260 --mPostReorderingPreZoomInRemainingAnimationCount;
2261 if (mPostReorderingPreZoomInRunnable != null &&
2262 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2263 mPostReorderingPreZoomInRunnable.run();
2264 mPostReorderingPreZoomInRunnable = null;
2265 }
2266 }
2267
Sunny Goyal1d08f702015-05-04 15:50:25 -07002268 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002269 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002270 }
2271
Adam Cohenf358a4b2013-07-23 16:47:31 -07002272 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002273 int dragViewIndex = indexOfChild(v);
2274
Adam Cohen327acfe2014-06-06 11:52:52 -07002275 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002276
Adam Cohen7d30a372013-07-01 17:03:59 -07002277 mTempVisiblePagesRange[0] = 0;
2278 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002279 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002280 mReorderingStarted = true;
2281
2282 // Check if we are within the reordering range
2283 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002284 dragViewIndex <= mTempVisiblePagesRange[1]) {
2285 // Find the drag view under the pointer
2286 mDragView = getChildAt(dragViewIndex);
2287 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2288 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002289 snapToPage(getPageNearestToCenterOfScreen());
2290 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002291 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002292 return true;
2293 }
2294 return false;
2295 }
2296
2297 boolean isReordering(boolean testTouchState) {
2298 boolean state = mIsReordering;
2299 if (testTouchState) {
2300 state &= (mTouchState == TOUCH_STATE_REORDERING);
2301 }
2302 return state;
2303 }
2304 void endReordering() {
2305 // For simplicity, we call endReordering sometimes even if reordering was never started.
2306 // In that case, we don't want to do anything.
2307 if (!mReorderingStarted) return;
2308 mReorderingStarted = false;
2309
2310 // If we haven't flung-to-delete the current child, then we just animate the drag view
2311 // back into position
2312 final Runnable onCompleteRunnable = new Runnable() {
2313 @Override
2314 public void run() {
2315 onEndReordering();
2316 }
2317 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002318
2319 mPostReorderingPreZoomInRunnable = new Runnable() {
2320 public void run() {
2321 onCompleteRunnable.run();
2322 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002323 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002324 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002325
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002326 mPostReorderingPreZoomInRemainingAnimationCount =
2327 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2328 // Snap to the current page
2329 snapToPage(indexOfChild(mDragView), 0);
2330 // Animate the drag view back to the front position
2331 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002332 }
2333
Adam Cohen7d30a372013-07-01 17:03:59 -07002334 private static final int ANIM_TAG_KEY = 100;
2335
Winson Chung6a0f57d2011-06-29 20:10:49 -07002336 /* Accessibility */
2337 @Override
2338 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2339 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002340 info.setScrollable(getPageCount() > 1);
2341 if (getCurrentPage() < getPageCount() - 1) {
2342 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2343 }
2344 if (getCurrentPage() > 0) {
2345 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2346 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002347 }
2348
2349 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002350 public void sendAccessibilityEvent(int eventType) {
2351 // Don't let the view send real scroll events.
2352 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2353 super.sendAccessibilityEvent(eventType);
2354 }
2355 }
2356
2357 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002358 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2359 super.onInitializeAccessibilityEvent(event);
2360 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002361 }
2362
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002363 @Override
2364 public boolean performAccessibilityAction(int action, Bundle arguments) {
2365 if (super.performAccessibilityAction(action, arguments)) {
2366 return true;
2367 }
2368 switch (action) {
2369 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2370 if (getCurrentPage() < getPageCount() - 1) {
2371 scrollRight();
2372 return true;
2373 }
2374 } break;
2375 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2376 if (getCurrentPage() > 0) {
2377 scrollLeft();
2378 return true;
2379 }
2380 } break;
2381 }
2382 return false;
2383 }
2384
Adam Cohen0ffac432013-07-10 11:19:26 -07002385 protected String getCurrentPageDescription() {
2386 return String.format(getContext().getString(R.string.default_scroll_format),
2387 getNextPage() + 1, getChildCount());
2388 }
2389
Winson Chungd11265e2011-08-30 13:37:23 -07002390 @Override
2391 public boolean onHoverEvent(android.view.MotionEvent event) {
2392 return true;
2393 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002394}