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