blob: e27ea64358a841531f31cccaa18c7bf95697525d [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070028import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070030import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.os.Parcel;
32import android.os.Parcelable;
33import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070034import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080036import android.view.InputDevice;
37import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070038import android.view.MotionEvent;
39import android.view.VelocityTracker;
40import android.view.View;
41import android.view.ViewConfiguration;
42import android.view.ViewGroup;
43import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070044import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070045import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080047import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070048
Adam Cohen091440a2015-03-18 14:16:05 -070049import com.android.launcher3.util.Thunk;
50
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
Michael Jurka8b805b12012-04-18 14:23:14 -070057public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070058 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohen7d30a372013-07-01 17:03:59 -070065 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070066 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070067 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070068 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb5ba0972011-09-07 18:02:31 -070070 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070071 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080072
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080074 // The page is moved more than halfway, automatically move to the next page on touch up.
75 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080076
Sunny Goyal8e2133b2015-05-06 13:39:07 -070077 private static final float MAX_SCROLL_PROGRESS = 1.0f;
78
Adam Cohen265b9a62011-12-07 14:37:18 -080079 // The following constants need to be scaled based on density. The scaled versions will be
80 // assigned to the corresponding member variables below.
81 private static final int FLING_THRESHOLD_VELOCITY = 500;
82 private static final int MIN_SNAP_VELOCITY = 1500;
83 private static final int MIN_FLING_VELOCITY = 250;
84
Adam Cohen7d30a372013-07-01 17:03:59 -070085 // We are disabling touch interaction of the widget region for factory ROM.
86 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070087 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070088
Adam Cohen21cd0022013-10-09 18:57:02 -070089 public static final int INVALID_RESTORE_PAGE = -1001;
90
Adam Cohenf358a4b2013-07-23 16:47:31 -070091 private boolean mFreeScroll = false;
92 private int mFreeScrollMinScrollX = -1;
93 private int mFreeScrollMaxScrollX = -1;
94
Winson Chung8aad6102012-05-11 16:27:49 -070095 static final int AUTOMATIC_PAGE_SPACING = -1;
96
Adam Cohen265b9a62011-12-07 14:37:18 -080097 protected int mFlingThresholdVelocity;
98 protected int mMinFlingVelocity;
99 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700100
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected float mSmoothingTime;
103 protected float mTouchX;
104
105 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700106 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700107
108 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700109 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700110 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800113 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800114 protected LauncherScroller mScroller;
115 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Adam Cohen7d30a372013-07-01 17:03:59 -0700119 private float mParentDownMotionX;
120 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700122 private float mDownMotionY;
123 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700124 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800125 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800126 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800127 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800128 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700129 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700130
Adam Cohendbdff6b2013-09-18 19:09:15 -0700131 private boolean mCancelTap;
132
Adam Cohenedb40762013-07-18 16:45:45 -0700133 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka0142d492010-08-25 17:46:15 -0700135 protected final static int TOUCH_STATE_REST = 0;
136 protected final static int TOUCH_STATE_SCROLLING = 1;
137 protected final static int TOUCH_STATE_PREV_PAGE = 2;
138 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700139 protected final static int TOUCH_STATE_REORDERING = 4;
140
Adam Cohene45440e2010-10-14 18:33:38 -0700141 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka0142d492010-08-25 17:46:15 -0700143 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700144 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka7426c422010-11-11 15:23:47 -0800148 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149 private int mPagingTouchSlop;
150 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700151 protected int mPageLayoutWidthGap;
152 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700153 protected int mCellCountX = 0;
154 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700155 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800156 protected boolean mAllowOverScroll = true;
157 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800158 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700159 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800162 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
163 // the screens from continuing to translate beyond the normal bounds.
164 protected int mOverScrollX;
165
Michael Jurka5f1c5092010-09-03 14:15:02 -0700166 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700167
Michael Jurka5f1c5092010-09-03 14:15:02 -0700168 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700169
Winson Chung86f77532010-08-24 11:08:22 -0700170 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
Michael Jurka0142d492010-08-25 17:46:15 -0700172 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700173 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700174
175 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
176 // to switch to a new page
177 protected boolean mUsePagingTouchSlop = true;
178
Michael Jurka8b805b12012-04-18 14:23:14 -0700179 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700180 // (SmoothPagedView does this)
181 protected boolean mDeferScrollUpdate = false;
182
Patrick Dubroy1262e362010-10-06 15:49:50 -0700183 protected boolean mIsPageMoving = false;
184
Adam Cohenc2d6e892014-10-16 09:49:24 -0700185 private boolean mWasInOverscroll = false;
186
Winson Chungd2be3812013-07-16 11:11:32 -0700187 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700188 @Thunk int mPageIndicatorViewId;
189 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700190 // The viewport whether the pages are to be contained (the actual view may be larger than the
191 // viewport)
192 private Rect mViewport = new Rect();
193
194 // Reordering
195 // We use the min scale to determine how much to expand the actually PagedView measured
196 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700197 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
198 private static int REORDERING_REORDER_REPOSITION_DURATION = 300;
199 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
200
Adam Cohen7d30a372013-07-01 17:03:59 -0700201 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700202 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700203 protected View mDragView;
204 protected AnimatorSet mZoomInOutAnim;
205 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700207 // This variable's scope is only for the duration of startReordering() and endReordering()
208 private boolean mReorderingStarted = false;
209 // This variable's scope is for the duration of startReordering() and after the zoomIn()
210 // animation after endReordering()
211 private boolean mIsReordering;
212 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
213 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
214 private int mPostReorderingPreZoomInRemainingAnimationCount;
215 private Runnable mPostReorderingPreZoomInRunnable;
216
Adam Cohen7d30a372013-07-01 17:03:59 -0700217 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700218 private static final Matrix sTmpInvMatrix = new Matrix();
219 private static final float[] sTmpPoint = new float[2];
220 private static final int[] sTmpIntPoint = new int[2];
221 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700222
John Spurlock77e1f472013-09-11 10:09:51 -0400223 protected final Rect mInsets = new Rect();
224
Winson Chung86f77532010-08-24 11:08:22 -0700225 public interface PageSwitchListener {
226 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 public PagedView(Context context) {
230 this(context, null);
231 }
232
233 public PagedView(Context context, AttributeSet attrs) {
234 this(context, attrs, 0);
235 }
236
237 public PagedView(Context context, AttributeSet attrs, int defStyle) {
238 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700239
Adam Cohen9c4949e2010-10-05 12:27:22 -0700240 TypedArray a = context.obtainStyledAttributes(attrs,
241 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700242
Winson Chungdf4b83d2010-10-20 17:49:27 -0700243 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700244 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700245 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700246 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700247 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700248 a.recycle();
249
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700251 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 }
253
254 /**
255 * Initializes various states for this workspace.
256 */
Michael Jurka0142d492010-08-25 17:46:15 -0700257 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800258 mScroller = new LauncherScroller(getContext());
259 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700260 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700261 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700262
263 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700264 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
266 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700267 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800268
269 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
270 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
271 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700272 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 }
274
Adam Cohenf9618852013-11-08 06:45:03 -0800275 protected void setDefaultInterpolator(Interpolator interpolator) {
276 mDefaultInterpolator = interpolator;
277 mScroller.setInterpolator(mDefaultInterpolator);
278 }
279
Winson Chungd2be3812013-07-16 11:11:32 -0700280 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700281 super.onAttachedToWindow();
282
Winson Chungd2be3812013-07-16 11:11:32 -0700283 // Hook up the page indicator
284 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700285 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700286 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700287 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700288 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700289
Winson Chung7819a562013-09-19 15:55:45 -0700290 ArrayList<PageIndicator.PageMarkerResources> markers =
291 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700292 for (int i = 0; i < getChildCount(); ++i) {
293 markers.add(getPageIndicatorMarker(i));
294 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700295
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700296 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700297
298 OnClickListener listener = getPageIndicatorClickListener();
299 if (listener != null) {
300 mPageIndicator.setOnClickListener(listener);
301 }
Adam Cohen53805212013-10-01 10:39:23 -0700302 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700303 }
304 }
305
Adam Cohen53805212013-10-01 10:39:23 -0700306 protected String getPageIndicatorDescription() {
307 return getCurrentPageDescription();
308 }
309
310 protected OnClickListener getPageIndicatorClickListener() {
311 return null;
312 }
313
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700314 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700315 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700316 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700317 // Unhook the page indicator
318 mPageIndicator = null;
319 }
320
Adam Cohen7d30a372013-07-01 17:03:59 -0700321 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700322 private float[] mapPointFromViewToParent(View v, float x, float y) {
323 sTmpPoint[0] = x;
324 sTmpPoint[1] = y;
325 v.getMatrix().mapPoints(sTmpPoint);
326 sTmpPoint[0] += v.getLeft();
327 sTmpPoint[1] += v.getTop();
328 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700329 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700330 private float[] mapPointFromParentToView(View v, float x, float y) {
331 sTmpPoint[0] = x - v.getLeft();
332 sTmpPoint[1] = y - v.getTop();
333 v.getMatrix().invert(sTmpInvMatrix);
334 sTmpInvMatrix.mapPoints(sTmpPoint);
335 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700336 }
337
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700338 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700339 if (mDragView != null) {
340 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
341 (mDragViewBaselineLeft - mDragView.getLeft());
342 float y = mLastMotionY - mDownMotionY;
343 mDragView.setTranslationX(x);
344 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700345
Adam Cohenf358a4b2013-07-23 16:47:31 -0700346 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
347 + x + ", " + y);
348 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700349 }
350
351 public void setMinScale(float f) {
352 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700353 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700354 requestLayout();
355 }
356
357 @Override
358 public void setScaleX(float scaleX) {
359 super.setScaleX(scaleX);
360 if (isReordering(true)) {
361 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
362 mLastMotionX = p[0];
363 mLastMotionY = p[1];
364 updateDragViewTranslationDuringDrag();
365 }
366 }
367
368 // Convenience methods to get the actual width/height of the PagedView (since it is measured
369 // to be larger to account for the minimum possible scale)
370 int getViewportWidth() {
371 return mViewport.width();
372 }
373 int getViewportHeight() {
374 return mViewport.height();
375 }
376
377 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
378 // PagedView both horizontally and vertically
379 int getViewportOffsetX() {
380 return (getMeasuredWidth() - getViewportWidth()) / 2;
381 }
382
383 int getViewportOffsetY() {
384 return (getMeasuredHeight() - getViewportHeight()) / 2;
385 }
386
Adam Cohenedb40762013-07-18 16:45:45 -0700387 PageIndicator getPageIndicator() {
388 return mPageIndicator;
389 }
Winson Chung7819a562013-09-19 15:55:45 -0700390 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
391 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700392 }
Adam Cohenedb40762013-07-18 16:45:45 -0700393
Adam Cohen674531f2013-12-13 15:07:14 -0800394 /**
395 * Add a page change listener which will be called when a page is _finished_ listening.
396 *
397 */
Winson Chung86f77532010-08-24 11:08:22 -0700398 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
399 mPageSwitchListener = pageSwitchListener;
400 if (mPageSwitchListener != null) {
401 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 }
403 }
404
405 /**
Winson Chung52aee602013-01-30 12:01:02 -0800406 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
407 */
408 public boolean isLayoutRtl() {
409 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
410 }
411
412 /**
Winson Chung86f77532010-08-24 11:08:22 -0700413 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700414 */
Winson Chung86f77532010-08-24 11:08:22 -0700415 int getCurrentPage() {
416 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700418
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700419 /**
420 * Returns the index of page to be shown immediately afterwards.
421 */
Winson Chung360e63f2012-04-27 13:48:05 -0700422 int getNextPage() {
423 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
424 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700425
Winson Chung86f77532010-08-24 11:08:22 -0700426 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 return getChildCount();
428 }
429
Winson Chung86f77532010-08-24 11:08:22 -0700430 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 return getChildAt(index);
432 }
433
Adam Cohenae4f1552011-10-20 00:15:42 -0700434 protected int indexToPage(int index) {
435 return index;
436 }
437
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800439 * Updates the scroll of the current page immediately to its final scroll position. We use this
440 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
441 * the previous tab page.
442 */
443 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700444 // If the current page is invalid, just reset the scroll position to zero
445 int newX = 0;
446 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700447 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700448 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800449 scrollTo(newX, 0);
450 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700451 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800452 }
453
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700454 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700455 mScroller.abortAnimation();
456 // We need to clean up the next page here to avoid computeScrollHelper from
457 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700458 if (resetNextPage) {
459 mNextPage = INVALID_PAGE;
460 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700461 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700462
463 private void forceFinishScroller() {
464 mScroller.forceFinished(true);
465 // We need to clean up the next page here to avoid computeScrollHelper from
466 // updating current page on the pass.
467 mNextPage = INVALID_PAGE;
468 }
469
Adam Cohen6f127a62014-06-12 14:54:41 -0700470 private int validateNewPage(int newPage) {
471 int validatedPage = newPage;
472 // When in free scroll mode, we need to clamp to the free scroll page range.
473 if (mFreeScroll) {
474 getFreeScrollPageRange(mTempVisiblePagesRange);
475 validatedPage = Math.max(mTempVisiblePagesRange[0],
476 Math.min(newPage, mTempVisiblePagesRange[1]));
477 }
478 // Ensure that it is clamped by the actual set of children in all cases
479 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
480 return validatedPage;
481 }
482
Chet Haasebc2f0822012-10-26 17:59:53 -0700483 /**
Winson Chung86f77532010-08-24 11:08:22 -0700484 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700486 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800487 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700488 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800489 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800490 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
491 // the default
492 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800493 return;
494 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700495 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700496 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700497 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700498 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800499 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 }
501
Winson Chung8c87cd82013-07-23 16:20:10 -0700502 /**
503 * The restore page will be set in place of the current page at the next (likely first)
504 * layout.
505 */
506 void setRestorePage(int restorePage) {
507 mRestorePage = restorePage;
508 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800509 int getRestorePage() {
510 return mRestorePage;
511 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700512
Adam Cohen674531f2013-12-13 15:07:14 -0800513 /**
514 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
515 * has settled.
516 */
Michael Jurka0142d492010-08-25 17:46:15 -0700517 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700518 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800519 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700520 }
Winson Chungd2be3812013-07-16 11:11:32 -0700521
Adam Cohen674531f2013-12-13 15:07:14 -0800522 updatePageIndicator();
523 }
524
525 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700526 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700527 if (mPageIndicator != null) {
528 mPageIndicator.setContentDescription(getPageIndicatorDescription());
529 if (!isReordering(false)) {
530 mPageIndicator.setActiveMarker(getNextPage());
531 }
Winson Chungd2be3812013-07-16 11:11:32 -0700532 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800534 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700535 if (!mIsPageMoving) {
536 mIsPageMoving = true;
537 onPageBeginMoving();
538 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700539 }
540
Michael Jurkace7e05f2011-02-01 22:02:35 -0800541 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700542 if (mIsPageMoving) {
543 mIsPageMoving = false;
544 onPageEndMoving();
545 }
Michael Jurka0142d492010-08-25 17:46:15 -0700546 }
547
Adam Cohen26976d92011-03-22 15:33:33 -0700548 protected boolean isPageMoving() {
549 return mIsPageMoving;
550 }
551
Michael Jurka0142d492010-08-25 17:46:15 -0700552 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700553 protected void onPageBeginMoving() {
554 }
555
556 // a method that subclasses can override to add behavior
557 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700558 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700559 }
560
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 /**
Winson Chung86f77532010-08-24 11:08:22 -0700562 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 *
564 * @param l The listener used to respond to long clicks.
565 */
566 @Override
567 public void setOnLongClickListener(OnLongClickListener l) {
568 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700569 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700571 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
Adam Cohen1697b792013-09-17 19:08:21 -0700573 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 }
575
576 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800577 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800579 }
580
581 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700582 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700583 // In free scroll mode, we clamp the scrollX
584 if (mFreeScroll) {
585 x = Math.min(x, mFreeScrollMaxScrollX);
586 x = Math.max(x, mFreeScrollMinScrollX);
587 }
588
Adam Cohen0ffac432013-07-10 11:19:26 -0700589 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800590 mUnboundedScrollX = x;
591
Adam Cohen0ffac432013-07-10 11:19:26 -0700592 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
593 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
594 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800595 super.scrollTo(0, y);
596 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700597 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700598 if (isRtl) {
599 overScroll(x - mMaxScrollX);
600 } else {
601 overScroll(x);
602 }
Adam Cohen68d73932010-11-15 10:50:58 -0800603 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700604 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800605 super.scrollTo(mMaxScrollX, y);
606 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700607 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700608 if (isRtl) {
609 overScroll(x);
610 } else {
611 overScroll(x - mMaxScrollX);
612 }
Adam Cohen68d73932010-11-15 10:50:58 -0800613 }
614 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700615 if (mWasInOverscroll) {
616 overScroll(0);
617 mWasInOverscroll = false;
618 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800619 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800620 super.scrollTo(x, y);
621 }
622
Michael Jurka0142d492010-08-25 17:46:15 -0700623 mTouchX = x;
624 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700625
626 // Update the last motion events when scrolling
627 if (isReordering(true)) {
628 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
629 mLastMotionX = p[0];
630 mLastMotionY = p[1];
631 updateDragViewTranslationDuringDrag();
632 }
Michael Jurka0142d492010-08-25 17:46:15 -0700633 }
634
Adam Cohen53805212013-10-01 10:39:23 -0700635 private void sendScrollAccessibilityEvent() {
636 AccessibilityManager am =
637 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
638 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700639 if (mCurrentPage != getNextPage()) {
640 AccessibilityEvent ev =
641 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
642 ev.setItemCount(getChildCount());
643 ev.setFromIndex(getNextPage());
644 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700645
Vadim Tryshevf4715972015-05-08 17:21:03 -0700646 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700647 }
Adam Cohen53805212013-10-01 10:39:23 -0700648 }
649 }
650
Michael Jurka0142d492010-08-25 17:46:15 -0700651 // we moved this functionality to a helper function so SmoothPagedView can reuse it
652 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700654 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700655 if (getScrollX() != mScroller.getCurrX()
656 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700657 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700658 float scaleX = mFreeScroll ? getScaleX() : 1f;
659 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
660 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700661 }
Michael Jurka0142d492010-08-25 17:46:15 -0700662 invalidate();
663 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700664 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700665 sendScrollAccessibilityEvent();
666
Adam Cohen6f127a62014-06-12 14:54:41 -0700667 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700668 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700669 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700670
Adam Cohen73aa9752010-11-24 16:26:58 -0800671 // We don't want to trigger a page end moving unless the page has settled
672 // and the user has stopped scrolling
673 if (mTouchState == TOUCH_STATE_REST) {
674 pageEndMoving();
675 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700676
Adam Cohen7d30a372013-07-01 17:03:59 -0700677 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700678 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700679 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700680 if (am.isEnabled()) {
681 // Notify the user when the page changes
682 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700683 }
Michael Jurka0142d492010-08-25 17:46:15 -0700684 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
Michael Jurka0142d492010-08-25 17:46:15 -0700686 return false;
687 }
688
689 @Override
690 public void computeScroll() {
691 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700692 }
693
Adam Cohen96d30a12013-07-16 18:13:21 -0700694 public static class LayoutParams extends ViewGroup.LayoutParams {
695 public boolean isFullScreenPage = false;
696
697 /**
698 * {@inheritDoc}
699 */
700 public LayoutParams(int width, int height) {
701 super(width, height);
702 }
703
704 public LayoutParams(ViewGroup.LayoutParams source) {
705 super(source);
706 }
707 }
708
709 protected LayoutParams generateDefaultLayoutParams() {
710 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
711 }
712
Adam Cohenedb40762013-07-18 16:45:45 -0700713 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700714 LayoutParams lp = generateDefaultLayoutParams();
715 lp.isFullScreenPage = true;
716 super.addView(page, 0, lp);
717 }
718
Adam Cohen410f3cd2013-09-22 12:09:32 -0700719 public int getNormalChildHeight() {
720 return mNormalChildHeight;
721 }
722
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 @Override
724 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700725 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700726 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
727 return;
728 }
729
Adam Cohen7d30a372013-07-01 17:03:59 -0700730 // We measure the dimensions of the PagedView to be larger than the pages so that when we
731 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700732 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
733 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
734 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700735 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800736 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700737 // viewport, we can be at most one and a half screens offset once we scale down
738 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700739 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
740 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700741
Winson Chungc82d2622013-11-06 13:23:29 -0800742 int parentWidthSize = (int) (2f * maxSize);
743 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700744 int scaledWidthSize, scaledHeightSize;
745 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700746 scaledWidthSize = (int) (parentWidthSize / mMinScale);
747 scaledHeightSize = (int) (parentHeightSize / mMinScale);
748 } else {
749 scaledWidthSize = widthSize;
750 scaledHeightSize = heightSize;
751 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700752 mViewport.set(0, 0, widthSize, heightSize);
753
754 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
755 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
756 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 }
758
Winson Chung8aad6102012-05-11 16:27:49 -0700759 // Return early if we aren't given a proper dimension
760 if (widthSize <= 0 || heightSize <= 0) {
761 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
762 return;
763 }
764
Adam Lesinski6b879f02010-11-04 16:15:23 -0700765 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
766 * of the All apps view on XLarge displays to not take up more space then it needs. Width
767 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
768 * each page to have the same width.
769 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 final int verticalPadding = getPaddingTop() + getPaddingBottom();
771 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700772
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700773 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700775 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700776 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700777 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
778 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
779 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
780 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 final int childCount = getChildCount();
782 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700783 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700784 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100785 if (child.getVisibility() != GONE) {
786 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700787
Vladimir Marko2824b072013-10-04 16:42:17 +0100788 int childWidthMode;
789 int childHeightMode;
790 int childWidth;
791 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700792
Vladimir Marko2824b072013-10-04 16:42:17 +0100793 if (!lp.isFullScreenPage) {
794 if (lp.width == LayoutParams.WRAP_CONTENT) {
795 childWidthMode = MeasureSpec.AT_MOST;
796 } else {
797 childWidthMode = MeasureSpec.EXACTLY;
798 }
799
800 if (lp.height == LayoutParams.WRAP_CONTENT) {
801 childHeightMode = MeasureSpec.AT_MOST;
802 } else {
803 childHeightMode = MeasureSpec.EXACTLY;
804 }
805
Adam Cohen3b185e22013-10-29 14:45:58 -0700806 childWidth = getViewportWidth() - horizontalPadding
807 - mInsets.left - mInsets.right;
808 childHeight = getViewportHeight() - verticalPadding
809 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100810 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700811 } else {
812 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700813 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100814
Adam Cohen3b185e22013-10-29 14:45:58 -0700815 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
816 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700817 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700818 if (referenceChildWidth == 0) {
819 referenceChildWidth = childWidth;
820 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700821
Vladimir Marko2824b072013-10-04 16:42:17 +0100822 final int childWidthMeasureSpec =
823 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
824 final int childHeightMeasureSpec =
825 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
826 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700827 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700828 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700829 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800830 }
831
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700833 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700834 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700835 return;
836 }
837
Winson Chung785d2eb2011-04-14 16:08:02 -0700838 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700840
Adam Cohen7d30a372013-07-01 17:03:59 -0700841 int offsetX = getViewportOffsetX();
842 int offsetY = getViewportOffsetY();
843
844 // Update the viewport offsets
845 mViewport.offset(offsetX, offsetY);
846
Adam Cohen0ffac432013-07-10 11:19:26 -0700847 final boolean isRtl = isLayoutRtl();
848
849 final int startIndex = isRtl ? childCount - 1 : 0;
850 final int endIndex = isRtl ? -1 : childCount;
851 final int delta = isRtl ? -1 : 1;
852
Adam Cohen7d30a372013-07-01 17:03:59 -0700853 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700854
Adam Cohen3b185e22013-10-29 14:45:58 -0700855 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000856 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700857
858 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700859 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
860 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700861 }
862
Adam Cohen0ffac432013-07-10 11:19:26 -0700863 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700864 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700866 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100867 int childTop;
868 if (lp.isFullScreenPage) {
869 childTop = offsetY;
870 } else {
871 childTop = offsetY + getPaddingTop() + mInsets.top;
872 if (mCenterPagesVertically) {
873 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
874 }
875 }
876
Adam Cohen96d30a12013-07-16 18:13:21 -0700877 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700878 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800879
Winson Chung785d2eb2011-04-14 16:08:02 -0700880 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700881 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800882 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700883
Adam Cohen3b185e22013-10-29 14:45:58 -0700884 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
885 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000886
887 int pageGap = mPageSpacing;
888 int next = i + delta;
889 if (next != endIndex) {
890 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
891 } else {
892 nextLp = null;
893 }
894
895 // Prevent full screen pages from showing in the viewport
896 // when they are not the current page.
897 if (lp.isFullScreenPage) {
898 pageGap = getPaddingLeft();
899 } else if (nextLp != null && nextLp.isFullScreenPage) {
900 pageGap = getPaddingRight();
901 }
902
Sunny Goyal22fc63e2015-05-13 16:01:07 -0700903 childLeft += childWidth + pageGap
904 + (lp.isFullScreenPage ? 0 : (getPaddingLeft() + getPaddingRight()));
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 }
906 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700907
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700908 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -0800909 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700910 mFirstLayout = false;
911 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700912
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700913 final LayoutTransition transition = getLayoutTransition();
914 // If the transition is running defer updating max scroll, as some empty pages could
915 // still be present, and a max scroll change could cause sudden jumps in scroll.
916 if (transition != null && transition.isRunning()) {
917 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
918
919 @Override
920 public void startTransition(LayoutTransition transition, ViewGroup container,
921 View view, int transitionType) { }
922
923 @Override
924 public void endTransition(LayoutTransition transition, ViewGroup container,
925 View view, int transitionType) {
926 // Wait until all transitions are complete.
927 if (!transition.isRunning()) {
928 transition.removeTransitionListener(this);
929 updateMaxScrollX();
930 }
931 }
932 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700933 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700934 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700935 }
936
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700937 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700938 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700939 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700940 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700941 } else {
942 setCurrentPage(getNextPage());
943 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700944 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700945 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700946
947 if (isReordering(true)) {
948 updateDragViewTranslationDuringDrag();
949 }
Winson Chunge3193b92010-09-10 11:44:42 -0700950 }
951
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700952 private void updateMaxScrollX() {
953 int childCount = getChildCount();
954 if (childCount > 0) {
955 final int index = isLayoutRtl() ? 0 : childCount - 1;
956 mMaxScrollX = getScrollForPage(index);
957 } else {
958 mMaxScrollX = 0;
959 }
960 }
961
Adam Cohen3b185e22013-10-29 14:45:58 -0700962 public void setPageSpacing(int pageSpacing) {
963 mPageSpacing = pageSpacing;
964 requestLayout();
965 }
966
Adam Cohenf34bab52010-09-30 14:11:56 -0700967 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700968 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
969
970 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700971 for (int i = 0; i < getChildCount(); i++) {
972 View child = getChildAt(i);
973 if (child != null) {
974 float scrollProgress = getScrollProgress(screenCenter, child, i);
975 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800976 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700977 }
978 }
979 invalidate();
980 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700981 }
982
Winson Chunge3193b92010-09-10 11:44:42 -0700983 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700984 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700985 // Update the page indicator, we don't update the page indicator as we
986 // add/remove pages
987 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700988 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700989 mPageIndicator.addMarker(pageIndex,
990 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700991 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700992 }
993
Adam Cohen2591f6a2011-10-25 14:36:40 -0700994 // This ensures that when children are added, they get the correct transforms / alphas
995 // in accordance with any scroll effects.
996 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700997 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700998 invalidate();
999 }
1000
Michael Jurka8b805b12012-04-18 14:23:14 -07001001 @Override
1002 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001003 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001004 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001005 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001006 }
1007
Winson Chungd2be3812013-07-16 11:11:32 -07001008 private void removeMarkerForView(int index) {
1009 // Update the page indicator, we don't update the page indicator as we
1010 // add/remove pages
1011 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001012 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001013 }
1014 }
1015
1016 @Override
1017 public void removeView(View v) {
1018 // XXX: We should find a better way to hook into this before the view
1019 // gets removed form its parent...
1020 removeMarkerForView(indexOfChild(v));
1021 super.removeView(v);
1022 }
1023 @Override
1024 public void removeViewInLayout(View v) {
1025 // XXX: We should find a better way to hook into this before the view
1026 // gets removed form its parent...
1027 removeMarkerForView(indexOfChild(v));
1028 super.removeViewInLayout(v);
1029 }
1030 @Override
1031 public void removeViewAt(int index) {
1032 // XXX: We should find a better way to hook into this before the view
1033 // gets removed form its parent...
1034 removeViewAt(index);
1035 super.removeViewAt(index);
1036 }
1037 @Override
1038 public void removeAllViewsInLayout() {
1039 // Update the page indicator, we don't update the page indicator as we
1040 // add/remove pages
1041 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001042 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001043 }
1044
1045 super.removeAllViewsInLayout();
1046 }
1047
Adam Cohen73894962011-10-31 13:17:17 -07001048 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001049 if (index < 0 || index > getChildCount() - 1) return 0;
1050
Adam Cohenf698c6e2013-07-17 18:44:25 -07001051 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001052
Adam Cohenf698c6e2013-07-17 18:44:25 -07001053 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001054 }
1055
Adam Cohen6f127a62014-06-12 14:54:41 -07001056 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001057 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001058 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001059 }
1060
Michael Jurkadde558b2011-11-09 22:09:06 -08001061 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001062 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001063 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001064
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001065 range[0] = -1;
1066 range[1] = -1;
1067
Michael Jurkac4fb9172010-09-02 17:19:20 -07001068 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001069 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001070 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001071
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001072 int count = getChildCount();
1073 for (int i = 0; i < count; i++) {
1074 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001075
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001076 sTmpIntPoint[0] = 0;
1077 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1078 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001079 if (range[0] == -1) {
1080 continue;
1081 } else {
1082 break;
1083 }
1084 }
1085
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001086 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1087 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1088 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001089 if (range[0] == -1) {
1090 continue;
1091 } else {
1092 break;
1093 }
1094 }
1095 curScreen = i;
1096 if (range[0] < 0) {
1097 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001098 }
1099 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001100
1101 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001102 } else {
1103 range[0] = -1;
1104 range[1] = -1;
1105 }
1106 }
1107
Michael Jurka920d7f42012-05-14 16:29:55 -07001108 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001109 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001110 }
1111
Michael Jurkadde558b2011-11-09 22:09:06 -08001112 @Override
1113 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001114 // Find out which screens are visible; as an optimization we only call draw on them
1115 final int pageCount = getChildCount();
1116 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001117 int halfScreenSize = getViewportWidth() / 2;
1118 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1119 // Otherwise it is equal to the scaled overscroll position.
1120 int screenCenter = mOverScrollX + halfScreenSize;
1121
1122 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1123 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1124 // set it for the next frame
1125 mForceScreenScrolled = false;
1126 screenScrolled(screenCenter);
1127 mLastScreenCenter = screenCenter;
1128 }
1129
Michael Jurkadde558b2011-11-09 22:09:06 -08001130 getVisiblePages(mTempVisiblePagesRange);
1131 final int leftScreen = mTempVisiblePagesRange[0];
1132 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001133 if (leftScreen != -1 && rightScreen != -1) {
1134 final long drawingTime = getDrawingTime();
1135 // Clip to the bounds
1136 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001137 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1138 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001139
Adam Cohen7d30a372013-07-01 17:03:59 -07001140 // Draw all the children, leaving the drag view for last
1141 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001142 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001143 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001144 if (mForceDrawAllChildrenNextFrame ||
1145 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001146 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001147 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001148 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001149 // Draw the drag view on top (if there is one)
1150 if (mDragView != null) {
1151 drawChild(canvas, mDragView, drawingTime);
1152 }
1153
Michael Jurka5e368ff2012-05-14 23:13:15 -07001154 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001155 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001156 }
Michael Jurka0142d492010-08-25 17:46:15 -07001157 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 }
1159
1160 @Override
1161 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001162 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001163 if (page != mCurrentPage || !mScroller.isFinished()) {
1164 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 return true;
1166 }
1167 return false;
1168 }
1169
1170 @Override
1171 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001172 int focusablePage;
1173 if (mNextPage != INVALID_PAGE) {
1174 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001176 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 }
Winson Chung86f77532010-08-24 11:08:22 -07001178 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001180 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 }
1182 return false;
1183 }
1184
1185 @Override
1186 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001187 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001189 if (getCurrentPage() > 0) {
1190 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 return true;
1192 }
1193 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001194 if (getCurrentPage() < getPageCount() - 1) {
1195 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 return true;
1197 }
1198 }
1199 return super.dispatchUnhandledMove(focused, direction);
1200 }
1201
1202 @Override
1203 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001204 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001205 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001206 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 }
1208 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001209 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001210 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 }
1212 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001213 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001214 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216 }
1217 }
1218
1219 /**
1220 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001221 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 *
Winson Chung86f77532010-08-24 11:08:22 -07001223 * This happens when live folders requery, and if they're off page, they
1224 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 */
1226 @Override
1227 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001228 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 View v = focused;
1230 while (true) {
1231 if (v == current) {
1232 super.focusableViewAvailable(focused);
1233 return;
1234 }
1235 if (v == this) {
1236 return;
1237 }
1238 ViewParent parent = v.getParent();
1239 if (parent instanceof View) {
1240 v = (View)v.getParent();
1241 } else {
1242 return;
1243 }
1244 }
1245 }
1246
1247 /**
1248 * {@inheritDoc}
1249 */
1250 @Override
1251 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1252 if (disallowIntercept) {
1253 // We need to make sure to cancel our long press if
1254 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001255 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001256 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 }
1258 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1259 }
1260
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001261 /**
1262 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1263 */
1264 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001265 if (isLayoutRtl()) {
1266 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001267 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001268 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001269 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001270 }
1271
1272 /**
1273 * Return true if a tap at (x, y) should trigger a flip to the next page.
1274 */
1275 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001276 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001277 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001278 }
1279 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001280 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001281 }
Winson Chung52aee602013-01-30 12:01:02 -08001282
Adam Cohen7d30a372013-07-01 17:03:59 -07001283 /** Returns whether x and y originated within the buffered viewport */
1284 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001285 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001286 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001287 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001288 }
1289
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 @Override
1291 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001292 if (DISABLE_TOUCH_INTERACTION) {
1293 return false;
1294 }
1295
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 /*
1297 * This method JUST determines whether we want to intercept the motion.
1298 * If we return true, onTouchEvent will be called and we do the actual
1299 * scrolling there.
1300 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001301 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001302
Winson Chung45e1d6e2010-11-09 17:19:49 -08001303 // Skip touch handling if there are no pages to swipe
1304 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1305
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 /*
1307 * Shortcut the most recurring case: the user is in the dragging
1308 * state and he is moving his finger. We want to intercept this
1309 * motion.
1310 */
1311 final int action = ev.getAction();
1312 if ((action == MotionEvent.ACTION_MOVE) &&
1313 (mTouchState == TOUCH_STATE_SCROLLING)) {
1314 return true;
1315 }
1316
Winson Chung321e9ee2010-08-09 13:37:56 -07001317 switch (action & MotionEvent.ACTION_MASK) {
1318 case MotionEvent.ACTION_MOVE: {
1319 /*
1320 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1321 * whether the user has moved far enough from his original down touch.
1322 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001323 if (mActivePointerId != INVALID_POINTER) {
1324 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001325 }
1326 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1327 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1328 // i.e. fall through to the next case (don't break)
1329 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1330 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001331 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 }
1333
1334 case MotionEvent.ACTION_DOWN: {
1335 final float x = ev.getX();
1336 final float y = ev.getY();
1337 // Remember location of down touch
1338 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001339 mDownMotionY = y;
1340 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 mLastMotionX = x;
1342 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001343 float[] p = mapPointFromViewToParent(this, x, y);
1344 mParentDownMotionX = p[0];
1345 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001346 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001347 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001349
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 /*
1351 * If being flinged and user touches the screen, initiate drag;
1352 * otherwise don't. mScroller.isFinished should be false when
1353 * being flinged.
1354 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001355 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001356 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001357
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001358 if (finishedScrolling) {
1359 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001360 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001361 setCurrentPage(getNextPage());
1362 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001363 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001364 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001365 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1366 mTouchState = TOUCH_STATE_SCROLLING;
1367 } else {
1368 mTouchState = TOUCH_STATE_REST;
1369 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001370 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001371
Winson Chung86f77532010-08-24 11:08:22 -07001372 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001374 if (!DISABLE_TOUCH_SIDE_PAGES) {
1375 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1376 if (getChildCount() > 0) {
1377 if (hitsPreviousPage(x, y)) {
1378 mTouchState = TOUCH_STATE_PREV_PAGE;
1379 } else if (hitsNextPage(x, y)) {
1380 mTouchState = TOUCH_STATE_NEXT_PAGE;
1381 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 }
1383 }
1384 }
1385 break;
1386 }
1387
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001389 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 break;
1392
1393 case MotionEvent.ACTION_POINTER_UP:
1394 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001395 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397 }
1398
1399 /*
1400 * The only time we want to intercept motion events is if we are in the
1401 * drag mode.
1402 */
1403 return mTouchState != TOUCH_STATE_REST;
1404 }
1405
Adam Cohenf8d28232011-02-01 21:47:00 -08001406 protected void determineScrollingStart(MotionEvent ev) {
1407 determineScrollingStart(ev, 1.0f);
1408 }
1409
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 /*
1411 * Determines if we should change the touch state to start scrolling after the
1412 * user moves their touch point too far.
1413 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001414 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001415 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001417 if (pointerIndex == -1) return;
1418
1419 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 final float x = ev.getX(pointerIndex);
1421 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1423
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 final int xDiff = (int) Math.abs(x - mLastMotionX);
1425 final int yDiff = (int) Math.abs(y - mLastMotionY);
1426
Adam Cohenf8d28232011-02-01 21:47:00 -08001427 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 boolean xPaged = xDiff > mPagingTouchSlop;
1429 boolean xMoved = xDiff > touchSlop;
1430 boolean yMoved = yDiff > touchSlop;
1431
Adam Cohenf8d28232011-02-01 21:47:00 -08001432 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001433 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 // Scroll if the user moved far enough along the X axis
1435 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001436 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001438 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001440 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001441 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001442 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001444 }
1445 }
1446
1447 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001448 // Try canceling the long press. It could also have been scheduled
1449 // by a distant descendant, so use the mAllowLongPress flag to block
1450 // everything
1451 final View currentPage = getPageAt(mCurrentPage);
1452 if (currentPage != null) {
1453 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 }
1455 }
1456
Adam Cohenb5ba0972011-09-07 18:02:31 -07001457 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001459
Adam Cohenedb40762013-07-18 16:45:45 -07001460 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001461 int count = getChildCount();
1462
1463 final int totalDistance;
1464
1465 int adjacentPage = page + 1;
1466 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1467 adjacentPage = page - 1;
1468 }
1469
1470 if (adjacentPage < 0 || adjacentPage > count - 1) {
1471 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1472 } else {
1473 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1474 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001475
1476 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001477 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1478 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001479 return scrollProgress;
1480 }
1481
Adam Cohenedb40762013-07-18 16:45:45 -07001482 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001483 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001484 return 0;
1485 } else {
1486 return mPageScrolls[index];
1487 }
1488 }
1489
Adam Cohen564a2e72013-10-09 14:47:32 -07001490 // While layout transitions are occurring, a child's position may stray from its baseline
1491 // position. This method returns the magnitude of this stray at any given time.
1492 public int getLayoutTransitionOffsetForPage(int index) {
1493 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1494 return 0;
1495 } else {
1496 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001497
1498 int scrollOffset = 0;
1499 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1500 if (!lp.isFullScreenPage) {
1501 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1502 }
1503
Adam Cohen564a2e72013-10-09 14:47:32 -07001504 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1505 return (int) (child.getX() - baselineX);
1506 }
1507 }
1508
Adam Cohene0f66b52010-11-23 15:06:07 -08001509 // This curve determines how the effect of scrolling over the limits of the page dimishes
1510 // as the user pulls further and further from the bounds
1511 private float overScrollInfluenceCurve(float f) {
1512 f -= 1.0f;
1513 return f * f * f + 1.0f;
1514 }
1515
Adam Cohen1e4359c2014-08-18 13:12:16 -07001516 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001517 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001518
1519 // We want to reach the max over scroll effect when the user has
1520 // over scrolled half the size of the screen
1521 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1522
Adam Cohen1e4359c2014-08-18 13:12:16 -07001523 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001524
1525 // Clamp this factor, f, to -1 < f < 1
1526 if (Math.abs(f) >= 1) {
1527 f /= Math.abs(f);
1528 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001529 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001530 }
1531
1532 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001533 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001534
1535 float f = (amount / screenSize);
1536
1537 if (f == 0) return;
1538 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1539
Adam Cohen7bfc9792011-01-28 13:52:37 -08001540 // Clamp this factor, f, to -1 < f < 1
1541 if (Math.abs(f) >= 1) {
1542 f /= Math.abs(f);
1543 }
1544
Adam Cohene0f66b52010-11-23 15:06:07 -08001545 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001546 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001547 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001548 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001549 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001550 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001551 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001552 }
1553 invalidate();
1554 }
1555
Adam Cohenb5ba0972011-09-07 18:02:31 -07001556 protected void overScroll(float amount) {
1557 dampedOverScroll(amount);
1558 }
1559
Michael Jurkac5b262c2011-01-12 20:24:50 -08001560 protected float maxOverScroll() {
1561 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001562 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001563 float f = 1.0f;
1564 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1565 return OVERSCROLL_DAMP_FACTOR * f;
1566 }
1567
Winson Chungdc61c4d2015-04-20 18:26:57 -07001568 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001569 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001570 }
1571
Winson Chungdc61c4d2015-04-20 18:26:57 -07001572 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001573 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001574 }
1575
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001576 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001577 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001578 if (isLayoutRtl()) {
1579 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1580 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1581 } else {
1582 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1583 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1584 }
1585 }
1586
Adam Cohenf9618852013-11-08 06:45:03 -08001587 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001588 mFreeScroll = freeScroll;
1589
Adam Cohenf9618852013-11-08 06:45:03 -08001590 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001591 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001592 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001593 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1594 setCurrentPage(mTempVisiblePagesRange[0]);
1595 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1596 setCurrentPage(mTempVisiblePagesRange[1]);
1597 }
1598 }
1599
1600 setEnableOverscroll(!freeScroll);
1601 }
1602
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001603 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001604 mAllowOverScroll = enable;
1605 }
1606
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001607 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001608 if (mDragView != null) {
1609 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1610 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001611 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001612 int minDistance = Integer.MAX_VALUE;
1613 int minIndex = indexOfChild(mDragView);
1614 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1615 View page = getPageAt(i);
1616 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1617 int d = Math.abs(dragX - pageX);
1618 if (d < minDistance) {
1619 minIndex = i;
1620 minDistance = d;
1621 }
1622 }
1623 return minIndex;
1624 }
1625 return -1;
1626 }
1627
Winson Chung321e9ee2010-08-09 13:37:56 -07001628 @Override
1629 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001630 if (DISABLE_TOUCH_INTERACTION) {
1631 return false;
1632 }
1633
Adam Cohen1697b792013-09-17 19:08:21 -07001634 super.onTouchEvent(ev);
1635
Winson Chung45e1d6e2010-11-09 17:19:49 -08001636 // Skip touch handling if there are no pages to swipe
1637 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1638
Michael Jurkab8f06722010-10-10 15:58:46 -07001639 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001640
1641 final int action = ev.getAction();
1642
1643 switch (action & MotionEvent.ACTION_MASK) {
1644 case MotionEvent.ACTION_DOWN:
1645 /*
1646 * If being flinged and user touches, stop the fling. isFinished
1647 * will be false if being flinged.
1648 */
1649 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001650 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001651 }
1652
1653 // Remember where the motion event started
1654 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001655 mDownMotionY = mLastMotionY = ev.getY();
1656 mDownScrollX = getScrollX();
1657 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1658 mParentDownMotionX = p[0];
1659 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001660 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001661 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001662 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001663
Michael Jurka0142d492010-08-25 17:46:15 -07001664 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001665 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001666 pageBeginMoving();
1667 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001668 break;
1669
1670 case MotionEvent.ACTION_MOVE:
1671 if (mTouchState == TOUCH_STATE_SCROLLING) {
1672 // Scroll to follow the motion event
1673 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001674
1675 if (pointerIndex == -1) return true;
1676
Winson Chung321e9ee2010-08-09 13:37:56 -07001677 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001678 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001679
Adam Cohenaefd4e12011-02-14 16:39:38 -08001680 mTotalMotionX += Math.abs(deltaX);
1681
Winson Chungc0844aa2011-02-02 15:25:58 -08001682 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1683 // keep the remainder because we are actually testing if we've moved from the last
1684 // scrolled position (which is discrete).
1685 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001686 mTouchX += deltaX;
1687 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1688 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001689 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001690 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001691 } else {
1692 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001693 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001694 mLastMotionX = x;
1695 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001696 } else {
1697 awakenScrollBars();
1698 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1700 // Update the last motion position
1701 mLastMotionX = ev.getX();
1702 mLastMotionY = ev.getY();
1703
1704 // Update the parent down so that our zoom animations take this new movement into
1705 // account
1706 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1707 mParentDownMotionX = pt[0];
1708 mParentDownMotionY = pt[1];
1709 updateDragViewTranslationDuringDrag();
1710
1711 // Find the closest page to the touch point
1712 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001713
Adam Cohen7d30a372013-07-01 17:03:59 -07001714 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1715 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1716 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1717 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1718
Adam Cohenf358a4b2013-07-23 16:47:31 -07001719 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001720 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001721 mTempVisiblePagesRange[0] = 0;
1722 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001723 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001724 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1725 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1726 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1727 mSidePageHoverIndex = pageUnderPointIndex;
1728 mSidePageHoverRunnable = new Runnable() {
1729 @Override
1730 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001731 // Setup the scroll to the correct page before we swap the views
1732 snapToPage(pageUnderPointIndex);
1733
1734 // For each of the pages between the paged view and the drag view,
1735 // animate them from the previous position to the new position in
1736 // the layout (as a result of the drag view moving in the layout)
1737 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1738 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1739 dragViewIndex + 1 : pageUnderPointIndex;
1740 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1741 dragViewIndex - 1 : pageUnderPointIndex;
1742 for (int i = lowerIndex; i <= upperIndex; ++i) {
1743 View v = getChildAt(i);
1744 // dragViewIndex < pageUnderPointIndex, so after we remove the
1745 // drag view all subsequent views to pageUnderPointIndex will
1746 // shift down.
1747 int oldX = getViewportOffsetX() + getChildOffset(i);
1748 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1749
1750 // Animate the view translation from its old position to its new
1751 // position
1752 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1753 if (anim != null) {
1754 anim.cancel();
1755 }
1756
1757 v.setTranslationX(oldX - newX);
1758 anim = new AnimatorSet();
1759 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1760 anim.playTogether(
1761 ObjectAnimator.ofFloat(v, "translationX", 0f));
1762 anim.start();
1763 v.setTag(anim);
1764 }
1765
1766 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001767 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001768 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001769 if (mPageIndicator != null) {
1770 mPageIndicator.setActiveMarker(getNextPage());
1771 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001772 }
1773 };
1774 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1775 }
1776 } else {
1777 removeCallbacks(mSidePageHoverRunnable);
1778 mSidePageHoverIndex = -1;
1779 }
Adam Cohen564976a2010-10-13 18:52:07 -07001780 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 determineScrollingStart(ev);
1782 }
1783 break;
1784
1785 case MotionEvent.ACTION_UP:
1786 if (mTouchState == TOUCH_STATE_SCROLLING) {
1787 final int activePointerId = mActivePointerId;
1788 final int pointerIndex = ev.findPointerIndex(activePointerId);
1789 final float x = ev.getX(pointerIndex);
1790 final VelocityTracker velocityTracker = mVelocityTracker;
1791 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1792 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001793 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001794 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001795 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1796 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001797
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001798 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1799
Adam Cohen00481b32011-11-18 12:03:48 -08001800 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001801 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001802
Adam Cohenf358a4b2013-07-23 16:47:31 -07001803 if (!mFreeScroll) {
1804 // In the case that the page is moved far to one direction and then is flung
1805 // in the opposite direction, we use a threshold to determine whether we should
1806 // just return to the starting page, or if we should skip one further.
1807 boolean returnToOriginalPage = false;
1808 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1809 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1810 returnToOriginalPage = true;
1811 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001812
Adam Cohenf358a4b2013-07-23 16:47:31 -07001813 int finalPage;
1814 // We give flings precedence over large moves, which is why we short-circuit our
1815 // test for a large move if a fling has been registered. That is, a large
1816 // move to the left and fling to the right will register as a fling to the right.
1817 final boolean isRtl = isLayoutRtl();
1818 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1819 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1820 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1821 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1822 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1823 snapToPageWithVelocity(finalPage, velocityX);
1824 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1825 (isFling && isVelocityXLeft)) &&
1826 mCurrentPage < getChildCount() - 1) {
1827 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1828 snapToPageWithVelocity(finalPage, velocityX);
1829 } else {
1830 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001831 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001832 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001833 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001834 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001835 }
1836
1837 float scaleX = getScaleX();
1838 int vX = (int) (-velocityX * scaleX);
1839 int initialScrollX = (int) (getScrollX() * scaleX);
1840
Adam Cohenf9618852013-11-08 06:45:03 -08001841 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001842 mScroller.fling(initialScrollX,
1843 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1844 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001845 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001846 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001847 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1848 // at this point we have not moved beyond the touch slop
1849 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1850 // we can just page
1851 int nextPage = Math.max(0, mCurrentPage - 1);
1852 if (nextPage != mCurrentPage) {
1853 snapToPage(nextPage);
1854 } else {
1855 snapToDestination();
1856 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001857 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001858 // at this point we have not moved beyond the touch slop
1859 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1860 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001861 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1862 if (nextPage != mCurrentPage) {
1863 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001864 } else {
1865 snapToDestination();
1866 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001867 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1868 // Update the last motion position
1869 mLastMotionX = ev.getX();
1870 mLastMotionY = ev.getY();
1871
1872 // Update the parent down so that our zoom animations take this new movement into
1873 // account
1874 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1875 mParentDownMotionX = pt[0];
1876 mParentDownMotionY = pt[1];
1877 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001878 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001879 if (!mCancelTap) {
1880 onUnhandledTap(ev);
1881 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001882 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001883
1884 // Remove the callback to wait for the side page hover timeout
1885 removeCallbacks(mSidePageHoverRunnable);
1886 // End any intermediate reordering states
1887 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001888 break;
1889
1890 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001891 if (mTouchState == TOUCH_STATE_SCROLLING) {
1892 snapToDestination();
1893 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001894 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001895 break;
1896
1897 case MotionEvent.ACTION_POINTER_UP:
1898 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001899 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001900 break;
1901 }
1902
1903 return true;
1904 }
1905
Adam Cohen7d30a372013-07-01 17:03:59 -07001906 private void resetTouchState() {
1907 releaseVelocityTracker();
1908 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001909 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001910 mTouchState = TOUCH_STATE_REST;
1911 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001912 }
1913
Adam Cohenc2d6e892014-10-16 09:49:24 -07001914 /**
1915 * Triggered by scrolling via touch
1916 */
1917 protected void onScrollInteractionBegin() {
1918 }
1919
1920 protected void onScrollInteractionEnd() {
1921 }
1922
Adam Cohen1697b792013-09-17 19:08:21 -07001923 protected void onUnhandledTap(MotionEvent ev) {
1924 ((Launcher) getContext()).onClick(this);
1925 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001926
Winson Chung185d7162011-02-28 13:47:29 -08001927 @Override
1928 public boolean onGenericMotionEvent(MotionEvent event) {
1929 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1930 switch (event.getAction()) {
1931 case MotionEvent.ACTION_SCROLL: {
1932 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1933 final float vscroll;
1934 final float hscroll;
1935 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1936 vscroll = 0;
1937 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1938 } else {
1939 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1940 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1941 }
1942 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001943 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1944 : (hscroll > 0 || vscroll > 0);
1945 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001946 scrollRight();
1947 } else {
1948 scrollLeft();
1949 }
1950 return true;
1951 }
1952 }
1953 }
1954 }
1955 return super.onGenericMotionEvent(event);
1956 }
1957
Michael Jurkab8f06722010-10-10 15:58:46 -07001958 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1959 if (mVelocityTracker == null) {
1960 mVelocityTracker = VelocityTracker.obtain();
1961 }
1962 mVelocityTracker.addMovement(ev);
1963 }
1964
1965 private void releaseVelocityTracker() {
1966 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001967 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001968 mVelocityTracker.recycle();
1969 mVelocityTracker = null;
1970 }
1971 }
1972
Winson Chung321e9ee2010-08-09 13:37:56 -07001973 private void onSecondaryPointerUp(MotionEvent ev) {
1974 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1975 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1976 final int pointerId = ev.getPointerId(pointerIndex);
1977 if (pointerId == mActivePointerId) {
1978 // This was our active pointer going up. Choose a new
1979 // active pointer and adjust accordingly.
1980 // TODO: Make this decision more intelligent.
1981 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1982 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1983 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001984 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001985 mActivePointerId = ev.getPointerId(newPointerIndex);
1986 if (mVelocityTracker != null) {
1987 mVelocityTracker.clear();
1988 }
1989 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001990 }
1991
Winson Chung321e9ee2010-08-09 13:37:56 -07001992 @Override
1993 public void requestChildFocus(View child, View focused) {
1994 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001995 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001996 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001997 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001998 }
1999 }
2000
Adam Cohend19d3ca2010-09-15 14:43:42 -07002001 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002002 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002004 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002005 final int childCount = getChildCount();
2006 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002007 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002008 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002009 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002010 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002011 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2012 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2013 minDistanceFromScreenCenter = distanceFromScreenCenter;
2014 minDistanceFromScreenCenterIndex = i;
2015 }
2016 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002017 return minDistanceFromScreenCenterIndex;
2018 }
2019
Adam Cohen1e4359c2014-08-18 13:12:16 -07002020 protected boolean isInOverScroll() {
2021 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2022 }
2023
2024 protected int getPageSnapDuration() {
2025 if (isInOverScroll()) {
2026 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2027 }
2028 return PAGE_SNAP_ANIMATION_DURATION;
2029
2030 }
2031
Adam Cohend19d3ca2010-09-15 14:43:42 -07002032 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002033 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002034 }
2035
Adam Cohene0f66b52010-11-23 15:06:07 -08002036 private static class ScrollInterpolator implements Interpolator {
2037 public ScrollInterpolator() {
2038 }
2039
2040 public float getInterpolation(float t) {
2041 t -= 1.0f;
2042 return t*t*t*t*t + 1;
2043 }
2044 }
2045
2046 // We want the duration of the page snap animation to be influenced by the distance that
2047 // the screen has to travel, however, we don't want this duration to be effected in a
2048 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2049 // of travel has on the overall snap duration.
2050 float distanceInfluenceForSnapDuration(float f) {
2051 f -= 0.5f; // center the values about 0.
2052 f *= 0.3f * Math.PI / 2.0f;
2053 return (float) Math.sin(f);
2054 }
2055
Michael Jurka0142d492010-08-25 17:46:15 -07002056 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002057 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002058 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002059
Adam Cohenedb40762013-07-18 16:45:45 -07002060 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002061 int delta = newX - mUnboundedScrollX;
2062 int duration = 0;
2063
Adam Cohen1e4359c2014-08-18 13:12:16 -07002064 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002065 // If the velocity is low enough, then treat this more as an automatic page advance
2066 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002067 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002068 return;
2069 }
2070
2071 // Here we compute a "distance" that will be used in the computation of the overall
2072 // snap duration. This is a function of the actual distance that needs to be traveled;
2073 // we keep this value close to half screen size in order to reduce the variance in snap
2074 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002075 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002076 float distance = halfScreenSize + halfScreenSize *
2077 distanceInfluenceForSnapDuration(distanceRatio);
2078
2079 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002080 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002081
2082 // we want the page's snap velocity to approximately match the velocity at which the
2083 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002084 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2085 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002086
2087 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002088 }
2089
2090 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002091 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002092 }
2093
Adam Cohen7d30a372013-07-01 17:03:59 -07002094 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002095 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002096 }
2097
Michael Jurka0142d492010-08-25 17:46:15 -07002098 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002099 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002100 }
2101
Adam Cohenf9618852013-11-08 06:45:03 -08002102 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2103 snapToPage(whichPage, duration, false, interpolator);
2104 }
2105
2106 protected void snapToPage(int whichPage, int duration, boolean immediate,
2107 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002108 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002109
Adam Cohenedb40762013-07-18 16:45:45 -07002110 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002111 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002112 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002113 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002114 }
2115
2116 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002117 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002118 }
Michael Jurka0142d492010-08-25 17:46:15 -07002119
Adam Cohenf9618852013-11-08 06:45:03 -08002120 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2121 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002122 whichPage = validateNewPage(whichPage);
2123
Adam Cohen7d30a372013-07-01 17:03:59 -07002124 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002125 View focusedChild = getFocusedChild();
2126 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002127 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002128 focusedChild.clearFocus();
2129 }
2130
2131 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002132 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002133 if (immediate) {
2134 duration = 0;
2135 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002136 duration = Math.abs(delta);
2137 }
2138
Adam Cohenf358a4b2013-07-23 16:47:31 -07002139 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002140 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002141 }
Adam Cohenf9618852013-11-08 06:45:03 -08002142
2143 if (interpolator != null) {
2144 mScroller.setInterpolator(interpolator);
2145 } else {
2146 mScroller.setInterpolator(mDefaultInterpolator);
2147 }
2148
Adam Cohen68d73932010-11-15 10:50:58 -08002149 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002150
Adam Cohen674531f2013-12-13 15:07:14 -08002151 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002152
2153 // Trigger a compute() to finish switching pages if necessary
2154 if (immediate) {
2155 computeScroll();
2156 }
2157
2158 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002159 invalidate();
2160 }
2161
Winson Chung321e9ee2010-08-09 13:37:56 -07002162 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002163 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002164 }
2165
2166 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002167 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002168 }
2169
Winson Chung86f77532010-08-24 11:08:22 -07002170 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002171 int result = -1;
2172 if (v != null) {
2173 ViewParent vp = v.getParent();
2174 int count = getChildCount();
2175 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002176 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002177 return i;
2178 }
2179 }
2180 }
2181 return result;
2182 }
2183
Adam Cohendbdff6b2013-09-18 19:09:15 -07002184 @Override
2185 public boolean performLongClick() {
2186 mCancelTap = true;
2187 return super.performLongClick();
2188 }
2189
Winson Chung321e9ee2010-08-09 13:37:56 -07002190 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002191 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002192
2193 SavedState(Parcelable superState) {
2194 super(superState);
2195 }
2196
Adam Cohen091440a2015-03-18 14:16:05 -07002197 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002198 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002199 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002200 }
2201
2202 @Override
2203 public void writeToParcel(Parcel out, int flags) {
2204 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002205 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002206 }
2207
2208 public static final Parcelable.Creator<SavedState> CREATOR =
2209 new Parcelable.Creator<SavedState>() {
2210 public SavedState createFromParcel(Parcel in) {
2211 return new SavedState(in);
2212 }
2213
2214 public SavedState[] newArray(int size) {
2215 return new SavedState[size];
2216 }
2217 };
2218 }
2219
Adam Cohen7d30a372013-07-01 17:03:59 -07002220 // Animate the drag view back to the original position
2221 void animateDragViewToOriginalPosition() {
2222 if (mDragView != null) {
2223 AnimatorSet anim = new AnimatorSet();
2224 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2225 anim.playTogether(
2226 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002227 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2228 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2229 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002230 anim.addListener(new AnimatorListenerAdapter() {
2231 @Override
2232 public void onAnimationEnd(Animator animation) {
2233 onPostReorderingAnimationCompleted();
2234 }
2235 });
2236 anim.start();
2237 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002238 }
2239
Sunny Goyal1d08f702015-05-04 15:50:25 -07002240 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002241 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2242 mTouchState = TOUCH_STATE_REORDERING;
2243 mIsReordering = true;
2244
Adam Cohen7d30a372013-07-01 17:03:59 -07002245 // We must invalidate to trigger a redraw to update the layers such that the drag view
2246 // is always drawn on top
2247 invalidate();
2248 }
2249
Adam Cohen091440a2015-03-18 14:16:05 -07002250 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002251 // Trigger the callback when reordering has settled
2252 --mPostReorderingPreZoomInRemainingAnimationCount;
2253 if (mPostReorderingPreZoomInRunnable != null &&
2254 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2255 mPostReorderingPreZoomInRunnable.run();
2256 mPostReorderingPreZoomInRunnable = null;
2257 }
2258 }
2259
Sunny Goyal1d08f702015-05-04 15:50:25 -07002260 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002261 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002262 }
2263
Adam Cohenf358a4b2013-07-23 16:47:31 -07002264 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002265 int dragViewIndex = indexOfChild(v);
2266
Adam Cohen327acfe2014-06-06 11:52:52 -07002267 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002268
Adam Cohen7d30a372013-07-01 17:03:59 -07002269 mTempVisiblePagesRange[0] = 0;
2270 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002271 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002272 mReorderingStarted = true;
2273
2274 // Check if we are within the reordering range
2275 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002276 dragViewIndex <= mTempVisiblePagesRange[1]) {
2277 // Find the drag view under the pointer
2278 mDragView = getChildAt(dragViewIndex);
2279 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2280 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002281 snapToPage(getPageNearestToCenterOfScreen());
2282 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002283 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002284 return true;
2285 }
2286 return false;
2287 }
2288
2289 boolean isReordering(boolean testTouchState) {
2290 boolean state = mIsReordering;
2291 if (testTouchState) {
2292 state &= (mTouchState == TOUCH_STATE_REORDERING);
2293 }
2294 return state;
2295 }
2296 void endReordering() {
2297 // For simplicity, we call endReordering sometimes even if reordering was never started.
2298 // In that case, we don't want to do anything.
2299 if (!mReorderingStarted) return;
2300 mReorderingStarted = false;
2301
2302 // If we haven't flung-to-delete the current child, then we just animate the drag view
2303 // back into position
2304 final Runnable onCompleteRunnable = new Runnable() {
2305 @Override
2306 public void run() {
2307 onEndReordering();
2308 }
2309 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002310
2311 mPostReorderingPreZoomInRunnable = new Runnable() {
2312 public void run() {
2313 onCompleteRunnable.run();
2314 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002315 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002316 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002317
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002318 mPostReorderingPreZoomInRemainingAnimationCount =
2319 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2320 // Snap to the current page
2321 snapToPage(indexOfChild(mDragView), 0);
2322 // Animate the drag view back to the front position
2323 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002324 }
2325
Adam Cohen7d30a372013-07-01 17:03:59 -07002326 private static final int ANIM_TAG_KEY = 100;
2327
Winson Chung6a0f57d2011-06-29 20:10:49 -07002328 /* Accessibility */
2329 @Override
2330 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2331 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002332 info.setScrollable(getPageCount() > 1);
2333 if (getCurrentPage() < getPageCount() - 1) {
2334 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2335 }
2336 if (getCurrentPage() > 0) {
2337 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2338 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002339 }
2340
2341 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002342 public void sendAccessibilityEvent(int eventType) {
2343 // Don't let the view send real scroll events.
2344 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2345 super.sendAccessibilityEvent(eventType);
2346 }
2347 }
2348
2349 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002350 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2351 super.onInitializeAccessibilityEvent(event);
2352 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002353 }
2354
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002355 @Override
2356 public boolean performAccessibilityAction(int action, Bundle arguments) {
2357 if (super.performAccessibilityAction(action, arguments)) {
2358 return true;
2359 }
2360 switch (action) {
2361 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2362 if (getCurrentPage() < getPageCount() - 1) {
2363 scrollRight();
2364 return true;
2365 }
2366 } break;
2367 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2368 if (getCurrentPage() > 0) {
2369 scrollLeft();
2370 return true;
2371 }
2372 } break;
2373 }
2374 return false;
2375 }
2376
Adam Cohen0ffac432013-07-10 11:19:26 -07002377 protected String getCurrentPageDescription() {
2378 return String.format(getContext().getString(R.string.default_scroll_format),
2379 getNextPage() + 1, getChildCount());
2380 }
2381
Winson Chungd11265e2011-08-30 13:37:23 -07002382 @Override
2383 public boolean onHoverEvent(android.view.MotionEvent event) {
2384 return true;
2385 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002386}