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 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 650 | // If true, the start edge of the page snaps to the start edge of the viewport. |
| 651 | public boolean matchStartEdge = false; |
| 652 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 653 | /** |
| 654 | * {@inheritDoc} |
| 655 | */ |
| 656 | public LayoutParams(int width, int height) { |
| 657 | super(width, height); |
| 658 | } |
| 659 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 660 | public LayoutParams(Context context, AttributeSet attrs) { |
| 661 | super(context, attrs); |
| 662 | } |
| 663 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 664 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 665 | super(source); |
| 666 | } |
| 667 | } |
| 668 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 669 | @Override |
| 670 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 671 | return new LayoutParams(getContext(), attrs); |
| 672 | } |
| 673 | |
| 674 | @Override |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 675 | protected LayoutParams generateDefaultLayoutParams() { |
| 676 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 677 | } |
| 678 | |
Sunny Goyal | 41b22c0 | 2015-05-14 15:20:48 -0700 | [diff] [blame] | 679 | @Override |
| 680 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 681 | return new LayoutParams(p); |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 686 | return p instanceof LayoutParams; |
| 687 | } |
| 688 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 689 | public void addFullScreenPage(View page) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 690 | LayoutParams lp = generateDefaultLayoutParams(); |
| 691 | lp.isFullScreenPage = true; |
| 692 | super.addView(page, 0, lp); |
| 693 | } |
| 694 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 695 | public int getNormalChildHeight() { |
| 696 | return mNormalChildHeight; |
| 697 | } |
| 698 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 699 | @Override |
| 700 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 701 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 702 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 703 | return; |
| 704 | } |
| 705 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 706 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 707 | // 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] | 708 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 709 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 710 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 711 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 712 | // 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] | 713 | // viewport, we can be at most one and a half screens offset once we scale down |
| 714 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 715 | int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right, |
| 716 | dm.heightPixels + mInsets.top + mInsets.bottom); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 717 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 718 | int parentWidthSize = (int) (2f * maxSize); |
| 719 | int parentHeightSize = (int) (2f * maxSize); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 720 | int scaledWidthSize, scaledHeightSize; |
| 721 | if (mUseMinScale) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 722 | scaledWidthSize = (int) (parentWidthSize / mMinScale); |
| 723 | scaledHeightSize = (int) (parentHeightSize / mMinScale); |
| 724 | } else { |
| 725 | scaledWidthSize = widthSize; |
| 726 | scaledHeightSize = heightSize; |
| 727 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 728 | mViewport.set(0, 0, widthSize, heightSize); |
| 729 | |
| 730 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 731 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 732 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 735 | // Return early if we aren't given a proper dimension |
| 736 | if (widthSize <= 0 || heightSize <= 0) { |
| 737 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 738 | return; |
| 739 | } |
| 740 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 741 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 742 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 743 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 744 | * each page to have the same width. |
| 745 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 746 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 747 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 748 | |
Adam Cohen | 1d3d4f1 | 2014-08-14 19:14:52 -0700 | [diff] [blame] | 749 | int referenceChildWidth = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 750 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 751 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 752 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 753 | if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize); |
| 754 | if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize); |
| 755 | if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding); |
| 756 | if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 757 | final int childCount = getChildCount(); |
| 758 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 759 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 760 | final View child = getPageAt(i); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 761 | if (child.getVisibility() != GONE) { |
| 762 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 763 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 764 | int childWidthMode; |
| 765 | int childHeightMode; |
| 766 | int childWidth; |
| 767 | int childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 768 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 769 | if (!lp.isFullScreenPage) { |
| 770 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 771 | childWidthMode = MeasureSpec.AT_MOST; |
| 772 | } else { |
| 773 | childWidthMode = MeasureSpec.EXACTLY; |
| 774 | } |
| 775 | |
| 776 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 777 | childHeightMode = MeasureSpec.AT_MOST; |
| 778 | } else { |
| 779 | childHeightMode = MeasureSpec.EXACTLY; |
| 780 | } |
| 781 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 782 | childWidth = getViewportWidth() - horizontalPadding |
| 783 | - mInsets.left - mInsets.right; |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 784 | |
| 785 | if (lp.matchStartEdge) { |
| 786 | childWidth += getPaddingStart(); |
| 787 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 788 | childHeight = getViewportHeight() - verticalPadding |
| 789 | - mInsets.top - mInsets.bottom; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 790 | mNormalChildHeight = childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 791 | } else { |
| 792 | childWidthMode = MeasureSpec.EXACTLY; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 793 | childHeightMode = MeasureSpec.EXACTLY; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 794 | |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 795 | childWidth = getViewportWidth(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 796 | childHeight = getViewportHeight(); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 797 | } |
Adam Cohen | 1d3d4f1 | 2014-08-14 19:14:52 -0700 | [diff] [blame] | 798 | if (referenceChildWidth == 0) { |
| 799 | referenceChildWidth = childWidth; |
| 800 | } |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 801 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 802 | final int childWidthMeasureSpec = |
| 803 | MeasureSpec.makeMeasureSpec(childWidth, childWidthMode); |
| 804 | final int childHeightMeasureSpec = |
| 805 | MeasureSpec.makeMeasureSpec(childHeight, childHeightMode); |
| 806 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 807 | } |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 808 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 809 | setMeasuredDimension(scaledWidthSize, scaledHeightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 812 | @SuppressLint("DrawAllocation") |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 813 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 814 | 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] | 815 | if (getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 816 | return; |
| 817 | } |
| 818 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 819 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 820 | final int childCount = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 821 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 822 | int offsetX = getViewportOffsetX(); |
| 823 | int offsetY = getViewportOffsetY(); |
| 824 | |
| 825 | // Update the viewport offsets |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 826 | mViewport.offset(offsetX, offsetY); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 827 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 828 | final int startIndex = mIsRtl ? childCount - 1 : 0; |
| 829 | final int endIndex = mIsRtl ? -1 : childCount; |
| 830 | final int delta = mIsRtl ? -1 : 1; |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 831 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 832 | int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 833 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 834 | LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams(); |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 835 | LayoutParams nextLp; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 836 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 837 | int childLeft = offsetX + |
| 838 | ((lp.isFullScreenPage || (!mIsRtl && lp.matchStartEdge)) ? 0 : getPaddingLeft()); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 839 | if (mPageScrolls == null || childCount != mChildCountOnLastLayout) { |
| 840 | mPageScrolls = new int[childCount]; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 843 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 844 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 845 | if (child.getVisibility() != View.GONE) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 846 | lp = (LayoutParams) child.getLayoutParams(); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 847 | int childTop; |
| 848 | if (lp.isFullScreenPage) { |
| 849 | childTop = offsetY; |
| 850 | } else { |
| 851 | childTop = offsetY + getPaddingTop() + mInsets.top; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 852 | childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 855 | final int childWidth = child.getMeasuredWidth(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 856 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 857 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 858 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 859 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 860 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 861 | |
Sunny Goyal | 7c786f7 | 2016-06-01 14:08:21 -0700 | [diff] [blame] | 862 | int scrollOffsetLeft = (lp.isFullScreenPage || (!mIsRtl & lp.matchStartEdge)) ? |
| 863 | 0 : getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 864 | mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX; |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 865 | |
| 866 | int pageGap = mPageSpacing; |
| 867 | int next = i + delta; |
| 868 | if (next != endIndex) { |
| 869 | nextLp = (LayoutParams) getPageAt(next).getLayoutParams(); |
| 870 | } else { |
| 871 | nextLp = null; |
| 872 | } |
| 873 | |
| 874 | // Prevent full screen pages from showing in the viewport |
| 875 | // when they are not the current page. |
| 876 | if (lp.isFullScreenPage) { |
| 877 | pageGap = getPaddingLeft(); |
| 878 | } else if (nextLp != null && nextLp.isFullScreenPage) { |
| 879 | pageGap = getPaddingRight(); |
| 880 | } |
| 881 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 882 | childLeft += childWidth + pageGap + getChildGap(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 885 | |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 886 | final LayoutTransition transition = getLayoutTransition(); |
| 887 | // If the transition is running defer updating max scroll, as some empty pages could |
| 888 | // still be present, and a max scroll change could cause sudden jumps in scroll. |
| 889 | if (transition != null && transition.isRunning()) { |
| 890 | transition.addTransitionListener(new LayoutTransition.TransitionListener() { |
| 891 | |
| 892 | @Override |
| 893 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 894 | View view, int transitionType) { } |
| 895 | |
| 896 | @Override |
| 897 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 898 | View view, int transitionType) { |
| 899 | // Wait until all transitions are complete. |
| 900 | if (!transition.isRunning()) { |
| 901 | transition.removeTransitionListener(this); |
| 902 | updateMaxScrollX(); |
| 903 | } |
| 904 | } |
| 905 | }); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 906 | } else { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 907 | updateMaxScrollX(); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 910 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) { |
| 911 | updateCurrentPageScroll(); |
| 912 | mFirstLayout = false; |
| 913 | } |
| 914 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 915 | if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) { |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 916 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 917 | setCurrentPage(mRestorePage); |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 918 | mRestorePage = INVALID_RESTORE_PAGE; |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 919 | } else { |
| 920 | setCurrentPage(getNextPage()); |
| 921 | } |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 922 | } |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 923 | mChildCountOnLastLayout = childCount; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 924 | |
| 925 | if (isReordering(true)) { |
| 926 | updateDragViewTranslationDuringDrag(); |
| 927 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Sunny Goyal | a1fbd84 | 2015-05-20 13:40:27 -0700 | [diff] [blame] | 930 | protected int getChildGap() { |
| 931 | return 0; |
| 932 | } |
| 933 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 934 | @Thunk void updateMaxScrollX() { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 935 | mMaxScrollX = computeMaxScrollX(); |
| 936 | } |
| 937 | |
| 938 | protected int computeMaxScrollX() { |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 939 | int childCount = getChildCount(); |
| 940 | if (childCount > 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 941 | final int index = mIsRtl ? 0 : childCount - 1; |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 942 | return getScrollForPage(index); |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 943 | } else { |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 944 | return 0; |
Sunny Goyal | 3e2ff8a | 2015-04-15 16:31:22 -0700 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 948 | public void setPageSpacing(int pageSpacing) { |
| 949 | mPageSpacing = pageSpacing; |
| 950 | requestLayout(); |
| 951 | } |
| 952 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 953 | /** |
| 954 | * Called when the center screen changes during scrolling. |
| 955 | */ |
| 956 | protected void screenScrolled(int screenCenter) { } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 957 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 958 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 959 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 960 | // Update the page indicator, we don't update the page indicator as we |
| 961 | // add/remove pages |
| 962 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 963 | mPageIndicator.addMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 966 | // This ensures that when children are added, they get the correct transforms / alphas |
| 967 | // in accordance with any scroll effects. |
| 968 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 969 | updateFreescrollBounds(); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 970 | invalidate(); |
| 971 | } |
| 972 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 973 | @Override |
| 974 | public void onChildViewRemoved(View parent, View child) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 975 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 976 | updateFreescrollBounds(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 977 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 980 | private void removeMarkerForView() { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 981 | // Update the page indicator, we don't update the page indicator as we |
| 982 | // add/remove pages |
| 983 | if (mPageIndicator != null && !isReordering(false)) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 984 | mPageIndicator.removeMarker(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
| 988 | @Override |
| 989 | public void removeView(View v) { |
| 990 | // XXX: We should find a better way to hook into this before the view |
| 991 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 992 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 993 | super.removeView(v); |
| 994 | } |
| 995 | @Override |
| 996 | public void removeViewInLayout(View v) { |
| 997 | // XXX: We should find a better way to hook into this before the view |
| 998 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 999 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1000 | super.removeViewInLayout(v); |
| 1001 | } |
| 1002 | @Override |
| 1003 | public void removeViewAt(int index) { |
| 1004 | // XXX: We should find a better way to hook into this before the view |
| 1005 | // gets removed form its parent... |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 1006 | removeMarkerForView(); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1007 | super.removeViewAt(index); |
| 1008 | } |
| 1009 | @Override |
| 1010 | public void removeAllViewsInLayout() { |
| 1011 | // Update the page indicator, we don't update the page indicator as we |
| 1012 | // add/remove pages |
| 1013 | if (mPageIndicator != null) { |
Sunny Goyal | c64cfdd | 2016-05-18 14:12:02 -0700 | [diff] [blame] | 1014 | mPageIndicator.setMarkersCount(0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | super.removeAllViewsInLayout(); |
| 1018 | } |
| 1019 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1020 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1021 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 1022 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1023 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1024 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1025 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1028 | protected void getFreeScrollPageRange(int[] range) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1029 | range[0] = 0; |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1030 | range[1] = Math.max(0, getChildCount() - 1); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1033 | protected void getVisiblePages(int[] range) { |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1034 | final int count = getChildCount(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1035 | range[0] = -1; |
| 1036 | range[1] = -1; |
| 1037 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1038 | if (count > 0) { |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 1039 | final int visibleLeft = -getLeft(); |
| 1040 | final int visibleRight = visibleLeft + getViewportWidth(); |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1041 | final Matrix pageShiftMatrix = getPageShiftMatrix(); |
| 1042 | int curScreen = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1043 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1044 | for (int i = 0; i < count; i++) { |
| 1045 | View currPage = getPageAt(i); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1046 | |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 1047 | // Verify if the page bounds are within the visible range. |
| 1048 | sTmpRectF.left = 0; |
| 1049 | sTmpRectF.right = currPage.getMeasuredWidth(); |
| 1050 | currPage.getMatrix().mapRect(sTmpRectF); |
| 1051 | sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0); |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1052 | pageShiftMatrix.mapRect(sTmpRectF); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1053 | |
Sunny Goyal | 8bf6f31 | 2016-01-23 14:40:35 -0800 | [diff] [blame] | 1054 | if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) { |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1055 | if (range[0] == -1) { |
| 1056 | continue; |
| 1057 | } else { |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1061 | curScreen = i; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1062 | if (range[0] < 0) { |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1063 | range[0] = curScreen; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1066 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1067 | range[1] = curScreen; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1068 | } else { |
| 1069 | range[0] = -1; |
| 1070 | range[1] = -1; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Sunny Goyal | ee68816 | 2016-02-12 14:24:21 -0800 | [diff] [blame] | 1074 | protected Matrix getPageShiftMatrix() { |
| 1075 | return getMatrix(); |
| 1076 | } |
| 1077 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1078 | protected boolean shouldDrawChild(View child) { |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 1079 | return child.getVisibility() == VISIBLE; |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1082 | @Override |
| 1083 | protected void dispatchDraw(Canvas canvas) { |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1084 | // Find out which screens are visible; as an optimization we only call draw on them |
| 1085 | final int pageCount = getChildCount(); |
| 1086 | if (pageCount > 0) { |
Adam Cohen | 4de0974 | 2013-12-12 16:16:39 -0800 | [diff] [blame] | 1087 | int halfScreenSize = getViewportWidth() / 2; |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1088 | int screenCenter = getScrollX() + halfScreenSize; |
Adam Cohen | 4de0974 | 2013-12-12 16:16:39 -0800 | [diff] [blame] | 1089 | |
| 1090 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
| 1091 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 1092 | // set it for the next frame |
| 1093 | mForceScreenScrolled = false; |
| 1094 | screenScrolled(screenCenter); |
| 1095 | mLastScreenCenter = screenCenter; |
| 1096 | } |
| 1097 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1098 | getVisiblePages(mTempVisiblePagesRange); |
| 1099 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 1100 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1101 | if (leftScreen != -1 && rightScreen != -1) { |
| 1102 | final long drawingTime = getDrawingTime(); |
| 1103 | // Clip to the bounds |
| 1104 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1105 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 1106 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1107 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1108 | // Draw all the children, leaving the drag view for last |
| 1109 | for (int i = pageCount - 1; i >= 0; i--) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1110 | final View v = getPageAt(i); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1111 | if (v == mDragView) continue; |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 1112 | if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1113 | drawChild(canvas, v, drawingTime); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1114 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1115 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1116 | // Draw the drag view on top (if there is one) |
| 1117 | if (mDragView != null) { |
| 1118 | drawChild(canvas, mDragView, drawingTime); |
| 1119 | } |
| 1120 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1121 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1122 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1123 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | @Override |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1127 | public void draw(Canvas canvas) { |
| 1128 | super.draw(canvas); |
| 1129 | if (getPageCount() > 0) { |
| 1130 | if (!mEdgeGlowLeft.isFinished()) { |
| 1131 | final int restoreCount = canvas.save(); |
| 1132 | Rect display = mViewport; |
| 1133 | canvas.translate(display.left, display.top); |
| 1134 | canvas.rotate(270); |
| 1135 | |
| 1136 | getEdgeVerticalPostion(sTmpIntPoint); |
| 1137 | canvas.translate(display.top - sTmpIntPoint[1], 0); |
| 1138 | mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width()); |
| 1139 | if (mEdgeGlowLeft.draw(canvas)) { |
| 1140 | postInvalidateOnAnimation(); |
| 1141 | } |
| 1142 | canvas.restoreToCount(restoreCount); |
| 1143 | } |
| 1144 | if (!mEdgeGlowRight.isFinished()) { |
| 1145 | final int restoreCount = canvas.save(); |
| 1146 | Rect display = mViewport; |
Sunny Goyal | cb037ee | 2015-07-08 16:41:21 -0700 | [diff] [blame] | 1147 | canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1148 | canvas.rotate(90); |
| 1149 | |
| 1150 | getEdgeVerticalPostion(sTmpIntPoint); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 1151 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 1152 | canvas.translate(sTmpIntPoint[0] - display.top, -display.width()); |
| 1153 | mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width()); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1154 | if (mEdgeGlowRight.draw(canvas)) { |
| 1155 | postInvalidateOnAnimation(); |
| 1156 | } |
| 1157 | canvas.restoreToCount(restoreCount); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * Returns the top and bottom position for the edge effect. |
| 1164 | */ |
| 1165 | protected abstract void getEdgeVerticalPostion(int[] pos); |
| 1166 | |
| 1167 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1168 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1169 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1170 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 1171 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1172 | return true; |
| 1173 | } |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | @Override |
| 1178 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1179 | int focusablePage; |
| 1180 | if (mNextPage != INVALID_PAGE) { |
| 1181 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1182 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1183 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1184 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1185 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1186 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1187 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1188 | } |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | @Override |
| 1193 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 1194 | if (super.dispatchUnhandledMove(focused, direction)) { |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
| 1198 | if (mIsRtl) { |
| 1199 | if (direction == View.FOCUS_LEFT) { |
| 1200 | direction = View.FOCUS_RIGHT; |
| 1201 | } else if (direction == View.FOCUS_RIGHT) { |
| 1202 | direction = View.FOCUS_LEFT; |
| 1203 | } |
| 1204 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1205 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1206 | if (getCurrentPage() > 0) { |
| 1207 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1208 | return true; |
| 1209 | } |
| 1210 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1211 | if (getCurrentPage() < getPageCount() - 1) { |
| 1212 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1213 | return true; |
| 1214 | } |
| 1215 | } |
Sunny Goyal | 0c4e372 | 2015-12-01 13:21:49 -0800 | [diff] [blame] | 1216 | return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | @Override |
| 1220 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1221 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1222 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1223 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1224 | } |
| 1225 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1226 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1227 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1230 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1231 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * 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] | 1238 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1239 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1240 | * This happens when live folders requery, and if they're off page, they |
| 1241 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1242 | */ |
| 1243 | @Override |
| 1244 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1245 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1246 | View v = focused; |
| 1247 | while (true) { |
| 1248 | if (v == current) { |
| 1249 | super.focusableViewAvailable(focused); |
| 1250 | return; |
| 1251 | } |
| 1252 | if (v == this) { |
| 1253 | return; |
| 1254 | } |
| 1255 | ViewParent parent = v.getParent(); |
| 1256 | if (parent instanceof View) { |
| 1257 | v = (View)v.getParent(); |
| 1258 | } else { |
| 1259 | return; |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * {@inheritDoc} |
| 1266 | */ |
| 1267 | @Override |
| 1268 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1269 | if (disallowIntercept) { |
| 1270 | // We need to make sure to cancel our long press if |
| 1271 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1272 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1273 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1274 | } |
| 1275 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1276 | } |
| 1277 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1278 | /** |
| 1279 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1280 | */ |
| 1281 | protected boolean hitsPreviousPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1282 | if (mIsRtl) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1283 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1284 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1285 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1286 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1291 | */ |
| 1292 | protected boolean hitsNextPage(float x, float y) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1293 | if (mIsRtl) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1294 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1295 | } |
| 1296 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1297 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1298 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1299 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1300 | /** Returns whether x and y originated within the buffered viewport */ |
| 1301 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1302 | sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1303 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1304 | return sTmpRect.contains(x, y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1307 | @Override |
| 1308 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 1309 | /* |
| 1310 | * This method JUST determines whether we want to intercept the motion. |
| 1311 | * If we return true, onTouchEvent will be called and we do the actual |
| 1312 | * scrolling there. |
| 1313 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1314 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1315 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1316 | // Skip touch handling if there are no pages to swipe |
| 1317 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1318 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * Shortcut the most recurring case: the user is in the dragging |
| 1321 | * state and he is moving his finger. We want to intercept this |
| 1322 | * motion. |
| 1323 | */ |
| 1324 | final int action = ev.getAction(); |
| 1325 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1326 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1330 | switch (action & MotionEvent.ACTION_MASK) { |
| 1331 | case MotionEvent.ACTION_MOVE: { |
| 1332 | /* |
| 1333 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1334 | * whether the user has moved far enough from his original down touch. |
| 1335 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1336 | if (mActivePointerId != INVALID_POINTER) { |
| 1337 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1338 | } |
| 1339 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1340 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1341 | // i.e. fall through to the next case (don't break) |
| 1342 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1343 | // 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] | 1344 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | case MotionEvent.ACTION_DOWN: { |
| 1348 | final float x = ev.getX(); |
| 1349 | final float y = ev.getY(); |
| 1350 | // Remember location of down touch |
| 1351 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1352 | mDownMotionY = y; |
| 1353 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1354 | mLastMotionX = x; |
| 1355 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1356 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1357 | mParentDownMotionX = p[0]; |
| 1358 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1359 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1360 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1361 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1362 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1363 | /* |
| 1364 | * If being flinged and user touches the screen, initiate drag; |
| 1365 | * otherwise don't. mScroller.isFinished should be false when |
| 1366 | * being flinged. |
| 1367 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1368 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1369 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1370 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1371 | if (finishedScrolling) { |
| 1372 | mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 59b5c79 | 2013-12-04 16:09:07 -0800 | [diff] [blame] | 1373 | if (!mScroller.isFinished() && !mFreeScroll) { |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame] | 1374 | setCurrentPage(getNextPage()); |
| 1375 | pageEndMoving(); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1376 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1377 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1378 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1379 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1380 | } else { |
| 1381 | mTouchState = TOUCH_STATE_REST; |
| 1382 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1383 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1384 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1388 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1389 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1390 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1391 | break; |
| 1392 | |
| 1393 | case MotionEvent.ACTION_POINTER_UP: |
| 1394 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1395 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1396 | break; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * The only time we want to intercept motion events is if we are in the |
| 1401 | * drag mode. |
| 1402 | */ |
| 1403 | return mTouchState != TOUCH_STATE_REST; |
| 1404 | } |
| 1405 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1406 | protected void determineScrollingStart(MotionEvent ev) { |
| 1407 | determineScrollingStart(ev, 1.0f); |
| 1408 | } |
| 1409 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1410 | /* |
| 1411 | * Determines if we should change the touch state to start scrolling after the |
| 1412 | * user moves their touch point too far. |
| 1413 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1414 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1415 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1416 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1417 | if (pointerIndex == -1) return; |
| 1418 | |
| 1419 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1420 | final float x = ev.getX(pointerIndex); |
| 1421 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1422 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1423 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1424 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1425 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1426 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1427 | boolean xMoved = xDiff > touchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1428 | |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1429 | if (xMoved) { |
| 1430 | // Scroll if the user moved far enough along the X axis |
| 1431 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1432 | mTotalMotionX += Math.abs(mLastMotionX - x); |
| 1433 | mLastMotionX = x; |
| 1434 | mLastMotionXRemainder = 0; |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1435 | onScrollInteractionBegin(); |
| 1436 | pageBeginMoving(); |
Tony Wickham | dadb304 | 2016-02-24 11:07:00 -0800 | [diff] [blame] | 1437 | // Stop listening for things like pinches. |
| 1438 | requestDisallowInterceptTouchEvent(true); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | protected void cancelCurrentPageLongPress() { |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1443 | // Try canceling the long press. It could also have been scheduled |
| 1444 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1445 | // everything |
| 1446 | final View currentPage = getPageAt(mCurrentPage); |
| 1447 | if (currentPage != null) { |
| 1448 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1452 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1453 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1454 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1455 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1456 | int count = getChildCount(); |
| 1457 | |
| 1458 | final int totalDistance; |
| 1459 | |
| 1460 | int adjacentPage = page + 1; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1461 | if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1462 | adjacentPage = page - 1; |
| 1463 | } |
| 1464 | |
| 1465 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1466 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1467 | } else { |
| 1468 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1469 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1470 | |
| 1471 | float scrollProgress = delta / (totalDistance * 1.0f); |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1472 | scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS); |
| 1473 | scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1474 | return scrollProgress; |
| 1475 | } |
| 1476 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1477 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1478 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1479 | return 0; |
| 1480 | } else { |
| 1481 | return mPageScrolls[index]; |
| 1482 | } |
| 1483 | } |
| 1484 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1485 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1486 | // position. This method returns the magnitude of this stray at any given time. |
| 1487 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1488 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1489 | return 0; |
| 1490 | } else { |
| 1491 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1492 | |
| 1493 | int scrollOffset = 0; |
| 1494 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1495 | if (!lp.isFullScreenPage) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1496 | scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1499 | int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX(); |
| 1500 | return (int) (child.getX() - baselineX); |
| 1501 | } |
| 1502 | } |
| 1503 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1504 | protected void dampedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1505 | int screenSize = getViewportWidth(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1506 | float f = (amount / screenSize); |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1507 | if (f < 0) { |
| 1508 | mEdgeGlowLeft.onPull(-f); |
| 1509 | } else if (f > 0) { |
| 1510 | mEdgeGlowRight.onPull(f); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1511 | } else { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1512 | return; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1513 | } |
| 1514 | invalidate(); |
| 1515 | } |
| 1516 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1517 | protected void overScroll(float amount) { |
| 1518 | dampedOverScroll(amount); |
| 1519 | } |
| 1520 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1521 | public void enableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1522 | setEnableFreeScroll(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 1525 | public void disableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1526 | setEnableFreeScroll(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1529 | void updateFreescrollBounds() { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1530 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1531 | if (mIsRtl) { |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1532 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1533 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1534 | } else { |
| 1535 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1536 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1537 | } |
| 1538 | } |
| 1539 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1540 | private void setEnableFreeScroll(boolean freeScroll) { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1541 | boolean wasFreeScroll = mFreeScroll; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1542 | mFreeScroll = freeScroll; |
| 1543 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1544 | if (mFreeScroll) { |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1545 | updateFreescrollBounds(); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1546 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1547 | if (getCurrentPage() < mTempVisiblePagesRange[0]) { |
| 1548 | setCurrentPage(mTempVisiblePagesRange[0]); |
| 1549 | } else if (getCurrentPage() > mTempVisiblePagesRange[1]) { |
| 1550 | setCurrentPage(mTempVisiblePagesRange[1]); |
| 1551 | } |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1552 | } else if (wasFreeScroll) { |
| 1553 | snapToPage(getNextPage()); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | setEnableOverscroll(!freeScroll); |
| 1557 | } |
| 1558 | |
Sunny Goyal | 2f0ec85 | 2015-03-26 13:38:27 -0700 | [diff] [blame] | 1559 | protected void setEnableOverscroll(boolean enable) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1560 | mAllowOverScroll = enable; |
| 1561 | } |
| 1562 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 1563 | private int getNearestHoverOverPageIndex() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1564 | if (mDragView != null) { |
| 1565 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1566 | + mDragView.getTranslationX()); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1567 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1568 | int minDistance = Integer.MAX_VALUE; |
| 1569 | int minIndex = indexOfChild(mDragView); |
| 1570 | for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) { |
| 1571 | View page = getPageAt(i); |
| 1572 | int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2); |
| 1573 | int d = Math.abs(dragX - pageX); |
| 1574 | if (d < minDistance) { |
| 1575 | minIndex = i; |
| 1576 | minDistance = d; |
| 1577 | } |
| 1578 | } |
| 1579 | return minIndex; |
| 1580 | } |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1584 | @Override |
| 1585 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1586 | super.onTouchEvent(ev); |
| 1587 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1588 | // Skip touch handling if there are no pages to swipe |
| 1589 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1590 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1591 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1592 | |
| 1593 | final int action = ev.getAction(); |
| 1594 | |
| 1595 | switch (action & MotionEvent.ACTION_MASK) { |
| 1596 | case MotionEvent.ACTION_DOWN: |
| 1597 | /* |
| 1598 | * If being flinged and user touches, stop the fling. isFinished |
| 1599 | * will be false if being flinged. |
| 1600 | */ |
| 1601 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1602 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Remember where the motion event started |
| 1606 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1607 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1608 | mDownScrollX = getScrollX(); |
| 1609 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1610 | mParentDownMotionX = p[0]; |
| 1611 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1612 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1613 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1614 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1615 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1616 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1617 | onScrollInteractionBegin(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1618 | pageBeginMoving(); |
| 1619 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1620 | break; |
| 1621 | |
| 1622 | case MotionEvent.ACTION_MOVE: |
| 1623 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1624 | // Scroll to follow the motion event |
| 1625 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1626 | |
| 1627 | if (pointerIndex == -1) return true; |
| 1628 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1629 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1630 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1631 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1632 | mTotalMotionX += Math.abs(deltaX); |
| 1633 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1634 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1635 | // keep the remainder because we are actually testing if we've moved from the last |
| 1636 | // scrolled position (which is discrete). |
| 1637 | if (Math.abs(deltaX) >= 1.0f) { |
Sunny Goyal | 5a1f53b | 2015-05-27 10:24:24 -0700 | [diff] [blame] | 1638 | scrollBy((int) deltaX, 0); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1639 | mLastMotionX = x; |
| 1640 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1641 | } else { |
| 1642 | awakenScrollBars(); |
| 1643 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1644 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1645 | // Update the last motion position |
| 1646 | mLastMotionX = ev.getX(); |
| 1647 | mLastMotionY = ev.getY(); |
| 1648 | |
| 1649 | // Update the parent down so that our zoom animations take this new movement into |
| 1650 | // account |
| 1651 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1652 | mParentDownMotionX = pt[0]; |
| 1653 | mParentDownMotionY = pt[1]; |
| 1654 | updateDragViewTranslationDuringDrag(); |
| 1655 | |
| 1656 | // Find the closest page to the touch point |
| 1657 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1658 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1659 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1660 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1661 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1662 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1663 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1664 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1665 | // Do not allow any page to be moved to 0th position. |
| 1666 | if (pageUnderPointIndex > 0 && pageUnderPointIndex != indexOfChild(mDragView)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1667 | mTempVisiblePagesRange[0] = 0; |
| 1668 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1669 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1670 | if (mTempVisiblePagesRange[0] <= pageUnderPointIndex && |
| 1671 | pageUnderPointIndex <= mTempVisiblePagesRange[1] && |
| 1672 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1673 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1674 | mSidePageHoverRunnable = new Runnable() { |
| 1675 | @Override |
| 1676 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1677 | // Setup the scroll to the correct page before we swap the views |
| 1678 | snapToPage(pageUnderPointIndex); |
| 1679 | |
| 1680 | // For each of the pages between the paged view and the drag view, |
| 1681 | // animate them from the previous position to the new position in |
| 1682 | // the layout (as a result of the drag view moving in the layout) |
| 1683 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1684 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1685 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1686 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1687 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1688 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1689 | View v = getChildAt(i); |
| 1690 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1691 | // drag view all subsequent views to pageUnderPointIndex will |
| 1692 | // shift down. |
| 1693 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1694 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1695 | |
| 1696 | // Animate the view translation from its old position to its new |
| 1697 | // position |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1698 | ObjectAnimator anim = (ObjectAnimator) v.getTag(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1699 | if (anim != null) { |
| 1700 | anim.cancel(); |
| 1701 | } |
| 1702 | |
| 1703 | v.setTranslationX(oldX - newX); |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 1704 | anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1705 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1706 | anim.start(); |
| 1707 | v.setTag(anim); |
| 1708 | } |
| 1709 | |
| 1710 | removeView(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1711 | addView(mDragView, pageUnderPointIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1712 | mSidePageHoverIndex = -1; |
Winson Chung | 876a619 | 2013-11-06 14:49:50 -0800 | [diff] [blame] | 1713 | if (mPageIndicator != null) { |
| 1714 | mPageIndicator.setActiveMarker(getNextPage()); |
| 1715 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1716 | } |
| 1717 | }; |
| 1718 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1719 | } |
| 1720 | } else { |
| 1721 | removeCallbacks(mSidePageHoverRunnable); |
| 1722 | mSidePageHoverIndex = -1; |
| 1723 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1724 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1725 | determineScrollingStart(ev); |
| 1726 | } |
| 1727 | break; |
| 1728 | |
| 1729 | case MotionEvent.ACTION_UP: |
| 1730 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1731 | final int activePointerId = mActivePointerId; |
| 1732 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1733 | final float x = ev.getX(pointerIndex); |
| 1734 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1735 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1736 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1737 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1738 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1739 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1740 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1741 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1742 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1743 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1744 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1745 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1746 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1747 | if (!mFreeScroll) { |
| 1748 | // In the case that the page is moved far to one direction and then is flung |
| 1749 | // in the opposite direction, we use a threshold to determine whether we should |
| 1750 | // just return to the starting page, or if we should skip one further. |
| 1751 | boolean returnToOriginalPage = false; |
| 1752 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1753 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1754 | returnToOriginalPage = true; |
| 1755 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1756 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1757 | int finalPage; |
| 1758 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1759 | // test for a large move if a fling has been registered. That is, a large |
| 1760 | // 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] | 1761 | boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0; |
| 1762 | boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1763 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1764 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1765 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1766 | snapToPageWithVelocity(finalPage, velocityX); |
| 1767 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1768 | (isFling && isVelocityXLeft)) && |
| 1769 | mCurrentPage < getChildCount() - 1) { |
| 1770 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1771 | snapToPageWithVelocity(finalPage, velocityX); |
| 1772 | } else { |
| 1773 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1774 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1775 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1776 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1777 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | float scaleX = getScaleX(); |
| 1781 | int vX = (int) (-velocityX * scaleX); |
| 1782 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1783 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1784 | mScroller.setInterpolator(mDefaultInterpolator); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1785 | mScroller.fling(initialScrollX, |
| 1786 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1787 | mNextPage = getPageNearestToCenterOfScreen((int) (mScroller.getFinalX() / scaleX)); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1788 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1789 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1790 | onScrollInteractionEnd(); |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 1791 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1792 | // at this point we have not moved beyond the touch slop |
| 1793 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1794 | // we can just page |
| 1795 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1796 | if (nextPage != mCurrentPage) { |
| 1797 | snapToPage(nextPage); |
| 1798 | } else { |
| 1799 | snapToDestination(); |
| 1800 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1801 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1802 | // at this point we have not moved beyond the touch slop |
| 1803 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1804 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1805 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1806 | if (nextPage != mCurrentPage) { |
| 1807 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1808 | } else { |
| 1809 | snapToDestination(); |
| 1810 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1811 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1812 | // Update the last motion position |
| 1813 | mLastMotionX = ev.getX(); |
| 1814 | mLastMotionY = ev.getY(); |
| 1815 | |
| 1816 | // Update the parent down so that our zoom animations take this new movement into |
| 1817 | // account |
| 1818 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1819 | mParentDownMotionX = pt[0]; |
| 1820 | mParentDownMotionY = pt[1]; |
| 1821 | updateDragViewTranslationDuringDrag(); |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1822 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1823 | if (!mCancelTap) { |
| 1824 | onUnhandledTap(ev); |
| 1825 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1826 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1827 | |
| 1828 | // Remove the callback to wait for the side page hover timeout |
| 1829 | removeCallbacks(mSidePageHoverRunnable); |
| 1830 | // End any intermediate reordering states |
| 1831 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1832 | break; |
| 1833 | |
| 1834 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1835 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1836 | snapToDestination(); |
| 1837 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1838 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1839 | break; |
| 1840 | |
| 1841 | case MotionEvent.ACTION_POINTER_UP: |
| 1842 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1843 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1844 | break; |
| 1845 | } |
| 1846 | |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1850 | private void resetTouchState() { |
| 1851 | releaseVelocityTracker(); |
| 1852 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1853 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1854 | mTouchState = TOUCH_STATE_REST; |
| 1855 | mActivePointerId = INVALID_POINTER; |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1856 | mEdgeGlowLeft.onRelease(); |
| 1857 | mEdgeGlowRight.onRelease(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1860 | /** |
| 1861 | * Triggered by scrolling via touch |
| 1862 | */ |
| 1863 | protected void onScrollInteractionBegin() { |
| 1864 | } |
| 1865 | |
| 1866 | protected void onScrollInteractionEnd() { |
| 1867 | } |
| 1868 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1869 | protected void onUnhandledTap(MotionEvent ev) { |
| 1870 | ((Launcher) getContext()).onClick(this); |
| 1871 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1872 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1873 | @Override |
| 1874 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1875 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1876 | switch (event.getAction()) { |
| 1877 | case MotionEvent.ACTION_SCROLL: { |
| 1878 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1879 | final float vscroll; |
| 1880 | final float hscroll; |
| 1881 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1882 | vscroll = 0; |
| 1883 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1884 | } else { |
| 1885 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1886 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1887 | } |
| 1888 | if (hscroll != 0 || vscroll != 0) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 1889 | boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1890 | : (hscroll > 0 || vscroll > 0); |
| 1891 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1892 | scrollRight(); |
| 1893 | } else { |
| 1894 | scrollLeft(); |
| 1895 | } |
| 1896 | return true; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | return super.onGenericMotionEvent(event); |
| 1902 | } |
| 1903 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1904 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1905 | if (mVelocityTracker == null) { |
| 1906 | mVelocityTracker = VelocityTracker.obtain(); |
| 1907 | } |
| 1908 | mVelocityTracker.addMovement(ev); |
| 1909 | } |
| 1910 | |
| 1911 | private void releaseVelocityTracker() { |
| 1912 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1913 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1914 | mVelocityTracker.recycle(); |
| 1915 | mVelocityTracker = null; |
| 1916 | } |
| 1917 | } |
| 1918 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1919 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1920 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1921 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1922 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1923 | if (pointerId == mActivePointerId) { |
| 1924 | // This was our active pointer going up. Choose a new |
| 1925 | // active pointer and adjust accordingly. |
| 1926 | // TODO: Make this decision more intelligent. |
| 1927 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1928 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1929 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1930 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1931 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1932 | if (mVelocityTracker != null) { |
| 1933 | mVelocityTracker.clear(); |
| 1934 | } |
| 1935 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1938 | @Override |
| 1939 | public void requestChildFocus(View child, View focused) { |
| 1940 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1941 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1942 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1943 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1947 | int getPageNearestToCenterOfScreen() { |
Tony Wickham | 8f7ead3 | 2016-04-07 18:46:44 -0700 | [diff] [blame] | 1948 | return getPageNearestToCenterOfScreen(getScrollX()); |
| 1949 | } |
| 1950 | |
| 1951 | private int getPageNearestToCenterOfScreen(int scaledScrollX) { |
| 1952 | int screenCenter = getViewportOffsetX() + scaledScrollX + (getViewportWidth() / 2); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1953 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1954 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1955 | final int childCount = getChildCount(); |
| 1956 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1957 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1958 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1959 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1960 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1961 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1962 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1963 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1964 | minDistanceFromScreenCenterIndex = i; |
| 1965 | } |
| 1966 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1967 | return minDistanceFromScreenCenterIndex; |
| 1968 | } |
| 1969 | |
| 1970 | protected void snapToDestination() { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1971 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1974 | private static class ScrollInterpolator implements Interpolator { |
| 1975 | public ScrollInterpolator() { |
| 1976 | } |
| 1977 | |
| 1978 | public float getInterpolation(float t) { |
| 1979 | t -= 1.0f; |
| 1980 | return t*t*t*t*t + 1; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1985 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1986 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1987 | // of travel has on the overall snap duration. |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 1988 | private float distanceInfluenceForSnapDuration(float f) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1989 | f -= 0.5f; // center the values about 0. |
| 1990 | f *= 0.3f * Math.PI / 2.0f; |
| 1991 | return (float) Math.sin(f); |
| 1992 | } |
| 1993 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1994 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 1995 | whichPage = validateNewPage(whichPage); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1996 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1997 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1998 | final int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 1999 | int delta = newX - getUnboundedScrollX(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2000 | int duration = 0; |
| 2001 | |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2002 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2003 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 2004 | // as opposed to an apparent physical response to flinging |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2005 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2006 | return; |
| 2007 | } |
| 2008 | |
| 2009 | // Here we compute a "distance" that will be used in the computation of the overall |
| 2010 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 2011 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 2012 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 2013 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2014 | float distance = halfScreenSize + halfScreenSize * |
| 2015 | distanceInfluenceForSnapDuration(distanceRatio); |
| 2016 | |
| 2017 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 2018 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2019 | |
| 2020 | // we want the page's snap velocity to approximately match the velocity at which the |
| 2021 | // 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] | 2022 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 2023 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2024 | |
| 2025 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Sunny Goyal | 1740d90 | 2015-05-27 11:14:01 -0700 | [diff] [blame] | 2028 | public void snapToPage(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2029 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 2032 | public void snapToPageImmediately(int whichPage) { |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2033 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2036 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2037 | snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2040 | protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
| 2041 | snapToPage(whichPage, duration, false, interpolator); |
| 2042 | } |
| 2043 | |
| 2044 | protected void snapToPage(int whichPage, int duration, boolean immediate, |
| 2045 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2046 | whichPage = validateNewPage(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2047 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 2048 | int newX = getScrollForPage(whichPage); |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 2049 | final int delta = newX - getUnboundedScrollX(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2050 | snapToPage(whichPage, delta, duration, immediate, interpolator); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2054 | snapToPage(whichPage, delta, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2055 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2056 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2057 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate, |
| 2058 | TimeInterpolator interpolator) { |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2059 | whichPage = validateNewPage(whichPage); |
| 2060 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2061 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2062 | |
| 2063 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2064 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2065 | if (immediate) { |
| 2066 | duration = 0; |
| 2067 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2068 | duration = Math.abs(delta); |
| 2069 | } |
| 2070 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2071 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2072 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2073 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2074 | |
| 2075 | if (interpolator != null) { |
| 2076 | mScroller.setInterpolator(interpolator); |
| 2077 | } else { |
| 2078 | mScroller.setInterpolator(mDefaultInterpolator); |
| 2079 | } |
| 2080 | |
Sunny Goyal | c86df47 | 2016-02-25 09:19:38 -0800 | [diff] [blame] | 2081 | mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2082 | |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 2083 | updatePageIndicator(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2084 | |
| 2085 | // Trigger a compute() to finish switching pages if necessary |
| 2086 | if (immediate) { |
| 2087 | computeScroll(); |
| 2088 | } |
| 2089 | |
| 2090 | mForceScreenScrolled = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2091 | invalidate(); |
| 2092 | } |
| 2093 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2094 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2095 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2099 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2102 | @Override |
| 2103 | public boolean performLongClick() { |
| 2104 | mCancelTap = true; |
| 2105 | return super.performLongClick(); |
| 2106 | } |
| 2107 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2108 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2109 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2110 | |
| 2111 | SavedState(Parcelable superState) { |
| 2112 | super(superState); |
| 2113 | } |
| 2114 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2115 | @Thunk SavedState(Parcel in) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2116 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2117 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | @Override |
| 2121 | public void writeToParcel(Parcel out, int flags) { |
| 2122 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2123 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 2127 | new Parcelable.Creator<SavedState>() { |
| 2128 | public SavedState createFromParcel(Parcel in) { |
| 2129 | return new SavedState(in); |
| 2130 | } |
| 2131 | |
| 2132 | public SavedState[] newArray(int size) { |
| 2133 | return new SavedState[size]; |
| 2134 | } |
| 2135 | }; |
| 2136 | } |
| 2137 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2138 | // Animate the drag view back to the original position |
Sunny Goyal | 4d113a5 | 2015-05-27 10:05:28 -0700 | [diff] [blame] | 2139 | private void animateDragViewToOriginalPosition() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2140 | if (mDragView != null) { |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 2141 | Animator anim = new LauncherViewPropertyAnimator(mDragView) |
| 2142 | .translationX(0) |
| 2143 | .translationY(0) |
| 2144 | .scaleX(1) |
| 2145 | .scaleY(1) |
| 2146 | .setDuration(REORDERING_DROP_REPOSITION_DURATION); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2147 | anim.addListener(new AnimatorListenerAdapter() { |
| 2148 | @Override |
| 2149 | public void onAnimationEnd(Animator animation) { |
| 2150 | onPostReorderingAnimationCompleted(); |
| 2151 | } |
| 2152 | }); |
| 2153 | anim.start(); |
| 2154 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 2157 | public void onStartReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2158 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 2159 | mTouchState = TOUCH_STATE_REORDERING; |
| 2160 | mIsReordering = true; |
| 2161 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2162 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 2163 | // is always drawn on top |
| 2164 | invalidate(); |
| 2165 | } |
| 2166 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2167 | @Thunk void onPostReorderingAnimationCompleted() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2168 | // Trigger the callback when reordering has settled |
| 2169 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 2170 | if (mPostReorderingPreZoomInRunnable != null && |
| 2171 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 2172 | mPostReorderingPreZoomInRunnable.run(); |
| 2173 | mPostReorderingPreZoomInRunnable = null; |
| 2174 | } |
| 2175 | } |
| 2176 | |
Sunny Goyal | 1d08f70 | 2015-05-04 15:50:25 -0700 | [diff] [blame] | 2177 | public void onEndReordering() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2178 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2181 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2182 | int dragViewIndex = indexOfChild(v); |
| 2183 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 2184 | // Do not allow the first page to be moved around |
| 2185 | if (mTouchState != TOUCH_STATE_REST || dragViewIndex <= 0) return false; |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2186 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2187 | mTempVisiblePagesRange[0] = 0; |
| 2188 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2189 | getFreeScrollPageRange(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2190 | mReorderingStarted = true; |
| 2191 | |
| 2192 | // Check if we are within the reordering range |
| 2193 | if (mTempVisiblePagesRange[0] <= dragViewIndex && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2194 | dragViewIndex <= mTempVisiblePagesRange[1]) { |
| 2195 | // Find the drag view under the pointer |
| 2196 | mDragView = getChildAt(dragViewIndex); |
| 2197 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 2198 | mDragViewBaselineLeft = mDragView.getLeft(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2199 | snapToPage(getPageNearestToCenterOfScreen()); |
| 2200 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2201 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2202 | return true; |
| 2203 | } |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
| 2207 | boolean isReordering(boolean testTouchState) { |
| 2208 | boolean state = mIsReordering; |
| 2209 | if (testTouchState) { |
| 2210 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2211 | } |
| 2212 | return state; |
| 2213 | } |
| 2214 | void endReordering() { |
| 2215 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2216 | // In that case, we don't want to do anything. |
| 2217 | if (!mReorderingStarted) return; |
| 2218 | mReorderingStarted = false; |
| 2219 | |
| 2220 | // If we haven't flung-to-delete the current child, then we just animate the drag view |
| 2221 | // back into position |
| 2222 | final Runnable onCompleteRunnable = new Runnable() { |
| 2223 | @Override |
| 2224 | public void run() { |
| 2225 | onEndReordering(); |
| 2226 | } |
| 2227 | }; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2228 | |
| 2229 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2230 | public void run() { |
| 2231 | onCompleteRunnable.run(); |
| 2232 | enableFreeScroll(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2233 | }; |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2234 | }; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2235 | |
Sunny Goyal | 8e2133b | 2015-05-06 13:39:07 -0700 | [diff] [blame] | 2236 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2237 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2238 | // Snap to the current page |
| 2239 | snapToPage(indexOfChild(mDragView), 0); |
| 2240 | // Animate the drag view back to the front position |
| 2241 | animateDragViewToOriginalPosition(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2244 | /* Accessibility */ |
Sunny Goyal | cf25b52 | 2015-07-09 00:01:18 -0700 | [diff] [blame] | 2245 | @SuppressWarnings("deprecation") |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 2246 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2247 | @Override |
| 2248 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2249 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2250 | info.setScrollable(getPageCount() > 1); |
| 2251 | if (getCurrentPage() < getPageCount() - 1) { |
| 2252 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2253 | } |
| 2254 | if (getCurrentPage() > 0) { |
| 2255 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2256 | } |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 2257 | info.setClassName(getClass().getName()); |
| 2258 | |
| 2259 | // Accessibility-wise, PagedView doesn't support long click, so disabling it. |
| 2260 | // Besides disabling the accessibility long-click, this also prevents this view from getting |
| 2261 | // accessibility focus. |
| 2262 | info.setLongClickable(false); |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 2263 | if (Utilities.ATLEAST_LOLLIPOP) { |
Vadim Tryshev | 7af0d44 | 2015-05-15 08:48:11 -0700 | [diff] [blame] | 2264 | info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK); |
| 2265 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 2269 | public void sendAccessibilityEvent(int eventType) { |
| 2270 | // Don't let the view send real scroll events. |
| 2271 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2272 | super.sendAccessibilityEvent(eventType); |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2277 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2278 | super.onInitializeAccessibilityEvent(event); |
Vadim Tryshev | 7066c12 | 2015-05-21 14:06:35 -0700 | [diff] [blame] | 2279 | event.setScrollable(getPageCount() > 1); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2282 | @Override |
| 2283 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2284 | if (super.performAccessibilityAction(action, arguments)) { |
| 2285 | return true; |
| 2286 | } |
| 2287 | switch (action) { |
| 2288 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2289 | if (getCurrentPage() < getPageCount() - 1) { |
| 2290 | scrollRight(); |
| 2291 | return true; |
| 2292 | } |
| 2293 | } break; |
| 2294 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2295 | if (getCurrentPage() > 0) { |
| 2296 | scrollLeft(); |
| 2297 | return true; |
| 2298 | } |
| 2299 | } break; |
| 2300 | } |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2304 | protected String getCurrentPageDescription() { |
Sunny Goyal | f4f89ef | 2015-09-02 15:06:12 -0700 | [diff] [blame] | 2305 | return getContext().getString(R.string.default_scroll_format, |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2306 | getNextPage() + 1, getChildCount()); |
| 2307 | } |
| 2308 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 2309 | @Override |
| 2310 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2311 | return true; |
| 2312 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2313 | } |