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