blob: 4a85b448fd87541f24baa2f39865a079db1ab4bd [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070031import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Adam Cohen091440a2015-03-18 14:16:05 -070050import com.android.launcher3.util.Thunk;
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 mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700150 protected int mPageLayoutWidthGap;
151 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700152 protected int mCellCountX = 0;
153 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700154 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800155 protected boolean mAllowOverScroll = true;
156 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800157 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700158 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800161 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
162 // the screens from continuing to translate beyond the normal bounds.
163 protected int mOverScrollX;
164
Michael Jurka5f1c5092010-09-03 14:15:02 -0700165 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700166
Michael Jurka5f1c5092010-09-03 14:15:02 -0700167 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700168
Winson Chung86f77532010-08-24 11:08:22 -0700169 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700170
Michael Jurka0142d492010-08-25 17:46:15 -0700171 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700173
Patrick Dubroy1262e362010-10-06 15:49:50 -0700174 protected boolean mIsPageMoving = false;
175
Adam Cohenc2d6e892014-10-16 09:49:24 -0700176 private boolean mWasInOverscroll = false;
177
Winson Chungd2be3812013-07-16 11:11:32 -0700178 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk int mPageIndicatorViewId;
180 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 // The viewport whether the pages are to be contained (the actual view may be larger than the
182 // viewport)
183 private Rect mViewport = new Rect();
184
185 // Reordering
186 // We use the min scale to determine how much to expand the actually PagedView measured
187 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700188 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700189 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700190 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
191
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700193 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700194 protected View mDragView;
195 protected AnimatorSet mZoomInOutAnim;
196 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700197 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700198 // This variable's scope is only for the duration of startReordering() and endReordering()
199 private boolean mReorderingStarted = false;
200 // This variable's scope is for the duration of startReordering() and after the zoomIn()
201 // animation after endReordering()
202 private boolean mIsReordering;
203 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
204 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
205 private int mPostReorderingPreZoomInRemainingAnimationCount;
206 private Runnable mPostReorderingPreZoomInRunnable;
207
Adam Cohen7d30a372013-07-01 17:03:59 -0700208 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700209 private static final Matrix sTmpInvMatrix = new Matrix();
210 private static final float[] sTmpPoint = new float[2];
211 private static final int[] sTmpIntPoint = new int[2];
212 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700213
John Spurlock77e1f472013-09-11 10:09:51 -0400214 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700215 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400216
Winson Chung86f77532010-08-24 11:08:22 -0700217 public interface PageSwitchListener {
218 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 }
220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 public PagedView(Context context) {
222 this(context, null);
223 }
224
225 public PagedView(Context context, AttributeSet attrs) {
226 this(context, attrs, 0);
227 }
228
229 public PagedView(Context context, AttributeSet attrs, int defStyle) {
230 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700231
Adam Cohen9c4949e2010-10-05 12:27:22 -0700232 TypedArray a = context.obtainStyledAttributes(attrs,
233 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700234
Winson Chungdf4b83d2010-10-20 17:49:27 -0700235 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700236 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700237 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700238 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700239 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700240 a.recycle();
241
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700243 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700244 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246
247 /**
248 * Initializes various states for this workspace.
249 */
Michael Jurka0142d492010-08-25 17:46:15 -0700250 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800251 mScroller = new LauncherScroller(getContext());
252 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700253 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700254 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700255
256 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700257 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700259 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800260
261 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
262 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
263 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700264 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 }
266
Adam Cohenf9618852013-11-08 06:45:03 -0800267 protected void setDefaultInterpolator(Interpolator interpolator) {
268 mDefaultInterpolator = interpolator;
269 mScroller.setInterpolator(mDefaultInterpolator);
270 }
271
Winson Chungd2be3812013-07-16 11:11:32 -0700272 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700273 super.onAttachedToWindow();
274
Winson Chungd2be3812013-07-16 11:11:32 -0700275 // Hook up the page indicator
276 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700277 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700278 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700279 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700280 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700281
Winson Chung7819a562013-09-19 15:55:45 -0700282 ArrayList<PageIndicator.PageMarkerResources> markers =
283 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700284 for (int i = 0; i < getChildCount(); ++i) {
285 markers.add(getPageIndicatorMarker(i));
286 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700287
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700288 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700289
290 OnClickListener listener = getPageIndicatorClickListener();
291 if (listener != null) {
292 mPageIndicator.setOnClickListener(listener);
293 }
Adam Cohen53805212013-10-01 10:39:23 -0700294 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700295 }
296 }
297
Adam Cohen53805212013-10-01 10:39:23 -0700298 protected String getPageIndicatorDescription() {
299 return getCurrentPageDescription();
300 }
301
302 protected OnClickListener getPageIndicatorClickListener() {
303 return null;
304 }
305
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700306 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700307 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700308 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700309 // Unhook the page indicator
310 mPageIndicator = null;
311 }
312
Adam Cohen7d30a372013-07-01 17:03:59 -0700313 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700314 private float[] mapPointFromViewToParent(View v, float x, float y) {
315 sTmpPoint[0] = x;
316 sTmpPoint[1] = y;
317 v.getMatrix().mapPoints(sTmpPoint);
318 sTmpPoint[0] += v.getLeft();
319 sTmpPoint[1] += v.getTop();
320 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700321 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700322 private float[] mapPointFromParentToView(View v, float x, float y) {
323 sTmpPoint[0] = x - v.getLeft();
324 sTmpPoint[1] = y - v.getTop();
325 v.getMatrix().invert(sTmpInvMatrix);
326 sTmpInvMatrix.mapPoints(sTmpPoint);
327 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700328 }
329
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700330 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700331 if (mDragView != null) {
332 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
333 (mDragViewBaselineLeft - mDragView.getLeft());
334 float y = mLastMotionY - mDownMotionY;
335 mDragView.setTranslationX(x);
336 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700337
Adam Cohenf358a4b2013-07-23 16:47:31 -0700338 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
339 + x + ", " + y);
340 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700341 }
342
343 public void setMinScale(float f) {
344 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700345 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700346 requestLayout();
347 }
348
349 @Override
350 public void setScaleX(float scaleX) {
351 super.setScaleX(scaleX);
352 if (isReordering(true)) {
353 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
354 mLastMotionX = p[0];
355 mLastMotionY = p[1];
356 updateDragViewTranslationDuringDrag();
357 }
358 }
359
360 // Convenience methods to get the actual width/height of the PagedView (since it is measured
361 // to be larger to account for the minimum possible scale)
362 int getViewportWidth() {
363 return mViewport.width();
364 }
365 int getViewportHeight() {
366 return mViewport.height();
367 }
368
369 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
370 // PagedView both horizontally and vertically
371 int getViewportOffsetX() {
372 return (getMeasuredWidth() - getViewportWidth()) / 2;
373 }
374
375 int getViewportOffsetY() {
376 return (getMeasuredHeight() - getViewportHeight()) / 2;
377 }
378
Adam Cohenedb40762013-07-18 16:45:45 -0700379 PageIndicator getPageIndicator() {
380 return mPageIndicator;
381 }
Winson Chung7819a562013-09-19 15:55:45 -0700382 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
383 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700384 }
Adam Cohenedb40762013-07-18 16:45:45 -0700385
Adam Cohen674531f2013-12-13 15:07:14 -0800386 /**
387 * Add a page change listener which will be called when a page is _finished_ listening.
388 *
389 */
Winson Chung86f77532010-08-24 11:08:22 -0700390 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
391 mPageSwitchListener = pageSwitchListener;
392 if (mPageSwitchListener != null) {
393 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 }
395 }
396
397 /**
Winson Chung86f77532010-08-24 11:08:22 -0700398 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700400 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700401 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700403
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700404 /**
405 * Returns the index of page to be shown immediately afterwards.
406 */
Winson Chung360e63f2012-04-27 13:48:05 -0700407 int getNextPage() {
408 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
409 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700410
Winson Chung86f77532010-08-24 11:08:22 -0700411 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 return getChildCount();
413 }
414
Sunny Goyal83a8f042015-05-19 12:52:12 -0700415 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 return getChildAt(index);
417 }
418
Adam Cohenae4f1552011-10-20 00:15:42 -0700419 protected int indexToPage(int index) {
420 return index;
421 }
422
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800424 * Updates the scroll of the current page immediately to its final scroll position. We use this
425 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
426 * the previous tab page.
427 */
428 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700429 // If the current page is invalid, just reset the scroll position to zero
430 int newX = 0;
431 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700432 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700433 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800434 scrollTo(newX, 0);
435 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700436 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800437 }
438
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700439 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700440 mScroller.abortAnimation();
441 // We need to clean up the next page here to avoid computeScrollHelper from
442 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700443 if (resetNextPage) {
444 mNextPage = INVALID_PAGE;
445 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700446 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700447
448 private void forceFinishScroller() {
449 mScroller.forceFinished(true);
450 // We need to clean up the next page here to avoid computeScrollHelper from
451 // updating current page on the pass.
452 mNextPage = INVALID_PAGE;
453 }
454
Adam Cohen6f127a62014-06-12 14:54:41 -0700455 private int validateNewPage(int newPage) {
456 int validatedPage = newPage;
457 // When in free scroll mode, we need to clamp to the free scroll page range.
458 if (mFreeScroll) {
459 getFreeScrollPageRange(mTempVisiblePagesRange);
460 validatedPage = Math.max(mTempVisiblePagesRange[0],
461 Math.min(newPage, mTempVisiblePagesRange[1]));
462 }
463 // Ensure that it is clamped by the actual set of children in all cases
464 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
465 return validatedPage;
466 }
467
Chet Haasebc2f0822012-10-26 17:59:53 -0700468 /**
Winson Chung86f77532010-08-24 11:08:22 -0700469 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700471 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800472 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700473 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800474 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800475 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
476 // the default
477 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800478 return;
479 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700480 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700481 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700482 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700483 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800484 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 }
486
Winson Chung8c87cd82013-07-23 16:20:10 -0700487 /**
488 * The restore page will be set in place of the current page at the next (likely first)
489 * layout.
490 */
491 void setRestorePage(int restorePage) {
492 mRestorePage = restorePage;
493 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800494 int getRestorePage() {
495 return mRestorePage;
496 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700497
Adam Cohen674531f2013-12-13 15:07:14 -0800498 /**
499 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
500 * has settled.
501 */
Michael Jurka0142d492010-08-25 17:46:15 -0700502 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700503 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800504 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 }
Winson Chungd2be3812013-07-16 11:11:32 -0700506
Adam Cohen674531f2013-12-13 15:07:14 -0800507 updatePageIndicator();
508 }
509
510 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700511 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700512 if (mPageIndicator != null) {
513 mPageIndicator.setContentDescription(getPageIndicatorDescription());
514 if (!isReordering(false)) {
515 mPageIndicator.setActiveMarker(getNextPage());
516 }
Winson Chungd2be3812013-07-16 11:11:32 -0700517 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700518 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800519 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700520 if (!mIsPageMoving) {
521 mIsPageMoving = true;
522 onPageBeginMoving();
523 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700524 }
525
Michael Jurkace7e05f2011-02-01 22:02:35 -0800526 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700527 if (mIsPageMoving) {
528 mIsPageMoving = false;
529 onPageEndMoving();
530 }
Michael Jurka0142d492010-08-25 17:46:15 -0700531 }
532
Adam Cohen26976d92011-03-22 15:33:33 -0700533 protected boolean isPageMoving() {
534 return mIsPageMoving;
535 }
536
Michael Jurka0142d492010-08-25 17:46:15 -0700537 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700538 protected void onPageBeginMoving() {
539 }
540
541 // a method that subclasses can override to add behavior
542 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700543 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700544 }
545
Winson Chung321e9ee2010-08-09 13:37:56 -0700546 /**
Winson Chung86f77532010-08-24 11:08:22 -0700547 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 *
549 * @param l The listener used to respond to long clicks.
550 */
551 @Override
552 public void setOnLongClickListener(OnLongClickListener l) {
553 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700554 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700556 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
Adam Cohen1697b792013-09-17 19:08:21 -0700558 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 }
560
561 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800562 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700563 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800564 }
565
566 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700567 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700568 // In free scroll mode, we clamp the scrollX
569 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700570 // If the scroller is trying to move to a location beyond the maximum allowed
571 // in the free scroll mode, we make sure to end the scroll operation.
572 if (!mScroller.isFinished() &&
573 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
574 forceFinishScroller();
575 }
576
Adam Cohenf358a4b2013-07-23 16:47:31 -0700577 x = Math.min(x, mFreeScrollMaxScrollX);
578 x = Math.max(x, mFreeScrollMinScrollX);
579 }
580
Adam Cohen68d73932010-11-15 10:50:58 -0800581 mUnboundedScrollX = x;
582
Sunny Goyal70660032015-05-14 00:07:08 -0700583 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
584 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700585 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800586 super.scrollTo(0, y);
587 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700588 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700589 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700590 overScroll(x - mMaxScrollX);
591 } else {
592 overScroll(x);
593 }
Adam Cohen68d73932010-11-15 10:50:58 -0800594 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700595 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800596 super.scrollTo(mMaxScrollX, y);
597 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700598 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700599 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700600 overScroll(x);
601 } else {
602 overScroll(x - mMaxScrollX);
603 }
Adam Cohen68d73932010-11-15 10:50:58 -0800604 }
605 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700606 if (mWasInOverscroll) {
607 overScroll(0);
608 mWasInOverscroll = false;
609 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800610 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800611 super.scrollTo(x, y);
612 }
613
Michael Jurka0142d492010-08-25 17:46:15 -0700614 mTouchX = x;
615 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700616
617 // Update the last motion events when scrolling
618 if (isReordering(true)) {
619 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
620 mLastMotionX = p[0];
621 mLastMotionY = p[1];
622 updateDragViewTranslationDuringDrag();
623 }
Michael Jurka0142d492010-08-25 17:46:15 -0700624 }
625
Adam Cohen53805212013-10-01 10:39:23 -0700626 private void sendScrollAccessibilityEvent() {
627 AccessibilityManager am =
628 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
629 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700630 if (mCurrentPage != getNextPage()) {
631 AccessibilityEvent ev =
632 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700633 ev.setScrollable(true);
634 ev.setScrollX(getScrollX());
635 ev.setScrollY(getScrollY());
636 ev.setMaxScrollX(mMaxScrollX);
637 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700638
Vadim Tryshevf4715972015-05-08 17:21:03 -0700639 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700640 }
Adam Cohen53805212013-10-01 10:39:23 -0700641 }
642 }
643
Michael Jurka0142d492010-08-25 17:46:15 -0700644 // we moved this functionality to a helper function so SmoothPagedView can reuse it
645 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700648 if (getScrollX() != mScroller.getCurrX()
649 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700650 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700651 float scaleX = mFreeScroll ? getScaleX() : 1f;
652 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
653 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700654 }
Michael Jurka0142d492010-08-25 17:46:15 -0700655 invalidate();
656 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700657 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700658 sendScrollAccessibilityEvent();
659
Adam Cohen6f127a62014-06-12 14:54:41 -0700660 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700661 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700662 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700663
Adam Cohen73aa9752010-11-24 16:26:58 -0800664 // We don't want to trigger a page end moving unless the page has settled
665 // and the user has stopped scrolling
666 if (mTouchState == TOUCH_STATE_REST) {
667 pageEndMoving();
668 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700669
Adam Cohen7d30a372013-07-01 17:03:59 -0700670 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700671 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700672 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700673 if (am.isEnabled()) {
674 // Notify the user when the page changes
675 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700676 }
Michael Jurka0142d492010-08-25 17:46:15 -0700677 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 }
Michael Jurka0142d492010-08-25 17:46:15 -0700679 return false;
680 }
681
682 @Override
683 public void computeScroll() {
684 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
686
Adam Cohen96d30a12013-07-16 18:13:21 -0700687 public static class LayoutParams extends ViewGroup.LayoutParams {
688 public boolean isFullScreenPage = false;
689
690 /**
691 * {@inheritDoc}
692 */
693 public LayoutParams(int width, int height) {
694 super(width, height);
695 }
696
Sunny Goyal41b22c02015-05-14 15:20:48 -0700697 public LayoutParams(Context context, AttributeSet attrs) {
698 super(context, attrs);
699 }
700
Adam Cohen96d30a12013-07-16 18:13:21 -0700701 public LayoutParams(ViewGroup.LayoutParams source) {
702 super(source);
703 }
704 }
705
Sunny Goyal41b22c02015-05-14 15:20:48 -0700706 @Override
707 public LayoutParams generateLayoutParams(AttributeSet attrs) {
708 return new LayoutParams(getContext(), attrs);
709 }
710
711 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700712 protected LayoutParams generateDefaultLayoutParams() {
713 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
714 }
715
Sunny Goyal41b22c02015-05-14 15:20:48 -0700716 @Override
717 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
718 return new LayoutParams(p);
719 }
720
721 @Override
722 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
723 return p instanceof LayoutParams;
724 }
725
Adam Cohenedb40762013-07-18 16:45:45 -0700726 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700727 LayoutParams lp = generateDefaultLayoutParams();
728 lp.isFullScreenPage = true;
729 super.addView(page, 0, lp);
730 }
731
Adam Cohen410f3cd2013-09-22 12:09:32 -0700732 public int getNormalChildHeight() {
733 return mNormalChildHeight;
734 }
735
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 @Override
737 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700738 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700739 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
740 return;
741 }
742
Adam Cohen7d30a372013-07-01 17:03:59 -0700743 // We measure the dimensions of the PagedView to be larger than the pages so that when we
744 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700745 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
746 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
747 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700748 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800749 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700750 // viewport, we can be at most one and a half screens offset once we scale down
751 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700752 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
753 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700754
Winson Chungc82d2622013-11-06 13:23:29 -0800755 int parentWidthSize = (int) (2f * maxSize);
756 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700757 int scaledWidthSize, scaledHeightSize;
758 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700759 scaledWidthSize = (int) (parentWidthSize / mMinScale);
760 scaledHeightSize = (int) (parentHeightSize / mMinScale);
761 } else {
762 scaledWidthSize = widthSize;
763 scaledHeightSize = heightSize;
764 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700765 mViewport.set(0, 0, widthSize, heightSize);
766
767 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
768 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
769 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 }
771
Winson Chung8aad6102012-05-11 16:27:49 -0700772 // Return early if we aren't given a proper dimension
773 if (widthSize <= 0 || heightSize <= 0) {
774 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
775 return;
776 }
777
Adam Lesinski6b879f02010-11-04 16:15:23 -0700778 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
779 * of the All apps view on XLarge displays to not take up more space then it needs. Width
780 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
781 * each page to have the same width.
782 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 final int verticalPadding = getPaddingTop() + getPaddingBottom();
784 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700785
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700786 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700788 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700789 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700790 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
791 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
792 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
793 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 final int childCount = getChildCount();
795 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700796 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700797 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100798 if (child.getVisibility() != GONE) {
799 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700800
Vladimir Marko2824b072013-10-04 16:42:17 +0100801 int childWidthMode;
802 int childHeightMode;
803 int childWidth;
804 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700805
Vladimir Marko2824b072013-10-04 16:42:17 +0100806 if (!lp.isFullScreenPage) {
807 if (lp.width == LayoutParams.WRAP_CONTENT) {
808 childWidthMode = MeasureSpec.AT_MOST;
809 } else {
810 childWidthMode = MeasureSpec.EXACTLY;
811 }
812
813 if (lp.height == LayoutParams.WRAP_CONTENT) {
814 childHeightMode = MeasureSpec.AT_MOST;
815 } else {
816 childHeightMode = MeasureSpec.EXACTLY;
817 }
818
Adam Cohen3b185e22013-10-29 14:45:58 -0700819 childWidth = getViewportWidth() - horizontalPadding
820 - mInsets.left - mInsets.right;
821 childHeight = getViewportHeight() - verticalPadding
822 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100823 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700824 } else {
825 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700826 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100827
Adam Cohen3b185e22013-10-29 14:45:58 -0700828 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
829 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700830 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700831 if (referenceChildWidth == 0) {
832 referenceChildWidth = childWidth;
833 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700834
Vladimir Marko2824b072013-10-04 16:42:17 +0100835 final int childWidthMeasureSpec =
836 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
837 final int childHeightMeasureSpec =
838 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
839 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700840 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700841 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700842 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800843 }
844
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700846 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700847 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700848 return;
849 }
850
Winson Chung785d2eb2011-04-14 16:08:02 -0700851 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700853
Adam Cohen7d30a372013-07-01 17:03:59 -0700854 int offsetX = getViewportOffsetX();
855 int offsetY = getViewportOffsetY();
856
857 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700858 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700859
Sunny Goyal70660032015-05-14 00:07:08 -0700860 final int startIndex = mIsRtl ? childCount - 1 : 0;
861 final int endIndex = mIsRtl ? -1 : childCount;
862 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700863
Adam Cohen7d30a372013-07-01 17:03:59 -0700864 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700865
Adam Cohen3b185e22013-10-29 14:45:58 -0700866 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000867 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700868
869 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700870 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
871 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700872 }
873
Adam Cohen0ffac432013-07-10 11:19:26 -0700874 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700875 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700877 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100878 int childTop;
879 if (lp.isFullScreenPage) {
880 childTop = offsetY;
881 } else {
882 childTop = offsetY + getPaddingTop() + mInsets.top;
883 if (mCenterPagesVertically) {
884 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
885 }
886 }
887
Adam Cohen96d30a12013-07-16 18:13:21 -0700888 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700889 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800890
Winson Chung785d2eb2011-04-14 16:08:02 -0700891 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700892 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800893 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700894
Adam Cohen3b185e22013-10-29 14:45:58 -0700895 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
896 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000897
898 int pageGap = mPageSpacing;
899 int next = i + delta;
900 if (next != endIndex) {
901 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
902 } else {
903 nextLp = null;
904 }
905
906 // Prevent full screen pages from showing in the viewport
907 // when they are not the current page.
908 if (lp.isFullScreenPage) {
909 pageGap = getPaddingLeft();
910 } else if (nextLp != null && nextLp.isFullScreenPage) {
911 pageGap = getPaddingRight();
912 }
913
Sunny Goyala1fbd842015-05-20 13:40:27 -0700914 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 }
916 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700917
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700918 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -0800919 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700920 mFirstLayout = false;
921 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700922
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700923 final LayoutTransition transition = getLayoutTransition();
924 // If the transition is running defer updating max scroll, as some empty pages could
925 // still be present, and a max scroll change could cause sudden jumps in scroll.
926 if (transition != null && transition.isRunning()) {
927 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
928
929 @Override
930 public void startTransition(LayoutTransition transition, ViewGroup container,
931 View view, int transitionType) { }
932
933 @Override
934 public void endTransition(LayoutTransition transition, ViewGroup container,
935 View view, int transitionType) {
936 // Wait until all transitions are complete.
937 if (!transition.isRunning()) {
938 transition.removeTransitionListener(this);
939 updateMaxScrollX();
940 }
941 }
942 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700943 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700944 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700945 }
946
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700947 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700948 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700949 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700950 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700951 } else {
952 setCurrentPage(getNextPage());
953 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700954 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700955 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700956
957 if (isReordering(true)) {
958 updateDragViewTranslationDuringDrag();
959 }
Winson Chunge3193b92010-09-10 11:44:42 -0700960 }
961
Sunny Goyala1fbd842015-05-20 13:40:27 -0700962 protected int getChildGap() {
963 return 0;
964 }
965
Sunny Goyal316490e2015-06-02 09:38:28 -0700966 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700967 int childCount = getChildCount();
968 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700969 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700970 mMaxScrollX = getScrollForPage(index);
971 } else {
972 mMaxScrollX = 0;
973 }
974 }
975
Adam Cohen3b185e22013-10-29 14:45:58 -0700976 public void setPageSpacing(int pageSpacing) {
977 mPageSpacing = pageSpacing;
978 requestLayout();
979 }
980
Adam Cohenf34bab52010-09-30 14:11:56 -0700981 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700982 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
983
984 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700985 for (int i = 0; i < getChildCount(); i++) {
986 View child = getChildAt(i);
987 if (child != null) {
988 float scrollProgress = getScrollProgress(screenCenter, child, i);
989 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800990 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700991 }
992 }
993 invalidate();
994 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700995 }
996
Winson Chunge3193b92010-09-10 11:44:42 -0700997 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700999 // Update the page indicator, we don't update the page indicator as we
1000 // add/remove pages
1001 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001002 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001003 mPageIndicator.addMarker(pageIndex,
1004 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001005 true);
Winson Chungd2be3812013-07-16 11:11:32 -07001006 }
1007
Adam Cohen2591f6a2011-10-25 14:36:40 -07001008 // This ensures that when children are added, they get the correct transforms / alphas
1009 // in accordance with any scroll effects.
1010 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001011 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001012 invalidate();
1013 }
1014
Michael Jurka8b805b12012-04-18 14:23:14 -07001015 @Override
1016 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001017 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001018 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001019 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001020 }
1021
Winson Chungd2be3812013-07-16 11:11:32 -07001022 private void removeMarkerForView(int index) {
1023 // Update the page indicator, we don't update the page indicator as we
1024 // add/remove pages
1025 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001026 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001027 }
1028 }
1029
1030 @Override
1031 public void removeView(View v) {
1032 // XXX: We should find a better way to hook into this before the view
1033 // gets removed form its parent...
1034 removeMarkerForView(indexOfChild(v));
1035 super.removeView(v);
1036 }
1037 @Override
1038 public void removeViewInLayout(View v) {
1039 // XXX: We should find a better way to hook into this before the view
1040 // gets removed form its parent...
1041 removeMarkerForView(indexOfChild(v));
1042 super.removeViewInLayout(v);
1043 }
1044 @Override
1045 public void removeViewAt(int index) {
1046 // XXX: We should find a better way to hook into this before the view
1047 // gets removed form its parent...
1048 removeViewAt(index);
1049 super.removeViewAt(index);
1050 }
1051 @Override
1052 public void removeAllViewsInLayout() {
1053 // Update the page indicator, we don't update the page indicator as we
1054 // add/remove pages
1055 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001056 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001057 }
1058
1059 super.removeAllViewsInLayout();
1060 }
1061
Adam Cohen73894962011-10-31 13:17:17 -07001062 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001063 if (index < 0 || index > getChildCount() - 1) return 0;
1064
Adam Cohenf698c6e2013-07-17 18:44:25 -07001065 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001066
Adam Cohenf698c6e2013-07-17 18:44:25 -07001067 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001068 }
1069
Adam Cohen6f127a62014-06-12 14:54:41 -07001070 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001071 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001072 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001073 }
1074
Michael Jurkadde558b2011-11-09 22:09:06 -08001075 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001076 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001077 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001078
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001079 range[0] = -1;
1080 range[1] = -1;
1081
Michael Jurkac4fb9172010-09-02 17:19:20 -07001082 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001083 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001084 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001085
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001086 int count = getChildCount();
1087 for (int i = 0; i < count; i++) {
1088 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001089
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001090 sTmpIntPoint[0] = 0;
1091 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1092 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001093 if (range[0] == -1) {
1094 continue;
1095 } else {
1096 break;
1097 }
1098 }
1099
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001100 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1101 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1102 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001103 if (range[0] == -1) {
1104 continue;
1105 } else {
1106 break;
1107 }
1108 }
1109 curScreen = i;
1110 if (range[0] < 0) {
1111 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001112 }
1113 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001114
1115 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001116 } else {
1117 range[0] = -1;
1118 range[1] = -1;
1119 }
1120 }
1121
Michael Jurka920d7f42012-05-14 16:29:55 -07001122 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001123 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001124 }
1125
Michael Jurkadde558b2011-11-09 22:09:06 -08001126 @Override
1127 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001128 // Find out which screens are visible; as an optimization we only call draw on them
1129 final int pageCount = getChildCount();
1130 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001131 int halfScreenSize = getViewportWidth() / 2;
1132 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1133 // Otherwise it is equal to the scaled overscroll position.
1134 int screenCenter = mOverScrollX + halfScreenSize;
1135
1136 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1137 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1138 // set it for the next frame
1139 mForceScreenScrolled = false;
1140 screenScrolled(screenCenter);
1141 mLastScreenCenter = screenCenter;
1142 }
1143
Michael Jurkadde558b2011-11-09 22:09:06 -08001144 getVisiblePages(mTempVisiblePagesRange);
1145 final int leftScreen = mTempVisiblePagesRange[0];
1146 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001147 if (leftScreen != -1 && rightScreen != -1) {
1148 final long drawingTime = getDrawingTime();
1149 // Clip to the bounds
1150 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001151 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1152 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001153
Adam Cohen7d30a372013-07-01 17:03:59 -07001154 // Draw all the children, leaving the drag view for last
1155 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001156 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001157 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001158 if (mForceDrawAllChildrenNextFrame ||
1159 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001160 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001161 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001162 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001163 // Draw the drag view on top (if there is one)
1164 if (mDragView != null) {
1165 drawChild(canvas, mDragView, drawingTime);
1166 }
1167
Michael Jurka5e368ff2012-05-14 23:13:15 -07001168 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001169 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001170 }
Michael Jurka0142d492010-08-25 17:46:15 -07001171 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 }
1173
1174 @Override
1175 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001176 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001177 if (page != mCurrentPage || !mScroller.isFinished()) {
1178 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 return true;
1180 }
1181 return false;
1182 }
1183
1184 @Override
1185 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001186 int focusablePage;
1187 if (mNextPage != INVALID_PAGE) {
1188 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001190 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 }
Winson Chung86f77532010-08-24 11:08:22 -07001192 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001194 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 }
1196 return false;
1197 }
1198
1199 @Override
1200 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001201 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001203 if (getCurrentPage() > 0) {
1204 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 return true;
1206 }
1207 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001208 if (getCurrentPage() < getPageCount() - 1) {
1209 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 return true;
1211 }
1212 }
1213 return super.dispatchUnhandledMove(focused, direction);
1214 }
1215
1216 @Override
1217 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001218 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001219 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001220 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 }
1222 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001223 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001224 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
1226 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001227 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001228 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 }
1230 }
1231 }
1232
1233 /**
1234 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001235 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 *
Winson Chung86f77532010-08-24 11:08:22 -07001237 * This happens when live folders requery, and if they're off page, they
1238 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 */
1240 @Override
1241 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001242 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 View v = focused;
1244 while (true) {
1245 if (v == current) {
1246 super.focusableViewAvailable(focused);
1247 return;
1248 }
1249 if (v == this) {
1250 return;
1251 }
1252 ViewParent parent = v.getParent();
1253 if (parent instanceof View) {
1254 v = (View)v.getParent();
1255 } else {
1256 return;
1257 }
1258 }
1259 }
1260
1261 /**
1262 * {@inheritDoc}
1263 */
1264 @Override
1265 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1266 if (disallowIntercept) {
1267 // We need to make sure to cancel our long press if
1268 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001269 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001270 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 }
1272 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1273 }
1274
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001275 /**
1276 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1277 */
1278 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001279 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001280 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001281 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001282 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001283 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001284 }
1285
1286 /**
1287 * Return true if a tap at (x, y) should trigger a flip to the next page.
1288 */
1289 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001290 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001291 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001292 }
1293 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001294 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001295 }
Winson Chung52aee602013-01-30 12:01:02 -08001296
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 /** Returns whether x and y originated within the buffered viewport */
1298 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001299 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001300 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001301 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001302 }
1303
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 @Override
1305 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001306 if (DISABLE_TOUCH_INTERACTION) {
1307 return false;
1308 }
1309
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 /*
1311 * This method JUST determines whether we want to intercept the motion.
1312 * If we return true, onTouchEvent will be called and we do the actual
1313 * scrolling there.
1314 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001315 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001316
Winson Chung45e1d6e2010-11-09 17:19:49 -08001317 // Skip touch handling if there are no pages to swipe
1318 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1319
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 /*
1321 * Shortcut the most recurring case: the user is in the dragging
1322 * state and he is moving his finger. We want to intercept this
1323 * motion.
1324 */
1325 final int action = ev.getAction();
1326 if ((action == MotionEvent.ACTION_MOVE) &&
1327 (mTouchState == TOUCH_STATE_SCROLLING)) {
1328 return true;
1329 }
1330
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 switch (action & MotionEvent.ACTION_MASK) {
1332 case MotionEvent.ACTION_MOVE: {
1333 /*
1334 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1335 * whether the user has moved far enough from his original down touch.
1336 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001337 if (mActivePointerId != INVALID_POINTER) {
1338 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001339 }
1340 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1341 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1342 // i.e. fall through to the next case (don't break)
1343 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1344 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001345 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 }
1347
1348 case MotionEvent.ACTION_DOWN: {
1349 final float x = ev.getX();
1350 final float y = ev.getY();
1351 // Remember location of down touch
1352 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001353 mDownMotionY = y;
1354 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 mLastMotionX = x;
1356 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 float[] p = mapPointFromViewToParent(this, x, y);
1358 mParentDownMotionX = p[0];
1359 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001360 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001361 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001363
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 /*
1365 * If being flinged and user touches the screen, initiate drag;
1366 * otherwise don't. mScroller.isFinished should be false when
1367 * being flinged.
1368 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001369 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001370 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001371
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001372 if (finishedScrolling) {
1373 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001374 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001375 setCurrentPage(getNextPage());
1376 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001377 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001378 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001379 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1380 mTouchState = TOUCH_STATE_SCROLLING;
1381 } else {
1382 mTouchState = TOUCH_STATE_REST;
1383 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001384 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001385
Winson Chung86f77532010-08-24 11:08:22 -07001386 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001388 if (!DISABLE_TOUCH_SIDE_PAGES) {
1389 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1390 if (getChildCount() > 0) {
1391 if (hitsPreviousPage(x, y)) {
1392 mTouchState = TOUCH_STATE_PREV_PAGE;
1393 } else if (hitsNextPage(x, y)) {
1394 mTouchState = TOUCH_STATE_NEXT_PAGE;
1395 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 }
1397 }
1398 }
1399 break;
1400 }
1401
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001403 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001404 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 break;
1406
1407 case MotionEvent.ACTION_POINTER_UP:
1408 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001409 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 break;
1411 }
1412
1413 /*
1414 * The only time we want to intercept motion events is if we are in the
1415 * drag mode.
1416 */
1417 return mTouchState != TOUCH_STATE_REST;
1418 }
1419
Adam Cohenf8d28232011-02-01 21:47:00 -08001420 protected void determineScrollingStart(MotionEvent ev) {
1421 determineScrollingStart(ev, 1.0f);
1422 }
1423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 /*
1425 * Determines if we should change the touch state to start scrolling after the
1426 * user moves their touch point too far.
1427 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001428 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001429 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001431 if (pointerIndex == -1) return;
1432
1433 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 final float x = ev.getX(pointerIndex);
1435 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001436 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1437
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001439
Adam Cohenf8d28232011-02-01 21:47:00 -08001440 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001442
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001443 if (xMoved) {
1444 // Scroll if the user moved far enough along the X axis
1445 mTouchState = TOUCH_STATE_SCROLLING;
1446 mTotalMotionX += Math.abs(mLastMotionX - x);
1447 mLastMotionX = x;
1448 mLastMotionXRemainder = 0;
1449 mTouchX = getViewportOffsetX() + getScrollX();
1450 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1451 onScrollInteractionBegin();
1452 pageBeginMoving();
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;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001697 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001698 mLastMotionX = x;
1699 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001700 } else {
1701 awakenScrollBars();
1702 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001703 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1704 // Update the last motion position
1705 mLastMotionX = ev.getX();
1706 mLastMotionY = ev.getY();
1707
1708 // Update the parent down so that our zoom animations take this new movement into
1709 // account
1710 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1711 mParentDownMotionX = pt[0];
1712 mParentDownMotionY = pt[1];
1713 updateDragViewTranslationDuringDrag();
1714
1715 // Find the closest page to the touch point
1716 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001717
Adam Cohen7d30a372013-07-01 17:03:59 -07001718 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1719 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1720 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1721 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1722
Adam Cohenf358a4b2013-07-23 16:47:31 -07001723 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001724 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001725 mTempVisiblePagesRange[0] = 0;
1726 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001727 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001728 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1729 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1730 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1731 mSidePageHoverIndex = pageUnderPointIndex;
1732 mSidePageHoverRunnable = new Runnable() {
1733 @Override
1734 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001735 // Setup the scroll to the correct page before we swap the views
1736 snapToPage(pageUnderPointIndex);
1737
1738 // For each of the pages between the paged view and the drag view,
1739 // animate them from the previous position to the new position in
1740 // the layout (as a result of the drag view moving in the layout)
1741 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1742 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1743 dragViewIndex + 1 : pageUnderPointIndex;
1744 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1745 dragViewIndex - 1 : pageUnderPointIndex;
1746 for (int i = lowerIndex; i <= upperIndex; ++i) {
1747 View v = getChildAt(i);
1748 // dragViewIndex < pageUnderPointIndex, so after we remove the
1749 // drag view all subsequent views to pageUnderPointIndex will
1750 // shift down.
1751 int oldX = getViewportOffsetX() + getChildOffset(i);
1752 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1753
1754 // Animate the view translation from its old position to its new
1755 // position
1756 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1757 if (anim != null) {
1758 anim.cancel();
1759 }
1760
1761 v.setTranslationX(oldX - newX);
1762 anim = new AnimatorSet();
1763 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1764 anim.playTogether(
1765 ObjectAnimator.ofFloat(v, "translationX", 0f));
1766 anim.start();
1767 v.setTag(anim);
1768 }
1769
1770 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001771 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001772 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001773 if (mPageIndicator != null) {
1774 mPageIndicator.setActiveMarker(getNextPage());
1775 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001776 }
1777 };
1778 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1779 }
1780 } else {
1781 removeCallbacks(mSidePageHoverRunnable);
1782 mSidePageHoverIndex = -1;
1783 }
Adam Cohen564976a2010-10-13 18:52:07 -07001784 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001785 determineScrollingStart(ev);
1786 }
1787 break;
1788
1789 case MotionEvent.ACTION_UP:
1790 if (mTouchState == TOUCH_STATE_SCROLLING) {
1791 final int activePointerId = mActivePointerId;
1792 final int pointerIndex = ev.findPointerIndex(activePointerId);
1793 final float x = ev.getX(pointerIndex);
1794 final VelocityTracker velocityTracker = mVelocityTracker;
1795 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1796 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001797 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001798 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001799 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1800 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001801
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001802 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1803
Adam Cohen00481b32011-11-18 12:03:48 -08001804 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001805 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001806
Adam Cohenf358a4b2013-07-23 16:47:31 -07001807 if (!mFreeScroll) {
1808 // In the case that the page is moved far to one direction and then is flung
1809 // in the opposite direction, we use a threshold to determine whether we should
1810 // just return to the starting page, or if we should skip one further.
1811 boolean returnToOriginalPage = false;
1812 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1813 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1814 returnToOriginalPage = true;
1815 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001816
Adam Cohenf358a4b2013-07-23 16:47:31 -07001817 int finalPage;
1818 // We give flings precedence over large moves, which is why we short-circuit our
1819 // test for a large move if a fling has been registered. That is, a large
1820 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001821 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1822 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001823 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1824 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1825 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1826 snapToPageWithVelocity(finalPage, velocityX);
1827 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1828 (isFling && isVelocityXLeft)) &&
1829 mCurrentPage < getChildCount() - 1) {
1830 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1831 snapToPageWithVelocity(finalPage, velocityX);
1832 } else {
1833 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001834 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001835 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001836 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001837 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001838 }
1839
1840 float scaleX = getScaleX();
1841 int vX = (int) (-velocityX * scaleX);
1842 int initialScrollX = (int) (getScrollX() * scaleX);
1843
Adam Cohenf9618852013-11-08 06:45:03 -08001844 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001845 mScroller.fling(initialScrollX,
1846 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1847 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001848 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001849 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001850 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1851 // at this point we have not moved beyond the touch slop
1852 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1853 // we can just page
1854 int nextPage = Math.max(0, mCurrentPage - 1);
1855 if (nextPage != mCurrentPage) {
1856 snapToPage(nextPage);
1857 } else {
1858 snapToDestination();
1859 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001860 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001861 // at this point we have not moved beyond the touch slop
1862 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1863 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001864 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1865 if (nextPage != mCurrentPage) {
1866 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001867 } else {
1868 snapToDestination();
1869 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001870 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1871 // Update the last motion position
1872 mLastMotionX = ev.getX();
1873 mLastMotionY = ev.getY();
1874
1875 // Update the parent down so that our zoom animations take this new movement into
1876 // account
1877 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1878 mParentDownMotionX = pt[0];
1879 mParentDownMotionY = pt[1];
1880 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001881 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001882 if (!mCancelTap) {
1883 onUnhandledTap(ev);
1884 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001885 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001886
1887 // Remove the callback to wait for the side page hover timeout
1888 removeCallbacks(mSidePageHoverRunnable);
1889 // End any intermediate reordering states
1890 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001891 break;
1892
1893 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001894 if (mTouchState == TOUCH_STATE_SCROLLING) {
1895 snapToDestination();
1896 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001897 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001898 break;
1899
1900 case MotionEvent.ACTION_POINTER_UP:
1901 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001902 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001903 break;
1904 }
1905
1906 return true;
1907 }
1908
Adam Cohen7d30a372013-07-01 17:03:59 -07001909 private void resetTouchState() {
1910 releaseVelocityTracker();
1911 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001912 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001913 mTouchState = TOUCH_STATE_REST;
1914 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001915 }
1916
Adam Cohenc2d6e892014-10-16 09:49:24 -07001917 /**
1918 * Triggered by scrolling via touch
1919 */
1920 protected void onScrollInteractionBegin() {
1921 }
1922
1923 protected void onScrollInteractionEnd() {
1924 }
1925
Adam Cohen1697b792013-09-17 19:08:21 -07001926 protected void onUnhandledTap(MotionEvent ev) {
1927 ((Launcher) getContext()).onClick(this);
1928 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001929
Winson Chung185d7162011-02-28 13:47:29 -08001930 @Override
1931 public boolean onGenericMotionEvent(MotionEvent event) {
1932 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1933 switch (event.getAction()) {
1934 case MotionEvent.ACTION_SCROLL: {
1935 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1936 final float vscroll;
1937 final float hscroll;
1938 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1939 vscroll = 0;
1940 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1941 } else {
1942 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1943 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1944 }
1945 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001946 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001947 : (hscroll > 0 || vscroll > 0);
1948 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001949 scrollRight();
1950 } else {
1951 scrollLeft();
1952 }
1953 return true;
1954 }
1955 }
1956 }
1957 }
1958 return super.onGenericMotionEvent(event);
1959 }
1960
Michael Jurkab8f06722010-10-10 15:58:46 -07001961 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1962 if (mVelocityTracker == null) {
1963 mVelocityTracker = VelocityTracker.obtain();
1964 }
1965 mVelocityTracker.addMovement(ev);
1966 }
1967
1968 private void releaseVelocityTracker() {
1969 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001970 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001971 mVelocityTracker.recycle();
1972 mVelocityTracker = null;
1973 }
1974 }
1975
Winson Chung321e9ee2010-08-09 13:37:56 -07001976 private void onSecondaryPointerUp(MotionEvent ev) {
1977 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1978 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1979 final int pointerId = ev.getPointerId(pointerIndex);
1980 if (pointerId == mActivePointerId) {
1981 // This was our active pointer going up. Choose a new
1982 // active pointer and adjust accordingly.
1983 // TODO: Make this decision more intelligent.
1984 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1985 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1986 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001987 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001988 mActivePointerId = ev.getPointerId(newPointerIndex);
1989 if (mVelocityTracker != null) {
1990 mVelocityTracker.clear();
1991 }
1992 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001993 }
1994
Winson Chung321e9ee2010-08-09 13:37:56 -07001995 @Override
1996 public void requestChildFocus(View child, View focused) {
1997 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001998 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001999 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002000 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002001 }
2002 }
2003
Adam Cohend19d3ca2010-09-15 14:43:42 -07002004 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002005 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002006 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002007 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002008 final int childCount = getChildCount();
2009 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002010 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002011 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002012 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002013 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002014 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2015 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2016 minDistanceFromScreenCenter = distanceFromScreenCenter;
2017 minDistanceFromScreenCenterIndex = i;
2018 }
2019 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002020 return minDistanceFromScreenCenterIndex;
2021 }
2022
Adam Cohen1e4359c2014-08-18 13:12:16 -07002023 protected boolean isInOverScroll() {
2024 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2025 }
2026
2027 protected int getPageSnapDuration() {
2028 if (isInOverScroll()) {
2029 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2030 }
2031 return PAGE_SNAP_ANIMATION_DURATION;
2032
2033 }
2034
Adam Cohend19d3ca2010-09-15 14:43:42 -07002035 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002036 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002037 }
2038
Adam Cohene0f66b52010-11-23 15:06:07 -08002039 private static class ScrollInterpolator implements Interpolator {
2040 public ScrollInterpolator() {
2041 }
2042
2043 public float getInterpolation(float t) {
2044 t -= 1.0f;
2045 return t*t*t*t*t + 1;
2046 }
2047 }
2048
2049 // We want the duration of the page snap animation to be influenced by the distance that
2050 // the screen has to travel, however, we don't want this duration to be effected in a
2051 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2052 // of travel has on the overall snap duration.
2053 float distanceInfluenceForSnapDuration(float f) {
2054 f -= 0.5f; // center the values about 0.
2055 f *= 0.3f * Math.PI / 2.0f;
2056 return (float) Math.sin(f);
2057 }
2058
Michael Jurka0142d492010-08-25 17:46:15 -07002059 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002060 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002061 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002062
Adam Cohenedb40762013-07-18 16:45:45 -07002063 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002064 int delta = newX - mUnboundedScrollX;
2065 int duration = 0;
2066
Adam Cohen1e4359c2014-08-18 13:12:16 -07002067 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002068 // If the velocity is low enough, then treat this more as an automatic page advance
2069 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002070 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002071 return;
2072 }
2073
2074 // Here we compute a "distance" that will be used in the computation of the overall
2075 // snap duration. This is a function of the actual distance that needs to be traveled;
2076 // we keep this value close to half screen size in order to reduce the variance in snap
2077 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002078 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002079 float distance = halfScreenSize + halfScreenSize *
2080 distanceInfluenceForSnapDuration(distanceRatio);
2081
2082 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002083 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002084
2085 // we want the page's snap velocity to approximately match the velocity at which the
2086 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002087 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2088 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002089
2090 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002091 }
2092
Sunny Goyal1740d902015-05-27 11:14:01 -07002093 public void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002094 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002095 }
2096
Adam Cohen7d30a372013-07-01 17:03:59 -07002097 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002098 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002099 }
2100
Michael Jurka0142d492010-08-25 17:46:15 -07002101 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002102 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002103 }
2104
Adam Cohenf9618852013-11-08 06:45:03 -08002105 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2106 snapToPage(whichPage, duration, false, interpolator);
2107 }
2108
2109 protected void snapToPage(int whichPage, int duration, boolean immediate,
2110 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002111 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002112
Adam Cohenedb40762013-07-18 16:45:45 -07002113 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002114 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002116 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002117 }
2118
2119 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002120 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002121 }
Michael Jurka0142d492010-08-25 17:46:15 -07002122
Adam Cohenf9618852013-11-08 06:45:03 -08002123 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2124 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002125 whichPage = validateNewPage(whichPage);
2126
Adam Cohen7d30a372013-07-01 17:03:59 -07002127 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002128 View focusedChild = getFocusedChild();
2129 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002130 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002131 focusedChild.clearFocus();
2132 }
2133
2134 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002135 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002136 if (immediate) {
2137 duration = 0;
2138 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002139 duration = Math.abs(delta);
2140 }
2141
Adam Cohenf358a4b2013-07-23 16:47:31 -07002142 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002143 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002144 }
Adam Cohenf9618852013-11-08 06:45:03 -08002145
2146 if (interpolator != null) {
2147 mScroller.setInterpolator(interpolator);
2148 } else {
2149 mScroller.setInterpolator(mDefaultInterpolator);
2150 }
2151
Adam Cohen68d73932010-11-15 10:50:58 -08002152 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002153
Adam Cohen674531f2013-12-13 15:07:14 -08002154 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002155
2156 // Trigger a compute() to finish switching pages if necessary
2157 if (immediate) {
2158 computeScroll();
2159 }
2160
2161 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002162 invalidate();
2163 }
2164
Winson Chung321e9ee2010-08-09 13:37:56 -07002165 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002166 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002167 }
2168
2169 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002170 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002171 }
2172
Winson Chung86f77532010-08-24 11:08:22 -07002173 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002174 int result = -1;
2175 if (v != null) {
2176 ViewParent vp = v.getParent();
2177 int count = getChildCount();
2178 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002179 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002180 return i;
2181 }
2182 }
2183 }
2184 return result;
2185 }
2186
Adam Cohendbdff6b2013-09-18 19:09:15 -07002187 @Override
2188 public boolean performLongClick() {
2189 mCancelTap = true;
2190 return super.performLongClick();
2191 }
2192
Winson Chung321e9ee2010-08-09 13:37:56 -07002193 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002194 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002195
2196 SavedState(Parcelable superState) {
2197 super(superState);
2198 }
2199
Adam Cohen091440a2015-03-18 14:16:05 -07002200 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002201 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002202 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002203 }
2204
2205 @Override
2206 public void writeToParcel(Parcel out, int flags) {
2207 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002208 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002209 }
2210
2211 public static final Parcelable.Creator<SavedState> CREATOR =
2212 new Parcelable.Creator<SavedState>() {
2213 public SavedState createFromParcel(Parcel in) {
2214 return new SavedState(in);
2215 }
2216
2217 public SavedState[] newArray(int size) {
2218 return new SavedState[size];
2219 }
2220 };
2221 }
2222
Adam Cohen7d30a372013-07-01 17:03:59 -07002223 // Animate the drag view back to the original position
2224 void animateDragViewToOriginalPosition() {
2225 if (mDragView != null) {
2226 AnimatorSet anim = new AnimatorSet();
2227 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2228 anim.playTogether(
2229 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002230 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2231 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2232 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002233 anim.addListener(new AnimatorListenerAdapter() {
2234 @Override
2235 public void onAnimationEnd(Animator animation) {
2236 onPostReorderingAnimationCompleted();
2237 }
2238 });
2239 anim.start();
2240 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002241 }
2242
Sunny Goyal1d08f702015-05-04 15:50:25 -07002243 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002244 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2245 mTouchState = TOUCH_STATE_REORDERING;
2246 mIsReordering = true;
2247
Adam Cohen7d30a372013-07-01 17:03:59 -07002248 // We must invalidate to trigger a redraw to update the layers such that the drag view
2249 // is always drawn on top
2250 invalidate();
2251 }
2252
Adam Cohen091440a2015-03-18 14:16:05 -07002253 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002254 // Trigger the callback when reordering has settled
2255 --mPostReorderingPreZoomInRemainingAnimationCount;
2256 if (mPostReorderingPreZoomInRunnable != null &&
2257 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2258 mPostReorderingPreZoomInRunnable.run();
2259 mPostReorderingPreZoomInRunnable = null;
2260 }
2261 }
2262
Sunny Goyal1d08f702015-05-04 15:50:25 -07002263 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002264 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002265 }
2266
Adam Cohenf358a4b2013-07-23 16:47:31 -07002267 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002268 int dragViewIndex = indexOfChild(v);
2269
Adam Cohen327acfe2014-06-06 11:52:52 -07002270 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002271
Adam Cohen7d30a372013-07-01 17:03:59 -07002272 mTempVisiblePagesRange[0] = 0;
2273 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002274 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002275 mReorderingStarted = true;
2276
2277 // Check if we are within the reordering range
2278 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002279 dragViewIndex <= mTempVisiblePagesRange[1]) {
2280 // Find the drag view under the pointer
2281 mDragView = getChildAt(dragViewIndex);
2282 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2283 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002284 snapToPage(getPageNearestToCenterOfScreen());
2285 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002286 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002287 return true;
2288 }
2289 return false;
2290 }
2291
2292 boolean isReordering(boolean testTouchState) {
2293 boolean state = mIsReordering;
2294 if (testTouchState) {
2295 state &= (mTouchState == TOUCH_STATE_REORDERING);
2296 }
2297 return state;
2298 }
2299 void endReordering() {
2300 // For simplicity, we call endReordering sometimes even if reordering was never started.
2301 // In that case, we don't want to do anything.
2302 if (!mReorderingStarted) return;
2303 mReorderingStarted = false;
2304
2305 // If we haven't flung-to-delete the current child, then we just animate the drag view
2306 // back into position
2307 final Runnable onCompleteRunnable = new Runnable() {
2308 @Override
2309 public void run() {
2310 onEndReordering();
2311 }
2312 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002313
2314 mPostReorderingPreZoomInRunnable = new Runnable() {
2315 public void run() {
2316 onCompleteRunnable.run();
2317 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002318 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002319 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002320
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002321 mPostReorderingPreZoomInRemainingAnimationCount =
2322 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2323 // Snap to the current page
2324 snapToPage(indexOfChild(mDragView), 0);
2325 // Animate the drag view back to the front position
2326 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002327 }
2328
Adam Cohen7d30a372013-07-01 17:03:59 -07002329 private static final int ANIM_TAG_KEY = 100;
2330
Winson Chung6a0f57d2011-06-29 20:10:49 -07002331 /* Accessibility */
Sunny Goyal316490e2015-06-02 09:38:28 -07002332 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002333 @Override
2334 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2335 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002336 info.setScrollable(getPageCount() > 1);
2337 if (getCurrentPage() < getPageCount() - 1) {
2338 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2339 }
2340 if (getCurrentPage() > 0) {
2341 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2342 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002343 info.setClassName(getClass().getName());
2344
2345 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2346 // Besides disabling the accessibility long-click, this also prevents this view from getting
2347 // accessibility focus.
2348 info.setLongClickable(false);
2349 if (Utilities.isLmpOrAbove()) {
2350 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2351 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002352 }
2353
2354 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002355 public void sendAccessibilityEvent(int eventType) {
2356 // Don't let the view send real scroll events.
2357 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2358 super.sendAccessibilityEvent(eventType);
2359 }
2360 }
2361
2362 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002363 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2364 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002365 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002366 }
2367
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002368 @Override
2369 public boolean performAccessibilityAction(int action, Bundle arguments) {
2370 if (super.performAccessibilityAction(action, arguments)) {
2371 return true;
2372 }
2373 switch (action) {
2374 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2375 if (getCurrentPage() < getPageCount() - 1) {
2376 scrollRight();
2377 return true;
2378 }
2379 } break;
2380 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2381 if (getCurrentPage() > 0) {
2382 scrollLeft();
2383 return true;
2384 }
2385 } break;
2386 }
2387 return false;
2388 }
2389
Adam Cohen0ffac432013-07-10 11:19:26 -07002390 protected String getCurrentPageDescription() {
2391 return String.format(getContext().getString(R.string.default_scroll_format),
2392 getNextPage() + 1, getChildCount());
2393 }
2394
Winson Chungd11265e2011-08-30 13:37:23 -07002395 @Override
2396 public boolean onHoverEvent(android.view.MotionEvent event) {
2397 return true;
2398 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002399}