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