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