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