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