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; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 25 | import android.annotation.TargetApi; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 29 | import android.graphics.Matrix; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 30 | import android.graphics.Rect; |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 31 | import android.graphics.RectF; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 32 | import android.os.Build; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 34 | import android.os.Parcel; |
| 35 | import android.os.Parcelable; |
| 36 | import android.util.AttributeSet; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 37 | import android.util.DisplayMetrics; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 38 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 39 | import android.view.InputDevice; |
| 40 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 41 | import android.view.MotionEvent; |
| 42 | import android.view.VelocityTracker; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 45 | import android.view.ViewDebug; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 46 | import android.view.ViewGroup; |
| 47 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 50 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 51 | import android.view.animation.Interpolator; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 52 | |
| 53 | import com.android.launcher3.pageindicators.PageIndicator; |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 54 | import com.android.launcher3.util.LauncherEdgeEffect; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 55 | import com.android.launcher3.util.Thunk; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 56 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 57 | import java.util.ArrayList; |
| 58 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 59 | /** |
| 60 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 61 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 62 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 63 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 64 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 65 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 66 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 67 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 68 | // 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] | 69 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 70 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 71 | public static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 72 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 73 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 74 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 75 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 76 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 77 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 78 | private static final float MAX_SCROLL_PROGRESS = 1.0f; |
| 79 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 80 | // The following constants need to be scaled based on density. The scaled versions will be |
| 81 | // assigned to the corresponding member variables below. |
| 82 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 83 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 84 | private static final int MIN_FLING_VELOCITY = 250; |
| 85 | |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 86 | public static final int INVALID_RESTORE_PAGE = -1001; |
| 87 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 88 | private boolean mFreeScroll = false; |
| 89 | private int mFreeScrollMinScrollX = -1; |
| 90 | private int mFreeScrollMaxScrollX = -1; |
| 91 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 92 | protected int mFlingThresholdVelocity; |
| 93 | protected int mMinFlingVelocity; |
| 94 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 95 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 96 | protected boolean mFirstLayout = true; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 97 | private int mNormalChildHeight; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 99 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 100 | protected int mCurrentPage; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 101 | private int mChildCountOnLastLayout; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 102 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 103 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 104 | protected int mNextPage = INVALID_PAGE; |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 105 | protected int mMaxScrollX; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 106 | protected LauncherScroller mScroller; |
| 107 | private Interpolator mDefaultInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 108 | private VelocityTracker mVelocityTracker; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 109 | @Thunk int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 110 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 111 | private float mParentDownMotionX; |
| 112 | private float mParentDownMotionY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 113 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 114 | private float mDownMotionY; |
| 115 | private float mDownScrollX; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 116 | private float mDragViewBaselineLeft; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 117 | private float mLastMotionX; |
| 118 | private float mLastMotionXRemainder; |
| 119 | private float mLastMotionY; |
| 120 | private float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 121 | private int mLastScreenCenter = -1; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 122 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 123 | private boolean mCancelTap; |
| 124 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 125 | private int[] mPageScrolls; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 126 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 127 | protected final static int TOUCH_STATE_REST = 0; |
| 128 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 129 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 130 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 131 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 132 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 133 | protected int mTouchState = TOUCH_STATE_REST; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 134 | private boolean mForceScreenScrolled = false; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 135 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 136 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 137 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 138 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 139 | private int mMaximumVelocity; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 140 | protected boolean mAllowOverScroll = true; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 141 | protected int[] mTempVisiblePagesRange = new int[2]; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 142 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 143 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 144 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 145 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 146 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 147 | protected boolean mIsPageMoving = false; |
| 148 | |
Sunny Goyal | 061380a | 2016-02-29 15:15:03 -0800 | [diff] [blame] | 149 | protected boolean mWasInOverscroll = false; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 150 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 151 | // Page Indicator |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 152 | @Thunk int mPageIndicatorViewId; |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 153 | protected PageIndicator mPageIndicator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 154 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 155 | // viewport) |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 156 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 157 | private Rect mViewport = new Rect(); |
| 158 | |
| 159 | // Reordering |
| 160 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 161 | // dimensions such that when we are zoomed out, the view is not clipped |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 162 | private static int REORDERING_DROP_REPOSITION_DURATION = 200; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 163 | @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 164 | private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80; |
| 165 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 166 | private float mMinScale = 1f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 167 | private boolean mUseMinScale = false; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 168 | @Thunk View mDragView; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 169 | private Runnable mSidePageHoverRunnable; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 170 | @Thunk int mSidePageHoverIndex = -1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 171 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 172 | private boolean mReorderingStarted = false; |
| 173 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 174 | // animation after endReordering() |
| 175 | private boolean mIsReordering; |
| 176 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 177 | private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 178 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 179 | private Runnable mPostReorderingPreZoomInRunnable; |
| 180 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 181 | // Convenience/caching |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 182 | private static final Matrix sTmpInvMatrix = new Matrix(); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 183 | private static final float[] sTmpPoint = new float[2]; |
| 184 | private static final int[] sTmpIntPoint = new int[2]; |
| 185 | private static final Rect sTmpRect = new Rect(); |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 186 | private static final RectF sTmpRectF = new RectF(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 187 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 188 | protected final Rect mInsets = new Rect(); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 189 | protected final boolean mIsRtl; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 190 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 191 | // Edge effect |
| 192 | private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect(); |
| 193 | private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect(); |
| 194 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 195 | public PagedView(Context context) { |
| 196 | this(context, null); |
| 197 | } |
| 198 | |
| 199 | public PagedView(Context context, AttributeSet attrs) { |
| 200 | this(context, attrs, 0); |
| 201 | } |
| 202 | |
| 203 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 204 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 205 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 206 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 207 | R.styleable.PagedView, defStyle, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 208 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 209 | a.recycle(); |
| 210 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 211 | setHapticFeedbackEnabled(false); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 212 | mIsRtl = Utilities.isRtl(getResources()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 213 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Initializes various states for this workspace. |
| 218 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 219 | protected void init() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 220 | mScroller = new LauncherScroller(getContext()); |
| 221 | setDefaultInterpolator(new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 222 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 223 | |
| 224 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 225 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 226 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 227 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 228 | float density = getResources().getDisplayMetrics().density; |
| 229 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density); |
| 230 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density); |
| 231 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 232 | setOnHierarchyChangeListener(this); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 233 | setWillNotDraw(false); |
| 234 | } |
| 235 | |
| 236 | protected void setEdgeGlowColor(int color) { |
| 237 | mEdgeGlowLeft.setColor(color); |
| 238 | mEdgeGlowRight.setColor(color); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 241 | protected void setDefaultInterpolator(Interpolator interpolator) { |
| 242 | mDefaultInterpolator = interpolator; |
| 243 | mScroller.setInterpolator(mDefaultInterpolator); |
| 244 | } |
| 245 | |
Sunny Goyal | d0a6ae7 | 2016-06-16 12:29:03 -0700 | [diff] [blame] | 246 | public void initParentViews(View parent) { |
| 247 | if (mPageIndicatorViewId > -1) { |
| 248 | mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId); |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 249 | mPageIndicator.setMarkersCount(getChildCount()); |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 250 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 254 | // Convenience methods to map points from self to parent and vice versa |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 255 | private float[] mapPointFromViewToParent(View v, float x, float y) { |
| 256 | sTmpPoint[0] = x; |
| 257 | sTmpPoint[1] = y; |
| 258 | v.getMatrix().mapPoints(sTmpPoint); |
| 259 | sTmpPoint[0] += v.getLeft(); |
| 260 | sTmpPoint[1] += v.getTop(); |
| 261 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 262 | } |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 263 | private float[] mapPointFromParentToView(View v, float x, float y) { |
| 264 | sTmpPoint[0] = x - v.getLeft(); |
| 265 | sTmpPoint[1] = y - v.getTop(); |
| 266 | v.getMatrix().invert(sTmpInvMatrix); |
| 267 | sTmpInvMatrix.mapPoints(sTmpPoint); |
| 268 | return sTmpPoint; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 271 | private void updateDragViewTranslationDuringDrag() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 272 | if (mDragView != null) { |
| 273 | float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) + |
| 274 | (mDragViewBaselineLeft - mDragView.getLeft()); |
| 275 | float y = mLastMotionY - mDownMotionY; |
| 276 | mDragView.setTranslationX(x); |
| 277 | mDragView.setTranslationY(y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 278 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 279 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " |
| 280 | + x + ", " + y); |
| 281 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | public void setMinScale(float f) { |
| 285 | mMinScale = f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 286 | mUseMinScale = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 287 | requestLayout(); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void setScaleX(float scaleX) { |
| 292 | super.setScaleX(scaleX); |
| 293 | if (isReordering(true)) { |
| 294 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 295 | mLastMotionX = p[0]; |
| 296 | mLastMotionY = p[1]; |
| 297 | updateDragViewTranslationDuringDrag(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
| 302 | // to be larger to account for the minimum possible scale) |
| 303 | int getViewportWidth() { |
| 304 | return mViewport.width(); |
| 305 | } |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 306 | public int getViewportHeight() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 307 | return mViewport.height(); |
| 308 | } |
| 309 | |
| 310 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
| 311 | // PagedView both horizontally and vertically |
| 312 | int getViewportOffsetX() { |
| 313 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
| 314 | } |
| 315 | |
| 316 | int getViewportOffsetY() { |
| 317 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
| 318 | } |
| 319 | |
Hyunyoung Song | 7744169 | 2016-06-27 22:00:48 -0700 | [diff] [blame] | 320 | public PageIndicator getPageIndicator() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 321 | return mPageIndicator; |
| 322 | } |
| 323 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 324 | /** |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 325 | * Returns the index of the currently displayed page. When in free scroll mode, this is the page |
| 326 | * that the user was on before entering free scroll mode (e.g. the home screen page they |
| 327 | * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()} |
| 328 | * to get the page the user is currently scrolling over. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 329 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 330 | public int getCurrentPage() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 331 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 332 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 333 | |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Returns the index of page to be shown immediately afterwards. |
| 336 | */ |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 337 | public int getNextPage() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 338 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 339 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 340 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 341 | public int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 342 | return getChildCount(); |
| 343 | } |
| 344 | |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 345 | public View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 346 | return getChildAt(index); |
| 347 | } |
| 348 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 349 | protected int indexToPage(int index) { |
| 350 | return index; |
| 351 | } |
| 352 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 353 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 354 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 355 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 356 | * the previous tab page. |
| 357 | */ |
| 358 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 359 | // If the current page is invalid, just reset the scroll position to zero |
| 360 | int newX = 0; |
| 361 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 362 | newX = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 363 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 364 | scrollTo(newX, 0); |
| 365 | mScroller.setFinalX(newX); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 366 | forceFinishScroller(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 369 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 370 | mScroller.abortAnimation(); |
| 371 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 372 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 373 | if (resetNextPage) { |
| 374 | mNextPage = INVALID_PAGE; |
| 375 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 376 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 377 | |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 378 | private void forceFinishScroller(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 379 | mScroller.forceFinished(true); |
| 380 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 381 | // updating current page on the pass. |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 382 | if (resetNextPage) { |
| 383 | mNextPage = INVALID_PAGE; |
| 384 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 387 | private int validateNewPage(int newPage) { |
| 388 | int validatedPage = newPage; |
| 389 | // When in free scroll mode, we need to clamp to the free scroll page range. |
| 390 | if (mFreeScroll) { |
| 391 | getFreeScrollPageRange(mTempVisiblePagesRange); |
| 392 | validatedPage = Math.max(mTempVisiblePagesRange[0], |
| 393 | Math.min(newPage, mTempVisiblePagesRange[1])); |
| 394 | } |
| 395 | // 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] | 396 | validatedPage = Utilities.boundToRange(validatedPage, 0, getPageCount() - 1); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 397 | return validatedPage; |
| 398 | } |
| 399 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 400 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 401 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 402 | */ |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 403 | public void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 404 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 405 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 406 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 407 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 408 | // the default |
| 409 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 410 | return; |
| 411 | } |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 412 | mForceScreenScrolled = true; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 413 | mCurrentPage = validateNewPage(currentPage); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 414 | updateCurrentPageScroll(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 415 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 416 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 419 | /** |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 420 | * Should be called whenever the page changes. In the case of a scroll, we wait until the page |
| 421 | * has settled. |
| 422 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 423 | protected void notifyPageSwitchListener() { |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 424 | updatePageIndicator(); |
| 425 | } |
| 426 | |
| 427 | private void updatePageIndicator() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 428 | // Update the page indicator (when we aren't reordering) |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 429 | if (mPageIndicator != null) { |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 430 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Sunny Goyal | 4cbf046 | 2014-08-28 16:19:39 -0700 | [diff] [blame] | 431 | if (!isReordering(false)) { |
| 432 | mPageIndicator.setActiveMarker(getNextPage()); |
| 433 | } |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 434 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 435 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 436 | protected void pageBeginMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 437 | if (!mIsPageMoving) { |
| 438 | mIsPageMoving = true; |
| 439 | onPageBeginMoving(); |
| 440 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 443 | protected void pageEndMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 444 | if (mIsPageMoving) { |
| 445 | mIsPageMoving = false; |
| 446 | onPageEndMoving(); |
| 447 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 450 | protected boolean isPageMoving() { |
| 451 | return mIsPageMoving; |
| 452 | } |
| 453 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 454 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 455 | protected void onPageBeginMoving() { |
| 456 | } |
| 457 | |
| 458 | // a method that subclasses can override to add behavior |
| 459 | protected void onPageEndMoving() { |
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 |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 567 | if (getScrollX() != 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) { |
| 587 | pageEndMoving(); |
| 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 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 902 | /** |
| 903 | * Called when the center screen changes during scrolling. |
| 904 | */ |
| 905 | protected void screenScrolled(int screenCenter) { } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 906 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 907 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 908 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 909 | // Update the page indicator, we don't update the page indicator as we |
| 910 | // add/remove pages |
| 911 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 912 | mPageIndicator.addMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 915 | // This ensures that when children are added, they get the correct transforms / alphas |
| 916 | // in accordance with any scroll effects. |
| 917 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 918 | updateFreescrollBounds(); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 919 | invalidate(); |
| 920 | } |
| 921 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 922 | @Override |
| 923 | public void onChildViewRemoved(View parent, View child) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 924 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 925 | updateFreescrollBounds(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 926 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 929 | private void removeMarkerForView() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 930 | // Update the page indicator, we don't update the page indicator as we |
| 931 | // add/remove pages |
| 932 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 933 | mPageIndicator.removeMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public void removeView(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.removeView(v); |
| 943 | } |
| 944 | @Override |
| 945 | public void removeViewInLayout(View v) { |
| 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.removeViewInLayout(v); |
| 950 | } |
| 951 | @Override |
| 952 | public void removeViewAt(int index) { |
| 953 | // XXX: We should find a better way to hook into this before the view |
| 954 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 955 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 956 | super.removeViewAt(index); |
| 957 | } |
| 958 | @Override |
| 959 | public void removeAllViewsInLayout() { |
| 960 | // Update the page indicator, we don't update the page indicator as we |
| 961 | // add/remove pages |
| 962 | if (mPageIndicator != null) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 963 | mPageIndicator.setMarkersCount(0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | super.removeAllViewsInLayout(); |
| 967 | } |
| 968 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 969 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 970 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 971 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 972 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 973 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 974 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 977 | protected void getFreeScrollPageRange(int[] range) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 978 | range[0] = 0; |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 979 | range[1] = Math.max(0, getChildCount() - 1); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 982 | protected void getVisiblePages(int[] range) { |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 983 | final int count = getChildCount(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 984 | range[0] = -1; |
| 985 | range[1] = -1; |
| 986 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 987 | if (count > 0) { |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 988 | final int visibleLeft = -getLeft(); |
| 989 | final int visibleRight = visibleLeft + getViewportWidth(); |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 990 | final Matrix pageShiftMatrix = getPageShiftMatrix(); |
| 991 | int curScreen = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 992 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 993 | for (int i = 0; i < count; i++) { |
| 994 | View currPage = getPageAt(i); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 995 | |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 996 | // Verify if the page bounds are within the visible range. |
| 997 | sTmpRectF.left = 0; |
| 998 | sTmpRectF.right = currPage.getMeasuredWidth(); |
| 999 | currPage.getMatrix().mapRect(sTmpRectF); |
| 1000 | sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0); |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1001 | pageShiftMatrix.mapRect(sTmpRectF); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1002 | |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 1003 | if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) { |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1004 | if (range[0] == -1) { |
| 1005 | continue; |
| 1006 | } else { |
| 1007 | break; |
| 1008 | } |
| 1009 | } |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1010 | curScreen = i; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1011 | if (range[0] < 0) { |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1012 | range[0] = curScreen; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1015 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1016 | range[1] = curScreen; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1017 | } else { |
| 1018 | range[0] = -1; |
| 1019 | range[1] = -1; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1023 | protected Matrix getPageShiftMatrix() { |
| 1024 | return getMatrix(); |
| 1025 | } |
| 1026 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1027 | protected boolean shouldDrawChild(View child) { |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 1028 | return child.getVisibility() == VISIBLE; |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1031 | @Override |
| 1032 | protected void dispatchDraw(Canvas canvas) { |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1033 | // Find out which screens are visible; as an optimization we only call draw on them |
| 1034 | final int pageCount = getChildCount(); |
| 1035 | if (pageCount > 0) { |
Adam Cohen | 4de0974 | 2013-12-12 16:16:39 -0800 | [diff] [blame] | 1036 | int halfScreenSize = getViewportWidth() / 2; |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1037 | int screenCenter = getScrollX() + halfScreenSize; |
Adam Cohen | 4de0974 | 2013-12-12 16:16:39 -0800 | [diff] [blame] | 1038 | |
| 1039 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
| 1040 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 1041 | // set it for the next frame |
| 1042 | mForceScreenScrolled = false; |
| 1043 | screenScrolled(screenCenter); |
| 1044 | mLastScreenCenter = screenCenter; |
| 1045 | } |
| 1046 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1047 | getVisiblePages(mTempVisiblePagesRange); |
| 1048 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 1049 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1050 | if (leftScreen != -1 && rightScreen != -1) { |
| 1051 | final long drawingTime = getDrawingTime(); |
| 1052 | // Clip to the bounds |
| 1053 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1054 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 1055 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1056 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1057 | // Draw all the children, leaving the drag view for last |
| 1058 | for (int i = pageCount - 1; i >= 0; i--) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1059 | final View v = getPageAt(i); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1060 | if (v == mDragView) continue; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1061 | if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1062 | drawChild(canvas, v, drawingTime); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1063 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1064 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1065 | // Draw the drag view on top (if there is one) |
| 1066 | if (mDragView != null) { |
| 1067 | drawChild(canvas, mDragView, drawingTime); |
| 1068 | } |
| 1069 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1070 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1071 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1072 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | @Override |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1076 | public void draw(Canvas canvas) { |
| 1077 | super.draw(canvas); |
| 1078 | if (getPageCount() > 0) { |
| 1079 | if (!mEdgeGlowLeft.isFinished()) { |
| 1080 | final int restoreCount = canvas.save(); |
| 1081 | Rect display = mViewport; |
| 1082 | canvas.translate(display.left, display.top); |
| 1083 | canvas.rotate(270); |
| 1084 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1085 | getEdgeVerticalPosition(sTmpIntPoint); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1086 | canvas.translate(display.top - sTmpIntPoint[1], 0); |
| 1087 | mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width()); |
| 1088 | if (mEdgeGlowLeft.draw(canvas)) { |
| 1089 | postInvalidateOnAnimation(); |
| 1090 | } |
| 1091 | canvas.restoreToCount(restoreCount); |
| 1092 | } |
| 1093 | if (!mEdgeGlowRight.isFinished()) { |
| 1094 | final int restoreCount = canvas.save(); |
| 1095 | Rect display = mViewport; |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 1096 | canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1097 | canvas.rotate(90); |
| 1098 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1099 | getEdgeVerticalPosition(sTmpIntPoint); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 1100 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 1101 | canvas.translate(sTmpIntPoint[0] - display.top, -display.width()); |
| 1102 | mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width()); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1103 | if (mEdgeGlowRight.draw(canvas)) { |
| 1104 | postInvalidateOnAnimation(); |
| 1105 | } |
| 1106 | canvas.restoreToCount(restoreCount); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * Returns the top and bottom position for the edge effect. |
| 1113 | */ |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 1114 | protected abstract void getEdgeVerticalPosition(int[] pos); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1115 | |
| 1116 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1117 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1118 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1119 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 1120 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1121 | return true; |
| 1122 | } |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
| 1126 | @Override |
| 1127 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1128 | int focusablePage; |
| 1129 | if (mNextPage != INVALID_PAGE) { |
| 1130 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1131 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1132 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1133 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1134 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1135 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1136 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1137 | } |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
| 1141 | @Override |
| 1142 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 1143 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | if (mIsRtl) { |
| 1148 | if (direction == View.FOCUS_LEFT) { |
| 1149 | direction = View.FOCUS_RIGHT; |
| 1150 | } else if (direction == View.FOCUS_RIGHT) { |
| 1151 | direction = View.FOCUS_LEFT; |
| 1152 | } |
| 1153 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1154 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1155 | if (getCurrentPage() > 0) { |
| 1156 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1160 | if (getCurrentPage() < getPageCount() - 1) { |
| 1161 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1162 | return true; |
| 1163 | } |
| 1164 | } |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 1165 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | @Override |
| 1169 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1170 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1171 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1172 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1173 | } |
| 1174 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1175 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1176 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1177 | } |
| 1178 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1179 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1180 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * 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] | 1187 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1188 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1189 | * This happens when live folders requery, and if they're off page, they |
| 1190 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1191 | */ |
| 1192 | @Override |
| 1193 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1194 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1195 | View v = focused; |
| 1196 | while (true) { |
| 1197 | if (v == current) { |
| 1198 | super.focusableViewAvailable(focused); |
| 1199 | return; |
| 1200 | } |
| 1201 | if (v == this) { |
| 1202 | return; |
| 1203 | } |
| 1204 | ViewParent parent = v.getParent(); |
| 1205 | if (parent instanceof View) { |
| 1206 | v = (View)v.getParent(); |
| 1207 | } else { |
| 1208 | return; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * {@inheritDoc} |
| 1215 | */ |
| 1216 | @Override |
| 1217 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1218 | if (disallowIntercept) { |
| 1219 | // We need to make sure to cancel our long press if |
| 1220 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1221 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1222 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1223 | } |
| 1224 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1225 | } |
| 1226 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1227 | /** |
| 1228 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1229 | */ |
| 1230 | protected boolean hitsPreviousPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1231 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1232 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1233 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1234 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1235 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1240 | */ |
| 1241 | protected boolean hitsNextPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1242 | if (mIsRtl) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1243 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1244 | } |
| 1245 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1246 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1247 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1248 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1249 | /** Returns whether x and y originated within the buffered viewport */ |
| 1250 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1251 | sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1252 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1253 | return sTmpRect.contains(x, y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1256 | @Override |
| 1257 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 1258 | /* |
| 1259 | * This method JUST determines whether we want to intercept the motion. |
| 1260 | * If we return true, onTouchEvent will be called and we do the actual |
| 1261 | * scrolling there. |
| 1262 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1263 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1264 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1265 | // Skip touch handling if there are no pages to swipe |
| 1266 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1267 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1268 | /* |
| 1269 | * Shortcut the most recurring case: the user is in the dragging |
| 1270 | * state and he is moving his finger. We want to intercept this |
| 1271 | * motion. |
| 1272 | */ |
| 1273 | final int action = ev.getAction(); |
| 1274 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1275 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1279 | switch (action & MotionEvent.ACTION_MASK) { |
| 1280 | case MotionEvent.ACTION_MOVE: { |
| 1281 | /* |
| 1282 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1283 | * whether the user has moved far enough from his original down touch. |
| 1284 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1285 | if (mActivePointerId != INVALID_POINTER) { |
| 1286 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1287 | } |
| 1288 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1289 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1290 | // i.e. fall through to the next case (don't break) |
| 1291 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1292 | // 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] | 1293 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | case MotionEvent.ACTION_DOWN: { |
| 1297 | final float x = ev.getX(); |
| 1298 | final float y = ev.getY(); |
| 1299 | // Remember location of down touch |
| 1300 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1301 | mDownMotionY = y; |
| 1302 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1303 | mLastMotionX = x; |
| 1304 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1305 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1306 | mParentDownMotionX = p[0]; |
| 1307 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1308 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1309 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1310 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1311 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1312 | /* |
| 1313 | * If being flinged and user touches the screen, initiate drag; |
| 1314 | * otherwise don't. mScroller.isFinished should be false when |
| 1315 | * being flinged. |
| 1316 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1317 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1318 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1319 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1320 | if (finishedScrolling) { |
| 1321 | mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 59b5c79 | 2013-12-04 16:09:07 -0800 | [diff] [blame] | 1322 | if (!mScroller.isFinished() && !mFreeScroll) { |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1323 | setCurrentPage(getNextPage()); |
| 1324 | pageEndMoving(); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1325 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1326 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1327 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1328 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1329 | } else { |
| 1330 | mTouchState = TOUCH_STATE_REST; |
| 1331 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1332 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1333 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1337 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1338 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1339 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1340 | break; |
| 1341 | |
| 1342 | case MotionEvent.ACTION_POINTER_UP: |
| 1343 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1344 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * The only time we want to intercept motion events is if we are in the |
| 1350 | * drag mode. |
| 1351 | */ |
| 1352 | return mTouchState != TOUCH_STATE_REST; |
| 1353 | } |
| 1354 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1355 | protected void determineScrollingStart(MotionEvent ev) { |
| 1356 | determineScrollingStart(ev, 1.0f); |
| 1357 | } |
| 1358 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1359 | /* |
| 1360 | * Determines if we should change the touch state to start scrolling after the |
| 1361 | * user moves their touch point too far. |
| 1362 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1363 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1364 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1365 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1366 | if (pointerIndex == -1) return; |
| 1367 | |
| 1368 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1369 | final float x = ev.getX(pointerIndex); |
| 1370 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1371 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1372 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1373 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1374 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1375 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1376 | boolean xMoved = xDiff > touchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1377 | |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1378 | if (xMoved) { |
| 1379 | // Scroll if the user moved far enough along the X axis |
| 1380 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1381 | mTotalMotionX += Math.abs(mLastMotionX - x); |
| 1382 | mLastMotionX = x; |
| 1383 | mLastMotionXRemainder = 0; |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1384 | onScrollInteractionBegin(); |
| 1385 | pageBeginMoving(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1386 | // Stop listening for things like pinches. |
| 1387 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1392 | // Try canceling the long press. It could also have been scheduled |
| 1393 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1394 | // everything |
| 1395 | final View currentPage = getPageAt(mCurrentPage); |
| 1396 | if (currentPage != null) { |
| 1397 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1401 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1402 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1403 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1404 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1405 | int count = getChildCount(); |
| 1406 | |
| 1407 | final int totalDistance; |
| 1408 | |
| 1409 | int adjacentPage = page + 1; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1410 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1411 | adjacentPage = page - 1; |
| 1412 | } |
| 1413 | |
| 1414 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1415 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1416 | } else { |
| 1417 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1418 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1419 | |
| 1420 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1421 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
| 1422 | scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1423 | return scrollProgress; |
| 1424 | } |
| 1425 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1426 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1427 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1428 | return 0; |
| 1429 | } else { |
| 1430 | return mPageScrolls[index]; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1434 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1435 | // position. This method returns the magnitude of this stray at any given time. |
| 1436 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1437 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1438 | return 0; |
| 1439 | } else { |
| 1440 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1441 | |
| 1442 | int scrollOffset = 0; |
| 1443 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1444 | if (!lp.isFullScreenPage) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1445 | scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1448 | int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX(); |
| 1449 | return (int) (child.getX() - baselineX); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1453 | protected void dampedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1454 | int screenSize = getViewportWidth(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1455 | float f = (amount / screenSize); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1456 | if (f < 0) { |
| 1457 | mEdgeGlowLeft.onPull(-f); |
| 1458 | } else if (f > 0) { |
| 1459 | mEdgeGlowRight.onPull(f); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1460 | } else { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1461 | return; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1462 | } |
| 1463 | invalidate(); |
| 1464 | } |
| 1465 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1466 | protected void overScroll(float amount) { |
| 1467 | dampedOverScroll(amount); |
| 1468 | } |
| 1469 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1470 | public void enableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1471 | setEnableFreeScroll(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1474 | public void disableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1475 | setEnableFreeScroll(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1478 | void updateFreescrollBounds() { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1479 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1480 | if (mIsRtl) { |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1481 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1482 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1483 | } else { |
| 1484 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1485 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1486 | } |
| 1487 | } |
| 1488 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1489 | private void setEnableFreeScroll(boolean freeScroll) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1490 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1491 | mFreeScroll = freeScroll; |
| 1492 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1493 | if (mFreeScroll) { |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1494 | updateFreescrollBounds(); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1495 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1496 | if (getCurrentPage() < mTempVisiblePagesRange[0]) { |
| 1497 | setCurrentPage(mTempVisiblePagesRange[0]); |
| 1498 | } else if (getCurrentPage() > mTempVisiblePagesRange[1]) { |
| 1499 | setCurrentPage(mTempVisiblePagesRange[1]); |
| 1500 | } |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1501 | } else if (wasFreeScroll) { |
| 1502 | snapToPage(getNextPage()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | setEnableOverscroll(!freeScroll); |
| 1506 | } |
| 1507 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1508 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1509 | mAllowOverScroll = enable; |
| 1510 | } |
| 1511 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1512 | private int getNearestHoverOverPageIndex() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1513 | if (mDragView != null) { |
| 1514 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1515 | + mDragView.getTranslationX()); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1516 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1517 | int minDistance = Integer.MAX_VALUE; |
| 1518 | int minIndex = indexOfChild(mDragView); |
| 1519 | for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) { |
| 1520 | View page = getPageAt(i); |
| 1521 | int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2); |
| 1522 | int d = Math.abs(dragX - pageX); |
| 1523 | if (d < minDistance) { |
| 1524 | minIndex = i; |
| 1525 | minDistance = d; |
| 1526 | } |
| 1527 | } |
| 1528 | return minIndex; |
| 1529 | } |
| 1530 | return -1; |
| 1531 | } |
| 1532 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1533 | @Override |
| 1534 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1535 | super.onTouchEvent(ev); |
| 1536 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1537 | // Skip touch handling if there are no pages to swipe |
| 1538 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1539 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1540 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1541 | |
| 1542 | final int action = ev.getAction(); |
| 1543 | |
| 1544 | switch (action & MotionEvent.ACTION_MASK) { |
| 1545 | case MotionEvent.ACTION_DOWN: |
| 1546 | /* |
| 1547 | * If being flinged and user touches, stop the fling. isFinished |
| 1548 | * will be false if being flinged. |
| 1549 | */ |
| 1550 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1551 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | // Remember where the motion event started |
| 1555 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1556 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1557 | mDownScrollX = getScrollX(); |
| 1558 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1559 | mParentDownMotionX = p[0]; |
| 1560 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1561 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1562 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1563 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1564 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1565 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1566 | onScrollInteractionBegin(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1567 | pageBeginMoving(); |
| 1568 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1569 | break; |
| 1570 | |
| 1571 | case MotionEvent.ACTION_MOVE: |
| 1572 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1573 | // Scroll to follow the motion event |
| 1574 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1575 | |
| 1576 | if (pointerIndex == -1) return true; |
| 1577 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1578 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1579 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1580 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1581 | mTotalMotionX += Math.abs(deltaX); |
| 1582 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1583 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1584 | // keep the remainder because we are actually testing if we've moved from the last |
| 1585 | // scrolled position (which is discrete). |
| 1586 | if (Math.abs(deltaX) >= 1.0f) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1587 | scrollBy((int) deltaX, 0); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1588 | mLastMotionX = x; |
| 1589 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1590 | } else { |
| 1591 | awakenScrollBars(); |
| 1592 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1593 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1594 | // Update the last motion position |
| 1595 | mLastMotionX = ev.getX(); |
| 1596 | mLastMotionY = ev.getY(); |
| 1597 | |
| 1598 | // Update the parent down so that our zoom animations take this new movement into |
| 1599 | // account |
| 1600 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1601 | mParentDownMotionX = pt[0]; |
| 1602 | mParentDownMotionY = pt[1]; |
| 1603 | updateDragViewTranslationDuringDrag(); |
| 1604 | |
| 1605 | // Find the closest page to the touch point |
| 1606 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1607 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1608 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1609 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1610 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1611 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1612 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1613 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1614 | // Do not allow any page to be moved to 0th position. |
| 1615 | if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1616 | mTempVisiblePagesRange[0] = 0; |
| 1617 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1618 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1619 | if (mTempVisiblePagesRange[0] <= pageUnderPointIndex && |
| 1620 | pageUnderPointIndex <= mTempVisiblePagesRange[1] && |
| 1621 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1622 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1623 | mSidePageHoverRunnable = new Runnable() { |
| 1624 | @Override |
| 1625 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1626 | // Setup the scroll to the correct page before we swap the views |
| 1627 | snapToPage(pageUnderPointIndex); |
| 1628 | |
| 1629 | // For each of the pages between the paged view and the drag view, |
| 1630 | // animate them from the previous position to the new position in |
| 1631 | // the layout (as a result of the drag view moving in the layout) |
| 1632 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1633 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1634 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1635 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1636 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1637 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1638 | View v = getChildAt(i); |
| 1639 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1640 | // drag view all subsequent views to pageUnderPointIndex will |
| 1641 | // shift down. |
| 1642 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1643 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1644 | |
| 1645 | // Animate the view translation from its old position to its new |
| 1646 | // position |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1647 | ObjectAnimator anim = (ObjectAnimator) v.getTag(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1648 | if (anim != null) { |
| 1649 | anim.cancel(); |
| 1650 | } |
| 1651 | |
| 1652 | v.setTranslationX(oldX - newX); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1653 | anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1654 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1655 | anim.start(); |
| 1656 | v.setTag(anim); |
| 1657 | } |
| 1658 | |
| 1659 | removeView(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1660 | addView(mDragView, pageUnderPointIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1661 | mSidePageHoverIndex = -1; |
Winson Chung | 876a619 | 2013-11-06 14:49:50 -0800 | [diff] [blame] | 1662 | if (mPageIndicator != null) { |
| 1663 | mPageIndicator.setActiveMarker(getNextPage()); |
| 1664 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1665 | } |
| 1666 | }; |
| 1667 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1668 | } |
| 1669 | } else { |
| 1670 | removeCallbacks(mSidePageHoverRunnable); |
| 1671 | mSidePageHoverIndex = -1; |
| 1672 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1673 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1674 | determineScrollingStart(ev); |
| 1675 | } |
| 1676 | break; |
| 1677 | |
| 1678 | case MotionEvent.ACTION_UP: |
| 1679 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1680 | final int activePointerId = mActivePointerId; |
| 1681 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1682 | final float x = ev.getX(pointerIndex); |
| 1683 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1684 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1685 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1686 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1687 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1688 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1689 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1690 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1691 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1692 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1693 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1694 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1695 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1696 | if (!mFreeScroll) { |
| 1697 | // In the case that the page is moved far to one direction and then is flung |
| 1698 | // in the opposite direction, we use a threshold to determine whether we should |
| 1699 | // just return to the starting page, or if we should skip one further. |
| 1700 | boolean returnToOriginalPage = false; |
| 1701 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1702 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1703 | returnToOriginalPage = true; |
| 1704 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1705 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1706 | int finalPage; |
| 1707 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1708 | // test for a large move if a fling has been registered. That is, a large |
| 1709 | // 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] | 1710 | boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0; |
| 1711 | boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1712 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1713 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1714 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1715 | snapToPageWithVelocity(finalPage, velocityX); |
| 1716 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1717 | (isFling && isVelocityXLeft)) && |
| 1718 | mCurrentPage < getChildCount() - 1) { |
| 1719 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1720 | snapToPageWithVelocity(finalPage, velocityX); |
| 1721 | } else { |
| 1722 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1723 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1724 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1725 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1726 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | float scaleX = getScaleX(); |
| 1730 | int vX = (int) (-velocityX * scaleX); |
| 1731 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1732 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1733 | mScroller.setInterpolator(mDefaultInterpolator); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1734 | mScroller.fling(initialScrollX, |
| 1735 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1736 | mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX)); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1737 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1738 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1739 | onScrollInteractionEnd(); |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 1740 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1741 | // at this point we have not moved beyond the touch slop |
| 1742 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1743 | // we can just page |
| 1744 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1745 | if (nextPage != mCurrentPage) { |
| 1746 | snapToPage(nextPage); |
| 1747 | } else { |
| 1748 | snapToDestination(); |
| 1749 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1750 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1751 | // at this point we have not moved beyond the touch slop |
| 1752 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1753 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1754 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1755 | if (nextPage != mCurrentPage) { |
| 1756 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1757 | } else { |
| 1758 | snapToDestination(); |
| 1759 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1760 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1761 | // Update the last motion position |
| 1762 | mLastMotionX = ev.getX(); |
| 1763 | mLastMotionY = ev.getY(); |
| 1764 | |
| 1765 | // Update the parent down so that our zoom animations take this new movement into |
| 1766 | // account |
| 1767 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1768 | mParentDownMotionX = pt[0]; |
| 1769 | mParentDownMotionY = pt[1]; |
| 1770 | updateDragViewTranslationDuringDrag(); |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1771 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1772 | if (!mCancelTap) { |
| 1773 | onUnhandledTap(ev); |
| 1774 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1775 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1776 | |
| 1777 | // Remove the callback to wait for the side page hover timeout |
| 1778 | removeCallbacks(mSidePageHoverRunnable); |
| 1779 | // End any intermediate reordering states |
| 1780 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1781 | break; |
| 1782 | |
| 1783 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1784 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1785 | snapToDestination(); |
Sunny Goyal | 4ff424a | 2016-08-12 12:46:01 -0700 | [diff] [blame] | 1786 | onScrollInteractionEnd(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1787 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1788 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1789 | break; |
| 1790 | |
| 1791 | case MotionEvent.ACTION_POINTER_UP: |
| 1792 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1793 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1794 | break; |
| 1795 | } |
| 1796 | |
| 1797 | return true; |
| 1798 | } |
| 1799 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1800 | private void resetTouchState() { |
| 1801 | releaseVelocityTracker(); |
| 1802 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1803 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1804 | mTouchState = TOUCH_STATE_REST; |
| 1805 | mActivePointerId = INVALID_POINTER; |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1806 | mEdgeGlowLeft.onRelease(); |
| 1807 | mEdgeGlowRight.onRelease(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1810 | /** |
| 1811 | * Triggered by scrolling via touch |
| 1812 | */ |
| 1813 | protected void onScrollInteractionBegin() { |
| 1814 | } |
| 1815 | |
| 1816 | protected void onScrollInteractionEnd() { |
| 1817 | } |
| 1818 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1819 | protected void onUnhandledTap(MotionEvent ev) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 1820 | Launcher.getLauncher(getContext()).onClick(this); |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1821 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1822 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1823 | @Override |
| 1824 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1825 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1826 | switch (event.getAction()) { |
| 1827 | case MotionEvent.ACTION_SCROLL: { |
| 1828 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1829 | final float vscroll; |
| 1830 | final float hscroll; |
| 1831 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1832 | vscroll = 0; |
| 1833 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1834 | } else { |
| 1835 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1836 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1837 | } |
| 1838 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1839 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1840 | : (hscroll > 0 || vscroll > 0); |
| 1841 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1842 | scrollRight(); |
| 1843 | } else { |
| 1844 | scrollLeft(); |
| 1845 | } |
| 1846 | return true; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | return super.onGenericMotionEvent(event); |
| 1852 | } |
| 1853 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1854 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1855 | if (mVelocityTracker == null) { |
| 1856 | mVelocityTracker = VelocityTracker.obtain(); |
| 1857 | } |
| 1858 | mVelocityTracker.addMovement(ev); |
| 1859 | } |
| 1860 | |
| 1861 | private void releaseVelocityTracker() { |
| 1862 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1863 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1864 | mVelocityTracker.recycle(); |
| 1865 | mVelocityTracker = null; |
| 1866 | } |
| 1867 | } |
| 1868 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1869 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1870 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1871 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1872 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1873 | if (pointerId == mActivePointerId) { |
| 1874 | // This was our active pointer going up. Choose a new |
| 1875 | // active pointer and adjust accordingly. |
| 1876 | // TODO: Make this decision more intelligent. |
| 1877 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1878 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1879 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1880 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1881 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1882 | if (mVelocityTracker != null) { |
| 1883 | mVelocityTracker.clear(); |
| 1884 | } |
| 1885 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1888 | @Override |
| 1889 | public void requestChildFocus(View child, View focused) { |
| 1890 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1891 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1892 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1893 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1897 | int getPageNearestToCenterOfScreen() { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1898 | return getPageNearestToCenterOfScreen(getScrollX()); |
| 1899 | } |
| 1900 | |
| 1901 | private int getPageNearestToCenterOfScreen(int scaledScrollX) { |
| 1902 | int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1903 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1904 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1905 | final int childCount = getChildCount(); |
| 1906 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1907 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1908 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1909 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1910 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1911 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1912 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1913 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1914 | minDistanceFromScreenCenterIndex = i; |
| 1915 | } |
| 1916 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1917 | return minDistanceFromScreenCenterIndex; |
| 1918 | } |
| 1919 | |
| 1920 | protected void snapToDestination() { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1921 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Hyunyoung Song | 74b5af3 | 2016-06-08 16:29:32 -0700 | [diff] [blame] | 1924 | public static class ScrollInterpolator implements Interpolator { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1925 | public ScrollInterpolator() { |
| 1926 | } |
| 1927 | |
| 1928 | public float getInterpolation(float t) { |
| 1929 | t -= 1.0f; |
| 1930 | return t*t*t*t*t + 1; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1935 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1936 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1937 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1938 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1939 | f -= 0.5f; // center the values about 0. |
| 1940 | f *= 0.3f * Math.PI / 2.0f; |
| 1941 | return (float) Math.sin(f); |
| 1942 | } |
| 1943 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1944 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1945 | whichPage = validateNewPage(whichPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1946 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1947 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1948 | final int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1949 | int delta = newX - getUnboundedScrollX(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1950 | int duration = 0; |
| 1951 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1952 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1953 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1954 | // as opposed to an apparent physical response to flinging |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1955 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1956 | return; |
| 1957 | } |
| 1958 | |
| 1959 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1960 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1961 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1962 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1963 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1964 | float distance = halfScreenSize + halfScreenSize * |
| 1965 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1966 | |
| 1967 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1968 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1969 | |
| 1970 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1971 | // 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] | 1972 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1973 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1974 | |
| 1975 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Sunny Goyal | 1740d90 | 2015-05-27 11:14:01 -0700 | [diff] [blame] | 1978 | public void snapToPage(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1979 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 1982 | public void snapToPageImmediately(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1983 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1986 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1987 | snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1990 | protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
| 1991 | snapToPage(whichPage, duration, false, interpolator); |
| 1992 | } |
| 1993 | |
| 1994 | protected void snapToPage(int whichPage, int duration, boolean immediate, |
| 1995 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1996 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1997 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1998 | int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1999 | final int delta = newX - getUnboundedScrollX(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2000 | snapToPage(whichPage, delta, duration, immediate, interpolator); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2004 | snapToPage(whichPage, delta, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2005 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2006 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2007 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate, |
| 2008 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2009 | whichPage = validateNewPage(whichPage); |
| 2010 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2011 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2012 | |
| 2013 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2014 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2015 | if (immediate) { |
| 2016 | duration = 0; |
| 2017 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2018 | duration = Math.abs(delta); |
| 2019 | } |
| 2020 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2021 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2022 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2023 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2024 | |
| 2025 | if (interpolator != null) { |
| 2026 | mScroller.setInterpolator(interpolator); |
| 2027 | } else { |
| 2028 | mScroller.setInterpolator(mDefaultInterpolator); |
| 2029 | } |
| 2030 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 2031 | mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2032 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 2033 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2034 | |
| 2035 | // Trigger a compute() to finish switching pages if necessary |
| 2036 | if (immediate) { |
| 2037 | computeScroll(); |
| 2038 | } |
| 2039 | |
| 2040 | mForceScreenScrolled = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2041 | invalidate(); |
| 2042 | } |
| 2043 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2044 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2045 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2049 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2052 | @Override |
| 2053 | public boolean performLongClick() { |
| 2054 | mCancelTap = true; |
| 2055 | return super.performLongClick(); |
| 2056 | } |
| 2057 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2058 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2059 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2060 | |
| 2061 | SavedState(Parcelable superState) { |
| 2062 | super(superState); |
| 2063 | } |
| 2064 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2065 | @Thunk SavedState(Parcel in) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2066 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2067 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | @Override |
| 2071 | public void writeToParcel(Parcel out, int flags) { |
| 2072 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2073 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 2077 | new Parcelable.Creator<SavedState>() { |
| 2078 | public SavedState createFromParcel(Parcel in) { |
| 2079 | return new SavedState(in); |
| 2080 | } |
| 2081 | |
| 2082 | public SavedState[] newArray(int size) { |
| 2083 | return new SavedState[size]; |
| 2084 | } |
| 2085 | }; |
| 2086 | } |
| 2087 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2088 | // Animate the drag view back to the original position |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2089 | private void animateDragViewToOriginalPosition() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2090 | if (mDragView != null) { |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 2091 | Animator anim = new LauncherViewPropertyAnimator(mDragView) |
| 2092 | .translationX(0) |
| 2093 | .translationY(0) |
| 2094 | .scaleX(1) |
| 2095 | .scaleY(1) |
| 2096 | .setDuration(REORDERING_DROP_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2097 | anim.addListener(new AnimatorListenerAdapter() { |
| 2098 | @Override |
| 2099 | public void onAnimationEnd(Animator animation) { |
| 2100 | onPostReorderingAnimationCompleted(); |
| 2101 | } |
| 2102 | }); |
| 2103 | anim.start(); |
| 2104 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 2107 | public void onStartReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2108 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 2109 | mTouchState = TOUCH_STATE_REORDERING; |
| 2110 | mIsReordering = true; |
| 2111 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2112 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 2113 | // is always drawn on top |
| 2114 | invalidate(); |
| 2115 | } |
| 2116 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2117 | @Thunk void onPostReorderingAnimationCompleted() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2118 | // Trigger the callback when reordering has settled |
| 2119 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 2120 | if (mPostReorderingPreZoomInRunnable != null && |
| 2121 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 2122 | mPostReorderingPreZoomInRunnable.run(); |
| 2123 | mPostReorderingPreZoomInRunnable = null; |
| 2124 | } |
| 2125 | } |
| 2126 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 2127 | public void onEndReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2128 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2131 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2132 | int dragViewIndex = indexOfChild(v); |
| 2133 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 2134 | // Do not allow the first page to be moved around |
| 2135 | if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false; |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2136 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2137 | mTempVisiblePagesRange[0] = 0; |
| 2138 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2139 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2140 | mReorderingStarted = true; |
| 2141 | |
| 2142 | // Check if we are within the reordering range |
| 2143 | if (mTempVisiblePagesRange[0] <= dragViewIndex && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2144 | dragViewIndex <= mTempVisiblePagesRange[1]) { |
| 2145 | // Find the drag view under the pointer |
| 2146 | mDragView = getChildAt(dragViewIndex); |
| 2147 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 2148 | mDragViewBaselineLeft = mDragView.getLeft(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2149 | snapToPage(getPageNearestToCenterOfScreen()); |
| 2150 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2151 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2152 | return true; |
| 2153 | } |
| 2154 | return false; |
| 2155 | } |
| 2156 | |
| 2157 | boolean isReordering(boolean testTouchState) { |
| 2158 | boolean state = mIsReordering; |
| 2159 | if (testTouchState) { |
| 2160 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2161 | } |
| 2162 | return state; |
| 2163 | } |
| 2164 | void endReordering() { |
| 2165 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2166 | // In that case, we don't want to do anything. |
| 2167 | if (!mReorderingStarted) return; |
| 2168 | mReorderingStarted = false; |
| 2169 | |
| 2170 | // If we haven't flung-to-delete the current child, then we just animate the drag view |
| 2171 | // back into position |
| 2172 | final Runnable onCompleteRunnable = new Runnable() { |
| 2173 | @Override |
| 2174 | public void run() { |
| 2175 | onEndReordering(); |
| 2176 | } |
| 2177 | }; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2178 | |
| 2179 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2180 | public void run() { |
| 2181 | onCompleteRunnable.run(); |
| 2182 | enableFreeScroll(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2183 | }; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2184 | }; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2185 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2186 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2187 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2188 | // Snap to the current page |
| 2189 | snapToPage(indexOfChild(mDragView), 0); |
| 2190 | // Animate the drag view back to the front position |
| 2191 | animateDragViewToOriginalPosition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2194 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 2195 | @SuppressWarnings("deprecation") |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 2196 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2197 | @Override |
| 2198 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2199 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2200 | info.setScrollable(getPageCount() > 1); |
| 2201 | if (getCurrentPage() < getPageCount() - 1) { |
| 2202 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2203 | } |
| 2204 | if (getCurrentPage() > 0) { |
| 2205 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2206 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 2207 | info.setClassName(getClass().getName()); |
| 2208 | |
| 2209 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 2210 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 2211 | // accessibility focus. |
| 2212 | info.setLongClickable(false); |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 2213 | if (Utilities.ATLEAST_LOLLIPOP) { |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 2214 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
| 2215 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 2219 | public void sendAccessibilityEvent(int eventType) { |
| 2220 | // Don't let the view send real scroll events. |
| 2221 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2222 | super.sendAccessibilityEvent(eventType); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2227 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2228 | super.onInitializeAccessibilityEvent(event); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 2229 | event.setScrollable(getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2232 | @Override |
| 2233 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2234 | if (super.performAccessibilityAction(action, arguments)) { |
| 2235 | return true; |
| 2236 | } |
| 2237 | switch (action) { |
| 2238 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2239 | if (getCurrentPage() < getPageCount() - 1) { |
| 2240 | scrollRight(); |
| 2241 | return true; |
| 2242 | } |
| 2243 | } break; |
| 2244 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2245 | if (getCurrentPage() > 0) { |
| 2246 | scrollLeft(); |
| 2247 | return true; |
| 2248 | } |
| 2249 | } break; |
| 2250 | } |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
Sunny Goyal | 53fe1f2 | 2016-07-08 08:47:07 -0700 | [diff] [blame] | 2254 | protected String getPageIndicatorDescription() { |
| 2255 | return getCurrentPageDescription(); |
| 2256 | } |
| 2257 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2258 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 2259 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2260 | getNextPage() + 1, getChildCount()); |
| 2261 | } |
| 2262 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 2263 | @Override |
| 2264 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2265 | return true; |
| 2266 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2267 | } |