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