blob: e3cdc29a0f98443a2dd3aec16302e55f5432451c [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 Goyal4d113a52015-05-27 10:05:28 -0700205 // Edge effect
206 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
207 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
208
Winson Chung86f77532010-08-24 11:08:22 -0700209 public interface PageSwitchListener {
210 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 }
212
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 public PagedView(Context context) {
214 this(context, null);
215 }
216
217 public PagedView(Context context, AttributeSet attrs) {
218 this(context, attrs, 0);
219 }
220
221 public PagedView(Context context, AttributeSet attrs, int defStyle) {
222 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700223
Adam Cohen9c4949e2010-10-05 12:27:22 -0700224 TypedArray a = context.obtainStyledAttributes(attrs,
225 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700226
Winson Chungdf4b83d2010-10-20 17:49:27 -0700227 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700228 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700229 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700230 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700231 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700232 a.recycle();
233
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700235 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700236 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 }
238
239 /**
240 * Initializes various states for this workspace.
241 */
Michael Jurka0142d492010-08-25 17:46:15 -0700242 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800243 mScroller = new LauncherScroller(getContext());
244 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700245 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700246 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700247
248 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700249 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700251 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800252
253 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
254 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
255 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700256 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700257 setWillNotDraw(false);
258 }
259
260 protected void setEdgeGlowColor(int color) {
261 mEdgeGlowLeft.setColor(color);
262 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 }
264
Adam Cohenf9618852013-11-08 06:45:03 -0800265 protected void setDefaultInterpolator(Interpolator interpolator) {
266 mDefaultInterpolator = interpolator;
267 mScroller.setInterpolator(mDefaultInterpolator);
268 }
269
Winson Chungd2be3812013-07-16 11:11:32 -0700270 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700271 super.onAttachedToWindow();
272
Winson Chungd2be3812013-07-16 11:11:32 -0700273 // Hook up the page indicator
274 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700275 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700276 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700277 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700278 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700279
Winson Chung7819a562013-09-19 15:55:45 -0700280 ArrayList<PageIndicator.PageMarkerResources> markers =
281 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700282 for (int i = 0; i < getChildCount(); ++i) {
283 markers.add(getPageIndicatorMarker(i));
284 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700285
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700286 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700287
288 OnClickListener listener = getPageIndicatorClickListener();
289 if (listener != null) {
290 mPageIndicator.setOnClickListener(listener);
291 }
Adam Cohen53805212013-10-01 10:39:23 -0700292 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700293 }
294 }
295
Adam Cohen53805212013-10-01 10:39:23 -0700296 protected String getPageIndicatorDescription() {
297 return getCurrentPageDescription();
298 }
299
300 protected OnClickListener getPageIndicatorClickListener() {
301 return null;
302 }
303
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700304 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700305 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700306 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700307 // Unhook the page indicator
308 mPageIndicator = null;
309 }
310
Adam Cohen7d30a372013-07-01 17:03:59 -0700311 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700312 private float[] mapPointFromViewToParent(View v, float x, float y) {
313 sTmpPoint[0] = x;
314 sTmpPoint[1] = y;
315 v.getMatrix().mapPoints(sTmpPoint);
316 sTmpPoint[0] += v.getLeft();
317 sTmpPoint[1] += v.getTop();
318 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700319 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700320 private float[] mapPointFromParentToView(View v, float x, float y) {
321 sTmpPoint[0] = x - v.getLeft();
322 sTmpPoint[1] = y - v.getTop();
323 v.getMatrix().invert(sTmpInvMatrix);
324 sTmpInvMatrix.mapPoints(sTmpPoint);
325 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700326 }
327
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700328 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700329 if (mDragView != null) {
330 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
331 (mDragViewBaselineLeft - mDragView.getLeft());
332 float y = mLastMotionY - mDownMotionY;
333 mDragView.setTranslationX(x);
334 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700335
Adam Cohenf358a4b2013-07-23 16:47:31 -0700336 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
337 + x + ", " + y);
338 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700339 }
340
341 public void setMinScale(float f) {
342 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700343 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700344 requestLayout();
345 }
346
347 @Override
348 public void setScaleX(float scaleX) {
349 super.setScaleX(scaleX);
350 if (isReordering(true)) {
351 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
352 mLastMotionX = p[0];
353 mLastMotionY = p[1];
354 updateDragViewTranslationDuringDrag();
355 }
356 }
357
358 // Convenience methods to get the actual width/height of the PagedView (since it is measured
359 // to be larger to account for the minimum possible scale)
360 int getViewportWidth() {
361 return mViewport.width();
362 }
363 int getViewportHeight() {
364 return mViewport.height();
365 }
366
367 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
368 // PagedView both horizontally and vertically
369 int getViewportOffsetX() {
370 return (getMeasuredWidth() - getViewportWidth()) / 2;
371 }
372
373 int getViewportOffsetY() {
374 return (getMeasuredHeight() - getViewportHeight()) / 2;
375 }
376
Adam Cohenedb40762013-07-18 16:45:45 -0700377 PageIndicator getPageIndicator() {
378 return mPageIndicator;
379 }
Winson Chung7819a562013-09-19 15:55:45 -0700380 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
381 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700382 }
Adam Cohenedb40762013-07-18 16:45:45 -0700383
Adam Cohen674531f2013-12-13 15:07:14 -0800384 /**
385 * Add a page change listener which will be called when a page is _finished_ listening.
386 *
387 */
Winson Chung86f77532010-08-24 11:08:22 -0700388 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
389 mPageSwitchListener = pageSwitchListener;
390 if (mPageSwitchListener != null) {
391 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 }
393 }
394
395 /**
Winson Chung86f77532010-08-24 11:08:22 -0700396 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700397 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700398 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700399 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700401
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700402 /**
403 * Returns the index of page to be shown immediately afterwards.
404 */
Winson Chung360e63f2012-04-27 13:48:05 -0700405 int getNextPage() {
406 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
407 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700408
Winson Chung86f77532010-08-24 11:08:22 -0700409 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 return getChildCount();
411 }
412
Sunny Goyal83a8f042015-05-19 12:52:12 -0700413 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700414 return getChildAt(index);
415 }
416
Adam Cohenae4f1552011-10-20 00:15:42 -0700417 protected int indexToPage(int index) {
418 return index;
419 }
420
Winson Chung321e9ee2010-08-09 13:37:56 -0700421 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800422 * Updates the scroll of the current page immediately to its final scroll position. We use this
423 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
424 * the previous tab page.
425 */
426 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700427 // If the current page is invalid, just reset the scroll position to zero
428 int newX = 0;
429 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700430 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700431 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800432 scrollTo(newX, 0);
433 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700434 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800435 }
436
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700437 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700438 mScroller.abortAnimation();
439 // We need to clean up the next page here to avoid computeScrollHelper from
440 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700441 if (resetNextPage) {
442 mNextPage = INVALID_PAGE;
443 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700444 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700445
446 private void forceFinishScroller() {
447 mScroller.forceFinished(true);
448 // We need to clean up the next page here to avoid computeScrollHelper from
449 // updating current page on the pass.
450 mNextPage = INVALID_PAGE;
451 }
452
Adam Cohen6f127a62014-06-12 14:54:41 -0700453 private int validateNewPage(int newPage) {
454 int validatedPage = newPage;
455 // When in free scroll mode, we need to clamp to the free scroll page range.
456 if (mFreeScroll) {
457 getFreeScrollPageRange(mTempVisiblePagesRange);
458 validatedPage = Math.max(mTempVisiblePagesRange[0],
459 Math.min(newPage, mTempVisiblePagesRange[1]));
460 }
461 // Ensure that it is clamped by the actual set of children in all cases
462 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
463 return validatedPage;
464 }
465
Chet Haasebc2f0822012-10-26 17:59:53 -0700466 /**
Winson Chung86f77532010-08-24 11:08:22 -0700467 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700469 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800470 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700471 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800472 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800473 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
474 // the default
475 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800476 return;
477 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700478 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700479 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700480 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700481 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800482 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
484
Winson Chung8c87cd82013-07-23 16:20:10 -0700485 /**
486 * The restore page will be set in place of the current page at the next (likely first)
487 * layout.
488 */
489 void setRestorePage(int restorePage) {
490 mRestorePage = restorePage;
491 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800492 int getRestorePage() {
493 return mRestorePage;
494 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700495
Adam Cohen674531f2013-12-13 15:07:14 -0800496 /**
497 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
498 * has settled.
499 */
Michael Jurka0142d492010-08-25 17:46:15 -0700500 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700501 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800502 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 }
Winson Chungd2be3812013-07-16 11:11:32 -0700504
Adam Cohen674531f2013-12-13 15:07:14 -0800505 updatePageIndicator();
506 }
507
508 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700509 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700510 if (mPageIndicator != null) {
511 mPageIndicator.setContentDescription(getPageIndicatorDescription());
512 if (!isReordering(false)) {
513 mPageIndicator.setActiveMarker(getNextPage());
514 }
Winson Chungd2be3812013-07-16 11:11:32 -0700515 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700516 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800517 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700518 if (!mIsPageMoving) {
519 mIsPageMoving = true;
520 onPageBeginMoving();
521 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700522 }
523
Michael Jurkace7e05f2011-02-01 22:02:35 -0800524 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700525 if (mIsPageMoving) {
526 mIsPageMoving = false;
527 onPageEndMoving();
528 }
Michael Jurka0142d492010-08-25 17:46:15 -0700529 }
530
Adam Cohen26976d92011-03-22 15:33:33 -0700531 protected boolean isPageMoving() {
532 return mIsPageMoving;
533 }
534
Michael Jurka0142d492010-08-25 17:46:15 -0700535 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700536 protected void onPageBeginMoving() {
537 }
538
539 // a method that subclasses can override to add behavior
540 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700541 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700542 }
543
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 /**
Winson Chung86f77532010-08-24 11:08:22 -0700545 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700546 *
547 * @param l The listener used to respond to long clicks.
548 */
549 @Override
550 public void setOnLongClickListener(OnLongClickListener l) {
551 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700552 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700554 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
Adam Cohen1697b792013-09-17 19:08:21 -0700556 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
558
559 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800560 public void scrollBy(int x, int y) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700561 scrollTo(getScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800562 }
563
564 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700565 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700566 // In free scroll mode, we clamp the scrollX
567 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700568 // If the scroller is trying to move to a location beyond the maximum allowed
569 // in the free scroll mode, we make sure to end the scroll operation.
570 if (!mScroller.isFinished() &&
571 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
572 forceFinishScroller();
573 }
574
Adam Cohenf358a4b2013-07-23 16:47:31 -0700575 x = Math.min(x, mFreeScrollMaxScrollX);
576 x = Math.max(x, mFreeScrollMinScrollX);
577 }
578
Sunny Goyal70660032015-05-14 00:07:08 -0700579 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
580 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700581 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700582 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800583 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700584 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700585 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700586 overScroll(x - mMaxScrollX);
587 } else {
588 overScroll(x);
589 }
Adam Cohen68d73932010-11-15 10:50:58 -0800590 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700591 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700592 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800593 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700594 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700595 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700596 overScroll(x);
597 } else {
598 overScroll(x - mMaxScrollX);
599 }
Adam Cohen68d73932010-11-15 10:50:58 -0800600 }
601 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700602 if (mWasInOverscroll) {
603 overScroll(0);
604 mWasInOverscroll = false;
605 }
Adam Cohen68d73932010-11-15 10:50:58 -0800606 super.scrollTo(x, y);
607 }
608
Michael Jurka0142d492010-08-25 17:46:15 -0700609 mTouchX = x;
610 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700611
612 // Update the last motion events when scrolling
613 if (isReordering(true)) {
614 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
615 mLastMotionX = p[0];
616 mLastMotionY = p[1];
617 updateDragViewTranslationDuringDrag();
618 }
Michael Jurka0142d492010-08-25 17:46:15 -0700619 }
620
Adam Cohen53805212013-10-01 10:39:23 -0700621 private void sendScrollAccessibilityEvent() {
622 AccessibilityManager am =
623 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
624 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700625 if (mCurrentPage != getNextPage()) {
626 AccessibilityEvent ev =
627 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700628 ev.setScrollable(true);
629 ev.setScrollX(getScrollX());
630 ev.setScrollY(getScrollY());
631 ev.setMaxScrollX(mMaxScrollX);
632 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700633
Vadim Tryshevf4715972015-05-08 17:21:03 -0700634 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700635 }
Adam Cohen53805212013-10-01 10:39:23 -0700636 }
637 }
638
Michael Jurka0142d492010-08-25 17:46:15 -0700639 // we moved this functionality to a helper function so SmoothPagedView can reuse it
640 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700642 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 if (getScrollX() != mScroller.getCurrX()
Sunny Goyal4d113a52015-05-27 10:05:28 -0700644 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700645 float scaleX = mFreeScroll ? getScaleX() : 1f;
646 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
647 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700648 }
Michael Jurka0142d492010-08-25 17:46:15 -0700649 invalidate();
650 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700651 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700652 sendScrollAccessibilityEvent();
653
Adam Cohen6f127a62014-06-12 14:54:41 -0700654 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700655 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700656 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700657
Adam Cohen73aa9752010-11-24 16:26:58 -0800658 // We don't want to trigger a page end moving unless the page has settled
659 // and the user has stopped scrolling
660 if (mTouchState == TOUCH_STATE_REST) {
661 pageEndMoving();
662 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700663
Adam Cohen7d30a372013-07-01 17:03:59 -0700664 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700665 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700666 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700667 if (am.isEnabled()) {
668 // Notify the user when the page changes
669 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700670 }
Michael Jurka0142d492010-08-25 17:46:15 -0700671 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 }
Michael Jurka0142d492010-08-25 17:46:15 -0700673 return false;
674 }
675
676 @Override
677 public void computeScroll() {
678 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 }
680
Adam Cohen96d30a12013-07-16 18:13:21 -0700681 public static class LayoutParams extends ViewGroup.LayoutParams {
682 public boolean isFullScreenPage = false;
683
684 /**
685 * {@inheritDoc}
686 */
687 public LayoutParams(int width, int height) {
688 super(width, height);
689 }
690
Sunny Goyal41b22c02015-05-14 15:20:48 -0700691 public LayoutParams(Context context, AttributeSet attrs) {
692 super(context, attrs);
693 }
694
Adam Cohen96d30a12013-07-16 18:13:21 -0700695 public LayoutParams(ViewGroup.LayoutParams source) {
696 super(source);
697 }
698 }
699
Sunny Goyal41b22c02015-05-14 15:20:48 -0700700 @Override
701 public LayoutParams generateLayoutParams(AttributeSet attrs) {
702 return new LayoutParams(getContext(), attrs);
703 }
704
705 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700706 protected LayoutParams generateDefaultLayoutParams() {
707 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
708 }
709
Sunny Goyal41b22c02015-05-14 15:20:48 -0700710 @Override
711 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
712 return new LayoutParams(p);
713 }
714
715 @Override
716 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
717 return p instanceof LayoutParams;
718 }
719
Adam Cohenedb40762013-07-18 16:45:45 -0700720 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700721 LayoutParams lp = generateDefaultLayoutParams();
722 lp.isFullScreenPage = true;
723 super.addView(page, 0, lp);
724 }
725
Adam Cohen410f3cd2013-09-22 12:09:32 -0700726 public int getNormalChildHeight() {
727 return mNormalChildHeight;
728 }
729
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 @Override
731 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700732 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700733 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
734 return;
735 }
736
Adam Cohen7d30a372013-07-01 17:03:59 -0700737 // We measure the dimensions of the PagedView to be larger than the pages so that when we
738 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700739 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
740 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
741 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700742 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800743 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700744 // viewport, we can be at most one and a half screens offset once we scale down
745 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700746 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
747 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700748
Winson Chungc82d2622013-11-06 13:23:29 -0800749 int parentWidthSize = (int) (2f * maxSize);
750 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700751 int scaledWidthSize, scaledHeightSize;
752 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700753 scaledWidthSize = (int) (parentWidthSize / mMinScale);
754 scaledHeightSize = (int) (parentHeightSize / mMinScale);
755 } else {
756 scaledWidthSize = widthSize;
757 scaledHeightSize = heightSize;
758 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700759 mViewport.set(0, 0, widthSize, heightSize);
760
761 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
762 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
763 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765
Winson Chung8aad6102012-05-11 16:27:49 -0700766 // Return early if we aren't given a proper dimension
767 if (widthSize <= 0 || heightSize <= 0) {
768 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
769 return;
770 }
771
Adam Lesinski6b879f02010-11-04 16:15:23 -0700772 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
773 * of the All apps view on XLarge displays to not take up more space then it needs. Width
774 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
775 * each page to have the same width.
776 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 final int verticalPadding = getPaddingTop() + getPaddingBottom();
778 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700779
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700780 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700782 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700783 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700784 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
785 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
786 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
787 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 final int childCount = getChildCount();
789 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700790 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700791 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100792 if (child.getVisibility() != GONE) {
793 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700794
Vladimir Marko2824b072013-10-04 16:42:17 +0100795 int childWidthMode;
796 int childHeightMode;
797 int childWidth;
798 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700799
Vladimir Marko2824b072013-10-04 16:42:17 +0100800 if (!lp.isFullScreenPage) {
801 if (lp.width == LayoutParams.WRAP_CONTENT) {
802 childWidthMode = MeasureSpec.AT_MOST;
803 } else {
804 childWidthMode = MeasureSpec.EXACTLY;
805 }
806
807 if (lp.height == LayoutParams.WRAP_CONTENT) {
808 childHeightMode = MeasureSpec.AT_MOST;
809 } else {
810 childHeightMode = MeasureSpec.EXACTLY;
811 }
812
Adam Cohen3b185e22013-10-29 14:45:58 -0700813 childWidth = getViewportWidth() - horizontalPadding
814 - mInsets.left - mInsets.right;
815 childHeight = getViewportHeight() - verticalPadding
816 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100817 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700818 } else {
819 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700820 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100821
Adam Cohen3b185e22013-10-29 14:45:58 -0700822 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
823 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700824 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700825 if (referenceChildWidth == 0) {
826 referenceChildWidth = childWidth;
827 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700828
Vladimir Marko2824b072013-10-04 16:42:17 +0100829 final int childWidthMeasureSpec =
830 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
831 final int childHeightMeasureSpec =
832 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
833 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700834 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700835 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700836 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800837 }
838
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700840 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700841 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700842 return;
843 }
844
Winson Chung785d2eb2011-04-14 16:08:02 -0700845 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700847
Adam Cohen7d30a372013-07-01 17:03:59 -0700848 int offsetX = getViewportOffsetX();
849 int offsetY = getViewportOffsetY();
850
851 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700852 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700853
Sunny Goyal70660032015-05-14 00:07:08 -0700854 final int startIndex = mIsRtl ? childCount - 1 : 0;
855 final int endIndex = mIsRtl ? -1 : childCount;
856 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700857
Adam Cohen7d30a372013-07-01 17:03:59 -0700858 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700859
Adam Cohen3b185e22013-10-29 14:45:58 -0700860 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000861 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700862
863 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700864 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
865 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700866 }
867
Adam Cohen0ffac432013-07-10 11:19:26 -0700868 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700869 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700871 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100872 int childTop;
873 if (lp.isFullScreenPage) {
874 childTop = offsetY;
875 } else {
876 childTop = offsetY + getPaddingTop() + mInsets.top;
877 if (mCenterPagesVertically) {
878 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
879 }
880 }
881
Adam Cohen96d30a12013-07-16 18:13:21 -0700882 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700883 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800884
Winson Chung785d2eb2011-04-14 16:08:02 -0700885 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700886 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800887 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700888
Adam Cohen3b185e22013-10-29 14:45:58 -0700889 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
890 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000891
892 int pageGap = mPageSpacing;
893 int next = i + delta;
894 if (next != endIndex) {
895 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
896 } else {
897 nextLp = null;
898 }
899
900 // Prevent full screen pages from showing in the viewport
901 // when they are not the current page.
902 if (lp.isFullScreenPage) {
903 pageGap = getPaddingLeft();
904 } else if (nextLp != null && nextLp.isFullScreenPage) {
905 pageGap = getPaddingRight();
906 }
907
Sunny Goyala1fbd842015-05-20 13:40:27 -0700908 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 }
910 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700911
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700912 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -0800913 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700914 mFirstLayout = false;
915 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700916
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700917 final LayoutTransition transition = getLayoutTransition();
918 // If the transition is running defer updating max scroll, as some empty pages could
919 // still be present, and a max scroll change could cause sudden jumps in scroll.
920 if (transition != null && transition.isRunning()) {
921 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
922
923 @Override
924 public void startTransition(LayoutTransition transition, ViewGroup container,
925 View view, int transitionType) { }
926
927 @Override
928 public void endTransition(LayoutTransition transition, ViewGroup container,
929 View view, int transitionType) {
930 // Wait until all transitions are complete.
931 if (!transition.isRunning()) {
932 transition.removeTransitionListener(this);
933 updateMaxScrollX();
934 }
935 }
936 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700937 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700938 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700939 }
940
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700941 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700942 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700943 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700944 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700945 } else {
946 setCurrentPage(getNextPage());
947 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700948 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700949 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700950
951 if (isReordering(true)) {
952 updateDragViewTranslationDuringDrag();
953 }
Winson Chunge3193b92010-09-10 11:44:42 -0700954 }
955
Sunny Goyala1fbd842015-05-20 13:40:27 -0700956 protected int getChildGap() {
957 return 0;
958 }
959
Sunny Goyal316490e2015-06-02 09:38:28 -0700960 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700961 int childCount = getChildCount();
962 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700963 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700964 mMaxScrollX = getScrollForPage(index);
965 } else {
966 mMaxScrollX = 0;
967 }
968 }
969
Adam Cohen3b185e22013-10-29 14:45:58 -0700970 public void setPageSpacing(int pageSpacing) {
971 mPageSpacing = pageSpacing;
972 requestLayout();
973 }
974
Sunny Goyal4d113a52015-05-27 10:05:28 -0700975 /**
976 * Called when the center screen changes during scrolling.
977 */
978 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700979
Winson Chunge3193b92010-09-10 11:44:42 -0700980 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700981 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700982 // Update the page indicator, we don't update the page indicator as we
983 // add/remove pages
984 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700985 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700986 mPageIndicator.addMarker(pageIndex,
987 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700988 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700989 }
990
Adam Cohen2591f6a2011-10-25 14:36:40 -0700991 // This ensures that when children are added, they get the correct transforms / alphas
992 // in accordance with any scroll effects.
993 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700994 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700995 invalidate();
996 }
997
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 @Override
999 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001000 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001001 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001002 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001003 }
1004
Winson Chungd2be3812013-07-16 11:11:32 -07001005 private void removeMarkerForView(int index) {
1006 // Update the page indicator, we don't update the page indicator as we
1007 // add/remove pages
1008 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001009 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001010 }
1011 }
1012
1013 @Override
1014 public void removeView(View v) {
1015 // XXX: We should find a better way to hook into this before the view
1016 // gets removed form its parent...
1017 removeMarkerForView(indexOfChild(v));
1018 super.removeView(v);
1019 }
1020 @Override
1021 public void removeViewInLayout(View v) {
1022 // XXX: We should find a better way to hook into this before the view
1023 // gets removed form its parent...
1024 removeMarkerForView(indexOfChild(v));
1025 super.removeViewInLayout(v);
1026 }
1027 @Override
1028 public void removeViewAt(int index) {
1029 // XXX: We should find a better way to hook into this before the view
1030 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001031 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001032 super.removeViewAt(index);
1033 }
1034 @Override
1035 public void removeAllViewsInLayout() {
1036 // Update the page indicator, we don't update the page indicator as we
1037 // add/remove pages
1038 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001039 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001040 }
1041
1042 super.removeAllViewsInLayout();
1043 }
1044
Adam Cohen73894962011-10-31 13:17:17 -07001045 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001046 if (index < 0 || index > getChildCount() - 1) return 0;
1047
Adam Cohenf698c6e2013-07-17 18:44:25 -07001048 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001049
Adam Cohenf698c6e2013-07-17 18:44:25 -07001050 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001051 }
1052
Adam Cohen6f127a62014-06-12 14:54:41 -07001053 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001054 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001055 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001056 }
1057
Michael Jurkadde558b2011-11-09 22:09:06 -08001058 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001059 final int pageCount = getChildCount();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001060 sTmpIntPoint[0] = sTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001061
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001062 range[0] = -1;
1063 range[1] = -1;
1064
Michael Jurkac4fb9172010-09-02 17:19:20 -07001065 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001066 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001067 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001068
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001069 int count = getChildCount();
1070 for (int i = 0; i < count; i++) {
1071 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001072
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001073 sTmpIntPoint[0] = 0;
1074 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1075 if (sTmpIntPoint[0] > viewportWidth) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001076 if (range[0] == -1) {
1077 continue;
1078 } else {
1079 break;
1080 }
1081 }
1082
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001083 sTmpIntPoint[0] = currPage.getMeasuredWidth();
1084 Utilities.getDescendantCoordRelativeToParent(currPage, this, sTmpIntPoint, false);
1085 if (sTmpIntPoint[0] < 0) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001086 if (range[0] == -1) {
1087 continue;
1088 } else {
1089 break;
1090 }
1091 }
1092 curScreen = i;
1093 if (range[0] < 0) {
1094 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001095 }
1096 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001097
1098 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001099 } else {
1100 range[0] = -1;
1101 range[1] = -1;
1102 }
1103 }
1104
Michael Jurka920d7f42012-05-14 16:29:55 -07001105 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001106 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001107 }
1108
Michael Jurkadde558b2011-11-09 22:09:06 -08001109 @Override
1110 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001111 // Find out which screens are visible; as an optimization we only call draw on them
1112 final int pageCount = getChildCount();
1113 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001114 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001115 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001116
1117 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1118 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1119 // set it for the next frame
1120 mForceScreenScrolled = false;
1121 screenScrolled(screenCenter);
1122 mLastScreenCenter = screenCenter;
1123 }
1124
Michael Jurkadde558b2011-11-09 22:09:06 -08001125 getVisiblePages(mTempVisiblePagesRange);
1126 final int leftScreen = mTempVisiblePagesRange[0];
1127 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001128 if (leftScreen != -1 && rightScreen != -1) {
1129 final long drawingTime = getDrawingTime();
1130 // Clip to the bounds
1131 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001132 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1133 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001134
Adam Cohen7d30a372013-07-01 17:03:59 -07001135 // Draw all the children, leaving the drag view for last
1136 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001137 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001138 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001139 if (mForceDrawAllChildrenNextFrame ||
1140 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001141 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001142 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001143 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001144 // Draw the drag view on top (if there is one)
1145 if (mDragView != null) {
1146 drawChild(canvas, mDragView, drawingTime);
1147 }
1148
Michael Jurka5e368ff2012-05-14 23:13:15 -07001149 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001150 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001151 }
Michael Jurka0142d492010-08-25 17:46:15 -07001152 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 }
1154
1155 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001156 public void draw(Canvas canvas) {
1157 super.draw(canvas);
1158 if (getPageCount() > 0) {
1159 if (!mEdgeGlowLeft.isFinished()) {
1160 final int restoreCount = canvas.save();
1161 Rect display = mViewport;
1162 canvas.translate(display.left, display.top);
1163 canvas.rotate(270);
1164
1165 getEdgeVerticalPostion(sTmpIntPoint);
1166 canvas.translate(display.top - sTmpIntPoint[1], 0);
1167 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1168 if (mEdgeGlowLeft.draw(canvas)) {
1169 postInvalidateOnAnimation();
1170 }
1171 canvas.restoreToCount(restoreCount);
1172 }
1173 if (!mEdgeGlowRight.isFinished()) {
1174 final int restoreCount = canvas.save();
1175 Rect display = mViewport;
1176 canvas.translate(display.left +
1177 display.width() * (getChildCount() - 1), display.top);
1178 canvas.rotate(90);
1179
1180 getEdgeVerticalPostion(sTmpIntPoint);
1181 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1182 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1183 if (mEdgeGlowRight.draw(canvas)) {
1184 postInvalidateOnAnimation();
1185 }
1186 canvas.restoreToCount(restoreCount);
1187 }
1188 }
1189 }
1190
1191 /**
1192 * Returns the top and bottom position for the edge effect.
1193 */
1194 protected abstract void getEdgeVerticalPostion(int[] pos);
1195
1196 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001198 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001199 if (page != mCurrentPage || !mScroller.isFinished()) {
1200 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 return true;
1202 }
1203 return false;
1204 }
1205
1206 @Override
1207 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001208 int focusablePage;
1209 if (mNextPage != INVALID_PAGE) {
1210 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001212 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 }
Winson Chung86f77532010-08-24 11:08:22 -07001214 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001216 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 }
1218 return false;
1219 }
1220
1221 @Override
1222 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001223 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001225 if (getCurrentPage() > 0) {
1226 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 return true;
1228 }
1229 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001230 if (getCurrentPage() < getPageCount() - 1) {
1231 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 return true;
1233 }
1234 }
1235 return super.dispatchUnhandledMove(focused, direction);
1236 }
1237
1238 @Override
1239 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001240 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001241 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001242 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
1244 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001245 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001246 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 }
1248 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001249 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001250 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
1252 }
1253 }
1254
1255 /**
1256 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001257 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 *
Winson Chung86f77532010-08-24 11:08:22 -07001259 * This happens when live folders requery, and if they're off page, they
1260 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 */
1262 @Override
1263 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001264 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 View v = focused;
1266 while (true) {
1267 if (v == current) {
1268 super.focusableViewAvailable(focused);
1269 return;
1270 }
1271 if (v == this) {
1272 return;
1273 }
1274 ViewParent parent = v.getParent();
1275 if (parent instanceof View) {
1276 v = (View)v.getParent();
1277 } else {
1278 return;
1279 }
1280 }
1281 }
1282
1283 /**
1284 * {@inheritDoc}
1285 */
1286 @Override
1287 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1288 if (disallowIntercept) {
1289 // We need to make sure to cancel our long press if
1290 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001291 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001292 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
1294 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1295 }
1296
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001297 /**
1298 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1299 */
1300 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001301 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001302 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001303 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001304 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001305 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001306 }
1307
1308 /**
1309 * Return true if a tap at (x, y) should trigger a flip to the next page.
1310 */
1311 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001312 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001313 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001314 }
1315 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001316 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 }
Winson Chung52aee602013-01-30 12:01:02 -08001318
Adam Cohen7d30a372013-07-01 17:03:59 -07001319 /** Returns whether x and y originated within the buffered viewport */
1320 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001321 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001323 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001324 }
1325
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 @Override
1327 public boolean onInterceptTouchEvent(MotionEvent ev) {
1328 /*
1329 * This method JUST determines whether we want to intercept the motion.
1330 * If we return true, onTouchEvent will be called and we do the actual
1331 * scrolling there.
1332 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001333 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001334
Winson Chung45e1d6e2010-11-09 17:19:49 -08001335 // Skip touch handling if there are no pages to swipe
1336 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1337
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 /*
1339 * Shortcut the most recurring case: the user is in the dragging
1340 * state and he is moving his finger. We want to intercept this
1341 * motion.
1342 */
1343 final int action = ev.getAction();
1344 if ((action == MotionEvent.ACTION_MOVE) &&
1345 (mTouchState == TOUCH_STATE_SCROLLING)) {
1346 return true;
1347 }
1348
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 switch (action & MotionEvent.ACTION_MASK) {
1350 case MotionEvent.ACTION_MOVE: {
1351 /*
1352 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1353 * whether the user has moved far enough from his original down touch.
1354 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001355 if (mActivePointerId != INVALID_POINTER) {
1356 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001357 }
1358 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1359 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1360 // i.e. fall through to the next case (don't break)
1361 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1362 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001363 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 }
1365
1366 case MotionEvent.ACTION_DOWN: {
1367 final float x = ev.getX();
1368 final float y = ev.getY();
1369 // Remember location of down touch
1370 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001371 mDownMotionY = y;
1372 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 mLastMotionX = x;
1374 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001375 float[] p = mapPointFromViewToParent(this, x, y);
1376 mParentDownMotionX = p[0];
1377 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001378 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001379 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001381
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 /*
1383 * If being flinged and user touches the screen, initiate drag;
1384 * otherwise don't. mScroller.isFinished should be false when
1385 * being flinged.
1386 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001387 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001388 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001389
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001390 if (finishedScrolling) {
1391 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001392 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001393 setCurrentPage(getNextPage());
1394 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001395 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001396 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1398 mTouchState = TOUCH_STATE_SCROLLING;
1399 } else {
1400 mTouchState = TOUCH_STATE_REST;
1401 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001402 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001403
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 break;
1405 }
1406
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001408 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001409 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 break;
1411
1412 case MotionEvent.ACTION_POINTER_UP:
1413 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001414 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 break;
1416 }
1417
1418 /*
1419 * The only time we want to intercept motion events is if we are in the
1420 * drag mode.
1421 */
1422 return mTouchState != TOUCH_STATE_REST;
1423 }
1424
Adam Cohenf8d28232011-02-01 21:47:00 -08001425 protected void determineScrollingStart(MotionEvent ev) {
1426 determineScrollingStart(ev, 1.0f);
1427 }
1428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 /*
1430 * Determines if we should change the touch state to start scrolling after the
1431 * user moves their touch point too far.
1432 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001433 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001434 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001436 if (pointerIndex == -1) return;
1437
1438 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 final float x = ev.getX(pointerIndex);
1440 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001441 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1442
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001444
Adam Cohenf8d28232011-02-01 21:47:00 -08001445 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001447
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001448 if (xMoved) {
1449 // Scroll if the user moved far enough along the X axis
1450 mTouchState = TOUCH_STATE_SCROLLING;
1451 mTotalMotionX += Math.abs(mLastMotionX - x);
1452 mLastMotionX = x;
1453 mLastMotionXRemainder = 0;
1454 mTouchX = getViewportOffsetX() + getScrollX();
1455 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1456 onScrollInteractionBegin();
1457 pageBeginMoving();
Adam Cohenf8d28232011-02-01 21:47:00 -08001458 }
1459 }
1460
1461 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001462 // Try canceling the long press. It could also have been scheduled
1463 // by a distant descendant, so use the mAllowLongPress flag to block
1464 // everything
1465 final View currentPage = getPageAt(mCurrentPage);
1466 if (currentPage != null) {
1467 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 }
1469 }
1470
Adam Cohenb5ba0972011-09-07 18:02:31 -07001471 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001472 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001473
Adam Cohenedb40762013-07-18 16:45:45 -07001474 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001475 int count = getChildCount();
1476
1477 final int totalDistance;
1478
1479 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001480 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001481 adjacentPage = page - 1;
1482 }
1483
1484 if (adjacentPage < 0 || adjacentPage > count - 1) {
1485 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1486 } else {
1487 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1488 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001489
1490 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001491 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1492 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001493 return scrollProgress;
1494 }
1495
Adam Cohenedb40762013-07-18 16:45:45 -07001496 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001497 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001498 return 0;
1499 } else {
1500 return mPageScrolls[index];
1501 }
1502 }
1503
Adam Cohen564a2e72013-10-09 14:47:32 -07001504 // While layout transitions are occurring, a child's position may stray from its baseline
1505 // position. This method returns the magnitude of this stray at any given time.
1506 public int getLayoutTransitionOffsetForPage(int index) {
1507 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1508 return 0;
1509 } else {
1510 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001511
1512 int scrollOffset = 0;
1513 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1514 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001515 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001516 }
1517
Adam Cohen564a2e72013-10-09 14:47:32 -07001518 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1519 return (int) (child.getX() - baselineX);
1520 }
1521 }
1522
Adam Cohenb5ba0972011-09-07 18:02:31 -07001523 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001524 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001525 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001526 if (f < 0) {
1527 mEdgeGlowLeft.onPull(-f);
1528 } else if (f > 0) {
1529 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001530 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001531 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001532 }
1533 invalidate();
1534 }
1535
Adam Cohenb5ba0972011-09-07 18:02:31 -07001536 protected void overScroll(float amount) {
1537 dampedOverScroll(amount);
1538 }
1539
Winson Chungdc61c4d2015-04-20 18:26:57 -07001540 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001541 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001542 }
1543
Winson Chungdc61c4d2015-04-20 18:26:57 -07001544 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001545 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001546 }
1547
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001548 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001549 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001550 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001551 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1552 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1553 } else {
1554 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1555 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1556 }
1557 }
1558
Adam Cohenf9618852013-11-08 06:45:03 -08001559 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001560 mFreeScroll = freeScroll;
1561
Adam Cohenf9618852013-11-08 06:45:03 -08001562 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001563 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001564 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001565 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1566 setCurrentPage(mTempVisiblePagesRange[0]);
1567 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1568 setCurrentPage(mTempVisiblePagesRange[1]);
1569 }
1570 }
1571
1572 setEnableOverscroll(!freeScroll);
1573 }
1574
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001575 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001576 mAllowOverScroll = enable;
1577 }
1578
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001579 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001580 if (mDragView != null) {
1581 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1582 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001583 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001584 int minDistance = Integer.MAX_VALUE;
1585 int minIndex = indexOfChild(mDragView);
1586 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1587 View page = getPageAt(i);
1588 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1589 int d = Math.abs(dragX - pageX);
1590 if (d < minDistance) {
1591 minIndex = i;
1592 minDistance = d;
1593 }
1594 }
1595 return minIndex;
1596 }
1597 return -1;
1598 }
1599
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 @Override
1601 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001602 super.onTouchEvent(ev);
1603
Winson Chung45e1d6e2010-11-09 17:19:49 -08001604 // Skip touch handling if there are no pages to swipe
1605 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1606
Michael Jurkab8f06722010-10-10 15:58:46 -07001607 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001608
1609 final int action = ev.getAction();
1610
1611 switch (action & MotionEvent.ACTION_MASK) {
1612 case MotionEvent.ACTION_DOWN:
1613 /*
1614 * If being flinged and user touches, stop the fling. isFinished
1615 * will be false if being flinged.
1616 */
1617 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001618 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001619 }
1620
1621 // Remember where the motion event started
1622 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001623 mDownMotionY = mLastMotionY = ev.getY();
1624 mDownScrollX = getScrollX();
1625 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1626 mParentDownMotionX = p[0];
1627 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001628 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001629 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001630 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001631
Michael Jurka0142d492010-08-25 17:46:15 -07001632 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001633 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001634 pageBeginMoving();
1635 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001636 break;
1637
1638 case MotionEvent.ACTION_MOVE:
1639 if (mTouchState == TOUCH_STATE_SCROLLING) {
1640 // Scroll to follow the motion event
1641 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001642
1643 if (pointerIndex == -1) return true;
1644
Winson Chung321e9ee2010-08-09 13:37:56 -07001645 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001646 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001647
Adam Cohenaefd4e12011-02-14 16:39:38 -08001648 mTotalMotionX += Math.abs(deltaX);
1649
Winson Chungc0844aa2011-02-02 15:25:58 -08001650 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1651 // keep the remainder because we are actually testing if we've moved from the last
1652 // scrolled position (which is discrete).
1653 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001654 mTouchX += deltaX;
1655 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001656 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001657 mLastMotionX = x;
1658 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 } else {
1660 awakenScrollBars();
1661 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001662 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1663 // Update the last motion position
1664 mLastMotionX = ev.getX();
1665 mLastMotionY = ev.getY();
1666
1667 // Update the parent down so that our zoom animations take this new movement into
1668 // account
1669 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1670 mParentDownMotionX = pt[0];
1671 mParentDownMotionY = pt[1];
1672 updateDragViewTranslationDuringDrag();
1673
1674 // Find the closest page to the touch point
1675 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001676
Adam Cohen7d30a372013-07-01 17:03:59 -07001677 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1678 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1679 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1680 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1681
Adam Cohenf358a4b2013-07-23 16:47:31 -07001682 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001683 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001684 mTempVisiblePagesRange[0] = 0;
1685 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001686 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001687 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1688 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1689 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1690 mSidePageHoverIndex = pageUnderPointIndex;
1691 mSidePageHoverRunnable = new Runnable() {
1692 @Override
1693 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001694 // Setup the scroll to the correct page before we swap the views
1695 snapToPage(pageUnderPointIndex);
1696
1697 // For each of the pages between the paged view and the drag view,
1698 // animate them from the previous position to the new position in
1699 // the layout (as a result of the drag view moving in the layout)
1700 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1701 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1702 dragViewIndex + 1 : pageUnderPointIndex;
1703 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1704 dragViewIndex - 1 : pageUnderPointIndex;
1705 for (int i = lowerIndex; i <= upperIndex; ++i) {
1706 View v = getChildAt(i);
1707 // dragViewIndex < pageUnderPointIndex, so after we remove the
1708 // drag view all subsequent views to pageUnderPointIndex will
1709 // shift down.
1710 int oldX = getViewportOffsetX() + getChildOffset(i);
1711 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1712
1713 // Animate the view translation from its old position to its new
1714 // position
1715 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1716 if (anim != null) {
1717 anim.cancel();
1718 }
1719
1720 v.setTranslationX(oldX - newX);
1721 anim = new AnimatorSet();
1722 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1723 anim.playTogether(
1724 ObjectAnimator.ofFloat(v, "translationX", 0f));
1725 anim.start();
1726 v.setTag(anim);
1727 }
1728
1729 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001730 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001731 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001732 if (mPageIndicator != null) {
1733 mPageIndicator.setActiveMarker(getNextPage());
1734 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001735 }
1736 };
1737 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1738 }
1739 } else {
1740 removeCallbacks(mSidePageHoverRunnable);
1741 mSidePageHoverIndex = -1;
1742 }
Adam Cohen564976a2010-10-13 18:52:07 -07001743 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001744 determineScrollingStart(ev);
1745 }
1746 break;
1747
1748 case MotionEvent.ACTION_UP:
1749 if (mTouchState == TOUCH_STATE_SCROLLING) {
1750 final int activePointerId = mActivePointerId;
1751 final int pointerIndex = ev.findPointerIndex(activePointerId);
1752 final float x = ev.getX(pointerIndex);
1753 final VelocityTracker velocityTracker = mVelocityTracker;
1754 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1755 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001756 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001757 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001758 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1759 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001760
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001761 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1762
Adam Cohen00481b32011-11-18 12:03:48 -08001763 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001764 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001765
Adam Cohenf358a4b2013-07-23 16:47:31 -07001766 if (!mFreeScroll) {
1767 // In the case that the page is moved far to one direction and then is flung
1768 // in the opposite direction, we use a threshold to determine whether we should
1769 // just return to the starting page, or if we should skip one further.
1770 boolean returnToOriginalPage = false;
1771 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1772 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1773 returnToOriginalPage = true;
1774 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001775
Adam Cohenf358a4b2013-07-23 16:47:31 -07001776 int finalPage;
1777 // We give flings precedence over large moves, which is why we short-circuit our
1778 // test for a large move if a fling has been registered. That is, a large
1779 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001780 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1781 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001782 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1783 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1784 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1785 snapToPageWithVelocity(finalPage, velocityX);
1786 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1787 (isFling && isVelocityXLeft)) &&
1788 mCurrentPage < getChildCount() - 1) {
1789 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1790 snapToPageWithVelocity(finalPage, velocityX);
1791 } else {
1792 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001793 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001794 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001795 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001796 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001797 }
1798
1799 float scaleX = getScaleX();
1800 int vX = (int) (-velocityX * scaleX);
1801 int initialScrollX = (int) (getScrollX() * scaleX);
1802
Adam Cohenf9618852013-11-08 06:45:03 -08001803 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001804 mScroller.fling(initialScrollX,
1805 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1806 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001807 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001808 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001809 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1810 // at this point we have not moved beyond the touch slop
1811 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1812 // we can just page
1813 int nextPage = Math.max(0, mCurrentPage - 1);
1814 if (nextPage != mCurrentPage) {
1815 snapToPage(nextPage);
1816 } else {
1817 snapToDestination();
1818 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001819 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001820 // at this point we have not moved beyond the touch slop
1821 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1822 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001823 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1824 if (nextPage != mCurrentPage) {
1825 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001826 } else {
1827 snapToDestination();
1828 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1830 // Update the last motion position
1831 mLastMotionX = ev.getX();
1832 mLastMotionY = ev.getY();
1833
1834 // Update the parent down so that our zoom animations take this new movement into
1835 // account
1836 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1837 mParentDownMotionX = pt[0];
1838 mParentDownMotionY = pt[1];
1839 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001840 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001841 if (!mCancelTap) {
1842 onUnhandledTap(ev);
1843 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001844 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001845
1846 // Remove the callback to wait for the side page hover timeout
1847 removeCallbacks(mSidePageHoverRunnable);
1848 // End any intermediate reordering states
1849 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001850 break;
1851
1852 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001853 if (mTouchState == TOUCH_STATE_SCROLLING) {
1854 snapToDestination();
1855 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001856 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001857 break;
1858
1859 case MotionEvent.ACTION_POINTER_UP:
1860 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001861 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001862 break;
1863 }
1864
1865 return true;
1866 }
1867
Adam Cohen7d30a372013-07-01 17:03:59 -07001868 private void resetTouchState() {
1869 releaseVelocityTracker();
1870 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001871 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001872 mTouchState = TOUCH_STATE_REST;
1873 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001874 mEdgeGlowLeft.onRelease();
1875 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001876 }
1877
Adam Cohenc2d6e892014-10-16 09:49:24 -07001878 /**
1879 * Triggered by scrolling via touch
1880 */
1881 protected void onScrollInteractionBegin() {
1882 }
1883
1884 protected void onScrollInteractionEnd() {
1885 }
1886
Adam Cohen1697b792013-09-17 19:08:21 -07001887 protected void onUnhandledTap(MotionEvent ev) {
1888 ((Launcher) getContext()).onClick(this);
1889 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001890
Winson Chung185d7162011-02-28 13:47:29 -08001891 @Override
1892 public boolean onGenericMotionEvent(MotionEvent event) {
1893 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1894 switch (event.getAction()) {
1895 case MotionEvent.ACTION_SCROLL: {
1896 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1897 final float vscroll;
1898 final float hscroll;
1899 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1900 vscroll = 0;
1901 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1902 } else {
1903 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1904 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1905 }
1906 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001907 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001908 : (hscroll > 0 || vscroll > 0);
1909 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001910 scrollRight();
1911 } else {
1912 scrollLeft();
1913 }
1914 return true;
1915 }
1916 }
1917 }
1918 }
1919 return super.onGenericMotionEvent(event);
1920 }
1921
Michael Jurkab8f06722010-10-10 15:58:46 -07001922 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1923 if (mVelocityTracker == null) {
1924 mVelocityTracker = VelocityTracker.obtain();
1925 }
1926 mVelocityTracker.addMovement(ev);
1927 }
1928
1929 private void releaseVelocityTracker() {
1930 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001932 mVelocityTracker.recycle();
1933 mVelocityTracker = null;
1934 }
1935 }
1936
Winson Chung321e9ee2010-08-09 13:37:56 -07001937 private void onSecondaryPointerUp(MotionEvent ev) {
1938 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1939 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1940 final int pointerId = ev.getPointerId(pointerIndex);
1941 if (pointerId == mActivePointerId) {
1942 // This was our active pointer going up. Choose a new
1943 // active pointer and adjust accordingly.
1944 // TODO: Make this decision more intelligent.
1945 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1946 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1947 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001948 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001949 mActivePointerId = ev.getPointerId(newPointerIndex);
1950 if (mVelocityTracker != null) {
1951 mVelocityTracker.clear();
1952 }
1953 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001954 }
1955
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 @Override
1957 public void requestChildFocus(View child, View focused) {
1958 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001959 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001960 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001961 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001962 }
1963 }
1964
Adam Cohend19d3ca2010-09-15 14:43:42 -07001965 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001966 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001967 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001968 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 final int childCount = getChildCount();
1970 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001971 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001972 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001973 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001974 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001975 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1976 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1977 minDistanceFromScreenCenter = distanceFromScreenCenter;
1978 minDistanceFromScreenCenterIndex = i;
1979 }
1980 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001981 return minDistanceFromScreenCenterIndex;
1982 }
1983
1984 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001985 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001986 }
1987
Adam Cohene0f66b52010-11-23 15:06:07 -08001988 private static class ScrollInterpolator implements Interpolator {
1989 public ScrollInterpolator() {
1990 }
1991
1992 public float getInterpolation(float t) {
1993 t -= 1.0f;
1994 return t*t*t*t*t + 1;
1995 }
1996 }
1997
1998 // We want the duration of the page snap animation to be influenced by the distance that
1999 // the screen has to travel, however, we don't want this duration to be effected in a
2000 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2001 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07002002 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002003 f -= 0.5f; // center the values about 0.
2004 f *= 0.3f * Math.PI / 2.0f;
2005 return (float) Math.sin(f);
2006 }
2007
Michael Jurka0142d492010-08-25 17:46:15 -07002008 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002009 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002010 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002011
Adam Cohenedb40762013-07-18 16:45:45 -07002012 final int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002013 int delta = newX - getScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002014 int duration = 0;
2015
Sunny Goyal4d113a52015-05-27 10:05:28 -07002016 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002017 // If the velocity is low enough, then treat this more as an automatic page advance
2018 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002019 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002020 return;
2021 }
2022
2023 // Here we compute a "distance" that will be used in the computation of the overall
2024 // snap duration. This is a function of the actual distance that needs to be traveled;
2025 // we keep this value close to half screen size in order to reduce the variance in snap
2026 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002027 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002028 float distance = halfScreenSize + halfScreenSize *
2029 distanceInfluenceForSnapDuration(distanceRatio);
2030
2031 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002032 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002033
2034 // we want the page's snap velocity to approximately match the velocity at which the
2035 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002036 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2037 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002038
2039 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002040 }
2041
Sunny Goyal1740d902015-05-27 11:14:01 -07002042 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002043 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002044 }
2045
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 protected void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002047 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002048 }
2049
Michael Jurka0142d492010-08-25 17:46:15 -07002050 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002051 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 }
2053
Adam Cohenf9618852013-11-08 06:45:03 -08002054 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2055 snapToPage(whichPage, duration, false, interpolator);
2056 }
2057
2058 protected void snapToPage(int whichPage, int duration, boolean immediate,
2059 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002060 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002061
Adam Cohenedb40762013-07-18 16:45:45 -07002062 int newX = getScrollForPage(whichPage);
Sunny Goyal4d113a52015-05-27 10:05:28 -07002063 final int delta = newX - getScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002064 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002065 }
2066
2067 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002068 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002069 }
Michael Jurka0142d492010-08-25 17:46:15 -07002070
Adam Cohenf9618852013-11-08 06:45:03 -08002071 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2072 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002073 whichPage = validateNewPage(whichPage);
2074
Adam Cohen7d30a372013-07-01 17:03:59 -07002075 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002076 View focusedChild = getFocusedChild();
2077 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002078 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002079 focusedChild.clearFocus();
2080 }
2081
2082 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002084 if (immediate) {
2085 duration = 0;
2086 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002087 duration = Math.abs(delta);
2088 }
2089
Adam Cohenf358a4b2013-07-23 16:47:31 -07002090 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002091 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002092 }
Adam Cohenf9618852013-11-08 06:45:03 -08002093
2094 if (interpolator != null) {
2095 mScroller.setInterpolator(interpolator);
2096 } else {
2097 mScroller.setInterpolator(mDefaultInterpolator);
2098 }
2099
Sunny Goyal4d113a52015-05-27 10:05:28 -07002100 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002101
Adam Cohen674531f2013-12-13 15:07:14 -08002102 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002103
2104 // Trigger a compute() to finish switching pages if necessary
2105 if (immediate) {
2106 computeScroll();
2107 }
2108
2109 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002110 invalidate();
2111 }
2112
Winson Chung321e9ee2010-08-09 13:37:56 -07002113 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002114 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002115 }
2116
2117 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002118 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002119 }
2120
Winson Chung86f77532010-08-24 11:08:22 -07002121 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002122 int result = -1;
2123 if (v != null) {
2124 ViewParent vp = v.getParent();
2125 int count = getChildCount();
2126 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002127 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002128 return i;
2129 }
2130 }
2131 }
2132 return result;
2133 }
2134
Adam Cohendbdff6b2013-09-18 19:09:15 -07002135 @Override
2136 public boolean performLongClick() {
2137 mCancelTap = true;
2138 return super.performLongClick();
2139 }
2140
Winson Chung321e9ee2010-08-09 13:37:56 -07002141 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002142 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002143
2144 SavedState(Parcelable superState) {
2145 super(superState);
2146 }
2147
Adam Cohen091440a2015-03-18 14:16:05 -07002148 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002149 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002150 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002151 }
2152
2153 @Override
2154 public void writeToParcel(Parcel out, int flags) {
2155 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002156 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002157 }
2158
2159 public static final Parcelable.Creator<SavedState> CREATOR =
2160 new Parcelable.Creator<SavedState>() {
2161 public SavedState createFromParcel(Parcel in) {
2162 return new SavedState(in);
2163 }
2164
2165 public SavedState[] newArray(int size) {
2166 return new SavedState[size];
2167 }
2168 };
2169 }
2170
Adam Cohen7d30a372013-07-01 17:03:59 -07002171 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002172 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002173 if (mDragView != null) {
2174 AnimatorSet anim = new AnimatorSet();
2175 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2176 anim.playTogether(
2177 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002178 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2179 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2180 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002181 anim.addListener(new AnimatorListenerAdapter() {
2182 @Override
2183 public void onAnimationEnd(Animator animation) {
2184 onPostReorderingAnimationCompleted();
2185 }
2186 });
2187 anim.start();
2188 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002189 }
2190
Sunny Goyal1d08f702015-05-04 15:50:25 -07002191 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002192 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2193 mTouchState = TOUCH_STATE_REORDERING;
2194 mIsReordering = true;
2195
Adam Cohen7d30a372013-07-01 17:03:59 -07002196 // We must invalidate to trigger a redraw to update the layers such that the drag view
2197 // is always drawn on top
2198 invalidate();
2199 }
2200
Adam Cohen091440a2015-03-18 14:16:05 -07002201 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 // Trigger the callback when reordering has settled
2203 --mPostReorderingPreZoomInRemainingAnimationCount;
2204 if (mPostReorderingPreZoomInRunnable != null &&
2205 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2206 mPostReorderingPreZoomInRunnable.run();
2207 mPostReorderingPreZoomInRunnable = null;
2208 }
2209 }
2210
Sunny Goyal1d08f702015-05-04 15:50:25 -07002211 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002212 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002213 }
2214
Adam Cohenf358a4b2013-07-23 16:47:31 -07002215 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002216 int dragViewIndex = indexOfChild(v);
2217
Adam Cohen327acfe2014-06-06 11:52:52 -07002218 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002219
Adam Cohen7d30a372013-07-01 17:03:59 -07002220 mTempVisiblePagesRange[0] = 0;
2221 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002222 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002223 mReorderingStarted = true;
2224
2225 // Check if we are within the reordering range
2226 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002227 dragViewIndex <= mTempVisiblePagesRange[1]) {
2228 // Find the drag view under the pointer
2229 mDragView = getChildAt(dragViewIndex);
2230 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2231 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002232 snapToPage(getPageNearestToCenterOfScreen());
2233 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002234 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002235 return true;
2236 }
2237 return false;
2238 }
2239
2240 boolean isReordering(boolean testTouchState) {
2241 boolean state = mIsReordering;
2242 if (testTouchState) {
2243 state &= (mTouchState == TOUCH_STATE_REORDERING);
2244 }
2245 return state;
2246 }
2247 void endReordering() {
2248 // For simplicity, we call endReordering sometimes even if reordering was never started.
2249 // In that case, we don't want to do anything.
2250 if (!mReorderingStarted) return;
2251 mReorderingStarted = false;
2252
2253 // If we haven't flung-to-delete the current child, then we just animate the drag view
2254 // back into position
2255 final Runnable onCompleteRunnable = new Runnable() {
2256 @Override
2257 public void run() {
2258 onEndReordering();
2259 }
2260 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002261
2262 mPostReorderingPreZoomInRunnable = new Runnable() {
2263 public void run() {
2264 onCompleteRunnable.run();
2265 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002266 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002267 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002268
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002269 mPostReorderingPreZoomInRemainingAnimationCount =
2270 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2271 // Snap to the current page
2272 snapToPage(indexOfChild(mDragView), 0);
2273 // Animate the drag view back to the front position
2274 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002275 }
2276
Adam Cohen7d30a372013-07-01 17:03:59 -07002277 private static final int ANIM_TAG_KEY = 100;
2278
Winson Chung6a0f57d2011-06-29 20:10:49 -07002279 /* Accessibility */
Sunny Goyal316490e2015-06-02 09:38:28 -07002280 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002281 @Override
2282 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2283 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002284 info.setScrollable(getPageCount() > 1);
2285 if (getCurrentPage() < getPageCount() - 1) {
2286 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2287 }
2288 if (getCurrentPage() > 0) {
2289 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2290 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002291 info.setClassName(getClass().getName());
2292
2293 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2294 // Besides disabling the accessibility long-click, this also prevents this view from getting
2295 // accessibility focus.
2296 info.setLongClickable(false);
2297 if (Utilities.isLmpOrAbove()) {
2298 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2299 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002300 }
2301
2302 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002303 public void sendAccessibilityEvent(int eventType) {
2304 // Don't let the view send real scroll events.
2305 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2306 super.sendAccessibilityEvent(eventType);
2307 }
2308 }
2309
2310 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002311 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2312 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002313 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002314 }
2315
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002316 @Override
2317 public boolean performAccessibilityAction(int action, Bundle arguments) {
2318 if (super.performAccessibilityAction(action, arguments)) {
2319 return true;
2320 }
2321 switch (action) {
2322 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2323 if (getCurrentPage() < getPageCount() - 1) {
2324 scrollRight();
2325 return true;
2326 }
2327 } break;
2328 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2329 if (getCurrentPage() > 0) {
2330 scrollLeft();
2331 return true;
2332 }
2333 } break;
2334 }
2335 return false;
2336 }
2337
Adam Cohen0ffac432013-07-10 11:19:26 -07002338 protected String getCurrentPageDescription() {
2339 return String.format(getContext().getString(R.string.default_scroll_format),
2340 getNextPage() + 1, getChildCount());
2341 }
2342
Winson Chungd11265e2011-08-30 13:37:23 -07002343 @Override
2344 public boolean onHoverEvent(android.view.MotionEvent event) {
2345 return true;
2346 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002347}