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