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