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