blob: f38a883008758e363423023c63f42db47ec909bc [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal316490e2015-06-02 09:38:28 -070031import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080049import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070050
51import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Sunny Goyal4d113a52015-05-27 10:05:28 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Adam Cohen7d30a372013-07-01 17:03:59 -070068 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070070 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Sunny Goyal8e2133b2015-05-06 13:39:07 -070076 private static final float MAX_SCROLL_PROGRESS = 1.0f;
77
Adam Cohen265b9a62011-12-07 14:37:18 -080078 // The following constants need to be scaled based on density. The scaled versions will be
79 // assigned to the corresponding member variables below.
80 private static final int FLING_THRESHOLD_VELOCITY = 500;
81 private static final int MIN_SNAP_VELOCITY = 1500;
82 private static final int MIN_FLING_VELOCITY = 250;
83
Adam Cohen21cd0022013-10-09 18:57:02 -070084 public static final int INVALID_RESTORE_PAGE = -1001;
85
Adam Cohenf358a4b2013-07-23 16:47:31 -070086 private boolean mFreeScroll = false;
87 private int mFreeScrollMinScrollX = -1;
88 private int mFreeScrollMaxScrollX = -1;
89
Winson Chung8aad6102012-05-11 16:27:49 -070090 static final int AUTOMATIC_PAGE_SPACING = -1;
91
Adam Cohen265b9a62011-12-07 14:37:18 -080092 protected int mFlingThresholdVelocity;
93 protected int mMinFlingVelocity;
94 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Adam Cohenb5ba0972011-09-07 18:02:31 -070096 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected float mSmoothingTime;
98 protected float mTouchX;
99
100 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700101 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700102
103 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700104 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700105 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800108 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800109 protected LauncherScroller mScroller;
110 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Adam Cohen7d30a372013-07-01 17:03:59 -0700114 private float mParentDownMotionX;
115 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700117 private float mDownMotionY;
118 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700119 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800121 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800122 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800123 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700124 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700125
Adam Cohendbdff6b2013-09-18 19:09:15 -0700126 private boolean mCancelTap;
127
Adam Cohenedb40762013-07-18 16:45:45 -0700128 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Michael Jurka0142d492010-08-25 17:46:15 -0700130 protected final static int TOUCH_STATE_REST = 0;
131 protected final static int TOUCH_STATE_SCROLLING = 1;
132 protected final static int TOUCH_STATE_PREV_PAGE = 2;
133 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700134 protected final static int TOUCH_STATE_REORDERING = 4;
135
Adam Cohene45440e2010-10-14 18:33:38 -0700136 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka0142d492010-08-25 17:46:15 -0700138 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700139 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800140
Michael Jurka0142d492010-08-25 17:46:15 -0700141 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka7426c422010-11-11 15:23:47 -0800143 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700145 protected int mPageLayoutWidthGap;
146 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700147 protected int mCellCountX = 0;
148 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700149 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800150 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800151 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700152 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka5f1c5092010-09-03 14:15:02 -0700154 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700155
Michael Jurka5f1c5092010-09-03 14:15:02 -0700156 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Winson Chung86f77532010-08-24 11:08:22 -0700158 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159
Michael Jurka0142d492010-08-25 17:46:15 -0700160 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700161 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700162
Patrick Dubroy1262e362010-10-06 15:49:50 -0700163 protected boolean mIsPageMoving = false;
164
Adam Cohenc2d6e892014-10-16 09:49:24 -0700165 private boolean mWasInOverscroll = false;
166
Winson Chungd2be3812013-07-16 11:11:32 -0700167 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk int mPageIndicatorViewId;
169 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700170 // The viewport whether the pages are to be contained (the actual view may be larger than the
171 // viewport)
172 private Rect mViewport = new Rect();
173
174 // Reordering
175 // We use the min scale to determine how much to expand the actually PagedView measured
176 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700177 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700178 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700179 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
180
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700182 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700183 protected View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700185 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700186 // This variable's scope is only for the duration of startReordering() and endReordering()
187 private boolean mReorderingStarted = false;
188 // This variable's scope is for the duration of startReordering() and after the zoomIn()
189 // animation after endReordering()
190 private boolean mIsReordering;
191 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
192 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
193 private int mPostReorderingPreZoomInRemainingAnimationCount;
194 private Runnable mPostReorderingPreZoomInRunnable;
195
Adam Cohen7d30a372013-07-01 17:03:59 -0700196 // Convenience/caching
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700197 private static final Matrix sTmpInvMatrix = new Matrix();
198 private static final float[] sTmpPoint = new float[2];
199 private static final int[] sTmpIntPoint = new int[2];
200 private static final Rect sTmpRect = new Rect();
Winson Chungb44b5242011-06-13 11:32:14 -0700201
John Spurlock77e1f472013-09-11 10:09:51 -0400202 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700203 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400204
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700205 // When set to true, full screen content and overscroll effect is shited inside by right inset.
206 protected boolean mIgnoreRightInset;
207
Sunny Goyal4d113a52015-05-27 10:05:28 -0700208 // Edge effect
209 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
210 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
211
Winson Chung86f77532010-08-24 11:08:22 -0700212 public interface PageSwitchListener {
213 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 }
215
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 public PagedView(Context context) {
217 this(context, null);
218 }
219
220 public PagedView(Context context, AttributeSet attrs) {
221 this(context, attrs, 0);
222 }
223
224 public PagedView(Context context, AttributeSet attrs, int defStyle) {
225 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700226
Adam Cohen9c4949e2010-10-05 12:27:22 -0700227 TypedArray a = context.obtainStyledAttributes(attrs,
228 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700229
Winson Chungdf4b83d2010-10-20 17:49:27 -0700230 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700231 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700232 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700233 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700234 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700235 a.recycle();
236
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700238 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700239 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 }
241
242 /**
243 * Initializes various states for this workspace.
244 */
Michael Jurka0142d492010-08-25 17:46:15 -0700245 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800246 mScroller = new LauncherScroller(getContext());
247 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700248 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700249 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700250
251 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700252 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700253 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700254 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800255
256 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
257 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
258 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700259 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700260 setWillNotDraw(false);
261 }
262
263 protected void setEdgeGlowColor(int color) {
264 mEdgeGlowLeft.setColor(color);
265 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 }
267
Adam Cohenf9618852013-11-08 06:45:03 -0800268 protected void setDefaultInterpolator(Interpolator interpolator) {
269 mDefaultInterpolator = interpolator;
270 mScroller.setInterpolator(mDefaultInterpolator);
271 }
272
Winson Chungd2be3812013-07-16 11:11:32 -0700273 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700274 super.onAttachedToWindow();
275
Winson Chungd2be3812013-07-16 11:11:32 -0700276 // Hook up the page indicator
277 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700278 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700279 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700280 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700281 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700282
Winson Chung7819a562013-09-19 15:55:45 -0700283 ArrayList<PageIndicator.PageMarkerResources> markers =
284 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700285 for (int i = 0; i < getChildCount(); ++i) {
286 markers.add(getPageIndicatorMarker(i));
287 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700288
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700289 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700290
291 OnClickListener listener = getPageIndicatorClickListener();
292 if (listener != null) {
293 mPageIndicator.setOnClickListener(listener);
294 }
Adam Cohen53805212013-10-01 10:39:23 -0700295 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700296 }
297 }
298
Adam Cohen53805212013-10-01 10:39:23 -0700299 protected String getPageIndicatorDescription() {
300 return getCurrentPageDescription();
301 }
302
303 protected OnClickListener getPageIndicatorClickListener() {
304 return null;
305 }
306
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700307 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700308 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700309 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700310 // Unhook the page indicator
311 mPageIndicator = null;
312 }
313
Adam Cohen7d30a372013-07-01 17:03:59 -0700314 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700315 private float[] mapPointFromViewToParent(View v, float x, float y) {
316 sTmpPoint[0] = x;
317 sTmpPoint[1] = y;
318 v.getMatrix().mapPoints(sTmpPoint);
319 sTmpPoint[0] += v.getLeft();
320 sTmpPoint[1] += v.getTop();
321 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700323 private float[] mapPointFromParentToView(View v, float x, float y) {
324 sTmpPoint[0] = x - v.getLeft();
325 sTmpPoint[1] = y - v.getTop();
326 v.getMatrix().invert(sTmpInvMatrix);
327 sTmpInvMatrix.mapPoints(sTmpPoint);
328 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700329 }
330
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700331 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700332 if (mDragView != null) {
333 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
334 (mDragViewBaselineLeft - mDragView.getLeft());
335 float y = mLastMotionY - mDownMotionY;
336 mDragView.setTranslationX(x);
337 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700338
Adam Cohenf358a4b2013-07-23 16:47:31 -0700339 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
340 + x + ", " + y);
341 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700342 }
343
344 public void setMinScale(float f) {
345 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700346 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700347 requestLayout();
348 }
349
350 @Override
351 public void setScaleX(float scaleX) {
352 super.setScaleX(scaleX);
353 if (isReordering(true)) {
354 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
355 mLastMotionX = p[0];
356 mLastMotionY = p[1];
357 updateDragViewTranslationDuringDrag();
358 }
359 }
360
361 // Convenience methods to get the actual width/height of the PagedView (since it is measured
362 // to be larger to account for the minimum possible scale)
363 int getViewportWidth() {
364 return mViewport.width();
365 }
366 int getViewportHeight() {
367 return mViewport.height();
368 }
369
370 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
371 // PagedView both horizontally and vertically
372 int getViewportOffsetX() {
373 return (getMeasuredWidth() - getViewportWidth()) / 2;
374 }
375
376 int getViewportOffsetY() {
377 return (getMeasuredHeight() - getViewportHeight()) / 2;
378 }
379
Adam Cohenedb40762013-07-18 16:45:45 -0700380 PageIndicator getPageIndicator() {
381 return mPageIndicator;
382 }
Winson Chung7819a562013-09-19 15:55:45 -0700383 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
384 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700385 }
Adam Cohenedb40762013-07-18 16:45:45 -0700386
Adam Cohen674531f2013-12-13 15:07:14 -0800387 /**
388 * Add a page change listener which will be called when a page is _finished_ listening.
389 *
390 */
Winson Chung86f77532010-08-24 11:08:22 -0700391 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
392 mPageSwitchListener = pageSwitchListener;
393 if (mPageSwitchListener != null) {
394 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 }
396 }
397
398 /**
Winson Chung86f77532010-08-24 11:08:22 -0700399 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700401 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700402 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700404
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700405 /**
406 * Returns the index of page to be shown immediately afterwards.
407 */
Winson Chung360e63f2012-04-27 13:48:05 -0700408 int getNextPage() {
409 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
410 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700411
Winson Chung86f77532010-08-24 11:08:22 -0700412 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700413 return getChildCount();
414 }
415
Sunny Goyal83a8f042015-05-19 12:52:12 -0700416 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 return getChildAt(index);
418 }
419
Adam Cohenae4f1552011-10-20 00:15:42 -0700420 protected int indexToPage(int index) {
421 return index;
422 }
423
Winson Chung321e9ee2010-08-09 13:37:56 -0700424 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800425 * Updates the scroll of the current page immediately to its final scroll position. We use this
426 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
427 * the previous tab page.
428 */
429 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700430 // If the current page is invalid, just reset the scroll position to zero
431 int newX = 0;
432 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700433 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700434 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800435 scrollTo(newX, 0);
436 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700437 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800438 }
439
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700440 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700441 mScroller.abortAnimation();
442 // We need to clean up the next page here to avoid computeScrollHelper from
443 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700444 if (resetNextPage) {
445 mNextPage = INVALID_PAGE;
446 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700447 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700448
449 private void forceFinishScroller() {
450 mScroller.forceFinished(true);
451 // We need to clean up the next page here to avoid computeScrollHelper from
452 // updating current page on the pass.
453 mNextPage = INVALID_PAGE;
454 }
455
Adam Cohen6f127a62014-06-12 14:54:41 -0700456 private int validateNewPage(int newPage) {
457 int validatedPage = newPage;
458 // When in free scroll mode, we need to clamp to the free scroll page range.
459 if (mFreeScroll) {
460 getFreeScrollPageRange(mTempVisiblePagesRange);
461 validatedPage = Math.max(mTempVisiblePagesRange[0],
462 Math.min(newPage, mTempVisiblePagesRange[1]));
463 }
464 // Ensure that it is clamped by the actual set of children in all cases
465 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
466 return validatedPage;
467 }
468
Chet Haasebc2f0822012-10-26 17:59:53 -0700469 /**
Winson Chung86f77532010-08-24 11:08:22 -0700470 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700471 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700472 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800473 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700474 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800475 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800476 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
477 // the default
478 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800479 return;
480 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700481 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700482 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700483 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700484 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800485 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
487
Winson Chung8c87cd82013-07-23 16:20:10 -0700488 /**
489 * The restore page will be set in place of the current page at the next (likely first)
490 * layout.
491 */
492 void setRestorePage(int restorePage) {
493 mRestorePage = restorePage;
494 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800495 int getRestorePage() {
496 return mRestorePage;
497 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700498
Adam Cohen674531f2013-12-13 15:07:14 -0800499 /**
500 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
501 * has settled.
502 */
Michael Jurka0142d492010-08-25 17:46:15 -0700503 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700504 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800505 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
Winson Chungd2be3812013-07-16 11:11:32 -0700507
Adam Cohen674531f2013-12-13 15:07:14 -0800508 updatePageIndicator();
509 }
510
511 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700512 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700513 if (mPageIndicator != null) {
514 mPageIndicator.setContentDescription(getPageIndicatorDescription());
515 if (!isReordering(false)) {
516 mPageIndicator.setActiveMarker(getNextPage());
517 }
Winson Chungd2be3812013-07-16 11:11:32 -0700518 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800520 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700521 if (!mIsPageMoving) {
522 mIsPageMoving = true;
523 onPageBeginMoving();
524 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700525 }
526
Michael Jurkace7e05f2011-02-01 22:02:35 -0800527 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700528 if (mIsPageMoving) {
529 mIsPageMoving = false;
530 onPageEndMoving();
531 }
Michael Jurka0142d492010-08-25 17:46:15 -0700532 }
533
Adam Cohen26976d92011-03-22 15:33:33 -0700534 protected boolean isPageMoving() {
535 return mIsPageMoving;
536 }
537
Michael Jurka0142d492010-08-25 17:46:15 -0700538 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700539 protected void onPageBeginMoving() {
540 }
541
542 // a method that subclasses can override to add behavior
543 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700544 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700545 }
546
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 /**
Winson Chung86f77532010-08-24 11:08:22 -0700548 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 *
550 * @param l The listener used to respond to long clicks.
551 */
552 @Override
553 public void setOnLongClickListener(OnLongClickListener l) {
554 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700555 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700557 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 }
Adam Cohen1697b792013-09-17 19:08:21 -0700559 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 }
561
562 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800563 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700564 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800565 }
566
567 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700568 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700569 // In free scroll mode, we clamp the scrollX
570 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700571 // If the scroller is trying to move to a location beyond the maximum allowed
572 // in the free scroll mode, we make sure to end the scroll operation.
573 if (!mScroller.isFinished() &&
574 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
575 forceFinishScroller();
576 }
577
Adam Cohenf358a4b2013-07-23 16:47:31 -0700578 x = Math.min(x, mFreeScrollMaxScrollX);
579 x = Math.max(x, mFreeScrollMinScrollX);
580 }
581
Sunny Goyal70660032015-05-14 00:07:08 -0700582 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
583 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700584 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700585 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800586 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700587 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700588 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700589 overScroll(x - mMaxScrollX);
590 } else {
591 overScroll(x);
592 }
Adam Cohen68d73932010-11-15 10:50:58 -0800593 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700594 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700595 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800596 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700597 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700598 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700599 overScroll(x);
600 } else {
601 overScroll(x - mMaxScrollX);
602 }
Adam Cohen68d73932010-11-15 10:50:58 -0800603 }
604 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700605 if (mWasInOverscroll) {
606 overScroll(0);
607 mWasInOverscroll = false;
608 }
Adam Cohen68d73932010-11-15 10:50:58 -0800609 super.scrollTo(x, y);
610 }
611
Michael Jurka0142d492010-08-25 17:46:15 -0700612 mTouchX = x;
613 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700614
615 // Update the last motion events when scrolling
616 if (isReordering(true)) {
617 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
618 mLastMotionX = p[0];
619 mLastMotionY = p[1];
620 updateDragViewTranslationDuringDrag();
621 }
Michael Jurka0142d492010-08-25 17:46:15 -0700622 }
623
Adam Cohen53805212013-10-01 10:39:23 -0700624 private void sendScrollAccessibilityEvent() {
625 AccessibilityManager am =
626 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
627 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700628 if (mCurrentPage != getNextPage()) {
629 AccessibilityEvent ev =
630 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700631 ev.setScrollable(true);
632 ev.setScrollX(getScrollX());
633 ev.setScrollY(getScrollY());
634 ev.setMaxScrollX(mMaxScrollX);
635 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700636
Vadim Tryshevf4715972015-05-08 17:21:03 -0700637 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700638 }
Adam Cohen53805212013-10-01 10:39:23 -0700639 }
640 }
641
Michael Jurka0142d492010-08-25 17:46:15 -0700642 // we moved this functionality to a helper function so SmoothPagedView can reuse it
643 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700647 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700648 float scaleX = mFreeScroll ? getScaleX() : 1f;
649 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
650 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700651 }
Michael Jurka0142d492010-08-25 17:46:15 -0700652 invalidate();
653 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700654 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700655 sendScrollAccessibilityEvent();
656
Adam Cohen6f127a62014-06-12 14:54:41 -0700657 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700658 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700659 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700660
Adam Cohen73aa9752010-11-24 16:26:58 -0800661 // We don't want to trigger a page end moving unless the page has settled
662 // and the user has stopped scrolling
663 if (mTouchState == TOUCH_STATE_REST) {
664 pageEndMoving();
665 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700666
Adam Cohen7d30a372013-07-01 17:03:59 -0700667 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700668 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700669 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700670 if (am.isEnabled()) {
671 // Notify the user when the page changes
672 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700673 }
Michael Jurka0142d492010-08-25 17:46:15 -0700674 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 }
Michael Jurka0142d492010-08-25 17:46:15 -0700676 return false;
677 }
678
679 @Override
680 public void computeScroll() {
681 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 }
683
Adam Cohen96d30a12013-07-16 18:13:21 -0700684 public static class LayoutParams extends ViewGroup.LayoutParams {
685 public boolean isFullScreenPage = false;
686
687 /**
688 * {@inheritDoc}
689 */
690 public LayoutParams(int width, int height) {
691 super(width, height);
692 }
693
Sunny Goyal41b22c02015-05-14 15:20:48 -0700694 public LayoutParams(Context context, AttributeSet attrs) {
695 super(context, attrs);
696 }
697
Adam Cohen96d30a12013-07-16 18:13:21 -0700698 public LayoutParams(ViewGroup.LayoutParams source) {
699 super(source);
700 }
701 }
702
Sunny Goyal41b22c02015-05-14 15:20:48 -0700703 @Override
704 public LayoutParams generateLayoutParams(AttributeSet attrs) {
705 return new LayoutParams(getContext(), attrs);
706 }
707
708 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700709 protected LayoutParams generateDefaultLayoutParams() {
710 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
711 }
712
Sunny Goyal41b22c02015-05-14 15:20:48 -0700713 @Override
714 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
715 return new LayoutParams(p);
716 }
717
718 @Override
719 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
720 return p instanceof LayoutParams;
721 }
722
Adam Cohenedb40762013-07-18 16:45:45 -0700723 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700724 LayoutParams lp = generateDefaultLayoutParams();
725 lp.isFullScreenPage = true;
726 super.addView(page, 0, lp);
727 }
728
Adam Cohen410f3cd2013-09-22 12:09:32 -0700729 public int getNormalChildHeight() {
730 return mNormalChildHeight;
731 }
732
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 @Override
734 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700735 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700736 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
737 return;
738 }
739
Adam Cohen7d30a372013-07-01 17:03:59 -0700740 // We measure the dimensions of the PagedView to be larger than the pages so that when we
741 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700742 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
743 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
744 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700745 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800746 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700747 // viewport, we can be at most one and a half screens offset once we scale down
748 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700749 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
750 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700751
Winson Chungc82d2622013-11-06 13:23:29 -0800752 int parentWidthSize = (int) (2f * maxSize);
753 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700754 int scaledWidthSize, scaledHeightSize;
755 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700756 scaledWidthSize = (int) (parentWidthSize / mMinScale);
757 scaledHeightSize = (int) (parentHeightSize / mMinScale);
758 } else {
759 scaledWidthSize = widthSize;
760 scaledHeightSize = heightSize;
761 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700762 mViewport.set(0, 0, widthSize, heightSize);
763
764 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
765 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
766 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 }
768
Winson Chung8aad6102012-05-11 16:27:49 -0700769 // Return early if we aren't given a proper dimension
770 if (widthSize <= 0 || heightSize <= 0) {
771 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
772 return;
773 }
774
Adam Lesinski6b879f02010-11-04 16:15:23 -0700775 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
776 * of the All apps view on XLarge displays to not take up more space then it needs. Width
777 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
778 * each page to have the same width.
779 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 final int verticalPadding = getPaddingTop() + getPaddingBottom();
781 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700782
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700783 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700785 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700786 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700787 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
788 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
789 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
790 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 final int childCount = getChildCount();
792 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700793 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700794 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100795 if (child.getVisibility() != GONE) {
796 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700797
Vladimir Marko2824b072013-10-04 16:42:17 +0100798 int childWidthMode;
799 int childHeightMode;
800 int childWidth;
801 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700802
Vladimir Marko2824b072013-10-04 16:42:17 +0100803 if (!lp.isFullScreenPage) {
804 if (lp.width == LayoutParams.WRAP_CONTENT) {
805 childWidthMode = MeasureSpec.AT_MOST;
806 } else {
807 childWidthMode = MeasureSpec.EXACTLY;
808 }
809
810 if (lp.height == LayoutParams.WRAP_CONTENT) {
811 childHeightMode = MeasureSpec.AT_MOST;
812 } else {
813 childHeightMode = MeasureSpec.EXACTLY;
814 }
815
Adam Cohen3b185e22013-10-29 14:45:58 -0700816 childWidth = getViewportWidth() - horizontalPadding
817 - mInsets.left - mInsets.right;
818 childHeight = getViewportHeight() - verticalPadding
819 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100820 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700821 } else {
822 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700823 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100824
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700825 childWidth = getViewportWidth() - mInsets.left
826 - (mIgnoreRightInset ? mInsets.right : 0);
Adam Cohen3b185e22013-10-29 14:45:58 -0700827 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700828 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700829 if (referenceChildWidth == 0) {
830 referenceChildWidth = childWidth;
831 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700832
Vladimir Marko2824b072013-10-04 16:42:17 +0100833 final int childWidthMeasureSpec =
834 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
835 final int childHeightMeasureSpec =
836 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
837 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700838 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700839 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700840 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800841 }
842
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700844 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700845 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700846 return;
847 }
848
Winson Chung785d2eb2011-04-14 16:08:02 -0700849 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700851
Adam Cohen7d30a372013-07-01 17:03:59 -0700852 int offsetX = getViewportOffsetX();
853 int offsetY = getViewportOffsetY();
854
855 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700856 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700857
Sunny Goyal70660032015-05-14 00:07:08 -0700858 final int startIndex = mIsRtl ? childCount - 1 : 0;
859 final int endIndex = mIsRtl ? -1 : childCount;
860 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700861
Adam Cohen7d30a372013-07-01 17:03:59 -0700862 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700863
Adam Cohen3b185e22013-10-29 14:45:58 -0700864 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000865 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700866
867 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700868 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
869 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700870 }
871
Adam Cohen0ffac432013-07-10 11:19:26 -0700872 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700873 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700875 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100876 int childTop;
877 if (lp.isFullScreenPage) {
878 childTop = offsetY;
879 } else {
880 childTop = offsetY + getPaddingTop() + mInsets.top;
881 if (mCenterPagesVertically) {
882 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
883 }
884 }
885
Adam Cohen96d30a12013-07-16 18:13:21 -0700886 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700887 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800888
Winson Chung785d2eb2011-04-14 16:08:02 -0700889 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700890 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800891 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700892
Adam Cohen3b185e22013-10-29 14:45:58 -0700893 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
894 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000895
896 int pageGap = mPageSpacing;
897 int next = i + delta;
898 if (next != endIndex) {
899 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
900 } else {
901 nextLp = null;
902 }
903
904 // Prevent full screen pages from showing in the viewport
905 // when they are not the current page.
906 if (lp.isFullScreenPage) {
907 pageGap = getPaddingLeft();
908 } else if (nextLp != null && nextLp.isFullScreenPage) {
909 pageGap = getPaddingRight();
910 }
911
Sunny Goyala1fbd842015-05-20 13:40:27 -0700912 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700913 }
914 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700915
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700916 final LayoutTransition transition = getLayoutTransition();
917 // If the transition is running defer updating max scroll, as some empty pages could
918 // still be present, and a max scroll change could cause sudden jumps in scroll.
919 if (transition != null && transition.isRunning()) {
920 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
921
922 @Override
923 public void startTransition(LayoutTransition transition, ViewGroup container,
924 View view, int transitionType) { }
925
926 @Override
927 public void endTransition(LayoutTransition transition, ViewGroup container,
928 View view, int transitionType) {
929 // Wait until all transitions are complete.
930 if (!transition.isRunning()) {
931 transition.removeTransitionListener(this);
932 updateMaxScrollX();
933 }
934 }
935 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700936 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700937 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700938 }
939
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700940 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
941 updateCurrentPageScroll();
942 mFirstLayout = false;
943 }
944
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700945 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700946 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700947 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700948 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700949 } else {
950 setCurrentPage(getNextPage());
951 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700952 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700953 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700954
955 if (isReordering(true)) {
956 updateDragViewTranslationDuringDrag();
957 }
Winson Chunge3193b92010-09-10 11:44:42 -0700958 }
959
Sunny Goyala1fbd842015-05-20 13:40:27 -0700960 protected int getChildGap() {
961 return 0;
962 }
963
Sunny Goyal316490e2015-06-02 09:38:28 -0700964 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700965 int childCount = getChildCount();
966 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700967 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700968 mMaxScrollX = getScrollForPage(index);
969 } else {
970 mMaxScrollX = 0;
971 }
972 }
973
Adam Cohen3b185e22013-10-29 14:45:58 -0700974 public void setPageSpacing(int pageSpacing) {
975 mPageSpacing = pageSpacing;
976 requestLayout();
977 }
978
Sunny Goyal4d113a52015-05-27 10:05:28 -0700979 /**
980 * Called when the center screen changes during scrolling.
981 */
982 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700983
Winson Chunge3193b92010-09-10 11:44:42 -0700984 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700985 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700986 // Update the page indicator, we don't update the page indicator as we
987 // add/remove pages
988 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700989 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700990 mPageIndicator.addMarker(pageIndex,
991 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700992 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700993 }
994
Adam Cohen2591f6a2011-10-25 14:36:40 -0700995 // This ensures that when children are added, they get the correct transforms / alphas
996 // in accordance with any scroll effects.
997 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700998 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700999 invalidate();
1000 }
1001
Michael Jurka8b805b12012-04-18 14:23:14 -07001002 @Override
1003 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001004 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001005 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001006 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001007 }
1008
Winson Chungd2be3812013-07-16 11:11:32 -07001009 private void removeMarkerForView(int index) {
1010 // Update the page indicator, we don't update the page indicator as we
1011 // add/remove pages
1012 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001013 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001014 }
1015 }
1016
1017 @Override
1018 public void removeView(View v) {
1019 // XXX: We should find a better way to hook into this before the view
1020 // gets removed form its parent...
1021 removeMarkerForView(indexOfChild(v));
1022 super.removeView(v);
1023 }
1024 @Override
1025 public void removeViewInLayout(View v) {
1026 // XXX: We should find a better way to hook into this before the view
1027 // gets removed form its parent...
1028 removeMarkerForView(indexOfChild(v));
1029 super.removeViewInLayout(v);
1030 }
1031 @Override
1032 public void removeViewAt(int index) {
1033 // XXX: We should find a better way to hook into this before the view
1034 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001035 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001036 super.removeViewAt(index);
1037 }
1038 @Override
1039 public void removeAllViewsInLayout() {
1040 // Update the page indicator, we don't update the page indicator as we
1041 // add/remove pages
1042 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001043 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001044 }
1045
1046 super.removeAllViewsInLayout();
1047 }
1048
Adam Cohen73894962011-10-31 13:17:17 -07001049 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001050 if (index < 0 || index > getChildCount() - 1) return 0;
1051
Adam Cohenf698c6e2013-07-17 18:44:25 -07001052 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001053
Adam Cohenf698c6e2013-07-17 18:44:25 -07001054 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001055 }
1056
Adam Cohen6f127a62014-06-12 14:54:41 -07001057 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001058 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001059 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001060 }
1061
Michael Jurkadde558b2011-11-09 22:09:06 -08001062 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001063 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001064 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001065
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001066 range[0] = -1;
1067 range[1] = -1;
1068
Michael Jurkac4fb9172010-09-02 17:19:20 -07001069 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001070 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001071 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001072
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001073 int count = getChildCount();
1074 for (int i = 0; i < count; i++) {
1075 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001076
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001077 sTmpIntPoint[0] = 0;
1078 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1079 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001080 if (range[0] == -1) {
1081 continue;
1082 } else {
1083 break;
1084 }
1085 }
1086
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001087 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1088 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1089 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001090 if (range[0] == -1) {
1091 continue;
1092 } else {
1093 break;
1094 }
1095 }
1096 curScreen = i;
1097 if (range[0] < 0) {
1098 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001099 }
1100 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001101
1102 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001103 } else {
1104 range[0] = -1;
1105 range[1] = -1;
1106 }
1107 }
1108
Michael Jurka920d7f42012-05-14 16:29:55 -07001109 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001110 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001111 }
1112
Michael Jurkadde558b2011-11-09 22:09:06 -08001113 @Override
1114 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001115 // Find out which screens are visible; as an optimization we only call draw on them
1116 final int pageCount = getChildCount();
1117 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001118 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001119 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001120
1121 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1122 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1123 // set it for the next frame
1124 mForceScreenScrolled = false;
1125 screenScrolled(screenCenter);
1126 mLastScreenCenter = screenCenter;
1127 }
1128
Michael Jurkadde558b2011-11-09 22:09:06 -08001129 getVisiblePages(mTempVisiblePagesRange);
1130 final int leftScreen = mTempVisiblePagesRange[0];
1131 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001132 if (leftScreen != -1 && rightScreen != -1) {
1133 final long drawingTime = getDrawingTime();
1134 // Clip to the bounds
1135 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001136 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1137 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001138
Adam Cohen7d30a372013-07-01 17:03:59 -07001139 // Draw all the children, leaving the drag view for last
1140 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001141 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001142 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001143 if (mForceDrawAllChildrenNextFrame ||
1144 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001145 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001146 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001147 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001148 // Draw the drag view on top (if there is one)
1149 if (mDragView != null) {
1150 drawChild(canvas, mDragView, drawingTime);
1151 }
1152
Michael Jurka5e368ff2012-05-14 23:13:15 -07001153 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001154 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001155 }
Michael Jurka0142d492010-08-25 17:46:15 -07001156 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 }
1158
1159 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001160 public void draw(Canvas canvas) {
1161 super.draw(canvas);
1162 if (getPageCount() > 0) {
1163 if (!mEdgeGlowLeft.isFinished()) {
1164 final int restoreCount = canvas.save();
1165 Rect display = mViewport;
1166 canvas.translate(display.left, display.top);
1167 canvas.rotate(270);
1168
1169 getEdgeVerticalPostion(sTmpIntPoint);
1170 canvas.translate(display.top - sTmpIntPoint[1], 0);
1171 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1172 if (mEdgeGlowLeft.draw(canvas)) {
1173 postInvalidateOnAnimation();
1174 }
1175 canvas.restoreToCount(restoreCount);
1176 }
1177 if (!mEdgeGlowRight.isFinished()) {
1178 final int restoreCount = canvas.save();
1179 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001180 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001181 canvas.rotate(90);
1182
1183 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001184
1185 int width = mIgnoreRightInset ? (display.width() - mInsets.right) : display.width();
1186 canvas.translate(sTmpIntPoint[0] - display.top, -width);
1187 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], width);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001188 if (mEdgeGlowRight.draw(canvas)) {
1189 postInvalidateOnAnimation();
1190 }
1191 canvas.restoreToCount(restoreCount);
1192 }
1193 }
1194 }
1195
1196 /**
1197 * Returns the top and bottom position for the edge effect.
1198 */
1199 protected abstract void getEdgeVerticalPostion(int[] pos);
1200
1201 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001203 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001204 if (page != mCurrentPage || !mScroller.isFinished()) {
1205 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 return true;
1207 }
1208 return false;
1209 }
1210
1211 @Override
1212 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001213 int focusablePage;
1214 if (mNextPage != INVALID_PAGE) {
1215 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001217 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 }
Winson Chung86f77532010-08-24 11:08:22 -07001219 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001221 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223 return false;
1224 }
1225
1226 @Override
1227 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001228 if (super.dispatchUnhandledMove(focused, direction)) {
1229 return true;
1230 }
1231
1232 if (mIsRtl) {
1233 if (direction == View.FOCUS_LEFT) {
1234 direction = View.FOCUS_RIGHT;
1235 } else if (direction == View.FOCUS_RIGHT) {
1236 direction = View.FOCUS_LEFT;
1237 }
1238 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001240 if (getCurrentPage() > 0) {
1241 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 return true;
1243 }
1244 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001245 if (getCurrentPage() < getPageCount() - 1) {
1246 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 return true;
1248 }
1249 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001250 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
1252
1253 @Override
1254 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001255 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001256 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001257 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 }
1259 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001260 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001261 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 }
1263 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001264 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001265 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267 }
1268 }
1269
1270 /**
1271 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001272 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 *
Winson Chung86f77532010-08-24 11:08:22 -07001274 * This happens when live folders requery, and if they're off page, they
1275 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 */
1277 @Override
1278 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001279 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 View v = focused;
1281 while (true) {
1282 if (v == current) {
1283 super.focusableViewAvailable(focused);
1284 return;
1285 }
1286 if (v == this) {
1287 return;
1288 }
1289 ViewParent parent = v.getParent();
1290 if (parent instanceof View) {
1291 v = (View)v.getParent();
1292 } else {
1293 return;
1294 }
1295 }
1296 }
1297
1298 /**
1299 * {@inheritDoc}
1300 */
1301 @Override
1302 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1303 if (disallowIntercept) {
1304 // We need to make sure to cancel our long press if
1305 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001306 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001307 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 }
1309 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1310 }
1311
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001312 /**
1313 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1314 */
1315 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001316 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001317 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001318 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001319 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001320 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001321 }
1322
1323 /**
1324 * Return true if a tap at (x, y) should trigger a flip to the next page.
1325 */
1326 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001327 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001328 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001329 }
1330 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001331 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001332 }
Winson Chung52aee602013-01-30 12:01:02 -08001333
Adam Cohen7d30a372013-07-01 17:03:59 -07001334 /** Returns whether x and y originated within the buffered viewport */
1335 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001336 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001338 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001339 }
1340
Winson Chung321e9ee2010-08-09 13:37:56 -07001341 @Override
1342 public boolean onInterceptTouchEvent(MotionEvent ev) {
1343 /*
1344 * This method JUST determines whether we want to intercept the motion.
1345 * If we return true, onTouchEvent will be called and we do the actual
1346 * scrolling there.
1347 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001348 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001349
Winson Chung45e1d6e2010-11-09 17:19:49 -08001350 // Skip touch handling if there are no pages to swipe
1351 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1352
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 /*
1354 * Shortcut the most recurring case: the user is in the dragging
1355 * state and he is moving his finger. We want to intercept this
1356 * motion.
1357 */
1358 final int action = ev.getAction();
1359 if ((action == MotionEvent.ACTION_MOVE) &&
1360 (mTouchState == TOUCH_STATE_SCROLLING)) {
1361 return true;
1362 }
1363
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 switch (action & MotionEvent.ACTION_MASK) {
1365 case MotionEvent.ACTION_MOVE: {
1366 /*
1367 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1368 * whether the user has moved far enough from his original down touch.
1369 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001370 if (mActivePointerId != INVALID_POINTER) {
1371 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001372 }
1373 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1374 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1375 // i.e. fall through to the next case (don't break)
1376 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1377 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001378 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 }
1380
1381 case MotionEvent.ACTION_DOWN: {
1382 final float x = ev.getX();
1383 final float y = ev.getY();
1384 // Remember location of down touch
1385 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001386 mDownMotionY = y;
1387 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 mLastMotionX = x;
1389 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 float[] p = mapPointFromViewToParent(this, x, y);
1391 mParentDownMotionX = p[0];
1392 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001393 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001394 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001396
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 /*
1398 * If being flinged and user touches the screen, initiate drag;
1399 * otherwise don't. mScroller.isFinished should be false when
1400 * being flinged.
1401 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001402 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001403 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001404
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001405 if (finishedScrolling) {
1406 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001407 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001408 setCurrentPage(getNextPage());
1409 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001410 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001412 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1413 mTouchState = TOUCH_STATE_SCROLLING;
1414 } else {
1415 mTouchState = TOUCH_STATE_REST;
1416 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001417 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001418
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 break;
1420 }
1421
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001423 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001424 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 break;
1426
1427 case MotionEvent.ACTION_POINTER_UP:
1428 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001429 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 break;
1431 }
1432
1433 /*
1434 * The only time we want to intercept motion events is if we are in the
1435 * drag mode.
1436 */
1437 return mTouchState != TOUCH_STATE_REST;
1438 }
1439
Adam Cohenf8d28232011-02-01 21:47:00 -08001440 protected void determineScrollingStart(MotionEvent ev) {
1441 determineScrollingStart(ev, 1.0f);
1442 }
1443
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 /*
1445 * Determines if we should change the touch state to start scrolling after the
1446 * user moves their touch point too far.
1447 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001448 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001449 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001451 if (pointerIndex == -1) return;
1452
1453 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 final float x = ev.getX(pointerIndex);
1455 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001456 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1457
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001459
Adam Cohenf8d28232011-02-01 21:47:00 -08001460 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001462
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001463 if (xMoved) {
1464 // Scroll if the user moved far enough along the X axis
1465 mTouchState = TOUCH_STATE_SCROLLING;
1466 mTotalMotionX += Math.abs(mLastMotionX - x);
1467 mLastMotionX = x;
1468 mLastMotionXRemainder = 0;
1469 mTouchX = getViewportOffsetX() + getScrollX();
1470 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1471 onScrollInteractionBegin();
1472 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001473 }
1474 }
1475
1476 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001477 // Try canceling the long press. It could also have been scheduled
1478 // by a distant descendant, so use the mAllowLongPress flag to block
1479 // everything
1480 final View currentPage = getPageAt(mCurrentPage);
1481 if (currentPage != null) {
1482 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 }
1484 }
1485
Adam Cohenb5ba0972011-09-07 18:02:31 -07001486 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001487 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001488
Adam Cohenedb40762013-07-18 16:45:45 -07001489 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001490 int count = getChildCount();
1491
1492 final int totalDistance;
1493
1494 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001495 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001496 adjacentPage = page - 1;
1497 }
1498
1499 if (adjacentPage < 0 || adjacentPage > count - 1) {
1500 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1501 } else {
1502 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1503 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001504
1505 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001506 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1507 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001508 return scrollProgress;
1509 }
1510
Adam Cohenedb40762013-07-18 16:45:45 -07001511 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001512 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001513 return 0;
1514 } else {
1515 return mPageScrolls[index];
1516 }
1517 }
1518
Adam Cohen564a2e72013-10-09 14:47:32 -07001519 // While layout transitions are occurring, a child's position may stray from its baseline
1520 // position. This method returns the magnitude of this stray at any given time.
1521 public int getLayoutTransitionOffsetForPage(int index) {
1522 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1523 return 0;
1524 } else {
1525 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001526
1527 int scrollOffset = 0;
1528 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1529 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001530 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001531 }
1532
Adam Cohen564a2e72013-10-09 14:47:32 -07001533 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1534 return (int) (child.getX() - baselineX);
1535 }
1536 }
1537
Adam Cohenb5ba0972011-09-07 18:02:31 -07001538 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001539 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001540 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001541 if (f < 0) {
1542 mEdgeGlowLeft.onPull(-f);
1543 } else if (f > 0) {
1544 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001545 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001546 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001547 }
1548 invalidate();
1549 }
1550
Adam Cohenb5ba0972011-09-07 18:02:31 -07001551 protected void overScroll(float amount) {
1552 dampedOverScroll(amount);
1553 }
1554
Winson Chungdc61c4d2015-04-20 18:26:57 -07001555 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001556 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001557 }
1558
Winson Chungdc61c4d2015-04-20 18:26:57 -07001559 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001560 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001561 }
1562
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001563 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001564 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001565 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001566 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1567 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1568 } else {
1569 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1570 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1571 }
1572 }
1573
Adam Cohenf9618852013-11-08 06:45:03 -08001574 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001575 mFreeScroll = freeScroll;
1576
Adam Cohenf9618852013-11-08 06:45:03 -08001577 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001578 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001579 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001580 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1581 setCurrentPage(mTempVisiblePagesRange[0]);
1582 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1583 setCurrentPage(mTempVisiblePagesRange[1]);
1584 }
1585 }
1586
1587 setEnableOverscroll(!freeScroll);
1588 }
1589
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001590 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001591 mAllowOverScroll = enable;
1592 }
1593
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001594 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001595 if (mDragView != null) {
1596 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1597 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001598 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001599 int minDistance = Integer.MAX_VALUE;
1600 int minIndex = indexOfChild(mDragView);
1601 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1602 View page = getPageAt(i);
1603 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1604 int d = Math.abs(dragX - pageX);
1605 if (d < minDistance) {
1606 minIndex = i;
1607 minDistance = d;
1608 }
1609 }
1610 return minIndex;
1611 }
1612 return -1;
1613 }
1614
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 @Override
1616 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001617 super.onTouchEvent(ev);
1618
Winson Chung45e1d6e2010-11-09 17:19:49 -08001619 // Skip touch handling if there are no pages to swipe
1620 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1621
Michael Jurkab8f06722010-10-10 15:58:46 -07001622 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001623
1624 final int action = ev.getAction();
1625
1626 switch (action & MotionEvent.ACTION_MASK) {
1627 case MotionEvent.ACTION_DOWN:
1628 /*
1629 * If being flinged and user touches, stop the fling. isFinished
1630 * will be false if being flinged.
1631 */
1632 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001633 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 }
1635
1636 // Remember where the motion event started
1637 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001638 mDownMotionY = mLastMotionY = ev.getY();
1639 mDownScrollX = getScrollX();
1640 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1641 mParentDownMotionX = p[0];
1642 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001643 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001644 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001645 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001646
Michael Jurka0142d492010-08-25 17:46:15 -07001647 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001648 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001649 pageBeginMoving();
1650 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001651 break;
1652
1653 case MotionEvent.ACTION_MOVE:
1654 if (mTouchState == TOUCH_STATE_SCROLLING) {
1655 // Scroll to follow the motion event
1656 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001657
1658 if (pointerIndex == -1) return true;
1659
Winson Chung321e9ee2010-08-09 13:37:56 -07001660 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001661 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001662
Adam Cohenaefd4e12011-02-14 16:39:38 -08001663 mTotalMotionX += Math.abs(deltaX);
1664
Winson Chungc0844aa2011-02-02 15:25:58 -08001665 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1666 // keep the remainder because we are actually testing if we've moved from the last
1667 // scrolled position (which is discrete).
1668 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001669 mTouchX += deltaX;
1670 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001671 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001672 mLastMotionX = x;
1673 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001674 } else {
1675 awakenScrollBars();
1676 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1678 // Update the last motion position
1679 mLastMotionX = ev.getX();
1680 mLastMotionY = ev.getY();
1681
1682 // Update the parent down so that our zoom animations take this new movement into
1683 // account
1684 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1685 mParentDownMotionX = pt[0];
1686 mParentDownMotionY = pt[1];
1687 updateDragViewTranslationDuringDrag();
1688
1689 // Find the closest page to the touch point
1690 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001691
Adam Cohen7d30a372013-07-01 17:03:59 -07001692 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1693 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1694 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1695 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1696
Adam Cohenf358a4b2013-07-23 16:47:31 -07001697 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001698 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001699 mTempVisiblePagesRange[0] = 0;
1700 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001701 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001702 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1703 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1704 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1705 mSidePageHoverIndex = pageUnderPointIndex;
1706 mSidePageHoverRunnable = new Runnable() {
1707 @Override
1708 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001709 // Setup the scroll to the correct page before we swap the views
1710 snapToPage(pageUnderPointIndex);
1711
1712 // For each of the pages between the paged view and the drag view,
1713 // animate them from the previous position to the new position in
1714 // the layout (as a result of the drag view moving in the layout)
1715 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1716 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1717 dragViewIndex + 1 : pageUnderPointIndex;
1718 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1719 dragViewIndex - 1 : pageUnderPointIndex;
1720 for (int i = lowerIndex; i <= upperIndex; ++i) {
1721 View v = getChildAt(i);
1722 // dragViewIndex < pageUnderPointIndex, so after we remove the
1723 // drag view all subsequent views to pageUnderPointIndex will
1724 // shift down.
1725 int oldX = getViewportOffsetX() + getChildOffset(i);
1726 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1727
1728 // Animate the view translation from its old position to its new
1729 // position
1730 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1731 if (anim != null) {
1732 anim.cancel();
1733 }
1734
1735 v.setTranslationX(oldX - newX);
1736 anim = new AnimatorSet();
1737 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1738 anim.playTogether(
1739 ObjectAnimator.ofFloat(v, "translationX", 0f));
1740 anim.start();
1741 v.setTag(anim);
1742 }
1743
1744 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001745 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001746 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001747 if (mPageIndicator != null) {
1748 mPageIndicator.setActiveMarker(getNextPage());
1749 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001750 }
1751 };
1752 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1753 }
1754 } else {
1755 removeCallbacks(mSidePageHoverRunnable);
1756 mSidePageHoverIndex = -1;
1757 }
Adam Cohen564976a2010-10-13 18:52:07 -07001758 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001759 determineScrollingStart(ev);
1760 }
1761 break;
1762
1763 case MotionEvent.ACTION_UP:
1764 if (mTouchState == TOUCH_STATE_SCROLLING) {
1765 final int activePointerId = mActivePointerId;
1766 final int pointerIndex = ev.findPointerIndex(activePointerId);
1767 final float x = ev.getX(pointerIndex);
1768 final VelocityTracker velocityTracker = mVelocityTracker;
1769 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1770 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001771 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001772 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001773 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1774 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001775
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001776 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1777
Adam Cohen00481b32011-11-18 12:03:48 -08001778 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001779 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001780
Adam Cohenf358a4b2013-07-23 16:47:31 -07001781 if (!mFreeScroll) {
1782 // In the case that the page is moved far to one direction and then is flung
1783 // in the opposite direction, we use a threshold to determine whether we should
1784 // just return to the starting page, or if we should skip one further.
1785 boolean returnToOriginalPage = false;
1786 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1787 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1788 returnToOriginalPage = true;
1789 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001790
Adam Cohenf358a4b2013-07-23 16:47:31 -07001791 int finalPage;
1792 // We give flings precedence over large moves, which is why we short-circuit our
1793 // test for a large move if a fling has been registered. That is, a large
1794 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001795 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1796 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001797 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1798 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1799 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1800 snapToPageWithVelocity(finalPage, velocityX);
1801 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1802 (isFling && isVelocityXLeft)) &&
1803 mCurrentPage < getChildCount() - 1) {
1804 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1805 snapToPageWithVelocity(finalPage, velocityX);
1806 } else {
1807 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001808 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001809 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001810 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001811 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001812 }
1813
1814 float scaleX = getScaleX();
1815 int vX = (int) (-velocityX * scaleX);
1816 int initialScrollX = (int) (getScrollX() * scaleX);
1817
Adam Cohenf9618852013-11-08 06:45:03 -08001818 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001819 mScroller.fling(initialScrollX,
1820 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1821 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001822 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001823 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001824 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1825 // at this point we have not moved beyond the touch slop
1826 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1827 // we can just page
1828 int nextPage = Math.max(0, mCurrentPage - 1);
1829 if (nextPage != mCurrentPage) {
1830 snapToPage(nextPage);
1831 } else {
1832 snapToDestination();
1833 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001834 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001835 // at this point we have not moved beyond the touch slop
1836 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1837 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001838 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1839 if (nextPage != mCurrentPage) {
1840 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001841 } else {
1842 snapToDestination();
1843 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001844 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1845 // Update the last motion position
1846 mLastMotionX = ev.getX();
1847 mLastMotionY = ev.getY();
1848
1849 // Update the parent down so that our zoom animations take this new movement into
1850 // account
1851 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1852 mParentDownMotionX = pt[0];
1853 mParentDownMotionY = pt[1];
1854 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001855 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001856 if (!mCancelTap) {
1857 onUnhandledTap(ev);
1858 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001859 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001860
1861 // Remove the callback to wait for the side page hover timeout
1862 removeCallbacks(mSidePageHoverRunnable);
1863 // End any intermediate reordering states
1864 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001865 break;
1866
1867 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001868 if (mTouchState == TOUCH_STATE_SCROLLING) {
1869 snapToDestination();
1870 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001871 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001872 break;
1873
1874 case MotionEvent.ACTION_POINTER_UP:
1875 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001876 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001877 break;
1878 }
1879
1880 return true;
1881 }
1882
Adam Cohen7d30a372013-07-01 17:03:59 -07001883 private void resetTouchState() {
1884 releaseVelocityTracker();
1885 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001886 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001887 mTouchState = TOUCH_STATE_REST;
1888 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001889 mEdgeGlowLeft.onRelease();
1890 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001891 }
1892
Adam Cohenc2d6e892014-10-16 09:49:24 -07001893 /**
1894 * Triggered by scrolling via touch
1895 */
1896 protected void onScrollInteractionBegin() {
1897 }
1898
1899 protected void onScrollInteractionEnd() {
1900 }
1901
Adam Cohen1697b792013-09-17 19:08:21 -07001902 protected void onUnhandledTap(MotionEvent ev) {
1903 ((Launcher) getContext()).onClick(this);
1904 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001905
Winson Chung185d7162011-02-28 13:47:29 -08001906 @Override
1907 public boolean onGenericMotionEvent(MotionEvent event) {
1908 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1909 switch (event.getAction()) {
1910 case MotionEvent.ACTION_SCROLL: {
1911 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1912 final float vscroll;
1913 final float hscroll;
1914 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1915 vscroll = 0;
1916 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1917 } else {
1918 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1919 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1920 }
1921 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001922 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001923 : (hscroll > 0 || vscroll > 0);
1924 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001925 scrollRight();
1926 } else {
1927 scrollLeft();
1928 }
1929 return true;
1930 }
1931 }
1932 }
1933 }
1934 return super.onGenericMotionEvent(event);
1935 }
1936
Michael Jurkab8f06722010-10-10 15:58:46 -07001937 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1938 if (mVelocityTracker == null) {
1939 mVelocityTracker = VelocityTracker.obtain();
1940 }
1941 mVelocityTracker.addMovement(ev);
1942 }
1943
1944 private void releaseVelocityTracker() {
1945 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001946 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001947 mVelocityTracker.recycle();
1948 mVelocityTracker = null;
1949 }
1950 }
1951
Winson Chung321e9ee2010-08-09 13:37:56 -07001952 private void onSecondaryPointerUp(MotionEvent ev) {
1953 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1954 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1955 final int pointerId = ev.getPointerId(pointerIndex);
1956 if (pointerId == mActivePointerId) {
1957 // This was our active pointer going up. Choose a new
1958 // active pointer and adjust accordingly.
1959 // TODO: Make this decision more intelligent.
1960 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1961 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1962 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001963 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001964 mActivePointerId = ev.getPointerId(newPointerIndex);
1965 if (mVelocityTracker != null) {
1966 mVelocityTracker.clear();
1967 }
1968 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001969 }
1970
Winson Chung321e9ee2010-08-09 13:37:56 -07001971 @Override
1972 public void requestChildFocus(View child, View focused) {
1973 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001974 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001975 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001976 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001977 }
1978 }
1979
Adam Cohend19d3ca2010-09-15 14:43:42 -07001980 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001981 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001982 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001983 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 final int childCount = getChildCount();
1985 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001986 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001987 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001988 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001989 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001990 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1991 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1992 minDistanceFromScreenCenter = distanceFromScreenCenter;
1993 minDistanceFromScreenCenterIndex = i;
1994 }
1995 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001996 return minDistanceFromScreenCenterIndex;
1997 }
1998
1999 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002000 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002001 }
2002
Adam Cohene0f66b52010-11-23 15:06:07 -08002003 private static class ScrollInterpolator implements Interpolator {
2004 public ScrollInterpolator() {
2005 }
2006
2007 public float getInterpolation(float t) {
2008 t -= 1.0f;
2009 return t*t*t*t*t + 1;
2010 }
2011 }
2012
2013 // We want the duration of the page snap animation to be influenced by the distance that
2014 // the screen has to travel, however, we don't want this duration to be effected in a
2015 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2016 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07002017 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002018 f -= 0.5f; // center the values about 0.
2019 f *= 0.3f * Math.PI / 2.0f;
2020 return (float) Math.sin(f);
2021 }
2022
Michael Jurka0142d492010-08-25 17:46:15 -07002023 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002024 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002026
Adam Cohenedb40762013-07-18 16:45:45 -07002027 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002028 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002029 int duration = 0;
2030
Sunny Goyal4d113a52015-05-27 10:05:28 -07002031 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002032 // If the velocity is low enough, then treat this more as an automatic page advance
2033 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002034 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002035 return;
2036 }
2037
2038 // Here we compute a "distance" that will be used in the computation of the overall
2039 // snap duration. This is a function of the actual distance that needs to be traveled;
2040 // we keep this value close to half screen size in order to reduce the variance in snap
2041 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002042 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002043 float distance = halfScreenSize + halfScreenSize *
2044 distanceInfluenceForSnapDuration(distanceRatio);
2045
2046 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002047 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002048
2049 // we want the page's snap velocity to approximately match the velocity at which the
2050 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002051 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2052 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002053
2054 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002055 }
2056
Sunny Goyal1740d902015-05-27 11:14:01 -07002057 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002058 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002059 }
2060
Adam Cohen7d30a372013-07-01 17:03:59 -07002061 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002062 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002063 }
2064
Michael Jurka0142d492010-08-25 17:46:15 -07002065 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002066 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002067 }
2068
Adam Cohenf9618852013-11-08 06:45:03 -08002069 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2070 snapToPage(whichPage, duration, false, interpolator);
2071 }
2072
2073 protected void snapToPage(int whichPage, int duration, boolean immediate,
2074 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002075 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002076
Adam Cohenedb40762013-07-18 16:45:45 -07002077 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002078 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002079 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002080 }
2081
2082 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002083 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002084 }
Michael Jurka0142d492010-08-25 17:46:15 -07002085
Adam Cohenf9618852013-11-08 06:45:03 -08002086 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2087 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002088 whichPage = validateNewPage(whichPage);
2089
Adam Cohen7d30a372013-07-01 17:03:59 -07002090 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002091
2092 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002093 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002094 if (immediate) {
2095 duration = 0;
2096 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002097 duration = Math.abs(delta);
2098 }
2099
Adam Cohenf358a4b2013-07-23 16:47:31 -07002100 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002101 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002102 }
Adam Cohenf9618852013-11-08 06:45:03 -08002103
2104 if (interpolator != null) {
2105 mScroller.setInterpolator(interpolator);
2106 } else {
2107 mScroller.setInterpolator(mDefaultInterpolator);
2108 }
2109
Sunny Goyal4d113a52015-05-27 10:05:28 -07002110 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002111
Adam Cohen674531f2013-12-13 15:07:14 -08002112 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002113
2114 // Trigger a compute() to finish switching pages if necessary
2115 if (immediate) {
2116 computeScroll();
2117 }
2118
2119 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 invalidate();
2121 }
2122
Winson Chung321e9ee2010-08-09 13:37:56 -07002123 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002124 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002125 }
2126
2127 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002128 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002129 }
2130
Winson Chung86f77532010-08-24 11:08:22 -07002131 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002132 int result = -1;
2133 if (v != null) {
2134 ViewParent vp = v.getParent();
2135 int count = getChildCount();
2136 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002137 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002138 return i;
2139 }
2140 }
2141 }
2142 return result;
2143 }
2144
Adam Cohendbdff6b2013-09-18 19:09:15 -07002145 @Override
2146 public boolean performLongClick() {
2147 mCancelTap = true;
2148 return super.performLongClick();
2149 }
2150
Winson Chung321e9ee2010-08-09 13:37:56 -07002151 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002152 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002153
2154 SavedState(Parcelable superState) {
2155 super(superState);
2156 }
2157
Adam Cohen091440a2015-03-18 14:16:05 -07002158 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002159 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002160 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002161 }
2162
2163 @Override
2164 public void writeToParcel(Parcel out, int flags) {
2165 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002166 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002167 }
2168
2169 public static final Parcelable.Creator<SavedState> CREATOR =
2170 new Parcelable.Creator<SavedState>() {
2171 public SavedState createFromParcel(Parcel in) {
2172 return new SavedState(in);
2173 }
2174
2175 public SavedState[] newArray(int size) {
2176 return new SavedState[size];
2177 }
2178 };
2179 }
2180
Adam Cohen7d30a372013-07-01 17:03:59 -07002181 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002182 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002183 if (mDragView != null) {
2184 AnimatorSet anim = new AnimatorSet();
2185 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2186 anim.playTogether(
2187 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002188 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2189 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2190 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002191 anim.addListener(new AnimatorListenerAdapter() {
2192 @Override
2193 public void onAnimationEnd(Animator animation) {
2194 onPostReorderingAnimationCompleted();
2195 }
2196 });
2197 anim.start();
2198 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002199 }
2200
Sunny Goyal1d08f702015-05-04 15:50:25 -07002201 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2203 mTouchState = TOUCH_STATE_REORDERING;
2204 mIsReordering = true;
2205
Adam Cohen7d30a372013-07-01 17:03:59 -07002206 // We must invalidate to trigger a redraw to update the layers such that the drag view
2207 // is always drawn on top
2208 invalidate();
2209 }
2210
Adam Cohen091440a2015-03-18 14:16:05 -07002211 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002212 // Trigger the callback when reordering has settled
2213 --mPostReorderingPreZoomInRemainingAnimationCount;
2214 if (mPostReorderingPreZoomInRunnable != null &&
2215 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2216 mPostReorderingPreZoomInRunnable.run();
2217 mPostReorderingPreZoomInRunnable = null;
2218 }
2219 }
2220
Sunny Goyal1d08f702015-05-04 15:50:25 -07002221 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002222 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002223 }
2224
Adam Cohenf358a4b2013-07-23 16:47:31 -07002225 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002226 int dragViewIndex = indexOfChild(v);
2227
Adam Cohen327acfe2014-06-06 11:52:52 -07002228 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002229
Adam Cohen7d30a372013-07-01 17:03:59 -07002230 mTempVisiblePagesRange[0] = 0;
2231 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002232 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002233 mReorderingStarted = true;
2234
2235 // Check if we are within the reordering range
2236 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002237 dragViewIndex <= mTempVisiblePagesRange[1]) {
2238 // Find the drag view under the pointer
2239 mDragView = getChildAt(dragViewIndex);
2240 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2241 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002242 snapToPage(getPageNearestToCenterOfScreen());
2243 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002244 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002245 return true;
2246 }
2247 return false;
2248 }
2249
2250 boolean isReordering(boolean testTouchState) {
2251 boolean state = mIsReordering;
2252 if (testTouchState) {
2253 state &= (mTouchState == TOUCH_STATE_REORDERING);
2254 }
2255 return state;
2256 }
2257 void endReordering() {
2258 // For simplicity, we call endReordering sometimes even if reordering was never started.
2259 // In that case, we don't want to do anything.
2260 if (!mReorderingStarted) return;
2261 mReorderingStarted = false;
2262
2263 // If we haven't flung-to-delete the current child, then we just animate the drag view
2264 // back into position
2265 final Runnable onCompleteRunnable = new Runnable() {
2266 @Override
2267 public void run() {
2268 onEndReordering();
2269 }
2270 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002271
2272 mPostReorderingPreZoomInRunnable = new Runnable() {
2273 public void run() {
2274 onCompleteRunnable.run();
2275 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002276 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002277 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002278
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002279 mPostReorderingPreZoomInRemainingAnimationCount =
2280 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2281 // Snap to the current page
2282 snapToPage(indexOfChild(mDragView), 0);
2283 // Animate the drag view back to the front position
2284 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002285 }
2286
Adam Cohen7d30a372013-07-01 17:03:59 -07002287 private static final int ANIM_TAG_KEY = 100;
2288
Winson Chung6a0f57d2011-06-29 20:10:49 -07002289 /* Accessibility */
Sunny Goyal316490e2015-06-02 09:38:28 -07002290 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002291 @Override
2292 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2293 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002294 info.setScrollable(getPageCount() > 1);
2295 if (getCurrentPage() < getPageCount() - 1) {
2296 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2297 }
2298 if (getCurrentPage() > 0) {
2299 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2300 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002301 info.setClassName(getClass().getName());
2302
2303 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2304 // Besides disabling the accessibility long-click, this also prevents this view from getting
2305 // accessibility focus.
2306 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002307 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002308 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2309 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002310 }
2311
2312 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002313 public void sendAccessibilityEvent(int eventType) {
2314 // Don't let the view send real scroll events.
2315 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2316 super.sendAccessibilityEvent(eventType);
2317 }
2318 }
2319
2320 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002321 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2322 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002323 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002324 }
2325
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002326 @Override
2327 public boolean performAccessibilityAction(int action, Bundle arguments) {
2328 if (super.performAccessibilityAction(action, arguments)) {
2329 return true;
2330 }
2331 switch (action) {
2332 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2333 if (getCurrentPage() < getPageCount() - 1) {
2334 scrollRight();
2335 return true;
2336 }
2337 } break;
2338 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2339 if (getCurrentPage() > 0) {
2340 scrollLeft();
2341 return true;
2342 }
2343 } break;
2344 }
2345 return false;
2346 }
2347
Adam Cohen0ffac432013-07-10 11:19:26 -07002348 protected String getCurrentPageDescription() {
2349 return String.format(getContext().getString(R.string.default_scroll_format),
2350 getNextPage() + 1, getChildCount());
2351 }
2352
Winson Chungd11265e2011-08-30 13:37:23 -07002353 @Override
2354 public boolean onHoverEvent(android.view.MotionEvent event) {
2355 return true;
2356 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002357}