blob: aa8656730b9261ba302e2c9b1b8b249143127e2b [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()) {
639 AccessibilityEvent ev =
640 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700641 ev.setItemCount(getChildCount());
642 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700643 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700644
Alan Viverette254139a2013-10-08 13:13:46 -0700645 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700646 if (getNextPage() >= mCurrentPage) {
647 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
648 } else {
649 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
650 }
651
652 ev.setAction(action);
653 sendAccessibilityEventUnchecked(ev);
654 }
655 }
656
Michael Jurka0142d492010-08-25 17:46:15 -0700657 // we moved this functionality to a helper function so SmoothPagedView can reuse it
658 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700660 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700661 if (getScrollX() != mScroller.getCurrX()
662 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700663 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700664 float scaleX = mFreeScroll ? getScaleX() : 1f;
665 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
666 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 }
Michael Jurka0142d492010-08-25 17:46:15 -0700668 invalidate();
669 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700670 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700671 sendScrollAccessibilityEvent();
672
Adam Cohen6f127a62014-06-12 14:54:41 -0700673 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700674 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700675 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700676
Adam Cohen73aa9752010-11-24 16:26:58 -0800677 // We don't want to trigger a page end moving unless the page has settled
678 // and the user has stopped scrolling
679 if (mTouchState == TOUCH_STATE_REST) {
680 pageEndMoving();
681 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700682
Adam Cohen7d30a372013-07-01 17:03:59 -0700683 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700684 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700685 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700686 if (am.isEnabled()) {
687 // Notify the user when the page changes
688 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700689 }
Michael Jurka0142d492010-08-25 17:46:15 -0700690 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 }
Michael Jurka0142d492010-08-25 17:46:15 -0700692 return false;
693 }
694
695 @Override
696 public void computeScroll() {
697 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700698 }
699
Adam Cohen96d30a12013-07-16 18:13:21 -0700700 public static class LayoutParams extends ViewGroup.LayoutParams {
701 public boolean isFullScreenPage = false;
702
703 /**
704 * {@inheritDoc}
705 */
706 public LayoutParams(int width, int height) {
707 super(width, height);
708 }
709
710 public LayoutParams(ViewGroup.LayoutParams source) {
711 super(source);
712 }
713 }
714
715 protected LayoutParams generateDefaultLayoutParams() {
716 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
717 }
718
Adam Cohenedb40762013-07-18 16:45:45 -0700719 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700720 LayoutParams lp = generateDefaultLayoutParams();
721 lp.isFullScreenPage = true;
722 super.addView(page, 0, lp);
723 }
724
Adam Cohen410f3cd2013-09-22 12:09:32 -0700725 public int getNormalChildHeight() {
726 return mNormalChildHeight;
727 }
728
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 @Override
730 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700731 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700732 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
733 return;
734 }
735
Adam Cohen7d30a372013-07-01 17:03:59 -0700736 // We measure the dimensions of the PagedView to be larger than the pages so that when we
737 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700738 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
739 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
740 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700741 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800742 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700743 // viewport, we can be at most one and a half screens offset once we scale down
744 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700745 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
746 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700747
Winson Chungc82d2622013-11-06 13:23:29 -0800748 int parentWidthSize = (int) (2f * maxSize);
749 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700750 int scaledWidthSize, scaledHeightSize;
751 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700752 scaledWidthSize = (int) (parentWidthSize / mMinScale);
753 scaledHeightSize = (int) (parentHeightSize / mMinScale);
754 } else {
755 scaledWidthSize = widthSize;
756 scaledHeightSize = heightSize;
757 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700758 mViewport.set(0, 0, widthSize, heightSize);
759
760 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
761 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
762 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 }
764
Winson Chung8aad6102012-05-11 16:27:49 -0700765 // Return early if we aren't given a proper dimension
766 if (widthSize <= 0 || heightSize <= 0) {
767 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
768 return;
769 }
770
Adam Lesinski6b879f02010-11-04 16:15:23 -0700771 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
772 * of the All apps view on XLarge displays to not take up more space then it needs. Width
773 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
774 * each page to have the same width.
775 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 final int verticalPadding = getPaddingTop() + getPaddingBottom();
777 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700778
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700779 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700781 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700782 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700783 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
784 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
785 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
786 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 final int childCount = getChildCount();
788 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700789 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700790 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100791 if (child.getVisibility() != GONE) {
792 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700793
Vladimir Marko2824b072013-10-04 16:42:17 +0100794 int childWidthMode;
795 int childHeightMode;
796 int childWidth;
797 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700798
Vladimir Marko2824b072013-10-04 16:42:17 +0100799 if (!lp.isFullScreenPage) {
800 if (lp.width == LayoutParams.WRAP_CONTENT) {
801 childWidthMode = MeasureSpec.AT_MOST;
802 } else {
803 childWidthMode = MeasureSpec.EXACTLY;
804 }
805
806 if (lp.height == LayoutParams.WRAP_CONTENT) {
807 childHeightMode = MeasureSpec.AT_MOST;
808 } else {
809 childHeightMode = MeasureSpec.EXACTLY;
810 }
811
Adam Cohen3b185e22013-10-29 14:45:58 -0700812 childWidth = getViewportWidth() - horizontalPadding
813 - mInsets.left - mInsets.right;
814 childHeight = getViewportHeight() - verticalPadding
815 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100816 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700817 } else {
818 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700819 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100820
Adam Cohen3b185e22013-10-29 14:45:58 -0700821 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
822 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700823 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700824 if (referenceChildWidth == 0) {
825 referenceChildWidth = childWidth;
826 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700827
Vladimir Marko2824b072013-10-04 16:42:17 +0100828 final int childWidthMeasureSpec =
829 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
830 final int childHeightMeasureSpec =
831 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
832 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700833 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700834 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700835 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800836 }
837
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700839 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700840 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700841 return;
842 }
843
Winson Chung785d2eb2011-04-14 16:08:02 -0700844 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700846
Adam Cohen7d30a372013-07-01 17:03:59 -0700847 int offsetX = getViewportOffsetX();
848 int offsetY = getViewportOffsetY();
849
850 // Update the viewport offsets
851 mViewport.offset(offsetX, offsetY);
852
Adam Cohen0ffac432013-07-10 11:19:26 -0700853 final boolean isRtl = isLayoutRtl();
854
855 final int startIndex = isRtl ? childCount - 1 : 0;
856 final int endIndex = isRtl ? -1 : childCount;
857 final int delta = isRtl ? -1 : 1;
858
Adam Cohen7d30a372013-07-01 17:03:59 -0700859 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700860
Adam Cohen3b185e22013-10-29 14:45:58 -0700861 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000862 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700863
864 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700865 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
866 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700867 }
868
Adam Cohen0ffac432013-07-10 11:19:26 -0700869 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700870 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700872 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100873 int childTop;
874 if (lp.isFullScreenPage) {
875 childTop = offsetY;
876 } else {
877 childTop = offsetY + getPaddingTop() + mInsets.top;
878 if (mCenterPagesVertically) {
879 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
880 }
881 }
882
Adam Cohen96d30a12013-07-16 18:13:21 -0700883 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700884 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800885
Winson Chung785d2eb2011-04-14 16:08:02 -0700886 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700887 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800888 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700889
Adam Cohen3b185e22013-10-29 14:45:58 -0700890 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
891 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000892
893 int pageGap = mPageSpacing;
894 int next = i + delta;
895 if (next != endIndex) {
896 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
897 } else {
898 nextLp = null;
899 }
900
901 // Prevent full screen pages from showing in the viewport
902 // when they are not the current page.
903 if (lp.isFullScreenPage) {
904 pageGap = getPaddingLeft();
905 } else if (nextLp != null && nextLp.isFullScreenPage) {
906 pageGap = getPaddingRight();
907 }
908
909 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 }
911 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700912
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700913 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -0800914 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700915 mFirstLayout = false;
916 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700917
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700918 final LayoutTransition transition = getLayoutTransition();
919 // If the transition is running defer updating max scroll, as some empty pages could
920 // still be present, and a max scroll change could cause sudden jumps in scroll.
921 if (transition != null && transition.isRunning()) {
922 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
923
924 @Override
925 public void startTransition(LayoutTransition transition, ViewGroup container,
926 View view, int transitionType) { }
927
928 @Override
929 public void endTransition(LayoutTransition transition, ViewGroup container,
930 View view, int transitionType) {
931 // Wait until all transitions are complete.
932 if (!transition.isRunning()) {
933 transition.removeTransitionListener(this);
934 updateMaxScrollX();
935 }
936 }
937 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700938 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700939 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700940 }
941
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700942 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700943 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700944 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700945 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700946 } else {
947 setCurrentPage(getNextPage());
948 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700949 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700950 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700951
952 if (isReordering(true)) {
953 updateDragViewTranslationDuringDrag();
954 }
Winson Chunge3193b92010-09-10 11:44:42 -0700955 }
956
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700957 private void updateMaxScrollX() {
958 int childCount = getChildCount();
959 if (childCount > 0) {
960 final int index = isLayoutRtl() ? 0 : childCount - 1;
961 mMaxScrollX = getScrollForPage(index);
962 } else {
963 mMaxScrollX = 0;
964 }
965 }
966
Adam Cohen3b185e22013-10-29 14:45:58 -0700967 public void setPageSpacing(int pageSpacing) {
968 mPageSpacing = pageSpacing;
969 requestLayout();
970 }
971
Adam Cohenf34bab52010-09-30 14:11:56 -0700972 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700973 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
974
975 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700976 for (int i = 0; i < getChildCount(); i++) {
977 View child = getChildAt(i);
978 if (child != null) {
979 float scrollProgress = getScrollProgress(screenCenter, child, i);
980 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800981 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700982 }
983 }
984 invalidate();
985 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700986 }
987
Winson Chunge3193b92010-09-10 11:44:42 -0700988 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700989 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700990 // Update the page indicator, we don't update the page indicator as we
991 // add/remove pages
992 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700993 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700994 mPageIndicator.addMarker(pageIndex,
995 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700996 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700997 }
998
Adam Cohen2591f6a2011-10-25 14:36:40 -0700999 // This ensures that when children are added, they get the correct transforms / alphas
1000 // in accordance with any scroll effects.
1001 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001002 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001003 invalidate();
1004 }
1005
Michael Jurka8b805b12012-04-18 14:23:14 -07001006 @Override
1007 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001008 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001009 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001010 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001011 }
1012
Winson Chungd2be3812013-07-16 11:11:32 -07001013 private void removeMarkerForView(int index) {
1014 // Update the page indicator, we don't update the page indicator as we
1015 // add/remove pages
1016 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001017 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001018 }
1019 }
1020
1021 @Override
1022 public void removeView(View v) {
1023 // XXX: We should find a better way to hook into this before the view
1024 // gets removed form its parent...
1025 removeMarkerForView(indexOfChild(v));
1026 super.removeView(v);
1027 }
1028 @Override
1029 public void removeViewInLayout(View v) {
1030 // XXX: We should find a better way to hook into this before the view
1031 // gets removed form its parent...
1032 removeMarkerForView(indexOfChild(v));
1033 super.removeViewInLayout(v);
1034 }
1035 @Override
1036 public void removeViewAt(int index) {
1037 // XXX: We should find a better way to hook into this before the view
1038 // gets removed form its parent...
1039 removeViewAt(index);
1040 super.removeViewAt(index);
1041 }
1042 @Override
1043 public void removeAllViewsInLayout() {
1044 // Update the page indicator, we don't update the page indicator as we
1045 // add/remove pages
1046 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001047 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001048 }
1049
1050 super.removeAllViewsInLayout();
1051 }
1052
Adam Cohen73894962011-10-31 13:17:17 -07001053 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001054 if (index < 0 || index > getChildCount() - 1) return 0;
1055
Adam Cohenf698c6e2013-07-17 18:44:25 -07001056 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001057
Adam Cohenf698c6e2013-07-17 18:44:25 -07001058 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001059 }
1060
Adam Cohen6f127a62014-06-12 14:54:41 -07001061 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001062 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001063 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001064 }
1065
Michael Jurkadde558b2011-11-09 22:09:06 -08001066 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001067 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001068 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001069
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001070 range[0] = -1;
1071 range[1] = -1;
1072
Michael Jurkac4fb9172010-09-02 17:19:20 -07001073 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001074 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001075 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001076
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001077 int count = getChildCount();
1078 for (int i = 0; i < count; i++) {
1079 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001080
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001081 sTmpIntPoint[0] = 0;
1082 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1083 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001084 if (range[0] == -1) {
1085 continue;
1086 } else {
1087 break;
1088 }
1089 }
1090
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001091 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1092 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1093 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001094 if (range[0] == -1) {
1095 continue;
1096 } else {
1097 break;
1098 }
1099 }
1100 curScreen = i;
1101 if (range[0] < 0) {
1102 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001103 }
1104 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001105
1106 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001107 } else {
1108 range[0] = -1;
1109 range[1] = -1;
1110 }
1111 }
1112
Michael Jurka920d7f42012-05-14 16:29:55 -07001113 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001114 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001115 }
1116
Michael Jurkadde558b2011-11-09 22:09:06 -08001117 @Override
1118 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001119 // Find out which screens are visible; as an optimization we only call draw on them
1120 final int pageCount = getChildCount();
1121 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001122 int halfScreenSize = getViewportWidth() / 2;
1123 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1124 // Otherwise it is equal to the scaled overscroll position.
1125 int screenCenter = mOverScrollX + halfScreenSize;
1126
1127 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1128 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1129 // set it for the next frame
1130 mForceScreenScrolled = false;
1131 screenScrolled(screenCenter);
1132 mLastScreenCenter = screenCenter;
1133 }
1134
Michael Jurkadde558b2011-11-09 22:09:06 -08001135 getVisiblePages(mTempVisiblePagesRange);
1136 final int leftScreen = mTempVisiblePagesRange[0];
1137 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001138 if (leftScreen != -1 && rightScreen != -1) {
1139 final long drawingTime = getDrawingTime();
1140 // Clip to the bounds
1141 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001142 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1143 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001144
Adam Cohen7d30a372013-07-01 17:03:59 -07001145 // Draw all the children, leaving the drag view for last
1146 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001147 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001148 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001149 if (mForceDrawAllChildrenNextFrame ||
1150 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001151 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001152 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001153 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001154 // Draw the drag view on top (if there is one)
1155 if (mDragView != null) {
1156 drawChild(canvas, mDragView, drawingTime);
1157 }
1158
Michael Jurka5e368ff2012-05-14 23:13:15 -07001159 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001160 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001161 }
Michael Jurka0142d492010-08-25 17:46:15 -07001162 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 }
1164
1165 @Override
1166 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001167 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001168 if (page != mCurrentPage || !mScroller.isFinished()) {
1169 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 return true;
1171 }
1172 return false;
1173 }
1174
1175 @Override
1176 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001177 int focusablePage;
1178 if (mNextPage != INVALID_PAGE) {
1179 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001181 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
Winson Chung86f77532010-08-24 11:08:22 -07001183 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001185 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 }
1187 return false;
1188 }
1189
1190 @Override
1191 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001192 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001194 if (getCurrentPage() > 0) {
1195 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 return true;
1197 }
1198 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001199 if (getCurrentPage() < getPageCount() - 1) {
1200 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 return true;
1202 }
1203 }
1204 return super.dispatchUnhandledMove(focused, direction);
1205 }
1206
1207 @Override
1208 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001209 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001210 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001211 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 }
1213 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001214 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001215 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
1217 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001218 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001219 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 }
1222 }
1223
1224 /**
1225 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001226 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 *
Winson Chung86f77532010-08-24 11:08:22 -07001228 * This happens when live folders requery, and if they're off page, they
1229 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 */
1231 @Override
1232 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001233 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 View v = focused;
1235 while (true) {
1236 if (v == current) {
1237 super.focusableViewAvailable(focused);
1238 return;
1239 }
1240 if (v == this) {
1241 return;
1242 }
1243 ViewParent parent = v.getParent();
1244 if (parent instanceof View) {
1245 v = (View)v.getParent();
1246 } else {
1247 return;
1248 }
1249 }
1250 }
1251
1252 /**
1253 * {@inheritDoc}
1254 */
1255 @Override
1256 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1257 if (disallowIntercept) {
1258 // We need to make sure to cancel our long press if
1259 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001260 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001261 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 }
1263 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1264 }
1265
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001266 /**
1267 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1268 */
1269 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001270 if (isLayoutRtl()) {
1271 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001272 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001273 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001274 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001275 }
1276
1277 /**
1278 * Return true if a tap at (x, y) should trigger a flip to the next page.
1279 */
1280 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001281 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001282 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001283 }
1284 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001285 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001286 }
Winson Chung52aee602013-01-30 12:01:02 -08001287
Adam Cohen7d30a372013-07-01 17:03:59 -07001288 /** Returns whether x and y originated within the buffered viewport */
1289 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001290 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001291 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001292 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001293 }
1294
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 @Override
1296 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 if (DISABLE_TOUCH_INTERACTION) {
1298 return false;
1299 }
1300
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 /*
1302 * This method JUST determines whether we want to intercept the motion.
1303 * If we return true, onTouchEvent will be called and we do the actual
1304 * scrolling there.
1305 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001306 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001307
Winson Chung45e1d6e2010-11-09 17:19:49 -08001308 // Skip touch handling if there are no pages to swipe
1309 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1310
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 /*
1312 * Shortcut the most recurring case: the user is in the dragging
1313 * state and he is moving his finger. We want to intercept this
1314 * motion.
1315 */
1316 final int action = ev.getAction();
1317 if ((action == MotionEvent.ACTION_MOVE) &&
1318 (mTouchState == TOUCH_STATE_SCROLLING)) {
1319 return true;
1320 }
1321
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 switch (action & MotionEvent.ACTION_MASK) {
1323 case MotionEvent.ACTION_MOVE: {
1324 /*
1325 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1326 * whether the user has moved far enough from his original down touch.
1327 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001328 if (mActivePointerId != INVALID_POINTER) {
1329 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001330 }
1331 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1332 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1333 // i.e. fall through to the next case (don't break)
1334 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1335 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001336 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 }
1338
1339 case MotionEvent.ACTION_DOWN: {
1340 final float x = ev.getX();
1341 final float y = ev.getY();
1342 // Remember location of down touch
1343 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001344 mDownMotionY = y;
1345 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 mLastMotionX = x;
1347 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001348 float[] p = mapPointFromViewToParent(this, x, y);
1349 mParentDownMotionX = p[0];
1350 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001351 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001352 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001354
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 /*
1356 * If being flinged and user touches the screen, initiate drag;
1357 * otherwise don't. mScroller.isFinished should be false when
1358 * being flinged.
1359 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001360 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001361 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001362
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001363 if (finishedScrolling) {
1364 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001365 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001366 setCurrentPage(getNextPage());
1367 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001368 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001369 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001370 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1371 mTouchState = TOUCH_STATE_SCROLLING;
1372 } else {
1373 mTouchState = TOUCH_STATE_REST;
1374 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001376
Winson Chung86f77532010-08-24 11:08:22 -07001377 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001379 if (!DISABLE_TOUCH_SIDE_PAGES) {
1380 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1381 if (getChildCount() > 0) {
1382 if (hitsPreviousPage(x, y)) {
1383 mTouchState = TOUCH_STATE_PREV_PAGE;
1384 } else if (hitsNextPage(x, y)) {
1385 mTouchState = TOUCH_STATE_NEXT_PAGE;
1386 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 }
1388 }
1389 }
1390 break;
1391 }
1392
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001394 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 break;
1397
1398 case MotionEvent.ACTION_POINTER_UP:
1399 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001400 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 break;
1402 }
1403
1404 /*
1405 * The only time we want to intercept motion events is if we are in the
1406 * drag mode.
1407 */
1408 return mTouchState != TOUCH_STATE_REST;
1409 }
1410
Adam Cohenf8d28232011-02-01 21:47:00 -08001411 protected void determineScrollingStart(MotionEvent ev) {
1412 determineScrollingStart(ev, 1.0f);
1413 }
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 /*
1416 * Determines if we should change the touch state to start scrolling after the
1417 * user moves their touch point too far.
1418 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001419 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001420 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001422 if (pointerIndex == -1) return;
1423
1424 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 final float x = ev.getX(pointerIndex);
1426 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001427 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 final int xDiff = (int) Math.abs(x - mLastMotionX);
1430 final int yDiff = (int) Math.abs(y - mLastMotionY);
1431
Adam Cohenf8d28232011-02-01 21:47:00 -08001432 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 boolean xPaged = xDiff > mPagingTouchSlop;
1434 boolean xMoved = xDiff > touchSlop;
1435 boolean yMoved = yDiff > touchSlop;
1436
Adam Cohenf8d28232011-02-01 21:47:00 -08001437 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001438 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 // Scroll if the user moved far enough along the X axis
1440 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001441 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001443 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001445 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001446 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001447 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001449 }
1450 }
1451
1452 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001453 // Try canceling the long press. It could also have been scheduled
1454 // by a distant descendant, so use the mAllowLongPress flag to block
1455 // everything
1456 final View currentPage = getPageAt(mCurrentPage);
1457 if (currentPage != null) {
1458 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 }
1460 }
1461
Adam Cohenb5ba0972011-09-07 18:02:31 -07001462 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001463 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001464
Adam Cohenedb40762013-07-18 16:45:45 -07001465 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001466 int count = getChildCount();
1467
1468 final int totalDistance;
1469
1470 int adjacentPage = page + 1;
1471 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1472 adjacentPage = page - 1;
1473 }
1474
1475 if (adjacentPage < 0 || adjacentPage > count - 1) {
1476 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1477 } else {
1478 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1479 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001480
1481 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001482 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1483 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001484 return scrollProgress;
1485 }
1486
Adam Cohenedb40762013-07-18 16:45:45 -07001487 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001488 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001489 return 0;
1490 } else {
1491 return mPageScrolls[index];
1492 }
1493 }
1494
Adam Cohen564a2e72013-10-09 14:47:32 -07001495 // While layout transitions are occurring, a child's position may stray from its baseline
1496 // position. This method returns the magnitude of this stray at any given time.
1497 public int getLayoutTransitionOffsetForPage(int index) {
1498 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1499 return 0;
1500 } else {
1501 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001502
1503 int scrollOffset = 0;
1504 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1505 if (!lp.isFullScreenPage) {
1506 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1507 }
1508
Adam Cohen564a2e72013-10-09 14:47:32 -07001509 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1510 return (int) (child.getX() - baselineX);
1511 }
1512 }
1513
Adam Cohene0f66b52010-11-23 15:06:07 -08001514 // This curve determines how the effect of scrolling over the limits of the page dimishes
1515 // as the user pulls further and further from the bounds
1516 private float overScrollInfluenceCurve(float f) {
1517 f -= 1.0f;
1518 return f * f * f + 1.0f;
1519 }
1520
Adam Cohen1e4359c2014-08-18 13:12:16 -07001521 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001522 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001523
1524 // We want to reach the max over scroll effect when the user has
1525 // over scrolled half the size of the screen
1526 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1527
Adam Cohen1e4359c2014-08-18 13:12:16 -07001528 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001529
1530 // Clamp this factor, f, to -1 < f < 1
1531 if (Math.abs(f) >= 1) {
1532 f /= Math.abs(f);
1533 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001534 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001535 }
1536
1537 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001538 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001539
1540 float f = (amount / screenSize);
1541
1542 if (f == 0) return;
1543 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1544
Adam Cohen7bfc9792011-01-28 13:52:37 -08001545 // Clamp this factor, f, to -1 < f < 1
1546 if (Math.abs(f) >= 1) {
1547 f /= Math.abs(f);
1548 }
1549
Adam Cohene0f66b52010-11-23 15:06:07 -08001550 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001551 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001552 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001553 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001554 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001555 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001556 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001557 }
1558 invalidate();
1559 }
1560
Adam Cohenb5ba0972011-09-07 18:02:31 -07001561 protected void overScroll(float amount) {
1562 dampedOverScroll(amount);
1563 }
1564
Michael Jurkac5b262c2011-01-12 20:24:50 -08001565 protected float maxOverScroll() {
1566 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001567 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001568 float f = 1.0f;
1569 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1570 return OVERSCROLL_DAMP_FACTOR * f;
1571 }
1572
Winson Chungdc61c4d2015-04-20 18:26:57 -07001573 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001574 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001575 }
1576
Winson Chungdc61c4d2015-04-20 18:26:57 -07001577 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001578 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001579 }
1580
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001581 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001582 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001583 if (isLayoutRtl()) {
1584 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1585 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1586 } else {
1587 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1588 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1589 }
1590 }
1591
Adam Cohenf9618852013-11-08 06:45:03 -08001592 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001593 mFreeScroll = freeScroll;
1594
Adam Cohenf9618852013-11-08 06:45:03 -08001595 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001596 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001597 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001598 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1599 setCurrentPage(mTempVisiblePagesRange[0]);
1600 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1601 setCurrentPage(mTempVisiblePagesRange[1]);
1602 }
1603 }
1604
1605 setEnableOverscroll(!freeScroll);
1606 }
1607
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001608 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001609 mAllowOverScroll = enable;
1610 }
1611
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001612 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 if (mDragView != null) {
1614 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1615 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001616 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001617 int minDistance = Integer.MAX_VALUE;
1618 int minIndex = indexOfChild(mDragView);
1619 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1620 View page = getPageAt(i);
1621 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1622 int d = Math.abs(dragX - pageX);
1623 if (d < minDistance) {
1624 minIndex = i;
1625 minDistance = d;
1626 }
1627 }
1628 return minIndex;
1629 }
1630 return -1;
1631 }
1632
Winson Chung321e9ee2010-08-09 13:37:56 -07001633 @Override
1634 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001635 if (DISABLE_TOUCH_INTERACTION) {
1636 return false;
1637 }
1638
Adam Cohen1697b792013-09-17 19:08:21 -07001639 super.onTouchEvent(ev);
1640
Winson Chung45e1d6e2010-11-09 17:19:49 -08001641 // Skip touch handling if there are no pages to swipe
1642 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1643
Michael Jurkab8f06722010-10-10 15:58:46 -07001644 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001645
1646 final int action = ev.getAction();
1647
1648 switch (action & MotionEvent.ACTION_MASK) {
1649 case MotionEvent.ACTION_DOWN:
1650 /*
1651 * If being flinged and user touches, stop the fling. isFinished
1652 * will be false if being flinged.
1653 */
1654 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001655 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001656 }
1657
1658 // Remember where the motion event started
1659 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001660 mDownMotionY = mLastMotionY = ev.getY();
1661 mDownScrollX = getScrollX();
1662 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1663 mParentDownMotionX = p[0];
1664 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001665 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001666 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001667 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001668
Michael Jurka0142d492010-08-25 17:46:15 -07001669 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001670 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001671 pageBeginMoving();
1672 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001673 break;
1674
1675 case MotionEvent.ACTION_MOVE:
1676 if (mTouchState == TOUCH_STATE_SCROLLING) {
1677 // Scroll to follow the motion event
1678 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001679
1680 if (pointerIndex == -1) return true;
1681
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001683 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001684
Adam Cohenaefd4e12011-02-14 16:39:38 -08001685 mTotalMotionX += Math.abs(deltaX);
1686
Winson Chungc0844aa2011-02-02 15:25:58 -08001687 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1688 // keep the remainder because we are actually testing if we've moved from the last
1689 // scrolled position (which is discrete).
1690 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001691 mTouchX += deltaX;
1692 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1693 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001694 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001695 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001696 } else {
1697 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001698 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001699 mLastMotionX = x;
1700 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001701 } else {
1702 awakenScrollBars();
1703 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001704 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1705 // Update the last motion position
1706 mLastMotionX = ev.getX();
1707 mLastMotionY = ev.getY();
1708
1709 // Update the parent down so that our zoom animations take this new movement into
1710 // account
1711 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1712 mParentDownMotionX = pt[0];
1713 mParentDownMotionY = pt[1];
1714 updateDragViewTranslationDuringDrag();
1715
1716 // Find the closest page to the touch point
1717 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001718
Adam Cohen7d30a372013-07-01 17:03:59 -07001719 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1720 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1721 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1722 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1723
Adam Cohenf358a4b2013-07-23 16:47:31 -07001724 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001725 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001726 mTempVisiblePagesRange[0] = 0;
1727 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001728 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001729 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1730 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1731 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1732 mSidePageHoverIndex = pageUnderPointIndex;
1733 mSidePageHoverRunnable = new Runnable() {
1734 @Override
1735 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001736 // Setup the scroll to the correct page before we swap the views
1737 snapToPage(pageUnderPointIndex);
1738
1739 // For each of the pages between the paged view and the drag view,
1740 // animate them from the previous position to the new position in
1741 // the layout (as a result of the drag view moving in the layout)
1742 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1743 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1744 dragViewIndex + 1 : pageUnderPointIndex;
1745 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1746 dragViewIndex - 1 : pageUnderPointIndex;
1747 for (int i = lowerIndex; i <= upperIndex; ++i) {
1748 View v = getChildAt(i);
1749 // dragViewIndex < pageUnderPointIndex, so after we remove the
1750 // drag view all subsequent views to pageUnderPointIndex will
1751 // shift down.
1752 int oldX = getViewportOffsetX() + getChildOffset(i);
1753 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1754
1755 // Animate the view translation from its old position to its new
1756 // position
1757 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1758 if (anim != null) {
1759 anim.cancel();
1760 }
1761
1762 v.setTranslationX(oldX - newX);
1763 anim = new AnimatorSet();
1764 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1765 anim.playTogether(
1766 ObjectAnimator.ofFloat(v, "translationX", 0f));
1767 anim.start();
1768 v.setTag(anim);
1769 }
1770
1771 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001772 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001773 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001774 if (mPageIndicator != null) {
1775 mPageIndicator.setActiveMarker(getNextPage());
1776 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001777 }
1778 };
1779 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1780 }
1781 } else {
1782 removeCallbacks(mSidePageHoverRunnable);
1783 mSidePageHoverIndex = -1;
1784 }
Adam Cohen564976a2010-10-13 18:52:07 -07001785 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001786 determineScrollingStart(ev);
1787 }
1788 break;
1789
1790 case MotionEvent.ACTION_UP:
1791 if (mTouchState == TOUCH_STATE_SCROLLING) {
1792 final int activePointerId = mActivePointerId;
1793 final int pointerIndex = ev.findPointerIndex(activePointerId);
1794 final float x = ev.getX(pointerIndex);
1795 final VelocityTracker velocityTracker = mVelocityTracker;
1796 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1797 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001798 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001799 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001800 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1801 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001802
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001803 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1804
Adam Cohen00481b32011-11-18 12:03:48 -08001805 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001806 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001807
Adam Cohenf358a4b2013-07-23 16:47:31 -07001808 if (!mFreeScroll) {
1809 // In the case that the page is moved far to one direction and then is flung
1810 // in the opposite direction, we use a threshold to determine whether we should
1811 // just return to the starting page, or if we should skip one further.
1812 boolean returnToOriginalPage = false;
1813 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1814 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1815 returnToOriginalPage = true;
1816 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001817
Adam Cohenf358a4b2013-07-23 16:47:31 -07001818 int finalPage;
1819 // We give flings precedence over large moves, which is why we short-circuit our
1820 // test for a large move if a fling has been registered. That is, a large
1821 // move to the left and fling to the right will register as a fling to the right.
1822 final boolean isRtl = isLayoutRtl();
1823 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1824 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1825 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1826 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1827 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1828 snapToPageWithVelocity(finalPage, velocityX);
1829 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1830 (isFling && isVelocityXLeft)) &&
1831 mCurrentPage < getChildCount() - 1) {
1832 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1833 snapToPageWithVelocity(finalPage, velocityX);
1834 } else {
1835 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001836 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001837 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001838 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001839 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001840 }
1841
1842 float scaleX = getScaleX();
1843 int vX = (int) (-velocityX * scaleX);
1844 int initialScrollX = (int) (getScrollX() * scaleX);
1845
Adam Cohenf9618852013-11-08 06:45:03 -08001846 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001847 mScroller.fling(initialScrollX,
1848 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1849 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001850 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001851 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001852 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1853 // at this point we have not moved beyond the touch slop
1854 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1855 // we can just page
1856 int nextPage = Math.max(0, mCurrentPage - 1);
1857 if (nextPage != mCurrentPage) {
1858 snapToPage(nextPage);
1859 } else {
1860 snapToDestination();
1861 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001862 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001863 // at this point we have not moved beyond the touch slop
1864 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1865 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001866 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1867 if (nextPage != mCurrentPage) {
1868 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001869 } else {
1870 snapToDestination();
1871 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001872 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1873 // Update the last motion position
1874 mLastMotionX = ev.getX();
1875 mLastMotionY = ev.getY();
1876
1877 // Update the parent down so that our zoom animations take this new movement into
1878 // account
1879 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1880 mParentDownMotionX = pt[0];
1881 mParentDownMotionY = pt[1];
1882 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001883 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001884 if (!mCancelTap) {
1885 onUnhandledTap(ev);
1886 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001887 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001888
1889 // Remove the callback to wait for the side page hover timeout
1890 removeCallbacks(mSidePageHoverRunnable);
1891 // End any intermediate reordering states
1892 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001893 break;
1894
1895 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001896 if (mTouchState == TOUCH_STATE_SCROLLING) {
1897 snapToDestination();
1898 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001899 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001900 break;
1901
1902 case MotionEvent.ACTION_POINTER_UP:
1903 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001904 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 break;
1906 }
1907
1908 return true;
1909 }
1910
Adam Cohen7d30a372013-07-01 17:03:59 -07001911 private void resetTouchState() {
1912 releaseVelocityTracker();
1913 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001914 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001915 mTouchState = TOUCH_STATE_REST;
1916 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001917 }
1918
Adam Cohenc2d6e892014-10-16 09:49:24 -07001919 /**
1920 * Triggered by scrolling via touch
1921 */
1922 protected void onScrollInteractionBegin() {
1923 }
1924
1925 protected void onScrollInteractionEnd() {
1926 }
1927
Adam Cohen1697b792013-09-17 19:08:21 -07001928 protected void onUnhandledTap(MotionEvent ev) {
1929 ((Launcher) getContext()).onClick(this);
1930 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001931
Winson Chung185d7162011-02-28 13:47:29 -08001932 @Override
1933 public boolean onGenericMotionEvent(MotionEvent event) {
1934 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1935 switch (event.getAction()) {
1936 case MotionEvent.ACTION_SCROLL: {
1937 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1938 final float vscroll;
1939 final float hscroll;
1940 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1941 vscroll = 0;
1942 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1943 } else {
1944 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1945 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1946 }
1947 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001948 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1949 : (hscroll > 0 || vscroll > 0);
1950 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001951 scrollRight();
1952 } else {
1953 scrollLeft();
1954 }
1955 return true;
1956 }
1957 }
1958 }
1959 }
1960 return super.onGenericMotionEvent(event);
1961 }
1962
Michael Jurkab8f06722010-10-10 15:58:46 -07001963 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1964 if (mVelocityTracker == null) {
1965 mVelocityTracker = VelocityTracker.obtain();
1966 }
1967 mVelocityTracker.addMovement(ev);
1968 }
1969
1970 private void releaseVelocityTracker() {
1971 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001972 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001973 mVelocityTracker.recycle();
1974 mVelocityTracker = null;
1975 }
1976 }
1977
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 private void onSecondaryPointerUp(MotionEvent ev) {
1979 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1980 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1981 final int pointerId = ev.getPointerId(pointerIndex);
1982 if (pointerId == mActivePointerId) {
1983 // This was our active pointer going up. Choose a new
1984 // active pointer and adjust accordingly.
1985 // TODO: Make this decision more intelligent.
1986 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1987 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1988 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001989 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001990 mActivePointerId = ev.getPointerId(newPointerIndex);
1991 if (mVelocityTracker != null) {
1992 mVelocityTracker.clear();
1993 }
1994 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001995 }
1996
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 @Override
1998 public void requestChildFocus(View child, View focused) {
1999 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002000 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002001 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002002 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 }
2004 }
2005
Adam Cohend19d3ca2010-09-15 14:43:42 -07002006 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002007 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002008 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002009 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002010 final int childCount = getChildCount();
2011 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002012 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002013 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002014 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002016 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2017 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2018 minDistanceFromScreenCenter = distanceFromScreenCenter;
2019 minDistanceFromScreenCenterIndex = i;
2020 }
2021 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002022 return minDistanceFromScreenCenterIndex;
2023 }
2024
Adam Cohen1e4359c2014-08-18 13:12:16 -07002025 protected boolean isInOverScroll() {
2026 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2027 }
2028
2029 protected int getPageSnapDuration() {
2030 if (isInOverScroll()) {
2031 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2032 }
2033 return PAGE_SNAP_ANIMATION_DURATION;
2034
2035 }
2036
Adam Cohend19d3ca2010-09-15 14:43:42 -07002037 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002038 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002039 }
2040
Adam Cohene0f66b52010-11-23 15:06:07 -08002041 private static class ScrollInterpolator implements Interpolator {
2042 public ScrollInterpolator() {
2043 }
2044
2045 public float getInterpolation(float t) {
2046 t -= 1.0f;
2047 return t*t*t*t*t + 1;
2048 }
2049 }
2050
2051 // We want the duration of the page snap animation to be influenced by the distance that
2052 // the screen has to travel, however, we don't want this duration to be effected in a
2053 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2054 // of travel has on the overall snap duration.
2055 float distanceInfluenceForSnapDuration(float f) {
2056 f -= 0.5f; // center the values about 0.
2057 f *= 0.3f * Math.PI / 2.0f;
2058 return (float) Math.sin(f);
2059 }
2060
Michael Jurka0142d492010-08-25 17:46:15 -07002061 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002062 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002063 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002064
Adam Cohenedb40762013-07-18 16:45:45 -07002065 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002066 int delta = newX - mUnboundedScrollX;
2067 int duration = 0;
2068
Adam Cohen1e4359c2014-08-18 13:12:16 -07002069 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002070 // If the velocity is low enough, then treat this more as an automatic page advance
2071 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002072 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002073 return;
2074 }
2075
2076 // Here we compute a "distance" that will be used in the computation of the overall
2077 // snap duration. This is a function of the actual distance that needs to be traveled;
2078 // we keep this value close to half screen size in order to reduce the variance in snap
2079 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002080 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002081 float distance = halfScreenSize + halfScreenSize *
2082 distanceInfluenceForSnapDuration(distanceRatio);
2083
2084 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002085 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002086
2087 // we want the page's snap velocity to approximately match the velocity at which the
2088 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002089 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2090 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002091
2092 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002093 }
2094
2095 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002096 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002097 }
2098
Adam Cohen7d30a372013-07-01 17:03:59 -07002099 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002100 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002101 }
2102
Michael Jurka0142d492010-08-25 17:46:15 -07002103 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002104 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002105 }
2106
Adam Cohenf9618852013-11-08 06:45:03 -08002107 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2108 snapToPage(whichPage, duration, false, interpolator);
2109 }
2110
2111 protected void snapToPage(int whichPage, int duration, boolean immediate,
2112 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002113 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002114
Adam Cohenedb40762013-07-18 16:45:45 -07002115 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002116 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002117 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002118 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002119 }
2120
2121 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002122 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002123 }
Michael Jurka0142d492010-08-25 17:46:15 -07002124
Adam Cohenf9618852013-11-08 06:45:03 -08002125 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2126 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002127 whichPage = validateNewPage(whichPage);
2128
Adam Cohen7d30a372013-07-01 17:03:59 -07002129 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002130 View focusedChild = getFocusedChild();
2131 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002132 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002133 focusedChild.clearFocus();
2134 }
2135
Adam Cohen53805212013-10-01 10:39:23 -07002136 sendScrollAccessibilityEvent();
2137
Michael Jurka0142d492010-08-25 17:46:15 -07002138 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002139 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002140 if (immediate) {
2141 duration = 0;
2142 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002143 duration = Math.abs(delta);
2144 }
2145
Adam Cohenf358a4b2013-07-23 16:47:31 -07002146 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002147 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002148 }
Adam Cohenf9618852013-11-08 06:45:03 -08002149
2150 if (interpolator != null) {
2151 mScroller.setInterpolator(interpolator);
2152 } else {
2153 mScroller.setInterpolator(mDefaultInterpolator);
2154 }
2155
Adam Cohen68d73932010-11-15 10:50:58 -08002156 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002157
Adam Cohen674531f2013-12-13 15:07:14 -08002158 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002159
2160 // Trigger a compute() to finish switching pages if necessary
2161 if (immediate) {
2162 computeScroll();
2163 }
2164
2165 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002166 invalidate();
2167 }
2168
Winson Chung321e9ee2010-08-09 13:37:56 -07002169 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002170 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002171 }
2172
2173 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002174 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002175 }
2176
Winson Chung86f77532010-08-24 11:08:22 -07002177 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002178 int result = -1;
2179 if (v != null) {
2180 ViewParent vp = v.getParent();
2181 int count = getChildCount();
2182 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002183 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002184 return i;
2185 }
2186 }
2187 }
2188 return result;
2189 }
2190
Adam Cohendbdff6b2013-09-18 19:09:15 -07002191 @Override
2192 public boolean performLongClick() {
2193 mCancelTap = true;
2194 return super.performLongClick();
2195 }
2196
Winson Chung321e9ee2010-08-09 13:37:56 -07002197 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002198 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002199
2200 SavedState(Parcelable superState) {
2201 super(superState);
2202 }
2203
Adam Cohen091440a2015-03-18 14:16:05 -07002204 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002205 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002206 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002207 }
2208
2209 @Override
2210 public void writeToParcel(Parcel out, int flags) {
2211 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002212 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002213 }
2214
2215 public static final Parcelable.Creator<SavedState> CREATOR =
2216 new Parcelable.Creator<SavedState>() {
2217 public SavedState createFromParcel(Parcel in) {
2218 return new SavedState(in);
2219 }
2220
2221 public SavedState[] newArray(int size) {
2222 return new SavedState[size];
2223 }
2224 };
2225 }
2226
Adam Cohen7d30a372013-07-01 17:03:59 -07002227 // Animate the drag view back to the original position
2228 void animateDragViewToOriginalPosition() {
2229 if (mDragView != null) {
2230 AnimatorSet anim = new AnimatorSet();
2231 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2232 anim.playTogether(
2233 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002234 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2235 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2236 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002237 anim.addListener(new AnimatorListenerAdapter() {
2238 @Override
2239 public void onAnimationEnd(Animator animation) {
2240 onPostReorderingAnimationCompleted();
2241 }
2242 });
2243 anim.start();
2244 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002245 }
2246
Sunny Goyal1d08f702015-05-04 15:50:25 -07002247 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002248 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2249 mTouchState = TOUCH_STATE_REORDERING;
2250 mIsReordering = true;
2251
Adam Cohen7d30a372013-07-01 17:03:59 -07002252 // We must invalidate to trigger a redraw to update the layers such that the drag view
2253 // is always drawn on top
2254 invalidate();
2255 }
2256
Adam Cohen091440a2015-03-18 14:16:05 -07002257 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002258 // Trigger the callback when reordering has settled
2259 --mPostReorderingPreZoomInRemainingAnimationCount;
2260 if (mPostReorderingPreZoomInRunnable != null &&
2261 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2262 mPostReorderingPreZoomInRunnable.run();
2263 mPostReorderingPreZoomInRunnable = null;
2264 }
2265 }
2266
Sunny Goyal1d08f702015-05-04 15:50:25 -07002267 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002268 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002269 }
2270
Adam Cohenf358a4b2013-07-23 16:47:31 -07002271 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002272 int dragViewIndex = indexOfChild(v);
2273
Adam Cohen327acfe2014-06-06 11:52:52 -07002274 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002275
Adam Cohen7d30a372013-07-01 17:03:59 -07002276 mTempVisiblePagesRange[0] = 0;
2277 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002278 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002279 mReorderingStarted = true;
2280
2281 // Check if we are within the reordering range
2282 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002283 dragViewIndex <= mTempVisiblePagesRange[1]) {
2284 // Find the drag view under the pointer
2285 mDragView = getChildAt(dragViewIndex);
2286 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2287 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002288 snapToPage(getPageNearestToCenterOfScreen());
2289 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002290 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002291 return true;
2292 }
2293 return false;
2294 }
2295
2296 boolean isReordering(boolean testTouchState) {
2297 boolean state = mIsReordering;
2298 if (testTouchState) {
2299 state &= (mTouchState == TOUCH_STATE_REORDERING);
2300 }
2301 return state;
2302 }
2303 void endReordering() {
2304 // For simplicity, we call endReordering sometimes even if reordering was never started.
2305 // In that case, we don't want to do anything.
2306 if (!mReorderingStarted) return;
2307 mReorderingStarted = false;
2308
2309 // If we haven't flung-to-delete the current child, then we just animate the drag view
2310 // back into position
2311 final Runnable onCompleteRunnable = new Runnable() {
2312 @Override
2313 public void run() {
2314 onEndReordering();
2315 }
2316 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002317
2318 mPostReorderingPreZoomInRunnable = new Runnable() {
2319 public void run() {
2320 onCompleteRunnable.run();
2321 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002322 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002323 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002324
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002325 mPostReorderingPreZoomInRemainingAnimationCount =
2326 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2327 // Snap to the current page
2328 snapToPage(indexOfChild(mDragView), 0);
2329 // Animate the drag view back to the front position
2330 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002331 }
2332
Adam Cohen7d30a372013-07-01 17:03:59 -07002333 private static final int ANIM_TAG_KEY = 100;
2334
Winson Chung6a0f57d2011-06-29 20:10:49 -07002335 /* Accessibility */
2336 @Override
2337 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2338 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002339 info.setScrollable(getPageCount() > 1);
2340 if (getCurrentPage() < getPageCount() - 1) {
2341 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2342 }
2343 if (getCurrentPage() > 0) {
2344 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2345 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002346 }
2347
2348 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002349 public void sendAccessibilityEvent(int eventType) {
2350 // Don't let the view send real scroll events.
2351 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2352 super.sendAccessibilityEvent(eventType);
2353 }
2354 }
2355
2356 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002357 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2358 super.onInitializeAccessibilityEvent(event);
2359 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002360 }
2361
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002362 @Override
2363 public boolean performAccessibilityAction(int action, Bundle arguments) {
2364 if (super.performAccessibilityAction(action, arguments)) {
2365 return true;
2366 }
2367 switch (action) {
2368 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2369 if (getCurrentPage() < getPageCount() - 1) {
2370 scrollRight();
2371 return true;
2372 }
2373 } break;
2374 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2375 if (getCurrentPage() > 0) {
2376 scrollLeft();
2377 return true;
2378 }
2379 } break;
2380 }
2381 return false;
2382 }
2383
Adam Cohen0ffac432013-07-10 11:19:26 -07002384 protected String getCurrentPageDescription() {
2385 return String.format(getContext().getString(R.string.default_scroll_format),
2386 getNextPage() + 1, getChildCount());
2387 }
2388
Winson Chungd11265e2011-08-30 13:37:23 -07002389 @Override
2390 public boolean onHoverEvent(android.view.MotionEvent event) {
2391 return true;
2392 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002393}