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