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