Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 3 | * |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 18 | |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 21 | import android.animation.LayoutTransition; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.TimeInterpolator; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 24 | import android.annotation.SuppressLint; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 27 | import android.graphics.Matrix; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
| 32 | import android.util.AttributeSet; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 33 | import android.util.DisplayMetrics; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 35 | import android.view.InputDevice; |
| 36 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 37 | import android.view.MotionEvent; |
| 38 | import android.view.VelocityTracker; |
| 39 | import android.view.View; |
| 40 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 41 | import android.view.ViewDebug; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 42 | import android.view.ViewGroup; |
| 43 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 44 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 45 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 46 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 47 | import android.view.animation.Interpolator; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 49 | import com.android.launcher3.anim.PropertyListBuilder; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 50 | import com.android.launcher3.pageindicators.PageIndicator; |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 51 | import com.android.launcher3.touch.OverScroll; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 52 | import com.android.launcher3.util.Thunk; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 53 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
| 55 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 56 | /** |
| 57 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 58 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 59 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 60 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 62 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 63 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 64 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 65 | // the min drag distance for a fling to register, to prevent random page shifts |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 66 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 67 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 68 | public static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 69 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 70 | |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 71 | // OverScroll constants |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 72 | private final static int OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION = 270; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 73 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 74 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 75 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 76 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 77 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 78 | private static final float MAX_SCROLL_PROGRESS = 1.0f; |
| 79 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 80 | // The following constants need to be scaled based on density. The scaled versions will be |
| 81 | // assigned to the corresponding member variables below. |
| 82 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 83 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 84 | private static final int MIN_FLING_VELOCITY = 250; |
| 85 | |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 86 | public static final int INVALID_RESTORE_PAGE = -1001; |
| 87 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 88 | private boolean mFreeScroll = false; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 89 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 90 | protected int mFlingThresholdVelocity; |
| 91 | protected int mMinFlingVelocity; |
| 92 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 93 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 94 | protected boolean mFirstLayout = true; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 95 | private int mNormalChildHeight; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 96 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 97 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | protected int mCurrentPage; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 99 | private int mChildCountOnLastLayout; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 100 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 101 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 102 | protected int mNextPage = INVALID_PAGE; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 103 | protected int mMaxScrollX; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 104 | protected LauncherScroller mScroller; |
| 105 | private Interpolator mDefaultInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 106 | private VelocityTracker mVelocityTracker; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 107 | @Thunk int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 108 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 109 | private float mParentDownMotionX; |
| 110 | private float mParentDownMotionY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 111 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 112 | private float mDownMotionY; |
| 113 | private float mDownScrollX; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 114 | private float mDragViewBaselineLeft; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 115 | private float mLastMotionX; |
| 116 | private float mLastMotionXRemainder; |
| 117 | private float mLastMotionY; |
| 118 | private float mTotalMotionX; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 119 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 120 | private boolean mCancelTap; |
| 121 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 122 | private int[] mPageScrolls; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 123 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 124 | protected final static int TOUCH_STATE_REST = 0; |
| 125 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 126 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 127 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 128 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 129 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 130 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 131 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 132 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 134 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 135 | private int mMaximumVelocity; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 136 | protected boolean mAllowOverScroll = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 137 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 138 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 139 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 140 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 141 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 142 | protected boolean mIsPageInTransition = false; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 143 | |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 144 | protected boolean mWasInOverscroll = false; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 145 | |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 146 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise |
| 147 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 148 | // the screens from continuing to translate beyond the normal bounds. |
| 149 | protected int mOverScrollX; |
| 150 | |
| 151 | protected int mUnboundedScrollX; |
| 152 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 153 | // Page Indicator |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 154 | @Thunk int mPageIndicatorViewId; |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 155 | protected PageIndicator mPageIndicator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 156 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 157 | // viewport) |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 158 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 159 | private Rect mViewport = new Rect(); |
| 160 | |
| 161 | // Reordering |
| 162 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 163 | // dimensions such that when we are zoomed out, the view is not clipped |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 164 | private static int REORDERING_DROP_REPOSITION_DURATION = 200; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 165 | @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 166 | private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80; |
| 167 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 168 | private float mMinScale = 1f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 169 | private boolean mUseMinScale = false; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 170 | @Thunk View mDragView; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 171 | private Runnable mSidePageHoverRunnable; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 172 | @Thunk int mSidePageHoverIndex = -1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 173 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 174 | private boolean mReorderingStarted = false; |
| 175 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 176 | // animation after endReordering() |
| 177 | private boolean mIsReordering; |
| 178 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 179 | private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 180 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 181 | private Runnable mPostReorderingPreZoomInRunnable; |
| 182 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 183 | // Convenience/caching |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 184 | private static final Matrix sTmpInvMatrix = new Matrix(); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 185 | private static final float[] sTmpPoint = new float[2]; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 186 | private static final Rect sTmpRect = new Rect(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 187 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 188 | protected final Rect mInsets = new Rect(); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 189 | protected final boolean mIsRtl; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 190 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 191 | public PagedView(Context context) { |
| 192 | this(context, null); |
| 193 | } |
| 194 | |
| 195 | public PagedView(Context context, AttributeSet attrs) { |
| 196 | this(context, attrs, 0); |
| 197 | } |
| 198 | |
| 199 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 200 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 201 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 202 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 203 | R.styleable.PagedView, defStyle, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 204 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 205 | a.recycle(); |
| 206 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 207 | setHapticFeedbackEnabled(false); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 208 | mIsRtl = Utilities.isRtl(getResources()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 209 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Initializes various states for this workspace. |
| 214 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 215 | protected void init() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 216 | mScroller = new LauncherScroller(getContext()); |
| 217 | setDefaultInterpolator(new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 218 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 219 | |
| 220 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 221 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 222 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 223 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 224 | float density = getResources().getDisplayMetrics().density; |
| 225 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density); |
| 226 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density); |
| 227 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 228 | setOnHierarchyChangeListener(this); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 229 | setWillNotDraw(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 232 | protected void setDefaultInterpolator(Interpolator interpolator) { |
| 233 | mDefaultInterpolator = interpolator; |
| 234 | mScroller.setInterpolator(mDefaultInterpolator); |
| 235 | } |
| 236 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 237 | public void initParentViews(View parent) { |
| 238 | if (mPageIndicatorViewId > -1) { |
| 239 | mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId); |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 240 | mPageIndicator.setMarkersCount(getChildCount()); |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 241 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 245 | // Convenience methods to map points from self to parent and vice versa |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 246 | private float[] mapPointFromViewToParent(View v, float x, float y) { |
| 247 | sTmpPoint[0] = x; |
| 248 | sTmpPoint[1] = y; |
| 249 | v.getMatrix().mapPoints(sTmpPoint); |
| 250 | sTmpPoint[0] += v.getLeft(); |
| 251 | sTmpPoint[1] += v.getTop(); |
| 252 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 253 | } |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 254 | private float[] mapPointFromParentToView(View v, float x, float y) { |
| 255 | sTmpPoint[0] = x - v.getLeft(); |
| 256 | sTmpPoint[1] = y - v.getTop(); |
| 257 | v.getMatrix().invert(sTmpInvMatrix); |
| 258 | sTmpInvMatrix.mapPoints(sTmpPoint); |
| 259 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 262 | private void updateDragViewTranslationDuringDrag() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 263 | if (mDragView != null) { |
| 264 | float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) + |
| 265 | (mDragViewBaselineLeft - mDragView.getLeft()); |
| 266 | float y = mLastMotionY - mDownMotionY; |
| 267 | mDragView.setTranslationX(x); |
| 268 | mDragView.setTranslationY(y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 269 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 270 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " |
| 271 | + x + ", " + y); |
| 272 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | public void setMinScale(float f) { |
| 276 | mMinScale = f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 277 | mUseMinScale = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 278 | requestLayout(); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void setScaleX(float scaleX) { |
| 283 | super.setScaleX(scaleX); |
| 284 | if (isReordering(true)) { |
| 285 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 286 | mLastMotionX = p[0]; |
| 287 | mLastMotionY = p[1]; |
| 288 | updateDragViewTranslationDuringDrag(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
| 293 | // to be larger to account for the minimum possible scale) |
| 294 | int getViewportWidth() { |
| 295 | return mViewport.width(); |
| 296 | } |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 297 | public int getViewportHeight() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 298 | return mViewport.height(); |
| 299 | } |
| 300 | |
| 301 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
| 302 | // PagedView both horizontally and vertically |
| 303 | int getViewportOffsetX() { |
| 304 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
| 305 | } |
| 306 | |
| 307 | int getViewportOffsetY() { |
| 308 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
| 309 | } |
| 310 | |
Hyunyoung Song | 7744169 | 2016-06-27 22:00:48 -0700 | [diff] [blame] | 311 | public PageIndicator getPageIndicator() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 312 | return mPageIndicator; |
| 313 | } |
| 314 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 315 | /** |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 316 | * Returns the index of the currently displayed page. When in free scroll mode, this is the page |
| 317 | * that the user was on before entering free scroll mode (e.g. the home screen page they |
| 318 | * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()} |
| 319 | * to get the page the user is currently scrolling over. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 321 | public int getCurrentPage() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 322 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 323 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 324 | |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Returns the index of page to be shown immediately afterwards. |
| 327 | */ |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 328 | public int getNextPage() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 329 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 330 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 331 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 332 | public int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 333 | return getChildCount(); |
| 334 | } |
| 335 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 336 | public View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 337 | return getChildAt(index); |
| 338 | } |
| 339 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 340 | protected int indexToPage(int index) { |
| 341 | return index; |
| 342 | } |
| 343 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 344 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 345 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 346 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 347 | * the previous tab page. |
| 348 | */ |
| 349 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 350 | // If the current page is invalid, just reset the scroll position to zero |
| 351 | int newX = 0; |
| 352 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 353 | newX = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 354 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 355 | scrollTo(newX, 0); |
| 356 | mScroller.setFinalX(newX); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 357 | forceFinishScroller(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 360 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 361 | mScroller.abortAnimation(); |
| 362 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 363 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 364 | if (resetNextPage) { |
| 365 | mNextPage = INVALID_PAGE; |
| 366 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 367 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 368 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 369 | private void forceFinishScroller(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 370 | mScroller.forceFinished(true); |
| 371 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 372 | // updating current page on the pass. |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 373 | if (resetNextPage) { |
| 374 | mNextPage = INVALID_PAGE; |
| 375 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 378 | private int validateNewPage(int newPage) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 379 | // Ensure that it is clamped by the actual set of children in all cases |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 380 | return Utilities.boundToRange(newPage, 0, getPageCount() - 1); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 383 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 384 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 385 | */ |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 386 | public void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 387 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 388 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 389 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 390 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 391 | // the default |
| 392 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 393 | return; |
| 394 | } |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 395 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 396 | mCurrentPage = validateNewPage(currentPage); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 397 | updateCurrentPageScroll(); |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 398 | notifyPageSwitchListener(prevPage); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 399 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 402 | /** |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 403 | * Should be called whenever the page changes. In the case of a scroll, we wait until the page |
| 404 | * has settled. |
| 405 | */ |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 406 | protected void notifyPageSwitchListener(int prevPage) { |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 407 | updatePageIndicator(); |
| 408 | } |
| 409 | |
| 410 | private void updatePageIndicator() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 411 | // Update the page indicator (when we aren't reordering) |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 412 | if (mPageIndicator != null) { |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 413 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 414 | if (!isReordering(false)) { |
| 415 | mPageIndicator.setActiveMarker(getNextPage()); |
| 416 | } |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 417 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 418 | } |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 419 | protected void pageBeginTransition() { |
| 420 | if (!mIsPageInTransition) { |
| 421 | mIsPageInTransition = true; |
| 422 | onPageBeginTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 423 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 426 | protected void pageEndTransition() { |
| 427 | if (mIsPageInTransition) { |
| 428 | mIsPageInTransition = false; |
| 429 | onPageEndTransition(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 430 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 433 | protected boolean isPageInTransition() { |
| 434 | return mIsPageInTransition; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 437 | /** |
| 438 | * Called when the page starts moving as part of the scroll. Subclasses can override this |
| 439 | * to provide custom behavior during animation. |
| 440 | */ |
| 441 | protected void onPageBeginTransition() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Called when the page ends moving as part of the scroll. Subclasses can override this |
| 446 | * to provide custom behavior during animation. |
| 447 | */ |
| 448 | protected void onPageEndTransition() { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 449 | mWasInOverscroll = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 452 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 453 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 454 | * |
| 455 | * @param l The listener used to respond to long clicks. |
| 456 | */ |
| 457 | @Override |
| 458 | public void setOnLongClickListener(OnLongClickListener l) { |
| 459 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 460 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 461 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 462 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 463 | } |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 464 | super.setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 467 | protected int getUnboundedScrollX() { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 468 | return mUnboundedScrollX; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 471 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 472 | public void scrollBy(int x, int y) { |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 473 | scrollTo(getUnboundedScrollX() + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 477 | public void scrollTo(int x, int y) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 478 | // In free scroll mode, we clamp the scrollX |
| 479 | if (mFreeScroll) { |
Adam Cohen | b2b02b9 | 2015-06-10 18:40:05 -0700 | [diff] [blame] | 480 | // If the scroller is trying to move to a location beyond the maximum allowed |
| 481 | // in the free scroll mode, we make sure to end the scroll operation. |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 482 | if (!mScroller.isFinished() && (x > mMaxScrollX || x < 0)) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 483 | forceFinishScroller(false); |
Adam Cohen | b2b02b9 | 2015-06-10 18:40:05 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 486 | x = Utilities.boundToRange(x, 0, mMaxScrollX); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 489 | mUnboundedScrollX = x; |
| 490 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 491 | boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0); |
| 492 | boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 493 | if (isXBeforeFirstPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 494 | super.scrollTo(mIsRtl ? mMaxScrollX : 0, y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 495 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 496 | mWasInOverscroll = true; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 497 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 498 | overScroll(x - mMaxScrollX); |
| 499 | } else { |
| 500 | overScroll(x); |
| 501 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 502 | } |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 503 | } else if (isXAfterLastPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 504 | super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 505 | if (mAllowOverScroll) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 506 | mWasInOverscroll = true; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 507 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 508 | overScroll(x); |
| 509 | } else { |
| 510 | overScroll(x - mMaxScrollX); |
| 511 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 512 | } |
| 513 | } else { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 514 | if (mWasInOverscroll) { |
| 515 | overScroll(0); |
| 516 | mWasInOverscroll = false; |
| 517 | } |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 518 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 519 | super.scrollTo(x, y); |
| 520 | } |
| 521 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 522 | // Update the last motion events when scrolling |
| 523 | if (isReordering(true)) { |
| 524 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 525 | mLastMotionX = p[0]; |
| 526 | mLastMotionY = p[1]; |
| 527 | updateDragViewTranslationDuringDrag(); |
| 528 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 531 | private void sendScrollAccessibilityEvent() { |
| 532 | AccessibilityManager am = |
| 533 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 534 | if (am.isEnabled()) { |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 535 | if (mCurrentPage != getNextPage()) { |
| 536 | AccessibilityEvent ev = |
| 537 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 538 | ev.setScrollable(true); |
| 539 | ev.setScrollX(getScrollX()); |
| 540 | ev.setScrollY(getScrollY()); |
| 541 | ev.setMaxScrollX(mMaxScrollX); |
| 542 | ev.setMaxScrollY(0); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 543 | |
Vadim Tryshev | f471597 | 2015-05-08 17:21:03 -0700 | [diff] [blame] | 544 | sendAccessibilityEventUnchecked(ev); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 545 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 549 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 550 | protected boolean computeScrollHelper() { |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 551 | return computeScrollHelper(true); |
| 552 | } |
| 553 | |
| 554 | protected boolean computeScrollHelper(boolean shouldInvalidate) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 555 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 556 | // Don't bother scrolling if the page does not need to be moved |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 557 | if (getUnboundedScrollX() != mScroller.getCurrX() |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 558 | || getScrollY() != mScroller.getCurrY() |
| 559 | || mOverScrollX != mScroller.getCurrX()) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 560 | float scaleX = mFreeScroll ? getScaleX() : 1f; |
| 561 | int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX)); |
| 562 | scrollTo(scrollX, mScroller.getCurrY()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 563 | } |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 564 | if (shouldInvalidate) { |
| 565 | invalidate(); |
| 566 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 567 | return true; |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 568 | } else if (mNextPage != INVALID_PAGE && shouldInvalidate) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 569 | sendScrollAccessibilityEvent(); |
| 570 | |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 571 | int prevPage = mCurrentPage; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 572 | mCurrentPage = validateNewPage(mNextPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 573 | mNextPage = INVALID_PAGE; |
Hyunyoung Song | 3f5a11f | 2017-06-29 16:42:04 -0700 | [diff] [blame] | 574 | notifyPageSwitchListener(prevPage); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 575 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 576 | // We don't want to trigger a page end moving unless the page has settled |
| 577 | // and the user has stopped scrolling |
| 578 | if (mTouchState == TOUCH_STATE_REST) { |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 579 | pageEndTransition(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 580 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 581 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 582 | onPostReorderingAnimationCompleted(); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 583 | AccessibilityManager am = (AccessibilityManager) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 584 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 585 | if (am.isEnabled()) { |
| 586 | // Notify the user when the page changes |
| 587 | announceForAccessibility(getCurrentPageDescription()); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 588 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 589 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 590 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public void computeScroll() { |
| 596 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 599 | public static class LayoutParams extends ViewGroup.LayoutParams { |
| 600 | public boolean isFullScreenPage = false; |
| 601 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 602 | // If true, the start edge of the page snaps to the start edge of the viewport. |
| 603 | public boolean matchStartEdge = false; |
| 604 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 605 | /** |
| 606 | * {@inheritDoc} |
| 607 | */ |
| 608 | public LayoutParams(int width, int height) { |
| 609 | super(width, height); |
| 610 | } |
| 611 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 612 | public LayoutParams(Context context, AttributeSet attrs) { |
| 613 | super(context, attrs); |
| 614 | } |
| 615 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 616 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 617 | super(source); |
| 618 | } |
| 619 | } |
| 620 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 621 | @Override |
| 622 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 623 | return new LayoutParams(getContext(), attrs); |
| 624 | } |
| 625 | |
| 626 | @Override |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 627 | protected LayoutParams generateDefaultLayoutParams() { |
| 628 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 629 | } |
| 630 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 631 | @Override |
| 632 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 633 | return new LayoutParams(p); |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 638 | return p instanceof LayoutParams; |
| 639 | } |
| 640 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 641 | public void addFullScreenPage(View page) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 642 | LayoutParams lp = generateDefaultLayoutParams(); |
| 643 | lp.isFullScreenPage = true; |
| 644 | super.addView(page, 0, lp); |
| 645 | } |
| 646 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 647 | public int getNormalChildHeight() { |
| 648 | return mNormalChildHeight; |
| 649 | } |
| 650 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 651 | @Override |
| 652 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 653 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 654 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 655 | return; |
| 656 | } |
| 657 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 658 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 659 | // zoom out (and scale down), the view is still contained in the parent |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 660 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 661 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 662 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 663 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 664 | // NOTE: We multiply by 2f to account for the fact that depending on the offset of the |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 665 | // viewport, we can be at most one and a half screens offset once we scale down |
| 666 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 667 | int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right, |
| 668 | dm.heightPixels + mInsets.top + mInsets.bottom); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 669 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 670 | int parentWidthSize = (int) (2f * maxSize); |
| 671 | int parentHeightSize = (int) (2f * maxSize); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 672 | int scaledWidthSize, scaledHeightSize; |
| 673 | if (mUseMinScale) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 674 | scaledWidthSize = (int) (parentWidthSize / mMinScale); |
| 675 | scaledHeightSize = (int) (parentHeightSize / mMinScale); |
| 676 | } else { |
| 677 | scaledWidthSize = widthSize; |
| 678 | scaledHeightSize = heightSize; |
| 679 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 680 | mViewport.set(0, 0, widthSize, heightSize); |
| 681 | |
| 682 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 683 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 684 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 687 | // Return early if we aren't given a proper dimension |
| 688 | if (widthSize <= 0 || heightSize <= 0) { |
| 689 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 690 | return; |
| 691 | } |
| 692 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 693 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 694 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 695 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 696 | * each page to have the same width. |
| 697 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 698 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 699 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 700 | |
Adam Cohen | 1d3d4f1 | 2014-08-14 19:14:52 -0700 | [diff] [blame] | 701 | int referenceChildWidth = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 702 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 703 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 704 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 705 | if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize); |
| 706 | if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize); |
| 707 | if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding); |
| 708 | if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 709 | final int childCount = getChildCount(); |
| 710 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 711 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 712 | final View child = getPageAt(i); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 713 | if (child.getVisibility() != GONE) { |
| 714 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 715 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 716 | int childWidthMode; |
| 717 | int childHeightMode; |
| 718 | int childWidth; |
| 719 | int childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 720 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 721 | if (!lp.isFullScreenPage) { |
| 722 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 723 | childWidthMode = MeasureSpec.AT_MOST; |
| 724 | } else { |
| 725 | childWidthMode = MeasureSpec.EXACTLY; |
| 726 | } |
| 727 | |
| 728 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 729 | childHeightMode = MeasureSpec.AT_MOST; |
| 730 | } else { |
| 731 | childHeightMode = MeasureSpec.EXACTLY; |
| 732 | } |
| 733 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 734 | childWidth = getViewportWidth() - horizontalPadding |
| 735 | - mInsets.left - mInsets.right; |
| 736 | childHeight = getViewportHeight() - verticalPadding |
| 737 | - mInsets.top - mInsets.bottom; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 738 | mNormalChildHeight = childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 739 | } else { |
| 740 | childWidthMode = MeasureSpec.EXACTLY; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 741 | childHeightMode = MeasureSpec.EXACTLY; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 742 | |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 743 | childWidth = getViewportWidth(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 744 | childHeight = getViewportHeight(); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 745 | } |
Adam Cohen | 1d3d4f1 | 2014-08-14 19:14:52 -0700 | [diff] [blame] | 746 | if (referenceChildWidth == 0) { |
| 747 | referenceChildWidth = childWidth; |
| 748 | } |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 749 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 750 | final int childWidthMeasureSpec = |
| 751 | MeasureSpec.makeMeasureSpec(childWidth, childWidthMode); |
| 752 | final int childHeightMeasureSpec = |
| 753 | MeasureSpec.makeMeasureSpec(childHeight, childHeightMode); |
| 754 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 755 | } |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 756 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 757 | setMeasuredDimension(scaledWidthSize, scaledHeightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 760 | @SuppressLint("DrawAllocation") |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 761 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 762 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 763 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 767 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 768 | final int childCount = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 769 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 770 | int offsetX = getViewportOffsetX(); |
| 771 | int offsetY = getViewportOffsetY(); |
| 772 | |
| 773 | // Update the viewport offsets |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 774 | mViewport.offset(offsetX, offsetY); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 775 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 776 | final int startIndex = mIsRtl ? childCount - 1 : 0; |
| 777 | final int endIndex = mIsRtl ? -1 : childCount; |
| 778 | final int delta = mIsRtl ? -1 : 1; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 779 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 780 | int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 781 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 782 | LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams(); |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 783 | LayoutParams nextLp; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 784 | |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 785 | int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft()); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 786 | if (mPageScrolls == null || childCount != mChildCountOnLastLayout) { |
| 787 | mPageScrolls = new int[childCount]; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 790 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 791 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 792 | if (child.getVisibility() != View.GONE) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 793 | lp = (LayoutParams) child.getLayoutParams(); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 794 | int childTop; |
| 795 | if (lp.isFullScreenPage) { |
| 796 | childTop = offsetY; |
| 797 | } else { |
| 798 | childTop = offsetY + getPaddingTop() + mInsets.top; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 799 | childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 802 | final int childWidth = child.getMeasuredWidth(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 803 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 804 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 805 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 806 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 807 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 808 | |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 809 | int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 810 | mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX; |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 811 | |
| 812 | int pageGap = mPageSpacing; |
| 813 | int next = i + delta; |
| 814 | if (next != endIndex) { |
| 815 | nextLp = (LayoutParams) getPageAt(next).getLayoutParams(); |
| 816 | } else { |
| 817 | nextLp = null; |
| 818 | } |
| 819 | |
| 820 | // Prevent full screen pages from showing in the viewport |
| 821 | // when they are not the current page. |
| 822 | if (lp.isFullScreenPage) { |
| 823 | pageGap = getPaddingLeft(); |
| 824 | } else if (nextLp != null && nextLp.isFullScreenPage) { |
| 825 | pageGap = getPaddingRight(); |
| 826 | } |
| 827 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 828 | childLeft += childWidth + pageGap + getChildGap(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 831 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 832 | final LayoutTransition transition = getLayoutTransition(); |
| 833 | // If the transition is running defer updating max scroll, as some empty pages could |
| 834 | // still be present, and a max scroll change could cause sudden jumps in scroll. |
| 835 | if (transition != null && transition.isRunning()) { |
| 836 | transition.addTransitionListener(new LayoutTransition.TransitionListener() { |
| 837 | |
| 838 | @Override |
| 839 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 840 | View view, int transitionType) { } |
| 841 | |
| 842 | @Override |
| 843 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 844 | View view, int transitionType) { |
| 845 | // Wait until all transitions are complete. |
| 846 | if (!transition.isRunning()) { |
| 847 | transition.removeTransitionListener(this); |
| 848 | updateMaxScrollX(); |
| 849 | } |
| 850 | } |
| 851 | }); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 852 | } else { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 853 | updateMaxScrollX(); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 856 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) { |
| 857 | updateCurrentPageScroll(); |
| 858 | mFirstLayout = false; |
| 859 | } |
| 860 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 861 | if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) { |
Sunny Goyal | fe770c9 | 2016-09-27 14:38:58 -0700 | [diff] [blame] | 862 | setCurrentPage(getNextPage()); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 863 | } |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 864 | mChildCountOnLastLayout = childCount; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 865 | |
| 866 | if (isReordering(true)) { |
| 867 | updateDragViewTranslationDuringDrag(); |
| 868 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 871 | protected int getChildGap() { |
| 872 | return 0; |
| 873 | } |
| 874 | |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 875 | private void updateMaxScrollX() { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 876 | mMaxScrollX = computeMaxScrollX(); |
| 877 | } |
| 878 | |
| 879 | protected int computeMaxScrollX() { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 880 | int childCount = getChildCount(); |
| 881 | if (childCount > 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 882 | final int index = mIsRtl ? 0 : childCount - 1; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 883 | return getScrollForPage(index); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 884 | } else { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 885 | return 0; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 889 | public void setPageSpacing(int pageSpacing) { |
| 890 | mPageSpacing = pageSpacing; |
| 891 | requestLayout(); |
| 892 | } |
| 893 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 894 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 895 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 896 | // Update the page indicator, we don't update the page indicator as we |
| 897 | // add/remove pages |
| 898 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 899 | mPageIndicator.addMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 902 | // This ensures that when children are added, they get the correct transforms / alphas |
| 903 | // in accordance with any scroll effects. |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 904 | invalidate(); |
| 905 | } |
| 906 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 907 | @Override |
| 908 | public void onChildViewRemoved(View parent, View child) { |
Tony | a361c72 | 2017-07-04 09:43:06 -0700 | [diff] [blame] | 909 | mCurrentPage = validateNewPage(mCurrentPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 910 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 913 | private void removeMarkerForView() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 914 | // Update the page indicator, we don't update the page indicator as we |
| 915 | // add/remove pages |
| 916 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 917 | mPageIndicator.removeMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | @Override |
| 922 | public void removeView(View v) { |
| 923 | // XXX: We should find a better way to hook into this before the view |
| 924 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 925 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 926 | super.removeView(v); |
| 927 | } |
| 928 | @Override |
| 929 | public void removeViewInLayout(View v) { |
| 930 | // XXX: We should find a better way to hook into this before the view |
| 931 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 932 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 933 | super.removeViewInLayout(v); |
| 934 | } |
| 935 | @Override |
| 936 | public void removeViewAt(int index) { |
| 937 | // XXX: We should find a better way to hook into this before the view |
| 938 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 939 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 940 | super.removeViewAt(index); |
| 941 | } |
| 942 | @Override |
| 943 | public void removeAllViewsInLayout() { |
| 944 | // Update the page indicator, we don't update the page indicator as we |
| 945 | // add/remove pages |
| 946 | if (mPageIndicator != null) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 947 | mPageIndicator.setMarkersCount(0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | super.removeAllViewsInLayout(); |
| 951 | } |
| 952 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 953 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 954 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 955 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 956 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 957 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 958 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 961 | @Override |
| 962 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 963 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 964 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 965 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | @Override |
| 972 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 973 | int focusablePage; |
| 974 | if (mNextPage != INVALID_PAGE) { |
| 975 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 976 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 977 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 978 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 979 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 980 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 981 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 982 | } |
| 983 | return false; |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 988 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 989 | return true; |
| 990 | } |
| 991 | |
| 992 | if (mIsRtl) { |
| 993 | if (direction == View.FOCUS_LEFT) { |
| 994 | direction = View.FOCUS_RIGHT; |
| 995 | } else if (direction == View.FOCUS_RIGHT) { |
| 996 | direction = View.FOCUS_LEFT; |
| 997 | } |
| 998 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 999 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1000 | if (getCurrentPage() > 0) { |
| 1001 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1002 | return true; |
| 1003 | } |
| 1004 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1005 | if (getCurrentPage() < getPageCount() - 1) { |
| 1006 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1007 | return true; |
| 1008 | } |
| 1009 | } |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 1010 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | @Override |
| 1014 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Jon Miranda | 6a85817 | 2016-10-25 16:19:17 -0700 | [diff] [blame] | 1015 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1019 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1020 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1021 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1022 | } |
| 1023 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1024 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1025 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1028 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1029 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * If one of our descendant views decides that it could be focused now, only |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1036 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1037 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1038 | * This happens when live folders requery, and if they're off page, they |
| 1039 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1040 | */ |
| 1041 | @Override |
| 1042 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1043 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1044 | View v = focused; |
| 1045 | while (true) { |
| 1046 | if (v == current) { |
| 1047 | super.focusableViewAvailable(focused); |
| 1048 | return; |
| 1049 | } |
| 1050 | if (v == this) { |
| 1051 | return; |
| 1052 | } |
| 1053 | ViewParent parent = v.getParent(); |
| 1054 | if (parent instanceof View) { |
| 1055 | v = (View)v.getParent(); |
| 1056 | } else { |
| 1057 | return; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * {@inheritDoc} |
| 1064 | */ |
| 1065 | @Override |
| 1066 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1067 | if (disallowIntercept) { |
| 1068 | // We need to make sure to cancel our long press if |
| 1069 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1070 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1071 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1072 | } |
| 1073 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1074 | } |
| 1075 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1076 | /** |
| 1077 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1078 | */ |
| 1079 | protected boolean hitsPreviousPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1080 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1081 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1082 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1083 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1084 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1089 | */ |
| 1090 | protected boolean hitsNextPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1091 | if (mIsRtl) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1092 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1093 | } |
| 1094 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1095 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1096 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1097 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1098 | /** Returns whether x and y originated within the buffered viewport */ |
| 1099 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1100 | sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1101 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1102 | return sTmpRect.contains(x, y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1105 | @Override |
| 1106 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 1107 | /* |
| 1108 | * This method JUST determines whether we want to intercept the motion. |
| 1109 | * If we return true, onTouchEvent will be called and we do the actual |
| 1110 | * scrolling there. |
| 1111 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1112 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1113 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1114 | // Skip touch handling if there are no pages to swipe |
| 1115 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1116 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1117 | /* |
| 1118 | * Shortcut the most recurring case: the user is in the dragging |
| 1119 | * state and he is moving his finger. We want to intercept this |
| 1120 | * motion. |
| 1121 | */ |
| 1122 | final int action = ev.getAction(); |
| 1123 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1124 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1128 | switch (action & MotionEvent.ACTION_MASK) { |
| 1129 | case MotionEvent.ACTION_MOVE: { |
| 1130 | /* |
| 1131 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1132 | * whether the user has moved far enough from his original down touch. |
| 1133 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1134 | if (mActivePointerId != INVALID_POINTER) { |
| 1135 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1136 | } |
| 1137 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1138 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1139 | // i.e. fall through to the next case (don't break) |
| 1140 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1141 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1142 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | case MotionEvent.ACTION_DOWN: { |
| 1146 | final float x = ev.getX(); |
| 1147 | final float y = ev.getY(); |
| 1148 | // Remember location of down touch |
| 1149 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1150 | mDownMotionY = y; |
| 1151 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1152 | mLastMotionX = x; |
| 1153 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1154 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1155 | mParentDownMotionX = p[0]; |
| 1156 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1157 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1158 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1159 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1160 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * If being flinged and user touches the screen, initiate drag; |
| 1163 | * otherwise don't. mScroller.isFinished should be false when |
| 1164 | * being flinged. |
| 1165 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1166 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1167 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1168 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1169 | if (finishedScrolling) { |
| 1170 | mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 59b5c79 | 2013-12-04 16:09:07 -0800 | [diff] [blame] | 1171 | if (!mScroller.isFinished() && !mFreeScroll) { |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1172 | setCurrentPage(getNextPage()); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1173 | pageEndTransition(); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1174 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1175 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1176 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1177 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1178 | } else { |
| 1179 | mTouchState = TOUCH_STATE_REST; |
| 1180 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1181 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1182 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1183 | break; |
| 1184 | } |
| 1185 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1186 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1187 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1188 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | case MotionEvent.ACTION_POINTER_UP: |
| 1192 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1193 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * The only time we want to intercept motion events is if we are in the |
| 1199 | * drag mode. |
| 1200 | */ |
| 1201 | return mTouchState != TOUCH_STATE_REST; |
| 1202 | } |
| 1203 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1204 | protected void determineScrollingStart(MotionEvent ev) { |
| 1205 | determineScrollingStart(ev, 1.0f); |
| 1206 | } |
| 1207 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * Determines if we should change the touch state to start scrolling after the |
| 1210 | * user moves their touch point too far. |
| 1211 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1212 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1213 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1214 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1215 | if (pointerIndex == -1) return; |
| 1216 | |
| 1217 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1218 | final float x = ev.getX(pointerIndex); |
| 1219 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1220 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1221 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1222 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1223 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1224 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1225 | boolean xMoved = xDiff > touchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1226 | |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1227 | if (xMoved) { |
| 1228 | // Scroll if the user moved far enough along the X axis |
| 1229 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1230 | mTotalMotionX += Math.abs(mLastMotionX - x); |
| 1231 | mLastMotionX = x; |
| 1232 | mLastMotionXRemainder = 0; |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1233 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1234 | pageBeginTransition(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1235 | // Stop listening for things like pinches. |
| 1236 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1241 | // Try canceling the long press. It could also have been scheduled |
| 1242 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1243 | // everything |
| 1244 | final View currentPage = getPageAt(mCurrentPage); |
| 1245 | if (currentPage != null) { |
| 1246 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1250 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1251 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1252 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1253 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1254 | int count = getChildCount(); |
| 1255 | |
| 1256 | final int totalDistance; |
| 1257 | |
| 1258 | int adjacentPage = page + 1; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1259 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1260 | adjacentPage = page - 1; |
| 1261 | } |
| 1262 | |
| 1263 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1264 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1265 | } else { |
| 1266 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1267 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1268 | |
| 1269 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1270 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
| 1271 | scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1272 | return scrollProgress; |
| 1273 | } |
| 1274 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1275 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1276 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1277 | return 0; |
| 1278 | } else { |
| 1279 | return mPageScrolls[index]; |
| 1280 | } |
| 1281 | } |
| 1282 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1283 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1284 | // position. This method returns the magnitude of this stray at any given time. |
| 1285 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1286 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1287 | return 0; |
| 1288 | } else { |
| 1289 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1290 | |
| 1291 | int scrollOffset = 0; |
| 1292 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1293 | if (!lp.isFullScreenPage) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1294 | scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1297 | int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX(); |
| 1298 | return (int) (child.getX() - baselineX); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1302 | protected void dampedOverScroll(float amount) { |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 1303 | if (Float.compare(amount, 0f) == 0) return; |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1304 | |
Sunny Goyal | b72d8b2 | 2017-07-14 00:02:27 -0700 | [diff] [blame] | 1305 | int overScrollAmount = OverScroll.dampedScroll(amount, getViewportWidth()); |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1306 | if (amount < 0) { |
| 1307 | mOverScrollX = overScrollAmount; |
| 1308 | super.scrollTo(mOverScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1309 | } else { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1310 | mOverScrollX = mMaxScrollX + overScrollAmount; |
| 1311 | super.scrollTo(mOverScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1312 | } |
| 1313 | invalidate(); |
| 1314 | } |
| 1315 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1316 | protected void overScroll(float amount) { |
| 1317 | dampedOverScroll(amount); |
| 1318 | } |
| 1319 | |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 1320 | /** |
| 1321 | * return true if freescroll has been enabled, false otherwise |
| 1322 | */ |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1323 | protected void enableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1324 | setEnableFreeScroll(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Sunny Goyal | c99cb17 | 2017-10-19 16:15:09 -0700 | [diff] [blame] | 1327 | protected void disableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1328 | setEnableFreeScroll(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1331 | private void setEnableFreeScroll(boolean freeScroll) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1332 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1333 | mFreeScroll = freeScroll; |
| 1334 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1335 | if (mFreeScroll) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1336 | setCurrentPage(getNextPage()); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1337 | } else if (wasFreeScroll) { |
| 1338 | snapToPage(getNextPage()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | setEnableOverscroll(!freeScroll); |
| 1342 | } |
| 1343 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1344 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1345 | mAllowOverScroll = enable; |
| 1346 | } |
| 1347 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1348 | private int getNearestHoverOverPageIndex() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1349 | if (mDragView != null) { |
| 1350 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1351 | + mDragView.getTranslationX()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1352 | int minDistance = Integer.MAX_VALUE; |
| 1353 | int minIndex = indexOfChild(mDragView); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1354 | int maxPageNo = getChildCount() - 1; |
| 1355 | for (int i = 0; i <= maxPageNo; i++) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1356 | View page = getPageAt(i); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1357 | int pageX = (page.getLeft() + page.getMeasuredWidth() / 2); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1358 | int d = Math.abs(dragX - pageX); |
| 1359 | if (d < minDistance) { |
| 1360 | minIndex = i; |
| 1361 | minDistance = d; |
| 1362 | } |
| 1363 | } |
| 1364 | return minIndex; |
| 1365 | } |
| 1366 | return -1; |
| 1367 | } |
| 1368 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1369 | @Override |
| 1370 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1371 | super.onTouchEvent(ev); |
| 1372 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1373 | // Skip touch handling if there are no pages to swipe |
| 1374 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1375 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1376 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1377 | |
| 1378 | final int action = ev.getAction(); |
| 1379 | |
| 1380 | switch (action & MotionEvent.ACTION_MASK) { |
| 1381 | case MotionEvent.ACTION_DOWN: |
| 1382 | /* |
| 1383 | * If being flinged and user touches, stop the fling. isFinished |
| 1384 | * will be false if being flinged. |
| 1385 | */ |
| 1386 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1387 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | // Remember where the motion event started |
| 1391 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1392 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1393 | mDownScrollX = getScrollX(); |
| 1394 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1395 | mParentDownMotionX = p[0]; |
| 1396 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1397 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1398 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1399 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1400 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1401 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1402 | onScrollInteractionBegin(); |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1403 | pageBeginTransition(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1404 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | |
| 1407 | case MotionEvent.ACTION_MOVE: |
| 1408 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1409 | // Scroll to follow the motion event |
| 1410 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1411 | |
| 1412 | if (pointerIndex == -1) return true; |
| 1413 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1414 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1415 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1416 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1417 | mTotalMotionX += Math.abs(deltaX); |
| 1418 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1419 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1420 | // keep the remainder because we are actually testing if we've moved from the last |
| 1421 | // scrolled position (which is discrete). |
| 1422 | if (Math.abs(deltaX) >= 1.0f) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1423 | scrollBy((int) deltaX, 0); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1424 | mLastMotionX = x; |
| 1425 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1426 | } else { |
| 1427 | awakenScrollBars(); |
| 1428 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1429 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1430 | // Update the last motion position |
| 1431 | mLastMotionX = ev.getX(); |
| 1432 | mLastMotionY = ev.getY(); |
| 1433 | |
| 1434 | // Update the parent down so that our zoom animations take this new movement into |
| 1435 | // account |
| 1436 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1437 | mParentDownMotionX = pt[0]; |
| 1438 | mParentDownMotionY = pt[1]; |
| 1439 | updateDragViewTranslationDuringDrag(); |
| 1440 | |
| 1441 | // Find the closest page to the touch point |
| 1442 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1443 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1444 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1445 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1446 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1447 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1448 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1449 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1450 | // Do not allow any page to be moved to 0th position. |
| 1451 | if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) { |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1452 | if (0 <= pageUnderPointIndex && pageUnderPointIndex <= getPageCount() - 1 && |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1453 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1454 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1455 | mSidePageHoverRunnable = new Runnable() { |
| 1456 | @Override |
| 1457 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1458 | // Setup the scroll to the correct page before we swap the views |
| 1459 | snapToPage(pageUnderPointIndex); |
| 1460 | |
| 1461 | // For each of the pages between the paged view and the drag view, |
| 1462 | // animate them from the previous position to the new position in |
| 1463 | // the layout (as a result of the drag view moving in the layout) |
| 1464 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1465 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1466 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1467 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1468 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1469 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1470 | View v = getChildAt(i); |
| 1471 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1472 | // drag view all subsequent views to pageUnderPointIndex will |
| 1473 | // shift down. |
| 1474 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1475 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1476 | |
| 1477 | // Animate the view translation from its old position to its new |
| 1478 | // position |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1479 | ObjectAnimator anim = (ObjectAnimator) v.getTag(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1480 | if (anim != null) { |
| 1481 | anim.cancel(); |
| 1482 | } |
| 1483 | |
| 1484 | v.setTranslationX(oldX - newX); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1485 | anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1486 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1487 | anim.start(); |
| 1488 | v.setTag(anim); |
| 1489 | } |
| 1490 | |
| 1491 | removeView(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1492 | addView(mDragView, pageUnderPointIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1493 | mSidePageHoverIndex = -1; |
Winson Chung | 876a619 | 2013-11-06 14:49:50 -0800 | [diff] [blame] | 1494 | if (mPageIndicator != null) { |
| 1495 | mPageIndicator.setActiveMarker(getNextPage()); |
| 1496 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1497 | } |
| 1498 | }; |
| 1499 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1500 | } |
| 1501 | } else { |
| 1502 | removeCallbacks(mSidePageHoverRunnable); |
| 1503 | mSidePageHoverIndex = -1; |
| 1504 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1505 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1506 | determineScrollingStart(ev); |
| 1507 | } |
| 1508 | break; |
| 1509 | |
| 1510 | case MotionEvent.ACTION_UP: |
| 1511 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1512 | final int activePointerId = mActivePointerId; |
| 1513 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1514 | final float x = ev.getX(pointerIndex); |
| 1515 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1516 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1517 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1518 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1519 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1520 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1521 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1522 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1523 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1524 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1525 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1526 | shouldFlingForVelocity(velocityX); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1527 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1528 | if (!mFreeScroll) { |
| 1529 | // In the case that the page is moved far to one direction and then is flung |
| 1530 | // in the opposite direction, we use a threshold to determine whether we should |
| 1531 | // just return to the starting page, or if we should skip one further. |
| 1532 | boolean returnToOriginalPage = false; |
| 1533 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1534 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1535 | returnToOriginalPage = true; |
| 1536 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1537 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1538 | int finalPage; |
| 1539 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1540 | // test for a large move if a fling has been registered. That is, a large |
| 1541 | // move to the left and fling to the right will register as a fling to the right. |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1542 | boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0; |
| 1543 | boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1544 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1545 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1546 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1547 | snapToPageWithVelocity(finalPage, velocityX); |
| 1548 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1549 | (isFling && isVelocityXLeft)) && |
| 1550 | mCurrentPage < getChildCount() - 1) { |
| 1551 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1552 | snapToPageWithVelocity(finalPage, velocityX); |
| 1553 | } else { |
| 1554 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1555 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1556 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1557 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1558 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | float scaleX = getScaleX(); |
| 1562 | int vX = (int) (-velocityX * scaleX); |
| 1563 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1564 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1565 | mScroller.setInterpolator(mDefaultInterpolator); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1566 | mScroller.fling(initialScrollX, |
| 1567 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1568 | mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX)); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1569 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1570 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1571 | onScrollInteractionEnd(); |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 1572 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1573 | // at this point we have not moved beyond the touch slop |
| 1574 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1575 | // we can just page |
| 1576 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1577 | if (nextPage != mCurrentPage) { |
| 1578 | snapToPage(nextPage); |
| 1579 | } else { |
| 1580 | snapToDestination(); |
| 1581 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1582 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1583 | // at this point we have not moved beyond the touch slop |
| 1584 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1585 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1586 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1587 | if (nextPage != mCurrentPage) { |
| 1588 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1589 | } else { |
| 1590 | snapToDestination(); |
| 1591 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1592 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1593 | // Update the last motion position |
| 1594 | mLastMotionX = ev.getX(); |
| 1595 | mLastMotionY = ev.getY(); |
| 1596 | |
| 1597 | // Update the parent down so that our zoom animations take this new movement into |
| 1598 | // account |
| 1599 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1600 | mParentDownMotionX = pt[0]; |
| 1601 | mParentDownMotionY = pt[1]; |
| 1602 | updateDragViewTranslationDuringDrag(); |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1603 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1604 | if (!mCancelTap) { |
| 1605 | onUnhandledTap(ev); |
| 1606 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1607 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1608 | |
| 1609 | // Remove the callback to wait for the side page hover timeout |
| 1610 | removeCallbacks(mSidePageHoverRunnable); |
| 1611 | // End any intermediate reordering states |
| 1612 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1613 | break; |
| 1614 | |
| 1615 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1616 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1617 | snapToDestination(); |
Sunny Goyal | 4ff424a | 2016-08-12 12:46:01 -0700 | [diff] [blame] | 1618 | onScrollInteractionEnd(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1619 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1620 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1621 | break; |
| 1622 | |
| 1623 | case MotionEvent.ACTION_POINTER_UP: |
| 1624 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1625 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1626 | break; |
| 1627 | } |
| 1628 | |
| 1629 | return true; |
| 1630 | } |
| 1631 | |
Sunny Goyal | 65d9ceb | 2017-05-08 09:17:42 -0700 | [diff] [blame] | 1632 | protected boolean shouldFlingForVelocity(int velocityX) { |
| 1633 | return Math.abs(velocityX) > mFlingThresholdVelocity; |
| 1634 | } |
| 1635 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1636 | private void resetTouchState() { |
| 1637 | releaseVelocityTracker(); |
| 1638 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1639 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1640 | mTouchState = TOUCH_STATE_REST; |
| 1641 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1644 | /** |
| 1645 | * Triggered by scrolling via touch |
| 1646 | */ |
| 1647 | protected void onScrollInteractionBegin() { |
| 1648 | } |
| 1649 | |
| 1650 | protected void onScrollInteractionEnd() { |
| 1651 | } |
| 1652 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1653 | protected void onUnhandledTap(MotionEvent ev) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 1654 | Launcher.getLauncher(getContext()).onClick(this); |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1655 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1656 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1657 | @Override |
| 1658 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1659 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1660 | switch (event.getAction()) { |
| 1661 | case MotionEvent.ACTION_SCROLL: { |
| 1662 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1663 | final float vscroll; |
| 1664 | final float hscroll; |
| 1665 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1666 | vscroll = 0; |
| 1667 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1668 | } else { |
| 1669 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1670 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1671 | } |
| 1672 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1673 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1674 | : (hscroll > 0 || vscroll > 0); |
| 1675 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1676 | scrollRight(); |
| 1677 | } else { |
| 1678 | scrollLeft(); |
| 1679 | } |
| 1680 | return true; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | return super.onGenericMotionEvent(event); |
| 1686 | } |
| 1687 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1688 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1689 | if (mVelocityTracker == null) { |
| 1690 | mVelocityTracker = VelocityTracker.obtain(); |
| 1691 | } |
| 1692 | mVelocityTracker.addMovement(ev); |
| 1693 | } |
| 1694 | |
| 1695 | private void releaseVelocityTracker() { |
| 1696 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1697 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1698 | mVelocityTracker.recycle(); |
| 1699 | mVelocityTracker = null; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1703 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1704 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1705 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1706 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1707 | if (pointerId == mActivePointerId) { |
| 1708 | // This was our active pointer going up. Choose a new |
| 1709 | // active pointer and adjust accordingly. |
| 1710 | // TODO: Make this decision more intelligent. |
| 1711 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1712 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1713 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1714 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1715 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1716 | if (mVelocityTracker != null) { |
| 1717 | mVelocityTracker.clear(); |
| 1718 | } |
| 1719 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1722 | @Override |
| 1723 | public void requestChildFocus(View child, View focused) { |
| 1724 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1725 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1726 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1727 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1731 | int getPageNearestToCenterOfScreen() { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1732 | return getPageNearestToCenterOfScreen(getScrollX()); |
| 1733 | } |
| 1734 | |
| 1735 | private int getPageNearestToCenterOfScreen(int scaledScrollX) { |
| 1736 | int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1737 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1738 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1739 | final int childCount = getChildCount(); |
| 1740 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1741 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1742 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1743 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1744 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1745 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1746 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1747 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1748 | minDistanceFromScreenCenterIndex = i; |
| 1749 | } |
| 1750 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1751 | return minDistanceFromScreenCenterIndex; |
| 1752 | } |
| 1753 | |
| 1754 | protected void snapToDestination() { |
Adam Cohen | 8d769d6 | 2017-06-23 17:27:38 -0700 | [diff] [blame] | 1755 | snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration()); |
| 1756 | } |
| 1757 | |
| 1758 | protected boolean isInOverScroll() { |
| 1759 | return (mOverScrollX > mMaxScrollX || mOverScrollX < 0); |
| 1760 | } |
| 1761 | |
| 1762 | protected int getPageSnapDuration() { |
| 1763 | if (isInOverScroll()) { |
| 1764 | return OVERSCROLL_PAGE_SNAP_ANIMATION_DURATION; |
| 1765 | } |
| 1766 | return PAGE_SNAP_ANIMATION_DURATION; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Hyunyoung Song | 74b5af3 | 2016-06-08 16:29:32 -0700 | [diff] [blame] | 1769 | public static class ScrollInterpolator implements Interpolator { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1770 | public ScrollInterpolator() { |
| 1771 | } |
| 1772 | |
| 1773 | public float getInterpolation(float t) { |
| 1774 | t -= 1.0f; |
| 1775 | return t*t*t*t*t + 1; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1780 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1781 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1782 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1783 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1784 | f -= 0.5f; // center the values about 0. |
| 1785 | f *= 0.3f * Math.PI / 2.0f; |
| 1786 | return (float) Math.sin(f); |
| 1787 | } |
| 1788 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1789 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1790 | whichPage = validateNewPage(whichPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1791 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1792 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1793 | final int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1794 | int delta = newX - getUnboundedScrollX(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1795 | int duration = 0; |
| 1796 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1797 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1798 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1799 | // as opposed to an apparent physical response to flinging |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1800 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1801 | return; |
| 1802 | } |
| 1803 | |
| 1804 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1805 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1806 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1807 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1808 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1809 | float distance = halfScreenSize + halfScreenSize * |
| 1810 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1811 | |
| 1812 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1813 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1814 | |
| 1815 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1816 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1817 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1818 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1819 | |
| 1820 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Sunny Goyal | 1740d90 | 2015-05-27 11:14:01 -0700 | [diff] [blame] | 1823 | public void snapToPage(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1824 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 1827 | public void snapToPageImmediately(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1828 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1831 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1832 | snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1835 | protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
| 1836 | snapToPage(whichPage, duration, false, interpolator); |
| 1837 | } |
| 1838 | |
| 1839 | protected void snapToPage(int whichPage, int duration, boolean immediate, |
| 1840 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1841 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1842 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1843 | int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1844 | final int delta = newX - getUnboundedScrollX(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1845 | snapToPage(whichPage, delta, duration, immediate, interpolator); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1849 | snapToPage(whichPage, delta, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1850 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1851 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1852 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate, |
| 1853 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1854 | whichPage = validateNewPage(whichPage); |
| 1855 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1856 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1857 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1858 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1859 | if (immediate) { |
| 1860 | duration = 0; |
| 1861 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1862 | duration = Math.abs(delta); |
| 1863 | } |
| 1864 | |
Sunny Goyal | 9ccafbf | 2016-10-26 13:06:08 -0700 | [diff] [blame] | 1865 | if (duration != 0) { |
| 1866 | pageBeginTransition(); |
| 1867 | } |
| 1868 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1869 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1870 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1871 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1872 | |
| 1873 | if (interpolator != null) { |
| 1874 | mScroller.setInterpolator(interpolator); |
| 1875 | } else { |
| 1876 | mScroller.setInterpolator(mDefaultInterpolator); |
| 1877 | } |
| 1878 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1879 | mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1880 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 1881 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1882 | |
| 1883 | // Trigger a compute() to finish switching pages if necessary |
| 1884 | if (immediate) { |
| 1885 | computeScroll(); |
Sunny Goyal | 76dbf6f | 2017-01-03 14:55:47 -0800 | [diff] [blame] | 1886 | pageEndTransition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1889 | invalidate(); |
| 1890 | } |
| 1891 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1892 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 1893 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 1897 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1900 | @Override |
| 1901 | public boolean performLongClick() { |
| 1902 | mCancelTap = true; |
| 1903 | return super.performLongClick(); |
| 1904 | } |
| 1905 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1906 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1907 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1908 | |
| 1909 | SavedState(Parcelable superState) { |
| 1910 | super(superState); |
| 1911 | } |
| 1912 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1913 | @Thunk SavedState(Parcel in) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1914 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1915 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | @Override |
| 1919 | public void writeToParcel(Parcel out, int flags) { |
| 1920 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1921 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1925 | new Parcelable.Creator<SavedState>() { |
| 1926 | public SavedState createFromParcel(Parcel in) { |
| 1927 | return new SavedState(in); |
| 1928 | } |
| 1929 | |
| 1930 | public SavedState[] newArray(int size) { |
| 1931 | return new SavedState[size]; |
| 1932 | } |
| 1933 | }; |
| 1934 | } |
| 1935 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1936 | // Animate the drag view back to the original position |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1937 | private void animateDragViewToOriginalPosition() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1938 | if (mDragView != null) { |
Sunny Goyal | 9e76f68 | 2017-02-13 12:13:43 -0800 | [diff] [blame] | 1939 | Animator anim = LauncherAnimUtils.ofPropertyValuesHolder(mDragView, |
| 1940 | new PropertyListBuilder() |
| 1941 | .scale(1) |
| 1942 | .translationX(0) |
| 1943 | .translationY(0) |
| 1944 | .build()) |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1945 | .setDuration(REORDERING_DROP_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1946 | anim.addListener(new AnimatorListenerAdapter() { |
| 1947 | @Override |
| 1948 | public void onAnimationEnd(Animator animation) { |
| 1949 | onPostReorderingAnimationCompleted(); |
| 1950 | } |
| 1951 | }); |
| 1952 | anim.start(); |
| 1953 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1956 | public void onStartReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1957 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 1958 | mTouchState = TOUCH_STATE_REORDERING; |
| 1959 | mIsReordering = true; |
| 1960 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1961 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 1962 | // is always drawn on top |
| 1963 | invalidate(); |
| 1964 | } |
| 1965 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1966 | @Thunk void onPostReorderingAnimationCompleted() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1967 | // Trigger the callback when reordering has settled |
| 1968 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 1969 | if (mPostReorderingPreZoomInRunnable != null && |
| 1970 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 1971 | mPostReorderingPreZoomInRunnable.run(); |
| 1972 | mPostReorderingPreZoomInRunnable = null; |
| 1973 | } |
| 1974 | } |
| 1975 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 1976 | public void onEndReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1977 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1980 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1981 | int dragViewIndex = indexOfChild(v); |
| 1982 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1983 | // Do not allow the first page to be moved around |
| 1984 | if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false; |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 1985 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1986 | // Check if we are within the reordering range |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1987 | if (0 <= dragViewIndex && dragViewIndex <= getPageCount() - 1) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1988 | // Find the drag view under the pointer |
| 1989 | mDragView = getChildAt(dragViewIndex); |
| 1990 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 1991 | mDragViewBaselineLeft = mDragView.getLeft(); |
Sunny Goyal | 4d519f2 | 2017-10-24 10:32:40 -0700 | [diff] [blame^] | 1992 | mReorderingStarted = true; |
| 1993 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1994 | snapToPage(getPageNearestToCenterOfScreen()); |
| 1995 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1996 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1997 | return true; |
| 1998 | } |
| 1999 | return false; |
| 2000 | } |
| 2001 | |
| 2002 | boolean isReordering(boolean testTouchState) { |
| 2003 | boolean state = mIsReordering; |
| 2004 | if (testTouchState) { |
| 2005 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2006 | } |
| 2007 | return state; |
| 2008 | } |
| 2009 | void endReordering() { |
| 2010 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2011 | // In that case, we don't want to do anything. |
| 2012 | if (!mReorderingStarted) return; |
| 2013 | mReorderingStarted = false; |
| 2014 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2015 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2016 | public void run() { |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 2017 | // If we haven't flung-to-delete the current child, |
| 2018 | // then we just animate the drag view back into position |
| 2019 | onEndReordering(); |
| 2020 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2021 | enableFreeScroll(); |
Mario Bertschler | dcc26fc | 2016-12-21 11:48:48 -0800 | [diff] [blame] | 2022 | } |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2023 | }; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2024 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2025 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2026 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2027 | // Snap to the current page |
| 2028 | snapToPage(indexOfChild(mDragView), 0); |
| 2029 | // Animate the drag view back to the front position |
| 2030 | animateDragViewToOriginalPosition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2033 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 2034 | @SuppressWarnings("deprecation") |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2035 | @Override |
| 2036 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2037 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2038 | info.setScrollable(getPageCount() > 1); |
| 2039 | if (getCurrentPage() < getPageCount() - 1) { |
| 2040 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2041 | } |
| 2042 | if (getCurrentPage() > 0) { |
| 2043 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2044 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 2045 | info.setClassName(getClass().getName()); |
| 2046 | |
| 2047 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 2048 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 2049 | // accessibility focus. |
| 2050 | info.setLongClickable(false); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 2051 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 2055 | public void sendAccessibilityEvent(int eventType) { |
| 2056 | // Don't let the view send real scroll events. |
| 2057 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2058 | super.sendAccessibilityEvent(eventType); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2063 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2064 | super.onInitializeAccessibilityEvent(event); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 2065 | event.setScrollable(getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2068 | @Override |
| 2069 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2070 | if (super.performAccessibilityAction(action, arguments)) { |
| 2071 | return true; |
| 2072 | } |
| 2073 | switch (action) { |
| 2074 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2075 | if (getCurrentPage() < getPageCount() - 1) { |
| 2076 | scrollRight(); |
| 2077 | return true; |
| 2078 | } |
| 2079 | } break; |
| 2080 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2081 | if (getCurrentPage() > 0) { |
| 2082 | scrollLeft(); |
| 2083 | return true; |
| 2084 | } |
| 2085 | } break; |
| 2086 | } |
| 2087 | return false; |
| 2088 | } |
| 2089 | |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 2090 | protected String getPageIndicatorDescription() { |
| 2091 | return getCurrentPageDescription(); |
| 2092 | } |
| 2093 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2094 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 2095 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2096 | getNextPage() + 1, getChildCount()); |
| 2097 | } |
| 2098 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 2099 | @Override |
| 2100 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2101 | return true; |
| 2102 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2103 | } |